qapi: Add missing JSON files in build dependencies
[qemu/armbru.git] / ui / sdl.c
blob9804ee8dfbf638a692d7f3ab0f8503cc3a679065
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.
25 /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
26 #undef WIN32_LEAN_AND_MEAN
28 #include "qemu/osdep.h"
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"
36 #include "x_keymap.h"
37 #include "sdl_zoom.h"
39 static DisplayChangeListener *dcl;
40 static DisplaySurface *surface;
41 static SDL_Surface *real_screen;
42 static SDL_Surface *guest_screen = NULL;
43 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
44 static int last_vm_running;
45 static bool gui_saved_scaling;
46 static int gui_saved_width;
47 static int gui_saved_height;
48 static int gui_saved_grab;
49 static int gui_fullscreen;
50 static int gui_noframe;
51 static int gui_key_modifier_pressed;
52 static int gui_keysym;
53 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
54 static uint8_t modifiers_state[256];
55 static SDL_Cursor *sdl_cursor_normal;
56 static SDL_Cursor *sdl_cursor_hidden;
57 static int absolute_enabled = 0;
58 static int guest_cursor = 0;
59 static int guest_x, guest_y;
60 static SDL_Cursor *guest_sprite = NULL;
61 static SDL_PixelFormat host_format;
62 static int scaling_active = 0;
63 static Notifier mouse_mode_notifier;
64 static int idle_counter;
66 #define SDL_REFRESH_INTERVAL_BUSY 10
67 #define SDL_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
68 / SDL_REFRESH_INTERVAL_BUSY + 1)
70 #if 0
71 #define DEBUG_SDL
72 #endif
74 static void sdl_update(DisplayChangeListener *dcl,
75 int x, int y, int w, int h)
77 SDL_Rect rec;
78 rec.x = x;
79 rec.y = y;
80 rec.w = w;
81 rec.h = h;
83 #ifdef DEBUG_SDL
84 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
85 x, y, w, h, scaling_active);
86 #endif
88 if (guest_screen) {
89 if (!scaling_active) {
90 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
91 } else {
92 if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) {
93 fprintf(stderr, "Zoom blit failed\n");
94 exit(1);
98 SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h);
101 static void do_sdl_resize(int width, int height, int bpp)
103 int flags;
104 SDL_Surface *tmp_screen;
106 #ifdef DEBUG_SDL
107 printf("SDL: Resizing to %dx%d bpp %d\n", width, height, bpp);
108 #endif
110 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
111 if (gui_fullscreen) {
112 flags |= SDL_FULLSCREEN;
113 } else {
114 flags |= SDL_RESIZABLE;
116 if (gui_noframe)
117 flags |= SDL_NOFRAME;
119 tmp_screen = SDL_SetVideoMode(width, height, bpp, flags);
120 if (!real_screen) {
121 if (!tmp_screen) {
122 fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n",
123 width, height, bpp, SDL_GetError());
124 exit(1);
126 } else {
128 * Revert to the previous video mode if the change of resizing or
129 * resolution failed.
131 if (!tmp_screen) {
132 fprintf(stderr, "Failed to set SDL display (%dx%dx%d): %s\n",
133 width, height, bpp, SDL_GetError());
134 return;
138 real_screen = tmp_screen;
141 static void sdl_switch(DisplayChangeListener *dcl,
142 DisplaySurface *new_surface)
144 PixelFormat pf;
146 /* temporary hack: allows to call sdl_switch to handle scaling changes */
147 if (new_surface) {
148 surface = new_surface;
150 pf = qemu_pixelformat_from_pixman(surface->format);
152 if (!scaling_active) {
153 do_sdl_resize(surface_width(surface), surface_height(surface), 0);
154 } else if (real_screen->format->BitsPerPixel !=
155 surface_bits_per_pixel(surface)) {
156 do_sdl_resize(real_screen->w, real_screen->h,
157 surface_bits_per_pixel(surface));
160 if (guest_screen != NULL) {
161 SDL_FreeSurface(guest_screen);
164 #ifdef DEBUG_SDL
165 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
166 pf.rmask, pf.gmask, pf.bmask, pf.amask);
167 #endif
169 guest_screen = SDL_CreateRGBSurfaceFrom
170 (surface_data(surface),
171 surface_width(surface), surface_height(surface),
172 surface_bits_per_pixel(surface), surface_stride(surface),
173 pf.rmask, pf.gmask,
174 pf.bmask, pf.amask);
177 static bool sdl_check_format(DisplayChangeListener *dcl,
178 pixman_format_code_t format)
181 * We let SDL convert for us a few more formats than,
182 * the native ones. Thes are the ones I have tested.
184 return (format == PIXMAN_x8r8g8b8 ||
185 format == PIXMAN_b8g8r8x8 ||
186 format == PIXMAN_x1r5g5b5 ||
187 format == PIXMAN_r5g6b5);
190 /* generic keyboard conversion */
192 #include "sdl_keysym.h"
194 static kbd_layout_t *kbd_layout = NULL;
196 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
198 int keysym;
199 /* workaround for X11+SDL bug with AltGR */
200 keysym = ev->keysym.sym;
201 if (keysym == 0 && ev->keysym.scancode == 113)
202 keysym = SDLK_MODE;
203 /* For Japanese key '\' and '|' */
204 if (keysym == 92 && ev->keysym.scancode == 133) {
205 keysym = 0xa5;
207 return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
210 /* specific keyboard conversions from scan codes */
212 #if defined(_WIN32)
214 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
216 return ev->keysym.scancode;
219 #else
221 #if defined(SDL_VIDEO_DRIVER_X11)
222 #include <X11/XKBlib.h>
224 static int check_for_evdev(void)
226 SDL_SysWMinfo info;
227 XkbDescPtr desc = NULL;
228 int has_evdev = 0;
229 char *keycodes = NULL;
231 SDL_VERSION(&info.version);
232 if (!SDL_GetWMInfo(&info)) {
233 return 0;
235 desc = XkbGetKeyboard(info.info.x11.display,
236 XkbGBN_AllComponentsMask,
237 XkbUseCoreKbd);
238 if (desc && desc->names) {
239 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
240 if (keycodes == NULL) {
241 fprintf(stderr, "could not lookup keycode name\n");
242 } else if (strstart(keycodes, "evdev", NULL)) {
243 has_evdev = 1;
244 } else if (!strstart(keycodes, "xfree86", NULL)) {
245 fprintf(stderr, "unknown keycodes `%s', please report to "
246 "qemu-devel@nongnu.org\n", keycodes);
250 if (desc) {
251 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
253 if (keycodes) {
254 XFree(keycodes);
256 return has_evdev;
258 #else
259 static int check_for_evdev(void)
261 return 0;
263 #endif
265 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
267 int keycode;
268 static int has_evdev = -1;
270 if (has_evdev == -1)
271 has_evdev = check_for_evdev();
273 keycode = ev->keysym.scancode;
275 if (keycode < 9) {
276 keycode = 0;
277 } else if (keycode < 97) {
278 keycode -= 8; /* just an offset */
279 } else if (keycode < 158) {
280 /* use conversion table */
281 if (has_evdev)
282 keycode = translate_evdev_keycode(keycode - 97);
283 else
284 keycode = translate_xfree86_keycode(keycode - 97);
285 } else if (keycode == 208) { /* Hiragana_Katakana */
286 keycode = 0x70;
287 } else if (keycode == 211) { /* backslash */
288 keycode = 0x73;
289 } else {
290 keycode = 0;
292 return keycode;
295 #endif
297 static void reset_keys(void)
299 int i;
300 for(i = 0; i < 256; i++) {
301 if (modifiers_state[i]) {
302 qemu_input_event_send_key_number(dcl->con, i, false);
303 modifiers_state[i] = 0;
308 static void sdl_process_key(SDL_KeyboardEvent *ev)
310 int keycode;
312 if (ev->keysym.sym == SDLK_PAUSE) {
313 /* specific case */
314 qemu_input_event_send_key_qcode(dcl->con, Q_KEY_CODE_PAUSE,
315 ev->type == SDL_KEYDOWN);
316 return;
319 if (kbd_layout) {
320 keycode = sdl_keyevent_to_keycode_generic(ev);
321 } else {
322 keycode = sdl_keyevent_to_keycode(ev);
325 switch(keycode) {
326 case 0x00:
327 /* sent when leaving window: reset the modifiers state */
328 reset_keys();
329 return;
330 case 0x2a: /* Left Shift */
331 case 0x36: /* Right Shift */
332 case 0x1d: /* Left CTRL */
333 case 0x9d: /* Right CTRL */
334 case 0x38: /* Left ALT */
335 case 0xb8: /* Right ALT */
336 if (ev->type == SDL_KEYUP)
337 modifiers_state[keycode] = 0;
338 else
339 modifiers_state[keycode] = 1;
340 break;
341 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
342 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
343 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
344 case 0x45: /* num lock */
345 case 0x3a: /* caps lock */
346 /* SDL does not send the key up event, so we generate it */
347 qemu_input_event_send_key_number(dcl->con, keycode, true);
348 qemu_input_event_send_key_number(dcl->con, keycode, false);
349 return;
350 #endif
353 /* now send the key code */
354 qemu_input_event_send_key_number(dcl->con, keycode,
355 ev->type == SDL_KEYDOWN);
358 static void sdl_update_caption(void)
360 char win_title[1024];
361 char icon_title[1024];
362 const char *status = "";
364 if (!runstate_is_running())
365 status = " [Stopped]";
366 else if (gui_grab) {
367 if (alt_grab)
368 status = " - Press Ctrl-Alt-Shift to exit mouse grab";
369 else if (ctrl_grab)
370 status = " - Press Right-Ctrl to exit mouse grab";
371 else
372 status = " - Press Ctrl-Alt to exit mouse grab";
375 if (qemu_name) {
376 snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
377 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
378 } else {
379 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
380 snprintf(icon_title, sizeof(icon_title), "QEMU");
383 SDL_WM_SetCaption(win_title, icon_title);
386 static void sdl_hide_cursor(void)
388 if (!cursor_hide)
389 return;
391 if (qemu_input_is_absolute()) {
392 SDL_ShowCursor(1);
393 SDL_SetCursor(sdl_cursor_hidden);
394 } else {
395 SDL_ShowCursor(0);
399 static void sdl_show_cursor(void)
401 if (!cursor_hide)
402 return;
404 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL)) {
405 SDL_ShowCursor(1);
406 if (guest_cursor &&
407 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
408 SDL_SetCursor(guest_sprite);
409 else
410 SDL_SetCursor(sdl_cursor_normal);
414 static void sdl_grab_start(void)
417 * If the application is not active, do not try to enter grab state. This
418 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
419 * application (SDL bug).
421 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) {
422 return;
424 if (guest_cursor) {
425 SDL_SetCursor(guest_sprite);
426 if (!qemu_input_is_absolute() && !absolute_enabled) {
427 SDL_WarpMouse(guest_x, guest_y);
429 } else
430 sdl_hide_cursor();
431 SDL_WM_GrabInput(SDL_GRAB_ON);
432 gui_grab = 1;
433 sdl_update_caption();
436 static void sdl_grab_end(void)
438 SDL_WM_GrabInput(SDL_GRAB_OFF);
439 gui_grab = 0;
440 sdl_show_cursor();
441 sdl_update_caption();
444 static void absolute_mouse_grab(void)
446 int mouse_x, mouse_y;
448 SDL_GetMouseState(&mouse_x, &mouse_y);
449 if (mouse_x > 0 && mouse_x < real_screen->w - 1 &&
450 mouse_y > 0 && mouse_y < real_screen->h - 1) {
451 sdl_grab_start();
455 static void sdl_mouse_mode_change(Notifier *notify, void *data)
457 if (qemu_input_is_absolute()) {
458 if (!absolute_enabled) {
459 absolute_enabled = 1;
460 if (qemu_console_is_graphic(NULL)) {
461 absolute_mouse_grab();
464 } else if (absolute_enabled) {
465 if (!gui_fullscreen) {
466 sdl_grab_end();
468 absolute_enabled = 0;
472 static void sdl_send_mouse_event(int dx, int dy, int x, int y, int state)
474 static uint32_t bmap[INPUT_BUTTON__MAX] = {
475 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
476 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
477 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
478 [INPUT_BUTTON_WHEELUP] = SDL_BUTTON(SDL_BUTTON_WHEELUP),
479 [INPUT_BUTTON_WHEELDOWN] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN),
481 static uint32_t prev_state;
483 if (prev_state != state) {
484 qemu_input_update_buttons(dcl->con, bmap, prev_state, state);
485 prev_state = state;
488 if (qemu_input_is_absolute()) {
489 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, x,
490 real_screen->w);
491 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, y,
492 real_screen->h);
493 } else {
494 if (guest_cursor) {
495 x -= guest_x;
496 y -= guest_y;
497 guest_x += x;
498 guest_y += y;
499 dx = x;
500 dy = y;
502 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, dx);
503 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, dy);
505 qemu_input_event_sync();
508 static void sdl_scale(int width, int height)
510 int bpp = real_screen->format->BitsPerPixel;
512 #ifdef DEBUG_SDL
513 printf("SDL: Scaling to %dx%d bpp %d\n", width, height, bpp);
514 #endif
516 if (bpp != 16 && bpp != 32) {
517 bpp = 32;
519 do_sdl_resize(width, height, bpp);
520 scaling_active = 1;
523 static void toggle_full_screen(void)
525 int width = surface_width(surface);
526 int height = surface_height(surface);
527 int bpp = surface_bits_per_pixel(surface);
529 gui_fullscreen = !gui_fullscreen;
530 if (gui_fullscreen) {
531 gui_saved_width = real_screen->w;
532 gui_saved_height = real_screen->h;
533 gui_saved_scaling = scaling_active;
535 do_sdl_resize(width, height, bpp);
536 scaling_active = 0;
538 gui_saved_grab = gui_grab;
539 sdl_grab_start();
540 } else {
541 if (gui_saved_scaling) {
542 sdl_scale(gui_saved_width, gui_saved_height);
543 } else {
544 do_sdl_resize(width, height, 0);
546 if (!gui_saved_grab || !qemu_console_is_graphic(NULL)) {
547 sdl_grab_end();
550 graphic_hw_invalidate(NULL);
551 graphic_hw_update(NULL);
554 static void handle_keydown(SDL_Event *ev)
556 int mod_state;
557 int keycode;
559 if (alt_grab) {
560 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
561 (gui_grab_code | KMOD_LSHIFT);
562 } else if (ctrl_grab) {
563 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
564 } else {
565 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
567 gui_key_modifier_pressed = mod_state;
569 if (gui_key_modifier_pressed) {
570 keycode = sdl_keyevent_to_keycode(&ev->key);
571 switch (keycode) {
572 case 0x21: /* 'f' key on US keyboard */
573 toggle_full_screen();
574 gui_keysym = 1;
575 break;
576 case 0x16: /* 'u' key on US keyboard */
577 if (scaling_active) {
578 scaling_active = 0;
579 sdl_switch(dcl, NULL);
580 graphic_hw_invalidate(NULL);
581 graphic_hw_update(NULL);
583 gui_keysym = 1;
584 break;
585 case 0x02 ... 0x0a: /* '1' to '9' keys */
586 /* Reset the modifiers sent to the current console */
587 reset_keys();
588 console_select(keycode - 0x02);
589 gui_keysym = 1;
590 if (gui_fullscreen) {
591 break;
593 if (!qemu_console_is_graphic(NULL)) {
594 /* release grab if going to a text console */
595 if (gui_grab) {
596 sdl_grab_end();
597 } else if (absolute_enabled) {
598 sdl_show_cursor();
600 } else if (absolute_enabled) {
601 sdl_hide_cursor();
602 absolute_mouse_grab();
604 break;
605 case 0x1b: /* '+' */
606 case 0x35: /* '-' */
607 if (!gui_fullscreen) {
608 int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50),
609 160);
610 int height = (surface_height(surface) * width) /
611 surface_width(surface);
613 sdl_scale(width, height);
614 graphic_hw_invalidate(NULL);
615 graphic_hw_update(NULL);
616 gui_keysym = 1;
618 default:
619 break;
621 } else if (!qemu_console_is_graphic(NULL)) {
622 int keysym = 0;
624 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
625 switch (ev->key.keysym.sym) {
626 case SDLK_UP:
627 keysym = QEMU_KEY_CTRL_UP;
628 break;
629 case SDLK_DOWN:
630 keysym = QEMU_KEY_CTRL_DOWN;
631 break;
632 case SDLK_LEFT:
633 keysym = QEMU_KEY_CTRL_LEFT;
634 break;
635 case SDLK_RIGHT:
636 keysym = QEMU_KEY_CTRL_RIGHT;
637 break;
638 case SDLK_HOME:
639 keysym = QEMU_KEY_CTRL_HOME;
640 break;
641 case SDLK_END:
642 keysym = QEMU_KEY_CTRL_END;
643 break;
644 case SDLK_PAGEUP:
645 keysym = QEMU_KEY_CTRL_PAGEUP;
646 break;
647 case SDLK_PAGEDOWN:
648 keysym = QEMU_KEY_CTRL_PAGEDOWN;
649 break;
650 default:
651 break;
653 } else {
654 switch (ev->key.keysym.sym) {
655 case SDLK_UP:
656 keysym = QEMU_KEY_UP;
657 break;
658 case SDLK_DOWN:
659 keysym = QEMU_KEY_DOWN;
660 break;
661 case SDLK_LEFT:
662 keysym = QEMU_KEY_LEFT;
663 break;
664 case SDLK_RIGHT:
665 keysym = QEMU_KEY_RIGHT;
666 break;
667 case SDLK_HOME:
668 keysym = QEMU_KEY_HOME;
669 break;
670 case SDLK_END:
671 keysym = QEMU_KEY_END;
672 break;
673 case SDLK_PAGEUP:
674 keysym = QEMU_KEY_PAGEUP;
675 break;
676 case SDLK_PAGEDOWN:
677 keysym = QEMU_KEY_PAGEDOWN;
678 break;
679 case SDLK_BACKSPACE:
680 keysym = QEMU_KEY_BACKSPACE;
681 break;
682 case SDLK_DELETE:
683 keysym = QEMU_KEY_DELETE;
684 break;
685 default:
686 break;
689 if (keysym) {
690 kbd_put_keysym(keysym);
691 } else if (ev->key.keysym.unicode != 0) {
692 kbd_put_keysym(ev->key.keysym.unicode);
695 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
696 sdl_process_key(&ev->key);
700 static void handle_keyup(SDL_Event *ev)
702 int mod_state;
704 if (!alt_grab) {
705 mod_state = (ev->key.keysym.mod & gui_grab_code);
706 } else {
707 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
709 if (!mod_state && gui_key_modifier_pressed) {
710 gui_key_modifier_pressed = 0;
711 if (gui_keysym == 0) {
712 /* exit/enter grab if pressing Ctrl-Alt */
713 if (!gui_grab) {
714 if (qemu_console_is_graphic(NULL)) {
715 sdl_grab_start();
717 } else if (!gui_fullscreen) {
718 sdl_grab_end();
720 /* SDL does not send back all the modifiers key, so we must
721 * correct it. */
722 reset_keys();
723 return;
725 gui_keysym = 0;
727 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
728 sdl_process_key(&ev->key);
732 static void handle_mousemotion(SDL_Event *ev)
734 int max_x, max_y;
736 if (qemu_console_is_graphic(NULL) &&
737 (qemu_input_is_absolute() || absolute_enabled)) {
738 max_x = real_screen->w - 1;
739 max_y = real_screen->h - 1;
740 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
741 ev->motion.x == max_x || ev->motion.y == max_y)) {
742 sdl_grab_end();
744 if (!gui_grab &&
745 (ev->motion.x > 0 && ev->motion.x < max_x &&
746 ev->motion.y > 0 && ev->motion.y < max_y)) {
747 sdl_grab_start();
750 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
751 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel,
752 ev->motion.x, ev->motion.y, ev->motion.state);
756 static void handle_mousebutton(SDL_Event *ev)
758 int buttonstate = SDL_GetMouseState(NULL, NULL);
759 SDL_MouseButtonEvent *bev;
761 if (!qemu_console_is_graphic(NULL)) {
762 return;
765 bev = &ev->button;
766 if (!gui_grab && !qemu_input_is_absolute()) {
767 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
768 /* start grabbing all events */
769 sdl_grab_start();
771 } else {
772 if (ev->type == SDL_MOUSEBUTTONDOWN) {
773 buttonstate |= SDL_BUTTON(bev->button);
774 } else {
775 buttonstate &= ~SDL_BUTTON(bev->button);
777 sdl_send_mouse_event(0, 0, bev->x, bev->y, buttonstate);
781 static void handle_activation(SDL_Event *ev)
783 #ifdef _WIN32
784 /* Disable grab if the window no longer has the focus
785 * (Windows-only workaround) */
786 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
787 !ev->active.gain && !gui_fullscreen) {
788 sdl_grab_end();
790 #endif
791 if (!gui_grab && ev->active.gain && qemu_console_is_graphic(NULL) &&
792 (qemu_input_is_absolute() || absolute_enabled)) {
793 absolute_mouse_grab();
795 if (ev->active.state & SDL_APPACTIVE) {
796 if (ev->active.gain) {
797 /* Back to default interval */
798 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
799 } else {
800 /* Sleeping interval. Not using the long default here as
801 * sdl_refresh does not only update the guest screen, but
802 * also checks for gui events. */
803 update_displaychangelistener(dcl, 500);
808 static void sdl_refresh(DisplayChangeListener *dcl)
810 SDL_Event ev1, *ev = &ev1;
811 int idle = 1;
813 if (last_vm_running != runstate_is_running()) {
814 last_vm_running = runstate_is_running();
815 sdl_update_caption();
818 graphic_hw_update(NULL);
819 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL));
821 while (SDL_PollEvent(ev)) {
822 switch (ev->type) {
823 case SDL_VIDEOEXPOSE:
824 sdl_update(dcl, 0, 0, real_screen->w, real_screen->h);
825 break;
826 case SDL_KEYDOWN:
827 idle = 0;
828 handle_keydown(ev);
829 break;
830 case SDL_KEYUP:
831 idle = 0;
832 handle_keyup(ev);
833 break;
834 case SDL_QUIT:
835 if (!no_quit) {
836 no_shutdown = 0;
837 qemu_system_shutdown_request();
839 break;
840 case SDL_MOUSEMOTION:
841 idle = 0;
842 handle_mousemotion(ev);
843 break;
844 case SDL_MOUSEBUTTONDOWN:
845 case SDL_MOUSEBUTTONUP:
846 idle = 0;
847 handle_mousebutton(ev);
848 break;
849 case SDL_ACTIVEEVENT:
850 handle_activation(ev);
851 break;
852 case SDL_VIDEORESIZE:
853 sdl_scale(ev->resize.w, ev->resize.h);
854 graphic_hw_invalidate(NULL);
855 graphic_hw_update(NULL);
856 break;
857 default:
858 break;
862 if (idle) {
863 if (idle_counter < SDL_MAX_IDLE_COUNT) {
864 idle_counter++;
865 if (idle_counter >= SDL_MAX_IDLE_COUNT) {
866 dcl->update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
869 } else {
870 idle_counter = 0;
871 dcl->update_interval = SDL_REFRESH_INTERVAL_BUSY;
875 static void sdl_mouse_warp(DisplayChangeListener *dcl,
876 int x, int y, int on)
878 if (on) {
879 if (!guest_cursor)
880 sdl_show_cursor();
881 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
882 SDL_SetCursor(guest_sprite);
883 if (!qemu_input_is_absolute() && !absolute_enabled) {
884 SDL_WarpMouse(x, y);
887 } else if (gui_grab)
888 sdl_hide_cursor();
889 guest_cursor = on;
890 guest_x = x, guest_y = y;
893 static void sdl_mouse_define(DisplayChangeListener *dcl,
894 QEMUCursor *c)
896 uint8_t *image, *mask;
897 int bpl;
899 if (guest_sprite)
900 SDL_FreeCursor(guest_sprite);
902 bpl = cursor_get_mono_bpl(c);
903 image = g_malloc0(bpl * c->height);
904 mask = g_malloc0(bpl * c->height);
905 cursor_get_mono_image(c, 0x000000, image);
906 cursor_get_mono_mask(c, 0, mask);
907 guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
908 c->hot_x, c->hot_y);
909 g_free(image);
910 g_free(mask);
912 if (guest_cursor &&
913 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
914 SDL_SetCursor(guest_sprite);
917 static void sdl_cleanup(void)
919 if (guest_sprite)
920 SDL_FreeCursor(guest_sprite);
921 SDL_QuitSubSystem(SDL_INIT_VIDEO);
924 static const DisplayChangeListenerOps dcl_ops = {
925 .dpy_name = "sdl",
926 .dpy_gfx_update = sdl_update,
927 .dpy_gfx_switch = sdl_switch,
928 .dpy_gfx_check_format = sdl_check_format,
929 .dpy_refresh = sdl_refresh,
930 .dpy_mouse_set = sdl_mouse_warp,
931 .dpy_cursor_define = sdl_mouse_define,
934 void sdl_display_early_init(int opengl)
936 if (opengl == 1 /* on */) {
937 fprintf(stderr,
938 "SDL1 display code has no opengl support.\n"
939 "Please recompile qemu with SDL2, using\n"
940 "./configure --enable-sdl --with-sdlabi=2.0\n");
944 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
946 int flags;
947 uint8_t data = 0;
948 const SDL_VideoInfo *vi;
949 char *filename;
951 #if defined(__APPLE__)
952 /* always use generic keymaps */
953 if (!keyboard_layout)
954 keyboard_layout = "en-us";
955 #endif
956 if(keyboard_layout) {
957 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
958 if (!kbd_layout)
959 exit(1);
962 if (no_frame)
963 gui_noframe = 1;
965 if (!full_screen) {
966 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
968 #ifdef __linux__
969 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
970 * accessible $DISPLAY to open X11 window. This is often the case
971 * when qemu is run using sudo. But in this case, and when actually
972 * run in X11 environment, SDL fights with X11 for the video card,
973 * making current display unavailable, often until reboot.
974 * So make x11 the default SDL video driver if this variable is unset.
975 * This is a bit hackish but saves us from bigger problem.
976 * Maybe it's a good idea to fix this in SDL instead.
978 setenv("SDL_VIDEODRIVER", "x11", 0);
979 #endif
981 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
982 * This requires SDL >= 1.2.14. */
983 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
985 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
986 if (SDL_Init (flags)) {
987 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
988 SDL_GetError());
989 exit(1);
991 vi = SDL_GetVideoInfo();
992 host_format = *(vi->vfmt);
994 /* Load a 32x32x4 image. White pixels are transparent. */
995 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
996 if (filename) {
997 SDL_Surface *image = SDL_LoadBMP(filename);
998 if (image) {
999 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
1000 SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
1001 SDL_WM_SetIcon(image, NULL);
1003 g_free(filename);
1006 if (full_screen) {
1007 gui_fullscreen = 1;
1008 sdl_grab_start();
1011 dcl = g_new0(DisplayChangeListener, 1);
1012 dcl->ops = &dcl_ops;
1013 register_displaychangelistener(dcl);
1015 mouse_mode_notifier.notify = sdl_mouse_mode_change;
1016 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
1018 sdl_update_caption();
1019 SDL_EnableKeyRepeat(250, 50);
1020 gui_grab = 0;
1022 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
1023 sdl_cursor_normal = SDL_GetCursor();
1025 atexit(sdl_cleanup);