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
30 #include <SDL_syswm.h>
32 #include "qemu-common.h"
33 #include "ui/console.h"
35 #include "sysemu/sysemu.h"
37 #include "sdl2-keymap.h"
39 static int sdl2_num_outputs
;
40 static struct sdl2_state
{
41 DisplayChangeListener dcl
;
42 DisplaySurface
*surface
;
44 SDL_Window
*real_window
;
45 SDL_Renderer
*real_renderer
;
47 int last_vm_running
; /* per console for caption reasons */
52 static SDL_Surface
*guest_sprite_surface
;
53 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
55 static bool gui_saved_scaling
;
56 static int gui_saved_width
;
57 static int gui_saved_height
;
58 static int gui_saved_grab
;
59 static int gui_fullscreen
;
60 static int gui_noframe
;
61 static int gui_key_modifier_pressed
;
62 static int gui_keysym
;
63 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
64 static uint8_t modifiers_state
[SDL_NUM_SCANCODES
];
65 static SDL_Cursor
*sdl_cursor_normal
;
66 static SDL_Cursor
*sdl_cursor_hidden
;
67 static int absolute_enabled
;
68 static int guest_cursor
;
69 static int guest_x
, guest_y
;
70 static SDL_Cursor
*guest_sprite
;
71 static int scaling_active
;
72 static Notifier mouse_mode_notifier
;
74 static void sdl_update_caption(struct sdl2_state
*scon
);
76 static struct sdl2_state
*get_scon_from_window(uint32_t window_id
)
79 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
80 if (sdl2_console
[i
].real_window
== SDL_GetWindowFromID(window_id
)) {
81 return &sdl2_console
[i
];
87 static void sdl_update(DisplayChangeListener
*dcl
,
88 int x
, int y
, int w
, int h
)
90 struct sdl2_state
*scon
= container_of(dcl
, struct sdl2_state
, dcl
);
92 DisplaySurface
*surf
= qemu_console_surface(dcl
->con
);
106 SDL_UpdateTexture(scon
->texture
, NULL
, surface_data(surf
),
107 surface_stride(surf
));
108 SDL_RenderCopy(scon
->real_renderer
, scon
->texture
, &rect
, &rect
);
109 SDL_RenderPresent(scon
->real_renderer
);
112 static void do_sdl_resize(struct sdl2_state
*scon
, int width
, int height
,
117 if (scon
->real_window
&& scon
->real_renderer
) {
118 if (width
&& height
) {
119 SDL_RenderSetLogicalSize(scon
->real_renderer
, width
, height
);
120 SDL_SetWindowSize(scon
->real_window
, width
, height
);
122 SDL_DestroyRenderer(scon
->real_renderer
);
123 SDL_DestroyWindow(scon
->real_window
);
124 scon
->real_renderer
= NULL
;
125 scon
->real_window
= NULL
;
128 if (!width
|| !height
) {
132 if (gui_fullscreen
) {
133 flags
|= SDL_WINDOW_FULLSCREEN
;
135 flags
|= SDL_WINDOW_RESIZABLE
;
138 flags
|= SDL_WINDOW_HIDDEN
;
141 scon
->real_window
= SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED
,
142 SDL_WINDOWPOS_UNDEFINED
,
143 width
, height
, flags
);
144 scon
->real_renderer
= SDL_CreateRenderer(scon
->real_window
, -1, 0);
145 sdl_update_caption(scon
);
149 static void sdl_switch(DisplayChangeListener
*dcl
,
150 DisplaySurface
*new_surface
)
152 struct sdl2_state
*scon
= container_of(dcl
, struct sdl2_state
, dcl
);
155 DisplaySurface
*old_surface
= scon
->surface
;
157 /* temporary hack: allows to call sdl_switch to handle scaling changes */
159 scon
->surface
= new_surface
;
162 if (!new_surface
&& idx
> 0) {
163 scon
->surface
= NULL
;
166 if (new_surface
== NULL
) {
167 do_sdl_resize(scon
, 0, 0, 0);
169 do_sdl_resize(scon
, surface_width(scon
->surface
),
170 surface_height(scon
->surface
), 0);
173 if (old_surface
&& scon
->texture
) {
174 SDL_DestroyTexture(scon
->texture
);
175 scon
->texture
= NULL
;
179 if (!scon
->texture
) {
180 if (surface_bits_per_pixel(scon
->surface
) == 16) {
181 format
= SDL_PIXELFORMAT_RGB565
;
182 } else if (surface_bits_per_pixel(scon
->surface
) == 32) {
183 format
= SDL_PIXELFORMAT_ARGB8888
;
186 scon
->texture
= SDL_CreateTexture(scon
->real_renderer
, format
,
187 SDL_TEXTUREACCESS_STREAMING
,
188 surface_width(new_surface
),
189 surface_height(new_surface
));
194 static void reset_keys(struct sdl2_state
*scon
)
196 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
199 for (i
= 0; i
< 256; i
++) {
200 if (modifiers_state
[i
]) {
201 int qcode
= sdl2_scancode_to_qcode
[i
];
202 qemu_input_event_send_key_qcode(con
, qcode
, false);
203 modifiers_state
[i
] = 0;
208 static void sdl_process_key(struct sdl2_state
*scon
,
209 SDL_KeyboardEvent
*ev
)
211 int qcode
= sdl2_scancode_to_qcode
[ev
->keysym
.scancode
];
212 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
214 if (!qemu_console_is_graphic(con
)) {
215 if (ev
->type
== SDL_KEYDOWN
) {
216 switch (ev
->keysym
.scancode
) {
217 case SDL_SCANCODE_RETURN
:
218 kbd_put_keysym_console(con
, '\n');
220 case SDL_SCANCODE_BACKSPACE
:
221 kbd_put_keysym_console(con
, QEMU_KEY_BACKSPACE
);
224 kbd_put_qcode_console(con
, qcode
);
231 switch (ev
->keysym
.scancode
) {
233 case SDL_SCANCODE_NUMLOCKCLEAR
:
234 case SDL_SCANCODE_CAPSLOCK
:
235 /* SDL does not send the key up event, so we generate it */
236 qemu_input_event_send_key_qcode(con
, qcode
, true);
237 qemu_input_event_send_key_qcode(con
, qcode
, false);
240 case SDL_SCANCODE_LCTRL
:
241 case SDL_SCANCODE_LSHIFT
:
242 case SDL_SCANCODE_LALT
:
243 case SDL_SCANCODE_LGUI
:
244 case SDL_SCANCODE_RCTRL
:
245 case SDL_SCANCODE_RSHIFT
:
246 case SDL_SCANCODE_RALT
:
247 case SDL_SCANCODE_RGUI
:
248 if (ev
->type
== SDL_KEYUP
) {
249 modifiers_state
[ev
->keysym
.scancode
] = 0;
251 modifiers_state
[ev
->keysym
.scancode
] = 1;
255 qemu_input_event_send_key_qcode(con
, qcode
,
256 ev
->type
== SDL_KEYDOWN
);
260 static void sdl_update_caption(struct sdl2_state
*scon
)
262 char win_title
[1024];
263 char icon_title
[1024];
264 const char *status
= "";
266 if (!runstate_is_running()) {
267 status
= " [Stopped]";
268 } else if (gui_grab
) {
270 status
= " - Press Ctrl-Alt-Shift to exit mouse grab";
271 } else if (ctrl_grab
) {
272 status
= " - Press Right-Ctrl to exit mouse grab";
274 status
= " - Press Ctrl-Alt to exit mouse grab";
279 snprintf(win_title
, sizeof(win_title
), "QEMU (%s-%d)%s", qemu_name
,
281 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
283 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
284 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
287 if (scon
->real_window
) {
288 SDL_SetWindowTitle(scon
->real_window
, win_title
);
292 static void sdl_hide_cursor(void)
298 if (qemu_input_is_absolute()) {
300 SDL_SetCursor(sdl_cursor_hidden
);
302 SDL_SetRelativeMouseMode(SDL_TRUE
);
306 static void sdl_show_cursor(void)
312 if (!qemu_input_is_absolute()) {
313 SDL_SetRelativeMouseMode(SDL_FALSE
);
316 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
317 SDL_SetCursor(guest_sprite
);
319 SDL_SetCursor(sdl_cursor_normal
);
324 static void sdl_grab_start(struct sdl2_state
*scon
)
326 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
328 if (!con
|| !qemu_console_is_graphic(con
)) {
332 * If the application is not active, do not try to enter grab state. This
333 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
334 * application (SDL bug).
336 if (!(SDL_GetWindowFlags(scon
->real_window
) & SDL_WINDOW_INPUT_FOCUS
)) {
340 SDL_SetCursor(guest_sprite
);
341 if (!qemu_input_is_absolute() && !absolute_enabled
) {
342 SDL_WarpMouseInWindow(scon
->real_window
, guest_x
, guest_y
);
347 SDL_SetWindowGrab(scon
->real_window
, SDL_TRUE
);
349 sdl_update_caption(scon
);
352 static void sdl_grab_end(struct sdl2_state
*scon
)
354 SDL_SetWindowGrab(scon
->real_window
, SDL_FALSE
);
357 sdl_update_caption(scon
);
360 static void absolute_mouse_grab(struct sdl2_state
*scon
)
362 int mouse_x
, mouse_y
;
364 SDL_GetMouseState(&mouse_x
, &mouse_y
);
365 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
366 if (mouse_x
> 0 && mouse_x
< scr_w
- 1 &&
367 mouse_y
> 0 && mouse_y
< scr_h
- 1) {
368 sdl_grab_start(scon
);
372 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
374 if (qemu_input_is_absolute()) {
375 if (!absolute_enabled
) {
376 absolute_enabled
= 1;
377 absolute_mouse_grab(&sdl2_console
[0]);
379 } else if (absolute_enabled
) {
380 if (!gui_fullscreen
) {
381 sdl_grab_end(&sdl2_console
[0]);
383 absolute_enabled
= 0;
387 static void sdl_send_mouse_event(struct sdl2_state
*scon
, int dx
, int dy
,
388 int x
, int y
, int state
)
390 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
391 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
392 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
393 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
395 static uint32_t prev_state
;
397 if (prev_state
!= state
) {
398 qemu_input_update_buttons(scon
->dcl
.con
, bmap
, prev_state
, state
);
402 if (qemu_input_is_absolute()) {
404 int max_w
= 0, max_h
= 0;
405 int off_x
= 0, off_y
= 0;
406 int cur_off_x
= 0, cur_off_y
= 0;
409 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
410 struct sdl2_state
*thiscon
= &sdl2_console
[i
];
411 if (thiscon
->real_window
&& thiscon
->surface
) {
412 SDL_GetWindowSize(thiscon
->real_window
, &scr_w
, &scr_h
);
413 cur_off_x
= thiscon
->x
;
414 cur_off_y
= thiscon
->y
;
415 if (scr_w
+ cur_off_x
> max_w
) {
416 max_w
= scr_w
+ cur_off_x
;
418 if (scr_h
+ cur_off_y
> max_h
) {
419 max_h
= scr_h
+ cur_off_y
;
421 if (i
== scon
->idx
) {
427 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_X
, off_x
+ x
, max_w
);
428 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_Y
, off_y
+ y
, max_h
);
438 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_X
, dx
);
439 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_Y
, dy
);
441 qemu_input_event_sync();
444 static void sdl_scale(struct sdl2_state
*scon
, int width
, int height
)
447 do_sdl_resize(scon
, width
, height
, bpp
);
451 static void toggle_full_screen(struct sdl2_state
*scon
)
453 int width
= surface_width(scon
->surface
);
454 int height
= surface_height(scon
->surface
);
455 int bpp
= surface_bits_per_pixel(scon
->surface
);
457 gui_fullscreen
= !gui_fullscreen
;
458 if (gui_fullscreen
) {
459 SDL_GetWindowSize(scon
->real_window
,
460 &gui_saved_width
, &gui_saved_height
);
461 gui_saved_scaling
= scaling_active
;
463 do_sdl_resize(scon
, width
, height
, bpp
);
466 gui_saved_grab
= gui_grab
;
467 sdl_grab_start(scon
);
469 if (gui_saved_scaling
) {
470 sdl_scale(scon
, gui_saved_width
, gui_saved_height
);
472 do_sdl_resize(scon
, width
, height
, 0);
474 if (!gui_saved_grab
) {
478 graphic_hw_invalidate(scon
->dcl
.con
);
479 graphic_hw_update(scon
->dcl
.con
);
482 static void handle_keydown(SDL_Event
*ev
)
485 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
488 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
489 (gui_grab_code
| KMOD_LSHIFT
);
490 } else if (ctrl_grab
) {
491 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
493 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
495 gui_key_modifier_pressed
= mod_state
;
497 if (gui_key_modifier_pressed
) {
498 switch (ev
->key
.keysym
.scancode
) {
507 win
= ev
->key
.keysym
.scancode
- SDL_SCANCODE_1
;
508 if (win
< sdl2_num_outputs
) {
509 sdl2_console
[win
].hidden
= !sdl2_console
[win
].hidden
;
510 if (sdl2_console
[win
].real_window
) {
511 if (sdl2_console
[win
].hidden
) {
512 SDL_HideWindow(sdl2_console
[win
].real_window
);
514 SDL_ShowWindow(sdl2_console
[win
].real_window
);
521 toggle_full_screen(scon
);
525 if (scaling_active
) {
527 sdl_switch(&scon
->dcl
, NULL
);
528 graphic_hw_invalidate(scon
->dcl
.con
);
529 graphic_hw_update(scon
->dcl
.con
);
533 case SDL_SCANCODE_KP_PLUS
:
534 case SDL_SCANCODE_KP_MINUS
:
535 if (!gui_fullscreen
) {
538 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
540 width
= MAX(scr_w
+ (ev
->key
.keysym
.scancode
==
541 SDL_SCANCODE_KP_PLUS
? 50 : -50),
543 height
= (surface_height(scon
->surface
) * width
) /
544 surface_width(scon
->surface
);
546 sdl_scale(scon
, width
, height
);
547 graphic_hw_invalidate(NULL
);
548 graphic_hw_update(NULL
);
556 sdl_process_key(scon
, &ev
->key
);
560 static void handle_keyup(SDL_Event
*ev
)
563 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
566 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
568 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
570 if (!mod_state
&& gui_key_modifier_pressed
) {
571 gui_key_modifier_pressed
= 0;
572 if (gui_keysym
== 0) {
573 /* exit/enter grab if pressing Ctrl-Alt */
575 sdl_grab_start(scon
);
576 } else if (!gui_fullscreen
) {
579 /* SDL does not send back all the modifiers key, so we must
587 sdl_process_key(scon
, &ev
->key
);
591 static void handle_textinput(SDL_Event
*ev
)
593 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
594 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
596 if (qemu_console_is_graphic(con
)) {
599 kbd_put_string_console(con
, ev
->text
.text
, strlen(ev
->text
.text
));
602 static void handle_mousemotion(SDL_Event
*ev
)
605 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
607 if (qemu_input_is_absolute() || absolute_enabled
) {
609 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
612 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
613 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
617 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
618 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
619 sdl_grab_start(scon
);
622 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
623 sdl_send_mouse_event(scon
, ev
->motion
.xrel
, ev
->motion
.yrel
,
624 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
628 static void handle_mousebutton(SDL_Event
*ev
)
630 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
631 SDL_MouseButtonEvent
*bev
;
632 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
635 if (!gui_grab
&& !qemu_input_is_absolute()) {
636 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
637 /* start grabbing all events */
638 sdl_grab_start(scon
);
641 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
642 buttonstate
|= SDL_BUTTON(bev
->button
);
644 buttonstate
&= ~SDL_BUTTON(bev
->button
);
646 sdl_send_mouse_event(scon
, 0, 0, bev
->x
, bev
->y
, buttonstate
);
650 static void handle_mousewheel(SDL_Event
*ev
)
652 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
653 SDL_MouseWheelEvent
*wev
= &ev
->wheel
;
657 btn
= INPUT_BUTTON_WHEEL_UP
;
658 } else if (wev
->y
< 0) {
659 btn
= INPUT_BUTTON_WHEEL_DOWN
;
664 qemu_input_queue_btn(scon
->dcl
.con
, btn
, true);
665 qemu_input_event_sync();
666 qemu_input_queue_btn(scon
->dcl
.con
, btn
, false);
667 qemu_input_event_sync();
670 static void handle_windowevent(DisplayChangeListener
*dcl
, SDL_Event
*ev
)
673 struct sdl2_state
*scon
= get_scon_from_window(ev
->key
.windowID
);
675 switch (ev
->window
.event
) {
676 case SDL_WINDOWEVENT_RESIZED
:
677 sdl_scale(scon
, ev
->window
.data1
, ev
->window
.data2
);
680 memset(&info
, 0, sizeof(info
));
681 info
.width
= ev
->window
.data1
;
682 info
.height
= ev
->window
.data2
;
683 dpy_set_ui_info(scon
->dcl
.con
, &info
);
685 graphic_hw_invalidate(scon
->dcl
.con
);
686 graphic_hw_update(scon
->dcl
.con
);
688 case SDL_WINDOWEVENT_EXPOSED
:
689 SDL_GetWindowSize(SDL_GetWindowFromID(ev
->window
.windowID
), &w
, &h
);
690 sdl_update(dcl
, 0, 0, w
, h
);
692 case SDL_WINDOWEVENT_FOCUS_GAINED
:
693 case SDL_WINDOWEVENT_ENTER
:
694 if (!gui_grab
&& (qemu_input_is_absolute() || absolute_enabled
)) {
695 absolute_mouse_grab(scon
);
698 case SDL_WINDOWEVENT_FOCUS_LOST
:
699 if (gui_grab
&& !gui_fullscreen
) {
703 case SDL_WINDOWEVENT_RESTORED
:
704 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
706 case SDL_WINDOWEVENT_MINIMIZED
:
707 update_displaychangelistener(dcl
, 500);
709 case SDL_WINDOWEVENT_CLOSE
:
712 qemu_system_shutdown_request();
718 static void sdl_refresh(DisplayChangeListener
*dcl
)
720 struct sdl2_state
*scon
= container_of(dcl
, struct sdl2_state
, dcl
);
721 SDL_Event ev1
, *ev
= &ev1
;
723 if (scon
->last_vm_running
!= runstate_is_running()) {
724 scon
->last_vm_running
= runstate_is_running();
725 sdl_update_caption(scon
);
728 graphic_hw_update(dcl
->con
);
730 while (SDL_PollEvent(ev
)) {
739 handle_textinput(ev
);
744 qemu_system_shutdown_request();
747 case SDL_MOUSEMOTION
:
748 handle_mousemotion(ev
);
750 case SDL_MOUSEBUTTONDOWN
:
751 case SDL_MOUSEBUTTONUP
:
752 handle_mousebutton(ev
);
755 handle_mousewheel(ev
);
757 case SDL_WINDOWEVENT
:
758 handle_windowevent(dcl
, ev
);
766 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
767 int x
, int y
, int on
)
769 struct sdl2_state
*scon
= container_of(dcl
, struct sdl2_state
, dcl
);
774 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
775 SDL_SetCursor(guest_sprite
);
776 if (!qemu_input_is_absolute() && !absolute_enabled
) {
777 SDL_WarpMouseInWindow(scon
->real_window
, x
, y
);
780 } else if (gui_grab
) {
784 guest_x
= x
, guest_y
= y
;
787 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
792 SDL_FreeCursor(guest_sprite
);
795 if (guest_sprite_surface
) {
796 SDL_FreeSurface(guest_sprite_surface
);
799 guest_sprite_surface
=
800 SDL_CreateRGBSurfaceFrom(c
->data
, c
->width
, c
->height
, 32, c
->width
* 4,
801 0xff0000, 0x00ff00, 0xff, 0xff000000);
803 if (!guest_sprite_surface
) {
804 fprintf(stderr
, "Failed to make rgb surface from %p\n", c
);
807 guest_sprite
= SDL_CreateColorCursor(guest_sprite_surface
,
810 fprintf(stderr
, "Failed to make color cursor from %p\n", c
);
814 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
815 SDL_SetCursor(guest_sprite
);
819 static void sdl_cleanup(void)
822 SDL_FreeCursor(guest_sprite
);
824 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
827 static const DisplayChangeListenerOps dcl_ops
= {
829 .dpy_gfx_update
= sdl_update
,
830 .dpy_gfx_switch
= sdl_switch
,
831 .dpy_refresh
= sdl_refresh
,
832 .dpy_mouse_set
= sdl_mouse_warp
,
833 .dpy_cursor_define
= sdl_mouse_define
,
836 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
848 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
849 * accessible $DISPLAY to open X11 window. This is often the case
850 * when qemu is run using sudo. But in this case, and when actually
851 * run in X11 environment, SDL fights with X11 for the video card,
852 * making current display unavailable, often until reboot.
853 * So make x11 the default SDL video driver if this variable is unset.
854 * This is a bit hackish but saves us from bigger problem.
855 * Maybe it's a good idea to fix this in SDL instead.
857 setenv("SDL_VIDEODRIVER", "x11", 0);
860 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
861 if (SDL_Init(flags
)) {
862 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
868 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
873 sdl2_num_outputs
= i
;
874 sdl2_console
= g_new0(struct sdl2_state
, sdl2_num_outputs
);
875 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
876 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
877 if (!qemu_console_is_graphic(con
)) {
878 sdl2_console
[i
].hidden
= true;
880 sdl2_console
[i
].dcl
.ops
= &dcl_ops
;
881 sdl2_console
[i
].dcl
.con
= con
;
882 register_displaychangelistener(&sdl2_console
[i
].dcl
);
883 sdl2_console
[i
].idx
= i
;
886 /* Load a 32x32x4 image. White pixels are transparent. */
887 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
889 SDL_Surface
*image
= SDL_LoadBMP(filename
);
891 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
892 SDL_SetColorKey(image
, SDL_TRUE
, colorkey
);
893 SDL_SetWindowIcon(sdl2_console
[0].real_window
, image
);
903 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
904 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
908 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
909 sdl_cursor_normal
= SDL_GetCursor();