Factor out some drawing code.
[kugel-rb.git] / apps / action.c
blob3d4ea9c48a80972328d9e482cc3c6ef4c993a7a3
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"
42 static int last_button = BUTTON_NONE|BUTTON_REL; /* allow the ipod wheel to
43 work on startup */
44 static intptr_t last_data = 0;
45 static int last_action = ACTION_NONE;
46 static bool repeated = false;
47 static bool wait_for_release = false;
49 #ifdef HAVE_TOUCHSCREEN
50 static bool short_press = false;
51 #endif
53 #define REPEAT_WINDOW_TICKS HZ/10
54 static int last_action_tick = 0;
56 /* software keylock stuff */
57 #ifndef HAS_BUTTON_HOLD
58 static bool keys_locked = false;
59 static int unlock_combo = BUTTON_NONE;
60 static bool screen_has_lock = false;
61 #endif /* HAVE_SOFTWARE_KEYLOCK */
64 * do_button_check is the worker function for get_default_action.
65 * returns ACTION_UNKNOWN or the requested return value from the list.
67 static inline int do_button_check(const struct button_mapping *items,
68 int button, int last_button, int *start)
70 int i = 0;
71 int ret = ACTION_UNKNOWN;
73 while (items[i].button_code != BUTTON_NONE)
75 if (items[i].button_code == button)
77 if ((items[i].pre_button_code == BUTTON_NONE)
78 || (items[i].pre_button_code == last_button))
80 ret = items[i].action_code;
81 break;
84 i++;
86 *start = i;
87 return ret;
90 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
92 * button is horizontally inverted to support RTL language if the given language
93 * and context combination require that
95 static int button_flip_horizontally(int context, int button)
97 int newbutton;
99 if (!(lang_is_rtl() && ((context == CONTEXT_STD) ||
100 (context == CONTEXT_TREE) || (context == CONTEXT_LIST) ||
101 (context == CONTEXT_MAINMENU))))
103 return button;
106 newbutton = button &
107 ~(BUTTON_LEFT | BUTTON_RIGHT
108 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD) && \
109 !defined(SIMULATOR)
110 | BUTTON_SCROLL_BACK | BUTTON_SCROLL_FWD
111 #endif
112 #if defined(BUTTON_MINUS) && defined(BUTTON_PLUS) && \
113 !defined(SIMULATOR)
114 | BUTTON_MINUS | BUTTON_PLUS
115 #endif
118 if (button & BUTTON_LEFT)
119 newbutton |= BUTTON_RIGHT;
120 if (button & BUTTON_RIGHT)
121 newbutton |= BUTTON_LEFT;
122 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD) && \
123 !defined(SIMULATOR)
124 if (button & BUTTON_SCROLL_BACK)
125 newbutton |= BUTTON_SCROLL_FWD;
126 if (button & BUTTON_SCROLL_FWD)
127 newbutton |= BUTTON_SCROLL_BACK;
128 #endif
129 #if defined(BUTTON_MINUS) && defined(BUTTON_PLUS) && \
130 !defined(SIMULATOR)
131 if (button & BUTTON_MINUS)
132 newbutton |= BUTTON_PLUS;
133 if (button & BUTTON_PLUS)
134 newbutton |= BUTTON_MINUS;
135 #endif
137 return newbutton;
139 #endif
141 static inline int get_next_context(const struct button_mapping *items, int i)
143 while (items[i].button_code != BUTTON_NONE)
144 i++;
145 return (items[i].action_code == ACTION_NONE ) ?
146 CONTEXT_STD :
147 items[i].action_code;
150 * int get_action_worker(int context, struct button_mapping *user_mappings,
151 int timeout)
152 This function searches the button list for the given context for the just
153 pressed button.
154 If there is a match it returns the value from the list.
155 If there is no match..
156 the last item in the list "points" to the next context in a chain
157 so the "chain" is followed until the button is found.
158 putting ACTION_NONE will get CONTEXT_STD which is always the last list checked.
160 Timeout can be TIMEOUT_NOBLOCK to return immediatly
161 TIMEOUT_BLOCK to wait for a button press
162 Any number >0 to wait that many ticks for a press
164 static int get_action_worker(int context, int timeout,
165 const struct button_mapping* (*get_context_map)(int) )
167 const struct button_mapping *items = NULL;
168 int button;
169 int i=0;
170 int ret = ACTION_UNKNOWN;
171 static int last_context = CONTEXT_STD;
174 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
176 if (timeout == TIMEOUT_NOBLOCK)
177 button = button_get(false);
178 else if (timeout == TIMEOUT_BLOCK)
179 button = button_get(true);
180 else
181 button = button_get_w_tmo(timeout);
183 /* Data from sys events can be pulled with button_get_data */
184 if (button == BUTTON_NONE || button & SYS_EVENT)
185 return button;
186 /* Don't send any buttons through untill we see the release event */
187 if (wait_for_release)
189 if (button&BUTTON_REL)
191 wait_for_release = false;
193 return ACTION_NONE;
197 #if CONFIG_CODEC == SWCODEC
198 /* Produce keyclick */
199 if (global_settings.keyclick && !(button & BUTTON_REL))
200 if (!(button & BUTTON_REPEAT) || global_settings.keyclick_repeats)
201 pcmbuf_beep(4000, KEYCLICK_DURATION, 2500*global_settings.keyclick);
202 #endif
204 if ((context != last_context) && ((last_button & BUTTON_REL) == 0))
206 if (button & BUTTON_REL)
208 last_button = button;
209 last_action = ACTION_NONE;
211 /* eat all buttons until the previous button was |BUTTON_REL
212 (also eat the |BUTTON_REL button) */
213 return ACTION_NONE; /* "safest" return value */
215 last_context = context;
216 #ifndef HAS_BUTTON_HOLD
217 screen_has_lock = ((context & ALLOW_SOFTLOCK) == ALLOW_SOFTLOCK);
218 if (screen_has_lock && keys_locked)
220 if (button == unlock_combo)
222 last_button = BUTTON_NONE;
223 keys_locked = false;
224 splash(HZ/2, str(LANG_KEYLOCK_OFF));
225 return ACTION_REDRAW;
227 else
228 #if (BUTTON_REMOTE != 0)
229 if ((button & BUTTON_REMOTE) == 0)
230 #endif
232 if ((button & BUTTON_REL))
233 splash(HZ/2, str(LANG_KEYLOCK_ON));
234 return ACTION_REDRAW;
237 context &= ~ALLOW_SOFTLOCK;
238 #endif /* HAS_BUTTON_HOLD */
240 #ifdef HAVE_TOUCHSCREEN
241 if (button & BUTTON_TOUCHSCREEN)
243 repeated = false;
244 short_press = false;
245 if (last_button & BUTTON_TOUCHSCREEN)
247 if ((button & BUTTON_REL) &&
248 ((last_button & BUTTON_REPEAT)==0))
250 short_press = true;
252 else if (button & BUTTON_REPEAT)
253 repeated = true;
255 last_button = button;
256 return ACTION_TOUCHSCREEN;
258 #endif
260 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
261 button = button_flip_horizontally(context, button);
262 #endif
264 /* logf("%x,%x",last_button,button); */
265 while (1)
267 /* logf("context = %x",context); */
268 #if (BUTTON_REMOTE != 0)
269 if (button & BUTTON_REMOTE)
270 context |= CONTEXT_REMOTE;
271 #endif
272 if ((context & CONTEXT_PLUGIN) && get_context_map)
273 items = get_context_map(context);
274 else
275 items = get_context_mapping(context);
277 if (items == NULL)
278 break;
280 ret = do_button_check(items,button,last_button,&i);
282 if (ret == ACTION_UNKNOWN)
284 context = get_next_context(items,i);
286 if (context != (int)CONTEXT_STOPSEARCHING)
288 i = 0;
289 continue;
293 /* Action was found or STOPSEARCHING was specified */
294 break;
296 /* DEBUGF("ret = %x\n",ret); */
297 #ifndef HAS_BUTTON_HOLD
298 if (screen_has_lock && (ret == ACTION_STD_KEYLOCK))
300 unlock_combo = button;
301 keys_locked = true;
302 splash(HZ/2, str(LANG_KEYLOCK_ON));
304 button_clear_queue();
305 return ACTION_REDRAW;
307 #endif
308 if ((current_tick - last_action_tick < REPEAT_WINDOW_TICKS)
309 && (ret == last_action))
310 repeated = true;
311 else
312 repeated = false;
314 last_button = button;
315 last_action = ret;
316 last_data = button_get_data();
317 last_action_tick = current_tick;
318 return ret;
321 int get_action(int context, int timeout)
323 return get_action_worker(context,timeout,NULL);
326 int get_custom_action(int context,int timeout,
327 const struct button_mapping* (*get_context_map)(int))
329 return get_action_worker(context,timeout,get_context_map);
332 bool action_userabort(int timeout)
334 int action = get_action_worker(CONTEXT_STD,timeout,NULL);
335 bool ret = (action == ACTION_STD_CANCEL);
336 if(!ret)
338 default_event_handler(action);
340 return ret;
343 #ifndef HAS_BUTTON_HOLD
344 bool is_keys_locked(void)
346 return (screen_has_lock && keys_locked);
348 #endif
350 intptr_t get_action_data(void)
352 return last_data;
355 int get_action_statuscode(int *button)
357 int ret = 0;
358 if (button)
359 *button = last_button;
361 if (last_button & BUTTON_REMOTE)
362 ret |= ACTION_REMOTE;
363 if (repeated)
364 ret |= ACTION_REPEAT;
365 return ret;
368 #ifdef HAVE_TOUCHSCREEN
369 /* return BUTTON_NONE on error
370 * BUTTON_REPEAT if repeated press
371 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
372 * BUTTON_REL if its a short press = release after press
373 * BUTTON_TOUCHSCREEN if press
375 int action_get_touchscreen_press(short *x, short *y)
377 static int last_data = 0;
378 int data;
379 if ((last_button & BUTTON_TOUCHSCREEN) == 0)
380 return BUTTON_NONE;
381 data = button_get_data();
382 if (last_button & BUTTON_REL)
384 *x = (last_data&0xffff0000)>>16;
385 *y = (last_data&0xffff);
387 else
389 *x = (data&0xffff0000)>>16;
390 *y = (data&0xffff);
392 last_data = data;
393 if (repeated)
394 return BUTTON_REPEAT;
395 if (short_press)
396 return BUTTON_REL;
397 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
398 if (last_button & BUTTON_REL)
399 return BUTTON_REPEAT|BUTTON_REL;
400 return BUTTON_TOUCHSCREEN;
403 int action_get_touchscreen_press_in_vp(short *x1, short *y1, struct viewport *vp)
405 short x, y;
406 int ret;
408 ret = action_get_touchscreen_press(&x, &y);
410 if (ret != BUTTON_NONE && viewport_point_within_vp(vp, x, y))
412 *x1 = x - vp->x;
413 *y1 = y - vp->y;
414 return ret;
416 if (ret & BUTTON_TOUCHSCREEN)
417 return ACTION_UNKNOWN;
418 return BUTTON_NONE;
420 #endif
422 /* Don't let get_action*() return any ACTION_* values untill the current buttons
423 * have ben release. SYS_* and BUTTON_NONE will go through.
424 * Any actions relying on _RELEASE won't get seen
426 void action_wait_for_release(void)
428 wait_for_release = true;