s390-virtio: Check for NULL device in reset hypercall
[qemu/agraf.git] / ui / sdl.c
blob1657848e9f5d696da82ce5a6c103edc5e48a8384
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 <SDL.h>
29 #include <SDL_syswm.h>
31 #include "qemu-common.h"
32 #include "ui/console.h"
33 #include "sysemu/sysemu.h"
34 #include "x_keymap.h"
35 #include "sdl_zoom.h"
37 static DisplayChangeListener *dcl;
38 static SDL_Surface *real_screen;
39 static SDL_Surface *guest_screen = NULL;
40 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
41 static int last_vm_running;
42 static bool gui_saved_scaling;
43 static int gui_saved_width;
44 static int gui_saved_height;
45 static int gui_saved_grab;
46 static int gui_fullscreen;
47 static int gui_noframe;
48 static int gui_key_modifier_pressed;
49 static int gui_keysym;
50 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
51 static uint8_t modifiers_state[256];
52 static SDL_Cursor *sdl_cursor_normal;
53 static SDL_Cursor *sdl_cursor_hidden;
54 static int absolute_enabled = 0;
55 static int guest_cursor = 0;
56 static int guest_x, guest_y;
57 static SDL_Cursor *guest_sprite = NULL;
58 static SDL_PixelFormat host_format;
59 static int scaling_active = 0;
60 static Notifier mouse_mode_notifier;
62 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
64 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
65 SDL_Rect rec;
66 rec.x = x;
67 rec.y = y;
68 rec.w = w;
69 rec.h = h;
71 if (guest_screen) {
72 if (!scaling_active) {
73 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
74 } else {
75 if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) {
76 fprintf(stderr, "Zoom blit failed\n");
77 exit(1);
81 SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h);
84 static void sdl_setdata(DisplayState *ds)
86 if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
88 guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds),
89 ds_get_bits_per_pixel(ds), ds_get_linesize(ds),
90 ds->surface->pf.rmask, ds->surface->pf.gmask,
91 ds->surface->pf.bmask, ds->surface->pf.amask);
94 static void do_sdl_resize(int width, int height, int bpp)
96 int flags;
98 // printf("resizing to %d %d\n", w, h);
100 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
101 if (gui_fullscreen) {
102 flags |= SDL_FULLSCREEN;
103 } else {
104 flags |= SDL_RESIZABLE;
106 if (gui_noframe)
107 flags |= SDL_NOFRAME;
109 real_screen = SDL_SetVideoMode(width, height, bpp, flags);
110 if (!real_screen) {
111 fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n", width,
112 height, bpp, SDL_GetError());
113 exit(1);
117 static void sdl_resize(DisplayState *ds)
119 if (!scaling_active) {
120 do_sdl_resize(ds_get_width(ds), ds_get_height(ds), 0);
121 } else if (real_screen->format->BitsPerPixel != ds_get_bits_per_pixel(ds)) {
122 do_sdl_resize(real_screen->w, real_screen->h,
123 ds_get_bits_per_pixel(ds));
125 sdl_setdata(ds);
128 /* generic keyboard conversion */
130 #include "sdl_keysym.h"
132 static kbd_layout_t *kbd_layout = NULL;
134 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
136 int keysym;
137 /* workaround for X11+SDL bug with AltGR */
138 keysym = ev->keysym.sym;
139 if (keysym == 0 && ev->keysym.scancode == 113)
140 keysym = SDLK_MODE;
141 /* For Japanese key '\' and '|' */
142 if (keysym == 92 && ev->keysym.scancode == 133) {
143 keysym = 0xa5;
145 return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
148 /* specific keyboard conversions from scan codes */
150 #if defined(_WIN32)
152 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
154 return ev->keysym.scancode;
157 #else
159 #if defined(SDL_VIDEO_DRIVER_X11)
160 #include <X11/XKBlib.h>
162 static int check_for_evdev(void)
164 SDL_SysWMinfo info;
165 XkbDescPtr desc = NULL;
166 int has_evdev = 0;
167 char *keycodes = NULL;
169 SDL_VERSION(&info.version);
170 if (!SDL_GetWMInfo(&info)) {
171 return 0;
173 desc = XkbGetKeyboard(info.info.x11.display,
174 XkbGBN_AllComponentsMask,
175 XkbUseCoreKbd);
176 if (desc && desc->names) {
177 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
178 if (keycodes == NULL) {
179 fprintf(stderr, "could not lookup keycode name\n");
180 } else if (strstart(keycodes, "evdev", NULL)) {
181 has_evdev = 1;
182 } else if (!strstart(keycodes, "xfree86", NULL)) {
183 fprintf(stderr, "unknown keycodes `%s', please report to "
184 "qemu-devel@nongnu.org\n", keycodes);
188 if (desc) {
189 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
191 if (keycodes) {
192 XFree(keycodes);
194 return has_evdev;
196 #else
197 static int check_for_evdev(void)
199 return 0;
201 #endif
203 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
205 int keycode;
206 static int has_evdev = -1;
208 if (has_evdev == -1)
209 has_evdev = check_for_evdev();
211 keycode = ev->keysym.scancode;
213 if (keycode < 9) {
214 keycode = 0;
215 } else if (keycode < 97) {
216 keycode -= 8; /* just an offset */
217 } else if (keycode < 158) {
218 /* use conversion table */
219 if (has_evdev)
220 keycode = translate_evdev_keycode(keycode - 97);
221 else
222 keycode = translate_xfree86_keycode(keycode - 97);
223 } else if (keycode == 208) { /* Hiragana_Katakana */
224 keycode = 0x70;
225 } else if (keycode == 211) { /* backslash */
226 keycode = 0x73;
227 } else {
228 keycode = 0;
230 return keycode;
233 #endif
235 static void reset_keys(void)
237 int i;
238 for(i = 0; i < 256; i++) {
239 if (modifiers_state[i]) {
240 if (i & SCANCODE_GREY)
241 kbd_put_keycode(SCANCODE_EMUL0);
242 kbd_put_keycode(i | SCANCODE_UP);
243 modifiers_state[i] = 0;
248 static void sdl_process_key(SDL_KeyboardEvent *ev)
250 int keycode, v;
252 if (ev->keysym.sym == SDLK_PAUSE) {
253 /* specific case */
254 v = 0;
255 if (ev->type == SDL_KEYUP)
256 v |= SCANCODE_UP;
257 kbd_put_keycode(0xe1);
258 kbd_put_keycode(0x1d | v);
259 kbd_put_keycode(0x45 | v);
260 return;
263 if (kbd_layout) {
264 keycode = sdl_keyevent_to_keycode_generic(ev);
265 } else {
266 keycode = sdl_keyevent_to_keycode(ev);
269 switch(keycode) {
270 case 0x00:
271 /* sent when leaving window: reset the modifiers state */
272 reset_keys();
273 return;
274 case 0x2a: /* Left Shift */
275 case 0x36: /* Right Shift */
276 case 0x1d: /* Left CTRL */
277 case 0x9d: /* Right CTRL */
278 case 0x38: /* Left ALT */
279 case 0xb8: /* Right ALT */
280 if (ev->type == SDL_KEYUP)
281 modifiers_state[keycode] = 0;
282 else
283 modifiers_state[keycode] = 1;
284 break;
285 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
286 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
287 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
288 case 0x45: /* num lock */
289 case 0x3a: /* caps lock */
290 /* SDL does not send the key up event, so we generate it */
291 kbd_put_keycode(keycode);
292 kbd_put_keycode(keycode | SCANCODE_UP);
293 return;
294 #endif
297 /* now send the key code */
298 if (keycode & SCANCODE_GREY)
299 kbd_put_keycode(SCANCODE_EMUL0);
300 if (ev->type == SDL_KEYUP)
301 kbd_put_keycode(keycode | SCANCODE_UP);
302 else
303 kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
306 static void sdl_update_caption(void)
308 char win_title[1024];
309 char icon_title[1024];
310 const char *status = "";
312 if (!runstate_is_running())
313 status = " [Stopped]";
314 else if (gui_grab) {
315 if (alt_grab)
316 status = " - Press Ctrl-Alt-Shift to exit mouse grab";
317 else if (ctrl_grab)
318 status = " - Press Right-Ctrl to exit mouse grab";
319 else
320 status = " - Press Ctrl-Alt to exit mouse grab";
323 if (qemu_name) {
324 snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
325 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
326 } else {
327 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
328 snprintf(icon_title, sizeof(icon_title), "QEMU");
331 SDL_WM_SetCaption(win_title, icon_title);
334 static void sdl_hide_cursor(void)
336 if (!cursor_hide)
337 return;
339 if (kbd_mouse_is_absolute()) {
340 SDL_ShowCursor(1);
341 SDL_SetCursor(sdl_cursor_hidden);
342 } else {
343 SDL_ShowCursor(0);
347 static void sdl_show_cursor(void)
349 if (!cursor_hide)
350 return;
352 if (!kbd_mouse_is_absolute() || !is_graphic_console()) {
353 SDL_ShowCursor(1);
354 if (guest_cursor &&
355 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
356 SDL_SetCursor(guest_sprite);
357 else
358 SDL_SetCursor(sdl_cursor_normal);
362 static void sdl_grab_start(void)
365 * If the application is not active, do not try to enter grab state. This
366 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
367 * application (SDL bug).
369 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) {
370 return;
372 if (guest_cursor) {
373 SDL_SetCursor(guest_sprite);
374 if (!kbd_mouse_is_absolute() && !absolute_enabled)
375 SDL_WarpMouse(guest_x, guest_y);
376 } else
377 sdl_hide_cursor();
378 SDL_WM_GrabInput(SDL_GRAB_ON);
379 gui_grab = 1;
380 sdl_update_caption();
383 static void sdl_grab_end(void)
385 SDL_WM_GrabInput(SDL_GRAB_OFF);
386 gui_grab = 0;
387 sdl_show_cursor();
388 sdl_update_caption();
391 static void absolute_mouse_grab(void)
393 int mouse_x, mouse_y;
395 SDL_GetMouseState(&mouse_x, &mouse_y);
396 if (mouse_x > 0 && mouse_x < real_screen->w - 1 &&
397 mouse_y > 0 && mouse_y < real_screen->h - 1) {
398 sdl_grab_start();
402 static void sdl_mouse_mode_change(Notifier *notify, void *data)
404 if (kbd_mouse_is_absolute()) {
405 if (!absolute_enabled) {
406 absolute_enabled = 1;
407 if (is_graphic_console()) {
408 absolute_mouse_grab();
411 } else if (absolute_enabled) {
412 if (!gui_fullscreen) {
413 sdl_grab_end();
415 absolute_enabled = 0;
419 static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
421 int buttons = 0;
423 if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) {
424 buttons |= MOUSE_EVENT_LBUTTON;
426 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
427 buttons |= MOUSE_EVENT_RBUTTON;
429 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) {
430 buttons |= MOUSE_EVENT_MBUTTON;
433 if (kbd_mouse_is_absolute()) {
434 dx = x * 0x7FFF / (real_screen->w - 1);
435 dy = y * 0x7FFF / (real_screen->h - 1);
436 } else if (guest_cursor) {
437 x -= guest_x;
438 y -= guest_y;
439 guest_x += x;
440 guest_y += y;
441 dx = x;
442 dy = y;
445 kbd_mouse_event(dx, dy, dz, buttons);
448 static void sdl_scale(DisplayState *ds, int width, int height)
450 int bpp = real_screen->format->BitsPerPixel;
452 if (bpp != 16 && bpp != 32) {
453 bpp = 32;
455 do_sdl_resize(width, height, bpp);
456 scaling_active = 1;
457 if (!is_buffer_shared(ds->surface)) {
458 ds->surface = qemu_resize_displaysurface(ds, ds_get_width(ds),
459 ds_get_height(ds));
460 dpy_gfx_resize(ds);
464 static void toggle_full_screen(DisplayState *ds)
466 gui_fullscreen = !gui_fullscreen;
467 if (gui_fullscreen) {
468 gui_saved_width = real_screen->w;
469 gui_saved_height = real_screen->h;
470 gui_saved_scaling = scaling_active;
472 do_sdl_resize(ds_get_width(ds), ds_get_height(ds),
473 ds_get_bits_per_pixel(ds));
474 scaling_active = 0;
476 gui_saved_grab = gui_grab;
477 sdl_grab_start();
478 } else {
479 if (gui_saved_scaling) {
480 sdl_scale(ds, gui_saved_width, gui_saved_height);
481 } else {
482 do_sdl_resize(ds_get_width(ds), ds_get_height(ds), 0);
484 if (!gui_saved_grab || !is_graphic_console()) {
485 sdl_grab_end();
488 vga_hw_invalidate();
489 vga_hw_update();
492 static void handle_keydown(DisplayState *ds, SDL_Event *ev)
494 int mod_state;
495 int keycode;
497 if (alt_grab) {
498 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
499 (gui_grab_code | KMOD_LSHIFT);
500 } else if (ctrl_grab) {
501 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
502 } else {
503 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
505 gui_key_modifier_pressed = mod_state;
507 if (gui_key_modifier_pressed) {
508 keycode = sdl_keyevent_to_keycode(&ev->key);
509 switch (keycode) {
510 case 0x21: /* 'f' key on US keyboard */
511 toggle_full_screen(ds);
512 gui_keysym = 1;
513 break;
514 case 0x16: /* 'u' key on US keyboard */
515 if (scaling_active) {
516 scaling_active = 0;
517 sdl_resize(ds);
518 vga_hw_invalidate();
519 vga_hw_update();
521 gui_keysym = 1;
522 break;
523 case 0x02 ... 0x0a: /* '1' to '9' keys */
524 /* Reset the modifiers sent to the current console */
525 reset_keys();
526 console_select(keycode - 0x02);
527 gui_keysym = 1;
528 if (gui_fullscreen) {
529 break;
531 if (!is_graphic_console()) {
532 /* release grab if going to a text console */
533 if (gui_grab) {
534 sdl_grab_end();
535 } else if (absolute_enabled) {
536 sdl_show_cursor();
538 } else if (absolute_enabled) {
539 sdl_hide_cursor();
540 absolute_mouse_grab();
542 break;
543 case 0x1b: /* '+' */
544 case 0x35: /* '-' */
545 if (!gui_fullscreen) {
546 int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50),
547 160);
548 int height = (ds_get_height(ds) * width) / ds_get_width(ds);
550 sdl_scale(ds, width, height);
551 vga_hw_invalidate();
552 vga_hw_update();
553 gui_keysym = 1;
555 default:
556 break;
558 } else if (!is_graphic_console()) {
559 int keysym = 0;
561 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
562 switch (ev->key.keysym.sym) {
563 case SDLK_UP:
564 keysym = QEMU_KEY_CTRL_UP;
565 break;
566 case SDLK_DOWN:
567 keysym = QEMU_KEY_CTRL_DOWN;
568 break;
569 case SDLK_LEFT:
570 keysym = QEMU_KEY_CTRL_LEFT;
571 break;
572 case SDLK_RIGHT:
573 keysym = QEMU_KEY_CTRL_RIGHT;
574 break;
575 case SDLK_HOME:
576 keysym = QEMU_KEY_CTRL_HOME;
577 break;
578 case SDLK_END:
579 keysym = QEMU_KEY_CTRL_END;
580 break;
581 case SDLK_PAGEUP:
582 keysym = QEMU_KEY_CTRL_PAGEUP;
583 break;
584 case SDLK_PAGEDOWN:
585 keysym = QEMU_KEY_CTRL_PAGEDOWN;
586 break;
587 default:
588 break;
590 } else {
591 switch (ev->key.keysym.sym) {
592 case SDLK_UP:
593 keysym = QEMU_KEY_UP;
594 break;
595 case SDLK_DOWN:
596 keysym = QEMU_KEY_DOWN;
597 break;
598 case SDLK_LEFT:
599 keysym = QEMU_KEY_LEFT;
600 break;
601 case SDLK_RIGHT:
602 keysym = QEMU_KEY_RIGHT;
603 break;
604 case SDLK_HOME:
605 keysym = QEMU_KEY_HOME;
606 break;
607 case SDLK_END:
608 keysym = QEMU_KEY_END;
609 break;
610 case SDLK_PAGEUP:
611 keysym = QEMU_KEY_PAGEUP;
612 break;
613 case SDLK_PAGEDOWN:
614 keysym = QEMU_KEY_PAGEDOWN;
615 break;
616 case SDLK_BACKSPACE:
617 keysym = QEMU_KEY_BACKSPACE;
618 break;
619 case SDLK_DELETE:
620 keysym = QEMU_KEY_DELETE;
621 break;
622 default:
623 break;
626 if (keysym) {
627 kbd_put_keysym(keysym);
628 } else if (ev->key.keysym.unicode != 0) {
629 kbd_put_keysym(ev->key.keysym.unicode);
632 if (is_graphic_console() && !gui_keysym) {
633 sdl_process_key(&ev->key);
637 static void handle_keyup(DisplayState *ds, SDL_Event *ev)
639 int mod_state;
641 if (!alt_grab) {
642 mod_state = (ev->key.keysym.mod & gui_grab_code);
643 } else {
644 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
646 if (!mod_state && gui_key_modifier_pressed) {
647 gui_key_modifier_pressed = 0;
648 if (gui_keysym == 0) {
649 /* exit/enter grab if pressing Ctrl-Alt */
650 if (!gui_grab) {
651 if (is_graphic_console()) {
652 sdl_grab_start();
654 } else if (!gui_fullscreen) {
655 sdl_grab_end();
657 /* SDL does not send back all the modifiers key, so we must
658 * correct it. */
659 reset_keys();
660 return;
662 gui_keysym = 0;
664 if (is_graphic_console() && !gui_keysym) {
665 sdl_process_key(&ev->key);
669 static void handle_mousemotion(DisplayState *ds, SDL_Event *ev)
671 int max_x, max_y;
673 if (is_graphic_console() &&
674 (kbd_mouse_is_absolute() || absolute_enabled)) {
675 max_x = real_screen->w - 1;
676 max_y = real_screen->h - 1;
677 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
678 ev->motion.x == max_x || ev->motion.y == max_y)) {
679 sdl_grab_end();
681 if (!gui_grab &&
682 (ev->motion.x > 0 && ev->motion.x < max_x &&
683 ev->motion.y > 0 && ev->motion.y < max_y)) {
684 sdl_grab_start();
687 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
688 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
689 ev->motion.x, ev->motion.y, ev->motion.state);
693 static void handle_mousebutton(DisplayState *ds, SDL_Event *ev)
695 int buttonstate = SDL_GetMouseState(NULL, NULL);
696 SDL_MouseButtonEvent *bev;
697 int dz;
699 if (!is_graphic_console()) {
700 return;
703 bev = &ev->button;
704 if (!gui_grab && !kbd_mouse_is_absolute()) {
705 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
706 /* start grabbing all events */
707 sdl_grab_start();
709 } else {
710 dz = 0;
711 if (ev->type == SDL_MOUSEBUTTONDOWN) {
712 buttonstate |= SDL_BUTTON(bev->button);
713 } else {
714 buttonstate &= ~SDL_BUTTON(bev->button);
716 #ifdef SDL_BUTTON_WHEELUP
717 if (bev->button == SDL_BUTTON_WHEELUP &&
718 ev->type == SDL_MOUSEBUTTONDOWN) {
719 dz = -1;
720 } else if (bev->button == SDL_BUTTON_WHEELDOWN &&
721 ev->type == SDL_MOUSEBUTTONDOWN) {
722 dz = 1;
724 #endif
725 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
729 static void handle_activation(DisplayState *ds, SDL_Event *ev)
731 #ifdef _WIN32
732 /* Disable grab if the window no longer has the focus
733 * (Windows-only workaround) */
734 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
735 !ev->active.gain && !gui_fullscreen) {
736 sdl_grab_end();
738 #endif
739 if (!gui_grab && ev->active.gain && is_graphic_console() &&
740 (kbd_mouse_is_absolute() || absolute_enabled)) {
741 absolute_mouse_grab();
743 if (ev->active.state & SDL_APPACTIVE) {
744 if (ev->active.gain) {
745 /* Back to default interval */
746 dcl->gui_timer_interval = 0;
747 dcl->idle = 0;
748 } else {
749 /* Sleeping interval */
750 dcl->gui_timer_interval = 500;
751 dcl->idle = 1;
756 static void sdl_refresh(DisplayState *ds)
758 SDL_Event ev1, *ev = &ev1;
760 if (last_vm_running != runstate_is_running()) {
761 last_vm_running = runstate_is_running();
762 sdl_update_caption();
765 vga_hw_update();
766 SDL_EnableUNICODE(!is_graphic_console());
768 while (SDL_PollEvent(ev)) {
769 switch (ev->type) {
770 case SDL_VIDEOEXPOSE:
771 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
772 break;
773 case SDL_KEYDOWN:
774 handle_keydown(ds, ev);
775 break;
776 case SDL_KEYUP:
777 handle_keyup(ds, ev);
778 break;
779 case SDL_QUIT:
780 if (!no_quit) {
781 no_shutdown = 0;
782 qemu_system_shutdown_request();
784 break;
785 case SDL_MOUSEMOTION:
786 handle_mousemotion(ds, ev);
787 break;
788 case SDL_MOUSEBUTTONDOWN:
789 case SDL_MOUSEBUTTONUP:
790 handle_mousebutton(ds, ev);
791 break;
792 case SDL_ACTIVEEVENT:
793 handle_activation(ds, ev);
794 break;
795 case SDL_VIDEORESIZE:
796 sdl_scale(ds, ev->resize.w, ev->resize.h);
797 vga_hw_invalidate();
798 vga_hw_update();
799 break;
800 default:
801 break;
806 static void sdl_mouse_warp(DisplayState *ds, int x, int y, int on)
808 if (on) {
809 if (!guest_cursor)
810 sdl_show_cursor();
811 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
812 SDL_SetCursor(guest_sprite);
813 if (!kbd_mouse_is_absolute() && !absolute_enabled)
814 SDL_WarpMouse(x, y);
816 } else if (gui_grab)
817 sdl_hide_cursor();
818 guest_cursor = on;
819 guest_x = x, guest_y = y;
822 static void sdl_mouse_define(DisplayState *ds, QEMUCursor *c)
824 uint8_t *image, *mask;
825 int bpl;
827 if (guest_sprite)
828 SDL_FreeCursor(guest_sprite);
830 bpl = cursor_get_mono_bpl(c);
831 image = g_malloc0(bpl * c->height);
832 mask = g_malloc0(bpl * c->height);
833 cursor_get_mono_image(c, 0x000000, image);
834 cursor_get_mono_mask(c, 0, mask);
835 guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
836 c->hot_x, c->hot_y);
837 g_free(image);
838 g_free(mask);
840 if (guest_cursor &&
841 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
842 SDL_SetCursor(guest_sprite);
845 static void sdl_cleanup(void)
847 if (guest_sprite)
848 SDL_FreeCursor(guest_sprite);
849 SDL_QuitSubSystem(SDL_INIT_VIDEO);
852 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
854 int flags;
855 uint8_t data = 0;
856 const SDL_VideoInfo *vi;
857 char *filename;
859 #if defined(__APPLE__)
860 /* always use generic keymaps */
861 if (!keyboard_layout)
862 keyboard_layout = "en-us";
863 #endif
864 if(keyboard_layout) {
865 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
866 if (!kbd_layout)
867 exit(1);
870 if (no_frame)
871 gui_noframe = 1;
873 if (!full_screen) {
874 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
876 #ifdef __linux__
877 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
878 * accessible $DISPLAY to open X11 window. This is often the case
879 * when qemu is run using sudo. But in this case, and when actually
880 * run in X11 environment, SDL fights with X11 for the video card,
881 * making current display unavailable, often until reboot.
882 * So make x11 the default SDL video driver if this variable is unset.
883 * This is a bit hackish but saves us from bigger problem.
884 * Maybe it's a good idea to fix this in SDL instead.
886 setenv("SDL_VIDEODRIVER", "x11", 0);
887 #endif
889 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
890 * This requires SDL >= 1.2.14. */
891 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
893 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
894 if (SDL_Init (flags)) {
895 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
896 SDL_GetError());
897 exit(1);
899 vi = SDL_GetVideoInfo();
900 host_format = *(vi->vfmt);
902 /* Load a 32x32x4 image. White pixels are transparent. */
903 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
904 if (filename) {
905 SDL_Surface *image = SDL_LoadBMP(filename);
906 if (image) {
907 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
908 SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
909 SDL_WM_SetIcon(image, NULL);
911 g_free(filename);
914 if (full_screen) {
915 gui_fullscreen = 1;
916 sdl_grab_start();
919 dcl = g_malloc0(sizeof(DisplayChangeListener));
920 dcl->dpy_gfx_update = sdl_update;
921 dcl->dpy_gfx_resize = sdl_resize;
922 dcl->dpy_refresh = sdl_refresh;
923 dcl->dpy_gfx_setdata = sdl_setdata;
924 dcl->dpy_mouse_set = sdl_mouse_warp;
925 dcl->dpy_cursor_define = sdl_mouse_define;
926 register_displaychangelistener(ds, dcl);
928 mouse_mode_notifier.notify = sdl_mouse_mode_change;
929 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
931 sdl_update_caption();
932 SDL_EnableKeyRepeat(250, 50);
933 gui_grab = 0;
935 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
936 sdl_cursor_normal = SDL_GetCursor();
938 atexit(sdl_cleanup);