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