VBRfix description, part 2: actually this note was way outdated. Files recorded with...
[kugel-rb.git] / uisimulator / sdl / uisdl.c
blobbac11ff7a9cec003967516fae7ebc8d4e7bbfa5e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Daniel Everton <dan@iocaine.org>
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdlib.h>
21 #include <string.h>
22 #include "autoconf.h"
23 #include "button.h"
24 #include "thread.h"
25 #include "kernel.h"
26 #include "uisdl.h"
27 #include "lcd-sdl.h"
28 #ifdef HAVE_LCD_BITMAP
29 #include "lcd-bitmap.h"
30 #elif defined(HAVE_LCD_CHARCELLS)
31 #include "lcd-charcells.h"
32 #endif
33 #ifdef HAVE_REMOTE_LCD
34 #include "lcd-remote-bitmap.h"
35 #endif
36 #include "thread-sdl.h"
37 #include "SDL_mutex.h"
38 #include "SDL_thread.h"
40 /* extern functions */
41 extern void app_main (void *); /* mod entry point */
42 extern void new_key(int key);
43 extern void sim_tick_tasks(void);
45 void button_event(int key, bool pressed);
47 SDL_Surface *gui_surface;
48 bool background = false; /* Don't use backgrounds by default */
50 SDL_Thread *gui_thread;
51 SDL_TimerID tick_timer_id;
53 bool lcd_display_redraw = true; /* Used for player simulator */
54 char having_new_lcd = true; /* Used for player simulator */
56 bool debug_audio = false;
58 bool debug_wps = false;
60 long start_tick;
62 Uint32 tick_timer(Uint32 interval, void *param)
64 long new_tick;
66 (void) interval;
67 (void) param;
69 new_tick = (SDL_GetTicks() - start_tick) * HZ / 1000;
71 if (new_tick != current_tick) {
72 long i;
73 for (i = new_tick - current_tick; i > 0; i--)
74 sim_tick_tasks();
75 current_tick = new_tick;
78 return 1;
81 void gui_message_loop(void)
83 SDL_Event event;
84 bool done = false;
86 while(!done && SDL_WaitEvent(&event))
88 switch(event.type)
90 case SDL_KEYDOWN:
91 button_event(event.key.keysym.sym, true);
92 break;
93 case SDL_KEYUP:
94 button_event(event.key.keysym.sym, false);
95 break;
96 case SDL_QUIT:
97 done = true;
98 break;
99 default:
100 /*printf("Unhandled event\n"); */
101 break;
106 bool gui_startup(void)
108 SDL_Surface *picture_surface;
109 int width, height;
111 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)) {
112 fprintf(stderr, "fatal: %s", SDL_GetError());
113 return false;
116 atexit(SDL_Quit);
118 /* Try and load the background image. If it fails go without */
119 if (background) {
120 picture_surface = SDL_LoadBMP("UI256.bmp");
121 if (picture_surface == NULL) {
122 background = false;
123 fprintf(stderr, "warn: %s", SDL_GetError());
127 /* Set things up */
129 if (background) {
130 width = UI_WIDTH;
131 height = UI_HEIGHT;
132 } else {
133 #ifdef HAVE_REMOTE_LCD
134 width = UI_LCD_WIDTH > UI_REMOTE_WIDTH ? UI_LCD_WIDTH : UI_REMOTE_WIDTH;
135 height = UI_LCD_HEIGHT + UI_REMOTE_HEIGHT;
136 #else
137 width = UI_LCD_WIDTH;
138 height = UI_LCD_HEIGHT;
139 #endif
143 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
144 fprintf(stderr, "fatal: %s\n", SDL_GetError());
145 return false;
148 SDL_WM_SetCaption(UI_TITLE, NULL);
150 sim_lcd_init();
151 #ifdef HAVE_REMOTE_LCD
152 sim_lcd_remote_init();
153 #endif
155 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
157 if (background && picture_surface != NULL) {
158 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
159 SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
162 start_tick = SDL_GetTicks();
164 return true;
167 bool gui_shutdown(void)
169 int i;
171 SDL_KillThread(gui_thread);
172 SDL_RemoveTimer(tick_timer_id);
174 for (i = 0; i < threadCount; i++)
176 SDL_KillThread(threads[i]);
179 return true;
183 * Thin wrapper around normal app_main() to stop gcc complaining about types.
185 int sim_app_main(void *param)
187 app_main(param);
189 return 0;
192 #if defined(WIN32) && defined(main)
193 /* Don't use SDL_main on windows -> no more stdio redirection */
194 #undef main
195 #endif
197 int main(int argc, char *argv[])
199 if (argc >= 1) {
200 int x;
201 for (x = 1; x < argc; x++) {
202 if (!strcmp("--debugaudio", argv[x])) {
203 debug_audio = true;
204 printf("Writing debug audio file.\n");
205 } else if (!strcmp("--debugwps", argv[x])) {
206 debug_wps = true;
207 printf("WPS debug mode enabled.\n");
208 } else if (!strcmp("--background", argv[x])) {
209 background = true;
210 printf("Using background image.\n");
211 } else if (!strcmp("--old_lcd", argv[x])) {
212 having_new_lcd = false;
213 printf("Using old LCD layout.\n");
214 } else if (!strcmp("--zoom", argv[x])) {
215 x++;
216 if(x < argc)
217 display_zoom=atoi(argv[x]);
218 else
219 display_zoom = 2;
220 printf("Window zoom is %d\n", display_zoom);
221 } else {
222 printf("rockboxui\n");
223 printf("Arguments:\n");
224 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
225 printf(" --debugwps \t Print advanced WPS debug info\n");
226 printf(" --background \t Use background image of hardware\n");
227 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
228 printf(" --zoom [VAL]\t window zoom (will disable backgrounds)\n");
229 exit(0);
234 if (display_zoom > 1) {
235 background = false;
238 if (!gui_startup())
239 return -1;
241 gui_thread = SDL_CreateThread(sim_app_main, NULL);
242 if (gui_thread == NULL) {
243 printf("Error creating GUI thread!\n");
244 return -1;
247 tick_timer_id = SDL_AddTimer(10, tick_timer, NULL);
249 gui_message_loop();
251 return gui_shutdown();