Run Pandora port fullscreen
[maemo-rb.git] / firmware / target / hosted / sdl / system-sdl.c
blobb22266ae31592805572faac793e9d0c6f3329e9b
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;
75 void sys_poweroff(void)
80 * This thread will read the buttons in an interrupt like fashion, and
81 * also initializes SDL_INIT_VIDEO and the surfaces
83 * it must be done in the same thread (at least on windows) because events only
84 * work in the thread which called SDL_Init(SubSystem) with SDL_INIT_VIDEO
86 * This is an SDL thread and relies on preemptive behavoir of the host
87 **/
88 static int sdl_event_thread(void * param)
90 SDL_InitSubSystem(SDL_INIT_VIDEO);
92 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
93 SDL_sem *wait_for_maemo_startup;
94 #endif
95 SDL_Surface *picture_surface = NULL;
96 int width, height;
97 int depth;
98 Uint32 flags;
100 /* Try and load the background image. If it fails go without */
101 if (background) {
102 picture_surface = SDL_LoadBMP("UI256.bmp");
103 if (picture_surface == NULL) {
104 background = false;
105 DEBUGF("warn: %s\n", SDL_GetError());
109 /* Set things up */
110 if (background)
112 width = UI_WIDTH;
113 height = UI_HEIGHT;
115 else
117 #ifdef HAVE_REMOTE_LCD
118 if (showremote)
120 width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH;
121 height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT;
123 else
124 #endif
126 width = SIM_LCD_WIDTH;
127 height = SIM_LCD_HEIGHT;
131 depth = LCD_DEPTH;
132 if (depth < 8)
133 depth = 16;
135 flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
136 #if (CONFIG_PLATFORM & PLATFORM_MAEMO|PLATFORM_PANDORA)
137 /* Fullscreen mode for maemo app */
138 flags |= SDL_FULLSCREEN;
139 #endif
141 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, depth, flags)) == NULL) {
142 panicf("%s", SDL_GetError());
145 #if (CONFIG_PLATFORM & PLATFORM_MAEMO|PLATFORM_PANDORA)
146 /* Hide mouse cursor on real touchscreen device */
147 SDL_ShowCursor(SDL_DISABLE);
148 #endif
150 SDL_WM_SetCaption(UI_TITLE, NULL);
152 if (background && picture_surface != NULL)
153 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
155 /* let system_init proceed */
156 SDL_SemPost((SDL_sem *)param);
158 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
159 /* Start maemo thread: Listen to display on/off events and battery monitoring */
160 wait_for_maemo_startup = SDL_CreateSemaphore(0); /* 0-count so it blocks */
161 SDL_Thread *maemo_thread = SDL_CreateThread(maemo_thread_func, wait_for_maemo_startup);
162 #endif
165 * finally enter the button loop */
166 gui_message_loop();
168 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
169 /* Ensure maemo thread is up and running */
170 SDL_SemWait(wait_for_maemo_startup);
171 SDL_DestroySemaphore(wait_for_maemo_startup);
173 #if (CONFIG_PLATFORM & PLATFORM_MAEMO5)
174 pcm_shutdown_gstreamer();
175 #endif
177 g_main_loop_quit (maemo_main_loop);
178 g_main_loop_unref(maemo_main_loop);
179 SDL_WaitThread(maemo_thread, NULL);
180 #endif
182 if(picture_surface)
183 SDL_FreeSurface(picture_surface);
185 /* Order here is relevent to prevent deadlocks and use of destroyed
186 sync primitives by kernel threads */
187 #ifdef HAVE_SDL_THREADS
188 sim_thread_shutdown(); /* not needed for native threads */
189 #endif
190 sim_kernel_shutdown();
192 return 0;
195 void sim_do_exit(void)
197 /* wait for event thread to finish */
198 SDL_WaitThread(evt_thread, NULL);
200 SDL_Quit();
201 exit(EXIT_SUCCESS);
204 uintptr_t *stackbegin;
205 uintptr_t *stackend;
206 void system_init(void)
208 SDL_sem *s;
209 /* fake stack, OS manages size (and growth) */
210 stackbegin = stackend = (uintptr_t*)&s;
212 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
213 /* Make glib thread safe */
214 g_thread_init(NULL);
215 g_type_init();
216 #endif
218 if (SDL_Init(SDL_INIT_TIMER))
219 panicf("%s", SDL_GetError());
221 s = SDL_CreateSemaphore(0); /* 0-count so it blocks */
223 evt_thread = SDL_CreateThread(sdl_event_thread, s);
225 /* wait for sdl_event_thread to run so that it can initialize the surfaces
226 * and video subsystem needed for SDL events */
227 SDL_SemWait(s);
228 /* cleanup */
229 SDL_DestroySemaphore(s);
233 void system_reboot(void)
235 #ifdef HAVE_SDL_THREADS
236 sim_thread_exception_wait();
237 #else
238 sim_do_exit();
239 #endif
242 void system_exception_wait(void)
244 system_reboot();
247 void sys_handle_argv(int argc, char *argv[])
249 if (argc >= 1)
251 int x;
252 for (x = 1; x < argc; x++)
254 #ifdef DEBUG
255 if (!strcmp("--debugaudio", argv[x]))
257 debug_audio = true;
258 printf("Writing debug audio file.\n");
260 else
261 #endif
262 if (!strcmp("--debugwps", argv[x]))
264 debug_wps = true;
265 printf("WPS debug mode enabled.\n");
267 else if (!strcmp("--nobackground", argv[x]))
269 background = false;
270 printf("Disabling background image.\n");
272 #ifdef HAVE_REMOTE_LCD
273 else if (!strcmp("--noremote", argv[x]))
275 showremote = false;
276 background = false;
277 printf("Disabling remote image.\n");
279 #endif
280 else if (!strcmp("--old_lcd", argv[x]))
282 having_new_lcd = false;
283 printf("Using old LCD layout.\n");
285 else if (!strcmp("--zoom", argv[x]))
287 x++;
288 if(x < argc)
289 display_zoom=atoi(argv[x]);
290 else
291 display_zoom = 2;
292 printf("Window zoom is %d\n", display_zoom);
294 else if (!strcmp("--alarm", argv[x]))
296 sim_alarm_wakeup = true;
297 printf("Simulating alarm wakeup.\n");
299 else if (!strcmp("--root", argv[x]))
301 x++;
302 if (x < argc)
304 sim_root_dir = argv[x];
305 printf("Root directory: %s\n", sim_root_dir);
308 else if (!strcmp("--mapping", argv[x]))
310 mapping = true;
311 printf("Printing click coords with drag radii.\n");
313 else if (!strcmp("--debugbuttons", argv[x]))
315 debug_buttons = true;
316 printf("Printing background button clicks.\n");
318 else
320 printf("rockboxui\n");
321 printf("Arguments:\n");
322 #ifdef DEBUG
323 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
324 #endif
325 printf(" --debugwps \t Print advanced WPS debug info\n");
326 printf(" --nobackground \t Disable the background image\n");
327 #ifdef HAVE_REMOTE_LCD
328 printf(" --noremote \t Disable the remote image (will disable backgrounds)\n");
329 #endif
330 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
331 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
332 printf(" --alarm \t Simulate a wake-up on alarm\n");
333 printf(" --root [DIR]\t Set root directory\n");
334 printf(" --mapping \t Output coordinates and radius for mapping backgrounds\n");
335 exit(0);
339 if (display_zoom > 1) {
340 background = false;