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
;
88 scon
->real_window
= SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED
,
89 SDL_WINDOWPOS_UNDEFINED
,
90 surface_width(scon
->surface
),
91 surface_height(scon
->surface
),
93 scon
->real_renderer
= SDL_CreateRenderer(scon
->real_window
, -1, 0);
95 scon
->winctx
= SDL_GL_GetCurrentContext();
97 sdl_update_caption(scon
);
100 void sdl2_window_destroy(struct sdl2_console
*scon
)
102 if (!scon
->real_window
) {
106 SDL_DestroyRenderer(scon
->real_renderer
);
107 scon
->real_renderer
= NULL
;
108 SDL_DestroyWindow(scon
->real_window
);
109 scon
->real_window
= NULL
;
112 void sdl2_window_resize(struct sdl2_console
*scon
)
114 if (!scon
->real_window
) {
118 SDL_SetWindowSize(scon
->real_window
,
119 surface_width(scon
->surface
),
120 surface_height(scon
->surface
));
123 static void sdl2_redraw(struct sdl2_console
*scon
)
127 sdl2_gl_redraw(scon
);
130 sdl2_2d_redraw(scon
);
134 static void sdl_update_caption(struct sdl2_console
*scon
)
136 char win_title
[1024];
137 char icon_title
[1024];
138 const char *status
= "";
140 if (!runstate_is_running()) {
141 status
= " [Stopped]";
142 } else if (gui_grab
) {
144 status
= " - Press Ctrl-Alt-Shift-G to exit grab";
145 } else if (ctrl_grab
) {
146 status
= " - Press Right-Ctrl-G to exit grab";
148 status
= " - Press Ctrl-Alt-G to exit grab";
153 snprintf(win_title
, sizeof(win_title
), "QEMU (%s-%d)%s", qemu_name
,
155 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
157 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
158 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
161 if (scon
->real_window
) {
162 SDL_SetWindowTitle(scon
->real_window
, win_title
);
166 static void sdl_hide_cursor(struct sdl2_console
*scon
)
168 if (scon
->opts
->has_show_cursor
&& scon
->opts
->show_cursor
) {
172 SDL_ShowCursor(SDL_DISABLE
);
173 SDL_SetCursor(sdl_cursor_hidden
);
175 if (!qemu_input_is_absolute()) {
176 SDL_SetRelativeMouseMode(SDL_TRUE
);
180 static void sdl_show_cursor(struct sdl2_console
*scon
)
182 if (scon
->opts
->has_show_cursor
&& scon
->opts
->show_cursor
) {
186 if (!qemu_input_is_absolute()) {
187 SDL_SetRelativeMouseMode(SDL_FALSE
);
191 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
192 SDL_SetCursor(guest_sprite
);
194 SDL_SetCursor(sdl_cursor_normal
);
197 SDL_ShowCursor(SDL_ENABLE
);
200 static void sdl_grab_start(struct sdl2_console
*scon
)
202 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
204 if (!con
|| !qemu_console_is_graphic(con
)) {
208 * If the application is not active, do not try to enter grab state. This
209 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
210 * application (SDL bug).
212 if (!(SDL_GetWindowFlags(scon
->real_window
) & SDL_WINDOW_INPUT_FOCUS
)) {
216 SDL_SetCursor(guest_sprite
);
217 if (!qemu_input_is_absolute() && !absolute_enabled
) {
218 SDL_WarpMouseInWindow(scon
->real_window
, guest_x
, guest_y
);
221 sdl_hide_cursor(scon
);
223 SDL_SetWindowGrab(scon
->real_window
, SDL_TRUE
);
225 win32_kbd_set_grab(true);
226 sdl_update_caption(scon
);
229 static void sdl_grab_end(struct sdl2_console
*scon
)
231 SDL_SetWindowGrab(scon
->real_window
, SDL_FALSE
);
233 win32_kbd_set_grab(false);
234 sdl_show_cursor(scon
);
235 sdl_update_caption(scon
);
238 static void absolute_mouse_grab(struct sdl2_console
*scon
)
240 int mouse_x
, mouse_y
;
242 SDL_GetMouseState(&mouse_x
, &mouse_y
);
243 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
244 if (mouse_x
> 0 && mouse_x
< scr_w
- 1 &&
245 mouse_y
> 0 && mouse_y
< scr_h
- 1) {
246 sdl_grab_start(scon
);
250 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
252 if (qemu_input_is_absolute()) {
253 if (!absolute_enabled
) {
254 absolute_enabled
= 1;
255 SDL_SetRelativeMouseMode(SDL_FALSE
);
256 absolute_mouse_grab(&sdl2_console
[0]);
258 } else if (absolute_enabled
) {
259 if (!gui_fullscreen
) {
260 sdl_grab_end(&sdl2_console
[0]);
262 absolute_enabled
= 0;
266 static void sdl_send_mouse_event(struct sdl2_console
*scon
, int dx
, int dy
,
267 int x
, int y
, int state
)
269 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
270 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
271 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
272 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
274 static uint32_t prev_state
;
276 if (prev_state
!= state
) {
277 qemu_input_update_buttons(scon
->dcl
.con
, bmap
, prev_state
, state
);
281 if (qemu_input_is_absolute()) {
282 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_X
,
283 x
, 0, surface_width(scon
->surface
));
284 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_Y
,
285 y
, 0, surface_height(scon
->surface
));
295 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_X
, dx
);
296 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_Y
, dy
);
298 qemu_input_event_sync();
301 static void toggle_full_screen(struct sdl2_console
*scon
)
303 gui_fullscreen
= !gui_fullscreen
;
304 if (gui_fullscreen
) {
305 SDL_SetWindowFullscreen(scon
->real_window
,
306 SDL_WINDOW_FULLSCREEN_DESKTOP
);
307 gui_saved_grab
= gui_grab
;
308 sdl_grab_start(scon
);
310 if (!gui_saved_grab
) {
313 SDL_SetWindowFullscreen(scon
->real_window
, 0);
318 static int get_mod_state(void)
320 SDL_Keymod mod
= SDL_GetModState();
323 return (mod
& (gui_grab_code
| KMOD_LSHIFT
)) ==
324 (gui_grab_code
| KMOD_LSHIFT
);
325 } else if (ctrl_grab
) {
326 return (mod
& KMOD_RCTRL
) == KMOD_RCTRL
;
328 return (mod
& gui_grab_code
) == gui_grab_code
;
332 static void *sdl2_win32_get_hwnd(struct sdl2_console
*scon
)
337 SDL_VERSION(&info
.version
);
338 if (SDL_GetWindowWMInfo(scon
->real_window
, &info
)) {
339 return info
.info
.win
.window
;
345 static void handle_keydown(SDL_Event
*ev
)
348 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
349 int gui_key_modifier_pressed
= get_mod_state();
356 if (!scon
->ignore_hotkeys
&& gui_key_modifier_pressed
&& !ev
->key
.repeat
) {
357 switch (ev
->key
.keysym
.scancode
) {
370 win
= ev
->key
.keysym
.scancode
- SDL_SCANCODE_1
;
371 if (win
< sdl2_num_outputs
) {
372 sdl2_console
[win
].hidden
= !sdl2_console
[win
].hidden
;
373 if (sdl2_console
[win
].real_window
) {
374 if (sdl2_console
[win
].hidden
) {
375 SDL_HideWindow(sdl2_console
[win
].real_window
);
377 SDL_ShowWindow(sdl2_console
[win
].real_window
);
384 toggle_full_screen(scon
);
390 sdl_grab_start(scon
);
391 } else if (!gui_fullscreen
) {
396 sdl2_window_resize(scon
);
398 /* re-create scon->texture */
399 sdl2_2d_switch(&scon
->dcl
, scon
->surface
);
404 case SDL_SCANCODE_KP_PLUS
:
405 case SDL_SCANCODE_KP_MINUS
:
406 if (!gui_fullscreen
) {
409 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
411 width
= MAX(scr_w
+ (ev
->key
.keysym
.scancode
==
412 SDL_SCANCODE_KP_PLUS
? 50 : -50),
414 height
= (surface_height(scon
->surface
) * width
) /
415 surface_width(scon
->surface
);
416 fprintf(stderr
, "%s: scale to %dx%d\n",
417 __func__
, width
, height
);
418 sdl_scale(scon
, width
, height
);
428 sdl2_process_key(scon
, &ev
->key
);
432 static void handle_keyup(SDL_Event
*ev
)
434 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
440 scon
->ignore_hotkeys
= false;
441 sdl2_process_key(scon
, &ev
->key
);
444 static void handle_textinput(SDL_Event
*ev
)
446 struct sdl2_console
*scon
= get_scon_from_window(ev
->text
.windowID
);
447 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
453 if (qemu_console_is_graphic(con
)) {
456 kbd_put_string_console(con
, ev
->text
.text
, strlen(ev
->text
.text
));
459 static void handle_mousemotion(SDL_Event
*ev
)
462 struct sdl2_console
*scon
= get_scon_from_window(ev
->motion
.windowID
);
464 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
468 if (qemu_input_is_absolute() || absolute_enabled
) {
470 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
473 if (gui_grab
&& !gui_fullscreen
474 && (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
475 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
479 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
480 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
481 sdl_grab_start(scon
);
484 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
485 sdl_send_mouse_event(scon
, ev
->motion
.xrel
, ev
->motion
.yrel
,
486 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
490 static void handle_mousebutton(SDL_Event
*ev
)
492 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
493 SDL_MouseButtonEvent
*bev
;
494 struct sdl2_console
*scon
= get_scon_from_window(ev
->button
.windowID
);
496 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
501 if (!gui_grab
&& !qemu_input_is_absolute()) {
502 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
503 /* start grabbing all events */
504 sdl_grab_start(scon
);
507 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
508 buttonstate
|= SDL_BUTTON(bev
->button
);
510 buttonstate
&= ~SDL_BUTTON(bev
->button
);
512 sdl_send_mouse_event(scon
, 0, 0, bev
->x
, bev
->y
, buttonstate
);
516 static void handle_mousewheel(SDL_Event
*ev
)
518 struct sdl2_console
*scon
= get_scon_from_window(ev
->wheel
.windowID
);
519 SDL_MouseWheelEvent
*wev
= &ev
->wheel
;
522 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
527 btn
= INPUT_BUTTON_WHEEL_UP
;
528 } else if (wev
->y
< 0) {
529 btn
= INPUT_BUTTON_WHEEL_DOWN
;
534 qemu_input_queue_btn(scon
->dcl
.con
, btn
, true);
535 qemu_input_event_sync();
536 qemu_input_queue_btn(scon
->dcl
.con
, btn
, false);
537 qemu_input_event_sync();
540 static void handle_windowevent(SDL_Event
*ev
)
542 struct sdl2_console
*scon
= get_scon_from_window(ev
->window
.windowID
);
543 bool allow_close
= true;
549 switch (ev
->window
.event
) {
550 case SDL_WINDOWEVENT_RESIZED
:
553 memset(&info
, 0, sizeof(info
));
554 info
.width
= ev
->window
.data1
;
555 info
.height
= ev
->window
.data2
;
556 dpy_set_ui_info(scon
->dcl
.con
, &info
);
560 case SDL_WINDOWEVENT_EXPOSED
:
563 case SDL_WINDOWEVENT_FOCUS_GAINED
:
564 win32_kbd_set_grab(gui_grab
);
565 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
566 win32_kbd_set_window(sdl2_win32_get_hwnd(scon
));
569 case SDL_WINDOWEVENT_ENTER
:
570 if (!gui_grab
&& (qemu_input_is_absolute() || absolute_enabled
)) {
571 absolute_mouse_grab(scon
);
573 /* If a new console window opened using a hotkey receives the
574 * focus, SDL sends another KEYDOWN event to the new window,
575 * closing the console window immediately after.
577 * Work around this by ignoring further hotkey events until a
580 scon
->ignore_hotkeys
= get_mod_state();
582 case SDL_WINDOWEVENT_FOCUS_LOST
:
583 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
584 win32_kbd_set_window(NULL
);
586 if (gui_grab
&& !gui_fullscreen
) {
590 case SDL_WINDOWEVENT_RESTORED
:
591 update_displaychangelistener(&scon
->dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
593 case SDL_WINDOWEVENT_MINIMIZED
:
594 update_displaychangelistener(&scon
->dcl
, 500);
596 case SDL_WINDOWEVENT_CLOSE
:
597 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
598 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
603 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
606 SDL_HideWindow(scon
->real_window
);
610 case SDL_WINDOWEVENT_SHOWN
:
611 scon
->hidden
= false;
613 case SDL_WINDOWEVENT_HIDDEN
:
619 void sdl2_poll_events(struct sdl2_console
*scon
)
621 SDL_Event ev1
, *ev
= &ev1
;
622 bool allow_close
= true;
625 if (scon
->last_vm_running
!= runstate_is_running()) {
626 scon
->last_vm_running
= runstate_is_running();
627 sdl_update_caption(scon
);
630 while (SDL_PollEvent(ev
)) {
642 handle_textinput(ev
);
645 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
650 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
653 case SDL_MOUSEMOTION
:
655 handle_mousemotion(ev
);
657 case SDL_MOUSEBUTTONDOWN
:
658 case SDL_MOUSEBUTTONUP
:
660 handle_mousebutton(ev
);
664 handle_mousewheel(ev
);
666 case SDL_WINDOWEVENT
:
667 handle_windowevent(ev
);
675 if (scon
->idle_counter
< SDL2_MAX_IDLE_COUNT
) {
676 scon
->idle_counter
++;
677 if (scon
->idle_counter
>= SDL2_MAX_IDLE_COUNT
) {
678 scon
->dcl
.update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
682 scon
->idle_counter
= 0;
683 scon
->dcl
.update_interval
= SDL2_REFRESH_INTERVAL_BUSY
;
687 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
688 int x
, int y
, int on
)
690 struct sdl2_console
*scon
= container_of(dcl
, struct sdl2_console
, dcl
);
692 if (!qemu_console_is_graphic(scon
->dcl
.con
)) {
698 sdl_show_cursor(scon
);
700 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
701 SDL_SetCursor(guest_sprite
);
702 if (!qemu_input_is_absolute() && !absolute_enabled
) {
703 SDL_WarpMouseInWindow(scon
->real_window
, x
, y
);
706 } else if (gui_grab
) {
707 sdl_hide_cursor(scon
);
710 guest_x
= x
, guest_y
= y
;
713 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
718 SDL_FreeCursor(guest_sprite
);
721 if (guest_sprite_surface
) {
722 SDL_FreeSurface(guest_sprite_surface
);
725 guest_sprite_surface
=
726 SDL_CreateRGBSurfaceFrom(c
->data
, c
->width
, c
->height
, 32, c
->width
* 4,
727 0xff0000, 0x00ff00, 0xff, 0xff000000);
729 if (!guest_sprite_surface
) {
730 fprintf(stderr
, "Failed to make rgb surface from %p\n", c
);
733 guest_sprite
= SDL_CreateColorCursor(guest_sprite_surface
,
736 fprintf(stderr
, "Failed to make color cursor from %p\n", c
);
740 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
741 SDL_SetCursor(guest_sprite
);
745 static void sdl_cleanup(void)
748 SDL_FreeCursor(guest_sprite
);
750 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
753 static const DisplayChangeListenerOps dcl_2d_ops
= {
754 .dpy_name
= "sdl2-2d",
755 .dpy_gfx_update
= sdl2_2d_update
,
756 .dpy_gfx_switch
= sdl2_2d_switch
,
757 .dpy_gfx_check_format
= sdl2_2d_check_format
,
758 .dpy_refresh
= sdl2_2d_refresh
,
759 .dpy_mouse_set
= sdl_mouse_warp
,
760 .dpy_cursor_define
= sdl_mouse_define
,
764 static const DisplayChangeListenerOps dcl_gl_ops
= {
765 .dpy_name
= "sdl2-gl",
766 .dpy_gfx_update
= sdl2_gl_update
,
767 .dpy_gfx_switch
= sdl2_gl_switch
,
768 .dpy_gfx_check_format
= console_gl_check_format
,
769 .dpy_refresh
= sdl2_gl_refresh
,
770 .dpy_mouse_set
= sdl_mouse_warp
,
771 .dpy_cursor_define
= sdl_mouse_define
,
773 .dpy_gl_ctx_create
= sdl2_gl_create_context
,
774 .dpy_gl_ctx_destroy
= sdl2_gl_destroy_context
,
775 .dpy_gl_ctx_make_current
= sdl2_gl_make_context_current
,
776 .dpy_gl_ctx_get_current
= sdl2_gl_get_current_context
,
777 .dpy_gl_scanout_disable
= sdl2_gl_scanout_disable
,
778 .dpy_gl_scanout_texture
= sdl2_gl_scanout_texture
,
779 .dpy_gl_update
= sdl2_gl_scanout_flush
,
783 static void sdl2_display_early_init(DisplayOptions
*o
)
785 assert(o
->type
== DISPLAY_TYPE_SDL
);
786 if (o
->has_gl
&& o
->gl
) {
793 static void sdl2_display_init(DisplayState
*ds
, DisplayOptions
*o
)
798 SDL_Surface
*icon
= NULL
;
801 assert(o
->type
== DISPLAY_TYPE_SDL
);
804 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
805 * accessible $DISPLAY to open X11 window. This is often the case
806 * when qemu is run using sudo. But in this case, and when actually
807 * run in X11 environment, SDL fights with X11 for the video card,
808 * making current display unavailable, often until reboot.
809 * So make x11 the default SDL video driver if this variable is unset.
810 * This is a bit hackish but saves us from bigger problem.
811 * Maybe it's a good idea to fix this in SDL instead.
813 g_setenv("SDL_VIDEODRIVER", "x11", 0);
816 if (SDL_Init(SDL_INIT_VIDEO
)) {
817 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
821 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
822 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
, "0");
824 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD
, "1");
825 memset(&info
, 0, sizeof(info
));
826 SDL_VERSION(&info
.version
);
828 gui_fullscreen
= o
->has_full_screen
&& o
->full_screen
;
831 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
836 sdl2_num_outputs
= i
;
837 if (sdl2_num_outputs
== 0) {
840 sdl2_console
= g_new0(struct sdl2_console
, sdl2_num_outputs
);
841 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
842 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
844 if (!qemu_console_is_graphic(con
) &&
845 qemu_console_get_index(con
) != 0) {
846 sdl2_console
[i
].hidden
= true;
848 sdl2_console
[i
].idx
= i
;
849 sdl2_console
[i
].opts
= o
;
851 sdl2_console
[i
].opengl
= display_opengl
;
852 sdl2_console
[i
].dcl
.ops
= display_opengl
? &dcl_gl_ops
: &dcl_2d_ops
;
854 sdl2_console
[i
].opengl
= 0;
855 sdl2_console
[i
].dcl
.ops
= &dcl_2d_ops
;
857 sdl2_console
[i
].dcl
.con
= con
;
858 sdl2_console
[i
].kbd
= qkbd_state_init(con
);
859 register_displaychangelistener(&sdl2_console
[i
].dcl
);
861 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
862 if (SDL_GetWindowWMInfo(sdl2_console
[i
].real_window
, &info
)) {
863 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
864 qemu_console_set_window_id(con
, (uintptr_t)info
.info
.win
.window
);
865 #elif defined(SDL_VIDEO_DRIVER_X11)
866 qemu_console_set_window_id(con
, info
.info
.x11
.window
);
872 #ifdef CONFIG_SDL_IMAGE
873 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/128x128/apps/qemu.png");
874 icon
= IMG_Load(dir
);
876 /* Load a 32x32x4 image. White pixels are transparent. */
877 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/32x32/apps/qemu.bmp");
878 icon
= SDL_LoadBMP(dir
);
880 uint32_t colorkey
= SDL_MapRGB(icon
->format
, 255, 255, 255);
881 SDL_SetColorKey(icon
, SDL_TRUE
, colorkey
);
886 SDL_SetWindowIcon(sdl2_console
[0].real_window
, icon
);
889 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
890 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
892 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
893 sdl_cursor_normal
= SDL_GetCursor();
895 if (gui_fullscreen
) {
896 sdl_grab_start(&sdl2_console
[0]);
902 static QemuDisplay qemu_display_sdl2
= {
903 .type
= DISPLAY_TYPE_SDL
,
904 .early_init
= sdl2_display_early_init
,
905 .init
= sdl2_display_init
,
908 static void register_sdl1(void)
910 qemu_display_register(&qemu_display_sdl2
);
913 type_init(register_sdl1
);