Implement as genuine a set_irq_level function for the sim as possible. The yield...
[Rockbox.git] / uisimulator / sdl / uisdl.c
blobd9c64abce2ef515cfd116ac5878703d77b35d1f4
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 <setjmp.h>
23 #include "autoconf.h"
24 #include "button.h"
25 #include "system-sdl.h"
26 #include "thread.h"
27 #include "kernel.h"
28 #include "uisdl.h"
29 #include "lcd-sdl.h"
30 #ifdef HAVE_LCD_BITMAP
31 #include "lcd-bitmap.h"
32 #elif defined(HAVE_LCD_CHARCELLS)
33 #include "lcd-charcells.h"
34 #endif
35 #ifdef HAVE_REMOTE_LCD
36 #include "lcd-remote-bitmap.h"
37 #endif
38 #include "thread-sdl.h"
39 #include "SDL_mutex.h"
40 #include "SDL_thread.h"
42 /* extern functions */
43 extern void app_main (void *); /* mod entry point */
44 extern void new_key(int key);
45 extern void sim_tick_tasks(void);
46 extern bool sim_io_init(void);
47 extern void sim_io_shutdown(void);
49 void button_event(int key, bool pressed);
51 SDL_Surface *gui_surface;
52 bool background = false; /* Don't use backgrounds by default */
54 SDL_TimerID tick_timer_id;
56 bool lcd_display_redraw = true; /* Used for player simulator */
57 char having_new_lcd = true; /* Used for player simulator */
58 bool sim_alarm_wakeup = false;
60 bool debug_audio = false;
62 bool debug_wps = false;
63 int wps_verbose_level = 3;
65 long start_tick;
67 Uint32 tick_timer(Uint32 interval, void *param)
69 long new_tick;
71 (void) interval;
72 (void) param;
74 new_tick = (SDL_GetTicks() - start_tick) / (1000/HZ);
76 if (new_tick != current_tick) {
77 long i;
78 for (i = new_tick - current_tick; i > 0; i--)
80 sim_enter_irq_handler();
81 sim_tick_tasks();
82 sim_exit_irq_handler();
84 current_tick = new_tick;
87 return 1;
90 void gui_message_loop(void)
92 SDL_Event event;
93 bool done = false;
95 while(!done && SDL_WaitEvent(&event))
97 switch(event.type)
99 case SDL_KEYDOWN:
100 sim_enter_irq_handler();
101 button_event(event.key.keysym.sym, true);
102 sim_exit_irq_handler();
103 break;
104 case SDL_KEYUP:
105 sim_enter_irq_handler();
106 button_event(event.key.keysym.sym, false);
107 sim_exit_irq_handler();
108 break;
109 case SDL_QUIT:
110 done = true;
111 break;
112 default:
113 /*printf("Unhandled event\n"); */
114 break;
119 bool gui_startup(void)
121 SDL_Surface *picture_surface;
122 int width, height;
124 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)) {
125 fprintf(stderr, "fatal: %s\n", SDL_GetError());
126 return false;
129 atexit(SDL_Quit);
131 /* Try and load the background image. If it fails go without */
132 if (background) {
133 picture_surface = SDL_LoadBMP("UI256.bmp");
134 if (picture_surface == NULL) {
135 background = false;
136 fprintf(stderr, "warn: %s\n", SDL_GetError());
140 /* Set things up */
142 if (background) {
143 width = UI_WIDTH;
144 height = UI_HEIGHT;
145 } else {
146 #ifdef HAVE_REMOTE_LCD
147 width = UI_LCD_WIDTH > UI_REMOTE_WIDTH ? UI_LCD_WIDTH : UI_REMOTE_WIDTH;
148 height = UI_LCD_HEIGHT + UI_REMOTE_HEIGHT;
149 #else
150 width = UI_LCD_WIDTH;
151 height = UI_LCD_HEIGHT;
152 #endif
156 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
157 fprintf(stderr, "fatal: %s\n", SDL_GetError());
158 return false;
161 SDL_WM_SetCaption(UI_TITLE, NULL);
163 sim_lcd_init();
164 #ifdef HAVE_REMOTE_LCD
165 sim_lcd_remote_init();
166 #endif
168 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
170 if (background && picture_surface != NULL) {
171 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
172 SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
175 start_tick = SDL_GetTicks();
177 return true;
180 bool gui_shutdown(void)
182 /* Order here is relevent to prevent deadlocks and use of destroyed
183 sync primitives by kernel threads */
184 thread_sdl_shutdown();
185 SDL_RemoveTimer(tick_timer_id);
186 sim_io_shutdown();
187 sim_kernel_shutdown();
188 return true;
191 #if defined(WIN32) && defined(main)
192 /* Don't use SDL_main on windows -> no more stdio redirection */
193 #undef main
194 #endif
196 int main(int argc, char *argv[])
198 if (argc >= 1) {
199 int x;
200 for (x = 1; x < argc; x++) {
201 if (!strcmp("--debugaudio", argv[x])) {
202 debug_audio = true;
203 printf("Writing debug audio file.\n");
204 } else if (!strcmp("--debugwps", argv[x])) {
205 debug_wps = true;
206 printf("WPS debug mode enabled.\n");
207 } else if (!strcmp("--background", argv[x])) {
208 background = true;
209 printf("Using background image.\n");
210 } else if (!strcmp("--old_lcd", argv[x])) {
211 having_new_lcd = false;
212 printf("Using old LCD layout.\n");
213 } else if (!strcmp("--zoom", argv[x])) {
214 x++;
215 if(x < argc)
216 display_zoom=atoi(argv[x]);
217 else
218 display_zoom = 2;
219 printf("Window zoom is %d\n", display_zoom);
220 } else if (!strcmp("--alarm", argv[x])) {
221 sim_alarm_wakeup = true;
222 printf("Simulating alarm wakeup.\n");
223 } else {
224 printf("rockboxui\n");
225 printf("Arguments:\n");
226 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
227 printf(" --debugwps \t Print advanced WPS debug info\n");
228 printf(" --background \t Use background image of hardware\n");
229 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
230 printf(" --zoom [VAL]\t window zoom (will disable backgrounds)\n");
231 printf(" --alarm \t Simulate a wakup-up on Alarm\n");
232 exit(0);
237 if (display_zoom > 1) {
238 background = false;
241 if (!sim_kernel_init()) {
242 fprintf(stderr, "sim_kernel_init failed\n");
243 return -1;
246 if (!sim_io_init()) {
247 fprintf(stderr, "sim_io_init failed\n");
248 return -1;
251 if (!gui_startup()) {
252 fprintf(stderr, "gui_startup failed\n");
253 return -1;
256 /* app_main will be called by the new main thread */
257 if (!thread_sdl_init(NULL)) {
258 fprintf(stderr, "thread_sdl_init failed\n");
259 return -1;
262 tick_timer_id = SDL_AddTimer(10, tick_timer, NULL);
264 gui_message_loop();
266 return gui_shutdown();