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
25 #include <SDL_syswm.h>
31 #include "qemu-common.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;
56 static uint8_t allocator
;
57 static uint8_t hostbpp
;
59 static void sdl_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
61 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
68 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
70 SDL_UpdateRect(real_screen
, x
, y
, w
, h
);
73 static void sdl_setdata(DisplayState
*ds
)
78 rec
.w
= real_screen
->w
;
79 rec
.h
= real_screen
->h
;
81 if (guest_screen
!= NULL
) SDL_FreeSurface(guest_screen
);
83 guest_screen
= SDL_CreateRGBSurfaceFrom(ds_get_data(ds
), ds_get_width(ds
), ds_get_height(ds
),
84 ds_get_bits_per_pixel(ds
), ds_get_linesize(ds
),
85 ds
->surface
->pf
.rmask
, ds
->surface
->pf
.gmask
,
86 ds
->surface
->pf
.bmask
, ds
->surface
->pf
.amask
);
89 static void do_sdl_resize(int new_width
, int new_height
, int bpp
)
93 // printf("resizing to %d %d\n", w, h);
95 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
97 flags
|= SDL_FULLSCREEN
;
103 real_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
105 fprintf(stderr
, "Could not open SDL display\n");
110 static void sdl_resize(DisplayState
*ds
)
113 do_sdl_resize(ds_get_width(ds
), ds_get_height(ds
), 0);
116 if (guest_screen
!= NULL
) {
117 SDL_FreeSurface(guest_screen
);
123 static PixelFormat
sdl_to_qemu_pixelformat(SDL_PixelFormat
*sdl_pf
)
127 memset(&qemu_pf
, 0x00, sizeof(PixelFormat
));
129 qemu_pf
.bits_per_pixel
= sdl_pf
->BitsPerPixel
;
130 qemu_pf
.bytes_per_pixel
= sdl_pf
->BytesPerPixel
;
131 qemu_pf
.depth
= (qemu_pf
.bits_per_pixel
) == 32 ? 24 : (qemu_pf
.bits_per_pixel
);
133 qemu_pf
.rmask
= sdl_pf
->Rmask
;
134 qemu_pf
.gmask
= sdl_pf
->Gmask
;
135 qemu_pf
.bmask
= sdl_pf
->Bmask
;
136 qemu_pf
.amask
= sdl_pf
->Amask
;
138 qemu_pf
.rshift
= sdl_pf
->Rshift
;
139 qemu_pf
.gshift
= sdl_pf
->Gshift
;
140 qemu_pf
.bshift
= sdl_pf
->Bshift
;
141 qemu_pf
.ashift
= sdl_pf
->Ashift
;
143 qemu_pf
.rbits
= 8 - sdl_pf
->Rloss
;
144 qemu_pf
.gbits
= 8 - sdl_pf
->Gloss
;
145 qemu_pf
.bbits
= 8 - sdl_pf
->Bloss
;
146 qemu_pf
.abits
= 8 - sdl_pf
->Aloss
;
148 qemu_pf
.rmax
= ((1 << qemu_pf
.rbits
) - 1);
149 qemu_pf
.gmax
= ((1 << qemu_pf
.gbits
) - 1);
150 qemu_pf
.bmax
= ((1 << qemu_pf
.bbits
) - 1);
151 qemu_pf
.amax
= ((1 << qemu_pf
.abits
) - 1);
156 static DisplaySurface
* sdl_create_displaysurface(int width
, int height
)
158 DisplaySurface
*surface
= (DisplaySurface
*) qemu_mallocz(sizeof(DisplaySurface
));
159 if (surface
== NULL
) {
160 fprintf(stderr
, "sdl_create_displaysurface: malloc failed\n");
164 surface
->width
= width
;
165 surface
->height
= height
;
168 do_sdl_resize(width
, height
, 16);
170 do_sdl_resize(width
, height
, 32);
172 surface
->pf
= sdl_to_qemu_pixelformat(real_screen
->format
);
173 surface
->linesize
= real_screen
->pitch
;
174 surface
->data
= real_screen
->pixels
;
176 #ifdef WORDS_BIGENDIAN
177 surface
->flags
= QEMU_ALLOCATED_FLAG
| QEMU_BIG_ENDIAN_FLAG
;
179 surface
->flags
= QEMU_ALLOCATED_FLAG
;
186 static void sdl_free_displaysurface(DisplaySurface
*surface
)
194 static DisplaySurface
* sdl_resize_displaysurface(DisplaySurface
*surface
, int width
, int height
)
196 sdl_free_displaysurface(surface
);
197 return sdl_create_displaysurface(width
, height
);
200 /* generic keyboard conversion */
202 #include "sdl_keysym.h"
204 static kbd_layout_t
*kbd_layout
= NULL
;
206 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
209 /* workaround for X11+SDL bug with AltGR */
210 keysym
= ev
->keysym
.sym
;
211 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
213 /* For Japanese key '\' and '|' */
214 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
217 return keysym2scancode(kbd_layout
, keysym
);
220 /* specific keyboard conversions from scan codes */
224 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
226 return ev
->keysym
.scancode
;
231 #if defined(SDL_VIDEO_DRIVER_X11)
232 #include <X11/XKBlib.h>
234 static int check_for_evdev(void)
239 const char *keycodes
;
241 SDL_VERSION(&info
.version
);
242 if (!SDL_GetWMInfo(&info
))
245 desc
= XkbGetKeyboard(info
.info
.x11
.display
,
246 XkbGBN_AllComponentsMask
,
248 if (desc
== NULL
|| desc
->names
== NULL
)
251 keycodes
= XGetAtomName(info
.info
.x11
.display
, desc
->names
->keycodes
);
252 if (keycodes
== NULL
)
253 fprintf(stderr
, "could not lookup keycode name\n");
254 else if (strstart(keycodes
, "evdev_", NULL
))
256 else if (!strstart(keycodes
, "xfree86_", NULL
))
258 "unknown keycodes `%s', please report to qemu-devel@nongnu.org\n",
261 XkbFreeClientMap(desc
, XkbGBN_AllComponentsMask
, True
);
266 static int check_for_evdev(void)
272 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
275 static int has_evdev
= -1;
278 has_evdev
= check_for_evdev();
280 keycode
= ev
->keysym
.scancode
;
284 } else if (keycode
< 97) {
285 keycode
-= 8; /* just an offset */
286 } else if (keycode
< 158) {
287 /* use conversion table */
289 keycode
= translate_evdev_keycode(keycode
- 97);
291 keycode
= translate_xfree86_keycode(keycode
- 97);
292 } else if (keycode
== 208) { /* Hiragana_Katakana */
294 } else if (keycode
== 211) { /* backslash */
304 static void reset_keys(void)
307 for(i
= 0; i
< 256; i
++) {
308 if (modifiers_state
[i
]) {
310 kbd_put_keycode(0xe0);
311 kbd_put_keycode(i
| 0x80);
312 modifiers_state
[i
] = 0;
317 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
321 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
324 if (ev
->type
== SDL_KEYUP
)
326 kbd_put_keycode(0xe1);
327 kbd_put_keycode(0x1d | v
);
328 kbd_put_keycode(0x45 | v
);
333 keycode
= sdl_keyevent_to_keycode_generic(ev
);
335 keycode
= sdl_keyevent_to_keycode(ev
);
340 /* sent when leaving window: reset the modifiers state */
343 case 0x2a: /* Left Shift */
344 case 0x36: /* Right Shift */
345 case 0x1d: /* Left CTRL */
346 case 0x9d: /* Right CTRL */
347 case 0x38: /* Left ALT */
348 case 0xb8: /* Right ALT */
349 if (ev
->type
== SDL_KEYUP
)
350 modifiers_state
[keycode
] = 0;
352 modifiers_state
[keycode
] = 1;
354 case 0x45: /* num lock */
355 case 0x3a: /* caps lock */
356 /* SDL does not send the key up event, so we generate it */
357 kbd_put_keycode(keycode
);
358 kbd_put_keycode(keycode
| 0x80);
362 /* now send the key code */
364 kbd_put_keycode(0xe0);
365 if (ev
->type
== SDL_KEYUP
)
366 kbd_put_keycode(keycode
| 0x80);
368 kbd_put_keycode(keycode
& 0x7f);
371 static void sdl_update_caption(void)
374 const char *status
= "";
377 status
= " [Stopped]";
380 status
= " - Press Ctrl-Alt to exit grab";
382 status
= " - Press Ctrl-Alt-Shift to exit grab";
386 snprintf(buf
, sizeof(buf
), "QEMU (%s)%s", qemu_name
, status
);
388 snprintf(buf
, sizeof(buf
), "QEMU%s", status
);
390 SDL_WM_SetCaption(buf
, "QEMU");
393 static void sdl_hide_cursor(void)
398 if (kbd_mouse_is_absolute()) {
400 SDL_SetCursor(sdl_cursor_hidden
);
406 static void sdl_show_cursor(void)
411 if (!kbd_mouse_is_absolute()) {
414 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
415 SDL_SetCursor(guest_sprite
);
417 SDL_SetCursor(sdl_cursor_normal
);
421 static void sdl_grab_start(void)
424 SDL_SetCursor(guest_sprite
);
425 if (!kbd_mouse_is_absolute() && !absolute_enabled
)
426 SDL_WarpMouse(guest_x
, guest_y
);
430 if (SDL_WM_GrabInput(SDL_GRAB_ON
) == SDL_GRAB_ON
) {
432 sdl_update_caption();
437 static void sdl_grab_end(void)
439 SDL_WM_GrabInput(SDL_GRAB_OFF
);
442 sdl_update_caption();
445 static void sdl_send_mouse_event(int dx
, int dy
, int dz
, int x
, int y
, int state
)
449 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
450 buttons
|= MOUSE_EVENT_LBUTTON
;
451 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
452 buttons
|= MOUSE_EVENT_RBUTTON
;
453 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
454 buttons
|= MOUSE_EVENT_MBUTTON
;
456 if (kbd_mouse_is_absolute()) {
457 if (!absolute_enabled
) {
462 absolute_enabled
= 1;
465 dx
= x
* 0x7FFF / (width
- 1);
466 dy
= y
* 0x7FFF / (height
- 1);
467 } else if (absolute_enabled
) {
469 absolute_enabled
= 0;
470 } else if (guest_cursor
) {
479 kbd_mouse_event(dx
, dy
, dz
, buttons
);
482 static void toggle_full_screen(DisplayState
*ds
)
484 gui_fullscreen
= !gui_fullscreen
;
485 do_sdl_resize(real_screen
->w
, real_screen
->h
, real_screen
->format
->BitsPerPixel
);
486 if (gui_fullscreen
) {
487 gui_saved_grab
= gui_grab
;
497 static void sdl_refresh(DisplayState
*ds
)
499 SDL_Event ev1
, *ev
= &ev1
;
501 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
503 if (last_vm_running
!= vm_running
) {
504 last_vm_running
= vm_running
;
505 sdl_update_caption();
509 SDL_EnableUNICODE(!is_graphic_console());
511 while (SDL_PollEvent(ev
)) {
513 case SDL_VIDEOEXPOSE
:
514 sdl_update(ds
, 0, 0, real_screen
->w
, real_screen
->h
);
518 if (ev
->type
== SDL_KEYDOWN
) {
520 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
523 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
524 (gui_grab_code
| KMOD_LSHIFT
);
526 gui_key_modifier_pressed
= mod_state
;
527 if (gui_key_modifier_pressed
) {
529 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
531 case 0x21: /* 'f' key on US keyboard */
532 toggle_full_screen(ds
);
535 case 0x02 ... 0x0a: /* '1' to '9' keys */
536 /* Reset the modifiers sent to the current console */
538 console_select(keycode
- 0x02);
539 if (!is_graphic_console()) {
540 /* display grab if going to a text console */
549 } else if (!is_graphic_console()) {
552 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
553 switch(ev
->key
.keysym
.sym
) {
554 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
555 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
556 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
557 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
558 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
559 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
560 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
561 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
565 switch(ev
->key
.keysym
.sym
) {
566 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
567 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
568 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
569 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
570 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
571 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
572 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
573 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
574 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break;
575 case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
580 kbd_put_keysym(keysym
);
581 } else if (ev
->key
.keysym
.unicode
!= 0) {
582 kbd_put_keysym(ev
->key
.keysym
.unicode
);
585 } else if (ev
->type
== SDL_KEYUP
) {
587 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
589 mod_state
= (ev
->key
.keysym
.mod
&
590 (gui_grab_code
| KMOD_LSHIFT
));
593 if (gui_key_modifier_pressed
) {
594 gui_key_modifier_pressed
= 0;
595 if (gui_keysym
== 0) {
596 /* exit/enter grab if pressing Ctrl-Alt */
598 /* if the application is not active,
599 do not try to enter grab state. It
601 'SDL_WM_GrabInput(SDL_GRAB_ON)'
602 from blocking all the application
604 if (SDL_GetAppState() & SDL_APPACTIVE
)
609 /* SDL does not send back all the
610 modifiers key, so we must correct it */
618 if (is_graphic_console() && !gui_keysym
)
619 sdl_process_key(&ev
->key
);
623 qemu_system_shutdown_request();
625 case SDL_MOUSEMOTION
:
626 if (gui_grab
|| kbd_mouse_is_absolute() ||
628 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
, 0,
629 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
632 case SDL_MOUSEBUTTONDOWN
:
633 case SDL_MOUSEBUTTONUP
:
635 SDL_MouseButtonEvent
*bev
= &ev
->button
;
636 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
637 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
638 (bev
->button
== SDL_BUTTON_LEFT
)) {
639 /* start grabbing all events */
645 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
646 buttonstate
|= SDL_BUTTON(bev
->button
);
648 buttonstate
&= ~SDL_BUTTON(bev
->button
);
650 #ifdef SDL_BUTTON_WHEELUP
651 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
653 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
657 sdl_send_mouse_event(0, 0, dz
, bev
->x
, bev
->y
, buttonstate
);
661 case SDL_ACTIVEEVENT
:
662 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
663 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
666 if (ev
->active
.state
& SDL_APPACTIVE
) {
667 if (ev
->active
.gain
) {
668 /* Back to default interval */
669 dcl
->gui_timer_interval
= 0;
672 /* Sleeping interval */
673 dcl
->gui_timer_interval
= 500;
684 static void sdl_fill(DisplayState
*ds
, int x
, int y
, int w
, int h
, uint32_t c
)
686 SDL_Rect dst
= { x
, y
, w
, h
};
687 SDL_FillRect(real_screen
, &dst
, c
);
690 static void sdl_mouse_warp(int x
, int y
, int on
)
695 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
696 SDL_SetCursor(guest_sprite
);
697 if (!kbd_mouse_is_absolute() && !absolute_enabled
)
703 guest_x
= x
, guest_y
= y
;
706 static void sdl_mouse_define(int width
, int height
, int bpp
,
707 int hot_x
, int hot_y
,
708 uint8_t *image
, uint8_t *mask
)
710 uint8_t sprite
[256], *line
;
711 int x
, y
, dst
, bypl
, src
= 0;
713 SDL_FreeCursor(guest_sprite
);
715 memset(sprite
, 0, 256);
716 bypl
= ((width
* bpp
+ 31) >> 5) << 2;
717 for (y
= 0, dst
= 0; y
< height
; y
++, image
+= bypl
) {
719 for (x
= 0; x
< width
; x
++, dst
++) {
722 src
= *(line
++); src
|= *(line
++); src
|= *(line
++);
726 src
= *(line
++); src
|= *(line
++);
732 src
= 0xf & (line
[x
>> 1] >> ((x
& 1)) << 2);
735 src
= 3 & (line
[x
>> 2] >> ((x
& 3)) << 1);
738 src
= 1 & (line
[x
>> 3] >> (x
& 7));
742 sprite
[dst
>> 3] |= (1 << (~dst
& 7)) & mask
[dst
>> 3];
745 guest_sprite
= SDL_CreateCursor(sprite
, mask
, width
, height
, hot_x
, hot_y
);
748 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
749 SDL_SetCursor(guest_sprite
);
752 static void sdl_cleanup(void)
755 SDL_FreeCursor(guest_sprite
);
759 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
763 DisplayAllocator
*da
;
764 const SDL_VideoInfo
*vi
;
766 #if defined(__APPLE__)
767 /* always use generic keymaps */
768 if (!keyboard_layout
)
769 keyboard_layout
= "en-us";
771 if(keyboard_layout
) {
772 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
780 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
781 if (SDL_Init (flags
)) {
782 fprintf(stderr
, "Could not initialize SDL - exiting\n");
785 vi
= SDL_GetVideoInfo();
786 hostbpp
= vi
->vfmt
->BitsPerPixel
;
788 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
789 dcl
->dpy_update
= sdl_update
;
790 dcl
->dpy_resize
= sdl_resize
;
791 dcl
->dpy_refresh
= sdl_refresh
;
792 dcl
->dpy_setdata
= sdl_setdata
;
793 dcl
->dpy_fill
= sdl_fill
;
794 ds
->mouse_set
= sdl_mouse_warp
;
795 ds
->cursor_define
= sdl_mouse_define
;
796 register_displaychangelistener(ds
, dcl
);
798 da
= qemu_mallocz(sizeof(DisplayAllocator
));
799 da
->create_displaysurface
= sdl_create_displaysurface
;
800 da
->resize_displaysurface
= sdl_resize_displaysurface
;
801 da
->free_displaysurface
= sdl_free_displaysurface
;
802 if (register_displayallocator(ds
, da
) == da
) {
803 DisplaySurface
*surf
;
804 surf
= sdl_create_displaysurface(ds_get_width(ds
), ds_get_height(ds
));
805 defaultallocator_free_displaysurface(ds
->surface
);
810 sdl_update_caption();
811 SDL_EnableKeyRepeat(250, 50);
814 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
815 sdl_cursor_normal
= SDL_GetCursor();
820 gui_fullscreen_initial_grab
= 1;