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
28 #include "qemu/osdep.h"
30 #include <SDL_syswm.h>
32 #include "qemu-common.h"
33 #include "ui/console.h"
35 #include "sysemu/sysemu.h"
39 static DisplayChangeListener
*dcl
;
40 static DisplaySurface
*surface
;
41 static SDL_Surface
*real_screen
;
42 static SDL_Surface
*guest_screen
= NULL
;
43 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
44 static int last_vm_running
;
45 static bool gui_saved_scaling
;
46 static int gui_saved_width
;
47 static int gui_saved_height
;
48 static int gui_saved_grab
;
49 static int gui_fullscreen
;
50 static int gui_noframe
;
51 static int gui_key_modifier_pressed
;
52 static int gui_keysym
;
53 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
54 static uint8_t modifiers_state
[256];
55 static SDL_Cursor
*sdl_cursor_normal
;
56 static SDL_Cursor
*sdl_cursor_hidden
;
57 static int absolute_enabled
= 0;
58 static int guest_cursor
= 0;
59 static int guest_x
, guest_y
;
60 static SDL_Cursor
*guest_sprite
= NULL
;
61 static SDL_PixelFormat host_format
;
62 static int scaling_active
= 0;
63 static Notifier mouse_mode_notifier
;
64 static int idle_counter
;
66 #define SDL_REFRESH_INTERVAL_BUSY 10
67 #define SDL_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
68 / SDL_REFRESH_INTERVAL_BUSY + 1)
74 static void sdl_update(DisplayChangeListener
*dcl
,
75 int x
, int y
, int w
, int h
)
84 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
85 x
, y
, w
, h
, scaling_active
);
89 if (!scaling_active
) {
90 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
92 if (sdl_zoom_blit(guest_screen
, real_screen
, SMOOTHING_ON
, &rec
) < 0) {
93 fprintf(stderr
, "Zoom blit failed\n");
98 SDL_UpdateRect(real_screen
, rec
.x
, rec
.y
, rec
.w
, rec
.h
);
101 static void do_sdl_resize(int width
, int height
, int bpp
)
104 SDL_Surface
*tmp_screen
;
107 printf("SDL: Resizing to %dx%d bpp %d\n", width
, height
, bpp
);
110 flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
;
111 if (gui_fullscreen
) {
112 flags
|= SDL_FULLSCREEN
;
114 flags
|= SDL_RESIZABLE
;
117 flags
|= SDL_NOFRAME
;
119 tmp_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
122 fprintf(stderr
, "Could not open SDL display (%dx%dx%d): %s\n",
123 width
, height
, bpp
, SDL_GetError());
128 * Revert to the previous video mode if the change of resizing or
132 fprintf(stderr
, "Failed to set SDL display (%dx%dx%d): %s\n",
133 width
, height
, bpp
, SDL_GetError());
138 real_screen
= tmp_screen
;
141 static void sdl_switch(DisplayChangeListener
*dcl
,
142 DisplaySurface
*new_surface
)
146 /* temporary hack: allows to call sdl_switch to handle scaling changes */
148 surface
= new_surface
;
150 pf
= qemu_pixelformat_from_pixman(surface
->format
);
152 if (!scaling_active
) {
153 do_sdl_resize(surface_width(surface
), surface_height(surface
), 0);
154 } else if (real_screen
->format
->BitsPerPixel
!=
155 surface_bits_per_pixel(surface
)) {
156 do_sdl_resize(real_screen
->w
, real_screen
->h
,
157 surface_bits_per_pixel(surface
));
160 if (guest_screen
!= NULL
) {
161 SDL_FreeSurface(guest_screen
);
165 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
166 pf
.rmask
, pf
.gmask
, pf
.bmask
, pf
.amask
);
169 guest_screen
= SDL_CreateRGBSurfaceFrom
170 (surface_data(surface
),
171 surface_width(surface
), surface_height(surface
),
172 surface_bits_per_pixel(surface
), surface_stride(surface
),
177 static bool sdl_check_format(DisplayChangeListener
*dcl
,
178 pixman_format_code_t format
)
181 * We let SDL convert for us a few more formats than,
182 * the native ones. Thes are the ones I have tested.
184 return (format
== PIXMAN_x8r8g8b8
||
185 format
== PIXMAN_b8g8r8x8
||
186 format
== PIXMAN_x1r5g5b5
||
187 format
== PIXMAN_r5g6b5
);
190 /* generic keyboard conversion */
192 #include "sdl_keysym.h"
194 static kbd_layout_t
*kbd_layout
= NULL
;
196 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
199 /* workaround for X11+SDL bug with AltGR */
200 keysym
= ev
->keysym
.sym
;
201 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
203 /* For Japanese key '\' and '|' */
204 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
207 return keysym2scancode(kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
210 /* specific keyboard conversions from scan codes */
214 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
216 return ev
->keysym
.scancode
;
221 #if defined(SDL_VIDEO_DRIVER_X11)
222 #include <X11/XKBlib.h>
224 static int check_for_evdev(void)
227 XkbDescPtr desc
= NULL
;
229 char *keycodes
= NULL
;
231 SDL_VERSION(&info
.version
);
232 if (!SDL_GetWMInfo(&info
)) {
235 desc
= XkbGetKeyboard(info
.info
.x11
.display
,
236 XkbGBN_AllComponentsMask
,
238 if (desc
&& desc
->names
) {
239 keycodes
= XGetAtomName(info
.info
.x11
.display
, desc
->names
->keycodes
);
240 if (keycodes
== NULL
) {
241 fprintf(stderr
, "could not lookup keycode name\n");
242 } else if (strstart(keycodes
, "evdev", NULL
)) {
244 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
245 fprintf(stderr
, "unknown keycodes `%s', please report to "
246 "qemu-devel@nongnu.org\n", keycodes
);
251 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
259 static int check_for_evdev(void)
265 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
268 static int has_evdev
= -1;
271 has_evdev
= check_for_evdev();
273 keycode
= ev
->keysym
.scancode
;
277 } else if (keycode
< 97) {
278 keycode
-= 8; /* just an offset */
279 } else if (keycode
< 158) {
280 /* use conversion table */
282 keycode
= translate_evdev_keycode(keycode
- 97);
284 keycode
= translate_xfree86_keycode(keycode
- 97);
285 } else if (keycode
== 208) { /* Hiragana_Katakana */
287 } else if (keycode
== 211) { /* backslash */
297 static void reset_keys(void)
300 for(i
= 0; i
< 256; i
++) {
301 if (modifiers_state
[i
]) {
302 qemu_input_event_send_key_number(dcl
->con
, i
, false);
303 modifiers_state
[i
] = 0;
308 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
312 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
314 qemu_input_event_send_key_qcode(dcl
->con
, Q_KEY_CODE_PAUSE
,
315 ev
->type
== SDL_KEYDOWN
);
320 keycode
= sdl_keyevent_to_keycode_generic(ev
);
322 keycode
= sdl_keyevent_to_keycode(ev
);
327 /* sent when leaving window: reset the modifiers state */
330 case 0x2a: /* Left Shift */
331 case 0x36: /* Right Shift */
332 case 0x1d: /* Left CTRL */
333 case 0x9d: /* Right CTRL */
334 case 0x38: /* Left ALT */
335 case 0xb8: /* Right ALT */
336 if (ev
->type
== SDL_KEYUP
)
337 modifiers_state
[keycode
] = 0;
339 modifiers_state
[keycode
] = 1;
341 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
342 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
343 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
344 case 0x45: /* num lock */
345 case 0x3a: /* caps lock */
346 /* SDL does not send the key up event, so we generate it */
347 qemu_input_event_send_key_number(dcl
->con
, keycode
, true);
348 qemu_input_event_send_key_number(dcl
->con
, keycode
, false);
353 /* now send the key code */
354 qemu_input_event_send_key_number(dcl
->con
, keycode
,
355 ev
->type
== SDL_KEYDOWN
);
358 static void sdl_update_caption(void)
360 char win_title
[1024];
361 char icon_title
[1024];
362 const char *status
= "";
364 if (!runstate_is_running())
365 status
= " [Stopped]";
368 status
= " - Press Ctrl-Alt-Shift to exit mouse grab";
370 status
= " - Press Right-Ctrl to exit mouse grab";
372 status
= " - Press Ctrl-Alt to exit mouse grab";
376 snprintf(win_title
, sizeof(win_title
), "QEMU (%s)%s", qemu_name
, status
);
377 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
379 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
380 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
383 SDL_WM_SetCaption(win_title
, icon_title
);
386 static void sdl_hide_cursor(void)
391 if (qemu_input_is_absolute()) {
393 SDL_SetCursor(sdl_cursor_hidden
);
399 static void sdl_show_cursor(void)
404 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL
)) {
407 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
408 SDL_SetCursor(guest_sprite
);
410 SDL_SetCursor(sdl_cursor_normal
);
414 static void sdl_grab_start(void)
417 * If the application is not active, do not try to enter grab state. This
418 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
419 * application (SDL bug).
421 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS
)) {
425 SDL_SetCursor(guest_sprite
);
426 if (!qemu_input_is_absolute() && !absolute_enabled
) {
427 SDL_WarpMouse(guest_x
, guest_y
);
431 SDL_WM_GrabInput(SDL_GRAB_ON
);
433 sdl_update_caption();
436 static void sdl_grab_end(void)
438 SDL_WM_GrabInput(SDL_GRAB_OFF
);
441 sdl_update_caption();
444 static void absolute_mouse_grab(void)
446 int mouse_x
, mouse_y
;
448 SDL_GetMouseState(&mouse_x
, &mouse_y
);
449 if (mouse_x
> 0 && mouse_x
< real_screen
->w
- 1 &&
450 mouse_y
> 0 && mouse_y
< real_screen
->h
- 1) {
455 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
457 if (qemu_input_is_absolute()) {
458 if (!absolute_enabled
) {
459 absolute_enabled
= 1;
460 if (qemu_console_is_graphic(NULL
)) {
461 absolute_mouse_grab();
464 } else if (absolute_enabled
) {
465 if (!gui_fullscreen
) {
468 absolute_enabled
= 0;
472 static void sdl_send_mouse_event(int dx
, int dy
, int x
, int y
, int state
)
474 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
475 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
476 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
477 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
478 [INPUT_BUTTON_WHEELUP
] = SDL_BUTTON(SDL_BUTTON_WHEELUP
),
479 [INPUT_BUTTON_WHEELDOWN
] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN
),
481 static uint32_t prev_state
;
483 if (prev_state
!= state
) {
484 qemu_input_update_buttons(dcl
->con
, bmap
, prev_state
, state
);
488 if (qemu_input_is_absolute()) {
489 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_X
, x
,
491 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_Y
, y
,
502 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_X
, dx
);
503 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_Y
, dy
);
505 qemu_input_event_sync();
508 static void sdl_scale(int width
, int height
)
510 int bpp
= real_screen
->format
->BitsPerPixel
;
513 printf("SDL: Scaling to %dx%d bpp %d\n", width
, height
, bpp
);
516 if (bpp
!= 16 && bpp
!= 32) {
519 do_sdl_resize(width
, height
, bpp
);
523 static void toggle_full_screen(void)
525 int width
= surface_width(surface
);
526 int height
= surface_height(surface
);
527 int bpp
= surface_bits_per_pixel(surface
);
529 gui_fullscreen
= !gui_fullscreen
;
530 if (gui_fullscreen
) {
531 gui_saved_width
= real_screen
->w
;
532 gui_saved_height
= real_screen
->h
;
533 gui_saved_scaling
= scaling_active
;
535 do_sdl_resize(width
, height
, bpp
);
538 gui_saved_grab
= gui_grab
;
541 if (gui_saved_scaling
) {
542 sdl_scale(gui_saved_width
, gui_saved_height
);
544 do_sdl_resize(width
, height
, 0);
546 if (!gui_saved_grab
|| !qemu_console_is_graphic(NULL
)) {
550 graphic_hw_invalidate(NULL
);
551 graphic_hw_update(NULL
);
554 static void handle_keydown(SDL_Event
*ev
)
560 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
561 (gui_grab_code
| KMOD_LSHIFT
);
562 } else if (ctrl_grab
) {
563 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
565 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
567 gui_key_modifier_pressed
= mod_state
;
569 if (gui_key_modifier_pressed
) {
570 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
572 case 0x21: /* 'f' key on US keyboard */
573 toggle_full_screen();
576 case 0x16: /* 'u' key on US keyboard */
577 if (scaling_active
) {
579 sdl_switch(dcl
, NULL
);
580 graphic_hw_invalidate(NULL
);
581 graphic_hw_update(NULL
);
585 case 0x02 ... 0x0a: /* '1' to '9' keys */
586 /* Reset the modifiers sent to the current console */
588 console_select(keycode
- 0x02);
590 if (gui_fullscreen
) {
593 if (!qemu_console_is_graphic(NULL
)) {
594 /* release grab if going to a text console */
597 } else if (absolute_enabled
) {
600 } else if (absolute_enabled
) {
602 absolute_mouse_grab();
607 if (!gui_fullscreen
) {
608 int width
= MAX(real_screen
->w
+ (keycode
== 0x1b ? 50 : -50),
610 int height
= (surface_height(surface
) * width
) /
611 surface_width(surface
);
613 sdl_scale(width
, height
);
614 graphic_hw_invalidate(NULL
);
615 graphic_hw_update(NULL
);
621 } else if (!qemu_console_is_graphic(NULL
)) {
624 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
625 switch (ev
->key
.keysym
.sym
) {
627 keysym
= QEMU_KEY_CTRL_UP
;
630 keysym
= QEMU_KEY_CTRL_DOWN
;
633 keysym
= QEMU_KEY_CTRL_LEFT
;
636 keysym
= QEMU_KEY_CTRL_RIGHT
;
639 keysym
= QEMU_KEY_CTRL_HOME
;
642 keysym
= QEMU_KEY_CTRL_END
;
645 keysym
= QEMU_KEY_CTRL_PAGEUP
;
648 keysym
= QEMU_KEY_CTRL_PAGEDOWN
;
654 switch (ev
->key
.keysym
.sym
) {
656 keysym
= QEMU_KEY_UP
;
659 keysym
= QEMU_KEY_DOWN
;
662 keysym
= QEMU_KEY_LEFT
;
665 keysym
= QEMU_KEY_RIGHT
;
668 keysym
= QEMU_KEY_HOME
;
671 keysym
= QEMU_KEY_END
;
674 keysym
= QEMU_KEY_PAGEUP
;
677 keysym
= QEMU_KEY_PAGEDOWN
;
680 keysym
= QEMU_KEY_BACKSPACE
;
683 keysym
= QEMU_KEY_DELETE
;
690 kbd_put_keysym(keysym
);
691 } else if (ev
->key
.keysym
.unicode
!= 0) {
692 kbd_put_keysym(ev
->key
.keysym
.unicode
);
695 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
696 sdl_process_key(&ev
->key
);
700 static void handle_keyup(SDL_Event
*ev
)
705 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
707 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
709 if (!mod_state
&& gui_key_modifier_pressed
) {
710 gui_key_modifier_pressed
= 0;
711 if (gui_keysym
== 0) {
712 /* exit/enter grab if pressing Ctrl-Alt */
714 if (qemu_console_is_graphic(NULL
)) {
717 } else if (!gui_fullscreen
) {
720 /* SDL does not send back all the modifiers key, so we must
727 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
728 sdl_process_key(&ev
->key
);
732 static void handle_mousemotion(SDL_Event
*ev
)
736 if (qemu_console_is_graphic(NULL
) &&
737 (qemu_input_is_absolute() || absolute_enabled
)) {
738 max_x
= real_screen
->w
- 1;
739 max_y
= real_screen
->h
- 1;
740 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
741 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
745 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
746 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
750 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
751 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
,
752 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
756 static void handle_mousebutton(SDL_Event
*ev
)
758 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
759 SDL_MouseButtonEvent
*bev
;
761 if (!qemu_console_is_graphic(NULL
)) {
766 if (!gui_grab
&& !qemu_input_is_absolute()) {
767 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
768 /* start grabbing all events */
772 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
773 buttonstate
|= SDL_BUTTON(bev
->button
);
775 buttonstate
&= ~SDL_BUTTON(bev
->button
);
777 sdl_send_mouse_event(0, 0, bev
->x
, bev
->y
, buttonstate
);
781 static void handle_activation(SDL_Event
*ev
)
784 /* Disable grab if the window no longer has the focus
785 * (Windows-only workaround) */
786 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
787 !ev
->active
.gain
&& !gui_fullscreen
) {
791 if (!gui_grab
&& ev
->active
.gain
&& qemu_console_is_graphic(NULL
) &&
792 (qemu_input_is_absolute() || absolute_enabled
)) {
793 absolute_mouse_grab();
795 if (ev
->active
.state
& SDL_APPACTIVE
) {
796 if (ev
->active
.gain
) {
797 /* Back to default interval */
798 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
800 /* Sleeping interval. Not using the long default here as
801 * sdl_refresh does not only update the guest screen, but
802 * also checks for gui events. */
803 update_displaychangelistener(dcl
, 500);
808 static void sdl_refresh(DisplayChangeListener
*dcl
)
810 SDL_Event ev1
, *ev
= &ev1
;
813 if (last_vm_running
!= runstate_is_running()) {
814 last_vm_running
= runstate_is_running();
815 sdl_update_caption();
818 graphic_hw_update(NULL
);
819 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL
));
821 while (SDL_PollEvent(ev
)) {
823 case SDL_VIDEOEXPOSE
:
824 sdl_update(dcl
, 0, 0, real_screen
->w
, real_screen
->h
);
837 qemu_system_shutdown_request();
840 case SDL_MOUSEMOTION
:
842 handle_mousemotion(ev
);
844 case SDL_MOUSEBUTTONDOWN
:
845 case SDL_MOUSEBUTTONUP
:
847 handle_mousebutton(ev
);
849 case SDL_ACTIVEEVENT
:
850 handle_activation(ev
);
852 case SDL_VIDEORESIZE
:
853 sdl_scale(ev
->resize
.w
, ev
->resize
.h
);
854 graphic_hw_invalidate(NULL
);
855 graphic_hw_update(NULL
);
863 if (idle_counter
< SDL_MAX_IDLE_COUNT
) {
865 if (idle_counter
>= SDL_MAX_IDLE_COUNT
) {
866 dcl
->update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
871 dcl
->update_interval
= SDL_REFRESH_INTERVAL_BUSY
;
875 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
876 int x
, int y
, int on
)
881 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
882 SDL_SetCursor(guest_sprite
);
883 if (!qemu_input_is_absolute() && !absolute_enabled
) {
890 guest_x
= x
, guest_y
= y
;
893 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
896 uint8_t *image
, *mask
;
900 SDL_FreeCursor(guest_sprite
);
902 bpl
= cursor_get_mono_bpl(c
);
903 image
= g_malloc0(bpl
* c
->height
);
904 mask
= g_malloc0(bpl
* c
->height
);
905 cursor_get_mono_image(c
, 0x000000, image
);
906 cursor_get_mono_mask(c
, 0, mask
);
907 guest_sprite
= SDL_CreateCursor(image
, mask
, c
->width
, c
->height
,
913 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
914 SDL_SetCursor(guest_sprite
);
917 static void sdl_cleanup(void)
920 SDL_FreeCursor(guest_sprite
);
921 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
924 static const DisplayChangeListenerOps dcl_ops
= {
926 .dpy_gfx_update
= sdl_update
,
927 .dpy_gfx_switch
= sdl_switch
,
928 .dpy_gfx_check_format
= sdl_check_format
,
929 .dpy_refresh
= sdl_refresh
,
930 .dpy_mouse_set
= sdl_mouse_warp
,
931 .dpy_cursor_define
= sdl_mouse_define
,
934 void sdl_display_early_init(int opengl
)
936 if (opengl
== 1 /* on */) {
938 "SDL1 display code has no opengl support.\n"
939 "Please recompile qemu with SDL2, using\n"
940 "./configure --enable-sdl --with-sdlabi=2.0\n");
944 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
948 const SDL_VideoInfo
*vi
;
951 #if defined(__APPLE__)
952 /* always use generic keymaps */
953 if (!keyboard_layout
)
954 keyboard_layout
= "en-us";
956 if(keyboard_layout
) {
957 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
966 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
969 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
970 * accessible $DISPLAY to open X11 window. This is often the case
971 * when qemu is run using sudo. But in this case, and when actually
972 * run in X11 environment, SDL fights with X11 for the video card,
973 * making current display unavailable, often until reboot.
974 * So make x11 the default SDL video driver if this variable is unset.
975 * This is a bit hackish but saves us from bigger problem.
976 * Maybe it's a good idea to fix this in SDL instead.
978 setenv("SDL_VIDEODRIVER", "x11", 0);
981 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
982 * This requires SDL >= 1.2.14. */
983 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
985 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
986 if (SDL_Init (flags
)) {
987 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
991 vi
= SDL_GetVideoInfo();
992 host_format
= *(vi
->vfmt
);
994 /* Load a 32x32x4 image. White pixels are transparent. */
995 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
997 SDL_Surface
*image
= SDL_LoadBMP(filename
);
999 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
1000 SDL_SetColorKey(image
, SDL_SRCCOLORKEY
, colorkey
);
1001 SDL_WM_SetIcon(image
, NULL
);
1011 dcl
= g_new0(DisplayChangeListener
, 1);
1012 dcl
->ops
= &dcl_ops
;
1013 register_displaychangelistener(dcl
);
1015 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
1016 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
1018 sdl_update_caption();
1019 SDL_EnableKeyRepeat(250, 50);
1022 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
1023 sdl_cursor_normal
= SDL_GetCursor();
1025 atexit(sdl_cleanup
);