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
),
279 static uint32_t prev_state
;
281 if (prev_state
!= state
) {
282 qemu_input_update_buttons(scon
->dcl
.con
, bmap
, prev_state
, state
);
286 if (qemu_input_is_absolute()) {
287 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_X
,
288 x
, 0, surface_width(scon
->surface
));
289 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_Y
,
290 y
, 0, surface_height(scon
->surface
));
300 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_X
, dx
);
301 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_Y
, dy
);
303 qemu_input_event_sync();
306 static void toggle_full_screen(struct sdl2_console
*scon
)
308 gui_fullscreen
= !gui_fullscreen
;
309 if (gui_fullscreen
) {
310 SDL_SetWindowFullscreen(scon
->real_window
,
311 SDL_WINDOW_FULLSCREEN_DESKTOP
);
312 gui_saved_grab
= gui_grab
;
313 sdl_grab_start(scon
);
315 if (!gui_saved_grab
) {
318 SDL_SetWindowFullscreen(scon
->real_window
, 0);
323 static int get_mod_state(void)
325 SDL_Keymod mod
= SDL_GetModState();
328 return (mod
& (gui_grab_code
| KMOD_LSHIFT
)) ==
329 (gui_grab_code
| KMOD_LSHIFT
);
330 } else if (ctrl_grab
) {
331 return (mod
& KMOD_RCTRL
) == KMOD_RCTRL
;
333 return (mod
& gui_grab_code
) == gui_grab_code
;
337 static void *sdl2_win32_get_hwnd(struct sdl2_console
*scon
)
342 SDL_VERSION(&info
.version
);
343 if (SDL_GetWindowWMInfo(scon
->real_window
, &info
)) {
344 return info
.info
.win
.window
;
350 static void handle_keydown(SDL_Event
*ev
)
353 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
354 int gui_key_modifier_pressed
= get_mod_state();
361 if (!scon
->ignore_hotkeys
&& gui_key_modifier_pressed
&& !ev
->key
.repeat
) {
362 switch (ev
->key
.keysym
.scancode
) {
375 win
= ev
->key
.keysym
.scancode
- SDL_SCANCODE_1
;
376 if (win
< sdl2_num_outputs
) {
377 sdl2_console
[win
].hidden
= !sdl2_console
[win
].hidden
;
378 if (sdl2_console
[win
].real_window
) {
379 if (sdl2_console
[win
].hidden
) {
380 SDL_HideWindow(sdl2_console
[win
].real_window
);
382 SDL_ShowWindow(sdl2_console
[win
].real_window
);
389 toggle_full_screen(scon
);
395 sdl_grab_start(scon
);
396 } else if (!gui_fullscreen
) {
401 sdl2_window_resize(scon
);
403 /* re-create scon->texture */
404 sdl2_2d_switch(&scon
->dcl
, scon
->surface
);
409 case SDL_SCANCODE_KP_PLUS
:
410 case SDL_SCANCODE_KP_MINUS
:
411 if (!gui_fullscreen
) {
414 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
416 width
= MAX(scr_w
+ (ev
->key
.keysym
.scancode
==
417 SDL_SCANCODE_KP_PLUS
? 50 : -50),
419 height
= (surface_height(scon
->surface
) * width
) /
420 surface_width(scon
->surface
);
421 fprintf(stderr
, "%s: scale to %dx%d\n",
422 __func__
, width
, height
);
423 sdl_scale(scon
, width
, height
);
433 sdl2_process_key(scon
, &ev
->key
);
437 static void handle_keyup(SDL_Event
*ev
)
439 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
445 scon
->ignore_hotkeys
= false;
446 sdl2_process_key(scon
, &ev
->key
);
449 static void handle_textinput(SDL_Event
*ev
)
451 struct sdl2_console
*scon
= get_scon_from_window(ev
->text
.windowID
);
452 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
458 if (qemu_console_is_graphic(con
)) {
461 kbd_put_string_console(con
, ev
->text
.text
, strlen(ev
->text
.text
));
464 static void handle_mousemotion(SDL_Event
*ev
)
467 struct sdl2_console
*scon
= get_scon_from_window(ev
->motion
.windowID
);
469 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
473 if (qemu_input_is_absolute() || absolute_enabled
) {
475 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
478 if (gui_grab
&& !gui_fullscreen
479 && (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
480 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
484 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
485 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
486 sdl_grab_start(scon
);
489 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
490 sdl_send_mouse_event(scon
, ev
->motion
.xrel
, ev
->motion
.yrel
,
491 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
495 static void handle_mousebutton(SDL_Event
*ev
)
497 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
498 SDL_MouseButtonEvent
*bev
;
499 struct sdl2_console
*scon
= get_scon_from_window(ev
->button
.windowID
);
501 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
506 if (!gui_grab
&& !qemu_input_is_absolute()) {
507 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
508 /* start grabbing all events */
509 sdl_grab_start(scon
);
512 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
513 buttonstate
|= SDL_BUTTON(bev
->button
);
515 buttonstate
&= ~SDL_BUTTON(bev
->button
);
517 sdl_send_mouse_event(scon
, 0, 0, bev
->x
, bev
->y
, buttonstate
);
521 static void handle_mousewheel(SDL_Event
*ev
)
523 struct sdl2_console
*scon
= get_scon_from_window(ev
->wheel
.windowID
);
524 SDL_MouseWheelEvent
*wev
= &ev
->wheel
;
527 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
532 btn
= INPUT_BUTTON_WHEEL_UP
;
533 } else if (wev
->y
< 0) {
534 btn
= INPUT_BUTTON_WHEEL_DOWN
;
539 qemu_input_queue_btn(scon
->dcl
.con
, btn
, true);
540 qemu_input_event_sync();
541 qemu_input_queue_btn(scon
->dcl
.con
, btn
, false);
542 qemu_input_event_sync();
545 static void handle_windowevent(SDL_Event
*ev
)
547 struct sdl2_console
*scon
= get_scon_from_window(ev
->window
.windowID
);
548 bool allow_close
= true;
554 switch (ev
->window
.event
) {
555 case SDL_WINDOWEVENT_RESIZED
:
558 memset(&info
, 0, sizeof(info
));
559 info
.width
= ev
->window
.data1
;
560 info
.height
= ev
->window
.data2
;
561 dpy_set_ui_info(scon
->dcl
.con
, &info
);
565 case SDL_WINDOWEVENT_EXPOSED
:
568 case SDL_WINDOWEVENT_FOCUS_GAINED
:
569 win32_kbd_set_grab(gui_grab
);
570 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
571 win32_kbd_set_window(sdl2_win32_get_hwnd(scon
));
574 case SDL_WINDOWEVENT_ENTER
:
575 if (!gui_grab
&& (qemu_input_is_absolute() || absolute_enabled
)) {
576 absolute_mouse_grab(scon
);
578 /* If a new console window opened using a hotkey receives the
579 * focus, SDL sends another KEYDOWN event to the new window,
580 * closing the console window immediately after.
582 * Work around this by ignoring further hotkey events until a
585 scon
->ignore_hotkeys
= get_mod_state();
587 case SDL_WINDOWEVENT_FOCUS_LOST
:
588 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
589 win32_kbd_set_window(NULL
);
591 if (gui_grab
&& !gui_fullscreen
) {
595 case SDL_WINDOWEVENT_RESTORED
:
596 update_displaychangelistener(&scon
->dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
598 case SDL_WINDOWEVENT_MINIMIZED
:
599 update_displaychangelistener(&scon
->dcl
, 500);
601 case SDL_WINDOWEVENT_CLOSE
:
602 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
603 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
608 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
611 SDL_HideWindow(scon
->real_window
);
615 case SDL_WINDOWEVENT_SHOWN
:
616 scon
->hidden
= false;
618 case SDL_WINDOWEVENT_HIDDEN
:
624 void sdl2_poll_events(struct sdl2_console
*scon
)
626 SDL_Event ev1
, *ev
= &ev1
;
627 bool allow_close
= true;
630 if (scon
->last_vm_running
!= runstate_is_running()) {
631 scon
->last_vm_running
= runstate_is_running();
632 sdl_update_caption(scon
);
635 while (SDL_PollEvent(ev
)) {
647 handle_textinput(ev
);
650 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
655 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
658 case SDL_MOUSEMOTION
:
660 handle_mousemotion(ev
);
662 case SDL_MOUSEBUTTONDOWN
:
663 case SDL_MOUSEBUTTONUP
:
665 handle_mousebutton(ev
);
669 handle_mousewheel(ev
);
671 case SDL_WINDOWEVENT
:
672 handle_windowevent(ev
);
680 if (scon
->idle_counter
< SDL2_MAX_IDLE_COUNT
) {
681 scon
->idle_counter
++;
682 if (scon
->idle_counter
>= SDL2_MAX_IDLE_COUNT
) {
683 scon
->dcl
.update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
687 scon
->idle_counter
= 0;
688 scon
->dcl
.update_interval
= SDL2_REFRESH_INTERVAL_BUSY
;
692 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
693 int x
, int y
, int on
)
695 struct sdl2_console
*scon
= container_of(dcl
, struct sdl2_console
, dcl
);
697 if (!qemu_console_is_graphic(scon
->dcl
.con
)) {
703 sdl_show_cursor(scon
);
705 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
706 SDL_SetCursor(guest_sprite
);
707 if (!qemu_input_is_absolute() && !absolute_enabled
) {
708 SDL_WarpMouseInWindow(scon
->real_window
, x
, y
);
711 } else if (gui_grab
) {
712 sdl_hide_cursor(scon
);
715 guest_x
= x
, guest_y
= y
;
718 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
723 SDL_FreeCursor(guest_sprite
);
726 if (guest_sprite_surface
) {
727 SDL_FreeSurface(guest_sprite_surface
);
730 guest_sprite_surface
=
731 SDL_CreateRGBSurfaceFrom(c
->data
, c
->width
, c
->height
, 32, c
->width
* 4,
732 0xff0000, 0x00ff00, 0xff, 0xff000000);
734 if (!guest_sprite_surface
) {
735 fprintf(stderr
, "Failed to make rgb surface from %p\n", c
);
738 guest_sprite
= SDL_CreateColorCursor(guest_sprite_surface
,
741 fprintf(stderr
, "Failed to make color cursor from %p\n", c
);
745 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
746 SDL_SetCursor(guest_sprite
);
750 static void sdl_cleanup(void)
753 SDL_FreeCursor(guest_sprite
);
755 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
758 static const DisplayChangeListenerOps dcl_2d_ops
= {
759 .dpy_name
= "sdl2-2d",
760 .dpy_gfx_update
= sdl2_2d_update
,
761 .dpy_gfx_switch
= sdl2_2d_switch
,
762 .dpy_gfx_check_format
= sdl2_2d_check_format
,
763 .dpy_refresh
= sdl2_2d_refresh
,
764 .dpy_mouse_set
= sdl_mouse_warp
,
765 .dpy_cursor_define
= sdl_mouse_define
,
769 static const DisplayChangeListenerOps dcl_gl_ops
= {
770 .dpy_name
= "sdl2-gl",
771 .dpy_gfx_update
= sdl2_gl_update
,
772 .dpy_gfx_switch
= sdl2_gl_switch
,
773 .dpy_gfx_check_format
= console_gl_check_format
,
774 .dpy_refresh
= sdl2_gl_refresh
,
775 .dpy_mouse_set
= sdl_mouse_warp
,
776 .dpy_cursor_define
= sdl_mouse_define
,
778 .dpy_gl_ctx_create
= sdl2_gl_create_context
,
779 .dpy_gl_ctx_destroy
= sdl2_gl_destroy_context
,
780 .dpy_gl_ctx_make_current
= sdl2_gl_make_context_current
,
781 .dpy_gl_ctx_get_current
= sdl2_gl_get_current_context
,
782 .dpy_gl_scanout_disable
= sdl2_gl_scanout_disable
,
783 .dpy_gl_scanout_texture
= sdl2_gl_scanout_texture
,
784 .dpy_gl_update
= sdl2_gl_scanout_flush
,
788 static void sdl2_display_early_init(DisplayOptions
*o
)
790 assert(o
->type
== DISPLAY_TYPE_SDL
);
791 if (o
->has_gl
&& o
->gl
) {
798 static void sdl2_display_init(DisplayState
*ds
, DisplayOptions
*o
)
803 SDL_Surface
*icon
= NULL
;
806 assert(o
->type
== DISPLAY_TYPE_SDL
);
809 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
810 * accessible $DISPLAY to open X11 window. This is often the case
811 * when qemu is run using sudo. But in this case, and when actually
812 * run in X11 environment, SDL fights with X11 for the video card,
813 * making current display unavailable, often until reboot.
814 * So make x11 the default SDL video driver if this variable is unset.
815 * This is a bit hackish but saves us from bigger problem.
816 * Maybe it's a good idea to fix this in SDL instead.
818 g_setenv("SDL_VIDEODRIVER", "x11", 0);
821 if (SDL_Init(SDL_INIT_VIDEO
)) {
822 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
826 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
827 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
, "0");
829 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD
, "1");
830 memset(&info
, 0, sizeof(info
));
831 SDL_VERSION(&info
.version
);
833 gui_fullscreen
= o
->has_full_screen
&& o
->full_screen
;
836 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
841 sdl2_num_outputs
= i
;
842 if (sdl2_num_outputs
== 0) {
845 sdl2_console
= g_new0(struct sdl2_console
, sdl2_num_outputs
);
846 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
847 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
849 if (!qemu_console_is_graphic(con
) &&
850 qemu_console_get_index(con
) != 0) {
851 sdl2_console
[i
].hidden
= true;
853 sdl2_console
[i
].idx
= i
;
854 sdl2_console
[i
].opts
= o
;
856 sdl2_console
[i
].opengl
= display_opengl
;
857 sdl2_console
[i
].dcl
.ops
= display_opengl
? &dcl_gl_ops
: &dcl_2d_ops
;
859 sdl2_console
[i
].opengl
= 0;
860 sdl2_console
[i
].dcl
.ops
= &dcl_2d_ops
;
862 sdl2_console
[i
].dcl
.con
= con
;
863 sdl2_console
[i
].kbd
= qkbd_state_init(con
);
864 register_displaychangelistener(&sdl2_console
[i
].dcl
);
866 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
867 if (SDL_GetWindowWMInfo(sdl2_console
[i
].real_window
, &info
)) {
868 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
869 qemu_console_set_window_id(con
, (uintptr_t)info
.info
.win
.window
);
870 #elif defined(SDL_VIDEO_DRIVER_X11)
871 qemu_console_set_window_id(con
, info
.info
.x11
.window
);
877 #ifdef CONFIG_SDL_IMAGE
878 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/128x128/apps/qemu.png");
879 icon
= IMG_Load(dir
);
881 /* Load a 32x32x4 image. White pixels are transparent. */
882 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/32x32/apps/qemu.bmp");
883 icon
= SDL_LoadBMP(dir
);
885 uint32_t colorkey
= SDL_MapRGB(icon
->format
, 255, 255, 255);
886 SDL_SetColorKey(icon
, SDL_TRUE
, colorkey
);
891 SDL_SetWindowIcon(sdl2_console
[0].real_window
, icon
);
894 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
895 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
897 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
898 sdl_cursor_normal
= SDL_GetCursor();
900 if (gui_fullscreen
) {
901 sdl_grab_start(&sdl2_console
[0]);
907 static QemuDisplay qemu_display_sdl2
= {
908 .type
= DISPLAY_TYPE_SDL
,
909 .early_init
= sdl2_display_early_init
,
910 .init
= sdl2_display_init
,
913 static void register_sdl1(void)
915 qemu_display_register(&qemu_display_sdl2
);
918 type_init(register_sdl1
);