1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Daniel Everton <dan@iocaine.org>
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 ****************************************************************************/
27 #include "system-sdl.h"
32 #ifdef HAVE_LCD_BITMAP
33 #include "lcd-bitmap.h"
34 #elif defined(HAVE_LCD_CHARCELLS)
35 #include "lcd-charcells.h"
37 #ifdef HAVE_REMOTE_LCD
38 #include "lcd-remote-bitmap.h"
40 #include "thread-sdl.h"
41 #include "SDL_mutex.h"
42 #include "SDL_thread.h"
46 /* extern functions */
47 extern void new_key(int key
);
48 extern int xy2button( int x
, int y
);
49 void button_event(int key
, bool pressed
);
51 SDL_Surface
*gui_surface
;
52 bool background
= true; /* use backgrounds by default */
53 #ifdef HAVE_REMOTE_LCD
54 static bool showremote
= true; /* include remote by default */
57 bool debug_buttons
= false;
59 bool lcd_display_redraw
= true; /* Used for player simulator */
60 char having_new_lcd
= true; /* Used for player simulator */
61 bool sim_alarm_wakeup
= false;
62 const char *sim_root_dir
= NULL
;
64 bool debug_audio
= false;
66 bool debug_wps
= false;
67 int wps_verbose_level
= 3;
70 void irq_button_event(int key
, bool pressed
) {
71 sim_enter_irq_handler();
72 button_event( key
, pressed
);
73 sim_exit_irq_handler();
80 void gui_message_loop(void)
84 static int x
,y
,xybutton
= 0;
86 while(!done
&& SDL_WaitEvent(&event
))
91 irq_button_event(event
.key
.keysym
.sym
, true);
94 irq_button_event(event
.key
.keysym
.sym
, false);
95 case SDL_MOUSEBUTTONDOWN
:
96 switch ( event
.button
.button
) {
97 #ifdef HAVE_SCROLLWHEEL
98 case SDL_BUTTON_WHEELUP
:
99 irq_button_event( SDLK_UP
, true );
101 case SDL_BUTTON_WHEELDOWN
:
102 irq_button_event( SDLK_DOWN
, true );
105 case SDL_BUTTON_LEFT
:
106 case SDL_BUTTON_MIDDLE
:
107 if ( mapping
&& background
) {
112 xybutton
= xy2button( event
.button
.x
, event
.button
.y
);
114 irq_button_event( xybutton
, true );
121 if (debug_wps
&& event
.button
.button
== 1)
125 if ( event
.button
.y
< UI_REMOTE_POSY
) /* Main Screen */
126 printf("Mouse at: (%d, %d)\n", event
.button
.x
- UI_LCD_POSX
-1 , event
.button
.y
- UI_LCD_POSY
- 1 );
128 printf("Mouse at: (%d, %d)\n", event
.button
.x
- UI_REMOTE_POSX
-1 , event
.button
.y
- UI_REMOTE_POSY
- 1 );
130 printf("Mouse at: (%d, %d)\n", event
.button
.x
- UI_LCD_POSX
-1 , event
.button
.y
- UI_LCD_POSY
- 1 );
133 if ( event
.button
.y
/display_zoom
< LCD_HEIGHT
) /* Main Screen */
134 printf("Mouse at: (%d, %d)\n", event
.button
.x
/display_zoom
, event
.button
.y
/display_zoom
);
137 printf("Mouse at: (%d, %d)\n", event
.button
.x
/display_zoom
, event
.button
.y
/display_zoom
- LCD_HEIGHT
);
141 case SDL_MOUSEBUTTONUP
:
142 switch ( event
.button
.button
) {
143 /* The scrollwheel button up events are ignored as they are queued immediately */
144 case SDL_BUTTON_LEFT
:
145 case SDL_BUTTON_MIDDLE
:
146 if ( mapping
&& background
) {
147 printf(" { SDLK_, %d, %d, %d, \"\" },\n", x
, y
, (int)sqrt( sqr(x
-(int)event
.button
.x
) + sqr(y
-(int)event
.button
.y
)) );
149 if ( background
&& xybutton
) {
150 irq_button_event( xybutton
, false );
153 #ifdef HAVE_TOUCHSCREEN
155 irq_button_event(BUTTON_TOUCHSCREEN
, false);
169 /*printf("Unhandled event\n"); */
175 bool gui_startup(void)
177 SDL_Surface
*picture_surface
;
180 if (SDL_Init(SDL_INIT_VIDEO
|SDL_INIT_AUDIO
|SDL_INIT_TIMER
)) {
181 fprintf(stderr
, "fatal: %s\n", SDL_GetError());
187 /* Try and load the background image. If it fails go without */
189 picture_surface
= SDL_LoadBMP("UI256.bmp");
190 if (picture_surface
== NULL
) {
192 fprintf(stderr
, "warn: %s\n", SDL_GetError());
204 #ifdef HAVE_REMOTE_LCD
207 width
= SIM_LCD_WIDTH
> SIM_REMOTE_WIDTH
? SIM_LCD_WIDTH
: SIM_REMOTE_WIDTH
;
208 height
= SIM_LCD_HEIGHT
+ SIM_REMOTE_HEIGHT
;
213 width
= SIM_LCD_WIDTH
;
214 height
= SIM_LCD_HEIGHT
;
219 if ((gui_surface
= SDL_SetVideoMode(width
* display_zoom
, height
* display_zoom
, 24, SDL_HWSURFACE
|SDL_DOUBLEBUF
)) == NULL
) {
220 fprintf(stderr
, "fatal: %s\n", SDL_GetError());
224 SDL_WM_SetCaption(UI_TITLE
, NULL
);
227 #ifdef HAVE_REMOTE_LCD
229 sim_lcd_remote_init();
232 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
234 if (background
&& picture_surface
!= NULL
) {
235 SDL_BlitSurface(picture_surface
, NULL
, gui_surface
, NULL
);
236 SDL_UpdateRect(gui_surface
, 0, 0, 0, 0);
242 bool gui_shutdown(void)
244 /* Order here is relevent to prevent deadlocks and use of destroyed
245 sync primitives by kernel threads */
246 thread_sdl_shutdown();
247 sim_kernel_shutdown();
251 #if defined(WIN32) && defined(main)
252 /* Don't use SDL_main on windows -> no more stdio redirection */
256 int main(int argc
, char *argv
[])
261 for (x
= 1; x
< argc
; x
++)
263 if (!strcmp("--debugaudio", argv
[x
]))
266 printf("Writing debug audio file.\n");
268 else if (!strcmp("--debugwps", argv
[x
]))
271 printf("WPS debug mode enabled.\n");
273 else if (!strcmp("--nobackground", argv
[x
]))
276 printf("Disabling background image.\n");
278 #ifdef HAVE_REMOTE_LCD
279 else if (!strcmp("--noremote", argv
[x
]))
283 printf("Disabling remote image.\n");
286 else if (!strcmp("--old_lcd", argv
[x
]))
288 having_new_lcd
= false;
289 printf("Using old LCD layout.\n");
291 else if (!strcmp("--zoom", argv
[x
]))
295 display_zoom
=atoi(argv
[x
]);
298 printf("Window zoom is %d\n", display_zoom
);
300 else if (!strcmp("--alarm", argv
[x
]))
302 sim_alarm_wakeup
= true;
303 printf("Simulating alarm wakeup.\n");
305 else if (!strcmp("--root", argv
[x
]))
310 sim_root_dir
= argv
[x
];
311 printf("Root directory: %s\n", sim_root_dir
);
314 else if (!strcmp("--mapping", argv
[x
]))
317 printf("Printing click coords with drag radii.\n");
319 else if (!strcmp("--debugbuttons", argv
[x
]))
321 debug_buttons
= true;
322 printf("Printing background button clicks.\n");
326 printf("rockboxui\n");
327 printf("Arguments:\n");
328 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
329 printf(" --debugwps \t Print advanced WPS debug info\n");
330 printf(" --nobackground \t Disable the background image\n");
331 #ifdef HAVE_REMOTE_LCD
332 printf(" --noremote \t Disable the remote image (will disable backgrounds)\n");
334 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
335 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
336 printf(" --alarm \t Simulate a wake-up on alarm\n");
337 printf(" --root [DIR]\t Set root directory\n");
338 printf(" --mapping \t Output coordinates and radius for mapping backgrounds\n");
344 if (display_zoom
> 1) {
348 if (!sim_kernel_init()) {
349 fprintf(stderr
, "sim_kernel_init failed\n");
353 if (!gui_startup()) {
354 fprintf(stderr
, "gui_startup failed\n");
358 /* app_main will be called by the new main thread */
359 if (!thread_sdl_init(NULL
)) {
360 fprintf(stderr
, "thread_sdl_init failed\n");
366 return gui_shutdown();