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