rbutil: fix h10_5gbmtp download issue.
[kugel-rb.git] / apps / action.c
blob5ceeeb896f26dca65225f261332f7fd0a695fb3e
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 "button.h"
29 #include "action.h"
30 #include "kernel.h"
31 #include "debug.h"
32 #include "splash.h"
33 #include "settings.h"
34 #include "pcmbuf.h"
36 static int last_button = BUTTON_NONE|BUTTON_REL; /* allow the ipod wheel to
37 work on startup */
38 static intptr_t last_data = 0;
39 static int last_action = ACTION_NONE;
40 static bool repeated = false;
42 #ifdef HAVE_TOUCHSCREEN
43 static bool short_press = false;
44 #endif
46 #define REPEAT_WINDOW_TICKS HZ/10
47 static int last_action_tick = 0;
49 /* software keylock stuff */
50 #ifndef HAS_BUTTON_HOLD
51 static bool keys_locked = false;
52 static int unlock_combo = BUTTON_NONE;
53 static bool screen_has_lock = false;
54 #endif /* HAVE_SOFTWARE_KEYLOCK */
57 * do_button_check is the worker function for get_default_action.
58 * returns ACTION_UNKNOWN or the requested return value from the list.
60 static inline int do_button_check(const struct button_mapping *items,
61 int button, int last_button, int *start)
63 int i = 0;
64 int ret = ACTION_UNKNOWN;
65 if (items == NULL)
66 return 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
109 static int get_action_worker(int context, int timeout,
110 const struct button_mapping* (*get_context_map)(int) )
112 const struct button_mapping *items = NULL;
113 int button;
114 int i=0;
115 int ret = ACTION_UNKNOWN;
116 static int last_context = CONTEXT_STD;
118 if (timeout == TIMEOUT_NOBLOCK)
119 button = button_get(false);
120 else if (timeout == TIMEOUT_BLOCK)
121 button = button_get(true);
122 else
123 button = button_get_w_tmo(timeout);
125 /* Data from sys events can be pulled with button_get_data */
126 if (button == BUTTON_NONE || button & SYS_EVENT)
127 return button;
129 #if CONFIG_CODEC == SWCODEC
130 /* Produce keyclick */
131 if (global_settings.keyclick && !(button & BUTTON_REL))
132 if (!(button & BUTTON_REPEAT) || global_settings.keyclick_repeats)
133 pcmbuf_beep(5000, 2, 2500*global_settings.keyclick);
134 #endif
136 if ((context != last_context) && ((last_button & BUTTON_REL) == 0))
138 if (button & BUTTON_REL)
140 last_button = button;
141 last_action = ACTION_NONE;
143 /* eat all buttons until the previous button was |BUTTON_REL
144 (also eat the |BUTTON_REL button) */
145 return ACTION_NONE; /* "safest" return value */
147 last_context = context;
148 #ifdef HAVE_TOUCHSCREEN
149 if (button & BUTTON_TOUCHSCREEN)
151 repeated = false;
152 short_press = false;
153 if (last_button & BUTTON_TOUCHSCREEN)
155 if ((button & BUTTON_REL) &&
156 ((last_button & BUTTON_REPEAT)==0))
158 short_press = true;
160 else if (button & BUTTON_REPEAT)
161 repeated = true;
163 last_button = button;
164 return ACTION_TOUCHSCREEN;
166 #endif
167 #ifndef HAS_BUTTON_HOLD
168 screen_has_lock = ((context & ALLOW_SOFTLOCK) == ALLOW_SOFTLOCK);
169 if (screen_has_lock && (keys_locked == true))
171 if (button == unlock_combo)
173 last_button = BUTTON_NONE;
174 keys_locked = false;
175 splash(HZ/2, str(LANG_KEYLOCK_OFF));
176 return ACTION_REDRAW;
178 else
179 #if (BUTTON_REMOTE != 0)
180 if ((button & BUTTON_REMOTE) == 0)
181 #endif
183 if ((button & BUTTON_REL))
184 splash(HZ/2, str(LANG_KEYLOCK_ON));
185 return ACTION_REDRAW;
188 context &= ~ALLOW_SOFTLOCK;
189 #endif /* HAS_BUTTON_HOLD */
191 /* logf("%x,%x",last_button,button); */
194 /* logf("context = %x",context); */
195 #if (BUTTON_REMOTE != 0)
196 if (button & BUTTON_REMOTE)
197 context |= CONTEXT_REMOTE;
198 #endif
199 if ((context & CONTEXT_CUSTOM) && get_context_map)
200 items = get_context_map(context);
201 else
202 items = get_context_mapping(context);
204 ret = do_button_check(items,button,last_button,&i);
206 if ((context ==(int)CONTEXT_STOPSEARCHING) ||
207 items == NULL )
208 break;
210 if (ret == ACTION_UNKNOWN )
212 context = get_next_context(items,i);
213 i = 0;
215 else break;
216 } while (1);
217 /* DEBUGF("ret = %x\n",ret); */
218 #ifndef HAS_BUTTON_HOLD
219 if (screen_has_lock && (ret == ACTION_STD_KEYLOCK))
221 unlock_combo = button;
222 keys_locked = true;
223 splash(HZ/2, str(LANG_KEYLOCK_ON));
225 button_clear_queue();
226 return ACTION_REDRAW;
228 #endif
229 if ((current_tick - last_action_tick < REPEAT_WINDOW_TICKS)
230 && (ret == last_action))
231 repeated = true;
232 else
233 repeated = false;
235 last_button = button;
236 last_action = ret;
237 last_data = button_get_data();
238 last_action_tick = current_tick;
239 return ret;
242 int get_action(int context, int timeout)
244 return get_action_worker(context,timeout,NULL);
247 int get_custom_action(int context,int timeout,
248 const struct button_mapping* (*get_context_map)(int))
250 return get_action_worker(context,timeout,get_context_map);
253 bool action_userabort(int timeout)
255 int action = get_action_worker(CONTEXT_STD,timeout,NULL);
256 bool ret = (action == ACTION_STD_CANCEL);
257 return ret;
260 #ifndef HAS_BUTTON_HOLD
261 bool is_keys_locked(void)
263 return (screen_has_lock && (keys_locked == true));
265 #endif
267 intptr_t get_action_data(void)
269 return last_data;
272 int get_action_statuscode(int *button)
274 int ret = 0;
275 if (button)
276 *button = last_button;
278 if (last_button & BUTTON_REMOTE)
279 ret |= ACTION_REMOTE;
280 if (repeated)
281 ret |= ACTION_REPEAT;
282 return ret;
285 #ifdef HAVE_TOUCHSCREEN
286 /* return BUTTON_NONE on error
287 * BUTTON_REPEAT if repeated press
288 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
289 * BUTTON_REL if its a short press = release after press
290 * BUTTON_TOUCHSCREEN if press
292 int action_get_touchscreen_press(short *x, short *y)
294 static int last_data = 0;
295 int data;
296 if ((last_button & BUTTON_TOUCHSCREEN) == 0)
297 return BUTTON_NONE;
298 data = button_get_data();
299 if (last_button & BUTTON_REL)
301 *x = (last_data&0xffff0000)>>16;
302 *y = (last_data&0xffff);
304 else
306 *x = (data&0xffff0000)>>16;
307 *y = (data&0xffff);
309 last_data = data;
310 if (repeated)
311 return BUTTON_REPEAT;
312 if (short_press)
313 return BUTTON_REL;
314 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
315 if (last_button & BUTTON_REL)
316 return BUTTON_REPEAT|BUTTON_REL;
317 return BUTTON_TOUCHSCREEN;
319 #endif