keybox: do not leak filehandle when the wrong password is entered.
[maemo-rb.git] / apps / action.c
blob8f427c8d68b5b1792edf28a6466f2d2ff9522a84
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/10
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 * int get_action_worker(int context, struct button_mapping *user_mappings,
154 int timeout)
155 This function searches the button list for the given context for the just
156 pressed button.
157 If there is a match it returns the value from the list.
158 If there is no match..
159 the last item in the list "points" to the next context in a chain
160 so the "chain" is followed until the button is found.
161 putting ACTION_NONE will get CONTEXT_STD which is always the last list checked.
163 Timeout can be TIMEOUT_NOBLOCK to return immediatly
164 TIMEOUT_BLOCK to wait for a button press
165 Any number >0 to wait that many ticks for a press
167 static int get_action_worker(int context, int timeout,
168 const struct button_mapping* (*get_context_map)(int) )
170 const struct button_mapping *items = NULL;
171 int button;
172 int i=0;
173 int ret = ACTION_UNKNOWN;
174 static int last_context = CONTEXT_STD;
177 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
179 if (timeout == TIMEOUT_NOBLOCK)
180 button = button_get(false);
181 else if (timeout == TIMEOUT_BLOCK)
182 button = button_get(true);
183 else
184 button = button_get_w_tmo(timeout);
186 /* Data from sys events can be pulled with button_get_data */
187 if (button == BUTTON_NONE || button & SYS_EVENT)
188 return button;
189 /* Don't send any buttons through untill we see the release event */
190 if (wait_for_release)
192 if (button&BUTTON_REL)
194 wait_for_release = false;
196 return ACTION_NONE;
200 #if CONFIG_CODEC == SWCODEC
201 /* Produce keyclick */
202 if (global_settings.keyclick && !(button & BUTTON_REL))
203 if (!(button & BUTTON_REPEAT) || global_settings.keyclick_repeats)
204 pcmbuf_beep(4000, KEYCLICK_DURATION, 2500*global_settings.keyclick);
205 #endif
207 if ((context != last_context) && ((last_button & BUTTON_REL) == 0))
209 if (button & BUTTON_REL)
211 last_button = button;
212 last_action = ACTION_NONE;
214 /* eat all buttons until the previous button was |BUTTON_REL
215 (also eat the |BUTTON_REL button) */
216 return ACTION_NONE; /* "safest" return value */
218 last_context = context;
219 #ifndef HAS_BUTTON_HOLD
220 screen_has_lock = ((context & ALLOW_SOFTLOCK) == ALLOW_SOFTLOCK);
221 if (screen_has_lock && keys_locked)
223 if (button == unlock_combo)
225 last_button = BUTTON_NONE;
226 keys_locked = false;
227 splash(HZ/2, str(LANG_KEYLOCK_OFF));
228 return ACTION_REDRAW;
230 else
231 #if (BUTTON_REMOTE != 0)
232 if ((button & BUTTON_REMOTE) == 0)
233 #endif
235 if ((button & BUTTON_REL))
236 splash(HZ/2, str(LANG_KEYLOCK_ON));
237 return ACTION_REDRAW;
240 context &= ~ALLOW_SOFTLOCK;
241 #endif /* HAS_BUTTON_HOLD */
243 #ifdef HAVE_TOUCHSCREEN
244 if (button & BUTTON_TOUCHSCREEN)
246 repeated = false;
247 short_press = false;
248 if (last_button & BUTTON_TOUCHSCREEN)
250 if ((button & BUTTON_REL) &&
251 ((last_button & BUTTON_REPEAT)==0))
253 short_press = true;
255 else if (button & BUTTON_REPEAT)
256 repeated = true;
258 last_button = button;
259 return ACTION_TOUCHSCREEN;
261 #endif
263 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
264 button = button_flip_horizontally(context, button);
265 #endif
267 /* logf("%x,%x",last_button,button); */
268 while (1)
270 /* logf("context = %x",context); */
271 #if (BUTTON_REMOTE != 0)
272 if (button & BUTTON_REMOTE)
273 context |= CONTEXT_REMOTE;
274 #endif
275 if ((context & CONTEXT_PLUGIN) && get_context_map)
276 items = get_context_map(context);
277 else
278 items = get_context_mapping(context);
280 if (items == NULL)
281 break;
283 ret = do_button_check(items,button,last_button,&i);
285 if (ret == ACTION_UNKNOWN)
287 context = get_next_context(items,i);
289 if (context != (int)CONTEXT_STOPSEARCHING)
291 i = 0;
292 continue;
296 /* Action was found or STOPSEARCHING was specified */
297 break;
299 /* DEBUGF("ret = %x\n",ret); */
300 #ifndef HAS_BUTTON_HOLD
301 if (screen_has_lock && (ret == ACTION_STD_KEYLOCK))
303 unlock_combo = button;
304 keys_locked = true;
305 splash(HZ/2, str(LANG_KEYLOCK_ON));
307 button_clear_queue();
308 return ACTION_REDRAW;
310 #endif
311 if ((current_tick - last_action_tick < REPEAT_WINDOW_TICKS)
312 && (ret == last_action))
313 repeated = true;
314 else
315 repeated = false;
317 last_button = button;
318 last_action = ret;
319 last_data = button_get_data();
320 last_action_tick = current_tick;
321 return ret;
324 int get_action(int context, int timeout)
326 int button = get_action_worker(context,timeout,NULL);
327 #ifdef HAVE_TOUCHSCREEN
328 if (button == ACTION_TOUCHSCREEN)
329 button = sb_touch_to_button(context);
330 #endif
331 return button;
334 int get_custom_action(int context,int timeout,
335 const struct button_mapping* (*get_context_map)(int))
337 return get_action_worker(context,timeout,get_context_map);
340 bool action_userabort(int timeout)
342 int action = get_action_worker(CONTEXT_STD,timeout,NULL);
343 bool ret = (action == ACTION_STD_CANCEL);
344 if(!ret)
346 default_event_handler(action);
348 return ret;
351 #ifndef HAS_BUTTON_HOLD
352 bool is_keys_locked(void)
354 return (screen_has_lock && keys_locked);
356 #endif
358 intptr_t get_action_data(void)
360 return last_data;
363 int get_action_statuscode(int *button)
365 int ret = 0;
366 if (button)
367 *button = last_button;
369 if (last_button & BUTTON_REMOTE)
370 ret |= ACTION_REMOTE;
371 if (repeated)
372 ret |= ACTION_REPEAT;
373 return ret;
376 #ifdef HAVE_TOUCHSCREEN
377 /* return BUTTON_NONE on error
378 * BUTTON_REPEAT if repeated press
379 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
380 * BUTTON_REL if it's a short press = release after press
381 * BUTTON_TOUCHSCREEN if press
383 int action_get_touchscreen_press(short *x, short *y)
385 static int last_data = 0;
386 int data;
387 if ((last_button & BUTTON_TOUCHSCREEN) == 0)
388 return BUTTON_NONE;
389 data = button_get_data();
390 if (last_button & BUTTON_REL)
392 *x = (last_data&0xffff0000)>>16;
393 *y = (last_data&0xffff);
395 else
397 *x = (data&0xffff0000)>>16;
398 *y = (data&0xffff);
400 last_data = data;
401 if (repeated)
402 return BUTTON_REPEAT;
403 if (short_press)
404 return BUTTON_REL;
405 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
406 if (last_button & BUTTON_REL)
407 return BUTTON_REPEAT|BUTTON_REL;
408 return BUTTON_TOUCHSCREEN;
411 int action_get_touchscreen_press_in_vp(short *x1, short *y1, struct viewport *vp)
413 short x, y;
414 int ret;
416 ret = action_get_touchscreen_press(&x, &y);
418 if (ret != BUTTON_NONE && viewport_point_within_vp(vp, x, y))
420 *x1 = x - vp->x;
421 *y1 = y - vp->y;
422 return ret;
424 if (ret & BUTTON_TOUCHSCREEN)
425 return ACTION_UNKNOWN;
426 return BUTTON_NONE;
428 #endif
430 /* Don't let get_action*() return any ACTION_* values until the current buttons
431 * have been released. SYS_* and BUTTON_NONE will go through.
432 * Any actions relying on _RELEASE won't get seen.
434 void action_wait_for_release(void)
436 wait_for_release = true;