Fix SDL touch screen handling on the screen edges
[maemo-rb.git] / firmware / target / hosted / sdl / system-sdl.c
blob93a8a7cb66cbd88b2c9adc8814fdf913928e97ae
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 * This thread will read the buttons in an interrupt like fashion, and
76 * also initializes SDL_INIT_VIDEO and the surfaces
78 * it must be done in the same thread (at least on windows) because events only
79 * work in the thread which called SDL_Init(SubSystem) with SDL_INIT_VIDEO
81 * This is an SDL thread and relies on preemptive behavoir of the host
82 **/
83 static int sdl_event_thread(void * param)
85 SDL_InitSubSystem(SDL_INIT_VIDEO);
87 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
88 SDL_sem *wait_for_maemo_startup;
89 #endif
90 SDL_Surface *picture_surface = NULL;
91 int width, height;
92 int depth;
93 Uint32 flags;
95 /* Try and load the background image. If it fails go without */
96 if (background) {
97 picture_surface = SDL_LoadBMP("UI256.bmp");
98 if (picture_surface == NULL) {
99 background = false;
100 DEBUGF("warn: %s\n", SDL_GetError());
104 /* Set things up */
105 if (background)
107 width = UI_WIDTH;
108 height = UI_HEIGHT;
110 else
112 #ifdef HAVE_REMOTE_LCD
113 if (showremote)
115 width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH;
116 height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT;
118 else
119 #endif
121 width = SIM_LCD_WIDTH;
122 height = SIM_LCD_HEIGHT;
126 depth = LCD_DEPTH;
127 if (depth < 8)
128 depth = 16;
130 flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
131 #if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
132 /* Fullscreen mode for maemo app */
133 flags |= SDL_FULLSCREEN;
134 #endif
136 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, depth, flags)) == NULL) {
137 panicf("%s", SDL_GetError());
140 #if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
141 /* SDL touch screen fix: Work around a SDL assumption that returns
142 relative mouse coordinates when you get to the screen edges
143 using the touchscreen and a disabled mouse cursor.
145 uint8_t hiddenCursorData = 0;
146 SDL_Cursor *hiddenCursor = SDL_CreateCursor(&hiddenCursorData, &hiddenCursorData, 8, 1, 0, 0);
148 SDL_ShowCursor(SDL_ENABLE);
149 SDL_SetCursor(hiddenCursor);
150 #endif
152 SDL_WM_SetCaption(UI_TITLE, NULL);
154 if (background && picture_surface != NULL)
155 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
157 /* let system_init proceed */
158 SDL_SemPost((SDL_sem *)param);
160 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
161 /* Start maemo thread: Listen to display on/off events and battery monitoring */
162 wait_for_maemo_startup = SDL_CreateSemaphore(0); /* 0-count so it blocks */
163 SDL_Thread *maemo_thread = SDL_CreateThread(maemo_thread_func, wait_for_maemo_startup);
164 #endif
167 * finally enter the button loop */
168 gui_message_loop();
170 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
171 /* Ensure maemo thread is up and running */
172 SDL_SemWait(wait_for_maemo_startup);
173 SDL_DestroySemaphore(wait_for_maemo_startup);
175 #if (CONFIG_PLATFORM & PLATFORM_MAEMO5)
176 pcm_shutdown_gstreamer();
177 #endif
179 g_main_loop_quit (maemo_main_loop);
180 g_main_loop_unref(maemo_main_loop);
181 SDL_WaitThread(maemo_thread, NULL);
182 #endif
184 #if (CONFIG_PLATFORM & (PLATFORM_MAEMO|PLATFORM_PANDORA))
185 SDL_FreeCursor(hiddenCursor);
186 #endif
188 if(picture_surface)
189 SDL_FreeSurface(picture_surface);
191 /* Order here is relevent to prevent deadlocks and use of destroyed
192 sync primitives by kernel threads */
193 #ifdef HAVE_SDL_THREADS
194 sim_thread_shutdown(); /* not needed for native threads */
195 #endif
196 return 0;
199 void shutdown_hw(void)
201 /* Shut down SDL event loop */
202 SDL_Event event;
203 memset(&event, 0, sizeof(SDL_Event));
204 event.type = SDL_USEREVENT;
205 SDL_PushEvent(&event);
206 #ifdef HAVE_SDL_THREADS
207 /* since sim_thread_shutdown() grabs the mutex we need to let it free,
208 * otherwise SDL_WaitThread will deadlock */
209 struct thread_entry* t = sim_thread_unlock();
210 #endif
211 /* wait for event thread to finish */
212 SDL_WaitThread(evt_thread, NULL);
214 #ifdef HAVE_SDL_THREADS
215 /* lock again before entering the scheduler */
216 sim_thread_lock(t);
217 /* sim_thread_shutdown() will cause sim_do_exit() to be called via longjmp,
218 * but only if we let the sdl thread scheduler exit the other threads */
219 while(1) yield();
220 #else
221 sim_do_exit();
222 #endif
225 void sim_do_exit()
227 sim_kernel_shutdown();
229 SDL_Quit();
230 exit(EXIT_SUCCESS);
233 uintptr_t *stackbegin;
234 uintptr_t *stackend;
235 void system_init(void)
237 SDL_sem *s;
238 /* fake stack, OS manages size (and growth) */
239 stackbegin = stackend = (uintptr_t*)&s;
241 #if (CONFIG_PLATFORM & PLATFORM_MAEMO)
242 /* Make glib thread safe */
243 g_thread_init(NULL);
244 g_type_init();
245 #endif
247 if (SDL_Init(SDL_INIT_TIMER))
248 panicf("%s", SDL_GetError());
250 s = SDL_CreateSemaphore(0); /* 0-count so it blocks */
252 evt_thread = SDL_CreateThread(sdl_event_thread, s);
254 /* wait for sdl_event_thread to run so that it can initialize the surfaces
255 * and video subsystem needed for SDL events */
256 SDL_SemWait(s);
257 /* cleanup */
258 SDL_DestroySemaphore(s);
262 void system_reboot(void)
264 #ifdef HAVE_SDL_THREADS
265 sim_thread_exception_wait();
266 #else
267 sim_do_exit();
268 #endif
271 void system_exception_wait(void)
273 system_reboot();
276 void sys_handle_argv(int argc, char *argv[])
278 if (argc >= 1)
280 int x;
281 for (x = 1; x < argc; x++)
283 #ifdef DEBUG
284 if (!strcmp("--debugaudio", argv[x]))
286 debug_audio = true;
287 printf("Writing debug audio file.\n");
289 else
290 #endif
291 if (!strcmp("--debugwps", argv[x]))
293 debug_wps = true;
294 printf("WPS debug mode enabled.\n");
296 else if (!strcmp("--nobackground", argv[x]))
298 background = false;
299 printf("Disabling background image.\n");
301 #ifdef HAVE_REMOTE_LCD
302 else if (!strcmp("--noremote", argv[x]))
304 showremote = false;
305 background = false;
306 printf("Disabling remote image.\n");
308 #endif
309 else if (!strcmp("--old_lcd", argv[x]))
311 having_new_lcd = false;
312 printf("Using old LCD layout.\n");
314 else if (!strcmp("--zoom", argv[x]))
316 x++;
317 if(x < argc)
318 display_zoom=atoi(argv[x]);
319 else
320 display_zoom = 2;
321 printf("Window zoom is %d\n", display_zoom);
323 else if (!strcmp("--alarm", argv[x]))
325 sim_alarm_wakeup = true;
326 printf("Simulating alarm wakeup.\n");
328 else if (!strcmp("--root", argv[x]))
330 x++;
331 if (x < argc)
333 sim_root_dir = argv[x];
334 printf("Root directory: %s\n", sim_root_dir);
337 else if (!strcmp("--mapping", argv[x]))
339 mapping = true;
340 printf("Printing click coords with drag radii.\n");
342 else if (!strcmp("--debugbuttons", argv[x]))
344 debug_buttons = true;
345 printf("Printing background button clicks.\n");
347 else
349 printf("rockboxui\n");
350 printf("Arguments:\n");
351 #ifdef DEBUG
352 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
353 #endif
354 printf(" --debugwps \t Print advanced WPS debug info\n");
355 printf(" --nobackground \t Disable the background image\n");
356 #ifdef HAVE_REMOTE_LCD
357 printf(" --noremote \t Disable the remote image (will disable backgrounds)\n");
358 #endif
359 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
360 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
361 printf(" --alarm \t Simulate a wake-up on alarm\n");
362 printf(" --root [DIR]\t Set root directory\n");
363 printf(" --mapping \t Output coordinates and radius for mapping backgrounds\n");
364 exit(0);
368 if (display_zoom > 1) {
369 background = false;