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
->winctx
= SDL_GL_CreateContext(scon
->real_window
);
119 /* The SDL renderer is only used by sdl2-2D, when OpenGL is disabled */
120 scon
->real_renderer
= SDL_CreateRenderer(scon
->real_window
, -1, 0);
122 sdl_update_caption(scon
);
125 void sdl2_window_destroy(struct sdl2_console
*scon
)
127 if (!scon
->real_window
) {
132 SDL_GL_DeleteContext(scon
->winctx
);
135 if (scon
->real_renderer
) {
136 SDL_DestroyRenderer(scon
->real_renderer
);
137 scon
->real_renderer
= NULL
;
139 SDL_DestroyWindow(scon
->real_window
);
140 scon
->real_window
= NULL
;
143 void sdl2_window_resize(struct sdl2_console
*scon
)
145 if (!scon
->real_window
) {
149 SDL_SetWindowSize(scon
->real_window
,
150 surface_width(scon
->surface
),
151 surface_height(scon
->surface
));
154 static void sdl2_redraw(struct sdl2_console
*scon
)
158 sdl2_gl_redraw(scon
);
161 sdl2_2d_redraw(scon
);
165 static void sdl_update_caption(struct sdl2_console
*scon
)
167 char win_title
[1024];
168 char icon_title
[1024];
169 const char *status
= "";
171 if (!runstate_is_running()) {
172 status
= " [Stopped]";
173 } else if (gui_grab
) {
175 status
= " - Press Ctrl-Alt-Shift-G to exit grab";
176 } else if (ctrl_grab
) {
177 status
= " - Press Right-Ctrl-G to exit grab";
179 status
= " - Press Ctrl-Alt-G to exit grab";
184 snprintf(win_title
, sizeof(win_title
), "QEMU (%s-%d)%s", qemu_name
,
186 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
188 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
189 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
192 if (scon
->real_window
) {
193 SDL_SetWindowTitle(scon
->real_window
, win_title
);
197 static void sdl_hide_cursor(struct sdl2_console
*scon
)
199 if (scon
->opts
->has_show_cursor
&& scon
->opts
->show_cursor
) {
203 SDL_ShowCursor(SDL_DISABLE
);
204 SDL_SetCursor(sdl_cursor_hidden
);
206 if (!qemu_input_is_absolute()) {
207 SDL_SetRelativeMouseMode(SDL_TRUE
);
211 static void sdl_show_cursor(struct sdl2_console
*scon
)
213 if (scon
->opts
->has_show_cursor
&& scon
->opts
->show_cursor
) {
217 if (!qemu_input_is_absolute()) {
218 SDL_SetRelativeMouseMode(SDL_FALSE
);
222 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
223 SDL_SetCursor(guest_sprite
);
225 SDL_SetCursor(sdl_cursor_normal
);
228 SDL_ShowCursor(SDL_ENABLE
);
231 static void sdl_grab_start(struct sdl2_console
*scon
)
233 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
235 if (!con
|| !qemu_console_is_graphic(con
)) {
239 * If the application is not active, do not try to enter grab state. This
240 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
241 * application (SDL bug).
243 if (!(SDL_GetWindowFlags(scon
->real_window
) & SDL_WINDOW_INPUT_FOCUS
)) {
247 SDL_SetCursor(guest_sprite
);
248 if (!qemu_input_is_absolute() && !absolute_enabled
) {
249 SDL_WarpMouseInWindow(scon
->real_window
, guest_x
, guest_y
);
252 sdl_hide_cursor(scon
);
254 SDL_SetWindowGrab(scon
->real_window
, SDL_TRUE
);
256 win32_kbd_set_grab(true);
257 sdl_update_caption(scon
);
260 static void sdl_grab_end(struct sdl2_console
*scon
)
262 SDL_SetWindowGrab(scon
->real_window
, SDL_FALSE
);
264 win32_kbd_set_grab(false);
265 sdl_show_cursor(scon
);
266 sdl_update_caption(scon
);
269 static void absolute_mouse_grab(struct sdl2_console
*scon
)
271 int mouse_x
, mouse_y
;
273 SDL_GetMouseState(&mouse_x
, &mouse_y
);
274 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
275 if (mouse_x
> 0 && mouse_x
< scr_w
- 1 &&
276 mouse_y
> 0 && mouse_y
< scr_h
- 1) {
277 sdl_grab_start(scon
);
281 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
283 if (qemu_input_is_absolute()) {
284 if (!absolute_enabled
) {
285 absolute_enabled
= 1;
286 SDL_SetRelativeMouseMode(SDL_FALSE
);
287 absolute_mouse_grab(&sdl2_console
[0]);
289 } else if (absolute_enabled
) {
290 if (!gui_fullscreen
) {
291 sdl_grab_end(&sdl2_console
[0]);
293 absolute_enabled
= 0;
297 static void sdl_send_mouse_event(struct sdl2_console
*scon
, int dx
, int dy
,
298 int x
, int y
, int state
)
300 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
301 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
302 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
303 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
304 [INPUT_BUTTON_SIDE
] = SDL_BUTTON(SDL_BUTTON_X1
),
305 [INPUT_BUTTON_EXTRA
] = SDL_BUTTON(SDL_BUTTON_X2
)
307 static uint32_t prev_state
;
309 if (prev_state
!= state
) {
310 qemu_input_update_buttons(scon
->dcl
.con
, bmap
, prev_state
, state
);
314 if (qemu_input_is_absolute()) {
315 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_X
,
316 x
, 0, surface_width(scon
->surface
));
317 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_Y
,
318 y
, 0, surface_height(scon
->surface
));
328 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_X
, dx
);
329 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_Y
, dy
);
331 qemu_input_event_sync();
334 static void toggle_full_screen(struct sdl2_console
*scon
)
336 gui_fullscreen
= !gui_fullscreen
;
337 if (gui_fullscreen
) {
338 SDL_SetWindowFullscreen(scon
->real_window
,
339 SDL_WINDOW_FULLSCREEN_DESKTOP
);
340 gui_saved_grab
= gui_grab
;
341 sdl_grab_start(scon
);
343 if (!gui_saved_grab
) {
346 SDL_SetWindowFullscreen(scon
->real_window
, 0);
351 static int get_mod_state(void)
353 SDL_Keymod mod
= SDL_GetModState();
356 return (mod
& (gui_grab_code
| KMOD_LSHIFT
)) ==
357 (gui_grab_code
| KMOD_LSHIFT
);
358 } else if (ctrl_grab
) {
359 return (mod
& KMOD_RCTRL
) == KMOD_RCTRL
;
361 return (mod
& gui_grab_code
) == gui_grab_code
;
365 static void *sdl2_win32_get_hwnd(struct sdl2_console
*scon
)
370 SDL_VERSION(&info
.version
);
371 if (SDL_GetWindowWMInfo(scon
->real_window
, &info
)) {
372 return info
.info
.win
.window
;
378 static void handle_keydown(SDL_Event
*ev
)
381 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
382 int gui_key_modifier_pressed
= get_mod_state();
389 if (!scon
->ignore_hotkeys
&& gui_key_modifier_pressed
&& !ev
->key
.repeat
) {
390 switch (ev
->key
.keysym
.scancode
) {
403 win
= ev
->key
.keysym
.scancode
- SDL_SCANCODE_1
;
404 if (win
< sdl2_num_outputs
) {
405 sdl2_console
[win
].hidden
= !sdl2_console
[win
].hidden
;
406 if (sdl2_console
[win
].real_window
) {
407 if (sdl2_console
[win
].hidden
) {
408 SDL_HideWindow(sdl2_console
[win
].real_window
);
410 SDL_ShowWindow(sdl2_console
[win
].real_window
);
417 toggle_full_screen(scon
);
423 sdl_grab_start(scon
);
424 } else if (!gui_fullscreen
) {
429 sdl2_window_resize(scon
);
431 /* re-create scon->texture */
432 sdl2_2d_switch(&scon
->dcl
, scon
->surface
);
437 case SDL_SCANCODE_KP_PLUS
:
438 case SDL_SCANCODE_KP_MINUS
:
439 if (!gui_fullscreen
) {
442 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
444 width
= MAX(scr_w
+ (ev
->key
.keysym
.scancode
==
445 SDL_SCANCODE_KP_PLUS
? 50 : -50),
447 height
= (surface_height(scon
->surface
) * width
) /
448 surface_width(scon
->surface
);
449 fprintf(stderr
, "%s: scale to %dx%d\n",
450 __func__
, width
, height
);
451 sdl_scale(scon
, width
, height
);
461 sdl2_process_key(scon
, &ev
->key
);
465 static void handle_keyup(SDL_Event
*ev
)
467 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
473 scon
->ignore_hotkeys
= false;
474 sdl2_process_key(scon
, &ev
->key
);
477 static void handle_textinput(SDL_Event
*ev
)
479 struct sdl2_console
*scon
= get_scon_from_window(ev
->text
.windowID
);
480 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
486 if (qemu_console_is_graphic(con
)) {
489 kbd_put_string_console(con
, ev
->text
.text
, strlen(ev
->text
.text
));
492 static void handle_mousemotion(SDL_Event
*ev
)
495 struct sdl2_console
*scon
= get_scon_from_window(ev
->motion
.windowID
);
497 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
501 if (qemu_input_is_absolute() || absolute_enabled
) {
503 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
506 if (gui_grab
&& !gui_fullscreen
507 && (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
508 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
512 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
513 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
514 sdl_grab_start(scon
);
517 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
518 sdl_send_mouse_event(scon
, ev
->motion
.xrel
, ev
->motion
.yrel
,
519 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
523 static void handle_mousebutton(SDL_Event
*ev
)
525 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
526 SDL_MouseButtonEvent
*bev
;
527 struct sdl2_console
*scon
= get_scon_from_window(ev
->button
.windowID
);
529 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
534 if (!gui_grab
&& !qemu_input_is_absolute()) {
535 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
536 /* start grabbing all events */
537 sdl_grab_start(scon
);
540 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
541 buttonstate
|= SDL_BUTTON(bev
->button
);
543 buttonstate
&= ~SDL_BUTTON(bev
->button
);
545 sdl_send_mouse_event(scon
, 0, 0, bev
->x
, bev
->y
, buttonstate
);
549 static void handle_mousewheel(SDL_Event
*ev
)
551 struct sdl2_console
*scon
= get_scon_from_window(ev
->wheel
.windowID
);
552 SDL_MouseWheelEvent
*wev
= &ev
->wheel
;
555 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
560 btn
= INPUT_BUTTON_WHEEL_UP
;
561 } else if (wev
->y
< 0) {
562 btn
= INPUT_BUTTON_WHEEL_DOWN
;
563 } else if (wev
->x
< 0) {
564 btn
= INPUT_BUTTON_WHEEL_RIGHT
;
565 } else if (wev
->x
> 0) {
566 btn
= INPUT_BUTTON_WHEEL_LEFT
;
571 qemu_input_queue_btn(scon
->dcl
.con
, btn
, true);
572 qemu_input_event_sync();
573 qemu_input_queue_btn(scon
->dcl
.con
, btn
, false);
574 qemu_input_event_sync();
577 static void handle_windowevent(SDL_Event
*ev
)
579 struct sdl2_console
*scon
= get_scon_from_window(ev
->window
.windowID
);
580 bool allow_close
= true;
586 switch (ev
->window
.event
) {
587 case SDL_WINDOWEVENT_RESIZED
:
590 memset(&info
, 0, sizeof(info
));
591 info
.width
= ev
->window
.data1
;
592 info
.height
= ev
->window
.data2
;
593 dpy_set_ui_info(scon
->dcl
.con
, &info
, true);
597 case SDL_WINDOWEVENT_EXPOSED
:
600 case SDL_WINDOWEVENT_FOCUS_GAINED
:
601 win32_kbd_set_grab(gui_grab
);
602 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
603 win32_kbd_set_window(sdl2_win32_get_hwnd(scon
));
606 case SDL_WINDOWEVENT_ENTER
:
607 if (!gui_grab
&& (qemu_input_is_absolute() || absolute_enabled
)) {
608 absolute_mouse_grab(scon
);
610 /* If a new console window opened using a hotkey receives the
611 * focus, SDL sends another KEYDOWN event to the new window,
612 * closing the console window immediately after.
614 * Work around this by ignoring further hotkey events until a
617 scon
->ignore_hotkeys
= get_mod_state();
619 case SDL_WINDOWEVENT_FOCUS_LOST
:
620 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
621 win32_kbd_set_window(NULL
);
623 if (gui_grab
&& !gui_fullscreen
) {
627 case SDL_WINDOWEVENT_RESTORED
:
628 update_displaychangelistener(&scon
->dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
630 case SDL_WINDOWEVENT_MINIMIZED
:
631 update_displaychangelistener(&scon
->dcl
, 500);
633 case SDL_WINDOWEVENT_CLOSE
:
634 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
635 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
639 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
640 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
643 SDL_HideWindow(scon
->real_window
);
647 case SDL_WINDOWEVENT_SHOWN
:
648 scon
->hidden
= false;
650 case SDL_WINDOWEVENT_HIDDEN
:
656 void sdl2_poll_events(struct sdl2_console
*scon
)
658 SDL_Event ev1
, *ev
= &ev1
;
659 bool allow_close
= true;
662 if (scon
->last_vm_running
!= runstate_is_running()) {
663 scon
->last_vm_running
= runstate_is_running();
664 sdl_update_caption(scon
);
667 while (SDL_PollEvent(ev
)) {
679 handle_textinput(ev
);
682 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
686 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
687 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
690 case SDL_MOUSEMOTION
:
692 handle_mousemotion(ev
);
694 case SDL_MOUSEBUTTONDOWN
:
695 case SDL_MOUSEBUTTONUP
:
697 handle_mousebutton(ev
);
701 handle_mousewheel(ev
);
703 case SDL_WINDOWEVENT
:
704 handle_windowevent(ev
);
712 if (scon
->idle_counter
< SDL2_MAX_IDLE_COUNT
) {
713 scon
->idle_counter
++;
714 if (scon
->idle_counter
>= SDL2_MAX_IDLE_COUNT
) {
715 scon
->dcl
.update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
719 scon
->idle_counter
= 0;
720 scon
->dcl
.update_interval
= SDL2_REFRESH_INTERVAL_BUSY
;
724 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
725 int x
, int y
, int on
)
727 struct sdl2_console
*scon
= container_of(dcl
, struct sdl2_console
, dcl
);
729 if (!qemu_console_is_graphic(scon
->dcl
.con
)) {
735 sdl_show_cursor(scon
);
737 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
738 SDL_SetCursor(guest_sprite
);
739 if (!qemu_input_is_absolute() && !absolute_enabled
) {
740 SDL_WarpMouseInWindow(scon
->real_window
, x
, y
);
743 } else if (gui_grab
) {
744 sdl_hide_cursor(scon
);
747 guest_x
= x
, guest_y
= y
;
750 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
755 SDL_FreeCursor(guest_sprite
);
758 if (guest_sprite_surface
) {
759 SDL_FreeSurface(guest_sprite_surface
);
762 guest_sprite_surface
=
763 SDL_CreateRGBSurfaceFrom(c
->data
, c
->width
, c
->height
, 32, c
->width
* 4,
764 0xff0000, 0x00ff00, 0xff, 0xff000000);
766 if (!guest_sprite_surface
) {
767 fprintf(stderr
, "Failed to make rgb surface from %p\n", c
);
770 guest_sprite
= SDL_CreateColorCursor(guest_sprite_surface
,
773 fprintf(stderr
, "Failed to make color cursor from %p\n", c
);
777 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
778 SDL_SetCursor(guest_sprite
);
782 static void sdl_cleanup(void)
785 SDL_FreeCursor(guest_sprite
);
787 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
790 static const DisplayChangeListenerOps dcl_2d_ops
= {
791 .dpy_name
= "sdl2-2d",
792 .dpy_gfx_update
= sdl2_2d_update
,
793 .dpy_gfx_switch
= sdl2_2d_switch
,
794 .dpy_gfx_check_format
= sdl2_2d_check_format
,
795 .dpy_refresh
= sdl2_2d_refresh
,
796 .dpy_mouse_set
= sdl_mouse_warp
,
797 .dpy_cursor_define
= sdl_mouse_define
,
801 static const DisplayChangeListenerOps dcl_gl_ops
= {
802 .dpy_name
= "sdl2-gl",
803 .dpy_gfx_update
= sdl2_gl_update
,
804 .dpy_gfx_switch
= sdl2_gl_switch
,
805 .dpy_gfx_check_format
= console_gl_check_format
,
806 .dpy_refresh
= sdl2_gl_refresh
,
807 .dpy_mouse_set
= sdl_mouse_warp
,
808 .dpy_cursor_define
= sdl_mouse_define
,
810 .dpy_gl_scanout_disable
= sdl2_gl_scanout_disable
,
811 .dpy_gl_scanout_texture
= sdl2_gl_scanout_texture
,
812 .dpy_gl_update
= sdl2_gl_scanout_flush
,
816 sdl2_gl_is_compatible_dcl(DisplayGLCtx
*dgc
,
817 DisplayChangeListener
*dcl
)
819 return dcl
->ops
== &dcl_gl_ops
;
822 static const DisplayGLCtxOps gl_ctx_ops
= {
823 .dpy_gl_ctx_is_compatible_dcl
= sdl2_gl_is_compatible_dcl
,
824 .dpy_gl_ctx_create
= sdl2_gl_create_context
,
825 .dpy_gl_ctx_destroy
= sdl2_gl_destroy_context
,
826 .dpy_gl_ctx_make_current
= sdl2_gl_make_context_current
,
830 static void sdl2_display_early_init(DisplayOptions
*o
)
832 assert(o
->type
== DISPLAY_TYPE_SDL
);
833 if (o
->has_gl
&& o
->gl
) {
840 static void sdl2_display_init(DisplayState
*ds
, DisplayOptions
*o
)
845 SDL_Surface
*icon
= NULL
;
848 assert(o
->type
== DISPLAY_TYPE_SDL
);
850 if (SDL_GetHintBoolean("QEMU_ENABLE_SDL_LOGGING", SDL_FALSE
)) {
851 SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE
);
854 if (SDL_Init(SDL_INIT_VIDEO
)) {
855 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
859 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
860 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
, "0");
863 /* QEMU uses its own low level keyboard hook procecure on Windows */
864 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD
, "1");
866 #ifdef SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED
867 SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED
, "0");
869 SDL_SetHint(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4
, "1");
870 memset(&info
, 0, sizeof(info
));
871 SDL_VERSION(&info
.version
);
873 gui_fullscreen
= o
->has_full_screen
&& o
->full_screen
;
875 if (o
->u
.sdl
.has_grab_mod
) {
876 if (o
->u
.sdl
.grab_mod
== HOT_KEY_MOD_LSHIFT_LCTRL_LALT
) {
878 } else if (o
->u
.sdl
.grab_mod
== HOT_KEY_MOD_RCTRL
) {
884 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
889 sdl2_num_outputs
= i
;
890 if (sdl2_num_outputs
== 0) {
893 sdl2_console
= g_new0(struct sdl2_console
, sdl2_num_outputs
);
894 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
895 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
897 if (!qemu_console_is_graphic(con
) &&
898 qemu_console_get_index(con
) != 0) {
899 sdl2_console
[i
].hidden
= true;
901 sdl2_console
[i
].idx
= i
;
902 sdl2_console
[i
].opts
= o
;
904 sdl2_console
[i
].opengl
= display_opengl
;
905 sdl2_console
[i
].dcl
.ops
= display_opengl
? &dcl_gl_ops
: &dcl_2d_ops
;
906 sdl2_console
[i
].dgc
.ops
= display_opengl
? &gl_ctx_ops
: NULL
;
908 sdl2_console
[i
].opengl
= 0;
909 sdl2_console
[i
].dcl
.ops
= &dcl_2d_ops
;
911 sdl2_console
[i
].dcl
.con
= con
;
912 sdl2_console
[i
].kbd
= qkbd_state_init(con
);
913 if (display_opengl
) {
914 qemu_console_set_display_gl_ctx(con
, &sdl2_console
[i
].dgc
);
916 register_displaychangelistener(&sdl2_console
[i
].dcl
);
918 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
919 if (SDL_GetWindowWMInfo(sdl2_console
[i
].real_window
, &info
)) {
920 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
921 qemu_console_set_window_id(con
, (uintptr_t)info
.info
.win
.window
);
922 #elif defined(SDL_VIDEO_DRIVER_X11)
923 qemu_console_set_window_id(con
, info
.info
.x11
.window
);
929 #ifdef CONFIG_SDL_IMAGE
930 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/128x128/apps/qemu.png");
931 icon
= IMG_Load(dir
);
933 /* Load a 32x32x4 image. White pixels are transparent. */
934 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/32x32/apps/qemu.bmp");
935 icon
= SDL_LoadBMP(dir
);
937 uint32_t colorkey
= SDL_MapRGB(icon
->format
, 255, 255, 255);
938 SDL_SetColorKey(icon
, SDL_TRUE
, colorkey
);
943 SDL_SetWindowIcon(sdl2_console
[0].real_window
, icon
);
946 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
947 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
949 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
950 sdl_cursor_normal
= SDL_GetCursor();
952 if (gui_fullscreen
) {
953 sdl_grab_start(&sdl2_console
[0]);
959 static QemuDisplay qemu_display_sdl2
= {
960 .type
= DISPLAY_TYPE_SDL
,
961 .early_init
= sdl2_display_early_init
,
962 .init
= sdl2_display_init
,
965 static void register_sdl1(void)
967 qemu_display_register(&qemu_display_sdl2
);
970 type_init(register_sdl1
);
973 module_dep("ui-opengl");