2 * QEMU SDL display driver
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 /* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
26 #include "qemu/osdep.h"
27 #include "qemu/module.h"
28 #include "qemu/cutils.h"
29 #include "ui/console.h"
32 #include "sysemu/runstate.h"
33 #include "sysemu/runstate-action.h"
34 #include "sysemu/sysemu.h"
35 #include "ui/win32-kbd-hook.h"
38 static int sdl2_num_outputs;
39 static struct sdl2_console *sdl2_console;
41 static SDL_Surface *guest_sprite_surface;
42 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
44 static bool ctrl_grab;
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;
57 #define SDL2_REFRESH_INTERVAL_BUSY 10
58 #define SDL2_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
59 / SDL2_REFRESH_INTERVAL_BUSY + 1)
61 static void sdl_update_caption(struct sdl2_console *scon);
63 static struct sdl2_console *get_scon_from_window(uint32_t window_id)
66 for (i = 0; i < sdl2_num_outputs; i++) {
67 if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
68 return &sdl2_console[i];
74 void sdl2_window_create(struct sdl2_console *scon)
81 assert(!scon->real_window);
84 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
86 flags |= SDL_WINDOW_RESIZABLE;
89 flags |= SDL_WINDOW_HIDDEN;
93 flags |= SDL_WINDOW_OPENGL;
97 scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
98 SDL_WINDOWPOS_UNDEFINED,
99 surface_width(scon->surface),
100 surface_height(scon->surface),
102 scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
104 scon->winctx = SDL_GL_CreateContext(scon->real_window);
106 sdl_update_caption(scon);
109 void sdl2_window_destroy(struct sdl2_console *scon)
111 if (!scon->real_window) {
115 SDL_GL_DeleteContext(scon->winctx);
117 SDL_DestroyRenderer(scon->real_renderer);
118 scon->real_renderer = NULL;
119 SDL_DestroyWindow(scon->real_window);
120 scon->real_window = NULL;
123 void sdl2_window_resize(struct sdl2_console *scon)
125 if (!scon->real_window) {
129 SDL_SetWindowSize(scon->real_window,
130 surface_width(scon->surface),
131 surface_height(scon->surface));
134 static void sdl2_redraw(struct sdl2_console *scon)
138 sdl2_gl_redraw(scon);
141 sdl2_2d_redraw(scon);
145 static void sdl_update_caption(struct sdl2_console *scon)
147 char win_title[1024];
148 char icon_title[1024];
149 const char *status = "";
151 if (!runstate_is_running()) {
152 status = " [Stopped]";
153 } else if (gui_grab) {
155 status = " - Press Ctrl-Alt-Shift-G to exit grab";
156 } else if (ctrl_grab) {
157 status = " - Press Right-Ctrl-G to exit grab";
159 status = " - Press Ctrl-Alt-G to exit grab";
164 snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
166 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
168 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
169 snprintf(icon_title, sizeof(icon_title), "QEMU");
172 if (scon->real_window) {
173 SDL_SetWindowTitle(scon->real_window, win_title);
177 static void sdl_hide_cursor(struct sdl2_console *scon)
179 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
183 SDL_ShowCursor(SDL_DISABLE);
184 SDL_SetCursor(sdl_cursor_hidden);
186 if (!qemu_input_is_absolute()) {
187 SDL_SetRelativeMouseMode(SDL_TRUE);
191 static void sdl_show_cursor(struct sdl2_console *scon)
193 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
197 if (!qemu_input_is_absolute()) {
198 SDL_SetRelativeMouseMode(SDL_FALSE);
202 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
203 SDL_SetCursor(guest_sprite);
205 SDL_SetCursor(sdl_cursor_normal);
208 SDL_ShowCursor(SDL_ENABLE);
211 static void sdl_grab_start(struct sdl2_console *scon)
213 QemuConsole *con = scon ? scon->dcl.con : NULL;
215 if (!con || !qemu_console_is_graphic(con)) {
219 * If the application is not active, do not try to enter grab state. This
220 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
221 * application (SDL bug).
223 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
227 SDL_SetCursor(guest_sprite);
228 if (!qemu_input_is_absolute() && !absolute_enabled) {
229 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
232 sdl_hide_cursor(scon);
234 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
236 win32_kbd_set_grab(true);
237 sdl_update_caption(scon);
240 static void sdl_grab_end(struct sdl2_console *scon)
242 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
244 win32_kbd_set_grab(false);
245 sdl_show_cursor(scon);
246 sdl_update_caption(scon);
249 static void absolute_mouse_grab(struct sdl2_console *scon)
251 int mouse_x, mouse_y;
253 SDL_GetMouseState(&mouse_x, &mouse_y);
254 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
255 if (mouse_x > 0 && mouse_x < scr_w - 1 &&
256 mouse_y > 0 && mouse_y < scr_h - 1) {
257 sdl_grab_start(scon);
261 static void sdl_mouse_mode_change(Notifier *notify, void *data)
263 if (qemu_input_is_absolute()) {
264 if (!absolute_enabled) {
265 absolute_enabled = 1;
266 SDL_SetRelativeMouseMode(SDL_FALSE);
267 absolute_mouse_grab(&sdl2_console[0]);
269 } else if (absolute_enabled) {
270 if (!gui_fullscreen) {
271 sdl_grab_end(&sdl2_console[0]);
273 absolute_enabled = 0;
277 static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
278 int x, int y, int state)
280 static uint32_t bmap[INPUT_BUTTON__MAX] = {
281 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
282 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
283 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
284 [INPUT_BUTTON_SIDE] = SDL_BUTTON(SDL_BUTTON_X1),
285 [INPUT_BUTTON_EXTRA] = SDL_BUTTON(SDL_BUTTON_X2)
287 static uint32_t prev_state;
289 if (prev_state != state) {
290 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
294 if (qemu_input_is_absolute()) {
295 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X,
296 x, 0, surface_width(scon->surface));
297 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y,
298 y, 0, surface_height(scon->surface));
308 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
309 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
311 qemu_input_event_sync();
314 static void toggle_full_screen(struct sdl2_console *scon)
316 gui_fullscreen = !gui_fullscreen;
317 if (gui_fullscreen) {
318 SDL_SetWindowFullscreen(scon->real_window,
319 SDL_WINDOW_FULLSCREEN_DESKTOP);
320 gui_saved_grab = gui_grab;
321 sdl_grab_start(scon);
323 if (!gui_saved_grab) {
326 SDL_SetWindowFullscreen(scon->real_window, 0);
331 static int get_mod_state(void)
333 SDL_Keymod mod = SDL_GetModState();
336 return (mod & (gui_grab_code | KMOD_LSHIFT)) ==
337 (gui_grab_code | KMOD_LSHIFT);
338 } else if (ctrl_grab) {
339 return (mod & KMOD_RCTRL) == KMOD_RCTRL;
341 return (mod & gui_grab_code) == gui_grab_code;
345 static void *sdl2_win32_get_hwnd(struct sdl2_console *scon)
350 SDL_VERSION(&info.version);
351 if (SDL_GetWindowWMInfo(scon->real_window, &info)) {
352 return info.info.win.window;
358 static void handle_keydown(SDL_Event *ev)
361 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
362 int gui_key_modifier_pressed = get_mod_state();
369 if (!scon->ignore_hotkeys && gui_key_modifier_pressed && !ev->key.repeat) {
370 switch (ev->key.keysym.scancode) {
383 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
384 if (win < sdl2_num_outputs) {
385 sdl2_console[win].hidden = !sdl2_console[win].hidden;
386 if (sdl2_console[win].real_window) {
387 if (sdl2_console[win].hidden) {
388 SDL_HideWindow(sdl2_console[win].real_window);
390 SDL_ShowWindow(sdl2_console[win].real_window);
397 toggle_full_screen(scon);
403 sdl_grab_start(scon);
404 } else if (!gui_fullscreen) {
409 sdl2_window_resize(scon);
411 /* re-create scon->texture */
412 sdl2_2d_switch(&scon->dcl, scon->surface);
417 case SDL_SCANCODE_KP_PLUS:
418 case SDL_SCANCODE_KP_MINUS:
419 if (!gui_fullscreen) {
422 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
424 width = MAX(scr_w + (ev->key.keysym.scancode ==
425 SDL_SCANCODE_KP_PLUS ? 50 : -50),
427 height = (surface_height(scon->surface) * width) /
428 surface_width(scon->surface);
429 fprintf(stderr, "%s: scale to %dx%d\n",
430 __func__, width, height);
431 sdl_scale(scon, width, height);
441 sdl2_process_key(scon, &ev->key);
445 static void handle_keyup(SDL_Event *ev)
447 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
453 scon->ignore_hotkeys = false;
454 sdl2_process_key(scon, &ev->key);
457 static void handle_textinput(SDL_Event *ev)
459 struct sdl2_console *scon = get_scon_from_window(ev->text.windowID);
460 QemuConsole *con = scon ? scon->dcl.con : NULL;
466 if (qemu_console_is_graphic(con)) {
469 kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
472 static void handle_mousemotion(SDL_Event *ev)
475 struct sdl2_console *scon = get_scon_from_window(ev->motion.windowID);
477 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
481 if (qemu_input_is_absolute() || absolute_enabled) {
483 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
486 if (gui_grab && !gui_fullscreen
487 && (ev->motion.x == 0 || ev->motion.y == 0 ||
488 ev->motion.x == max_x || ev->motion.y == max_y)) {
492 (ev->motion.x > 0 && ev->motion.x < max_x &&
493 ev->motion.y > 0 && ev->motion.y < max_y)) {
494 sdl_grab_start(scon);
497 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
498 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
499 ev->motion.x, ev->motion.y, ev->motion.state);
503 static void handle_mousebutton(SDL_Event *ev)
505 int buttonstate = SDL_GetMouseState(NULL, NULL);
506 SDL_MouseButtonEvent *bev;
507 struct sdl2_console *scon = get_scon_from_window(ev->button.windowID);
509 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
514 if (!gui_grab && !qemu_input_is_absolute()) {
515 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
516 /* start grabbing all events */
517 sdl_grab_start(scon);
520 if (ev->type == SDL_MOUSEBUTTONDOWN) {
521 buttonstate |= SDL_BUTTON(bev->button);
523 buttonstate &= ~SDL_BUTTON(bev->button);
525 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
529 static void handle_mousewheel(SDL_Event *ev)
531 struct sdl2_console *scon = get_scon_from_window(ev->wheel.windowID);
532 SDL_MouseWheelEvent *wev = &ev->wheel;
535 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
540 btn = INPUT_BUTTON_WHEEL_UP;
541 } else if (wev->y < 0) {
542 btn = INPUT_BUTTON_WHEEL_DOWN;
543 } else if (wev->x < 0) {
544 btn = INPUT_BUTTON_WHEEL_RIGHT;
545 } else if (wev->x > 0) {
546 btn = INPUT_BUTTON_WHEEL_LEFT;
551 qemu_input_queue_btn(scon->dcl.con, btn, true);
552 qemu_input_event_sync();
553 qemu_input_queue_btn(scon->dcl.con, btn, false);
554 qemu_input_event_sync();
557 static void handle_windowevent(SDL_Event *ev)
559 struct sdl2_console *scon = get_scon_from_window(ev->window.windowID);
560 bool allow_close = true;
566 switch (ev->window.event) {
567 case SDL_WINDOWEVENT_RESIZED:
570 memset(&info, 0, sizeof(info));
571 info.width = ev->window.data1;
572 info.height = ev->window.data2;
573 dpy_set_ui_info(scon->dcl.con, &info, true);
577 case SDL_WINDOWEVENT_EXPOSED:
580 case SDL_WINDOWEVENT_FOCUS_GAINED:
581 win32_kbd_set_grab(gui_grab);
582 if (qemu_console_is_graphic(scon->dcl.con)) {
583 win32_kbd_set_window(sdl2_win32_get_hwnd(scon));
586 case SDL_WINDOWEVENT_ENTER:
587 if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
588 absolute_mouse_grab(scon);
590 /* If a new console window opened using a hotkey receives the
591 * focus, SDL sends another KEYDOWN event to the new window,
592 * closing the console window immediately after.
594 * Work around this by ignoring further hotkey events until a
597 scon->ignore_hotkeys = get_mod_state();
599 case SDL_WINDOWEVENT_FOCUS_LOST:
600 if (qemu_console_is_graphic(scon->dcl.con)) {
601 win32_kbd_set_window(NULL);
603 if (gui_grab && !gui_fullscreen) {
607 case SDL_WINDOWEVENT_RESTORED:
608 update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT);
610 case SDL_WINDOWEVENT_MINIMIZED:
611 update_displaychangelistener(&scon->dcl, 500);
613 case SDL_WINDOWEVENT_CLOSE:
614 if (qemu_console_is_graphic(scon->dcl.con)) {
615 if (scon->opts->has_window_close && !scon->opts->window_close) {
619 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
620 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
623 SDL_HideWindow(scon->real_window);
627 case SDL_WINDOWEVENT_SHOWN:
628 scon->hidden = false;
630 case SDL_WINDOWEVENT_HIDDEN:
636 void sdl2_poll_events(struct sdl2_console *scon)
638 SDL_Event ev1, *ev = &ev1;
639 bool allow_close = true;
642 if (scon->last_vm_running != runstate_is_running()) {
643 scon->last_vm_running = runstate_is_running();
644 sdl_update_caption(scon);
647 while (SDL_PollEvent(ev)) {
659 handle_textinput(ev);
662 if (scon->opts->has_window_close && !scon->opts->window_close) {
666 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
667 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
670 case SDL_MOUSEMOTION:
672 handle_mousemotion(ev);
674 case SDL_MOUSEBUTTONDOWN:
675 case SDL_MOUSEBUTTONUP:
677 handle_mousebutton(ev);
681 handle_mousewheel(ev);
683 case SDL_WINDOWEVENT:
684 handle_windowevent(ev);
692 if (scon->idle_counter < SDL2_MAX_IDLE_COUNT) {
693 scon->idle_counter++;
694 if (scon->idle_counter >= SDL2_MAX_IDLE_COUNT) {
695 scon->dcl.update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
699 scon->idle_counter = 0;
700 scon->dcl.update_interval = SDL2_REFRESH_INTERVAL_BUSY;
704 static void sdl_mouse_warp(DisplayChangeListener *dcl,
705 int x, int y, int on)
707 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
709 if (!qemu_console_is_graphic(scon->dcl.con)) {
715 sdl_show_cursor(scon);
717 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
718 SDL_SetCursor(guest_sprite);
719 if (!qemu_input_is_absolute() && !absolute_enabled) {
720 SDL_WarpMouseInWindow(scon->real_window, x, y);
723 } else if (gui_grab) {
724 sdl_hide_cursor(scon);
727 guest_x = x, guest_y = y;
730 static void sdl_mouse_define(DisplayChangeListener *dcl,
735 SDL_FreeCursor(guest_sprite);
738 if (guest_sprite_surface) {
739 SDL_FreeSurface(guest_sprite_surface);
742 guest_sprite_surface =
743 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
744 0xff0000, 0x00ff00, 0xff, 0xff000000);
746 if (!guest_sprite_surface) {
747 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
750 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
753 fprintf(stderr, "Failed to make color cursor from %p\n", c);
757 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
758 SDL_SetCursor(guest_sprite);
762 static void sdl_cleanup(void)
765 SDL_FreeCursor(guest_sprite);
767 SDL_QuitSubSystem(SDL_INIT_VIDEO);
770 static const DisplayChangeListenerOps dcl_2d_ops = {
771 .dpy_name = "sdl2-2d",
772 .dpy_gfx_update = sdl2_2d_update,
773 .dpy_gfx_switch = sdl2_2d_switch,
774 .dpy_gfx_check_format = sdl2_2d_check_format,
775 .dpy_refresh = sdl2_2d_refresh,
776 .dpy_mouse_set = sdl_mouse_warp,
777 .dpy_cursor_define = sdl_mouse_define,
781 static const DisplayChangeListenerOps dcl_gl_ops = {
782 .dpy_name = "sdl2-gl",
783 .dpy_gfx_update = sdl2_gl_update,
784 .dpy_gfx_switch = sdl2_gl_switch,
785 .dpy_gfx_check_format = console_gl_check_format,
786 .dpy_refresh = sdl2_gl_refresh,
787 .dpy_mouse_set = sdl_mouse_warp,
788 .dpy_cursor_define = sdl_mouse_define,
790 .dpy_gl_scanout_disable = sdl2_gl_scanout_disable,
791 .dpy_gl_scanout_texture = sdl2_gl_scanout_texture,
792 .dpy_gl_update = sdl2_gl_scanout_flush,
796 sdl2_gl_is_compatible_dcl(DisplayGLCtx *dgc,
797 DisplayChangeListener *dcl)
799 return dcl->ops == &dcl_gl_ops;
802 static const DisplayGLCtxOps gl_ctx_ops = {
803 .dpy_gl_ctx_is_compatible_dcl = sdl2_gl_is_compatible_dcl,
804 .dpy_gl_ctx_create = sdl2_gl_create_context,
805 .dpy_gl_ctx_destroy = sdl2_gl_destroy_context,
806 .dpy_gl_ctx_make_current = sdl2_gl_make_context_current,
810 static void sdl2_display_early_init(DisplayOptions *o)
812 assert(o->type == DISPLAY_TYPE_SDL);
813 if (o->has_gl && o->gl) {
820 static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
825 SDL_Surface *icon = NULL;
828 assert(o->type == DISPLAY_TYPE_SDL);
831 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
832 * accessible $DISPLAY to open X11 window. This is often the case
833 * when qemu is run using sudo. But in this case, and when actually
834 * run in X11 environment, SDL fights with X11 for the video card,
835 * making current display unavailable, often until reboot.
836 * So make x11 the default SDL video driver if this variable is unset.
837 * This is a bit hackish but saves us from bigger problem.
838 * Maybe it's a good idea to fix this in SDL instead.
840 if (!g_setenv("SDL_VIDEODRIVER", "x11", 0)) {
841 fprintf(stderr, "Could not set SDL_VIDEODRIVER environment variable\n");
846 if (SDL_Init(SDL_INIT_VIDEO)) {
847 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
851 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
852 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
854 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
855 memset(&info, 0, sizeof(info));
856 SDL_VERSION(&info.version);
858 gui_fullscreen = o->has_full_screen && o->full_screen;
860 if (o->u.sdl.has_grab_mod) {
861 if (o->u.sdl.grab_mod == HOT_KEY_MOD_LSHIFT_LCTRL_LALT) {
863 } else if (o->u.sdl.grab_mod == HOT_KEY_MOD_RCTRL) {
869 QemuConsole *con = qemu_console_lookup_by_index(i);
874 sdl2_num_outputs = i;
875 if (sdl2_num_outputs == 0) {
878 sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
879 for (i = 0; i < sdl2_num_outputs; i++) {
880 QemuConsole *con = qemu_console_lookup_by_index(i);
882 if (!qemu_console_is_graphic(con) &&
883 qemu_console_get_index(con) != 0) {
884 sdl2_console[i].hidden = true;
886 sdl2_console[i].idx = i;
887 sdl2_console[i].opts = o;
889 sdl2_console[i].opengl = display_opengl;
890 sdl2_console[i].dcl.ops = display_opengl ? &dcl_gl_ops : &dcl_2d_ops;
891 sdl2_console[i].dgc.ops = display_opengl ? &gl_ctx_ops : NULL;
893 sdl2_console[i].opengl = 0;
894 sdl2_console[i].dcl.ops = &dcl_2d_ops;
896 sdl2_console[i].dcl.con = con;
897 sdl2_console[i].kbd = qkbd_state_init(con);
898 if (display_opengl) {
899 qemu_console_set_display_gl_ctx(con, &sdl2_console[i].dgc);
901 register_displaychangelistener(&sdl2_console[i].dcl);
903 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
904 if (SDL_GetWindowWMInfo(sdl2_console[i].real_window, &info)) {
905 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
906 qemu_console_set_window_id(con, (uintptr_t)info.info.win.window);
907 #elif defined(SDL_VIDEO_DRIVER_X11)
908 qemu_console_set_window_id(con, info.info.x11.window);
914 #ifdef CONFIG_SDL_IMAGE
915 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/128x128/apps/qemu.png");
916 icon = IMG_Load(dir);
918 /* Load a 32x32x4 image. White pixels are transparent. */
919 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/32x32/apps/qemu.bmp");
920 icon = SDL_LoadBMP(dir);
922 uint32_t colorkey = SDL_MapRGB(icon->format, 255, 255, 255);
923 SDL_SetColorKey(icon, SDL_TRUE, colorkey);
928 SDL_SetWindowIcon(sdl2_console[0].real_window, icon);
931 mouse_mode_notifier.notify = sdl_mouse_mode_change;
932 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
934 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
935 sdl_cursor_normal = SDL_GetCursor();
937 if (gui_fullscreen) {
938 sdl_grab_start(&sdl2_console[0]);
944 static QemuDisplay qemu_display_sdl2 = {
945 .type = DISPLAY_TYPE_SDL,
946 .early_init = sdl2_display_early_init,
947 .init = sdl2_display_init,
950 static void register_sdl1(void)
952 qemu_display_register(&qemu_display_sdl2);
955 type_init(register_sdl1);
958 module_dep("ui-opengl");