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