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 /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
26 #undef WIN32_LEAN_AND_MEAN
29 #include <SDL_syswm.h>
31 #include "qemu-common.h"
32 #include "ui/console.h"
34 #include "sysemu/sysemu.h"
38 static DisplayChangeListener
*dcl
;
39 static DisplaySurface
*surface
;
40 static SDL_Surface
*real_screen
;
41 static SDL_Surface
*guest_screen
= NULL
;
42 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
43 static int last_vm_running
;
44 static bool gui_saved_scaling
;
45 static int gui_saved_width
;
46 static int gui_saved_height
;
47 static int gui_saved_grab
;
48 static int gui_fullscreen
;
49 static int gui_noframe
;
50 static int gui_key_modifier_pressed
;
51 static int gui_keysym
;
52 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
53 static uint8_t modifiers_state
[256];
54 static SDL_Cursor
*sdl_cursor_normal
;
55 static SDL_Cursor
*sdl_cursor_hidden
;
56 static int absolute_enabled
= 0;
57 static int guest_cursor
= 0;
58 static int guest_x
, guest_y
;
59 static SDL_Cursor
*guest_sprite
= NULL
;
60 static SDL_PixelFormat host_format
;
61 static int scaling_active
= 0;
62 static Notifier mouse_mode_notifier
;
64 static void sdl_update(DisplayChangeListener
*dcl
,
65 int x
, int y
, int w
, int h
)
67 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
75 if (!scaling_active
) {
76 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
78 if (sdl_zoom_blit(guest_screen
, real_screen
, SMOOTHING_ON
, &rec
) < 0) {
79 fprintf(stderr
, "Zoom blit failed\n");
84 SDL_UpdateRect(real_screen
, rec
.x
, rec
.y
, rec
.w
, rec
.h
);
87 static void do_sdl_resize(int width
, int height
, int bpp
)
90 SDL_Surface
*tmp_screen
;
92 // printf("resizing to %d %d\n", w, h);
94 flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
;
96 flags
|= SDL_FULLSCREEN
;
98 flags
|= SDL_RESIZABLE
;
101 flags
|= SDL_NOFRAME
;
103 tmp_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
106 fprintf(stderr
, "Could not open SDL display (%dx%dx%d): %s\n",
107 width
, height
, bpp
, SDL_GetError());
112 * Revert to the previous video mode if the change of resizing or
116 fprintf(stderr
, "Failed to set SDL display (%dx%dx%d): %s\n",
117 width
, height
, bpp
, SDL_GetError());
122 real_screen
= tmp_screen
;
125 static void sdl_switch(DisplayChangeListener
*dcl
,
126 DisplaySurface
*new_surface
)
128 PixelFormat pf
= qemu_pixelformat_from_pixman(new_surface
->format
);
130 /* temporary hack: allows to call sdl_switch to handle scaling changes */
132 surface
= new_surface
;
135 if (!scaling_active
) {
136 do_sdl_resize(surface_width(surface
), surface_height(surface
), 0);
137 } else if (real_screen
->format
->BitsPerPixel
!=
138 surface_bits_per_pixel(surface
)) {
139 do_sdl_resize(real_screen
->w
, real_screen
->h
,
140 surface_bits_per_pixel(surface
));
143 if (guest_screen
!= NULL
) {
144 SDL_FreeSurface(guest_screen
);
146 guest_screen
= SDL_CreateRGBSurfaceFrom
147 (surface_data(surface
),
148 surface_width(surface
), surface_height(surface
),
149 surface_bits_per_pixel(surface
), surface_stride(surface
),
154 static bool sdl_check_format(DisplayChangeListener
*dcl
,
155 pixman_format_code_t format
)
158 * We let SDL convert for us a few more formats than,
159 * the native ones. Thes are the ones I have tested.
161 return (format
== PIXMAN_x8r8g8b8
||
162 format
== PIXMAN_b8g8r8x8
||
163 format
== PIXMAN_x1r5g5b5
||
164 format
== PIXMAN_r5g6b5
);
167 /* generic keyboard conversion */
169 #include "sdl_keysym.h"
171 static kbd_layout_t
*kbd_layout
= NULL
;
173 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
176 /* workaround for X11+SDL bug with AltGR */
177 keysym
= ev
->keysym
.sym
;
178 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
180 /* For Japanese key '\' and '|' */
181 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
184 return keysym2scancode(kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
187 /* specific keyboard conversions from scan codes */
191 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
193 return ev
->keysym
.scancode
;
198 #if defined(SDL_VIDEO_DRIVER_X11)
199 #include <X11/XKBlib.h>
201 static int check_for_evdev(void)
204 XkbDescPtr desc
= NULL
;
206 char *keycodes
= NULL
;
208 SDL_VERSION(&info
.version
);
209 if (!SDL_GetWMInfo(&info
)) {
212 desc
= XkbGetKeyboard(info
.info
.x11
.display
,
213 XkbGBN_AllComponentsMask
,
215 if (desc
&& desc
->names
) {
216 keycodes
= XGetAtomName(info
.info
.x11
.display
, desc
->names
->keycodes
);
217 if (keycodes
== NULL
) {
218 fprintf(stderr
, "could not lookup keycode name\n");
219 } else if (strstart(keycodes
, "evdev", NULL
)) {
221 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
222 fprintf(stderr
, "unknown keycodes `%s', please report to "
223 "qemu-devel@nongnu.org\n", keycodes
);
228 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
236 static int check_for_evdev(void)
242 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
245 static int has_evdev
= -1;
248 has_evdev
= check_for_evdev();
250 keycode
= ev
->keysym
.scancode
;
254 } else if (keycode
< 97) {
255 keycode
-= 8; /* just an offset */
256 } else if (keycode
< 158) {
257 /* use conversion table */
259 keycode
= translate_evdev_keycode(keycode
- 97);
261 keycode
= translate_xfree86_keycode(keycode
- 97);
262 } else if (keycode
== 208) { /* Hiragana_Katakana */
264 } else if (keycode
== 211) { /* backslash */
274 static void reset_keys(void)
277 for(i
= 0; i
< 256; i
++) {
278 if (modifiers_state
[i
]) {
279 qemu_input_event_send_key_number(dcl
->con
, i
, false);
280 modifiers_state
[i
] = 0;
285 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
289 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
291 qemu_input_event_send_key_qcode(dcl
->con
, Q_KEY_CODE_PAUSE
,
292 ev
->type
== SDL_KEYDOWN
);
297 keycode
= sdl_keyevent_to_keycode_generic(ev
);
299 keycode
= sdl_keyevent_to_keycode(ev
);
304 /* sent when leaving window: reset the modifiers state */
307 case 0x2a: /* Left Shift */
308 case 0x36: /* Right Shift */
309 case 0x1d: /* Left CTRL */
310 case 0x9d: /* Right CTRL */
311 case 0x38: /* Left ALT */
312 case 0xb8: /* Right ALT */
313 if (ev
->type
== SDL_KEYUP
)
314 modifiers_state
[keycode
] = 0;
316 modifiers_state
[keycode
] = 1;
318 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
319 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
320 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
321 case 0x45: /* num lock */
322 case 0x3a: /* caps lock */
323 /* SDL does not send the key up event, so we generate it */
324 qemu_input_event_send_key_number(dcl
->con
, keycode
, true);
325 qemu_input_event_send_key_number(dcl
->con
, keycode
, false);
330 /* now send the key code */
331 qemu_input_event_send_key_number(dcl
->con
, keycode
,
332 ev
->type
== SDL_KEYDOWN
);
335 static void sdl_update_caption(void)
337 char win_title
[1024];
338 char icon_title
[1024];
339 const char *status
= "";
341 if (!runstate_is_running())
342 status
= " [Stopped]";
345 status
= " - Press Ctrl-Alt-Shift to exit mouse grab";
347 status
= " - Press Right-Ctrl to exit mouse grab";
349 status
= " - Press Ctrl-Alt to exit mouse grab";
353 snprintf(win_title
, sizeof(win_title
), "QEMU (%s)%s", qemu_name
, status
);
354 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
356 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
357 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
360 SDL_WM_SetCaption(win_title
, icon_title
);
363 static void sdl_hide_cursor(void)
368 if (qemu_input_is_absolute()) {
370 SDL_SetCursor(sdl_cursor_hidden
);
376 static void sdl_show_cursor(void)
381 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL
)) {
384 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
385 SDL_SetCursor(guest_sprite
);
387 SDL_SetCursor(sdl_cursor_normal
);
391 static void sdl_grab_start(void)
394 * If the application is not active, do not try to enter grab state. This
395 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
396 * application (SDL bug).
398 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS
)) {
402 SDL_SetCursor(guest_sprite
);
403 if (!qemu_input_is_absolute() && !absolute_enabled
) {
404 SDL_WarpMouse(guest_x
, guest_y
);
408 SDL_WM_GrabInput(SDL_GRAB_ON
);
410 sdl_update_caption();
413 static void sdl_grab_end(void)
415 SDL_WM_GrabInput(SDL_GRAB_OFF
);
418 sdl_update_caption();
421 static void absolute_mouse_grab(void)
423 int mouse_x
, mouse_y
;
425 SDL_GetMouseState(&mouse_x
, &mouse_y
);
426 if (mouse_x
> 0 && mouse_x
< real_screen
->w
- 1 &&
427 mouse_y
> 0 && mouse_y
< real_screen
->h
- 1) {
432 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
434 if (qemu_input_is_absolute()) {
435 if (!absolute_enabled
) {
436 absolute_enabled
= 1;
437 if (qemu_console_is_graphic(NULL
)) {
438 absolute_mouse_grab();
441 } else if (absolute_enabled
) {
442 if (!gui_fullscreen
) {
445 absolute_enabled
= 0;
449 static void sdl_send_mouse_event(int dx
, int dy
, int x
, int y
, int state
)
451 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
452 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
453 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
454 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
455 [INPUT_BUTTON_WHEEL_UP
] = SDL_BUTTON(SDL_BUTTON_WHEELUP
),
456 [INPUT_BUTTON_WHEEL_DOWN
] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN
),
458 static uint32_t prev_state
;
460 if (prev_state
!= state
) {
461 qemu_input_update_buttons(dcl
->con
, bmap
, prev_state
, state
);
465 if (qemu_input_is_absolute()) {
466 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_X
, x
,
468 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_Y
, y
,
479 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_X
, dx
);
480 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_Y
, dy
);
482 qemu_input_event_sync();
485 static void sdl_scale(int width
, int height
)
487 int bpp
= real_screen
->format
->BitsPerPixel
;
489 if (bpp
!= 16 && bpp
!= 32) {
492 do_sdl_resize(width
, height
, bpp
);
496 static void toggle_full_screen(void)
498 int width
= surface_width(surface
);
499 int height
= surface_height(surface
);
500 int bpp
= surface_bits_per_pixel(surface
);
502 gui_fullscreen
= !gui_fullscreen
;
503 if (gui_fullscreen
) {
504 gui_saved_width
= real_screen
->w
;
505 gui_saved_height
= real_screen
->h
;
506 gui_saved_scaling
= scaling_active
;
508 do_sdl_resize(width
, height
, bpp
);
511 gui_saved_grab
= gui_grab
;
514 if (gui_saved_scaling
) {
515 sdl_scale(gui_saved_width
, gui_saved_height
);
517 do_sdl_resize(width
, height
, 0);
519 if (!gui_saved_grab
|| !qemu_console_is_graphic(NULL
)) {
523 graphic_hw_invalidate(NULL
);
524 graphic_hw_update(NULL
);
527 static void handle_keydown(SDL_Event
*ev
)
533 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
534 (gui_grab_code
| KMOD_LSHIFT
);
535 } else if (ctrl_grab
) {
536 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
538 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
540 gui_key_modifier_pressed
= mod_state
;
542 if (gui_key_modifier_pressed
) {
543 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
545 case 0x21: /* 'f' key on US keyboard */
546 toggle_full_screen();
549 case 0x16: /* 'u' key on US keyboard */
550 if (scaling_active
) {
552 sdl_switch(dcl
, NULL
);
553 graphic_hw_invalidate(NULL
);
554 graphic_hw_update(NULL
);
558 case 0x02 ... 0x0a: /* '1' to '9' keys */
559 /* Reset the modifiers sent to the current console */
561 console_select(keycode
- 0x02);
563 if (gui_fullscreen
) {
566 if (!qemu_console_is_graphic(NULL
)) {
567 /* release grab if going to a text console */
570 } else if (absolute_enabled
) {
573 } else if (absolute_enabled
) {
575 absolute_mouse_grab();
580 if (!gui_fullscreen
) {
581 int width
= MAX(real_screen
->w
+ (keycode
== 0x1b ? 50 : -50),
583 int height
= (surface_height(surface
) * width
) /
584 surface_width(surface
);
586 sdl_scale(width
, height
);
587 graphic_hw_invalidate(NULL
);
588 graphic_hw_update(NULL
);
594 } else if (!qemu_console_is_graphic(NULL
)) {
597 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
598 switch (ev
->key
.keysym
.sym
) {
600 keysym
= QEMU_KEY_CTRL_UP
;
603 keysym
= QEMU_KEY_CTRL_DOWN
;
606 keysym
= QEMU_KEY_CTRL_LEFT
;
609 keysym
= QEMU_KEY_CTRL_RIGHT
;
612 keysym
= QEMU_KEY_CTRL_HOME
;
615 keysym
= QEMU_KEY_CTRL_END
;
618 keysym
= QEMU_KEY_CTRL_PAGEUP
;
621 keysym
= QEMU_KEY_CTRL_PAGEDOWN
;
627 switch (ev
->key
.keysym
.sym
) {
629 keysym
= QEMU_KEY_UP
;
632 keysym
= QEMU_KEY_DOWN
;
635 keysym
= QEMU_KEY_LEFT
;
638 keysym
= QEMU_KEY_RIGHT
;
641 keysym
= QEMU_KEY_HOME
;
644 keysym
= QEMU_KEY_END
;
647 keysym
= QEMU_KEY_PAGEUP
;
650 keysym
= QEMU_KEY_PAGEDOWN
;
653 keysym
= QEMU_KEY_BACKSPACE
;
656 keysym
= QEMU_KEY_DELETE
;
663 kbd_put_keysym(keysym
);
664 } else if (ev
->key
.keysym
.unicode
!= 0) {
665 kbd_put_keysym(ev
->key
.keysym
.unicode
);
668 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
669 sdl_process_key(&ev
->key
);
673 static void handle_keyup(SDL_Event
*ev
)
678 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
680 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
682 if (!mod_state
&& gui_key_modifier_pressed
) {
683 gui_key_modifier_pressed
= 0;
684 if (gui_keysym
== 0) {
685 /* exit/enter grab if pressing Ctrl-Alt */
687 if (qemu_console_is_graphic(NULL
)) {
690 } else if (!gui_fullscreen
) {
693 /* SDL does not send back all the modifiers key, so we must
700 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
701 sdl_process_key(&ev
->key
);
705 static void handle_mousemotion(SDL_Event
*ev
)
709 if (qemu_console_is_graphic(NULL
) &&
710 (qemu_input_is_absolute() || absolute_enabled
)) {
711 max_x
= real_screen
->w
- 1;
712 max_y
= real_screen
->h
- 1;
713 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
714 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
718 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
719 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
723 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
724 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
,
725 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
729 static void handle_mousebutton(SDL_Event
*ev
)
731 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
732 SDL_MouseButtonEvent
*bev
;
734 if (!qemu_console_is_graphic(NULL
)) {
739 if (!gui_grab
&& !qemu_input_is_absolute()) {
740 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
741 /* start grabbing all events */
745 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
746 buttonstate
|= SDL_BUTTON(bev
->button
);
748 buttonstate
&= ~SDL_BUTTON(bev
->button
);
750 sdl_send_mouse_event(0, 0, bev
->x
, bev
->y
, buttonstate
);
754 static void handle_activation(SDL_Event
*ev
)
757 /* Disable grab if the window no longer has the focus
758 * (Windows-only workaround) */
759 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
760 !ev
->active
.gain
&& !gui_fullscreen
) {
764 if (!gui_grab
&& ev
->active
.gain
&& qemu_console_is_graphic(NULL
) &&
765 (qemu_input_is_absolute() || absolute_enabled
)) {
766 absolute_mouse_grab();
768 if (ev
->active
.state
& SDL_APPACTIVE
) {
769 if (ev
->active
.gain
) {
770 /* Back to default interval */
771 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
773 /* Sleeping interval. Not using the long default here as
774 * sdl_refresh does not only update the guest screen, but
775 * also checks for gui events. */
776 update_displaychangelistener(dcl
, 500);
781 static void sdl_refresh(DisplayChangeListener
*dcl
)
783 SDL_Event ev1
, *ev
= &ev1
;
785 if (last_vm_running
!= runstate_is_running()) {
786 last_vm_running
= runstate_is_running();
787 sdl_update_caption();
790 graphic_hw_update(NULL
);
791 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL
));
793 while (SDL_PollEvent(ev
)) {
795 case SDL_VIDEOEXPOSE
:
796 sdl_update(dcl
, 0, 0, real_screen
->w
, real_screen
->h
);
807 qemu_system_shutdown_request();
810 case SDL_MOUSEMOTION
:
811 handle_mousemotion(ev
);
813 case SDL_MOUSEBUTTONDOWN
:
814 case SDL_MOUSEBUTTONUP
:
815 handle_mousebutton(ev
);
817 case SDL_ACTIVEEVENT
:
818 handle_activation(ev
);
820 case SDL_VIDEORESIZE
:
821 sdl_scale(ev
->resize
.w
, ev
->resize
.h
);
822 graphic_hw_invalidate(NULL
);
823 graphic_hw_update(NULL
);
831 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
832 int x
, int y
, int on
)
837 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
838 SDL_SetCursor(guest_sprite
);
839 if (!qemu_input_is_absolute() && !absolute_enabled
) {
846 guest_x
= x
, guest_y
= y
;
849 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
852 uint8_t *image
, *mask
;
856 SDL_FreeCursor(guest_sprite
);
858 bpl
= cursor_get_mono_bpl(c
);
859 image
= g_malloc0(bpl
* c
->height
);
860 mask
= g_malloc0(bpl
* c
->height
);
861 cursor_get_mono_image(c
, 0x000000, image
);
862 cursor_get_mono_mask(c
, 0, mask
);
863 guest_sprite
= SDL_CreateCursor(image
, mask
, c
->width
, c
->height
,
869 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
870 SDL_SetCursor(guest_sprite
);
873 static void sdl_cleanup(void)
876 SDL_FreeCursor(guest_sprite
);
877 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
880 static const DisplayChangeListenerOps dcl_ops
= {
882 .dpy_gfx_update
= sdl_update
,
883 .dpy_gfx_switch
= sdl_switch
,
884 .dpy_gfx_check_format
= sdl_check_format
,
885 .dpy_refresh
= sdl_refresh
,
886 .dpy_mouse_set
= sdl_mouse_warp
,
887 .dpy_cursor_define
= sdl_mouse_define
,
890 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
894 const SDL_VideoInfo
*vi
;
897 #if defined(__APPLE__)
898 /* always use generic keymaps */
899 if (!keyboard_layout
)
900 keyboard_layout
= "en-us";
902 if(keyboard_layout
) {
903 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
912 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
915 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
916 * accessible $DISPLAY to open X11 window. This is often the case
917 * when qemu is run using sudo. But in this case, and when actually
918 * run in X11 environment, SDL fights with X11 for the video card,
919 * making current display unavailable, often until reboot.
920 * So make x11 the default SDL video driver if this variable is unset.
921 * This is a bit hackish but saves us from bigger problem.
922 * Maybe it's a good idea to fix this in SDL instead.
924 setenv("SDL_VIDEODRIVER", "x11", 0);
927 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
928 * This requires SDL >= 1.2.14. */
929 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
931 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
932 if (SDL_Init (flags
)) {
933 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
937 vi
= SDL_GetVideoInfo();
938 host_format
= *(vi
->vfmt
);
940 /* Load a 32x32x4 image. White pixels are transparent. */
941 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
943 SDL_Surface
*image
= SDL_LoadBMP(filename
);
945 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
946 SDL_SetColorKey(image
, SDL_SRCCOLORKEY
, colorkey
);
947 SDL_WM_SetIcon(image
, NULL
);
957 dcl
= g_malloc0(sizeof(DisplayChangeListener
));
959 register_displaychangelistener(dcl
);
961 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
962 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
964 sdl_update_caption();
965 SDL_EnableKeyRepeat(250, 50);
968 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
969 sdl_cursor_normal
= SDL_GetCursor();