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;
72 #ifdef HAS_BUTTON_HOLD
73 bool hold_button_state
= false;
74 bool button_hold(void) {
75 return hold_button_state
;
79 #ifdef HAS_REMOTE_BUTTON_HOLD
80 bool remote_hold_button_state
= false;
81 bool remote_button_hold(void) {
82 return remote_hold_button_state
;
85 static void button_event(int key
, bool pressed
);
86 extern bool debug_wps
;
89 #ifdef HAVE_TOUCHSCREEN
90 static void touchscreen_event(int x
, int y
)
97 if(x
>= 0 && y
>= 0 && x
< SIM_LCD_WIDTH
&& y
< SIM_LCD_HEIGHT
) {
98 mouse_coords
= (x
<< 16) | y
;
99 button_event(BUTTON_TOUCHSCREEN
, true);
101 printf("Mouse at 1: (%d, %d)\n", x
, y
);
106 static void mouse_event(SDL_MouseButtonEvent
*event
, bool button_up
)
108 #define SQUARE(x) ((x)*(x))
111 static int xybutton
= 0;
115 switch ( event
->button
)
117 #ifdef HAVE_SCROLLWHEEL
118 case SDL_BUTTON_WHEELUP
:
119 case SDL_BUTTON_WHEELDOWN
:
121 case SDL_BUTTON_MIDDLE
:
122 case SDL_BUTTON_RIGHT
:
123 button_event( event
->button
, false );
125 /* The scrollwheel button up events are ignored as they are queued immediately */
126 case SDL_BUTTON_LEFT
:
127 if ( mapping
&& background
) {
128 printf(" { SDLK_, %d, %d, %d, \"\" },\n", x
, y
,
129 (int)sqrt( SQUARE(x
-(int)event
->x
) + SQUARE(y
-(int)event
->y
))
133 if ( background
&& xybutton
) {
134 button_event( xybutton
, false );
138 #ifdef HAVE_TOUCHSCREEN
140 button_event(BUTTON_TOUCHSCREEN
, false);
144 } else { /* button down */
145 switch ( event
->button
)
147 #ifdef HAVE_SCROLLWHEEL
148 case SDL_BUTTON_WHEELUP
:
149 case SDL_BUTTON_WHEELDOWN
:
151 case SDL_BUTTON_MIDDLE
:
152 case SDL_BUTTON_RIGHT
:
153 button_event( event
->button
, true );
155 case SDL_BUTTON_LEFT
:
156 if ( mapping
&& background
) {
162 xybutton
= xy2button( event
->x
, event
->y
);
164 button_event( xybutton
, true );
169 #ifdef HAVE_TOUCHSCREEN
170 touchscreen_event(event
->x
, event
->y
);
175 if (debug_wps
&& event
->button
== SDL_BUTTON_LEFT
)
184 if ( event
->y
>= UI_REMOTE_POSY
) /* Remote Screen */
186 m_x
-= UI_REMOTE_POSX
;
187 m_y
-= UI_REMOTE_POSY
;
201 if ( m_y
>= LCD_HEIGHT
) /* Remote Screen */
206 printf("Mouse at 2: (%d, %d)\n", m_x
, m_y
);
212 static bool event_handler(SDL_Event
*event
)
218 case SDL_ACTIVEEVENT
:
219 if (event
->active
.state
& SDL_APPINPUTFOCUS
)
221 if (event
->active
.gain
== 1)
222 sdl_app_has_input_focus
= 1;
224 sdl_app_has_input_focus
= 0;
229 ev_key
= event
->key
.keysym
.sym
;
230 #if (CONFIG_PLATFORM & PLATFORM_MAEMO5)
231 /* N900 with shared up/down cursor mapping. Seen on the German,
232 Finnish, Italian, French and Russian version. Probably more. */
233 if (event
->key
.keysym
.mod
& KMOD_MODE
)
235 if (ev_key
== SDLK_LEFT
)
237 else if (ev_key
== SDLK_RIGHT
)
241 button_event(ev_key
, event
->type
== SDL_KEYDOWN
);
243 #ifdef HAVE_TOUCHSCREEN
244 case SDL_MOUSEMOTION
:
245 if (event
->motion
.state
& SDL_BUTTON(1))
247 int x
= event
->motion
.x
/ display_zoom
;
248 int y
= event
->motion
.y
/ display_zoom
;
249 touchscreen_event(x
, y
);
254 case SDL_MOUSEBUTTONUP
:
255 case SDL_MOUSEBUTTONDOWN
:
257 SDL_MouseButtonEvent
*mev
= &event
->button
;
258 mev
->x
/= display_zoom
;
259 mev
->y
/= display_zoom
;
260 mouse_event(mev
, event
->type
== SDL_MOUSEBUTTONUP
);
264 /* Post SYS_POWEROFF event. Will post SDL_USEREVENT in shutdown_hw() if successful. */
265 queue_broadcast(SYS_POWEROFF
, 0);
275 void gui_message_loop(void)
281 /* wait for the next event */
282 while(SDL_WaitEvent(&event
) == 0)
283 printf("SDL_WaitEvent() error\n");
285 sim_enter_irq_handler();
286 quit
= event_handler(&event
);
287 sim_exit_irq_handler();
292 static void button_event(int key
, bool pressed
)
295 static bool usb_connected
= false;
296 if (usb_connected
&& key
!= USB_KEY
)
303 usb_connected
= !usb_connected
;
305 queue_post(&button_queue
, SYS_USB_CONNECTED
, 0);
307 queue_post(&button_queue
, SYS_USB_DISCONNECTED
, 0);
311 #ifdef HAS_BUTTON_HOLD
315 hold_button_state
= !hold_button_state
;
316 DEBUGF("Hold button is %s\n", hold_button_state
?"ON":"OFF");
321 #ifdef HAS_REMOTE_BUTTON_HOLD
325 remote_hold_button_state
= !remote_hold_button_state
;
326 DEBUGF("Remote hold button is %s\n",
327 remote_hold_button_state
?"ON":"OFF");
332 #if defined(IRIVER_H100_SERIES) || defined (IRIVER_H300_SERIES)
337 case REMOTETYPE_UNPLUGGED
:
338 _remote_type
=REMOTETYPE_H100_LCD
;
339 DEBUGF("Changed remote type to H100\n");
341 case REMOTETYPE_H100_LCD
:
342 _remote_type
=REMOTETYPE_H300_LCD
;
343 DEBUGF("Changed remote type to H300\n");
345 case REMOTETYPE_H300_LCD
:
346 _remote_type
=REMOTETYPE_H300_NONLCD
;
347 DEBUGF("Changed remote type to H300 NON-LCD\n");
349 case REMOTETYPE_H300_NONLCD
:
350 _remote_type
=REMOTETYPE_UNPLUGGED
;
351 DEBUGF("Changed remote type to none\n");
360 sim_trigger_screendump();
364 #ifdef HAVE_TOUCHSCREEN
368 touchscreen_set_mode(touchscreen_get_mode() == TOUCHSCREEN_POINT
? TOUCHSCREEN_BUTTON
: TOUCHSCREEN_POINT
);
369 printf("Touchscreen mode: %s\n", touchscreen_get_mode() == TOUCHSCREEN_POINT
? "TOUCHSCREEN_POINT" : "TOUCHSCREEN_BUTTON");
373 #ifdef HAVE_TOUCHSCREEN
374 new_btn
= key_to_touch(key
, mouse_coords
);
377 new_btn
= key_to_button(key
);
380 /* Call to make up for scrollwheel target implementation. This is
381 * not handled in the main button.c driver, but on the target
382 * implementation (look at button-e200.c for example if you are trying to
383 * figure out why using button_get_data needed a hack before).
385 #if defined(BUTTON_SCROLL_FWD) && defined(BUTTON_SCROLL_BACK)
386 if((new_btn
== BUTTON_SCROLL_FWD
|| new_btn
== BUTTON_SCROLL_BACK
) &&
389 /* Clear these buttons from the data - adding them to the queue is
390 * handled in the scrollwheel drivers for the targets. They do not
391 * store the scroll forward/back buttons in their button data for
392 * the button_read call.
394 #ifdef HAVE_BACKLIGHT
397 #ifdef HAVE_BUTTON_LIGHT
400 queue_post(&button_queue
, new_btn
, 1<<24);
401 new_btn
&= ~(BUTTON_SCROLL_FWD
| BUTTON_SCROLL_BACK
);
410 #if defined(HAVE_BUTTON_DATA) && defined(HAVE_TOUCHSCREEN)
411 int button_read_device(int* data
)
413 *data
= mouse_coords
;
415 int button_read_device(void)
418 #ifdef HAS_BUTTON_HOLD
419 int hold_button
= button_hold();
421 #ifdef HAVE_BACKLIGHT
423 static int hold_button_old
= false;
424 if (hold_button
!= hold_button_old
)
426 hold_button_old
= hold_button
;
427 backlight_hold_changed(hold_button
);
439 void button_init_device(void)
441 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);