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];
43 static void sdl_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
45 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
46 SDL_UpdateRect(screen
, x
, y
, w
, h
);
49 static void sdl_resize(DisplayState
*ds
, int w
, int h
)
53 // printf("resizing to %d %d\n", w, h);
55 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
56 flags
|= SDL_RESIZABLE
;
58 flags
|= SDL_FULLSCREEN
;
59 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
61 fprintf(stderr
, "Could not open SDL display\n");
64 ds
->data
= screen
->pixels
;
65 ds
->linesize
= screen
->pitch
;
66 ds
->depth
= screen
->format
->BitsPerPixel
;
71 /* generic keyboard conversion */
73 #include "sdl_keysym.h"
76 static kbd_layout_t
*kbd_layout
= NULL
;
78 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
81 /* workaround for X11+SDL bug with AltGR */
82 keysym
= ev
->keysym
.sym
;
83 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
85 return keysym2scancode(kbd_layout
, keysym
);
88 /* specific keyboard conversions from scan codes */
92 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
94 return ev
->keysym
.scancode
;
99 static const uint8_t x_keycode_to_pc_keycode
[61] = {
105 0xcd, /* 102 Right */
111 0x9c, /* 108 Enter */
112 0x9d, /* 109 Ctrl-R */
114 0xb7, /* 111 Print */
115 0xb5, /* 112 Divide */
116 0xb8, /* 113 Alt-R */
117 0xc6, /* 114 Break */
123 0x70, /* 120 Hiragana_Katakana */
126 0x73, /* 123 backslash */
132 0x79, /* 129 Henkan */
134 0x7b, /* 131 Muhenkan */
150 0x47, /* 147 KP_HOME */
151 0x48, /* 148 KP_UP */
152 0x49, /* 149 KP_PgUp */
153 0x4b, /* 150 KP_Left */
155 0x4d, /* 152 KP_Right */
156 0x4f, /* 153 KP_End */
157 0x50, /* 154 KP_Down */
158 0x51, /* 155 KP_PgDn */
159 0x52, /* 156 KP_Ins */
160 0x53, /* 157 KP_Del */
163 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
167 keycode
= ev
->keysym
.scancode
;
171 } else if (keycode
< 97) {
172 keycode
-= 8; /* just an offset */
173 } else if (keycode
< 158) {
174 /* use conversion table */
175 keycode
= x_keycode_to_pc_keycode
[keycode
- 97];
184 static void reset_keys(void)
187 for(i
= 0; i
< 256; i
++) {
188 if (modifiers_state
[i
]) {
190 kbd_put_keycode(0xe0);
191 kbd_put_keycode(i
| 0x80);
192 modifiers_state
[i
] = 0;
197 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
201 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
204 if (ev
->type
== SDL_KEYUP
)
206 kbd_put_keycode(0xe1);
207 kbd_put_keycode(0x1d | v
);
208 kbd_put_keycode(0x45 | v
);
213 keycode
= sdl_keyevent_to_keycode_generic(ev
);
215 keycode
= sdl_keyevent_to_keycode(ev
);
220 /* sent when leaving window: reset the modifiers state */
223 case 0x2a: /* Left Shift */
224 case 0x36: /* Right Shift */
225 case 0x1d: /* Left CTRL */
226 case 0x9d: /* Right CTRL */
227 case 0x38: /* Left ALT */
228 case 0xb8: /* Right ALT */
229 if (ev
->type
== SDL_KEYUP
)
230 modifiers_state
[keycode
] = 0;
232 modifiers_state
[keycode
] = 1;
234 case 0x45: /* num lock */
235 case 0x3a: /* caps lock */
236 /* SDL does not send the key up event, so we generate it */
237 kbd_put_keycode(keycode
);
238 kbd_put_keycode(keycode
| 0x80);
242 /* now send the key code */
244 kbd_put_keycode(0xe0);
245 if (ev
->type
== SDL_KEYUP
)
246 kbd_put_keycode(keycode
| 0x80);
248 kbd_put_keycode(keycode
& 0x7f);
251 static void sdl_update_caption(void)
256 strcat(buf
, " [Stopped]");
259 strcat(buf
, " - Press Ctrl-Alt to exit grab");
261 SDL_WM_SetCaption(buf
, "QEMU");
264 static void sdl_grab_start(void)
267 SDL_WM_GrabInput(SDL_GRAB_ON
);
268 /* dummy read to avoid moving the mouse */
269 SDL_GetRelativeMouseState(NULL
, NULL
);
271 sdl_update_caption();
274 static void sdl_grab_end(void)
276 SDL_WM_GrabInput(SDL_GRAB_OFF
);
279 sdl_update_caption();
282 static void sdl_send_mouse_event(int dz
)
284 int dx
, dy
, state
, buttons
;
285 state
= SDL_GetRelativeMouseState(&dx
, &dy
);
287 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
288 buttons
|= MOUSE_EVENT_LBUTTON
;
289 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
290 buttons
|= MOUSE_EVENT_RBUTTON
;
291 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
292 buttons
|= MOUSE_EVENT_MBUTTON
;
293 kbd_mouse_event(dx
, dy
, dz
, buttons
);
296 static void toggle_full_screen(DisplayState
*ds
)
298 gui_fullscreen
= !gui_fullscreen
;
299 sdl_resize(ds
, screen
->w
, screen
->h
);
300 if (gui_fullscreen
) {
301 gui_saved_grab
= gui_grab
;
307 vga_invalidate_display();
308 vga_update_display();
311 static void sdl_refresh(DisplayState
*ds
)
313 SDL_Event ev1
, *ev
= &ev1
;
316 if (last_vm_running
!= vm_running
) {
317 last_vm_running
= vm_running
;
318 sdl_update_caption();
321 if (is_active_console(vga_console
))
322 vga_update_display();
324 while (SDL_PollEvent(ev
)) {
326 case SDL_VIDEOEXPOSE
:
327 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
331 if (ev
->type
== SDL_KEYDOWN
) {
332 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
334 gui_key_modifier_pressed
= mod_state
;
335 if (gui_key_modifier_pressed
) {
337 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
339 case 0x21: /* 'f' key on US keyboard */
340 toggle_full_screen(ds
);
343 case 0x02 ... 0x0a: /* '1' to '9' keys */
344 console_select(keycode
- 0x02);
345 if (is_active_console(vga_console
)) {
346 /* tell the vga console to redisplay itself */
347 vga_invalidate_display();
349 /* display grab if going to a text console */
358 } else if (!is_active_console(vga_console
)) {
361 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
362 switch(ev
->key
.keysym
.sym
) {
363 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
364 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
365 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
366 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
367 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
368 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
369 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
370 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
374 switch(ev
->key
.keysym
.sym
) {
375 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
376 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
377 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
378 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
379 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
380 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
381 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
382 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
383 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break; case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
388 kbd_put_keysym(keysym
);
389 } else if (ev
->key
.keysym
.unicode
!= 0) {
390 kbd_put_keysym(ev
->key
.keysym
.unicode
);
393 } else if (ev
->type
== SDL_KEYUP
) {
394 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
396 if (gui_key_modifier_pressed
) {
397 if (gui_keysym
== 0) {
398 /* exit/enter grab if pressing Ctrl-Alt */
403 /* SDL does not send back all the
404 modifiers key, so we must correct it */
408 gui_key_modifier_pressed
= 0;
413 if (is_active_console(vga_console
))
414 sdl_process_key(&ev
->key
);
417 qemu_system_shutdown_request();
419 case SDL_MOUSEMOTION
:
421 sdl_send_mouse_event(0);
424 case SDL_MOUSEBUTTONDOWN
:
425 case SDL_MOUSEBUTTONUP
:
427 SDL_MouseButtonEvent
*bev
= &ev
->button
;
429 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
430 (bev
->state
& SDL_BUTTON_LMASK
)) {
431 /* start grabbing all events */
437 #ifdef SDL_BUTTON_WHEELUP
438 if (bev
->button
== SDL_BUTTON_WHEELUP
) {
440 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
) {
444 sdl_send_mouse_event(dz
);
448 case SDL_ACTIVEEVENT
:
449 if (gui_grab
&& (ev
->active
.gain
& SDL_ACTIVEEVENTMASK
) == 0 &&
450 !gui_fullscreen_initial_grab
) {
460 static void sdl_cleanup(void)
465 void sdl_display_init(DisplayState
*ds
, int full_screen
)
469 #if defined(__APPLE__)
470 /* always use generic keymaps */
471 if (!keyboard_layout
)
472 keyboard_layout
= "en-us";
474 if(keyboard_layout
) {
475 kbd_layout
= init_keyboard_layout(keyboard_layout
);
480 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
481 if (SDL_Init (flags
)) {
482 fprintf(stderr
, "Could not initialize SDL - exiting\n");
486 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
487 signal(SIGINT
, SIG_DFL
);
488 signal(SIGQUIT
, SIG_DFL
);
491 ds
->dpy_update
= sdl_update
;
492 ds
->dpy_resize
= sdl_resize
;
493 ds
->dpy_refresh
= sdl_refresh
;
495 sdl_resize(ds
, 640, 400);
496 sdl_update_caption();
497 SDL_EnableKeyRepeat(250, 50);
498 SDL_EnableUNICODE(1);
504 gui_fullscreen_initial_grab
= 1;