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/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"
113 static kbd_layout_t
*kbd_layout
= NULL
;
115 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
118 /* workaround for X11+SDL bug with AltGR */
119 keysym
= ev
->keysym
.sym
;
120 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
122 /* For Japanese key '\' and '|' */
123 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
126 return keysym2scancode(kbd_layout
, keysym
);
129 /* specific keyboard conversions from scan codes */
133 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
135 return ev
->keysym
.scancode
;
140 #if defined(SDL_VIDEO_DRIVER_X11)
141 #include <X11/XKBlib.h>
143 static int check_for_evdev(void)
148 const char *keycodes
;
150 SDL_VERSION(&info
.version
);
151 if (!SDL_GetWMInfo(&info
))
154 desc
= XkbGetKeyboard(info
.info
.x11
.display
,
155 XkbGBN_AllComponentsMask
,
157 if (desc
== NULL
|| desc
->names
== NULL
)
160 keycodes
= XGetAtomName(info
.info
.x11
.display
, desc
->names
->keycodes
);
161 if (keycodes
== NULL
)
162 fprintf(stderr
, "could not lookup keycode name\n");
163 else if (strstart(keycodes
, "evdev_", NULL
))
165 else if (!strstart(keycodes
, "xfree86_", NULL
))
167 "unknown keycodes `%s', please report to qemu-devel@nongnu.org\n",
170 XkbFreeClientMap(desc
, XkbGBN_AllComponentsMask
, True
);
175 static int check_for_evdev(void)
181 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
184 static int has_evdev
= -1;
187 has_evdev
= check_for_evdev();
189 keycode
= ev
->keysym
.scancode
;
193 } else if (keycode
< 97) {
194 keycode
-= 8; /* just an offset */
195 } else if (keycode
< 158) {
196 /* use conversion table */
198 keycode
= translate_evdev_keycode(keycode
- 97);
200 keycode
= translate_xfree86_keycode(keycode
- 97);
201 } else if (keycode
== 208) { /* Hiragana_Katakana */
203 } else if (keycode
== 211) { /* backslash */
213 static void reset_keys(void)
216 for(i
= 0; i
< 256; i
++) {
217 if (modifiers_state
[i
]) {
219 kbd_put_keycode(0xe0);
220 kbd_put_keycode(i
| 0x80);
221 modifiers_state
[i
] = 0;
226 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
230 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
233 if (ev
->type
== SDL_KEYUP
)
235 kbd_put_keycode(0xe1);
236 kbd_put_keycode(0x1d | v
);
237 kbd_put_keycode(0x45 | v
);
242 keycode
= sdl_keyevent_to_keycode_generic(ev
);
244 keycode
= sdl_keyevent_to_keycode(ev
);
249 /* sent when leaving window: reset the modifiers state */
252 case 0x2a: /* Left Shift */
253 case 0x36: /* Right Shift */
254 case 0x1d: /* Left CTRL */
255 case 0x9d: /* Right CTRL */
256 case 0x38: /* Left ALT */
257 case 0xb8: /* Right ALT */
258 if (ev
->type
== SDL_KEYUP
)
259 modifiers_state
[keycode
] = 0;
261 modifiers_state
[keycode
] = 1;
263 case 0x45: /* num lock */
264 case 0x3a: /* caps lock */
265 /* SDL does not send the key up event, so we generate it */
266 kbd_put_keycode(keycode
);
267 kbd_put_keycode(keycode
| 0x80);
271 /* now send the key code */
273 kbd_put_keycode(0xe0);
274 if (ev
->type
== SDL_KEYUP
)
275 kbd_put_keycode(keycode
| 0x80);
277 kbd_put_keycode(keycode
& 0x7f);
280 static void sdl_update_caption(void)
283 const char *status
= "";
286 status
= " [Stopped]";
289 status
= " - Press Ctrl-Alt to exit grab";
291 status
= " - Press Ctrl-Alt-Shift to exit grab";
295 snprintf(buf
, sizeof(buf
), "QEMU (%s)%s", qemu_name
, status
);
297 snprintf(buf
, sizeof(buf
), "QEMU%s", status
);
299 SDL_WM_SetCaption(buf
, "QEMU");
302 static void sdl_hide_cursor(void)
307 if (kbd_mouse_is_absolute()) {
309 SDL_SetCursor(sdl_cursor_hidden
);
315 static void sdl_show_cursor(void)
320 if (!kbd_mouse_is_absolute()) {
323 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
324 SDL_SetCursor(guest_sprite
);
326 SDL_SetCursor(sdl_cursor_normal
);
330 static void sdl_grab_start(void)
333 SDL_SetCursor(guest_sprite
);
334 if (!kbd_mouse_is_absolute() && !absolute_enabled
)
335 SDL_WarpMouse(guest_x
, guest_y
);
339 if (SDL_WM_GrabInput(SDL_GRAB_ON
) == SDL_GRAB_ON
) {
341 sdl_update_caption();
346 static void sdl_grab_end(void)
348 SDL_WM_GrabInput(SDL_GRAB_OFF
);
351 sdl_update_caption();
354 static void sdl_send_mouse_event(int dx
, int dy
, int dz
, int x
, int y
, int state
)
358 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
359 buttons
|= MOUSE_EVENT_LBUTTON
;
360 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
361 buttons
|= MOUSE_EVENT_RBUTTON
;
362 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
363 buttons
|= MOUSE_EVENT_MBUTTON
;
365 if (kbd_mouse_is_absolute()) {
366 if (!absolute_enabled
) {
371 absolute_enabled
= 1;
374 dx
= x
* 0x7FFF / (width
- 1);
375 dy
= y
* 0x7FFF / (height
- 1);
376 } else if (absolute_enabled
) {
378 absolute_enabled
= 0;
379 } else if (guest_cursor
) {
388 kbd_mouse_event(dx
, dy
, dz
, buttons
);
391 static void toggle_full_screen(DisplayState
*ds
)
393 gui_fullscreen
= !gui_fullscreen
;
395 if (gui_fullscreen
) {
396 gui_saved_grab
= gui_grab
;
406 static void sdl_refresh(DisplayState
*ds
)
408 SDL_Event ev1
, *ev
= &ev1
;
410 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
412 if (last_vm_running
!= vm_running
) {
413 last_vm_running
= vm_running
;
414 sdl_update_caption();
418 SDL_EnableUNICODE(!is_graphic_console());
420 while (SDL_PollEvent(ev
)) {
422 case SDL_VIDEOEXPOSE
:
423 sdl_update(ds
, 0, 0, real_screen
->w
, real_screen
->h
);
427 if (ev
->type
== SDL_KEYDOWN
) {
429 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
432 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
433 (gui_grab_code
| KMOD_LSHIFT
);
435 gui_key_modifier_pressed
= mod_state
;
436 if (gui_key_modifier_pressed
) {
438 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
440 case 0x21: /* 'f' key on US keyboard */
441 toggle_full_screen(ds
);
444 case 0x02 ... 0x0a: /* '1' to '9' keys */
445 /* Reset the modifiers sent to the current console */
447 console_select(keycode
- 0x02);
448 if (!is_graphic_console()) {
449 /* display grab if going to a text console */
458 } else if (!is_graphic_console()) {
461 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
462 switch(ev
->key
.keysym
.sym
) {
463 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
464 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
465 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
466 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
467 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
468 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
469 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
470 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
474 switch(ev
->key
.keysym
.sym
) {
475 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
476 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
477 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
478 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
479 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
480 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
481 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
482 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
483 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break;
484 case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
489 kbd_put_keysym(keysym
);
490 } else if (ev
->key
.keysym
.unicode
!= 0) {
491 kbd_put_keysym(ev
->key
.keysym
.unicode
);
494 } else if (ev
->type
== SDL_KEYUP
) {
496 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
498 mod_state
= (ev
->key
.keysym
.mod
&
499 (gui_grab_code
| KMOD_LSHIFT
));
502 if (gui_key_modifier_pressed
) {
503 gui_key_modifier_pressed
= 0;
504 if (gui_keysym
== 0) {
505 /* exit/enter grab if pressing Ctrl-Alt */
507 /* if the application is not active,
508 do not try to enter grab state. It
510 'SDL_WM_GrabInput(SDL_GRAB_ON)'
511 from blocking all the application
513 if (SDL_GetAppState() & SDL_APPACTIVE
)
518 /* SDL does not send back all the
519 modifiers key, so we must correct it */
527 if (is_graphic_console() && !gui_keysym
)
528 sdl_process_key(&ev
->key
);
532 qemu_system_shutdown_request();
534 case SDL_MOUSEMOTION
:
535 if (gui_grab
|| kbd_mouse_is_absolute() ||
537 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
, 0,
538 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
541 case SDL_MOUSEBUTTONDOWN
:
542 case SDL_MOUSEBUTTONUP
:
544 SDL_MouseButtonEvent
*bev
= &ev
->button
;
545 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
546 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
547 (bev
->button
== SDL_BUTTON_LEFT
)) {
548 /* start grabbing all events */
554 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
555 buttonstate
|= SDL_BUTTON(bev
->button
);
557 buttonstate
&= ~SDL_BUTTON(bev
->button
);
559 #ifdef SDL_BUTTON_WHEELUP
560 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
562 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
566 sdl_send_mouse_event(0, 0, dz
, bev
->x
, bev
->y
, buttonstate
);
570 case SDL_ACTIVEEVENT
:
571 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
572 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
575 if (ev
->active
.state
& SDL_APPACTIVE
) {
576 if (ev
->active
.gain
) {
577 /* Back to default interval */
578 dcl
->gui_timer_interval
= 0;
581 /* Sleeping interval */
582 dcl
->gui_timer_interval
= 500;
593 static void sdl_fill(DisplayState
*ds
, int x
, int y
, int w
, int h
, uint32_t c
)
595 SDL_Rect dst
= { x
, y
, w
, h
};
596 SDL_FillRect(real_screen
, &dst
, c
);
599 static void sdl_mouse_warp(int x
, int y
, int on
)
604 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
605 SDL_SetCursor(guest_sprite
);
606 if (!kbd_mouse_is_absolute() && !absolute_enabled
)
612 guest_x
= x
, guest_y
= y
;
615 static void sdl_mouse_define(int width
, int height
, int bpp
,
616 int hot_x
, int hot_y
,
617 uint8_t *image
, uint8_t *mask
)
619 uint8_t sprite
[256], *line
;
620 int x
, y
, dst
, bypl
, src
= 0;
622 SDL_FreeCursor(guest_sprite
);
624 memset(sprite
, 0, 256);
625 bypl
= ((width
* bpp
+ 31) >> 5) << 2;
626 for (y
= 0, dst
= 0; y
< height
; y
++, image
+= bypl
) {
628 for (x
= 0; x
< width
; x
++, dst
++) {
631 src
= *(line
++); src
|= *(line
++); src
|= *(line
++);
635 src
= *(line
++); src
|= *(line
++);
641 src
= 0xf & (line
[x
>> 1] >> ((x
& 1)) << 2);
644 src
= 3 & (line
[x
>> 2] >> ((x
& 3)) << 1);
647 src
= 1 & (line
[x
>> 3] >> (x
& 7));
651 sprite
[dst
>> 3] |= (1 << (~dst
& 7)) & mask
[dst
>> 3];
654 guest_sprite
= SDL_CreateCursor(sprite
, mask
, width
, height
, hot_x
, hot_y
);
657 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
658 SDL_SetCursor(guest_sprite
);
661 static void sdl_cleanup(void)
664 SDL_FreeCursor(guest_sprite
);
668 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
673 #if defined(__APPLE__)
674 /* always use generic keymaps */
675 if (!keyboard_layout
)
676 keyboard_layout
= "en-us";
678 if(keyboard_layout
) {
679 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
687 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
688 if (SDL_Init (flags
)) {
689 fprintf(stderr
, "Could not initialize SDL - exiting\n");
693 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
694 dcl
->dpy_update
= sdl_update
;
695 dcl
->dpy_resize
= sdl_resize
;
696 dcl
->dpy_refresh
= sdl_refresh
;
697 dcl
->dpy_setdata
= sdl_setdata
;
698 dcl
->dpy_fill
= sdl_fill
;
699 ds
->mouse_set
= sdl_mouse_warp
;
700 ds
->cursor_define
= sdl_mouse_define
;
701 register_displaychangelistener(ds
, dcl
);
703 sdl_update_caption();
704 SDL_EnableKeyRepeat(250, 50);
707 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
708 sdl_cursor_normal
= SDL_GetCursor();
713 gui_fullscreen_initial_grab
= 1;