Add crypt_firmware plugin for Nano2G - this uses the hardware crypto unit to encrypt...
[kugel-rb.git] / apps / action.c
blobfc10c927e5143bcb8d3c8819f896f0559d8882d8
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_LIST) ||
100 (context == CONTEXT_MAINMENU))))
102 return button;
105 newbutton = button &
106 ~(BUTTON_LEFT | BUTTON_RIGHT
107 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD) && \
108 !defined(SIMULATOR)
109 | BUTTON_SCROLL_BACK | BUTTON_SCROLL_FWD
110 #endif
113 if (button & BUTTON_LEFT)
114 newbutton |= BUTTON_RIGHT;
115 if (button & BUTTON_RIGHT)
116 newbutton |= BUTTON_LEFT;
117 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD) && \
118 !defined(SIMULATOR)
119 if (button & BUTTON_SCROLL_BACK)
120 newbutton |= BUTTON_SCROLL_FWD;
121 if (button & BUTTON_SCROLL_FWD)
122 newbutton |= BUTTON_SCROLL_BACK;
123 #endif
125 return newbutton;
127 #endif
129 static inline int get_next_context(const struct button_mapping *items, int i)
131 while (items[i].button_code != BUTTON_NONE)
132 i++;
133 return (items[i].action_code == ACTION_NONE ) ?
134 CONTEXT_STD :
135 items[i].action_code;
138 * int get_action_worker(int context, struct button_mapping *user_mappings,
139 int timeout)
140 This function searches the button list for the given context for the just
141 pressed button.
142 If there is a match it returns the value from the list.
143 If there is no match..
144 the last item in the list "points" to the next context in a chain
145 so the "chain" is followed until the button is found.
146 putting ACTION_NONE will get CONTEXT_STD which is always the last list checked.
148 Timeout can be TIMEOUT_NOBLOCK to return immediatly
149 TIMEOUT_BLOCK to wait for a button press
150 Any number >0 to wait that many ticks for a press
152 static int get_action_worker(int context, int timeout,
153 const struct button_mapping* (*get_context_map)(int) )
155 const struct button_mapping *items = NULL;
156 int button;
157 int i=0;
158 int ret = ACTION_UNKNOWN;
159 static int last_context = CONTEXT_STD;
162 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
164 if (timeout == TIMEOUT_NOBLOCK)
165 button = button_get(false);
166 else if (timeout == TIMEOUT_BLOCK)
167 button = button_get(true);
168 else
169 button = button_get_w_tmo(timeout);
171 /* Data from sys events can be pulled with button_get_data */
172 if (button == BUTTON_NONE || button & SYS_EVENT)
173 return button;
174 /* Don't send any buttons through untill we see the release event */
175 if (wait_for_release)
177 if (button&BUTTON_REL)
179 wait_for_release = false;
181 return ACTION_NONE;
185 #if CONFIG_CODEC == SWCODEC
186 /* Produce keyclick */
187 if (global_settings.keyclick && !(button & BUTTON_REL))
188 if (!(button & BUTTON_REPEAT) || global_settings.keyclick_repeats)
189 pcmbuf_beep(4000, KEYCLICK_DURATION, 2500*global_settings.keyclick);
190 #endif
192 if ((context != last_context) && ((last_button & BUTTON_REL) == 0))
194 if (button & BUTTON_REL)
196 last_button = button;
197 last_action = ACTION_NONE;
199 /* eat all buttons until the previous button was |BUTTON_REL
200 (also eat the |BUTTON_REL button) */
201 return ACTION_NONE; /* "safest" return value */
203 last_context = context;
204 #ifdef HAVE_TOUCHSCREEN
205 if (button & BUTTON_TOUCHSCREEN)
207 repeated = false;
208 short_press = false;
209 if (last_button & BUTTON_TOUCHSCREEN)
211 if ((button & BUTTON_REL) &&
212 ((last_button & BUTTON_REPEAT)==0))
214 short_press = true;
216 else if (button & BUTTON_REPEAT)
217 repeated = true;
219 last_button = button;
220 return ACTION_TOUCHSCREEN;
222 #endif
223 #ifndef HAS_BUTTON_HOLD
224 screen_has_lock = ((context & ALLOW_SOFTLOCK) == ALLOW_SOFTLOCK);
225 if (screen_has_lock && keys_locked)
227 if (button == unlock_combo)
229 last_button = BUTTON_NONE;
230 keys_locked = false;
231 splash(HZ/2, str(LANG_KEYLOCK_OFF));
232 return ACTION_REDRAW;
234 else
235 #if (BUTTON_REMOTE != 0)
236 if ((button & BUTTON_REMOTE) == 0)
237 #endif
239 if ((button & BUTTON_REL))
240 splash(HZ/2, str(LANG_KEYLOCK_ON));
241 return ACTION_REDRAW;
244 context &= ~ALLOW_SOFTLOCK;
245 #endif /* HAS_BUTTON_HOLD */
247 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
248 button = button_flip_horizontally(context, button);
249 #endif
251 /* logf("%x,%x",last_button,button); */
252 while (1)
254 /* logf("context = %x",context); */
255 #if (BUTTON_REMOTE != 0)
256 if (button & BUTTON_REMOTE)
257 context |= CONTEXT_REMOTE;
258 #endif
259 if ((context & CONTEXT_PLUGIN) && get_context_map)
260 items = get_context_map(context);
261 else
262 items = get_context_mapping(context);
264 if (items == NULL)
265 break;
267 ret = do_button_check(items,button,last_button,&i);
269 if (ret == ACTION_UNKNOWN)
271 context = get_next_context(items,i);
273 if (context != (int)CONTEXT_STOPSEARCHING)
275 i = 0;
276 continue;
280 /* Action was found or STOPSEARCHING was specified */
281 break;
283 /* DEBUGF("ret = %x\n",ret); */
284 #ifndef HAS_BUTTON_HOLD
285 if (screen_has_lock && (ret == ACTION_STD_KEYLOCK))
287 unlock_combo = button;
288 keys_locked = true;
289 splash(HZ/2, str(LANG_KEYLOCK_ON));
291 button_clear_queue();
292 return ACTION_REDRAW;
294 #endif
295 if ((current_tick - last_action_tick < REPEAT_WINDOW_TICKS)
296 && (ret == last_action))
297 repeated = true;
298 else
299 repeated = false;
301 last_button = button;
302 last_action = ret;
303 last_data = button_get_data();
304 last_action_tick = current_tick;
305 return ret;
308 int get_action(int context, int timeout)
310 return get_action_worker(context,timeout,NULL);
313 int get_custom_action(int context,int timeout,
314 const struct button_mapping* (*get_context_map)(int))
316 return get_action_worker(context,timeout,get_context_map);
319 bool action_userabort(int timeout)
321 int action = get_action_worker(CONTEXT_STD,timeout,NULL);
322 bool ret = (action == ACTION_STD_CANCEL);
323 if(!ret)
325 default_event_handler(action);
327 return ret;
330 #ifndef HAS_BUTTON_HOLD
331 bool is_keys_locked(void)
333 return (screen_has_lock && keys_locked);
335 #endif
337 intptr_t get_action_data(void)
339 return last_data;
342 int get_action_statuscode(int *button)
344 int ret = 0;
345 if (button)
346 *button = last_button;
348 if (last_button & BUTTON_REMOTE)
349 ret |= ACTION_REMOTE;
350 if (repeated)
351 ret |= ACTION_REPEAT;
352 return ret;
355 #ifdef HAVE_TOUCHSCREEN
356 /* return BUTTON_NONE on error
357 * BUTTON_REPEAT if repeated press
358 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
359 * BUTTON_REL if its a short press = release after press
360 * BUTTON_TOUCHSCREEN if press
362 int action_get_touchscreen_press(short *x, short *y)
364 static int last_data = 0;
365 int data;
366 if ((last_button & BUTTON_TOUCHSCREEN) == 0)
367 return BUTTON_NONE;
368 data = button_get_data();
369 if (last_button & BUTTON_REL)
371 *x = (last_data&0xffff0000)>>16;
372 *y = (last_data&0xffff);
374 else
376 *x = (data&0xffff0000)>>16;
377 *y = (data&0xffff);
379 last_data = data;
380 if (repeated)
381 return BUTTON_REPEAT;
382 if (short_press)
383 return BUTTON_REL;
384 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
385 if (last_button & BUTTON_REL)
386 return BUTTON_REPEAT|BUTTON_REL;
387 return BUTTON_TOUCHSCREEN;
389 #endif
391 /* Don't let get_action*() return any ACTION_* values untill the current buttons
392 * have ben release. SYS_* and BUTTON_NONE will go through.
393 * Any actions relying on _RELEASE won't get seen
395 void action_wait_for_release(void)
397 wait_for_release = true;