typo--
[kugel-rb.git] / apps / action.c
blob5810003744a6411bf4bcafae70514188b5910cd0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
25 #include "config.h"
26 #include "lang.h"
28 #include "appevents.h"
29 #include "button.h"
30 #include "action.h"
31 #include "kernel.h"
32 #include "debug.h"
33 #include "splash.h"
34 #include "settings.h"
35 #include "pcmbuf.h"
37 static int last_button = BUTTON_NONE|BUTTON_REL; /* allow the ipod wheel to
38 work on startup */
39 static intptr_t last_data = 0;
40 static int last_action = ACTION_NONE;
41 static bool repeated = false;
43 #ifdef HAVE_TOUCHSCREEN
44 static bool short_press = false;
45 #endif
47 #define REPEAT_WINDOW_TICKS HZ/10
48 static int last_action_tick = 0;
50 /* software keylock stuff */
51 #ifndef HAS_BUTTON_HOLD
52 static bool keys_locked = false;
53 static int unlock_combo = BUTTON_NONE;
54 static bool screen_has_lock = false;
55 #endif /* HAVE_SOFTWARE_KEYLOCK */
58 * do_button_check is the worker function for get_default_action.
59 * returns ACTION_UNKNOWN or the requested return value from the list.
61 static inline int do_button_check(const struct button_mapping *items,
62 int button, int last_button, int *start)
64 int i = 0;
65 int ret = ACTION_UNKNOWN;
67 while (items[i].button_code != BUTTON_NONE)
69 if (items[i].button_code == button)
71 if ((items[i].pre_button_code == BUTTON_NONE)
72 || (items[i].pre_button_code == last_button))
74 ret = items[i].action_code;
75 break;
78 i++;
80 *start = i;
81 return ret;
84 static inline int get_next_context(const struct button_mapping *items, int i)
86 while (items[i].button_code != BUTTON_NONE)
87 i++;
88 return (items[i].action_code == ACTION_NONE ) ?
89 CONTEXT_STD :
90 items[i].action_code;
93 * int get_action_worker(int context, struct button_mapping *user_mappings,
94 int timeout)
95 This function searches the button list for the given context for the just
96 pressed button.
97 If there is a match it returns the value from the list.
98 If there is no match..
99 the last item in the list "points" to the next context in a chain
100 so the "chain" is followed until the button is found.
101 putting ACTION_NONE will get CONTEXT_STD which is always the last list checked.
103 Timeout can be TIMEOUT_NOBLOCK to return immediatly
104 TIMEOUT_BLOCK to wait for a button press
105 Any number >0 to wait that many ticks for a press
107 static int get_action_worker(int context, int timeout,
108 const struct button_mapping* (*get_context_map)(int) )
110 const struct button_mapping *items = NULL;
111 int button;
112 int i=0;
113 int ret = ACTION_UNKNOWN;
114 static int last_context = CONTEXT_STD;
117 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
119 if (timeout == TIMEOUT_NOBLOCK)
120 button = button_get(false);
121 else if (timeout == TIMEOUT_BLOCK)
122 button = button_get(true);
123 else
124 button = button_get_w_tmo(timeout);
126 /* Data from sys events can be pulled with button_get_data */
127 if (button == BUTTON_NONE || button & SYS_EVENT)
128 return button;
130 #if CONFIG_CODEC == SWCODEC
131 /* Produce keyclick */
132 if (global_settings.keyclick && !(button & BUTTON_REL))
133 if (!(button & BUTTON_REPEAT) || global_settings.keyclick_repeats)
134 pcmbuf_beep(4000, KEYCLICK_DURATION, 2500*global_settings.keyclick);
135 #endif
137 if ((context != last_context) && ((last_button & BUTTON_REL) == 0))
139 if (button & BUTTON_REL)
141 last_button = button;
142 last_action = ACTION_NONE;
144 /* eat all buttons until the previous button was |BUTTON_REL
145 (also eat the |BUTTON_REL button) */
146 return ACTION_NONE; /* "safest" return value */
148 last_context = context;
149 #ifdef HAVE_TOUCHSCREEN
150 if (button & BUTTON_TOUCHSCREEN)
152 repeated = false;
153 short_press = false;
154 if (last_button & BUTTON_TOUCHSCREEN)
156 if ((button & BUTTON_REL) &&
157 ((last_button & BUTTON_REPEAT)==0))
159 short_press = true;
161 else if (button & BUTTON_REPEAT)
162 repeated = true;
164 last_button = button;
165 return ACTION_TOUCHSCREEN;
167 #endif
168 #ifndef HAS_BUTTON_HOLD
169 screen_has_lock = ((context & ALLOW_SOFTLOCK) == ALLOW_SOFTLOCK);
170 if (screen_has_lock && keys_locked)
172 if (button == unlock_combo)
174 last_button = BUTTON_NONE;
175 keys_locked = false;
176 splash(HZ/2, str(LANG_KEYLOCK_OFF));
177 return ACTION_REDRAW;
179 else
180 #if (BUTTON_REMOTE != 0)
181 if ((button & BUTTON_REMOTE) == 0)
182 #endif
184 if ((button & BUTTON_REL))
185 splash(HZ/2, str(LANG_KEYLOCK_ON));
186 return ACTION_REDRAW;
189 context &= ~ALLOW_SOFTLOCK;
190 #endif /* HAS_BUTTON_HOLD */
192 /* logf("%x,%x",last_button,button); */
193 while (1)
195 /* logf("context = %x",context); */
196 #if (BUTTON_REMOTE != 0)
197 if (button & BUTTON_REMOTE)
198 context |= CONTEXT_REMOTE;
199 #endif
200 if ((context & CONTEXT_PLUGIN) && get_context_map)
201 items = get_context_map(context);
202 else
203 items = get_context_mapping(context);
205 if (items == NULL)
206 break;
208 ret = do_button_check(items,button,last_button,&i);
210 if (ret == ACTION_UNKNOWN)
212 context = get_next_context(items,i);
214 if (context != (int)CONTEXT_STOPSEARCHING)
216 i = 0;
217 continue;
221 /* Action was found or STOPSEARCHING was specified */
222 break;
224 /* DEBUGF("ret = %x\n",ret); */
225 #ifndef HAS_BUTTON_HOLD
226 if (screen_has_lock && (ret == ACTION_STD_KEYLOCK))
228 unlock_combo = button;
229 keys_locked = true;
230 splash(HZ/2, str(LANG_KEYLOCK_ON));
232 button_clear_queue();
233 return ACTION_REDRAW;
235 #endif
236 if ((current_tick - last_action_tick < REPEAT_WINDOW_TICKS)
237 && (ret == last_action))
238 repeated = true;
239 else
240 repeated = false;
242 last_button = button;
243 last_action = ret;
244 last_data = button_get_data();
245 last_action_tick = current_tick;
246 return ret;
249 int get_action(int context, int timeout)
251 return get_action_worker(context,timeout,NULL);
254 int get_custom_action(int context,int timeout,
255 const struct button_mapping* (*get_context_map)(int))
257 return get_action_worker(context,timeout,get_context_map);
260 bool action_userabort(int timeout)
262 int action = get_action_worker(CONTEXT_STD,timeout,NULL);
263 bool ret = (action == ACTION_STD_CANCEL);
264 return ret;
267 #ifndef HAS_BUTTON_HOLD
268 bool is_keys_locked(void)
270 return (screen_has_lock && keys_locked);
272 #endif
274 intptr_t get_action_data(void)
276 return last_data;
279 int get_action_statuscode(int *button)
281 int ret = 0;
282 if (button)
283 *button = last_button;
285 if (last_button & BUTTON_REMOTE)
286 ret |= ACTION_REMOTE;
287 if (repeated)
288 ret |= ACTION_REPEAT;
289 return ret;
292 #ifdef HAVE_TOUCHSCREEN
293 /* return BUTTON_NONE on error
294 * BUTTON_REPEAT if repeated press
295 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
296 * BUTTON_REL if its a short press = release after press
297 * BUTTON_TOUCHSCREEN if press
299 int action_get_touchscreen_press(short *x, short *y)
301 static int last_data = 0;
302 int data;
303 if ((last_button & BUTTON_TOUCHSCREEN) == 0)
304 return BUTTON_NONE;
305 data = button_get_data();
306 if (last_button & BUTTON_REL)
308 *x = (last_data&0xffff0000)>>16;
309 *y = (last_data&0xffff);
311 else
313 *x = (data&0xffff0000)>>16;
314 *y = (data&0xffff);
316 last_data = data;
317 if (repeated)
318 return BUTTON_REPEAT;
319 if (short_press)
320 return BUTTON_REL;
321 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
322 if (last_button & BUTTON_REL)
323 return BUTTON_REPEAT|BUTTON_REL;
324 return BUTTON_TOUCHSCREEN;
326 #endif