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
;
60 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
62 fprintf(stderr
, "Could not open SDL display\n");
65 if (!screen
->pixels
&& (flags
& SDL_HWSURFACE
) && (flags
& SDL_FULLSCREEN
)) {
66 flags
&= ~SDL_HWSURFACE
;
70 if (!screen
->pixels
) {
71 fprintf(stderr
, "Could not open SDL display\n");
74 ds
->data
= screen
->pixels
;
75 ds
->linesize
= screen
->pitch
;
76 ds
->depth
= screen
->format
->BitsPerPixel
;
81 /* generic keyboard conversion */
83 #include "sdl_keysym.h"
86 static kbd_layout_t
*kbd_layout
= NULL
;
88 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
91 /* workaround for X11+SDL bug with AltGR */
92 keysym
= ev
->keysym
.sym
;
93 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
95 return keysym2scancode(kbd_layout
, keysym
);
98 /* specific keyboard conversions from scan codes */
102 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
104 return ev
->keysym
.scancode
;
109 static const uint8_t x_keycode_to_pc_keycode
[61] = {
115 0xcd, /* 102 Right */
121 0x9c, /* 108 Enter */
122 0x9d, /* 109 Ctrl-R */
124 0xb7, /* 111 Print */
125 0xb5, /* 112 Divide */
126 0xb8, /* 113 Alt-R */
127 0xc6, /* 114 Break */
133 0x70, /* 120 Hiragana_Katakana */
136 0x73, /* 123 backslash */
142 0x79, /* 129 Henkan */
144 0x7b, /* 131 Muhenkan */
160 0x47, /* 147 KP_HOME */
161 0x48, /* 148 KP_UP */
162 0x49, /* 149 KP_PgUp */
163 0x4b, /* 150 KP_Left */
165 0x4d, /* 152 KP_Right */
166 0x4f, /* 153 KP_End */
167 0x50, /* 154 KP_Down */
168 0x51, /* 155 KP_PgDn */
169 0x52, /* 156 KP_Ins */
170 0x53, /* 157 KP_Del */
173 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
177 keycode
= ev
->keysym
.scancode
;
181 } else if (keycode
< 97) {
182 keycode
-= 8; /* just an offset */
183 } else if (keycode
< 158) {
184 /* use conversion table */
185 keycode
= x_keycode_to_pc_keycode
[keycode
- 97];
194 static void reset_keys(void)
197 for(i
= 0; i
< 256; i
++) {
198 if (modifiers_state
[i
]) {
200 kbd_put_keycode(0xe0);
201 kbd_put_keycode(i
| 0x80);
202 modifiers_state
[i
] = 0;
207 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
211 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
214 if (ev
->type
== SDL_KEYUP
)
216 kbd_put_keycode(0xe1);
217 kbd_put_keycode(0x1d | v
);
218 kbd_put_keycode(0x45 | v
);
223 keycode
= sdl_keyevent_to_keycode_generic(ev
);
225 keycode
= sdl_keyevent_to_keycode(ev
);
230 /* sent when leaving window: reset the modifiers state */
233 case 0x2a: /* Left Shift */
234 case 0x36: /* Right Shift */
235 case 0x1d: /* Left CTRL */
236 case 0x9d: /* Right CTRL */
237 case 0x38: /* Left ALT */
238 case 0xb8: /* Right ALT */
239 if (ev
->type
== SDL_KEYUP
)
240 modifiers_state
[keycode
] = 0;
242 modifiers_state
[keycode
] = 1;
244 case 0x45: /* num lock */
245 case 0x3a: /* caps lock */
246 /* SDL does not send the key up event, so we generate it */
247 kbd_put_keycode(keycode
);
248 kbd_put_keycode(keycode
| 0x80);
252 /* now send the key code */
254 kbd_put_keycode(0xe0);
255 if (ev
->type
== SDL_KEYUP
)
256 kbd_put_keycode(keycode
| 0x80);
258 kbd_put_keycode(keycode
& 0x7f);
261 static void sdl_update_caption(void)
266 strcat(buf
, " [Stopped]");
269 strcat(buf
, " - Press Ctrl-Alt to exit grab");
271 SDL_WM_SetCaption(buf
, "QEMU");
274 static void sdl_grab_start(void)
277 SDL_WM_GrabInput(SDL_GRAB_ON
);
278 /* dummy read to avoid moving the mouse */
279 SDL_GetRelativeMouseState(NULL
, NULL
);
281 sdl_update_caption();
284 static void sdl_grab_end(void)
286 SDL_WM_GrabInput(SDL_GRAB_OFF
);
289 sdl_update_caption();
292 static void sdl_send_mouse_event(int dz
)
294 int dx
, dy
, state
, buttons
;
295 state
= SDL_GetRelativeMouseState(&dx
, &dy
);
297 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
298 buttons
|= MOUSE_EVENT_LBUTTON
;
299 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
300 buttons
|= MOUSE_EVENT_RBUTTON
;
301 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
302 buttons
|= MOUSE_EVENT_MBUTTON
;
303 kbd_mouse_event(dx
, dy
, dz
, buttons
);
306 static void toggle_full_screen(DisplayState
*ds
)
308 gui_fullscreen
= !gui_fullscreen
;
309 sdl_resize(ds
, screen
->w
, screen
->h
);
310 if (gui_fullscreen
) {
311 gui_saved_grab
= gui_grab
;
321 static void sdl_refresh(DisplayState
*ds
)
323 SDL_Event ev1
, *ev
= &ev1
;
326 if (last_vm_running
!= vm_running
) {
327 last_vm_running
= vm_running
;
328 sdl_update_caption();
333 while (SDL_PollEvent(ev
)) {
335 case SDL_VIDEOEXPOSE
:
336 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
340 if (ev
->type
== SDL_KEYDOWN
) {
341 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
343 gui_key_modifier_pressed
= mod_state
;
344 if (gui_key_modifier_pressed
) {
346 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
348 case 0x21: /* 'f' key on US keyboard */
349 toggle_full_screen(ds
);
352 case 0x02 ... 0x0a: /* '1' to '9' keys */
353 console_select(keycode
- 0x02);
354 if (!is_graphic_console()) {
355 /* display grab if going to a text console */
364 } else if (!is_graphic_console()) {
367 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
368 switch(ev
->key
.keysym
.sym
) {
369 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
370 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
371 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
372 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
373 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
374 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
375 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
376 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
380 switch(ev
->key
.keysym
.sym
) {
381 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
382 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
383 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
384 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
385 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
386 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
387 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
388 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
389 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break; case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
394 kbd_put_keysym(keysym
);
395 } else if (ev
->key
.keysym
.unicode
!= 0) {
396 kbd_put_keysym(ev
->key
.keysym
.unicode
);
399 } else if (ev
->type
== SDL_KEYUP
) {
400 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
402 if (gui_key_modifier_pressed
) {
403 gui_key_modifier_pressed
= 0;
404 if (gui_keysym
== 0) {
405 /* exit/enter grab if pressing Ctrl-Alt */
410 /* SDL does not send back all the
411 modifiers key, so we must correct it */
419 if (is_graphic_console())
420 sdl_process_key(&ev
->key
);
423 qemu_system_shutdown_request();
425 case SDL_MOUSEMOTION
:
427 sdl_send_mouse_event(0);
430 case SDL_MOUSEBUTTONDOWN
:
431 case SDL_MOUSEBUTTONUP
:
433 SDL_MouseButtonEvent
*bev
= &ev
->button
;
435 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
436 (bev
->state
& SDL_BUTTON_LMASK
)) {
437 /* start grabbing all events */
443 #ifdef SDL_BUTTON_WHEELUP
444 if (bev
->button
== SDL_BUTTON_WHEELUP
) {
446 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
) {
450 sdl_send_mouse_event(dz
);
454 case SDL_ACTIVEEVENT
:
455 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
456 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
466 static void sdl_cleanup(void)
471 void sdl_display_init(DisplayState
*ds
, int full_screen
)
475 #if defined(__APPLE__)
476 /* always use generic keymaps */
477 if (!keyboard_layout
)
478 keyboard_layout
= "en-us";
480 if(keyboard_layout
) {
481 kbd_layout
= init_keyboard_layout(keyboard_layout
);
486 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
487 if (SDL_Init (flags
)) {
488 fprintf(stderr
, "Could not initialize SDL - exiting\n");
492 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
493 signal(SIGINT
, SIG_DFL
);
494 signal(SIGQUIT
, SIG_DFL
);
497 ds
->dpy_update
= sdl_update
;
498 ds
->dpy_resize
= sdl_resize
;
499 ds
->dpy_refresh
= sdl_refresh
;
501 sdl_resize(ds
, 640, 400);
502 sdl_update_caption();
503 SDL_EnableKeyRepeat(250, 50);
504 SDL_EnableUNICODE(1);
510 gui_fullscreen_initial_grab
= 1;