Make button handling more target like, do it in a tick task completely.
[kugel-rb.git] / uisimulator / sdl / uisdl.c
blob4ec4ce2d2790033458112c649ff25a055f3c3948
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"
43 #include "math.h"
46 /* extern functions */
47 extern void new_key(int key);
48 extern int xy2button( int x, int y);
49 void button_event(int key, bool pressed);
51 SDL_Surface *gui_surface;
52 bool background = true; /* use backgrounds by default */
53 #ifdef HAVE_REMOTE_LCD
54 static 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;
64 #ifdef DEBUG
65 bool debug_audio = false;
66 #endif
68 bool debug_wps = false;
69 int wps_verbose_level = 3;
71 bool gui_startup(void)
73 SDL_Surface *picture_surface;
74 int width, height;
76 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)) {
77 fprintf(stderr, "fatal: %s\n", SDL_GetError());
78 return false;
81 atexit(SDL_Quit);
83 /* Try and load the background image. If it fails go without */
84 if (background) {
85 picture_surface = SDL_LoadBMP("UI256.bmp");
86 if (picture_surface == NULL) {
87 background = false;
88 fprintf(stderr, "warn: %s\n", SDL_GetError());
92 /* Set things up */
93 if (background)
95 width = UI_WIDTH;
96 height = UI_HEIGHT;
98 else
100 #ifdef HAVE_REMOTE_LCD
101 if (showremote)
103 width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH;
104 height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT;
106 else
107 #endif
109 width = SIM_LCD_WIDTH;
110 height = SIM_LCD_HEIGHT;
115 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
116 fprintf(stderr, "fatal: %s\n", SDL_GetError());
117 return false;
120 SDL_WM_SetCaption(UI_TITLE, NULL);
122 sim_lcd_init();
123 #ifdef HAVE_REMOTE_LCD
124 if (showremote)
125 sim_lcd_remote_init();
126 #endif
128 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
130 if (background && picture_surface != NULL) {
131 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
132 SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
135 return true;
138 void gui_shutdown(void)
140 /* Order here is relevent to prevent deadlocks and use of destroyed
141 sync primitives by kernel threads */
142 thread_sdl_shutdown();
143 sim_kernel_shutdown();
146 #if defined(WIN32) && defined(main)
147 /* Don't use SDL_main on windows -> no more stdio redirection */
148 #undef main
149 #endif
150 int main(int argc, char *argv[])
152 if (argc >= 1)
154 int x;
155 for (x = 1; x < argc; x++)
157 #ifdef DEBUG
158 if (!strcmp("--debugaudio", argv[x]))
160 debug_audio = true;
161 printf("Writing debug audio file.\n");
163 else
164 #endif
165 if (!strcmp("--debugwps", argv[x]))
167 debug_wps = true;
168 printf("WPS debug mode enabled.\n");
170 else if (!strcmp("--nobackground", argv[x]))
172 background = false;
173 printf("Disabling background image.\n");
175 #ifdef HAVE_REMOTE_LCD
176 else if (!strcmp("--noremote", argv[x]))
178 showremote = false;
179 background = false;
180 printf("Disabling remote image.\n");
182 #endif
183 else if (!strcmp("--old_lcd", argv[x]))
185 having_new_lcd = false;
186 printf("Using old LCD layout.\n");
188 else if (!strcmp("--zoom", argv[x]))
190 x++;
191 if(x < argc)
192 display_zoom=atoi(argv[x]);
193 else
194 display_zoom = 2;
195 printf("Window zoom is %d\n", display_zoom);
197 else if (!strcmp("--alarm", argv[x]))
199 sim_alarm_wakeup = true;
200 printf("Simulating alarm wakeup.\n");
202 else if (!strcmp("--root", argv[x]))
204 x++;
205 if (x < argc)
207 sim_root_dir = argv[x];
208 printf("Root directory: %s\n", sim_root_dir);
211 else if (!strcmp("--mapping", argv[x]))
213 mapping = true;
214 printf("Printing click coords with drag radii.\n");
216 else if (!strcmp("--debugbuttons", argv[x]))
218 debug_buttons = true;
219 printf("Printing background button clicks.\n");
221 else
223 printf("rockboxui\n");
224 printf("Arguments:\n");
225 #ifdef DEBUG
226 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
227 #endif
228 printf(" --debugwps \t Print advanced WPS debug info\n");
229 printf(" --nobackground \t Disable the background image\n");
230 #ifdef HAVE_REMOTE_LCD
231 printf(" --noremote \t Disable the remote image (will disable backgrounds)\n");
232 #endif
233 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
234 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
235 printf(" --alarm \t Simulate a wake-up on alarm\n");
236 printf(" --root [DIR]\t Set root directory\n");
237 printf(" --mapping \t Output coordinates and radius for mapping backgrounds\n");
238 exit(0);
243 if (display_zoom > 1) {
244 background = false;
247 if (!sim_kernel_init()) {
248 fprintf(stderr, "sim_kernel_init failed\n");
249 return -1;
252 if (!gui_startup()) {
253 fprintf(stderr, "gui_startup failed\n");
254 return -1;
257 struct thread_entry *t = NULL;
258 int status = 0;
259 /* app_main will be called by the new main thread */
260 if (!thread_sdl_init(&t)) {
261 fprintf(stderr, "thread_sdl_init failed\n");
262 return -1;
265 /* HACK!! */
266 SDL_WaitThread(t->context.t, &status);
268 gui_shutdown();
269 return 0;