target/nios2: Move IIC code into CPU object proper
[qemu/ar7.git] / ui / sdl2.c
bloba578017268ae4fb13e9c72b7b225ecd189fd9b63
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;
87 #ifdef CONFIG_OPENGL
88 if (scon->opengl) {
89 flags |= SDL_WINDOW_OPENGL;
91 #endif
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();
102 sdl_update_caption(scon);
105 void sdl2_window_destroy(struct sdl2_console *scon)
107 if (!scon->real_window) {
108 return;
111 SDL_DestroyRenderer(scon->real_renderer);
112 scon->real_renderer = NULL;
113 SDL_DestroyWindow(scon->real_window);
114 scon->real_window = NULL;
117 void sdl2_window_resize(struct sdl2_console *scon)
119 if (!scon->real_window) {
120 return;
123 SDL_SetWindowSize(scon->real_window,
124 surface_width(scon->surface),
125 surface_height(scon->surface));
128 static void sdl2_redraw(struct sdl2_console *scon)
130 if (scon->opengl) {
131 #ifdef CONFIG_OPENGL
132 sdl2_gl_redraw(scon);
133 #endif
134 } else {
135 sdl2_2d_redraw(scon);
139 static void sdl_update_caption(struct sdl2_console *scon)
141 char win_title[1024];
142 char icon_title[1024];
143 const char *status = "";
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";
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");
166 if (scon->real_window) {
167 SDL_SetWindowTitle(scon->real_window, win_title);
171 static void sdl_hide_cursor(struct sdl2_console *scon)
173 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
174 return;
177 SDL_ShowCursor(SDL_DISABLE);
178 SDL_SetCursor(sdl_cursor_hidden);
180 if (!qemu_input_is_absolute()) {
181 SDL_SetRelativeMouseMode(SDL_TRUE);
185 static void sdl_show_cursor(struct sdl2_console *scon)
187 if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
188 return;
191 if (!qemu_input_is_absolute()) {
192 SDL_SetRelativeMouseMode(SDL_FALSE);
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);
202 SDL_ShowCursor(SDL_ENABLE);
205 static void sdl_grab_start(struct sdl2_console *scon)
207 QemuConsole *con = scon ? scon->dcl.con : NULL;
209 if (!con || !qemu_console_is_graphic(con)) {
210 return;
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).
217 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
218 return;
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);
225 } else {
226 sdl_hide_cursor(scon);
228 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
229 gui_grab = 1;
230 win32_kbd_set_grab(true);
231 sdl_update_caption(scon);
234 static void sdl_grab_end(struct sdl2_console *scon)
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);
243 static void absolute_mouse_grab(struct sdl2_console *scon)
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);
255 static void sdl_mouse_mode_change(Notifier *notify, void *data)
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]);
263 } else if (absolute_enabled) {
264 if (!gui_fullscreen) {
265 sdl_grab_end(&sdl2_console[0]);
267 absolute_enabled = 0;
271 static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
272 int x, int y, int state)
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 [INPUT_BUTTON_SIDE] = SDL_BUTTON(SDL_BUTTON_X1),
279 [INPUT_BUTTON_EXTRA] = SDL_BUTTON(SDL_BUTTON_X2)
281 static uint32_t prev_state;
283 if (prev_state != state) {
284 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
285 prev_state = state;
288 if (qemu_input_is_absolute()) {
289 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X,
290 x, 0, surface_width(scon->surface));
291 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y,
292 y, 0, surface_height(scon->surface));
293 } else {
294 if (guest_cursor) {
295 x -= guest_x;
296 y -= guest_y;
297 guest_x += x;
298 guest_y += y;
299 dx = x;
300 dy = y;
302 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
303 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
305 qemu_input_event_sync();
308 static void toggle_full_screen(struct sdl2_console *scon)
310 gui_fullscreen = !gui_fullscreen;
311 if (gui_fullscreen) {
312 SDL_SetWindowFullscreen(scon->real_window,
313 SDL_WINDOW_FULLSCREEN_DESKTOP);
314 gui_saved_grab = gui_grab;
315 sdl_grab_start(scon);
316 } else {
317 if (!gui_saved_grab) {
318 sdl_grab_end(scon);
320 SDL_SetWindowFullscreen(scon->real_window, 0);
322 sdl2_redraw(scon);
325 static int get_mod_state(void)
327 SDL_Keymod mod = SDL_GetModState();
329 if (alt_grab) {
330 return (mod & (gui_grab_code | KMOD_LSHIFT)) ==
331 (gui_grab_code | KMOD_LSHIFT);
332 } else if (ctrl_grab) {
333 return (mod & KMOD_RCTRL) == KMOD_RCTRL;
334 } else {
335 return (mod & gui_grab_code) == gui_grab_code;
339 static void *sdl2_win32_get_hwnd(struct sdl2_console *scon)
341 #ifdef CONFIG_WIN32
342 SDL_SysWMinfo info;
344 SDL_VERSION(&info.version);
345 if (SDL_GetWindowWMInfo(scon->real_window, &info)) {
346 return info.info.win.window;
348 #endif
349 return NULL;
352 static void handle_keydown(SDL_Event *ev)
354 int win;
355 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
356 int gui_key_modifier_pressed = get_mod_state();
357 int gui_keysym = 0;
359 if (!scon) {
360 return;
363 if (!scon->ignore_hotkeys && gui_key_modifier_pressed && !ev->key.repeat) {
364 switch (ev->key.keysym.scancode) {
365 case SDL_SCANCODE_2:
366 case SDL_SCANCODE_3:
367 case SDL_SCANCODE_4:
368 case SDL_SCANCODE_5:
369 case SDL_SCANCODE_6:
370 case SDL_SCANCODE_7:
371 case SDL_SCANCODE_8:
372 case SDL_SCANCODE_9:
373 if (gui_grab) {
374 sdl_grab_end(scon);
377 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
378 if (win < sdl2_num_outputs) {
379 sdl2_console[win].hidden = !sdl2_console[win].hidden;
380 if (sdl2_console[win].real_window) {
381 if (sdl2_console[win].hidden) {
382 SDL_HideWindow(sdl2_console[win].real_window);
383 } else {
384 SDL_ShowWindow(sdl2_console[win].real_window);
387 gui_keysym = 1;
389 break;
390 case SDL_SCANCODE_F:
391 toggle_full_screen(scon);
392 gui_keysym = 1;
393 break;
394 case SDL_SCANCODE_G:
395 gui_keysym = 1;
396 if (!gui_grab) {
397 sdl_grab_start(scon);
398 } else if (!gui_fullscreen) {
399 sdl_grab_end(scon);
401 break;
402 case SDL_SCANCODE_U:
403 sdl2_window_resize(scon);
404 if (!scon->opengl) {
405 /* re-create scon->texture */
406 sdl2_2d_switch(&scon->dcl, scon->surface);
408 gui_keysym = 1;
409 break;
410 #if 0
411 case SDL_SCANCODE_KP_PLUS:
412 case SDL_SCANCODE_KP_MINUS:
413 if (!gui_fullscreen) {
414 int scr_w, scr_h;
415 int width, height;
416 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
418 width = MAX(scr_w + (ev->key.keysym.scancode ==
419 SDL_SCANCODE_KP_PLUS ? 50 : -50),
420 160);
421 height = (surface_height(scon->surface) * width) /
422 surface_width(scon->surface);
423 fprintf(stderr, "%s: scale to %dx%d\n",
424 __func__, width, height);
425 sdl_scale(scon, width, height);
426 sdl2_redraw(scon);
427 gui_keysym = 1;
429 #endif
430 default:
431 break;
434 if (!gui_keysym) {
435 sdl2_process_key(scon, &ev->key);
439 static void handle_keyup(SDL_Event *ev)
441 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
443 if (!scon) {
444 return;
447 scon->ignore_hotkeys = false;
448 sdl2_process_key(scon, &ev->key);
451 static void handle_textinput(SDL_Event *ev)
453 struct sdl2_console *scon = get_scon_from_window(ev->text.windowID);
454 QemuConsole *con = scon ? scon->dcl.con : NULL;
456 if (!con) {
457 return;
460 if (qemu_console_is_graphic(con)) {
461 return;
463 kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
466 static void handle_mousemotion(SDL_Event *ev)
468 int max_x, max_y;
469 struct sdl2_console *scon = get_scon_from_window(ev->motion.windowID);
471 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
472 return;
475 if (qemu_input_is_absolute() || absolute_enabled) {
476 int scr_w, scr_h;
477 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
478 max_x = scr_w - 1;
479 max_y = scr_h - 1;
480 if (gui_grab && !gui_fullscreen
481 && (ev->motion.x == 0 || ev->motion.y == 0 ||
482 ev->motion.x == max_x || ev->motion.y == max_y)) {
483 sdl_grab_end(scon);
485 if (!gui_grab &&
486 (ev->motion.x > 0 && ev->motion.x < max_x &&
487 ev->motion.y > 0 && ev->motion.y < max_y)) {
488 sdl_grab_start(scon);
491 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
492 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
493 ev->motion.x, ev->motion.y, ev->motion.state);
497 static void handle_mousebutton(SDL_Event *ev)
499 int buttonstate = SDL_GetMouseState(NULL, NULL);
500 SDL_MouseButtonEvent *bev;
501 struct sdl2_console *scon = get_scon_from_window(ev->button.windowID);
503 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
504 return;
507 bev = &ev->button;
508 if (!gui_grab && !qemu_input_is_absolute()) {
509 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
510 /* start grabbing all events */
511 sdl_grab_start(scon);
513 } else {
514 if (ev->type == SDL_MOUSEBUTTONDOWN) {
515 buttonstate |= SDL_BUTTON(bev->button);
516 } else {
517 buttonstate &= ~SDL_BUTTON(bev->button);
519 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
523 static void handle_mousewheel(SDL_Event *ev)
525 struct sdl2_console *scon = get_scon_from_window(ev->wheel.windowID);
526 SDL_MouseWheelEvent *wev = &ev->wheel;
527 InputButton btn;
529 if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
530 return;
533 if (wev->y > 0) {
534 btn = INPUT_BUTTON_WHEEL_UP;
535 } else if (wev->y < 0) {
536 btn = INPUT_BUTTON_WHEEL_DOWN;
537 } else {
538 return;
541 qemu_input_queue_btn(scon->dcl.con, btn, true);
542 qemu_input_event_sync();
543 qemu_input_queue_btn(scon->dcl.con, btn, false);
544 qemu_input_event_sync();
547 static void handle_windowevent(SDL_Event *ev)
549 struct sdl2_console *scon = get_scon_from_window(ev->window.windowID);
550 bool allow_close = true;
552 if (!scon) {
553 return;
556 switch (ev->window.event) {
557 case SDL_WINDOWEVENT_RESIZED:
559 QemuUIInfo info;
560 memset(&info, 0, sizeof(info));
561 info.width = ev->window.data1;
562 info.height = ev->window.data2;
563 dpy_set_ui_info(scon->dcl.con, &info);
565 sdl2_redraw(scon);
566 break;
567 case SDL_WINDOWEVENT_EXPOSED:
568 sdl2_redraw(scon);
569 break;
570 case SDL_WINDOWEVENT_FOCUS_GAINED:
571 win32_kbd_set_grab(gui_grab);
572 if (qemu_console_is_graphic(scon->dcl.con)) {
573 win32_kbd_set_window(sdl2_win32_get_hwnd(scon));
575 /* fall through */
576 case SDL_WINDOWEVENT_ENTER:
577 if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
578 absolute_mouse_grab(scon);
580 /* If a new console window opened using a hotkey receives the
581 * focus, SDL sends another KEYDOWN event to the new window,
582 * closing the console window immediately after.
584 * Work around this by ignoring further hotkey events until a
585 * key is released.
587 scon->ignore_hotkeys = get_mod_state();
588 break;
589 case SDL_WINDOWEVENT_FOCUS_LOST:
590 if (qemu_console_is_graphic(scon->dcl.con)) {
591 win32_kbd_set_window(NULL);
593 if (gui_grab && !gui_fullscreen) {
594 sdl_grab_end(scon);
596 break;
597 case SDL_WINDOWEVENT_RESTORED:
598 update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT);
599 break;
600 case SDL_WINDOWEVENT_MINIMIZED:
601 update_displaychangelistener(&scon->dcl, 500);
602 break;
603 case SDL_WINDOWEVENT_CLOSE:
604 if (qemu_console_is_graphic(scon->dcl.con)) {
605 if (scon->opts->has_window_close && !scon->opts->window_close) {
606 allow_close = false;
608 if (allow_close) {
609 no_shutdown = 0;
610 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
612 } else {
613 SDL_HideWindow(scon->real_window);
614 scon->hidden = true;
616 break;
617 case SDL_WINDOWEVENT_SHOWN:
618 scon->hidden = false;
619 break;
620 case SDL_WINDOWEVENT_HIDDEN:
621 scon->hidden = true;
622 break;
626 void sdl2_poll_events(struct sdl2_console *scon)
628 SDL_Event ev1, *ev = &ev1;
629 bool allow_close = true;
630 int idle = 1;
632 if (scon->last_vm_running != runstate_is_running()) {
633 scon->last_vm_running = runstate_is_running();
634 sdl_update_caption(scon);
637 while (SDL_PollEvent(ev)) {
638 switch (ev->type) {
639 case SDL_KEYDOWN:
640 idle = 0;
641 handle_keydown(ev);
642 break;
643 case SDL_KEYUP:
644 idle = 0;
645 handle_keyup(ev);
646 break;
647 case SDL_TEXTINPUT:
648 idle = 0;
649 handle_textinput(ev);
650 break;
651 case SDL_QUIT:
652 if (scon->opts->has_window_close && !scon->opts->window_close) {
653 allow_close = false;
655 if (allow_close) {
656 no_shutdown = 0;
657 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
659 break;
660 case SDL_MOUSEMOTION:
661 idle = 0;
662 handle_mousemotion(ev);
663 break;
664 case SDL_MOUSEBUTTONDOWN:
665 case SDL_MOUSEBUTTONUP:
666 idle = 0;
667 handle_mousebutton(ev);
668 break;
669 case SDL_MOUSEWHEEL:
670 idle = 0;
671 handle_mousewheel(ev);
672 break;
673 case SDL_WINDOWEVENT:
674 handle_windowevent(ev);
675 break;
676 default:
677 break;
681 if (idle) {
682 if (scon->idle_counter < SDL2_MAX_IDLE_COUNT) {
683 scon->idle_counter++;
684 if (scon->idle_counter >= SDL2_MAX_IDLE_COUNT) {
685 scon->dcl.update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
688 } else {
689 scon->idle_counter = 0;
690 scon->dcl.update_interval = SDL2_REFRESH_INTERVAL_BUSY;
694 static void sdl_mouse_warp(DisplayChangeListener *dcl,
695 int x, int y, int on)
697 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
699 if (!qemu_console_is_graphic(scon->dcl.con)) {
700 return;
703 if (on) {
704 if (!guest_cursor) {
705 sdl_show_cursor(scon);
707 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
708 SDL_SetCursor(guest_sprite);
709 if (!qemu_input_is_absolute() && !absolute_enabled) {
710 SDL_WarpMouseInWindow(scon->real_window, x, y);
713 } else if (gui_grab) {
714 sdl_hide_cursor(scon);
716 guest_cursor = on;
717 guest_x = x, guest_y = y;
720 static void sdl_mouse_define(DisplayChangeListener *dcl,
721 QEMUCursor *c)
724 if (guest_sprite) {
725 SDL_FreeCursor(guest_sprite);
728 if (guest_sprite_surface) {
729 SDL_FreeSurface(guest_sprite_surface);
732 guest_sprite_surface =
733 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
734 0xff0000, 0x00ff00, 0xff, 0xff000000);
736 if (!guest_sprite_surface) {
737 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
738 return;
740 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
741 c->hot_x, c->hot_y);
742 if (!guest_sprite) {
743 fprintf(stderr, "Failed to make color cursor from %p\n", c);
744 return;
746 if (guest_cursor &&
747 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
748 SDL_SetCursor(guest_sprite);
752 static void sdl_cleanup(void)
754 if (guest_sprite) {
755 SDL_FreeCursor(guest_sprite);
757 SDL_QuitSubSystem(SDL_INIT_VIDEO);
760 static const DisplayChangeListenerOps dcl_2d_ops = {
761 .dpy_name = "sdl2-2d",
762 .dpy_gfx_update = sdl2_2d_update,
763 .dpy_gfx_switch = sdl2_2d_switch,
764 .dpy_gfx_check_format = sdl2_2d_check_format,
765 .dpy_refresh = sdl2_2d_refresh,
766 .dpy_mouse_set = sdl_mouse_warp,
767 .dpy_cursor_define = sdl_mouse_define,
770 #ifdef CONFIG_OPENGL
771 static const DisplayChangeListenerOps dcl_gl_ops = {
772 .dpy_name = "sdl2-gl",
773 .dpy_gfx_update = sdl2_gl_update,
774 .dpy_gfx_switch = sdl2_gl_switch,
775 .dpy_gfx_check_format = console_gl_check_format,
776 .dpy_refresh = sdl2_gl_refresh,
777 .dpy_mouse_set = sdl_mouse_warp,
778 .dpy_cursor_define = sdl_mouse_define,
780 .dpy_gl_ctx_create = sdl2_gl_create_context,
781 .dpy_gl_ctx_destroy = sdl2_gl_destroy_context,
782 .dpy_gl_ctx_make_current = sdl2_gl_make_context_current,
783 .dpy_gl_ctx_get_current = sdl2_gl_get_current_context,
784 .dpy_gl_scanout_disable = sdl2_gl_scanout_disable,
785 .dpy_gl_scanout_texture = sdl2_gl_scanout_texture,
786 .dpy_gl_update = sdl2_gl_scanout_flush,
788 #endif
790 static void sdl2_display_early_init(DisplayOptions *o)
792 assert(o->type == DISPLAY_TYPE_SDL);
793 if (o->has_gl && o->gl) {
794 #ifdef CONFIG_OPENGL
795 display_opengl = 1;
796 #endif
800 static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
802 uint8_t data = 0;
803 int i;
804 SDL_SysWMinfo info;
805 SDL_Surface *icon = NULL;
806 char *dir;
808 assert(o->type == DISPLAY_TYPE_SDL);
810 #ifdef __linux__
811 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
812 * accessible $DISPLAY to open X11 window. This is often the case
813 * when qemu is run using sudo. But in this case, and when actually
814 * run in X11 environment, SDL fights with X11 for the video card,
815 * making current display unavailable, often until reboot.
816 * So make x11 the default SDL video driver if this variable is unset.
817 * This is a bit hackish but saves us from bigger problem.
818 * Maybe it's a good idea to fix this in SDL instead.
820 g_setenv("SDL_VIDEODRIVER", "x11", 0);
821 #endif
823 if (SDL_Init(SDL_INIT_VIDEO)) {
824 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
825 SDL_GetError());
826 exit(1);
828 #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
829 SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
830 #endif
831 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
832 memset(&info, 0, sizeof(info));
833 SDL_VERSION(&info.version);
835 gui_fullscreen = o->has_full_screen && o->full_screen;
837 for (i = 0;; i++) {
838 QemuConsole *con = qemu_console_lookup_by_index(i);
839 if (!con) {
840 break;
843 sdl2_num_outputs = i;
844 if (sdl2_num_outputs == 0) {
845 return;
847 sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
848 for (i = 0; i < sdl2_num_outputs; i++) {
849 QemuConsole *con = qemu_console_lookup_by_index(i);
850 assert(con != NULL);
851 if (!qemu_console_is_graphic(con) &&
852 qemu_console_get_index(con) != 0) {
853 sdl2_console[i].hidden = true;
855 sdl2_console[i].idx = i;
856 sdl2_console[i].opts = o;
857 #ifdef CONFIG_OPENGL
858 sdl2_console[i].opengl = display_opengl;
859 sdl2_console[i].dcl.ops = display_opengl ? &dcl_gl_ops : &dcl_2d_ops;
860 #else
861 sdl2_console[i].opengl = 0;
862 sdl2_console[i].dcl.ops = &dcl_2d_ops;
863 #endif
864 sdl2_console[i].dcl.con = con;
865 sdl2_console[i].kbd = qkbd_state_init(con);
866 register_displaychangelistener(&sdl2_console[i].dcl);
868 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
869 if (SDL_GetWindowWMInfo(sdl2_console[i].real_window, &info)) {
870 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
871 qemu_console_set_window_id(con, (uintptr_t)info.info.win.window);
872 #elif defined(SDL_VIDEO_DRIVER_X11)
873 qemu_console_set_window_id(con, info.info.x11.window);
874 #endif
876 #endif
879 #ifdef CONFIG_SDL_IMAGE
880 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/128x128/apps/qemu.png");
881 icon = IMG_Load(dir);
882 #else
883 /* Load a 32x32x4 image. White pixels are transparent. */
884 dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/32x32/apps/qemu.bmp");
885 icon = SDL_LoadBMP(dir);
886 if (icon) {
887 uint32_t colorkey = SDL_MapRGB(icon->format, 255, 255, 255);
888 SDL_SetColorKey(icon, SDL_TRUE, colorkey);
890 #endif
891 g_free(dir);
892 if (icon) {
893 SDL_SetWindowIcon(sdl2_console[0].real_window, icon);
896 mouse_mode_notifier.notify = sdl_mouse_mode_change;
897 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
899 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
900 sdl_cursor_normal = SDL_GetCursor();
902 if (gui_fullscreen) {
903 sdl_grab_start(&sdl2_console[0]);
906 atexit(sdl_cleanup);
909 static QemuDisplay qemu_display_sdl2 = {
910 .type = DISPLAY_TYPE_SDL,
911 .early_init = sdl2_display_early_init,
912 .init = sdl2_display_init,
915 static void register_sdl1(void)
917 qemu_display_register(&qemu_display_sdl2);
920 type_init(register_sdl1);