diff options
Diffstat (limited to 'uisimulator/sdl/button-x11.c')
-rw-r--r-- | uisimulator/sdl/button-x11.c | 307 |
1 files changed, 307 insertions, 0 deletions
diff --git a/uisimulator/sdl/button-x11.c b/uisimulator/sdl/button-x11.c new file mode 100644 index 0000000000..5fda7cf45a --- /dev/null +++ b/uisimulator/sdl/button-x11.c | |||
@@ -0,0 +1,307 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 by Björn Stenberg | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #include <stdlib.h> | ||
20 | #include "config.h" | ||
21 | #include "button.h" | ||
22 | #include "kernel.h" | ||
23 | #include "debug.h" | ||
24 | #include "backlight.h" | ||
25 | #include "misc.h" | ||
26 | #include <SDL.h> | ||
27 | |||
28 | extern int screenhack_handle_events(bool *release); | ||
29 | |||
30 | struct event_queue button_queue; | ||
31 | |||
32 | static int button_state = 0; /* keeps track of pressed keys */ | ||
33 | static long lastbtn; /* Last valid button status */ | ||
34 | |||
35 | /* how often we check to see if a button is pressed */ | ||
36 | #define POLL_FREQUENCY HZ/25 | ||
37 | |||
38 | /* how long until repeat kicks in */ | ||
39 | #define REPEAT_START 8 | ||
40 | |||
41 | /* the speed repeat starts at */ | ||
42 | #define REPEAT_INTERVAL_START 4 | ||
43 | |||
44 | /* speed repeat finishes at */ | ||
45 | #define REPEAT_INTERVAL_FINISH 2 | ||
46 | |||
47 | /* mostly copied from real button.c */ | ||
48 | void button_read (void); | ||
49 | |||
50 | static void button_tick(void) | ||
51 | { | ||
52 | static int tick = 0; | ||
53 | static int count = 0; | ||
54 | static int repeat_speed = REPEAT_INTERVAL_START; | ||
55 | static int repeat_count = 0; | ||
56 | static bool repeat = false; | ||
57 | int diff; | ||
58 | int btn; | ||
59 | |||
60 | /* only poll every X ticks */ | ||
61 | if ( ++tick >= POLL_FREQUENCY ) | ||
62 | { | ||
63 | bool post = false; | ||
64 | button_read(); | ||
65 | btn = button_state; | ||
66 | |||
67 | /* Find out if a key has been released */ | ||
68 | diff = btn ^ lastbtn; | ||
69 | if(diff && (btn & diff) == 0) | ||
70 | { | ||
71 | queue_post(&button_queue, BUTTON_REL | diff, NULL); | ||
72 | } | ||
73 | else | ||
74 | { | ||
75 | if ( btn ) | ||
76 | { | ||
77 | /* normal keypress */ | ||
78 | if ( btn != lastbtn ) | ||
79 | { | ||
80 | post = true; | ||
81 | repeat = false; | ||
82 | repeat_speed = REPEAT_INTERVAL_START; | ||
83 | |||
84 | } | ||
85 | else /* repeat? */ | ||
86 | { | ||
87 | if ( repeat ) | ||
88 | { | ||
89 | count--; | ||
90 | if (count == 0) { | ||
91 | post = true; | ||
92 | /* yes we have repeat */ | ||
93 | repeat_speed--; | ||
94 | if (repeat_speed < REPEAT_INTERVAL_FINISH) | ||
95 | repeat_speed = REPEAT_INTERVAL_FINISH; | ||
96 | count = repeat_speed; | ||
97 | |||
98 | repeat_count++; | ||
99 | |||
100 | } | ||
101 | } | ||
102 | else | ||
103 | { | ||
104 | if (count++ > REPEAT_START) | ||
105 | { | ||
106 | post = true; | ||
107 | repeat = true; | ||
108 | repeat_count = 0; | ||
109 | /* initial repeat */ | ||
110 | count = REPEAT_INTERVAL_START; | ||
111 | } | ||
112 | } | ||
113 | } | ||
114 | if ( post ) | ||
115 | { | ||
116 | if (repeat) | ||
117 | queue_post(&button_queue, BUTTON_REPEAT | btn, NULL); | ||
118 | else | ||
119 | queue_post(&button_queue, btn, NULL); | ||
120 | #ifdef HAVE_REMOTE_LCD | ||
121 | if(btn & BUTTON_REMOTE) | ||
122 | remote_backlight_on(); | ||
123 | else | ||
124 | #endif | ||
125 | backlight_on(); | ||
126 | |||
127 | } | ||
128 | } | ||
129 | else | ||
130 | { | ||
131 | repeat = false; | ||
132 | count = 0; | ||
133 | } | ||
134 | } | ||
135 | lastbtn = btn & ~(BUTTON_REL | BUTTON_REPEAT); | ||
136 | tick = 0; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | /* | ||
141 | * Read SDL keys and translate to rockbox buttons | ||
142 | */ | ||
143 | |||
144 | void button_read (void) | ||
145 | { | ||
146 | int k; | ||
147 | bool release = false; /* is this a release event */ | ||
148 | int ev = screenhack_handle_events(&release); | ||
149 | |||
150 | switch (ev) | ||
151 | { | ||
152 | case SDLK_KP4: | ||
153 | case SDLK_LEFT: | ||
154 | case SDLK_4: | ||
155 | k = BUTTON_LEFT; | ||
156 | break; | ||
157 | |||
158 | case SDLK_KP6: | ||
159 | case SDLK_RIGHT: | ||
160 | case SDLK_6: | ||
161 | k = BUTTON_RIGHT; | ||
162 | break; | ||
163 | |||
164 | case SDLK_KP8: | ||
165 | case SDLK_UP: | ||
166 | case SDLK_8: | ||
167 | #ifdef BUTTON_UP | ||
168 | k = BUTTON_UP; | ||
169 | #elif defined BUTTON_PLAY | ||
170 | k = BUTTON_PLAY; | ||
171 | #endif | ||
172 | break; | ||
173 | |||
174 | case SDLK_KP2: | ||
175 | case SDLK_DOWN: | ||
176 | case SDLK_2: | ||
177 | #ifdef BUTTON_DOWN | ||
178 | k = BUTTON_DOWN; | ||
179 | #elif defined BUTTON_STOP | ||
180 | k = BUTTON_STOP; | ||
181 | #endif | ||
182 | break; | ||
183 | |||
184 | #ifdef BUTTON_ON | ||
185 | case SDLK_KP_PLUS: | ||
186 | case SDLK_q: | ||
187 | k = BUTTON_ON; | ||
188 | break; | ||
189 | #endif | ||
190 | |||
191 | #ifdef BUTTON_OFF | ||
192 | case SDLK_KP_ENTER: | ||
193 | case SDLK_a: | ||
194 | k = BUTTON_OFF; | ||
195 | break; | ||
196 | #endif | ||
197 | |||
198 | #ifdef BUTTON_F1 | ||
199 | case SDLK_KP_DIVIDE: | ||
200 | case SDLK_F1: | ||
201 | k = BUTTON_F1; | ||
202 | break; | ||
203 | |||
204 | case SDLK_KP_MULTIPLY: | ||
205 | case SDLK_F2: | ||
206 | k = BUTTON_F2; | ||
207 | break; | ||
208 | |||
209 | case SDLK_KP_MINUS: | ||
210 | case SDLK_F3: | ||
211 | k = BUTTON_F3; | ||
212 | break; | ||
213 | #elif defined(BUTTON_REC) | ||
214 | case SDLK_KP_DIVIDE: | ||
215 | case SDLK_F1: | ||
216 | k = BUTTON_REC; | ||
217 | break; | ||
218 | #endif | ||
219 | |||
220 | case SDLK_KP5: | ||
221 | case SDLK_5: | ||
222 | case SDLK_SPACE: | ||
223 | #ifdef BUTTON_PLAY | ||
224 | k = BUTTON_PLAY; | ||
225 | #elif defined(BUTTON_SELECT) | ||
226 | k = BUTTON_SELECT; | ||
227 | #endif | ||
228 | break; | ||
229 | |||
230 | #ifdef HAVE_LCD_BITMAP | ||
231 | case SDLK_7: | ||
232 | if(!release) | ||
233 | screen_dump(); | ||
234 | break; | ||
235 | #endif | ||
236 | |||
237 | case SDLK_KP_PERIOD: | ||
238 | case SDLK_INSERT: | ||
239 | #ifdef BUTTON_MENU | ||
240 | k = BUTTON_MENU; | ||
241 | #elif defined(BUTTON_MODE) | ||
242 | k = BUTTON_MODE; | ||
243 | #endif | ||
244 | break; | ||
245 | |||
246 | default: | ||
247 | k = 0; | ||
248 | if(ev) | ||
249 | DEBUGF("received ev %d\n", ev); | ||
250 | break; | ||
251 | } | ||
252 | |||
253 | if (release) | ||
254 | button_state &= ~k; | ||
255 | else | ||
256 | button_state |= k; | ||
257 | } | ||
258 | |||
259 | /* Again copied from real button.c... */ | ||
260 | |||
261 | long button_get(bool block) | ||
262 | { | ||
263 | struct event ev; | ||
264 | |||
265 | if ( block || !queue_empty(&button_queue) ) | ||
266 | { | ||
267 | queue_wait(&button_queue, &ev); | ||
268 | return ev.id; | ||
269 | } | ||
270 | return BUTTON_NONE; | ||
271 | } | ||
272 | |||
273 | long button_get_w_tmo(int ticks) | ||
274 | { | ||
275 | struct event ev; | ||
276 | queue_wait_w_tmo(&button_queue, &ev, ticks); | ||
277 | return (ev.id != SYS_TIMEOUT)? ev.id: BUTTON_NONE; | ||
278 | } | ||
279 | |||
280 | void button_init(void) | ||
281 | { | ||
282 | tick_add_task(button_tick); | ||
283 | } | ||
284 | |||
285 | int button_status(void) | ||
286 | { | ||
287 | return lastbtn; | ||
288 | } | ||
289 | |||
290 | void button_clear_queue(void) | ||
291 | { | ||
292 | queue_clear(&button_queue); | ||
293 | } | ||
294 | |||
295 | #ifdef HAS_BUTTON_HOLD | ||
296 | bool button_hold(void) { | ||
297 | /* temp fix for hold button on irivers */ | ||
298 | return false; | ||
299 | } | ||
300 | #endif | ||
301 | |||
302 | #ifdef HAS_REMOTE_BUTTON_HOLD | ||
303 | bool remote_button_hold(void) { | ||
304 | /* temp fix for hold button on irivers */ | ||
305 | return false; | ||
306 | } | ||
307 | #endif | ||