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"
34 #include "sysemu/sysemu.h"
38 static DisplayChangeListener
*dcl
;
39 static DisplaySurface
*surface
;
40 static SDL_Surface
*real_screen
;
41 static SDL_Surface
*guest_screen
= NULL
;
42 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
43 static int last_vm_running
;
44 static bool gui_saved_scaling
;
45 static int gui_saved_width
;
46 static int gui_saved_height
;
47 static int gui_saved_grab
;
48 static int gui_fullscreen
;
49 static int gui_noframe
;
50 static int gui_key_modifier_pressed
;
51 static int gui_keysym
;
52 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
53 static uint8_t modifiers_state
[256];
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 SDL_PixelFormat host_format
;
61 static int scaling_active
= 0;
62 static Notifier mouse_mode_notifier
;
68 static void sdl_update(DisplayChangeListener
*dcl
,
69 int x
, int y
, int w
, int h
)
78 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
79 x
, y
, w
, h
, scaling_active
);
83 if (!scaling_active
) {
84 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
86 if (sdl_zoom_blit(guest_screen
, real_screen
, SMOOTHING_ON
, &rec
) < 0) {
87 fprintf(stderr
, "Zoom blit failed\n");
92 SDL_UpdateRect(real_screen
, rec
.x
, rec
.y
, rec
.w
, rec
.h
);
95 static void do_sdl_resize(int width
, int height
, int bpp
)
98 SDL_Surface
*tmp_screen
;
101 printf("SDL: Resizing to %dx%d bpp %d\n", width
, height
, bpp
);
104 flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
;
105 if (gui_fullscreen
) {
106 flags
|= SDL_FULLSCREEN
;
108 flags
|= SDL_RESIZABLE
;
111 flags
|= SDL_NOFRAME
;
113 tmp_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
116 fprintf(stderr
, "Could not open SDL display (%dx%dx%d): %s\n",
117 width
, height
, bpp
, SDL_GetError());
122 * Revert to the previous video mode if the change of resizing or
126 fprintf(stderr
, "Failed to set SDL display (%dx%dx%d): %s\n",
127 width
, height
, bpp
, SDL_GetError());
132 real_screen
= tmp_screen
;
135 static void sdl_switch(DisplayChangeListener
*dcl
,
136 DisplaySurface
*new_surface
)
140 /* temporary hack: allows to call sdl_switch to handle scaling changes */
142 surface
= new_surface
;
144 pf
= qemu_pixelformat_from_pixman(surface
->format
);
146 if (!scaling_active
) {
147 do_sdl_resize(surface_width(surface
), surface_height(surface
), 0);
148 } else if (real_screen
->format
->BitsPerPixel
!=
149 surface_bits_per_pixel(surface
)) {
150 do_sdl_resize(real_screen
->w
, real_screen
->h
,
151 surface_bits_per_pixel(surface
));
154 if (guest_screen
!= NULL
) {
155 SDL_FreeSurface(guest_screen
);
159 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
160 pf
.rmask
, pf
.gmask
, pf
.bmask
, pf
.amask
);
163 guest_screen
= SDL_CreateRGBSurfaceFrom
164 (surface_data(surface
),
165 surface_width(surface
), surface_height(surface
),
166 surface_bits_per_pixel(surface
), surface_stride(surface
),
171 static bool sdl_check_format(DisplayChangeListener
*dcl
,
172 pixman_format_code_t format
)
175 * We let SDL convert for us a few more formats than,
176 * the native ones. Thes are the ones I have tested.
178 return (format
== PIXMAN_x8r8g8b8
||
179 format
== PIXMAN_b8g8r8x8
||
180 format
== PIXMAN_x1r5g5b5
||
181 format
== PIXMAN_r5g6b5
);
184 /* generic keyboard conversion */
186 #include "sdl_keysym.h"
188 static kbd_layout_t
*kbd_layout
= NULL
;
190 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
193 /* workaround for X11+SDL bug with AltGR */
194 keysym
= ev
->keysym
.sym
;
195 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
197 /* For Japanese key '\' and '|' */
198 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
201 return keysym2scancode(kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
204 /* specific keyboard conversions from scan codes */
208 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
210 return ev
->keysym
.scancode
;
215 #if defined(SDL_VIDEO_DRIVER_X11)
216 #include <X11/XKBlib.h>
218 static int check_for_evdev(void)
221 XkbDescPtr desc
= NULL
;
223 char *keycodes
= NULL
;
225 SDL_VERSION(&info
.version
);
226 if (!SDL_GetWMInfo(&info
)) {
229 desc
= XkbGetKeyboard(info
.info
.x11
.display
,
230 XkbGBN_AllComponentsMask
,
232 if (desc
&& desc
->names
) {
233 keycodes
= XGetAtomName(info
.info
.x11
.display
, desc
->names
->keycodes
);
234 if (keycodes
== NULL
) {
235 fprintf(stderr
, "could not lookup keycode name\n");
236 } else if (strstart(keycodes
, "evdev", NULL
)) {
238 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
239 fprintf(stderr
, "unknown keycodes `%s', please report to "
240 "qemu-devel@nongnu.org\n", keycodes
);
245 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
253 static int check_for_evdev(void)
259 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
262 static int has_evdev
= -1;
265 has_evdev
= check_for_evdev();
267 keycode
= ev
->keysym
.scancode
;
271 } else if (keycode
< 97) {
272 keycode
-= 8; /* just an offset */
273 } else if (keycode
< 158) {
274 /* use conversion table */
276 keycode
= translate_evdev_keycode(keycode
- 97);
278 keycode
= translate_xfree86_keycode(keycode
- 97);
279 } else if (keycode
== 208) { /* Hiragana_Katakana */
281 } else if (keycode
== 211) { /* backslash */
291 static void reset_keys(void)
294 for(i
= 0; i
< 256; i
++) {
295 if (modifiers_state
[i
]) {
296 qemu_input_event_send_key_number(dcl
->con
, i
, false);
297 modifiers_state
[i
] = 0;
302 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
306 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
308 qemu_input_event_send_key_qcode(dcl
->con
, Q_KEY_CODE_PAUSE
,
309 ev
->type
== SDL_KEYDOWN
);
314 keycode
= sdl_keyevent_to_keycode_generic(ev
);
316 keycode
= sdl_keyevent_to_keycode(ev
);
321 /* sent when leaving window: reset the modifiers state */
324 case 0x2a: /* Left Shift */
325 case 0x36: /* Right Shift */
326 case 0x1d: /* Left CTRL */
327 case 0x9d: /* Right CTRL */
328 case 0x38: /* Left ALT */
329 case 0xb8: /* Right ALT */
330 if (ev
->type
== SDL_KEYUP
)
331 modifiers_state
[keycode
] = 0;
333 modifiers_state
[keycode
] = 1;
335 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
336 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
337 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
338 case 0x45: /* num lock */
339 case 0x3a: /* caps lock */
340 /* SDL does not send the key up event, so we generate it */
341 qemu_input_event_send_key_number(dcl
->con
, keycode
, true);
342 qemu_input_event_send_key_number(dcl
->con
, keycode
, false);
347 /* now send the key code */
348 qemu_input_event_send_key_number(dcl
->con
, keycode
,
349 ev
->type
== SDL_KEYDOWN
);
352 static void sdl_update_caption(void)
354 char win_title
[1024];
355 char icon_title
[1024];
356 const char *status
= "";
358 if (!runstate_is_running())
359 status
= " [Stopped]";
362 status
= " - Press Ctrl-Alt-Shift to exit mouse grab";
364 status
= " - Press Right-Ctrl to exit mouse grab";
366 status
= " - Press Ctrl-Alt to exit mouse grab";
370 snprintf(win_title
, sizeof(win_title
), "QEMU (%s)%s", qemu_name
, status
);
371 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
373 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
374 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
377 SDL_WM_SetCaption(win_title
, icon_title
);
380 static void sdl_hide_cursor(void)
385 if (qemu_input_is_absolute()) {
387 SDL_SetCursor(sdl_cursor_hidden
);
393 static void sdl_show_cursor(void)
398 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL
)) {
401 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
402 SDL_SetCursor(guest_sprite
);
404 SDL_SetCursor(sdl_cursor_normal
);
408 static void sdl_grab_start(void)
411 * If the application is not active, do not try to enter grab state. This
412 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
413 * application (SDL bug).
415 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS
)) {
419 SDL_SetCursor(guest_sprite
);
420 if (!qemu_input_is_absolute() && !absolute_enabled
) {
421 SDL_WarpMouse(guest_x
, guest_y
);
425 SDL_WM_GrabInput(SDL_GRAB_ON
);
427 sdl_update_caption();
430 static void sdl_grab_end(void)
432 SDL_WM_GrabInput(SDL_GRAB_OFF
);
435 sdl_update_caption();
438 static void absolute_mouse_grab(void)
440 int mouse_x
, mouse_y
;
442 SDL_GetMouseState(&mouse_x
, &mouse_y
);
443 if (mouse_x
> 0 && mouse_x
< real_screen
->w
- 1 &&
444 mouse_y
> 0 && mouse_y
< real_screen
->h
- 1) {
449 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
451 if (qemu_input_is_absolute()) {
452 if (!absolute_enabled
) {
453 absolute_enabled
= 1;
454 if (qemu_console_is_graphic(NULL
)) {
455 absolute_mouse_grab();
458 } else if (absolute_enabled
) {
459 if (!gui_fullscreen
) {
462 absolute_enabled
= 0;
466 static void sdl_send_mouse_event(int dx
, int dy
, int x
, int y
, int state
)
468 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
469 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
470 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
471 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
472 [INPUT_BUTTON_WHEEL_UP
] = SDL_BUTTON(SDL_BUTTON_WHEELUP
),
473 [INPUT_BUTTON_WHEEL_DOWN
] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN
),
475 static uint32_t prev_state
;
477 if (prev_state
!= state
) {
478 qemu_input_update_buttons(dcl
->con
, bmap
, prev_state
, state
);
482 if (qemu_input_is_absolute()) {
483 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_X
, x
,
485 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_Y
, y
,
496 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_X
, dx
);
497 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_Y
, dy
);
499 qemu_input_event_sync();
502 static void sdl_scale(int width
, int height
)
504 int bpp
= real_screen
->format
->BitsPerPixel
;
507 printf("SDL: Scaling to %dx%d bpp %d\n", width
, height
, bpp
);
510 if (bpp
!= 16 && bpp
!= 32) {
513 do_sdl_resize(width
, height
, bpp
);
517 static void toggle_full_screen(void)
519 int width
= surface_width(surface
);
520 int height
= surface_height(surface
);
521 int bpp
= surface_bits_per_pixel(surface
);
523 gui_fullscreen
= !gui_fullscreen
;
524 if (gui_fullscreen
) {
525 gui_saved_width
= real_screen
->w
;
526 gui_saved_height
= real_screen
->h
;
527 gui_saved_scaling
= scaling_active
;
529 do_sdl_resize(width
, height
, bpp
);
532 gui_saved_grab
= gui_grab
;
535 if (gui_saved_scaling
) {
536 sdl_scale(gui_saved_width
, gui_saved_height
);
538 do_sdl_resize(width
, height
, 0);
540 if (!gui_saved_grab
|| !qemu_console_is_graphic(NULL
)) {
544 graphic_hw_invalidate(NULL
);
545 graphic_hw_update(NULL
);
548 static void handle_keydown(SDL_Event
*ev
)
554 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
555 (gui_grab_code
| KMOD_LSHIFT
);
556 } else if (ctrl_grab
) {
557 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
559 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
561 gui_key_modifier_pressed
= mod_state
;
563 if (gui_key_modifier_pressed
) {
564 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
566 case 0x21: /* 'f' key on US keyboard */
567 toggle_full_screen();
570 case 0x16: /* 'u' key on US keyboard */
571 if (scaling_active
) {
573 sdl_switch(dcl
, NULL
);
574 graphic_hw_invalidate(NULL
);
575 graphic_hw_update(NULL
);
579 case 0x02 ... 0x0a: /* '1' to '9' keys */
580 /* Reset the modifiers sent to the current console */
582 console_select(keycode
- 0x02);
584 if (gui_fullscreen
) {
587 if (!qemu_console_is_graphic(NULL
)) {
588 /* release grab if going to a text console */
591 } else if (absolute_enabled
) {
594 } else if (absolute_enabled
) {
596 absolute_mouse_grab();
601 if (!gui_fullscreen
) {
602 int width
= MAX(real_screen
->w
+ (keycode
== 0x1b ? 50 : -50),
604 int height
= (surface_height(surface
) * width
) /
605 surface_width(surface
);
607 sdl_scale(width
, height
);
608 graphic_hw_invalidate(NULL
);
609 graphic_hw_update(NULL
);
615 } else if (!qemu_console_is_graphic(NULL
)) {
618 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
619 switch (ev
->key
.keysym
.sym
) {
621 keysym
= QEMU_KEY_CTRL_UP
;
624 keysym
= QEMU_KEY_CTRL_DOWN
;
627 keysym
= QEMU_KEY_CTRL_LEFT
;
630 keysym
= QEMU_KEY_CTRL_RIGHT
;
633 keysym
= QEMU_KEY_CTRL_HOME
;
636 keysym
= QEMU_KEY_CTRL_END
;
639 keysym
= QEMU_KEY_CTRL_PAGEUP
;
642 keysym
= QEMU_KEY_CTRL_PAGEDOWN
;
648 switch (ev
->key
.keysym
.sym
) {
650 keysym
= QEMU_KEY_UP
;
653 keysym
= QEMU_KEY_DOWN
;
656 keysym
= QEMU_KEY_LEFT
;
659 keysym
= QEMU_KEY_RIGHT
;
662 keysym
= QEMU_KEY_HOME
;
665 keysym
= QEMU_KEY_END
;
668 keysym
= QEMU_KEY_PAGEUP
;
671 keysym
= QEMU_KEY_PAGEDOWN
;
674 keysym
= QEMU_KEY_BACKSPACE
;
677 keysym
= QEMU_KEY_DELETE
;
684 kbd_put_keysym(keysym
);
685 } else if (ev
->key
.keysym
.unicode
!= 0) {
686 kbd_put_keysym(ev
->key
.keysym
.unicode
);
689 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
690 sdl_process_key(&ev
->key
);
694 static void handle_keyup(SDL_Event
*ev
)
699 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
701 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
703 if (!mod_state
&& gui_key_modifier_pressed
) {
704 gui_key_modifier_pressed
= 0;
705 if (gui_keysym
== 0) {
706 /* exit/enter grab if pressing Ctrl-Alt */
708 if (qemu_console_is_graphic(NULL
)) {
711 } else if (!gui_fullscreen
) {
714 /* SDL does not send back all the modifiers key, so we must
721 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
722 sdl_process_key(&ev
->key
);
726 static void handle_mousemotion(SDL_Event
*ev
)
730 if (qemu_console_is_graphic(NULL
) &&
731 (qemu_input_is_absolute() || absolute_enabled
)) {
732 max_x
= real_screen
->w
- 1;
733 max_y
= real_screen
->h
- 1;
734 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
735 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
739 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
740 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
744 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
745 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
,
746 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
750 static void handle_mousebutton(SDL_Event
*ev
)
752 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
753 SDL_MouseButtonEvent
*bev
;
755 if (!qemu_console_is_graphic(NULL
)) {
760 if (!gui_grab
&& !qemu_input_is_absolute()) {
761 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
762 /* start grabbing all events */
766 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
767 buttonstate
|= SDL_BUTTON(bev
->button
);
769 buttonstate
&= ~SDL_BUTTON(bev
->button
);
771 sdl_send_mouse_event(0, 0, bev
->x
, bev
->y
, buttonstate
);
775 static void handle_activation(SDL_Event
*ev
)
778 /* Disable grab if the window no longer has the focus
779 * (Windows-only workaround) */
780 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
781 !ev
->active
.gain
&& !gui_fullscreen
) {
785 if (!gui_grab
&& ev
->active
.gain
&& qemu_console_is_graphic(NULL
) &&
786 (qemu_input_is_absolute() || absolute_enabled
)) {
787 absolute_mouse_grab();
789 if (ev
->active
.state
& SDL_APPACTIVE
) {
790 if (ev
->active
.gain
) {
791 /* Back to default interval */
792 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
794 /* Sleeping interval. Not using the long default here as
795 * sdl_refresh does not only update the guest screen, but
796 * also checks for gui events. */
797 update_displaychangelistener(dcl
, 500);
802 static void sdl_refresh(DisplayChangeListener
*dcl
)
804 SDL_Event ev1
, *ev
= &ev1
;
806 if (last_vm_running
!= runstate_is_running()) {
807 last_vm_running
= runstate_is_running();
808 sdl_update_caption();
811 graphic_hw_update(NULL
);
812 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL
));
814 while (SDL_PollEvent(ev
)) {
816 case SDL_VIDEOEXPOSE
:
817 sdl_update(dcl
, 0, 0, real_screen
->w
, real_screen
->h
);
828 qemu_system_shutdown_request();
831 case SDL_MOUSEMOTION
:
832 handle_mousemotion(ev
);
834 case SDL_MOUSEBUTTONDOWN
:
835 case SDL_MOUSEBUTTONUP
:
836 handle_mousebutton(ev
);
838 case SDL_ACTIVEEVENT
:
839 handle_activation(ev
);
841 case SDL_VIDEORESIZE
:
842 sdl_scale(ev
->resize
.w
, ev
->resize
.h
);
843 graphic_hw_invalidate(NULL
);
844 graphic_hw_update(NULL
);
852 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
853 int x
, int y
, int on
)
858 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
859 SDL_SetCursor(guest_sprite
);
860 if (!qemu_input_is_absolute() && !absolute_enabled
) {
867 guest_x
= x
, guest_y
= y
;
870 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
873 uint8_t *image
, *mask
;
877 SDL_FreeCursor(guest_sprite
);
879 bpl
= cursor_get_mono_bpl(c
);
880 image
= g_malloc0(bpl
* c
->height
);
881 mask
= g_malloc0(bpl
* c
->height
);
882 cursor_get_mono_image(c
, 0x000000, image
);
883 cursor_get_mono_mask(c
, 0, mask
);
884 guest_sprite
= SDL_CreateCursor(image
, mask
, c
->width
, c
->height
,
890 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
891 SDL_SetCursor(guest_sprite
);
894 static void sdl_cleanup(void)
897 SDL_FreeCursor(guest_sprite
);
898 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
901 static const DisplayChangeListenerOps dcl_ops
= {
903 .dpy_gfx_update
= sdl_update
,
904 .dpy_gfx_switch
= sdl_switch
,
905 .dpy_gfx_check_format
= sdl_check_format
,
906 .dpy_refresh
= sdl_refresh
,
907 .dpy_mouse_set
= sdl_mouse_warp
,
908 .dpy_cursor_define
= sdl_mouse_define
,
911 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
915 const SDL_VideoInfo
*vi
;
918 #if defined(__APPLE__)
919 /* always use generic keymaps */
920 if (!keyboard_layout
)
921 keyboard_layout
= "en-us";
923 if(keyboard_layout
) {
924 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
933 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
936 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
937 * accessible $DISPLAY to open X11 window. This is often the case
938 * when qemu is run using sudo. But in this case, and when actually
939 * run in X11 environment, SDL fights with X11 for the video card,
940 * making current display unavailable, often until reboot.
941 * So make x11 the default SDL video driver if this variable is unset.
942 * This is a bit hackish but saves us from bigger problem.
943 * Maybe it's a good idea to fix this in SDL instead.
945 setenv("SDL_VIDEODRIVER", "x11", 0);
948 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
949 * This requires SDL >= 1.2.14. */
950 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
952 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
953 if (SDL_Init (flags
)) {
954 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
958 vi
= SDL_GetVideoInfo();
959 host_format
= *(vi
->vfmt
);
961 /* Load a 32x32x4 image. White pixels are transparent. */
962 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
964 SDL_Surface
*image
= SDL_LoadBMP(filename
);
966 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
967 SDL_SetColorKey(image
, SDL_SRCCOLORKEY
, colorkey
);
968 SDL_WM_SetIcon(image
, NULL
);
978 dcl
= g_malloc0(sizeof(DisplayChangeListener
));
980 register_displaychangelistener(dcl
);
982 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
983 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
985 sdl_update_caption();
986 SDL_EnableKeyRepeat(250, 50);
989 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
990 sdl_cursor_normal
= SDL_GetCursor();