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)
42 static int last_button
= BUTTON_NONE
|BUTTON_REL
; /* allow the ipod wheel to
44 static intptr_t last_data
= 0;
45 static int last_action
= ACTION_NONE
;
46 static bool repeated
= false;
47 static bool wait_for_release
= false;
49 #ifdef HAVE_TOUCHSCREEN
50 static bool short_press
= false;
53 #define REPEAT_WINDOW_TICKS HZ/10
54 static int last_action_tick
= 0;
56 /* software keylock stuff */
57 #ifndef HAS_BUTTON_HOLD
58 static bool keys_locked
= false;
59 static int unlock_combo
= BUTTON_NONE
;
60 static bool screen_has_lock
= false;
61 #endif /* HAVE_SOFTWARE_KEYLOCK */
64 * do_button_check is the worker function for get_default_action.
65 * returns ACTION_UNKNOWN or the requested return value from the list.
67 static inline int do_button_check(const struct button_mapping
*items
,
68 int button
, int last_button
, int *start
)
71 int ret
= ACTION_UNKNOWN
;
73 while (items
[i
].button_code
!= BUTTON_NONE
)
75 if (items
[i
].button_code
== button
)
77 if ((items
[i
].pre_button_code
== BUTTON_NONE
)
78 || (items
[i
].pre_button_code
== last_button
))
80 ret
= items
[i
].action_code
;
90 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
92 * button is horizontally inverted to support RTL language if the given language
93 * and context combination require that
95 static int button_flip_horizontally(int context
, int button
)
99 if (!(lang_is_rtl() && ((context
== CONTEXT_STD
) ||
100 (context
== CONTEXT_TREE
) || (context
== CONTEXT_LIST
) ||
101 (context
== CONTEXT_MAINMENU
))))
107 ~(BUTTON_LEFT
| BUTTON_RIGHT
108 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD) && \
110 | BUTTON_SCROLL_BACK
| BUTTON_SCROLL_FWD
114 if (button
& BUTTON_LEFT
)
115 newbutton
|= BUTTON_RIGHT
;
116 if (button
& BUTTON_RIGHT
)
117 newbutton
|= BUTTON_LEFT
;
118 #if defined(BUTTON_SCROLL_BACK) && defined(BUTTON_SCROLL_FWD) && \
120 if (button
& BUTTON_SCROLL_BACK
)
121 newbutton
|= BUTTON_SCROLL_FWD
;
122 if (button
& BUTTON_SCROLL_FWD
)
123 newbutton
|= BUTTON_SCROLL_BACK
;
130 static inline int get_next_context(const struct button_mapping
*items
, int i
)
132 while (items
[i
].button_code
!= BUTTON_NONE
)
134 return (items
[i
].action_code
== ACTION_NONE
) ?
136 items
[i
].action_code
;
139 * int get_action_worker(int context, struct button_mapping *user_mappings,
141 This function searches the button list for the given context for the just
143 If there is a match it returns the value from the list.
144 If there is no match..
145 the last item in the list "points" to the next context in a chain
146 so the "chain" is followed until the button is found.
147 putting ACTION_NONE will get CONTEXT_STD which is always the last list checked.
149 Timeout can be TIMEOUT_NOBLOCK to return immediatly
150 TIMEOUT_BLOCK to wait for a button press
151 Any number >0 to wait that many ticks for a press
153 static int get_action_worker(int context
, int timeout
,
154 const struct button_mapping
* (*get_context_map
)(int) )
156 const struct button_mapping
*items
= NULL
;
159 int ret
= ACTION_UNKNOWN
;
160 static int last_context
= CONTEXT_STD
;
163 send_event(GUI_EVENT_ACTIONUPDATE
, NULL
);
165 if (timeout
== TIMEOUT_NOBLOCK
)
166 button
= button_get(false);
167 else if (timeout
== TIMEOUT_BLOCK
)
168 button
= button_get(true);
170 button
= button_get_w_tmo(timeout
);
172 /* Data from sys events can be pulled with button_get_data */
173 if (button
== BUTTON_NONE
|| button
& SYS_EVENT
)
175 /* Don't send any buttons through untill we see the release event */
176 if (wait_for_release
)
178 if (button
&BUTTON_REL
)
180 wait_for_release
= false;
186 #if CONFIG_CODEC == SWCODEC
187 /* Produce keyclick */
188 if (global_settings
.keyclick
&& !(button
& BUTTON_REL
))
189 if (!(button
& BUTTON_REPEAT
) || global_settings
.keyclick_repeats
)
190 pcmbuf_beep(4000, KEYCLICK_DURATION
, 2500*global_settings
.keyclick
);
193 if ((context
!= last_context
) && ((last_button
& BUTTON_REL
) == 0))
195 if (button
& BUTTON_REL
)
197 last_button
= button
;
198 last_action
= ACTION_NONE
;
200 /* eat all buttons until the previous button was |BUTTON_REL
201 (also eat the |BUTTON_REL button) */
202 return ACTION_NONE
; /* "safest" return value */
204 last_context
= context
;
205 #ifdef HAVE_TOUCHSCREEN
206 if (button
& BUTTON_TOUCHSCREEN
)
210 if (last_button
& BUTTON_TOUCHSCREEN
)
212 if ((button
& BUTTON_REL
) &&
213 ((last_button
& BUTTON_REPEAT
)==0))
217 else if (button
& BUTTON_REPEAT
)
220 last_button
= button
;
221 return ACTION_TOUCHSCREEN
;
224 #ifndef HAS_BUTTON_HOLD
225 screen_has_lock
= ((context
& ALLOW_SOFTLOCK
) == ALLOW_SOFTLOCK
);
226 if (screen_has_lock
&& keys_locked
)
228 if (button
== unlock_combo
)
230 last_button
= BUTTON_NONE
;
232 splash(HZ
/2, str(LANG_KEYLOCK_OFF
));
233 return ACTION_REDRAW
;
236 #if (BUTTON_REMOTE != 0)
237 if ((button
& BUTTON_REMOTE
) == 0)
240 if ((button
& BUTTON_REL
))
241 splash(HZ
/2, str(LANG_KEYLOCK_ON
));
242 return ACTION_REDRAW
;
245 context
&= ~ALLOW_SOFTLOCK
;
246 #endif /* HAS_BUTTON_HOLD */
248 #if defined(HAVE_LCD_BITMAP) && !defined(BOOTLOADER)
249 button
= button_flip_horizontally(context
, button
);
252 /* logf("%x,%x",last_button,button); */
255 /* logf("context = %x",context); */
256 #if (BUTTON_REMOTE != 0)
257 if (button
& BUTTON_REMOTE
)
258 context
|= CONTEXT_REMOTE
;
260 if ((context
& CONTEXT_PLUGIN
) && get_context_map
)
261 items
= get_context_map(context
);
263 items
= get_context_mapping(context
);
268 ret
= do_button_check(items
,button
,last_button
,&i
);
270 if (ret
== ACTION_UNKNOWN
)
272 context
= get_next_context(items
,i
);
274 if (context
!= (int)CONTEXT_STOPSEARCHING
)
281 /* Action was found or STOPSEARCHING was specified */
284 /* DEBUGF("ret = %x\n",ret); */
285 #ifndef HAS_BUTTON_HOLD
286 if (screen_has_lock
&& (ret
== ACTION_STD_KEYLOCK
))
288 unlock_combo
= button
;
290 splash(HZ
/2, str(LANG_KEYLOCK_ON
));
292 button_clear_queue();
293 return ACTION_REDRAW
;
296 if ((current_tick
- last_action_tick
< REPEAT_WINDOW_TICKS
)
297 && (ret
== last_action
))
302 last_button
= button
;
304 last_data
= button_get_data();
305 last_action_tick
= current_tick
;
309 int get_action(int context
, int timeout
)
311 return get_action_worker(context
,timeout
,NULL
);
314 int get_custom_action(int context
,int timeout
,
315 const struct button_mapping
* (*get_context_map
)(int))
317 return get_action_worker(context
,timeout
,get_context_map
);
320 bool action_userabort(int timeout
)
322 int action
= get_action_worker(CONTEXT_STD
,timeout
,NULL
);
323 bool ret
= (action
== ACTION_STD_CANCEL
);
326 default_event_handler(action
);
331 #ifndef HAS_BUTTON_HOLD
332 bool is_keys_locked(void)
334 return (screen_has_lock
&& keys_locked
);
338 intptr_t get_action_data(void)
343 int get_action_statuscode(int *button
)
347 *button
= last_button
;
349 if (last_button
& BUTTON_REMOTE
)
350 ret
|= ACTION_REMOTE
;
352 ret
|= ACTION_REPEAT
;
356 #ifdef HAVE_TOUCHSCREEN
357 /* return BUTTON_NONE on error
358 * BUTTON_REPEAT if repeated press
359 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
360 * BUTTON_REL if its a short press = release after press
361 * BUTTON_TOUCHSCREEN if press
363 int action_get_touchscreen_press(short *x
, short *y
)
365 static int last_data
= 0;
367 if ((last_button
& BUTTON_TOUCHSCREEN
) == 0)
369 data
= button_get_data();
370 if (last_button
& BUTTON_REL
)
372 *x
= (last_data
&0xffff0000)>>16;
373 *y
= (last_data
&0xffff);
377 *x
= (data
&0xffff0000)>>16;
382 return BUTTON_REPEAT
;
385 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
386 if (last_button
& BUTTON_REL
)
387 return BUTTON_REPEAT
|BUTTON_REL
;
388 return BUTTON_TOUCHSCREEN
;
391 int action_get_touchscreen_press_in_vp(short *x1
, short *y1
, struct viewport
*vp
)
396 ret
= action_get_touchscreen_press(&x
, &y
);
398 if (ret
!= BUTTON_NONE
&& viewport_point_within_vp(vp
, x
, y
))
404 if (ret
& BUTTON_TOUCHSCREEN
)
405 return ACTION_UNKNOWN
;
410 /* Don't let get_action*() return any ACTION_* values untill the current buttons
411 * have ben release. SYS_* and BUTTON_NONE will go through.
412 * Any actions relying on _RELEASE won't get seen
414 void action_wait_for_release(void)
416 wait_for_release
= true;