FS#10317 - Sansa AMS 32-bit timers. Configure the timers for 32-bit mode instead...
[kugel-rb.git] / apps / action.c
blobf493296a031d401f6a5c6deb250d0a404862488f
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"
38 static int last_button = BUTTON_NONE|BUTTON_REL; /* allow the ipod wheel to
39 work on startup */
40 static intptr_t last_data = 0;
41 static int last_action = ACTION_NONE;
42 static bool repeated = false;
44 #ifdef HAVE_TOUCHSCREEN
45 static bool short_press = false;
46 #endif
48 #define REPEAT_WINDOW_TICKS HZ/10
49 static int last_action_tick = 0;
51 /* software keylock stuff */
52 #ifndef HAS_BUTTON_HOLD
53 static bool keys_locked = false;
54 static int unlock_combo = BUTTON_NONE;
55 static bool screen_has_lock = false;
56 #endif /* HAVE_SOFTWARE_KEYLOCK */
59 * do_button_check is the worker function for get_default_action.
60 * returns ACTION_UNKNOWN or the requested return value from the list.
62 static inline int do_button_check(const struct button_mapping *items,
63 int button, int last_button, int *start)
65 int i = 0;
66 int ret = ACTION_UNKNOWN;
68 while (items[i].button_code != BUTTON_NONE)
70 if (items[i].button_code == button)
72 if ((items[i].pre_button_code == BUTTON_NONE)
73 || (items[i].pre_button_code == last_button))
75 ret = items[i].action_code;
76 break;
79 i++;
81 *start = i;
82 return ret;
85 static inline int get_next_context(const struct button_mapping *items, int i)
87 while (items[i].button_code != BUTTON_NONE)
88 i++;
89 return (items[i].action_code == ACTION_NONE ) ?
90 CONTEXT_STD :
91 items[i].action_code;
94 * int get_action_worker(int context, struct button_mapping *user_mappings,
95 int timeout)
96 This function searches the button list for the given context for the just
97 pressed button.
98 If there is a match it returns the value from the list.
99 If there is no match..
100 the last item in the list "points" to the next context in a chain
101 so the "chain" is followed until the button is found.
102 putting ACTION_NONE will get CONTEXT_STD which is always the last list checked.
104 Timeout can be TIMEOUT_NOBLOCK to return immediatly
105 TIMEOUT_BLOCK to wait for a button press
106 Any number >0 to wait that many ticks for a press
108 static int get_action_worker(int context, int timeout,
109 const struct button_mapping* (*get_context_map)(int) )
111 const struct button_mapping *items = NULL;
112 int button;
113 int i=0;
114 int ret = ACTION_UNKNOWN;
115 static int last_context = CONTEXT_STD;
118 send_event(GUI_EVENT_ACTIONUPDATE, NULL);
120 if (timeout == TIMEOUT_NOBLOCK)
121 button = button_get(false);
122 else if (timeout == TIMEOUT_BLOCK)
123 button = button_get(true);
124 else
125 button = button_get_w_tmo(timeout);
127 /* Data from sys events can be pulled with button_get_data */
128 if (button == BUTTON_NONE || button & SYS_EVENT)
129 return button;
131 #if CONFIG_CODEC == SWCODEC
132 /* Produce keyclick */
133 if (global_settings.keyclick && !(button & BUTTON_REL))
134 if (!(button & BUTTON_REPEAT) || global_settings.keyclick_repeats)
135 pcmbuf_beep(4000, KEYCLICK_DURATION, 2500*global_settings.keyclick);
136 #endif
138 if ((context != last_context) && ((last_button & BUTTON_REL) == 0))
140 if (button & BUTTON_REL)
142 last_button = button;
143 last_action = ACTION_NONE;
145 /* eat all buttons until the previous button was |BUTTON_REL
146 (also eat the |BUTTON_REL button) */
147 return ACTION_NONE; /* "safest" return value */
149 last_context = context;
150 #ifdef HAVE_TOUCHSCREEN
151 if (button & BUTTON_TOUCHSCREEN)
153 repeated = false;
154 short_press = false;
155 if (last_button & BUTTON_TOUCHSCREEN)
157 if ((button & BUTTON_REL) &&
158 ((last_button & BUTTON_REPEAT)==0))
160 short_press = true;
162 else if (button & BUTTON_REPEAT)
163 repeated = true;
165 last_button = button;
166 return ACTION_TOUCHSCREEN;
168 #endif
169 #ifndef HAS_BUTTON_HOLD
170 screen_has_lock = ((context & ALLOW_SOFTLOCK) == ALLOW_SOFTLOCK);
171 if (screen_has_lock && keys_locked)
173 if (button == unlock_combo)
175 last_button = BUTTON_NONE;
176 keys_locked = false;
177 splash(HZ/2, str(LANG_KEYLOCK_OFF));
178 return ACTION_REDRAW;
180 else
181 #if (BUTTON_REMOTE != 0)
182 if ((button & BUTTON_REMOTE) == 0)
183 #endif
185 if ((button & BUTTON_REL))
186 splash(HZ/2, str(LANG_KEYLOCK_ON));
187 return ACTION_REDRAW;
190 context &= ~ALLOW_SOFTLOCK;
191 #endif /* HAS_BUTTON_HOLD */
193 /* logf("%x,%x",last_button,button); */
194 while (1)
196 /* logf("context = %x",context); */
197 #if (BUTTON_REMOTE != 0)
198 if (button & BUTTON_REMOTE)
199 context |= CONTEXT_REMOTE;
200 #endif
201 if ((context & CONTEXT_PLUGIN) && get_context_map)
202 items = get_context_map(context);
203 else
204 items = get_context_mapping(context);
206 if (items == NULL)
207 break;
209 ret = do_button_check(items,button,last_button,&i);
211 if (ret == ACTION_UNKNOWN)
213 context = get_next_context(items,i);
215 if (context != (int)CONTEXT_STOPSEARCHING)
217 i = 0;
218 continue;
222 /* Action was found or STOPSEARCHING was specified */
223 break;
225 /* DEBUGF("ret = %x\n",ret); */
226 #ifndef HAS_BUTTON_HOLD
227 if (screen_has_lock && (ret == ACTION_STD_KEYLOCK))
229 unlock_combo = button;
230 keys_locked = true;
231 splash(HZ/2, str(LANG_KEYLOCK_ON));
233 button_clear_queue();
234 return ACTION_REDRAW;
236 #endif
237 if ((current_tick - last_action_tick < REPEAT_WINDOW_TICKS)
238 && (ret == last_action))
239 repeated = true;
240 else
241 repeated = false;
243 last_button = button;
244 last_action = ret;
245 last_data = button_get_data();
246 last_action_tick = current_tick;
247 return ret;
250 int get_action(int context, int timeout)
252 return get_action_worker(context,timeout,NULL);
255 int get_custom_action(int context,int timeout,
256 const struct button_mapping* (*get_context_map)(int))
258 return get_action_worker(context,timeout,get_context_map);
261 bool action_userabort(int timeout)
263 int action = get_action_worker(CONTEXT_STD,timeout,NULL);
264 bool ret = (action == ACTION_STD_CANCEL);
265 if(!ret)
267 default_event_handler(action);
269 return ret;
272 #ifndef HAS_BUTTON_HOLD
273 bool is_keys_locked(void)
275 return (screen_has_lock && keys_locked);
277 #endif
279 intptr_t get_action_data(void)
281 return last_data;
284 int get_action_statuscode(int *button)
286 int ret = 0;
287 if (button)
288 *button = last_button;
290 if (last_button & BUTTON_REMOTE)
291 ret |= ACTION_REMOTE;
292 if (repeated)
293 ret |= ACTION_REPEAT;
294 return ret;
297 #ifdef HAVE_TOUCHSCREEN
298 /* return BUTTON_NONE on error
299 * BUTTON_REPEAT if repeated press
300 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
301 * BUTTON_REL if its a short press = release after press
302 * BUTTON_TOUCHSCREEN if press
304 int action_get_touchscreen_press(short *x, short *y)
306 static int last_data = 0;
307 int data;
308 if ((last_button & BUTTON_TOUCHSCREEN) == 0)
309 return BUTTON_NONE;
310 data = button_get_data();
311 if (last_button & BUTTON_REL)
313 *x = (last_data&0xffff0000)>>16;
314 *y = (last_data&0xffff);
316 else
318 *x = (data&0xffff0000)>>16;
319 *y = (data&0xffff);
321 last_data = data;
322 if (repeated)
323 return BUTTON_REPEAT;
324 if (short_press)
325 return BUTTON_REL;
326 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
327 if (last_button & BUTTON_REL)
328 return BUTTON_REPEAT|BUTTON_REL;
329 return BUTTON_TOUCHSCREEN;
331 #endif