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"
34 static SDL_Surface *screen;
35 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
36 static int last_vm_running;
37 static int gui_saved_grab;
38 static int gui_fullscreen;
39 static int gui_noframe;
40 static int gui_key_modifier_pressed;
41 static int gui_keysym;
42 static int gui_fullscreen_initial_grab;
43 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
44 static uint8_t modifiers_state[256];
45 static int width, height;
46 static SDL_Cursor *sdl_cursor_normal;
47 static SDL_Cursor *sdl_cursor_hidden;
48 static int absolute_enabled = 0;
49 static int guest_cursor = 0;
50 static int guest_x, guest_y;
51 static SDL_Cursor *guest_sprite = 0;
53 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
55 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
56 SDL_UpdateRect(screen, x, y, w, h);
59 static void sdl_resize(DisplayState *ds, int w, int h)
63 // printf("resizing to %d %d\n", w, h);
65 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
67 flags |= SDL_FULLSCREEN;
75 screen = SDL_SetVideoMode(w, h, 0, flags);
77 fprintf(stderr, "Could not open SDL display\n");
80 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
81 flags &= ~SDL_HWSURFACE;
85 if (!screen->pixels) {
86 fprintf(stderr, "Could not open SDL display\n");
89 ds->data = screen->pixels;
90 ds->linesize = screen->pitch;
91 ds->depth = screen->format->BitsPerPixel;
92 /* SDL BitsPerPixel never indicates any values other than
93 multiples of 8, so we need to check for strange depths. */
94 if (ds->depth == 16) {
97 mask = screen->format->Rmask;
98 mask |= screen->format->Gmask;
99 mask |= screen->format->Bmask;
100 if ((mask & 0x8000) == 0)
103 if (ds->depth == 32 && screen->format->Rshift == 0) {
112 /* generic keyboard conversion */
114 #include "sdl_keysym.h"
117 static kbd_layout_t *kbd_layout = NULL;
119 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
122 /* workaround for X11+SDL bug with AltGR */
123 keysym = ev->keysym.sym;
124 if (keysym == 0 && ev->keysym.scancode == 113)
126 /* For Japanese key '\' and '|' */
127 if (keysym == 92 && ev->keysym.scancode == 133) {
130 return keysym2scancode(kbd_layout, keysym);
133 /* specific keyboard conversions from scan codes */
137 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
139 return ev->keysym.scancode;
144 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
148 keycode = ev->keysym.scancode;
152 } else if (keycode < 97) {
153 keycode -= 8; /* just an offset */
154 } else if (keycode < 212) {
155 /* use conversion table */
156 keycode = _translate_keycode(keycode - 97);
165 static void reset_keys(void)
168 for(i = 0; i < 256; i++) {
169 if (modifiers_state[i]) {
171 kbd_put_keycode(0xe0);
172 kbd_put_keycode(i | 0x80);
173 modifiers_state[i] = 0;
178 static void sdl_process_key(SDL_KeyboardEvent *ev)
182 if (ev->keysym.sym == SDLK_PAUSE) {
185 if (ev->type == SDL_KEYUP)
187 kbd_put_keycode(0xe1);
188 kbd_put_keycode(0x1d | v);
189 kbd_put_keycode(0x45 | v);
194 keycode = sdl_keyevent_to_keycode_generic(ev);
196 keycode = sdl_keyevent_to_keycode(ev);
201 /* sent when leaving window: reset the modifiers state */
204 case 0x2a: /* Left Shift */
205 case 0x36: /* Right Shift */
206 case 0x1d: /* Left CTRL */
207 case 0x9d: /* Right CTRL */
208 case 0x38: /* Left ALT */
209 case 0xb8: /* Right ALT */
210 if (ev->type == SDL_KEYUP)
211 modifiers_state[keycode] = 0;
213 modifiers_state[keycode] = 1;
215 case 0x45: /* num lock */
216 case 0x3a: /* caps lock */
217 /* SDL does not send the key up event, so we generate it */
218 kbd_put_keycode(keycode);
219 kbd_put_keycode(keycode | 0x80);
223 /* now send the key code */
225 kbd_put_keycode(0xe0);
226 if (ev->type == SDL_KEYUP)
227 kbd_put_keycode(keycode | 0x80);
229 kbd_put_keycode(keycode & 0x7f);
232 static void sdl_update_caption(void)
235 const char *status = "";
238 status = " [Stopped]";
241 status = " - Press Ctrl-Alt to exit grab";
243 status = " - Press Ctrl-Alt-Shift to exit grab";
247 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
249 snprintf(buf, sizeof(buf), "QEMU%s", status);
251 SDL_WM_SetCaption(buf, "QEMU");
254 static void sdl_hide_cursor(void)
259 if (kbd_mouse_is_absolute()) {
261 SDL_SetCursor(sdl_cursor_hidden);
267 static void sdl_show_cursor(void)
272 if (!kbd_mouse_is_absolute()) {
275 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
276 SDL_SetCursor(guest_sprite);
278 SDL_SetCursor(sdl_cursor_normal);
282 static void sdl_grab_start(void)
285 SDL_SetCursor(guest_sprite);
286 SDL_WarpMouse(guest_x, guest_y);
290 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
292 sdl_update_caption();
297 static void sdl_grab_end(void)
299 SDL_WM_GrabInput(SDL_GRAB_OFF);
302 sdl_update_caption();
305 static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
309 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
310 buttons |= MOUSE_EVENT_LBUTTON;
311 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
312 buttons |= MOUSE_EVENT_RBUTTON;
313 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
314 buttons |= MOUSE_EVENT_MBUTTON;
316 if (kbd_mouse_is_absolute()) {
317 if (!absolute_enabled) {
322 absolute_enabled = 1;
325 dx = x * 0x7FFF / (width - 1);
326 dy = y * 0x7FFF / (height - 1);
327 } else if (absolute_enabled) {
329 absolute_enabled = 0;
330 } else if (guest_cursor) {
339 kbd_mouse_event(dx, dy, dz, buttons);
342 static void toggle_full_screen(DisplayState *ds)
344 gui_fullscreen = !gui_fullscreen;
345 sdl_resize(ds, screen->w, screen->h);
346 if (gui_fullscreen) {
347 gui_saved_grab = gui_grab;
357 static void sdl_refresh(DisplayState *ds)
359 SDL_Event ev1, *ev = &ev1;
361 int buttonstate = SDL_GetMouseState(NULL, NULL);
363 if (last_vm_running != vm_running) {
364 last_vm_running = vm_running;
365 sdl_update_caption();
369 SDL_EnableUNICODE(!is_graphic_console());
371 while (SDL_PollEvent(ev)) {
373 case SDL_VIDEOEXPOSE:
374 sdl_update(ds, 0, 0, screen->w, screen->h);
378 if (ev->type == SDL_KEYDOWN) {
380 mod_state = (SDL_GetModState() & gui_grab_code) ==
383 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
384 (gui_grab_code | KMOD_LSHIFT);
386 gui_key_modifier_pressed = mod_state;
387 if (gui_key_modifier_pressed) {
389 keycode = sdl_keyevent_to_keycode(&ev->key);
391 case 0x21: /* 'f' key on US keyboard */
392 toggle_full_screen(ds);
395 case 0x02 ... 0x0a: /* '1' to '9' keys */
396 /* Reset the modifiers sent to the current console */
398 console_select(keycode - 0x02);
399 if (!is_graphic_console()) {
400 /* display grab if going to a text console */
409 } else if (!is_graphic_console()) {
412 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
413 switch(ev->key.keysym.sym) {
414 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
415 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
416 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
417 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
418 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
419 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
420 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
421 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
425 switch(ev->key.keysym.sym) {
426 case SDLK_UP: keysym = QEMU_KEY_UP; break;
427 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
428 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
429 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
430 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
431 case SDLK_END: keysym = QEMU_KEY_END; break;
432 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
433 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
434 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
435 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
440 kbd_put_keysym(keysym);
441 } else if (ev->key.keysym.unicode != 0) {
442 kbd_put_keysym(ev->key.keysym.unicode);
445 } else if (ev->type == SDL_KEYUP) {
447 mod_state = (ev->key.keysym.mod & gui_grab_code);
449 mod_state = (ev->key.keysym.mod &
450 (gui_grab_code | KMOD_LSHIFT));
453 if (gui_key_modifier_pressed) {
454 gui_key_modifier_pressed = 0;
455 if (gui_keysym == 0) {
456 /* exit/enter grab if pressing Ctrl-Alt */
458 /* if the application is not active,
459 do not try to enter grab state. It
461 'SDL_WM_GrabInput(SDL_GRAB_ON)'
462 from blocking all the application
464 if (SDL_GetAppState() & SDL_APPACTIVE)
469 /* SDL does not send back all the
470 modifiers key, so we must correct it */
478 if (is_graphic_console() && !gui_keysym)
479 sdl_process_key(&ev->key);
483 qemu_system_shutdown_request();
485 case SDL_MOUSEMOTION:
486 if (gui_grab || kbd_mouse_is_absolute() ||
488 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
489 ev->motion.x, ev->motion.y, ev->motion.state);
492 case SDL_MOUSEBUTTONDOWN:
493 case SDL_MOUSEBUTTONUP:
495 SDL_MouseButtonEvent *bev = &ev->button;
496 if (!gui_grab && !kbd_mouse_is_absolute()) {
497 if (ev->type == SDL_MOUSEBUTTONDOWN &&
498 (bev->button == SDL_BUTTON_LEFT)) {
499 /* start grabbing all events */
505 if (ev->type == SDL_MOUSEBUTTONDOWN) {
506 buttonstate |= SDL_BUTTON(bev->button);
508 buttonstate &= ~SDL_BUTTON(bev->button);
510 #ifdef SDL_BUTTON_WHEELUP
511 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
513 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
517 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
521 case SDL_ACTIVEEVENT:
522 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
523 !ev->active.gain && !gui_fullscreen_initial_grab) {
526 if (ev->active.state & SDL_APPACTIVE) {
527 if (ev->active.gain) {
528 /* Back to default interval */
529 ds->gui_timer_interval = 0;
532 /* Sleeping interval */
533 ds->gui_timer_interval = 500;
544 static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
546 SDL_Rect dst = { x, y, w, h };
547 SDL_FillRect(screen, &dst, c);
550 static void sdl_mouse_warp(int x, int y, int on)
555 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
556 SDL_SetCursor(guest_sprite);
562 guest_x = x, guest_y = y;
565 static void sdl_mouse_define(int width, int height, int bpp,
566 int hot_x, int hot_y,
567 uint8_t *image, uint8_t *mask)
569 uint8_t sprite[256], *line;
570 int x, y, dst, bypl, src = 0;
572 SDL_FreeCursor(guest_sprite);
574 memset(sprite, 0, 256);
575 bypl = ((width * bpp + 31) >> 5) << 2;
576 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
578 for (x = 0; x < width; x ++, dst ++) {
581 src = *(line ++); src |= *(line ++); src |= *(line ++);
585 src = *(line ++); src |= *(line ++);
591 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
594 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
597 src = 1 & (line[x >> 3] >> (x & 7));
601 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
604 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
607 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
608 SDL_SetCursor(guest_sprite);
611 static void sdl_cleanup(void)
614 SDL_FreeCursor(guest_sprite);
618 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
623 #if defined(__APPLE__)
624 /* always use generic keymaps */
625 if (!keyboard_layout)
626 keyboard_layout = "en-us";
628 if(keyboard_layout) {
629 kbd_layout = init_keyboard_layout(keyboard_layout);
637 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
638 if (SDL_Init (flags)) {
639 fprintf(stderr, "Could not initialize SDL - exiting\n");
643 ds->dpy_update = sdl_update;
644 ds->dpy_resize = sdl_resize;
645 ds->dpy_refresh = sdl_refresh;
646 ds->dpy_fill = sdl_fill;
647 ds->mouse_set = sdl_mouse_warp;
648 ds->cursor_define = sdl_mouse_define;
650 sdl_resize(ds, 640, 400);
651 sdl_update_caption();
652 SDL_EnableKeyRepeat(250, 50);
655 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
656 sdl_cursor_normal = SDL_GetCursor();
661 gui_fullscreen_initial_grab = 1;