Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / ui / sdl2.c
blobbd4f5a9da14a997c3b2d707baf1e71cf6e98efb3
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/runstate-action.h"
34 #include "sysemu/sysemu.h"
35 #include "ui/win32-kbd-hook.h"
36 #include "qemu/log.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 */
43 static bool alt_grab;
44 static bool ctrl_grab;
46 static int gui_saved_grab;
47 static int gui_fullscreen;
48 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
49 static SDL_Cursor *sdl_cursor_normal;
50 static SDL_Cursor *sdl_cursor_hidden;
51 static int absolute_enabled;
52 static bool guest_cursor;
53 static int guest_x, guest_y;
54 static SDL_Cursor *guest_sprite;
55 static Notifier mouse_mode_notifier;
57 #define SDL2_REFRESH_INTERVAL_BUSY 10
58 #define SDL2_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
59 / SDL2_REFRESH_INTERVAL_BUSY + 1)
61 /* introduced in SDL 2.0.10 */
62 #ifndef SDL_HINT_RENDER_BATCHING
63 #define SDL_HINT_RENDER_BATCHING "SDL_RENDER_BATCHING"
64 #endif
66 static void sdl_update_caption(struct sdl2_console *scon);
68 static struct sdl2_console *get_scon_from_window(uint32_t window_id)
70 int i;
71 for (i = 0; i < sdl2_num_outputs; i++) {
72 if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
73 return &sdl2_console[i];
76 return NULL;
79 void sdl2_window_create(struct sdl2_console *scon)
81 int flags = 0;
83 if (!scon->surface) {
84 return;
86 assert(!scon->real_window);
88 if (gui_fullscreen) {
89 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
90 } else {
91 flags |= SDL_WINDOW_RESIZABLE;
93 if (scon->hidden) {
94 flags |= SDL_WINDOW_HIDDEN;
96 #ifdef CONFIG_OPENGL
97 if (scon->opengl) {
98 flags |= SDL_WINDOW_OPENGL;
100 #endif
102 scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
103 SDL_WINDOWPOS_UNDEFINED,
104 surface_width(scon->surface),
105 surface_height(scon->surface),
106 flags);
107 if (scon->opengl) {
108 const char *driver = "opengl";
110 if (scon->opts->gl == DISPLAY_GL_MODE_ES) {
111 driver = "opengles2";
114 SDL_SetHint(SDL_HINT_RENDER_DRIVER, driver);
115 SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
117 scon->winctx = SDL_GL_CreateContext(scon->real_window);
118 SDL_GL_SetSwapInterval(0);
119 } else {
120 /* The SDL renderer is only used by sdl2-2D, when OpenGL is disabled */
121 scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
123 sdl_update_caption(scon);
126 void sdl2_window_destroy(struct sdl2_console *scon)
128 if (!scon->real_window) {
129 return;
132 if (scon->winctx) {
133 SDL_GL_DeleteContext(scon->winctx);
134 scon->winctx = NULL;
136 if (scon->real_renderer) {
137 SDL_DestroyRenderer(scon->real_renderer);
138 scon->real_renderer = NULL;
140 SDL_DestroyWindow(scon->real_window);
141 scon->real_window = NULL;
144 void sdl2_window_resize(struct sdl2_console *scon)
146 if (!scon->real_window) {
147 return;
150 SDL_SetWindowSize(scon->real_window,
151 surface_width(scon->surface),
152 surface_height(scon->surface));
155 static void sdl2_redraw(struct sdl2_console *scon)
157 if (scon->opengl) {
158 #ifdef CONFIG_OPENGL
159 sdl2_gl_redraw(scon);
160 #endif
161 } else {
162 sdl2_2d_redraw(scon);
166 static void sdl_update_caption(struct sdl2_console *scon)
168 char win_title[1024];
169 char icon_title[1024];
170 const char *status = "";
172 if (!runstate_is_running()) {
173 status = " [Stopped]";
174 } else if (gui_grab) {
175 if (alt_grab) {
176 #ifdef CONFIG_DARWIN
177 status = " - Press ⌃⌥⇧G to exit grab";
178 #else
179 status = " - Press Ctrl-Alt-Shift-G to exit grab";
180 #endif
181 } else if (ctrl_grab) {
182 status = " - Press Right-Ctrl-G to exit grab";
183 } else {
184 #ifdef CONFIG_DARWIN
185 status = " - Press ⌃⌥G to exit grab";
186 #else
187 status = " - Press Ctrl-Alt-G to exit grab";
188 #endif
192 if (qemu_name) {
193 snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
194 scon->idx, status);
195 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
196 } else {
197 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
198 snprintf(icon_title, sizeof(icon_title), "QEMU");
201 if (scon->real_window) {
202 SDL_SetWindowTitle(scon->real_window, win_title);
206 static void sdl_hide_cursor(struct sdl2_console *scon)
208 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
209 return;
212 SDL_ShowCursor(SDL_DISABLE);
213 SDL_SetCursor(sdl_cursor_hidden);
215 if (!qemu_input_is_absolute(scon->dcl.con)) {
216 SDL_SetRelativeMouseMode(SDL_TRUE);
220 static void sdl_show_cursor(struct sdl2_console *scon)
222 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
223 return;
226 if (!qemu_input_is_absolute(scon->dcl.con)) {
227 SDL_SetRelativeMouseMode(SDL_FALSE);
230 if (guest_cursor &&
231 (gui_grab || qemu_input_is_absolute(scon->dcl.con) || absolute_enabled)) {
232 SDL_SetCursor(guest_sprite);
233 } else {
234 SDL_SetCursor(sdl_cursor_normal);
237 SDL_ShowCursor(SDL_ENABLE);
240 static void sdl_grab_start(struct sdl2_console *scon)
242 QemuConsole *con = scon ? scon->dcl.con : NULL;
244 if (!con || !qemu_console_is_graphic(con)) {
245 return;
248 * If the application is not active, do not try to enter grab state. This
249 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
250 * application (SDL bug).
252 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
253 return;
255 if (guest_cursor) {
256 SDL_SetCursor(guest_sprite);
257 if (!qemu_input_is_absolute(scon->dcl.con) && !absolute_enabled) {
258 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
260 } else {
261 sdl_hide_cursor(scon);
263 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
264 gui_grab = 1;
265 win32_kbd_set_grab(true);
266 sdl_update_caption(scon);
269 static void sdl_grab_end(struct sdl2_console *scon)
271 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
272 gui_grab = 0;
273 win32_kbd_set_grab(false);
274 sdl_show_cursor(scon);
275 sdl_update_caption(scon);
278 static void absolute_mouse_grab(struct sdl2_console *scon)
280 int mouse_x, mouse_y;
281 int scr_w, scr_h;
282 SDL_GetMouseState(&mouse_x, &mouse_y);
283 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
284 if (mouse_x > 0 && mouse_x < scr_w - 1 &&
285 mouse_y > 0 && mouse_y < scr_h - 1) {
286 sdl_grab_start(scon);
290 static void sdl_mouse_mode_change(Notifier *notify, void *data)
292 if (qemu_input_is_absolute(sdl2_console[0].dcl.con)) {
293 if (!absolute_enabled) {
294 absolute_enabled = 1;
295 SDL_SetRelativeMouseMode(SDL_FALSE);
296 absolute_mouse_grab(&sdl2_console[0]);
298 } else if (absolute_enabled) {
299 if (!gui_fullscreen) {
300 sdl_grab_end(&sdl2_console[0]);
302 absolute_enabled = 0;
306 static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
307 int x, int y, int state)
309 static uint32_t bmap[INPUT_BUTTON__MAX] = {
310 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
311 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
312 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
313 [INPUT_BUTTON_SIDE] = SDL_BUTTON(SDL_BUTTON_X1),
314 [INPUT_BUTTON_EXTRA] = SDL_BUTTON(SDL_BUTTON_X2)
316 static uint32_t prev_state;
318 if (prev_state != state) {
319 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
320 prev_state = state;
323 if (qemu_input_is_absolute(scon->dcl.con)) {
324 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X,
325 x, 0, surface_width(scon->surface));
326 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y,
327 y, 0, surface_height(scon->surface));
328 } else {
329 if (guest_cursor) {
330 x -= guest_x;
331 y -= guest_y;
332 guest_x += x;
333 guest_y += y;
334 dx = x;
335 dy = y;
337 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
338 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
340 qemu_input_event_sync();
343 static void toggle_full_screen(struct sdl2_console *scon)
345 gui_fullscreen = !gui_fullscreen;
346 if (gui_fullscreen) {
347 SDL_SetWindowFullscreen(scon->real_window,
348 SDL_WINDOW_FULLSCREEN_DESKTOP);
349 gui_saved_grab = gui_grab;
350 sdl_grab_start(scon);
351 } else {
352 if (!gui_saved_grab) {
353 sdl_grab_end(scon);
355 SDL_SetWindowFullscreen(scon->real_window, 0);
357 sdl2_redraw(scon);
360 static int get_mod_state(void)
362 SDL_Keymod mod = SDL_GetModState();
364 if (alt_grab) {
365 return (mod & (gui_grab_code | KMOD_LSHIFT)) ==
366 (gui_grab_code | KMOD_LSHIFT);
367 } else if (ctrl_grab) {
368 return (mod & KMOD_RCTRL) == KMOD_RCTRL;
369 } else {
370 return (mod & gui_grab_code) == gui_grab_code;
374 static void *sdl2_win32_get_hwnd(struct sdl2_console *scon)
376 #ifdef CONFIG_WIN32
377 SDL_SysWMinfo info;
379 SDL_VERSION(&info.version);
380 if (SDL_GetWindowWMInfo(scon->real_window, &info)) {
381 return info.info.win.window;
383 #endif
384 return NULL;
387 static void handle_keydown(SDL_Event *ev)
389 int win;
390 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
391 int gui_key_modifier_pressed = get_mod_state();
393 if (!scon) {
394 return;
397 scon->gui_keysym = false;
399 if (!scon->ignore_hotkeys && gui_key_modifier_pressed && !ev->key.repeat) {
400 switch (ev->key.keysym.scancode) {
401 case SDL_SCANCODE_2:
402 case SDL_SCANCODE_3:
403 case SDL_SCANCODE_4:
404 case SDL_SCANCODE_5:
405 case SDL_SCANCODE_6:
406 case SDL_SCANCODE_7:
407 case SDL_SCANCODE_8:
408 case SDL_SCANCODE_9:
409 if (gui_grab) {
410 sdl_grab_end(scon);
413 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
414 if (win < sdl2_num_outputs) {
415 sdl2_console[win].hidden = !sdl2_console[win].hidden;
416 if (sdl2_console[win].real_window) {
417 if (sdl2_console[win].hidden) {
418 SDL_HideWindow(sdl2_console[win].real_window);
419 } else {
420 SDL_ShowWindow(sdl2_console[win].real_window);
423 sdl2_release_modifiers(scon);
424 scon->gui_keysym = true;
426 break;
427 case SDL_SCANCODE_F:
428 toggle_full_screen(scon);
429 scon->gui_keysym = true;
430 break;
431 case SDL_SCANCODE_G:
432 scon->gui_keysym = true;
433 if (!gui_grab) {
434 sdl_grab_start(scon);
435 } else if (!gui_fullscreen) {
436 sdl_grab_end(scon);
438 break;
439 case SDL_SCANCODE_U:
440 sdl2_window_resize(scon);
441 if (!scon->opengl) {
442 /* re-create scon->texture */
443 sdl2_2d_switch(&scon->dcl, scon->surface);
445 scon->gui_keysym = true;
446 break;
447 #if 0
448 case SDL_SCANCODE_KP_PLUS:
449 case SDL_SCANCODE_KP_MINUS:
450 if (!gui_fullscreen) {
451 int scr_w, scr_h;
452 int width, height;
453 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
455 width = MAX(scr_w + (ev->key.keysym.scancode ==
456 SDL_SCANCODE_KP_PLUS ? 50 : -50),
457 160);
458 height = (surface_height(scon->surface) * width) /
459 surface_width(scon->surface);
460 fprintf(stderr, "%s: scale to %dx%d\n",
461 __func__, width, height);
462 sdl_scale(scon, width, height);
463 sdl2_redraw(scon);
464 scon->gui_keysym = true;
466 #endif
467 default:
468 break;
471 if (!scon->gui_keysym) {
472 sdl2_process_key(scon, &ev->key);
476 static void handle_keyup(SDL_Event *ev)
478 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
480 if (!scon) {
481 return;
484 scon->ignore_hotkeys = false;
485 sdl2_process_key(scon, &ev->key);
488 static void handle_textinput(SDL_Event *ev)
490 struct sdl2_console *scon = get_scon_from_window(ev->text.windowID);
491 QemuConsole *con = scon ? scon->dcl.con : NULL;
493 if (!con) {
494 return;
497 if (!scon->gui_keysym && QEMU_IS_TEXT_CONSOLE(con)) {
498 qemu_text_console_put_string(QEMU_TEXT_CONSOLE(con), ev->text.text, strlen(ev->text.text));
502 static void handle_mousemotion(SDL_Event *ev)
504 int max_x, max_y;
505 struct sdl2_console *scon = get_scon_from_window(ev->motion.windowID);
507 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
508 return;
511 if (qemu_input_is_absolute(scon->dcl.con) || absolute_enabled) {
512 int scr_w, scr_h;
513 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
514 max_x = scr_w - 1;
515 max_y = scr_h - 1;
516 if (gui_grab && !gui_fullscreen
517 && (ev->motion.x == 0 || ev->motion.y == 0 ||
518 ev->motion.x == max_x || ev->motion.y == max_y)) {
519 sdl_grab_end(scon);
521 if (!gui_grab &&
522 (ev->motion.x > 0 && ev->motion.x < max_x &&
523 ev->motion.y > 0 && ev->motion.y < max_y)) {
524 sdl_grab_start(scon);
527 if (gui_grab || qemu_input_is_absolute(scon->dcl.con) || absolute_enabled) {
528 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
529 ev->motion.x, ev->motion.y, ev->motion.state);
533 static void handle_mousebutton(SDL_Event *ev)
535 int buttonstate = SDL_GetMouseState(NULL, NULL);
536 SDL_MouseButtonEvent *bev;
537 struct sdl2_console *scon = get_scon_from_window(ev->button.windowID);
539 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
540 return;
543 bev = &ev->button;
544 if (!gui_grab && !qemu_input_is_absolute(scon->dcl.con)) {
545 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
546 /* start grabbing all events */
547 sdl_grab_start(scon);
549 } else {
550 if (ev->type == SDL_MOUSEBUTTONDOWN) {
551 buttonstate |= SDL_BUTTON(bev->button);
552 } else {
553 buttonstate &= ~SDL_BUTTON(bev->button);
555 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
559 static void handle_mousewheel(SDL_Event *ev)
561 struct sdl2_console *scon = get_scon_from_window(ev->wheel.windowID);
562 SDL_MouseWheelEvent *wev = &ev->wheel;
563 InputButton btn;
565 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
566 return;
569 if (wev->y > 0) {
570 btn = INPUT_BUTTON_WHEEL_UP;
571 } else if (wev->y < 0) {
572 btn = INPUT_BUTTON_WHEEL_DOWN;
573 } else if (wev->x < 0) {
574 btn = INPUT_BUTTON_WHEEL_RIGHT;
575 } else if (wev->x > 0) {
576 btn = INPUT_BUTTON_WHEEL_LEFT;
577 } else {
578 return;
581 qemu_input_queue_btn(scon->dcl.con, btn, true);
582 qemu_input_event_sync();
583 qemu_input_queue_btn(scon->dcl.con, btn, false);
584 qemu_input_event_sync();
587 static void handle_windowevent(SDL_Event *ev)
589 struct sdl2_console *scon = get_scon_from_window(ev->window.windowID);
590 bool allow_close = true;
592 if (!scon) {
593 return;
596 switch (ev->window.event) {
597 case SDL_WINDOWEVENT_RESIZED:
599 QemuUIInfo info;
600 memset(&info, 0, sizeof(info));
601 info.width = ev->window.data1;
602 info.height = ev->window.data2;
603 dpy_set_ui_info(scon->dcl.con, &info, true);
605 sdl2_redraw(scon);
606 break;
607 case SDL_WINDOWEVENT_EXPOSED:
608 sdl2_redraw(scon);
609 break;
610 case SDL_WINDOWEVENT_FOCUS_GAINED:
611 win32_kbd_set_grab(gui_grab);
612 if (qemu_console_is_graphic(scon->dcl.con)) {
613 win32_kbd_set_window(sdl2_win32_get_hwnd(scon));
615 /* fall through */
616 case SDL_WINDOWEVENT_ENTER:
617 if (!gui_grab && (qemu_input_is_absolute(scon->dcl.con) || absolute_enabled)) {
618 absolute_mouse_grab(scon);
620 /* If a new console window opened using a hotkey receives the
621 * focus, SDL sends another KEYDOWN event to the new window,
622 * closing the console window immediately after.
624 * Work around this by ignoring further hotkey events until a
625 * key is released.
627 scon->ignore_hotkeys = get_mod_state();
628 break;
629 case SDL_WINDOWEVENT_FOCUS_LOST:
630 if (qemu_console_is_graphic(scon->dcl.con)) {
631 win32_kbd_set_window(NULL);
633 if (gui_grab && !gui_fullscreen) {
634 sdl_grab_end(scon);
636 break;
637 case SDL_WINDOWEVENT_RESTORED:
638 update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT);
639 break;
640 case SDL_WINDOWEVENT_MINIMIZED:
641 update_displaychangelistener(&scon->dcl, 500);
642 break;
643 case SDL_WINDOWEVENT_CLOSE:
644 if (qemu_console_is_graphic(scon->dcl.con)) {
645 if (scon->opts->has_window_close && !scon->opts->window_close) {
646 allow_close = false;
648 if (allow_close) {
649 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
650 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
652 } else {
653 SDL_HideWindow(scon->real_window);
654 scon->hidden = true;
656 break;
657 case SDL_WINDOWEVENT_SHOWN:
658 scon->hidden = false;
659 break;
660 case SDL_WINDOWEVENT_HIDDEN:
661 scon->hidden = true;
662 break;
666 void sdl2_poll_events(struct sdl2_console *scon)
668 SDL_Event ev1, *ev = &ev1;
669 bool allow_close = true;
670 int idle = 1;
672 if (scon->last_vm_running != runstate_is_running()) {
673 scon->last_vm_running = runstate_is_running();
674 sdl_update_caption(scon);
677 while (SDL_PollEvent(ev)) {
678 switch (ev->type) {
679 case SDL_KEYDOWN:
680 idle = 0;
681 handle_keydown(ev);
682 break;
683 case SDL_KEYUP:
684 idle = 0;
685 handle_keyup(ev);
686 break;
687 case SDL_TEXTINPUT:
688 idle = 0;
689 handle_textinput(ev);
690 break;
691 case SDL_QUIT:
692 if (scon->opts->has_window_close && !scon->opts->window_close) {
693 allow_close = false;
695 if (allow_close) {
696 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
697 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
699 break;
700 case SDL_MOUSEMOTION:
701 idle = 0;
702 handle_mousemotion(ev);
703 break;
704 case SDL_MOUSEBUTTONDOWN:
705 case SDL_MOUSEBUTTONUP:
706 idle = 0;
707 handle_mousebutton(ev);
708 break;
709 case SDL_MOUSEWHEEL:
710 idle = 0;
711 handle_mousewheel(ev);
712 break;
713 case SDL_WINDOWEVENT:
714 handle_windowevent(ev);
715 break;
716 default:
717 break;
721 if (idle) {
722 if (scon->idle_counter < SDL2_MAX_IDLE_COUNT) {
723 scon->idle_counter++;
724 if (scon->idle_counter >= SDL2_MAX_IDLE_COUNT) {
725 scon->dcl.update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
728 } else {
729 scon->idle_counter = 0;
730 scon->dcl.update_interval = SDL2_REFRESH_INTERVAL_BUSY;
734 static void sdl_mouse_warp(DisplayChangeListener *dcl,
735 int x, int y, bool on)
737 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
739 if (!qemu_console_is_graphic(scon->dcl.con)) {
740 return;
743 if (on) {
744 if (!guest_cursor) {
745 sdl_show_cursor(scon);
747 if (gui_grab || qemu_input_is_absolute(scon->dcl.con) || absolute_enabled) {
748 SDL_SetCursor(guest_sprite);
749 if (!qemu_input_is_absolute(scon->dcl.con) && !absolute_enabled) {
750 SDL_WarpMouseInWindow(scon->real_window, x, y);
753 } else if (gui_grab) {
754 sdl_hide_cursor(scon);
756 guest_cursor = on;
757 guest_x = x, guest_y = y;
760 static void sdl_mouse_define(DisplayChangeListener *dcl,
761 QEMUCursor *c)
764 if (guest_sprite) {
765 SDL_FreeCursor(guest_sprite);
768 if (guest_sprite_surface) {
769 SDL_FreeSurface(guest_sprite_surface);
772 guest_sprite_surface =
773 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
774 0xff0000, 0x00ff00, 0xff, 0xff000000);
776 if (!guest_sprite_surface) {
777 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
778 return;
780 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
781 c->hot_x, c->hot_y);
782 if (!guest_sprite) {
783 fprintf(stderr, "Failed to make color cursor from %p\n", c);
784 return;
786 if (guest_cursor &&
787 (gui_grab || qemu_input_is_absolute(dcl->con) || absolute_enabled)) {
788 SDL_SetCursor(guest_sprite);
792 static void sdl_cleanup(void)
794 if (guest_sprite) {
795 SDL_FreeCursor(guest_sprite);
797 SDL_QuitSubSystem(SDL_INIT_VIDEO);
800 static const DisplayChangeListenerOps dcl_2d_ops = {
801 .dpy_name = "sdl2-2d",
802 .dpy_gfx_update = sdl2_2d_update,
803 .dpy_gfx_switch = sdl2_2d_switch,
804 .dpy_gfx_check_format = sdl2_2d_check_format,
805 .dpy_refresh = sdl2_2d_refresh,
806 .dpy_mouse_set = sdl_mouse_warp,
807 .dpy_cursor_define = sdl_mouse_define,
810 #ifdef CONFIG_OPENGL
811 static const DisplayChangeListenerOps dcl_gl_ops = {
812 .dpy_name = "sdl2-gl",
813 .dpy_gfx_update = sdl2_gl_update,
814 .dpy_gfx_switch = sdl2_gl_switch,
815 .dpy_gfx_check_format = console_gl_check_format,
816 .dpy_refresh = sdl2_gl_refresh,
817 .dpy_mouse_set = sdl_mouse_warp,
818 .dpy_cursor_define = sdl_mouse_define,
820 .dpy_gl_scanout_disable = sdl2_gl_scanout_disable,
821 .dpy_gl_scanout_texture = sdl2_gl_scanout_texture,
822 .dpy_gl_update = sdl2_gl_scanout_flush,
825 static bool
826 sdl2_gl_is_compatible_dcl(DisplayGLCtx *dgc,
827 DisplayChangeListener *dcl)
829 return dcl->ops == &dcl_gl_ops;
832 static const DisplayGLCtxOps gl_ctx_ops = {
833 .dpy_gl_ctx_is_compatible_dcl = sdl2_gl_is_compatible_dcl,
834 .dpy_gl_ctx_create = sdl2_gl_create_context,
835 .dpy_gl_ctx_destroy = sdl2_gl_destroy_context,
836 .dpy_gl_ctx_make_current = sdl2_gl_make_context_current,
838 #endif
840 static void sdl2_display_early_init(DisplayOptions *o)
842 assert(o->type == DISPLAY_TYPE_SDL);
843 if (o->has_gl && o->gl) {
844 #ifdef CONFIG_OPENGL
845 display_opengl = 1;
846 #endif
850 static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
852 uint8_t data = 0;
853 int i;
854 SDL_SysWMinfo info;
855 SDL_Surface *icon = NULL;
856 char *dir;
858 assert(o->type == DISPLAY_TYPE_SDL);
860 if (SDL_GetHintBoolean("QEMU_ENABLE_SDL_LOGGING", SDL_FALSE)) {
861 SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
864 if (SDL_Init(SDL_INIT_VIDEO)) {
865 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
866 SDL_GetError());
867 exit(1);
869 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
870 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
871 #endif
872 #ifndef CONFIG_WIN32
873 /* QEMU uses its own low level keyboard hook procedure on Windows */
874 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
875 #endif
876 #ifdef SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED
877 SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "0");
878 #endif
879 SDL_SetHint(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4, "1");
880 SDL_EnableScreenSaver();
881 memset(&info, 0, sizeof(info));
882 SDL_VERSION(&info.version);
884 gui_fullscreen = o->has_full_screen && o->full_screen;
886 if (o->u.sdl.has_grab_mod) {
887 if (o->u.sdl.grab_mod == HOT_KEY_MOD_LSHIFT_LCTRL_LALT) {
888 alt_grab = true;
889 } else if (o->u.sdl.grab_mod == HOT_KEY_MOD_RCTRL) {
890 ctrl_grab = true;
894 for (i = 0;; i++) {
895 QemuConsole *con = qemu_console_lookup_by_index(i);
896 if (!con) {
897 break;
900 sdl2_num_outputs = i;
901 if (sdl2_num_outputs == 0) {
902 return;
904 sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
905 for (i = 0; i < sdl2_num_outputs; i++) {
906 QemuConsole *con = qemu_console_lookup_by_index(i);
907 assert(con != NULL);
908 if (!qemu_console_is_graphic(con) &&
909 qemu_console_get_index(con) != 0) {
910 sdl2_console[i].hidden = true;
912 sdl2_console[i].idx = i;
913 sdl2_console[i].opts = o;
914 #ifdef CONFIG_OPENGL
915 sdl2_console[i].opengl = display_opengl;
916 sdl2_console[i].dcl.ops = display_opengl ? &dcl_gl_ops : &dcl_2d_ops;
917 sdl2_console[i].dgc.ops = display_opengl ? &gl_ctx_ops : NULL;
918 #else
919 sdl2_console[i].opengl = 0;
920 sdl2_console[i].dcl.ops = &dcl_2d_ops;
921 #endif
922 sdl2_console[i].dcl.con = con;
923 sdl2_console[i].kbd = qkbd_state_init(con);
924 if (display_opengl) {
925 qemu_console_set_display_gl_ctx(con, &sdl2_console[i].dgc);
927 register_displaychangelistener(&sdl2_console[i].dcl);
929 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
930 if (SDL_GetWindowWMInfo(sdl2_console[i].real_window, &info)) {
931 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
932 qemu_console_set_window_id(con, (uintptr_t)info.info.win.window);
933 #elif defined(SDL_VIDEO_DRIVER_X11)
934 qemu_console_set_window_id(con, info.info.x11.window);
935 #endif
937 #endif
940 #ifdef CONFIG_SDL_IMAGE
941 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/128x128/apps/qemu.png");
942 icon = IMG_Load(dir);
943 #else
944 /* Load a 32x32x4 image. White pixels are transparent. */
945 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/32x32/apps/qemu.bmp");
946 icon = SDL_LoadBMP(dir);
947 if (icon) {
948 uint32_t colorkey = SDL_MapRGB(icon->format, 255, 255, 255);
949 SDL_SetColorKey(icon, SDL_TRUE, colorkey);
951 #endif
952 g_free(dir);
953 if (icon) {
954 SDL_SetWindowIcon(sdl2_console[0].real_window, icon);
957 mouse_mode_notifier.notify = sdl_mouse_mode_change;
958 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
960 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
961 sdl_cursor_normal = SDL_GetCursor();
963 if (gui_fullscreen) {
964 sdl_grab_start(&sdl2_console[0]);
967 atexit(sdl_cleanup);
970 static QemuDisplay qemu_display_sdl2 = {
971 .type = DISPLAY_TYPE_SDL,
972 .early_init = sdl2_display_early_init,
973 .init = sdl2_display_init,
976 static void register_sdl1(void)
978 qemu_display_register(&qemu_display_sdl2);
981 type_init(register_sdl1);
983 #ifdef CONFIG_OPENGL
984 module_dep("ui-opengl");
985 #endif