Vastly increase speed of SDL screen updates for RGB565.
[kugel-rb.git] / firmware / target / hosted / sdl / system-sdl.c
blobb900d38b430066f7849ae5c1203c29c8666b6db4
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 SDL_Surface *gui_surface;
45 bool background = true; /* use backgrounds by default */
46 #ifdef HAVE_REMOTE_LCD
47 bool showremote = true; /* include remote by default */
48 #endif
49 bool mapping = false;
50 bool debug_buttons = false;
52 bool lcd_display_redraw = true; /* Used for player simulator */
53 char having_new_lcd = true; /* Used for player simulator */
54 bool sim_alarm_wakeup = false;
55 const char *sim_root_dir = NULL;
56 extern int display_zoom;
58 static SDL_Thread *evt_thread = NULL;
60 #ifdef DEBUG
61 bool debug_audio = false;
62 #endif
64 bool debug_wps = false;
65 int wps_verbose_level = 3;
68 void sys_poweroff(void)
73 * This thread will read the buttons in an interrupt like fashion, and
74 * also initializes SDL_INIT_VIDEO and the surfaces
76 * it must be done in the same thread (at least on windows) because events only
77 * work in the thread which called SDL_Init(SubSystem) with SDL_INIT_VIDEO
79 * This is an SDL thread and relies on preemptive behavoir of the host
80 **/
81 static int sdl_event_thread(void * param)
83 SDL_InitSubSystem(SDL_INIT_VIDEO);
85 SDL_Surface *picture_surface = NULL;
86 int width, height;
87 int depth;
89 /* Try and load the background image. If it fails go without */
90 if (background) {
91 picture_surface = SDL_LoadBMP("UI256.bmp");
92 if (picture_surface == NULL) {
93 background = false;
94 DEBUGF("warn: %s\n", SDL_GetError());
98 /* Set things up */
99 if (background)
101 width = UI_WIDTH;
102 height = UI_HEIGHT;
104 else
106 #ifdef HAVE_REMOTE_LCD
107 if (showremote)
109 width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH;
110 height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT;
112 else
113 #endif
115 width = SIM_LCD_WIDTH;
116 height = SIM_LCD_HEIGHT;
120 depth = LCD_DEPTH;
121 if (depth < 8)
122 depth = 16;
124 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, depth, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
125 panicf("%s", SDL_GetError());
128 SDL_WM_SetCaption(UI_TITLE, NULL);
130 if (background && picture_surface != NULL)
131 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
133 /* let system_init proceed */
134 SDL_SemPost((SDL_sem *)param);
137 * finally enter the button loop */
138 gui_message_loop();
140 if(picture_surface)
141 SDL_FreeSurface(picture_surface);
143 /* Order here is relevent to prevent deadlocks and use of destroyed
144 sync primitives by kernel threads */
145 sim_thread_shutdown();
146 sim_kernel_shutdown();
148 return 0;
151 void sim_do_exit(void)
153 /* wait for event thread to finish */
154 SDL_WaitThread(evt_thread, NULL);
156 SDL_Quit();
157 exit(EXIT_SUCCESS);
160 void system_init(void)
162 SDL_sem *s;
164 if (SDL_Init(SDL_INIT_TIMER))
165 panicf("%s", SDL_GetError());
167 s = SDL_CreateSemaphore(0); /* 0-count so it blocks */
169 evt_thread = SDL_CreateThread(sdl_event_thread, s);
171 /* wait for sdl_event_thread to run so that it can initialize the surfaces
172 * and video subsystem needed for SDL events */
173 SDL_SemWait(s);
175 /* cleanup */
176 SDL_DestroySemaphore(s);
179 void system_exception_wait(void)
181 sim_thread_exception_wait();
184 void system_reboot(void)
186 sim_thread_exception_wait();
190 void sys_handle_argv(int argc, char *argv[])
192 if (argc >= 1)
194 int x;
195 for (x = 1; x < argc; x++)
197 #ifdef DEBUG
198 if (!strcmp("--debugaudio", argv[x]))
200 debug_audio = true;
201 printf("Writing debug audio file.\n");
203 else
204 #endif
205 if (!strcmp("--debugwps", argv[x]))
207 debug_wps = true;
208 printf("WPS debug mode enabled.\n");
210 else if (!strcmp("--nobackground", argv[x]))
212 background = false;
213 printf("Disabling background image.\n");
215 #ifdef HAVE_REMOTE_LCD
216 else if (!strcmp("--noremote", argv[x]))
218 showremote = false;
219 background = false;
220 printf("Disabling remote image.\n");
222 #endif
223 else if (!strcmp("--old_lcd", argv[x]))
225 having_new_lcd = false;
226 printf("Using old LCD layout.\n");
228 else if (!strcmp("--zoom", argv[x]))
230 x++;
231 if(x < argc)
232 display_zoom=atoi(argv[x]);
233 else
234 display_zoom = 2;
235 printf("Window zoom is %d\n", display_zoom);
237 else if (!strcmp("--alarm", argv[x]))
239 sim_alarm_wakeup = true;
240 printf("Simulating alarm wakeup.\n");
242 else if (!strcmp("--root", argv[x]))
244 x++;
245 if (x < argc)
247 sim_root_dir = argv[x];
248 printf("Root directory: %s\n", sim_root_dir);
251 else if (!strcmp("--mapping", argv[x]))
253 mapping = true;
254 printf("Printing click coords with drag radii.\n");
256 else if (!strcmp("--debugbuttons", argv[x]))
258 debug_buttons = true;
259 printf("Printing background button clicks.\n");
261 else
263 printf("rockboxui\n");
264 printf("Arguments:\n");
265 #ifdef DEBUG
266 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
267 #endif
268 printf(" --debugwps \t Print advanced WPS debug info\n");
269 printf(" --nobackground \t Disable the background image\n");
270 #ifdef HAVE_REMOTE_LCD
271 printf(" --noremote \t Disable the remote image (will disable backgrounds)\n");
272 #endif
273 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
274 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
275 printf(" --alarm \t Simulate a wake-up on alarm\n");
276 printf(" --root [DIR]\t Set root directory\n");
277 printf(" --mapping \t Output coordinates and radius for mapping backgrounds\n");
278 exit(0);
282 if (display_zoom > 1) {
283 background = false;