libqtest: fix the order of buffered events
[qemu/ar7.git] / ui / sdl2.c
blob189d26e2a9510e81ea04f0d98535ffa91f13e892
1 /*
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
22 * THE SOFTWARE.
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"
30 #include "ui/input.h"
31 #include "ui/sdl2.h"
32 #include "sysemu/runstate.h"
33 #include "sysemu/sysemu.h"
34 #include "ui/win32-kbd-hook.h"
36 static int sdl2_num_outputs;
37 static struct sdl2_console *sdl2_console;
39 static SDL_Surface *guest_sprite_surface;
40 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
42 static int gui_saved_grab;
43 static int gui_fullscreen;
44 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
45 static SDL_Cursor *sdl_cursor_normal;
46 static SDL_Cursor *sdl_cursor_hidden;
47 static int absolute_enabled;
48 static int guest_cursor;
49 static int guest_x, guest_y;
50 static SDL_Cursor *guest_sprite;
51 static Notifier mouse_mode_notifier;
53 #define SDL2_REFRESH_INTERVAL_BUSY 10
54 #define SDL2_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
55 / SDL2_REFRESH_INTERVAL_BUSY + 1)
57 static void sdl_update_caption(struct sdl2_console *scon);
59 static struct sdl2_console *get_scon_from_window(uint32_t window_id)
61 int i;
62 for (i = 0; i < sdl2_num_outputs; i++) {
63 if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
64 return &sdl2_console[i];
67 return NULL;
70 void sdl2_window_create(struct sdl2_console *scon)
72 int flags = 0;
74 if (!scon->surface) {
75 return;
77 assert(!scon->real_window);
79 if (gui_fullscreen) {
80 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
81 } else {
82 flags |= SDL_WINDOW_RESIZABLE;
84 if (scon->hidden) {
85 flags |= SDL_WINDOW_HIDDEN;
87 #ifdef CONFIG_OPENGL
88 if (scon->opengl) {
89 flags |= SDL_WINDOW_OPENGL;
91 #endif
93 scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
94 SDL_WINDOWPOS_UNDEFINED,
95 surface_width(scon->surface),
96 surface_height(scon->surface),
97 flags);
98 scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
99 if (scon->opengl) {
100 scon->winctx = SDL_GL_GetCurrentContext();
102 sdl_update_caption(scon);
105 void sdl2_window_destroy(struct sdl2_console *scon)
107 if (!scon->real_window) {
108 return;
111 SDL_DestroyRenderer(scon->real_renderer);
112 scon->real_renderer = NULL;
113 SDL_DestroyWindow(scon->real_window);
114 scon->real_window = NULL;
117 void sdl2_window_resize(struct sdl2_console *scon)
119 if (!scon->real_window) {
120 return;
123 SDL_SetWindowSize(scon->real_window,
124 surface_width(scon->surface),
125 surface_height(scon->surface));
128 static void sdl2_redraw(struct sdl2_console *scon)
130 if (scon->opengl) {
131 #ifdef CONFIG_OPENGL
132 sdl2_gl_redraw(scon);
133 #endif
134 } else {
135 sdl2_2d_redraw(scon);
139 static void sdl_update_caption(struct sdl2_console *scon)
141 char win_title[1024];
142 char icon_title[1024];
143 const char *status = "";
145 if (!runstate_is_running()) {
146 status = " [Stopped]";
147 } else if (gui_grab) {
148 if (alt_grab) {
149 status = " - Press Ctrl-Alt-Shift-G to exit grab";
150 } else if (ctrl_grab) {
151 status = " - Press Right-Ctrl-G to exit grab";
152 } else {
153 status = " - Press Ctrl-Alt-G to exit grab";
157 if (qemu_name) {
158 snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
159 scon->idx, status);
160 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
161 } else {
162 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
163 snprintf(icon_title, sizeof(icon_title), "QEMU");
166 if (scon->real_window) {
167 SDL_SetWindowTitle(scon->real_window, win_title);
171 static void sdl_hide_cursor(struct sdl2_console *scon)
173 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
174 return;
177 SDL_ShowCursor(SDL_DISABLE);
178 SDL_SetCursor(sdl_cursor_hidden);
180 if (!qemu_input_is_absolute()) {
181 SDL_SetRelativeMouseMode(SDL_TRUE);
185 static void sdl_show_cursor(struct sdl2_console *scon)
187 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
188 return;
191 if (!qemu_input_is_absolute()) {
192 SDL_SetRelativeMouseMode(SDL_FALSE);
195 if (guest_cursor &&
196 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
197 SDL_SetCursor(guest_sprite);
198 } else {
199 SDL_SetCursor(sdl_cursor_normal);
202 SDL_ShowCursor(SDL_ENABLE);
205 static void sdl_grab_start(struct sdl2_console *scon)
207 QemuConsole *con = scon ? scon->dcl.con : NULL;
209 if (!con || !qemu_console_is_graphic(con)) {
210 return;
213 * If the application is not active, do not try to enter grab state. This
214 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
215 * application (SDL bug).
217 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
218 return;
220 if (guest_cursor) {
221 SDL_SetCursor(guest_sprite);
222 if (!qemu_input_is_absolute() && !absolute_enabled) {
223 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
225 } else {
226 sdl_hide_cursor(scon);
228 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
229 gui_grab = 1;
230 win32_kbd_set_grab(true);
231 sdl_update_caption(scon);
234 static void sdl_grab_end(struct sdl2_console *scon)
236 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
237 gui_grab = 0;
238 win32_kbd_set_grab(false);
239 sdl_show_cursor(scon);
240 sdl_update_caption(scon);
243 static void absolute_mouse_grab(struct sdl2_console *scon)
245 int mouse_x, mouse_y;
246 int scr_w, scr_h;
247 SDL_GetMouseState(&mouse_x, &mouse_y);
248 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
249 if (mouse_x > 0 && mouse_x < scr_w - 1 &&
250 mouse_y > 0 && mouse_y < scr_h - 1) {
251 sdl_grab_start(scon);
255 static void sdl_mouse_mode_change(Notifier *notify, void *data)
257 if (qemu_input_is_absolute()) {
258 if (!absolute_enabled) {
259 absolute_enabled = 1;
260 SDL_SetRelativeMouseMode(SDL_FALSE);
261 absolute_mouse_grab(&sdl2_console[0]);
263 } else if (absolute_enabled) {
264 if (!gui_fullscreen) {
265 sdl_grab_end(&sdl2_console[0]);
267 absolute_enabled = 0;
271 static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
272 int x, int y, int state)
274 static uint32_t bmap[INPUT_BUTTON__MAX] = {
275 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
276 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
277 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
279 static uint32_t prev_state;
281 if (prev_state != state) {
282 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
283 prev_state = state;
286 if (qemu_input_is_absolute()) {
287 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X,
288 x, 0, surface_width(scon->surface));
289 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y,
290 y, 0, surface_height(scon->surface));
291 } else {
292 if (guest_cursor) {
293 x -= guest_x;
294 y -= guest_y;
295 guest_x += x;
296 guest_y += y;
297 dx = x;
298 dy = y;
300 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
301 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
303 qemu_input_event_sync();
306 static void toggle_full_screen(struct sdl2_console *scon)
308 gui_fullscreen = !gui_fullscreen;
309 if (gui_fullscreen) {
310 SDL_SetWindowFullscreen(scon->real_window,
311 SDL_WINDOW_FULLSCREEN_DESKTOP);
312 gui_saved_grab = gui_grab;
313 sdl_grab_start(scon);
314 } else {
315 if (!gui_saved_grab) {
316 sdl_grab_end(scon);
318 SDL_SetWindowFullscreen(scon->real_window, 0);
320 sdl2_redraw(scon);
323 static int get_mod_state(void)
325 SDL_Keymod mod = SDL_GetModState();
327 if (alt_grab) {
328 return (mod & (gui_grab_code | KMOD_LSHIFT)) ==
329 (gui_grab_code | KMOD_LSHIFT);
330 } else if (ctrl_grab) {
331 return (mod & KMOD_RCTRL) == KMOD_RCTRL;
332 } else {
333 return (mod & gui_grab_code) == gui_grab_code;
337 static void *sdl2_win32_get_hwnd(struct sdl2_console *scon)
339 #ifdef CONFIG_WIN32
340 SDL_SysWMinfo info;
342 SDL_VERSION(&info.version);
343 if (SDL_GetWindowWMInfo(scon->real_window, &info)) {
344 return info.info.win.window;
346 #endif
347 return NULL;
350 static void handle_keydown(SDL_Event *ev)
352 int win;
353 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
354 int gui_key_modifier_pressed = get_mod_state();
355 int gui_keysym = 0;
357 if (!scon) {
358 return;
361 if (!scon->ignore_hotkeys && gui_key_modifier_pressed && !ev->key.repeat) {
362 switch (ev->key.keysym.scancode) {
363 case SDL_SCANCODE_2:
364 case SDL_SCANCODE_3:
365 case SDL_SCANCODE_4:
366 case SDL_SCANCODE_5:
367 case SDL_SCANCODE_6:
368 case SDL_SCANCODE_7:
369 case SDL_SCANCODE_8:
370 case SDL_SCANCODE_9:
371 if (gui_grab) {
372 sdl_grab_end(scon);
375 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
376 if (win < sdl2_num_outputs) {
377 sdl2_console[win].hidden = !sdl2_console[win].hidden;
378 if (sdl2_console[win].real_window) {
379 if (sdl2_console[win].hidden) {
380 SDL_HideWindow(sdl2_console[win].real_window);
381 } else {
382 SDL_ShowWindow(sdl2_console[win].real_window);
385 gui_keysym = 1;
387 break;
388 case SDL_SCANCODE_F:
389 toggle_full_screen(scon);
390 gui_keysym = 1;
391 break;
392 case SDL_SCANCODE_G:
393 gui_keysym = 1;
394 if (!gui_grab) {
395 sdl_grab_start(scon);
396 } else if (!gui_fullscreen) {
397 sdl_grab_end(scon);
399 break;
400 case SDL_SCANCODE_U:
401 sdl2_window_resize(scon);
402 if (!scon->opengl) {
403 /* re-create scon->texture */
404 sdl2_2d_switch(&scon->dcl, scon->surface);
406 gui_keysym = 1;
407 break;
408 #if 0
409 case SDL_SCANCODE_KP_PLUS:
410 case SDL_SCANCODE_KP_MINUS:
411 if (!gui_fullscreen) {
412 int scr_w, scr_h;
413 int width, height;
414 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
416 width = MAX(scr_w + (ev->key.keysym.scancode ==
417 SDL_SCANCODE_KP_PLUS ? 50 : -50),
418 160);
419 height = (surface_height(scon->surface) * width) /
420 surface_width(scon->surface);
421 fprintf(stderr, "%s: scale to %dx%d\n",
422 __func__, width, height);
423 sdl_scale(scon, width, height);
424 sdl2_redraw(scon);
425 gui_keysym = 1;
427 #endif
428 default:
429 break;
432 if (!gui_keysym) {
433 sdl2_process_key(scon, &ev->key);
437 static void handle_keyup(SDL_Event *ev)
439 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
441 if (!scon) {
442 return;
445 scon->ignore_hotkeys = false;
446 sdl2_process_key(scon, &ev->key);
449 static void handle_textinput(SDL_Event *ev)
451 struct sdl2_console *scon = get_scon_from_window(ev->text.windowID);
452 QemuConsole *con = scon ? scon->dcl.con : NULL;
454 if (!con) {
455 return;
458 if (qemu_console_is_graphic(con)) {
459 return;
461 kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
464 static void handle_mousemotion(SDL_Event *ev)
466 int max_x, max_y;
467 struct sdl2_console *scon = get_scon_from_window(ev->motion.windowID);
469 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
470 return;
473 if (qemu_input_is_absolute() || absolute_enabled) {
474 int scr_w, scr_h;
475 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
476 max_x = scr_w - 1;
477 max_y = scr_h - 1;
478 if (gui_grab && !gui_fullscreen
479 && (ev->motion.x == 0 || ev->motion.y == 0 ||
480 ev->motion.x == max_x || ev->motion.y == max_y)) {
481 sdl_grab_end(scon);
483 if (!gui_grab &&
484 (ev->motion.x > 0 && ev->motion.x < max_x &&
485 ev->motion.y > 0 && ev->motion.y < max_y)) {
486 sdl_grab_start(scon);
489 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
490 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
491 ev->motion.x, ev->motion.y, ev->motion.state);
495 static void handle_mousebutton(SDL_Event *ev)
497 int buttonstate = SDL_GetMouseState(NULL, NULL);
498 SDL_MouseButtonEvent *bev;
499 struct sdl2_console *scon = get_scon_from_window(ev->button.windowID);
501 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
502 return;
505 bev = &ev->button;
506 if (!gui_grab && !qemu_input_is_absolute()) {
507 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
508 /* start grabbing all events */
509 sdl_grab_start(scon);
511 } else {
512 if (ev->type == SDL_MOUSEBUTTONDOWN) {
513 buttonstate |= SDL_BUTTON(bev->button);
514 } else {
515 buttonstate &= ~SDL_BUTTON(bev->button);
517 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
521 static void handle_mousewheel(SDL_Event *ev)
523 struct sdl2_console *scon = get_scon_from_window(ev->wheel.windowID);
524 SDL_MouseWheelEvent *wev = &ev->wheel;
525 InputButton btn;
527 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
528 return;
531 if (wev->y > 0) {
532 btn = INPUT_BUTTON_WHEEL_UP;
533 } else if (wev->y < 0) {
534 btn = INPUT_BUTTON_WHEEL_DOWN;
535 } else {
536 return;
539 qemu_input_queue_btn(scon->dcl.con, btn, true);
540 qemu_input_event_sync();
541 qemu_input_queue_btn(scon->dcl.con, btn, false);
542 qemu_input_event_sync();
545 static void handle_windowevent(SDL_Event *ev)
547 struct sdl2_console *scon = get_scon_from_window(ev->window.windowID);
548 bool allow_close = true;
550 if (!scon) {
551 return;
554 switch (ev->window.event) {
555 case SDL_WINDOWEVENT_RESIZED:
557 QemuUIInfo info;
558 memset(&info, 0, sizeof(info));
559 info.width = ev->window.data1;
560 info.height = ev->window.data2;
561 dpy_set_ui_info(scon->dcl.con, &info);
563 sdl2_redraw(scon);
564 break;
565 case SDL_WINDOWEVENT_EXPOSED:
566 sdl2_redraw(scon);
567 break;
568 case SDL_WINDOWEVENT_FOCUS_GAINED:
569 win32_kbd_set_grab(gui_grab);
570 if (qemu_console_is_graphic(scon->dcl.con)) {
571 win32_kbd_set_window(sdl2_win32_get_hwnd(scon));
573 /* fall through */
574 case SDL_WINDOWEVENT_ENTER:
575 if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
576 absolute_mouse_grab(scon);
578 /* If a new console window opened using a hotkey receives the
579 * focus, SDL sends another KEYDOWN event to the new window,
580 * closing the console window immediately after.
582 * Work around this by ignoring further hotkey events until a
583 * key is released.
585 scon->ignore_hotkeys = get_mod_state();
586 break;
587 case SDL_WINDOWEVENT_FOCUS_LOST:
588 if (qemu_console_is_graphic(scon->dcl.con)) {
589 win32_kbd_set_window(NULL);
591 if (gui_grab && !gui_fullscreen) {
592 sdl_grab_end(scon);
594 break;
595 case SDL_WINDOWEVENT_RESTORED:
596 update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT);
597 break;
598 case SDL_WINDOWEVENT_MINIMIZED:
599 update_displaychangelistener(&scon->dcl, 500);
600 break;
601 case SDL_WINDOWEVENT_CLOSE:
602 if (qemu_console_is_graphic(scon->dcl.con)) {
603 if (scon->opts->has_window_close && !scon->opts->window_close) {
604 allow_close = false;
606 if (allow_close) {
607 no_shutdown = 0;
608 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
610 } else {
611 SDL_HideWindow(scon->real_window);
612 scon->hidden = true;
614 break;
615 case SDL_WINDOWEVENT_SHOWN:
616 scon->hidden = false;
617 break;
618 case SDL_WINDOWEVENT_HIDDEN:
619 scon->hidden = true;
620 break;
624 void sdl2_poll_events(struct sdl2_console *scon)
626 SDL_Event ev1, *ev = &ev1;
627 bool allow_close = true;
628 int idle = 1;
630 if (scon->last_vm_running != runstate_is_running()) {
631 scon->last_vm_running = runstate_is_running();
632 sdl_update_caption(scon);
635 while (SDL_PollEvent(ev)) {
636 switch (ev->type) {
637 case SDL_KEYDOWN:
638 idle = 0;
639 handle_keydown(ev);
640 break;
641 case SDL_KEYUP:
642 idle = 0;
643 handle_keyup(ev);
644 break;
645 case SDL_TEXTINPUT:
646 idle = 0;
647 handle_textinput(ev);
648 break;
649 case SDL_QUIT:
650 if (scon->opts->has_window_close && !scon->opts->window_close) {
651 allow_close = false;
653 if (allow_close) {
654 no_shutdown = 0;
655 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
657 break;
658 case SDL_MOUSEMOTION:
659 idle = 0;
660 handle_mousemotion(ev);
661 break;
662 case SDL_MOUSEBUTTONDOWN:
663 case SDL_MOUSEBUTTONUP:
664 idle = 0;
665 handle_mousebutton(ev);
666 break;
667 case SDL_MOUSEWHEEL:
668 idle = 0;
669 handle_mousewheel(ev);
670 break;
671 case SDL_WINDOWEVENT:
672 handle_windowevent(ev);
673 break;
674 default:
675 break;
679 if (idle) {
680 if (scon->idle_counter < SDL2_MAX_IDLE_COUNT) {
681 scon->idle_counter++;
682 if (scon->idle_counter >= SDL2_MAX_IDLE_COUNT) {
683 scon->dcl.update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
686 } else {
687 scon->idle_counter = 0;
688 scon->dcl.update_interval = SDL2_REFRESH_INTERVAL_BUSY;
692 static void sdl_mouse_warp(DisplayChangeListener *dcl,
693 int x, int y, int on)
695 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
697 if (!qemu_console_is_graphic(scon->dcl.con)) {
698 return;
701 if (on) {
702 if (!guest_cursor) {
703 sdl_show_cursor(scon);
705 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
706 SDL_SetCursor(guest_sprite);
707 if (!qemu_input_is_absolute() && !absolute_enabled) {
708 SDL_WarpMouseInWindow(scon->real_window, x, y);
711 } else if (gui_grab) {
712 sdl_hide_cursor(scon);
714 guest_cursor = on;
715 guest_x = x, guest_y = y;
718 static void sdl_mouse_define(DisplayChangeListener *dcl,
719 QEMUCursor *c)
722 if (guest_sprite) {
723 SDL_FreeCursor(guest_sprite);
726 if (guest_sprite_surface) {
727 SDL_FreeSurface(guest_sprite_surface);
730 guest_sprite_surface =
731 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
732 0xff0000, 0x00ff00, 0xff, 0xff000000);
734 if (!guest_sprite_surface) {
735 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
736 return;
738 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
739 c->hot_x, c->hot_y);
740 if (!guest_sprite) {
741 fprintf(stderr, "Failed to make color cursor from %p\n", c);
742 return;
744 if (guest_cursor &&
745 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
746 SDL_SetCursor(guest_sprite);
750 static void sdl_cleanup(void)
752 if (guest_sprite) {
753 SDL_FreeCursor(guest_sprite);
755 SDL_QuitSubSystem(SDL_INIT_VIDEO);
758 static const DisplayChangeListenerOps dcl_2d_ops = {
759 .dpy_name = "sdl2-2d",
760 .dpy_gfx_update = sdl2_2d_update,
761 .dpy_gfx_switch = sdl2_2d_switch,
762 .dpy_gfx_check_format = sdl2_2d_check_format,
763 .dpy_refresh = sdl2_2d_refresh,
764 .dpy_mouse_set = sdl_mouse_warp,
765 .dpy_cursor_define = sdl_mouse_define,
768 #ifdef CONFIG_OPENGL
769 static const DisplayChangeListenerOps dcl_gl_ops = {
770 .dpy_name = "sdl2-gl",
771 .dpy_gfx_update = sdl2_gl_update,
772 .dpy_gfx_switch = sdl2_gl_switch,
773 .dpy_gfx_check_format = console_gl_check_format,
774 .dpy_refresh = sdl2_gl_refresh,
775 .dpy_mouse_set = sdl_mouse_warp,
776 .dpy_cursor_define = sdl_mouse_define,
778 .dpy_gl_ctx_create = sdl2_gl_create_context,
779 .dpy_gl_ctx_destroy = sdl2_gl_destroy_context,
780 .dpy_gl_ctx_make_current = sdl2_gl_make_context_current,
781 .dpy_gl_ctx_get_current = sdl2_gl_get_current_context,
782 .dpy_gl_scanout_disable = sdl2_gl_scanout_disable,
783 .dpy_gl_scanout_texture = sdl2_gl_scanout_texture,
784 .dpy_gl_update = sdl2_gl_scanout_flush,
786 #endif
788 static void sdl2_display_early_init(DisplayOptions *o)
790 assert(o->type == DISPLAY_TYPE_SDL);
791 if (o->has_gl && o->gl) {
792 #ifdef CONFIG_OPENGL
793 display_opengl = 1;
794 #endif
798 static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
800 uint8_t data = 0;
801 int i;
802 SDL_SysWMinfo info;
803 SDL_Surface *icon = NULL;
804 char *dir;
806 assert(o->type == DISPLAY_TYPE_SDL);
808 #ifdef __linux__
809 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
810 * accessible $DISPLAY to open X11 window. This is often the case
811 * when qemu is run using sudo. But in this case, and when actually
812 * run in X11 environment, SDL fights with X11 for the video card,
813 * making current display unavailable, often until reboot.
814 * So make x11 the default SDL video driver if this variable is unset.
815 * This is a bit hackish but saves us from bigger problem.
816 * Maybe it's a good idea to fix this in SDL instead.
818 g_setenv("SDL_VIDEODRIVER", "x11", 0);
819 #endif
821 if (SDL_Init(SDL_INIT_VIDEO)) {
822 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
823 SDL_GetError());
824 exit(1);
826 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
827 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
828 #endif
829 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
830 memset(&info, 0, sizeof(info));
831 SDL_VERSION(&info.version);
833 gui_fullscreen = o->has_full_screen && o->full_screen;
835 for (i = 0;; i++) {
836 QemuConsole *con = qemu_console_lookup_by_index(i);
837 if (!con) {
838 break;
841 sdl2_num_outputs = i;
842 if (sdl2_num_outputs == 0) {
843 return;
845 sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
846 for (i = 0; i < sdl2_num_outputs; i++) {
847 QemuConsole *con = qemu_console_lookup_by_index(i);
848 assert(con != NULL);
849 if (!qemu_console_is_graphic(con) &&
850 qemu_console_get_index(con) != 0) {
851 sdl2_console[i].hidden = true;
853 sdl2_console[i].idx = i;
854 sdl2_console[i].opts = o;
855 #ifdef CONFIG_OPENGL
856 sdl2_console[i].opengl = display_opengl;
857 sdl2_console[i].dcl.ops = display_opengl ? &dcl_gl_ops : &dcl_2d_ops;
858 #else
859 sdl2_console[i].opengl = 0;
860 sdl2_console[i].dcl.ops = &dcl_2d_ops;
861 #endif
862 sdl2_console[i].dcl.con = con;
863 sdl2_console[i].kbd = qkbd_state_init(con);
864 register_displaychangelistener(&sdl2_console[i].dcl);
866 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
867 if (SDL_GetWindowWMInfo(sdl2_console[i].real_window, &info)) {
868 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
869 qemu_console_set_window_id(con, (uintptr_t)info.info.win.window);
870 #elif defined(SDL_VIDEO_DRIVER_X11)
871 qemu_console_set_window_id(con, info.info.x11.window);
872 #endif
874 #endif
877 #ifdef CONFIG_SDL_IMAGE
878 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/128x128/apps/qemu.png");
879 icon = IMG_Load(dir);
880 #else
881 /* Load a 32x32x4 image. White pixels are transparent. */
882 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/32x32/apps/qemu.bmp");
883 icon = SDL_LoadBMP(dir);
884 if (icon) {
885 uint32_t colorkey = SDL_MapRGB(icon->format, 255, 255, 255);
886 SDL_SetColorKey(icon, SDL_TRUE, colorkey);
888 #endif
889 g_free(dir);
890 if (icon) {
891 SDL_SetWindowIcon(sdl2_console[0].real_window, icon);
894 mouse_mode_notifier.notify = sdl_mouse_mode_change;
895 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
897 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
898 sdl_cursor_normal = SDL_GetCursor();
900 if (gui_fullscreen) {
901 sdl_grab_start(&sdl2_console[0]);
904 atexit(sdl_cleanup);
907 static QemuDisplay qemu_display_sdl2 = {
908 .type = DISPLAY_TYPE_SDL,
909 .early_init = sdl2_display_early_init,
910 .init = sdl2_display_init,
913 static void register_sdl1(void)
915 qemu_display_register(&qemu_display_sdl2);
918 type_init(register_sdl1);