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 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_noframe;
38 static int gui_key_modifier_pressed;
39 static int gui_keysym;
40 static int gui_fullscreen_initial_grab;
41 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
42 static uint8_t modifiers_state[256];
43 static int width, height;
44 static SDL_Cursor *sdl_cursor_normal;
45 static SDL_Cursor *sdl_cursor_hidden;
46 static int absolute_enabled = 0;
48 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
50 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
51 SDL_UpdateRect(screen, x, y, w, h);
54 static void sdl_resize(DisplayState *ds, int w, int h)
58 // printf("resizing to %d %d\n", w, h);
60 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
62 flags |= SDL_FULLSCREEN;
70 screen = SDL_SetVideoMode(w, h, 0, flags);
72 fprintf(stderr, "Could not open SDL display\n");
75 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
76 flags &= ~SDL_HWSURFACE;
80 if (!screen->pixels) {
81 fprintf(stderr, "Could not open SDL display\n");
84 ds->data = screen->pixels;
85 ds->linesize = screen->pitch;
86 ds->depth = screen->format->BitsPerPixel;
87 if (ds->depth == 32 && screen->format->Rshift == 0) {
96 /* generic keyboard conversion */
98 #include "sdl_keysym.h"
101 static kbd_layout_t *kbd_layout = NULL;
103 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
106 /* workaround for X11+SDL bug with AltGR */
107 keysym = ev->keysym.sym;
108 if (keysym == 0 && ev->keysym.scancode == 113)
110 /* For Japanese key '\' and '|' */
111 if (keysym == 92 && ev->keysym.scancode == 133) {
114 return keysym2scancode(kbd_layout, keysym);
117 /* specific keyboard conversions from scan codes */
121 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
123 return ev->keysym.scancode;
128 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
132 keycode = ev->keysym.scancode;
136 } else if (keycode < 97) {
137 keycode -= 8; /* just an offset */
138 } else if (keycode < 212) {
139 /* use conversion table */
140 keycode = _translate_keycode(keycode - 97);
149 static void reset_keys(void)
152 for(i = 0; i < 256; i++) {
153 if (modifiers_state[i]) {
155 kbd_put_keycode(0xe0);
156 kbd_put_keycode(i | 0x80);
157 modifiers_state[i] = 0;
162 static void sdl_process_key(SDL_KeyboardEvent *ev)
166 if (ev->keysym.sym == SDLK_PAUSE) {
169 if (ev->type == SDL_KEYUP)
171 kbd_put_keycode(0xe1);
172 kbd_put_keycode(0x1d | v);
173 kbd_put_keycode(0x45 | v);
178 keycode = sdl_keyevent_to_keycode_generic(ev);
180 keycode = sdl_keyevent_to_keycode(ev);
185 /* sent when leaving window: reset the modifiers state */
188 case 0x2a: /* Left Shift */
189 case 0x36: /* Right Shift */
190 case 0x1d: /* Left CTRL */
191 case 0x9d: /* Right CTRL */
192 case 0x38: /* Left ALT */
193 case 0xb8: /* Right ALT */
194 if (ev->type == SDL_KEYUP)
195 modifiers_state[keycode] = 0;
197 modifiers_state[keycode] = 1;
199 case 0x45: /* num lock */
200 case 0x3a: /* caps lock */
201 /* SDL does not send the key up event, so we generate it */
202 kbd_put_keycode(keycode);
203 kbd_put_keycode(keycode | 0x80);
207 /* now send the key code */
209 kbd_put_keycode(0xe0);
210 if (ev->type == SDL_KEYUP)
211 kbd_put_keycode(keycode | 0x80);
213 kbd_put_keycode(keycode & 0x7f);
216 static void sdl_update_caption(void)
219 const char *status = "";
222 status = " [Stopped]";
224 status = " - Press Ctrl-Alt to exit grab";
227 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
229 snprintf(buf, sizeof(buf), "QEMU%s", status);
231 SDL_WM_SetCaption(buf, "QEMU");
234 static void sdl_hide_cursor(void)
236 if (kbd_mouse_is_absolute()) {
238 SDL_SetCursor(sdl_cursor_hidden);
244 static void sdl_show_cursor(void)
246 if (!kbd_mouse_is_absolute()) {
248 SDL_SetCursor(sdl_cursor_normal);
252 static void sdl_grab_start(void)
255 SDL_WM_GrabInput(SDL_GRAB_ON);
256 /* dummy read to avoid moving the mouse */
257 SDL_GetRelativeMouseState(NULL, NULL);
259 sdl_update_caption();
262 static void sdl_grab_end(void)
264 SDL_WM_GrabInput(SDL_GRAB_OFF);
267 sdl_update_caption();
270 static void sdl_send_mouse_event(int dz)
272 int dx, dy, state, buttons;
273 state = SDL_GetRelativeMouseState(&dx, &dy);
275 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
276 buttons |= MOUSE_EVENT_LBUTTON;
277 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
278 buttons |= MOUSE_EVENT_RBUTTON;
279 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
280 buttons |= MOUSE_EVENT_MBUTTON;
282 if (kbd_mouse_is_absolute()) {
283 if (!absolute_enabled) {
288 absolute_enabled = 1;
291 SDL_GetMouseState(&dx, &dy);
292 dx = dx * 0x7FFF / width;
293 dy = dy * 0x7FFF / height;
294 } else if (absolute_enabled) {
296 absolute_enabled = 0;
299 kbd_mouse_event(dx, dy, dz, buttons);
302 static void toggle_full_screen(DisplayState *ds)
304 gui_fullscreen = !gui_fullscreen;
305 sdl_resize(ds, screen->w, screen->h);
306 if (gui_fullscreen) {
307 gui_saved_grab = gui_grab;
317 static void sdl_refresh(DisplayState *ds)
319 SDL_Event ev1, *ev = &ev1;
322 if (last_vm_running != vm_running) {
323 last_vm_running = vm_running;
324 sdl_update_caption();
329 while (SDL_PollEvent(ev)) {
331 case SDL_VIDEOEXPOSE:
332 sdl_update(ds, 0, 0, screen->w, screen->h);
336 if (ev->type == SDL_KEYDOWN) {
337 mod_state = (SDL_GetModState() & gui_grab_code) ==
339 gui_key_modifier_pressed = mod_state;
340 if (gui_key_modifier_pressed) {
342 keycode = sdl_keyevent_to_keycode(&ev->key);
344 case 0x21: /* 'f' key on US keyboard */
345 toggle_full_screen(ds);
348 case 0x02 ... 0x0a: /* '1' to '9' keys */
349 /* Reset the modifiers sent to the current console */
351 console_select(keycode - 0x02);
352 if (!is_graphic_console()) {
353 /* display grab if going to a text console */
362 } else if (!is_graphic_console()) {
365 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
366 switch(ev->key.keysym.sym) {
367 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
368 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
369 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
370 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
371 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
372 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
373 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
374 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
378 switch(ev->key.keysym.sym) {
379 case SDLK_UP: keysym = QEMU_KEY_UP; break;
380 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
381 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
382 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
383 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
384 case SDLK_END: keysym = QEMU_KEY_END; break;
385 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
386 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
387 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
392 kbd_put_keysym(keysym);
393 } else if (ev->key.keysym.unicode != 0) {
394 kbd_put_keysym(ev->key.keysym.unicode);
397 } else if (ev->type == SDL_KEYUP) {
398 mod_state = (ev->key.keysym.mod & gui_grab_code);
400 if (gui_key_modifier_pressed) {
401 gui_key_modifier_pressed = 0;
402 if (gui_keysym == 0) {
403 /* exit/enter grab if pressing Ctrl-Alt */
405 /* if the application is not active,
406 do not try to enter grab state. It
408 'SDL_WM_GrabInput(SDL_GRAB_ON)'
409 from blocking all the application
411 if (SDL_GetAppState() & SDL_APPACTIVE)
416 /* SDL does not send back all the
417 modifiers key, so we must correct it */
425 if (is_graphic_console() && !gui_keysym)
426 sdl_process_key(&ev->key);
430 qemu_system_shutdown_request();
433 case SDL_MOUSEMOTION:
434 if (gui_grab || kbd_mouse_is_absolute() ||
436 sdl_send_mouse_event(0);
439 case SDL_MOUSEBUTTONDOWN:
440 case SDL_MOUSEBUTTONUP:
442 SDL_MouseButtonEvent *bev = &ev->button;
443 if (!gui_grab && !kbd_mouse_is_absolute()) {
444 if (ev->type == SDL_MOUSEBUTTONDOWN &&
445 (bev->state & SDL_BUTTON_LMASK)) {
446 /* start grabbing all events */
452 #ifdef SDL_BUTTON_WHEELUP
453 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
455 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
459 sdl_send_mouse_event(dz);
463 case SDL_ACTIVEEVENT:
464 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
465 !ev->active.gain && !gui_fullscreen_initial_grab) {
475 static void sdl_cleanup(void)
480 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
485 #if defined(__APPLE__)
486 /* always use generic keymaps */
487 if (!keyboard_layout)
488 keyboard_layout = "en-us";
490 if(keyboard_layout) {
491 kbd_layout = init_keyboard_layout(keyboard_layout);
499 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
500 if (SDL_Init (flags)) {
501 fprintf(stderr, "Could not initialize SDL - exiting\n");
505 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
506 signal(SIGINT, SIG_DFL);
507 signal(SIGQUIT, SIG_DFL);
510 ds->dpy_update = sdl_update;
511 ds->dpy_resize = sdl_resize;
512 ds->dpy_refresh = sdl_refresh;
514 sdl_resize(ds, 640, 400);
515 sdl_update_caption();
516 SDL_EnableKeyRepeat(250, 50);
517 SDL_EnableUNICODE(1);
520 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
521 sdl_cursor_normal = SDL_GetCursor();
526 gui_fullscreen_initial_grab = 1;