Add -incoming help text
[qemu/cris-port.git] / ui / sdl2.c
blob2d601798ea2751f9ddbffb7b0118060e546bfd95
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-common.h"
27 #include "ui/console.h"
28 #include "ui/input.h"
29 #include "ui/sdl2.h"
30 #include "sysemu/sysemu.h"
32 static int sdl2_num_outputs;
33 static struct sdl2_console *sdl2_console;
35 static SDL_Surface *guest_sprite_surface;
36 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
38 static int gui_saved_grab;
39 static int gui_fullscreen;
40 static int gui_noframe;
41 static int gui_key_modifier_pressed;
42 static int gui_keysym;
43 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
44 static SDL_Cursor *sdl_cursor_normal;
45 static SDL_Cursor *sdl_cursor_hidden;
46 static int absolute_enabled;
47 static int guest_cursor;
48 static int guest_x, guest_y;
49 static SDL_Cursor *guest_sprite;
50 static Notifier mouse_mode_notifier;
52 static void sdl_update_caption(struct sdl2_console *scon);
54 static struct sdl2_console *get_scon_from_window(uint32_t window_id)
56 int i;
57 for (i = 0; i < sdl2_num_outputs; i++) {
58 if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
59 return &sdl2_console[i];
62 return NULL;
65 void sdl2_window_create(struct sdl2_console *scon)
67 int flags = 0;
69 if (!scon->surface) {
70 return;
72 assert(!scon->real_window);
74 if (gui_fullscreen) {
75 flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
76 } else {
77 flags |= SDL_WINDOW_RESIZABLE;
79 if (scon->hidden) {
80 flags |= SDL_WINDOW_HIDDEN;
83 scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
84 SDL_WINDOWPOS_UNDEFINED,
85 surface_width(scon->surface),
86 surface_height(scon->surface),
87 flags);
88 scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
89 if (scon->opengl) {
90 scon->winctx = SDL_GL_GetCurrentContext();
92 sdl_update_caption(scon);
95 void sdl2_window_destroy(struct sdl2_console *scon)
97 if (!scon->real_window) {
98 return;
101 SDL_DestroyRenderer(scon->real_renderer);
102 scon->real_renderer = NULL;
103 SDL_DestroyWindow(scon->real_window);
104 scon->real_window = NULL;
107 void sdl2_window_resize(struct sdl2_console *scon)
109 if (!scon->real_window) {
110 return;
113 SDL_SetWindowSize(scon->real_window,
114 surface_width(scon->surface),
115 surface_height(scon->surface));
118 static void sdl2_redraw(struct sdl2_console *scon)
120 if (scon->opengl) {
121 #ifdef CONFIG_OPENGL
122 sdl2_gl_redraw(scon);
123 #endif
124 } else {
125 sdl2_2d_redraw(scon);
129 static void sdl_update_caption(struct sdl2_console *scon)
131 char win_title[1024];
132 char icon_title[1024];
133 const char *status = "";
135 if (!runstate_is_running()) {
136 status = " [Stopped]";
137 } else if (gui_grab) {
138 if (alt_grab) {
139 status = " - Press Ctrl-Alt-Shift to exit grab";
140 } else if (ctrl_grab) {
141 status = " - Press Right-Ctrl to exit grab";
142 } else {
143 status = " - Press Ctrl-Alt to exit grab";
147 if (qemu_name) {
148 snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
149 scon->idx, status);
150 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
151 } else {
152 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
153 snprintf(icon_title, sizeof(icon_title), "QEMU");
156 if (scon->real_window) {
157 SDL_SetWindowTitle(scon->real_window, win_title);
161 static void sdl_hide_cursor(void)
163 if (!cursor_hide) {
164 return;
167 if (qemu_input_is_absolute()) {
168 SDL_ShowCursor(1);
169 SDL_SetCursor(sdl_cursor_hidden);
170 } else {
171 SDL_SetRelativeMouseMode(SDL_TRUE);
175 static void sdl_show_cursor(void)
177 if (!cursor_hide) {
178 return;
181 if (!qemu_input_is_absolute()) {
182 SDL_SetRelativeMouseMode(SDL_FALSE);
183 SDL_ShowCursor(1);
184 if (guest_cursor &&
185 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
186 SDL_SetCursor(guest_sprite);
187 } else {
188 SDL_SetCursor(sdl_cursor_normal);
193 static void sdl_grab_start(struct sdl2_console *scon)
195 QemuConsole *con = scon ? scon->dcl.con : NULL;
197 if (!con || !qemu_console_is_graphic(con)) {
198 return;
201 * If the application is not active, do not try to enter grab state. This
202 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
203 * application (SDL bug).
205 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
206 return;
208 if (guest_cursor) {
209 SDL_SetCursor(guest_sprite);
210 if (!qemu_input_is_absolute() && !absolute_enabled) {
211 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
213 } else {
214 sdl_hide_cursor();
216 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
217 gui_grab = 1;
218 sdl_update_caption(scon);
221 static void sdl_grab_end(struct sdl2_console *scon)
223 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
224 gui_grab = 0;
225 sdl_show_cursor();
226 sdl_update_caption(scon);
229 static void absolute_mouse_grab(struct sdl2_console *scon)
231 int mouse_x, mouse_y;
232 int scr_w, scr_h;
233 SDL_GetMouseState(&mouse_x, &mouse_y);
234 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
235 if (mouse_x > 0 && mouse_x < scr_w - 1 &&
236 mouse_y > 0 && mouse_y < scr_h - 1) {
237 sdl_grab_start(scon);
241 static void sdl_mouse_mode_change(Notifier *notify, void *data)
243 if (qemu_input_is_absolute()) {
244 if (!absolute_enabled) {
245 absolute_enabled = 1;
246 absolute_mouse_grab(&sdl2_console[0]);
248 } else if (absolute_enabled) {
249 if (!gui_fullscreen) {
250 sdl_grab_end(&sdl2_console[0]);
252 absolute_enabled = 0;
256 static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
257 int x, int y, int state)
259 static uint32_t bmap[INPUT_BUTTON_MAX] = {
260 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
261 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
262 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
264 static uint32_t prev_state;
266 if (prev_state != state) {
267 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
268 prev_state = state;
271 if (qemu_input_is_absolute()) {
272 int scr_w, scr_h;
273 int max_w = 0, max_h = 0;
274 int off_x = 0, off_y = 0;
275 int cur_off_x = 0, cur_off_y = 0;
276 int i;
278 for (i = 0; i < sdl2_num_outputs; i++) {
279 struct sdl2_console *thiscon = &sdl2_console[i];
280 if (thiscon->real_window && thiscon->surface) {
281 SDL_GetWindowSize(thiscon->real_window, &scr_w, &scr_h);
282 cur_off_x = thiscon->x;
283 cur_off_y = thiscon->y;
284 if (scr_w + cur_off_x > max_w) {
285 max_w = scr_w + cur_off_x;
287 if (scr_h + cur_off_y > max_h) {
288 max_h = scr_h + cur_off_y;
290 if (i == scon->idx) {
291 off_x = cur_off_x;
292 off_y = cur_off_y;
296 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X, off_x + x, max_w);
297 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y, off_y + y, max_h);
298 } else {
299 if (guest_cursor) {
300 x -= guest_x;
301 y -= guest_y;
302 guest_x += x;
303 guest_y += y;
304 dx = x;
305 dy = y;
307 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
308 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
310 qemu_input_event_sync();
313 static void toggle_full_screen(struct sdl2_console *scon)
315 gui_fullscreen = !gui_fullscreen;
316 if (gui_fullscreen) {
317 SDL_SetWindowFullscreen(scon->real_window,
318 SDL_WINDOW_FULLSCREEN_DESKTOP);
319 gui_saved_grab = gui_grab;
320 sdl_grab_start(scon);
321 } else {
322 if (!gui_saved_grab) {
323 sdl_grab_end(scon);
325 SDL_SetWindowFullscreen(scon->real_window, 0);
327 sdl2_redraw(scon);
330 static void handle_keydown(SDL_Event *ev)
332 int mod_state, win;
333 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
335 if (alt_grab) {
336 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
337 (gui_grab_code | KMOD_LSHIFT);
338 } else if (ctrl_grab) {
339 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
340 } else {
341 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
343 gui_key_modifier_pressed = mod_state;
345 if (gui_key_modifier_pressed) {
346 switch (ev->key.keysym.scancode) {
347 case SDL_SCANCODE_2:
348 case SDL_SCANCODE_3:
349 case SDL_SCANCODE_4:
350 case SDL_SCANCODE_5:
351 case SDL_SCANCODE_6:
352 case SDL_SCANCODE_7:
353 case SDL_SCANCODE_8:
354 case SDL_SCANCODE_9:
355 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
356 if (win < sdl2_num_outputs) {
357 sdl2_console[win].hidden = !sdl2_console[win].hidden;
358 if (sdl2_console[win].real_window) {
359 if (sdl2_console[win].hidden) {
360 SDL_HideWindow(sdl2_console[win].real_window);
361 } else {
362 SDL_ShowWindow(sdl2_console[win].real_window);
365 gui_keysym = 1;
367 break;
368 case SDL_SCANCODE_F:
369 toggle_full_screen(scon);
370 gui_keysym = 1;
371 break;
372 case SDL_SCANCODE_U:
373 sdl2_window_destroy(scon);
374 sdl2_window_create(scon);
375 if (!scon->opengl) {
376 /* re-create scon->texture */
377 sdl2_2d_switch(&scon->dcl, scon->surface);
379 gui_keysym = 1;
380 break;
381 #if 0
382 case SDL_SCANCODE_KP_PLUS:
383 case SDL_SCANCODE_KP_MINUS:
384 if (!gui_fullscreen) {
385 int scr_w, scr_h;
386 int width, height;
387 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
389 width = MAX(scr_w + (ev->key.keysym.scancode ==
390 SDL_SCANCODE_KP_PLUS ? 50 : -50),
391 160);
392 height = (surface_height(scon->surface) * width) /
393 surface_width(scon->surface);
394 fprintf(stderr, "%s: scale to %dx%d\n",
395 __func__, width, height);
396 sdl_scale(scon, width, height);
397 sdl2_redraw(scon);
398 gui_keysym = 1;
400 #endif
401 default:
402 break;
405 if (!gui_keysym) {
406 sdl2_process_key(scon, &ev->key);
410 static void handle_keyup(SDL_Event *ev)
412 int mod_state;
413 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
415 if (!alt_grab) {
416 mod_state = (ev->key.keysym.mod & gui_grab_code);
417 } else {
418 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
420 if (!mod_state && gui_key_modifier_pressed) {
421 gui_key_modifier_pressed = 0;
422 if (gui_keysym == 0) {
423 /* exit/enter grab if pressing Ctrl-Alt */
424 if (!gui_grab) {
425 sdl_grab_start(scon);
426 } else if (!gui_fullscreen) {
427 sdl_grab_end(scon);
429 /* SDL does not send back all the modifiers key, so we must
430 * correct it. */
431 sdl2_reset_keys(scon);
432 return;
434 gui_keysym = 0;
436 if (!gui_keysym) {
437 sdl2_process_key(scon, &ev->key);
441 static void handle_textinput(SDL_Event *ev)
443 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
444 QemuConsole *con = scon ? scon->dcl.con : NULL;
446 if (qemu_console_is_graphic(con)) {
447 return;
449 kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
452 static void handle_mousemotion(SDL_Event *ev)
454 int max_x, max_y;
455 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
457 if (qemu_input_is_absolute() || absolute_enabled) {
458 int scr_w, scr_h;
459 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
460 max_x = scr_w - 1;
461 max_y = scr_h - 1;
462 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
463 ev->motion.x == max_x || ev->motion.y == max_y)) {
464 sdl_grab_end(scon);
466 if (!gui_grab &&
467 (ev->motion.x > 0 && ev->motion.x < max_x &&
468 ev->motion.y > 0 && ev->motion.y < max_y)) {
469 sdl_grab_start(scon);
472 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
473 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
474 ev->motion.x, ev->motion.y, ev->motion.state);
478 static void handle_mousebutton(SDL_Event *ev)
480 int buttonstate = SDL_GetMouseState(NULL, NULL);
481 SDL_MouseButtonEvent *bev;
482 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
484 bev = &ev->button;
485 if (!gui_grab && !qemu_input_is_absolute()) {
486 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
487 /* start grabbing all events */
488 sdl_grab_start(scon);
490 } else {
491 if (ev->type == SDL_MOUSEBUTTONDOWN) {
492 buttonstate |= SDL_BUTTON(bev->button);
493 } else {
494 buttonstate &= ~SDL_BUTTON(bev->button);
496 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
500 static void handle_mousewheel(SDL_Event *ev)
502 struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
503 SDL_MouseWheelEvent *wev = &ev->wheel;
504 InputButton btn;
506 if (wev->y > 0) {
507 btn = INPUT_BUTTON_WHEEL_UP;
508 } else if (wev->y < 0) {
509 btn = INPUT_BUTTON_WHEEL_DOWN;
510 } else {
511 return;
514 qemu_input_queue_btn(scon->dcl.con, btn, true);
515 qemu_input_event_sync();
516 qemu_input_queue_btn(scon->dcl.con, btn, false);
517 qemu_input_event_sync();
520 static void handle_windowevent(SDL_Event *ev)
522 struct sdl2_console *scon = get_scon_from_window(ev->window.windowID);
524 switch (ev->window.event) {
525 case SDL_WINDOWEVENT_RESIZED:
527 QemuUIInfo info;
528 memset(&info, 0, sizeof(info));
529 info.width = ev->window.data1;
530 info.height = ev->window.data2;
531 dpy_set_ui_info(scon->dcl.con, &info);
533 sdl2_redraw(scon);
534 break;
535 case SDL_WINDOWEVENT_EXPOSED:
536 sdl2_redraw(scon);
537 break;
538 case SDL_WINDOWEVENT_FOCUS_GAINED:
539 case SDL_WINDOWEVENT_ENTER:
540 if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
541 absolute_mouse_grab(scon);
543 break;
544 case SDL_WINDOWEVENT_FOCUS_LOST:
545 if (gui_grab && !gui_fullscreen) {
546 sdl_grab_end(scon);
548 break;
549 case SDL_WINDOWEVENT_RESTORED:
550 update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT);
551 break;
552 case SDL_WINDOWEVENT_MINIMIZED:
553 update_displaychangelistener(&scon->dcl, 500);
554 break;
555 case SDL_WINDOWEVENT_CLOSE:
556 if (!no_quit) {
557 no_shutdown = 0;
558 qemu_system_shutdown_request();
560 break;
561 case SDL_WINDOWEVENT_SHOWN:
562 if (scon->hidden) {
563 SDL_HideWindow(scon->real_window);
565 break;
566 case SDL_WINDOWEVENT_HIDDEN:
567 if (!scon->hidden) {
568 SDL_ShowWindow(scon->real_window);
570 break;
574 void sdl2_poll_events(struct sdl2_console *scon)
576 SDL_Event ev1, *ev = &ev1;
578 if (scon->last_vm_running != runstate_is_running()) {
579 scon->last_vm_running = runstate_is_running();
580 sdl_update_caption(scon);
583 while (SDL_PollEvent(ev)) {
584 switch (ev->type) {
585 case SDL_KEYDOWN:
586 handle_keydown(ev);
587 break;
588 case SDL_KEYUP:
589 handle_keyup(ev);
590 break;
591 case SDL_TEXTINPUT:
592 handle_textinput(ev);
593 break;
594 case SDL_QUIT:
595 if (!no_quit) {
596 no_shutdown = 0;
597 qemu_system_shutdown_request();
599 break;
600 case SDL_MOUSEMOTION:
601 handle_mousemotion(ev);
602 break;
603 case SDL_MOUSEBUTTONDOWN:
604 case SDL_MOUSEBUTTONUP:
605 handle_mousebutton(ev);
606 break;
607 case SDL_MOUSEWHEEL:
608 handle_mousewheel(ev);
609 break;
610 case SDL_WINDOWEVENT:
611 handle_windowevent(ev);
612 break;
613 default:
614 break;
619 static void sdl_mouse_warp(DisplayChangeListener *dcl,
620 int x, int y, int on)
622 struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
623 if (on) {
624 if (!guest_cursor) {
625 sdl_show_cursor();
627 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
628 SDL_SetCursor(guest_sprite);
629 if (!qemu_input_is_absolute() && !absolute_enabled) {
630 SDL_WarpMouseInWindow(scon->real_window, x, y);
633 } else if (gui_grab) {
634 sdl_hide_cursor();
636 guest_cursor = on;
637 guest_x = x, guest_y = y;
640 static void sdl_mouse_define(DisplayChangeListener *dcl,
641 QEMUCursor *c)
644 if (guest_sprite) {
645 SDL_FreeCursor(guest_sprite);
648 if (guest_sprite_surface) {
649 SDL_FreeSurface(guest_sprite_surface);
652 guest_sprite_surface =
653 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
654 0xff0000, 0x00ff00, 0xff, 0xff000000);
656 if (!guest_sprite_surface) {
657 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
658 return;
660 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
661 c->hot_x, c->hot_y);
662 if (!guest_sprite) {
663 fprintf(stderr, "Failed to make color cursor from %p\n", c);
664 return;
666 if (guest_cursor &&
667 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
668 SDL_SetCursor(guest_sprite);
672 static void sdl_cleanup(void)
674 if (guest_sprite) {
675 SDL_FreeCursor(guest_sprite);
677 SDL_QuitSubSystem(SDL_INIT_VIDEO);
680 static const DisplayChangeListenerOps dcl_2d_ops = {
681 .dpy_name = "sdl2-2d",
682 .dpy_gfx_update = sdl2_2d_update,
683 .dpy_gfx_switch = sdl2_2d_switch,
684 .dpy_gfx_check_format = sdl2_2d_check_format,
685 .dpy_refresh = sdl2_2d_refresh,
686 .dpy_mouse_set = sdl_mouse_warp,
687 .dpy_cursor_define = sdl_mouse_define,
690 #ifdef CONFIG_OPENGL
691 static const DisplayChangeListenerOps dcl_gl_ops = {
692 .dpy_name = "sdl2-gl",
693 .dpy_gfx_update = sdl2_gl_update,
694 .dpy_gfx_switch = sdl2_gl_switch,
695 .dpy_gfx_check_format = console_gl_check_format,
696 .dpy_refresh = sdl2_gl_refresh,
697 .dpy_mouse_set = sdl_mouse_warp,
698 .dpy_cursor_define = sdl_mouse_define,
700 #endif
702 void sdl_display_early_init(int opengl)
704 switch (opengl) {
705 case -1: /* default */
706 case 0: /* off */
707 break;
708 case 1: /* on */
709 #ifdef CONFIG_OPENGL
710 display_opengl = 1;
711 #endif
712 break;
713 default:
714 g_assert_not_reached();
715 break;
719 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
721 int flags;
722 uint8_t data = 0;
723 char *filename;
724 int i;
726 if (no_frame) {
727 gui_noframe = 1;
730 #ifdef __linux__
731 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
732 * accessible $DISPLAY to open X11 window. This is often the case
733 * when qemu is run using sudo. But in this case, and when actually
734 * run in X11 environment, SDL fights with X11 for the video card,
735 * making current display unavailable, often until reboot.
736 * So make x11 the default SDL video driver if this variable is unset.
737 * This is a bit hackish but saves us from bigger problem.
738 * Maybe it's a good idea to fix this in SDL instead.
740 setenv("SDL_VIDEODRIVER", "x11", 0);
741 #endif
743 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
744 if (SDL_Init(flags)) {
745 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
746 SDL_GetError());
747 exit(1);
749 SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
751 for (i = 0;; i++) {
752 QemuConsole *con = qemu_console_lookup_by_index(i);
753 if (!con) {
754 break;
757 sdl2_num_outputs = i;
758 sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
759 for (i = 0; i < sdl2_num_outputs; i++) {
760 QemuConsole *con = qemu_console_lookup_by_index(i);
761 if (!qemu_console_is_graphic(con)) {
762 sdl2_console[i].hidden = true;
764 sdl2_console[i].idx = i;
765 #ifdef CONFIG_OPENGL
766 sdl2_console[i].opengl = display_opengl;
767 sdl2_console[i].dcl.ops = display_opengl ? &dcl_gl_ops : &dcl_2d_ops;
768 #else
769 sdl2_console[i].opengl = 0;
770 sdl2_console[i].dcl.ops = &dcl_2d_ops;
771 #endif
772 sdl2_console[i].dcl.con = con;
773 register_displaychangelistener(&sdl2_console[i].dcl);
776 /* Load a 32x32x4 image. White pixels are transparent. */
777 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
778 if (filename) {
779 SDL_Surface *image = SDL_LoadBMP(filename);
780 if (image) {
781 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
782 SDL_SetColorKey(image, SDL_TRUE, colorkey);
783 SDL_SetWindowIcon(sdl2_console[0].real_window, image);
785 g_free(filename);
788 if (full_screen) {
789 gui_fullscreen = 1;
790 sdl_grab_start(0);
793 mouse_mode_notifier.notify = sdl_mouse_mode_change;
794 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
796 gui_grab = 0;
798 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
799 sdl_cursor_normal = SDL_GetCursor();
801 atexit(sdl_cleanup);