Prepare new maemo release
[maemo-rb.git] / firmware / target / hosted / sdl / system-sdl.c
blobb03ed89d498ab30d4ad7801a08bd6947a7f2b755
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 "system-sdl.h"
30 #include "sim-ui-defines.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 "panic.h"
41 #include "debug.h"
43 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
44 #include <glib.h>
45 #include <glib-object.h>
46 #include "maemo-thread.h"
48 #endif
50 SDL_Surface *gui_surface;
52 bool background = true; /* use backgrounds by default */
53 #ifdef HAVE_REMOTE_LCD
54 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 static SDL_Thread *evt_thread = NULL;
66 #ifdef DEBUG
67 bool debug_audio = false;
68 #endif
70 bool debug_wps = false;
71 int wps_verbose_level = 3;
74 * This thread will read the buttons in an interrupt like fashion, and
75 * also initializes SDL_INIT_VIDEO and the surfaces
77 * it must be done in the same thread (at least on windows) because events only
78 * work in the thread which called SDL_Init(SubSystem) with SDL_INIT_VIDEO
80 * This is an SDL thread and relies on preemptive behavoir of the host
81 **/
82 static int sdl_event_thread(void * param)
84 SDL_InitSubSystem(SDL_INIT_VIDEO);
86 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
87 SDL_sem *wait_for_maemo_startup;
88 #endif
89 SDL_Surface *picture_surface = NULL;
90 int width, height;
91 int depth;
92 Uint32 flags;
94 /* Try and load the background image. If it fails go without */
95 if (background) {
96 picture_surface = SDL_LoadBMP("UI256.bmp");
97 if (picture_surface == NULL) {
98 background = false;
99 DEBUGF("warn: %s\n", SDL_GetError());
103 /* Set things up */
104 if (background)
106 width = UI_WIDTH;
107 height = UI_HEIGHT;
109 else
111 #ifdef HAVE_REMOTE_LCD
112 if (showremote)
114 width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH;
115 height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT;
117 else
118 #endif
120 width = SIM_LCD_WIDTH;
121 height = SIM_LCD_HEIGHT;
125 depth = LCD_DEPTH;
126 if (depth < 8)
127 depth = 16;
129 flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
130 #if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
131 /* Fullscreen mode for maemo app */
132 flags |= SDL_FULLSCREEN;
133 #endif
135 SDL_WM_SetCaption(UI_TITLE, NULL);
137 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, depth, flags)) == NULL) {
138 panicf("%s", SDL_GetError());
141 #if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
142 /* SDL touch screen fix: Work around a SDL assumption that returns
143 relative mouse coordinates when you get to the screen edges
144 using the touchscreen and a disabled mouse cursor.
146 uint8_t hiddenCursorData = 0;
147 SDL_Cursor *hiddenCursor = SDL_CreateCursor(&hiddenCursorData, &hiddenCursorData, 8, 1, 0, 0);
149 SDL_ShowCursor(SDL_ENABLE);
150 SDL_SetCursor(hiddenCursor);
151 #endif
153 if (background && picture_surface != NULL)
154 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
156 /* let system_init proceed */
157 SDL_SemPost((SDL_sem *)param);
159 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
160 /* Start maemo thread: Listen to display on/off events and battery monitoring */
161 wait_for_maemo_startup = SDL_CreateSemaphore(0); /* 0-count so it blocks */
162 SDL_Thread *maemo_thread = SDL_CreateThread(maemo_thread_func, wait_for_maemo_startup);
163 #endif
166 * finally enter the button loop */
167 gui_message_loop();
169 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
170 /* Ensure maemo thread is up and running */
171 SDL_SemWait(wait_for_maemo_startup);
172 SDL_DestroySemaphore(wait_for_maemo_startup);
174 #if (CONFIG_PLATFORM & PLATFORM_MAEMO5)
175 pcm_shutdown_gstreamer();
176 #endif
178 g_main_loop_quit (maemo_main_loop);
179 g_main_loop_unref(maemo_main_loop);
180 SDL_WaitThread(maemo_thread, NULL);
181 #endif
183 #if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
184 SDL_FreeCursor(hiddenCursor);
185 #endif
187 if(picture_surface)
188 SDL_FreeSurface(picture_surface);
190 /* Order here is relevent to prevent deadlocks and use of destroyed
191 sync primitives by kernel threads */
192 #ifdef HAVE_SDL_THREADS
193 sim_thread_shutdown(); /* not needed for native threads */
194 #endif
195 return 0;
198 void power_off(void)
200 /* Shut down SDL event loop */
201 SDL_Event event;
202 memset(&event, 0, sizeof(SDL_Event));
203 event.type = SDL_USEREVENT;
204 SDL_PushEvent(&event);
205 #ifdef HAVE_SDL_THREADS
206 /* since sim_thread_shutdown() grabs the mutex we need to let it free,
207 * otherwise SDL_WaitThread will deadlock */
208 struct thread_entry* t = sim_thread_unlock();
209 #endif
210 /* wait for event thread to finish */
211 SDL_WaitThread(evt_thread, NULL);
213 #ifdef HAVE_SDL_THREADS
214 /* lock again before entering the scheduler */
215 sim_thread_lock(t);
216 /* sim_thread_shutdown() will cause sim_do_exit() to be called via longjmp,
217 * but only if we let the sdl thread scheduler exit the other threads */
218 while(1) yield();
219 #else
220 sim_do_exit();
221 #endif
224 void sim_do_exit()
226 sim_kernel_shutdown();
228 SDL_Quit();
229 exit(EXIT_SUCCESS);
232 uintptr_t *stackbegin;
233 uintptr_t *stackend;
234 void system_init(void)
236 SDL_sem *s;
237 /* fake stack, OS manages size (and growth) */
238 stackbegin = stackend = (uintptr_t*)&s;
240 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
241 /* Make glib thread safe */
242 g_thread_init(NULL);
243 g_type_init();
244 #endif
246 if (SDL_Init(SDL_INIT_TIMER))
247 panicf("%s", SDL_GetError());
249 s = SDL_CreateSemaphore(0); /* 0-count so it blocks */
251 evt_thread = SDL_CreateThread(sdl_event_thread, s);
253 /* wait for sdl_event_thread to run so that it can initialize the surfaces
254 * and video subsystem needed for SDL events */
255 SDL_SemWait(s);
256 /* cleanup */
257 SDL_DestroySemaphore(s);
261 void system_reboot(void)
263 #ifdef HAVE_SDL_THREADS
264 sim_thread_exception_wait();
265 #else
266 sim_do_exit();
267 #endif
270 void system_exception_wait(void)
272 system_reboot();
275 void sys_handle_argv(int argc, char *argv[])
277 if (argc >= 1)
279 int x;
280 for (x = 1; x < argc; x++)
282 #ifdef DEBUG
283 if (!strcmp("--debugaudio", argv[x]))
285 debug_audio = true;
286 printf("Writing debug audio file.\n");
288 else
289 #endif
290 if (!strcmp("--debugwps", argv[x]))
292 debug_wps = true;
293 printf("WPS debug mode enabled.\n");
295 else if (!strcmp("--nobackground", argv[x]))
297 background = false;
298 printf("Disabling background image.\n");
300 #ifdef HAVE_REMOTE_LCD
301 else if (!strcmp("--noremote", argv[x]))
303 showremote = false;
304 background = false;
305 printf("Disabling remote image.\n");
307 #endif
308 else if (!strcmp("--old_lcd", argv[x]))
310 having_new_lcd = false;
311 printf("Using old LCD layout.\n");
313 else if (!strcmp("--zoom", argv[x]))
315 x++;
316 if(x < argc)
317 display_zoom=atof(argv[x]);
318 else
319 display_zoom = 2;
320 printf("Window zoom is %d\n", display_zoom);
322 else if (!strcmp("--alarm", argv[x]))
324 sim_alarm_wakeup = true;
325 printf("Simulating alarm wakeup.\n");
327 else if (!strcmp("--root", argv[x]))
329 x++;
330 if (x < argc)
332 sim_root_dir = argv[x];
333 printf("Root directory: %s\n", sim_root_dir);
336 else if (!strcmp("--mapping", argv[x]))
338 mapping = true;
339 printf("Printing click coords with drag radii.\n");
341 else if (!strcmp("--debugbuttons", argv[x]))
343 debug_buttons = true;
344 printf("Printing background button clicks.\n");
346 else
348 printf("rockboxui\n");
349 printf("Arguments:\n");
350 #ifdef DEBUG
351 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
352 #endif
353 printf(" --debugwps \t Print advanced WPS debug info\n");
354 printf(" --nobackground \t Disable the background image\n");
355 #ifdef HAVE_REMOTE_LCD
356 printf(" --noremote \t Disable the remote image (will disable backgrounds)\n");
357 #endif
358 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
359 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
360 printf(" --alarm \t Simulate a wake-up on alarm\n");
361 printf(" --root [DIR]\t Set root directory\n");
362 printf(" --mapping \t Output coordinates and radius for mapping backgrounds\n");
363 exit(0);
367 if (display_zoom != 1) {
368 background = false;