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
24 #include "qemu-common.h"
30 #include <SDL/SDL_syswm.h>
36 static DisplayChangeListener *dcl;
37 static SDL_Surface *real_screen;
38 static SDL_Surface *guest_screen = NULL;
39 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
40 static int last_vm_running;
41 static int gui_saved_grab;
42 static int gui_fullscreen;
43 static int gui_noframe;
44 static int gui_key_modifier_pressed;
45 static int gui_keysym;
46 static int gui_fullscreen_initial_grab;
47 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
48 static uint8_t modifiers_state[256];
49 static int width, height;
50 static SDL_Cursor *sdl_cursor_normal;
51 static SDL_Cursor *sdl_cursor_hidden;
52 static int absolute_enabled = 0;
53 static int guest_cursor = 0;
54 static int guest_x, guest_y;
55 static SDL_Cursor *guest_sprite = 0;
57 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
64 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
66 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
67 SDL_UpdateRect(real_screen, x, y, w, h);
70 static void sdl_setdata(DisplayState *ds)
75 rec.w = real_screen->w;
76 rec.h = real_screen->h;
78 if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
80 guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds),
81 ds_get_bits_per_pixel(ds), ds_get_linesize(ds),
82 ds->surface->pf.rmask, ds->surface->pf.gmask,
83 ds->surface->pf.bmask, ds->surface->pf.amask);
86 static void sdl_resize(DisplayState *ds)
90 // printf("resizing to %d %d\n", w, h);
92 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
94 flags |= SDL_FULLSCREEN;
98 width = ds_get_width(ds);
99 height = ds_get_height(ds);
100 real_screen = SDL_SetVideoMode(width, height, 0, flags);
102 fprintf(stderr, "Could not open SDL display\n");
109 /* generic keyboard conversion */
111 #include "sdl_keysym.h"
114 static kbd_layout_t *kbd_layout = NULL;
116 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
119 /* workaround for X11+SDL bug with AltGR */
120 keysym = ev->keysym.sym;
121 if (keysym == 0 && ev->keysym.scancode == 113)
123 /* For Japanese key '\' and '|' */
124 if (keysym == 92 && ev->keysym.scancode == 133) {
127 return keysym2scancode(kbd_layout, keysym);
130 /* specific keyboard conversions from scan codes */
134 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
136 return ev->keysym.scancode;
141 #if defined(SDL_VIDEO_DRIVER_X11)
142 #include <X11/XKBlib.h>
144 static int check_for_evdev(void)
149 const char *keycodes;
151 SDL_VERSION(&info.version);
152 if (!SDL_GetWMInfo(&info))
155 desc = XkbGetKeyboard(info.info.x11.display,
156 XkbGBN_AllComponentsMask,
158 if (desc == NULL || desc->names == NULL)
161 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
162 if (keycodes == NULL)
163 fprintf(stderr, "could not lookup keycode name\n");
164 else if (strstart(keycodes, "evdev_", NULL))
166 else if (!strstart(keycodes, "xfree86_", NULL))
168 "unknown keycodes `%s', please report to qemu-devel@nongnu.org\n",
171 XkbFreeClientMap(desc, XkbGBN_AllComponentsMask, True);
176 static int check_for_evdev(void)
182 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
185 static int has_evdev = -1;
188 has_evdev = check_for_evdev();
190 keycode = ev->keysym.scancode;
194 } else if (keycode < 97) {
195 keycode -= 8; /* just an offset */
196 } else if (keycode < 158) {
197 /* use conversion table */
199 keycode = translate_evdev_keycode(keycode - 97);
201 keycode = translate_xfree86_keycode(keycode - 97);
202 } else if (keycode == 208) { /* Hiragana_Katakana */
204 } else if (keycode == 211) { /* backslash */
214 static void reset_keys(void)
217 for(i = 0; i < 256; i++) {
218 if (modifiers_state[i]) {
220 kbd_put_keycode(0xe0);
221 kbd_put_keycode(i | 0x80);
222 modifiers_state[i] = 0;
227 static void sdl_process_key(SDL_KeyboardEvent *ev)
231 if (ev->keysym.sym == SDLK_PAUSE) {
234 if (ev->type == SDL_KEYUP)
236 kbd_put_keycode(0xe1);
237 kbd_put_keycode(0x1d | v);
238 kbd_put_keycode(0x45 | v);
243 keycode = sdl_keyevent_to_keycode_generic(ev);
245 keycode = sdl_keyevent_to_keycode(ev);
250 /* sent when leaving window: reset the modifiers state */
253 case 0x2a: /* Left Shift */
254 case 0x36: /* Right Shift */
255 case 0x1d: /* Left CTRL */
256 case 0x9d: /* Right CTRL */
257 case 0x38: /* Left ALT */
258 case 0xb8: /* Right ALT */
259 if (ev->type == SDL_KEYUP)
260 modifiers_state[keycode] = 0;
262 modifiers_state[keycode] = 1;
264 case 0x45: /* num lock */
265 case 0x3a: /* caps lock */
266 /* SDL does not send the key up event, so we generate it */
267 kbd_put_keycode(keycode);
268 kbd_put_keycode(keycode | 0x80);
272 /* now send the key code */
274 kbd_put_keycode(0xe0);
275 if (ev->type == SDL_KEYUP)
276 kbd_put_keycode(keycode | 0x80);
278 kbd_put_keycode(keycode & 0x7f);
281 static void sdl_update_caption(void)
284 const char *status = "";
287 status = " [Stopped]";
290 status = " - Press Ctrl-Alt to exit grab";
292 status = " - Press Ctrl-Alt-Shift to exit grab";
296 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
298 snprintf(buf, sizeof(buf), "QEMU%s", status);
300 SDL_WM_SetCaption(buf, "QEMU");
303 static void sdl_hide_cursor(void)
308 if (kbd_mouse_is_absolute()) {
310 SDL_SetCursor(sdl_cursor_hidden);
316 static void sdl_show_cursor(void)
321 if (!kbd_mouse_is_absolute()) {
324 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
325 SDL_SetCursor(guest_sprite);
327 SDL_SetCursor(sdl_cursor_normal);
331 static void sdl_grab_start(void)
334 SDL_SetCursor(guest_sprite);
335 if (!kbd_mouse_is_absolute() && !absolute_enabled)
336 SDL_WarpMouse(guest_x, guest_y);
340 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
342 sdl_update_caption();
347 static void sdl_grab_end(void)
349 SDL_WM_GrabInput(SDL_GRAB_OFF);
352 sdl_update_caption();
355 static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
359 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
360 buttons |= MOUSE_EVENT_LBUTTON;
361 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
362 buttons |= MOUSE_EVENT_RBUTTON;
363 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
364 buttons |= MOUSE_EVENT_MBUTTON;
366 if (kbd_mouse_is_absolute()) {
367 if (!absolute_enabled) {
372 absolute_enabled = 1;
375 dx = x * 0x7FFF / (width - 1);
376 dy = y * 0x7FFF / (height - 1);
377 } else if (absolute_enabled) {
379 absolute_enabled = 0;
380 } else if (guest_cursor) {
389 kbd_mouse_event(dx, dy, dz, buttons);
392 static void toggle_full_screen(DisplayState *ds)
394 gui_fullscreen = !gui_fullscreen;
396 if (gui_fullscreen) {
397 gui_saved_grab = gui_grab;
407 static void sdl_refresh(DisplayState *ds)
409 SDL_Event ev1, *ev = &ev1;
411 int buttonstate = SDL_GetMouseState(NULL, NULL);
413 if (last_vm_running != vm_running) {
414 last_vm_running = vm_running;
415 sdl_update_caption();
419 SDL_EnableUNICODE(!is_graphic_console());
421 while (SDL_PollEvent(ev)) {
423 case SDL_VIDEOEXPOSE:
424 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
428 if (ev->type == SDL_KEYDOWN) {
430 mod_state = (SDL_GetModState() & gui_grab_code) ==
433 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
434 (gui_grab_code | KMOD_LSHIFT);
436 gui_key_modifier_pressed = mod_state;
437 if (gui_key_modifier_pressed) {
439 keycode = sdl_keyevent_to_keycode(&ev->key);
441 case 0x21: /* 'f' key on US keyboard */
442 toggle_full_screen(ds);
445 case 0x02 ... 0x0a: /* '1' to '9' keys */
446 /* Reset the modifiers sent to the current console */
448 console_select(keycode - 0x02);
449 if (!is_graphic_console()) {
450 /* display grab if going to a text console */
459 } else if (!is_graphic_console()) {
462 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
463 switch(ev->key.keysym.sym) {
464 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
465 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
466 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
467 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
468 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
469 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
470 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
471 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
475 switch(ev->key.keysym.sym) {
476 case SDLK_UP: keysym = QEMU_KEY_UP; break;
477 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
478 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
479 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
480 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
481 case SDLK_END: keysym = QEMU_KEY_END; break;
482 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
483 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
484 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
485 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
490 kbd_put_keysym(keysym);
491 } else if (ev->key.keysym.unicode != 0) {
492 kbd_put_keysym(ev->key.keysym.unicode);
495 } else if (ev->type == SDL_KEYUP) {
497 mod_state = (ev->key.keysym.mod & gui_grab_code);
499 mod_state = (ev->key.keysym.mod &
500 (gui_grab_code | KMOD_LSHIFT));
503 if (gui_key_modifier_pressed) {
504 gui_key_modifier_pressed = 0;
505 if (gui_keysym == 0) {
506 /* exit/enter grab if pressing Ctrl-Alt */
508 /* if the application is not active,
509 do not try to enter grab state. It
511 'SDL_WM_GrabInput(SDL_GRAB_ON)'
512 from blocking all the application
514 if (SDL_GetAppState() & SDL_APPACTIVE)
519 /* SDL does not send back all the
520 modifiers key, so we must correct it */
528 if (is_graphic_console() && !gui_keysym)
529 sdl_process_key(&ev->key);
533 qemu_system_shutdown_request();
535 case SDL_MOUSEMOTION:
536 if (gui_grab || kbd_mouse_is_absolute() ||
538 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
539 ev->motion.x, ev->motion.y, ev->motion.state);
542 case SDL_MOUSEBUTTONDOWN:
543 case SDL_MOUSEBUTTONUP:
545 SDL_MouseButtonEvent *bev = &ev->button;
546 if (!gui_grab && !kbd_mouse_is_absolute()) {
547 if (ev->type == SDL_MOUSEBUTTONDOWN &&
548 (bev->button == SDL_BUTTON_LEFT)) {
549 /* start grabbing all events */
555 if (ev->type == SDL_MOUSEBUTTONDOWN) {
556 buttonstate |= SDL_BUTTON(bev->button);
558 buttonstate &= ~SDL_BUTTON(bev->button);
560 #ifdef SDL_BUTTON_WHEELUP
561 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
563 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
567 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
571 case SDL_ACTIVEEVENT:
572 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
573 !ev->active.gain && !gui_fullscreen_initial_grab) {
576 if (ev->active.state & SDL_APPACTIVE) {
577 if (ev->active.gain) {
578 /* Back to default interval */
579 dcl->gui_timer_interval = 0;
582 /* Sleeping interval */
583 dcl->gui_timer_interval = 500;
594 static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
596 SDL_Rect dst = { x, y, w, h };
597 SDL_FillRect(real_screen, &dst, c);
600 static void sdl_mouse_warp(int x, int y, int on)
605 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
606 SDL_SetCursor(guest_sprite);
607 if (!kbd_mouse_is_absolute() && !absolute_enabled)
613 guest_x = x, guest_y = y;
616 static void sdl_mouse_define(int width, int height, int bpp,
617 int hot_x, int hot_y,
618 uint8_t *image, uint8_t *mask)
620 uint8_t sprite[256], *line;
621 int x, y, dst, bypl, src = 0;
623 SDL_FreeCursor(guest_sprite);
625 memset(sprite, 0, 256);
626 bypl = ((width * bpp + 31) >> 5) << 2;
627 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
629 for (x = 0; x < width; x ++, dst ++) {
632 src = *(line ++); src |= *(line ++); src |= *(line ++);
636 src = *(line ++); src |= *(line ++);
642 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
645 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
648 src = 1 & (line[x >> 3] >> (x & 7));
652 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
655 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
658 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
659 SDL_SetCursor(guest_sprite);
662 static void sdl_cleanup(void)
665 SDL_FreeCursor(guest_sprite);
669 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
674 #if defined(__APPLE__)
675 /* always use generic keymaps */
676 if (!keyboard_layout)
677 keyboard_layout = "en-us";
679 if(keyboard_layout) {
680 kbd_layout = init_keyboard_layout(keyboard_layout);
688 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
689 if (SDL_Init (flags)) {
690 fprintf(stderr, "Could not initialize SDL - exiting\n");
694 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
695 dcl->dpy_update = sdl_update;
696 dcl->dpy_resize = sdl_resize;
697 dcl->dpy_refresh = sdl_refresh;
698 dcl->dpy_setdata = sdl_setdata;
699 dcl->dpy_fill = sdl_fill;
700 ds->mouse_set = sdl_mouse_warp;
701 ds->cursor_define = sdl_mouse_define;
702 register_displaychangelistener(ds, dcl);
704 sdl_update_caption();
705 SDL_EnableKeyRepeat(250, 50);
708 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
709 sdl_cursor_normal = SDL_GetCursor();
714 gui_fullscreen_initial_grab = 1;