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/runstate-action.h"
34 #include "sysemu/sysemu.h"
35 #include "ui/win32-kbd-hook.h"
38 static int sdl2_num_outputs
;
39 static struct sdl2_console
*sdl2_console
;
41 static SDL_Surface
*guest_sprite_surface
;
42 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
44 static bool ctrl_grab
;
46 static int gui_saved_grab
;
47 static int gui_fullscreen
;
48 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
49 static SDL_Cursor
*sdl_cursor_normal
;
50 static SDL_Cursor
*sdl_cursor_hidden
;
51 static int absolute_enabled
;
52 static int guest_cursor
;
53 static int guest_x
, guest_y
;
54 static SDL_Cursor
*guest_sprite
;
55 static Notifier mouse_mode_notifier
;
57 #define SDL2_REFRESH_INTERVAL_BUSY 10
58 #define SDL2_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
59 / SDL2_REFRESH_INTERVAL_BUSY + 1)
61 /* introduced in SDL 2.0.10 */
62 #ifndef SDL_HINT_RENDER_BATCHING
63 #define SDL_HINT_RENDER_BATCHING "SDL_RENDER_BATCHING"
66 static void sdl_update_caption(struct sdl2_console
*scon
);
68 static struct sdl2_console
*get_scon_from_window(uint32_t window_id
)
71 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
72 if (sdl2_console
[i
].real_window
== SDL_GetWindowFromID(window_id
)) {
73 return &sdl2_console
[i
];
79 void sdl2_window_create(struct sdl2_console
*scon
)
86 assert(!scon
->real_window
);
89 flags
|= SDL_WINDOW_FULLSCREEN_DESKTOP
;
91 flags
|= SDL_WINDOW_RESIZABLE
;
94 flags
|= SDL_WINDOW_HIDDEN
;
98 flags
|= SDL_WINDOW_OPENGL
;
102 scon
->real_window
= SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED
,
103 SDL_WINDOWPOS_UNDEFINED
,
104 surface_width(scon
->surface
),
105 surface_height(scon
->surface
),
108 const char *driver
= "opengl";
110 if (scon
->opts
->gl
== DISPLAYGL_MODE_ES
) {
111 driver
= "opengles2";
114 SDL_SetHint(SDL_HINT_RENDER_DRIVER
, driver
);
115 SDL_SetHint(SDL_HINT_RENDER_BATCHING
, "1");
117 scon
->real_renderer
= SDL_CreateRenderer(scon
->real_window
, -1, 0);
120 scon
->winctx
= SDL_GL_CreateContext(scon
->real_window
);
122 sdl_update_caption(scon
);
125 void sdl2_window_destroy(struct sdl2_console
*scon
)
127 if (!scon
->real_window
) {
131 SDL_GL_DeleteContext(scon
->winctx
);
133 SDL_DestroyRenderer(scon
->real_renderer
);
134 scon
->real_renderer
= NULL
;
135 SDL_DestroyWindow(scon
->real_window
);
136 scon
->real_window
= NULL
;
139 void sdl2_window_resize(struct sdl2_console
*scon
)
141 if (!scon
->real_window
) {
145 SDL_SetWindowSize(scon
->real_window
,
146 surface_width(scon
->surface
),
147 surface_height(scon
->surface
));
150 static void sdl2_redraw(struct sdl2_console
*scon
)
154 sdl2_gl_redraw(scon
);
157 sdl2_2d_redraw(scon
);
161 static void sdl_update_caption(struct sdl2_console
*scon
)
163 char win_title
[1024];
164 char icon_title
[1024];
165 const char *status
= "";
167 if (!runstate_is_running()) {
168 status
= " [Stopped]";
169 } else if (gui_grab
) {
171 status
= " - Press Ctrl-Alt-Shift-G to exit grab";
172 } else if (ctrl_grab
) {
173 status
= " - Press Right-Ctrl-G to exit grab";
175 status
= " - Press Ctrl-Alt-G to exit grab";
180 snprintf(win_title
, sizeof(win_title
), "QEMU (%s-%d)%s", qemu_name
,
182 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
184 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
185 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
188 if (scon
->real_window
) {
189 SDL_SetWindowTitle(scon
->real_window
, win_title
);
193 static void sdl_hide_cursor(struct sdl2_console
*scon
)
195 if (scon
->opts
->has_show_cursor
&& scon
->opts
->show_cursor
) {
199 SDL_ShowCursor(SDL_DISABLE
);
200 SDL_SetCursor(sdl_cursor_hidden
);
202 if (!qemu_input_is_absolute()) {
203 SDL_SetRelativeMouseMode(SDL_TRUE
);
207 static void sdl_show_cursor(struct sdl2_console
*scon
)
209 if (scon
->opts
->has_show_cursor
&& scon
->opts
->show_cursor
) {
213 if (!qemu_input_is_absolute()) {
214 SDL_SetRelativeMouseMode(SDL_FALSE
);
218 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
219 SDL_SetCursor(guest_sprite
);
221 SDL_SetCursor(sdl_cursor_normal
);
224 SDL_ShowCursor(SDL_ENABLE
);
227 static void sdl_grab_start(struct sdl2_console
*scon
)
229 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
231 if (!con
|| !qemu_console_is_graphic(con
)) {
235 * If the application is not active, do not try to enter grab state. This
236 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
237 * application (SDL bug).
239 if (!(SDL_GetWindowFlags(scon
->real_window
) & SDL_WINDOW_INPUT_FOCUS
)) {
243 SDL_SetCursor(guest_sprite
);
244 if (!qemu_input_is_absolute() && !absolute_enabled
) {
245 SDL_WarpMouseInWindow(scon
->real_window
, guest_x
, guest_y
);
248 sdl_hide_cursor(scon
);
250 SDL_SetWindowGrab(scon
->real_window
, SDL_TRUE
);
252 win32_kbd_set_grab(true);
253 sdl_update_caption(scon
);
256 static void sdl_grab_end(struct sdl2_console
*scon
)
258 SDL_SetWindowGrab(scon
->real_window
, SDL_FALSE
);
260 win32_kbd_set_grab(false);
261 sdl_show_cursor(scon
);
262 sdl_update_caption(scon
);
265 static void absolute_mouse_grab(struct sdl2_console
*scon
)
267 int mouse_x
, mouse_y
;
269 SDL_GetMouseState(&mouse_x
, &mouse_y
);
270 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
271 if (mouse_x
> 0 && mouse_x
< scr_w
- 1 &&
272 mouse_y
> 0 && mouse_y
< scr_h
- 1) {
273 sdl_grab_start(scon
);
277 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
279 if (qemu_input_is_absolute()) {
280 if (!absolute_enabled
) {
281 absolute_enabled
= 1;
282 SDL_SetRelativeMouseMode(SDL_FALSE
);
283 absolute_mouse_grab(&sdl2_console
[0]);
285 } else if (absolute_enabled
) {
286 if (!gui_fullscreen
) {
287 sdl_grab_end(&sdl2_console
[0]);
289 absolute_enabled
= 0;
293 static void sdl_send_mouse_event(struct sdl2_console
*scon
, int dx
, int dy
,
294 int x
, int y
, int state
)
296 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
297 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
298 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
299 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
300 [INPUT_BUTTON_SIDE
] = SDL_BUTTON(SDL_BUTTON_X1
),
301 [INPUT_BUTTON_EXTRA
] = SDL_BUTTON(SDL_BUTTON_X2
)
303 static uint32_t prev_state
;
305 if (prev_state
!= state
) {
306 qemu_input_update_buttons(scon
->dcl
.con
, bmap
, prev_state
, state
);
310 if (qemu_input_is_absolute()) {
311 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_X
,
312 x
, 0, surface_width(scon
->surface
));
313 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_Y
,
314 y
, 0, surface_height(scon
->surface
));
324 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_X
, dx
);
325 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_Y
, dy
);
327 qemu_input_event_sync();
330 static void toggle_full_screen(struct sdl2_console
*scon
)
332 gui_fullscreen
= !gui_fullscreen
;
333 if (gui_fullscreen
) {
334 SDL_SetWindowFullscreen(scon
->real_window
,
335 SDL_WINDOW_FULLSCREEN_DESKTOP
);
336 gui_saved_grab
= gui_grab
;
337 sdl_grab_start(scon
);
339 if (!gui_saved_grab
) {
342 SDL_SetWindowFullscreen(scon
->real_window
, 0);
347 static int get_mod_state(void)
349 SDL_Keymod mod
= SDL_GetModState();
352 return (mod
& (gui_grab_code
| KMOD_LSHIFT
)) ==
353 (gui_grab_code
| KMOD_LSHIFT
);
354 } else if (ctrl_grab
) {
355 return (mod
& KMOD_RCTRL
) == KMOD_RCTRL
;
357 return (mod
& gui_grab_code
) == gui_grab_code
;
361 static void *sdl2_win32_get_hwnd(struct sdl2_console
*scon
)
366 SDL_VERSION(&info
.version
);
367 if (SDL_GetWindowWMInfo(scon
->real_window
, &info
)) {
368 return info
.info
.win
.window
;
374 static void handle_keydown(SDL_Event
*ev
)
377 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
378 int gui_key_modifier_pressed
= get_mod_state();
385 if (!scon
->ignore_hotkeys
&& gui_key_modifier_pressed
&& !ev
->key
.repeat
) {
386 switch (ev
->key
.keysym
.scancode
) {
399 win
= ev
->key
.keysym
.scancode
- SDL_SCANCODE_1
;
400 if (win
< sdl2_num_outputs
) {
401 sdl2_console
[win
].hidden
= !sdl2_console
[win
].hidden
;
402 if (sdl2_console
[win
].real_window
) {
403 if (sdl2_console
[win
].hidden
) {
404 SDL_HideWindow(sdl2_console
[win
].real_window
);
406 SDL_ShowWindow(sdl2_console
[win
].real_window
);
413 toggle_full_screen(scon
);
419 sdl_grab_start(scon
);
420 } else if (!gui_fullscreen
) {
425 sdl2_window_resize(scon
);
427 /* re-create scon->texture */
428 sdl2_2d_switch(&scon
->dcl
, scon
->surface
);
433 case SDL_SCANCODE_KP_PLUS
:
434 case SDL_SCANCODE_KP_MINUS
:
435 if (!gui_fullscreen
) {
438 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
440 width
= MAX(scr_w
+ (ev
->key
.keysym
.scancode
==
441 SDL_SCANCODE_KP_PLUS
? 50 : -50),
443 height
= (surface_height(scon
->surface
) * width
) /
444 surface_width(scon
->surface
);
445 fprintf(stderr
, "%s: scale to %dx%d\n",
446 __func__
, width
, height
);
447 sdl_scale(scon
, width
, height
);
457 sdl2_process_key(scon
, &ev
->key
);
461 static void handle_keyup(SDL_Event
*ev
)
463 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
469 scon
->ignore_hotkeys
= false;
470 sdl2_process_key(scon
, &ev
->key
);
473 static void handle_textinput(SDL_Event
*ev
)
475 struct sdl2_console
*scon
= get_scon_from_window(ev
->text
.windowID
);
476 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
482 if (qemu_console_is_graphic(con
)) {
485 kbd_put_string_console(con
, ev
->text
.text
, strlen(ev
->text
.text
));
488 static void handle_mousemotion(SDL_Event
*ev
)
491 struct sdl2_console
*scon
= get_scon_from_window(ev
->motion
.windowID
);
493 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
497 if (qemu_input_is_absolute() || absolute_enabled
) {
499 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
502 if (gui_grab
&& !gui_fullscreen
503 && (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
504 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
508 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
509 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
510 sdl_grab_start(scon
);
513 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
514 sdl_send_mouse_event(scon
, ev
->motion
.xrel
, ev
->motion
.yrel
,
515 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
519 static void handle_mousebutton(SDL_Event
*ev
)
521 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
522 SDL_MouseButtonEvent
*bev
;
523 struct sdl2_console
*scon
= get_scon_from_window(ev
->button
.windowID
);
525 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
530 if (!gui_grab
&& !qemu_input_is_absolute()) {
531 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
532 /* start grabbing all events */
533 sdl_grab_start(scon
);
536 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
537 buttonstate
|= SDL_BUTTON(bev
->button
);
539 buttonstate
&= ~SDL_BUTTON(bev
->button
);
541 sdl_send_mouse_event(scon
, 0, 0, bev
->x
, bev
->y
, buttonstate
);
545 static void handle_mousewheel(SDL_Event
*ev
)
547 struct sdl2_console
*scon
= get_scon_from_window(ev
->wheel
.windowID
);
548 SDL_MouseWheelEvent
*wev
= &ev
->wheel
;
551 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
556 btn
= INPUT_BUTTON_WHEEL_UP
;
557 } else if (wev
->y
< 0) {
558 btn
= INPUT_BUTTON_WHEEL_DOWN
;
559 } else if (wev
->x
< 0) {
560 btn
= INPUT_BUTTON_WHEEL_RIGHT
;
561 } else if (wev
->x
> 0) {
562 btn
= INPUT_BUTTON_WHEEL_LEFT
;
567 qemu_input_queue_btn(scon
->dcl
.con
, btn
, true);
568 qemu_input_event_sync();
569 qemu_input_queue_btn(scon
->dcl
.con
, btn
, false);
570 qemu_input_event_sync();
573 static void handle_windowevent(SDL_Event
*ev
)
575 struct sdl2_console
*scon
= get_scon_from_window(ev
->window
.windowID
);
576 bool allow_close
= true;
582 switch (ev
->window
.event
) {
583 case SDL_WINDOWEVENT_RESIZED
:
586 memset(&info
, 0, sizeof(info
));
587 info
.width
= ev
->window
.data1
;
588 info
.height
= ev
->window
.data2
;
589 dpy_set_ui_info(scon
->dcl
.con
, &info
, true);
593 case SDL_WINDOWEVENT_EXPOSED
:
596 case SDL_WINDOWEVENT_FOCUS_GAINED
:
597 win32_kbd_set_grab(gui_grab
);
598 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
599 win32_kbd_set_window(sdl2_win32_get_hwnd(scon
));
602 case SDL_WINDOWEVENT_ENTER
:
603 if (!gui_grab
&& (qemu_input_is_absolute() || absolute_enabled
)) {
604 absolute_mouse_grab(scon
);
606 /* If a new console window opened using a hotkey receives the
607 * focus, SDL sends another KEYDOWN event to the new window,
608 * closing the console window immediately after.
610 * Work around this by ignoring further hotkey events until a
613 scon
->ignore_hotkeys
= get_mod_state();
615 case SDL_WINDOWEVENT_FOCUS_LOST
:
616 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
617 win32_kbd_set_window(NULL
);
619 if (gui_grab
&& !gui_fullscreen
) {
623 case SDL_WINDOWEVENT_RESTORED
:
624 update_displaychangelistener(&scon
->dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
626 case SDL_WINDOWEVENT_MINIMIZED
:
627 update_displaychangelistener(&scon
->dcl
, 500);
629 case SDL_WINDOWEVENT_CLOSE
:
630 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
631 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
635 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
636 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
639 SDL_HideWindow(scon
->real_window
);
643 case SDL_WINDOWEVENT_SHOWN
:
644 scon
->hidden
= false;
646 case SDL_WINDOWEVENT_HIDDEN
:
652 void sdl2_poll_events(struct sdl2_console
*scon
)
654 SDL_Event ev1
, *ev
= &ev1
;
655 bool allow_close
= true;
658 if (scon
->last_vm_running
!= runstate_is_running()) {
659 scon
->last_vm_running
= runstate_is_running();
660 sdl_update_caption(scon
);
663 while (SDL_PollEvent(ev
)) {
675 handle_textinput(ev
);
678 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
682 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
683 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
686 case SDL_MOUSEMOTION
:
688 handle_mousemotion(ev
);
690 case SDL_MOUSEBUTTONDOWN
:
691 case SDL_MOUSEBUTTONUP
:
693 handle_mousebutton(ev
);
697 handle_mousewheel(ev
);
699 case SDL_WINDOWEVENT
:
700 handle_windowevent(ev
);
708 if (scon
->idle_counter
< SDL2_MAX_IDLE_COUNT
) {
709 scon
->idle_counter
++;
710 if (scon
->idle_counter
>= SDL2_MAX_IDLE_COUNT
) {
711 scon
->dcl
.update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
715 scon
->idle_counter
= 0;
716 scon
->dcl
.update_interval
= SDL2_REFRESH_INTERVAL_BUSY
;
720 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
721 int x
, int y
, int on
)
723 struct sdl2_console
*scon
= container_of(dcl
, struct sdl2_console
, dcl
);
725 if (!qemu_console_is_graphic(scon
->dcl
.con
)) {
731 sdl_show_cursor(scon
);
733 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
734 SDL_SetCursor(guest_sprite
);
735 if (!qemu_input_is_absolute() && !absolute_enabled
) {
736 SDL_WarpMouseInWindow(scon
->real_window
, x
, y
);
739 } else if (gui_grab
) {
740 sdl_hide_cursor(scon
);
743 guest_x
= x
, guest_y
= y
;
746 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
751 SDL_FreeCursor(guest_sprite
);
754 if (guest_sprite_surface
) {
755 SDL_FreeSurface(guest_sprite_surface
);
758 guest_sprite_surface
=
759 SDL_CreateRGBSurfaceFrom(c
->data
, c
->width
, c
->height
, 32, c
->width
* 4,
760 0xff0000, 0x00ff00, 0xff, 0xff000000);
762 if (!guest_sprite_surface
) {
763 fprintf(stderr
, "Failed to make rgb surface from %p\n", c
);
766 guest_sprite
= SDL_CreateColorCursor(guest_sprite_surface
,
769 fprintf(stderr
, "Failed to make color cursor from %p\n", c
);
773 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
774 SDL_SetCursor(guest_sprite
);
778 static void sdl_cleanup(void)
781 SDL_FreeCursor(guest_sprite
);
783 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
786 static const DisplayChangeListenerOps dcl_2d_ops
= {
787 .dpy_name
= "sdl2-2d",
788 .dpy_gfx_update
= sdl2_2d_update
,
789 .dpy_gfx_switch
= sdl2_2d_switch
,
790 .dpy_gfx_check_format
= sdl2_2d_check_format
,
791 .dpy_refresh
= sdl2_2d_refresh
,
792 .dpy_mouse_set
= sdl_mouse_warp
,
793 .dpy_cursor_define
= sdl_mouse_define
,
797 static const DisplayChangeListenerOps dcl_gl_ops
= {
798 .dpy_name
= "sdl2-gl",
799 .dpy_gfx_update
= sdl2_gl_update
,
800 .dpy_gfx_switch
= sdl2_gl_switch
,
801 .dpy_gfx_check_format
= console_gl_check_format
,
802 .dpy_refresh
= sdl2_gl_refresh
,
803 .dpy_mouse_set
= sdl_mouse_warp
,
804 .dpy_cursor_define
= sdl_mouse_define
,
806 .dpy_gl_scanout_disable
= sdl2_gl_scanout_disable
,
807 .dpy_gl_scanout_texture
= sdl2_gl_scanout_texture
,
808 .dpy_gl_update
= sdl2_gl_scanout_flush
,
812 sdl2_gl_is_compatible_dcl(DisplayGLCtx
*dgc
,
813 DisplayChangeListener
*dcl
)
815 return dcl
->ops
== &dcl_gl_ops
;
818 static const DisplayGLCtxOps gl_ctx_ops
= {
819 .dpy_gl_ctx_is_compatible_dcl
= sdl2_gl_is_compatible_dcl
,
820 .dpy_gl_ctx_create
= sdl2_gl_create_context
,
821 .dpy_gl_ctx_destroy
= sdl2_gl_destroy_context
,
822 .dpy_gl_ctx_make_current
= sdl2_gl_make_context_current
,
826 static void sdl2_display_early_init(DisplayOptions
*o
)
828 assert(o
->type
== DISPLAY_TYPE_SDL
);
829 if (o
->has_gl
&& o
->gl
) {
836 static void sdl2_display_init(DisplayState
*ds
, DisplayOptions
*o
)
841 SDL_Surface
*icon
= NULL
;
844 assert(o
->type
== DISPLAY_TYPE_SDL
);
846 if (SDL_GetHintBoolean("QEMU_ENABLE_SDL_LOGGING", SDL_FALSE
)) {
847 SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE
);
850 if (SDL_Init(SDL_INIT_VIDEO
)) {
851 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
855 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
856 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
, "0");
858 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD
, "1");
859 memset(&info
, 0, sizeof(info
));
860 SDL_VERSION(&info
.version
);
862 gui_fullscreen
= o
->has_full_screen
&& o
->full_screen
;
864 if (o
->u
.sdl
.has_grab_mod
) {
865 if (o
->u
.sdl
.grab_mod
== HOT_KEY_MOD_LSHIFT_LCTRL_LALT
) {
867 } else if (o
->u
.sdl
.grab_mod
== HOT_KEY_MOD_RCTRL
) {
873 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
878 sdl2_num_outputs
= i
;
879 if (sdl2_num_outputs
== 0) {
882 sdl2_console
= g_new0(struct sdl2_console
, sdl2_num_outputs
);
883 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
884 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
886 if (!qemu_console_is_graphic(con
) &&
887 qemu_console_get_index(con
) != 0) {
888 sdl2_console
[i
].hidden
= true;
890 sdl2_console
[i
].idx
= i
;
891 sdl2_console
[i
].opts
= o
;
893 sdl2_console
[i
].opengl
= display_opengl
;
894 sdl2_console
[i
].dcl
.ops
= display_opengl
? &dcl_gl_ops
: &dcl_2d_ops
;
895 sdl2_console
[i
].dgc
.ops
= display_opengl
? &gl_ctx_ops
: NULL
;
897 sdl2_console
[i
].opengl
= 0;
898 sdl2_console
[i
].dcl
.ops
= &dcl_2d_ops
;
900 sdl2_console
[i
].dcl
.con
= con
;
901 sdl2_console
[i
].kbd
= qkbd_state_init(con
);
902 if (display_opengl
) {
903 qemu_console_set_display_gl_ctx(con
, &sdl2_console
[i
].dgc
);
905 register_displaychangelistener(&sdl2_console
[i
].dcl
);
907 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
908 if (SDL_GetWindowWMInfo(sdl2_console
[i
].real_window
, &info
)) {
909 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
910 qemu_console_set_window_id(con
, (uintptr_t)info
.info
.win
.window
);
911 #elif defined(SDL_VIDEO_DRIVER_X11)
912 qemu_console_set_window_id(con
, info
.info
.x11
.window
);
918 #ifdef CONFIG_SDL_IMAGE
919 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/128x128/apps/qemu.png");
920 icon
= IMG_Load(dir
);
922 /* Load a 32x32x4 image. White pixels are transparent. */
923 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/32x32/apps/qemu.bmp");
924 icon
= SDL_LoadBMP(dir
);
926 uint32_t colorkey
= SDL_MapRGB(icon
->format
, 255, 255, 255);
927 SDL_SetColorKey(icon
, SDL_TRUE
, colorkey
);
932 SDL_SetWindowIcon(sdl2_console
[0].real_window
, icon
);
935 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
936 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
938 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
939 sdl_cursor_normal
= SDL_GetCursor();
941 if (gui_fullscreen
) {
942 sdl_grab_start(&sdl2_console
[0]);
948 static QemuDisplay qemu_display_sdl2
= {
949 .type
= DISPLAY_TYPE_SDL
,
950 .early_init
= sdl2_display_early_init
,
951 .init
= sdl2_display_init
,
954 static void register_sdl1(void)
956 qemu_display_register(&qemu_display_sdl2
);
959 type_init(register_sdl1
);
962 module_dep("ui-opengl");