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-common.h"
27 #include "ui/console.h"
30 #include "sysemu/sysemu.h"
32 static int sdl2_num_outputs;
33 static struct sdl2_console *sdl2_console;
35 static SDL_Surface *guest_sprite_surface;
36 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
38 static int gui_saved_grab;
39 static int gui_fullscreen;
40 static int gui_noframe;
41 static int gui_key_modifier_pressed;
42 static int gui_keysym;
43 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
44 static SDL_Cursor *sdl_cursor_normal;
45 static SDL_Cursor *sdl_cursor_hidden;
46 static int absolute_enabled;
47 static int guest_cursor;
48 static int guest_x, guest_y;
49 static SDL_Cursor *guest_sprite;
50 static Notifier mouse_mode_notifier;
52 static void sdl_update_caption(struct sdl2_console *scon);
54 static struct sdl2_console *get_scon_from_window(uint32_t window_id)
57 for (i = 0; i < sdl2_num_outputs; i++) {
58 if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
59 return &sdl2_console[i];
65 void sdl2_window_create(struct sdl2_console *scon)
72 assert(!scon->real_window);
75 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
77 flags |= SDL_WINDOW_RESIZABLE;
80 flags |= SDL_WINDOW_HIDDEN;
83 scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
84 SDL_WINDOWPOS_UNDEFINED,
85 surface_width(scon->surface),
86 surface_height(scon->surface),
88 scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
89 sdl_update_caption(scon);
92 void sdl2_window_destroy(struct sdl2_console *scon)
94 if (!scon->real_window) {
98 SDL_DestroyRenderer(scon->real_renderer);
99 scon->real_renderer = NULL;
100 SDL_DestroyWindow(scon->real_window);
101 scon->real_window = NULL;
104 void sdl2_window_resize(struct sdl2_console *scon)
106 if (!scon->real_window) {
110 SDL_SetWindowSize(scon->real_window,
111 surface_width(scon->surface),
112 surface_height(scon->surface));
115 static void sdl_update_caption(struct sdl2_console *scon)
117 char win_title[1024];
118 char icon_title[1024];
119 const char *status = "";
121 if (!runstate_is_running()) {
122 status = " [Stopped]";
123 } else if (gui_grab) {
125 status = " - Press Ctrl-Alt-Shift to exit grab";
126 } else if (ctrl_grab) {
127 status = " - Press Right-Ctrl to exit grab";
129 status = " - Press Ctrl-Alt to exit grab";
134 snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
136 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
138 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
139 snprintf(icon_title, sizeof(icon_title), "QEMU");
142 if (scon->real_window) {
143 SDL_SetWindowTitle(scon->real_window, win_title);
147 static void sdl_hide_cursor(void)
153 if (qemu_input_is_absolute()) {
155 SDL_SetCursor(sdl_cursor_hidden);
157 SDL_SetRelativeMouseMode(SDL_TRUE);
161 static void sdl_show_cursor(void)
167 if (!qemu_input_is_absolute()) {
168 SDL_SetRelativeMouseMode(SDL_FALSE);
171 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
172 SDL_SetCursor(guest_sprite);
174 SDL_SetCursor(sdl_cursor_normal);
179 static void sdl_grab_start(struct sdl2_console *scon)
181 QemuConsole *con = scon ? scon->dcl.con : NULL;
183 if (!con || !qemu_console_is_graphic(con)) {
187 * If the application is not active, do not try to enter grab state. This
188 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
189 * application (SDL bug).
191 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
195 SDL_SetCursor(guest_sprite);
196 if (!qemu_input_is_absolute() && !absolute_enabled) {
197 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
202 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
204 sdl_update_caption(scon);
207 static void sdl_grab_end(struct sdl2_console *scon)
209 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
212 sdl_update_caption(scon);
215 static void absolute_mouse_grab(struct sdl2_console *scon)
217 int mouse_x, mouse_y;
219 SDL_GetMouseState(&mouse_x, &mouse_y);
220 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
221 if (mouse_x > 0 && mouse_x < scr_w - 1 &&
222 mouse_y > 0 && mouse_y < scr_h - 1) {
223 sdl_grab_start(scon);
227 static void sdl_mouse_mode_change(Notifier *notify, void *data)
229 if (qemu_input_is_absolute()) {
230 if (!absolute_enabled) {
231 absolute_enabled = 1;
232 absolute_mouse_grab(&sdl2_console[0]);
234 } else if (absolute_enabled) {
235 if (!gui_fullscreen) {
236 sdl_grab_end(&sdl2_console[0]);
238 absolute_enabled = 0;
242 static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
243 int x, int y, int state)
245 static uint32_t bmap[INPUT_BUTTON_MAX] = {
246 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
247 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
248 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
250 static uint32_t prev_state;
252 if (prev_state != state) {
253 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
257 if (qemu_input_is_absolute()) {
259 int max_w = 0, max_h = 0;
260 int off_x = 0, off_y = 0;
261 int cur_off_x = 0, cur_off_y = 0;
264 for (i = 0; i < sdl2_num_outputs; i++) {
265 struct sdl2_console *thiscon = &sdl2_console[i];
266 if (thiscon->real_window && thiscon->surface) {
267 SDL_GetWindowSize(thiscon->real_window, &scr_w, &scr_h);
268 cur_off_x = thiscon->x;
269 cur_off_y = thiscon->y;
270 if (scr_w + cur_off_x > max_w) {
271 max_w = scr_w + cur_off_x;
273 if (scr_h + cur_off_y > max_h) {
274 max_h = scr_h + cur_off_y;
276 if (i == scon->idx) {
282 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X, off_x + x, max_w);
283 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y, off_y + y, max_h);
293 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
294 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
296 qemu_input_event_sync();
299 static void toggle_full_screen(struct sdl2_console *scon)
301 gui_fullscreen = !gui_fullscreen;
302 if (gui_fullscreen) {
303 SDL_SetWindowFullscreen(scon->real_window,
304 SDL_WINDOW_FULLSCREEN_DESKTOP);
305 gui_saved_grab = gui_grab;
306 sdl_grab_start(scon);
308 if (!gui_saved_grab) {
311 SDL_SetWindowFullscreen(scon->real_window, 0);
313 sdl2_2d_redraw(scon);
316 static void handle_keydown(SDL_Event *ev)
319 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
322 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
323 (gui_grab_code | KMOD_LSHIFT);
324 } else if (ctrl_grab) {
325 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
327 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
329 gui_key_modifier_pressed = mod_state;
331 if (gui_key_modifier_pressed) {
332 switch (ev->key.keysym.scancode) {
341 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
342 if (win < sdl2_num_outputs) {
343 sdl2_console[win].hidden = !sdl2_console[win].hidden;
344 if (sdl2_console[win].real_window) {
345 if (sdl2_console[win].hidden) {
346 SDL_HideWindow(sdl2_console[win].real_window);
348 SDL_ShowWindow(sdl2_console[win].real_window);
355 toggle_full_screen(scon);
359 sdl2_window_destroy(scon);
360 sdl2_window_create(scon);
361 /* re-create texture */
362 sdl2_2d_switch(&scon->dcl, scon->surface);
366 case SDL_SCANCODE_KP_PLUS:
367 case SDL_SCANCODE_KP_MINUS:
368 if (!gui_fullscreen) {
371 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
373 width = MAX(scr_w + (ev->key.keysym.scancode ==
374 SDL_SCANCODE_KP_PLUS ? 50 : -50),
376 height = (surface_height(scon->surface) * width) /
377 surface_width(scon->surface);
378 fprintf(stderr, "%s: scale to %dx%d\n",
379 __func__, width, height);
380 sdl_scale(scon, width, height);
381 sdl2_2d_redraw(scon);
390 sdl2_process_key(scon, &ev->key);
394 static void handle_keyup(SDL_Event *ev)
397 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
400 mod_state = (ev->key.keysym.mod & gui_grab_code);
402 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
404 if (!mod_state && gui_key_modifier_pressed) {
405 gui_key_modifier_pressed = 0;
406 if (gui_keysym == 0) {
407 /* exit/enter grab if pressing Ctrl-Alt */
409 sdl_grab_start(scon);
410 } else if (!gui_fullscreen) {
413 /* SDL does not send back all the modifiers key, so we must
415 sdl2_reset_keys(scon);
421 sdl2_process_key(scon, &ev->key);
425 static void handle_textinput(SDL_Event *ev)
427 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
428 QemuConsole *con = scon ? scon->dcl.con : NULL;
430 if (qemu_console_is_graphic(con)) {
433 kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
436 static void handle_mousemotion(SDL_Event *ev)
439 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
441 if (qemu_input_is_absolute() || absolute_enabled) {
443 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
446 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
447 ev->motion.x == max_x || ev->motion.y == max_y)) {
451 (ev->motion.x > 0 && ev->motion.x < max_x &&
452 ev->motion.y > 0 && ev->motion.y < max_y)) {
453 sdl_grab_start(scon);
456 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
457 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
458 ev->motion.x, ev->motion.y, ev->motion.state);
462 static void handle_mousebutton(SDL_Event *ev)
464 int buttonstate = SDL_GetMouseState(NULL, NULL);
465 SDL_MouseButtonEvent *bev;
466 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
469 if (!gui_grab && !qemu_input_is_absolute()) {
470 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
471 /* start grabbing all events */
472 sdl_grab_start(scon);
475 if (ev->type == SDL_MOUSEBUTTONDOWN) {
476 buttonstate |= SDL_BUTTON(bev->button);
478 buttonstate &= ~SDL_BUTTON(bev->button);
480 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
484 static void handle_mousewheel(SDL_Event *ev)
486 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
487 SDL_MouseWheelEvent *wev = &ev->wheel;
491 btn = INPUT_BUTTON_WHEEL_UP;
492 } else if (wev->y < 0) {
493 btn = INPUT_BUTTON_WHEEL_DOWN;
498 qemu_input_queue_btn(scon->dcl.con, btn, true);
499 qemu_input_event_sync();
500 qemu_input_queue_btn(scon->dcl.con, btn, false);
501 qemu_input_event_sync();
504 static void handle_windowevent(SDL_Event *ev)
506 struct sdl2_console *scon = get_scon_from_window(ev->window.windowID);
508 switch (ev->window.event) {
509 case SDL_WINDOWEVENT_RESIZED:
512 memset(&info, 0, sizeof(info));
513 info.width = ev->window.data1;
514 info.height = ev->window.data2;
515 dpy_set_ui_info(scon->dcl.con, &info);
517 sdl2_2d_redraw(scon);
519 case SDL_WINDOWEVENT_EXPOSED:
520 sdl2_2d_redraw(scon);
522 case SDL_WINDOWEVENT_FOCUS_GAINED:
523 case SDL_WINDOWEVENT_ENTER:
524 if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
525 absolute_mouse_grab(scon);
528 case SDL_WINDOWEVENT_FOCUS_LOST:
529 if (gui_grab && !gui_fullscreen) {
533 case SDL_WINDOWEVENT_RESTORED:
534 update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT);
536 case SDL_WINDOWEVENT_MINIMIZED:
537 update_displaychangelistener(&scon->dcl, 500);
539 case SDL_WINDOWEVENT_CLOSE:
542 qemu_system_shutdown_request();
545 case SDL_WINDOWEVENT_SHOWN:
547 SDL_HideWindow(scon->real_window);
550 case SDL_WINDOWEVENT_HIDDEN:
552 SDL_ShowWindow(scon->real_window);
558 void sdl2_poll_events(struct sdl2_console *scon)
560 SDL_Event ev1, *ev = &ev1;
562 if (scon->last_vm_running != runstate_is_running()) {
563 scon->last_vm_running = runstate_is_running();
564 sdl_update_caption(scon);
567 while (SDL_PollEvent(ev)) {
576 handle_textinput(ev);
581 qemu_system_shutdown_request();
584 case SDL_MOUSEMOTION:
585 handle_mousemotion(ev);
587 case SDL_MOUSEBUTTONDOWN:
588 case SDL_MOUSEBUTTONUP:
589 handle_mousebutton(ev);
592 handle_mousewheel(ev);
594 case SDL_WINDOWEVENT:
595 handle_windowevent(ev);
603 static void sdl_mouse_warp(DisplayChangeListener *dcl,
604 int x, int y, int on)
606 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
611 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
612 SDL_SetCursor(guest_sprite);
613 if (!qemu_input_is_absolute() && !absolute_enabled) {
614 SDL_WarpMouseInWindow(scon->real_window, x, y);
617 } else if (gui_grab) {
621 guest_x = x, guest_y = y;
624 static void sdl_mouse_define(DisplayChangeListener *dcl,
629 SDL_FreeCursor(guest_sprite);
632 if (guest_sprite_surface) {
633 SDL_FreeSurface(guest_sprite_surface);
636 guest_sprite_surface =
637 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
638 0xff0000, 0x00ff00, 0xff, 0xff000000);
640 if (!guest_sprite_surface) {
641 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
644 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
647 fprintf(stderr, "Failed to make color cursor from %p\n", c);
651 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
652 SDL_SetCursor(guest_sprite);
656 static void sdl_cleanup(void)
659 SDL_FreeCursor(guest_sprite);
661 SDL_QuitSubSystem(SDL_INIT_VIDEO);
664 static const DisplayChangeListenerOps dcl_2d_ops = {
665 .dpy_name = "sdl2-2d",
666 .dpy_gfx_update = sdl2_2d_update,
667 .dpy_gfx_switch = sdl2_2d_switch,
668 .dpy_gfx_check_format = sdl2_2d_check_format,
669 .dpy_refresh = sdl2_2d_refresh,
670 .dpy_mouse_set = sdl_mouse_warp,
671 .dpy_cursor_define = sdl_mouse_define,
674 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
686 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
687 * accessible $DISPLAY to open X11 window. This is often the case
688 * when qemu is run using sudo. But in this case, and when actually
689 * run in X11 environment, SDL fights with X11 for the video card,
690 * making current display unavailable, often until reboot.
691 * So make x11 the default SDL video driver if this variable is unset.
692 * This is a bit hackish but saves us from bigger problem.
693 * Maybe it's a good idea to fix this in SDL instead.
695 setenv("SDL_VIDEODRIVER", "x11", 0);
698 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
699 if (SDL_Init(flags)) {
700 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
704 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
707 QemuConsole *con = qemu_console_lookup_by_index(i);
712 sdl2_num_outputs = i;
713 sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
714 for (i = 0; i < sdl2_num_outputs; i++) {
715 QemuConsole *con = qemu_console_lookup_by_index(i);
716 if (!qemu_console_is_graphic(con)) {
717 sdl2_console[i].hidden = true;
719 sdl2_console[i].dcl.ops = &dcl_2d_ops;
720 sdl2_console[i].dcl.con = con;
721 register_displaychangelistener(&sdl2_console[i].dcl);
722 sdl2_console[i].idx = i;
725 /* Load a 32x32x4 image. White pixels are transparent. */
726 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
728 SDL_Surface *image = SDL_LoadBMP(filename);
730 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
731 SDL_SetColorKey(image, SDL_TRUE, colorkey);
732 SDL_SetWindowIcon(sdl2_console[0].real_window, image);
742 mouse_mode_notifier.notify = sdl_mouse_mode_change;
743 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
747 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
748 sdl_cursor_normal = SDL_GetCursor();