sdl: move version logic from source code to makefile
[qemu.git] / ui / sdl2.c
blob45f23b15e0fc14fd13225fc468d0a0b5e6bf0d9f
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 "sysemu/sysemu.h"
37 #include "sdl2-keymap.h"
39 static int sdl2_num_outputs;
40 static struct sdl2_state {
41 DisplayChangeListener dcl;
42 DisplaySurface *surface;
43 SDL_Texture *texture;
44 SDL_Window *real_window;
45 SDL_Renderer *real_renderer;
46 int idx;
47 int last_vm_running; /* per console for caption reasons */
48 int x, y;
49 int hidden;
50 } *sdl2_console;
52 static SDL_Surface *guest_sprite_surface;
53 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
55 static bool gui_saved_scaling;
56 static int gui_saved_width;
57 static int gui_saved_height;
58 static int gui_saved_grab;
59 static int gui_fullscreen;
60 static int gui_noframe;
61 static int gui_key_modifier_pressed;
62 static int gui_keysym;
63 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
64 static uint8_t modifiers_state[SDL_NUM_SCANCODES];
65 static SDL_Cursor *sdl_cursor_normal;
66 static SDL_Cursor *sdl_cursor_hidden;
67 static int absolute_enabled;
68 static int guest_cursor;
69 static int guest_x, guest_y;
70 static SDL_Cursor *guest_sprite;
71 static int scaling_active;
72 static Notifier mouse_mode_notifier;
74 static void sdl_update_caption(struct sdl2_state *scon);
76 static struct sdl2_state *get_scon_from_window(uint32_t window_id)
78 int i;
79 for (i = 0; i < sdl2_num_outputs; i++) {
80 if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
81 return &sdl2_console[i];
84 return NULL;
87 static void sdl_update(DisplayChangeListener *dcl,
88 int x, int y, int w, int h)
90 struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
91 SDL_Rect rect;
92 DisplaySurface *surf = qemu_console_surface(dcl->con);
94 if (!surf) {
95 return;
97 if (!scon->texture) {
98 return;
101 rect.x = x;
102 rect.y = y;
103 rect.w = w;
104 rect.h = h;
106 SDL_UpdateTexture(scon->texture, NULL, surface_data(surf),
107 surface_stride(surf));
108 SDL_RenderCopy(scon->real_renderer, scon->texture, &rect, &rect);
109 SDL_RenderPresent(scon->real_renderer);
112 static void do_sdl_resize(struct sdl2_state *scon, int width, int height,
113 int bpp)
115 int flags;
117 if (scon->real_window && scon->real_renderer) {
118 if (width && height) {
119 SDL_RenderSetLogicalSize(scon->real_renderer, width, height);
120 SDL_SetWindowSize(scon->real_window, width, height);
121 } else {
122 SDL_DestroyRenderer(scon->real_renderer);
123 SDL_DestroyWindow(scon->real_window);
124 scon->real_renderer = NULL;
125 scon->real_window = NULL;
127 } else {
128 if (!width || !height) {
129 return;
131 flags = 0;
132 if (gui_fullscreen) {
133 flags |= SDL_WINDOW_FULLSCREEN;
134 } else {
135 flags |= SDL_WINDOW_RESIZABLE;
137 if (scon->hidden) {
138 flags |= SDL_WINDOW_HIDDEN;
141 scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
142 SDL_WINDOWPOS_UNDEFINED,
143 width, height, flags);
144 scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
145 sdl_update_caption(scon);
149 static void sdl_switch(DisplayChangeListener *dcl,
150 DisplaySurface *new_surface)
152 struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
153 int format = 0;
154 int idx = scon->idx;
155 DisplaySurface *old_surface = scon->surface;
157 /* temporary hack: allows to call sdl_switch to handle scaling changes */
158 if (new_surface) {
159 scon->surface = new_surface;
162 if (!new_surface && idx > 0) {
163 scon->surface = NULL;
166 if (new_surface == NULL) {
167 do_sdl_resize(scon, 0, 0, 0);
168 } else {
169 do_sdl_resize(scon, surface_width(scon->surface),
170 surface_height(scon->surface), 0);
173 if (old_surface && scon->texture) {
174 SDL_DestroyTexture(scon->texture);
175 scon->texture = NULL;
178 if (new_surface) {
179 if (!scon->texture) {
180 if (surface_bits_per_pixel(scon->surface) == 16) {
181 format = SDL_PIXELFORMAT_RGB565;
182 } else if (surface_bits_per_pixel(scon->surface) == 32) {
183 format = SDL_PIXELFORMAT_ARGB8888;
186 scon->texture = SDL_CreateTexture(scon->real_renderer, format,
187 SDL_TEXTUREACCESS_STREAMING,
188 surface_width(new_surface),
189 surface_height(new_surface));
194 static void reset_keys(struct sdl2_state *scon)
196 QemuConsole *con = scon ? scon->dcl.con : NULL;
197 int i;
199 for (i = 0; i < 256; i++) {
200 if (modifiers_state[i]) {
201 int qcode = sdl2_scancode_to_qcode[i];
202 qemu_input_event_send_key_qcode(con, qcode, false);
203 modifiers_state[i] = 0;
208 static void sdl_process_key(struct sdl2_state *scon,
209 SDL_KeyboardEvent *ev)
211 int qcode = sdl2_scancode_to_qcode[ev->keysym.scancode];
212 QemuConsole *con = scon ? scon->dcl.con : NULL;
214 if (!qemu_console_is_graphic(con)) {
215 if (ev->type == SDL_KEYDOWN) {
216 switch (ev->keysym.scancode) {
217 case SDL_SCANCODE_RETURN:
218 kbd_put_keysym_console(con, '\n');
219 break;
220 case SDL_SCANCODE_BACKSPACE:
221 kbd_put_keysym_console(con, QEMU_KEY_BACKSPACE);
222 break;
223 default:
224 kbd_put_qcode_console(con, qcode);
225 break;
228 return;
231 switch (ev->keysym.scancode) {
232 #if 0
233 case SDL_SCANCODE_NUMLOCKCLEAR:
234 case SDL_SCANCODE_CAPSLOCK:
235 /* SDL does not send the key up event, so we generate it */
236 qemu_input_event_send_key_qcode(con, qcode, true);
237 qemu_input_event_send_key_qcode(con, qcode, false);
238 return;
239 #endif
240 case SDL_SCANCODE_LCTRL:
241 case SDL_SCANCODE_LSHIFT:
242 case SDL_SCANCODE_LALT:
243 case SDL_SCANCODE_LGUI:
244 case SDL_SCANCODE_RCTRL:
245 case SDL_SCANCODE_RSHIFT:
246 case SDL_SCANCODE_RALT:
247 case SDL_SCANCODE_RGUI:
248 if (ev->type == SDL_KEYUP) {
249 modifiers_state[ev->keysym.scancode] = 0;
250 } else {
251 modifiers_state[ev->keysym.scancode] = 1;
253 /* fall though */
254 default:
255 qemu_input_event_send_key_qcode(con, qcode,
256 ev->type == SDL_KEYDOWN);
260 static void sdl_update_caption(struct sdl2_state *scon)
262 char win_title[1024];
263 char icon_title[1024];
264 const char *status = "";
266 if (!runstate_is_running()) {
267 status = " [Stopped]";
268 } else if (gui_grab) {
269 if (alt_grab) {
270 status = " - Press Ctrl-Alt-Shift to exit mouse grab";
271 } else if (ctrl_grab) {
272 status = " - Press Right-Ctrl to exit mouse grab";
273 } else {
274 status = " - Press Ctrl-Alt to exit mouse grab";
278 if (qemu_name) {
279 snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
280 scon->idx, status);
281 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
282 } else {
283 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
284 snprintf(icon_title, sizeof(icon_title), "QEMU");
287 if (scon->real_window) {
288 SDL_SetWindowTitle(scon->real_window, win_title);
292 static void sdl_hide_cursor(void)
294 if (!cursor_hide) {
295 return;
298 if (qemu_input_is_absolute()) {
299 SDL_ShowCursor(1);
300 SDL_SetCursor(sdl_cursor_hidden);
301 } else {
302 SDL_SetRelativeMouseMode(SDL_TRUE);
306 static void sdl_show_cursor(void)
308 if (!cursor_hide) {
309 return;
312 if (!qemu_input_is_absolute()) {
313 SDL_SetRelativeMouseMode(SDL_FALSE);
314 SDL_ShowCursor(1);
315 if (guest_cursor &&
316 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
317 SDL_SetCursor(guest_sprite);
318 } else {
319 SDL_SetCursor(sdl_cursor_normal);
324 static void sdl_grab_start(struct sdl2_state *scon)
326 QemuConsole *con = scon ? scon->dcl.con : NULL;
328 if (!con || !qemu_console_is_graphic(con)) {
329 return;
332 * If the application is not active, do not try to enter grab state. This
333 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
334 * application (SDL bug).
336 if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
337 return;
339 if (guest_cursor) {
340 SDL_SetCursor(guest_sprite);
341 if (!qemu_input_is_absolute() && !absolute_enabled) {
342 SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
344 } else {
345 sdl_hide_cursor();
347 SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
348 gui_grab = 1;
349 sdl_update_caption(scon);
352 static void sdl_grab_end(struct sdl2_state *scon)
354 SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
355 gui_grab = 0;
356 sdl_show_cursor();
357 sdl_update_caption(scon);
360 static void absolute_mouse_grab(struct sdl2_state *scon)
362 int mouse_x, mouse_y;
363 int scr_w, scr_h;
364 SDL_GetMouseState(&mouse_x, &mouse_y);
365 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
366 if (mouse_x > 0 && mouse_x < scr_w - 1 &&
367 mouse_y > 0 && mouse_y < scr_h - 1) {
368 sdl_grab_start(scon);
372 static void sdl_mouse_mode_change(Notifier *notify, void *data)
374 if (qemu_input_is_absolute()) {
375 if (!absolute_enabled) {
376 absolute_enabled = 1;
377 absolute_mouse_grab(&sdl2_console[0]);
379 } else if (absolute_enabled) {
380 if (!gui_fullscreen) {
381 sdl_grab_end(&sdl2_console[0]);
383 absolute_enabled = 0;
387 static void sdl_send_mouse_event(struct sdl2_state *scon, int dx, int dy,
388 int x, int y, int state)
390 static uint32_t bmap[INPUT_BUTTON_MAX] = {
391 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
392 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
393 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
395 static uint32_t prev_state;
397 if (prev_state != state) {
398 qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
399 prev_state = state;
402 if (qemu_input_is_absolute()) {
403 int scr_w, scr_h;
404 int max_w = 0, max_h = 0;
405 int off_x = 0, off_y = 0;
406 int cur_off_x = 0, cur_off_y = 0;
407 int i;
409 for (i = 0; i < sdl2_num_outputs; i++) {
410 struct sdl2_state *thiscon = &sdl2_console[i];
411 if (thiscon->real_window && thiscon->surface) {
412 SDL_GetWindowSize(thiscon->real_window, &scr_w, &scr_h);
413 cur_off_x = thiscon->x;
414 cur_off_y = thiscon->y;
415 if (scr_w + cur_off_x > max_w) {
416 max_w = scr_w + cur_off_x;
418 if (scr_h + cur_off_y > max_h) {
419 max_h = scr_h + cur_off_y;
421 if (i == scon->idx) {
422 off_x = cur_off_x;
423 off_y = cur_off_y;
427 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X, off_x + x, max_w);
428 qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y, off_y + y, max_h);
429 } else {
430 if (guest_cursor) {
431 x -= guest_x;
432 y -= guest_y;
433 guest_x += x;
434 guest_y += y;
435 dx = x;
436 dy = y;
438 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
439 qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
441 qemu_input_event_sync();
444 static void sdl_scale(struct sdl2_state *scon, int width, int height)
446 int bpp = 0;
447 do_sdl_resize(scon, width, height, bpp);
448 scaling_active = 1;
451 static void toggle_full_screen(struct sdl2_state *scon)
453 int width = surface_width(scon->surface);
454 int height = surface_height(scon->surface);
455 int bpp = surface_bits_per_pixel(scon->surface);
457 gui_fullscreen = !gui_fullscreen;
458 if (gui_fullscreen) {
459 SDL_GetWindowSize(scon->real_window,
460 &gui_saved_width, &gui_saved_height);
461 gui_saved_scaling = scaling_active;
463 do_sdl_resize(scon, width, height, bpp);
464 scaling_active = 0;
466 gui_saved_grab = gui_grab;
467 sdl_grab_start(scon);
468 } else {
469 if (gui_saved_scaling) {
470 sdl_scale(scon, gui_saved_width, gui_saved_height);
471 } else {
472 do_sdl_resize(scon, width, height, 0);
474 if (!gui_saved_grab) {
475 sdl_grab_end(scon);
478 graphic_hw_invalidate(scon->dcl.con);
479 graphic_hw_update(scon->dcl.con);
482 static void handle_keydown(SDL_Event *ev)
484 int mod_state, win;
485 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
487 if (alt_grab) {
488 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
489 (gui_grab_code | KMOD_LSHIFT);
490 } else if (ctrl_grab) {
491 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
492 } else {
493 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
495 gui_key_modifier_pressed = mod_state;
497 if (gui_key_modifier_pressed) {
498 switch (ev->key.keysym.scancode) {
499 case SDL_SCANCODE_2:
500 case SDL_SCANCODE_3:
501 case SDL_SCANCODE_4:
502 case SDL_SCANCODE_5:
503 case SDL_SCANCODE_6:
504 case SDL_SCANCODE_7:
505 case SDL_SCANCODE_8:
506 case SDL_SCANCODE_9:
507 win = ev->key.keysym.scancode - SDL_SCANCODE_1;
508 if (win < sdl2_num_outputs) {
509 sdl2_console[win].hidden = !sdl2_console[win].hidden;
510 if (sdl2_console[win].real_window) {
511 if (sdl2_console[win].hidden) {
512 SDL_HideWindow(sdl2_console[win].real_window);
513 } else {
514 SDL_ShowWindow(sdl2_console[win].real_window);
517 gui_keysym = 1;
519 break;
520 case SDL_SCANCODE_F:
521 toggle_full_screen(scon);
522 gui_keysym = 1;
523 break;
524 case SDL_SCANCODE_U:
525 if (scaling_active) {
526 scaling_active = 0;
527 sdl_switch(&scon->dcl, NULL);
528 graphic_hw_invalidate(scon->dcl.con);
529 graphic_hw_update(scon->dcl.con);
531 gui_keysym = 1;
532 break;
533 case SDL_SCANCODE_KP_PLUS:
534 case SDL_SCANCODE_KP_MINUS:
535 if (!gui_fullscreen) {
536 int scr_w, scr_h;
537 int width, height;
538 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
540 width = MAX(scr_w + (ev->key.keysym.scancode ==
541 SDL_SCANCODE_KP_PLUS ? 50 : -50),
542 160);
543 height = (surface_height(scon->surface) * width) /
544 surface_width(scon->surface);
546 sdl_scale(scon, width, height);
547 graphic_hw_invalidate(NULL);
548 graphic_hw_update(NULL);
549 gui_keysym = 1;
551 default:
552 break;
555 if (!gui_keysym) {
556 sdl_process_key(scon, &ev->key);
560 static void handle_keyup(SDL_Event *ev)
562 int mod_state;
563 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
565 if (!alt_grab) {
566 mod_state = (ev->key.keysym.mod & gui_grab_code);
567 } else {
568 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
570 if (!mod_state && gui_key_modifier_pressed) {
571 gui_key_modifier_pressed = 0;
572 if (gui_keysym == 0) {
573 /* exit/enter grab if pressing Ctrl-Alt */
574 if (!gui_grab) {
575 sdl_grab_start(scon);
576 } else if (!gui_fullscreen) {
577 sdl_grab_end(scon);
579 /* SDL does not send back all the modifiers key, so we must
580 * correct it. */
581 reset_keys(scon);
582 return;
584 gui_keysym = 0;
586 if (!gui_keysym) {
587 sdl_process_key(scon, &ev->key);
591 static void handle_textinput(SDL_Event *ev)
593 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
594 QemuConsole *con = scon ? scon->dcl.con : NULL;
596 if (qemu_console_is_graphic(con)) {
597 return;
599 kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
602 static void handle_mousemotion(SDL_Event *ev)
604 int max_x, max_y;
605 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
607 if (qemu_input_is_absolute() || absolute_enabled) {
608 int scr_w, scr_h;
609 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
610 max_x = scr_w - 1;
611 max_y = scr_h - 1;
612 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
613 ev->motion.x == max_x || ev->motion.y == max_y)) {
614 sdl_grab_end(scon);
616 if (!gui_grab &&
617 (ev->motion.x > 0 && ev->motion.x < max_x &&
618 ev->motion.y > 0 && ev->motion.y < max_y)) {
619 sdl_grab_start(scon);
622 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
623 sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
624 ev->motion.x, ev->motion.y, ev->motion.state);
628 static void handle_mousebutton(SDL_Event *ev)
630 int buttonstate = SDL_GetMouseState(NULL, NULL);
631 SDL_MouseButtonEvent *bev;
632 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
634 bev = &ev->button;
635 if (!gui_grab && !qemu_input_is_absolute()) {
636 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
637 /* start grabbing all events */
638 sdl_grab_start(scon);
640 } else {
641 if (ev->type == SDL_MOUSEBUTTONDOWN) {
642 buttonstate |= SDL_BUTTON(bev->button);
643 } else {
644 buttonstate &= ~SDL_BUTTON(bev->button);
646 sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
650 static void handle_mousewheel(SDL_Event *ev)
652 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
653 SDL_MouseWheelEvent *wev = &ev->wheel;
654 InputButton btn;
656 if (wev->y > 0) {
657 btn = INPUT_BUTTON_WHEEL_UP;
658 } else if (wev->y < 0) {
659 btn = INPUT_BUTTON_WHEEL_DOWN;
660 } else {
661 return;
664 qemu_input_queue_btn(scon->dcl.con, btn, true);
665 qemu_input_event_sync();
666 qemu_input_queue_btn(scon->dcl.con, btn, false);
667 qemu_input_event_sync();
670 static void handle_windowevent(DisplayChangeListener *dcl, SDL_Event *ev)
672 int w, h;
673 struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
675 switch (ev->window.event) {
676 case SDL_WINDOWEVENT_RESIZED:
677 sdl_scale(scon, ev->window.data1, ev->window.data2);
679 QemuUIInfo info;
680 memset(&info, 0, sizeof(info));
681 info.width = ev->window.data1;
682 info.height = ev->window.data2;
683 dpy_set_ui_info(scon->dcl.con, &info);
685 graphic_hw_invalidate(scon->dcl.con);
686 graphic_hw_update(scon->dcl.con);
687 break;
688 case SDL_WINDOWEVENT_EXPOSED:
689 SDL_GetWindowSize(SDL_GetWindowFromID(ev->window.windowID), &w, &h);
690 sdl_update(dcl, 0, 0, w, h);
691 break;
692 case SDL_WINDOWEVENT_FOCUS_GAINED:
693 case SDL_WINDOWEVENT_ENTER:
694 if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
695 absolute_mouse_grab(scon);
697 break;
698 case SDL_WINDOWEVENT_FOCUS_LOST:
699 if (gui_grab && !gui_fullscreen) {
700 sdl_grab_end(scon);
702 break;
703 case SDL_WINDOWEVENT_RESTORED:
704 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
705 break;
706 case SDL_WINDOWEVENT_MINIMIZED:
707 update_displaychangelistener(dcl, 500);
708 break;
709 case SDL_WINDOWEVENT_CLOSE:
710 if (!no_quit) {
711 no_shutdown = 0;
712 qemu_system_shutdown_request();
714 break;
718 static void sdl_refresh(DisplayChangeListener *dcl)
720 struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
721 SDL_Event ev1, *ev = &ev1;
723 if (scon->last_vm_running != runstate_is_running()) {
724 scon->last_vm_running = runstate_is_running();
725 sdl_update_caption(scon);
728 graphic_hw_update(dcl->con);
730 while (SDL_PollEvent(ev)) {
731 switch (ev->type) {
732 case SDL_KEYDOWN:
733 handle_keydown(ev);
734 break;
735 case SDL_KEYUP:
736 handle_keyup(ev);
737 break;
738 case SDL_TEXTINPUT:
739 handle_textinput(ev);
740 break;
741 case SDL_QUIT:
742 if (!no_quit) {
743 no_shutdown = 0;
744 qemu_system_shutdown_request();
746 break;
747 case SDL_MOUSEMOTION:
748 handle_mousemotion(ev);
749 break;
750 case SDL_MOUSEBUTTONDOWN:
751 case SDL_MOUSEBUTTONUP:
752 handle_mousebutton(ev);
753 break;
754 case SDL_MOUSEWHEEL:
755 handle_mousewheel(ev);
756 break;
757 case SDL_WINDOWEVENT:
758 handle_windowevent(dcl, ev);
759 break;
760 default:
761 break;
766 static void sdl_mouse_warp(DisplayChangeListener *dcl,
767 int x, int y, int on)
769 struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
770 if (on) {
771 if (!guest_cursor) {
772 sdl_show_cursor();
774 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
775 SDL_SetCursor(guest_sprite);
776 if (!qemu_input_is_absolute() && !absolute_enabled) {
777 SDL_WarpMouseInWindow(scon->real_window, x, y);
780 } else if (gui_grab) {
781 sdl_hide_cursor();
783 guest_cursor = on;
784 guest_x = x, guest_y = y;
787 static void sdl_mouse_define(DisplayChangeListener *dcl,
788 QEMUCursor *c)
791 if (guest_sprite) {
792 SDL_FreeCursor(guest_sprite);
795 if (guest_sprite_surface) {
796 SDL_FreeSurface(guest_sprite_surface);
799 guest_sprite_surface =
800 SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
801 0xff0000, 0x00ff00, 0xff, 0xff000000);
803 if (!guest_sprite_surface) {
804 fprintf(stderr, "Failed to make rgb surface from %p\n", c);
805 return;
807 guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
808 c->hot_x, c->hot_y);
809 if (!guest_sprite) {
810 fprintf(stderr, "Failed to make color cursor from %p\n", c);
811 return;
813 if (guest_cursor &&
814 (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
815 SDL_SetCursor(guest_sprite);
819 static void sdl_cleanup(void)
821 if (guest_sprite) {
822 SDL_FreeCursor(guest_sprite);
824 SDL_QuitSubSystem(SDL_INIT_VIDEO);
827 static const DisplayChangeListenerOps dcl_ops = {
828 .dpy_name = "sdl",
829 .dpy_gfx_update = sdl_update,
830 .dpy_gfx_switch = sdl_switch,
831 .dpy_refresh = sdl_refresh,
832 .dpy_mouse_set = sdl_mouse_warp,
833 .dpy_cursor_define = sdl_mouse_define,
836 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
838 int flags;
839 uint8_t data = 0;
840 char *filename;
841 int i;
843 if (no_frame) {
844 gui_noframe = 1;
847 #ifdef __linux__
848 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
849 * accessible $DISPLAY to open X11 window. This is often the case
850 * when qemu is run using sudo. But in this case, and when actually
851 * run in X11 environment, SDL fights with X11 for the video card,
852 * making current display unavailable, often until reboot.
853 * So make x11 the default SDL video driver if this variable is unset.
854 * This is a bit hackish but saves us from bigger problem.
855 * Maybe it's a good idea to fix this in SDL instead.
857 setenv("SDL_VIDEODRIVER", "x11", 0);
858 #endif
860 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
861 if (SDL_Init(flags)) {
862 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
863 SDL_GetError());
864 exit(1);
867 for (i = 0;; i++) {
868 QemuConsole *con = qemu_console_lookup_by_index(i);
869 if (!con) {
870 break;
873 sdl2_num_outputs = i;
874 sdl2_console = g_new0(struct sdl2_state, sdl2_num_outputs);
875 for (i = 0; i < sdl2_num_outputs; i++) {
876 QemuConsole *con = qemu_console_lookup_by_index(i);
877 if (!qemu_console_is_graphic(con)) {
878 sdl2_console[i].hidden = true;
880 sdl2_console[i].dcl.ops = &dcl_ops;
881 sdl2_console[i].dcl.con = con;
882 register_displaychangelistener(&sdl2_console[i].dcl);
883 sdl2_console[i].idx = i;
886 /* Load a 32x32x4 image. White pixels are transparent. */
887 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
888 if (filename) {
889 SDL_Surface *image = SDL_LoadBMP(filename);
890 if (image) {
891 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
892 SDL_SetColorKey(image, SDL_TRUE, colorkey);
893 SDL_SetWindowIcon(sdl2_console[0].real_window, image);
895 g_free(filename);
898 if (full_screen) {
899 gui_fullscreen = 1;
900 sdl_grab_start(0);
903 mouse_mode_notifier.notify = sdl_mouse_mode_change;
904 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
906 gui_grab = 0;
908 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
909 sdl_cursor_normal = SDL_GetCursor();
911 atexit(sdl_cleanup);