Correct some comments (no functional changes)
[kugel-rb.git] / uisimulator / sdl / uisdl.c
blobd6a49d3a6087e11afc31d8e9d31be0623e332793
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 <stdlib.h>
23 #include <string.h>
24 #include <setjmp.h>
25 #include "autoconf.h"
26 #include "button.h"
27 #include "system-sdl.h"
28 #include "thread.h"
29 #include "kernel.h"
30 #include "uisdl.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 "thread-sdl.h"
41 #include "SDL_mutex.h"
42 #include "SDL_thread.h"
44 /* extern functions */
45 extern void new_key(int key);
47 void button_event(int key, bool pressed);
49 SDL_Surface *gui_surface;
50 bool background = true; /* use backgrounds by default */
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;
57 bool debug_audio = false;
59 bool debug_wps = false;
60 int wps_verbose_level = 3;
62 void gui_message_loop(void)
64 SDL_Event event;
65 bool done = false;
67 while(!done && SDL_WaitEvent(&event))
69 switch(event.type)
71 case SDL_KEYDOWN:
72 sim_enter_irq_handler();
73 button_event(event.key.keysym.sym, true);
74 sim_exit_irq_handler();
75 break;
76 case SDL_KEYUP:
77 sim_enter_irq_handler();
78 button_event(event.key.keysym.sym, false);
79 sim_exit_irq_handler();
80 break;
81 #ifndef HAVE_TOUCHSCREEN
82 case SDL_MOUSEBUTTONDOWN:
83 if (debug_wps && event.button.button == 1)
85 printf("Mouse at: (%d, %d)\n", event.button.x, event.button.y);
87 break;
88 #else
89 case SDL_MOUSEBUTTONUP:
90 sim_enter_irq_handler();
91 button_event(BUTTON_TOUCHSCREEN, false);
92 sim_exit_irq_handler();
93 break;
94 #endif
95 case SDL_QUIT:
96 done = true;
97 break;
98 default:
99 /*printf("Unhandled event\n"); */
100 break;
105 bool gui_startup(void)
107 SDL_Surface *picture_surface;
108 int width, height;
110 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)) {
111 fprintf(stderr, "fatal: %s\n", SDL_GetError());
112 return false;
115 atexit(SDL_Quit);
117 /* Try and load the background image. If it fails go without */
118 if (background) {
119 picture_surface = SDL_LoadBMP("UI256.bmp");
120 if (picture_surface == NULL) {
121 background = false;
122 fprintf(stderr, "warn: %s\n", SDL_GetError());
126 /* Set things up */
128 if (background) {
129 width = UI_WIDTH;
130 height = UI_HEIGHT;
131 } else {
132 #ifdef HAVE_REMOTE_LCD
133 width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH;
134 height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT;
135 #else
136 width = SIM_LCD_WIDTH;
137 height = SIM_LCD_HEIGHT;
138 #endif
142 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
143 fprintf(stderr, "fatal: %s\n", SDL_GetError());
144 return false;
147 SDL_WM_SetCaption(UI_TITLE, NULL);
149 sim_lcd_init();
150 #ifdef HAVE_REMOTE_LCD
151 sim_lcd_remote_init();
152 #endif
154 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
156 if (background && picture_surface != NULL) {
157 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
158 SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
161 return true;
164 bool gui_shutdown(void)
166 /* Order here is relevent to prevent deadlocks and use of destroyed
167 sync primitives by kernel threads */
168 thread_sdl_shutdown();
169 sim_kernel_shutdown();
170 return true;
173 #if defined(WIN32) && defined(main)
174 /* Don't use SDL_main on windows -> no more stdio redirection */
175 #undef main
176 #endif
178 int main(int argc, char *argv[])
180 if (argc >= 1)
182 int x;
183 for (x = 1; x < argc; x++)
185 if (!strcmp("--debugaudio", argv[x]))
187 debug_audio = true;
188 printf("Writing debug audio file.\n");
190 else if (!strcmp("--debugwps", argv[x]))
192 debug_wps = true;
193 printf("WPS debug mode enabled.\n");
195 else if (!strcmp("--nobackground", argv[x]))
197 background = false;
198 printf("Disabling background image.\n");
200 else if (!strcmp("--old_lcd", argv[x]))
202 having_new_lcd = false;
203 printf("Using old LCD layout.\n");
205 else if (!strcmp("--zoom", argv[x]))
207 x++;
208 if(x < argc)
209 display_zoom=atoi(argv[x]);
210 else
211 display_zoom = 2;
212 printf("Window zoom is %d\n", display_zoom);
214 else if (!strcmp("--alarm", argv[x]))
216 sim_alarm_wakeup = true;
217 printf("Simulating alarm wakeup.\n");
219 else if (!strcmp("--root", argv[x]))
221 x++;
222 if (x < argc)
224 sim_root_dir = argv[x];
225 printf("Root directory: %s\n", sim_root_dir);
228 else
230 printf("rockboxui\n");
231 printf("Arguments:\n");
232 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
233 printf(" --debugwps \t Print advanced WPS debug info\n");
234 printf(" --nobackground \t Disable the background image\n");
235 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
236 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
237 printf(" --alarm \t Simulate a wake-up on alarm\n");
238 printf(" --root [DIR]\t Set root directory\n");
239 exit(0);
244 if (display_zoom > 1) {
245 background = false;
248 if (!sim_kernel_init()) {
249 fprintf(stderr, "sim_kernel_init failed\n");
250 return -1;
253 if (!gui_startup()) {
254 fprintf(stderr, "gui_startup failed\n");
255 return -1;
258 /* app_main will be called by the new main thread */
259 if (!thread_sdl_init(NULL)) {
260 fprintf(stderr, "thread_sdl_init failed\n");
261 return -1;
264 gui_message_loop();
266 return gui_shutdown();