blob: bd4f5a9da14a997c3b2d707baf1e71cf6e98efb3 [file] [log] [blame]
Dave Airlie47c03742013-12-10 14:05:51 +10001/*
2 * QEMU SDL display driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24/* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
25
Peter Maydelle16f4c82016-01-29 17:49:51 +000026#include "qemu/osdep.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020027#include "qemu/module.h"
Paolo Bonzini77d910f2020-08-18 11:59:27 +020028#include "qemu/cutils.h"
Dave Airlie47c03742013-12-10 14:05:51 +100029#include "ui/console.h"
30#include "ui/input.h"
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +010031#include "ui/sdl2.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020032#include "sysemu/runstate.h"
Alejandro Jimeneze6dba042020-12-11 11:52:43 -050033#include "sysemu/runstate-action.h"
Dave Airlie47c03742013-12-10 14:05:51 +100034#include "sysemu/sysemu.h"
Volker Rümelin83047342020-05-16 09:20:09 +020035#include "ui/win32-kbd-hook.h"
Dmitry Petroved80f502022-01-08 16:39:46 +010036#include "qemu/log.h"
Dave Airlie47c03742013-12-10 14:05:51 +100037
Dave Airlie47c03742013-12-10 14:05:51 +100038static int sdl2_num_outputs;
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +010039static struct sdl2_console *sdl2_console;
Dave Airlie47c03742013-12-10 14:05:51 +100040
41static SDL_Surface *guest_sprite_surface;
42static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
Thomas Huth9eafdee2022-05-19 17:56:24 +020043static bool alt_grab;
44static bool ctrl_grab;
Dave Airlie47c03742013-12-10 14:05:51 +100045
Dave Airlie47c03742013-12-10 14:05:51 +100046static int gui_saved_grab;
47static int gui_fullscreen;
Dave Airlie47c03742013-12-10 14:05:51 +100048static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
Dave Airlie47c03742013-12-10 14:05:51 +100049static SDL_Cursor *sdl_cursor_normal;
50static SDL_Cursor *sdl_cursor_hidden;
51static int absolute_enabled;
Akihiko Odakia418e7a2024-07-15 14:25:43 +090052static bool guest_cursor;
Dave Airlie47c03742013-12-10 14:05:51 +100053static int guest_x, guest_y;
54static SDL_Cursor *guest_sprite;
Dave Airlie47c03742013-12-10 14:05:51 +100055static Notifier mouse_mode_notifier;
56
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +010057#define SDL2_REFRESH_INTERVAL_BUSY 10
58#define SDL2_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
59 / SDL2_REFRESH_INTERVAL_BUSY + 1)
60
Marc-André Lureauda3f7a32023-02-15 18:29:29 +040061/* introduced in SDL 2.0.10 */
62#ifndef SDL_HINT_RENDER_BATCHING
63#define SDL_HINT_RENDER_BATCHING "SDL_RENDER_BATCHING"
64#endif
65
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +010066static void sdl_update_caption(struct sdl2_console *scon);
Dave Airlie47c03742013-12-10 14:05:51 +100067
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +010068static struct sdl2_console *get_scon_from_window(uint32_t window_id)
Dave Airlie47c03742013-12-10 14:05:51 +100069{
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];
74 }
75 }
76 return NULL;
77}
78
Gerd Hoffmann2c3056f2014-11-11 13:22:49 +010079void sdl2_window_create(struct sdl2_console *scon)
Dave Airlie47c03742013-12-10 14:05:51 +100080{
Gerd Hoffmann46522a82014-11-11 13:12:02 +010081 int flags = 0;
Dave Airlie47c03742013-12-10 14:05:51 +100082
Gerd Hoffmann46522a82014-11-11 13:12:02 +010083 if (!scon->surface) {
84 return;
Dave Airlie47c03742013-12-10 14:05:51 +100085 }
Gerd Hoffmann46522a82014-11-11 13:12:02 +010086 assert(!scon->real_window);
87
88 if (gui_fullscreen) {
89 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
90 } else {
91 flags |= SDL_WINDOW_RESIZABLE;
92 }
93 if (scon->hidden) {
94 flags |= SDL_WINDOW_HIDDEN;
95 }
Jan Henrik Weinstock67c6f1d2020-10-04 12:42:21 +020096#ifdef CONFIG_OPENGL
97 if (scon->opengl) {
98 flags |= SDL_WINDOW_OPENGL;
99 }
100#endif
Gerd Hoffmann46522a82014-11-11 13:12:02 +0100101
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);
Marc-André Lureauda3f7a32023-02-15 18:29:29 +0400107 if (scon->opengl) {
108 const char *driver = "opengl";
109
Markus Armbruster154fd4d2024-09-04 13:18:25 +0200110 if (scon->opts->gl == DISPLAY_GL_MODE_ES) {
Marc-André Lureauda3f7a32023-02-15 18:29:29 +0400111 driver = "opengles2";
112 }
113
114 SDL_SetHint(SDL_HINT_RENDER_DRIVER, driver);
115 SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
Marc-André Lureauda3f7a32023-02-15 18:29:29 +0400116
Marc-André Lureau6f2688f2023-01-24 17:47:18 +0400117 scon->winctx = SDL_GL_CreateContext(scon->real_window);
Gert Wollnyae23cd02024-09-11 09:14:30 +0000118 SDL_GL_SetSwapInterval(0);
Antonio Caggiano176e3782023-06-12 11:19:59 +0200119 } 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);
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100122 }
Gerd Hoffmann46522a82014-11-11 13:12:02 +0100123 sdl_update_caption(scon);
124}
125
Gerd Hoffmann2c3056f2014-11-11 13:22:49 +0100126void sdl2_window_destroy(struct sdl2_console *scon)
Gerd Hoffmann46522a82014-11-11 13:12:02 +0100127{
128 if (!scon->real_window) {
129 return;
130 }
131
Antonio Caggiano176e3782023-06-12 11:19:59 +0200132 if (scon->winctx) {
133 SDL_GL_DeleteContext(scon->winctx);
134 scon->winctx = NULL;
135 }
136 if (scon->real_renderer) {
137 SDL_DestroyRenderer(scon->real_renderer);
138 scon->real_renderer = NULL;
139 }
Gerd Hoffmann46522a82014-11-11 13:12:02 +0100140 SDL_DestroyWindow(scon->real_window);
141 scon->real_window = NULL;
142}
143
Gerd Hoffmann2c3056f2014-11-11 13:22:49 +0100144void sdl2_window_resize(struct sdl2_console *scon)
Gerd Hoffmann46522a82014-11-11 13:12:02 +0100145{
146 if (!scon->real_window) {
147 return;
148 }
149
150 SDL_SetWindowSize(scon->real_window,
151 surface_width(scon->surface),
152 surface_height(scon->surface));
Dave Airlie47c03742013-12-10 14:05:51 +1000153}
154
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100155static void sdl2_redraw(struct sdl2_console *scon)
156{
157 if (scon->opengl) {
158#ifdef CONFIG_OPENGL
159 sdl2_gl_redraw(scon);
160#endif
161 } else {
162 sdl2_2d_redraw(scon);
163 }
164}
165
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +0100166static void sdl_update_caption(struct sdl2_console *scon)
Dave Airlie47c03742013-12-10 14:05:51 +1000167{
168 char win_title[1024];
169 char icon_title[1024];
170 const char *status = "";
171
172 if (!runstate_is_running()) {
173 status = " [Stopped]";
174 } else if (gui_grab) {
175 if (alt_grab) {
Adrian Wowk547ec5a2023-10-29 22:41:19 -0400176#ifdef CONFIG_DARWIN
177 status = " - Press ⌃⌥⇧G to exit grab";
178#else
Gerd Hoffmannf8d2c932018-01-15 16:48:54 +0100179 status = " - Press Ctrl-Alt-Shift-G to exit grab";
Adrian Wowk547ec5a2023-10-29 22:41:19 -0400180#endif
Dave Airlie47c03742013-12-10 14:05:51 +1000181 } else if (ctrl_grab) {
Gerd Hoffmannf8d2c932018-01-15 16:48:54 +0100182 status = " - Press Right-Ctrl-G to exit grab";
Dave Airlie47c03742013-12-10 14:05:51 +1000183 } else {
Adrian Wowk547ec5a2023-10-29 22:41:19 -0400184#ifdef CONFIG_DARWIN
185 status = " - Press ⌃⌥G to exit grab";
186#else
Gerd Hoffmannf8d2c932018-01-15 16:48:54 +0100187 status = " - Press Ctrl-Alt-G to exit grab";
Adrian Wowk547ec5a2023-10-29 22:41:19 -0400188#endif
Dave Airlie47c03742013-12-10 14:05:51 +1000189 }
190 }
191
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");
199 }
200
201 if (scon->real_window) {
202 SDL_SetWindowTitle(scon->real_window, win_title);
203 }
204}
205
Gerd Hoffmann86a088e2020-01-31 12:35:21 +0100206static void sdl_hide_cursor(struct sdl2_console *scon)
Dave Airlie47c03742013-12-10 14:05:51 +1000207{
Gerd Hoffmann86a088e2020-01-31 12:35:21 +0100208 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
Dave Airlie47c03742013-12-10 14:05:51 +1000209 return;
210 }
211
Jindrich Makovicka253347e2017-11-12 20:30:27 +0100212 SDL_ShowCursor(SDL_DISABLE);
213 SDL_SetCursor(sdl_cursor_hidden);
214
Akihiko Odaki0337e412023-09-21 17:29:34 +0900215 if (!qemu_input_is_absolute(scon->dcl.con)) {
Cole Robinson2d968ff2014-04-01 16:37:11 -0400216 SDL_SetRelativeMouseMode(SDL_TRUE);
Dave Airlie47c03742013-12-10 14:05:51 +1000217 }
218}
219
Gerd Hoffmann86a088e2020-01-31 12:35:21 +0100220static void sdl_show_cursor(struct sdl2_console *scon)
Dave Airlie47c03742013-12-10 14:05:51 +1000221{
Gerd Hoffmann86a088e2020-01-31 12:35:21 +0100222 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
Dave Airlie47c03742013-12-10 14:05:51 +1000223 return;
224 }
225
Akihiko Odaki0337e412023-09-21 17:29:34 +0900226 if (!qemu_input_is_absolute(scon->dcl.con)) {
Cole Robinson2d968ff2014-04-01 16:37:11 -0400227 SDL_SetRelativeMouseMode(SDL_FALSE);
Dave Airlie47c03742013-12-10 14:05:51 +1000228 }
Jindrich Makovicka253347e2017-11-12 20:30:27 +0100229
230 if (guest_cursor &&
Akihiko Odaki0337e412023-09-21 17:29:34 +0900231 (gui_grab || qemu_input_is_absolute(scon->dcl.con) || absolute_enabled)) {
Jindrich Makovicka253347e2017-11-12 20:30:27 +0100232 SDL_SetCursor(guest_sprite);
233 } else {
234 SDL_SetCursor(sdl_cursor_normal);
235 }
236
237 SDL_ShowCursor(SDL_ENABLE);
Dave Airlie47c03742013-12-10 14:05:51 +1000238}
239
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +0100240static void sdl_grab_start(struct sdl2_console *scon)
Dave Airlie47c03742013-12-10 14:05:51 +1000241{
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200242 QemuConsole *con = scon ? scon->dcl.con : NULL;
243
244 if (!con || !qemu_console_is_graphic(con)) {
245 return;
246 }
Dave Airlie47c03742013-12-10 14:05:51 +1000247 /*
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).
251 */
252 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
253 return;
254 }
255 if (guest_cursor) {
256 SDL_SetCursor(guest_sprite);
Akihiko Odaki0337e412023-09-21 17:29:34 +0900257 if (!qemu_input_is_absolute(scon->dcl.con) && !absolute_enabled) {
Dave Airlie47c03742013-12-10 14:05:51 +1000258 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
259 }
260 } else {
Gerd Hoffmann86a088e2020-01-31 12:35:21 +0100261 sdl_hide_cursor(scon);
Dave Airlie47c03742013-12-10 14:05:51 +1000262 }
263 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
264 gui_grab = 1;
Volker Rümelin83047342020-05-16 09:20:09 +0200265 win32_kbd_set_grab(true);
Dave Airlie47c03742013-12-10 14:05:51 +1000266 sdl_update_caption(scon);
267}
268
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +0100269static void sdl_grab_end(struct sdl2_console *scon)
Dave Airlie47c03742013-12-10 14:05:51 +1000270{
271 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
272 gui_grab = 0;
Volker Rümelin83047342020-05-16 09:20:09 +0200273 win32_kbd_set_grab(false);
Gerd Hoffmann86a088e2020-01-31 12:35:21 +0100274 sdl_show_cursor(scon);
Dave Airlie47c03742013-12-10 14:05:51 +1000275 sdl_update_caption(scon);
276}
277
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +0100278static void absolute_mouse_grab(struct sdl2_console *scon)
Dave Airlie47c03742013-12-10 14:05:51 +1000279{
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);
287 }
288}
289
290static void sdl_mouse_mode_change(Notifier *notify, void *data)
291{
Akihiko Odaki0337e412023-09-21 17:29:34 +0900292 if (qemu_input_is_absolute(sdl2_console[0].dcl.con)) {
Dave Airlie47c03742013-12-10 14:05:51 +1000293 if (!absolute_enabled) {
294 absolute_enabled = 1;
Gerd Hoffmann8dfa3062018-02-02 13:08:03 +0100295 SDL_SetRelativeMouseMode(SDL_FALSE);
Dave Airlie47c03742013-12-10 14:05:51 +1000296 absolute_mouse_grab(&sdl2_console[0]);
297 }
298 } else if (absolute_enabled) {
299 if (!gui_fullscreen) {
300 sdl_grab_end(&sdl2_console[0]);
301 }
302 absolute_enabled = 0;
303 }
304}
305
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +0100306static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
Cole Robinson3f2fde22014-04-21 18:58:50 -0400307 int x, int y, int state)
Dave Airlie47c03742013-12-10 14:05:51 +1000308{
Eric Blake7fb1cf12015-11-18 01:52:57 -0700309 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Dave Airlie47c03742013-12-10 14:05:51 +1000310 [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),
Darrell Walisser29511062020-12-10 14:33:06 +0000313 [INPUT_BUTTON_SIDE] = SDL_BUTTON(SDL_BUTTON_X1),
314 [INPUT_BUTTON_EXTRA] = SDL_BUTTON(SDL_BUTTON_X2)
Dave Airlie47c03742013-12-10 14:05:51 +1000315 };
316 static uint32_t prev_state;
317
318 if (prev_state != state) {
319 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
320 prev_state = state;
321 }
322
Akihiko Odaki0337e412023-09-21 17:29:34 +0900323 if (qemu_input_is_absolute(scon->dcl.con)) {
Jindrich Makovickad9f06262017-11-17 12:22:57 +0100324 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));
Cole Robinsonafbc0dd2014-04-01 16:37:10 -0400328 } 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;
336 }
337 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
338 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
Dave Airlie47c03742013-12-10 14:05:51 +1000339 }
340 qemu_input_event_sync();
341}
342
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +0100343static void toggle_full_screen(struct sdl2_console *scon)
Dave Airlie47c03742013-12-10 14:05:51 +1000344{
Dave Airlie47c03742013-12-10 14:05:51 +1000345 gui_fullscreen = !gui_fullscreen;
346 if (gui_fullscreen) {
Gerd Hoffmann46522a82014-11-11 13:12:02 +0100347 SDL_SetWindowFullscreen(scon->real_window,
348 SDL_WINDOW_FULLSCREEN_DESKTOP);
Dave Airlie47c03742013-12-10 14:05:51 +1000349 gui_saved_grab = gui_grab;
350 sdl_grab_start(scon);
351 } else {
Dave Airlie47c03742013-12-10 14:05:51 +1000352 if (!gui_saved_grab) {
353 sdl_grab_end(scon);
354 }
Gerd Hoffmann46522a82014-11-11 13:12:02 +0100355 SDL_SetWindowFullscreen(scon->real_window, 0);
Dave Airlie47c03742013-12-10 14:05:51 +1000356 }
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100357 sdl2_redraw(scon);
Dave Airlie47c03742013-12-10 14:05:51 +1000358}
359
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100360static int get_mod_state(void)
Dave Airlie47c03742013-12-10 14:05:51 +1000361{
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100362 SDL_Keymod mod = SDL_GetModState();
Dave Airlie47c03742013-12-10 14:05:51 +1000363
364 if (alt_grab) {
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100365 return (mod & (gui_grab_code | KMOD_LSHIFT)) ==
Dave Airlie47c03742013-12-10 14:05:51 +1000366 (gui_grab_code | KMOD_LSHIFT);
367 } else if (ctrl_grab) {
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100368 return (mod & KMOD_RCTRL) == KMOD_RCTRL;
Dave Airlie47c03742013-12-10 14:05:51 +1000369 } else {
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100370 return (mod & gui_grab_code) == gui_grab_code;
Dave Airlie47c03742013-12-10 14:05:51 +1000371 }
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100372}
Dave Airlie47c03742013-12-10 14:05:51 +1000373
Volker Rümelin83047342020-05-16 09:20:09 +0200374static void *sdl2_win32_get_hwnd(struct sdl2_console *scon)
375{
376#ifdef CONFIG_WIN32
377 SDL_SysWMinfo info;
378
379 SDL_VERSION(&info.version);
380 if (SDL_GetWindowWMInfo(scon->real_window, &info)) {
381 return info.info.win.window;
382 }
383#endif
384 return NULL;
385}
386
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100387static void handle_keydown(SDL_Event *ev)
388{
389 int win;
390 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
Gerd Hoffmannafb92eb2018-02-20 16:04:44 +0100391 int gui_key_modifier_pressed = get_mod_state();
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100392
Changbin Du32ec9832020-04-27 21:24:12 +0800393 if (!scon) {
394 return;
395 }
396
Volker Rümelineaea8032024-09-09 08:15:52 +0200397 scon->gui_keysym = false;
398
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100399 if (!scon->ignore_hotkeys && gui_key_modifier_pressed && !ev->key.repeat) {
Dave Airlie47c03742013-12-10 14:05:51 +1000400 switch (ev->key.keysym.scancode) {
Gerd Hoffmann363f59d2014-05-27 09:44:39 +0200401 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:
Cole Robinson56f289f2016-05-06 14:03:06 -0400409 if (gui_grab) {
410 sdl_grab_end(scon);
411 }
412
Gerd Hoffmann363f59d2014-05-27 09:44:39 +0200413 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);
421 }
422 }
Volker Rümelindf3c6102024-09-09 08:15:51 +0200423 sdl2_release_modifiers(scon);
Volker Rümelineaea8032024-09-09 08:15:52 +0200424 scon->gui_keysym = true;
Gerd Hoffmann363f59d2014-05-27 09:44:39 +0200425 }
426 break;
Dave Airlie47c03742013-12-10 14:05:51 +1000427 case SDL_SCANCODE_F:
428 toggle_full_screen(scon);
Volker Rümelineaea8032024-09-09 08:15:52 +0200429 scon->gui_keysym = true;
Dave Airlie47c03742013-12-10 14:05:51 +1000430 break;
Gerd Hoffmannf8d2c932018-01-15 16:48:54 +0100431 case SDL_SCANCODE_G:
Volker Rümelineaea8032024-09-09 08:15:52 +0200432 scon->gui_keysym = true;
Gerd Hoffmannf8d2c932018-01-15 16:48:54 +0100433 if (!gui_grab) {
434 sdl_grab_start(scon);
435 } else if (!gui_fullscreen) {
436 sdl_grab_end(scon);
437 }
438 break;
Dave Airlie47c03742013-12-10 14:05:51 +1000439 case SDL_SCANCODE_U:
Amadeusz Sławiński64bf97e2018-06-13 19:27:07 +0200440 sdl2_window_resize(scon);
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100441 if (!scon->opengl) {
442 /* re-create scon->texture */
443 sdl2_2d_switch(&scon->dcl, scon->surface);
444 }
Volker Rümelineaea8032024-09-09 08:15:52 +0200445 scon->gui_keysym = true;
Dave Airlie47c03742013-12-10 14:05:51 +1000446 break;
Gerd Hoffmann46522a82014-11-11 13:12:02 +0100447#if 0
Dave Airlie47c03742013-12-10 14:05:51 +1000448 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);
454
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);
Gerd Hoffmann46522a82014-11-11 13:12:02 +0100460 fprintf(stderr, "%s: scale to %dx%d\n",
461 __func__, width, height);
Dave Airlie47c03742013-12-10 14:05:51 +1000462 sdl_scale(scon, width, height);
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100463 sdl2_redraw(scon);
Volker Rümelineaea8032024-09-09 08:15:52 +0200464 scon->gui_keysym = true;
Dave Airlie47c03742013-12-10 14:05:51 +1000465 }
Gerd Hoffmann46522a82014-11-11 13:12:02 +0100466#endif
Dave Airlie47c03742013-12-10 14:05:51 +1000467 default:
468 break;
469 }
470 }
Volker Rümelineaea8032024-09-09 08:15:52 +0200471 if (!scon->gui_keysym) {
Gerd Hoffmann8fc1a3f2014-11-11 10:58:19 +0100472 sdl2_process_key(scon, &ev->key);
Dave Airlie47c03742013-12-10 14:05:51 +1000473 }
474}
475
476static void handle_keyup(SDL_Event *ev)
477{
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +0100478 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
Dave Airlie47c03742013-12-10 14:05:51 +1000479
Changbin Du32ec9832020-04-27 21:24:12 +0800480 if (!scon) {
481 return;
482 }
483
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100484 scon->ignore_hotkeys = false;
Gerd Hoffmann07333e12019-01-22 10:28:09 +0100485 sdl2_process_key(scon, &ev->key);
Dave Airlie47c03742013-12-10 14:05:51 +1000486}
487
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200488static void handle_textinput(SDL_Event *ev)
489{
Pavel Dovgalyuk48db08c2018-06-26 09:40:17 +0300490 struct sdl2_console *scon = get_scon_from_window(ev->text.windowID);
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200491 QemuConsole *con = scon ? scon->dcl.con : NULL;
492
Changbin Du32ec9832020-04-27 21:24:12 +0800493 if (!con) {
494 return;
495 }
496
Volker Rümelineaea8032024-09-09 08:15:52 +0200497 if (!scon->gui_keysym && QEMU_IS_TEXT_CONSOLE(con)) {
Marc-André Lureaucc6ba2c2023-08-30 13:38:20 +0400498 qemu_text_console_put_string(QEMU_TEXT_CONSOLE(con), ev->text.text, strlen(ev->text.text));
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200499 }
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200500}
501
Dave Airlie47c03742013-12-10 14:05:51 +1000502static void handle_mousemotion(SDL_Event *ev)
503{
504 int max_x, max_y;
Pavel Dovgalyuk48db08c2018-06-26 09:40:17 +0300505 struct sdl2_console *scon = get_scon_from_window(ev->motion.windowID);
Dave Airlie47c03742013-12-10 14:05:51 +1000506
Pavel Dovgalyuk49213b72018-06-26 09:47:29 +0300507 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
Jindrich Makovicka28216712017-11-17 12:22:56 +0100508 return;
509 }
510
Akihiko Odaki0337e412023-09-21 17:29:34 +0900511 if (qemu_input_is_absolute(scon->dcl.con) || absolute_enabled) {
Dave Airlie47c03742013-12-10 14:05:51 +1000512 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;
Jindrich Makovicka24952842017-11-12 20:30:31 +0100516 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)) {
Dave Airlie47c03742013-12-10 14:05:51 +1000519 sdl_grab_end(scon);
520 }
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);
525 }
526 }
Akihiko Odaki0337e412023-09-21 17:29:34 +0900527 if (gui_grab || qemu_input_is_absolute(scon->dcl.con) || absolute_enabled) {
Cole Robinson3f2fde22014-04-21 18:58:50 -0400528 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
Dave Airlie47c03742013-12-10 14:05:51 +1000529 ev->motion.x, ev->motion.y, ev->motion.state);
530 }
531}
532
533static void handle_mousebutton(SDL_Event *ev)
534{
535 int buttonstate = SDL_GetMouseState(NULL, NULL);
536 SDL_MouseButtonEvent *bev;
Pavel Dovgalyuk48db08c2018-06-26 09:40:17 +0300537 struct sdl2_console *scon = get_scon_from_window(ev->button.windowID);
Dave Airlie47c03742013-12-10 14:05:51 +1000538
Pavel Dovgalyuk49213b72018-06-26 09:47:29 +0300539 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
Jindrich Makovicka28216712017-11-17 12:22:56 +0100540 return;
541 }
542
Dave Airlie47c03742013-12-10 14:05:51 +1000543 bev = &ev->button;
Akihiko Odaki0337e412023-09-21 17:29:34 +0900544 if (!gui_grab && !qemu_input_is_absolute(scon->dcl.con)) {
Dave Airlie47c03742013-12-10 14:05:51 +1000545 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
546 /* start grabbing all events */
547 sdl_grab_start(scon);
548 }
549 } else {
Dave Airlie47c03742013-12-10 14:05:51 +1000550 if (ev->type == SDL_MOUSEBUTTONDOWN) {
551 buttonstate |= SDL_BUTTON(bev->button);
552 } else {
553 buttonstate &= ~SDL_BUTTON(bev->button);
554 }
Cole Robinson3f2fde22014-04-21 18:58:50 -0400555 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
Dave Airlie47c03742013-12-10 14:05:51 +1000556 }
557}
558
Cole Robinson3f2fde22014-04-21 18:58:50 -0400559static void handle_mousewheel(SDL_Event *ev)
560{
Pavel Dovgalyuk48db08c2018-06-26 09:40:17 +0300561 struct sdl2_console *scon = get_scon_from_window(ev->wheel.windowID);
Cole Robinson3f2fde22014-04-21 18:58:50 -0400562 SDL_MouseWheelEvent *wev = &ev->wheel;
563 InputButton btn;
564
Pavel Dovgalyuk49213b72018-06-26 09:47:29 +0300565 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
Jindrich Makovicka28216712017-11-17 12:22:56 +0100566 return;
567 }
568
Cole Robinson3f2fde22014-04-21 18:58:50 -0400569 if (wev->y > 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100570 btn = INPUT_BUTTON_WHEEL_UP;
Cole Robinson3f2fde22014-04-21 18:58:50 -0400571 } else if (wev->y < 0) {
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100572 btn = INPUT_BUTTON_WHEEL_DOWN;
Dmitry Petroved80f502022-01-08 16:39:46 +0100573 } else if (wev->x < 0) {
574 btn = INPUT_BUTTON_WHEEL_RIGHT;
575 } else if (wev->x > 0) {
576 btn = INPUT_BUTTON_WHEEL_LEFT;
Cole Robinson3f2fde22014-04-21 18:58:50 -0400577 } else {
578 return;
579 }
580
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();
585}
586
Max Reitz1dfc5c82014-12-12 10:52:51 +0100587static void handle_windowevent(SDL_Event *ev)
Dave Airlie47c03742013-12-10 14:05:51 +1000588{
Max Reitz1dfc5c82014-12-12 10:52:51 +0100589 struct sdl2_console *scon = get_scon_from_window(ev->window.windowID);
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100590 bool allow_close = true;
Max Reitz1dfc5c82014-12-12 10:52:51 +0100591
Alberto Garcia08d49df2015-06-08 11:12:15 +0200592 if (!scon) {
593 return;
594 }
595
Dave Airlie47c03742013-12-10 14:05:51 +1000596 switch (ev->window.event) {
597 case SDL_WINDOWEVENT_RESIZED:
Dave Airlie8b15d9f2014-03-25 16:50:36 +1000598 {
599 QemuUIInfo info;
600 memset(&info, 0, sizeof(info));
601 info.width = ev->window.data1;
602 info.height = ev->window.data2;
Marc-André Lureauca19ef52021-04-13 20:39:11 +0400603 dpy_set_ui_info(scon->dcl.con, &info, true);
Dave Airlie8b15d9f2014-03-25 16:50:36 +1000604 }
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100605 sdl2_redraw(scon);
Dave Airlie47c03742013-12-10 14:05:51 +1000606 break;
607 case SDL_WINDOWEVENT_EXPOSED:
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100608 sdl2_redraw(scon);
Dave Airlie47c03742013-12-10 14:05:51 +1000609 break;
610 case SDL_WINDOWEVENT_FOCUS_GAINED:
Volker Rümelin83047342020-05-16 09:20:09 +0200611 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));
614 }
615 /* fall through */
Dave Airlie47c03742013-12-10 14:05:51 +1000616 case SDL_WINDOWEVENT_ENTER:
Akihiko Odaki0337e412023-09-21 17:29:34 +0900617 if (!gui_grab && (qemu_input_is_absolute(scon->dcl.con) || absolute_enabled)) {
Dave Airlie47c03742013-12-10 14:05:51 +1000618 absolute_mouse_grab(scon);
619 }
Jindrich Makovicka849bbe62017-11-17 12:22:58 +0100620 /* 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.
623 *
624 * Work around this by ignoring further hotkey events until a
625 * key is released.
626 */
627 scon->ignore_hotkeys = get_mod_state();
Dave Airlie47c03742013-12-10 14:05:51 +1000628 break;
629 case SDL_WINDOWEVENT_FOCUS_LOST:
Volker Rümelin83047342020-05-16 09:20:09 +0200630 if (qemu_console_is_graphic(scon->dcl.con)) {
631 win32_kbd_set_window(NULL);
632 }
Dave Airlie47c03742013-12-10 14:05:51 +1000633 if (gui_grab && !gui_fullscreen) {
634 sdl_grab_end(scon);
635 }
636 break;
637 case SDL_WINDOWEVENT_RESTORED:
Gerd Hoffmann63ed4902014-11-12 08:01:27 +0100638 update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT);
Dave Airlie47c03742013-12-10 14:05:51 +1000639 break;
640 case SDL_WINDOWEVENT_MINIMIZED:
Gerd Hoffmann63ed4902014-11-12 08:01:27 +0100641 update_displaychangelistener(&scon->dcl, 500);
Dave Airlie47c03742013-12-10 14:05:51 +1000642 break;
643 case SDL_WINDOWEVENT_CLOSE:
Jindrich Makovickafc49e722017-11-12 20:30:26 +0100644 if (qemu_console_is_graphic(scon->dcl.con)) {
Elie Tournier844fd502018-04-13 14:58:41 +0100645 if (scon->opts->has_window_close && !scon->opts->window_close) {
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100646 allow_close = false;
647 }
648 if (allow_close) {
Alejandro Jimeneze6dba042020-12-11 11:52:43 -0500649 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
Jindrich Makovickafc49e722017-11-12 20:30:26 +0100650 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
651 }
652 } else {
653 SDL_HideWindow(scon->real_window);
654 scon->hidden = true;
Dave Airlie47c03742013-12-10 14:05:51 +1000655 }
656 break;
Max Reitzd3f3a0f2014-12-12 10:52:52 +0100657 case SDL_WINDOWEVENT_SHOWN:
Jindrich Makovickabcf43cd2017-11-12 20:30:25 +0100658 scon->hidden = false;
Max Reitzd3f3a0f2014-12-12 10:52:52 +0100659 break;
660 case SDL_WINDOWEVENT_HIDDEN:
Jindrich Makovickabcf43cd2017-11-12 20:30:25 +0100661 scon->hidden = true;
Max Reitzd3f3a0f2014-12-12 10:52:52 +0100662 break;
Dave Airlie47c03742013-12-10 14:05:51 +1000663 }
664}
665
Gerd Hoffmann63ed4902014-11-12 08:01:27 +0100666void sdl2_poll_events(struct sdl2_console *scon)
Dave Airlie47c03742013-12-10 14:05:51 +1000667{
Dave Airlie47c03742013-12-10 14:05:51 +1000668 SDL_Event ev1, *ev = &ev1;
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100669 bool allow_close = true;
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100670 int idle = 1;
Dave Airlie47c03742013-12-10 14:05:51 +1000671
672 if (scon->last_vm_running != runstate_is_running()) {
673 scon->last_vm_running = runstate_is_running();
674 sdl_update_caption(scon);
675 }
676
Dave Airlie47c03742013-12-10 14:05:51 +1000677 while (SDL_PollEvent(ev)) {
678 switch (ev->type) {
679 case SDL_KEYDOWN:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100680 idle = 0;
Dave Airlie47c03742013-12-10 14:05:51 +1000681 handle_keydown(ev);
682 break;
683 case SDL_KEYUP:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100684 idle = 0;
Dave Airlie47c03742013-12-10 14:05:51 +1000685 handle_keyup(ev);
686 break;
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200687 case SDL_TEXTINPUT:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100688 idle = 0;
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200689 handle_textinput(ev);
690 break;
Dave Airlie47c03742013-12-10 14:05:51 +1000691 case SDL_QUIT:
Elie Tournier844fd502018-04-13 14:58:41 +0100692 if (scon->opts->has_window_close && !scon->opts->window_close) {
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100693 allow_close = false;
694 }
695 if (allow_close) {
Alejandro Jimeneze6dba042020-12-11 11:52:43 -0500696 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
Eric Blakecf83f142017-05-15 16:41:13 -0500697 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
Dave Airlie47c03742013-12-10 14:05:51 +1000698 }
699 break;
700 case SDL_MOUSEMOTION:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100701 idle = 0;
Dave Airlie47c03742013-12-10 14:05:51 +1000702 handle_mousemotion(ev);
703 break;
704 case SDL_MOUSEBUTTONDOWN:
705 case SDL_MOUSEBUTTONUP:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100706 idle = 0;
Dave Airlie47c03742013-12-10 14:05:51 +1000707 handle_mousebutton(ev);
708 break;
Cole Robinson3f2fde22014-04-21 18:58:50 -0400709 case SDL_MOUSEWHEEL:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100710 idle = 0;
Cole Robinson3f2fde22014-04-21 18:58:50 -0400711 handle_mousewheel(ev);
712 break;
Dave Airlie47c03742013-12-10 14:05:51 +1000713 case SDL_WINDOWEVENT:
Max Reitz1dfc5c82014-12-12 10:52:51 +0100714 handle_windowevent(ev);
Dave Airlie47c03742013-12-10 14:05:51 +1000715 break;
716 default:
717 break;
718 }
719 }
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100720
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;
726 }
727 }
728 } else {
729 scon->idle_counter = 0;
730 scon->dcl.update_interval = SDL2_REFRESH_INTERVAL_BUSY;
731 }
Dave Airlie47c03742013-12-10 14:05:51 +1000732}
733
734static void sdl_mouse_warp(DisplayChangeListener *dcl,
Akihiko Odakia418e7a2024-07-15 14:25:43 +0900735 int x, int y, bool on)
Dave Airlie47c03742013-12-10 14:05:51 +1000736{
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +0100737 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
Jindrich Makovicka28216712017-11-17 12:22:56 +0100738
739 if (!qemu_console_is_graphic(scon->dcl.con)) {
740 return;
741 }
742
Dave Airlie47c03742013-12-10 14:05:51 +1000743 if (on) {
744 if (!guest_cursor) {
Gerd Hoffmann86a088e2020-01-31 12:35:21 +0100745 sdl_show_cursor(scon);
Dave Airlie47c03742013-12-10 14:05:51 +1000746 }
Akihiko Odaki0337e412023-09-21 17:29:34 +0900747 if (gui_grab || qemu_input_is_absolute(scon->dcl.con) || absolute_enabled) {
Dave Airlie47c03742013-12-10 14:05:51 +1000748 SDL_SetCursor(guest_sprite);
Akihiko Odaki0337e412023-09-21 17:29:34 +0900749 if (!qemu_input_is_absolute(scon->dcl.con) && !absolute_enabled) {
Dave Airlie47c03742013-12-10 14:05:51 +1000750 SDL_WarpMouseInWindow(scon->real_window, x, y);
751 }
752 }
753 } else if (gui_grab) {
Gerd Hoffmann86a088e2020-01-31 12:35:21 +0100754 sdl_hide_cursor(scon);
Dave Airlie47c03742013-12-10 14:05:51 +1000755 }
756 guest_cursor = on;
757 guest_x = x, guest_y = y;
758}
759
760static void sdl_mouse_define(DisplayChangeListener *dcl,
761 QEMUCursor *c)
762{
763
764 if (guest_sprite) {
765 SDL_FreeCursor(guest_sprite);
766 }
767
768 if (guest_sprite_surface) {
769 SDL_FreeSurface(guest_sprite_surface);
770 }
771
772 guest_sprite_surface =
773 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
774 0xff0000, 0x00ff00, 0xff, 0xff000000);
775
776 if (!guest_sprite_surface) {
777 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
778 return;
779 }
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;
785 }
786 if (guest_cursor &&
Akihiko Odaki0337e412023-09-21 17:29:34 +0900787 (gui_grab || qemu_input_is_absolute(dcl->con) || absolute_enabled)) {
Dave Airlie47c03742013-12-10 14:05:51 +1000788 SDL_SetCursor(guest_sprite);
789 }
790}
791
792static void sdl_cleanup(void)
793{
794 if (guest_sprite) {
795 SDL_FreeCursor(guest_sprite);
796 }
797 SDL_QuitSubSystem(SDL_INIT_VIDEO);
798}
799
Gerd Hoffmannf1ddebd2014-11-11 11:09:26 +0100800static const DisplayChangeListenerOps dcl_2d_ops = {
Gerd Hoffmann877417d2015-01-09 09:27:09 +0100801 .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,
Dave Airlie47c03742013-12-10 14:05:51 +1000808};
809
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100810#ifdef CONFIG_OPENGL
811static 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,
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100819
Gerd Hoffmanndb6cdfb2017-02-21 10:37:20 +0100820 .dpy_gl_scanout_disable = sdl2_gl_scanout_disable,
Gerd Hoffmannf4c36bd2017-02-21 10:37:16 +0100821 .dpy_gl_scanout_texture = sdl2_gl_scanout_texture,
Gerd Hoffmanncb47dc92014-11-19 14:56:46 +0100822 .dpy_gl_update = sdl2_gl_scanout_flush,
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100823};
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400824
Marc-André Lureaua62c4a12022-02-16 19:33:37 +0400825static bool
826sdl2_gl_is_compatible_dcl(DisplayGLCtx *dgc,
827 DisplayChangeListener *dcl)
828{
829 return dcl->ops == &dcl_gl_ops;
830}
831
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400832static const DisplayGLCtxOps gl_ctx_ops = {
Marc-André Lureaua62c4a12022-02-16 19:33:37 +0400833 .dpy_gl_ctx_is_compatible_dcl = sdl2_gl_is_compatible_dcl,
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400834 .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,
837};
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100838#endif
839
Gerd Hoffmann5ee17182018-03-01 11:05:36 +0100840static void sdl2_display_early_init(DisplayOptions *o)
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100841{
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100842 assert(o->type == DISPLAY_TYPE_SDL);
843 if (o->has_gl && o->gl) {
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100844#ifdef CONFIG_OPENGL
845 display_opengl = 1;
846#endif
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100847 }
848}
849
Gerd Hoffmann5ee17182018-03-01 11:05:36 +0100850static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
Dave Airlie47c03742013-12-10 14:05:51 +1000851{
Dave Airlie47c03742013-12-10 14:05:51 +1000852 uint8_t data = 0;
Dave Airlie47c03742013-12-10 14:05:51 +1000853 int i;
Samuel Thibaultd8253672016-12-21 01:38:06 +0100854 SDL_SysWMinfo info;
Daniel P. Berrangéa442fe22019-01-10 12:00:47 +0000855 SDL_Surface *icon = NULL;
Paolo Bonzini77d910f2020-08-18 11:59:27 +0200856 char *dir;
Dave Airlie47c03742013-12-10 14:05:51 +1000857
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100858 assert(o->type == DISPLAY_TYPE_SDL);
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100859
Marc-André Lureau82f483d2023-02-15 18:27:46 +0400860 if (SDL_GetHintBoolean("QEMU_ENABLE_SDL_LOGGING", SDL_FALSE)) {
861 SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
862 }
863
Thomas Huth2313e482018-08-08 11:46:42 +0200864 if (SDL_Init(SDL_INIT_VIDEO)) {
Dave Airlie47c03742013-12-10 14:05:51 +1000865 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
866 SDL_GetError());
867 exit(1);
868 }
Sebastian Krzyszkowiak8c2b8162018-10-24 16:37:48 +0200869#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
Volker Rümelin1dfea3f2023-04-18 08:28:23 +0200872#ifndef CONFIG_WIN32
Michael Tokarevd4761b62023-08-23 09:53:14 +0300873 /* QEMU uses its own low level keyboard hook procedure on Windows */
Gerd Hoffmann44f017d2014-11-11 12:02:50 +0100874 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
Volker Rümelin1dfea3f2023-04-18 08:28:23 +0200875#endif
Bernhard Beschowefc00a32023-04-17 21:21:38 +0200876#ifdef SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED
877 SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "0");
878#endif
Bernhard Beschow083db9d2023-04-17 21:21:39 +0200879 SDL_SetHint(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4, "1");
Bernhard Beschow2e701e62024-05-12 11:59:45 +0200880 SDL_EnableScreenSaver();
Samuel Thibaultd8253672016-12-21 01:38:06 +0100881 memset(&info, 0, sizeof(info));
882 SDL_VERSION(&info.version);
Dave Airlie47c03742013-12-10 14:05:51 +1000883
Thomas Huth6fb34ff2018-07-09 20:44:10 +0200884 gui_fullscreen = o->has_full_screen && o->full_screen;
885
Thomas Huth9eafdee2022-05-19 17:56:24 +0200886 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;
891 }
892 }
893
Dave Airlie47c03742013-12-10 14:05:51 +1000894 for (i = 0;; i++) {
895 QemuConsole *con = qemu_console_lookup_by_index(i);
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200896 if (!con) {
Dave Airlie47c03742013-12-10 14:05:51 +1000897 break;
898 }
899 }
900 sdl2_num_outputs = i;
Gerd Hoffmann8efa5f22016-06-01 16:08:36 +0200901 if (sdl2_num_outputs == 0) {
902 return;
903 }
Gerd Hoffmann5d0fe652014-11-11 10:21:24 +0100904 sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
Dave Airlie47c03742013-12-10 14:05:51 +1000905 for (i = 0; i < sdl2_num_outputs; i++) {
906 QemuConsole *con = qemu_console_lookup_by_index(i);
Gerd Hoffmann85970a62017-06-21 14:22:34 +0200907 assert(con != NULL);
Gerd Hoffmann6624c382018-09-12 13:43:00 +0200908 if (!qemu_console_is_graphic(con) &&
909 qemu_console_get_index(con) != 0) {
Gerd Hoffmannf2335792014-05-26 14:05:51 +0200910 sdl2_console[i].hidden = true;
911 }
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100912 sdl2_console[i].idx = i;
Gerd Hoffmannf88e5c52018-05-15 07:45:01 +0200913 sdl2_console[i].opts = o;
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100914#ifdef CONFIG_OPENGL
915 sdl2_console[i].opengl = display_opengl;
916 sdl2_console[i].dcl.ops = display_opengl ? &dcl_gl_ops : &dcl_2d_ops;
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400917 sdl2_console[i].dgc.ops = display_opengl ? &gl_ctx_ops : NULL;
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100918#else
919 sdl2_console[i].opengl = 0;
Gerd Hoffmannf1ddebd2014-11-11 11:09:26 +0100920 sdl2_console[i].dcl.ops = &dcl_2d_ops;
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100921#endif
Dave Airlie47c03742013-12-10 14:05:51 +1000922 sdl2_console[i].dcl.con = con;
Gerd Hoffmann07333e12019-01-22 10:28:09 +0100923 sdl2_console[i].kbd = qkbd_state_init(con);
Marc-André Lureauac32b2f2021-01-25 15:10:36 +0400924 if (display_opengl) {
Marc-André Lureau5e79d512021-10-09 23:48:46 +0400925 qemu_console_set_display_gl_ctx(con, &sdl2_console[i].dgc);
Marc-André Lureauac32b2f2021-01-25 15:10:36 +0400926 }
Dave Airlie47c03742013-12-10 14:05:51 +1000927 register_displaychangelistener(&sdl2_console[i].dcl);
Samuel Thibaultd8253672016-12-21 01:38:06 +0100928
Gerd Hoffmann8417cd02017-01-13 09:14:45 +0100929#if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
Samuel Thibaultd8253672016-12-21 01:38:06 +0100930 if (SDL_GetWindowWMInfo(sdl2_console[i].real_window, &info)) {
Gerd Hoffmann8417cd02017-01-13 09:14:45 +0100931#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)
Samuel Thibaultd8253672016-12-21 01:38:06 +0100934 qemu_console_set_window_id(con, info.info.x11.window);
Gerd Hoffmann8417cd02017-01-13 09:14:45 +0100935#endif
Samuel Thibaultd8253672016-12-21 01:38:06 +0100936 }
Gerd Hoffmann8417cd02017-01-13 09:14:45 +0100937#endif
Dave Airlie47c03742013-12-10 14:05:51 +1000938 }
939
Daniel P. Berrangéa442fe22019-01-10 12:00:47 +0000940#ifdef CONFIG_SDL_IMAGE
Paolo Bonzini77d910f2020-08-18 11:59:27 +0200941 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/128x128/apps/qemu.png");
942 icon = IMG_Load(dir);
Daniel P. Berrangéa442fe22019-01-10 12:00:47 +0000943#else
Dave Airlie47c03742013-12-10 14:05:51 +1000944 /* Load a 32x32x4 image. White pixels are transparent. */
Paolo Bonzini77d910f2020-08-18 11:59:27 +0200945 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/32x32/apps/qemu.bmp");
946 icon = SDL_LoadBMP(dir);
Daniel P. Berrangéa442fe22019-01-10 12:00:47 +0000947 if (icon) {
948 uint32_t colorkey = SDL_MapRGB(icon->format, 255, 255, 255);
949 SDL_SetColorKey(icon, SDL_TRUE, colorkey);
950 }
951#endif
Paolo Bonzini77d910f2020-08-18 11:59:27 +0200952 g_free(dir);
Daniel P. Berrangéa442fe22019-01-10 12:00:47 +0000953 if (icon) {
954 SDL_SetWindowIcon(sdl2_console[0].real_window, icon);
Dave Airlie47c03742013-12-10 14:05:51 +1000955 }
956
Dave Airlie47c03742013-12-10 14:05:51 +1000957 mouse_mode_notifier.notify = sdl_mouse_mode_change;
958 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
959
Dave Airlie47c03742013-12-10 14:05:51 +1000960 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
961 sdl_cursor_normal = SDL_GetCursor();
962
Volker Rümelin7dafc672020-05-16 09:20:10 +0200963 if (gui_fullscreen) {
964 sdl_grab_start(&sdl2_console[0]);
965 }
966
Dave Airlie47c03742013-12-10 14:05:51 +1000967 atexit(sdl_cleanup);
968}
Gerd Hoffmann5ee17182018-03-01 11:05:36 +0100969
970static QemuDisplay qemu_display_sdl2 = {
971 .type = DISPLAY_TYPE_SDL,
972 .early_init = sdl2_display_early_init,
973 .init = sdl2_display_init,
974};
975
976static void register_sdl1(void)
977{
978 qemu_display_register(&qemu_display_sdl2);
979}
980
981type_init(register_sdl1);
Gerd Hoffmannb36ae1c2021-06-24 12:38:13 +0200982
983#ifdef CONFIG_OPENGL
984module_dep("ui-opengl");
985#endif