A bit further cleanup in system-sdl.c.
[kugel-rb.git] / firmware / target / hosted / sdl / system-sdl.c
blob9d529de37f7061e5f8a76591ca13e303e2a1d0f3
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 <SDL_thread.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include "system.h"
28 #include "thread-sdl.h"
29 #include "sim-ui-defines.h"
30 #include "lcd-sdl.h"
31 #ifdef HAVE_LCD_BITMAP
32 #include "lcd-bitmap.h"
33 #elif defined(HAVE_LCD_CHARCELLS)
34 #include "lcd-charcells.h"
35 #endif
36 #ifdef HAVE_REMOTE_LCD
37 #include "lcd-remote-bitmap.h"
38 #endif
39 #include "panic.h"
40 #include "debug.h"
42 SDL_Surface *gui_surface;
44 bool background = true; /* use backgrounds by default */
45 #ifdef HAVE_REMOTE_LCD
46 bool showremote = true; /* include remote by default */
47 #endif
48 bool mapping = false;
49 bool debug_buttons = false;
51 bool lcd_display_redraw = true; /* Used for player simulator */
52 char having_new_lcd = true; /* Used for player simulator */
53 bool sim_alarm_wakeup = false;
54 const char *sim_root_dir = NULL;
55 extern int display_zoom;
57 #ifdef DEBUG
58 bool debug_audio = false;
59 #endif
61 bool debug_wps = false;
62 int wps_verbose_level = 3;
65 void sys_poweroff(void)
67 /* Order here is relevent to prevent deadlocks and use of destroyed
68 sync primitives by kernel threads */
69 sim_thread_shutdown();
70 sim_kernel_shutdown();
71 SDL_Quit();
75 * Button read loop */
76 void gui_message_loop(void);
79 * This callback let's the main thread run again after SDL has been initialized
80 **/
81 static uint32_t cond_signal(uint32_t interval, void *param)
83 (void)interval;
84 SDL_cond *c = (SDL_cond*)param;
85 /* remove timer, CondSignal returns 0 on success */
86 return SDL_CondSignal(c);
90 * This thread will read the buttons in an interrupt like fashion, and
91 * also initializes SDL_INIT_VIDEO and the surfaces
93 * it must be done in the same thread (at least on windows) because events only
94 * work in the thread which called SDL_Init(SubSystem) with SDL_INIT_VIDEO
96 * This is an SDL thread and relies on preemptive behavoir of the host
97 **/
98 static int sdl_event_thread(void * param)
100 SDL_InitSubSystem(SDL_INIT_VIDEO);
102 SDL_Surface *picture_surface;
103 int width, height;
105 /* Try and load the background image. If it fails go without */
106 if (background) {
107 picture_surface = SDL_LoadBMP("UI256.bmp");
108 if (picture_surface == NULL) {
109 background = false;
110 DEBUGF("warn: %s\n", SDL_GetError());
114 /* Set things up */
115 if (background)
117 width = UI_WIDTH;
118 height = UI_HEIGHT;
120 else
122 #ifdef HAVE_REMOTE_LCD
123 if (showremote)
125 width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH;
126 height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT;
128 else
129 #endif
131 width = SIM_LCD_WIDTH;
132 height = SIM_LCD_HEIGHT;
137 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
138 panicf("%s", SDL_GetError());
141 SDL_WM_SetCaption(UI_TITLE, NULL);
143 sim_lcd_init();
144 #ifdef HAVE_REMOTE_LCD
145 if (showremote)
146 sim_lcd_remote_init();
147 #endif
149 if (background && picture_surface != NULL)
150 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
152 /* calling SDL_CondSignal() right away here doesn't work reliably so
153 * post-pone it a bit */
154 SDL_AddTimer(100, cond_signal, param);
156 * finally enter the button loop */
157 while(1)
158 gui_message_loop();
160 return 0;
164 void system_init(void)
166 SDL_cond *c;
167 SDL_mutex *m;
168 if (SDL_Init(SDL_INIT_TIMER))
169 panicf("%s", SDL_GetError());
170 atexit(sys_poweroff);
172 c = SDL_CreateCond();
173 m = SDL_CreateMutex();
175 SDL_CreateThread(sdl_event_thread, c);
177 /* Lock mutex and wait for sdl_event_thread to run so that it can
178 * initialize the surfaces and video subsystem needed for SDL events */
179 SDL_LockMutex(m);
180 SDL_CondWait(c, m);
181 SDL_UnlockMutex(m);
183 /* cleanup */
184 SDL_DestroyCond(c);
185 SDL_DestroyMutex(m);
188 void system_exception_wait(void)
190 sim_thread_exception_wait();
193 void system_reboot(void)
195 sim_thread_exception_wait();
199 void sys_handle_argv(int argc, char *argv[])
201 if (argc >= 1)
203 int x;
204 for (x = 1; x < argc; x++)
206 #ifdef DEBUG
207 if (!strcmp("--debugaudio", argv[x]))
209 debug_audio = true;
210 printf("Writing debug audio file.\n");
212 else
213 #endif
214 if (!strcmp("--debugwps", argv[x]))
216 debug_wps = true;
217 printf("WPS debug mode enabled.\n");
219 else if (!strcmp("--nobackground", argv[x]))
221 background = false;
222 printf("Disabling background image.\n");
224 #ifdef HAVE_REMOTE_LCD
225 else if (!strcmp("--noremote", argv[x]))
227 showremote = false;
228 background = false;
229 printf("Disabling remote image.\n");
231 #endif
232 else if (!strcmp("--old_lcd", argv[x]))
234 having_new_lcd = false;
235 printf("Using old LCD layout.\n");
237 else if (!strcmp("--zoom", argv[x]))
239 x++;
240 if(x < argc)
241 display_zoom=atoi(argv[x]);
242 else
243 display_zoom = 2;
244 printf("Window zoom is %d\n", display_zoom);
246 else if (!strcmp("--alarm", argv[x]))
248 sim_alarm_wakeup = true;
249 printf("Simulating alarm wakeup.\n");
251 else if (!strcmp("--root", argv[x]))
253 x++;
254 if (x < argc)
256 sim_root_dir = argv[x];
257 printf("Root directory: %s\n", sim_root_dir);
260 else if (!strcmp("--mapping", argv[x]))
262 mapping = true;
263 printf("Printing click coords with drag radii.\n");
265 else if (!strcmp("--debugbuttons", argv[x]))
267 debug_buttons = true;
268 printf("Printing background button clicks.\n");
270 else
272 printf("rockboxui\n");
273 printf("Arguments:\n");
274 #ifdef DEBUG
275 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
276 #endif
277 printf(" --debugwps \t Print advanced WPS debug info\n");
278 printf(" --nobackground \t Disable the background image\n");
279 #ifdef HAVE_REMOTE_LCD
280 printf(" --noremote \t Disable the remote image (will disable backgrounds)\n");
281 #endif
282 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
283 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
284 printf(" --alarm \t Simulate a wake-up on alarm\n");
285 printf(" --root [DIR]\t Set root directory\n");
286 printf(" --mapping \t Output coordinates and radius for mapping backgrounds\n");
287 exit(0);
291 if (display_zoom > 1) {
292 background = false;