Setup the touchpads to have two modes - stylus and button - and set them in button...
[kugel-rb.git] / uisimulator / sdl / uisdl.c
blob28aaf59f82cae94b8a44f62542eace7ba71945af
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 new_key(int key);
45 void button_event(int key, bool pressed);
47 SDL_Surface *gui_surface;
48 bool background = false; /* Don't use backgrounds by default */
50 bool lcd_display_redraw = true; /* Used for player simulator */
51 char having_new_lcd = true; /* Used for player simulator */
52 bool sim_alarm_wakeup = false;
53 const char *sim_root_dir = NULL;
55 bool debug_audio = false;
57 bool debug_wps = false;
58 int wps_verbose_level = 3;
60 void gui_message_loop(void)
62 SDL_Event event;
63 bool done = false;
65 while(!done && SDL_WaitEvent(&event))
67 switch(event.type)
69 case SDL_KEYDOWN:
70 sim_enter_irq_handler();
71 button_event(event.key.keysym.sym, true);
72 sim_exit_irq_handler();
73 break;
74 case SDL_KEYUP:
75 sim_enter_irq_handler();
76 button_event(event.key.keysym.sym, false);
77 sim_exit_irq_handler();
78 break;
79 #ifndef HAVE_TOUCHPAD
80 case SDL_MOUSEBUTTONDOWN:
81 if (debug_wps && event.button.button == 1)
83 printf("Mouse at: (%d, %d)\n", event.button.x, event.button.y);
85 break;
86 #else
87 case SDL_MOUSEBUTTONUP:
88 sim_enter_irq_handler();
89 button_event(BUTTON_TOUCHPAD, false);
90 sim_exit_irq_handler();
91 break;
92 #endif
93 case SDL_QUIT:
94 done = true;
95 break;
96 default:
97 /*printf("Unhandled event\n"); */
98 break;
103 bool gui_startup(void)
105 SDL_Surface *picture_surface;
106 int width, height;
108 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)) {
109 fprintf(stderr, "fatal: %s\n", SDL_GetError());
110 return false;
113 atexit(SDL_Quit);
115 /* Try and load the background image. If it fails go without */
116 if (background) {
117 picture_surface = SDL_LoadBMP("UI256.bmp");
118 if (picture_surface == NULL) {
119 background = false;
120 fprintf(stderr, "warn: %s\n", SDL_GetError());
124 /* Set things up */
126 if (background) {
127 width = UI_WIDTH;
128 height = UI_HEIGHT;
129 } else {
130 #ifdef HAVE_REMOTE_LCD
131 width = UI_LCD_WIDTH > UI_REMOTE_WIDTH ? UI_LCD_WIDTH : UI_REMOTE_WIDTH;
132 height = UI_LCD_HEIGHT + UI_REMOTE_HEIGHT;
133 #else
134 width = UI_LCD_WIDTH;
135 height = UI_LCD_HEIGHT;
136 #endif
140 if ((gui_surface = SDL_SetVideoMode(width * display_zoom, height * display_zoom, 24, SDL_HWSURFACE|SDL_DOUBLEBUF)) == NULL) {
141 fprintf(stderr, "fatal: %s\n", SDL_GetError());
142 return false;
145 SDL_WM_SetCaption(UI_TITLE, NULL);
147 sim_lcd_init();
148 #ifdef HAVE_REMOTE_LCD
149 sim_lcd_remote_init();
150 #endif
152 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
154 if (background && picture_surface != NULL) {
155 SDL_BlitSurface(picture_surface, NULL, gui_surface, NULL);
156 SDL_UpdateRect(gui_surface, 0, 0, 0, 0);
159 return true;
162 bool gui_shutdown(void)
164 /* Order here is relevent to prevent deadlocks and use of destroyed
165 sync primitives by kernel threads */
166 thread_sdl_shutdown();
167 sim_kernel_shutdown();
168 return true;
171 #if defined(WIN32) && defined(main)
172 /* Don't use SDL_main on windows -> no more stdio redirection */
173 #undef main
174 #endif
176 int main(int argc, char *argv[])
178 if (argc >= 1)
180 int x;
181 for (x = 1; x < argc; x++)
183 if (!strcmp("--debugaudio", argv[x]))
185 debug_audio = true;
186 printf("Writing debug audio file.\n");
188 else if (!strcmp("--debugwps", argv[x]))
190 debug_wps = true;
191 printf("WPS debug mode enabled.\n");
193 else if (!strcmp("--background", argv[x]))
195 background = true;
196 printf("Using background image.\n");
198 else if (!strcmp("--old_lcd", argv[x]))
200 having_new_lcd = false;
201 printf("Using old LCD layout.\n");
203 else if (!strcmp("--zoom", argv[x]))
205 x++;
206 if(x < argc)
207 display_zoom=atoi(argv[x]);
208 else
209 display_zoom = 2;
210 printf("Window zoom is %d\n", display_zoom);
212 else if (!strcmp("--alarm", argv[x]))
214 sim_alarm_wakeup = true;
215 printf("Simulating alarm wakeup.\n");
217 else if (!strcmp("--root", argv[x]))
219 x++;
220 if (x < argc)
222 sim_root_dir = argv[x];
223 printf("Root directory: %s\n", sim_root_dir);
226 else
228 printf("rockboxui\n");
229 printf("Arguments:\n");
230 printf(" --debugaudio \t Write raw PCM data to audiodebug.raw\n");
231 printf(" --debugwps \t Print advanced WPS debug info\n");
232 printf(" --background \t Use background image of hardware\n");
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 exit(0);
242 if (display_zoom > 1) {
243 background = false;
246 if (!sim_kernel_init()) {
247 fprintf(stderr, "sim_kernel_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 gui_message_loop();
264 return gui_shutdown();