Allow multiple graphics devices.
[qemu/mini2440.git] / sdl.c
blobb8c82fd666f8269e7c2b2493f2c9d344402442bf
1 /*
2 * QEMU SDL display driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "vl.h"
26 #include <SDL.h>
28 #ifndef _WIN32
29 #include <signal.h>
30 #endif
32 static SDL_Surface *screen;
33 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
34 static int last_vm_running;
35 static int gui_saved_grab;
36 static int gui_fullscreen;
37 static int gui_key_modifier_pressed;
38 static int gui_keysym;
39 static int gui_fullscreen_initial_grab;
40 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
41 static uint8_t modifiers_state[256];
43 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
45 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
46 SDL_UpdateRect(screen, x, y, w, h);
49 static void sdl_resize(DisplayState *ds, int w, int h)
51 int flags;
53 // printf("resizing to %d %d\n", w, h);
55 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
56 if (gui_fullscreen)
57 flags |= SDL_FULLSCREEN;
59 again:
60 screen = SDL_SetVideoMode(w, h, 0, flags);
61 if (!screen) {
62 fprintf(stderr, "Could not open SDL display\n");
63 exit(1);
65 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
66 flags &= ~SDL_HWSURFACE;
67 goto again;
70 if (!screen->pixels) {
71 fprintf(stderr, "Could not open SDL display\n");
72 exit(1);
74 ds->data = screen->pixels;
75 ds->linesize = screen->pitch;
76 ds->depth = screen->format->BitsPerPixel;
77 ds->width = w;
78 ds->height = h;
81 /* generic keyboard conversion */
83 #include "sdl_keysym.h"
84 #include "keymaps.c"
86 static kbd_layout_t *kbd_layout = NULL;
88 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
90 int keysym;
91 /* workaround for X11+SDL bug with AltGR */
92 keysym = ev->keysym.sym;
93 if (keysym == 0 && ev->keysym.scancode == 113)
94 keysym = SDLK_MODE;
95 return keysym2scancode(kbd_layout, keysym);
98 /* specific keyboard conversions from scan codes */
100 #if defined(_WIN32)
102 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
104 return ev->keysym.scancode;
107 #else
109 static const uint8_t x_keycode_to_pc_keycode[61] = {
110 0xc7, /* 97 Home */
111 0xc8, /* 98 Up */
112 0xc9, /* 99 PgUp */
113 0xcb, /* 100 Left */
114 0x4c, /* 101 KP-5 */
115 0xcd, /* 102 Right */
116 0xcf, /* 103 End */
117 0xd0, /* 104 Down */
118 0xd1, /* 105 PgDn */
119 0xd2, /* 106 Ins */
120 0xd3, /* 107 Del */
121 0x9c, /* 108 Enter */
122 0x9d, /* 109 Ctrl-R */
123 0x0, /* 110 Pause */
124 0xb7, /* 111 Print */
125 0xb5, /* 112 Divide */
126 0xb8, /* 113 Alt-R */
127 0xc6, /* 114 Break */
128 0x0, /* 115 */
129 0x0, /* 116 */
130 0x0, /* 117 */
131 0x0, /* 118 */
132 0x0, /* 119 */
133 0x70, /* 120 Hiragana_Katakana */
134 0x0, /* 121 */
135 0x0, /* 122 */
136 0x73, /* 123 backslash */
137 0x0, /* 124 */
138 0x0, /* 125 */
139 0x0, /* 126 */
140 0x0, /* 127 */
141 0x0, /* 128 */
142 0x79, /* 129 Henkan */
143 0x0, /* 130 */
144 0x7b, /* 131 Muhenkan */
145 0x0, /* 132 */
146 0x7d, /* 133 Yen */
147 0x0, /* 134 */
148 0x0, /* 135 */
149 0x47, /* 136 KP_7 */
150 0x48, /* 137 KP_8 */
151 0x49, /* 138 KP_9 */
152 0x4b, /* 139 KP_4 */
153 0x4c, /* 140 KP_5 */
154 0x4d, /* 141 KP_6 */
155 0x4f, /* 142 KP_1 */
156 0x50, /* 143 KP_2 */
157 0x51, /* 144 KP_3 */
158 0x52, /* 145 KP_0 */
159 0x53, /* 146 KP_. */
160 0x47, /* 147 KP_HOME */
161 0x48, /* 148 KP_UP */
162 0x49, /* 149 KP_PgUp */
163 0x4b, /* 150 KP_Left */
164 0x4c, /* 151 KP_ */
165 0x4d, /* 152 KP_Right */
166 0x4f, /* 153 KP_End */
167 0x50, /* 154 KP_Down */
168 0x51, /* 155 KP_PgDn */
169 0x52, /* 156 KP_Ins */
170 0x53, /* 157 KP_Del */
173 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
175 int keycode;
177 keycode = ev->keysym.scancode;
179 if (keycode < 9) {
180 keycode = 0;
181 } else if (keycode < 97) {
182 keycode -= 8; /* just an offset */
183 } else if (keycode < 158) {
184 /* use conversion table */
185 keycode = x_keycode_to_pc_keycode[keycode - 97];
186 } else {
187 keycode = 0;
189 return keycode;
192 #endif
194 static void reset_keys(void)
196 int i;
197 for(i = 0; i < 256; i++) {
198 if (modifiers_state[i]) {
199 if (i & 0x80)
200 kbd_put_keycode(0xe0);
201 kbd_put_keycode(i | 0x80);
202 modifiers_state[i] = 0;
207 static void sdl_process_key(SDL_KeyboardEvent *ev)
209 int keycode, v;
211 if (ev->keysym.sym == SDLK_PAUSE) {
212 /* specific case */
213 v = 0;
214 if (ev->type == SDL_KEYUP)
215 v |= 0x80;
216 kbd_put_keycode(0xe1);
217 kbd_put_keycode(0x1d | v);
218 kbd_put_keycode(0x45 | v);
219 return;
222 if (kbd_layout) {
223 keycode = sdl_keyevent_to_keycode_generic(ev);
224 } else {
225 keycode = sdl_keyevent_to_keycode(ev);
228 switch(keycode) {
229 case 0x00:
230 /* sent when leaving window: reset the modifiers state */
231 reset_keys();
232 return;
233 case 0x2a: /* Left Shift */
234 case 0x36: /* Right Shift */
235 case 0x1d: /* Left CTRL */
236 case 0x9d: /* Right CTRL */
237 case 0x38: /* Left ALT */
238 case 0xb8: /* Right ALT */
239 if (ev->type == SDL_KEYUP)
240 modifiers_state[keycode] = 0;
241 else
242 modifiers_state[keycode] = 1;
243 break;
244 case 0x45: /* num lock */
245 case 0x3a: /* caps lock */
246 /* SDL does not send the key up event, so we generate it */
247 kbd_put_keycode(keycode);
248 kbd_put_keycode(keycode | 0x80);
249 return;
252 /* now send the key code */
253 if (keycode & 0x80)
254 kbd_put_keycode(0xe0);
255 if (ev->type == SDL_KEYUP)
256 kbd_put_keycode(keycode | 0x80);
257 else
258 kbd_put_keycode(keycode & 0x7f);
261 static void sdl_update_caption(void)
263 char buf[1024];
264 strcpy(buf, "QEMU");
265 if (!vm_running) {
266 strcat(buf, " [Stopped]");
268 if (gui_grab) {
269 strcat(buf, " - Press Ctrl-Alt to exit grab");
271 SDL_WM_SetCaption(buf, "QEMU");
274 static void sdl_grab_start(void)
276 SDL_ShowCursor(0);
277 SDL_WM_GrabInput(SDL_GRAB_ON);
278 /* dummy read to avoid moving the mouse */
279 SDL_GetRelativeMouseState(NULL, NULL);
280 gui_grab = 1;
281 sdl_update_caption();
284 static void sdl_grab_end(void)
286 SDL_WM_GrabInput(SDL_GRAB_OFF);
287 SDL_ShowCursor(1);
288 gui_grab = 0;
289 sdl_update_caption();
292 static void sdl_send_mouse_event(int dz)
294 int dx, dy, state, buttons;
295 state = SDL_GetRelativeMouseState(&dx, &dy);
296 buttons = 0;
297 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
298 buttons |= MOUSE_EVENT_LBUTTON;
299 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
300 buttons |= MOUSE_EVENT_RBUTTON;
301 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
302 buttons |= MOUSE_EVENT_MBUTTON;
303 kbd_mouse_event(dx, dy, dz, buttons);
306 static void toggle_full_screen(DisplayState *ds)
308 gui_fullscreen = !gui_fullscreen;
309 sdl_resize(ds, screen->w, screen->h);
310 if (gui_fullscreen) {
311 gui_saved_grab = gui_grab;
312 sdl_grab_start();
313 } else {
314 if (!gui_saved_grab)
315 sdl_grab_end();
317 vga_hw_invalidate();
318 vga_hw_update();
321 static void sdl_refresh(DisplayState *ds)
323 SDL_Event ev1, *ev = &ev1;
324 int mod_state;
326 if (last_vm_running != vm_running) {
327 last_vm_running = vm_running;
328 sdl_update_caption();
331 vga_hw_update();
333 while (SDL_PollEvent(ev)) {
334 switch (ev->type) {
335 case SDL_VIDEOEXPOSE:
336 sdl_update(ds, 0, 0, screen->w, screen->h);
337 break;
338 case SDL_KEYDOWN:
339 case SDL_KEYUP:
340 if (ev->type == SDL_KEYDOWN) {
341 mod_state = (SDL_GetModState() & gui_grab_code) ==
342 gui_grab_code;
343 gui_key_modifier_pressed = mod_state;
344 if (gui_key_modifier_pressed) {
345 int keycode;
346 keycode = sdl_keyevent_to_keycode(&ev->key);
347 switch(keycode) {
348 case 0x21: /* 'f' key on US keyboard */
349 toggle_full_screen(ds);
350 gui_keysym = 1;
351 break;
352 case 0x02 ... 0x0a: /* '1' to '9' keys */
353 console_select(keycode - 0x02);
354 if (!is_graphic_console()) {
355 /* display grab if going to a text console */
356 if (gui_grab)
357 sdl_grab_end();
359 gui_keysym = 1;
360 break;
361 default:
362 break;
364 } else if (!is_graphic_console()) {
365 int keysym;
366 keysym = 0;
367 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
368 switch(ev->key.keysym.sym) {
369 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
370 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
371 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
372 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
373 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
374 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
375 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
376 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
377 default: break;
379 } else {
380 switch(ev->key.keysym.sym) {
381 case SDLK_UP: keysym = QEMU_KEY_UP; break;
382 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
383 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
384 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
385 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
386 case SDLK_END: keysym = QEMU_KEY_END; break;
387 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
388 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
389 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
390 default: break;
393 if (keysym) {
394 kbd_put_keysym(keysym);
395 } else if (ev->key.keysym.unicode != 0) {
396 kbd_put_keysym(ev->key.keysym.unicode);
399 } else if (ev->type == SDL_KEYUP) {
400 mod_state = (ev->key.keysym.mod & gui_grab_code);
401 if (!mod_state) {
402 if (gui_key_modifier_pressed) {
403 gui_key_modifier_pressed = 0;
404 if (gui_keysym == 0) {
405 /* exit/enter grab if pressing Ctrl-Alt */
406 if (!gui_grab)
407 sdl_grab_start();
408 else
409 sdl_grab_end();
410 /* SDL does not send back all the
411 modifiers key, so we must correct it */
412 reset_keys();
413 break;
415 gui_keysym = 0;
419 if (is_graphic_console())
420 sdl_process_key(&ev->key);
421 break;
422 case SDL_QUIT:
423 qemu_system_shutdown_request();
424 break;
425 case SDL_MOUSEMOTION:
426 if (gui_grab) {
427 sdl_send_mouse_event(0);
429 break;
430 case SDL_MOUSEBUTTONDOWN:
431 case SDL_MOUSEBUTTONUP:
433 SDL_MouseButtonEvent *bev = &ev->button;
434 if (!gui_grab) {
435 if (ev->type == SDL_MOUSEBUTTONDOWN &&
436 (bev->state & SDL_BUTTON_LMASK)) {
437 /* start grabbing all events */
438 sdl_grab_start();
440 } else {
441 int dz;
442 dz = 0;
443 #ifdef SDL_BUTTON_WHEELUP
444 if (bev->button == SDL_BUTTON_WHEELUP) {
445 dz = -1;
446 } else if (bev->button == SDL_BUTTON_WHEELDOWN) {
447 dz = 1;
449 #endif
450 sdl_send_mouse_event(dz);
453 break;
454 case SDL_ACTIVEEVENT:
455 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
456 !ev->active.gain && !gui_fullscreen_initial_grab) {
457 sdl_grab_end();
459 break;
460 default:
461 break;
466 static void sdl_cleanup(void)
468 SDL_Quit();
471 void sdl_display_init(DisplayState *ds, int full_screen)
473 int flags;
475 #if defined(__APPLE__)
476 /* always use generic keymaps */
477 if (!keyboard_layout)
478 keyboard_layout = "en-us";
479 #endif
480 if(keyboard_layout) {
481 kbd_layout = init_keyboard_layout(keyboard_layout);
482 if (!kbd_layout)
483 exit(1);
486 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
487 if (SDL_Init (flags)) {
488 fprintf(stderr, "Could not initialize SDL - exiting\n");
489 exit(1);
491 #ifndef _WIN32
492 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
493 signal(SIGINT, SIG_DFL);
494 signal(SIGQUIT, SIG_DFL);
495 #endif
497 ds->dpy_update = sdl_update;
498 ds->dpy_resize = sdl_resize;
499 ds->dpy_refresh = sdl_refresh;
501 sdl_resize(ds, 640, 400);
502 sdl_update_caption();
503 SDL_EnableKeyRepeat(250, 50);
504 SDL_EnableUNICODE(1);
505 gui_grab = 0;
507 atexit(sdl_cleanup);
508 if (full_screen) {
509 gui_fullscreen = 1;
510 gui_fullscreen_initial_grab = 1;
511 sdl_grab_start();