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 #ifdef HAVE_TOUCHSCREEN
42 #include "statusbar-skinned.h"
45 static int last_button
= BUTTON_NONE
|BUTTON_REL
; /* allow the ipod wheel to
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;
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
)
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
;
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
)
102 if (!(lang_is_rtl() && ((context
== CONTEXT_STD
) ||
103 (context
== CONTEXT_TREE
) || (context
== CONTEXT_LIST
) ||
104 (context
== CONTEXT_MAINMENU
))))
110 ~(BUTTON_LEFT
| BUTTON_RIGHT
111 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD) && \
113 | BUTTON_SCROLL_BACK
| BUTTON_SCROLL_FWD
115 #if defined(BUTTON_MINUS) && defined(BUTTON_PLUS) && \
117 | BUTTON_MINUS
| BUTTON_PLUS
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) && \
127 if (button
& BUTTON_SCROLL_BACK
)
128 newbutton
|= BUTTON_SCROLL_FWD
;
129 if (button
& BUTTON_SCROLL_FWD
)
130 newbutton
|= BUTTON_SCROLL_BACK
;
132 #if defined(BUTTON_MINUS) && defined(BUTTON_PLUS) && \
134 if (button
& BUTTON_MINUS
)
135 newbutton
|= BUTTON_PLUS
;
136 if (button
& BUTTON_PLUS
)
137 newbutton
|= BUTTON_MINUS
;
144 static inline int get_next_context(const struct button_mapping
*items
, int i
)
146 while (items
[i
].button_code
!= BUTTON_NONE
)
148 return (items
[i
].action_code
== ACTION_NONE
) ?
150 items
[i
].action_code
;
153 * int get_action_worker(int context, struct button_mapping *user_mappings,
155 This function searches the button list for the given context for the just
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
;
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);
184 button
= button_get_w_tmo(timeout
);
186 /* Data from sys events can be pulled with button_get_data
187 * multimedia button presses don't go through the action system */
188 if (button
== BUTTON_NONE
|| button
& (SYS_EVENT
|BUTTON_MULTIMEDIA
))
190 /* Don't send any buttons through untill we see the release event */
191 if (wait_for_release
)
193 if (button
&BUTTON_REL
)
195 wait_for_release
= false;
201 #if CONFIG_CODEC == SWCODEC
202 /* Produce keyclick */
203 if (global_settings
.keyclick
&& !(button
& BUTTON_REL
))
204 if (!(button
& BUTTON_REPEAT
) || global_settings
.keyclick_repeats
)
205 pcmbuf_beep(4000, KEYCLICK_DURATION
, 2500*global_settings
.keyclick
);
208 if ((context
!= last_context
) && ((last_button
& BUTTON_REL
) == 0)
209 #ifdef HAVE_SCROLLWHEEL
210 /* Scrollwheel doesn't generate release events */
211 && !(last_button
& (BUTTON_SCROLL_BACK
| BUTTON_SCROLL_FWD
))
215 if (button
& BUTTON_REL
)
217 last_button
= button
;
218 last_action
= ACTION_NONE
;
220 /* eat all buttons until the previous button was |BUTTON_REL
221 (also eat the |BUTTON_REL button) */
222 return ACTION_NONE
; /* "safest" return value */
224 last_context
= context
;
225 #ifndef HAS_BUTTON_HOLD
226 screen_has_lock
= ((context
& ALLOW_SOFTLOCK
) == ALLOW_SOFTLOCK
);
227 if (screen_has_lock
&& keys_locked
)
229 if (button
== unlock_combo
)
231 last_button
= BUTTON_NONE
;
233 splash(HZ
/2, str(LANG_KEYLOCK_OFF
));
234 return ACTION_REDRAW
;
237 #if (BUTTON_REMOTE != 0)
238 if ((button
& BUTTON_REMOTE
) == 0)
241 if ((button
& BUTTON_REL
))
242 splash(HZ
/2, str(LANG_KEYLOCK_ON
));
243 return ACTION_REDRAW
;
246 context
&= ~ALLOW_SOFTLOCK
;
247 #endif /* HAS_BUTTON_HOLD */
249 #ifdef HAVE_TOUCHSCREEN
250 if (button
& BUTTON_TOUCHSCREEN
)
254 if (last_button
& BUTTON_TOUCHSCREEN
)
256 if ((button
& BUTTON_REL
) &&
257 ((last_button
& BUTTON_REPEAT
)==0))
261 else if (button
& BUTTON_REPEAT
)
264 last_button
= button
;
265 return ACTION_TOUCHSCREEN
;
269 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
270 button
= button_flip_horizontally(context
, button
);
273 /* logf("%x,%x",last_button,button); */
276 /* logf("context = %x",context); */
277 #if (BUTTON_REMOTE != 0)
278 if (button
& BUTTON_REMOTE
)
279 context
|= CONTEXT_REMOTE
;
281 if ((context
& CONTEXT_PLUGIN
) && get_context_map
)
282 items
= get_context_map(context
);
284 items
= get_context_mapping(context
);
289 ret
= do_button_check(items
,button
,last_button
,&i
);
291 if (ret
== ACTION_UNKNOWN
)
293 context
= get_next_context(items
,i
);
295 if (context
!= (int)CONTEXT_STOPSEARCHING
)
302 /* Action was found or STOPSEARCHING was specified */
305 /* DEBUGF("ret = %x\n",ret); */
306 #ifndef HAS_BUTTON_HOLD
307 if (screen_has_lock
&& (ret
== ACTION_STD_KEYLOCK
))
309 unlock_combo
= button
;
311 splash(HZ
/2, str(LANG_KEYLOCK_ON
));
313 button_clear_queue();
314 return ACTION_REDRAW
;
317 if ((current_tick
- last_action_tick
< REPEAT_WINDOW_TICKS
)
318 && (ret
== last_action
))
323 last_button
= button
;
325 last_data
= button_get_data();
326 last_action_tick
= current_tick
;
330 int get_action(int context
, int timeout
)
332 int button
= get_action_worker(context
,timeout
,NULL
);
333 #ifdef HAVE_TOUCHSCREEN
334 if (button
== ACTION_TOUCHSCREEN
)
335 button
= sb_touch_to_button(context
);
340 int get_custom_action(int context
,int timeout
,
341 const struct button_mapping
* (*get_context_map
)(int))
343 return get_action_worker(context
,timeout
,get_context_map
);
346 bool action_userabort(int timeout
)
348 int action
= get_action_worker(CONTEXT_STD
,timeout
,NULL
);
349 bool ret
= (action
== ACTION_STD_CANCEL
);
352 default_event_handler(action
);
357 #ifndef HAS_BUTTON_HOLD
358 bool is_keys_locked(void)
360 return (screen_has_lock
&& keys_locked
);
364 intptr_t get_action_data(void)
369 int get_action_statuscode(int *button
)
373 *button
= last_button
;
375 if (last_button
& BUTTON_REMOTE
)
376 ret
|= ACTION_REMOTE
;
378 ret
|= ACTION_REPEAT
;
382 #ifdef HAVE_TOUCHSCREEN
383 /* return BUTTON_NONE on error
384 * BUTTON_REPEAT if repeated press
385 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
386 * BUTTON_REL if it's a short press = release after press
387 * BUTTON_TOUCHSCREEN if press
389 int action_get_touchscreen_press(short *x
, short *y
)
391 static int last_data
= 0;
393 if ((last_button
& BUTTON_TOUCHSCREEN
) == 0)
395 data
= button_get_data();
396 if (last_button
& BUTTON_REL
)
398 *x
= (last_data
&0xffff0000)>>16;
399 *y
= (last_data
&0xffff);
403 *x
= (data
&0xffff0000)>>16;
408 return BUTTON_REPEAT
;
411 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
412 if (last_button
& BUTTON_REL
)
413 return BUTTON_REPEAT
|BUTTON_REL
;
414 return BUTTON_TOUCHSCREEN
;
417 int action_get_touchscreen_press_in_vp(short *x1
, short *y1
, struct viewport
*vp
)
422 ret
= action_get_touchscreen_press(&x
, &y
);
424 if (ret
!= BUTTON_NONE
&& viewport_point_within_vp(vp
, x
, y
))
430 if (ret
& BUTTON_TOUCHSCREEN
)
431 return ACTION_UNKNOWN
;
436 /* Don't let get_action*() return any ACTION_* values until the current buttons
437 * have been released. SYS_* and BUTTON_NONE will go through.
438 * Any actions relying on _RELEASE won't get seen.
440 void action_wait_for_release(void)
442 wait_for_release
= true;