Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 1 | /* |
| 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 Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 27 | #include "qemu/module.h" |
Paolo Bonzini | 77d910f | 2020-08-18 11:59:27 +0200 | [diff] [blame] | 28 | #include "qemu/cutils.h" |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 29 | #include "ui/console.h" |
| 30 | #include "ui/input.h" |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 31 | #include "ui/sdl2.h" |
Philippe Mathieu-Daudé | 32cad1f | 2024-12-03 15:20:13 +0100 | [diff] [blame] | 32 | #include "system/runstate.h" |
| 33 | #include "system/runstate-action.h" |
| 34 | #include "system/system.h" |
Volker Rümelin | 8304734 | 2020-05-16 09:20:09 +0200 | [diff] [blame] | 35 | #include "ui/win32-kbd-hook.h" |
Dmitry Petrov | ed80f50 | 2022-01-08 16:39:46 +0100 | [diff] [blame] | 36 | #include "qemu/log.h" |
Phil Dennis-Jordan | f5ab12c | 2024-10-24 12:27:59 +0200 | [diff] [blame] | 37 | #include "qemu-main.h" |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 38 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 39 | static int sdl2_num_outputs; |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 40 | static struct sdl2_console *sdl2_console; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 41 | |
| 42 | static SDL_Surface *guest_sprite_surface; |
| 43 | static int gui_grab; /* if true, all keyboard/mouse events are grabbed */ |
Thomas Huth | 9eafdee | 2022-05-19 17:56:24 +0200 | [diff] [blame] | 44 | static bool alt_grab; |
| 45 | static bool ctrl_grab; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 46 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 47 | static int gui_saved_grab; |
| 48 | static int gui_fullscreen; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 49 | static int gui_grab_code = KMOD_LALT | KMOD_LCTRL; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 50 | static SDL_Cursor *sdl_cursor_normal; |
| 51 | static SDL_Cursor *sdl_cursor_hidden; |
| 52 | static int absolute_enabled; |
Akihiko Odaki | a418e7a | 2024-07-15 14:25:43 +0900 | [diff] [blame] | 53 | static bool guest_cursor; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 54 | static int guest_x, guest_y; |
| 55 | static SDL_Cursor *guest_sprite; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 56 | static Notifier mouse_mode_notifier; |
| 57 | |
Jindřich Makovička | 56bdd4b | 2016-01-12 20:18:24 +0100 | [diff] [blame] | 58 | #define SDL2_REFRESH_INTERVAL_BUSY 10 |
| 59 | #define SDL2_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \ |
| 60 | / SDL2_REFRESH_INTERVAL_BUSY + 1) |
| 61 | |
Marc-André Lureau | da3f7a3 | 2023-02-15 18:29:29 +0400 | [diff] [blame] | 62 | /* introduced in SDL 2.0.10 */ |
| 63 | #ifndef SDL_HINT_RENDER_BATCHING |
| 64 | #define SDL_HINT_RENDER_BATCHING "SDL_RENDER_BATCHING" |
| 65 | #endif |
| 66 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 67 | static void sdl_update_caption(struct sdl2_console *scon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 68 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 69 | static struct sdl2_console *get_scon_from_window(uint32_t window_id) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 70 | { |
| 71 | int i; |
| 72 | for (i = 0; i < sdl2_num_outputs; i++) { |
| 73 | if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) { |
| 74 | return &sdl2_console[i]; |
| 75 | } |
| 76 | } |
| 77 | return NULL; |
| 78 | } |
| 79 | |
Gerd Hoffmann | 2c3056f | 2014-11-11 13:22:49 +0100 | [diff] [blame] | 80 | void sdl2_window_create(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 81 | { |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 82 | int flags = 0; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 83 | |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 84 | if (!scon->surface) { |
| 85 | return; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 86 | } |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 87 | assert(!scon->real_window); |
| 88 | |
| 89 | if (gui_fullscreen) { |
| 90 | flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; |
| 91 | } else { |
| 92 | flags |= SDL_WINDOW_RESIZABLE; |
| 93 | } |
| 94 | if (scon->hidden) { |
| 95 | flags |= SDL_WINDOW_HIDDEN; |
| 96 | } |
Jan Henrik Weinstock | 67c6f1d | 2020-10-04 12:42:21 +0200 | [diff] [blame] | 97 | #ifdef CONFIG_OPENGL |
| 98 | if (scon->opengl) { |
| 99 | flags |= SDL_WINDOW_OPENGL; |
| 100 | } |
| 101 | #endif |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 102 | |
| 103 | scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, |
| 104 | SDL_WINDOWPOS_UNDEFINED, |
| 105 | surface_width(scon->surface), |
| 106 | surface_height(scon->surface), |
| 107 | flags); |
Marc-André Lureau | da3f7a3 | 2023-02-15 18:29:29 +0400 | [diff] [blame] | 108 | if (scon->opengl) { |
| 109 | const char *driver = "opengl"; |
| 110 | |
Markus Armbruster | 154fd4d | 2024-09-04 13:18:25 +0200 | [diff] [blame] | 111 | if (scon->opts->gl == DISPLAY_GL_MODE_ES) { |
Marc-André Lureau | da3f7a3 | 2023-02-15 18:29:29 +0400 | [diff] [blame] | 112 | driver = "opengles2"; |
| 113 | } |
| 114 | |
| 115 | SDL_SetHint(SDL_HINT_RENDER_DRIVER, driver); |
| 116 | SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1"); |
Marc-André Lureau | da3f7a3 | 2023-02-15 18:29:29 +0400 | [diff] [blame] | 117 | |
Marc-André Lureau | 6f2688f | 2023-01-24 17:47:18 +0400 | [diff] [blame] | 118 | scon->winctx = SDL_GL_CreateContext(scon->real_window); |
Gert Wollny | ae23cd0 | 2024-09-11 09:14:30 +0000 | [diff] [blame] | 119 | SDL_GL_SetSwapInterval(0); |
Antonio Caggiano | 176e378 | 2023-06-12 11:19:59 +0200 | [diff] [blame] | 120 | } else { |
| 121 | /* The SDL renderer is only used by sdl2-2D, when OpenGL is disabled */ |
| 122 | scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0); |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 123 | } |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 124 | sdl_update_caption(scon); |
| 125 | } |
| 126 | |
Gerd Hoffmann | 2c3056f | 2014-11-11 13:22:49 +0100 | [diff] [blame] | 127 | void sdl2_window_destroy(struct sdl2_console *scon) |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 128 | { |
| 129 | if (!scon->real_window) { |
| 130 | return; |
| 131 | } |
| 132 | |
Antonio Caggiano | 176e378 | 2023-06-12 11:19:59 +0200 | [diff] [blame] | 133 | if (scon->winctx) { |
| 134 | SDL_GL_DeleteContext(scon->winctx); |
| 135 | scon->winctx = NULL; |
| 136 | } |
| 137 | if (scon->real_renderer) { |
| 138 | SDL_DestroyRenderer(scon->real_renderer); |
| 139 | scon->real_renderer = NULL; |
| 140 | } |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 141 | SDL_DestroyWindow(scon->real_window); |
| 142 | scon->real_window = NULL; |
| 143 | } |
| 144 | |
Gerd Hoffmann | 2c3056f | 2014-11-11 13:22:49 +0100 | [diff] [blame] | 145 | void sdl2_window_resize(struct sdl2_console *scon) |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 146 | { |
| 147 | if (!scon->real_window) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | SDL_SetWindowSize(scon->real_window, |
| 152 | surface_width(scon->surface), |
| 153 | surface_height(scon->surface)); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 154 | } |
| 155 | |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 156 | static void sdl2_redraw(struct sdl2_console *scon) |
| 157 | { |
| 158 | if (scon->opengl) { |
| 159 | #ifdef CONFIG_OPENGL |
| 160 | sdl2_gl_redraw(scon); |
| 161 | #endif |
| 162 | } else { |
| 163 | sdl2_2d_redraw(scon); |
| 164 | } |
| 165 | } |
| 166 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 167 | static void sdl_update_caption(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 168 | { |
| 169 | char win_title[1024]; |
| 170 | char icon_title[1024]; |
| 171 | const char *status = ""; |
| 172 | |
| 173 | if (!runstate_is_running()) { |
| 174 | status = " [Stopped]"; |
| 175 | } else if (gui_grab) { |
| 176 | if (alt_grab) { |
Adrian Wowk | 547ec5a | 2023-10-29 22:41:19 -0400 | [diff] [blame] | 177 | #ifdef CONFIG_DARWIN |
| 178 | status = " - Press ⌃⌥⇧G to exit grab"; |
| 179 | #else |
Gerd Hoffmann | f8d2c93 | 2018-01-15 16:48:54 +0100 | [diff] [blame] | 180 | status = " - Press Ctrl-Alt-Shift-G to exit grab"; |
Adrian Wowk | 547ec5a | 2023-10-29 22:41:19 -0400 | [diff] [blame] | 181 | #endif |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 182 | } else if (ctrl_grab) { |
Gerd Hoffmann | f8d2c93 | 2018-01-15 16:48:54 +0100 | [diff] [blame] | 183 | status = " - Press Right-Ctrl-G to exit grab"; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 184 | } else { |
Adrian Wowk | 547ec5a | 2023-10-29 22:41:19 -0400 | [diff] [blame] | 185 | #ifdef CONFIG_DARWIN |
| 186 | status = " - Press ⌃⌥G to exit grab"; |
| 187 | #else |
Gerd Hoffmann | f8d2c93 | 2018-01-15 16:48:54 +0100 | [diff] [blame] | 188 | status = " - Press Ctrl-Alt-G to exit grab"; |
Adrian Wowk | 547ec5a | 2023-10-29 22:41:19 -0400 | [diff] [blame] | 189 | #endif |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | if (qemu_name) { |
| 194 | snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name, |
| 195 | scon->idx, status); |
| 196 | snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name); |
| 197 | } else { |
| 198 | snprintf(win_title, sizeof(win_title), "QEMU%s", status); |
| 199 | snprintf(icon_title, sizeof(icon_title), "QEMU"); |
| 200 | } |
| 201 | |
| 202 | if (scon->real_window) { |
| 203 | SDL_SetWindowTitle(scon->real_window, win_title); |
| 204 | } |
| 205 | } |
| 206 | |
Gerd Hoffmann | 86a088e | 2020-01-31 12:35:21 +0100 | [diff] [blame] | 207 | static void sdl_hide_cursor(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 208 | { |
Gerd Hoffmann | 86a088e | 2020-01-31 12:35:21 +0100 | [diff] [blame] | 209 | if (scon->opts->has_show_cursor && scon->opts->show_cursor) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | |
Jindrich Makovicka | 253347e | 2017-11-12 20:30:27 +0100 | [diff] [blame] | 213 | SDL_ShowCursor(SDL_DISABLE); |
| 214 | SDL_SetCursor(sdl_cursor_hidden); |
| 215 | |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 216 | if (!qemu_input_is_absolute(scon->dcl.con)) { |
Cole Robinson | 2d968ff | 2014-04-01 16:37:11 -0400 | [diff] [blame] | 217 | SDL_SetRelativeMouseMode(SDL_TRUE); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Gerd Hoffmann | 86a088e | 2020-01-31 12:35:21 +0100 | [diff] [blame] | 221 | static void sdl_show_cursor(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 222 | { |
Gerd Hoffmann | 86a088e | 2020-01-31 12:35:21 +0100 | [diff] [blame] | 223 | if (scon->opts->has_show_cursor && scon->opts->show_cursor) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 227 | if (!qemu_input_is_absolute(scon->dcl.con)) { |
Cole Robinson | 2d968ff | 2014-04-01 16:37:11 -0400 | [diff] [blame] | 228 | SDL_SetRelativeMouseMode(SDL_FALSE); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 229 | } |
Jindrich Makovicka | 253347e | 2017-11-12 20:30:27 +0100 | [diff] [blame] | 230 | |
| 231 | if (guest_cursor && |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 232 | (gui_grab || qemu_input_is_absolute(scon->dcl.con) || absolute_enabled)) { |
Jindrich Makovicka | 253347e | 2017-11-12 20:30:27 +0100 | [diff] [blame] | 233 | SDL_SetCursor(guest_sprite); |
| 234 | } else { |
| 235 | SDL_SetCursor(sdl_cursor_normal); |
| 236 | } |
| 237 | |
| 238 | SDL_ShowCursor(SDL_ENABLE); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 239 | } |
| 240 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 241 | static void sdl_grab_start(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 242 | { |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 243 | QemuConsole *con = scon ? scon->dcl.con : NULL; |
| 244 | |
| 245 | if (!con || !qemu_console_is_graphic(con)) { |
| 246 | return; |
| 247 | } |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 248 | /* |
| 249 | * If the application is not active, do not try to enter grab state. This |
| 250 | * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the |
| 251 | * application (SDL bug). |
| 252 | */ |
| 253 | if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) { |
| 254 | return; |
| 255 | } |
| 256 | if (guest_cursor) { |
| 257 | SDL_SetCursor(guest_sprite); |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 258 | if (!qemu_input_is_absolute(scon->dcl.con) && !absolute_enabled) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 259 | SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y); |
| 260 | } |
| 261 | } else { |
Gerd Hoffmann | 86a088e | 2020-01-31 12:35:21 +0100 | [diff] [blame] | 262 | sdl_hide_cursor(scon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 263 | } |
| 264 | SDL_SetWindowGrab(scon->real_window, SDL_TRUE); |
| 265 | gui_grab = 1; |
Volker Rümelin | 8304734 | 2020-05-16 09:20:09 +0200 | [diff] [blame] | 266 | win32_kbd_set_grab(true); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 267 | sdl_update_caption(scon); |
| 268 | } |
| 269 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 270 | static void sdl_grab_end(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 271 | { |
| 272 | SDL_SetWindowGrab(scon->real_window, SDL_FALSE); |
| 273 | gui_grab = 0; |
Volker Rümelin | 8304734 | 2020-05-16 09:20:09 +0200 | [diff] [blame] | 274 | win32_kbd_set_grab(false); |
Gerd Hoffmann | 86a088e | 2020-01-31 12:35:21 +0100 | [diff] [blame] | 275 | sdl_show_cursor(scon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 276 | sdl_update_caption(scon); |
| 277 | } |
| 278 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 279 | static void absolute_mouse_grab(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 280 | { |
| 281 | int mouse_x, mouse_y; |
| 282 | int scr_w, scr_h; |
| 283 | SDL_GetMouseState(&mouse_x, &mouse_y); |
| 284 | SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h); |
| 285 | if (mouse_x > 0 && mouse_x < scr_w - 1 && |
| 286 | mouse_y > 0 && mouse_y < scr_h - 1) { |
| 287 | sdl_grab_start(scon); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static void sdl_mouse_mode_change(Notifier *notify, void *data) |
| 292 | { |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 293 | if (qemu_input_is_absolute(sdl2_console[0].dcl.con)) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 294 | if (!absolute_enabled) { |
| 295 | absolute_enabled = 1; |
Gerd Hoffmann | 8dfa306 | 2018-02-02 13:08:03 +0100 | [diff] [blame] | 296 | SDL_SetRelativeMouseMode(SDL_FALSE); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 297 | absolute_mouse_grab(&sdl2_console[0]); |
| 298 | } |
| 299 | } else if (absolute_enabled) { |
| 300 | if (!gui_fullscreen) { |
| 301 | sdl_grab_end(&sdl2_console[0]); |
| 302 | } |
| 303 | absolute_enabled = 0; |
| 304 | } |
| 305 | } |
| 306 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 307 | static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy, |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 308 | int x, int y, int state) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 309 | { |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 310 | static uint32_t bmap[INPUT_BUTTON__MAX] = { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 311 | [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT), |
| 312 | [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE), |
| 313 | [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT), |
Darrell Walisser | 2951106 | 2020-12-10 14:33:06 +0000 | [diff] [blame] | 314 | [INPUT_BUTTON_SIDE] = SDL_BUTTON(SDL_BUTTON_X1), |
| 315 | [INPUT_BUTTON_EXTRA] = SDL_BUTTON(SDL_BUTTON_X2) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 316 | }; |
| 317 | static uint32_t prev_state; |
| 318 | |
| 319 | if (prev_state != state) { |
| 320 | qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state); |
| 321 | prev_state = state; |
| 322 | } |
| 323 | |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 324 | if (qemu_input_is_absolute(scon->dcl.con)) { |
Jindrich Makovicka | d9f0626 | 2017-11-17 12:22:57 +0100 | [diff] [blame] | 325 | qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X, |
| 326 | x, 0, surface_width(scon->surface)); |
| 327 | qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y, |
| 328 | y, 0, surface_height(scon->surface)); |
Cole Robinson | afbc0dd | 2014-04-01 16:37:10 -0400 | [diff] [blame] | 329 | } else { |
| 330 | if (guest_cursor) { |
| 331 | x -= guest_x; |
| 332 | y -= guest_y; |
| 333 | guest_x += x; |
| 334 | guest_y += y; |
| 335 | dx = x; |
| 336 | dy = y; |
| 337 | } |
| 338 | qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx); |
| 339 | qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 340 | } |
| 341 | qemu_input_event_sync(); |
| 342 | } |
| 343 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 344 | static void toggle_full_screen(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 345 | { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 346 | gui_fullscreen = !gui_fullscreen; |
| 347 | if (gui_fullscreen) { |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 348 | SDL_SetWindowFullscreen(scon->real_window, |
| 349 | SDL_WINDOW_FULLSCREEN_DESKTOP); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 350 | gui_saved_grab = gui_grab; |
| 351 | sdl_grab_start(scon); |
| 352 | } else { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 353 | if (!gui_saved_grab) { |
| 354 | sdl_grab_end(scon); |
| 355 | } |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 356 | SDL_SetWindowFullscreen(scon->real_window, 0); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 357 | } |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 358 | sdl2_redraw(scon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 359 | } |
| 360 | |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 361 | static int get_mod_state(void) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 362 | { |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 363 | SDL_Keymod mod = SDL_GetModState(); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 364 | |
| 365 | if (alt_grab) { |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 366 | return (mod & (gui_grab_code | KMOD_LSHIFT)) == |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 367 | (gui_grab_code | KMOD_LSHIFT); |
| 368 | } else if (ctrl_grab) { |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 369 | return (mod & KMOD_RCTRL) == KMOD_RCTRL; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 370 | } else { |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 371 | return (mod & gui_grab_code) == gui_grab_code; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 372 | } |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 373 | } |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 374 | |
Volker Rümelin | 8304734 | 2020-05-16 09:20:09 +0200 | [diff] [blame] | 375 | static void *sdl2_win32_get_hwnd(struct sdl2_console *scon) |
| 376 | { |
| 377 | #ifdef CONFIG_WIN32 |
| 378 | SDL_SysWMinfo info; |
| 379 | |
| 380 | SDL_VERSION(&info.version); |
| 381 | if (SDL_GetWindowWMInfo(scon->real_window, &info)) { |
| 382 | return info.info.win.window; |
| 383 | } |
| 384 | #endif |
| 385 | return NULL; |
| 386 | } |
| 387 | |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 388 | static void handle_keydown(SDL_Event *ev) |
| 389 | { |
| 390 | int win; |
| 391 | struct sdl2_console *scon = get_scon_from_window(ev->key.windowID); |
Gerd Hoffmann | afb92eb | 2018-02-20 16:04:44 +0100 | [diff] [blame] | 392 | int gui_key_modifier_pressed = get_mod_state(); |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 393 | |
Changbin Du | 32ec983 | 2020-04-27 21:24:12 +0800 | [diff] [blame] | 394 | if (!scon) { |
| 395 | return; |
| 396 | } |
| 397 | |
Volker Rümelin | eaea803 | 2024-09-09 08:15:52 +0200 | [diff] [blame] | 398 | scon->gui_keysym = false; |
| 399 | |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 400 | if (!scon->ignore_hotkeys && gui_key_modifier_pressed && !ev->key.repeat) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 401 | switch (ev->key.keysym.scancode) { |
Gerd Hoffmann | 363f59d | 2014-05-27 09:44:39 +0200 | [diff] [blame] | 402 | case SDL_SCANCODE_2: |
| 403 | case SDL_SCANCODE_3: |
| 404 | case SDL_SCANCODE_4: |
| 405 | case SDL_SCANCODE_5: |
| 406 | case SDL_SCANCODE_6: |
| 407 | case SDL_SCANCODE_7: |
| 408 | case SDL_SCANCODE_8: |
| 409 | case SDL_SCANCODE_9: |
Cole Robinson | 56f289f | 2016-05-06 14:03:06 -0400 | [diff] [blame] | 410 | if (gui_grab) { |
| 411 | sdl_grab_end(scon); |
| 412 | } |
| 413 | |
Gerd Hoffmann | 363f59d | 2014-05-27 09:44:39 +0200 | [diff] [blame] | 414 | win = ev->key.keysym.scancode - SDL_SCANCODE_1; |
| 415 | if (win < sdl2_num_outputs) { |
| 416 | sdl2_console[win].hidden = !sdl2_console[win].hidden; |
| 417 | if (sdl2_console[win].real_window) { |
| 418 | if (sdl2_console[win].hidden) { |
| 419 | SDL_HideWindow(sdl2_console[win].real_window); |
| 420 | } else { |
| 421 | SDL_ShowWindow(sdl2_console[win].real_window); |
| 422 | } |
| 423 | } |
Volker Rümelin | df3c610 | 2024-09-09 08:15:51 +0200 | [diff] [blame] | 424 | sdl2_release_modifiers(scon); |
Volker Rümelin | eaea803 | 2024-09-09 08:15:52 +0200 | [diff] [blame] | 425 | scon->gui_keysym = true; |
Gerd Hoffmann | 363f59d | 2014-05-27 09:44:39 +0200 | [diff] [blame] | 426 | } |
| 427 | break; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 428 | case SDL_SCANCODE_F: |
| 429 | toggle_full_screen(scon); |
Volker Rümelin | eaea803 | 2024-09-09 08:15:52 +0200 | [diff] [blame] | 430 | scon->gui_keysym = true; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 431 | break; |
Gerd Hoffmann | f8d2c93 | 2018-01-15 16:48:54 +0100 | [diff] [blame] | 432 | case SDL_SCANCODE_G: |
Volker Rümelin | eaea803 | 2024-09-09 08:15:52 +0200 | [diff] [blame] | 433 | scon->gui_keysym = true; |
Gerd Hoffmann | f8d2c93 | 2018-01-15 16:48:54 +0100 | [diff] [blame] | 434 | if (!gui_grab) { |
| 435 | sdl_grab_start(scon); |
| 436 | } else if (!gui_fullscreen) { |
| 437 | sdl_grab_end(scon); |
| 438 | } |
| 439 | break; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 440 | case SDL_SCANCODE_U: |
Amadeusz Sławiński | 64bf97e | 2018-06-13 19:27:07 +0200 | [diff] [blame] | 441 | sdl2_window_resize(scon); |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 442 | if (!scon->opengl) { |
| 443 | /* re-create scon->texture */ |
| 444 | sdl2_2d_switch(&scon->dcl, scon->surface); |
| 445 | } |
Volker Rümelin | eaea803 | 2024-09-09 08:15:52 +0200 | [diff] [blame] | 446 | scon->gui_keysym = true; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 447 | break; |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 448 | #if 0 |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 449 | case SDL_SCANCODE_KP_PLUS: |
| 450 | case SDL_SCANCODE_KP_MINUS: |
| 451 | if (!gui_fullscreen) { |
| 452 | int scr_w, scr_h; |
| 453 | int width, height; |
| 454 | SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h); |
| 455 | |
| 456 | width = MAX(scr_w + (ev->key.keysym.scancode == |
| 457 | SDL_SCANCODE_KP_PLUS ? 50 : -50), |
| 458 | 160); |
| 459 | height = (surface_height(scon->surface) * width) / |
| 460 | surface_width(scon->surface); |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 461 | fprintf(stderr, "%s: scale to %dx%d\n", |
| 462 | __func__, width, height); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 463 | sdl_scale(scon, width, height); |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 464 | sdl2_redraw(scon); |
Volker Rümelin | eaea803 | 2024-09-09 08:15:52 +0200 | [diff] [blame] | 465 | scon->gui_keysym = true; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 466 | } |
Gerd Hoffmann | 46522a8 | 2014-11-11 13:12:02 +0100 | [diff] [blame] | 467 | #endif |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 468 | default: |
| 469 | break; |
| 470 | } |
| 471 | } |
Volker Rümelin | eaea803 | 2024-09-09 08:15:52 +0200 | [diff] [blame] | 472 | if (!scon->gui_keysym) { |
Gerd Hoffmann | 8fc1a3f | 2014-11-11 10:58:19 +0100 | [diff] [blame] | 473 | sdl2_process_key(scon, &ev->key); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | static void handle_keyup(SDL_Event *ev) |
| 478 | { |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 479 | struct sdl2_console *scon = get_scon_from_window(ev->key.windowID); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 480 | |
Changbin Du | 32ec983 | 2020-04-27 21:24:12 +0800 | [diff] [blame] | 481 | if (!scon) { |
| 482 | return; |
| 483 | } |
| 484 | |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 485 | scon->ignore_hotkeys = false; |
Gerd Hoffmann | 07333e1 | 2019-01-22 10:28:09 +0100 | [diff] [blame] | 486 | sdl2_process_key(scon, &ev->key); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 487 | } |
| 488 | |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 489 | static void handle_textinput(SDL_Event *ev) |
| 490 | { |
Pavel Dovgalyuk | 48db08c | 2018-06-26 09:40:17 +0300 | [diff] [blame] | 491 | struct sdl2_console *scon = get_scon_from_window(ev->text.windowID); |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 492 | QemuConsole *con = scon ? scon->dcl.con : NULL; |
| 493 | |
Changbin Du | 32ec983 | 2020-04-27 21:24:12 +0800 | [diff] [blame] | 494 | if (!con) { |
| 495 | return; |
| 496 | } |
| 497 | |
Volker Rümelin | eaea803 | 2024-09-09 08:15:52 +0200 | [diff] [blame] | 498 | if (!scon->gui_keysym && QEMU_IS_TEXT_CONSOLE(con)) { |
Marc-André Lureau | cc6ba2c | 2023-08-30 13:38:20 +0400 | [diff] [blame] | 499 | qemu_text_console_put_string(QEMU_TEXT_CONSOLE(con), ev->text.text, strlen(ev->text.text)); |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 500 | } |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 501 | } |
| 502 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 503 | static void handle_mousemotion(SDL_Event *ev) |
| 504 | { |
| 505 | int max_x, max_y; |
Pavel Dovgalyuk | 48db08c | 2018-06-26 09:40:17 +0300 | [diff] [blame] | 506 | struct sdl2_console *scon = get_scon_from_window(ev->motion.windowID); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 507 | |
Pavel Dovgalyuk | 49213b7 | 2018-06-26 09:47:29 +0300 | [diff] [blame] | 508 | if (!scon || !qemu_console_is_graphic(scon->dcl.con)) { |
Jindrich Makovicka | 2821671 | 2017-11-17 12:22:56 +0100 | [diff] [blame] | 509 | return; |
| 510 | } |
| 511 | |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 512 | if (qemu_input_is_absolute(scon->dcl.con) || absolute_enabled) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 513 | int scr_w, scr_h; |
| 514 | SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h); |
| 515 | max_x = scr_w - 1; |
| 516 | max_y = scr_h - 1; |
Jindrich Makovicka | 2495284 | 2017-11-12 20:30:31 +0100 | [diff] [blame] | 517 | if (gui_grab && !gui_fullscreen |
| 518 | && (ev->motion.x == 0 || ev->motion.y == 0 || |
| 519 | ev->motion.x == max_x || ev->motion.y == max_y)) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 520 | sdl_grab_end(scon); |
| 521 | } |
| 522 | if (!gui_grab && |
| 523 | (ev->motion.x > 0 && ev->motion.x < max_x && |
| 524 | ev->motion.y > 0 && ev->motion.y < max_y)) { |
| 525 | sdl_grab_start(scon); |
| 526 | } |
| 527 | } |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 528 | if (gui_grab || qemu_input_is_absolute(scon->dcl.con) || absolute_enabled) { |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 529 | sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel, |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 530 | ev->motion.x, ev->motion.y, ev->motion.state); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | static void handle_mousebutton(SDL_Event *ev) |
| 535 | { |
| 536 | int buttonstate = SDL_GetMouseState(NULL, NULL); |
| 537 | SDL_MouseButtonEvent *bev; |
Pavel Dovgalyuk | 48db08c | 2018-06-26 09:40:17 +0300 | [diff] [blame] | 538 | struct sdl2_console *scon = get_scon_from_window(ev->button.windowID); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 539 | |
Pavel Dovgalyuk | 49213b7 | 2018-06-26 09:47:29 +0300 | [diff] [blame] | 540 | if (!scon || !qemu_console_is_graphic(scon->dcl.con)) { |
Jindrich Makovicka | 2821671 | 2017-11-17 12:22:56 +0100 | [diff] [blame] | 541 | return; |
| 542 | } |
| 543 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 544 | bev = &ev->button; |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 545 | if (!gui_grab && !qemu_input_is_absolute(scon->dcl.con)) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 546 | if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) { |
| 547 | /* start grabbing all events */ |
| 548 | sdl_grab_start(scon); |
| 549 | } |
| 550 | } else { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 551 | if (ev->type == SDL_MOUSEBUTTONDOWN) { |
| 552 | buttonstate |= SDL_BUTTON(bev->button); |
| 553 | } else { |
| 554 | buttonstate &= ~SDL_BUTTON(bev->button); |
| 555 | } |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 556 | sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 560 | static void handle_mousewheel(SDL_Event *ev) |
| 561 | { |
Pavel Dovgalyuk | 48db08c | 2018-06-26 09:40:17 +0300 | [diff] [blame] | 562 | struct sdl2_console *scon = get_scon_from_window(ev->wheel.windowID); |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 563 | SDL_MouseWheelEvent *wev = &ev->wheel; |
| 564 | InputButton btn; |
| 565 | |
Pavel Dovgalyuk | 49213b7 | 2018-06-26 09:47:29 +0300 | [diff] [blame] | 566 | if (!scon || !qemu_console_is_graphic(scon->dcl.con)) { |
Jindrich Makovicka | 2821671 | 2017-11-17 12:22:56 +0100 | [diff] [blame] | 567 | return; |
| 568 | } |
| 569 | |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 570 | if (wev->y > 0) { |
Gerd Hoffmann | f22d0af | 2016-01-12 12:14:12 +0100 | [diff] [blame] | 571 | btn = INPUT_BUTTON_WHEEL_UP; |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 572 | } else if (wev->y < 0) { |
Gerd Hoffmann | f22d0af | 2016-01-12 12:14:12 +0100 | [diff] [blame] | 573 | btn = INPUT_BUTTON_WHEEL_DOWN; |
Dmitry Petrov | ed80f50 | 2022-01-08 16:39:46 +0100 | [diff] [blame] | 574 | } else if (wev->x < 0) { |
| 575 | btn = INPUT_BUTTON_WHEEL_RIGHT; |
| 576 | } else if (wev->x > 0) { |
| 577 | btn = INPUT_BUTTON_WHEEL_LEFT; |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 578 | } else { |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | qemu_input_queue_btn(scon->dcl.con, btn, true); |
| 583 | qemu_input_event_sync(); |
| 584 | qemu_input_queue_btn(scon->dcl.con, btn, false); |
| 585 | qemu_input_event_sync(); |
| 586 | } |
| 587 | |
Max Reitz | 1dfc5c8 | 2014-12-12 10:52:51 +0100 | [diff] [blame] | 588 | static void handle_windowevent(SDL_Event *ev) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 589 | { |
Max Reitz | 1dfc5c8 | 2014-12-12 10:52:51 +0100 | [diff] [blame] | 590 | struct sdl2_console *scon = get_scon_from_window(ev->window.windowID); |
Gerd Hoffmann | fe91f36 | 2018-02-02 12:10:15 +0100 | [diff] [blame] | 591 | bool allow_close = true; |
Max Reitz | 1dfc5c8 | 2014-12-12 10:52:51 +0100 | [diff] [blame] | 592 | |
Alberto Garcia | 08d49df | 2015-06-08 11:12:15 +0200 | [diff] [blame] | 593 | if (!scon) { |
| 594 | return; |
| 595 | } |
| 596 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 597 | switch (ev->window.event) { |
| 598 | case SDL_WINDOWEVENT_RESIZED: |
Dave Airlie | 8b15d9f | 2014-03-25 16:50:36 +1000 | [diff] [blame] | 599 | { |
| 600 | QemuUIInfo info; |
| 601 | memset(&info, 0, sizeof(info)); |
| 602 | info.width = ev->window.data1; |
| 603 | info.height = ev->window.data2; |
Marc-André Lureau | ca19ef5 | 2021-04-13 20:39:11 +0400 | [diff] [blame] | 604 | dpy_set_ui_info(scon->dcl.con, &info, true); |
Dave Airlie | 8b15d9f | 2014-03-25 16:50:36 +1000 | [diff] [blame] | 605 | } |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 606 | sdl2_redraw(scon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 607 | break; |
| 608 | case SDL_WINDOWEVENT_EXPOSED: |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 609 | sdl2_redraw(scon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 610 | break; |
| 611 | case SDL_WINDOWEVENT_FOCUS_GAINED: |
Volker Rümelin | 8304734 | 2020-05-16 09:20:09 +0200 | [diff] [blame] | 612 | win32_kbd_set_grab(gui_grab); |
| 613 | if (qemu_console_is_graphic(scon->dcl.con)) { |
| 614 | win32_kbd_set_window(sdl2_win32_get_hwnd(scon)); |
| 615 | } |
| 616 | /* fall through */ |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 617 | case SDL_WINDOWEVENT_ENTER: |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 618 | if (!gui_grab && (qemu_input_is_absolute(scon->dcl.con) || absolute_enabled)) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 619 | absolute_mouse_grab(scon); |
| 620 | } |
Jindrich Makovicka | 849bbe6 | 2017-11-17 12:22:58 +0100 | [diff] [blame] | 621 | /* If a new console window opened using a hotkey receives the |
| 622 | * focus, SDL sends another KEYDOWN event to the new window, |
| 623 | * closing the console window immediately after. |
| 624 | * |
| 625 | * Work around this by ignoring further hotkey events until a |
| 626 | * key is released. |
| 627 | */ |
| 628 | scon->ignore_hotkeys = get_mod_state(); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 629 | break; |
| 630 | case SDL_WINDOWEVENT_FOCUS_LOST: |
Volker Rümelin | 8304734 | 2020-05-16 09:20:09 +0200 | [diff] [blame] | 631 | if (qemu_console_is_graphic(scon->dcl.con)) { |
| 632 | win32_kbd_set_window(NULL); |
| 633 | } |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 634 | if (gui_grab && !gui_fullscreen) { |
| 635 | sdl_grab_end(scon); |
| 636 | } |
| 637 | break; |
| 638 | case SDL_WINDOWEVENT_RESTORED: |
Gerd Hoffmann | 63ed490 | 2014-11-12 08:01:27 +0100 | [diff] [blame] | 639 | update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 640 | break; |
| 641 | case SDL_WINDOWEVENT_MINIMIZED: |
Gerd Hoffmann | 63ed490 | 2014-11-12 08:01:27 +0100 | [diff] [blame] | 642 | update_displaychangelistener(&scon->dcl, 500); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 643 | break; |
| 644 | case SDL_WINDOWEVENT_CLOSE: |
Jindrich Makovicka | fc49e72 | 2017-11-12 20:30:26 +0100 | [diff] [blame] | 645 | if (qemu_console_is_graphic(scon->dcl.con)) { |
Elie Tournier | 844fd50 | 2018-04-13 14:58:41 +0100 | [diff] [blame] | 646 | if (scon->opts->has_window_close && !scon->opts->window_close) { |
Gerd Hoffmann | fe91f36 | 2018-02-02 12:10:15 +0100 | [diff] [blame] | 647 | allow_close = false; |
| 648 | } |
| 649 | if (allow_close) { |
Alejandro Jimenez | e6dba04 | 2020-12-11 11:52:43 -0500 | [diff] [blame] | 650 | shutdown_action = SHUTDOWN_ACTION_POWEROFF; |
Jindrich Makovicka | fc49e72 | 2017-11-12 20:30:26 +0100 | [diff] [blame] | 651 | qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI); |
| 652 | } |
| 653 | } else { |
| 654 | SDL_HideWindow(scon->real_window); |
| 655 | scon->hidden = true; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 656 | } |
| 657 | break; |
Max Reitz | d3f3a0f | 2014-12-12 10:52:52 +0100 | [diff] [blame] | 658 | case SDL_WINDOWEVENT_SHOWN: |
Jindrich Makovicka | bcf43cd | 2017-11-12 20:30:25 +0100 | [diff] [blame] | 659 | scon->hidden = false; |
Max Reitz | d3f3a0f | 2014-12-12 10:52:52 +0100 | [diff] [blame] | 660 | break; |
| 661 | case SDL_WINDOWEVENT_HIDDEN: |
Jindrich Makovicka | bcf43cd | 2017-11-12 20:30:25 +0100 | [diff] [blame] | 662 | scon->hidden = true; |
Max Reitz | d3f3a0f | 2014-12-12 10:52:52 +0100 | [diff] [blame] | 663 | break; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Gerd Hoffmann | 63ed490 | 2014-11-12 08:01:27 +0100 | [diff] [blame] | 667 | void sdl2_poll_events(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 668 | { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 669 | SDL_Event ev1, *ev = &ev1; |
Gerd Hoffmann | fe91f36 | 2018-02-02 12:10:15 +0100 | [diff] [blame] | 670 | bool allow_close = true; |
Jindřich Makovička | 56bdd4b | 2016-01-12 20:18:24 +0100 | [diff] [blame] | 671 | int idle = 1; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 672 | |
| 673 | if (scon->last_vm_running != runstate_is_running()) { |
| 674 | scon->last_vm_running = runstate_is_running(); |
| 675 | sdl_update_caption(scon); |
| 676 | } |
| 677 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 678 | while (SDL_PollEvent(ev)) { |
| 679 | switch (ev->type) { |
| 680 | case SDL_KEYDOWN: |
Jindřich Makovička | 56bdd4b | 2016-01-12 20:18:24 +0100 | [diff] [blame] | 681 | idle = 0; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 682 | handle_keydown(ev); |
| 683 | break; |
| 684 | case SDL_KEYUP: |
Jindřich Makovička | 56bdd4b | 2016-01-12 20:18:24 +0100 | [diff] [blame] | 685 | idle = 0; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 686 | handle_keyup(ev); |
| 687 | break; |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 688 | case SDL_TEXTINPUT: |
Jindřich Makovička | 56bdd4b | 2016-01-12 20:18:24 +0100 | [diff] [blame] | 689 | idle = 0; |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 690 | handle_textinput(ev); |
| 691 | break; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 692 | case SDL_QUIT: |
Elie Tournier | 844fd50 | 2018-04-13 14:58:41 +0100 | [diff] [blame] | 693 | if (scon->opts->has_window_close && !scon->opts->window_close) { |
Gerd Hoffmann | fe91f36 | 2018-02-02 12:10:15 +0100 | [diff] [blame] | 694 | allow_close = false; |
| 695 | } |
| 696 | if (allow_close) { |
Alejandro Jimenez | e6dba04 | 2020-12-11 11:52:43 -0500 | [diff] [blame] | 697 | shutdown_action = SHUTDOWN_ACTION_POWEROFF; |
Eric Blake | cf83f14 | 2017-05-15 16:41:13 -0500 | [diff] [blame] | 698 | qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 699 | } |
| 700 | break; |
| 701 | case SDL_MOUSEMOTION: |
Jindřich Makovička | 56bdd4b | 2016-01-12 20:18:24 +0100 | [diff] [blame] | 702 | idle = 0; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 703 | handle_mousemotion(ev); |
| 704 | break; |
| 705 | case SDL_MOUSEBUTTONDOWN: |
| 706 | case SDL_MOUSEBUTTONUP: |
Jindřich Makovička | 56bdd4b | 2016-01-12 20:18:24 +0100 | [diff] [blame] | 707 | idle = 0; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 708 | handle_mousebutton(ev); |
| 709 | break; |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 710 | case SDL_MOUSEWHEEL: |
Jindřich Makovička | 56bdd4b | 2016-01-12 20:18:24 +0100 | [diff] [blame] | 711 | idle = 0; |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 712 | handle_mousewheel(ev); |
| 713 | break; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 714 | case SDL_WINDOWEVENT: |
Max Reitz | 1dfc5c8 | 2014-12-12 10:52:51 +0100 | [diff] [blame] | 715 | handle_windowevent(ev); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 716 | break; |
| 717 | default: |
| 718 | break; |
| 719 | } |
| 720 | } |
Jindřich Makovička | 56bdd4b | 2016-01-12 20:18:24 +0100 | [diff] [blame] | 721 | |
| 722 | if (idle) { |
| 723 | if (scon->idle_counter < SDL2_MAX_IDLE_COUNT) { |
| 724 | scon->idle_counter++; |
| 725 | if (scon->idle_counter >= SDL2_MAX_IDLE_COUNT) { |
| 726 | scon->dcl.update_interval = GUI_REFRESH_INTERVAL_DEFAULT; |
| 727 | } |
| 728 | } |
| 729 | } else { |
| 730 | scon->idle_counter = 0; |
| 731 | scon->dcl.update_interval = SDL2_REFRESH_INTERVAL_BUSY; |
| 732 | } |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static void sdl_mouse_warp(DisplayChangeListener *dcl, |
Akihiko Odaki | a418e7a | 2024-07-15 14:25:43 +0900 | [diff] [blame] | 736 | int x, int y, bool on) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 737 | { |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 738 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); |
Jindrich Makovicka | 2821671 | 2017-11-17 12:22:56 +0100 | [diff] [blame] | 739 | |
| 740 | if (!qemu_console_is_graphic(scon->dcl.con)) { |
| 741 | return; |
| 742 | } |
| 743 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 744 | if (on) { |
| 745 | if (!guest_cursor) { |
Gerd Hoffmann | 86a088e | 2020-01-31 12:35:21 +0100 | [diff] [blame] | 746 | sdl_show_cursor(scon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 747 | } |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 748 | if (gui_grab || qemu_input_is_absolute(scon->dcl.con) || absolute_enabled) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 749 | SDL_SetCursor(guest_sprite); |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 750 | if (!qemu_input_is_absolute(scon->dcl.con) && !absolute_enabled) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 751 | SDL_WarpMouseInWindow(scon->real_window, x, y); |
| 752 | } |
| 753 | } |
| 754 | } else if (gui_grab) { |
Gerd Hoffmann | 86a088e | 2020-01-31 12:35:21 +0100 | [diff] [blame] | 755 | sdl_hide_cursor(scon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 756 | } |
| 757 | guest_cursor = on; |
| 758 | guest_x = x, guest_y = y; |
| 759 | } |
| 760 | |
| 761 | static void sdl_mouse_define(DisplayChangeListener *dcl, |
| 762 | QEMUCursor *c) |
| 763 | { |
| 764 | |
| 765 | if (guest_sprite) { |
| 766 | SDL_FreeCursor(guest_sprite); |
| 767 | } |
| 768 | |
| 769 | if (guest_sprite_surface) { |
| 770 | SDL_FreeSurface(guest_sprite_surface); |
| 771 | } |
| 772 | |
| 773 | guest_sprite_surface = |
| 774 | SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4, |
| 775 | 0xff0000, 0x00ff00, 0xff, 0xff000000); |
| 776 | |
| 777 | if (!guest_sprite_surface) { |
| 778 | fprintf(stderr, "Failed to make rgb surface from %p\n", c); |
| 779 | return; |
| 780 | } |
| 781 | guest_sprite = SDL_CreateColorCursor(guest_sprite_surface, |
| 782 | c->hot_x, c->hot_y); |
| 783 | if (!guest_sprite) { |
| 784 | fprintf(stderr, "Failed to make color cursor from %p\n", c); |
| 785 | return; |
| 786 | } |
| 787 | if (guest_cursor && |
Akihiko Odaki | 0337e41 | 2023-09-21 17:29:34 +0900 | [diff] [blame] | 788 | (gui_grab || qemu_input_is_absolute(dcl->con) || absolute_enabled)) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 789 | SDL_SetCursor(guest_sprite); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | static void sdl_cleanup(void) |
| 794 | { |
| 795 | if (guest_sprite) { |
| 796 | SDL_FreeCursor(guest_sprite); |
| 797 | } |
| 798 | SDL_QuitSubSystem(SDL_INIT_VIDEO); |
| 799 | } |
| 800 | |
Gerd Hoffmann | f1ddebd | 2014-11-11 11:09:26 +0100 | [diff] [blame] | 801 | static const DisplayChangeListenerOps dcl_2d_ops = { |
Gerd Hoffmann | 877417d | 2015-01-09 09:27:09 +0100 | [diff] [blame] | 802 | .dpy_name = "sdl2-2d", |
| 803 | .dpy_gfx_update = sdl2_2d_update, |
| 804 | .dpy_gfx_switch = sdl2_2d_switch, |
| 805 | .dpy_gfx_check_format = sdl2_2d_check_format, |
| 806 | .dpy_refresh = sdl2_2d_refresh, |
| 807 | .dpy_mouse_set = sdl_mouse_warp, |
| 808 | .dpy_cursor_define = sdl_mouse_define, |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 809 | }; |
| 810 | |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 811 | #ifdef CONFIG_OPENGL |
| 812 | static const DisplayChangeListenerOps dcl_gl_ops = { |
| 813 | .dpy_name = "sdl2-gl", |
| 814 | .dpy_gfx_update = sdl2_gl_update, |
| 815 | .dpy_gfx_switch = sdl2_gl_switch, |
| 816 | .dpy_gfx_check_format = console_gl_check_format, |
| 817 | .dpy_refresh = sdl2_gl_refresh, |
| 818 | .dpy_mouse_set = sdl_mouse_warp, |
| 819 | .dpy_cursor_define = sdl_mouse_define, |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 820 | |
Gerd Hoffmann | db6cdfb | 2017-02-21 10:37:20 +0100 | [diff] [blame] | 821 | .dpy_gl_scanout_disable = sdl2_gl_scanout_disable, |
Gerd Hoffmann | f4c36bd | 2017-02-21 10:37:16 +0100 | [diff] [blame] | 822 | .dpy_gl_scanout_texture = sdl2_gl_scanout_texture, |
Gerd Hoffmann | cb47dc9 | 2014-11-19 14:56:46 +0100 | [diff] [blame] | 823 | .dpy_gl_update = sdl2_gl_scanout_flush, |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 824 | }; |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 825 | |
Marc-André Lureau | a62c4a1 | 2022-02-16 19:33:37 +0400 | [diff] [blame] | 826 | static bool |
| 827 | sdl2_gl_is_compatible_dcl(DisplayGLCtx *dgc, |
| 828 | DisplayChangeListener *dcl) |
| 829 | { |
| 830 | return dcl->ops == &dcl_gl_ops; |
| 831 | } |
| 832 | |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 833 | static const DisplayGLCtxOps gl_ctx_ops = { |
Marc-André Lureau | a62c4a1 | 2022-02-16 19:33:37 +0400 | [diff] [blame] | 834 | .dpy_gl_ctx_is_compatible_dcl = sdl2_gl_is_compatible_dcl, |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 835 | .dpy_gl_ctx_create = sdl2_gl_create_context, |
| 836 | .dpy_gl_ctx_destroy = sdl2_gl_destroy_context, |
| 837 | .dpy_gl_ctx_make_current = sdl2_gl_make_context_current, |
| 838 | }; |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 839 | #endif |
| 840 | |
Gerd Hoffmann | 5ee1718 | 2018-03-01 11:05:36 +0100 | [diff] [blame] | 841 | static void sdl2_display_early_init(DisplayOptions *o) |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 842 | { |
Gerd Hoffmann | fe91f36 | 2018-02-02 12:10:15 +0100 | [diff] [blame] | 843 | assert(o->type == DISPLAY_TYPE_SDL); |
| 844 | if (o->has_gl && o->gl) { |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 845 | #ifdef CONFIG_OPENGL |
| 846 | display_opengl = 1; |
| 847 | #endif |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
Gerd Hoffmann | 5ee1718 | 2018-03-01 11:05:36 +0100 | [diff] [blame] | 851 | static void sdl2_display_init(DisplayState *ds, DisplayOptions *o) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 852 | { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 853 | uint8_t data = 0; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 854 | int i; |
Samuel Thibault | d825367 | 2016-12-21 01:38:06 +0100 | [diff] [blame] | 855 | SDL_SysWMinfo info; |
Daniel P. Berrangé | a442fe2 | 2019-01-10 12:00:47 +0000 | [diff] [blame] | 856 | SDL_Surface *icon = NULL; |
Paolo Bonzini | 77d910f | 2020-08-18 11:59:27 +0200 | [diff] [blame] | 857 | char *dir; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 858 | |
Gerd Hoffmann | fe91f36 | 2018-02-02 12:10:15 +0100 | [diff] [blame] | 859 | assert(o->type == DISPLAY_TYPE_SDL); |
Gerd Hoffmann | fe91f36 | 2018-02-02 12:10:15 +0100 | [diff] [blame] | 860 | |
Marc-André Lureau | 82f483d | 2023-02-15 18:27:46 +0400 | [diff] [blame] | 861 | if (SDL_GetHintBoolean("QEMU_ENABLE_SDL_LOGGING", SDL_FALSE)) { |
| 862 | SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE); |
| 863 | } |
| 864 | |
Thomas Huth | 2313e48 | 2018-08-08 11:46:42 +0200 | [diff] [blame] | 865 | if (SDL_Init(SDL_INIT_VIDEO)) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 866 | fprintf(stderr, "Could not initialize SDL(%s) - exiting\n", |
| 867 | SDL_GetError()); |
| 868 | exit(1); |
| 869 | } |
Sebastian Krzyszkowiak | 8c2b816 | 2018-10-24 16:37:48 +0200 | [diff] [blame] | 870 | #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */ |
| 871 | SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0"); |
| 872 | #endif |
Volker Rümelin | 1dfea3f | 2023-04-18 08:28:23 +0200 | [diff] [blame] | 873 | #ifndef CONFIG_WIN32 |
Michael Tokarev | d4761b6 | 2023-08-23 09:53:14 +0300 | [diff] [blame] | 874 | /* QEMU uses its own low level keyboard hook procedure on Windows */ |
Gerd Hoffmann | 44f017d | 2014-11-11 12:02:50 +0100 | [diff] [blame] | 875 | SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1"); |
Volker Rümelin | 1dfea3f | 2023-04-18 08:28:23 +0200 | [diff] [blame] | 876 | #endif |
Bernhard Beschow | efc00a3 | 2023-04-17 21:21:38 +0200 | [diff] [blame] | 877 | #ifdef SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED |
| 878 | SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "0"); |
| 879 | #endif |
Bernhard Beschow | 083db9d | 2023-04-17 21:21:39 +0200 | [diff] [blame] | 880 | SDL_SetHint(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4, "1"); |
Bernhard Beschow | 2e701e6 | 2024-05-12 11:59:45 +0200 | [diff] [blame] | 881 | SDL_EnableScreenSaver(); |
Samuel Thibault | d825367 | 2016-12-21 01:38:06 +0100 | [diff] [blame] | 882 | memset(&info, 0, sizeof(info)); |
| 883 | SDL_VERSION(&info.version); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 884 | |
Thomas Huth | 6fb34ff | 2018-07-09 20:44:10 +0200 | [diff] [blame] | 885 | gui_fullscreen = o->has_full_screen && o->full_screen; |
| 886 | |
Thomas Huth | 9eafdee | 2022-05-19 17:56:24 +0200 | [diff] [blame] | 887 | if (o->u.sdl.has_grab_mod) { |
| 888 | if (o->u.sdl.grab_mod == HOT_KEY_MOD_LSHIFT_LCTRL_LALT) { |
| 889 | alt_grab = true; |
| 890 | } else if (o->u.sdl.grab_mod == HOT_KEY_MOD_RCTRL) { |
| 891 | ctrl_grab = true; |
| 892 | } |
| 893 | } |
| 894 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 895 | for (i = 0;; i++) { |
| 896 | QemuConsole *con = qemu_console_lookup_by_index(i); |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 897 | if (!con) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 898 | break; |
| 899 | } |
| 900 | } |
| 901 | sdl2_num_outputs = i; |
Gerd Hoffmann | 8efa5f2 | 2016-06-01 16:08:36 +0200 | [diff] [blame] | 902 | if (sdl2_num_outputs == 0) { |
| 903 | return; |
| 904 | } |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 905 | sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 906 | for (i = 0; i < sdl2_num_outputs; i++) { |
| 907 | QemuConsole *con = qemu_console_lookup_by_index(i); |
Gerd Hoffmann | 85970a6 | 2017-06-21 14:22:34 +0200 | [diff] [blame] | 908 | assert(con != NULL); |
Gerd Hoffmann | 6624c38 | 2018-09-12 13:43:00 +0200 | [diff] [blame] | 909 | if (!qemu_console_is_graphic(con) && |
| 910 | qemu_console_get_index(con) != 0) { |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 911 | sdl2_console[i].hidden = true; |
| 912 | } |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 913 | sdl2_console[i].idx = i; |
Gerd Hoffmann | f88e5c5 | 2018-05-15 07:45:01 +0200 | [diff] [blame] | 914 | sdl2_console[i].opts = o; |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 915 | #ifdef CONFIG_OPENGL |
| 916 | sdl2_console[i].opengl = display_opengl; |
| 917 | sdl2_console[i].dcl.ops = display_opengl ? &dcl_gl_ops : &dcl_2d_ops; |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 918 | sdl2_console[i].dgc.ops = display_opengl ? &gl_ctx_ops : NULL; |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 919 | #else |
| 920 | sdl2_console[i].opengl = 0; |
Gerd Hoffmann | f1ddebd | 2014-11-11 11:09:26 +0100 | [diff] [blame] | 921 | sdl2_console[i].dcl.ops = &dcl_2d_ops; |
Gerd Hoffmann | 0b71a5d | 2014-11-11 16:54:45 +0100 | [diff] [blame] | 922 | #endif |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 923 | sdl2_console[i].dcl.con = con; |
Gerd Hoffmann | 07333e1 | 2019-01-22 10:28:09 +0100 | [diff] [blame] | 924 | sdl2_console[i].kbd = qkbd_state_init(con); |
Marc-André Lureau | ac32b2f | 2021-01-25 15:10:36 +0400 | [diff] [blame] | 925 | if (display_opengl) { |
Marc-André Lureau | 5e79d51 | 2021-10-09 23:48:46 +0400 | [diff] [blame] | 926 | qemu_console_set_display_gl_ctx(con, &sdl2_console[i].dgc); |
Marc-André Lureau | ac32b2f | 2021-01-25 15:10:36 +0400 | [diff] [blame] | 927 | } |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 928 | register_displaychangelistener(&sdl2_console[i].dcl); |
Samuel Thibault | d825367 | 2016-12-21 01:38:06 +0100 | [diff] [blame] | 929 | |
Gerd Hoffmann | 8417cd0 | 2017-01-13 09:14:45 +0100 | [diff] [blame] | 930 | #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11) |
Samuel Thibault | d825367 | 2016-12-21 01:38:06 +0100 | [diff] [blame] | 931 | if (SDL_GetWindowWMInfo(sdl2_console[i].real_window, &info)) { |
Gerd Hoffmann | 8417cd0 | 2017-01-13 09:14:45 +0100 | [diff] [blame] | 932 | #if defined(SDL_VIDEO_DRIVER_WINDOWS) |
| 933 | qemu_console_set_window_id(con, (uintptr_t)info.info.win.window); |
| 934 | #elif defined(SDL_VIDEO_DRIVER_X11) |
Samuel Thibault | d825367 | 2016-12-21 01:38:06 +0100 | [diff] [blame] | 935 | qemu_console_set_window_id(con, info.info.x11.window); |
Gerd Hoffmann | 8417cd0 | 2017-01-13 09:14:45 +0100 | [diff] [blame] | 936 | #endif |
Samuel Thibault | d825367 | 2016-12-21 01:38:06 +0100 | [diff] [blame] | 937 | } |
Gerd Hoffmann | 8417cd0 | 2017-01-13 09:14:45 +0100 | [diff] [blame] | 938 | #endif |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 939 | } |
| 940 | |
Daniel P. Berrangé | a442fe2 | 2019-01-10 12:00:47 +0000 | [diff] [blame] | 941 | #ifdef CONFIG_SDL_IMAGE |
Paolo Bonzini | 77d910f | 2020-08-18 11:59:27 +0200 | [diff] [blame] | 942 | dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/128x128/apps/qemu.png"); |
| 943 | icon = IMG_Load(dir); |
Daniel P. Berrangé | a442fe2 | 2019-01-10 12:00:47 +0000 | [diff] [blame] | 944 | #else |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 945 | /* Load a 32x32x4 image. White pixels are transparent. */ |
Paolo Bonzini | 77d910f | 2020-08-18 11:59:27 +0200 | [diff] [blame] | 946 | dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/32x32/apps/qemu.bmp"); |
| 947 | icon = SDL_LoadBMP(dir); |
Daniel P. Berrangé | a442fe2 | 2019-01-10 12:00:47 +0000 | [diff] [blame] | 948 | if (icon) { |
| 949 | uint32_t colorkey = SDL_MapRGB(icon->format, 255, 255, 255); |
| 950 | SDL_SetColorKey(icon, SDL_TRUE, colorkey); |
| 951 | } |
| 952 | #endif |
Paolo Bonzini | 77d910f | 2020-08-18 11:59:27 +0200 | [diff] [blame] | 953 | g_free(dir); |
Daniel P. Berrangé | a442fe2 | 2019-01-10 12:00:47 +0000 | [diff] [blame] | 954 | if (icon) { |
| 955 | SDL_SetWindowIcon(sdl2_console[0].real_window, icon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 956 | } |
| 957 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 958 | mouse_mode_notifier.notify = sdl_mouse_mode_change; |
| 959 | qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier); |
| 960 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 961 | sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0); |
| 962 | sdl_cursor_normal = SDL_GetCursor(); |
| 963 | |
Volker Rümelin | 7dafc67 | 2020-05-16 09:20:10 +0200 | [diff] [blame] | 964 | if (gui_fullscreen) { |
| 965 | sdl_grab_start(&sdl2_console[0]); |
| 966 | } |
| 967 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 968 | atexit(sdl_cleanup); |
Phil Dennis-Jordan | f5ab12c | 2024-10-24 12:27:59 +0200 | [diff] [blame] | 969 | |
| 970 | /* SDL's event polling (in dpy_refresh) must happen on the main thread. */ |
| 971 | qemu_main = NULL; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 972 | } |
Gerd Hoffmann | 5ee1718 | 2018-03-01 11:05:36 +0100 | [diff] [blame] | 973 | |
| 974 | static QemuDisplay qemu_display_sdl2 = { |
| 975 | .type = DISPLAY_TYPE_SDL, |
| 976 | .early_init = sdl2_display_early_init, |
| 977 | .init = sdl2_display_init, |
| 978 | }; |
| 979 | |
| 980 | static void register_sdl1(void) |
| 981 | { |
| 982 | qemu_display_register(&qemu_display_sdl2); |
| 983 | } |
| 984 | |
| 985 | type_init(register_sdl1); |
Gerd Hoffmann | b36ae1c | 2021-06-24 12:38:13 +0200 | [diff] [blame] | 986 | |
| 987 | #ifdef CONFIG_OPENGL |
| 988 | module_dep("ui-opengl"); |
| 989 | #endif |