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