brackets are important! fix the mouse being hidden in sims
[maemo-rb.git] / firmware / target / hosted / sdl / system-sdl.c
blob0b487d0c2b897b3e9046456a9b2ae451f664d94e
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;
63 extern int display_zoom;
65 static SDL_Thread *evt_thread = NULL;
67 #ifdef DEBUG
68 bool debug_audio = false;
69 #endif
71 bool debug_wps = false;
72 int wps_verbose_level = 3;
74 void sys_poweroff(void)
76 /* Post SYS_POWEROFF event. Will post SDL_USEREVENT in shutdown_hw() if successful. */
77 queue_broadcast(SYS_POWEROFF, 0);
81 * This thread will read the buttons in an interrupt like fashion, and
82 * also initializes SDL_INIT_VIDEO and the surfaces
84 * it must be done in the same thread (at least on windows) because events only
85 * work in the thread which called SDL_Init(SubSystem) with SDL_INIT_VIDEO
87 * This is an SDL thread and relies on preemptive behavoir of the host
88 **/
89 static int sdl_event_thread(void * param)
91 SDL_InitSubSystem(SDL_INIT_VIDEO);
93 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
94 SDL_sem *wait_for_maemo_startup;
95 #endif
96 SDL_Surface *picture_surface = NULL;
97 int width, height;
98 int depth;
99 Uint32 flags;
101 /* Try and load the background image. If it fails go without */
102 if (background) {
103 picture_surface = SDL_LoadBMP("UI256.bmp");
104 if (picture_surface == NULL) {
105 background = false;
106 DEBUGF("warn: %s\n", SDL_GetError());
110 /* Set things up */
111 if (background)
113 width = UI_WIDTH;
114 height = UI_HEIGHT;
116 else
118 #ifdef HAVE_REMOTE_LCD
119 if (showremote)
121 width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH;
122 height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT;
124 else
125 #endif
127 width = SIM_LCD_WIDTH;
128 height = SIM_LCD_HEIGHT;
132 depth = LCD_DEPTH;
133 if (depth < 8)
134 depth = 16;
136 flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
137 #if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
138 /* Fullscreen mode for maemo app */
139 flags |= SDL_FULLSCREEN;
140 #endif
142 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, depth, flags)) == NULL) {
143 panicf("%s", SDL_GetError());
146 #if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
147 /* Hide mouse cursor on real touchscreen device */
148 SDL_ShowCursor(SDL_DISABLE);
149 #endif
151 SDL_WM_SetCaption(UI_TITLE, NULL);
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(picture_surface)
184 SDL_FreeSurface(picture_surface);
186 /* Order here is relevent to prevent deadlocks and use of destroyed
187 sync primitives by kernel threads */
188 #ifdef HAVE_SDL_THREADS
189 sim_thread_shutdown(); /* not needed for native threads */
190 #endif
191 return 0;
194 void shutdown_hw(void)
196 /* Shut down SDL event loop */
197 SDL_Event event;
198 memset(&event, 0, sizeof(SDL_Event));
199 event.type = SDL_USEREVENT;
200 SDL_PushEvent(&event);
201 #ifdef HAVE_SDL_THREADS
202 /* since sim_thread_shutdown() grabs the mutex we need to let it free,
203 * otherwise SDL_WaitThread will deadlock */
204 struct thread_entry* t = sim_thread_unlock();
205 #endif
206 /* wait for event thread to finish */
207 SDL_WaitThread(evt_thread, NULL);
209 #ifdef HAVE_SDL_THREADS
210 /* lock again before entering the scheduler */
211 sim_thread_lock(t);
212 /* sim_thread_shutdown() will cause sim_do_exit() to be called via longjmp,
213 * but only if we let the sdl thread scheduler exit the other threads */
214 while(1) yield();
215 #else
216 sim_do_exit();
217 #endif
220 void sim_do_exit()
222 sim_kernel_shutdown();
224 SDL_Quit();
225 exit(EXIT_SUCCESS);
228 uintptr_t *stackbegin;
229 uintptr_t *stackend;
230 void system_init(void)
232 SDL_sem *s;
233 /* fake stack, OS manages size (and growth) */
234 stackbegin = stackend = (uintptr_t*)&s;
236 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
237 /* Make glib thread safe */
238 g_thread_init(NULL);
239 g_type_init();
240 #endif
242 if (SDL_Init(SDL_INIT_TIMER))
243 panicf("%s", SDL_GetError());
245 s = SDL_CreateSemaphore(0); /* 0-count so it blocks */
247 evt_thread = SDL_CreateThread(sdl_event_thread, s);
249 /* wait for sdl_event_thread to run so that it can initialize the surfaces
250 * and video subsystem needed for SDL events */
251 SDL_SemWait(s);
252 /* cleanup */
253 SDL_DestroySemaphore(s);
257 void system_reboot(void)
259 #ifdef HAVE_SDL_THREADS
260 sim_thread_exception_wait();
261 #else
262 sim_do_exit();
263 #endif
266 void system_exception_wait(void)
268 system_reboot();
271 void sys_handle_argv(int argc, char *argv[])
273 if (argc >= 1)
275 int x;
276 for (x = 1; x < argc; x++)
278 #ifdef DEBUG
279 if (!strcmp("--debugaudio", argv[x]))
281 debug_audio = true;
282 printf("Writing debug audio file.\n");
284 else
285 #endif
286 if (!strcmp("--debugwps", argv[x]))
288 debug_wps = true;
289 printf("WPS debug mode enabled.\n");
291 else if (!strcmp("--nobackground", argv[x]))
293 background = false;
294 printf("Disabling background image.\n");
296 #ifdef HAVE_REMOTE_LCD
297 else if (!strcmp("--noremote", argv[x]))
299 showremote = false;
300 background = false;
301 printf("Disabling remote image.\n");
303 #endif
304 else if (!strcmp("--old_lcd", argv[x]))
306 having_new_lcd = false;
307 printf("Using old LCD layout.\n");
309 else if (!strcmp("--zoom", argv[x]))
311 x++;
312 if(x < argc)
313 display_zoom=atoi(argv[x]);
314 else
315 display_zoom = 2;
316 printf("Window zoom is %d\n", display_zoom);
318 else if (!strcmp("--alarm", argv[x]))
320 sim_alarm_wakeup = true;
321 printf("Simulating alarm wakeup.\n");
323 else if (!strcmp("--root", argv[x]))
325 x++;
326 if (x < argc)
328 sim_root_dir = argv[x];
329 printf("Root directory: %s\n", sim_root_dir);
332 else if (!strcmp("--mapping", argv[x]))
334 mapping = true;
335 printf("Printing click coords with drag radii.\n");
337 else if (!strcmp("--debugbuttons", argv[x]))
339 debug_buttons = true;
340 printf("Printing background button clicks.\n");
342 else
344 printf("rockboxui\n");
345 printf("Arguments:\n");
346 #ifdef DEBUG
347 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
348 #endif
349 printf(" --debugwps \t Print advanced WPS debug info\n");
350 printf(" --nobackground \t Disable the background image\n");
351 #ifdef HAVE_REMOTE_LCD
352 printf(" --noremote \t Disable the remote image (will disable backgrounds)\n");
353 #endif
354 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
355 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
356 printf(" --alarm \t Simulate a wake-up on alarm\n");
357 printf(" --root [DIR]\t Set root directory\n");
358 printf(" --mapping \t Output coordinates and radius for mapping backgrounds\n");
359 exit(0);
363 if (display_zoom > 1) {
364 background = false;