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 static void sdl_update_caption(struct sdl2_console
*scon
);
63 static struct sdl2_console
*get_scon_from_window(uint32_t window_id
)
66 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
67 if (sdl2_console
[i
].real_window
== SDL_GetWindowFromID(window_id
)) {
68 return &sdl2_console
[i
];
74 void sdl2_window_create(struct sdl2_console
*scon
)
81 assert(!scon
->real_window
);
84 flags
|= SDL_WINDOW_FULLSCREEN_DESKTOP
;
86 flags
|= SDL_WINDOW_RESIZABLE
;
89 flags
|= SDL_WINDOW_HIDDEN
;
93 flags
|= SDL_WINDOW_OPENGL
;
97 scon
->real_window
= SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED
,
98 SDL_WINDOWPOS_UNDEFINED
,
99 surface_width(scon
->surface
),
100 surface_height(scon
->surface
),
102 scon
->real_renderer
= SDL_CreateRenderer(scon
->real_window
, -1, 0);
104 scon
->winctx
= SDL_GL_GetCurrentContext();
106 sdl_update_caption(scon
);
109 void sdl2_window_destroy(struct sdl2_console
*scon
)
111 if (!scon
->real_window
) {
115 SDL_DestroyRenderer(scon
->real_renderer
);
116 scon
->real_renderer
= NULL
;
117 SDL_DestroyWindow(scon
->real_window
);
118 scon
->real_window
= NULL
;
121 void sdl2_window_resize(struct sdl2_console
*scon
)
123 if (!scon
->real_window
) {
127 SDL_SetWindowSize(scon
->real_window
,
128 surface_width(scon
->surface
),
129 surface_height(scon
->surface
));
132 static void sdl2_redraw(struct sdl2_console
*scon
)
136 sdl2_gl_redraw(scon
);
139 sdl2_2d_redraw(scon
);
143 static void sdl_update_caption(struct sdl2_console
*scon
)
145 char win_title
[1024];
146 char icon_title
[1024];
147 const char *status
= "";
149 if (!runstate_is_running()) {
150 status
= " [Stopped]";
151 } else if (gui_grab
) {
153 status
= " - Press Ctrl-Alt-Shift-G to exit grab";
154 } else if (ctrl_grab
) {
155 status
= " - Press Right-Ctrl-G to exit grab";
157 status
= " - Press Ctrl-Alt-G to exit grab";
162 snprintf(win_title
, sizeof(win_title
), "QEMU (%s-%d)%s", qemu_name
,
164 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
166 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
167 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
170 if (scon
->real_window
) {
171 SDL_SetWindowTitle(scon
->real_window
, win_title
);
175 static void sdl_hide_cursor(struct sdl2_console
*scon
)
177 if (scon
->opts
->has_show_cursor
&& scon
->opts
->show_cursor
) {
181 SDL_ShowCursor(SDL_DISABLE
);
182 SDL_SetCursor(sdl_cursor_hidden
);
184 if (!qemu_input_is_absolute()) {
185 SDL_SetRelativeMouseMode(SDL_TRUE
);
189 static void sdl_show_cursor(struct sdl2_console
*scon
)
191 if (scon
->opts
->has_show_cursor
&& scon
->opts
->show_cursor
) {
195 if (!qemu_input_is_absolute()) {
196 SDL_SetRelativeMouseMode(SDL_FALSE
);
200 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
201 SDL_SetCursor(guest_sprite
);
203 SDL_SetCursor(sdl_cursor_normal
);
206 SDL_ShowCursor(SDL_ENABLE
);
209 static void sdl_grab_start(struct sdl2_console
*scon
)
211 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
213 if (!con
|| !qemu_console_is_graphic(con
)) {
217 * If the application is not active, do not try to enter grab state. This
218 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
219 * application (SDL bug).
221 if (!(SDL_GetWindowFlags(scon
->real_window
) & SDL_WINDOW_INPUT_FOCUS
)) {
225 SDL_SetCursor(guest_sprite
);
226 if (!qemu_input_is_absolute() && !absolute_enabled
) {
227 SDL_WarpMouseInWindow(scon
->real_window
, guest_x
, guest_y
);
230 sdl_hide_cursor(scon
);
232 SDL_SetWindowGrab(scon
->real_window
, SDL_TRUE
);
234 win32_kbd_set_grab(true);
235 sdl_update_caption(scon
);
238 static void sdl_grab_end(struct sdl2_console
*scon
)
240 SDL_SetWindowGrab(scon
->real_window
, SDL_FALSE
);
242 win32_kbd_set_grab(false);
243 sdl_show_cursor(scon
);
244 sdl_update_caption(scon
);
247 static void absolute_mouse_grab(struct sdl2_console
*scon
)
249 int mouse_x
, mouse_y
;
251 SDL_GetMouseState(&mouse_x
, &mouse_y
);
252 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
253 if (mouse_x
> 0 && mouse_x
< scr_w
- 1 &&
254 mouse_y
> 0 && mouse_y
< scr_h
- 1) {
255 sdl_grab_start(scon
);
259 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
261 if (qemu_input_is_absolute()) {
262 if (!absolute_enabled
) {
263 absolute_enabled
= 1;
264 SDL_SetRelativeMouseMode(SDL_FALSE
);
265 absolute_mouse_grab(&sdl2_console
[0]);
267 } else if (absolute_enabled
) {
268 if (!gui_fullscreen
) {
269 sdl_grab_end(&sdl2_console
[0]);
271 absolute_enabled
= 0;
275 static void sdl_send_mouse_event(struct sdl2_console
*scon
, int dx
, int dy
,
276 int x
, int y
, int state
)
278 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
279 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
280 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
281 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
282 [INPUT_BUTTON_SIDE
] = SDL_BUTTON(SDL_BUTTON_X1
),
283 [INPUT_BUTTON_EXTRA
] = SDL_BUTTON(SDL_BUTTON_X2
)
285 static uint32_t prev_state
;
287 if (prev_state
!= state
) {
288 qemu_input_update_buttons(scon
->dcl
.con
, bmap
, prev_state
, state
);
292 if (qemu_input_is_absolute()) {
293 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_X
,
294 x
, 0, surface_width(scon
->surface
));
295 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_Y
,
296 y
, 0, surface_height(scon
->surface
));
306 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_X
, dx
);
307 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_Y
, dy
);
309 qemu_input_event_sync();
312 static void toggle_full_screen(struct sdl2_console
*scon
)
314 gui_fullscreen
= !gui_fullscreen
;
315 if (gui_fullscreen
) {
316 SDL_SetWindowFullscreen(scon
->real_window
,
317 SDL_WINDOW_FULLSCREEN_DESKTOP
);
318 gui_saved_grab
= gui_grab
;
319 sdl_grab_start(scon
);
321 if (!gui_saved_grab
) {
324 SDL_SetWindowFullscreen(scon
->real_window
, 0);
329 static int get_mod_state(void)
331 SDL_Keymod mod
= SDL_GetModState();
334 return (mod
& (gui_grab_code
| KMOD_LSHIFT
)) ==
335 (gui_grab_code
| KMOD_LSHIFT
);
336 } else if (ctrl_grab
) {
337 return (mod
& KMOD_RCTRL
) == KMOD_RCTRL
;
339 return (mod
& gui_grab_code
) == gui_grab_code
;
343 static void *sdl2_win32_get_hwnd(struct sdl2_console
*scon
)
348 SDL_VERSION(&info
.version
);
349 if (SDL_GetWindowWMInfo(scon
->real_window
, &info
)) {
350 return info
.info
.win
.window
;
356 static void handle_keydown(SDL_Event
*ev
)
359 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
360 int gui_key_modifier_pressed
= get_mod_state();
367 if (!scon
->ignore_hotkeys
&& gui_key_modifier_pressed
&& !ev
->key
.repeat
) {
368 switch (ev
->key
.keysym
.scancode
) {
381 win
= ev
->key
.keysym
.scancode
- SDL_SCANCODE_1
;
382 if (win
< sdl2_num_outputs
) {
383 sdl2_console
[win
].hidden
= !sdl2_console
[win
].hidden
;
384 if (sdl2_console
[win
].real_window
) {
385 if (sdl2_console
[win
].hidden
) {
386 SDL_HideWindow(sdl2_console
[win
].real_window
);
388 SDL_ShowWindow(sdl2_console
[win
].real_window
);
395 toggle_full_screen(scon
);
401 sdl_grab_start(scon
);
402 } else if (!gui_fullscreen
) {
407 sdl2_window_resize(scon
);
409 /* re-create scon->texture */
410 sdl2_2d_switch(&scon
->dcl
, scon
->surface
);
415 case SDL_SCANCODE_KP_PLUS
:
416 case SDL_SCANCODE_KP_MINUS
:
417 if (!gui_fullscreen
) {
420 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
422 width
= MAX(scr_w
+ (ev
->key
.keysym
.scancode
==
423 SDL_SCANCODE_KP_PLUS
? 50 : -50),
425 height
= (surface_height(scon
->surface
) * width
) /
426 surface_width(scon
->surface
);
427 fprintf(stderr
, "%s: scale to %dx%d\n",
428 __func__
, width
, height
);
429 sdl_scale(scon
, width
, height
);
439 sdl2_process_key(scon
, &ev
->key
);
443 static void handle_keyup(SDL_Event
*ev
)
445 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
451 scon
->ignore_hotkeys
= false;
452 sdl2_process_key(scon
, &ev
->key
);
455 static void handle_textinput(SDL_Event
*ev
)
457 struct sdl2_console
*scon
= get_scon_from_window(ev
->text
.windowID
);
458 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
464 if (qemu_console_is_graphic(con
)) {
467 kbd_put_string_console(con
, ev
->text
.text
, strlen(ev
->text
.text
));
470 static void handle_mousemotion(SDL_Event
*ev
)
473 struct sdl2_console
*scon
= get_scon_from_window(ev
->motion
.windowID
);
475 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
479 if (qemu_input_is_absolute() || absolute_enabled
) {
481 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
484 if (gui_grab
&& !gui_fullscreen
485 && (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
486 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
490 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
491 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
492 sdl_grab_start(scon
);
495 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
496 sdl_send_mouse_event(scon
, ev
->motion
.xrel
, ev
->motion
.yrel
,
497 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
501 static void handle_mousebutton(SDL_Event
*ev
)
503 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
504 SDL_MouseButtonEvent
*bev
;
505 struct sdl2_console
*scon
= get_scon_from_window(ev
->button
.windowID
);
507 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
512 if (!gui_grab
&& !qemu_input_is_absolute()) {
513 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
514 /* start grabbing all events */
515 sdl_grab_start(scon
);
518 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
519 buttonstate
|= SDL_BUTTON(bev
->button
);
521 buttonstate
&= ~SDL_BUTTON(bev
->button
);
523 sdl_send_mouse_event(scon
, 0, 0, bev
->x
, bev
->y
, buttonstate
);
527 static void handle_mousewheel(SDL_Event
*ev
)
529 struct sdl2_console
*scon
= get_scon_from_window(ev
->wheel
.windowID
);
530 SDL_MouseWheelEvent
*wev
= &ev
->wheel
;
533 if (!scon
|| !qemu_console_is_graphic(scon
->dcl
.con
)) {
538 btn
= INPUT_BUTTON_WHEEL_UP
;
539 } else if (wev
->y
< 0) {
540 btn
= INPUT_BUTTON_WHEEL_DOWN
;
541 } else if (wev
->x
< 0) {
542 btn
= INPUT_BUTTON_WHEEL_RIGHT
;
543 } else if (wev
->x
> 0) {
544 btn
= INPUT_BUTTON_WHEEL_LEFT
;
549 qemu_input_queue_btn(scon
->dcl
.con
, btn
, true);
550 qemu_input_event_sync();
551 qemu_input_queue_btn(scon
->dcl
.con
, btn
, false);
552 qemu_input_event_sync();
555 static void handle_windowevent(SDL_Event
*ev
)
557 struct sdl2_console
*scon
= get_scon_from_window(ev
->window
.windowID
);
558 bool allow_close
= true;
564 switch (ev
->window
.event
) {
565 case SDL_WINDOWEVENT_RESIZED
:
568 memset(&info
, 0, sizeof(info
));
569 info
.width
= ev
->window
.data1
;
570 info
.height
= ev
->window
.data2
;
571 dpy_set_ui_info(scon
->dcl
.con
, &info
, true);
575 case SDL_WINDOWEVENT_EXPOSED
:
578 case SDL_WINDOWEVENT_FOCUS_GAINED
:
579 win32_kbd_set_grab(gui_grab
);
580 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
581 win32_kbd_set_window(sdl2_win32_get_hwnd(scon
));
584 case SDL_WINDOWEVENT_ENTER
:
585 if (!gui_grab
&& (qemu_input_is_absolute() || absolute_enabled
)) {
586 absolute_mouse_grab(scon
);
588 /* If a new console window opened using a hotkey receives the
589 * focus, SDL sends another KEYDOWN event to the new window,
590 * closing the console window immediately after.
592 * Work around this by ignoring further hotkey events until a
595 scon
->ignore_hotkeys
= get_mod_state();
597 case SDL_WINDOWEVENT_FOCUS_LOST
:
598 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
599 win32_kbd_set_window(NULL
);
601 if (gui_grab
&& !gui_fullscreen
) {
605 case SDL_WINDOWEVENT_RESTORED
:
606 update_displaychangelistener(&scon
->dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
608 case SDL_WINDOWEVENT_MINIMIZED
:
609 update_displaychangelistener(&scon
->dcl
, 500);
611 case SDL_WINDOWEVENT_CLOSE
:
612 if (qemu_console_is_graphic(scon
->dcl
.con
)) {
613 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
617 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
618 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
621 SDL_HideWindow(scon
->real_window
);
625 case SDL_WINDOWEVENT_SHOWN
:
626 scon
->hidden
= false;
628 case SDL_WINDOWEVENT_HIDDEN
:
634 void sdl2_poll_events(struct sdl2_console
*scon
)
636 SDL_Event ev1
, *ev
= &ev1
;
637 bool allow_close
= true;
640 if (scon
->last_vm_running
!= runstate_is_running()) {
641 scon
->last_vm_running
= runstate_is_running();
642 sdl_update_caption(scon
);
645 while (SDL_PollEvent(ev
)) {
657 handle_textinput(ev
);
660 if (scon
->opts
->has_window_close
&& !scon
->opts
->window_close
) {
664 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
665 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
668 case SDL_MOUSEMOTION
:
670 handle_mousemotion(ev
);
672 case SDL_MOUSEBUTTONDOWN
:
673 case SDL_MOUSEBUTTONUP
:
675 handle_mousebutton(ev
);
679 handle_mousewheel(ev
);
681 case SDL_WINDOWEVENT
:
682 handle_windowevent(ev
);
690 if (scon
->idle_counter
< SDL2_MAX_IDLE_COUNT
) {
691 scon
->idle_counter
++;
692 if (scon
->idle_counter
>= SDL2_MAX_IDLE_COUNT
) {
693 scon
->dcl
.update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
697 scon
->idle_counter
= 0;
698 scon
->dcl
.update_interval
= SDL2_REFRESH_INTERVAL_BUSY
;
702 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
703 int x
, int y
, int on
)
705 struct sdl2_console
*scon
= container_of(dcl
, struct sdl2_console
, dcl
);
707 if (!qemu_console_is_graphic(scon
->dcl
.con
)) {
713 sdl_show_cursor(scon
);
715 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
716 SDL_SetCursor(guest_sprite
);
717 if (!qemu_input_is_absolute() && !absolute_enabled
) {
718 SDL_WarpMouseInWindow(scon
->real_window
, x
, y
);
721 } else if (gui_grab
) {
722 sdl_hide_cursor(scon
);
725 guest_x
= x
, guest_y
= y
;
728 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
733 SDL_FreeCursor(guest_sprite
);
736 if (guest_sprite_surface
) {
737 SDL_FreeSurface(guest_sprite_surface
);
740 guest_sprite_surface
=
741 SDL_CreateRGBSurfaceFrom(c
->data
, c
->width
, c
->height
, 32, c
->width
* 4,
742 0xff0000, 0x00ff00, 0xff, 0xff000000);
744 if (!guest_sprite_surface
) {
745 fprintf(stderr
, "Failed to make rgb surface from %p\n", c
);
748 guest_sprite
= SDL_CreateColorCursor(guest_sprite_surface
,
751 fprintf(stderr
, "Failed to make color cursor from %p\n", c
);
755 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
756 SDL_SetCursor(guest_sprite
);
760 static void sdl_cleanup(void)
763 SDL_FreeCursor(guest_sprite
);
765 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
768 static const DisplayChangeListenerOps dcl_2d_ops
= {
769 .dpy_name
= "sdl2-2d",
770 .dpy_gfx_update
= sdl2_2d_update
,
771 .dpy_gfx_switch
= sdl2_2d_switch
,
772 .dpy_gfx_check_format
= sdl2_2d_check_format
,
773 .dpy_refresh
= sdl2_2d_refresh
,
774 .dpy_mouse_set
= sdl_mouse_warp
,
775 .dpy_cursor_define
= sdl_mouse_define
,
779 static const DisplayChangeListenerOps dcl_gl_ops
= {
780 .dpy_name
= "sdl2-gl",
781 .dpy_gfx_update
= sdl2_gl_update
,
782 .dpy_gfx_switch
= sdl2_gl_switch
,
783 .dpy_gfx_check_format
= console_gl_check_format
,
784 .dpy_refresh
= sdl2_gl_refresh
,
785 .dpy_mouse_set
= sdl_mouse_warp
,
786 .dpy_cursor_define
= sdl_mouse_define
,
788 .dpy_gl_scanout_disable
= sdl2_gl_scanout_disable
,
789 .dpy_gl_scanout_texture
= sdl2_gl_scanout_texture
,
790 .dpy_gl_update
= sdl2_gl_scanout_flush
,
794 sdl2_gl_is_compatible_dcl(DisplayGLCtx
*dgc
,
795 DisplayChangeListener
*dcl
)
797 return dcl
->ops
== &dcl_gl_ops
;
800 static const DisplayGLCtxOps gl_ctx_ops
= {
801 .dpy_gl_ctx_is_compatible_dcl
= sdl2_gl_is_compatible_dcl
,
802 .dpy_gl_ctx_create
= sdl2_gl_create_context
,
803 .dpy_gl_ctx_destroy
= sdl2_gl_destroy_context
,
804 .dpy_gl_ctx_make_current
= sdl2_gl_make_context_current
,
808 static void sdl2_display_early_init(DisplayOptions
*o
)
810 assert(o
->type
== DISPLAY_TYPE_SDL
);
811 if (o
->has_gl
&& o
->gl
) {
818 static void sdl2_display_init(DisplayState
*ds
, DisplayOptions
*o
)
823 SDL_Surface
*icon
= NULL
;
826 assert(o
->type
== DISPLAY_TYPE_SDL
);
829 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
830 * accessible $DISPLAY to open X11 window. This is often the case
831 * when qemu is run using sudo. But in this case, and when actually
832 * run in X11 environment, SDL fights with X11 for the video card,
833 * making current display unavailable, often until reboot.
834 * So make x11 the default SDL video driver if this variable is unset.
835 * This is a bit hackish but saves us from bigger problem.
836 * Maybe it's a good idea to fix this in SDL instead.
838 if (!g_setenv("SDL_VIDEODRIVER", "x11", 0)) {
839 fprintf(stderr
, "Could not set SDL_VIDEODRIVER environment variable\n");
844 if (SDL_Init(SDL_INIT_VIDEO
)) {
845 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
849 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
850 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR
, "0");
852 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD
, "1");
853 memset(&info
, 0, sizeof(info
));
854 SDL_VERSION(&info
.version
);
856 gui_fullscreen
= o
->has_full_screen
&& o
->full_screen
;
858 if (o
->u
.sdl
.has_grab_mod
) {
859 if (o
->u
.sdl
.grab_mod
== HOT_KEY_MOD_LSHIFT_LCTRL_LALT
) {
861 } else if (o
->u
.sdl
.grab_mod
== HOT_KEY_MOD_RCTRL
) {
867 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
872 sdl2_num_outputs
= i
;
873 if (sdl2_num_outputs
== 0) {
876 sdl2_console
= g_new0(struct sdl2_console
, sdl2_num_outputs
);
877 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
878 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
880 if (!qemu_console_is_graphic(con
) &&
881 qemu_console_get_index(con
) != 0) {
882 sdl2_console
[i
].hidden
= true;
884 sdl2_console
[i
].idx
= i
;
885 sdl2_console
[i
].opts
= o
;
887 sdl2_console
[i
].opengl
= display_opengl
;
888 sdl2_console
[i
].dcl
.ops
= display_opengl
? &dcl_gl_ops
: &dcl_2d_ops
;
889 sdl2_console
[i
].dgc
.ops
= display_opengl
? &gl_ctx_ops
: NULL
;
891 sdl2_console
[i
].opengl
= 0;
892 sdl2_console
[i
].dcl
.ops
= &dcl_2d_ops
;
894 sdl2_console
[i
].dcl
.con
= con
;
895 sdl2_console
[i
].kbd
= qkbd_state_init(con
);
896 if (display_opengl
) {
897 qemu_console_set_display_gl_ctx(con
, &sdl2_console
[i
].dgc
);
899 register_displaychangelistener(&sdl2_console
[i
].dcl
);
901 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
902 if (SDL_GetWindowWMInfo(sdl2_console
[i
].real_window
, &info
)) {
903 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
904 qemu_console_set_window_id(con
, (uintptr_t)info
.info
.win
.window
);
905 #elif defined(SDL_VIDEO_DRIVER_X11)
906 qemu_console_set_window_id(con
, info
.info
.x11
.window
);
912 #ifdef CONFIG_SDL_IMAGE
913 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/128x128/apps/qemu.png");
914 icon
= IMG_Load(dir
);
916 /* Load a 32x32x4 image. White pixels are transparent. */
917 dir
= get_relocated_path(CONFIG_QEMU_ICONDIR
"/hicolor/32x32/apps/qemu.bmp");
918 icon
= SDL_LoadBMP(dir
);
920 uint32_t colorkey
= SDL_MapRGB(icon
->format
, 255, 255, 255);
921 SDL_SetColorKey(icon
, SDL_TRUE
, colorkey
);
926 SDL_SetWindowIcon(sdl2_console
[0].real_window
, icon
);
929 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
930 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
932 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
933 sdl_cursor_normal
= SDL_GetCursor();
935 if (gui_fullscreen
) {
936 sdl_grab_start(&sdl2_console
[0]);
942 static QemuDisplay qemu_display_sdl2
= {
943 .type
= DISPLAY_TYPE_SDL
,
944 .early_init
= sdl2_display_early_init
,
945 .init
= sdl2_display_init
,
948 static void register_sdl1(void)
950 qemu_display_register(&qemu_display_sdl2
);
953 type_init(register_sdl1
);
956 module_dep("ui-opengl");