1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
28 #include "appevents.h"
37 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
41 static int last_button
= BUTTON_NONE
|BUTTON_REL
; /* allow the ipod wheel to
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;
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
)
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
;
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
)
98 if (!(lang_is_rtl() && ((context
== CONTEXT_STD
) ||
99 (context
& CONTEXT_TREE
) || (context
& CONTEXT_MAINMENU
) ||
100 (context
& CONTEXT_TREE
))))
106 ~(BUTTON_LEFT
| BUTTON_RIGHT
107 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD)
108 | BUTTON_SCROLL_BACK
| BUTTON_SCROLL_FWD
112 if (button
& BUTTON_LEFT
)
113 newbutton
|= BUTTON_RIGHT
;
114 if (button
& BUTTON_RIGHT
)
115 newbutton
|= BUTTON_LEFT
;
116 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD)
117 if (button
& BUTTON_SCROLL_BACK
)
118 newbutton
|= BUTTON_SCROLL_FWD
;
119 if (button
& BUTTON_SCROLL_FWD
)
120 newbutton
|= BUTTON_SCROLL_BACK
;
127 static inline int get_next_context(const struct button_mapping
*items
, int i
)
129 while (items
[i
].button_code
!= BUTTON_NONE
)
131 return (items
[i
].action_code
== ACTION_NONE
) ?
133 items
[i
].action_code
;
136 * int get_action_worker(int context, struct button_mapping *user_mappings,
138 This function searches the button list for the given context for the just
140 If there is a match it returns the value from the list.
141 If there is no match..
142 the last item in the list "points" to the next context in a chain
143 so the "chain" is followed until the button is found.
144 putting ACTION_NONE will get CONTEXT_STD which is always the last list checked.
146 Timeout can be TIMEOUT_NOBLOCK to return immediatly
147 TIMEOUT_BLOCK to wait for a button press
148 Any number >0 to wait that many ticks for a press
150 static int get_action_worker(int context
, int timeout
,
151 const struct button_mapping
* (*get_context_map
)(int) )
153 const struct button_mapping
*items
= NULL
;
156 int ret
= ACTION_UNKNOWN
;
157 static int last_context
= CONTEXT_STD
;
160 send_event(GUI_EVENT_ACTIONUPDATE
, NULL
);
162 if (timeout
== TIMEOUT_NOBLOCK
)
163 button
= button_get(false);
164 else if (timeout
== TIMEOUT_BLOCK
)
165 button
= button_get(true);
167 button
= button_get_w_tmo(timeout
);
169 /* Data from sys events can be pulled with button_get_data */
170 if (button
== BUTTON_NONE
|| button
& SYS_EVENT
)
172 /* Don't send any buttons through untill we see the release event */
173 if (wait_for_release
)
175 if (button
&BUTTON_REL
)
177 wait_for_release
= false;
183 #if CONFIG_CODEC == SWCODEC
184 /* Produce keyclick */
185 if (global_settings
.keyclick
&& !(button
& BUTTON_REL
))
186 if (!(button
& BUTTON_REPEAT
) || global_settings
.keyclick_repeats
)
187 pcmbuf_beep(4000, KEYCLICK_DURATION
, 2500*global_settings
.keyclick
);
190 if ((context
!= last_context
) && ((last_button
& BUTTON_REL
) == 0))
192 if (button
& BUTTON_REL
)
194 last_button
= button
;
195 last_action
= ACTION_NONE
;
197 /* eat all buttons until the previous button was |BUTTON_REL
198 (also eat the |BUTTON_REL button) */
199 return ACTION_NONE
; /* "safest" return value */
201 last_context
= context
;
202 #ifdef HAVE_TOUCHSCREEN
203 if (button
& BUTTON_TOUCHSCREEN
)
207 if (last_button
& BUTTON_TOUCHSCREEN
)
209 if ((button
& BUTTON_REL
) &&
210 ((last_button
& BUTTON_REPEAT
)==0))
214 else if (button
& BUTTON_REPEAT
)
217 last_button
= button
;
218 return ACTION_TOUCHSCREEN
;
221 #ifndef HAS_BUTTON_HOLD
222 screen_has_lock
= ((context
& ALLOW_SOFTLOCK
) == ALLOW_SOFTLOCK
);
223 if (screen_has_lock
&& keys_locked
)
225 if (button
== unlock_combo
)
227 last_button
= BUTTON_NONE
;
229 splash(HZ
/2, str(LANG_KEYLOCK_OFF
));
230 return ACTION_REDRAW
;
233 #if (BUTTON_REMOTE != 0)
234 if ((button
& BUTTON_REMOTE
) == 0)
237 if ((button
& BUTTON_REL
))
238 splash(HZ
/2, str(LANG_KEYLOCK_ON
));
239 return ACTION_REDRAW
;
242 context
&= ~ALLOW_SOFTLOCK
;
243 #endif /* HAS_BUTTON_HOLD */
245 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
246 button
= button_flip_horizontally(context
, button
);
249 /* logf("%x,%x",last_button,button); */
252 /* logf("context = %x",context); */
253 #if (BUTTON_REMOTE != 0)
254 if (button
& BUTTON_REMOTE
)
255 context
|= CONTEXT_REMOTE
;
257 if ((context
& CONTEXT_PLUGIN
) && get_context_map
)
258 items
= get_context_map(context
);
260 items
= get_context_mapping(context
);
265 ret
= do_button_check(items
,button
,last_button
,&i
);
267 if (ret
== ACTION_UNKNOWN
)
269 context
= get_next_context(items
,i
);
271 if (context
!= (int)CONTEXT_STOPSEARCHING
)
278 /* Action was found or STOPSEARCHING was specified */
281 /* DEBUGF("ret = %x\n",ret); */
282 #ifndef HAS_BUTTON_HOLD
283 if (screen_has_lock
&& (ret
== ACTION_STD_KEYLOCK
))
285 unlock_combo
= button
;
287 splash(HZ
/2, str(LANG_KEYLOCK_ON
));
289 button_clear_queue();
290 return ACTION_REDRAW
;
293 if ((current_tick
- last_action_tick
< REPEAT_WINDOW_TICKS
)
294 && (ret
== last_action
))
299 last_button
= button
;
301 last_data
= button_get_data();
302 last_action_tick
= current_tick
;
306 int get_action(int context
, int timeout
)
308 return get_action_worker(context
,timeout
,NULL
);
311 int get_custom_action(int context
,int timeout
,
312 const struct button_mapping
* (*get_context_map
)(int))
314 return get_action_worker(context
,timeout
,get_context_map
);
317 bool action_userabort(int timeout
)
319 int action
= get_action_worker(CONTEXT_STD
,timeout
,NULL
);
320 bool ret
= (action
== ACTION_STD_CANCEL
);
323 default_event_handler(action
);
328 #ifndef HAS_BUTTON_HOLD
329 bool is_keys_locked(void)
331 return (screen_has_lock
&& keys_locked
);
335 intptr_t get_action_data(void)
340 int get_action_statuscode(int *button
)
344 *button
= last_button
;
346 if (last_button
& BUTTON_REMOTE
)
347 ret
|= ACTION_REMOTE
;
349 ret
|= ACTION_REPEAT
;
353 #ifdef HAVE_TOUCHSCREEN
354 /* return BUTTON_NONE on error
355 * BUTTON_REPEAT if repeated press
356 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
357 * BUTTON_REL if its a short press = release after press
358 * BUTTON_TOUCHSCREEN if press
360 int action_get_touchscreen_press(short *x
, short *y
)
362 static int last_data
= 0;
364 if ((last_button
& BUTTON_TOUCHSCREEN
) == 0)
366 data
= button_get_data();
367 if (last_button
& BUTTON_REL
)
369 *x
= (last_data
&0xffff0000)>>16;
370 *y
= (last_data
&0xffff);
374 *x
= (data
&0xffff0000)>>16;
379 return BUTTON_REPEAT
;
382 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
383 if (last_button
& BUTTON_REL
)
384 return BUTTON_REPEAT
|BUTTON_REL
;
385 return BUTTON_TOUCHSCREEN
;
389 /* Don't let get_action*() return any ACTION_* values untill the current buttons
390 * have ben release. SYS_* and BUTTON_NONE will go through.
391 * Any actions relying on _RELEASE won't get seen
393 void action_wait_for_release(void)
395 wait_for_release
= true;