1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include <SDL_thread.h>
28 #include "thread-sdl.h"
29 #include "system-sdl.h"
30 #include "sim-ui-defines.h"
32 #ifdef HAVE_LCD_BITMAP
33 #include "lcd-bitmap.h"
34 #elif defined(HAVE_LCD_CHARCELLS)
35 #include "lcd-charcells.h"
37 #ifdef HAVE_REMOTE_LCD
38 #include "lcd-remote-bitmap.h"
43 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
45 #include <glib-object.h>
46 #include "maemo-thread.h"
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 */
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
;
67 bool debug_audio
= false;
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
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
;
89 SDL_Surface
*picture_surface
= NULL
;
94 /* Try and load the background image. If it fails go without */
96 picture_surface
= SDL_LoadBMP("UI256.bmp");
97 if (picture_surface
== NULL
) {
99 DEBUGF("warn: %s\n", SDL_GetError());
111 #ifdef HAVE_REMOTE_LCD
114 width
= SIM_LCD_WIDTH
> SIM_REMOTE_WIDTH
? SIM_LCD_WIDTH
: SIM_REMOTE_WIDTH
;
115 height
= SIM_LCD_HEIGHT
+ SIM_REMOTE_HEIGHT
;
120 width
= SIM_LCD_WIDTH
;
121 height
= SIM_LCD_HEIGHT
;
129 flags
= SDL_HWSURFACE
|SDL_DOUBLEBUF
;
130 #if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
131 /* Fullscreen mode for maemo app */
132 flags
|= SDL_FULLSCREEN
;
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
);
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
);
166 * finally enter the button 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();
178 g_main_loop_quit (maemo_main_loop
);
179 g_main_loop_unref(maemo_main_loop
);
180 SDL_WaitThread(maemo_thread
, NULL
);
183 #if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
184 SDL_FreeCursor(hiddenCursor
);
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 */
200 /* Shut down SDL event loop */
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();
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 */
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 */
226 sim_kernel_shutdown();
232 uintptr_t *stackbegin
;
234 void system_init(void)
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 */
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 */
257 SDL_DestroySemaphore(s
);
261 void system_reboot(void)
263 #ifdef HAVE_SDL_THREADS
264 sim_thread_exception_wait();
270 void system_exception_wait(void)
275 void sys_handle_argv(int argc
, char *argv
[])
280 for (x
= 1; x
< argc
; x
++)
283 if (!strcmp("--debugaudio", argv
[x
]))
286 printf("Writing debug audio file.\n");
290 if (!strcmp("--debugwps", argv
[x
]))
293 printf("WPS debug mode enabled.\n");
295 else if (!strcmp("--nobackground", argv
[x
]))
298 printf("Disabling background image.\n");
300 #ifdef HAVE_REMOTE_LCD
301 else if (!strcmp("--noremote", argv
[x
]))
305 printf("Disabling remote image.\n");
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
]))
317 display_zoom
=atof(argv
[x
]);
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
]))
332 sim_root_dir
= argv
[x
];
333 printf("Root directory: %s\n", sim_root_dir
);
336 else if (!strcmp("--mapping", argv
[x
]))
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");
348 printf("rockboxui\n");
349 printf("Arguments:\n");
351 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
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");
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");
367 if (display_zoom
!= 1) {