Bump version numbers for 3.13
[maemo-rb.git] / apps / action.c
blob2492a7d08ef5ab2da1612628c412d2bb582685de
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"
36 #include "misc.h"
37 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
38 #include "language.h"
39 #endif
40 #include "viewport.h"
41 #ifdef HAVE_TOUCHSCREEN
42 #include "statusbar-skinned.h"
43 #endif
45 static int last_button = BUTTON_NONE|BUTTON_REL; /* allow the ipod wheel to
46 work on startup */
47 static intptr_t last_data = 0;
48 static int last_action = ACTION_NONE;
49 static bool repeated = false;
50 static bool wait_for_release = false;
52 #ifdef HAVE_TOUCHSCREEN
53 static bool short_press = false;
54 #endif
56 #define REPEAT_WINDOW_TICKS HZ/4
57 static int last_action_tick = 0;
59 /* software keylock stuff */
60 #ifndef HAS_BUTTON_HOLD
61 static bool keys_locked = false;
62 static int unlock_combo = BUTTON_NONE;
63 static bool screen_has_lock = false;
64 #endif /* HAVE_SOFTWARE_KEYLOCK */
67 * do_button_check is the worker function for get_default_action.
68 * returns ACTION_UNKNOWN or the requested return value from the list.
70 static inline int do_button_check(const struct button_mapping *items,
71 int button, int last_button, int *start)
73 int i = 0;
74 int ret = ACTION_UNKNOWN;
76 while (items[i].button_code != BUTTON_NONE)
78 if (items[i].button_code == button)
80 if ((items[i].pre_button_code == BUTTON_NONE)
81 || (items[i].pre_button_code == last_button))
83 ret = items[i].action_code;
84 break;
87 i++;
89 *start = i;
90 return ret;
93 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
95 * button is horizontally inverted to support RTL language if the given language
96 * and context combination require that
98 static int button_flip_horizontally(int context, int button)
100 int newbutton;
102 if (!(lang_is_rtl() && ((context == CONTEXT_STD) ||
103 (context == CONTEXT_TREE) || (context == CONTEXT_LIST) ||
104 (context == CONTEXT_MAINMENU))))
106 return button;
109 newbutton = button &
110 ~(BUTTON_LEFT | BUTTON_RIGHT
111 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD) && \
112 !defined(SIMULATOR)
113 | BUTTON_SCROLL_BACK | BUTTON_SCROLL_FWD
114 #endif
115 #if defined(BUTTON_MINUS) && defined(BUTTON_PLUS) && \
116 !defined(SIMULATOR)
117 | BUTTON_MINUS | BUTTON_PLUS
118 #endif
121 if (button & BUTTON_LEFT)
122 newbutton |= BUTTON_RIGHT;
123 if (button & BUTTON_RIGHT)
124 newbutton |= BUTTON_LEFT;
125 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD) && \
126 !defined(SIMULATOR)
127 if (button & BUTTON_SCROLL_BACK)
128 newbutton |= BUTTON_SCROLL_FWD;
129 if (button & BUTTON_SCROLL_FWD)
130 newbutton |= BUTTON_SCROLL_BACK;
131 #endif
132 #if defined(BUTTON_MINUS) && defined(BUTTON_PLUS) && \
133 !defined(SIMULATOR)
134 if (button & BUTTON_MINUS)
135 newbutton |= BUTTON_PLUS;
136 if (button & BUTTON_PLUS)
137 newbutton |= BUTTON_MINUS;
138 #endif
140 return newbutton;
142 #endif
144 static inline int get_next_context(const struct button_mapping *items, int i)
146 while (items[i].button_code != BUTTON_NONE)
147 i++;
148 return (items[i].action_code == ACTION_NONE ) ?
149 CONTEXT_STD :
150 items[i].action_code;
153 #if defined(HAVE_GUI_BOOST) && defined(HAVE_ADJUSTABLE_CPU_FREQ)
155 /* Timeout for gui boost in seconds. */
156 #define GUI_BOOST_TIMEOUT (HZ)
158 /* Helper function which is called to boost / unboost CPU. This function
159 * avoids to increase boost_count with each call of gui_boost(). */
160 static void gui_boost(bool want_to_boost)
162 static bool boosted = false;
164 if (want_to_boost && !boosted)
166 cpu_boost(true);
167 boosted = true;
169 else if (!want_to_boost && boosted)
171 cpu_boost(false);
172 boosted = false;
176 /* gui_unboost_callback() is called GUI_BOOST_TIMEOUT seconds after the
177 * last wheel scrolling event. */
178 static int gui_unboost_callback(struct timeout *tmo)
180 (void)tmo;
181 gui_boost(false);
182 return 0;
184 #endif
187 * int get_action_worker(int context, struct button_mapping *user_mappings,
188 int timeout)
189 This function searches the button list for the given context for the just
190 pressed button.
191 If there is a match it returns the value from the list.
192 If there is no match..
193 the last item in the list "points" to the next context in a chain
194 so the "chain" is followed until the button is found.
195 putting ACTION_NONE will get CONTEXT_STD which is always the last list checked.
197 Timeout can be TIMEOUT_NOBLOCK to return immediatly
198 TIMEOUT_BLOCK to wait for a button press
199 Any number >0 to wait that many ticks for a press
201 static int get_action_worker(int context, int timeout,
202 const struct button_mapping* (*get_context_map)(int) )
204 const struct button_mapping *items = NULL;
205 int button;
206 int i=0;
207 int ret = ACTION_UNKNOWN;
208 static int last_context = CONTEXT_STD;
210 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
212 if (timeout == TIMEOUT_NOBLOCK)
213 button = button_get(false);
214 else if (timeout == TIMEOUT_BLOCK)
215 button = button_get(true);
216 else
217 button = button_get_w_tmo(timeout);
219 #if defined(HAVE_GUI_BOOST) && defined(HAVE_ADJUSTABLE_CPU_FREQ)
220 static struct timeout gui_unboost;
221 /* Boost the CPU in case of wheel scrolling activity in the defined contexts.
222 * Call unboost with a timeout of GUI_BOOST_TIMEOUT. */
223 if ((button&(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD)) &&
224 (context == CONTEXT_STD || context == CONTEXT_LIST ||
225 context == CONTEXT_MAINMENU || context == CONTEXT_TREE))
227 gui_boost(true);
228 timeout_register(&gui_unboost, gui_unboost_callback, GUI_BOOST_TIMEOUT, 0);
230 #endif
232 /* Data from sys events can be pulled with button_get_data
233 * multimedia button presses don't go through the action system */
234 if (button == BUTTON_NONE || button & (SYS_EVENT|BUTTON_MULTIMEDIA))
236 /* no button pressed so no point in waiting for release */
237 if (button == BUTTON_NONE)
238 wait_for_release = false;
239 return button;
242 /* the special redraw button should result in a screen refresh */
243 if (button == BUTTON_REDRAW)
244 return ACTION_REDRAW;
246 /* if action_wait_for_release() was called without a button being pressed
247 * then actually waiting for release would do the wrong thing, i.e.
248 * the next key press is entirely ignored. So, if here comes a normal
249 * button press (neither release nor repeat) the press is a fresh one and
250 * no point in waiting for release
252 * This logic doesn't work for touchscreen which can send normal
253 * button events repeatedly before the first repeat (as in BUTTON_REPEAT).
254 * These cannot be distinguished from the very first touch
255 * but there's nothing we can do about it here */
256 if ((button & (BUTTON_REPEAT|BUTTON_REL)) == 0)
257 wait_for_release = false;
259 /* Don't send any buttons through untill we see the release event */
260 if (wait_for_release)
262 if (button&BUTTON_REL)
264 /* remember the button for the below button eating on context
265 * change */
266 last_button = button;
267 wait_for_release = false;
269 return ACTION_NONE;
272 if ((context != last_context) && ((last_button & BUTTON_REL) == 0)
273 #ifdef HAVE_SCROLLWHEEL
274 /* Scrollwheel doesn't generate release events */
275 && !(last_button & (BUTTON_SCROLL_BACK | BUTTON_SCROLL_FWD))
276 #endif
279 if (button & BUTTON_REL)
281 last_button = button;
282 last_action = ACTION_NONE;
284 /* eat all buttons until the previous button was |BUTTON_REL
285 (also eat the |BUTTON_REL button) */
286 return ACTION_NONE; /* "safest" return value */
288 last_context = context;
289 #ifndef HAS_BUTTON_HOLD
290 screen_has_lock = ((context & ALLOW_SOFTLOCK) == ALLOW_SOFTLOCK);
291 if (is_keys_locked())
293 if (button == unlock_combo)
295 last_button = BUTTON_NONE;
296 keys_locked = false;
297 splash(HZ/2, str(LANG_KEYLOCK_OFF));
298 return ACTION_REDRAW;
300 else
301 #if (BUTTON_REMOTE != 0)
302 if ((button & BUTTON_REMOTE) == 0)
303 #endif
305 if ((button & BUTTON_REL))
306 splash(HZ/2, str(LANG_KEYLOCK_ON));
307 return ACTION_REDRAW;
310 context &= ~ALLOW_SOFTLOCK;
311 #endif /* HAS_BUTTON_HOLD */
313 #ifdef HAVE_TOUCHSCREEN
314 if (button & BUTTON_TOUCHSCREEN)
316 repeated = false;
317 short_press = false;
318 if (last_button & BUTTON_TOUCHSCREEN)
320 if ((button & BUTTON_REL) &&
321 ((last_button & BUTTON_REPEAT)==0))
323 short_press = true;
325 else if (button & BUTTON_REPEAT)
326 repeated = true;
328 last_button = button;
329 return ACTION_TOUCHSCREEN;
331 #endif
333 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
334 button = button_flip_horizontally(context, button);
335 #endif
337 /* logf("%x,%x",last_button,button); */
338 while (1)
340 /* logf("context = %x",context); */
341 #if (BUTTON_REMOTE != 0)
342 if (button & BUTTON_REMOTE)
343 context |= CONTEXT_REMOTE;
344 #endif
345 if ((context & CONTEXT_PLUGIN) && get_context_map)
346 items = get_context_map(context);
347 else
348 items = get_context_mapping(context);
350 if (items == NULL)
351 break;
353 ret = do_button_check(items,button,last_button,&i);
355 if (ret == ACTION_UNKNOWN)
357 context = get_next_context(items,i);
359 if (context != (int)CONTEXT_STOPSEARCHING)
361 i = 0;
362 continue;
366 /* Action was found or STOPSEARCHING was specified */
367 break;
369 /* DEBUGF("ret = %x\n",ret); */
370 #ifndef HAS_BUTTON_HOLD
371 if (screen_has_lock && (ret == ACTION_STD_KEYLOCK))
373 unlock_combo = button;
374 keys_locked = true;
375 splash(HZ/2, str(LANG_KEYLOCK_ON));
377 button_clear_queue();
378 return ACTION_REDRAW;
380 #endif
381 if ((current_tick - last_action_tick < REPEAT_WINDOW_TICKS)
382 && (ret == last_action))
383 repeated = true;
384 else
385 repeated = false;
387 last_button = button;
388 last_action = ret;
389 last_data = button_get_data();
390 last_action_tick = current_tick;
392 #if CONFIG_CODEC == SWCODEC
393 /* Produce keyclick */
394 keyclick_click(false, ret);
395 #endif
397 return ret;
400 int get_action(int context, int timeout)
402 int button = get_action_worker(context,timeout,NULL);
403 #ifdef HAVE_TOUCHSCREEN
404 if (button == ACTION_TOUCHSCREEN)
405 button = sb_touch_to_button(context);
406 #endif
407 return button;
410 int get_custom_action(int context,int timeout,
411 const struct button_mapping* (*get_context_map)(int))
413 return get_action_worker(context,timeout,get_context_map);
416 bool action_userabort(int timeout)
418 int action = get_action_worker(CONTEXT_STD,timeout,NULL);
419 bool ret = (action == ACTION_STD_CANCEL);
420 if(!ret)
422 default_event_handler(action);
424 return ret;
427 #ifndef HAS_BUTTON_HOLD
428 bool is_keys_locked(void)
430 return (screen_has_lock && keys_locked);
432 #endif
434 intptr_t get_action_data(void)
436 return last_data;
439 int get_action_statuscode(int *button)
441 int ret = 0;
442 if (button)
443 *button = last_button;
445 if (last_button & BUTTON_REMOTE)
446 ret |= ACTION_REMOTE;
447 if (repeated)
448 ret |= ACTION_REPEAT;
449 return ret;
452 #ifdef HAVE_TOUCHSCREEN
453 /* return BUTTON_NONE on error
454 * BUTTON_REPEAT if repeated press
455 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
456 * BUTTON_REL if it's a short press = release after press
457 * BUTTON_TOUCHSCREEN if press
459 int action_get_touchscreen_press(short *x, short *y)
461 static int last_data = 0;
462 int data;
463 if ((last_button & BUTTON_TOUCHSCREEN) == 0)
464 return BUTTON_NONE;
465 data = button_get_data();
466 if (last_button & BUTTON_REL)
468 *x = (last_data&0xffff0000)>>16;
469 *y = (last_data&0xffff);
471 else
473 *x = (data&0xffff0000)>>16;
474 *y = (data&0xffff);
476 last_data = data;
477 if (repeated)
478 return BUTTON_REPEAT;
479 if (short_press)
480 return BUTTON_REL;
481 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
482 if (last_button & BUTTON_REL)
483 return BUTTON_REPEAT|BUTTON_REL;
484 return BUTTON_TOUCHSCREEN;
487 int action_get_touchscreen_press_in_vp(short *x1, short *y1, struct viewport *vp)
489 short x, y;
490 int ret;
492 ret = action_get_touchscreen_press(&x, &y);
494 if (ret != BUTTON_NONE && viewport_point_within_vp(vp, x, y))
496 *x1 = x - vp->x;
497 *y1 = y - vp->y;
498 return ret;
500 if (ret & BUTTON_TOUCHSCREEN)
501 return ACTION_UNKNOWN;
502 return BUTTON_NONE;
504 #endif
506 /* Don't let get_action*() return any ACTION_* values until the current buttons
507 * have been released. SYS_* and BUTTON_NONE will go through.
508 * Any actions relying on _RELEASE won't get seen.
510 * Note this doesn't currently work for touchscreen targets if called
511 * when the screen isn't currently touched, because they can send normal
512 * (non-BUTTON_REPEAT) events repeatedly, if the touch coordinates change.
513 * This cannot be distinguished from normal buttons events.
515 void action_wait_for_release(void)
517 wait_for_release = true;