1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Felix Arends
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 ****************************************************************************/
23 #include <stdlib.h> /* EXIT_SUCCESS */
24 #include "sim-ui-defines.h"
25 #include "lcd-charcells.h"
26 #include "lcd-remote.h"
30 #include "backlight.h"
32 #include "button-sdl.h"
33 #include "backlight.h"
34 #include "sim_tasks.h"
35 #include "buttonmap.h"
38 #ifdef HAVE_TOUCHSCREEN
39 #include "touchscreen.h"
40 static int mouse_coords
= 0;
42 /* how long until repeat kicks in */
43 #define REPEAT_START 6
45 /* the speed repeat starts at */
46 #define REPEAT_INTERVAL_START 4
48 /* speed repeat finishes at */
49 #define REPEAT_INTERVAL_FINISH 2
51 #ifdef HAVE_TOUCHSCREEN
52 #define USB_KEY SDLK_c /* SDLK_u is taken by BUTTON_MIDLEFT */
54 #define USB_KEY SDLK_u
57 #if defined(IRIVER_H100_SERIES) || defined (IRIVER_H300_SERIES)
58 int _remote_type
=REMOTETYPE_H100_LCD
;
66 struct event_queue button_queue
;
68 static int btn
= 0; /* Hopefully keeps track of currently pressed keys... */
70 int sdl_app_has_input_focus
= 1;
71 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
72 static int n900_updown_key_pressed
= 0;
75 #ifdef HAS_BUTTON_HOLD
76 bool hold_button_state
= false;
77 bool button_hold(void) {
78 return hold_button_state
;
82 #ifdef HAS_REMOTE_BUTTON_HOLD
83 bool remote_hold_button_state
= false;
84 bool remote_button_hold(void) {
85 return remote_hold_button_state
;
88 static void button_event(int key
, bool pressed
);
89 extern bool debug_wps
;
92 #ifdef HAVE_TOUCHSCREEN
93 static void touchscreen_event(int x
, int y
)
100 if(x
>= 0 && y
>= 0 && x
< SIM_LCD_WIDTH
&& y
< SIM_LCD_HEIGHT
) {
101 mouse_coords
= (x
<< 16) | y
;
102 button_event(BUTTON_TOUCHSCREEN
, true);
104 printf("Mouse at 1: (%d, %d)\n", x
, y
);
109 static void mouse_event(SDL_MouseButtonEvent
*event
, bool button_up
)
111 #define SQUARE(x) ((x)*(x))
114 static int xybutton
= 0;
118 switch ( event
->button
)
120 #ifdef HAVE_SCROLLWHEEL
121 case SDL_BUTTON_WHEELUP
:
122 case SDL_BUTTON_WHEELDOWN
:
124 case SDL_BUTTON_MIDDLE
:
125 case SDL_BUTTON_RIGHT
:
126 button_event( event
->button
, false );
128 /* The scrollwheel button up events are ignored as they are queued immediately */
129 case SDL_BUTTON_LEFT
:
130 if ( mapping
&& background
) {
131 printf(" { SDLK_, %d, %d, %d, \"\" },\n", x
, y
,
132 (int)sqrt( SQUARE(x
-(int)event
->x
) + SQUARE(y
-(int)event
->y
))
136 if ( background
&& xybutton
) {
137 button_event( xybutton
, false );
141 #ifdef HAVE_TOUCHSCREEN
143 button_event(BUTTON_TOUCHSCREEN
, false);
147 } else { /* button down */
148 switch ( event
->button
)
150 #ifdef HAVE_SCROLLWHEEL
151 case SDL_BUTTON_WHEELUP
:
152 case SDL_BUTTON_WHEELDOWN
:
154 case SDL_BUTTON_MIDDLE
:
155 case SDL_BUTTON_RIGHT
:
156 button_event( event
->button
, true );
158 case SDL_BUTTON_LEFT
:
159 if ( mapping
&& background
) {
165 xybutton
= xy2button( event
->x
, event
->y
);
167 button_event( xybutton
, true );
172 #ifdef HAVE_TOUCHSCREEN
173 touchscreen_event(event
->x
, event
->y
);
178 if (debug_wps
&& event
->button
== SDL_BUTTON_LEFT
)
187 if ( event
->y
>= UI_REMOTE_POSY
) /* Remote Screen */
189 m_x
-= UI_REMOTE_POSX
;
190 m_y
-= UI_REMOTE_POSY
;
204 if ( m_y
>= LCD_HEIGHT
) /* Remote Screen */
209 printf("Mouse at 2: (%d, %d)\n", m_x
, m_y
);
215 static bool event_handler(SDL_Event
*event
)
221 case SDL_ACTIVEEVENT
:
222 if (event
->active
.state
& SDL_APPINPUTFOCUS
)
224 if (event
->active
.gain
== 1)
225 sdl_app_has_input_focus
= 1;
227 sdl_app_has_input_focus
= 0;
232 ev_key
= event
->key
.keysym
.sym
;
233 #if (CONFIG_PLATFORM & PLATFORM_MAEMO5)
234 /* N900 with shared up/down cursor mapping. Seen on the German,
235 Finnish, Italian, French and Russian version. Probably more. */
236 if (event
->key
.keysym
.mod
& KMOD_MODE
|| n900_updown_key_pressed
)
238 /* Prevent stuck up/down keys: If you release the ALT key before the cursor key,
239 rockbox will see a KEYUP event for left/right instead of up/down and
240 the previously pressed up/down key would stay active. */
241 if (ev_key
== SDLK_LEFT
|| ev_key
== SDLK_RIGHT
)
243 if (event
->type
== SDL_KEYDOWN
)
244 n900_updown_key_pressed
= 1;
246 n900_updown_key_pressed
= 0;
249 if (ev_key
== SDLK_LEFT
)
251 else if (ev_key
== SDLK_RIGHT
)
255 button_event(ev_key
, event
->type
== SDL_KEYDOWN
);
257 #ifdef HAVE_TOUCHSCREEN
258 case SDL_MOUSEMOTION
:
259 if (event
->motion
.state
& SDL_BUTTON(1))
261 int x
= event
->motion
.x
/ display_zoom
;
262 int y
= event
->motion
.y
/ display_zoom
;
263 touchscreen_event(x
, y
);
268 case SDL_MOUSEBUTTONUP
:
269 case SDL_MOUSEBUTTONDOWN
:
271 SDL_MouseButtonEvent
*mev
= &event
->button
;
272 mev
->x
/= display_zoom
;
273 mev
->y
/= display_zoom
;
274 mouse_event(mev
, event
->type
== SDL_MOUSEBUTTONUP
);
278 /* Post SYS_POWEROFF event. Will post SDL_USEREVENT in shutdown_hw() if successful. */
279 queue_broadcast(SYS_POWEROFF
, 0);
289 void gui_message_loop(void)
295 /* wait for the next event */
296 while(SDL_WaitEvent(&event
) == 0)
297 printf("SDL_WaitEvent() error\n");
299 sim_enter_irq_handler();
300 quit
= event_handler(&event
);
301 sim_exit_irq_handler();
306 static void button_event(int key
, bool pressed
)
309 static bool usb_connected
= false;
310 if (usb_connected
&& key
!= USB_KEY
)
317 usb_connected
= !usb_connected
;
319 queue_post(&button_queue
, SYS_USB_CONNECTED
, 0);
321 queue_post(&button_queue
, SYS_USB_DISCONNECTED
, 0);
325 #ifdef HAS_BUTTON_HOLD
329 hold_button_state
= !hold_button_state
;
330 DEBUGF("Hold button is %s\n", hold_button_state
?"ON":"OFF");
335 #ifdef HAS_REMOTE_BUTTON_HOLD
339 remote_hold_button_state
= !remote_hold_button_state
;
340 DEBUGF("Remote hold button is %s\n",
341 remote_hold_button_state
?"ON":"OFF");
346 #if defined(IRIVER_H100_SERIES) || defined (IRIVER_H300_SERIES)
351 case REMOTETYPE_UNPLUGGED
:
352 _remote_type
=REMOTETYPE_H100_LCD
;
353 DEBUGF("Changed remote type to H100\n");
355 case REMOTETYPE_H100_LCD
:
356 _remote_type
=REMOTETYPE_H300_LCD
;
357 DEBUGF("Changed remote type to H300\n");
359 case REMOTETYPE_H300_LCD
:
360 _remote_type
=REMOTETYPE_H300_NONLCD
;
361 DEBUGF("Changed remote type to H300 NON-LCD\n");
363 case REMOTETYPE_H300_NONLCD
:
364 _remote_type
=REMOTETYPE_UNPLUGGED
;
365 DEBUGF("Changed remote type to none\n");
374 sim_trigger_screendump();
378 #ifdef HAVE_TOUCHSCREEN
382 touchscreen_set_mode(touchscreen_get_mode() == TOUCHSCREEN_POINT
? TOUCHSCREEN_BUTTON
: TOUCHSCREEN_POINT
);
383 printf("Touchscreen mode: %s\n", touchscreen_get_mode() == TOUCHSCREEN_POINT
? "TOUCHSCREEN_POINT" : "TOUCHSCREEN_BUTTON");
387 #ifdef HAVE_TOUCHSCREEN
388 new_btn
= key_to_touch(key
, mouse_coords
);
391 new_btn
= key_to_button(key
);
394 /* Call to make up for scrollwheel target implementation. This is
395 * not handled in the main button.c driver, but on the target
396 * implementation (look at button-e200.c for example if you are trying to
397 * figure out why using button_get_data needed a hack before).
399 #if defined(BUTTON_SCROLL_FWD) && defined(BUTTON_SCROLL_BACK)
400 if((new_btn
== BUTTON_SCROLL_FWD
|| new_btn
== BUTTON_SCROLL_BACK
) &&
403 /* Clear these buttons from the data - adding them to the queue is
404 * handled in the scrollwheel drivers for the targets. They do not
405 * store the scroll forward/back buttons in their button data for
406 * the button_read call.
408 #ifdef HAVE_BACKLIGHT
411 #ifdef HAVE_BUTTON_LIGHT
414 queue_post(&button_queue
, new_btn
, 1<<24);
415 new_btn
&= ~(BUTTON_SCROLL_FWD
| BUTTON_SCROLL_BACK
);
424 #if defined(HAVE_BUTTON_DATA) && defined(HAVE_TOUCHSCREEN)
425 int button_read_device(int* data
)
427 *data
= mouse_coords
;
429 int button_read_device(void)
432 #ifdef HAS_BUTTON_HOLD
433 int hold_button
= button_hold();
435 #ifdef HAVE_BACKLIGHT
437 static int hold_button_old
= false;
438 if (hold_button
!= hold_button_old
)
440 hold_button_old
= hold_button
;
441 backlight_hold_changed(hold_button
);
453 void button_init_device(void)
455 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);