add global proxy / cache settings to httpget class. This removes the need of passing...
[Rockbox.git] / uisimulator / sdl / uisdl.c
blobe0a449ed48cf7932cdfb994add4c698bf61447f2
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;
59 const char *sim_root_dir = NULL;
61 bool debug_audio = false;
63 bool debug_wps = false;
64 int wps_verbose_level = 3;
66 long start_tick;
68 Uint32 tick_timer(Uint32 interval, void *param)
70 long new_tick;
72 (void) interval;
73 (void) param;
75 new_tick = (SDL_GetTicks() - start_tick) / (1000/HZ);
77 if (new_tick != current_tick) {
78 long i;
79 for (i = new_tick - current_tick; i > 0; i--)
81 sim_enter_irq_handler();
82 sim_tick_tasks();
83 sim_exit_irq_handler();
85 current_tick = new_tick;
88 return 1;
91 void gui_message_loop(void)
93 SDL_Event event;
94 bool done = false;
96 while(!done && SDL_WaitEvent(&event))
98 switch(event.type)
100 case SDL_KEYDOWN:
101 sim_enter_irq_handler();
102 button_event(event.key.keysym.sym, true);
103 sim_exit_irq_handler();
104 break;
105 case SDL_KEYUP:
106 sim_enter_irq_handler();
107 button_event(event.key.keysym.sym, false);
108 sim_exit_irq_handler();
109 break;
110 #ifndef HAVE_TOUCHPAD
111 case SDL_MOUSEBUTTONDOWN:
112 if (debug_wps && event.button.button == 1)
114 printf("Mouse at: (%d, %d)\n", event.button.x, event.button.y);
116 break;
117 #endif
118 case SDL_QUIT:
119 done = true;
120 break;
121 default:
122 /*printf("Unhandled event\n"); */
123 break;
128 bool gui_startup(void)
130 SDL_Surface *picture_surface;
131 int width, height;
133 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)) {
134 fprintf(stderr, "fatal: %s\n", SDL_GetError());
135 return false;
138 atexit(SDL_Quit);
140 /* Try and load the background image. If it fails go without */
141 if (background) {
142 picture_surface = SDL_LoadBMP("UI256.bmp");
143 if (picture_surface == NULL) {
144 background = false;
145 fprintf(stderr, "warn: %s\n", SDL_GetError());
149 /* Set things up */
151 if (background) {
152 width = UI_WIDTH;
153 height = UI_HEIGHT;
154 } else {
155 #ifdef HAVE_REMOTE_LCD
156 width = UI_LCD_WIDTH > UI_REMOTE_WIDTH ? UI_LCD_WIDTH : UI_REMOTE_WIDTH;
157 height = UI_LCD_HEIGHT + UI_REMOTE_HEIGHT;
158 #else
159 width = UI_LCD_WIDTH;
160 height = UI_LCD_HEIGHT;
161 #endif
165 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
166 fprintf(stderr, "fatal: %s\n", SDL_GetError());
167 return false;
170 SDL_WM_SetCaption(UI_TITLE, NULL);
172 sim_lcd_init();
173 #ifdef HAVE_REMOTE_LCD
174 sim_lcd_remote_init();
175 #endif
177 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
179 if (background && picture_surface != NULL) {
180 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
181 SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
184 start_tick = SDL_GetTicks();
186 return true;
189 bool gui_shutdown(void)
191 /* Order here is relevent to prevent deadlocks and use of destroyed
192 sync primitives by kernel threads */
193 thread_sdl_shutdown();
194 SDL_RemoveTimer(tick_timer_id);
195 sim_kernel_shutdown();
196 return true;
199 #if defined(WIN32) && defined(main)
200 /* Don't use SDL_main on windows -> no more stdio redirection */
201 #undef main
202 #endif
204 int main(int argc, char *argv[])
206 if (argc >= 1)
208 int x;
209 for (x = 1; x < argc; x++)
211 if (!strcmp("--debugaudio", argv[x]))
213 debug_audio = true;
214 printf("Writing debug audio file.\n");
216 else if (!strcmp("--debugwps", argv[x]))
218 debug_wps = true;
219 printf("WPS debug mode enabled.\n");
221 else if (!strcmp("--background", argv[x]))
223 background = true;
224 printf("Using background image.\n");
226 else if (!strcmp("--old_lcd", argv[x]))
228 having_new_lcd = false;
229 printf("Using old LCD layout.\n");
231 else if (!strcmp("--zoom", argv[x]))
233 x++;
234 if(x < argc)
235 display_zoom=atoi(argv[x]);
236 else
237 display_zoom = 2;
238 printf("Window zoom is %d\n", display_zoom);
240 else if (!strcmp("--alarm", argv[x]))
242 sim_alarm_wakeup = true;
243 printf("Simulating alarm wakeup.\n");
245 else if (!strcmp("--root", argv[x]))
247 x++;
248 if (x < argc)
250 sim_root_dir = argv[x];
251 printf("Root directory: %s\n", sim_root_dir);
254 else
256 printf("rockboxui\n");
257 printf("Arguments:\n");
258 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
259 printf(" --debugwps \t Print advanced WPS debug info\n");
260 printf(" --background \t Use background image of hardware\n");
261 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
262 printf(" --zoom [VAL]\t Window zoom (will disable backgrounds)\n");
263 printf(" --alarm \t Simulate a wake-up on alarm\n");
264 printf(" --root [DIR]\t Set root directory\n");
265 exit(0);
270 if (display_zoom > 1) {
271 background = false;
274 if (!sim_kernel_init()) {
275 fprintf(stderr, "sim_kernel_init failed\n");
276 return -1;
279 if (!gui_startup()) {
280 fprintf(stderr, "gui_startup failed\n");
281 return -1;
284 /* app_main will be called by the new main thread */
285 if (!thread_sdl_init(NULL)) {
286 fprintf(stderr, "thread_sdl_init failed\n");
287 return -1;
290 tick_timer_id = SDL_AddTimer(10, tick_timer, NULL);
292 gui_message_loop();
294 return gui_shutdown();