Fix red: Invert buttons in RTL mode
[kugel-rb.git] / apps / action.c
blob055171174b70504efea888a6d924ab4bd9317899
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
41 static int last_button = BUTTON_NONE|BUTTON_REL; /* allow the ipod wheel to
42 work on startup */
43 static intptr_t last_data = 0;
44 static int last_action = ACTION_NONE;
45 static bool repeated = false;
46 static bool wait_for_release = false;
48 #ifdef HAVE_TOUCHSCREEN
49 static bool short_press = false;
50 #endif
52 #define REPEAT_WINDOW_TICKS HZ/10
53 static int last_action_tick = 0;
55 /* software keylock stuff */
56 #ifndef HAS_BUTTON_HOLD
57 static bool keys_locked = false;
58 static int unlock_combo = BUTTON_NONE;
59 static bool screen_has_lock = false;
60 #endif /* HAVE_SOFTWARE_KEYLOCK */
63 * do_button_check is the worker function for get_default_action.
64 * returns ACTION_UNKNOWN or the requested return value from the list.
66 static inline int do_button_check(const struct button_mapping *items,
67 int button, int last_button, int *start)
69 int i = 0;
70 int ret = ACTION_UNKNOWN;
72 while (items[i].button_code != BUTTON_NONE)
74 if (items[i].button_code == button)
76 if ((items[i].pre_button_code == BUTTON_NONE)
77 || (items[i].pre_button_code == last_button))
79 ret = items[i].action_code;
80 break;
83 i++;
85 *start = i;
86 return ret;
89 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
91 * button is horizontally inverted to support RTL language if the given language
92 * and context combination require that
94 static int button_flip_horizontally(int context, int button)
96 int newbutton;
98 if (!(lang_is_rtl() && ((context == CONTEXT_STD) ||
99 (context & CONTEXT_TREE) || (context & CONTEXT_MAINMENU) ||
100 (context & CONTEXT_TREE))))
102 return button;
105 newbutton = button &
106 ~(BUTTON_LEFT | BUTTON_RIGHT
107 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD)
108 | BUTTON_SCROLL_BACK | BUTTON_SCROLL_FWD
109 #endif
112 if (button & BUTTON_LEFT)
113 newbutton |= BUTTON_RIGHT;
114 if (button & BUTTON_RIGHT)
115 newbutton |= BUTTON_LEFT;
116 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD)
117 if (button & BUTTON_SCROLL_BACK)
118 newbutton |= BUTTON_SCROLL_FWD;
119 if (button & BUTTON_SCROLL_FWD)
120 newbutton |= BUTTON_SCROLL_BACK;
121 #endif
123 return newbutton;
125 #endif
127 static inline int get_next_context(const struct button_mapping *items, int i)
129 while (items[i].button_code != BUTTON_NONE)
130 i++;
131 return (items[i].action_code == ACTION_NONE ) ?
132 CONTEXT_STD :
133 items[i].action_code;
136 * int get_action_worker(int context, struct button_mapping *user_mappings,
137 int timeout)
138 This function searches the button list for the given context for the just
139 pressed button.
140 If there is a match it returns the value from the list.
141 If there is no match..
142 the last item in the list "points" to the next context in a chain
143 so the "chain" is followed until the button is found.
144 putting ACTION_NONE will get CONTEXT_STD which is always the last list checked.
146 Timeout can be TIMEOUT_NOBLOCK to return immediatly
147 TIMEOUT_BLOCK to wait for a button press
148 Any number >0 to wait that many ticks for a press
150 static int get_action_worker(int context, int timeout,
151 const struct button_mapping* (*get_context_map)(int) )
153 const struct button_mapping *items = NULL;
154 int button;
155 int i=0;
156 int ret = ACTION_UNKNOWN;
157 static int last_context = CONTEXT_STD;
160 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
162 if (timeout == TIMEOUT_NOBLOCK)
163 button = button_get(false);
164 else if (timeout == TIMEOUT_BLOCK)
165 button = button_get(true);
166 else
167 button = button_get_w_tmo(timeout);
169 /* Data from sys events can be pulled with button_get_data */
170 if (button == BUTTON_NONE || button & SYS_EVENT)
171 return button;
172 /* Don't send any buttons through untill we see the release event */
173 if (wait_for_release)
175 if (button&BUTTON_REL)
177 wait_for_release = false;
179 return ACTION_NONE;
183 #if CONFIG_CODEC == SWCODEC
184 /* Produce keyclick */
185 if (global_settings.keyclick && !(button & BUTTON_REL))
186 if (!(button & BUTTON_REPEAT) || global_settings.keyclick_repeats)
187 pcmbuf_beep(4000, KEYCLICK_DURATION, 2500*global_settings.keyclick);
188 #endif
190 if ((context != last_context) && ((last_button & BUTTON_REL) == 0))
192 if (button & BUTTON_REL)
194 last_button = button;
195 last_action = ACTION_NONE;
197 /* eat all buttons until the previous button was |BUTTON_REL
198 (also eat the |BUTTON_REL button) */
199 return ACTION_NONE; /* "safest" return value */
201 last_context = context;
202 #ifdef HAVE_TOUCHSCREEN
203 if (button & BUTTON_TOUCHSCREEN)
205 repeated = false;
206 short_press = false;
207 if (last_button & BUTTON_TOUCHSCREEN)
209 if ((button & BUTTON_REL) &&
210 ((last_button & BUTTON_REPEAT)==0))
212 short_press = true;
214 else if (button & BUTTON_REPEAT)
215 repeated = true;
217 last_button = button;
218 return ACTION_TOUCHSCREEN;
220 #endif
221 #ifndef HAS_BUTTON_HOLD
222 screen_has_lock = ((context & ALLOW_SOFTLOCK) == ALLOW_SOFTLOCK);
223 if (screen_has_lock && keys_locked)
225 if (button == unlock_combo)
227 last_button = BUTTON_NONE;
228 keys_locked = false;
229 splash(HZ/2, str(LANG_KEYLOCK_OFF));
230 return ACTION_REDRAW;
232 else
233 #if (BUTTON_REMOTE != 0)
234 if ((button & BUTTON_REMOTE) == 0)
235 #endif
237 if ((button & BUTTON_REL))
238 splash(HZ/2, str(LANG_KEYLOCK_ON));
239 return ACTION_REDRAW;
242 context &= ~ALLOW_SOFTLOCK;
243 #endif /* HAS_BUTTON_HOLD */
245 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
246 button = button_flip_horizontally(context, button);
247 #endif
249 /* logf("%x,%x",last_button,button); */
250 while (1)
252 /* logf("context = %x",context); */
253 #if (BUTTON_REMOTE != 0)
254 if (button & BUTTON_REMOTE)
255 context |= CONTEXT_REMOTE;
256 #endif
257 if ((context & CONTEXT_PLUGIN) && get_context_map)
258 items = get_context_map(context);
259 else
260 items = get_context_mapping(context);
262 if (items == NULL)
263 break;
265 ret = do_button_check(items,button,last_button,&i);
267 if (ret == ACTION_UNKNOWN)
269 context = get_next_context(items,i);
271 if (context != (int)CONTEXT_STOPSEARCHING)
273 i = 0;
274 continue;
278 /* Action was found or STOPSEARCHING was specified */
279 break;
281 /* DEBUGF("ret = %x\n",ret); */
282 #ifndef HAS_BUTTON_HOLD
283 if (screen_has_lock && (ret == ACTION_STD_KEYLOCK))
285 unlock_combo = button;
286 keys_locked = true;
287 splash(HZ/2, str(LANG_KEYLOCK_ON));
289 button_clear_queue();
290 return ACTION_REDRAW;
292 #endif
293 if ((current_tick - last_action_tick < REPEAT_WINDOW_TICKS)
294 && (ret == last_action))
295 repeated = true;
296 else
297 repeated = false;
299 last_button = button;
300 last_action = ret;
301 last_data = button_get_data();
302 last_action_tick = current_tick;
303 return ret;
306 int get_action(int context, int timeout)
308 return get_action_worker(context,timeout,NULL);
311 int get_custom_action(int context,int timeout,
312 const struct button_mapping* (*get_context_map)(int))
314 return get_action_worker(context,timeout,get_context_map);
317 bool action_userabort(int timeout)
319 int action = get_action_worker(CONTEXT_STD,timeout,NULL);
320 bool ret = (action == ACTION_STD_CANCEL);
321 if(!ret)
323 default_event_handler(action);
325 return ret;
328 #ifndef HAS_BUTTON_HOLD
329 bool is_keys_locked(void)
331 return (screen_has_lock && keys_locked);
333 #endif
335 intptr_t get_action_data(void)
337 return last_data;
340 int get_action_statuscode(int *button)
342 int ret = 0;
343 if (button)
344 *button = last_button;
346 if (last_button & BUTTON_REMOTE)
347 ret |= ACTION_REMOTE;
348 if (repeated)
349 ret |= ACTION_REPEAT;
350 return ret;
353 #ifdef HAVE_TOUCHSCREEN
354 /* return BUTTON_NONE on error
355 * BUTTON_REPEAT if repeated press
356 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
357 * BUTTON_REL if its a short press = release after press
358 * BUTTON_TOUCHSCREEN if press
360 int action_get_touchscreen_press(short *x, short *y)
362 static int last_data = 0;
363 int data;
364 if ((last_button & BUTTON_TOUCHSCREEN) == 0)
365 return BUTTON_NONE;
366 data = button_get_data();
367 if (last_button & BUTTON_REL)
369 *x = (last_data&0xffff0000)>>16;
370 *y = (last_data&0xffff);
372 else
374 *x = (data&0xffff0000)>>16;
375 *y = (data&0xffff);
377 last_data = data;
378 if (repeated)
379 return BUTTON_REPEAT;
380 if (short_press)
381 return BUTTON_REL;
382 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
383 if (last_button & BUTTON_REL)
384 return BUTTON_REPEAT|BUTTON_REL;
385 return BUTTON_TOUCHSCREEN;
387 #endif
389 /* Don't let get_action*() return any ACTION_* values untill the current buttons
390 * have ben release. SYS_* and BUTTON_NONE will go through.
391 * Any actions relying on _RELEASE won't get seen
393 void action_wait_for_release(void)
395 wait_for_release = true;