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 | |
| 26 | /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */ |
| 27 | #undef WIN32_LEAN_AND_MEAN |
| 28 | |
| 29 | #include <SDL.h> |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 30 | #include <SDL_syswm.h> |
| 31 | |
| 32 | #include "qemu-common.h" |
| 33 | #include "ui/console.h" |
| 34 | #include "ui/input.h" |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 35 | #include "ui/sdl2.h" |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 36 | #include "sysemu/sysemu.h" |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 37 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 38 | static int sdl2_num_outputs; |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 39 | static struct sdl2_console *sdl2_console; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 40 | |
| 41 | static SDL_Surface *guest_sprite_surface; |
| 42 | static int gui_grab; /* if true, all keyboard/mouse events are grabbed */ |
| 43 | |
| 44 | static bool gui_saved_scaling; |
| 45 | static int gui_saved_width; |
| 46 | static int gui_saved_height; |
| 47 | static int gui_saved_grab; |
| 48 | static int gui_fullscreen; |
| 49 | static int gui_noframe; |
| 50 | static int gui_key_modifier_pressed; |
| 51 | static int gui_keysym; |
| 52 | static int gui_grab_code = KMOD_LALT | KMOD_LCTRL; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 53 | static SDL_Cursor *sdl_cursor_normal; |
| 54 | static SDL_Cursor *sdl_cursor_hidden; |
| 55 | static int absolute_enabled; |
| 56 | static int guest_cursor; |
| 57 | static int guest_x, guest_y; |
| 58 | static SDL_Cursor *guest_sprite; |
| 59 | static int scaling_active; |
| 60 | static Notifier mouse_mode_notifier; |
| 61 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 62 | static void sdl_update_caption(struct sdl2_console *scon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 63 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 64 | static struct sdl2_console *get_scon_from_window(uint32_t window_id) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 65 | { |
| 66 | int i; |
| 67 | for (i = 0; i < sdl2_num_outputs; i++) { |
| 68 | if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) { |
| 69 | return &sdl2_console[i]; |
| 70 | } |
| 71 | } |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | static void sdl_update(DisplayChangeListener *dcl, |
| 76 | int x, int y, int w, int h) |
| 77 | { |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 78 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 79 | SDL_Rect rect; |
| 80 | DisplaySurface *surf = qemu_console_surface(dcl->con); |
| 81 | |
| 82 | if (!surf) { |
| 83 | return; |
| 84 | } |
| 85 | if (!scon->texture) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | rect.x = x; |
| 90 | rect.y = y; |
| 91 | rect.w = w; |
| 92 | rect.h = h; |
| 93 | |
| 94 | SDL_UpdateTexture(scon->texture, NULL, surface_data(surf), |
| 95 | surface_stride(surf)); |
| 96 | SDL_RenderCopy(scon->real_renderer, scon->texture, &rect, &rect); |
| 97 | SDL_RenderPresent(scon->real_renderer); |
| 98 | } |
| 99 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 100 | static void do_sdl_resize(struct sdl2_console *scon, int width, int height, |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 101 | int bpp) |
| 102 | { |
| 103 | int flags; |
| 104 | |
| 105 | if (scon->real_window && scon->real_renderer) { |
| 106 | if (width && height) { |
| 107 | SDL_RenderSetLogicalSize(scon->real_renderer, width, height); |
| 108 | SDL_SetWindowSize(scon->real_window, width, height); |
| 109 | } else { |
| 110 | SDL_DestroyRenderer(scon->real_renderer); |
| 111 | SDL_DestroyWindow(scon->real_window); |
| 112 | scon->real_renderer = NULL; |
| 113 | scon->real_window = NULL; |
| 114 | } |
| 115 | } else { |
| 116 | if (!width || !height) { |
| 117 | return; |
| 118 | } |
| 119 | flags = 0; |
| 120 | if (gui_fullscreen) { |
| 121 | flags |= SDL_WINDOW_FULLSCREEN; |
| 122 | } else { |
| 123 | flags |= SDL_WINDOW_RESIZABLE; |
| 124 | } |
Gerd Hoffmann | 363f59d | 2014-05-27 09:44:39 +0200 | [diff] [blame] | 125 | if (scon->hidden) { |
| 126 | flags |= SDL_WINDOW_HIDDEN; |
| 127 | } |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 128 | |
| 129 | scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, |
| 130 | SDL_WINDOWPOS_UNDEFINED, |
| 131 | width, height, flags); |
| 132 | scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0); |
| 133 | sdl_update_caption(scon); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void sdl_switch(DisplayChangeListener *dcl, |
| 138 | DisplaySurface *new_surface) |
| 139 | { |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 140 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 141 | int format = 0; |
| 142 | int idx = scon->idx; |
| 143 | DisplaySurface *old_surface = scon->surface; |
| 144 | |
| 145 | /* temporary hack: allows to call sdl_switch to handle scaling changes */ |
| 146 | if (new_surface) { |
| 147 | scon->surface = new_surface; |
| 148 | } |
| 149 | |
| 150 | if (!new_surface && idx > 0) { |
| 151 | scon->surface = NULL; |
| 152 | } |
| 153 | |
| 154 | if (new_surface == NULL) { |
| 155 | do_sdl_resize(scon, 0, 0, 0); |
| 156 | } else { |
| 157 | do_sdl_resize(scon, surface_width(scon->surface), |
| 158 | surface_height(scon->surface), 0); |
| 159 | } |
| 160 | |
| 161 | if (old_surface && scon->texture) { |
| 162 | SDL_DestroyTexture(scon->texture); |
| 163 | scon->texture = NULL; |
| 164 | } |
| 165 | |
| 166 | if (new_surface) { |
| 167 | if (!scon->texture) { |
| 168 | if (surface_bits_per_pixel(scon->surface) == 16) { |
| 169 | format = SDL_PIXELFORMAT_RGB565; |
| 170 | } else if (surface_bits_per_pixel(scon->surface) == 32) { |
| 171 | format = SDL_PIXELFORMAT_ARGB8888; |
| 172 | } |
| 173 | |
| 174 | scon->texture = SDL_CreateTexture(scon->real_renderer, format, |
| 175 | SDL_TEXTUREACCESS_STREAMING, |
| 176 | surface_width(new_surface), |
| 177 | surface_height(new_surface)); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 182 | static void sdl_update_caption(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 183 | { |
| 184 | char win_title[1024]; |
| 185 | char icon_title[1024]; |
| 186 | const char *status = ""; |
| 187 | |
| 188 | if (!runstate_is_running()) { |
| 189 | status = " [Stopped]"; |
| 190 | } else if (gui_grab) { |
| 191 | if (alt_grab) { |
| 192 | status = " - Press Ctrl-Alt-Shift to exit mouse grab"; |
| 193 | } else if (ctrl_grab) { |
| 194 | status = " - Press Right-Ctrl to exit mouse grab"; |
| 195 | } else { |
| 196 | status = " - Press Ctrl-Alt to exit mouse grab"; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (qemu_name) { |
| 201 | snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name, |
| 202 | scon->idx, status); |
| 203 | snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name); |
| 204 | } else { |
| 205 | snprintf(win_title, sizeof(win_title), "QEMU%s", status); |
| 206 | snprintf(icon_title, sizeof(icon_title), "QEMU"); |
| 207 | } |
| 208 | |
| 209 | if (scon->real_window) { |
| 210 | SDL_SetWindowTitle(scon->real_window, win_title); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | static void sdl_hide_cursor(void) |
| 215 | { |
| 216 | if (!cursor_hide) { |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | if (qemu_input_is_absolute()) { |
| 221 | SDL_ShowCursor(1); |
| 222 | SDL_SetCursor(sdl_cursor_hidden); |
| 223 | } else { |
Cole Robinson | 2d968ff | 2014-04-01 16:37:11 -0400 | [diff] [blame] | 224 | SDL_SetRelativeMouseMode(SDL_TRUE); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | static void sdl_show_cursor(void) |
| 229 | { |
| 230 | if (!cursor_hide) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (!qemu_input_is_absolute()) { |
Cole Robinson | 2d968ff | 2014-04-01 16:37:11 -0400 | [diff] [blame] | 235 | SDL_SetRelativeMouseMode(SDL_FALSE); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 236 | SDL_ShowCursor(1); |
| 237 | if (guest_cursor && |
| 238 | (gui_grab || qemu_input_is_absolute() || absolute_enabled)) { |
| 239 | SDL_SetCursor(guest_sprite); |
| 240 | } else { |
| 241 | SDL_SetCursor(sdl_cursor_normal); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 246 | static void sdl_grab_start(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 247 | { |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 248 | QemuConsole *con = scon ? scon->dcl.con : NULL; |
| 249 | |
| 250 | if (!con || !qemu_console_is_graphic(con)) { |
| 251 | return; |
| 252 | } |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 253 | /* |
| 254 | * If the application is not active, do not try to enter grab state. This |
| 255 | * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the |
| 256 | * application (SDL bug). |
| 257 | */ |
| 258 | if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) { |
| 259 | return; |
| 260 | } |
| 261 | if (guest_cursor) { |
| 262 | SDL_SetCursor(guest_sprite); |
| 263 | if (!qemu_input_is_absolute() && !absolute_enabled) { |
| 264 | SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y); |
| 265 | } |
| 266 | } else { |
| 267 | sdl_hide_cursor(); |
| 268 | } |
| 269 | SDL_SetWindowGrab(scon->real_window, SDL_TRUE); |
| 270 | gui_grab = 1; |
| 271 | sdl_update_caption(scon); |
| 272 | } |
| 273 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 274 | static void sdl_grab_end(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 275 | { |
| 276 | SDL_SetWindowGrab(scon->real_window, SDL_FALSE); |
| 277 | gui_grab = 0; |
| 278 | sdl_show_cursor(); |
| 279 | sdl_update_caption(scon); |
| 280 | } |
| 281 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 282 | static void absolute_mouse_grab(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 283 | { |
| 284 | int mouse_x, mouse_y; |
| 285 | int scr_w, scr_h; |
| 286 | SDL_GetMouseState(&mouse_x, &mouse_y); |
| 287 | SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h); |
| 288 | if (mouse_x > 0 && mouse_x < scr_w - 1 && |
| 289 | mouse_y > 0 && mouse_y < scr_h - 1) { |
| 290 | sdl_grab_start(scon); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static void sdl_mouse_mode_change(Notifier *notify, void *data) |
| 295 | { |
| 296 | if (qemu_input_is_absolute()) { |
| 297 | if (!absolute_enabled) { |
| 298 | absolute_enabled = 1; |
| 299 | absolute_mouse_grab(&sdl2_console[0]); |
| 300 | } |
| 301 | } else if (absolute_enabled) { |
| 302 | if (!gui_fullscreen) { |
| 303 | sdl_grab_end(&sdl2_console[0]); |
| 304 | } |
| 305 | absolute_enabled = 0; |
| 306 | } |
| 307 | } |
| 308 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 309 | 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] | 310 | int x, int y, int state) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 311 | { |
| 312 | static uint32_t bmap[INPUT_BUTTON_MAX] = { |
| 313 | [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT), |
| 314 | [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE), |
| 315 | [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT), |
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 | |
| 324 | if (qemu_input_is_absolute()) { |
| 325 | int scr_w, scr_h; |
| 326 | int max_w = 0, max_h = 0; |
| 327 | int off_x = 0, off_y = 0; |
| 328 | int cur_off_x = 0, cur_off_y = 0; |
| 329 | int i; |
| 330 | |
| 331 | for (i = 0; i < sdl2_num_outputs; i++) { |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 332 | struct sdl2_console *thiscon = &sdl2_console[i]; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 333 | if (thiscon->real_window && thiscon->surface) { |
| 334 | SDL_GetWindowSize(thiscon->real_window, &scr_w, &scr_h); |
| 335 | cur_off_x = thiscon->x; |
| 336 | cur_off_y = thiscon->y; |
| 337 | if (scr_w + cur_off_x > max_w) { |
| 338 | max_w = scr_w + cur_off_x; |
| 339 | } |
| 340 | if (scr_h + cur_off_y > max_h) { |
| 341 | max_h = scr_h + cur_off_y; |
| 342 | } |
| 343 | if (i == scon->idx) { |
| 344 | off_x = cur_off_x; |
| 345 | off_y = cur_off_y; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X, off_x + x, max_w); |
| 350 | qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y, off_y + y, max_h); |
Cole Robinson | afbc0dd | 2014-04-01 16:37:10 -0400 | [diff] [blame] | 351 | } else { |
| 352 | if (guest_cursor) { |
| 353 | x -= guest_x; |
| 354 | y -= guest_y; |
| 355 | guest_x += x; |
| 356 | guest_y += y; |
| 357 | dx = x; |
| 358 | dy = y; |
| 359 | } |
| 360 | qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx); |
| 361 | qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 362 | } |
| 363 | qemu_input_event_sync(); |
| 364 | } |
| 365 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 366 | static void sdl_scale(struct sdl2_console *scon, int width, int height) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 367 | { |
| 368 | int bpp = 0; |
| 369 | do_sdl_resize(scon, width, height, bpp); |
| 370 | scaling_active = 1; |
| 371 | } |
| 372 | |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 373 | static void toggle_full_screen(struct sdl2_console *scon) |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 374 | { |
| 375 | int width = surface_width(scon->surface); |
| 376 | int height = surface_height(scon->surface); |
| 377 | int bpp = surface_bits_per_pixel(scon->surface); |
| 378 | |
| 379 | gui_fullscreen = !gui_fullscreen; |
| 380 | if (gui_fullscreen) { |
| 381 | SDL_GetWindowSize(scon->real_window, |
| 382 | &gui_saved_width, &gui_saved_height); |
| 383 | gui_saved_scaling = scaling_active; |
| 384 | |
| 385 | do_sdl_resize(scon, width, height, bpp); |
| 386 | scaling_active = 0; |
| 387 | |
| 388 | gui_saved_grab = gui_grab; |
| 389 | sdl_grab_start(scon); |
| 390 | } else { |
| 391 | if (gui_saved_scaling) { |
| 392 | sdl_scale(scon, gui_saved_width, gui_saved_height); |
| 393 | } else { |
| 394 | do_sdl_resize(scon, width, height, 0); |
| 395 | } |
| 396 | if (!gui_saved_grab) { |
| 397 | sdl_grab_end(scon); |
| 398 | } |
| 399 | } |
| 400 | graphic_hw_invalidate(scon->dcl.con); |
| 401 | graphic_hw_update(scon->dcl.con); |
| 402 | } |
| 403 | |
| 404 | static void handle_keydown(SDL_Event *ev) |
| 405 | { |
Gerd Hoffmann | 363f59d | 2014-05-27 09:44:39 +0200 | [diff] [blame] | 406 | int mod_state, win; |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 407 | struct sdl2_console *scon = get_scon_from_window(ev->key.windowID); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 408 | |
| 409 | if (alt_grab) { |
| 410 | mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) == |
| 411 | (gui_grab_code | KMOD_LSHIFT); |
| 412 | } else if (ctrl_grab) { |
| 413 | mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL; |
| 414 | } else { |
| 415 | mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code; |
| 416 | } |
| 417 | gui_key_modifier_pressed = mod_state; |
| 418 | |
| 419 | if (gui_key_modifier_pressed) { |
| 420 | switch (ev->key.keysym.scancode) { |
Gerd Hoffmann | 363f59d | 2014-05-27 09:44:39 +0200 | [diff] [blame] | 421 | case SDL_SCANCODE_2: |
| 422 | case SDL_SCANCODE_3: |
| 423 | case SDL_SCANCODE_4: |
| 424 | case SDL_SCANCODE_5: |
| 425 | case SDL_SCANCODE_6: |
| 426 | case SDL_SCANCODE_7: |
| 427 | case SDL_SCANCODE_8: |
| 428 | case SDL_SCANCODE_9: |
| 429 | win = ev->key.keysym.scancode - SDL_SCANCODE_1; |
| 430 | if (win < sdl2_num_outputs) { |
| 431 | sdl2_console[win].hidden = !sdl2_console[win].hidden; |
| 432 | if (sdl2_console[win].real_window) { |
| 433 | if (sdl2_console[win].hidden) { |
| 434 | SDL_HideWindow(sdl2_console[win].real_window); |
| 435 | } else { |
| 436 | SDL_ShowWindow(sdl2_console[win].real_window); |
| 437 | } |
| 438 | } |
| 439 | gui_keysym = 1; |
| 440 | } |
| 441 | break; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 442 | case SDL_SCANCODE_F: |
| 443 | toggle_full_screen(scon); |
| 444 | gui_keysym = 1; |
| 445 | break; |
| 446 | case SDL_SCANCODE_U: |
| 447 | if (scaling_active) { |
| 448 | scaling_active = 0; |
| 449 | sdl_switch(&scon->dcl, NULL); |
| 450 | graphic_hw_invalidate(scon->dcl.con); |
| 451 | graphic_hw_update(scon->dcl.con); |
| 452 | } |
| 453 | gui_keysym = 1; |
| 454 | break; |
| 455 | case SDL_SCANCODE_KP_PLUS: |
| 456 | case SDL_SCANCODE_KP_MINUS: |
| 457 | if (!gui_fullscreen) { |
| 458 | int scr_w, scr_h; |
| 459 | int width, height; |
| 460 | SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h); |
| 461 | |
| 462 | width = MAX(scr_w + (ev->key.keysym.scancode == |
| 463 | SDL_SCANCODE_KP_PLUS ? 50 : -50), |
| 464 | 160); |
| 465 | height = (surface_height(scon->surface) * width) / |
| 466 | surface_width(scon->surface); |
| 467 | |
| 468 | sdl_scale(scon, width, height); |
| 469 | graphic_hw_invalidate(NULL); |
| 470 | graphic_hw_update(NULL); |
| 471 | gui_keysym = 1; |
| 472 | } |
| 473 | default: |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | if (!gui_keysym) { |
Gerd Hoffmann | 8fc1a3f | 2014-11-11 10:58:19 +0100 | [diff] [blame^] | 478 | sdl2_process_key(scon, &ev->key); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | static void handle_keyup(SDL_Event *ev) |
| 483 | { |
| 484 | int mod_state; |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 485 | struct sdl2_console *scon = get_scon_from_window(ev->key.windowID); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 486 | |
| 487 | if (!alt_grab) { |
| 488 | mod_state = (ev->key.keysym.mod & gui_grab_code); |
| 489 | } else { |
| 490 | mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT)); |
| 491 | } |
| 492 | if (!mod_state && gui_key_modifier_pressed) { |
| 493 | gui_key_modifier_pressed = 0; |
| 494 | if (gui_keysym == 0) { |
| 495 | /* exit/enter grab if pressing Ctrl-Alt */ |
| 496 | if (!gui_grab) { |
| 497 | sdl_grab_start(scon); |
| 498 | } else if (!gui_fullscreen) { |
| 499 | sdl_grab_end(scon); |
| 500 | } |
| 501 | /* SDL does not send back all the modifiers key, so we must |
| 502 | * correct it. */ |
Gerd Hoffmann | 8fc1a3f | 2014-11-11 10:58:19 +0100 | [diff] [blame^] | 503 | sdl2_reset_keys(scon); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 504 | return; |
| 505 | } |
| 506 | gui_keysym = 0; |
| 507 | } |
| 508 | if (!gui_keysym) { |
Gerd Hoffmann | 8fc1a3f | 2014-11-11 10:58:19 +0100 | [diff] [blame^] | 509 | sdl2_process_key(scon, &ev->key); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 513 | static void handle_textinput(SDL_Event *ev) |
| 514 | { |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 515 | struct sdl2_console *scon = get_scon_from_window(ev->key.windowID); |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 516 | QemuConsole *con = scon ? scon->dcl.con : NULL; |
| 517 | |
| 518 | if (qemu_console_is_graphic(con)) { |
| 519 | return; |
| 520 | } |
| 521 | kbd_put_string_console(con, ev->text.text, strlen(ev->text.text)); |
| 522 | } |
| 523 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 524 | static void handle_mousemotion(SDL_Event *ev) |
| 525 | { |
| 526 | int max_x, max_y; |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 527 | struct sdl2_console *scon = get_scon_from_window(ev->key.windowID); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 528 | |
| 529 | if (qemu_input_is_absolute() || absolute_enabled) { |
| 530 | int scr_w, scr_h; |
| 531 | SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h); |
| 532 | max_x = scr_w - 1; |
| 533 | max_y = scr_h - 1; |
| 534 | if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 || |
| 535 | ev->motion.x == max_x || ev->motion.y == max_y)) { |
| 536 | sdl_grab_end(scon); |
| 537 | } |
| 538 | if (!gui_grab && |
| 539 | (ev->motion.x > 0 && ev->motion.x < max_x && |
| 540 | ev->motion.y > 0 && ev->motion.y < max_y)) { |
| 541 | sdl_grab_start(scon); |
| 542 | } |
| 543 | } |
| 544 | if (gui_grab || qemu_input_is_absolute() || absolute_enabled) { |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 545 | sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel, |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 546 | ev->motion.x, ev->motion.y, ev->motion.state); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | static void handle_mousebutton(SDL_Event *ev) |
| 551 | { |
| 552 | int buttonstate = SDL_GetMouseState(NULL, NULL); |
| 553 | SDL_MouseButtonEvent *bev; |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 554 | struct sdl2_console *scon = get_scon_from_window(ev->key.windowID); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 555 | |
| 556 | bev = &ev->button; |
| 557 | if (!gui_grab && !qemu_input_is_absolute()) { |
| 558 | if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) { |
| 559 | /* start grabbing all events */ |
| 560 | sdl_grab_start(scon); |
| 561 | } |
| 562 | } else { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 563 | if (ev->type == SDL_MOUSEBUTTONDOWN) { |
| 564 | buttonstate |= SDL_BUTTON(bev->button); |
| 565 | } else { |
| 566 | buttonstate &= ~SDL_BUTTON(bev->button); |
| 567 | } |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 568 | sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 572 | static void handle_mousewheel(SDL_Event *ev) |
| 573 | { |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 574 | struct sdl2_console *scon = get_scon_from_window(ev->key.windowID); |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 575 | SDL_MouseWheelEvent *wev = &ev->wheel; |
| 576 | InputButton btn; |
| 577 | |
| 578 | if (wev->y > 0) { |
| 579 | btn = INPUT_BUTTON_WHEEL_UP; |
| 580 | } else if (wev->y < 0) { |
| 581 | btn = INPUT_BUTTON_WHEEL_DOWN; |
| 582 | } else { |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | qemu_input_queue_btn(scon->dcl.con, btn, true); |
| 587 | qemu_input_event_sync(); |
| 588 | qemu_input_queue_btn(scon->dcl.con, btn, false); |
| 589 | qemu_input_event_sync(); |
| 590 | } |
| 591 | |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 592 | static void handle_windowevent(DisplayChangeListener *dcl, SDL_Event *ev) |
| 593 | { |
| 594 | int w, h; |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 595 | struct sdl2_console *scon = get_scon_from_window(ev->key.windowID); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 596 | |
| 597 | switch (ev->window.event) { |
| 598 | case SDL_WINDOWEVENT_RESIZED: |
| 599 | sdl_scale(scon, ev->window.data1, ev->window.data2); |
Dave Airlie | 8b15d9f | 2014-03-25 16:50:36 +1000 | [diff] [blame] | 600 | { |
| 601 | QemuUIInfo info; |
| 602 | memset(&info, 0, sizeof(info)); |
| 603 | info.width = ev->window.data1; |
| 604 | info.height = ev->window.data2; |
| 605 | dpy_set_ui_info(scon->dcl.con, &info); |
| 606 | } |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 607 | graphic_hw_invalidate(scon->dcl.con); |
| 608 | graphic_hw_update(scon->dcl.con); |
| 609 | break; |
| 610 | case SDL_WINDOWEVENT_EXPOSED: |
| 611 | SDL_GetWindowSize(SDL_GetWindowFromID(ev->window.windowID), &w, &h); |
| 612 | sdl_update(dcl, 0, 0, w, h); |
| 613 | break; |
| 614 | case SDL_WINDOWEVENT_FOCUS_GAINED: |
| 615 | case SDL_WINDOWEVENT_ENTER: |
| 616 | if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) { |
| 617 | absolute_mouse_grab(scon); |
| 618 | } |
| 619 | break; |
| 620 | case SDL_WINDOWEVENT_FOCUS_LOST: |
| 621 | if (gui_grab && !gui_fullscreen) { |
| 622 | sdl_grab_end(scon); |
| 623 | } |
| 624 | break; |
| 625 | case SDL_WINDOWEVENT_RESTORED: |
| 626 | update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT); |
| 627 | break; |
| 628 | case SDL_WINDOWEVENT_MINIMIZED: |
| 629 | update_displaychangelistener(dcl, 500); |
| 630 | break; |
| 631 | case SDL_WINDOWEVENT_CLOSE: |
| 632 | if (!no_quit) { |
| 633 | no_shutdown = 0; |
| 634 | qemu_system_shutdown_request(); |
| 635 | } |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | static void sdl_refresh(DisplayChangeListener *dcl) |
| 641 | { |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 642 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 643 | SDL_Event ev1, *ev = &ev1; |
| 644 | |
| 645 | if (scon->last_vm_running != runstate_is_running()) { |
| 646 | scon->last_vm_running = runstate_is_running(); |
| 647 | sdl_update_caption(scon); |
| 648 | } |
| 649 | |
| 650 | graphic_hw_update(dcl->con); |
| 651 | |
| 652 | while (SDL_PollEvent(ev)) { |
| 653 | switch (ev->type) { |
| 654 | case SDL_KEYDOWN: |
| 655 | handle_keydown(ev); |
| 656 | break; |
| 657 | case SDL_KEYUP: |
| 658 | handle_keyup(ev); |
| 659 | break; |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 660 | case SDL_TEXTINPUT: |
| 661 | handle_textinput(ev); |
| 662 | break; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 663 | case SDL_QUIT: |
| 664 | if (!no_quit) { |
| 665 | no_shutdown = 0; |
| 666 | qemu_system_shutdown_request(); |
| 667 | } |
| 668 | break; |
| 669 | case SDL_MOUSEMOTION: |
| 670 | handle_mousemotion(ev); |
| 671 | break; |
| 672 | case SDL_MOUSEBUTTONDOWN: |
| 673 | case SDL_MOUSEBUTTONUP: |
| 674 | handle_mousebutton(ev); |
| 675 | break; |
Cole Robinson | 3f2fde2 | 2014-04-21 18:58:50 -0400 | [diff] [blame] | 676 | case SDL_MOUSEWHEEL: |
| 677 | handle_mousewheel(ev); |
| 678 | break; |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 679 | case SDL_WINDOWEVENT: |
| 680 | handle_windowevent(dcl, ev); |
| 681 | break; |
| 682 | default: |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | static void sdl_mouse_warp(DisplayChangeListener *dcl, |
| 689 | int x, int y, int on) |
| 690 | { |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 691 | struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 692 | if (on) { |
| 693 | if (!guest_cursor) { |
| 694 | sdl_show_cursor(); |
| 695 | } |
| 696 | if (gui_grab || qemu_input_is_absolute() || absolute_enabled) { |
| 697 | SDL_SetCursor(guest_sprite); |
| 698 | if (!qemu_input_is_absolute() && !absolute_enabled) { |
| 699 | SDL_WarpMouseInWindow(scon->real_window, x, y); |
| 700 | } |
| 701 | } |
| 702 | } else if (gui_grab) { |
| 703 | sdl_hide_cursor(); |
| 704 | } |
| 705 | guest_cursor = on; |
| 706 | guest_x = x, guest_y = y; |
| 707 | } |
| 708 | |
| 709 | static void sdl_mouse_define(DisplayChangeListener *dcl, |
| 710 | QEMUCursor *c) |
| 711 | { |
| 712 | |
| 713 | if (guest_sprite) { |
| 714 | SDL_FreeCursor(guest_sprite); |
| 715 | } |
| 716 | |
| 717 | if (guest_sprite_surface) { |
| 718 | SDL_FreeSurface(guest_sprite_surface); |
| 719 | } |
| 720 | |
| 721 | guest_sprite_surface = |
| 722 | SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4, |
| 723 | 0xff0000, 0x00ff00, 0xff, 0xff000000); |
| 724 | |
| 725 | if (!guest_sprite_surface) { |
| 726 | fprintf(stderr, "Failed to make rgb surface from %p\n", c); |
| 727 | return; |
| 728 | } |
| 729 | guest_sprite = SDL_CreateColorCursor(guest_sprite_surface, |
| 730 | c->hot_x, c->hot_y); |
| 731 | if (!guest_sprite) { |
| 732 | fprintf(stderr, "Failed to make color cursor from %p\n", c); |
| 733 | return; |
| 734 | } |
| 735 | if (guest_cursor && |
| 736 | (gui_grab || qemu_input_is_absolute() || absolute_enabled)) { |
| 737 | SDL_SetCursor(guest_sprite); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | static void sdl_cleanup(void) |
| 742 | { |
| 743 | if (guest_sprite) { |
| 744 | SDL_FreeCursor(guest_sprite); |
| 745 | } |
| 746 | SDL_QuitSubSystem(SDL_INIT_VIDEO); |
| 747 | } |
| 748 | |
| 749 | static const DisplayChangeListenerOps dcl_ops = { |
| 750 | .dpy_name = "sdl", |
| 751 | .dpy_gfx_update = sdl_update, |
| 752 | .dpy_gfx_switch = sdl_switch, |
| 753 | .dpy_refresh = sdl_refresh, |
| 754 | .dpy_mouse_set = sdl_mouse_warp, |
| 755 | .dpy_cursor_define = sdl_mouse_define, |
| 756 | }; |
| 757 | |
| 758 | void sdl_display_init(DisplayState *ds, int full_screen, int no_frame) |
| 759 | { |
| 760 | int flags; |
| 761 | uint8_t data = 0; |
| 762 | char *filename; |
| 763 | int i; |
| 764 | |
| 765 | if (no_frame) { |
| 766 | gui_noframe = 1; |
| 767 | } |
| 768 | |
| 769 | #ifdef __linux__ |
| 770 | /* on Linux, SDL may use fbcon|directfb|svgalib when run without |
| 771 | * accessible $DISPLAY to open X11 window. This is often the case |
| 772 | * when qemu is run using sudo. But in this case, and when actually |
| 773 | * run in X11 environment, SDL fights with X11 for the video card, |
| 774 | * making current display unavailable, often until reboot. |
| 775 | * So make x11 the default SDL video driver if this variable is unset. |
| 776 | * This is a bit hackish but saves us from bigger problem. |
| 777 | * Maybe it's a good idea to fix this in SDL instead. |
| 778 | */ |
| 779 | setenv("SDL_VIDEODRIVER", "x11", 0); |
| 780 | #endif |
| 781 | |
| 782 | flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE; |
| 783 | if (SDL_Init(flags)) { |
| 784 | fprintf(stderr, "Could not initialize SDL(%s) - exiting\n", |
| 785 | SDL_GetError()); |
| 786 | exit(1); |
| 787 | } |
| 788 | |
| 789 | for (i = 0;; i++) { |
| 790 | QemuConsole *con = qemu_console_lookup_by_index(i); |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 791 | if (!con) { |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 792 | break; |
| 793 | } |
| 794 | } |
| 795 | sdl2_num_outputs = i; |
Gerd Hoffmann | 5d0fe65 | 2014-11-11 10:21:24 +0100 | [diff] [blame] | 796 | sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs); |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 797 | for (i = 0; i < sdl2_num_outputs; i++) { |
| 798 | QemuConsole *con = qemu_console_lookup_by_index(i); |
Gerd Hoffmann | f233579 | 2014-05-26 14:05:51 +0200 | [diff] [blame] | 799 | if (!qemu_console_is_graphic(con)) { |
| 800 | sdl2_console[i].hidden = true; |
| 801 | } |
Dave Airlie | 47c0374 | 2013-12-10 14:05:51 +1000 | [diff] [blame] | 802 | sdl2_console[i].dcl.ops = &dcl_ops; |
| 803 | sdl2_console[i].dcl.con = con; |
| 804 | register_displaychangelistener(&sdl2_console[i].dcl); |
| 805 | sdl2_console[i].idx = i; |
| 806 | } |
| 807 | |
| 808 | /* Load a 32x32x4 image. White pixels are transparent. */ |
| 809 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp"); |
| 810 | if (filename) { |
| 811 | SDL_Surface *image = SDL_LoadBMP(filename); |
| 812 | if (image) { |
| 813 | uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255); |
| 814 | SDL_SetColorKey(image, SDL_TRUE, colorkey); |
| 815 | SDL_SetWindowIcon(sdl2_console[0].real_window, image); |
| 816 | } |
| 817 | g_free(filename); |
| 818 | } |
| 819 | |
| 820 | if (full_screen) { |
| 821 | gui_fullscreen = 1; |
| 822 | sdl_grab_start(0); |
| 823 | } |
| 824 | |
| 825 | mouse_mode_notifier.notify = sdl_mouse_mode_change; |
| 826 | qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier); |
| 827 | |
| 828 | gui_grab = 0; |
| 829 | |
| 830 | sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0); |
| 831 | sdl_cursor_normal = SDL_GetCursor(); |
| 832 | |
| 833 | atexit(sdl_cleanup); |
| 834 | } |