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"
38 static int last_button
= BUTTON_NONE
|BUTTON_REL
; /* allow the ipod wheel to
40 static intptr_t last_data
= 0;
41 static int last_action
= ACTION_NONE
;
42 static bool repeated
= false;
43 static bool wait_for_release
= false;
45 #ifdef HAVE_TOUCHSCREEN
46 static bool short_press
= false;
49 #define REPEAT_WINDOW_TICKS HZ/10
50 static int last_action_tick
= 0;
52 /* software keylock stuff */
53 #ifndef HAS_BUTTON_HOLD
54 static bool keys_locked
= false;
55 static int unlock_combo
= BUTTON_NONE
;
56 static bool screen_has_lock
= false;
57 #endif /* HAVE_SOFTWARE_KEYLOCK */
60 * do_button_check is the worker function for get_default_action.
61 * returns ACTION_UNKNOWN or the requested return value from the list.
63 static inline int do_button_check(const struct button_mapping
*items
,
64 int button
, int last_button
, int *start
)
67 int ret
= ACTION_UNKNOWN
;
69 while (items
[i
].button_code
!= BUTTON_NONE
)
71 if (items
[i
].button_code
== button
)
73 if ((items
[i
].pre_button_code
== BUTTON_NONE
)
74 || (items
[i
].pre_button_code
== last_button
))
76 ret
= items
[i
].action_code
;
86 static inline int get_next_context(const struct button_mapping
*items
, int i
)
88 while (items
[i
].button_code
!= BUTTON_NONE
)
90 return (items
[i
].action_code
== ACTION_NONE
) ?
95 * int get_action_worker(int context, struct button_mapping *user_mappings,
97 This function searches the button list for the given context for the just
99 If there is a match it returns the value from the list.
100 If there is no match..
101 the last item in the list "points" to the next context in a chain
102 so the "chain" is followed until the button is found.
103 putting ACTION_NONE will get CONTEXT_STD which is always the last list checked.
105 Timeout can be TIMEOUT_NOBLOCK to return immediatly
106 TIMEOUT_BLOCK to wait for a button press
107 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
;
115 int ret
= ACTION_UNKNOWN
;
116 static int last_context
= CONTEXT_STD
;
119 send_event(GUI_EVENT_ACTIONUPDATE
, NULL
);
121 if (timeout
== TIMEOUT_NOBLOCK
)
122 button
= button_get(false);
123 else if (timeout
== TIMEOUT_BLOCK
)
124 button
= button_get(true);
126 button
= button_get_w_tmo(timeout
);
128 /* Data from sys events can be pulled with button_get_data */
129 if (button
== BUTTON_NONE
|| button
& SYS_EVENT
)
131 /* Don't send any buttons through untill we see the release event */
132 if (wait_for_release
)
134 if (button
&BUTTON_REL
)
136 wait_for_release
= false;
142 #if CONFIG_CODEC == SWCODEC
143 /* Produce keyclick */
144 if (global_settings
.keyclick
&& !(button
& BUTTON_REL
))
145 if (!(button
& BUTTON_REPEAT
) || global_settings
.keyclick_repeats
)
146 pcmbuf_beep(4000, KEYCLICK_DURATION
, 2500*global_settings
.keyclick
);
149 if ((context
!= last_context
) && ((last_button
& BUTTON_REL
) == 0))
151 if (button
& BUTTON_REL
)
153 last_button
= button
;
154 last_action
= ACTION_NONE
;
156 /* eat all buttons until the previous button was |BUTTON_REL
157 (also eat the |BUTTON_REL button) */
158 return ACTION_NONE
; /* "safest" return value */
160 last_context
= context
;
161 #ifdef HAVE_TOUCHSCREEN
162 if (button
& BUTTON_TOUCHSCREEN
)
166 if (last_button
& BUTTON_TOUCHSCREEN
)
168 if ((button
& BUTTON_REL
) &&
169 ((last_button
& BUTTON_REPEAT
)==0))
173 else if (button
& BUTTON_REPEAT
)
176 last_button
= button
;
177 return ACTION_TOUCHSCREEN
;
180 #ifndef HAS_BUTTON_HOLD
181 screen_has_lock
= ((context
& ALLOW_SOFTLOCK
) == ALLOW_SOFTLOCK
);
182 if (screen_has_lock
&& keys_locked
)
184 if (button
== unlock_combo
)
186 last_button
= BUTTON_NONE
;
188 splash(HZ
/2, str(LANG_KEYLOCK_OFF
));
189 return ACTION_REDRAW
;
192 #if (BUTTON_REMOTE != 0)
193 if ((button
& BUTTON_REMOTE
) == 0)
196 if ((button
& BUTTON_REL
))
197 splash(HZ
/2, str(LANG_KEYLOCK_ON
));
198 return ACTION_REDRAW
;
201 context
&= ~ALLOW_SOFTLOCK
;
202 #endif /* HAS_BUTTON_HOLD */
204 /* logf("%x,%x",last_button,button); */
207 /* logf("context = %x",context); */
208 #if (BUTTON_REMOTE != 0)
209 if (button
& BUTTON_REMOTE
)
210 context
|= CONTEXT_REMOTE
;
212 if ((context
& CONTEXT_PLUGIN
) && get_context_map
)
213 items
= get_context_map(context
);
215 items
= get_context_mapping(context
);
220 ret
= do_button_check(items
,button
,last_button
,&i
);
222 if (ret
== ACTION_UNKNOWN
)
224 context
= get_next_context(items
,i
);
226 if (context
!= (int)CONTEXT_STOPSEARCHING
)
233 /* Action was found or STOPSEARCHING was specified */
236 /* DEBUGF("ret = %x\n",ret); */
237 #ifndef HAS_BUTTON_HOLD
238 if (screen_has_lock
&& (ret
== ACTION_STD_KEYLOCK
))
240 unlock_combo
= button
;
242 splash(HZ
/2, str(LANG_KEYLOCK_ON
));
244 button_clear_queue();
245 return ACTION_REDRAW
;
248 if ((current_tick
- last_action_tick
< REPEAT_WINDOW_TICKS
)
249 && (ret
== last_action
))
254 last_button
= button
;
256 last_data
= button_get_data();
257 last_action_tick
= current_tick
;
261 int get_action(int context
, int timeout
)
263 return get_action_worker(context
,timeout
,NULL
);
266 int get_custom_action(int context
,int timeout
,
267 const struct button_mapping
* (*get_context_map
)(int))
269 return get_action_worker(context
,timeout
,get_context_map
);
272 bool action_userabort(int timeout
)
274 int action
= get_action_worker(CONTEXT_STD
,timeout
,NULL
);
275 bool ret
= (action
== ACTION_STD_CANCEL
);
278 default_event_handler(action
);
283 #ifndef HAS_BUTTON_HOLD
284 bool is_keys_locked(void)
286 return (screen_has_lock
&& keys_locked
);
290 intptr_t get_action_data(void)
295 int get_action_statuscode(int *button
)
299 *button
= last_button
;
301 if (last_button
& BUTTON_REMOTE
)
302 ret
|= ACTION_REMOTE
;
304 ret
|= ACTION_REPEAT
;
308 #ifdef HAVE_TOUCHSCREEN
309 /* return BUTTON_NONE on error
310 * BUTTON_REPEAT if repeated press
311 * BUTTON_REPEAT|BUTTON_REL if release after repeated press
312 * BUTTON_REL if its a short press = release after press
313 * BUTTON_TOUCHSCREEN if press
315 int action_get_touchscreen_press(short *x
, short *y
)
317 static int last_data
= 0;
319 if ((last_button
& BUTTON_TOUCHSCREEN
) == 0)
321 data
= button_get_data();
322 if (last_button
& BUTTON_REL
)
324 *x
= (last_data
&0xffff0000)>>16;
325 *y
= (last_data
&0xffff);
329 *x
= (data
&0xffff0000)>>16;
334 return BUTTON_REPEAT
;
337 /* This is to return a BUTTON_REL after a BUTTON_REPEAT. */
338 if (last_button
& BUTTON_REL
)
339 return BUTTON_REPEAT
|BUTTON_REL
;
340 return BUTTON_TOUCHSCREEN
;
344 /* Don't let get_action*() return any ACTION_* values untill the current buttons
345 * have ben release. SYS_* and BUTTON_NONE will go through.
346 * Any actions relying on _RELEASE won't get seen
348 void action_wait_for_release(void)
350 wait_for_release
= true;