block/nvme: Map doorbells pages write-only
[qemu/ar7.git] / ui / sdl2.c
blobabad7f981e50570372a1207f8a8fb0f7d06df581
1 /*
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
22 * THE SOFTWARE.
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"
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"
36 static int sdl2_num_outputs;
37 static struct sdl2_console *sdl2_console;
39 static SDL_Surface *guest_sprite_surface;
40 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
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;
53 #define SDL2_REFRESH_INTERVAL_BUSY 10
54 #define SDL2_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
55 / SDL2_REFRESH_INTERVAL_BUSY + 1)
57 static void sdl_update_caption(struct sdl2_console *scon);
59 static struct sdl2_console *get_scon_from_window(uint32_t window_id)
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];
67 return NULL;
70 void sdl2_window_create(struct sdl2_console *scon)
72 int flags = 0;
74 if (!scon->surface) {
75 return;
77 assert(!scon->real_window);
79 if (gui_fullscreen) {
80 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
81 } else {
82 flags |= SDL_WINDOW_RESIZABLE;
84 if (scon->hidden) {
85 flags |= SDL_WINDOW_HIDDEN;
88 scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
89 SDL_WINDOWPOS_UNDEFINED,
90 surface_width(scon->surface),
91 surface_height(scon->surface),
92 flags);
93 scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
94 if (scon->opengl) {
95 scon->winctx = SDL_GL_GetCurrentContext();
97 sdl_update_caption(scon);
100 void sdl2_window_destroy(struct sdl2_console *scon)
102 if (!scon->real_window) {
103 return;
106 SDL_DestroyRenderer(scon->real_renderer);
107 scon->real_renderer = NULL;
108 SDL_DestroyWindow(scon->real_window);
109 scon->real_window = NULL;
112 void sdl2_window_resize(struct sdl2_console *scon)
114 if (!scon->real_window) {
115 return;
118 SDL_SetWindowSize(scon->real_window,
119 surface_width(scon->surface),
120 surface_height(scon->surface));
123 static void sdl2_redraw(struct sdl2_console *scon)
125 if (scon->opengl) {
126 #ifdef CONFIG_OPENGL
127 sdl2_gl_redraw(scon);
128 #endif
129 } else {
130 sdl2_2d_redraw(scon);
134 static void sdl_update_caption(struct sdl2_console *scon)
136 char win_title[1024];
137 char icon_title[1024];
138 const char *status = "";
140 if (!runstate_is_running()) {
141 status = " [Stopped]";
142 } else if (gui_grab) {
143 if (alt_grab) {
144 status = " - Press Ctrl-Alt-Shift-G to exit grab";
145 } else if (ctrl_grab) {
146 status = " - Press Right-Ctrl-G to exit grab";
147 } else {
148 status = " - Press Ctrl-Alt-G to exit grab";
152 if (qemu_name) {
153 snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
154 scon->idx, status);
155 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
156 } else {
157 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
158 snprintf(icon_title, sizeof(icon_title), "QEMU");
161 if (scon->real_window) {
162 SDL_SetWindowTitle(scon->real_window, win_title);
166 static void sdl_hide_cursor(struct sdl2_console *scon)
168 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
169 return;
172 SDL_ShowCursor(SDL_DISABLE);
173 SDL_SetCursor(sdl_cursor_hidden);
175 if (!qemu_input_is_absolute()) {
176 SDL_SetRelativeMouseMode(SDL_TRUE);
180 static void sdl_show_cursor(struct sdl2_console *scon)
182 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
183 return;
186 if (!qemu_input_is_absolute()) {
187 SDL_SetRelativeMouseMode(SDL_FALSE);
190 if (guest_cursor &&
191 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
192 SDL_SetCursor(guest_sprite);
193 } else {
194 SDL_SetCursor(sdl_cursor_normal);
197 SDL_ShowCursor(SDL_ENABLE);
200 static void sdl_grab_start(struct sdl2_console *scon)
202 QemuConsole *con = scon ? scon->dcl.con : NULL;
204 if (!con || !qemu_console_is_graphic(con)) {
205 return;
208 * If the application is not active, do not try to enter grab state. This
209 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
210 * application (SDL bug).
212 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
213 return;
215 if (guest_cursor) {
216 SDL_SetCursor(guest_sprite);
217 if (!qemu_input_is_absolute() && !absolute_enabled) {
218 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
220 } else {
221 sdl_hide_cursor(scon);
223 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
224 gui_grab = 1;
225 win32_kbd_set_grab(true);
226 sdl_update_caption(scon);
229 static void sdl_grab_end(struct sdl2_console *scon)
231 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
232 gui_grab = 0;
233 win32_kbd_set_grab(false);
234 sdl_show_cursor(scon);
235 sdl_update_caption(scon);
238 static void absolute_mouse_grab(struct sdl2_console *scon)
240 int mouse_x, mouse_y;
241 int scr_w, scr_h;
242 SDL_GetMouseState(&mouse_x, &mouse_y);
243 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
244 if (mouse_x > 0 && mouse_x < scr_w - 1 &&
245 mouse_y > 0 && mouse_y < scr_h - 1) {
246 sdl_grab_start(scon);
250 static void sdl_mouse_mode_change(Notifier *notify, void *data)
252 if (qemu_input_is_absolute()) {
253 if (!absolute_enabled) {
254 absolute_enabled = 1;
255 SDL_SetRelativeMouseMode(SDL_FALSE);
256 absolute_mouse_grab(&sdl2_console[0]);
258 } else if (absolute_enabled) {
259 if (!gui_fullscreen) {
260 sdl_grab_end(&sdl2_console[0]);
262 absolute_enabled = 0;
266 static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
267 int x, int y, int state)
269 static uint32_t bmap[INPUT_BUTTON__MAX] = {
270 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
271 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
272 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
274 static uint32_t prev_state;
276 if (prev_state != state) {
277 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
278 prev_state = state;
281 if (qemu_input_is_absolute()) {
282 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X,
283 x, 0, surface_width(scon->surface));
284 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y,
285 y, 0, surface_height(scon->surface));
286 } else {
287 if (guest_cursor) {
288 x -= guest_x;
289 y -= guest_y;
290 guest_x += x;
291 guest_y += y;
292 dx = x;
293 dy = y;
295 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
296 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
298 qemu_input_event_sync();
301 static void toggle_full_screen(struct sdl2_console *scon)
303 gui_fullscreen = !gui_fullscreen;
304 if (gui_fullscreen) {
305 SDL_SetWindowFullscreen(scon->real_window,
306 SDL_WINDOW_FULLSCREEN_DESKTOP);
307 gui_saved_grab = gui_grab;
308 sdl_grab_start(scon);
309 } else {
310 if (!gui_saved_grab) {
311 sdl_grab_end(scon);
313 SDL_SetWindowFullscreen(scon->real_window, 0);
315 sdl2_redraw(scon);
318 static int get_mod_state(void)
320 SDL_Keymod mod = SDL_GetModState();
322 if (alt_grab) {
323 return (mod & (gui_grab_code | KMOD_LSHIFT)) ==
324 (gui_grab_code | KMOD_LSHIFT);
325 } else if (ctrl_grab) {
326 return (mod & KMOD_RCTRL) == KMOD_RCTRL;
327 } else {
328 return (mod & gui_grab_code) == gui_grab_code;
332 static void *sdl2_win32_get_hwnd(struct sdl2_console *scon)
334 #ifdef CONFIG_WIN32
335 SDL_SysWMinfo info;
337 SDL_VERSION(&info.version);
338 if (SDL_GetWindowWMInfo(scon->real_window, &info)) {
339 return info.info.win.window;
341 #endif
342 return NULL;
345 static void handle_keydown(SDL_Event *ev)
347 int win;
348 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
349 int gui_key_modifier_pressed = get_mod_state();
350 int gui_keysym = 0;
352 if (!scon) {
353 return;
356 if (!scon->ignore_hotkeys && gui_key_modifier_pressed && !ev->key.repeat) {
357 switch (ev->key.keysym.scancode) {
358 case SDL_SCANCODE_2:
359 case SDL_SCANCODE_3:
360 case SDL_SCANCODE_4:
361 case SDL_SCANCODE_5:
362 case SDL_SCANCODE_6:
363 case SDL_SCANCODE_7:
364 case SDL_SCANCODE_8:
365 case SDL_SCANCODE_9:
366 if (gui_grab) {
367 sdl_grab_end(scon);
370 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
371 if (win < sdl2_num_outputs) {
372 sdl2_console[win].hidden = !sdl2_console[win].hidden;
373 if (sdl2_console[win].real_window) {
374 if (sdl2_console[win].hidden) {
375 SDL_HideWindow(sdl2_console[win].real_window);
376 } else {
377 SDL_ShowWindow(sdl2_console[win].real_window);
380 gui_keysym = 1;
382 break;
383 case SDL_SCANCODE_F:
384 toggle_full_screen(scon);
385 gui_keysym = 1;
386 break;
387 case SDL_SCANCODE_G:
388 gui_keysym = 1;
389 if (!gui_grab) {
390 sdl_grab_start(scon);
391 } else if (!gui_fullscreen) {
392 sdl_grab_end(scon);
394 break;
395 case SDL_SCANCODE_U:
396 sdl2_window_resize(scon);
397 if (!scon->opengl) {
398 /* re-create scon->texture */
399 sdl2_2d_switch(&scon->dcl, scon->surface);
401 gui_keysym = 1;
402 break;
403 #if 0
404 case SDL_SCANCODE_KP_PLUS:
405 case SDL_SCANCODE_KP_MINUS:
406 if (!gui_fullscreen) {
407 int scr_w, scr_h;
408 int width, height;
409 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
411 width = MAX(scr_w + (ev->key.keysym.scancode ==
412 SDL_SCANCODE_KP_PLUS ? 50 : -50),
413 160);
414 height = (surface_height(scon->surface) * width) /
415 surface_width(scon->surface);
416 fprintf(stderr, "%s: scale to %dx%d\n",
417 __func__, width, height);
418 sdl_scale(scon, width, height);
419 sdl2_redraw(scon);
420 gui_keysym = 1;
422 #endif
423 default:
424 break;
427 if (!gui_keysym) {
428 sdl2_process_key(scon, &ev->key);
432 static void handle_keyup(SDL_Event *ev)
434 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
436 if (!scon) {
437 return;
440 scon->ignore_hotkeys = false;
441 sdl2_process_key(scon, &ev->key);
444 static void handle_textinput(SDL_Event *ev)
446 struct sdl2_console *scon = get_scon_from_window(ev->text.windowID);
447 QemuConsole *con = scon ? scon->dcl.con : NULL;
449 if (!con) {
450 return;
453 if (qemu_console_is_graphic(con)) {
454 return;
456 kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
459 static void handle_mousemotion(SDL_Event *ev)
461 int max_x, max_y;
462 struct sdl2_console *scon = get_scon_from_window(ev->motion.windowID);
464 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
465 return;
468 if (qemu_input_is_absolute() || absolute_enabled) {
469 int scr_w, scr_h;
470 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
471 max_x = scr_w - 1;
472 max_y = scr_h - 1;
473 if (gui_grab && !gui_fullscreen
474 && (ev->motion.x == 0 || ev->motion.y == 0 ||
475 ev->motion.x == max_x || ev->motion.y == max_y)) {
476 sdl_grab_end(scon);
478 if (!gui_grab &&
479 (ev->motion.x > 0 && ev->motion.x < max_x &&
480 ev->motion.y > 0 && ev->motion.y < max_y)) {
481 sdl_grab_start(scon);
484 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
485 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
486 ev->motion.x, ev->motion.y, ev->motion.state);
490 static void handle_mousebutton(SDL_Event *ev)
492 int buttonstate = SDL_GetMouseState(NULL, NULL);
493 SDL_MouseButtonEvent *bev;
494 struct sdl2_console *scon = get_scon_from_window(ev->button.windowID);
496 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
497 return;
500 bev = &ev->button;
501 if (!gui_grab && !qemu_input_is_absolute()) {
502 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
503 /* start grabbing all events */
504 sdl_grab_start(scon);
506 } else {
507 if (ev->type == SDL_MOUSEBUTTONDOWN) {
508 buttonstate |= SDL_BUTTON(bev->button);
509 } else {
510 buttonstate &= ~SDL_BUTTON(bev->button);
512 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
516 static void handle_mousewheel(SDL_Event *ev)
518 struct sdl2_console *scon = get_scon_from_window(ev->wheel.windowID);
519 SDL_MouseWheelEvent *wev = &ev->wheel;
520 InputButton btn;
522 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
523 return;
526 if (wev->y > 0) {
527 btn = INPUT_BUTTON_WHEEL_UP;
528 } else if (wev->y < 0) {
529 btn = INPUT_BUTTON_WHEEL_DOWN;
530 } else {
531 return;
534 qemu_input_queue_btn(scon->dcl.con, btn, true);
535 qemu_input_event_sync();
536 qemu_input_queue_btn(scon->dcl.con, btn, false);
537 qemu_input_event_sync();
540 static void handle_windowevent(SDL_Event *ev)
542 struct sdl2_console *scon = get_scon_from_window(ev->window.windowID);
543 bool allow_close = true;
545 if (!scon) {
546 return;
549 switch (ev->window.event) {
550 case SDL_WINDOWEVENT_RESIZED:
552 QemuUIInfo info;
553 memset(&info, 0, sizeof(info));
554 info.width = ev->window.data1;
555 info.height = ev->window.data2;
556 dpy_set_ui_info(scon->dcl.con, &info);
558 sdl2_redraw(scon);
559 break;
560 case SDL_WINDOWEVENT_EXPOSED:
561 sdl2_redraw(scon);
562 break;
563 case SDL_WINDOWEVENT_FOCUS_GAINED:
564 win32_kbd_set_grab(gui_grab);
565 if (qemu_console_is_graphic(scon->dcl.con)) {
566 win32_kbd_set_window(sdl2_win32_get_hwnd(scon));
568 /* fall through */
569 case SDL_WINDOWEVENT_ENTER:
570 if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
571 absolute_mouse_grab(scon);
573 /* If a new console window opened using a hotkey receives the
574 * focus, SDL sends another KEYDOWN event to the new window,
575 * closing the console window immediately after.
577 * Work around this by ignoring further hotkey events until a
578 * key is released.
580 scon->ignore_hotkeys = get_mod_state();
581 break;
582 case SDL_WINDOWEVENT_FOCUS_LOST:
583 if (qemu_console_is_graphic(scon->dcl.con)) {
584 win32_kbd_set_window(NULL);
586 if (gui_grab && !gui_fullscreen) {
587 sdl_grab_end(scon);
589 break;
590 case SDL_WINDOWEVENT_RESTORED:
591 update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT);
592 break;
593 case SDL_WINDOWEVENT_MINIMIZED:
594 update_displaychangelistener(&scon->dcl, 500);
595 break;
596 case SDL_WINDOWEVENT_CLOSE:
597 if (qemu_console_is_graphic(scon->dcl.con)) {
598 if (scon->opts->has_window_close && !scon->opts->window_close) {
599 allow_close = false;
601 if (allow_close) {
602 no_shutdown = 0;
603 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
605 } else {
606 SDL_HideWindow(scon->real_window);
607 scon->hidden = true;
609 break;
610 case SDL_WINDOWEVENT_SHOWN:
611 scon->hidden = false;
612 break;
613 case SDL_WINDOWEVENT_HIDDEN:
614 scon->hidden = true;
615 break;
619 void sdl2_poll_events(struct sdl2_console *scon)
621 SDL_Event ev1, *ev = &ev1;
622 bool allow_close = true;
623 int idle = 1;
625 if (scon->last_vm_running != runstate_is_running()) {
626 scon->last_vm_running = runstate_is_running();
627 sdl_update_caption(scon);
630 while (SDL_PollEvent(ev)) {
631 switch (ev->type) {
632 case SDL_KEYDOWN:
633 idle = 0;
634 handle_keydown(ev);
635 break;
636 case SDL_KEYUP:
637 idle = 0;
638 handle_keyup(ev);
639 break;
640 case SDL_TEXTINPUT:
641 idle = 0;
642 handle_textinput(ev);
643 break;
644 case SDL_QUIT:
645 if (scon->opts->has_window_close && !scon->opts->window_close) {
646 allow_close = false;
648 if (allow_close) {
649 no_shutdown = 0;
650 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
652 break;
653 case SDL_MOUSEMOTION:
654 idle = 0;
655 handle_mousemotion(ev);
656 break;
657 case SDL_MOUSEBUTTONDOWN:
658 case SDL_MOUSEBUTTONUP:
659 idle = 0;
660 handle_mousebutton(ev);
661 break;
662 case SDL_MOUSEWHEEL:
663 idle = 0;
664 handle_mousewheel(ev);
665 break;
666 case SDL_WINDOWEVENT:
667 handle_windowevent(ev);
668 break;
669 default:
670 break;
674 if (idle) {
675 if (scon->idle_counter < SDL2_MAX_IDLE_COUNT) {
676 scon->idle_counter++;
677 if (scon->idle_counter >= SDL2_MAX_IDLE_COUNT) {
678 scon->dcl.update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
681 } else {
682 scon->idle_counter = 0;
683 scon->dcl.update_interval = SDL2_REFRESH_INTERVAL_BUSY;
687 static void sdl_mouse_warp(DisplayChangeListener *dcl,
688 int x, int y, int on)
690 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
692 if (!qemu_console_is_graphic(scon->dcl.con)) {
693 return;
696 if (on) {
697 if (!guest_cursor) {
698 sdl_show_cursor(scon);
700 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
701 SDL_SetCursor(guest_sprite);
702 if (!qemu_input_is_absolute() && !absolute_enabled) {
703 SDL_WarpMouseInWindow(scon->real_window, x, y);
706 } else if (gui_grab) {
707 sdl_hide_cursor(scon);
709 guest_cursor = on;
710 guest_x = x, guest_y = y;
713 static void sdl_mouse_define(DisplayChangeListener *dcl,
714 QEMUCursor *c)
717 if (guest_sprite) {
718 SDL_FreeCursor(guest_sprite);
721 if (guest_sprite_surface) {
722 SDL_FreeSurface(guest_sprite_surface);
725 guest_sprite_surface =
726 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
727 0xff0000, 0x00ff00, 0xff, 0xff000000);
729 if (!guest_sprite_surface) {
730 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
731 return;
733 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
734 c->hot_x, c->hot_y);
735 if (!guest_sprite) {
736 fprintf(stderr, "Failed to make color cursor from %p\n", c);
737 return;
739 if (guest_cursor &&
740 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
741 SDL_SetCursor(guest_sprite);
745 static void sdl_cleanup(void)
747 if (guest_sprite) {
748 SDL_FreeCursor(guest_sprite);
750 SDL_QuitSubSystem(SDL_INIT_VIDEO);
753 static const DisplayChangeListenerOps dcl_2d_ops = {
754 .dpy_name = "sdl2-2d",
755 .dpy_gfx_update = sdl2_2d_update,
756 .dpy_gfx_switch = sdl2_2d_switch,
757 .dpy_gfx_check_format = sdl2_2d_check_format,
758 .dpy_refresh = sdl2_2d_refresh,
759 .dpy_mouse_set = sdl_mouse_warp,
760 .dpy_cursor_define = sdl_mouse_define,
763 #ifdef CONFIG_OPENGL
764 static const DisplayChangeListenerOps dcl_gl_ops = {
765 .dpy_name = "sdl2-gl",
766 .dpy_gfx_update = sdl2_gl_update,
767 .dpy_gfx_switch = sdl2_gl_switch,
768 .dpy_gfx_check_format = console_gl_check_format,
769 .dpy_refresh = sdl2_gl_refresh,
770 .dpy_mouse_set = sdl_mouse_warp,
771 .dpy_cursor_define = sdl_mouse_define,
773 .dpy_gl_ctx_create = sdl2_gl_create_context,
774 .dpy_gl_ctx_destroy = sdl2_gl_destroy_context,
775 .dpy_gl_ctx_make_current = sdl2_gl_make_context_current,
776 .dpy_gl_ctx_get_current = sdl2_gl_get_current_context,
777 .dpy_gl_scanout_disable = sdl2_gl_scanout_disable,
778 .dpy_gl_scanout_texture = sdl2_gl_scanout_texture,
779 .dpy_gl_update = sdl2_gl_scanout_flush,
781 #endif
783 static void sdl2_display_early_init(DisplayOptions *o)
785 assert(o->type == DISPLAY_TYPE_SDL);
786 if (o->has_gl && o->gl) {
787 #ifdef CONFIG_OPENGL
788 display_opengl = 1;
789 #endif
793 static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
795 uint8_t data = 0;
796 int i;
797 SDL_SysWMinfo info;
798 SDL_Surface *icon = NULL;
799 char *dir;
801 assert(o->type == DISPLAY_TYPE_SDL);
803 #ifdef __linux__
804 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
805 * accessible $DISPLAY to open X11 window. This is often the case
806 * when qemu is run using sudo. But in this case, and when actually
807 * run in X11 environment, SDL fights with X11 for the video card,
808 * making current display unavailable, often until reboot.
809 * So make x11 the default SDL video driver if this variable is unset.
810 * This is a bit hackish but saves us from bigger problem.
811 * Maybe it's a good idea to fix this in SDL instead.
813 g_setenv("SDL_VIDEODRIVER", "x11", 0);
814 #endif
816 if (SDL_Init(SDL_INIT_VIDEO)) {
817 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
818 SDL_GetError());
819 exit(1);
821 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
822 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
823 #endif
824 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
825 memset(&info, 0, sizeof(info));
826 SDL_VERSION(&info.version);
828 gui_fullscreen = o->has_full_screen && o->full_screen;
830 for (i = 0;; i++) {
831 QemuConsole *con = qemu_console_lookup_by_index(i);
832 if (!con) {
833 break;
836 sdl2_num_outputs = i;
837 if (sdl2_num_outputs == 0) {
838 return;
840 sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
841 for (i = 0; i < sdl2_num_outputs; i++) {
842 QemuConsole *con = qemu_console_lookup_by_index(i);
843 assert(con != NULL);
844 if (!qemu_console_is_graphic(con) &&
845 qemu_console_get_index(con) != 0) {
846 sdl2_console[i].hidden = true;
848 sdl2_console[i].idx = i;
849 sdl2_console[i].opts = o;
850 #ifdef CONFIG_OPENGL
851 sdl2_console[i].opengl = display_opengl;
852 sdl2_console[i].dcl.ops = display_opengl ? &dcl_gl_ops : &dcl_2d_ops;
853 #else
854 sdl2_console[i].opengl = 0;
855 sdl2_console[i].dcl.ops = &dcl_2d_ops;
856 #endif
857 sdl2_console[i].dcl.con = con;
858 sdl2_console[i].kbd = qkbd_state_init(con);
859 register_displaychangelistener(&sdl2_console[i].dcl);
861 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
862 if (SDL_GetWindowWMInfo(sdl2_console[i].real_window, &info)) {
863 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
864 qemu_console_set_window_id(con, (uintptr_t)info.info.win.window);
865 #elif defined(SDL_VIDEO_DRIVER_X11)
866 qemu_console_set_window_id(con, info.info.x11.window);
867 #endif
869 #endif
872 #ifdef CONFIG_SDL_IMAGE
873 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/128x128/apps/qemu.png");
874 icon = IMG_Load(dir);
875 #else
876 /* Load a 32x32x4 image. White pixels are transparent. */
877 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/32x32/apps/qemu.bmp");
878 icon = SDL_LoadBMP(dir);
879 if (icon) {
880 uint32_t colorkey = SDL_MapRGB(icon->format, 255, 255, 255);
881 SDL_SetColorKey(icon, SDL_TRUE, colorkey);
883 #endif
884 g_free(dir);
885 if (icon) {
886 SDL_SetWindowIcon(sdl2_console[0].real_window, icon);
889 mouse_mode_notifier.notify = sdl_mouse_mode_change;
890 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
892 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
893 sdl_cursor_normal = SDL_GetCursor();
895 if (gui_fullscreen) {
896 sdl_grab_start(&sdl2_console[0]);
899 atexit(sdl_cleanup);
902 static QemuDisplay qemu_display_sdl2 = {
903 .type = DISPLAY_TYPE_SDL,
904 .early_init = sdl2_display_early_init,
905 .init = sdl2_display_init,
908 static void register_sdl1(void)
910 qemu_display_register(&qemu_display_sdl2);
913 type_init(register_sdl1);