Fix FS#11280
[kugel-rb.git] / firmware / target / hosted / sdl / system-sdl.c
blob3d67de425bf2d8c2a9ff99ade2f58da56bc30621
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 <SDL.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <setjmp.h>
26 #include "system-sdl.h"
27 #include "thread-sdl.h"
28 #include "sim-ui-defines.h"
29 #include "lcd-sdl.h"
30 #ifdef HAVE_LCD_BITMAP
31 #include "lcd-bitmap.h"
32 #elif defined(HAVE_LCD_CHARCELLS)
33 #include "lcd-charcells.h"
34 #endif
35 #ifdef HAVE_REMOTE_LCD
36 #include "lcd-remote-bitmap.h"
37 #endif
38 #include "panic.h"
39 #include "debug.h"
41 SDL_Surface *gui_surface;
43 bool background = true; /* use backgrounds by default */
44 #ifdef HAVE_REMOTE_LCD
45 bool showremote = true; /* include remote by default */
46 #endif
47 bool mapping = false;
48 bool debug_buttons = false;
50 bool lcd_display_redraw = true; /* Used for player simulator */
51 char having_new_lcd = true; /* Used for player simulator */
52 bool sim_alarm_wakeup = false;
53 const char *sim_root_dir = NULL;
54 extern int display_zoom;
56 #ifdef DEBUG
57 bool debug_audio = false;
58 #endif
60 bool debug_wps = false;
61 int wps_verbose_level = 3;
64 void sys_poweroff(void)
66 /* Order here is relevent to prevent deadlocks and use of destroyed
67 sync primitives by kernel threads */
68 sim_thread_shutdown();
69 sim_kernel_shutdown();
70 SDL_Quit();
74 * Button read loop */
75 void gui_message_loop(void);
78 * This callback let's the main thread run again after SDL has been initialized
79 **/
80 static uint32_t cond_signal(uint32_t interval, void *param)
82 (void)interval;
83 SDL_cond *c = (SDL_cond*)param;
84 /* remove timer, CondSignal returns 0 on success */
85 return SDL_CondSignal(c);
89 * This thread will read the buttons in an interrupt like fashion, and
90 * also initializes SDL_INIT_VIDEO and the surfaces
92 * it must be done in the same thread (at least on windows) because events only
93 * work in the thread which called SDL_Init(SubSystem) with SDL_INIT_VIDEO
95 * This is an SDL thread and relies on preemptive behavoir of the host
96 **/
97 static int sdl_event_thread(void * param)
99 SDL_InitSubSystem(SDL_INIT_VIDEO);
101 SDL_Surface *picture_surface;
102 int width, height;
104 /* Try and load the background image. If it fails go without */
105 if (background) {
106 picture_surface = SDL_LoadBMP("UI256.bmp");
107 if (picture_surface == NULL) {
108 background = false;
109 DEBUGF("warn: %s\n", SDL_GetError());
113 /* Set things up */
114 if (background)
116 width = UI_WIDTH;
117 height = UI_HEIGHT;
119 else
121 #ifdef HAVE_REMOTE_LCD
122 if (showremote)
124 width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH;
125 height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT;
127 else
128 #endif
130 width = SIM_LCD_WIDTH;
131 height = SIM_LCD_HEIGHT;
136 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
137 panicf("%s", SDL_GetError());
140 SDL_WM_SetCaption(UI_TITLE, NULL);
142 sim_lcd_init();
143 #ifdef HAVE_REMOTE_LCD
144 if (showremote)
145 sim_lcd_remote_init();
146 #endif
148 if (background && picture_surface != NULL)
149 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
151 /* calling SDL_CondSignal() right away here doesn't work reliably so
152 * post-pone it a bit */
153 SDL_AddTimer(100, cond_signal, param);
155 * finally enter the button loop */
156 while(1)
157 gui_message_loop();
159 return 0;
163 void system_init(void)
165 SDL_cond *c;
166 SDL_mutex *m;
167 if (SDL_Init(SDL_INIT_TIMER))
168 panicf("%s", SDL_GetError());
169 atexit(SDL_Quit);
171 c = SDL_CreateCond();
172 m = SDL_CreateMutex();
174 SDL_CreateThread(sdl_event_thread, c);
176 /* Lock mutex and wait for sdl_event_thread to run so that it can
177 * initialize the surfaces and video subsystem needed for SDL events */
178 SDL_LockMutex(m);
179 SDL_CondWait(c, m);
180 SDL_UnlockMutex(m);
182 /* cleanup */
183 SDL_DestroyCond(c);
184 SDL_DestroyMutex(m);
187 void system_exception_wait(void)
189 sim_thread_exception_wait();
192 void system_reboot(void)
194 sim_thread_exception_wait();
198 void sys_handle_argv(int argc, char *argv[])
200 if (argc >= 1)
202 int x;
203 for (x = 1; x < argc; x++)
205 #ifdef DEBUG
206 if (!strcmp("--debugaudio", argv[x]))
208 debug_audio = true;
209 printf("Writing debug audio file.\n");
211 else
212 #endif
213 if (!strcmp("--debugwps", argv[x]))
215 debug_wps = true;
216 printf("WPS debug mode enabled.\n");
218 else if (!strcmp("--nobackground", argv[x]))
220 background = false;
221 printf("Disabling background image.\n");
223 #ifdef HAVE_REMOTE_LCD
224 else if (!strcmp("--noremote", argv[x]))
226 showremote = false;
227 background = false;
228 printf("Disabling remote image.\n");
230 #endif
231 else if (!strcmp("--old_lcd", argv[x]))
233 having_new_lcd = false;
234 printf("Using old LCD layout.\n");
236 else if (!strcmp("--zoom", argv[x]))
238 x++;
239 if(x < argc)
240 display_zoom=atoi(argv[x]);
241 else
242 display_zoom = 2;
243 printf("Window zoom is %d\n", display_zoom);
245 else if (!strcmp("--alarm", argv[x]))
247 sim_alarm_wakeup = true;
248 printf("Simulating alarm wakeup.\n");
250 else if (!strcmp("--root", argv[x]))
252 x++;
253 if (x < argc)
255 sim_root_dir = argv[x];
256 printf("Root directory: %s\n", sim_root_dir);
259 else if (!strcmp("--mapping", argv[x]))
261 mapping = true;
262 printf("Printing click coords with drag radii.\n");
264 else if (!strcmp("--debugbuttons", argv[x]))
266 debug_buttons = true;
267 printf("Printing background button clicks.\n");
269 else
271 printf("rockboxui\n");
272 printf("Arguments:\n");
273 #ifdef DEBUG
274 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
275 #endif
276 printf(" --debugwps \t Print advanced WPS debug info\n");
277 printf(" --nobackground \t Disable the background image\n");
278 #ifdef HAVE_REMOTE_LCD
279 printf(" --noremote \t Disable the remote image (will disable backgrounds)\n");
280 #endif
281 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
282 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
283 printf(" --alarm \t Simulate a wake-up on alarm\n");
284 printf(" --root [DIR]\t Set root directory\n");
285 printf(" --mapping \t Output coordinates and radius for mapping backgrounds\n");
286 exit(0);
290 if (display_zoom > 1) {
291 background = false;