Remove the full url path from links to the wiki and display the wiki name only instea...
[Rockbox.git] / uisimulator / sdl / uisdl.c
blobd7845c5bebdddd677f55b2824cb29f4656815f92
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 "thread.h"
26 #include "kernel.h"
27 #include "uisdl.h"
28 #include "lcd-sdl.h"
29 #ifdef HAVE_LCD_BITMAP
30 #include "lcd-bitmap.h"
31 #elif defined(HAVE_LCD_CHARCELLS)
32 #include "lcd-charcells.h"
33 #endif
34 #ifdef HAVE_REMOTE_LCD
35 #include "lcd-remote-bitmap.h"
36 #endif
37 #include "thread-sdl.h"
38 #include "SDL_mutex.h"
39 #include "SDL_thread.h"
41 /* extern functions */
42 extern void app_main (void *); /* mod entry point */
43 extern void new_key(int key);
44 extern void sim_tick_tasks(void);
45 extern bool sim_io_init(void);
46 extern void sim_io_shutdown(void);
48 void button_event(int key, bool pressed);
50 SDL_Surface *gui_surface;
51 bool background = false; /* Don't use backgrounds by default */
53 SDL_TimerID tick_timer_id;
55 bool lcd_display_redraw = true; /* Used for player simulator */
56 char having_new_lcd = true; /* Used for player simulator */
58 bool debug_audio = false;
60 bool debug_wps = false;
61 int wps_verbose_level = 3;
63 long start_tick;
65 Uint32 tick_timer(Uint32 interval, void *param)
67 long new_tick;
69 (void) interval;
70 (void) param;
72 new_tick = (SDL_GetTicks() - start_tick) / (1000/HZ);
74 if (new_tick != current_tick) {
75 long i;
76 for (i = new_tick - current_tick; i > 0; i--)
77 sim_tick_tasks();
78 current_tick = new_tick;
81 return 1;
84 void gui_message_loop(void)
86 SDL_Event event;
87 bool done = false;
89 while(!done && SDL_WaitEvent(&event))
91 switch(event.type)
93 case SDL_KEYDOWN:
94 button_event(event.key.keysym.sym, true);
95 break;
96 case SDL_KEYUP:
97 button_event(event.key.keysym.sym, false);
98 break;
99 case SDL_QUIT:
100 done = true;
101 break;
102 default:
103 /*printf("Unhandled event\n"); */
104 break;
109 bool gui_startup(void)
111 SDL_Surface *picture_surface;
112 int width, height;
114 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)) {
115 fprintf(stderr, "fatal: %s\n", SDL_GetError());
116 return false;
119 atexit(SDL_Quit);
121 /* Try and load the background image. If it fails go without */
122 if (background) {
123 picture_surface = SDL_LoadBMP("UI256.bmp");
124 if (picture_surface == NULL) {
125 background = false;
126 fprintf(stderr, "warn: %s\n", SDL_GetError());
130 /* Set things up */
132 if (background) {
133 width = UI_WIDTH;
134 height = UI_HEIGHT;
135 } else {
136 #ifdef HAVE_REMOTE_LCD
137 width = UI_LCD_WIDTH > UI_REMOTE_WIDTH ? UI_LCD_WIDTH : UI_REMOTE_WIDTH;
138 height = UI_LCD_HEIGHT + UI_REMOTE_HEIGHT;
139 #else
140 width = UI_LCD_WIDTH;
141 height = UI_LCD_HEIGHT;
142 #endif
146 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
147 fprintf(stderr, "fatal: %s\n", SDL_GetError());
148 return false;
151 SDL_WM_SetCaption(UI_TITLE, NULL);
153 sim_lcd_init();
154 #ifdef HAVE_REMOTE_LCD
155 sim_lcd_remote_init();
156 #endif
158 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
160 if (background && picture_surface != NULL) {
161 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
162 SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
165 start_tick = SDL_GetTicks();
167 return true;
170 bool gui_shutdown(void)
172 SDL_RemoveTimer(tick_timer_id);
173 /* Order here is relevent to prevent deadlocks and use of destroyed
174 sync primitives by kernel threads */
175 thread_sdl_shutdown();
176 sim_io_shutdown();
177 return true;
180 #if defined(WIN32) && defined(main)
181 /* Don't use SDL_main on windows -> no more stdio redirection */
182 #undef main
183 #endif
185 int main(int argc, char *argv[])
187 if (argc >= 1) {
188 int x;
189 for (x = 1; x < argc; x++) {
190 if (!strcmp("--debugaudio", argv[x])) {
191 debug_audio = true;
192 printf("Writing debug audio file.\n");
193 } else if (!strcmp("--debugwps", argv[x])) {
194 debug_wps = true;
195 printf("WPS debug mode enabled.\n");
196 } else if (!strcmp("--background", argv[x])) {
197 background = true;
198 printf("Using background image.\n");
199 } else if (!strcmp("--old_lcd", argv[x])) {
200 having_new_lcd = false;
201 printf("Using old LCD layout.\n");
202 } else if (!strcmp("--zoom", argv[x])) {
203 x++;
204 if(x < argc)
205 display_zoom=atoi(argv[x]);
206 else
207 display_zoom = 2;
208 printf("Window zoom is %d\n", display_zoom);
209 } else {
210 printf("rockboxui\n");
211 printf("Arguments:\n");
212 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
213 printf(" --debugwps \t Print advanced WPS debug info\n");
214 printf(" --background \t Use background image of hardware\n");
215 printf(" --old_lcd \t [Player] simulate old playermodel (ROM version<4.51)\n");
216 printf(" --zoom [VAL]\t window zoom (will disable backgrounds)\n");
217 exit(0);
222 if (display_zoom > 1) {
223 background = false;
226 if (!sim_io_init()) {
227 fprintf(stderr, "sim_io_init failed\n");
228 return -1;
231 if (!gui_startup()) {
232 fprintf(stderr, "gui_startup failed\n");
233 return -1;
236 /* app_main will be called by the new main thread */
237 if (!thread_sdl_init(NULL)) {
238 fprintf(stderr, "thread_sdl_init failed\n");
239 return -1;
242 tick_timer_id = SDL_AddTimer(10, tick_timer, NULL);
244 gui_message_loop();
246 return gui_shutdown();