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"
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_fullscreen_initial_grab
;
51 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
52 static uint8_t modifiers_state
[256];
53 static int width
, height
;
54 static SDL_Cursor
*sdl_cursor_normal
;
55 static SDL_Cursor
*sdl_cursor_hidden
;
56 static int absolute_enabled
= 0;
57 static int guest_cursor
= 0;
58 static int guest_x
, guest_y
;
59 static SDL_Cursor
*guest_sprite
= NULL
;
60 static uint8_t allocator
;
61 static SDL_PixelFormat host_format
;
62 static int scaling_active
= 0;
63 static Notifier mouse_mode_notifier
;
65 static void sdl_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
67 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
75 if (!scaling_active
) {
76 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
78 if (sdl_zoom_blit(guest_screen
, real_screen
, SMOOTHING_ON
, &rec
) < 0) {
79 fprintf(stderr
, "Zoom blit failed\n");
84 SDL_UpdateRect(real_screen
, rec
.x
, rec
.y
, rec
.w
, rec
.h
);
87 static void sdl_setdata(DisplayState
*ds
)
89 if (guest_screen
!= NULL
) SDL_FreeSurface(guest_screen
);
91 guest_screen
= SDL_CreateRGBSurfaceFrom(ds_get_data(ds
), ds_get_width(ds
), ds_get_height(ds
),
92 ds_get_bits_per_pixel(ds
), ds_get_linesize(ds
),
93 ds
->surface
->pf
.rmask
, ds
->surface
->pf
.gmask
,
94 ds
->surface
->pf
.bmask
, ds
->surface
->pf
.amask
);
97 static void do_sdl_resize(int new_width
, int new_height
, int bpp
)
101 // printf("resizing to %d %d\n", w, h);
103 flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
;
104 if (gui_fullscreen
) {
105 flags
|= SDL_FULLSCREEN
;
107 flags
|= SDL_RESIZABLE
;
110 flags
|= SDL_NOFRAME
;
114 real_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
116 fprintf(stderr
, "Could not open SDL display (%dx%dx%d): %s\n", width
,
117 height
, bpp
, SDL_GetError());
122 static void sdl_resize(DisplayState
*ds
)
126 do_sdl_resize(ds_get_width(ds
), ds_get_height(ds
), 0);
127 else if (real_screen
->format
->BitsPerPixel
!= ds_get_bits_per_pixel(ds
))
128 do_sdl_resize(real_screen
->w
, real_screen
->h
, ds_get_bits_per_pixel(ds
));
131 if (guest_screen
!= NULL
) {
132 SDL_FreeSurface(guest_screen
);
138 static PixelFormat
sdl_to_qemu_pixelformat(SDL_PixelFormat
*sdl_pf
)
142 memset(&qemu_pf
, 0x00, sizeof(PixelFormat
));
144 qemu_pf
.bits_per_pixel
= sdl_pf
->BitsPerPixel
;
145 qemu_pf
.bytes_per_pixel
= sdl_pf
->BytesPerPixel
;
146 qemu_pf
.depth
= (qemu_pf
.bits_per_pixel
) == 32 ? 24 : (qemu_pf
.bits_per_pixel
);
148 qemu_pf
.rmask
= sdl_pf
->Rmask
;
149 qemu_pf
.gmask
= sdl_pf
->Gmask
;
150 qemu_pf
.bmask
= sdl_pf
->Bmask
;
151 qemu_pf
.amask
= sdl_pf
->Amask
;
153 qemu_pf
.rshift
= sdl_pf
->Rshift
;
154 qemu_pf
.gshift
= sdl_pf
->Gshift
;
155 qemu_pf
.bshift
= sdl_pf
->Bshift
;
156 qemu_pf
.ashift
= sdl_pf
->Ashift
;
158 qemu_pf
.rbits
= 8 - sdl_pf
->Rloss
;
159 qemu_pf
.gbits
= 8 - sdl_pf
->Gloss
;
160 qemu_pf
.bbits
= 8 - sdl_pf
->Bloss
;
161 qemu_pf
.abits
= 8 - sdl_pf
->Aloss
;
163 qemu_pf
.rmax
= ((1 << qemu_pf
.rbits
) - 1);
164 qemu_pf
.gmax
= ((1 << qemu_pf
.gbits
) - 1);
165 qemu_pf
.bmax
= ((1 << qemu_pf
.bbits
) - 1);
166 qemu_pf
.amax
= ((1 << qemu_pf
.abits
) - 1);
171 static DisplaySurface
* sdl_create_displaysurface(int width
, int height
)
173 DisplaySurface
*surface
= (DisplaySurface
*) qemu_mallocz(sizeof(DisplaySurface
));
174 if (surface
== NULL
) {
175 fprintf(stderr
, "sdl_create_displaysurface: malloc failed\n");
179 surface
->width
= width
;
180 surface
->height
= height
;
182 if (scaling_active
) {
185 if (host_format
.BytesPerPixel
!= 2 && host_format
.BytesPerPixel
!= 4) {
186 linesize
= width
* 4;
187 pf
= qemu_default_pixelformat(32);
189 linesize
= width
* host_format
.BytesPerPixel
;
190 pf
= sdl_to_qemu_pixelformat(&host_format
);
192 qemu_alloc_display(surface
, width
, height
, linesize
, pf
, 0);
196 if (host_format
.BitsPerPixel
== 16)
197 do_sdl_resize(width
, height
, 16);
199 do_sdl_resize(width
, height
, 32);
201 surface
->pf
= sdl_to_qemu_pixelformat(real_screen
->format
);
202 surface
->linesize
= real_screen
->pitch
;
203 surface
->data
= real_screen
->pixels
;
205 #ifdef HOST_WORDS_BIGENDIAN
206 surface
->flags
= QEMU_REALPIXELS_FLAG
| QEMU_BIG_ENDIAN_FLAG
;
208 surface
->flags
= QEMU_REALPIXELS_FLAG
;
215 static void sdl_free_displaysurface(DisplaySurface
*surface
)
221 if (surface
->flags
& QEMU_ALLOCATED_FLAG
)
222 qemu_free(surface
->data
);
226 static DisplaySurface
* sdl_resize_displaysurface(DisplaySurface
*surface
, int width
, int height
)
228 sdl_free_displaysurface(surface
);
229 return sdl_create_displaysurface(width
, height
);
232 /* generic keyboard conversion */
234 #include "sdl_keysym.h"
236 static kbd_layout_t
*kbd_layout
= NULL
;
238 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
241 /* workaround for X11+SDL bug with AltGR */
242 keysym
= ev
->keysym
.sym
;
243 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
245 /* For Japanese key '\' and '|' */
246 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
249 return keysym2scancode(kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
252 /* specific keyboard conversions from scan codes */
256 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
258 return ev
->keysym
.scancode
;
263 #if defined(SDL_VIDEO_DRIVER_X11)
264 #include <X11/XKBlib.h>
266 static int check_for_evdev(void)
269 XkbDescPtr desc
= NULL
;
271 char *keycodes
= NULL
;
273 SDL_VERSION(&info
.version
);
274 if (!SDL_GetWMInfo(&info
)) {
277 desc
= XkbGetKeyboard(info
.info
.x11
.display
,
278 XkbGBN_AllComponentsMask
,
280 if (desc
&& desc
->names
) {
281 keycodes
= XGetAtomName(info
.info
.x11
.display
, desc
->names
->keycodes
);
282 if (keycodes
== NULL
) {
283 fprintf(stderr
, "could not lookup keycode name\n");
284 } else if (strstart(keycodes
, "evdev", NULL
)) {
286 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
287 fprintf(stderr
, "unknown keycodes `%s', please report to "
288 "qemu-devel@nongnu.org\n", keycodes
);
293 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
301 static int check_for_evdev(void)
307 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
310 static int has_evdev
= -1;
313 has_evdev
= check_for_evdev();
315 keycode
= ev
->keysym
.scancode
;
319 } else if (keycode
< 97) {
320 keycode
-= 8; /* just an offset */
321 } else if (keycode
< 158) {
322 /* use conversion table */
324 keycode
= translate_evdev_keycode(keycode
- 97);
326 keycode
= translate_xfree86_keycode(keycode
- 97);
327 } else if (keycode
== 208) { /* Hiragana_Katakana */
329 } else if (keycode
== 211) { /* backslash */
339 static void reset_keys(void)
342 for(i
= 0; i
< 256; i
++) {
343 if (modifiers_state
[i
]) {
344 if (i
& SCANCODE_GREY
)
345 kbd_put_keycode(SCANCODE_EMUL0
);
346 kbd_put_keycode(i
| SCANCODE_UP
);
347 modifiers_state
[i
] = 0;
352 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
356 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
359 if (ev
->type
== SDL_KEYUP
)
361 kbd_put_keycode(0xe1);
362 kbd_put_keycode(0x1d | v
);
363 kbd_put_keycode(0x45 | v
);
368 keycode
= sdl_keyevent_to_keycode_generic(ev
);
370 keycode
= sdl_keyevent_to_keycode(ev
);
375 /* sent when leaving window: reset the modifiers state */
378 case 0x2a: /* Left Shift */
379 case 0x36: /* Right Shift */
380 case 0x1d: /* Left CTRL */
381 case 0x9d: /* Right CTRL */
382 case 0x38: /* Left ALT */
383 case 0xb8: /* Right ALT */
384 if (ev
->type
== SDL_KEYUP
)
385 modifiers_state
[keycode
] = 0;
387 modifiers_state
[keycode
] = 1;
389 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
390 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
391 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
392 case 0x45: /* num lock */
393 case 0x3a: /* caps lock */
394 /* SDL does not send the key up event, so we generate it */
395 kbd_put_keycode(keycode
);
396 kbd_put_keycode(keycode
| SCANCODE_UP
);
401 /* now send the key code */
402 if (keycode
& SCANCODE_GREY
)
403 kbd_put_keycode(SCANCODE_EMUL0
);
404 if (ev
->type
== SDL_KEYUP
)
405 kbd_put_keycode(keycode
| SCANCODE_UP
);
407 kbd_put_keycode(keycode
& SCANCODE_KEYCODEMASK
);
410 static void sdl_update_caption(void)
412 char win_title
[1024];
413 char icon_title
[1024];
414 const char *status
= "";
417 status
= " [Stopped]";
420 status
= " - Press Ctrl-Alt-Shift to exit mouse grab";
422 status
= " - Press Right-Ctrl to exit mouse grab";
424 status
= " - Press Ctrl-Alt to exit mouse grab";
428 snprintf(win_title
, sizeof(win_title
), "QEMU (%s)%s", qemu_name
, status
);
429 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
431 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
432 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
435 SDL_WM_SetCaption(win_title
, icon_title
);
438 static void sdl_hide_cursor(void)
443 if (kbd_mouse_is_absolute()) {
445 SDL_SetCursor(sdl_cursor_hidden
);
451 static void sdl_show_cursor(void)
456 if (!kbd_mouse_is_absolute()) {
459 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
460 SDL_SetCursor(guest_sprite
);
462 SDL_SetCursor(sdl_cursor_normal
);
466 static void sdl_grab_start(void)
469 SDL_SetCursor(guest_sprite
);
470 if (!kbd_mouse_is_absolute() && !absolute_enabled
)
471 SDL_WarpMouse(guest_x
, guest_y
);
475 if (SDL_WM_GrabInput(SDL_GRAB_ON
) == SDL_GRAB_ON
) {
477 sdl_update_caption();
482 static void sdl_grab_end(void)
484 SDL_WM_GrabInput(SDL_GRAB_OFF
);
487 sdl_update_caption();
490 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
492 if (kbd_mouse_is_absolute()) {
493 if (!absolute_enabled
) {
498 absolute_enabled
= 1;
500 } else if (absolute_enabled
) {
502 absolute_enabled
= 0;
506 static void sdl_send_mouse_event(int dx
, int dy
, int dz
, int x
, int y
, int state
)
510 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
511 buttons
|= MOUSE_EVENT_LBUTTON
;
512 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
513 buttons
|= MOUSE_EVENT_RBUTTON
;
514 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
515 buttons
|= MOUSE_EVENT_MBUTTON
;
517 if (kbd_mouse_is_absolute()) {
518 dx
= x
* 0x7FFF / (width
- 1);
519 dy
= y
* 0x7FFF / (height
- 1);
520 } else if (guest_cursor
) {
529 kbd_mouse_event(dx
, dy
, dz
, buttons
);
532 static void sdl_scale(DisplayState
*ds
, int width
, int height
)
534 int bpp
= real_screen
->format
->BitsPerPixel
;
536 if (bpp
!= 16 && bpp
!= 32) {
539 do_sdl_resize(width
, height
, bpp
);
541 if (!is_buffer_shared(ds
->surface
)) {
542 ds
->surface
= qemu_resize_displaysurface(ds
, ds_get_width(ds
),
548 static void toggle_full_screen(DisplayState
*ds
)
550 gui_fullscreen
= !gui_fullscreen
;
551 if (gui_fullscreen
) {
552 gui_saved_width
= real_screen
->w
;
553 gui_saved_height
= real_screen
->h
;
554 gui_saved_scaling
= scaling_active
;
556 do_sdl_resize(ds_get_width(ds
), ds_get_height(ds
),
557 ds_get_bits_per_pixel(ds
));
560 gui_saved_grab
= gui_grab
;
563 if (gui_saved_scaling
) {
564 sdl_scale(ds
, gui_saved_width
, gui_saved_height
);
566 do_sdl_resize(ds_get_width(ds
), ds_get_height(ds
), 0);
575 static void sdl_refresh(DisplayState
*ds
)
577 SDL_Event ev1
, *ev
= &ev1
;
579 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
581 if (last_vm_running
!= vm_running
) {
582 last_vm_running
= vm_running
;
583 sdl_update_caption();
587 SDL_EnableUNICODE(!is_graphic_console());
589 while (SDL_PollEvent(ev
)) {
591 case SDL_VIDEOEXPOSE
:
592 sdl_update(ds
, 0, 0, real_screen
->w
, real_screen
->h
);
596 if (ev
->type
== SDL_KEYDOWN
) {
598 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
599 (gui_grab_code
| KMOD_LSHIFT
);
600 } else if (ctrl_grab
) {
601 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
603 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
606 gui_key_modifier_pressed
= mod_state
;
607 if (gui_key_modifier_pressed
) {
609 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
611 case 0x21: /* 'f' key on US keyboard */
612 toggle_full_screen(ds
);
615 case 0x16: /* 'u' key on US keyboard */
616 if (scaling_active
) {
624 case 0x02 ... 0x0a: /* '1' to '9' keys */
625 /* Reset the modifiers sent to the current console */
627 console_select(keycode
- 0x02);
628 if (!is_graphic_console()) {
629 /* display grab if going to a text console */
638 } else if (!is_graphic_console()) {
641 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
642 switch(ev
->key
.keysym
.sym
) {
643 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
644 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
645 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
646 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
647 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
648 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
649 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
650 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
654 switch(ev
->key
.keysym
.sym
) {
655 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
656 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
657 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
658 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
659 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
660 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
661 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
662 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
663 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break;
664 case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
669 kbd_put_keysym(keysym
);
670 } else if (ev
->key
.keysym
.unicode
!= 0) {
671 kbd_put_keysym(ev
->key
.keysym
.unicode
);
674 } else if (ev
->type
== SDL_KEYUP
) {
676 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
678 mod_state
= (ev
->key
.keysym
.mod
&
679 (gui_grab_code
| KMOD_LSHIFT
));
682 if (gui_key_modifier_pressed
) {
683 gui_key_modifier_pressed
= 0;
684 if (gui_keysym
== 0) {
685 /* exit/enter grab if pressing Ctrl-Alt */
687 /* if the application is not active,
688 do not try to enter grab state. It
690 'SDL_WM_GrabInput(SDL_GRAB_ON)'
691 from blocking all the application
693 if (SDL_GetAppState() & SDL_APPACTIVE
)
698 /* SDL does not send back all the
699 modifiers key, so we must correct it */
707 if (is_graphic_console() && !gui_keysym
)
708 sdl_process_key(&ev
->key
);
713 qemu_system_shutdown_request();
716 case SDL_MOUSEMOTION
:
717 if (gui_grab
|| kbd_mouse_is_absolute() ||
719 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
, 0,
720 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
723 case SDL_MOUSEBUTTONDOWN
:
724 case SDL_MOUSEBUTTONUP
:
726 SDL_MouseButtonEvent
*bev
= &ev
->button
;
727 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
728 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
729 (bev
->button
== SDL_BUTTON_LEFT
)) {
730 /* start grabbing all events */
736 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
737 buttonstate
|= SDL_BUTTON(bev
->button
);
739 buttonstate
&= ~SDL_BUTTON(bev
->button
);
741 #ifdef SDL_BUTTON_WHEELUP
742 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
744 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
748 sdl_send_mouse_event(0, 0, dz
, bev
->x
, bev
->y
, buttonstate
);
752 case SDL_ACTIVEEVENT
:
753 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
754 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
757 if (ev
->active
.state
& SDL_APPACTIVE
) {
758 if (ev
->active
.gain
) {
759 /* Back to default interval */
760 dcl
->gui_timer_interval
= 0;
763 /* Sleeping interval */
764 dcl
->gui_timer_interval
= 500;
769 case SDL_VIDEORESIZE
:
770 sdl_scale(ds
, ev
->resize
.w
, ev
->resize
.h
);
780 static void sdl_fill(DisplayState
*ds
, int x
, int y
, int w
, int h
, uint32_t c
)
782 SDL_Rect dst
= { x
, y
, w
, h
};
783 SDL_FillRect(real_screen
, &dst
, c
);
786 static void sdl_mouse_warp(int x
, int y
, int on
)
791 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
792 SDL_SetCursor(guest_sprite
);
793 if (!kbd_mouse_is_absolute() && !absolute_enabled
)
799 guest_x
= x
, guest_y
= y
;
802 static void sdl_mouse_define(QEMUCursor
*c
)
804 uint8_t *image
, *mask
;
808 SDL_FreeCursor(guest_sprite
);
810 bpl
= cursor_get_mono_bpl(c
);
811 image
= qemu_mallocz(bpl
* c
->height
);
812 mask
= qemu_mallocz(bpl
* c
->height
);
813 cursor_get_mono_image(c
, 0x000000, image
);
814 cursor_get_mono_mask(c
, 0, mask
);
815 guest_sprite
= SDL_CreateCursor(image
, mask
, c
->width
, c
->height
,
821 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
822 SDL_SetCursor(guest_sprite
);
825 static void sdl_cleanup(void)
828 SDL_FreeCursor(guest_sprite
);
829 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
832 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
836 DisplayAllocator
*da
;
837 const SDL_VideoInfo
*vi
;
840 #if defined(__APPLE__)
841 /* always use generic keymaps */
842 if (!keyboard_layout
)
843 keyboard_layout
= "en-us";
845 if(keyboard_layout
) {
846 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
855 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
858 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
859 * accessible $DISPLAY to open X11 window. This is often the case
860 * when qemu is run using sudo. But in this case, and when actually
861 * run in X11 environment, SDL fights with X11 for the video card,
862 * making current display unavailable, often until reboot.
863 * So make x11 the default SDL video driver if this variable is unset.
864 * This is a bit hackish but saves us from bigger problem.
865 * Maybe it's a good idea to fix this in SDL instead.
867 setenv("SDL_VIDEODRIVER", "x11", 0);
870 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
871 * This requires SDL >= 1.2.14. */
872 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
874 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
875 if (SDL_Init (flags
)) {
876 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
880 vi
= SDL_GetVideoInfo();
881 host_format
= *(vi
->vfmt
);
883 /* Load a 32x32x4 image. White pixels are transparent. */
884 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
886 SDL_Surface
*image
= SDL_LoadBMP(filename
);
888 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
889 SDL_SetColorKey(image
, SDL_SRCCOLORKEY
, colorkey
);
890 SDL_WM_SetIcon(image
, NULL
);
895 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
896 dcl
->dpy_update
= sdl_update
;
897 dcl
->dpy_resize
= sdl_resize
;
898 dcl
->dpy_refresh
= sdl_refresh
;
899 dcl
->dpy_setdata
= sdl_setdata
;
900 dcl
->dpy_fill
= sdl_fill
;
901 ds
->mouse_set
= sdl_mouse_warp
;
902 ds
->cursor_define
= sdl_mouse_define
;
903 register_displaychangelistener(ds
, dcl
);
905 da
= qemu_mallocz(sizeof(DisplayAllocator
));
906 da
->create_displaysurface
= sdl_create_displaysurface
;
907 da
->resize_displaysurface
= sdl_resize_displaysurface
;
908 da
->free_displaysurface
= sdl_free_displaysurface
;
909 if (register_displayallocator(ds
, da
) == da
) {
913 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
914 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
916 sdl_update_caption();
917 SDL_EnableKeyRepeat(250, 50);
920 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
921 sdl_cursor_normal
= SDL_GetCursor();
926 gui_fullscreen_initial_grab
= 1;