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"
36 #include "sysemu/sysemu.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 int gui_saved_grab
;
45 static int gui_fullscreen
;
46 static int gui_noframe
;
47 static int gui_key_modifier_pressed
;
48 static int gui_keysym
;
49 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
50 static SDL_Cursor
*sdl_cursor_normal
;
51 static SDL_Cursor
*sdl_cursor_hidden
;
52 static int absolute_enabled
;
53 static int guest_cursor
;
54 static int guest_x
, guest_y
;
55 static SDL_Cursor
*guest_sprite
;
56 static Notifier mouse_mode_notifier
;
58 static void sdl_update_caption(struct sdl2_console
*scon
);
60 static struct sdl2_console
*get_scon_from_window(uint32_t window_id
)
63 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
64 if (sdl2_console
[i
].real_window
== SDL_GetWindowFromID(window_id
)) {
65 return &sdl2_console
[i
];
71 void sdl2_window_create(struct sdl2_console
*scon
)
78 assert(!scon
->real_window
);
81 flags
|= SDL_WINDOW_FULLSCREEN_DESKTOP
;
83 flags
|= SDL_WINDOW_RESIZABLE
;
86 flags
|= SDL_WINDOW_HIDDEN
;
89 scon
->real_window
= SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED
,
90 SDL_WINDOWPOS_UNDEFINED
,
91 surface_width(scon
->surface
),
92 surface_height(scon
->surface
),
94 scon
->real_renderer
= SDL_CreateRenderer(scon
->real_window
, -1, 0);
95 sdl_update_caption(scon
);
98 void sdl2_window_destroy(struct sdl2_console
*scon
)
100 if (!scon
->real_window
) {
104 SDL_DestroyRenderer(scon
->real_renderer
);
105 scon
->real_renderer
= NULL
;
106 SDL_DestroyWindow(scon
->real_window
);
107 scon
->real_window
= NULL
;
110 void sdl2_window_resize(struct sdl2_console
*scon
)
112 if (!scon
->real_window
) {
116 SDL_SetWindowSize(scon
->real_window
,
117 surface_width(scon
->surface
),
118 surface_height(scon
->surface
));
121 static void sdl_update_caption(struct sdl2_console
*scon
)
123 char win_title
[1024];
124 char icon_title
[1024];
125 const char *status
= "";
127 if (!runstate_is_running()) {
128 status
= " [Stopped]";
129 } else if (gui_grab
) {
131 status
= " - Press Ctrl-Alt-Shift to exit grab";
132 } else if (ctrl_grab
) {
133 status
= " - Press Right-Ctrl to exit grab";
135 status
= " - Press Ctrl-Alt to exit grab";
140 snprintf(win_title
, sizeof(win_title
), "QEMU (%s-%d)%s", qemu_name
,
142 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
144 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
145 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
148 if (scon
->real_window
) {
149 SDL_SetWindowTitle(scon
->real_window
, win_title
);
153 static void sdl_hide_cursor(void)
159 if (qemu_input_is_absolute()) {
161 SDL_SetCursor(sdl_cursor_hidden
);
163 SDL_SetRelativeMouseMode(SDL_TRUE
);
167 static void sdl_show_cursor(void)
173 if (!qemu_input_is_absolute()) {
174 SDL_SetRelativeMouseMode(SDL_FALSE
);
177 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
178 SDL_SetCursor(guest_sprite
);
180 SDL_SetCursor(sdl_cursor_normal
);
185 static void sdl_grab_start(struct sdl2_console
*scon
)
187 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
189 if (!con
|| !qemu_console_is_graphic(con
)) {
193 * If the application is not active, do not try to enter grab state. This
194 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
195 * application (SDL bug).
197 if (!(SDL_GetWindowFlags(scon
->real_window
) & SDL_WINDOW_INPUT_FOCUS
)) {
201 SDL_SetCursor(guest_sprite
);
202 if (!qemu_input_is_absolute() && !absolute_enabled
) {
203 SDL_WarpMouseInWindow(scon
->real_window
, guest_x
, guest_y
);
208 SDL_SetWindowGrab(scon
->real_window
, SDL_TRUE
);
210 sdl_update_caption(scon
);
213 static void sdl_grab_end(struct sdl2_console
*scon
)
215 SDL_SetWindowGrab(scon
->real_window
, SDL_FALSE
);
218 sdl_update_caption(scon
);
221 static void absolute_mouse_grab(struct sdl2_console
*scon
)
223 int mouse_x
, mouse_y
;
225 SDL_GetMouseState(&mouse_x
, &mouse_y
);
226 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
227 if (mouse_x
> 0 && mouse_x
< scr_w
- 1 &&
228 mouse_y
> 0 && mouse_y
< scr_h
- 1) {
229 sdl_grab_start(scon
);
233 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
235 if (qemu_input_is_absolute()) {
236 if (!absolute_enabled
) {
237 absolute_enabled
= 1;
238 absolute_mouse_grab(&sdl2_console
[0]);
240 } else if (absolute_enabled
) {
241 if (!gui_fullscreen
) {
242 sdl_grab_end(&sdl2_console
[0]);
244 absolute_enabled
= 0;
248 static void sdl_send_mouse_event(struct sdl2_console
*scon
, int dx
, int dy
,
249 int x
, int y
, int state
)
251 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
252 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
253 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
254 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
256 static uint32_t prev_state
;
258 if (prev_state
!= state
) {
259 qemu_input_update_buttons(scon
->dcl
.con
, bmap
, prev_state
, state
);
263 if (qemu_input_is_absolute()) {
265 int max_w
= 0, max_h
= 0;
266 int off_x
= 0, off_y
= 0;
267 int cur_off_x
= 0, cur_off_y
= 0;
270 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
271 struct sdl2_console
*thiscon
= &sdl2_console
[i
];
272 if (thiscon
->real_window
&& thiscon
->surface
) {
273 SDL_GetWindowSize(thiscon
->real_window
, &scr_w
, &scr_h
);
274 cur_off_x
= thiscon
->x
;
275 cur_off_y
= thiscon
->y
;
276 if (scr_w
+ cur_off_x
> max_w
) {
277 max_w
= scr_w
+ cur_off_x
;
279 if (scr_h
+ cur_off_y
> max_h
) {
280 max_h
= scr_h
+ cur_off_y
;
282 if (i
== scon
->idx
) {
288 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_X
, off_x
+ x
, max_w
);
289 qemu_input_queue_abs(scon
->dcl
.con
, INPUT_AXIS_Y
, off_y
+ y
, max_h
);
299 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_X
, dx
);
300 qemu_input_queue_rel(scon
->dcl
.con
, INPUT_AXIS_Y
, dy
);
302 qemu_input_event_sync();
305 static void toggle_full_screen(struct sdl2_console
*scon
)
307 gui_fullscreen
= !gui_fullscreen
;
308 if (gui_fullscreen
) {
309 SDL_SetWindowFullscreen(scon
->real_window
,
310 SDL_WINDOW_FULLSCREEN_DESKTOP
);
311 gui_saved_grab
= gui_grab
;
312 sdl_grab_start(scon
);
314 if (!gui_saved_grab
) {
317 SDL_SetWindowFullscreen(scon
->real_window
, 0);
319 sdl2_2d_redraw(scon
);
322 static void handle_keydown(SDL_Event
*ev
)
325 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
328 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
329 (gui_grab_code
| KMOD_LSHIFT
);
330 } else if (ctrl_grab
) {
331 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
333 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
335 gui_key_modifier_pressed
= mod_state
;
337 if (gui_key_modifier_pressed
) {
338 switch (ev
->key
.keysym
.scancode
) {
347 win
= ev
->key
.keysym
.scancode
- SDL_SCANCODE_1
;
348 if (win
< sdl2_num_outputs
) {
349 sdl2_console
[win
].hidden
= !sdl2_console
[win
].hidden
;
350 if (sdl2_console
[win
].real_window
) {
351 if (sdl2_console
[win
].hidden
) {
352 SDL_HideWindow(sdl2_console
[win
].real_window
);
354 SDL_ShowWindow(sdl2_console
[win
].real_window
);
361 toggle_full_screen(scon
);
365 sdl2_window_destroy(scon
);
366 sdl2_window_create(scon
);
367 /* re-create texture */
368 sdl2_2d_switch(&scon
->dcl
, scon
->surface
);
372 case SDL_SCANCODE_KP_PLUS
:
373 case SDL_SCANCODE_KP_MINUS
:
374 if (!gui_fullscreen
) {
377 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
379 width
= MAX(scr_w
+ (ev
->key
.keysym
.scancode
==
380 SDL_SCANCODE_KP_PLUS
? 50 : -50),
382 height
= (surface_height(scon
->surface
) * width
) /
383 surface_width(scon
->surface
);
384 fprintf(stderr
, "%s: scale to %dx%d\n",
385 __func__
, width
, height
);
386 sdl_scale(scon
, width
, height
);
387 sdl2_2d_redraw(scon
);
396 sdl2_process_key(scon
, &ev
->key
);
400 static void handle_keyup(SDL_Event
*ev
)
403 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
406 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
408 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
410 if (!mod_state
&& gui_key_modifier_pressed
) {
411 gui_key_modifier_pressed
= 0;
412 if (gui_keysym
== 0) {
413 /* exit/enter grab if pressing Ctrl-Alt */
415 sdl_grab_start(scon
);
416 } else if (!gui_fullscreen
) {
419 /* SDL does not send back all the modifiers key, so we must
421 sdl2_reset_keys(scon
);
427 sdl2_process_key(scon
, &ev
->key
);
431 static void handle_textinput(SDL_Event
*ev
)
433 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
434 QemuConsole
*con
= scon
? scon
->dcl
.con
: NULL
;
436 if (qemu_console_is_graphic(con
)) {
439 kbd_put_string_console(con
, ev
->text
.text
, strlen(ev
->text
.text
));
442 static void handle_mousemotion(SDL_Event
*ev
)
445 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
447 if (qemu_input_is_absolute() || absolute_enabled
) {
449 SDL_GetWindowSize(scon
->real_window
, &scr_w
, &scr_h
);
452 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
453 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
457 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
458 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
459 sdl_grab_start(scon
);
462 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
463 sdl_send_mouse_event(scon
, ev
->motion
.xrel
, ev
->motion
.yrel
,
464 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
468 static void handle_mousebutton(SDL_Event
*ev
)
470 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
471 SDL_MouseButtonEvent
*bev
;
472 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
475 if (!gui_grab
&& !qemu_input_is_absolute()) {
476 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
477 /* start grabbing all events */
478 sdl_grab_start(scon
);
481 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
482 buttonstate
|= SDL_BUTTON(bev
->button
);
484 buttonstate
&= ~SDL_BUTTON(bev
->button
);
486 sdl_send_mouse_event(scon
, 0, 0, bev
->x
, bev
->y
, buttonstate
);
490 static void handle_mousewheel(SDL_Event
*ev
)
492 struct sdl2_console
*scon
= get_scon_from_window(ev
->key
.windowID
);
493 SDL_MouseWheelEvent
*wev
= &ev
->wheel
;
497 btn
= INPUT_BUTTON_WHEEL_UP
;
498 } else if (wev
->y
< 0) {
499 btn
= INPUT_BUTTON_WHEEL_DOWN
;
504 qemu_input_queue_btn(scon
->dcl
.con
, btn
, true);
505 qemu_input_event_sync();
506 qemu_input_queue_btn(scon
->dcl
.con
, btn
, false);
507 qemu_input_event_sync();
510 static void handle_windowevent(SDL_Event
*ev
)
512 struct sdl2_console
*scon
= get_scon_from_window(ev
->window
.windowID
);
514 switch (ev
->window
.event
) {
515 case SDL_WINDOWEVENT_RESIZED
:
518 memset(&info
, 0, sizeof(info
));
519 info
.width
= ev
->window
.data1
;
520 info
.height
= ev
->window
.data2
;
521 dpy_set_ui_info(scon
->dcl
.con
, &info
);
523 sdl2_2d_redraw(scon
);
525 case SDL_WINDOWEVENT_EXPOSED
:
526 sdl2_2d_redraw(scon
);
528 case SDL_WINDOWEVENT_FOCUS_GAINED
:
529 case SDL_WINDOWEVENT_ENTER
:
530 if (!gui_grab
&& (qemu_input_is_absolute() || absolute_enabled
)) {
531 absolute_mouse_grab(scon
);
534 case SDL_WINDOWEVENT_FOCUS_LOST
:
535 if (gui_grab
&& !gui_fullscreen
) {
539 case SDL_WINDOWEVENT_RESTORED
:
540 update_displaychangelistener(&scon
->dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
542 case SDL_WINDOWEVENT_MINIMIZED
:
543 update_displaychangelistener(&scon
->dcl
, 500);
545 case SDL_WINDOWEVENT_CLOSE
:
548 qemu_system_shutdown_request();
551 case SDL_WINDOWEVENT_SHOWN
:
553 SDL_HideWindow(scon
->real_window
);
556 case SDL_WINDOWEVENT_HIDDEN
:
558 SDL_ShowWindow(scon
->real_window
);
564 void sdl2_poll_events(struct sdl2_console
*scon
)
566 SDL_Event ev1
, *ev
= &ev1
;
568 if (scon
->last_vm_running
!= runstate_is_running()) {
569 scon
->last_vm_running
= runstate_is_running();
570 sdl_update_caption(scon
);
573 while (SDL_PollEvent(ev
)) {
582 handle_textinput(ev
);
587 qemu_system_shutdown_request();
590 case SDL_MOUSEMOTION
:
591 handle_mousemotion(ev
);
593 case SDL_MOUSEBUTTONDOWN
:
594 case SDL_MOUSEBUTTONUP
:
595 handle_mousebutton(ev
);
598 handle_mousewheel(ev
);
600 case SDL_WINDOWEVENT
:
601 handle_windowevent(ev
);
609 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
610 int x
, int y
, int on
)
612 struct sdl2_console
*scon
= container_of(dcl
, struct sdl2_console
, dcl
);
617 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
618 SDL_SetCursor(guest_sprite
);
619 if (!qemu_input_is_absolute() && !absolute_enabled
) {
620 SDL_WarpMouseInWindow(scon
->real_window
, x
, y
);
623 } else if (gui_grab
) {
627 guest_x
= x
, guest_y
= y
;
630 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
635 SDL_FreeCursor(guest_sprite
);
638 if (guest_sprite_surface
) {
639 SDL_FreeSurface(guest_sprite_surface
);
642 guest_sprite_surface
=
643 SDL_CreateRGBSurfaceFrom(c
->data
, c
->width
, c
->height
, 32, c
->width
* 4,
644 0xff0000, 0x00ff00, 0xff, 0xff000000);
646 if (!guest_sprite_surface
) {
647 fprintf(stderr
, "Failed to make rgb surface from %p\n", c
);
650 guest_sprite
= SDL_CreateColorCursor(guest_sprite_surface
,
653 fprintf(stderr
, "Failed to make color cursor from %p\n", c
);
657 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
)) {
658 SDL_SetCursor(guest_sprite
);
662 static void sdl_cleanup(void)
665 SDL_FreeCursor(guest_sprite
);
667 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
670 static const DisplayChangeListenerOps dcl_2d_ops
= {
671 .dpy_name
= "sdl2-2d",
672 .dpy_gfx_update
= sdl2_2d_update
,
673 .dpy_gfx_switch
= sdl2_2d_switch
,
674 .dpy_gfx_check_format
= sdl2_2d_check_format
,
675 .dpy_refresh
= sdl2_2d_refresh
,
676 .dpy_mouse_set
= sdl_mouse_warp
,
677 .dpy_cursor_define
= sdl_mouse_define
,
680 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
692 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
693 * accessible $DISPLAY to open X11 window. This is often the case
694 * when qemu is run using sudo. But in this case, and when actually
695 * run in X11 environment, SDL fights with X11 for the video card,
696 * making current display unavailable, often until reboot.
697 * So make x11 the default SDL video driver if this variable is unset.
698 * This is a bit hackish but saves us from bigger problem.
699 * Maybe it's a good idea to fix this in SDL instead.
701 setenv("SDL_VIDEODRIVER", "x11", 0);
704 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
705 if (SDL_Init(flags
)) {
706 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
710 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD
, "1");
713 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
718 sdl2_num_outputs
= i
;
719 sdl2_console
= g_new0(struct sdl2_console
, sdl2_num_outputs
);
720 for (i
= 0; i
< sdl2_num_outputs
; i
++) {
721 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
722 if (!qemu_console_is_graphic(con
)) {
723 sdl2_console
[i
].hidden
= true;
725 sdl2_console
[i
].dcl
.ops
= &dcl_2d_ops
;
726 sdl2_console
[i
].dcl
.con
= con
;
727 register_displaychangelistener(&sdl2_console
[i
].dcl
);
728 sdl2_console
[i
].idx
= i
;
731 /* Load a 32x32x4 image. White pixels are transparent. */
732 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
734 SDL_Surface
*image
= SDL_LoadBMP(filename
);
736 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
737 SDL_SetColorKey(image
, SDL_TRUE
, colorkey
);
738 SDL_SetWindowIcon(sdl2_console
[0].real_window
, image
);
748 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
749 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
753 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
754 sdl_cursor_normal
= SDL_GetCursor();