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_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];
42 static int width
, height
;
43 static SDL_Cursor
*sdl_cursor_normal
;
44 static SDL_Cursor
*sdl_cursor_hidden
;
45 static int absolute_enabled
= 0;
47 static void sdl_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
49 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
50 SDL_UpdateRect(screen
, x
, y
, w
, h
);
53 static void sdl_resize(DisplayState
*ds
, int w
, int h
)
57 // printf("resizing to %d %d\n", w, h);
59 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
61 flags
|= SDL_FULLSCREEN
;
67 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
69 fprintf(stderr
, "Could not open SDL display\n");
72 if (!screen
->pixels
&& (flags
& SDL_HWSURFACE
) && (flags
& SDL_FULLSCREEN
)) {
73 flags
&= ~SDL_HWSURFACE
;
77 if (!screen
->pixels
) {
78 fprintf(stderr
, "Could not open SDL display\n");
81 ds
->data
= screen
->pixels
;
82 ds
->linesize
= screen
->pitch
;
83 ds
->depth
= screen
->format
->BitsPerPixel
;
84 if (ds
->depth
== 32 && screen
->format
->Rshift
== 0) {
93 /* generic keyboard conversion */
95 #include "sdl_keysym.h"
98 static kbd_layout_t
*kbd_layout
= NULL
;
100 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
103 /* workaround for X11+SDL bug with AltGR */
104 keysym
= ev
->keysym
.sym
;
105 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
107 /* For Japanese key '\' and '|' */
108 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
111 return keysym2scancode(kbd_layout
, keysym
);
114 /* specific keyboard conversions from scan codes */
118 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
120 return ev
->keysym
.scancode
;
125 static const uint8_t x_keycode_to_pc_keycode
[115] = {
131 0xcd, /* 102 Right */
137 0x9c, /* 108 Enter */
138 0x9d, /* 109 Ctrl-R */
140 0xb7, /* 111 Print */
141 0xb5, /* 112 Divide */
142 0xb8, /* 113 Alt-R */
143 0xc6, /* 114 Break */
158 0x79, /* 129 Henkan */
160 0x7b, /* 131 Muhenkan */
176 0x47, /* 147 KP_HOME */
177 0x48, /* 148 KP_UP */
178 0x49, /* 149 KP_PgUp */
179 0x4b, /* 150 KP_Left */
181 0x4d, /* 152 KP_Right */
182 0x4f, /* 153 KP_End */
183 0x50, /* 154 KP_Down */
184 0x51, /* 155 KP_PgDn */
185 0x52, /* 156 KP_Ins */
186 0x53, /* 157 KP_Del */
190 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 170 */
191 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 180 */
192 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 190 */
193 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 200 */
201 0x70, /* 208 Hiragana_Katakana */
204 0x73, /* 211 backslash */
207 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
211 keycode
= ev
->keysym
.scancode
;
215 } else if (keycode
< 97) {
216 keycode
-= 8; /* just an offset */
217 } else if (keycode
< 212) {
218 /* use conversion table */
219 keycode
= x_keycode_to_pc_keycode
[keycode
- 97];
228 static void reset_keys(void)
231 for(i
= 0; i
< 256; i
++) {
232 if (modifiers_state
[i
]) {
234 kbd_put_keycode(0xe0);
235 kbd_put_keycode(i
| 0x80);
236 modifiers_state
[i
] = 0;
241 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
245 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
248 if (ev
->type
== SDL_KEYUP
)
250 kbd_put_keycode(0xe1);
251 kbd_put_keycode(0x1d | v
);
252 kbd_put_keycode(0x45 | v
);
257 keycode
= sdl_keyevent_to_keycode_generic(ev
);
259 keycode
= sdl_keyevent_to_keycode(ev
);
264 /* sent when leaving window: reset the modifiers state */
267 case 0x2a: /* Left Shift */
268 case 0x36: /* Right Shift */
269 case 0x1d: /* Left CTRL */
270 case 0x9d: /* Right CTRL */
271 case 0x38: /* Left ALT */
272 case 0xb8: /* Right ALT */
273 if (ev
->type
== SDL_KEYUP
)
274 modifiers_state
[keycode
] = 0;
276 modifiers_state
[keycode
] = 1;
278 case 0x45: /* num lock */
279 case 0x3a: /* caps lock */
280 /* SDL does not send the key up event, so we generate it */
281 kbd_put_keycode(keycode
);
282 kbd_put_keycode(keycode
| 0x80);
286 /* now send the key code */
288 kbd_put_keycode(0xe0);
289 if (ev
->type
== SDL_KEYUP
)
290 kbd_put_keycode(keycode
| 0x80);
292 kbd_put_keycode(keycode
& 0x7f);
295 static void sdl_update_caption(void)
300 strcat(buf
, " [Stopped]");
303 strcat(buf
, " - Press Ctrl-Alt to exit grab");
305 SDL_WM_SetCaption(buf
, "QEMU");
308 static void sdl_hide_cursor(void)
310 if (kbd_mouse_is_absolute()) {
312 SDL_SetCursor(sdl_cursor_hidden
);
318 static void sdl_show_cursor(void)
320 if (!kbd_mouse_is_absolute()) {
325 static void sdl_grab_start(void)
328 SDL_WM_GrabInput(SDL_GRAB_ON
);
329 /* dummy read to avoid moving the mouse */
330 SDL_GetRelativeMouseState(NULL
, NULL
);
332 sdl_update_caption();
335 static void sdl_grab_end(void)
337 SDL_WM_GrabInput(SDL_GRAB_OFF
);
340 sdl_update_caption();
343 static void sdl_send_mouse_event(int dz
)
345 int dx
, dy
, state
, buttons
;
346 state
= SDL_GetRelativeMouseState(&dx
, &dy
);
348 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
349 buttons
|= MOUSE_EVENT_LBUTTON
;
350 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
351 buttons
|= MOUSE_EVENT_RBUTTON
;
352 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
353 buttons
|= MOUSE_EVENT_MBUTTON
;
355 if (kbd_mouse_is_absolute()) {
356 if (!absolute_enabled
) {
361 absolute_enabled
= 1;
364 SDL_GetMouseState(&dx
, &dy
);
365 dx
= dx
* 0x7FFF / width
;
366 dy
= dy
* 0x7FFF / height
;
369 kbd_mouse_event(dx
, dy
, dz
, buttons
);
372 static void toggle_full_screen(DisplayState
*ds
)
374 gui_fullscreen
= !gui_fullscreen
;
375 sdl_resize(ds
, screen
->w
, screen
->h
);
376 if (gui_fullscreen
) {
377 gui_saved_grab
= gui_grab
;
387 static void sdl_refresh(DisplayState
*ds
)
389 SDL_Event ev1
, *ev
= &ev1
;
392 if (last_vm_running
!= vm_running
) {
393 last_vm_running
= vm_running
;
394 sdl_update_caption();
399 while (SDL_PollEvent(ev
)) {
401 case SDL_VIDEOEXPOSE
:
402 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
406 if (ev
->type
== SDL_KEYDOWN
) {
407 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
409 gui_key_modifier_pressed
= mod_state
;
410 if (gui_key_modifier_pressed
) {
412 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
414 case 0x21: /* 'f' key on US keyboard */
415 toggle_full_screen(ds
);
418 case 0x02 ... 0x0a: /* '1' to '9' keys */
419 /* Reset the modifiers sent to the current console */
421 console_select(keycode
- 0x02);
422 if (!is_graphic_console()) {
423 /* display grab if going to a text console */
432 } else if (!is_graphic_console()) {
435 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
436 switch(ev
->key
.keysym
.sym
) {
437 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
438 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
439 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
440 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
441 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
442 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
443 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
444 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
448 switch(ev
->key
.keysym
.sym
) {
449 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
450 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
451 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
452 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
453 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
454 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
455 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
456 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
457 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break; case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
462 kbd_put_keysym(keysym
);
463 } else if (ev
->key
.keysym
.unicode
!= 0) {
464 kbd_put_keysym(ev
->key
.keysym
.unicode
);
467 } else if (ev
->type
== SDL_KEYUP
) {
468 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
470 if (gui_key_modifier_pressed
) {
471 gui_key_modifier_pressed
= 0;
472 if (gui_keysym
== 0) {
473 /* exit/enter grab if pressing Ctrl-Alt */
475 /* if the application is not active,
476 do not try to enter grab state. It
478 'SDL_WM_GrabInput(SDL_GRAB_ON)'
479 from blocking all the application
481 if (SDL_GetAppState() & SDL_APPACTIVE
)
486 /* SDL does not send back all the
487 modifiers key, so we must correct it */
495 if (is_graphic_console() && !gui_keysym
)
496 sdl_process_key(&ev
->key
);
499 qemu_system_shutdown_request();
501 case SDL_MOUSEMOTION
:
502 if (gui_grab
|| kbd_mouse_is_absolute()) {
503 sdl_send_mouse_event(0);
506 case SDL_MOUSEBUTTONDOWN
:
507 case SDL_MOUSEBUTTONUP
:
509 SDL_MouseButtonEvent
*bev
= &ev
->button
;
510 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
511 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
512 (bev
->state
& SDL_BUTTON_LMASK
)) {
513 /* start grabbing all events */
519 #ifdef SDL_BUTTON_WHEELUP
520 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
522 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
526 sdl_send_mouse_event(dz
);
530 case SDL_ACTIVEEVENT
:
531 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
532 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
542 static void sdl_cleanup(void)
547 void sdl_display_init(DisplayState
*ds
, int full_screen
)
552 #if defined(__APPLE__)
553 /* always use generic keymaps */
554 if (!keyboard_layout
)
555 keyboard_layout
= "en-us";
557 if(keyboard_layout
) {
558 kbd_layout
= init_keyboard_layout(keyboard_layout
);
563 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
564 if (SDL_Init (flags
)) {
565 fprintf(stderr
, "Could not initialize SDL - exiting\n");
569 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
570 signal(SIGINT
, SIG_DFL
);
571 signal(SIGQUIT
, SIG_DFL
);
574 ds
->dpy_update
= sdl_update
;
575 ds
->dpy_resize
= sdl_resize
;
576 ds
->dpy_refresh
= sdl_refresh
;
578 sdl_resize(ds
, 640, 400);
579 sdl_update_caption();
580 SDL_EnableKeyRepeat(250, 50);
581 SDL_EnableUNICODE(1);
584 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
585 sdl_cursor_normal
= SDL_GetCursor();
590 gui_fullscreen_initial_grab
= 1;