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
;
57 flags
|= SDL_FULLSCREEN
;
58 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
60 fprintf(stderr
, "Could not open SDL display\n");
63 ds
->data
= screen
->pixels
;
64 ds
->linesize
= screen
->pitch
;
65 ds
->depth
= screen
->format
->BitsPerPixel
;
70 /* generic keyboard conversion */
72 #include "sdl_keysym.h"
75 static kbd_layout_t
*kbd_layout
= NULL
;
77 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
80 /* workaround for X11+SDL bug with AltGR */
81 keysym
= ev
->keysym
.sym
;
82 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
84 return keysym2scancode(kbd_layout
, keysym
);
87 /* specific keyboard conversions from scan codes */
91 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
93 return ev
->keysym
.scancode
;
98 static const uint8_t x_keycode_to_pc_keycode
[61] = {
104 0xcd, /* 102 Right */
110 0x9c, /* 108 Enter */
111 0x9d, /* 109 Ctrl-R */
113 0xb7, /* 111 Print */
114 0xb5, /* 112 Divide */
115 0xb8, /* 113 Alt-R */
116 0xc6, /* 114 Break */
122 0x70, /* 120 Hiragana_Katakana */
125 0x73, /* 123 backslash */
131 0x79, /* 129 Henkan */
133 0x7b, /* 131 Muhenkan */
149 0x47, /* 147 KP_HOME */
150 0x48, /* 148 KP_UP */
151 0x49, /* 149 KP_PgUp */
152 0x4b, /* 150 KP_Left */
154 0x4d, /* 152 KP_Right */
155 0x4f, /* 153 KP_End */
156 0x50, /* 154 KP_Down */
157 0x51, /* 155 KP_PgDn */
158 0x52, /* 156 KP_Ins */
159 0x53, /* 157 KP_Del */
162 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
166 keycode
= ev
->keysym
.scancode
;
170 } else if (keycode
< 97) {
171 keycode
-= 8; /* just an offset */
172 } else if (keycode
< 158) {
173 /* use conversion table */
174 keycode
= x_keycode_to_pc_keycode
[keycode
- 97];
183 static void reset_keys(void)
186 for(i
= 0; i
< 256; i
++) {
187 if (modifiers_state
[i
]) {
189 kbd_put_keycode(0xe0);
190 kbd_put_keycode(i
| 0x80);
191 modifiers_state
[i
] = 0;
196 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
200 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
203 if (ev
->type
== SDL_KEYUP
)
205 kbd_put_keycode(0xe1);
206 kbd_put_keycode(0x1d | v
);
207 kbd_put_keycode(0x45 | v
);
212 keycode
= sdl_keyevent_to_keycode_generic(ev
);
214 keycode
= sdl_keyevent_to_keycode(ev
);
219 /* sent when leaving window: reset the modifiers state */
222 case 0x2a: /* Left Shift */
223 case 0x36: /* Right Shift */
224 case 0x1d: /* Left CTRL */
225 case 0x9d: /* Right CTRL */
226 case 0x38: /* Left ALT */
227 case 0xb8: /* Right ALT */
228 if (ev
->type
== SDL_KEYUP
)
229 modifiers_state
[keycode
] = 0;
231 modifiers_state
[keycode
] = 1;
233 case 0x45: /* num lock */
234 case 0x3a: /* caps lock */
235 /* SDL does not send the key up event, so we generate it */
236 kbd_put_keycode(keycode
);
237 kbd_put_keycode(keycode
| 0x80);
241 /* now send the key code */
243 kbd_put_keycode(0xe0);
244 if (ev
->type
== SDL_KEYUP
)
245 kbd_put_keycode(keycode
| 0x80);
247 kbd_put_keycode(keycode
& 0x7f);
250 static void sdl_update_caption(void)
255 strcat(buf
, " [Stopped]");
258 strcat(buf
, " - Press Ctrl-Alt to exit grab");
260 SDL_WM_SetCaption(buf
, "QEMU");
263 static void sdl_grab_start(void)
266 SDL_WM_GrabInput(SDL_GRAB_ON
);
267 /* dummy read to avoid moving the mouse */
268 SDL_GetRelativeMouseState(NULL
, NULL
);
270 sdl_update_caption();
273 static void sdl_grab_end(void)
275 SDL_WM_GrabInput(SDL_GRAB_OFF
);
278 sdl_update_caption();
281 static void sdl_send_mouse_event(int dz
)
283 int dx
, dy
, state
, buttons
;
284 state
= SDL_GetRelativeMouseState(&dx
, &dy
);
286 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
287 buttons
|= MOUSE_EVENT_LBUTTON
;
288 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
289 buttons
|= MOUSE_EVENT_RBUTTON
;
290 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
291 buttons
|= MOUSE_EVENT_MBUTTON
;
292 kbd_mouse_event(dx
, dy
, dz
, buttons
);
295 static void toggle_full_screen(DisplayState
*ds
)
297 gui_fullscreen
= !gui_fullscreen
;
298 sdl_resize(ds
, screen
->w
, screen
->h
);
299 if (gui_fullscreen
) {
300 gui_saved_grab
= gui_grab
;
306 vga_invalidate_display();
307 vga_update_display();
310 static void sdl_refresh(DisplayState
*ds
)
312 SDL_Event ev1
, *ev
= &ev1
;
315 if (last_vm_running
!= vm_running
) {
316 last_vm_running
= vm_running
;
317 sdl_update_caption();
320 if (is_active_console(vga_console
))
321 vga_update_display();
323 while (SDL_PollEvent(ev
)) {
325 case SDL_VIDEOEXPOSE
:
326 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
330 if (ev
->type
== SDL_KEYDOWN
) {
331 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
333 gui_key_modifier_pressed
= mod_state
;
334 if (gui_key_modifier_pressed
) {
336 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
338 case 0x21: /* 'f' key on US keyboard */
339 toggle_full_screen(ds
);
342 case 0x02 ... 0x0a: /* '1' to '9' keys */
343 console_select(keycode
- 0x02);
344 if (is_active_console(vga_console
)) {
345 /* tell the vga console to redisplay itself */
346 vga_invalidate_display();
348 /* display grab if going to a text console */
357 } else if (!is_active_console(vga_console
)) {
360 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
361 switch(ev
->key
.keysym
.sym
) {
362 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
363 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
364 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
365 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
366 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
367 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
368 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
369 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
373 switch(ev
->key
.keysym
.sym
) {
374 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
375 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
376 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
377 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
378 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
379 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
380 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
381 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
382 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break; case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
387 kbd_put_keysym(keysym
);
388 } else if (ev
->key
.keysym
.unicode
!= 0) {
389 kbd_put_keysym(ev
->key
.keysym
.unicode
);
392 } else if (ev
->type
== SDL_KEYUP
) {
393 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
395 if (gui_key_modifier_pressed
) {
396 if (gui_keysym
== 0) {
397 /* exit/enter grab if pressing Ctrl-Alt */
402 /* SDL does not send back all the
403 modifiers key, so we must correct it */
407 gui_key_modifier_pressed
= 0;
412 if (is_active_console(vga_console
))
413 sdl_process_key(&ev
->key
);
416 qemu_system_shutdown_request();
418 case SDL_MOUSEMOTION
:
420 sdl_send_mouse_event(0);
423 case SDL_MOUSEBUTTONDOWN
:
424 case SDL_MOUSEBUTTONUP
:
426 SDL_MouseButtonEvent
*bev
= &ev
->button
;
428 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
429 (bev
->state
& SDL_BUTTON_LMASK
)) {
430 /* start grabbing all events */
436 #ifdef SDL_BUTTON_WHEELUP
437 if (bev
->button
== SDL_BUTTON_WHEELUP
) {
439 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
) {
443 sdl_send_mouse_event(dz
);
447 case SDL_ACTIVEEVENT
:
448 if (gui_grab
&& (ev
->active
.gain
& SDL_ACTIVEEVENTMASK
) == 0 &&
449 !gui_fullscreen_initial_grab
) {
459 static void sdl_cleanup(void)
464 void sdl_display_init(DisplayState
*ds
, int full_screen
)
468 #if defined(__APPLE__)
469 /* always use generic keymaps */
470 if (!keyboard_layout
)
471 keyboard_layout
= "en-us";
473 if(keyboard_layout
) {
474 kbd_layout
= init_keyboard_layout(keyboard_layout
);
479 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
480 if (SDL_Init (flags
)) {
481 fprintf(stderr
, "Could not initialize SDL - exiting\n");
485 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
486 signal(SIGINT
, SIG_DFL
);
487 signal(SIGQUIT
, SIG_DFL
);
490 ds
->dpy_update
= sdl_update
;
491 ds
->dpy_resize
= sdl_resize
;
492 ds
->dpy_refresh
= sdl_refresh
;
494 sdl_resize(ds
, 640, 400);
495 sdl_update_caption();
496 SDL_EnableKeyRepeat(250, 50);
497 SDL_EnableUNICODE(1);
503 gui_fullscreen_initial_grab
= 1;