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