include/fpu/softfloat: implement float16_chs helper
[qemu/kevin.git] / ui / sdl.c
blob963cdf77a7cb5f21e3a36305f4b287f5e39463d4
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 "qemu/cutils.h"
34 #include "ui/console.h"
35 #include "ui/input.h"
36 #include "sysemu/sysemu.h"
37 #ifndef WIN32
38 #include "x_keymap.h"
39 #endif
40 #include "sdl_zoom.h"
42 static DisplayChangeListener *dcl;
43 static DisplaySurface *surface;
44 static DisplayOptions *opts;
45 static SDL_Surface *real_screen;
46 static SDL_Surface *guest_screen = NULL;
47 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
48 static int last_vm_running;
49 static bool gui_saved_scaling;
50 static int gui_saved_width;
51 static int gui_saved_height;
52 static int gui_saved_grab;
53 static int gui_fullscreen;
54 static int gui_key_modifier_pressed;
55 static int gui_keysym;
56 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
57 static uint8_t modifiers_state[256];
58 static SDL_Cursor *sdl_cursor_normal;
59 static SDL_Cursor *sdl_cursor_hidden;
60 static int absolute_enabled = 0;
61 static int guest_cursor = 0;
62 static int guest_x, guest_y;
63 static SDL_Cursor *guest_sprite = NULL;
64 static SDL_PixelFormat host_format;
65 static int scaling_active = 0;
66 static Notifier mouse_mode_notifier;
67 static int idle_counter;
68 static const guint16 *keycode_map;
69 static size_t keycode_maplen;
71 #define SDL_REFRESH_INTERVAL_BUSY 10
72 #define SDL_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
73 / SDL_REFRESH_INTERVAL_BUSY + 1)
75 #if 0
76 #define DEBUG_SDL
77 #endif
79 static void sdl_update(DisplayChangeListener *dcl,
80 int x, int y, int w, int h)
82 SDL_Rect rec;
83 rec.x = x;
84 rec.y = y;
85 rec.w = w;
86 rec.h = h;
88 #ifdef DEBUG_SDL
89 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
90 x, y, w, h, scaling_active);
91 #endif
93 if (guest_screen) {
94 if (!scaling_active) {
95 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
96 } else {
97 if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) {
98 fprintf(stderr, "Zoom blit failed\n");
99 exit(1);
103 SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h);
106 static void do_sdl_resize(int width, int height, int bpp)
108 int flags;
109 SDL_Surface *tmp_screen;
111 #ifdef DEBUG_SDL
112 printf("SDL: Resizing to %dx%d bpp %d\n", width, height, bpp);
113 #endif
115 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
116 if (gui_fullscreen) {
117 flags |= SDL_FULLSCREEN;
118 } else {
119 flags |= SDL_RESIZABLE;
121 if (no_frame) {
122 flags |= SDL_NOFRAME;
125 tmp_screen = SDL_SetVideoMode(width, height, bpp, flags);
126 if (!real_screen) {
127 if (!tmp_screen) {
128 fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n",
129 width, height, bpp, SDL_GetError());
130 exit(1);
132 } else {
134 * Revert to the previous video mode if the change of resizing or
135 * resolution failed.
137 if (!tmp_screen) {
138 fprintf(stderr, "Failed to set SDL display (%dx%dx%d): %s\n",
139 width, height, bpp, SDL_GetError());
140 return;
144 real_screen = tmp_screen;
147 static void sdl_switch(DisplayChangeListener *dcl,
148 DisplaySurface *new_surface)
150 PixelFormat pf;
152 /* temporary hack: allows to call sdl_switch to handle scaling changes */
153 if (new_surface) {
154 surface = new_surface;
156 pf = qemu_pixelformat_from_pixman(surface->format);
158 if (!scaling_active) {
159 do_sdl_resize(surface_width(surface), surface_height(surface), 0);
160 } else if (real_screen->format->BitsPerPixel !=
161 surface_bits_per_pixel(surface)) {
162 do_sdl_resize(real_screen->w, real_screen->h,
163 surface_bits_per_pixel(surface));
166 if (guest_screen != NULL) {
167 SDL_FreeSurface(guest_screen);
170 #ifdef DEBUG_SDL
171 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
172 pf.rmask, pf.gmask, pf.bmask, pf.amask);
173 #endif
175 guest_screen = SDL_CreateRGBSurfaceFrom
176 (surface_data(surface),
177 surface_width(surface), surface_height(surface),
178 surface_bits_per_pixel(surface), surface_stride(surface),
179 pf.rmask, pf.gmask,
180 pf.bmask, pf.amask);
183 static bool sdl_check_format(DisplayChangeListener *dcl,
184 pixman_format_code_t format)
187 * We let SDL convert for us a few more formats than,
188 * the native ones. Thes are the ones I have tested.
190 return (format == PIXMAN_x8r8g8b8 ||
191 format == PIXMAN_b8g8r8x8 ||
192 format == PIXMAN_x1r5g5b5 ||
193 format == PIXMAN_r5g6b5);
196 /* generic keyboard conversion */
198 #include "sdl_keysym.h"
200 static kbd_layout_t *kbd_layout = NULL;
202 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
204 int keysym;
205 /* workaround for X11+SDL bug with AltGR */
206 keysym = ev->keysym.sym;
207 if (keysym == 0 && ev->keysym.scancode == 113)
208 keysym = SDLK_MODE;
209 /* For Japanese key '\' and '|' */
210 if (keysym == 92 && ev->keysym.scancode == 133) {
211 keysym = 0xa5;
213 return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
217 static const guint16 *sdl_get_keymap(size_t *maplen)
219 #if defined(WIN32)
220 *maplen = qemu_input_map_atset1_to_qcode_len;
221 return qemu_input_map_atset1_to_qcode;
222 #else
223 #if defined(SDL_VIDEO_DRIVER_X11)
224 SDL_SysWMinfo info;
226 SDL_VERSION(&info.version);
227 if (SDL_GetWMInfo(&info) > 0) {
228 return qemu_xkeymap_mapping_table(
229 info.info.x11.display, maplen);
231 #endif
232 g_warning("Unsupported SDL video driver / platform.\n"
233 "Assuming Linux KBD scancodes, but probably wrong.\n"
234 "Please report to qemu-devel@nongnu.org\n"
235 "including the following information:\n"
236 "\n"
237 " - Operating system\n"
238 " - SDL video driver\n");
239 *maplen = qemu_input_map_xorgkbd_to_qcode_len;
240 return qemu_input_map_xorgkbd_to_qcode;
241 #endif
244 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
246 int qcode;
247 if (!keycode_map) {
248 return 0;
250 if (ev->keysym.scancode > keycode_maplen) {
251 return 0;
254 qcode = keycode_map[ev->keysym.scancode];
256 if (qcode > qemu_input_map_qcode_to_qnum_len) {
257 return 0;
260 return qemu_input_map_qcode_to_qnum[qcode];
263 static void reset_keys(void)
265 int i;
266 for(i = 0; i < 256; i++) {
267 if (modifiers_state[i]) {
268 qemu_input_event_send_key_number(dcl->con, i, false);
269 modifiers_state[i] = 0;
274 static void sdl_process_key(SDL_KeyboardEvent *ev)
276 int keycode;
278 if (ev->keysym.sym == SDLK_PAUSE) {
279 /* specific case */
280 qemu_input_event_send_key_qcode(dcl->con, Q_KEY_CODE_PAUSE,
281 ev->type == SDL_KEYDOWN);
282 return;
285 if (kbd_layout) {
286 keycode = sdl_keyevent_to_keycode_generic(ev);
287 } else {
288 keycode = sdl_keyevent_to_keycode(ev);
291 switch(keycode) {
292 case 0x00:
293 /* sent when leaving window: reset the modifiers state */
294 reset_keys();
295 return;
296 case 0x2a: /* Left Shift */
297 case 0x36: /* Right Shift */
298 case 0x1d: /* Left CTRL */
299 case 0x9d: /* Right CTRL */
300 case 0x38: /* Left ALT */
301 case 0xb8: /* Right ALT */
302 if (ev->type == SDL_KEYUP)
303 modifiers_state[keycode] = 0;
304 else
305 modifiers_state[keycode] = 1;
306 break;
307 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
308 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
309 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
310 case 0x45: /* num lock */
311 case 0x3a: /* caps lock */
312 /* SDL does not send the key up event, so we generate it */
313 qemu_input_event_send_key_number(dcl->con, keycode, true);
314 qemu_input_event_send_key_number(dcl->con, keycode, false);
315 return;
316 #endif
319 /* now send the key code */
320 qemu_input_event_send_key_number(dcl->con, keycode,
321 ev->type == SDL_KEYDOWN);
324 static void sdl_update_caption(void)
326 char win_title[1024];
327 char icon_title[1024];
328 const char *status = "";
330 if (!runstate_is_running())
331 status = " [Stopped]";
332 else if (gui_grab) {
333 if (alt_grab)
334 status = " - Press Ctrl-Alt-Shift-G to exit mouse grab";
335 else if (ctrl_grab)
336 status = " - Press Right-Ctrl-G to exit mouse grab";
337 else
338 status = " - Press Ctrl-Alt-G to exit mouse grab";
341 if (qemu_name) {
342 snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
343 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
344 } else {
345 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
346 snprintf(icon_title, sizeof(icon_title), "QEMU");
349 SDL_WM_SetCaption(win_title, icon_title);
352 static void sdl_hide_cursor(void)
354 if (!cursor_hide)
355 return;
357 if (qemu_input_is_absolute()) {
358 SDL_ShowCursor(1);
359 SDL_SetCursor(sdl_cursor_hidden);
360 } else {
361 SDL_ShowCursor(0);
365 static void sdl_show_cursor(void)
367 if (!cursor_hide)
368 return;
370 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL)) {
371 SDL_ShowCursor(1);
372 if (guest_cursor &&
373 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
374 SDL_SetCursor(guest_sprite);
375 else
376 SDL_SetCursor(sdl_cursor_normal);
380 static void sdl_grab_start(void)
383 * If the application is not active, do not try to enter grab state. This
384 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
385 * application (SDL bug).
387 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) {
388 return;
390 if (guest_cursor) {
391 SDL_SetCursor(guest_sprite);
392 if (!qemu_input_is_absolute() && !absolute_enabled) {
393 SDL_WarpMouse(guest_x, guest_y);
395 } else
396 sdl_hide_cursor();
397 SDL_WM_GrabInput(SDL_GRAB_ON);
398 gui_grab = 1;
399 sdl_update_caption();
402 static void sdl_grab_end(void)
404 SDL_WM_GrabInput(SDL_GRAB_OFF);
405 gui_grab = 0;
406 sdl_show_cursor();
407 sdl_update_caption();
410 static void absolute_mouse_grab(void)
412 int mouse_x, mouse_y;
414 SDL_GetMouseState(&mouse_x, &mouse_y);
415 if (mouse_x > 0 && mouse_x < real_screen->w - 1 &&
416 mouse_y > 0 && mouse_y < real_screen->h - 1) {
417 sdl_grab_start();
421 static void sdl_mouse_mode_change(Notifier *notify, void *data)
423 if (qemu_input_is_absolute()) {
424 if (!absolute_enabled) {
425 absolute_enabled = 1;
426 if (qemu_console_is_graphic(NULL)) {
427 absolute_mouse_grab();
430 } else if (absolute_enabled) {
431 if (!gui_fullscreen) {
432 sdl_grab_end();
434 absolute_enabled = 0;
438 static void sdl_send_mouse_event(int dx, int dy, int x, int y, int state)
440 static uint32_t bmap[INPUT_BUTTON__MAX] = {
441 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
442 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
443 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
444 [INPUT_BUTTON_WHEEL_UP] = SDL_BUTTON(SDL_BUTTON_WHEELUP),
445 [INPUT_BUTTON_WHEEL_DOWN] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN),
447 static uint32_t prev_state;
449 if (prev_state != state) {
450 qemu_input_update_buttons(dcl->con, bmap, prev_state, state);
451 prev_state = state;
454 if (qemu_input_is_absolute()) {
455 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, x,
456 0, real_screen->w);
457 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, y,
458 0, real_screen->h);
459 } else {
460 if (guest_cursor) {
461 x -= guest_x;
462 y -= guest_y;
463 guest_x += x;
464 guest_y += y;
465 dx = x;
466 dy = y;
468 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, dx);
469 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, dy);
471 qemu_input_event_sync();
474 static void sdl_scale(int width, int height)
476 int bpp = real_screen->format->BitsPerPixel;
478 #ifdef DEBUG_SDL
479 printf("SDL: Scaling to %dx%d bpp %d\n", width, height, bpp);
480 #endif
482 if (bpp != 16 && bpp != 32) {
483 bpp = 32;
485 do_sdl_resize(width, height, bpp);
486 scaling_active = 1;
489 static void toggle_full_screen(void)
491 int width = surface_width(surface);
492 int height = surface_height(surface);
493 int bpp = surface_bits_per_pixel(surface);
495 gui_fullscreen = !gui_fullscreen;
496 if (gui_fullscreen) {
497 gui_saved_width = real_screen->w;
498 gui_saved_height = real_screen->h;
499 gui_saved_scaling = scaling_active;
501 do_sdl_resize(width, height, bpp);
502 scaling_active = 0;
504 gui_saved_grab = gui_grab;
505 sdl_grab_start();
506 } else {
507 if (gui_saved_scaling) {
508 sdl_scale(gui_saved_width, gui_saved_height);
509 } else {
510 do_sdl_resize(width, height, 0);
512 if (!gui_saved_grab || !qemu_console_is_graphic(NULL)) {
513 sdl_grab_end();
516 graphic_hw_invalidate(NULL);
517 graphic_hw_update(NULL);
520 static void handle_keydown(SDL_Event *ev)
522 int mod_state;
523 int keycode;
525 if (alt_grab) {
526 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
527 (gui_grab_code | KMOD_LSHIFT);
528 } else if (ctrl_grab) {
529 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
530 } else {
531 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
533 gui_key_modifier_pressed = mod_state;
535 if (gui_key_modifier_pressed) {
536 keycode = sdl_keyevent_to_keycode(&ev->key);
537 switch (keycode) {
538 case 0x21: /* 'f' key on US keyboard */
539 toggle_full_screen();
540 gui_keysym = 1;
541 break;
542 case 0x22: /* 'g' key */
543 if (!gui_grab) {
544 if (qemu_console_is_graphic(NULL)) {
545 sdl_grab_start();
547 } else if (!gui_fullscreen) {
548 sdl_grab_end();
550 gui_keysym = 1;
551 break;
552 case 0x16: /* 'u' key on US keyboard */
553 if (scaling_active) {
554 scaling_active = 0;
555 sdl_switch(dcl, NULL);
556 graphic_hw_invalidate(NULL);
557 graphic_hw_update(NULL);
559 gui_keysym = 1;
560 break;
561 case 0x02 ... 0x0a: /* '1' to '9' keys */
562 /* Reset the modifiers sent to the current console */
563 reset_keys();
564 console_select(keycode - 0x02);
565 gui_keysym = 1;
566 if (gui_fullscreen) {
567 break;
569 if (!qemu_console_is_graphic(NULL)) {
570 /* release grab if going to a text console */
571 if (gui_grab) {
572 sdl_grab_end();
573 } else if (absolute_enabled) {
574 sdl_show_cursor();
576 } else if (absolute_enabled) {
577 sdl_hide_cursor();
578 absolute_mouse_grab();
580 break;
581 case 0x1b: /* '+' */
582 case 0x35: /* '-' */
583 if (!gui_fullscreen) {
584 int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50),
585 160);
586 int height = (surface_height(surface) * width) /
587 surface_width(surface);
589 sdl_scale(width, height);
590 graphic_hw_invalidate(NULL);
591 graphic_hw_update(NULL);
592 gui_keysym = 1;
594 default:
595 break;
597 } else if (!qemu_console_is_graphic(NULL)) {
598 int keysym = 0;
600 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
601 switch (ev->key.keysym.sym) {
602 case SDLK_UP:
603 keysym = QEMU_KEY_CTRL_UP;
604 break;
605 case SDLK_DOWN:
606 keysym = QEMU_KEY_CTRL_DOWN;
607 break;
608 case SDLK_LEFT:
609 keysym = QEMU_KEY_CTRL_LEFT;
610 break;
611 case SDLK_RIGHT:
612 keysym = QEMU_KEY_CTRL_RIGHT;
613 break;
614 case SDLK_HOME:
615 keysym = QEMU_KEY_CTRL_HOME;
616 break;
617 case SDLK_END:
618 keysym = QEMU_KEY_CTRL_END;
619 break;
620 case SDLK_PAGEUP:
621 keysym = QEMU_KEY_CTRL_PAGEUP;
622 break;
623 case SDLK_PAGEDOWN:
624 keysym = QEMU_KEY_CTRL_PAGEDOWN;
625 break;
626 default:
627 break;
629 } else {
630 switch (ev->key.keysym.sym) {
631 case SDLK_UP:
632 keysym = QEMU_KEY_UP;
633 break;
634 case SDLK_DOWN:
635 keysym = QEMU_KEY_DOWN;
636 break;
637 case SDLK_LEFT:
638 keysym = QEMU_KEY_LEFT;
639 break;
640 case SDLK_RIGHT:
641 keysym = QEMU_KEY_RIGHT;
642 break;
643 case SDLK_HOME:
644 keysym = QEMU_KEY_HOME;
645 break;
646 case SDLK_END:
647 keysym = QEMU_KEY_END;
648 break;
649 case SDLK_PAGEUP:
650 keysym = QEMU_KEY_PAGEUP;
651 break;
652 case SDLK_PAGEDOWN:
653 keysym = QEMU_KEY_PAGEDOWN;
654 break;
655 case SDLK_BACKSPACE:
656 keysym = QEMU_KEY_BACKSPACE;
657 break;
658 case SDLK_DELETE:
659 keysym = QEMU_KEY_DELETE;
660 break;
661 default:
662 break;
665 if (keysym) {
666 kbd_put_keysym(keysym);
667 } else if (ev->key.keysym.unicode != 0) {
668 kbd_put_keysym(ev->key.keysym.unicode);
671 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
672 sdl_process_key(&ev->key);
676 static void handle_keyup(SDL_Event *ev)
678 int mod_state;
680 if (!alt_grab) {
681 mod_state = (ev->key.keysym.mod & gui_grab_code);
682 } else {
683 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
685 if (!mod_state && gui_key_modifier_pressed) {
686 gui_key_modifier_pressed = 0;
687 gui_keysym = 0;
689 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
690 sdl_process_key(&ev->key);
694 static void handle_mousemotion(SDL_Event *ev)
696 int max_x, max_y;
698 if (qemu_console_is_graphic(NULL) &&
699 (qemu_input_is_absolute() || absolute_enabled)) {
700 max_x = real_screen->w - 1;
701 max_y = real_screen->h - 1;
702 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
703 ev->motion.x == max_x || ev->motion.y == max_y)) {
704 sdl_grab_end();
706 if (!gui_grab &&
707 (ev->motion.x > 0 && ev->motion.x < max_x &&
708 ev->motion.y > 0 && ev->motion.y < max_y)) {
709 sdl_grab_start();
712 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
713 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel,
714 ev->motion.x, ev->motion.y, ev->motion.state);
718 static void handle_mousebutton(SDL_Event *ev)
720 int buttonstate = SDL_GetMouseState(NULL, NULL);
721 SDL_MouseButtonEvent *bev;
723 if (!qemu_console_is_graphic(NULL)) {
724 return;
727 bev = &ev->button;
728 if (!gui_grab && !qemu_input_is_absolute()) {
729 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
730 /* start grabbing all events */
731 sdl_grab_start();
733 } else {
734 if (ev->type == SDL_MOUSEBUTTONDOWN) {
735 buttonstate |= SDL_BUTTON(bev->button);
736 } else {
737 buttonstate &= ~SDL_BUTTON(bev->button);
739 sdl_send_mouse_event(0, 0, bev->x, bev->y, buttonstate);
743 static void handle_activation(SDL_Event *ev)
745 #ifdef _WIN32
746 /* Disable grab if the window no longer has the focus
747 * (Windows-only workaround) */
748 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
749 !ev->active.gain && !gui_fullscreen) {
750 sdl_grab_end();
752 #endif
753 if (!gui_grab && ev->active.gain && qemu_console_is_graphic(NULL) &&
754 (qemu_input_is_absolute() || absolute_enabled)) {
755 absolute_mouse_grab();
757 if (ev->active.state & SDL_APPACTIVE) {
758 if (ev->active.gain) {
759 /* Back to default interval */
760 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
761 } else {
762 /* Sleeping interval. Not using the long default here as
763 * sdl_refresh does not only update the guest screen, but
764 * also checks for gui events. */
765 update_displaychangelistener(dcl, 500);
770 static void sdl_refresh(DisplayChangeListener *dcl)
772 SDL_Event ev1, *ev = &ev1;
773 bool allow_close = true;
774 int idle = 1;
776 if (last_vm_running != runstate_is_running()) {
777 last_vm_running = runstate_is_running();
778 sdl_update_caption();
781 graphic_hw_update(NULL);
782 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL));
784 while (SDL_PollEvent(ev)) {
785 switch (ev->type) {
786 case SDL_VIDEOEXPOSE:
787 sdl_update(dcl, 0, 0, real_screen->w, real_screen->h);
788 break;
789 case SDL_KEYDOWN:
790 idle = 0;
791 handle_keydown(ev);
792 break;
793 case SDL_KEYUP:
794 idle = 0;
795 handle_keyup(ev);
796 break;
797 case SDL_QUIT:
798 if (opts->has_window_close && !opts->window_close) {
799 allow_close = false;
801 if (allow_close) {
802 no_shutdown = 0;
803 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
805 break;
806 case SDL_MOUSEMOTION:
807 idle = 0;
808 handle_mousemotion(ev);
809 break;
810 case SDL_MOUSEBUTTONDOWN:
811 case SDL_MOUSEBUTTONUP:
812 idle = 0;
813 handle_mousebutton(ev);
814 break;
815 case SDL_ACTIVEEVENT:
816 handle_activation(ev);
817 break;
818 case SDL_VIDEORESIZE:
819 sdl_scale(ev->resize.w, ev->resize.h);
820 graphic_hw_invalidate(NULL);
821 graphic_hw_update(NULL);
822 break;
823 default:
824 break;
828 if (idle) {
829 if (idle_counter < SDL_MAX_IDLE_COUNT) {
830 idle_counter++;
831 if (idle_counter >= SDL_MAX_IDLE_COUNT) {
832 dcl->update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
835 } else {
836 idle_counter = 0;
837 dcl->update_interval = SDL_REFRESH_INTERVAL_BUSY;
841 static void sdl_mouse_warp(DisplayChangeListener *dcl,
842 int x, int y, int on)
844 if (on) {
845 if (!guest_cursor)
846 sdl_show_cursor();
847 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
848 SDL_SetCursor(guest_sprite);
849 if (!qemu_input_is_absolute() && !absolute_enabled) {
850 SDL_WarpMouse(x, y);
853 } else if (gui_grab)
854 sdl_hide_cursor();
855 guest_cursor = on;
856 guest_x = x, guest_y = y;
859 static void sdl_mouse_define(DisplayChangeListener *dcl,
860 QEMUCursor *c)
862 uint8_t *image, *mask;
863 int bpl;
865 if (guest_sprite)
866 SDL_FreeCursor(guest_sprite);
868 bpl = cursor_get_mono_bpl(c);
869 image = g_malloc0(bpl * c->height);
870 mask = g_malloc0(bpl * c->height);
871 cursor_get_mono_image(c, 0x000000, image);
872 cursor_get_mono_mask(c, 0, mask);
873 guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
874 c->hot_x, c->hot_y);
875 g_free(image);
876 g_free(mask);
878 if (guest_cursor &&
879 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
880 SDL_SetCursor(guest_sprite);
883 static void sdl_cleanup(void)
885 if (guest_sprite)
886 SDL_FreeCursor(guest_sprite);
887 SDL_QuitSubSystem(SDL_INIT_VIDEO);
890 static const DisplayChangeListenerOps dcl_ops = {
891 .dpy_name = "sdl",
892 .dpy_gfx_update = sdl_update,
893 .dpy_gfx_switch = sdl_switch,
894 .dpy_gfx_check_format = sdl_check_format,
895 .dpy_refresh = sdl_refresh,
896 .dpy_mouse_set = sdl_mouse_warp,
897 .dpy_cursor_define = sdl_mouse_define,
900 void sdl_display_early_init(DisplayOptions *opts)
902 if (opts->has_gl && opts->gl) {
903 fprintf(stderr,
904 "SDL1 display code has no opengl support.\n"
905 "Please recompile qemu with SDL2, using\n"
906 "./configure --enable-sdl --with-sdlabi=2.0\n");
910 void sdl_display_init(DisplayState *ds, DisplayOptions *o)
912 int flags;
913 uint8_t data = 0;
914 const SDL_VideoInfo *vi;
915 SDL_SysWMinfo info;
916 char *filename;
918 assert(o->type == DISPLAY_TYPE_SDL);
919 opts = o;
920 #if defined(__APPLE__)
921 /* always use generic keymaps */
922 if (!keyboard_layout)
923 keyboard_layout = "en-us";
924 #endif
925 if(keyboard_layout) {
926 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
927 if (!kbd_layout)
928 exit(1);
931 g_printerr("Running QEMU with SDL 1.2 is deprecated, and will be removed\n"
932 "in a future release. Please switch to SDL 2.0 instead\n");
934 if (opts->has_full_screen && opts->full_screen) {
935 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
937 #ifdef __linux__
938 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
939 * accessible $DISPLAY to open X11 window. This is often the case
940 * when qemu is run using sudo. But in this case, and when actually
941 * run in X11 environment, SDL fights with X11 for the video card,
942 * making current display unavailable, often until reboot.
943 * So make x11 the default SDL video driver if this variable is unset.
944 * This is a bit hackish but saves us from bigger problem.
945 * Maybe it's a good idea to fix this in SDL instead.
947 setenv("SDL_VIDEODRIVER", "x11", 0);
948 #endif
950 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
951 * This requires SDL >= 1.2.14. */
952 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
954 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
955 if (SDL_Init (flags)) {
956 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
957 SDL_GetError());
958 exit(1);
960 vi = SDL_GetVideoInfo();
961 host_format = *(vi->vfmt);
963 keycode_map = sdl_get_keymap(&keycode_maplen);
965 /* Load a 32x32x4 image. White pixels are transparent. */
966 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
967 if (filename) {
968 SDL_Surface *image = SDL_LoadBMP(filename);
969 if (image) {
970 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
971 SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
972 SDL_WM_SetIcon(image, NULL);
974 g_free(filename);
977 if (opts->has_full_screen && opts->full_screen) {
978 gui_fullscreen = 1;
979 sdl_grab_start();
982 dcl = g_new0(DisplayChangeListener, 1);
983 dcl->ops = &dcl_ops;
984 register_displaychangelistener(dcl);
986 mouse_mode_notifier.notify = sdl_mouse_mode_change;
987 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
989 sdl_update_caption();
990 SDL_EnableKeyRepeat(250, 50);
991 gui_grab = 0;
993 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
994 sdl_cursor_normal = SDL_GetCursor();
996 memset(&info, 0, sizeof(info));
997 SDL_VERSION(&info.version);
998 if (SDL_GetWMInfo(&info)) {
999 int i;
1000 for (i = 0; ; i++) {
1001 /* All consoles share the same window */
1002 QemuConsole *con = qemu_console_lookup_by_index(i);
1003 if (con) {
1004 #if defined(SDL_VIDEO_DRIVER_X11)
1005 qemu_console_set_window_id(con, info.info.x11.wmwindow);
1006 #elif defined(SDL_VIDEO_DRIVER_NANOX) || \
1007 defined(SDL_VIDEO_DRIVER_WINDIB) || defined(SDL_VIDEO_DRIVER_DDRAW) || \
1008 defined(SDL_VIDEO_DRIVER_GAPI) || \
1009 defined(SDL_VIDEO_DRIVER_RISCOS)
1010 qemu_console_set_window_id(con, (int) (uintptr_t) info.window);
1011 #else
1012 qemu_console_set_window_id(con, info.data);
1013 #endif
1014 } else {
1015 break;
1020 atexit(sdl_cleanup);