sdl2: turn on keyboard grabs
[qemu.git] / ui / sdl2.c
blob9b66017e719bcd2add09f0157c7db47a6389f15f
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 /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
27 #undef WIN32_LEAN_AND_MEAN
29 #include <SDL.h>
30 #include <SDL_syswm.h>
32 #include "qemu-common.h"
33 #include "ui/console.h"
34 #include "ui/input.h"
35 #include "ui/sdl2.h"
36 #include "sysemu/sysemu.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 gui_saved_scaling;
45 static int gui_saved_width;
46 static int gui_saved_height;
47 static int gui_saved_grab;
48 static int gui_fullscreen;
49 static int gui_noframe;
50 static int gui_key_modifier_pressed;
51 static int gui_keysym;
52 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
53 static SDL_Cursor *sdl_cursor_normal;
54 static SDL_Cursor *sdl_cursor_hidden;
55 static int absolute_enabled;
56 static int guest_cursor;
57 static int guest_x, guest_y;
58 static SDL_Cursor *guest_sprite;
59 static int scaling_active;
60 static Notifier mouse_mode_notifier;
62 static void sdl_update_caption(struct sdl2_console *scon);
64 static struct sdl2_console *get_scon_from_window(uint32_t window_id)
66 int i;
67 for (i = 0; i < sdl2_num_outputs; i++) {
68 if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
69 return &sdl2_console[i];
72 return NULL;
75 static void sdl_update(DisplayChangeListener *dcl,
76 int x, int y, int w, int h)
78 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
79 SDL_Rect rect;
80 DisplaySurface *surf = qemu_console_surface(dcl->con);
82 if (!surf) {
83 return;
85 if (!scon->texture) {
86 return;
89 rect.x = x;
90 rect.y = y;
91 rect.w = w;
92 rect.h = h;
94 SDL_UpdateTexture(scon->texture, NULL, surface_data(surf),
95 surface_stride(surf));
96 SDL_RenderCopy(scon->real_renderer, scon->texture, &rect, &rect);
97 SDL_RenderPresent(scon->real_renderer);
100 static void do_sdl_resize(struct sdl2_console *scon, int width, int height,
101 int bpp)
103 int flags;
105 if (scon->real_window && scon->real_renderer) {
106 if (width && height) {
107 SDL_RenderSetLogicalSize(scon->real_renderer, width, height);
108 SDL_SetWindowSize(scon->real_window, width, height);
109 } else {
110 SDL_DestroyRenderer(scon->real_renderer);
111 SDL_DestroyWindow(scon->real_window);
112 scon->real_renderer = NULL;
113 scon->real_window = NULL;
115 } else {
116 if (!width || !height) {
117 return;
119 flags = 0;
120 if (gui_fullscreen) {
121 flags |= SDL_WINDOW_FULLSCREEN;
122 } else {
123 flags |= SDL_WINDOW_RESIZABLE;
125 if (scon->hidden) {
126 flags |= SDL_WINDOW_HIDDEN;
129 scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
130 SDL_WINDOWPOS_UNDEFINED,
131 width, height, flags);
132 scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
133 sdl_update_caption(scon);
137 static void sdl_switch(DisplayChangeListener *dcl,
138 DisplaySurface *new_surface)
140 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
141 int format = 0;
142 int idx = scon->idx;
143 DisplaySurface *old_surface = scon->surface;
145 /* temporary hack: allows to call sdl_switch to handle scaling changes */
146 if (new_surface) {
147 scon->surface = new_surface;
150 if (!new_surface && idx > 0) {
151 scon->surface = NULL;
154 if (new_surface == NULL) {
155 do_sdl_resize(scon, 0, 0, 0);
156 } else {
157 do_sdl_resize(scon, surface_width(scon->surface),
158 surface_height(scon->surface), 0);
161 if (old_surface && scon->texture) {
162 SDL_DestroyTexture(scon->texture);
163 scon->texture = NULL;
166 if (new_surface) {
167 if (!scon->texture) {
168 if (surface_bits_per_pixel(scon->surface) == 16) {
169 format = SDL_PIXELFORMAT_RGB565;
170 } else if (surface_bits_per_pixel(scon->surface) == 32) {
171 format = SDL_PIXELFORMAT_ARGB8888;
174 scon->texture = SDL_CreateTexture(scon->real_renderer, format,
175 SDL_TEXTUREACCESS_STREAMING,
176 surface_width(new_surface),
177 surface_height(new_surface));
182 static void sdl_update_caption(struct sdl2_console *scon)
184 char win_title[1024];
185 char icon_title[1024];
186 const char *status = "";
188 if (!runstate_is_running()) {
189 status = " [Stopped]";
190 } else if (gui_grab) {
191 if (alt_grab) {
192 status = " - Press Ctrl-Alt-Shift to exit grab";
193 } else if (ctrl_grab) {
194 status = " - Press Right-Ctrl to exit grab";
195 } else {
196 status = " - Press Ctrl-Alt to exit grab";
200 if (qemu_name) {
201 snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
202 scon->idx, status);
203 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
204 } else {
205 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
206 snprintf(icon_title, sizeof(icon_title), "QEMU");
209 if (scon->real_window) {
210 SDL_SetWindowTitle(scon->real_window, win_title);
214 static void sdl_hide_cursor(void)
216 if (!cursor_hide) {
217 return;
220 if (qemu_input_is_absolute()) {
221 SDL_ShowCursor(1);
222 SDL_SetCursor(sdl_cursor_hidden);
223 } else {
224 SDL_SetRelativeMouseMode(SDL_TRUE);
228 static void sdl_show_cursor(void)
230 if (!cursor_hide) {
231 return;
234 if (!qemu_input_is_absolute()) {
235 SDL_SetRelativeMouseMode(SDL_FALSE);
236 SDL_ShowCursor(1);
237 if (guest_cursor &&
238 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
239 SDL_SetCursor(guest_sprite);
240 } else {
241 SDL_SetCursor(sdl_cursor_normal);
246 static void sdl_grab_start(struct sdl2_console *scon)
248 QemuConsole *con = scon ? scon->dcl.con : NULL;
250 if (!con || !qemu_console_is_graphic(con)) {
251 return;
254 * If the application is not active, do not try to enter grab state. This
255 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
256 * application (SDL bug).
258 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
259 return;
261 if (guest_cursor) {
262 SDL_SetCursor(guest_sprite);
263 if (!qemu_input_is_absolute() && !absolute_enabled) {
264 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
266 } else {
267 sdl_hide_cursor();
269 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
270 gui_grab = 1;
271 sdl_update_caption(scon);
274 static void sdl_grab_end(struct sdl2_console *scon)
276 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
277 gui_grab = 0;
278 sdl_show_cursor();
279 sdl_update_caption(scon);
282 static void absolute_mouse_grab(struct sdl2_console *scon)
284 int mouse_x, mouse_y;
285 int scr_w, scr_h;
286 SDL_GetMouseState(&mouse_x, &mouse_y);
287 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
288 if (mouse_x > 0 && mouse_x < scr_w - 1 &&
289 mouse_y > 0 && mouse_y < scr_h - 1) {
290 sdl_grab_start(scon);
294 static void sdl_mouse_mode_change(Notifier *notify, void *data)
296 if (qemu_input_is_absolute()) {
297 if (!absolute_enabled) {
298 absolute_enabled = 1;
299 absolute_mouse_grab(&sdl2_console[0]);
301 } else if (absolute_enabled) {
302 if (!gui_fullscreen) {
303 sdl_grab_end(&sdl2_console[0]);
305 absolute_enabled = 0;
309 static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
310 int x, int y, int state)
312 static uint32_t bmap[INPUT_BUTTON_MAX] = {
313 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
314 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
315 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
317 static uint32_t prev_state;
319 if (prev_state != state) {
320 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
321 prev_state = state;
324 if (qemu_input_is_absolute()) {
325 int scr_w, scr_h;
326 int max_w = 0, max_h = 0;
327 int off_x = 0, off_y = 0;
328 int cur_off_x = 0, cur_off_y = 0;
329 int i;
331 for (i = 0; i < sdl2_num_outputs; i++) {
332 struct sdl2_console *thiscon = &sdl2_console[i];
333 if (thiscon->real_window && thiscon->surface) {
334 SDL_GetWindowSize(thiscon->real_window, &scr_w, &scr_h);
335 cur_off_x = thiscon->x;
336 cur_off_y = thiscon->y;
337 if (scr_w + cur_off_x > max_w) {
338 max_w = scr_w + cur_off_x;
340 if (scr_h + cur_off_y > max_h) {
341 max_h = scr_h + cur_off_y;
343 if (i == scon->idx) {
344 off_x = cur_off_x;
345 off_y = cur_off_y;
349 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X, off_x + x, max_w);
350 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y, off_y + y, max_h);
351 } else {
352 if (guest_cursor) {
353 x -= guest_x;
354 y -= guest_y;
355 guest_x += x;
356 guest_y += y;
357 dx = x;
358 dy = y;
360 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
361 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
363 qemu_input_event_sync();
366 static void sdl_scale(struct sdl2_console *scon, int width, int height)
368 int bpp = 0;
369 do_sdl_resize(scon, width, height, bpp);
370 scaling_active = 1;
373 static void toggle_full_screen(struct sdl2_console *scon)
375 int width = surface_width(scon->surface);
376 int height = surface_height(scon->surface);
377 int bpp = surface_bits_per_pixel(scon->surface);
379 gui_fullscreen = !gui_fullscreen;
380 if (gui_fullscreen) {
381 SDL_GetWindowSize(scon->real_window,
382 &gui_saved_width, &gui_saved_height);
383 gui_saved_scaling = scaling_active;
385 do_sdl_resize(scon, width, height, bpp);
386 scaling_active = 0;
388 gui_saved_grab = gui_grab;
389 sdl_grab_start(scon);
390 } else {
391 if (gui_saved_scaling) {
392 sdl_scale(scon, gui_saved_width, gui_saved_height);
393 } else {
394 do_sdl_resize(scon, width, height, 0);
396 if (!gui_saved_grab) {
397 sdl_grab_end(scon);
400 graphic_hw_invalidate(scon->dcl.con);
401 graphic_hw_update(scon->dcl.con);
404 static void handle_keydown(SDL_Event *ev)
406 int mod_state, win;
407 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
409 if (alt_grab) {
410 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
411 (gui_grab_code | KMOD_LSHIFT);
412 } else if (ctrl_grab) {
413 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
414 } else {
415 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
417 gui_key_modifier_pressed = mod_state;
419 if (gui_key_modifier_pressed) {
420 switch (ev->key.keysym.scancode) {
421 case SDL_SCANCODE_2:
422 case SDL_SCANCODE_3:
423 case SDL_SCANCODE_4:
424 case SDL_SCANCODE_5:
425 case SDL_SCANCODE_6:
426 case SDL_SCANCODE_7:
427 case SDL_SCANCODE_8:
428 case SDL_SCANCODE_9:
429 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
430 if (win < sdl2_num_outputs) {
431 sdl2_console[win].hidden = !sdl2_console[win].hidden;
432 if (sdl2_console[win].real_window) {
433 if (sdl2_console[win].hidden) {
434 SDL_HideWindow(sdl2_console[win].real_window);
435 } else {
436 SDL_ShowWindow(sdl2_console[win].real_window);
439 gui_keysym = 1;
441 break;
442 case SDL_SCANCODE_F:
443 toggle_full_screen(scon);
444 gui_keysym = 1;
445 break;
446 case SDL_SCANCODE_U:
447 if (scaling_active) {
448 scaling_active = 0;
449 sdl_switch(&scon->dcl, NULL);
450 graphic_hw_invalidate(scon->dcl.con);
451 graphic_hw_update(scon->dcl.con);
453 gui_keysym = 1;
454 break;
455 case SDL_SCANCODE_KP_PLUS:
456 case SDL_SCANCODE_KP_MINUS:
457 if (!gui_fullscreen) {
458 int scr_w, scr_h;
459 int width, height;
460 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
462 width = MAX(scr_w + (ev->key.keysym.scancode ==
463 SDL_SCANCODE_KP_PLUS ? 50 : -50),
464 160);
465 height = (surface_height(scon->surface) * width) /
466 surface_width(scon->surface);
468 sdl_scale(scon, width, height);
469 graphic_hw_invalidate(NULL);
470 graphic_hw_update(NULL);
471 gui_keysym = 1;
473 default:
474 break;
477 if (!gui_keysym) {
478 sdl2_process_key(scon, &ev->key);
482 static void handle_keyup(SDL_Event *ev)
484 int mod_state;
485 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
487 if (!alt_grab) {
488 mod_state = (ev->key.keysym.mod & gui_grab_code);
489 } else {
490 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
492 if (!mod_state && gui_key_modifier_pressed) {
493 gui_key_modifier_pressed = 0;
494 if (gui_keysym == 0) {
495 /* exit/enter grab if pressing Ctrl-Alt */
496 if (!gui_grab) {
497 sdl_grab_start(scon);
498 } else if (!gui_fullscreen) {
499 sdl_grab_end(scon);
501 /* SDL does not send back all the modifiers key, so we must
502 * correct it. */
503 sdl2_reset_keys(scon);
504 return;
506 gui_keysym = 0;
508 if (!gui_keysym) {
509 sdl2_process_key(scon, &ev->key);
513 static void handle_textinput(SDL_Event *ev)
515 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
516 QemuConsole *con = scon ? scon->dcl.con : NULL;
518 if (qemu_console_is_graphic(con)) {
519 return;
521 kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
524 static void handle_mousemotion(SDL_Event *ev)
526 int max_x, max_y;
527 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
529 if (qemu_input_is_absolute() || absolute_enabled) {
530 int scr_w, scr_h;
531 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
532 max_x = scr_w - 1;
533 max_y = scr_h - 1;
534 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
535 ev->motion.x == max_x || ev->motion.y == max_y)) {
536 sdl_grab_end(scon);
538 if (!gui_grab &&
539 (ev->motion.x > 0 && ev->motion.x < max_x &&
540 ev->motion.y > 0 && ev->motion.y < max_y)) {
541 sdl_grab_start(scon);
544 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
545 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
546 ev->motion.x, ev->motion.y, ev->motion.state);
550 static void handle_mousebutton(SDL_Event *ev)
552 int buttonstate = SDL_GetMouseState(NULL, NULL);
553 SDL_MouseButtonEvent *bev;
554 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
556 bev = &ev->button;
557 if (!gui_grab && !qemu_input_is_absolute()) {
558 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
559 /* start grabbing all events */
560 sdl_grab_start(scon);
562 } else {
563 if (ev->type == SDL_MOUSEBUTTONDOWN) {
564 buttonstate |= SDL_BUTTON(bev->button);
565 } else {
566 buttonstate &= ~SDL_BUTTON(bev->button);
568 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
572 static void handle_mousewheel(SDL_Event *ev)
574 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
575 SDL_MouseWheelEvent *wev = &ev->wheel;
576 InputButton btn;
578 if (wev->y > 0) {
579 btn = INPUT_BUTTON_WHEEL_UP;
580 } else if (wev->y < 0) {
581 btn = INPUT_BUTTON_WHEEL_DOWN;
582 } else {
583 return;
586 qemu_input_queue_btn(scon->dcl.con, btn, true);
587 qemu_input_event_sync();
588 qemu_input_queue_btn(scon->dcl.con, btn, false);
589 qemu_input_event_sync();
592 static void handle_windowevent(DisplayChangeListener *dcl, SDL_Event *ev)
594 int w, h;
595 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
597 switch (ev->window.event) {
598 case SDL_WINDOWEVENT_RESIZED:
599 sdl_scale(scon, ev->window.data1, ev->window.data2);
601 QemuUIInfo info;
602 memset(&info, 0, sizeof(info));
603 info.width = ev->window.data1;
604 info.height = ev->window.data2;
605 dpy_set_ui_info(scon->dcl.con, &info);
607 graphic_hw_invalidate(scon->dcl.con);
608 graphic_hw_update(scon->dcl.con);
609 break;
610 case SDL_WINDOWEVENT_EXPOSED:
611 SDL_GetWindowSize(SDL_GetWindowFromID(ev->window.windowID), &w, &h);
612 sdl_update(dcl, 0, 0, w, h);
613 break;
614 case SDL_WINDOWEVENT_FOCUS_GAINED:
615 case SDL_WINDOWEVENT_ENTER:
616 if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
617 absolute_mouse_grab(scon);
619 break;
620 case SDL_WINDOWEVENT_FOCUS_LOST:
621 if (gui_grab && !gui_fullscreen) {
622 sdl_grab_end(scon);
624 break;
625 case SDL_WINDOWEVENT_RESTORED:
626 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
627 break;
628 case SDL_WINDOWEVENT_MINIMIZED:
629 update_displaychangelistener(dcl, 500);
630 break;
631 case SDL_WINDOWEVENT_CLOSE:
632 if (!no_quit) {
633 no_shutdown = 0;
634 qemu_system_shutdown_request();
636 break;
640 static void sdl_refresh(DisplayChangeListener *dcl)
642 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
643 SDL_Event ev1, *ev = &ev1;
645 if (scon->last_vm_running != runstate_is_running()) {
646 scon->last_vm_running = runstate_is_running();
647 sdl_update_caption(scon);
650 graphic_hw_update(dcl->con);
652 while (SDL_PollEvent(ev)) {
653 switch (ev->type) {
654 case SDL_KEYDOWN:
655 handle_keydown(ev);
656 break;
657 case SDL_KEYUP:
658 handle_keyup(ev);
659 break;
660 case SDL_TEXTINPUT:
661 handle_textinput(ev);
662 break;
663 case SDL_QUIT:
664 if (!no_quit) {
665 no_shutdown = 0;
666 qemu_system_shutdown_request();
668 break;
669 case SDL_MOUSEMOTION:
670 handle_mousemotion(ev);
671 break;
672 case SDL_MOUSEBUTTONDOWN:
673 case SDL_MOUSEBUTTONUP:
674 handle_mousebutton(ev);
675 break;
676 case SDL_MOUSEWHEEL:
677 handle_mousewheel(ev);
678 break;
679 case SDL_WINDOWEVENT:
680 handle_windowevent(dcl, ev);
681 break;
682 default:
683 break;
688 static void sdl_mouse_warp(DisplayChangeListener *dcl,
689 int x, int y, int on)
691 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
692 if (on) {
693 if (!guest_cursor) {
694 sdl_show_cursor();
696 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
697 SDL_SetCursor(guest_sprite);
698 if (!qemu_input_is_absolute() && !absolute_enabled) {
699 SDL_WarpMouseInWindow(scon->real_window, x, y);
702 } else if (gui_grab) {
703 sdl_hide_cursor();
705 guest_cursor = on;
706 guest_x = x, guest_y = y;
709 static void sdl_mouse_define(DisplayChangeListener *dcl,
710 QEMUCursor *c)
713 if (guest_sprite) {
714 SDL_FreeCursor(guest_sprite);
717 if (guest_sprite_surface) {
718 SDL_FreeSurface(guest_sprite_surface);
721 guest_sprite_surface =
722 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
723 0xff0000, 0x00ff00, 0xff, 0xff000000);
725 if (!guest_sprite_surface) {
726 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
727 return;
729 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
730 c->hot_x, c->hot_y);
731 if (!guest_sprite) {
732 fprintf(stderr, "Failed to make color cursor from %p\n", c);
733 return;
735 if (guest_cursor &&
736 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
737 SDL_SetCursor(guest_sprite);
741 static void sdl_cleanup(void)
743 if (guest_sprite) {
744 SDL_FreeCursor(guest_sprite);
746 SDL_QuitSubSystem(SDL_INIT_VIDEO);
749 static const DisplayChangeListenerOps dcl_ops = {
750 .dpy_name = "sdl",
751 .dpy_gfx_update = sdl_update,
752 .dpy_gfx_switch = sdl_switch,
753 .dpy_refresh = sdl_refresh,
754 .dpy_mouse_set = sdl_mouse_warp,
755 .dpy_cursor_define = sdl_mouse_define,
758 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
760 int flags;
761 uint8_t data = 0;
762 char *filename;
763 int i;
765 if (no_frame) {
766 gui_noframe = 1;
769 #ifdef __linux__
770 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
771 * accessible $DISPLAY to open X11 window. This is often the case
772 * when qemu is run using sudo. But in this case, and when actually
773 * run in X11 environment, SDL fights with X11 for the video card,
774 * making current display unavailable, often until reboot.
775 * So make x11 the default SDL video driver if this variable is unset.
776 * This is a bit hackish but saves us from bigger problem.
777 * Maybe it's a good idea to fix this in SDL instead.
779 setenv("SDL_VIDEODRIVER", "x11", 0);
780 #endif
782 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
783 if (SDL_Init(flags)) {
784 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
785 SDL_GetError());
786 exit(1);
788 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
790 for (i = 0;; i++) {
791 QemuConsole *con = qemu_console_lookup_by_index(i);
792 if (!con) {
793 break;
796 sdl2_num_outputs = i;
797 sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
798 for (i = 0; i < sdl2_num_outputs; i++) {
799 QemuConsole *con = qemu_console_lookup_by_index(i);
800 if (!qemu_console_is_graphic(con)) {
801 sdl2_console[i].hidden = true;
803 sdl2_console[i].dcl.ops = &dcl_ops;
804 sdl2_console[i].dcl.con = con;
805 register_displaychangelistener(&sdl2_console[i].dcl);
806 sdl2_console[i].idx = i;
809 /* Load a 32x32x4 image. White pixels are transparent. */
810 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
811 if (filename) {
812 SDL_Surface *image = SDL_LoadBMP(filename);
813 if (image) {
814 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
815 SDL_SetColorKey(image, SDL_TRUE, colorkey);
816 SDL_SetWindowIcon(sdl2_console[0].real_window, image);
818 g_free(filename);
821 if (full_screen) {
822 gui_fullscreen = 1;
823 sdl_grab_start(0);
826 mouse_mode_notifier.notify = sdl_mouse_mode_change;
827 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
829 gui_grab = 0;
831 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
832 sdl_cursor_normal = SDL_GetCursor();
834 atexit(sdl_cleanup);