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
25 /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
26 #undef WIN32_LEAN_AND_MEAN
29 #include <SDL_syswm.h>
31 #include "qemu-common.h"
32 #include "ui/console.h"
33 #include "sysemu/sysemu.h"
37 static DisplayChangeListener
*dcl
;
38 static DisplaySurface
*surface
;
39 static SDL_Surface
*real_screen
;
40 static SDL_Surface
*guest_screen
= NULL
;
41 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
42 static int last_vm_running
;
43 static bool gui_saved_scaling
;
44 static int gui_saved_width
;
45 static int gui_saved_height
;
46 static int gui_saved_grab
;
47 static int gui_fullscreen
;
48 static int gui_noframe
;
49 static int gui_key_modifier_pressed
;
50 static int gui_keysym
;
51 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
52 static uint8_t modifiers_state
[256];
53 static SDL_Cursor
*sdl_cursor_normal
;
54 static SDL_Cursor
*sdl_cursor_hidden
;
55 static int absolute_enabled
= 0;
56 static int guest_cursor
= 0;
57 static int guest_x
, guest_y
;
58 static SDL_Cursor
*guest_sprite
= NULL
;
59 static SDL_PixelFormat host_format
;
60 static int scaling_active
= 0;
61 static Notifier mouse_mode_notifier
;
63 static void sdl_update(DisplayChangeListener
*dcl
,
64 int x
, int y
, int w
, int h
)
66 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
74 if (!scaling_active
) {
75 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
77 if (sdl_zoom_blit(guest_screen
, real_screen
, SMOOTHING_ON
, &rec
) < 0) {
78 fprintf(stderr
, "Zoom blit failed\n");
83 SDL_UpdateRect(real_screen
, rec
.x
, rec
.y
, rec
.w
, rec
.h
);
86 static void do_sdl_resize(int width
, int height
, int bpp
)
90 // printf("resizing to %d %d\n", w, h);
92 flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
;
94 flags
|= SDL_FULLSCREEN
;
96 flags
|= SDL_RESIZABLE
;
101 real_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
103 fprintf(stderr
, "Could not open SDL display (%dx%dx%d): %s\n", width
,
104 height
, bpp
, SDL_GetError());
109 static void sdl_switch(DisplayChangeListener
*dcl
,
110 DisplaySurface
*new_surface
)
113 /* temporary hack: allows to call sdl_switch to handle scaling changes */
115 surface
= new_surface
;
118 if (!scaling_active
) {
119 do_sdl_resize(surface_width(surface
), surface_height(surface
), 0);
120 } else if (real_screen
->format
->BitsPerPixel
!=
121 surface_bits_per_pixel(surface
)) {
122 do_sdl_resize(real_screen
->w
, real_screen
->h
,
123 surface_bits_per_pixel(surface
));
126 if (guest_screen
!= NULL
) {
127 SDL_FreeSurface(guest_screen
);
129 guest_screen
= SDL_CreateRGBSurfaceFrom
130 (surface_data(surface
),
131 surface_width(surface
), surface_height(surface
),
132 surface_bits_per_pixel(surface
), surface_stride(surface
),
133 surface
->pf
.rmask
, surface
->pf
.gmask
,
134 surface
->pf
.bmask
, surface
->pf
.amask
);
137 /* generic keyboard conversion */
139 #include "sdl_keysym.h"
141 static kbd_layout_t
*kbd_layout
= NULL
;
143 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
146 /* workaround for X11+SDL bug with AltGR */
147 keysym
= ev
->keysym
.sym
;
148 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
150 /* For Japanese key '\' and '|' */
151 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
154 return keysym2scancode(kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
157 /* specific keyboard conversions from scan codes */
161 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
163 return ev
->keysym
.scancode
;
168 #if defined(SDL_VIDEO_DRIVER_X11)
169 #include <X11/XKBlib.h>
171 static int check_for_evdev(void)
174 XkbDescPtr desc
= NULL
;
176 char *keycodes
= NULL
;
178 SDL_VERSION(&info
.version
);
179 if (!SDL_GetWMInfo(&info
)) {
182 desc
= XkbGetKeyboard(info
.info
.x11
.display
,
183 XkbGBN_AllComponentsMask
,
185 if (desc
&& desc
->names
) {
186 keycodes
= XGetAtomName(info
.info
.x11
.display
, desc
->names
->keycodes
);
187 if (keycodes
== NULL
) {
188 fprintf(stderr
, "could not lookup keycode name\n");
189 } else if (strstart(keycodes
, "evdev", NULL
)) {
191 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
192 fprintf(stderr
, "unknown keycodes `%s', please report to "
193 "qemu-devel@nongnu.org\n", keycodes
);
198 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
206 static int check_for_evdev(void)
212 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
215 static int has_evdev
= -1;
218 has_evdev
= check_for_evdev();
220 keycode
= ev
->keysym
.scancode
;
224 } else if (keycode
< 97) {
225 keycode
-= 8; /* just an offset */
226 } else if (keycode
< 158) {
227 /* use conversion table */
229 keycode
= translate_evdev_keycode(keycode
- 97);
231 keycode
= translate_xfree86_keycode(keycode
- 97);
232 } else if (keycode
== 208) { /* Hiragana_Katakana */
234 } else if (keycode
== 211) { /* backslash */
244 static void reset_keys(void)
247 for(i
= 0; i
< 256; i
++) {
248 if (modifiers_state
[i
]) {
249 if (i
& SCANCODE_GREY
)
250 kbd_put_keycode(SCANCODE_EMUL0
);
251 kbd_put_keycode(i
| SCANCODE_UP
);
252 modifiers_state
[i
] = 0;
257 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
261 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
264 if (ev
->type
== SDL_KEYUP
)
266 kbd_put_keycode(0xe1);
267 kbd_put_keycode(0x1d | v
);
268 kbd_put_keycode(0x45 | v
);
273 keycode
= sdl_keyevent_to_keycode_generic(ev
);
275 keycode
= sdl_keyevent_to_keycode(ev
);
280 /* sent when leaving window: reset the modifiers state */
283 case 0x2a: /* Left Shift */
284 case 0x36: /* Right Shift */
285 case 0x1d: /* Left CTRL */
286 case 0x9d: /* Right CTRL */
287 case 0x38: /* Left ALT */
288 case 0xb8: /* Right ALT */
289 if (ev
->type
== SDL_KEYUP
)
290 modifiers_state
[keycode
] = 0;
292 modifiers_state
[keycode
] = 1;
294 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
295 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
296 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
297 case 0x45: /* num lock */
298 case 0x3a: /* caps lock */
299 /* SDL does not send the key up event, so we generate it */
300 kbd_put_keycode(keycode
);
301 kbd_put_keycode(keycode
| SCANCODE_UP
);
306 /* now send the key code */
307 if (keycode
& SCANCODE_GREY
)
308 kbd_put_keycode(SCANCODE_EMUL0
);
309 if (ev
->type
== SDL_KEYUP
)
310 kbd_put_keycode(keycode
| SCANCODE_UP
);
312 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
315 static void sdl_update_caption(void)
317 char win_title
[1024];
318 char icon_title
[1024];
319 const char *status
= "";
321 if (!runstate_is_running())
322 status
= " [Stopped]";
325 status
= " - Press Ctrl-Alt-Shift to exit mouse grab";
327 status
= " - Press Right-Ctrl to exit mouse grab";
329 status
= " - Press Ctrl-Alt to exit mouse grab";
333 snprintf(win_title
, sizeof(win_title
), "QEMU (%s)%s", qemu_name
, status
);
334 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
336 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
337 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
340 SDL_WM_SetCaption(win_title
, icon_title
);
343 static void sdl_hide_cursor(void)
348 if (kbd_mouse_is_absolute()) {
350 SDL_SetCursor(sdl_cursor_hidden
);
356 static void sdl_show_cursor(void)
361 if (!kbd_mouse_is_absolute() || !qemu_console_is_graphic(NULL
)) {
364 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
365 SDL_SetCursor(guest_sprite
);
367 SDL_SetCursor(sdl_cursor_normal
);
371 static void sdl_grab_start(void)
374 * If the application is not active, do not try to enter grab state. This
375 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
376 * application (SDL bug).
378 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS
)) {
382 SDL_SetCursor(guest_sprite
);
383 if (!kbd_mouse_is_absolute() && !absolute_enabled
)
384 SDL_WarpMouse(guest_x
, guest_y
);
387 SDL_WM_GrabInput(SDL_GRAB_ON
);
389 sdl_update_caption();
392 static void sdl_grab_end(void)
394 SDL_WM_GrabInput(SDL_GRAB_OFF
);
397 sdl_update_caption();
400 static void absolute_mouse_grab(void)
402 int mouse_x
, mouse_y
;
404 SDL_GetMouseState(&mouse_x
, &mouse_y
);
405 if (mouse_x
> 0 && mouse_x
< real_screen
->w
- 1 &&
406 mouse_y
> 0 && mouse_y
< real_screen
->h
- 1) {
411 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
413 if (kbd_mouse_is_absolute()) {
414 if (!absolute_enabled
) {
415 absolute_enabled
= 1;
416 if (qemu_console_is_graphic(NULL
)) {
417 absolute_mouse_grab();
420 } else if (absolute_enabled
) {
421 if (!gui_fullscreen
) {
424 absolute_enabled
= 0;
428 static void sdl_send_mouse_event(int dx
, int dy
, int dz
, int x
, int y
, int state
)
432 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
)) {
433 buttons
|= MOUSE_EVENT_LBUTTON
;
435 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
)) {
436 buttons
|= MOUSE_EVENT_RBUTTON
;
438 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
)) {
439 buttons
|= MOUSE_EVENT_MBUTTON
;
442 if (kbd_mouse_is_absolute()) {
443 dx
= x
* 0x7FFF / (real_screen
->w
- 1);
444 dy
= y
* 0x7FFF / (real_screen
->h
- 1);
445 } else if (guest_cursor
) {
454 kbd_mouse_event(dx
, dy
, dz
, buttons
);
457 static void sdl_scale(int width
, int height
)
459 int bpp
= real_screen
->format
->BitsPerPixel
;
461 if (bpp
!= 16 && bpp
!= 32) {
464 do_sdl_resize(width
, height
, bpp
);
468 static void toggle_full_screen(void)
470 int width
= surface_width(surface
);
471 int height
= surface_height(surface
);
472 int bpp
= surface_bits_per_pixel(surface
);
474 gui_fullscreen
= !gui_fullscreen
;
475 if (gui_fullscreen
) {
476 gui_saved_width
= real_screen
->w
;
477 gui_saved_height
= real_screen
->h
;
478 gui_saved_scaling
= scaling_active
;
480 do_sdl_resize(width
, height
, bpp
);
483 gui_saved_grab
= gui_grab
;
486 if (gui_saved_scaling
) {
487 sdl_scale(gui_saved_width
, gui_saved_height
);
489 do_sdl_resize(width
, height
, 0);
491 if (!gui_saved_grab
|| !qemu_console_is_graphic(NULL
)) {
495 graphic_hw_invalidate(NULL
);
496 graphic_hw_update(NULL
);
499 static void handle_keydown(SDL_Event
*ev
)
505 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
506 (gui_grab_code
| KMOD_LSHIFT
);
507 } else if (ctrl_grab
) {
508 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
510 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
512 gui_key_modifier_pressed
= mod_state
;
514 if (gui_key_modifier_pressed
) {
515 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
517 case 0x21: /* 'f' key on US keyboard */
518 toggle_full_screen();
521 case 0x16: /* 'u' key on US keyboard */
522 if (scaling_active
) {
524 sdl_switch(dcl
, NULL
);
525 graphic_hw_invalidate(NULL
);
526 graphic_hw_update(NULL
);
530 case 0x02 ... 0x0a: /* '1' to '9' keys */
531 /* Reset the modifiers sent to the current console */
533 console_select(keycode
- 0x02);
535 if (gui_fullscreen
) {
538 if (!qemu_console_is_graphic(NULL
)) {
539 /* release grab if going to a text console */
542 } else if (absolute_enabled
) {
545 } else if (absolute_enabled
) {
547 absolute_mouse_grab();
552 if (!gui_fullscreen
) {
553 int width
= MAX(real_screen
->w
+ (keycode
== 0x1b ? 50 : -50),
555 int height
= (surface_height(surface
) * width
) /
556 surface_width(surface
);
558 sdl_scale(width
, height
);
559 graphic_hw_invalidate(NULL
);
560 graphic_hw_update(NULL
);
566 } else if (!qemu_console_is_graphic(NULL
)) {
569 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
570 switch (ev
->key
.keysym
.sym
) {
572 keysym
= QEMU_KEY_CTRL_UP
;
575 keysym
= QEMU_KEY_CTRL_DOWN
;
578 keysym
= QEMU_KEY_CTRL_LEFT
;
581 keysym
= QEMU_KEY_CTRL_RIGHT
;
584 keysym
= QEMU_KEY_CTRL_HOME
;
587 keysym
= QEMU_KEY_CTRL_END
;
590 keysym
= QEMU_KEY_CTRL_PAGEUP
;
593 keysym
= QEMU_KEY_CTRL_PAGEDOWN
;
599 switch (ev
->key
.keysym
.sym
) {
601 keysym
= QEMU_KEY_UP
;
604 keysym
= QEMU_KEY_DOWN
;
607 keysym
= QEMU_KEY_LEFT
;
610 keysym
= QEMU_KEY_RIGHT
;
613 keysym
= QEMU_KEY_HOME
;
616 keysym
= QEMU_KEY_END
;
619 keysym
= QEMU_KEY_PAGEUP
;
622 keysym
= QEMU_KEY_PAGEDOWN
;
625 keysym
= QEMU_KEY_BACKSPACE
;
628 keysym
= QEMU_KEY_DELETE
;
635 kbd_put_keysym(keysym
);
636 } else if (ev
->key
.keysym
.unicode
!= 0) {
637 kbd_put_keysym(ev
->key
.keysym
.unicode
);
640 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
641 sdl_process_key(&ev
->key
);
645 static void handle_keyup(SDL_Event
*ev
)
650 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
652 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
654 if (!mod_state
&& gui_key_modifier_pressed
) {
655 gui_key_modifier_pressed
= 0;
656 if (gui_keysym
== 0) {
657 /* exit/enter grab if pressing Ctrl-Alt */
659 if (qemu_console_is_graphic(NULL
)) {
662 } else if (!gui_fullscreen
) {
665 /* SDL does not send back all the modifiers key, so we must
672 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
673 sdl_process_key(&ev
->key
);
677 static void handle_mousemotion(SDL_Event
*ev
)
681 if (qemu_console_is_graphic(NULL
) &&
682 (kbd_mouse_is_absolute() || absolute_enabled
)) {
683 max_x
= real_screen
->w
- 1;
684 max_y
= real_screen
->h
- 1;
685 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
686 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
690 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
691 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
695 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
696 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
, 0,
697 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
701 static void handle_mousebutton(SDL_Event
*ev
)
703 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
704 SDL_MouseButtonEvent
*bev
;
707 if (!qemu_console_is_graphic(NULL
)) {
712 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
713 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
714 /* start grabbing all events */
719 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
720 buttonstate
|= SDL_BUTTON(bev
->button
);
722 buttonstate
&= ~SDL_BUTTON(bev
->button
);
724 #ifdef SDL_BUTTON_WHEELUP
725 if (bev
->button
== SDL_BUTTON_WHEELUP
&&
726 ev
->type
== SDL_MOUSEBUTTONDOWN
) {
728 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&&
729 ev
->type
== SDL_MOUSEBUTTONDOWN
) {
733 sdl_send_mouse_event(0, 0, dz
, bev
->x
, bev
->y
, buttonstate
);
737 static void handle_activation(SDL_Event
*ev
)
740 /* Disable grab if the window no longer has the focus
741 * (Windows-only workaround) */
742 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
743 !ev
->active
.gain
&& !gui_fullscreen
) {
747 if (!gui_grab
&& ev
->active
.gain
&& qemu_console_is_graphic(NULL
) &&
748 (kbd_mouse_is_absolute() || absolute_enabled
)) {
749 absolute_mouse_grab();
751 if (ev
->active
.state
& SDL_APPACTIVE
) {
752 if (ev
->active
.gain
) {
753 /* Back to default interval */
754 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
756 /* Sleeping interval. Not using the long default here as
757 * sdl_refresh does not only update the guest screen, but
758 * also checks for gui events. */
759 update_displaychangelistener(dcl
, 500);
764 static void sdl_refresh(DisplayChangeListener
*dcl
)
766 SDL_Event ev1
, *ev
= &ev1
;
768 if (last_vm_running
!= runstate_is_running()) {
769 last_vm_running
= runstate_is_running();
770 sdl_update_caption();
773 graphic_hw_update(NULL
);
774 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL
));
776 while (SDL_PollEvent(ev
)) {
778 case SDL_VIDEOEXPOSE
:
779 sdl_update(dcl
, 0, 0, real_screen
->w
, real_screen
->h
);
790 qemu_system_shutdown_request();
793 case SDL_MOUSEMOTION
:
794 handle_mousemotion(ev
);
796 case SDL_MOUSEBUTTONDOWN
:
797 case SDL_MOUSEBUTTONUP
:
798 handle_mousebutton(ev
);
800 case SDL_ACTIVEEVENT
:
801 handle_activation(ev
);
803 case SDL_VIDEORESIZE
:
804 sdl_scale(ev
->resize
.w
, ev
->resize
.h
);
805 graphic_hw_invalidate(NULL
);
806 graphic_hw_update(NULL
);
814 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
815 int x
, int y
, int on
)
820 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
821 SDL_SetCursor(guest_sprite
);
822 if (!kbd_mouse_is_absolute() && !absolute_enabled
)
828 guest_x
= x
, guest_y
= y
;
831 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
834 uint8_t *image
, *mask
;
838 SDL_FreeCursor(guest_sprite
);
840 bpl
= cursor_get_mono_bpl(c
);
841 image
= g_malloc0(bpl
* c
->height
);
842 mask
= g_malloc0(bpl
* c
->height
);
843 cursor_get_mono_image(c
, 0x000000, image
);
844 cursor_get_mono_mask(c
, 0, mask
);
845 guest_sprite
= SDL_CreateCursor(image
, mask
, c
->width
, c
->height
,
851 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
852 SDL_SetCursor(guest_sprite
);
855 static void sdl_cleanup(void)
858 SDL_FreeCursor(guest_sprite
);
859 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
862 static const DisplayChangeListenerOps dcl_ops
= {
864 .dpy_gfx_update
= sdl_update
,
865 .dpy_gfx_switch
= sdl_switch
,
866 .dpy_refresh
= sdl_refresh
,
867 .dpy_mouse_set
= sdl_mouse_warp
,
868 .dpy_cursor_define
= sdl_mouse_define
,
871 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
875 const SDL_VideoInfo
*vi
;
878 #if defined(__APPLE__)
879 /* always use generic keymaps */
880 if (!keyboard_layout
)
881 keyboard_layout
= "en-us";
883 if(keyboard_layout
) {
884 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
893 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
896 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
897 * accessible $DISPLAY to open X11 window. This is often the case
898 * when qemu is run using sudo. But in this case, and when actually
899 * run in X11 environment, SDL fights with X11 for the video card,
900 * making current display unavailable, often until reboot.
901 * So make x11 the default SDL video driver if this variable is unset.
902 * This is a bit hackish but saves us from bigger problem.
903 * Maybe it's a good idea to fix this in SDL instead.
905 setenv("SDL_VIDEODRIVER", "x11", 0);
908 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
909 * This requires SDL >= 1.2.14. */
910 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
912 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
913 if (SDL_Init (flags
)) {
914 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
918 vi
= SDL_GetVideoInfo();
919 host_format
= *(vi
->vfmt
);
921 /* Load a 32x32x4 image. White pixels are transparent. */
922 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
924 SDL_Surface
*image
= SDL_LoadBMP(filename
);
926 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
927 SDL_SetColorKey(image
, SDL_SRCCOLORKEY
, colorkey
);
928 SDL_WM_SetIcon(image
, NULL
);
938 dcl
= g_malloc0(sizeof(DisplayChangeListener
));
940 register_displaychangelistener(dcl
);
942 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
943 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
945 sdl_update_caption();
946 SDL_EnableKeyRepeat(250, 50);
949 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
950 sdl_cursor_normal
= SDL_GetCursor();