The install window doesn't need to be wider than the other ones.
[Rockbox.git] / uisimulator / sdl / uisdl.c
blob052fd4af83a3985dccf699b602a560841bbb73a0
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;
59 int wps_verbose_level = 3;
61 long start_tick;
63 Uint32 tick_timer(Uint32 interval, void *param)
65 long new_tick;
67 (void) interval;
68 (void) param;
70 new_tick = (SDL_GetTicks() - start_tick) * HZ / 1000;
72 if (new_tick != current_tick) {
73 long i;
74 for (i = new_tick - current_tick; i > 0; i--)
75 sim_tick_tasks();
76 current_tick = new_tick;
79 return 1;
82 void gui_message_loop(void)
84 SDL_Event event;
85 bool done = false;
87 while(!done && SDL_WaitEvent(&event))
89 switch(event.type)
91 case SDL_KEYDOWN:
92 button_event(event.key.keysym.sym, true);
93 break;
94 case SDL_KEYUP:
95 button_event(event.key.keysym.sym, false);
96 break;
97 case SDL_QUIT:
98 done = true;
99 break;
100 default:
101 /*printf("Unhandled event\n"); */
102 break;
107 bool gui_startup(void)
109 SDL_Surface *picture_surface;
110 int width, height;
112 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)) {
113 fprintf(stderr, "fatal: %s", SDL_GetError());
114 return false;
117 atexit(SDL_Quit);
119 /* Try and load the background image. If it fails go without */
120 if (background) {
121 picture_surface = SDL_LoadBMP("UI256.bmp");
122 if (picture_surface == NULL) {
123 background = false;
124 fprintf(stderr, "warn: %s", SDL_GetError());
128 /* Set things up */
130 if (background) {
131 width = UI_WIDTH;
132 height = UI_HEIGHT;
133 } else {
134 #ifdef HAVE_REMOTE_LCD
135 width = UI_LCD_WIDTH > UI_REMOTE_WIDTH ? UI_LCD_WIDTH : UI_REMOTE_WIDTH;
136 height = UI_LCD_HEIGHT + UI_REMOTE_HEIGHT;
137 #else
138 width = UI_LCD_WIDTH;
139 height = UI_LCD_HEIGHT;
140 #endif
144 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
145 fprintf(stderr, "fatal: %s\n", SDL_GetError());
146 return false;
149 SDL_WM_SetCaption(UI_TITLE, NULL);
151 sim_lcd_init();
152 #ifdef HAVE_REMOTE_LCD
153 sim_lcd_remote_init();
154 #endif
156 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
158 if (background && picture_surface != NULL) {
159 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
160 SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
163 start_tick = SDL_GetTicks();
165 return true;
168 bool gui_shutdown(void)
170 int i;
172 SDL_KillThread(gui_thread);
173 SDL_RemoveTimer(tick_timer_id);
175 for (i = 0; i < threadCount; i++)
177 SDL_KillThread(threads[i]);
180 return true;
184 * Thin wrapper around normal app_main() to stop gcc complaining about types.
186 int sim_app_main(void *param)
188 app_main(param);
190 return 0;
193 #if defined(WIN32) && defined(main)
194 /* Don't use SDL_main on windows -> no more stdio redirection */
195 #undef main
196 #endif
198 int main(int argc, char *argv[])
200 if (argc >= 1) {
201 int x;
202 for (x = 1; x < argc; x++) {
203 if (!strcmp("--debugaudio", argv[x])) {
204 debug_audio = true;
205 printf("Writing debug audio file.\n");
206 } else if (!strcmp("--debugwps", argv[x])) {
207 debug_wps = true;
208 printf("WPS debug mode enabled.\n");
209 } else if (!strcmp("--background", argv[x])) {
210 background = true;
211 printf("Using background image.\n");
212 } else if (!strcmp("--old_lcd", argv[x])) {
213 having_new_lcd = false;
214 printf("Using old LCD layout.\n");
215 } else if (!strcmp("--zoom", argv[x])) {
216 x++;
217 if(x < argc)
218 display_zoom=atoi(argv[x]);
219 else
220 display_zoom = 2;
221 printf("Window zoom is %d\n", display_zoom);
222 } else {
223 printf("rockboxui\n");
224 printf("Arguments:\n");
225 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
226 printf(" --debugwps \t Print advanced WPS debug info\n");
227 printf(" --background \t Use background image of hardware\n");
228 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
229 printf(" --zoom [VAL]\t window zoom (will disable backgrounds)\n");
230 exit(0);
235 if (display_zoom > 1) {
236 background = false;
239 if (!gui_startup())
240 return -1;
242 gui_thread = SDL_CreateThread(sim_app_main, NULL);
243 if (gui_thread == NULL) {
244 printf("Error creating GUI thread!\n");
245 return -1;
248 tick_timer_id = SDL_AddTimer(10, tick_timer, NULL);
250 gui_message_loop();
252 return gui_shutdown();