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
28 #include "qemu/osdep.h"
30 #include <SDL_syswm.h>
32 #include "qemu-common.h"
33 #include "qemu/cutils.h"
34 #include "ui/console.h"
36 #include "sysemu/sysemu.h"
42 static DisplayChangeListener
*dcl
;
43 static DisplaySurface
*surface
;
44 static SDL_Surface
*real_screen
;
45 static SDL_Surface
*guest_screen
= NULL
;
46 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
47 static int last_vm_running
;
48 static bool gui_saved_scaling
;
49 static int gui_saved_width
;
50 static int gui_saved_height
;
51 static int gui_saved_grab
;
52 static int gui_fullscreen
;
53 static int gui_key_modifier_pressed
;
54 static int gui_keysym
;
55 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
56 static uint8_t modifiers_state
[256];
57 static SDL_Cursor
*sdl_cursor_normal
;
58 static SDL_Cursor
*sdl_cursor_hidden
;
59 static int absolute_enabled
= 0;
60 static int guest_cursor
= 0;
61 static int guest_x
, guest_y
;
62 static SDL_Cursor
*guest_sprite
= NULL
;
63 static SDL_PixelFormat host_format
;
64 static int scaling_active
= 0;
65 static Notifier mouse_mode_notifier
;
66 static int idle_counter
;
67 static const guint16
*keycode_map
;
68 static size_t keycode_maplen
;
70 #define SDL_REFRESH_INTERVAL_BUSY 10
71 #define SDL_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
72 / SDL_REFRESH_INTERVAL_BUSY + 1)
78 static void sdl_update(DisplayChangeListener
*dcl
,
79 int x
, int y
, int w
, int h
)
88 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
89 x
, y
, w
, h
, scaling_active
);
93 if (!scaling_active
) {
94 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
96 if (sdl_zoom_blit(guest_screen
, real_screen
, SMOOTHING_ON
, &rec
) < 0) {
97 fprintf(stderr
, "Zoom blit failed\n");
102 SDL_UpdateRect(real_screen
, rec
.x
, rec
.y
, rec
.w
, rec
.h
);
105 static void do_sdl_resize(int width
, int height
, int bpp
)
108 SDL_Surface
*tmp_screen
;
111 printf("SDL: Resizing to %dx%d bpp %d\n", width
, height
, bpp
);
114 flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
;
115 if (gui_fullscreen
) {
116 flags
|= SDL_FULLSCREEN
;
118 flags
|= SDL_RESIZABLE
;
121 flags
|= SDL_NOFRAME
;
124 tmp_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
127 fprintf(stderr
, "Could not open SDL display (%dx%dx%d): %s\n",
128 width
, height
, bpp
, SDL_GetError());
133 * Revert to the previous video mode if the change of resizing or
137 fprintf(stderr
, "Failed to set SDL display (%dx%dx%d): %s\n",
138 width
, height
, bpp
, SDL_GetError());
143 real_screen
= tmp_screen
;
146 static void sdl_switch(DisplayChangeListener
*dcl
,
147 DisplaySurface
*new_surface
)
151 /* temporary hack: allows to call sdl_switch to handle scaling changes */
153 surface
= new_surface
;
155 pf
= qemu_pixelformat_from_pixman(surface
->format
);
157 if (!scaling_active
) {
158 do_sdl_resize(surface_width(surface
), surface_height(surface
), 0);
159 } else if (real_screen
->format
->BitsPerPixel
!=
160 surface_bits_per_pixel(surface
)) {
161 do_sdl_resize(real_screen
->w
, real_screen
->h
,
162 surface_bits_per_pixel(surface
));
165 if (guest_screen
!= NULL
) {
166 SDL_FreeSurface(guest_screen
);
170 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
171 pf
.rmask
, pf
.gmask
, pf
.bmask
, pf
.amask
);
174 guest_screen
= SDL_CreateRGBSurfaceFrom
175 (surface_data(surface
),
176 surface_width(surface
), surface_height(surface
),
177 surface_bits_per_pixel(surface
), surface_stride(surface
),
182 static bool sdl_check_format(DisplayChangeListener
*dcl
,
183 pixman_format_code_t format
)
186 * We let SDL convert for us a few more formats than,
187 * the native ones. Thes are the ones I have tested.
189 return (format
== PIXMAN_x8r8g8b8
||
190 format
== PIXMAN_b8g8r8x8
||
191 format
== PIXMAN_x1r5g5b5
||
192 format
== PIXMAN_r5g6b5
);
195 /* generic keyboard conversion */
197 #include "sdl_keysym.h"
199 static kbd_layout_t
*kbd_layout
= NULL
;
201 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
204 /* workaround for X11+SDL bug with AltGR */
205 keysym
= ev
->keysym
.sym
;
206 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
208 /* For Japanese key '\' and '|' */
209 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
212 return keysym2scancode(kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
216 static const guint16
*sdl_get_keymap(size_t *maplen
)
219 *maplen
= qemu_input_map_atset1_to_qcode_len
;
220 return qemu_input_map_atset1_to_qcode
;
222 #if defined(SDL_VIDEO_DRIVER_X11)
225 SDL_VERSION(&info
.version
);
226 if (SDL_GetWMInfo(&info
) > 0) {
227 return qemu_xkeymap_mapping_table(
228 info
.info
.x11
.display
, maplen
);
231 g_warning("Unsupported SDL video driver / platform.\n"
232 "Assuming Linux KBD scancodes, but probably wrong.\n"
233 "Please report to qemu-devel@nongnu.org\n"
234 "including the following information:\n"
236 " - Operating system\n"
237 " - SDL video driver\n");
238 *maplen
= qemu_input_map_xorgkbd_to_qcode_len
;
239 return qemu_input_map_xorgkbd_to_qcode
;
243 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
248 if (ev
->keysym
.scancode
> keycode_maplen
) {
252 return keycode_map
[ev
->keysym
.scancode
];
255 static void reset_keys(void)
258 for(i
= 0; i
< 256; i
++) {
259 if (modifiers_state
[i
]) {
260 qemu_input_event_send_key_number(dcl
->con
, i
, false);
261 modifiers_state
[i
] = 0;
266 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
270 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
272 qemu_input_event_send_key_qcode(dcl
->con
, Q_KEY_CODE_PAUSE
,
273 ev
->type
== SDL_KEYDOWN
);
278 keycode
= sdl_keyevent_to_keycode_generic(ev
);
280 keycode
= sdl_keyevent_to_keycode(ev
);
285 /* sent when leaving window: reset the modifiers state */
288 case 0x2a: /* Left Shift */
289 case 0x36: /* Right Shift */
290 case 0x1d: /* Left CTRL */
291 case 0x9d: /* Right CTRL */
292 case 0x38: /* Left ALT */
293 case 0xb8: /* Right ALT */
294 if (ev
->type
== SDL_KEYUP
)
295 modifiers_state
[keycode
] = 0;
297 modifiers_state
[keycode
] = 1;
299 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
300 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
301 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
302 case 0x45: /* num lock */
303 case 0x3a: /* caps lock */
304 /* SDL does not send the key up event, so we generate it */
305 qemu_input_event_send_key_number(dcl
->con
, keycode
, true);
306 qemu_input_event_send_key_number(dcl
->con
, keycode
, false);
311 /* now send the key code */
312 qemu_input_event_send_key_number(dcl
->con
, keycode
,
313 ev
->type
== SDL_KEYDOWN
);
316 static void sdl_update_caption(void)
318 char win_title
[1024];
319 char icon_title
[1024];
320 const char *status
= "";
322 if (!runstate_is_running())
323 status
= " [Stopped]";
326 status
= " - Press Ctrl-Alt-Shift-G to exit mouse grab";
328 status
= " - Press Right-Ctrl-G to exit mouse grab";
330 status
= " - Press Ctrl-Alt-G to exit mouse grab";
334 snprintf(win_title
, sizeof(win_title
), "QEMU (%s)%s", qemu_name
, status
);
335 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
337 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
338 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
341 SDL_WM_SetCaption(win_title
, icon_title
);
344 static void sdl_hide_cursor(void)
349 if (qemu_input_is_absolute()) {
351 SDL_SetCursor(sdl_cursor_hidden
);
357 static void sdl_show_cursor(void)
362 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL
)) {
365 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
366 SDL_SetCursor(guest_sprite
);
368 SDL_SetCursor(sdl_cursor_normal
);
372 static void sdl_grab_start(void)
375 * If the application is not active, do not try to enter grab state. This
376 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
377 * application (SDL bug).
379 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS
)) {
383 SDL_SetCursor(guest_sprite
);
384 if (!qemu_input_is_absolute() && !absolute_enabled
) {
385 SDL_WarpMouse(guest_x
, guest_y
);
389 SDL_WM_GrabInput(SDL_GRAB_ON
);
391 sdl_update_caption();
394 static void sdl_grab_end(void)
396 SDL_WM_GrabInput(SDL_GRAB_OFF
);
399 sdl_update_caption();
402 static void absolute_mouse_grab(void)
404 int mouse_x
, mouse_y
;
406 SDL_GetMouseState(&mouse_x
, &mouse_y
);
407 if (mouse_x
> 0 && mouse_x
< real_screen
->w
- 1 &&
408 mouse_y
> 0 && mouse_y
< real_screen
->h
- 1) {
413 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
415 if (qemu_input_is_absolute()) {
416 if (!absolute_enabled
) {
417 absolute_enabled
= 1;
418 if (qemu_console_is_graphic(NULL
)) {
419 absolute_mouse_grab();
422 } else if (absolute_enabled
) {
423 if (!gui_fullscreen
) {
426 absolute_enabled
= 0;
430 static void sdl_send_mouse_event(int dx
, int dy
, int x
, int y
, int state
)
432 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
433 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
434 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
435 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
436 [INPUT_BUTTON_WHEEL_UP
] = SDL_BUTTON(SDL_BUTTON_WHEELUP
),
437 [INPUT_BUTTON_WHEEL_DOWN
] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN
),
439 static uint32_t prev_state
;
441 if (prev_state
!= state
) {
442 qemu_input_update_buttons(dcl
->con
, bmap
, prev_state
, state
);
446 if (qemu_input_is_absolute()) {
447 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_X
, x
,
449 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_Y
, y
,
460 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_X
, dx
);
461 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_Y
, dy
);
463 qemu_input_event_sync();
466 static void sdl_scale(int width
, int height
)
468 int bpp
= real_screen
->format
->BitsPerPixel
;
471 printf("SDL: Scaling to %dx%d bpp %d\n", width
, height
, bpp
);
474 if (bpp
!= 16 && bpp
!= 32) {
477 do_sdl_resize(width
, height
, bpp
);
481 static void toggle_full_screen(void)
483 int width
= surface_width(surface
);
484 int height
= surface_height(surface
);
485 int bpp
= surface_bits_per_pixel(surface
);
487 gui_fullscreen
= !gui_fullscreen
;
488 if (gui_fullscreen
) {
489 gui_saved_width
= real_screen
->w
;
490 gui_saved_height
= real_screen
->h
;
491 gui_saved_scaling
= scaling_active
;
493 do_sdl_resize(width
, height
, bpp
);
496 gui_saved_grab
= gui_grab
;
499 if (gui_saved_scaling
) {
500 sdl_scale(gui_saved_width
, gui_saved_height
);
502 do_sdl_resize(width
, height
, 0);
504 if (!gui_saved_grab
|| !qemu_console_is_graphic(NULL
)) {
508 graphic_hw_invalidate(NULL
);
509 graphic_hw_update(NULL
);
512 static void handle_keydown(SDL_Event
*ev
)
518 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
519 (gui_grab_code
| KMOD_LSHIFT
);
520 } else if (ctrl_grab
) {
521 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
523 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
525 gui_key_modifier_pressed
= mod_state
;
527 if (gui_key_modifier_pressed
) {
528 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
530 case 0x21: /* 'f' key on US keyboard */
531 toggle_full_screen();
534 case 0x22: /* 'g' key */
536 if (qemu_console_is_graphic(NULL
)) {
539 } else if (!gui_fullscreen
) {
544 case 0x16: /* 'u' key on US keyboard */
545 if (scaling_active
) {
547 sdl_switch(dcl
, NULL
);
548 graphic_hw_invalidate(NULL
);
549 graphic_hw_update(NULL
);
553 case 0x02 ... 0x0a: /* '1' to '9' keys */
554 /* Reset the modifiers sent to the current console */
556 console_select(keycode
- 0x02);
558 if (gui_fullscreen
) {
561 if (!qemu_console_is_graphic(NULL
)) {
562 /* release grab if going to a text console */
565 } else if (absolute_enabled
) {
568 } else if (absolute_enabled
) {
570 absolute_mouse_grab();
575 if (!gui_fullscreen
) {
576 int width
= MAX(real_screen
->w
+ (keycode
== 0x1b ? 50 : -50),
578 int height
= (surface_height(surface
) * width
) /
579 surface_width(surface
);
581 sdl_scale(width
, height
);
582 graphic_hw_invalidate(NULL
);
583 graphic_hw_update(NULL
);
589 } else if (!qemu_console_is_graphic(NULL
)) {
592 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
593 switch (ev
->key
.keysym
.sym
) {
595 keysym
= QEMU_KEY_CTRL_UP
;
598 keysym
= QEMU_KEY_CTRL_DOWN
;
601 keysym
= QEMU_KEY_CTRL_LEFT
;
604 keysym
= QEMU_KEY_CTRL_RIGHT
;
607 keysym
= QEMU_KEY_CTRL_HOME
;
610 keysym
= QEMU_KEY_CTRL_END
;
613 keysym
= QEMU_KEY_CTRL_PAGEUP
;
616 keysym
= QEMU_KEY_CTRL_PAGEDOWN
;
622 switch (ev
->key
.keysym
.sym
) {
624 keysym
= QEMU_KEY_UP
;
627 keysym
= QEMU_KEY_DOWN
;
630 keysym
= QEMU_KEY_LEFT
;
633 keysym
= QEMU_KEY_RIGHT
;
636 keysym
= QEMU_KEY_HOME
;
639 keysym
= QEMU_KEY_END
;
642 keysym
= QEMU_KEY_PAGEUP
;
645 keysym
= QEMU_KEY_PAGEDOWN
;
648 keysym
= QEMU_KEY_BACKSPACE
;
651 keysym
= QEMU_KEY_DELETE
;
658 kbd_put_keysym(keysym
);
659 } else if (ev
->key
.keysym
.unicode
!= 0) {
660 kbd_put_keysym(ev
->key
.keysym
.unicode
);
663 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
664 sdl_process_key(&ev
->key
);
668 static void handle_keyup(SDL_Event
*ev
)
673 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
675 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
677 if (!mod_state
&& gui_key_modifier_pressed
) {
678 gui_key_modifier_pressed
= 0;
681 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
682 sdl_process_key(&ev
->key
);
686 static void handle_mousemotion(SDL_Event
*ev
)
690 if (qemu_console_is_graphic(NULL
) &&
691 (qemu_input_is_absolute() || absolute_enabled
)) {
692 max_x
= real_screen
->w
- 1;
693 max_y
= real_screen
->h
- 1;
694 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
695 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
699 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
700 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
704 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
705 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
,
706 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
710 static void handle_mousebutton(SDL_Event
*ev
)
712 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
713 SDL_MouseButtonEvent
*bev
;
715 if (!qemu_console_is_graphic(NULL
)) {
720 if (!gui_grab
&& !qemu_input_is_absolute()) {
721 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
722 /* start grabbing all events */
726 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
727 buttonstate
|= SDL_BUTTON(bev
->button
);
729 buttonstate
&= ~SDL_BUTTON(bev
->button
);
731 sdl_send_mouse_event(0, 0, bev
->x
, bev
->y
, buttonstate
);
735 static void handle_activation(SDL_Event
*ev
)
738 /* Disable grab if the window no longer has the focus
739 * (Windows-only workaround) */
740 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
741 !ev
->active
.gain
&& !gui_fullscreen
) {
745 if (!gui_grab
&& ev
->active
.gain
&& qemu_console_is_graphic(NULL
) &&
746 (qemu_input_is_absolute() || absolute_enabled
)) {
747 absolute_mouse_grab();
749 if (ev
->active
.state
& SDL_APPACTIVE
) {
750 if (ev
->active
.gain
) {
751 /* Back to default interval */
752 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
754 /* Sleeping interval. Not using the long default here as
755 * sdl_refresh does not only update the guest screen, but
756 * also checks for gui events. */
757 update_displaychangelistener(dcl
, 500);
762 static void sdl_refresh(DisplayChangeListener
*dcl
)
764 SDL_Event ev1
, *ev
= &ev1
;
767 if (last_vm_running
!= runstate_is_running()) {
768 last_vm_running
= runstate_is_running();
769 sdl_update_caption();
772 graphic_hw_update(NULL
);
773 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL
));
775 while (SDL_PollEvent(ev
)) {
777 case SDL_VIDEOEXPOSE
:
778 sdl_update(dcl
, 0, 0, real_screen
->w
, real_screen
->h
);
791 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
794 case SDL_MOUSEMOTION
:
796 handle_mousemotion(ev
);
798 case SDL_MOUSEBUTTONDOWN
:
799 case SDL_MOUSEBUTTONUP
:
801 handle_mousebutton(ev
);
803 case SDL_ACTIVEEVENT
:
804 handle_activation(ev
);
806 case SDL_VIDEORESIZE
:
807 sdl_scale(ev
->resize
.w
, ev
->resize
.h
);
808 graphic_hw_invalidate(NULL
);
809 graphic_hw_update(NULL
);
817 if (idle_counter
< SDL_MAX_IDLE_COUNT
) {
819 if (idle_counter
>= SDL_MAX_IDLE_COUNT
) {
820 dcl
->update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
825 dcl
->update_interval
= SDL_REFRESH_INTERVAL_BUSY
;
829 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
830 int x
, int y
, int on
)
835 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
836 SDL_SetCursor(guest_sprite
);
837 if (!qemu_input_is_absolute() && !absolute_enabled
) {
844 guest_x
= x
, guest_y
= y
;
847 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
850 uint8_t *image
, *mask
;
854 SDL_FreeCursor(guest_sprite
);
856 bpl
= cursor_get_mono_bpl(c
);
857 image
= g_malloc0(bpl
* c
->height
);
858 mask
= g_malloc0(bpl
* c
->height
);
859 cursor_get_mono_image(c
, 0x000000, image
);
860 cursor_get_mono_mask(c
, 0, mask
);
861 guest_sprite
= SDL_CreateCursor(image
, mask
, c
->width
, c
->height
,
867 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
868 SDL_SetCursor(guest_sprite
);
871 static void sdl_cleanup(void)
874 SDL_FreeCursor(guest_sprite
);
875 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
878 static const DisplayChangeListenerOps dcl_ops
= {
880 .dpy_gfx_update
= sdl_update
,
881 .dpy_gfx_switch
= sdl_switch
,
882 .dpy_gfx_check_format
= sdl_check_format
,
883 .dpy_refresh
= sdl_refresh
,
884 .dpy_mouse_set
= sdl_mouse_warp
,
885 .dpy_cursor_define
= sdl_mouse_define
,
888 void sdl_display_early_init(int opengl
)
890 if (opengl
== 1 /* on */) {
892 "SDL1 display code has no opengl support.\n"
893 "Please recompile qemu with SDL2, using\n"
894 "./configure --enable-sdl --with-sdlabi=2.0\n");
898 void sdl_display_init(DisplayState
*ds
, int full_screen
)
902 const SDL_VideoInfo
*vi
;
906 #if defined(__APPLE__)
907 /* always use generic keymaps */
908 if (!keyboard_layout
)
909 keyboard_layout
= "en-us";
911 if(keyboard_layout
) {
912 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
917 g_printerr("Running QEMU with SDL 1.2 is deprecated, and will be removed\n"
918 "in a future release. Please switch to SDL 2.0 instead\n");
921 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
924 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
925 * accessible $DISPLAY to open X11 window. This is often the case
926 * when qemu is run using sudo. But in this case, and when actually
927 * run in X11 environment, SDL fights with X11 for the video card,
928 * making current display unavailable, often until reboot.
929 * So make x11 the default SDL video driver if this variable is unset.
930 * This is a bit hackish but saves us from bigger problem.
931 * Maybe it's a good idea to fix this in SDL instead.
933 setenv("SDL_VIDEODRIVER", "x11", 0);
936 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
937 * This requires SDL >= 1.2.14. */
938 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
940 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
941 if (SDL_Init (flags
)) {
942 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
946 vi
= SDL_GetVideoInfo();
947 host_format
= *(vi
->vfmt
);
949 keycode_map
= sdl_get_keymap(&keycode_maplen
);
951 /* Load a 32x32x4 image. White pixels are transparent. */
952 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
954 SDL_Surface
*image
= SDL_LoadBMP(filename
);
956 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
957 SDL_SetColorKey(image
, SDL_SRCCOLORKEY
, colorkey
);
958 SDL_WM_SetIcon(image
, NULL
);
968 dcl
= g_new0(DisplayChangeListener
, 1);
970 register_displaychangelistener(dcl
);
972 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
973 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
975 sdl_update_caption();
976 SDL_EnableKeyRepeat(250, 50);
979 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
980 sdl_cursor_normal
= SDL_GetCursor();
982 memset(&info
, 0, sizeof(info
));
983 SDL_VERSION(&info
.version
);
984 if (SDL_GetWMInfo(&info
)) {
987 /* All consoles share the same window */
988 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
990 #if defined(SDL_VIDEO_DRIVER_X11)
991 qemu_console_set_window_id(con
, info
.info
.x11
.wmwindow
);
992 #elif defined(SDL_VIDEO_DRIVER_NANOX) || \
993 defined(SDL_VIDEO_DRIVER_WINDIB) || defined(SDL_VIDEO_DRIVER_DDRAW) || \
994 defined(SDL_VIDEO_DRIVER_GAPI) || \
995 defined(SDL_VIDEO_DRIVER_RISCOS)
996 qemu_console_set_window_id(con
, (int) (uintptr_t) info
.window
);
998 qemu_console_set_window_id(con
, info
.data
);
1006 atexit(sdl_cleanup
);