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