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
;
88 /* generic keyboard conversion */
90 #include "sdl_keysym.h"
93 static kbd_layout_t
*kbd_layout
= NULL
;
95 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
98 /* workaround for X11+SDL bug with AltGR */
99 keysym
= ev
->keysym
.sym
;
100 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
102 return keysym2scancode(kbd_layout
, keysym
);
105 /* specific keyboard conversions from scan codes */
109 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
111 return ev
->keysym
.scancode
;
116 static const uint8_t x_keycode_to_pc_keycode
[61] = {
122 0xcd, /* 102 Right */
128 0x9c, /* 108 Enter */
129 0x9d, /* 109 Ctrl-R */
131 0xb7, /* 111 Print */
132 0xb5, /* 112 Divide */
133 0xb8, /* 113 Alt-R */
134 0xc6, /* 114 Break */
140 0x70, /* 120 Hiragana_Katakana */
143 0x73, /* 123 backslash */
149 0x79, /* 129 Henkan */
151 0x7b, /* 131 Muhenkan */
167 0x47, /* 147 KP_HOME */
168 0x48, /* 148 KP_UP */
169 0x49, /* 149 KP_PgUp */
170 0x4b, /* 150 KP_Left */
172 0x4d, /* 152 KP_Right */
173 0x4f, /* 153 KP_End */
174 0x50, /* 154 KP_Down */
175 0x51, /* 155 KP_PgDn */
176 0x52, /* 156 KP_Ins */
177 0x53, /* 157 KP_Del */
180 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
184 keycode
= ev
->keysym
.scancode
;
188 } else if (keycode
< 97) {
189 keycode
-= 8; /* just an offset */
190 } else if (keycode
< 158) {
191 /* use conversion table */
192 keycode
= x_keycode_to_pc_keycode
[keycode
- 97];
201 static void reset_keys(void)
204 for(i
= 0; i
< 256; i
++) {
205 if (modifiers_state
[i
]) {
207 kbd_put_keycode(0xe0);
208 kbd_put_keycode(i
| 0x80);
209 modifiers_state
[i
] = 0;
214 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
218 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
221 if (ev
->type
== SDL_KEYUP
)
223 kbd_put_keycode(0xe1);
224 kbd_put_keycode(0x1d | v
);
225 kbd_put_keycode(0x45 | v
);
230 keycode
= sdl_keyevent_to_keycode_generic(ev
);
232 keycode
= sdl_keyevent_to_keycode(ev
);
237 /* sent when leaving window: reset the modifiers state */
240 case 0x2a: /* Left Shift */
241 case 0x36: /* Right Shift */
242 case 0x1d: /* Left CTRL */
243 case 0x9d: /* Right CTRL */
244 case 0x38: /* Left ALT */
245 case 0xb8: /* Right ALT */
246 if (ev
->type
== SDL_KEYUP
)
247 modifiers_state
[keycode
] = 0;
249 modifiers_state
[keycode
] = 1;
251 case 0x45: /* num lock */
252 case 0x3a: /* caps lock */
253 /* SDL does not send the key up event, so we generate it */
254 kbd_put_keycode(keycode
);
255 kbd_put_keycode(keycode
| 0x80);
259 /* now send the key code */
261 kbd_put_keycode(0xe0);
262 if (ev
->type
== SDL_KEYUP
)
263 kbd_put_keycode(keycode
| 0x80);
265 kbd_put_keycode(keycode
& 0x7f);
268 static void sdl_update_caption(void)
273 strcat(buf
, " [Stopped]");
276 strcat(buf
, " - Press Ctrl-Alt to exit grab");
278 SDL_WM_SetCaption(buf
, "QEMU");
281 static void sdl_hide_cursor(void)
283 SDL_SetCursor(sdl_cursor_hidden
);
286 static void sdl_show_cursor(void)
288 if (!kbd_mouse_is_absolute()) {
289 SDL_SetCursor(sdl_cursor_normal
);
293 static void sdl_grab_start(void)
296 SDL_WM_GrabInput(SDL_GRAB_ON
);
297 /* dummy read to avoid moving the mouse */
298 SDL_GetRelativeMouseState(NULL
, NULL
);
300 sdl_update_caption();
303 static void sdl_grab_end(void)
305 SDL_WM_GrabInput(SDL_GRAB_OFF
);
308 sdl_update_caption();
311 static void sdl_send_mouse_event(int dz
)
313 int dx
, dy
, state
, buttons
;
314 state
= SDL_GetRelativeMouseState(&dx
, &dy
);
316 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
317 buttons
|= MOUSE_EVENT_LBUTTON
;
318 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
319 buttons
|= MOUSE_EVENT_RBUTTON
;
320 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
321 buttons
|= MOUSE_EVENT_MBUTTON
;
323 if (kbd_mouse_is_absolute()) {
324 if (!absolute_enabled
) {
329 absolute_enabled
= 1;
332 SDL_GetMouseState(&dx
, &dy
);
333 dx
= dx
* 0x7FFF / width
;
334 dy
= dy
* 0x7FFF / height
;
337 kbd_mouse_event(dx
, dy
, dz
, buttons
);
340 static void toggle_full_screen(DisplayState
*ds
)
342 gui_fullscreen
= !gui_fullscreen
;
343 sdl_resize(ds
, screen
->w
, screen
->h
);
344 if (gui_fullscreen
) {
345 gui_saved_grab
= gui_grab
;
355 static void sdl_refresh(DisplayState
*ds
)
357 SDL_Event ev1
, *ev
= &ev1
;
360 if (last_vm_running
!= vm_running
) {
361 last_vm_running
= vm_running
;
362 sdl_update_caption();
367 while (SDL_PollEvent(ev
)) {
369 case SDL_VIDEOEXPOSE
:
370 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
374 if (ev
->type
== SDL_KEYDOWN
) {
375 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
377 gui_key_modifier_pressed
= mod_state
;
378 if (gui_key_modifier_pressed
) {
380 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
382 case 0x21: /* 'f' key on US keyboard */
383 toggle_full_screen(ds
);
386 case 0x02 ... 0x0a: /* '1' to '9' keys */
387 console_select(keycode
- 0x02);
388 if (!is_graphic_console()) {
389 /* display grab if going to a text console */
398 } else if (!is_graphic_console()) {
401 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
402 switch(ev
->key
.keysym
.sym
) {
403 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
404 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
405 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
406 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
407 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
408 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
409 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
410 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
414 switch(ev
->key
.keysym
.sym
) {
415 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
416 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
417 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
418 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
419 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
420 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
421 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
422 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
423 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break; case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
428 kbd_put_keysym(keysym
);
429 } else if (ev
->key
.keysym
.unicode
!= 0) {
430 kbd_put_keysym(ev
->key
.keysym
.unicode
);
433 } else if (ev
->type
== SDL_KEYUP
) {
434 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
436 if (gui_key_modifier_pressed
) {
437 gui_key_modifier_pressed
= 0;
438 if (gui_keysym
== 0) {
439 /* exit/enter grab if pressing Ctrl-Alt */
444 /* SDL does not send back all the
445 modifiers key, so we must correct it */
453 if (is_graphic_console())
454 sdl_process_key(&ev
->key
);
457 qemu_system_shutdown_request();
459 case SDL_MOUSEMOTION
:
460 if (gui_grab
|| kbd_mouse_is_absolute()) {
461 sdl_send_mouse_event(0);
464 case SDL_MOUSEBUTTONDOWN
:
465 case SDL_MOUSEBUTTONUP
:
467 SDL_MouseButtonEvent
*bev
= &ev
->button
;
468 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
469 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
470 (bev
->state
& SDL_BUTTON_LMASK
)) {
471 /* start grabbing all events */
477 #ifdef SDL_BUTTON_WHEELUP
478 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
480 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
484 sdl_send_mouse_event(dz
);
488 case SDL_ACTIVEEVENT
:
489 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
490 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
500 static void sdl_cleanup(void)
505 void sdl_display_init(DisplayState
*ds
, int full_screen
)
510 #if defined(__APPLE__)
511 /* always use generic keymaps */
512 if (!keyboard_layout
)
513 keyboard_layout
= "en-us";
515 if(keyboard_layout
) {
516 kbd_layout
= init_keyboard_layout(keyboard_layout
);
521 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
522 if (SDL_Init (flags
)) {
523 fprintf(stderr
, "Could not initialize SDL - exiting\n");
527 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
528 signal(SIGINT
, SIG_DFL
);
529 signal(SIGQUIT
, SIG_DFL
);
532 ds
->dpy_update
= sdl_update
;
533 ds
->dpy_resize
= sdl_resize
;
534 ds
->dpy_refresh
= sdl_refresh
;
536 sdl_resize(ds
, 640, 400);
537 sdl_update_caption();
538 SDL_EnableKeyRepeat(250, 50);
539 SDL_EnableUNICODE(1);
542 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
543 sdl_cursor_normal
= SDL_GetCursor();
548 gui_fullscreen_initial_grab
= 1;