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