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 /* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
26 #include "qemu/osdep.h"
27 #include "qemu/module.h"
28 #include "qemu/cutils.h"
29 #include "ui/console.h"
32 #include "sysemu/runstate.h"
33 #include "sysemu/sysemu.h"
34 #include "ui/win32-kbd-hook.h"
36 static int sdl2_num_outputs
;
37 static struct sdl2_console
*sdl2_console
;
39 static SDL_Surface
*guest_sprite_surface
;
40 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
42 static int gui_saved_grab
;
43 static int gui_fullscreen
;
44 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
45 static SDL_Cursor
*sdl_cursor_normal
;
46 static SDL_Cursor
*sdl_cursor_hidden
;
47 static int absolute_enabled
;
48 static int guest_cursor
;
49 static int guest_x
, guest_y
;
50 static SDL_Cursor
*guest_sprite
;
51 static Notifier mouse_mode_notifier
;
53 #define SDL2_REFRESH_INTERVAL_BUSY 10
54 #define SDL2_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
55 / SDL2_REFRESH_INTERVAL_BUSY + 1)
57 static void sdl_update_caption(struct sdl2_console
*scon
);
59 static struct sdl2_console
*get_scon_from_window(uint32_t window_id
)
62 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
63 if (sdl2_console
[i
].real_window
== SDL_GetWindowFromID(window_id
)) {
64 return &sdl2_console
[i
];
70 void sdl2_window_create(struct sdl2_console
*scon
)
77 assert(!scon
->real_window
);
80 flags
|= SDL_WINDOW_FULLSCREEN_DESKTOP
;
82 flags
|= SDL_WINDOW_RESIZABLE
;
85 flags
|= SDL_WINDOW_HIDDEN
;
89 flags
|= SDL_WINDOW_OPENGL
;
93 scon
->real_window
= SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED
,
94 SDL_WINDOWPOS_UNDEFINED
,
95 surface_width(scon
->surface
),
96 surface_height(scon
->surface
),
98 scon
->real_renderer
= SDL_CreateRenderer(scon
->real_window
, -1, 0);
100 scon
->winctx
= SDL_GL_GetCurrentContext();
102 sdl_update_caption(scon
);
105 void sdl2_window_destroy(struct sdl2_console
*scon
)
107 if (!scon
->real_window
) {
111 SDL_DestroyRenderer(scon
->real_renderer
);
112 scon
->real_renderer
= NULL
;
113 SDL_DestroyWindow(scon
->real_window
);
114 scon
->real_window
= NULL
;
117 void sdl2_window_resize(struct sdl2_console
*scon
)
119 if (!scon
->real_window
) {
123 SDL_SetWindowSize(scon
->real_window
,
124 surface_width(scon
->surface
),
125 surface_height(scon
->surface
));
128 static void sdl2_redraw(struct sdl2_console
*scon
)
132 sdl2_gl_redraw(scon
);
135 sdl2_2d_redraw(scon
);
139 static void sdl_update_caption(struct sdl2_console
*scon
)
141 char win_title
[1024];
142 char icon_title
[1024];
143 const char *status
= "";
145 if (!runstate_is_running()) {
146 status
= " [Stopped]";
147 } else if (gui_grab
) {
149 status
= " - Press Ctrl-Alt-Shift-G to exit grab";
150 } else if (ctrl_grab
) {
151 status
= " - Press Right-Ctrl-G to exit grab";
153 status
= " - Press Ctrl-Alt-G to exit grab";
158 snprintf(win_title
, sizeof(win_title
), "QEMU (%s-%d)%s", qemu_name
,
160 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
162 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
163 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
166 if (scon
->real_window
) {
167 SDL_SetWindowTitle(scon
->real_window
, win_title
);
171 static void sdl_hide_cursor(struct sdl2_console
*scon
)
173 if (scon
->opts
->has_show_cursor
&& scon
->opts
->show_cursor
) {
177 SDL_ShowCursor(SDL_DISABLE
);
178 SDL_SetCursor(sdl_cursor_hidden
);
180 if (!qemu_input_is_absolute()) {
181 SDL_SetRelativeMouseMode(SDL_TRUE
);
185 static void sdl_show_cursor(struct sdl2_console
*scon
)
187 if (scon
->opts
->has_show_cursor
&& scon
->opts
->show_cursor
) {
191 if (!qemu_input_is_absolute()) {
192 SDL_SetRelativeMouseMode(SDL_FALSE
);
196 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
197 SDL_SetCursor(guest_sprite
);
199 SDL_SetCursor(sdl_cursor_normal
);
202 SDL_ShowCursor(SDL_ENABLE
);
205 static void sdl_grab_start(struct sdl2_console
*scon
)
207 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
209 if (!con
|| !qemu_console_is_graphic(con
)) {
213 * If the application is not active, do not try to enter grab state. This
214 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
215 * application (SDL bug).
217 if (!(SDL_GetWindowFlags(scon
->real_window
) & SDL_WINDOW_INPUT_FOCUS
)) {
221 SDL_SetCursor(guest_sprite
);
222 if (!qemu_input_is_absolute() && !absolute_enabled
) {
223 SDL_WarpMouseInWindow(scon
->real_window
, guest_x
, guest_y
);
226 sdl_hide_cursor(scon
);
228 SDL_SetWindowGrab(scon
->real_window
, SDL_TRUE
);
230 win32_kbd_set_grab(true);
231 sdl_update_caption(scon
);
234 static void sdl_grab_end(struct sdl2_console
*scon
)
236 SDL_SetWindowGrab(scon
->real_window
, SDL_FALSE
);
238 win32_kbd_set_grab(false);
239 sdl_show_cursor(scon
);
240 sdl_update_caption(scon
);
243 static void absolute_mouse_grab(struct sdl2_console
*scon
)
245 int mouse_x
, mouse_y
;
247 SDL_GetMouseState(&mouse_x
, &mouse_y
);
248 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
249 if (mouse_x
> 0 && mouse_x
< scr_w
- 1 &&
250 mouse_y
> 0 && mouse_y
< scr_h
- 1) {
251 sdl_grab_start(scon
);
255 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
257 if (qemu_input_is_absolute()) {
258 if (!absolute_enabled
) {
259 absolute_enabled
= 1;
260 SDL_SetRelativeMouseMode(SDL_FALSE
);
261 absolute_mouse_grab(&sdl2_console
[0]);
263 } else if (absolute_enabled
) {
264 if (!gui_fullscreen
) {
265 sdl_grab_end(&sdl2_console
[0]);
267 absolute_enabled
= 0;
271 static void sdl_send_mouse_event(struct sdl2_console
*scon
, int dx
, int dy
,
272 int x
, int y
, int state
)
274 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
275 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
276 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
277 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
278 [INPUT_BUTTON_SIDE
] = SDL_BUTTON(SDL_BUTTON_X1
),
279 [INPUT_BUTTON_EXTRA
] = SDL_BUTTON(SDL_BUTTON_X2
)
281 static uint32_t prev_state
;
283 if (prev_state
!= state
) {
284 qemu_input_update_buttons(scon
->dcl
.con
, bmap
, prev_state
, state
);
288 if (qemu_input_is_absolute()) {
289 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_X
,
290 x
, 0, surface_width(scon
->surface
));
291 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_Y
,
292 y
, 0, surface_height(scon
->surface
));
302 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_X
, dx
);
303 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_Y
, dy
);
305 qemu_input_event_sync();
308 static void toggle_full_screen(struct sdl2_console
*scon
)
310 gui_fullscreen
= !gui_fullscreen
;
311 if (gui_fullscreen
) {
312 SDL_SetWindowFullscreen(scon
->real_window
,
313 SDL_WINDOW_FULLSCREEN_DESKTOP
);
314 gui_saved_grab
= gui_grab
;
315 sdl_grab_start(scon
);
317 if (!gui_saved_grab
) {
320 SDL_SetWindowFullscreen(scon
->real_window
, 0);
325 static int get_mod_state(void)
327 SDL_Keymod mod
= SDL_GetModState();
330 return (mod
& (gui_grab_code
| KMOD_LSHIFT
)) ==
331 (gui_grab_code
| KMOD_LSHIFT
);
332 } else if (ctrl_grab
) {
333 return (mod
& KMOD_RCTRL
) == KMOD_RCTRL
;
335 return (mod
& gui_grab_code
) == gui_grab_code
;
339 static void *sdl2_win32_get_hwnd(struct sdl2_console
*scon
)
344 SDL_VERSION(&info
.version
);
345 if (SDL_GetWindowWMInfo(scon
->real_window
, &info
)) {
346 return info
.info
.win
.window
;
352 static void handle_keydown(SDL_Event
*ev
)
355 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
356 int gui_key_modifier_pressed
= get_mod_state();
363 if (!scon
->ignore_hotkeys
&& gui_key_modifier_pressed
&& !ev
->key
.repeat
) {
364 switch (ev
->key
.keysym
.scancode
) {
377 win
= ev
->key
.keysym
.scancode
- SDL_SCANCODE_1
;
378 if (win
< sdl2_num_outputs
) {
379 sdl2_console
[win
].hidden
= !sdl2_console
[win
].hidden
;
380 if (sdl2_console
[win
].real_window
) {
381 if (sdl2_console
[win
].hidden
) {
382 SDL_HideWindow(sdl2_console
[win
].real_window
);
384 SDL_ShowWindow(sdl2_console
[win
].real_window
);
391 toggle_full_screen(scon
);
397 sdl_grab_start(scon
);
398 } else if (!gui_fullscreen
) {
403 sdl2_window_resize(scon
);
405 /* re-create scon->texture */
406 sdl2_2d_switch(&scon
->dcl
, scon
->surface
);
411 case SDL_SCANCODE_KP_PLUS
:
412 case SDL_SCANCODE_KP_MINUS
:
413 if (!gui_fullscreen
) {
416 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
418 width
= MAX(scr_w
+ (ev
->key
.keysym
.scancode
==
419 SDL_SCANCODE_KP_PLUS
? 50 : -50),
421 height
= (surface_height(scon
->surface
) * width
) /
422 surface_width(scon
->surface
);
423 fprintf(stderr
, "%s: scale to %dx%d\n",
424 __func__
, width
, height
);
425 sdl_scale(scon
, width
, height
);
435 sdl2_process_key(scon
, &ev
->key
);
439 static void handle_keyup(SDL_Event
*ev
)
441 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
447 scon
->ignore_hotkeys
= false;
448 sdl2_process_key(scon
, &ev
->key
);
451 static void handle_textinput(SDL_Event
*ev
)
453 struct sdl2_console
*scon
= get_scon_from_window(ev
->text
.windowID
);
454 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
460 if (qemu_console_is_graphic(con
)) {
463 kbd_put_string_console(con
, ev
->text
.text
, strlen(ev
->text
.text
));
466 static void handle_mousemotion(SDL_Event
*ev
)
469 struct sdl2_console
*scon
= get_scon_from_window(ev
->motion
.windowID
);
471 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
475 if (qemu_input_is_absolute() || absolute_enabled
) {
477 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
480 if (gui_grab
&& !gui_fullscreen
481 && (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
482 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
486 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
487 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
488 sdl_grab_start(scon
);
491 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
492 sdl_send_mouse_event(scon
, ev
->motion
.xrel
, ev
->motion
.yrel
,
493 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
497 static void handle_mousebutton(SDL_Event
*ev
)
499 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
500 SDL_MouseButtonEvent
*bev
;
501 struct sdl2_console
*scon
= get_scon_from_window(ev
->button
.windowID
);
503 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
508 if (!gui_grab
&& !qemu_input_is_absolute()) {
509 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
510 /* start grabbing all events */
511 sdl_grab_start(scon
);
514 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
515 buttonstate
|= SDL_BUTTON(bev
->button
);
517 buttonstate
&= ~SDL_BUTTON(bev
->button
);
519 sdl_send_mouse_event(scon
, 0, 0, bev
->x
, bev
->y
, buttonstate
);
523 static void handle_mousewheel(SDL_Event
*ev
)
525 struct sdl2_console
*scon
= get_scon_from_window(ev
->wheel
.windowID
);
526 SDL_MouseWheelEvent
*wev
= &ev
->wheel
;
529 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
534 btn
= INPUT_BUTTON_WHEEL_UP
;
535 } else if (wev
->y
< 0) {
536 btn
= INPUT_BUTTON_WHEEL_DOWN
;
541 qemu_input_queue_btn(scon
->dcl
.con
, btn
, true);
542 qemu_input_event_sync();
543 qemu_input_queue_btn(scon
->dcl
.con
, btn
, false);
544 qemu_input_event_sync();
547 static void handle_windowevent(SDL_Event
*ev
)
549 struct sdl2_console
*scon
= get_scon_from_window(ev
->window
.windowID
);
550 bool allow_close
= true;
556 switch (ev
->window
.event
) {
557 case SDL_WINDOWEVENT_RESIZED
:
560 memset(&info
, 0, sizeof(info
));
561 info
.width
= ev
->window
.data1
;
562 info
.height
= ev
->window
.data2
;
563 dpy_set_ui_info(scon
->dcl
.con
, &info
);
567 case SDL_WINDOWEVENT_EXPOSED
:
570 case SDL_WINDOWEVENT_FOCUS_GAINED
:
571 win32_kbd_set_grab(gui_grab
);
572 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
573 win32_kbd_set_window(sdl2_win32_get_hwnd(scon
));
576 case SDL_WINDOWEVENT_ENTER
:
577 if (!gui_grab
&& (qemu_input_is_absolute() || absolute_enabled
)) {
578 absolute_mouse_grab(scon
);
580 /* If a new console window opened using a hotkey receives the
581 * focus, SDL sends another KEYDOWN event to the new window,
582 * closing the console window immediately after.
584 * Work around this by ignoring further hotkey events until a
587 scon
->ignore_hotkeys
= get_mod_state();
589 case SDL_WINDOWEVENT_FOCUS_LOST
:
590 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
591 win32_kbd_set_window(NULL
);
593 if (gui_grab
&& !gui_fullscreen
) {
597 case SDL_WINDOWEVENT_RESTORED
:
598 update_displaychangelistener(&scon
->dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
600 case SDL_WINDOWEVENT_MINIMIZED
:
601 update_displaychangelistener(&scon
->dcl
, 500);
603 case SDL_WINDOWEVENT_CLOSE
:
604 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
605 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
610 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
613 SDL_HideWindow(scon
->real_window
);
617 case SDL_WINDOWEVENT_SHOWN
:
618 scon
->hidden
= false;
620 case SDL_WINDOWEVENT_HIDDEN
:
626 void sdl2_poll_events(struct sdl2_console
*scon
)
628 SDL_Event ev1
, *ev
= &ev1
;
629 bool allow_close
= true;
632 if (scon
->last_vm_running
!= runstate_is_running()) {
633 scon
->last_vm_running
= runstate_is_running();
634 sdl_update_caption(scon
);
637 while (SDL_PollEvent(ev
)) {
649 handle_textinput(ev
);
652 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
657 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
660 case SDL_MOUSEMOTION
:
662 handle_mousemotion(ev
);
664 case SDL_MOUSEBUTTONDOWN
:
665 case SDL_MOUSEBUTTONUP
:
667 handle_mousebutton(ev
);
671 handle_mousewheel(ev
);
673 case SDL_WINDOWEVENT
:
674 handle_windowevent(ev
);
682 if (scon
->idle_counter
< SDL2_MAX_IDLE_COUNT
) {
683 scon
->idle_counter
++;
684 if (scon
->idle_counter
>= SDL2_MAX_IDLE_COUNT
) {
685 scon
->dcl
.update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
689 scon
->idle_counter
= 0;
690 scon
->dcl
.update_interval
= SDL2_REFRESH_INTERVAL_BUSY
;
694 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
695 int x
, int y
, int on
)
697 struct sdl2_console
*scon
= container_of(dcl
, struct sdl2_console
, dcl
);
699 if (!qemu_console_is_graphic(scon
->dcl
.con
)) {
705 sdl_show_cursor(scon
);
707 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
708 SDL_SetCursor(guest_sprite
);
709 if (!qemu_input_is_absolute() && !absolute_enabled
) {
710 SDL_WarpMouseInWindow(scon
->real_window
, x
, y
);
713 } else if (gui_grab
) {
714 sdl_hide_cursor(scon
);
717 guest_x
= x
, guest_y
= y
;
720 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
725 SDL_FreeCursor(guest_sprite
);
728 if (guest_sprite_surface
) {
729 SDL_FreeSurface(guest_sprite_surface
);
732 guest_sprite_surface
=
733 SDL_CreateRGBSurfaceFrom(c
->data
, c
->width
, c
->height
, 32, c
->width
* 4,
734 0xff0000, 0x00ff00, 0xff, 0xff000000);
736 if (!guest_sprite_surface
) {
737 fprintf(stderr
, "Failed to make rgb surface from %p\n", c
);
740 guest_sprite
= SDL_CreateColorCursor(guest_sprite_surface
,
743 fprintf(stderr
, "Failed to make color cursor from %p\n", c
);
747 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
748 SDL_SetCursor(guest_sprite
);
752 static void sdl_cleanup(void)
755 SDL_FreeCursor(guest_sprite
);
757 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
760 static const DisplayChangeListenerOps dcl_2d_ops
= {
761 .dpy_name
= "sdl2-2d",
762 .dpy_gfx_update
= sdl2_2d_update
,
763 .dpy_gfx_switch
= sdl2_2d_switch
,
764 .dpy_gfx_check_format
= sdl2_2d_check_format
,
765 .dpy_refresh
= sdl2_2d_refresh
,
766 .dpy_mouse_set
= sdl_mouse_warp
,
767 .dpy_cursor_define
= sdl_mouse_define
,
771 static const DisplayChangeListenerOps dcl_gl_ops
= {
772 .dpy_name
= "sdl2-gl",
773 .dpy_gfx_update
= sdl2_gl_update
,
774 .dpy_gfx_switch
= sdl2_gl_switch
,
775 .dpy_gfx_check_format
= console_gl_check_format
,
776 .dpy_refresh
= sdl2_gl_refresh
,
777 .dpy_mouse_set
= sdl_mouse_warp
,
778 .dpy_cursor_define
= sdl_mouse_define
,
780 .dpy_gl_ctx_create
= sdl2_gl_create_context
,
781 .dpy_gl_ctx_destroy
= sdl2_gl_destroy_context
,
782 .dpy_gl_ctx_make_current
= sdl2_gl_make_context_current
,
783 .dpy_gl_ctx_get_current
= sdl2_gl_get_current_context
,
784 .dpy_gl_scanout_disable
= sdl2_gl_scanout_disable
,
785 .dpy_gl_scanout_texture
= sdl2_gl_scanout_texture
,
786 .dpy_gl_update
= sdl2_gl_scanout_flush
,
790 static void sdl2_display_early_init(DisplayOptions
*o
)
792 assert(o
->type
== DISPLAY_TYPE_SDL
);
793 if (o
->has_gl
&& o
->gl
) {
800 static void sdl2_display_init(DisplayState
*ds
, DisplayOptions
*o
)
805 SDL_Surface
*icon
= NULL
;
808 assert(o
->type
== DISPLAY_TYPE_SDL
);
811 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
812 * accessible $DISPLAY to open X11 window. This is often the case
813 * when qemu is run using sudo. But in this case, and when actually
814 * run in X11 environment, SDL fights with X11 for the video card,
815 * making current display unavailable, often until reboot.
816 * So make x11 the default SDL video driver if this variable is unset.
817 * This is a bit hackish but saves us from bigger problem.
818 * Maybe it's a good idea to fix this in SDL instead.
820 g_setenv("SDL_VIDEODRIVER", "x11", 0);
823 if (SDL_Init(SDL_INIT_VIDEO
)) {
824 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
828 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
829 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
, "0");
831 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD
, "1");
832 memset(&info
, 0, sizeof(info
));
833 SDL_VERSION(&info
.version
);
835 gui_fullscreen
= o
->has_full_screen
&& o
->full_screen
;
838 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
843 sdl2_num_outputs
= i
;
844 if (sdl2_num_outputs
== 0) {
847 sdl2_console
= g_new0(struct sdl2_console
, sdl2_num_outputs
);
848 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
849 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
851 if (!qemu_console_is_graphic(con
) &&
852 qemu_console_get_index(con
) != 0) {
853 sdl2_console
[i
].hidden
= true;
855 sdl2_console
[i
].idx
= i
;
856 sdl2_console
[i
].opts
= o
;
858 sdl2_console
[i
].opengl
= display_opengl
;
859 sdl2_console
[i
].dcl
.ops
= display_opengl
? &dcl_gl_ops
: &dcl_2d_ops
;
861 sdl2_console
[i
].opengl
= 0;
862 sdl2_console
[i
].dcl
.ops
= &dcl_2d_ops
;
864 sdl2_console
[i
].dcl
.con
= con
;
865 sdl2_console
[i
].kbd
= qkbd_state_init(con
);
866 register_displaychangelistener(&sdl2_console
[i
].dcl
);
868 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
869 if (SDL_GetWindowWMInfo(sdl2_console
[i
].real_window
, &info
)) {
870 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
871 qemu_console_set_window_id(con
, (uintptr_t)info
.info
.win
.window
);
872 #elif defined(SDL_VIDEO_DRIVER_X11)
873 qemu_console_set_window_id(con
, info
.info
.x11
.window
);
879 #ifdef CONFIG_SDL_IMAGE
880 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/128x128/apps/qemu.png");
881 icon
= IMG_Load(dir
);
883 /* Load a 32x32x4 image. White pixels are transparent. */
884 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/32x32/apps/qemu.bmp");
885 icon
= SDL_LoadBMP(dir
);
887 uint32_t colorkey
= SDL_MapRGB(icon
->format
, 255, 255, 255);
888 SDL_SetColorKey(icon
, SDL_TRUE
, colorkey
);
893 SDL_SetWindowIcon(sdl2_console
[0].real_window
, icon
);
896 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
897 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
899 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
900 sdl_cursor_normal
= SDL_GetCursor();
902 if (gui_fullscreen
) {
903 sdl_grab_start(&sdl2_console
[0]);
909 static QemuDisplay qemu_display_sdl2
= {
910 .type
= DISPLAY_TYPE_SDL
,
911 .early_init
= sdl2_display_early_init
,
912 .init
= sdl2_display_init
,
915 static void register_sdl1(void)
917 qemu_display_register(&qemu_display_sdl2
);
920 type_init(register_sdl1
);