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 /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
27 #undef WIN32_LEAN_AND_MEAN
31 #if SDL_MAJOR_VERSION == 2
32 #include <SDL_syswm.h>
34 #include "qemu-common.h"
35 #include "ui/console.h"
37 #include "sysemu/sysemu.h"
40 #include "sdl2-keymap.h"
42 static int sdl2_num_outputs
;
43 static struct sdl2_state
{
44 DisplayChangeListener dcl
;
45 DisplaySurface
*surface
;
47 SDL_Window
*real_window
;
48 SDL_Renderer
*real_renderer
;
50 int last_vm_running
; /* per console for caption reasons */
55 static SDL_Surface
*guest_sprite_surface
;
56 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
58 static bool gui_saved_scaling
;
59 static int gui_saved_width
;
60 static int gui_saved_height
;
61 static int gui_saved_grab
;
62 static int gui_fullscreen
;
63 static int gui_noframe
;
64 static int gui_key_modifier_pressed
;
65 static int gui_keysym
;
66 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
67 static uint8_t modifiers_state
[SDL_NUM_SCANCODES
];
68 static SDL_Cursor
*sdl_cursor_normal
;
69 static SDL_Cursor
*sdl_cursor_hidden
;
70 static int absolute_enabled
;
71 static int guest_cursor
;
72 static int guest_x
, guest_y
;
73 static SDL_Cursor
*guest_sprite
;
74 static int scaling_active
;
75 static Notifier mouse_mode_notifier
;
77 static void sdl_update_caption(struct sdl2_state
*scon
);
79 static struct sdl2_state
*get_scon_from_window(uint32_t window_id
)
82 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
83 if (sdl2_console
[i
].real_window
== SDL_GetWindowFromID(window_id
)) {
84 return &sdl2_console
[i
];
90 static void sdl_update(DisplayChangeListener
*dcl
,
91 int x
, int y
, int w
, int h
)
93 struct sdl2_state
*scon
= container_of(dcl
, struct sdl2_state
, dcl
);
95 DisplaySurface
*surf
= qemu_console_surface(dcl
->con
);
100 if (!scon
->texture
) {
109 SDL_UpdateTexture(scon
->texture
, NULL
, surface_data(surf
),
110 surface_stride(surf
));
111 SDL_RenderCopy(scon
->real_renderer
, scon
->texture
, &rect
, &rect
);
112 SDL_RenderPresent(scon
->real_renderer
);
115 static void do_sdl_resize(struct sdl2_state
*scon
, int width
, int height
,
120 if (scon
->real_window
&& scon
->real_renderer
) {
121 if (width
&& height
) {
122 SDL_RenderSetLogicalSize(scon
->real_renderer
, width
, height
);
123 SDL_SetWindowSize(scon
->real_window
, width
, height
);
125 SDL_DestroyRenderer(scon
->real_renderer
);
126 SDL_DestroyWindow(scon
->real_window
);
127 scon
->real_renderer
= NULL
;
128 scon
->real_window
= NULL
;
131 if (!width
|| !height
) {
135 if (gui_fullscreen
) {
136 flags
|= SDL_WINDOW_FULLSCREEN
;
138 flags
|= SDL_WINDOW_RESIZABLE
;
141 flags
|= SDL_WINDOW_HIDDEN
;
144 scon
->real_window
= SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED
,
145 SDL_WINDOWPOS_UNDEFINED
,
146 width
, height
, flags
);
147 scon
->real_renderer
= SDL_CreateRenderer(scon
->real_window
, -1, 0);
148 sdl_update_caption(scon
);
152 static void sdl_switch(DisplayChangeListener
*dcl
,
153 DisplaySurface
*new_surface
)
155 struct sdl2_state
*scon
= container_of(dcl
, struct sdl2_state
, dcl
);
158 DisplaySurface
*old_surface
= scon
->surface
;
160 /* temporary hack: allows to call sdl_switch to handle scaling changes */
162 scon
->surface
= new_surface
;
165 if (!new_surface
&& idx
> 0) {
166 scon
->surface
= NULL
;
169 if (new_surface
== NULL
) {
170 do_sdl_resize(scon
, 0, 0, 0);
172 do_sdl_resize(scon
, surface_width(scon
->surface
),
173 surface_height(scon
->surface
), 0);
176 if (old_surface
&& scon
->texture
) {
177 SDL_DestroyTexture(scon
->texture
);
178 scon
->texture
= NULL
;
182 if (!scon
->texture
) {
183 if (surface_bits_per_pixel(scon
->surface
) == 16) {
184 format
= SDL_PIXELFORMAT_RGB565
;
185 } else if (surface_bits_per_pixel(scon
->surface
) == 32) {
186 format
= SDL_PIXELFORMAT_ARGB8888
;
189 scon
->texture
= SDL_CreateTexture(scon
->real_renderer
, format
,
190 SDL_TEXTUREACCESS_STREAMING
,
191 surface_width(new_surface
),
192 surface_height(new_surface
));
197 static void reset_keys(struct sdl2_state
*scon
)
199 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
202 for (i
= 0; i
< 256; i
++) {
203 if (modifiers_state
[i
]) {
204 int qcode
= sdl2_scancode_to_qcode
[i
];
205 qemu_input_event_send_key_qcode(con
, qcode
, false);
206 modifiers_state
[i
] = 0;
211 static void sdl_process_key(struct sdl2_state
*scon
,
212 SDL_KeyboardEvent
*ev
)
214 int qcode
= sdl2_scancode_to_qcode
[ev
->keysym
.scancode
];
215 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
217 if (!qemu_console_is_graphic(con
)) {
218 if (ev
->type
== SDL_KEYDOWN
) {
219 switch (ev
->keysym
.scancode
) {
220 case SDL_SCANCODE_RETURN
:
221 kbd_put_keysym_console(con
, '\n');
223 case SDL_SCANCODE_BACKSPACE
:
224 kbd_put_keysym_console(con
, QEMU_KEY_BACKSPACE
);
227 kbd_put_qcode_console(con
, qcode
);
234 switch (ev
->keysym
.scancode
) {
236 case SDL_SCANCODE_NUMLOCKCLEAR
:
237 case SDL_SCANCODE_CAPSLOCK
:
238 /* SDL does not send the key up event, so we generate it */
239 qemu_input_event_send_key_qcode(con
, qcode
, true);
240 qemu_input_event_send_key_qcode(con
, qcode
, false);
243 case SDL_SCANCODE_LCTRL
:
244 case SDL_SCANCODE_LSHIFT
:
245 case SDL_SCANCODE_LALT
:
246 case SDL_SCANCODE_LGUI
:
247 case SDL_SCANCODE_RCTRL
:
248 case SDL_SCANCODE_RSHIFT
:
249 case SDL_SCANCODE_RALT
:
250 case SDL_SCANCODE_RGUI
:
251 if (ev
->type
== SDL_KEYUP
) {
252 modifiers_state
[ev
->keysym
.scancode
] = 0;
254 modifiers_state
[ev
->keysym
.scancode
] = 1;
258 qemu_input_event_send_key_qcode(con
, qcode
,
259 ev
->type
== SDL_KEYDOWN
);
263 static void sdl_update_caption(struct sdl2_state
*scon
)
265 char win_title
[1024];
266 char icon_title
[1024];
267 const char *status
= "";
269 if (!runstate_is_running()) {
270 status
= " [Stopped]";
271 } else if (gui_grab
) {
273 status
= " - Press Ctrl-Alt-Shift to exit mouse grab";
274 } else if (ctrl_grab
) {
275 status
= " - Press Right-Ctrl to exit mouse grab";
277 status
= " - Press Ctrl-Alt to exit mouse grab";
282 snprintf(win_title
, sizeof(win_title
), "QEMU (%s-%d)%s", qemu_name
,
284 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
286 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
287 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
290 if (scon
->real_window
) {
291 SDL_SetWindowTitle(scon
->real_window
, win_title
);
295 static void sdl_hide_cursor(void)
301 if (qemu_input_is_absolute()) {
303 SDL_SetCursor(sdl_cursor_hidden
);
305 SDL_SetRelativeMouseMode(SDL_TRUE
);
309 static void sdl_show_cursor(void)
315 if (!qemu_input_is_absolute()) {
316 SDL_SetRelativeMouseMode(SDL_FALSE
);
319 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
320 SDL_SetCursor(guest_sprite
);
322 SDL_SetCursor(sdl_cursor_normal
);
327 static void sdl_grab_start(struct sdl2_state
*scon
)
329 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
331 if (!con
|| !qemu_console_is_graphic(con
)) {
335 * If the application is not active, do not try to enter grab state. This
336 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
337 * application (SDL bug).
339 if (!(SDL_GetWindowFlags(scon
->real_window
) & SDL_WINDOW_INPUT_FOCUS
)) {
343 SDL_SetCursor(guest_sprite
);
344 if (!qemu_input_is_absolute() && !absolute_enabled
) {
345 SDL_WarpMouseInWindow(scon
->real_window
, guest_x
, guest_y
);
350 SDL_SetWindowGrab(scon
->real_window
, SDL_TRUE
);
352 sdl_update_caption(scon
);
355 static void sdl_grab_end(struct sdl2_state
*scon
)
357 SDL_SetWindowGrab(scon
->real_window
, SDL_FALSE
);
360 sdl_update_caption(scon
);
363 static void absolute_mouse_grab(struct sdl2_state
*scon
)
365 int mouse_x
, mouse_y
;
367 SDL_GetMouseState(&mouse_x
, &mouse_y
);
368 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
369 if (mouse_x
> 0 && mouse_x
< scr_w
- 1 &&
370 mouse_y
> 0 && mouse_y
< scr_h
- 1) {
371 sdl_grab_start(scon
);
375 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
377 if (qemu_input_is_absolute()) {
378 if (!absolute_enabled
) {
379 absolute_enabled
= 1;
380 absolute_mouse_grab(&sdl2_console
[0]);
382 } else if (absolute_enabled
) {
383 if (!gui_fullscreen
) {
384 sdl_grab_end(&sdl2_console
[0]);
386 absolute_enabled
= 0;
390 static void sdl_send_mouse_event(struct sdl2_state
*scon
, int dx
, int dy
,
391 int x
, int y
, int state
)
393 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
394 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
395 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
396 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
398 static uint32_t prev_state
;
400 if (prev_state
!= state
) {
401 qemu_input_update_buttons(scon
->dcl
.con
, bmap
, prev_state
, state
);
405 if (qemu_input_is_absolute()) {
407 int max_w
= 0, max_h
= 0;
408 int off_x
= 0, off_y
= 0;
409 int cur_off_x
= 0, cur_off_y
= 0;
412 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
413 struct sdl2_state
*thiscon
= &sdl2_console
[i
];
414 if (thiscon
->real_window
&& thiscon
->surface
) {
415 SDL_GetWindowSize(thiscon
->real_window
, &scr_w
, &scr_h
);
416 cur_off_x
= thiscon
->x
;
417 cur_off_y
= thiscon
->y
;
418 if (scr_w
+ cur_off_x
> max_w
) {
419 max_w
= scr_w
+ cur_off_x
;
421 if (scr_h
+ cur_off_y
> max_h
) {
422 max_h
= scr_h
+ cur_off_y
;
424 if (i
== scon
->idx
) {
430 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_X
, off_x
+ x
, max_w
);
431 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_Y
, off_y
+ y
, max_h
);
441 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_X
, dx
);
442 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_Y
, dy
);
444 qemu_input_event_sync();
447 static void sdl_scale(struct sdl2_state
*scon
, int width
, int height
)
450 do_sdl_resize(scon
, width
, height
, bpp
);
454 static void toggle_full_screen(struct sdl2_state
*scon
)
456 int width
= surface_width(scon
->surface
);
457 int height
= surface_height(scon
->surface
);
458 int bpp
= surface_bits_per_pixel(scon
->surface
);
460 gui_fullscreen
= !gui_fullscreen
;
461 if (gui_fullscreen
) {
462 SDL_GetWindowSize(scon
->real_window
,
463 &gui_saved_width
, &gui_saved_height
);
464 gui_saved_scaling
= scaling_active
;
466 do_sdl_resize(scon
, width
, height
, bpp
);
469 gui_saved_grab
= gui_grab
;
470 sdl_grab_start(scon
);
472 if (gui_saved_scaling
) {
473 sdl_scale(scon
, gui_saved_width
, gui_saved_height
);
475 do_sdl_resize(scon
, width
, height
, 0);
477 if (!gui_saved_grab
) {
481 graphic_hw_invalidate(scon
->dcl
.con
);
482 graphic_hw_update(scon
->dcl
.con
);
485 static void handle_keydown(SDL_Event
*ev
)
488 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
491 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
492 (gui_grab_code
| KMOD_LSHIFT
);
493 } else if (ctrl_grab
) {
494 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
496 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
498 gui_key_modifier_pressed
= mod_state
;
500 if (gui_key_modifier_pressed
) {
501 switch (ev
->key
.keysym
.scancode
) {
510 win
= ev
->key
.keysym
.scancode
- SDL_SCANCODE_1
;
511 if (win
< sdl2_num_outputs
) {
512 sdl2_console
[win
].hidden
= !sdl2_console
[win
].hidden
;
513 if (sdl2_console
[win
].real_window
) {
514 if (sdl2_console
[win
].hidden
) {
515 SDL_HideWindow(sdl2_console
[win
].real_window
);
517 SDL_ShowWindow(sdl2_console
[win
].real_window
);
524 toggle_full_screen(scon
);
528 if (scaling_active
) {
530 sdl_switch(&scon
->dcl
, NULL
);
531 graphic_hw_invalidate(scon
->dcl
.con
);
532 graphic_hw_update(scon
->dcl
.con
);
536 case SDL_SCANCODE_KP_PLUS
:
537 case SDL_SCANCODE_KP_MINUS
:
538 if (!gui_fullscreen
) {
541 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
543 width
= MAX(scr_w
+ (ev
->key
.keysym
.scancode
==
544 SDL_SCANCODE_KP_PLUS
? 50 : -50),
546 height
= (surface_height(scon
->surface
) * width
) /
547 surface_width(scon
->surface
);
549 sdl_scale(scon
, width
, height
);
550 graphic_hw_invalidate(NULL
);
551 graphic_hw_update(NULL
);
559 sdl_process_key(scon
, &ev
->key
);
563 static void handle_keyup(SDL_Event
*ev
)
566 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
569 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
571 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
573 if (!mod_state
&& gui_key_modifier_pressed
) {
574 gui_key_modifier_pressed
= 0;
575 if (gui_keysym
== 0) {
576 /* exit/enter grab if pressing Ctrl-Alt */
578 sdl_grab_start(scon
);
579 } else if (!gui_fullscreen
) {
582 /* SDL does not send back all the modifiers key, so we must
590 sdl_process_key(scon
, &ev
->key
);
594 static void handle_textinput(SDL_Event
*ev
)
596 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
597 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
599 if (qemu_console_is_graphic(con
)) {
602 kbd_put_string_console(con
, ev
->text
.text
, strlen(ev
->text
.text
));
605 static void handle_mousemotion(SDL_Event
*ev
)
608 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
610 if (qemu_input_is_absolute() || absolute_enabled
) {
612 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
615 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
616 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
620 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
621 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
622 sdl_grab_start(scon
);
625 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
626 sdl_send_mouse_event(scon
, ev
->motion
.xrel
, ev
->motion
.yrel
,
627 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
631 static void handle_mousebutton(SDL_Event
*ev
)
633 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
634 SDL_MouseButtonEvent
*bev
;
635 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
638 if (!gui_grab
&& !qemu_input_is_absolute()) {
639 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
640 /* start grabbing all events */
641 sdl_grab_start(scon
);
644 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
645 buttonstate
|= SDL_BUTTON(bev
->button
);
647 buttonstate
&= ~SDL_BUTTON(bev
->button
);
649 sdl_send_mouse_event(scon
, 0, 0, bev
->x
, bev
->y
, buttonstate
);
653 static void handle_mousewheel(SDL_Event
*ev
)
655 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
656 SDL_MouseWheelEvent
*wev
= &ev
->wheel
;
660 btn
= INPUT_BUTTON_WHEEL_UP
;
661 } else if (wev
->y
< 0) {
662 btn
= INPUT_BUTTON_WHEEL_DOWN
;
667 qemu_input_queue_btn(scon
->dcl
.con
, btn
, true);
668 qemu_input_event_sync();
669 qemu_input_queue_btn(scon
->dcl
.con
, btn
, false);
670 qemu_input_event_sync();
673 static void handle_windowevent(DisplayChangeListener
*dcl
, SDL_Event
*ev
)
676 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
678 switch (ev
->window
.event
) {
679 case SDL_WINDOWEVENT_RESIZED
:
680 sdl_scale(scon
, ev
->window
.data1
, ev
->window
.data2
);
683 memset(&info
, 0, sizeof(info
));
684 info
.width
= ev
->window
.data1
;
685 info
.height
= ev
->window
.data2
;
686 dpy_set_ui_info(scon
->dcl
.con
, &info
);
688 graphic_hw_invalidate(scon
->dcl
.con
);
689 graphic_hw_update(scon
->dcl
.con
);
691 case SDL_WINDOWEVENT_EXPOSED
:
692 SDL_GetWindowSize(SDL_GetWindowFromID(ev
->window
.windowID
), &w
, &h
);
693 sdl_update(dcl
, 0, 0, w
, h
);
695 case SDL_WINDOWEVENT_FOCUS_GAINED
:
696 case SDL_WINDOWEVENT_ENTER
:
697 if (!gui_grab
&& (qemu_input_is_absolute() || absolute_enabled
)) {
698 absolute_mouse_grab(scon
);
701 case SDL_WINDOWEVENT_FOCUS_LOST
:
702 if (gui_grab
&& !gui_fullscreen
) {
706 case SDL_WINDOWEVENT_RESTORED
:
707 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
709 case SDL_WINDOWEVENT_MINIMIZED
:
710 update_displaychangelistener(dcl
, 500);
712 case SDL_WINDOWEVENT_CLOSE
:
715 qemu_system_shutdown_request();
721 static void sdl_refresh(DisplayChangeListener
*dcl
)
723 struct sdl2_state
*scon
= container_of(dcl
, struct sdl2_state
, dcl
);
724 SDL_Event ev1
, *ev
= &ev1
;
726 if (scon
->last_vm_running
!= runstate_is_running()) {
727 scon
->last_vm_running
= runstate_is_running();
728 sdl_update_caption(scon
);
731 graphic_hw_update(dcl
->con
);
733 while (SDL_PollEvent(ev
)) {
742 handle_textinput(ev
);
747 qemu_system_shutdown_request();
750 case SDL_MOUSEMOTION
:
751 handle_mousemotion(ev
);
753 case SDL_MOUSEBUTTONDOWN
:
754 case SDL_MOUSEBUTTONUP
:
755 handle_mousebutton(ev
);
758 handle_mousewheel(ev
);
760 case SDL_WINDOWEVENT
:
761 handle_windowevent(dcl
, ev
);
769 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
770 int x
, int y
, int on
)
772 struct sdl2_state
*scon
= container_of(dcl
, struct sdl2_state
, dcl
);
777 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
778 SDL_SetCursor(guest_sprite
);
779 if (!qemu_input_is_absolute() && !absolute_enabled
) {
780 SDL_WarpMouseInWindow(scon
->real_window
, x
, y
);
783 } else if (gui_grab
) {
787 guest_x
= x
, guest_y
= y
;
790 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
795 SDL_FreeCursor(guest_sprite
);
798 if (guest_sprite_surface
) {
799 SDL_FreeSurface(guest_sprite_surface
);
802 guest_sprite_surface
=
803 SDL_CreateRGBSurfaceFrom(c
->data
, c
->width
, c
->height
, 32, c
->width
* 4,
804 0xff0000, 0x00ff00, 0xff, 0xff000000);
806 if (!guest_sprite_surface
) {
807 fprintf(stderr
, "Failed to make rgb surface from %p\n", c
);
810 guest_sprite
= SDL_CreateColorCursor(guest_sprite_surface
,
813 fprintf(stderr
, "Failed to make color cursor from %p\n", c
);
817 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
818 SDL_SetCursor(guest_sprite
);
822 static void sdl_cleanup(void)
825 SDL_FreeCursor(guest_sprite
);
827 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
830 static const DisplayChangeListenerOps dcl_ops
= {
832 .dpy_gfx_update
= sdl_update
,
833 .dpy_gfx_switch
= sdl_switch
,
834 .dpy_refresh
= sdl_refresh
,
835 .dpy_mouse_set
= sdl_mouse_warp
,
836 .dpy_cursor_define
= sdl_mouse_define
,
839 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
851 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
852 * accessible $DISPLAY to open X11 window. This is often the case
853 * when qemu is run using sudo. But in this case, and when actually
854 * run in X11 environment, SDL fights with X11 for the video card,
855 * making current display unavailable, often until reboot.
856 * So make x11 the default SDL video driver if this variable is unset.
857 * This is a bit hackish but saves us from bigger problem.
858 * Maybe it's a good idea to fix this in SDL instead.
860 setenv("SDL_VIDEODRIVER", "x11", 0);
863 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
864 if (SDL_Init(flags
)) {
865 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
871 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
876 sdl2_num_outputs
= i
;
877 sdl2_console
= g_new0(struct sdl2_state
, sdl2_num_outputs
);
878 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
879 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
880 if (!qemu_console_is_graphic(con
)) {
881 sdl2_console
[i
].hidden
= true;
883 sdl2_console
[i
].dcl
.ops
= &dcl_ops
;
884 sdl2_console
[i
].dcl
.con
= con
;
885 register_displaychangelistener(&sdl2_console
[i
].dcl
);
886 sdl2_console
[i
].idx
= i
;
889 /* Load a 32x32x4 image. White pixels are transparent. */
890 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
892 SDL_Surface
*image
= SDL_LoadBMP(filename
);
894 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
895 SDL_SetColorKey(image
, SDL_TRUE
, colorkey
);
896 SDL_SetWindowIcon(sdl2_console
[0].real_window
, image
);
906 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
907 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
911 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
912 sdl_cursor_normal
= SDL_GetCursor();