Move the PCM/audio playback part of SDL into the target tree.
[kugel-rb.git] / uisimulator / sdl / uisdl.c
blob970d6e6c5d1b2c86d7cf65b87751abfec7265098
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include <stdlib.h>
23 #include <string.h>
24 #include <setjmp.h>
25 #include "autoconf.h"
26 #include "button.h"
27 #include "system-sdl.h"
28 #include "thread.h"
29 #include "kernel.h"
30 #include "uisdl.h"
31 #include "lcd-sdl.h"
32 #ifdef HAVE_LCD_BITMAP
33 #include "lcd-bitmap.h"
34 #elif defined(HAVE_LCD_CHARCELLS)
35 #include "lcd-charcells.h"
36 #endif
37 #ifdef HAVE_REMOTE_LCD
38 #include "lcd-remote-bitmap.h"
39 #endif
40 #include "thread-sdl.h"
41 #include "SDL_mutex.h"
42 #include "SDL_thread.h"
43 #include "math.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 */
55 #endif
56 bool mapping = false;
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 #ifdef DEBUG
65 bool debug_audio = false;
66 #endif
68 bool debug_wps = false;
69 int wps_verbose_level = 3;
72 void irq_button_event(int key, bool pressed) {
73 sim_enter_irq_handler();
74 button_event( key, pressed );
75 sim_exit_irq_handler();
78 int sqr( int a ) {
79 return a*a;
82 void gui_message_loop(void)
84 SDL_Event event;
85 bool done = false;
86 static int x,y,xybutton = 0;
88 while(!done && SDL_WaitEvent(&event))
90 switch(event.type)
92 case SDL_KEYDOWN:
93 irq_button_event(event.key.keysym.sym, true);
94 break;
95 case SDL_KEYUP:
96 irq_button_event(event.key.keysym.sym, false);
97 case SDL_MOUSEBUTTONDOWN:
98 switch ( event.button.button ) {
99 #ifdef HAVE_SCROLLWHEEL
100 case SDL_BUTTON_WHEELUP:
101 irq_button_event( SDLK_UP, true );
102 break;
103 case SDL_BUTTON_WHEELDOWN:
104 irq_button_event( SDLK_DOWN, true );
105 break;
106 #endif
107 case SDL_BUTTON_LEFT:
108 case SDL_BUTTON_MIDDLE:
109 if ( mapping && background ) {
110 x = event.button.x;
111 y = event.button.y;
113 if ( background ) {
114 xybutton = xy2button( event.button.x, event.button.y );
115 if( xybutton )
116 irq_button_event( xybutton, true );
118 break;
119 default:
120 break;
123 if (debug_wps && event.button.button == 1)
125 if ( background )
126 #ifdef HAVE_REMOTE
127 if ( event.button.y < UI_REMOTE_POSY ) /* Main Screen */
128 printf("Mouse at: (%d, %d)\n", event.button.x - UI_LCD_POSX -1 , event.button.y - UI_LCD_POSY - 1 );
129 else
130 printf("Mouse at: (%d, %d)\n", event.button.x - UI_REMOTE_POSX -1 , event.button.y - UI_REMOTE_POSY - 1 );
131 #else
132 printf("Mouse at: (%d, %d)\n", event.button.x - UI_LCD_POSX -1 , event.button.y - UI_LCD_POSY - 1 );
133 #endif
134 else
135 if ( event.button.y/display_zoom < LCD_HEIGHT ) /* Main Screen */
136 printf("Mouse at: (%d, %d)\n", event.button.x/display_zoom, event.button.y/display_zoom );
137 #ifdef HAVE_REMOTE
138 else
139 printf("Mouse at: (%d, %d)\n", event.button.x/display_zoom, event.button.y/display_zoom - LCD_HEIGHT );
140 #endif
142 break;
143 case SDL_MOUSEBUTTONUP:
144 switch ( event.button.button ) {
145 /* The scrollwheel button up events are ignored as they are queued immediately */
146 case SDL_BUTTON_LEFT:
147 case SDL_BUTTON_MIDDLE:
148 if ( mapping && background ) {
149 printf(" { SDLK_, %d, %d, %d, \"\" },\n", x, y, (int)sqrt( sqr(x-(int)event.button.x) + sqr(y-(int)event.button.y)) );
151 if ( background && xybutton ) {
152 irq_button_event( xybutton, false );
153 xybutton = 0;
155 #ifdef HAVE_TOUCHSCREEN
156 else {
157 irq_button_event(BUTTON_TOUCHSCREEN, false);
159 #endif
160 break;
161 default:
162 break;
164 break;
167 case SDL_QUIT:
168 done = true;
169 break;
170 default:
171 /*printf("Unhandled event\n"); */
172 break;
177 bool gui_startup(void)
179 SDL_Surface *picture_surface;
180 int width, height;
182 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)) {
183 fprintf(stderr, "fatal: %s\n", SDL_GetError());
184 return false;
187 atexit(SDL_Quit);
189 /* Try and load the background image. If it fails go without */
190 if (background) {
191 picture_surface = SDL_LoadBMP("UI256.bmp");
192 if (picture_surface == NULL) {
193 background = false;
194 fprintf(stderr, "warn: %s\n", SDL_GetError());
198 /* Set things up */
199 if (background)
201 width = UI_WIDTH;
202 height = UI_HEIGHT;
204 else
206 #ifdef HAVE_REMOTE_LCD
207 if (showremote)
209 width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH;
210 height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT;
212 else
213 #endif
215 width = SIM_LCD_WIDTH;
216 height = SIM_LCD_HEIGHT;
221 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
222 fprintf(stderr, "fatal: %s\n", SDL_GetError());
223 return false;
226 SDL_WM_SetCaption(UI_TITLE, NULL);
228 sim_lcd_init();
229 #ifdef HAVE_REMOTE_LCD
230 if (showremote)
231 sim_lcd_remote_init();
232 #endif
234 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
236 if (background && picture_surface != NULL) {
237 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
238 SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
241 return true;
244 bool gui_shutdown(void)
246 /* Order here is relevent to prevent deadlocks and use of destroyed
247 sync primitives by kernel threads */
248 thread_sdl_shutdown();
249 sim_kernel_shutdown();
250 return true;
253 #if defined(WIN32) && defined(main)
254 /* Don't use SDL_main on windows -> no more stdio redirection */
255 #undef main
256 #endif
258 int main(int argc, char *argv[])
260 if (argc >= 1)
262 int x;
263 for (x = 1; x < argc; x++)
265 #ifdef DEBUG
266 if (!strcmp("--debugaudio", argv[x]))
268 debug_audio = true;
269 printf("Writing debug audio file.\n");
271 else
272 #endif
273 if (!strcmp("--debugwps", argv[x]))
275 debug_wps = true;
276 printf("WPS debug mode enabled.\n");
278 else if (!strcmp("--nobackground", argv[x]))
280 background = false;
281 printf("Disabling background image.\n");
283 #ifdef HAVE_REMOTE_LCD
284 else if (!strcmp("--noremote", argv[x]))
286 showremote = false;
287 background = false;
288 printf("Disabling remote image.\n");
290 #endif
291 else if (!strcmp("--old_lcd", argv[x]))
293 having_new_lcd = false;
294 printf("Using old LCD layout.\n");
296 else if (!strcmp("--zoom", argv[x]))
298 x++;
299 if(x < argc)
300 display_zoom=atoi(argv[x]);
301 else
302 display_zoom = 2;
303 printf("Window zoom is %d\n", display_zoom);
305 else if (!strcmp("--alarm", argv[x]))
307 sim_alarm_wakeup = true;
308 printf("Simulating alarm wakeup.\n");
310 else if (!strcmp("--root", argv[x]))
312 x++;
313 if (x < argc)
315 sim_root_dir = argv[x];
316 printf("Root directory: %s\n", sim_root_dir);
319 else if (!strcmp("--mapping", argv[x]))
321 mapping = true;
322 printf("Printing click coords with drag radii.\n");
324 else if (!strcmp("--debugbuttons", argv[x]))
326 debug_buttons = true;
327 printf("Printing background button clicks.\n");
329 else
331 printf("rockboxui\n");
332 printf("Arguments:\n");
333 #ifdef DEBUG
334 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
335 #endif
336 printf(" --debugwps \t Print advanced WPS debug info\n");
337 printf(" --nobackground \t Disable the background image\n");
338 #ifdef HAVE_REMOTE_LCD
339 printf(" --noremote \t Disable the remote image (will disable backgrounds)\n");
340 #endif
341 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
342 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
343 printf(" --alarm \t Simulate a wake-up on alarm\n");
344 printf(" --root [DIR]\t Set root directory\n");
345 printf(" --mapping \t Output coordinates and radius for mapping backgrounds\n");
346 exit(0);
351 if (display_zoom > 1) {
352 background = false;
355 if (!sim_kernel_init()) {
356 fprintf(stderr, "sim_kernel_init failed\n");
357 return -1;
360 if (!gui_startup()) {
361 fprintf(stderr, "gui_startup failed\n");
362 return -1;
365 /* app_main will be called by the new main thread */
366 if (!thread_sdl_init(NULL)) {
367 fprintf(stderr, "thread_sdl_init failed\n");
368 return -1;
371 gui_message_loop();
373 return gui_shutdown();