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
24 #include "qemu-common.h"
34 static SDL_Surface
*screen
;
35 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
36 static int last_vm_running
;
37 static int gui_saved_grab
;
38 static int gui_fullscreen
;
39 static int gui_noframe
;
40 static int gui_key_modifier_pressed
;
41 static int gui_keysym
;
42 static int gui_fullscreen_initial_grab
;
43 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
44 static uint8_t modifiers_state
[256];
45 static int width
, height
;
46 static SDL_Cursor
*sdl_cursor_normal
;
47 static SDL_Cursor
*sdl_cursor_hidden
;
48 static int absolute_enabled
= 0;
49 static int guest_cursor
= 0;
50 static int guest_x
, guest_y
;
51 static SDL_Cursor
*guest_sprite
= 0;
53 static void sdl_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
55 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
56 SDL_UpdateRect(screen
, x
, y
, w
, h
);
59 static void sdl_resize(DisplayState
*ds
, int w
, int h
)
63 // printf("resizing to %d %d\n", w, h);
65 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
67 flags
|= SDL_FULLSCREEN
;
75 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
77 fprintf(stderr
, "Could not open SDL display\n");
80 if (!screen
->pixels
&& (flags
& SDL_HWSURFACE
) && (flags
& SDL_FULLSCREEN
)) {
81 flags
&= ~SDL_HWSURFACE
;
85 if (!screen
->pixels
) {
86 fprintf(stderr
, "Could not open SDL display\n");
89 ds
->data
= screen
->pixels
;
90 ds
->linesize
= screen
->pitch
;
91 ds
->depth
= screen
->format
->BitsPerPixel
;
92 if (ds
->depth
== 32 && screen
->format
->Rshift
== 0) {
101 /* generic keyboard conversion */
103 #include "sdl_keysym.h"
106 static kbd_layout_t
*kbd_layout
= NULL
;
108 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
111 /* workaround for X11+SDL bug with AltGR */
112 keysym
= ev
->keysym
.sym
;
113 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
115 /* For Japanese key '\' and '|' */
116 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
119 return keysym2scancode(kbd_layout
, keysym
);
122 /* specific keyboard conversions from scan codes */
126 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
128 return ev
->keysym
.scancode
;
133 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
137 keycode
= ev
->keysym
.scancode
;
141 } else if (keycode
< 97) {
142 keycode
-= 8; /* just an offset */
143 } else if (keycode
< 212) {
144 /* use conversion table */
145 keycode
= _translate_keycode(keycode
- 97);
154 static void reset_keys(void)
157 for(i
= 0; i
< 256; i
++) {
158 if (modifiers_state
[i
]) {
160 kbd_put_keycode(0xe0);
161 kbd_put_keycode(i
| 0x80);
162 modifiers_state
[i
] = 0;
167 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
171 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
174 if (ev
->type
== SDL_KEYUP
)
176 kbd_put_keycode(0xe1);
177 kbd_put_keycode(0x1d | v
);
178 kbd_put_keycode(0x45 | v
);
183 keycode
= sdl_keyevent_to_keycode_generic(ev
);
185 keycode
= sdl_keyevent_to_keycode(ev
);
190 /* sent when leaving window: reset the modifiers state */
193 case 0x2a: /* Left Shift */
194 case 0x36: /* Right Shift */
195 case 0x1d: /* Left CTRL */
196 case 0x9d: /* Right CTRL */
197 case 0x38: /* Left ALT */
198 case 0xb8: /* Right ALT */
199 if (ev
->type
== SDL_KEYUP
)
200 modifiers_state
[keycode
] = 0;
202 modifiers_state
[keycode
] = 1;
204 case 0x45: /* num lock */
205 case 0x3a: /* caps lock */
206 /* SDL does not send the key up event, so we generate it */
207 kbd_put_keycode(keycode
);
208 kbd_put_keycode(keycode
| 0x80);
212 /* now send the key code */
214 kbd_put_keycode(0xe0);
215 if (ev
->type
== SDL_KEYUP
)
216 kbd_put_keycode(keycode
| 0x80);
218 kbd_put_keycode(keycode
& 0x7f);
221 static void sdl_update_caption(void)
223 char buf
[1024], appname
[20] = "QEMU";
224 const char *status
= "";
227 status
= " [Stopped]";
230 status
= " - Press Ctrl-Alt to exit grab";
232 status
= " - Press Ctrl-Alt-Shift to exit grab";
235 decorate_application_name(appname
, sizeof appname
);
238 snprintf(buf
, sizeof(buf
), "%s (%s)%s", appname
, qemu_name
, status
);
240 snprintf(buf
, sizeof(buf
), "%s%s", appname
, status
);
242 SDL_WM_SetCaption(buf
, appname
);
245 static void sdl_hide_cursor(void)
250 if (kbd_mouse_is_absolute()) {
252 SDL_SetCursor(sdl_cursor_hidden
);
258 static void sdl_show_cursor(void)
263 if (!kbd_mouse_is_absolute()) {
266 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
267 SDL_SetCursor(guest_sprite
);
269 SDL_SetCursor(sdl_cursor_normal
);
273 static void sdl_grab_start(void)
276 SDL_SetCursor(guest_sprite
);
277 SDL_WarpMouse(guest_x
, guest_y
);
280 SDL_WM_GrabInput(SDL_GRAB_ON
);
281 /* dummy read to avoid moving the mouse */
282 SDL_GetRelativeMouseState(NULL
, NULL
);
284 sdl_update_caption();
287 static void sdl_grab_end(void)
289 SDL_WM_GrabInput(SDL_GRAB_OFF
);
292 sdl_update_caption();
295 static void sdl_send_mouse_event(int dz
)
297 int dx
, dy
, state
, buttons
;
298 state
= SDL_GetRelativeMouseState(&dx
, &dy
);
300 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
301 buttons
|= MOUSE_EVENT_LBUTTON
;
302 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
303 buttons
|= MOUSE_EVENT_RBUTTON
;
304 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
305 buttons
|= MOUSE_EVENT_MBUTTON
;
307 if (kbd_mouse_is_absolute()) {
308 if (!absolute_enabled
) {
313 absolute_enabled
= 1;
316 SDL_GetMouseState(&dx
, &dy
);
317 dx
= dx
* 0x7FFF / width
;
318 dy
= dy
* 0x7FFF / height
;
319 } else if (absolute_enabled
) {
321 absolute_enabled
= 0;
322 } else if (guest_cursor
) {
323 SDL_GetMouseState(&dx
, &dy
);
330 kbd_mouse_event(dx
, dy
, dz
, buttons
);
333 static void toggle_full_screen(DisplayState
*ds
)
335 gui_fullscreen
= !gui_fullscreen
;
336 sdl_resize(ds
, screen
->w
, screen
->h
);
337 if (gui_fullscreen
) {
338 gui_saved_grab
= gui_grab
;
348 static void sdl_refresh(DisplayState
*ds
)
350 SDL_Event ev1
, *ev
= &ev1
;
353 if (last_vm_running
!= vm_running
) {
354 last_vm_running
= vm_running
;
355 sdl_update_caption();
360 while (SDL_PollEvent(ev
)) {
362 case SDL_VIDEOEXPOSE
:
363 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
367 if (ev
->type
== SDL_KEYDOWN
) {
369 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
372 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
373 (gui_grab_code
| KMOD_LSHIFT
);
375 gui_key_modifier_pressed
= mod_state
;
376 if (gui_key_modifier_pressed
) {
378 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
380 case 0x21: /* 'f' key on US keyboard */
381 toggle_full_screen(ds
);
384 case 0x02 ... 0x0a: /* '1' to '9' keys */
385 /* Reset the modifiers sent to the current console */
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;
424 case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
429 kbd_put_keysym(keysym
);
430 } else if (ev
->key
.keysym
.unicode
!= 0) {
431 kbd_put_keysym(ev
->key
.keysym
.unicode
);
434 } else if (ev
->type
== SDL_KEYUP
) {
436 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
438 mod_state
= (ev
->key
.keysym
.mod
&
439 (gui_grab_code
| KMOD_LSHIFT
));
442 if (gui_key_modifier_pressed
) {
443 gui_key_modifier_pressed
= 0;
444 if (gui_keysym
== 0) {
445 /* exit/enter grab if pressing Ctrl-Alt */
447 /* if the application is not active,
448 do not try to enter grab state. It
450 'SDL_WM_GrabInput(SDL_GRAB_ON)'
451 from blocking all the application
453 if (SDL_GetAppState() & SDL_APPACTIVE
)
458 /* SDL does not send back all the
459 modifiers key, so we must correct it */
467 if (is_graphic_console() && !gui_keysym
)
468 sdl_process_key(&ev
->key
);
472 qemu_system_shutdown_request();
473 vm_start(); /* In case we're paused */
476 case SDL_MOUSEMOTION
:
477 if (gui_grab
|| kbd_mouse_is_absolute() ||
479 sdl_send_mouse_event(0);
482 case SDL_MOUSEBUTTONDOWN
:
483 case SDL_MOUSEBUTTONUP
:
485 SDL_MouseButtonEvent
*bev
= &ev
->button
;
486 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
487 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
488 (bev
->state
& SDL_BUTTON_LMASK
)) {
489 /* start grabbing all events */
495 #ifdef SDL_BUTTON_WHEELUP
496 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
498 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
502 sdl_send_mouse_event(dz
);
506 case SDL_ACTIVEEVENT
:
507 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
508 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
518 static void sdl_fill(DisplayState
*ds
, int x
, int y
, int w
, int h
, uint32_t c
)
520 SDL_Rect dst
= { x
, y
, w
, h
};
521 SDL_FillRect(screen
, &dst
, c
);
524 static void sdl_mouse_warp(int x
, int y
, int on
)
529 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
530 SDL_SetCursor(guest_sprite
);
536 guest_x
= x
, guest_y
= y
;
539 static void sdl_mouse_define(int width
, int height
, int bpp
,
540 int hot_x
, int hot_y
,
541 uint8_t *image
, uint8_t *mask
)
543 uint8_t sprite
[256], *line
;
544 int x
, y
, dst
, bypl
, src
= 0;
546 SDL_FreeCursor(guest_sprite
);
548 memset(sprite
, 0, 256);
549 bypl
= ((width
* bpp
+ 31) >> 5) << 2;
550 for (y
= 0, dst
= 0; y
< height
; y
++, image
+= bypl
) {
552 for (x
= 0; x
< width
; x
++, dst
++) {
555 src
= *(line
++); src
|= *(line
++); src
|= *(line
++);
559 src
= *(line
++); src
|= *(line
++);
565 src
= 0xf & (line
[x
>> 1] >> ((x
& 1)) << 2);
568 src
= 3 & (line
[x
>> 2] >> ((x
& 3)) << 1);
571 src
= 1 & (line
[x
>> 3] >> (x
& 7));
575 sprite
[dst
>> 3] |= (1 << (~dst
& 7)) & mask
[dst
>> 3];
578 guest_sprite
= SDL_CreateCursor(sprite
, mask
, width
, height
, hot_x
, hot_y
);
581 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
582 SDL_SetCursor(guest_sprite
);
585 static void sdl_cleanup(void)
588 SDL_FreeCursor(guest_sprite
);
592 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
597 #if defined(__APPLE__)
598 /* always use generic keymaps */
599 if (!keyboard_layout
)
600 keyboard_layout
= "en-us";
602 if(keyboard_layout
) {
603 kbd_layout
= init_keyboard_layout(keyboard_layout
);
611 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
612 if (SDL_Init (flags
)) {
613 fprintf(stderr
, "Could not initialize SDL - exiting\n");
617 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
618 signal(SIGINT
, SIG_DFL
);
619 signal(SIGQUIT
, SIG_DFL
);
622 ds
->dpy_update
= sdl_update
;
623 ds
->dpy_resize
= sdl_resize
;
624 ds
->dpy_refresh
= sdl_refresh
;
625 ds
->dpy_fill
= sdl_fill
;
626 ds
->mouse_set
= sdl_mouse_warp
;
627 ds
->cursor_define
= sdl_mouse_define
;
629 sdl_resize(ds
, 640, 400);
630 sdl_update_caption();
631 SDL_EnableKeyRepeat(250, 50);
632 SDL_EnableUNICODE(1);
635 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
636 sdl_cursor_normal
= SDL_GetCursor();
641 gui_fullscreen_initial_grab
= 1;