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"
30 #include <SDL_syswm.h>
36 static DisplayChangeListener
*dcl
;
37 static SDL_Surface
*real_screen
;
38 static SDL_Surface
*guest_screen
= NULL
;
39 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
40 static int last_vm_running
;
41 static int gui_saved_grab
;
42 static int gui_fullscreen
;
43 static int gui_noframe
;
44 static int gui_key_modifier_pressed
;
45 static int gui_keysym
;
46 static int gui_fullscreen_initial_grab
;
47 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
48 static uint8_t modifiers_state
[256];
49 static int width
, height
;
50 static SDL_Cursor
*sdl_cursor_normal
;
51 static SDL_Cursor
*sdl_cursor_hidden
;
52 static int absolute_enabled
= 0;
53 static int guest_cursor
= 0;
54 static int guest_x
, guest_y
;
55 static SDL_Cursor
*guest_sprite
= 0;
57 static void sdl_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
64 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
66 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
67 SDL_UpdateRect(real_screen
, x
, y
, w
, h
);
70 static void sdl_setdata(DisplayState
*ds
)
75 rec
.w
= real_screen
->w
;
76 rec
.h
= real_screen
->h
;
78 if (guest_screen
!= NULL
) SDL_FreeSurface(guest_screen
);
80 guest_screen
= SDL_CreateRGBSurfaceFrom(ds_get_data(ds
), ds_get_width(ds
), ds_get_height(ds
),
81 ds_get_bits_per_pixel(ds
), ds_get_linesize(ds
),
82 ds
->surface
->pf
.rmask
, ds
->surface
->pf
.gmask
,
83 ds
->surface
->pf
.bmask
, ds
->surface
->pf
.amask
);
86 static void sdl_resize(DisplayState
*ds
)
90 // printf("resizing to %d %d\n", w, h);
92 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
94 flags
|= SDL_FULLSCREEN
;
98 width
= ds_get_width(ds
);
99 height
= ds_get_height(ds
);
100 real_screen
= SDL_SetVideoMode(width
, height
, 0, flags
);
102 fprintf(stderr
, "Could not open SDL display\n");
109 /* generic keyboard conversion */
111 #include "sdl_keysym.h"
114 static kbd_layout_t
*kbd_layout
= NULL
;
116 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
119 /* workaround for X11+SDL bug with AltGR */
120 keysym
= ev
->keysym
.sym
;
121 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
123 /* For Japanese key '\' and '|' */
124 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
127 return keysym2scancode(kbd_layout
, keysym
);
130 /* specific keyboard conversions from scan codes */
134 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
136 return ev
->keysym
.scancode
;
141 #if defined(SDL_VIDEO_DRIVER_X11)
142 #include <X11/XKBlib.h>
144 static int check_for_evdev(void)
147 XkbDescPtr desc
= NULL
;
149 char *keycodes
= NULL
;
151 SDL_VERSION(&info
.version
);
152 if (!SDL_GetWMInfo(&info
)) {
155 desc
= XkbGetKeyboard(info
.info
.x11
.display
,
156 XkbGBN_AllComponentsMask
,
158 if (desc
&& desc
->names
) {
159 keycodes
= XGetAtomName(info
.info
.x11
.display
, desc
->names
->keycodes
);
160 if (keycodes
== NULL
) {
161 fprintf(stderr
, "could not lookup keycode name\n");
162 } else if (strstart(keycodes
, "evdev", NULL
)) {
164 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
165 fprintf(stderr
, "unknown keycodes `%s', please report to "
166 "qemu-devel@nongnu.org\n", keycodes
);
171 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
179 static int check_for_evdev(void)
185 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
188 static int has_evdev
= -1;
191 has_evdev
= check_for_evdev();
193 keycode
= ev
->keysym
.scancode
;
197 } else if (keycode
< 97) {
198 keycode
-= 8; /* just an offset */
199 } else if (keycode
< 158) {
200 /* use conversion table */
202 keycode
= translate_evdev_keycode(keycode
- 97);
204 keycode
= translate_xfree86_keycode(keycode
- 97);
205 } else if (keycode
== 208) { /* Hiragana_Katakana */
207 } else if (keycode
== 211) { /* backslash */
217 static void reset_keys(void)
220 for(i
= 0; i
< 256; i
++) {
221 if (modifiers_state
[i
]) {
223 kbd_put_keycode(0xe0);
224 kbd_put_keycode(i
| 0x80);
225 modifiers_state
[i
] = 0;
230 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
234 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
237 if (ev
->type
== SDL_KEYUP
)
239 kbd_put_keycode(0xe1);
240 kbd_put_keycode(0x1d | v
);
241 kbd_put_keycode(0x45 | v
);
246 keycode
= sdl_keyevent_to_keycode_generic(ev
);
248 keycode
= sdl_keyevent_to_keycode(ev
);
253 /* sent when leaving window: reset the modifiers state */
256 case 0x2a: /* Left Shift */
257 case 0x36: /* Right Shift */
258 case 0x1d: /* Left CTRL */
259 case 0x9d: /* Right CTRL */
260 case 0x38: /* Left ALT */
261 case 0xb8: /* Right ALT */
262 if (ev
->type
== SDL_KEYUP
)
263 modifiers_state
[keycode
] = 0;
265 modifiers_state
[keycode
] = 1;
267 case 0x45: /* num lock */
268 case 0x3a: /* caps lock */
269 /* SDL does not send the key up event, so we generate it */
270 kbd_put_keycode(keycode
);
271 kbd_put_keycode(keycode
| 0x80);
275 /* now send the key code */
277 kbd_put_keycode(0xe0);
278 if (ev
->type
== SDL_KEYUP
)
279 kbd_put_keycode(keycode
| 0x80);
281 kbd_put_keycode(keycode
& 0x7f);
284 static void sdl_update_caption(void)
287 const char *status
= "";
290 status
= " [Stopped]";
293 status
= " - Press Ctrl-Alt to exit grab";
295 status
= " - Press Ctrl-Alt-Shift to exit grab";
299 snprintf(buf
, sizeof(buf
), "QEMU (%s)%s", qemu_name
, status
);
301 snprintf(buf
, sizeof(buf
), "QEMU%s", status
);
303 SDL_WM_SetCaption(buf
, "QEMU");
306 static void sdl_hide_cursor(void)
311 if (kbd_mouse_is_absolute()) {
313 SDL_SetCursor(sdl_cursor_hidden
);
319 static void sdl_show_cursor(void)
324 if (!kbd_mouse_is_absolute()) {
327 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
328 SDL_SetCursor(guest_sprite
);
330 SDL_SetCursor(sdl_cursor_normal
);
334 static void sdl_grab_start(void)
337 SDL_SetCursor(guest_sprite
);
338 if (!kbd_mouse_is_absolute() && !absolute_enabled
)
339 SDL_WarpMouse(guest_x
, guest_y
);
343 if (SDL_WM_GrabInput(SDL_GRAB_ON
) == SDL_GRAB_ON
) {
345 sdl_update_caption();
350 static void sdl_grab_end(void)
352 SDL_WM_GrabInput(SDL_GRAB_OFF
);
355 sdl_update_caption();
358 static void sdl_send_mouse_event(int dx
, int dy
, int dz
, int x
, int y
, int state
)
362 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
363 buttons
|= MOUSE_EVENT_LBUTTON
;
364 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
365 buttons
|= MOUSE_EVENT_RBUTTON
;
366 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
367 buttons
|= MOUSE_EVENT_MBUTTON
;
369 if (kbd_mouse_is_absolute()) {
370 if (!absolute_enabled
) {
375 absolute_enabled
= 1;
378 dx
= x
* 0x7FFF / (width
- 1);
379 dy
= y
* 0x7FFF / (height
- 1);
380 } else if (absolute_enabled
) {
382 absolute_enabled
= 0;
383 } else if (guest_cursor
) {
392 kbd_mouse_event(dx
, dy
, dz
, buttons
);
395 static void toggle_full_screen(DisplayState
*ds
)
397 gui_fullscreen
= !gui_fullscreen
;
399 if (gui_fullscreen
) {
400 gui_saved_grab
= gui_grab
;
410 static void sdl_refresh(DisplayState
*ds
)
412 SDL_Event ev1
, *ev
= &ev1
;
414 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
416 if (last_vm_running
!= vm_running
) {
417 last_vm_running
= vm_running
;
418 sdl_update_caption();
422 SDL_EnableUNICODE(!is_graphic_console());
424 while (SDL_PollEvent(ev
)) {
426 case SDL_VIDEOEXPOSE
:
427 sdl_update(ds
, 0, 0, real_screen
->w
, real_screen
->h
);
431 if (ev
->type
== SDL_KEYDOWN
) {
433 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
436 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
437 (gui_grab_code
| KMOD_LSHIFT
);
439 gui_key_modifier_pressed
= mod_state
;
440 if (gui_key_modifier_pressed
) {
442 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
444 case 0x21: /* 'f' key on US keyboard */
445 toggle_full_screen(ds
);
448 case 0x02 ... 0x0a: /* '1' to '9' keys */
449 /* Reset the modifiers sent to the current console */
451 console_select(keycode
- 0x02);
452 if (!is_graphic_console()) {
453 /* display grab if going to a text console */
462 } else if (!is_graphic_console()) {
465 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
466 switch(ev
->key
.keysym
.sym
) {
467 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
468 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
469 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
470 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
471 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
472 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
473 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
474 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
478 switch(ev
->key
.keysym
.sym
) {
479 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
480 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
481 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
482 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
483 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
484 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
485 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
486 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
487 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break;
488 case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
493 kbd_put_keysym(keysym
);
494 } else if (ev
->key
.keysym
.unicode
!= 0) {
495 kbd_put_keysym(ev
->key
.keysym
.unicode
);
498 } else if (ev
->type
== SDL_KEYUP
) {
500 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
502 mod_state
= (ev
->key
.keysym
.mod
&
503 (gui_grab_code
| KMOD_LSHIFT
));
506 if (gui_key_modifier_pressed
) {
507 gui_key_modifier_pressed
= 0;
508 if (gui_keysym
== 0) {
509 /* exit/enter grab if pressing Ctrl-Alt */
511 /* if the application is not active,
512 do not try to enter grab state. It
514 'SDL_WM_GrabInput(SDL_GRAB_ON)'
515 from blocking all the application
517 if (SDL_GetAppState() & SDL_APPACTIVE
)
522 /* SDL does not send back all the
523 modifiers key, so we must correct it */
531 if (is_graphic_console() && !gui_keysym
)
532 sdl_process_key(&ev
->key
);
536 qemu_system_shutdown_request();
538 case SDL_MOUSEMOTION
:
539 if (gui_grab
|| kbd_mouse_is_absolute() ||
541 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
, 0,
542 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
545 case SDL_MOUSEBUTTONDOWN
:
546 case SDL_MOUSEBUTTONUP
:
548 SDL_MouseButtonEvent
*bev
= &ev
->button
;
549 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
550 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
551 (bev
->button
== SDL_BUTTON_LEFT
)) {
552 /* start grabbing all events */
558 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
559 buttonstate
|= SDL_BUTTON(bev
->button
);
561 buttonstate
&= ~SDL_BUTTON(bev
->button
);
563 #ifdef SDL_BUTTON_WHEELUP
564 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
566 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
570 sdl_send_mouse_event(0, 0, dz
, bev
->x
, bev
->y
, buttonstate
);
574 case SDL_ACTIVEEVENT
:
575 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
576 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
579 if (ev
->active
.state
& SDL_APPACTIVE
) {
580 if (ev
->active
.gain
) {
581 /* Back to default interval */
582 dcl
->gui_timer_interval
= 0;
585 /* Sleeping interval */
586 dcl
->gui_timer_interval
= 500;
597 static void sdl_fill(DisplayState
*ds
, int x
, int y
, int w
, int h
, uint32_t c
)
599 SDL_Rect dst
= { x
, y
, w
, h
};
600 SDL_FillRect(real_screen
, &dst
, c
);
603 static void sdl_mouse_warp(int x
, int y
, int on
)
608 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
609 SDL_SetCursor(guest_sprite
);
610 if (!kbd_mouse_is_absolute() && !absolute_enabled
)
616 guest_x
= x
, guest_y
= y
;
619 static void sdl_mouse_define(int width
, int height
, int bpp
,
620 int hot_x
, int hot_y
,
621 uint8_t *image
, uint8_t *mask
)
623 uint8_t sprite
[256], *line
;
624 int x
, y
, dst
, bypl
, src
= 0;
626 SDL_FreeCursor(guest_sprite
);
628 memset(sprite
, 0, 256);
629 bypl
= ((width
* bpp
+ 31) >> 5) << 2;
630 for (y
= 0, dst
= 0; y
< height
; y
++, image
+= bypl
) {
632 for (x
= 0; x
< width
; x
++, dst
++) {
635 src
= *(line
++); src
|= *(line
++); src
|= *(line
++);
639 src
= *(line
++); src
|= *(line
++);
645 src
= 0xf & (line
[x
>> 1] >> ((x
& 1)) << 2);
648 src
= 3 & (line
[x
>> 2] >> ((x
& 3)) << 1);
651 src
= 1 & (line
[x
>> 3] >> (x
& 7));
655 sprite
[dst
>> 3] |= (1 << (~dst
& 7)) & mask
[dst
>> 3];
658 guest_sprite
= SDL_CreateCursor(sprite
, mask
, width
, height
, hot_x
, hot_y
);
661 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
662 SDL_SetCursor(guest_sprite
);
665 static void sdl_cleanup(void)
668 SDL_FreeCursor(guest_sprite
);
672 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
677 #if defined(__APPLE__)
678 /* always use generic keymaps */
679 if (!keyboard_layout
)
680 keyboard_layout
= "en-us";
682 if(keyboard_layout
) {
683 kbd_layout
= init_keyboard_layout(keyboard_layout
);
691 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
692 if (SDL_Init (flags
)) {
693 fprintf(stderr
, "Could not initialize SDL - exiting\n");
697 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
698 dcl
->dpy_update
= sdl_update
;
699 dcl
->dpy_resize
= sdl_resize
;
700 dcl
->dpy_refresh
= sdl_refresh
;
701 dcl
->dpy_setdata
= sdl_setdata
;
702 dcl
->dpy_fill
= sdl_fill
;
703 ds
->mouse_set
= sdl_mouse_warp
;
704 ds
->cursor_define
= sdl_mouse_define
;
705 register_displaychangelistener(ds
, dcl
);
707 sdl_update_caption();
708 SDL_EnableKeyRepeat(250, 50);
711 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
712 sdl_cursor_normal
= SDL_GetCursor();
717 gui_fullscreen_initial_grab
= 1;