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 #ifdef HAS_BUTTON_HOLD
71 bool hold_button_state
= false;
72 bool button_hold(void) {
73 return hold_button_state
;
77 #ifdef HAS_REMOTE_BUTTON_HOLD
78 bool remote_hold_button_state
= false;
79 bool remote_button_hold(void) {
80 return remote_hold_button_state
;
83 static void button_event(int key
, bool pressed
);
84 extern bool debug_wps
;
87 #ifdef HAVE_TOUCHSCREEN
88 static void touchscreen_event(int x
, int y
)
95 if(x
>= 0 && y
>= 0 && x
< SIM_LCD_WIDTH
&& y
< SIM_LCD_HEIGHT
) {
96 mouse_coords
= (x
<< 16) | y
;
97 button_event(BUTTON_TOUCHSCREEN
, true);
99 printf("Mouse at: (%d, %d)\n", x
, y
);
104 static void mouse_event(SDL_MouseButtonEvent
*event
, bool button_up
)
106 #define SQUARE(x) ((x)*(x))
109 static int xybutton
= 0;
113 switch ( event
->button
)
115 /* The scrollwheel button up events are ignored as they are queued immediately */
116 case SDL_BUTTON_LEFT
:
117 case SDL_BUTTON_MIDDLE
:
118 if ( mapping
&& background
) {
119 printf(" { SDLK_, %d, %d, %d, \"\" },\n", x
, y
,
120 (int)sqrt( SQUARE(x
-(int)event
->x
) + SQUARE(y
-(int)event
->y
))
124 if ( background
&& xybutton
) {
125 button_event( xybutton
, false );
129 #ifdef HAVE_TOUCHSCREEN
131 button_event(BUTTON_TOUCHSCREEN
, false);
135 } else { /* button down */
136 switch ( event
->button
)
138 #ifdef HAVE_SCROLLWHEEL
139 case SDL_BUTTON_WHEELUP
:
140 button_event( SDLK_UP
, true );
142 case SDL_BUTTON_WHEELDOWN
:
143 button_event( SDLK_DOWN
, true );
146 case SDL_BUTTON_LEFT
:
147 case SDL_BUTTON_MIDDLE
:
148 if ( mapping
&& background
) {
154 xybutton
= xy2button( event
->x
, event
->y
);
156 button_event( xybutton
, true );
161 #ifdef HAVE_TOUCHSCREEN
162 touchscreen_event(event
->x
, event
->y
);
167 if (debug_wps
&& event
->button
== BUTTON_LEFT
)
176 if ( event
->y
>= UI_REMOTE_POSY
) /* Remote Screen */
178 m_x
-= UI_REMOTE_POSX
;
179 m_y
-= UI_REMOTE_POSY
;
190 m_x
= event
->x
/ display_zoom
;
191 m_y
= event
->y
/ display_zoom
;
193 if ( m_y
>= LCD_HEIGHT
) /* Remote Screen */
198 printf("Mouse at: (%d, %d)\n", m_x
, m_y
);
204 static bool event_handler(SDL_Event
*event
)
210 button_event(event
->key
.keysym
.sym
, event
->type
== SDL_KEYDOWN
);
212 #ifdef HAVE_TOUCHSCREEN
213 case SDL_MOUSEMOTION
:
214 if (event
->motion
.state
& SDL_BUTTON(1))
215 touchscreen_event(event
->motion
.x
, event
->motion
.y
);
219 case SDL_MOUSEBUTTONUP
:
220 case SDL_MOUSEBUTTONDOWN
:
221 mouse_event(&event
->button
, event
->type
== SDL_MOUSEBUTTONUP
);
231 void gui_message_loop(void)
237 /* wait for the next event */
238 while(SDL_WaitEvent(&event
) == 0)
239 printf("SDL_WaitEvent() error\n");
241 sim_enter_irq_handler();
242 quit
= event_handler(&event
);
243 sim_exit_irq_handler();
248 static void button_event(int key
, bool pressed
)
251 static bool usb_connected
= false;
252 if (usb_connected
&& key
!= USB_KEY
)
259 usb_connected
= !usb_connected
;
261 queue_post(&button_queue
, SYS_USB_CONNECTED
, 0);
263 queue_post(&button_queue
, SYS_USB_DISCONNECTED
, 0);
267 #ifdef HAS_BUTTON_HOLD
271 hold_button_state
= !hold_button_state
;
272 DEBUGF("Hold button is %s\n", hold_button_state
?"ON":"OFF");
277 #ifdef HAS_REMOTE_BUTTON_HOLD
281 remote_hold_button_state
= !remote_hold_button_state
;
282 DEBUGF("Remote hold button is %s\n",
283 remote_hold_button_state
?"ON":"OFF");
288 #if defined(IRIVER_H100_SERIES) || defined (IRIVER_H300_SERIES)
293 case REMOTETYPE_UNPLUGGED
:
294 _remote_type
=REMOTETYPE_H100_LCD
;
295 DEBUGF("Changed remote type to H100\n");
297 case REMOTETYPE_H100_LCD
:
298 _remote_type
=REMOTETYPE_H300_LCD
;
299 DEBUGF("Changed remote type to H300\n");
301 case REMOTETYPE_H300_LCD
:
302 _remote_type
=REMOTETYPE_H300_NONLCD
;
303 DEBUGF("Changed remote type to H300 NON-LCD\n");
305 case REMOTETYPE_H300_NONLCD
:
306 _remote_type
=REMOTETYPE_UNPLUGGED
;
307 DEBUGF("Changed remote type to none\n");
316 sim_trigger_screendump();
320 #ifdef HAVE_TOUCHSCREEN
324 touchscreen_set_mode(touchscreen_get_mode() == TOUCHSCREEN_POINT
? TOUCHSCREEN_BUTTON
: TOUCHSCREEN_POINT
);
325 printf("Touchscreen mode: %s\n", touchscreen_get_mode() == TOUCHSCREEN_POINT
? "TOUCHSCREEN_POINT" : "TOUCHSCREEN_BUTTON");
329 #ifdef HAVE_TOUCHSCREEN
330 new_btn
= key_to_touch(key
, mouse_coords
);
333 new_btn
= key_to_button(key
);
336 /* Call to make up for scrollwheel target implementation. This is
337 * not handled in the main button.c driver, but on the target
338 * implementation (look at button-e200.c for example if you are trying to
339 * figure out why using button_get_data needed a hack before).
341 #if defined(BUTTON_SCROLL_FWD) && defined(BUTTON_SCROLL_BACK)
342 if((new_btn
== BUTTON_SCROLL_FWD
|| new_btn
== BUTTON_SCROLL_BACK
) &&
345 /* Clear these buttons from the data - adding them to the queue is
346 * handled in the scrollwheel drivers for the targets. They do not
347 * store the scroll forward/back buttons in their button data for
348 * the button_read call.
350 #ifdef HAVE_BACKLIGHT
353 #ifdef HAVE_BUTTON_LIGHT
356 queue_post(&button_queue
, new_btn
, 1<<24);
357 new_btn
&= ~(BUTTON_SCROLL_FWD
| BUTTON_SCROLL_BACK
);
366 #if defined(HAVE_BUTTON_DATA) && defined(HAVE_TOUCHSCREEN)
367 int button_read_device(int* data
)
369 *data
= mouse_coords
;
371 int button_read_device(void)
374 #ifdef HAS_BUTTON_HOLD
375 int hold_button
= button_hold();
377 #ifdef HAVE_BACKLIGHT
379 static int hold_button_old
= false;
380 if (hold_button
!= hold_button_old
)
382 hold_button_old
= hold_button
;
383 backlight_hold_changed(hold_button
);
395 void button_init_device(void)
397 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);