2 * QEMU SDL display driver
4 * Copyright (c) 2003 Fabrice Bellard
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
32 #if defined(__APPLE__)
33 #define CONFIG_SDL_GENERIC_KBD
36 static SDL_Surface *screen;
37 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
38 static int last_vm_running;
39 static int gui_saved_grab;
40 static int gui_fullscreen;
41 static int gui_key_modifier_pressed;
42 static int gui_keysym;
44 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
46 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
47 SDL_UpdateRect(screen, x, y, w, h);
50 static void sdl_resize(DisplayState *ds, int w, int h)
54 // printf("resizing to %d %d\n", w, h);
56 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
57 flags |= SDL_RESIZABLE;
59 flags |= SDL_FULLSCREEN;
60 screen = SDL_SetVideoMode(w, h, 0, flags);
62 fprintf(stderr, "Could not open SDL display\n");
65 ds->data = screen->pixels;
66 ds->linesize = screen->pitch;
67 ds->depth = screen->format->BitsPerPixel;
70 #ifdef CONFIG_SDL_GENERIC_KBD
72 /* XXX: use keymap tables defined in the VNC patch because the
73 following code suppose you have a US keyboard. */
75 static const uint8_t scancodes[SDLK_LAST] = {
89 [SDLK_BACKSPACE] = 0x0e,
101 [SDLK_LEFTBRACKET] = 0x1a,
102 [SDLK_RIGHTBRACKET] = 0x1b,
103 [SDLK_RETURN] = 0x1c,
114 [SDLK_SEMICOLON] = 0x27,
116 [SDLK_BACKQUOTE] = 0x29,
117 [SDLK_LSHIFT] = 0x2a,
118 [SDLK_BACKSLASH] = 0x2b,
127 [SDLK_PERIOD] = 0x34,
129 [SDLK_KP_MULTIPLY] = 0x37,
132 [SDLK_CAPSLOCK] = 0x3a,
143 [SDLK_NUMLOCK] = 0x45,
144 [SDLK_SCROLLOCK] = 0x46,
148 [SDLK_KP_MINUS] = 0x4a,
152 [SDLK_KP_PLUS] = 0x4e,
157 [SDLK_KP_PERIOD] = 0x53,
161 [SDLK_KP_ENTER] = 0x9c,
162 [SDLK_KP_DIVIDE] = 0xb5,
168 [SDLK_INSERT] = 0xd2,
171 [SDLK_PAGEUP] = 0xc9,
172 [SDLK_PAGEDOWN] = 0xd1,
173 [SDLK_DELETE] = 0xd3,
176 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
178 return scancodes[ev->keysym.sym];
181 #elif defined(_WIN32)
183 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
185 return ev->keysym.scancode;
190 static const uint8_t x_keycode_to_pc_keycode[61] = {
196 0xcd, /* 102 Right */
202 0x9c, /* 108 Enter */
203 0x9d, /* 109 Ctrl-R */
205 0xb7, /* 111 Print */
206 0xb5, /* 112 Divide */
207 0xb8, /* 113 Alt-R */
208 0xc6, /* 114 Break */
214 0x70, /* 120 Hiragana_Katakana */
217 0x73, /* 123 backslash */
223 0x79, /* 129 Henkan */
225 0x7b, /* 131 Muhenkan */
241 0x47, /* 147 KP_HOME */
242 0x48, /* 148 KP_UP */
243 0x49, /* 149 KP_PgUp */
244 0x4b, /* 150 KP_Left */
246 0x4d, /* 152 KP_Right */
247 0x4f, /* 153 KP_End */
248 0x50, /* 154 KP_Down */
249 0x51, /* 155 KP_PgDn */
250 0x52, /* 156 KP_Ins */
251 0x53, /* 157 KP_Del */
254 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
258 keycode = ev->keysym.scancode;
262 } else if (keycode < 97) {
263 keycode -= 8; /* just an offset */
264 } else if (keycode < 158) {
265 /* use conversion table */
266 keycode = x_keycode_to_pc_keycode[keycode - 97];
275 static void sdl_process_key(SDL_KeyboardEvent *ev)
278 static uint8_t modifiers_state[256];
280 if (ev->keysym.sym == SDLK_PAUSE) {
283 if (ev->type == SDL_KEYUP)
285 kbd_put_keycode(0xe1);
286 kbd_put_keycode(0x1d | v);
287 kbd_put_keycode(0x45 | v);
291 /* XXX: not portable, but avoids complicated mappings */
292 keycode = sdl_keyevent_to_keycode(ev);
296 /* sent when leaving window: reset the modifiers state */
297 for(i = 0; i < 256; i++) {
298 if (modifiers_state[i]) {
300 kbd_put_keycode(0xe0);
301 kbd_put_keycode(i | 0x80);
305 case 0x2a: /* Left Shift */
306 case 0x36: /* Right Shift */
307 case 0x1d: /* Left CTRL */
308 case 0x9d: /* Right CTRL */
309 case 0x38: /* Left ALT */
310 case 0xb8: /* Right ALT */
311 if (ev->type == SDL_KEYUP)
312 modifiers_state[keycode] = 0;
314 modifiers_state[keycode] = 1;
316 case 0x45: /* num lock */
317 case 0x3a: /* caps lock */
318 /* SDL does not send the key up event, so we generate it */
319 kbd_put_keycode(keycode);
320 kbd_put_keycode(keycode | 0x80);
324 /* now send the key code */
326 kbd_put_keycode(0xe0);
327 if (ev->type == SDL_KEYUP)
328 kbd_put_keycode(keycode | 0x80);
330 kbd_put_keycode(keycode & 0x7f);
333 static void sdl_update_caption(void)
338 strcat(buf, " [Stopped]");
341 strcat(buf, " - Press Ctrl-Shift to exit grab");
343 SDL_WM_SetCaption(buf, "QEMU");
346 static void sdl_grab_start(void)
349 SDL_WM_GrabInput(SDL_GRAB_ON);
350 /* dummy read to avoid moving the mouse */
351 SDL_GetRelativeMouseState(NULL, NULL);
353 sdl_update_caption();
356 static void sdl_grab_end(void)
358 SDL_WM_GrabInput(SDL_GRAB_OFF);
361 sdl_update_caption();
364 static void sdl_send_mouse_event(void)
366 int dx, dy, dz, state, buttons;
367 state = SDL_GetRelativeMouseState(&dx, &dy);
369 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
370 buttons |= MOUSE_EVENT_LBUTTON;
371 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
372 buttons |= MOUSE_EVENT_RBUTTON;
373 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
374 buttons |= MOUSE_EVENT_MBUTTON;
375 /* XXX: test wheel */
377 #ifdef SDL_BUTTON_WHEELUP
378 if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
380 if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
383 kbd_mouse_event(dx, dy, dz, buttons);
386 static void toggle_full_screen(DisplayState *ds)
388 gui_fullscreen = !gui_fullscreen;
389 sdl_resize(ds, screen->w, screen->h);
390 if (gui_fullscreen) {
391 gui_saved_grab = gui_grab;
397 vga_invalidate_display();
398 vga_update_display();
401 static void sdl_refresh(DisplayState *ds)
403 SDL_Event ev1, *ev = &ev1;
406 if (last_vm_running != vm_running) {
407 last_vm_running = vm_running;
408 sdl_update_caption();
411 vga_update_display();
412 while (SDL_PollEvent(ev)) {
414 case SDL_VIDEOEXPOSE:
415 sdl_update(ds, 0, 0, screen->w, screen->h);
419 if (ev->type == SDL_KEYDOWN) {
420 mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
421 (KMOD_LSHIFT | KMOD_LCTRL);
422 gui_key_modifier_pressed = mod_state;
423 if (gui_key_modifier_pressed &&
424 ev->key.keysym.sym == SDLK_f) {
425 gui_keysym = ev->key.keysym.sym;
427 } else if (ev->type == SDL_KEYUP) {
428 mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL));
430 if (gui_key_modifier_pressed) {
433 toggle_full_screen(ds);
436 /* exit/enter grab if pressing Ctrl-Shift */
443 gui_key_modifier_pressed = 0;
448 sdl_process_key(&ev->key);
451 qemu_system_shutdown_request();
453 case SDL_MOUSEMOTION:
455 sdl_send_mouse_event();
458 case SDL_MOUSEBUTTONDOWN:
459 case SDL_MOUSEBUTTONUP:
461 SDL_MouseButtonEvent *bev = &ev->button;
463 if (ev->type == SDL_MOUSEBUTTONDOWN &&
464 (bev->state & SDL_BUTTON_LMASK)) {
465 /* start grabbing all events */
469 sdl_send_mouse_event();
473 case SDL_ACTIVEEVENT:
474 if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0) {
484 static void sdl_cleanup(void)
489 void sdl_display_init(DisplayState *ds)
493 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
494 if (SDL_Init (flags)) {
495 fprintf(stderr, "Could not initialize SDL - exiting\n");
500 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
501 signal(SIGINT, SIG_DFL);
502 signal(SIGQUIT, SIG_DFL);
505 ds->dpy_update = sdl_update;
506 ds->dpy_resize = sdl_resize;
507 ds->dpy_refresh = sdl_refresh;
509 sdl_resize(ds, 640, 400);
510 sdl_update_caption();
511 SDL_EnableKeyRepeat(250, 50);