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 "qemu/cutils.h"
34 #include "ui/console.h"
36 #include "sysemu/sysemu.h"
40 static DisplayChangeListener
*dcl
;
41 static DisplaySurface
*surface
;
42 static SDL_Surface
*real_screen
;
43 static SDL_Surface
*guest_screen
= NULL
;
44 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
45 static int last_vm_running
;
46 static bool gui_saved_scaling
;
47 static int gui_saved_width
;
48 static int gui_saved_height
;
49 static int gui_saved_grab
;
50 static int gui_fullscreen
;
51 static int gui_noframe
;
52 static int gui_key_modifier_pressed
;
53 static int gui_keysym
;
54 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
55 static uint8_t modifiers_state
[256];
56 static SDL_Cursor
*sdl_cursor_normal
;
57 static SDL_Cursor
*sdl_cursor_hidden
;
58 static int absolute_enabled
= 0;
59 static int guest_cursor
= 0;
60 static int guest_x
, guest_y
;
61 static SDL_Cursor
*guest_sprite
= NULL
;
62 static SDL_PixelFormat host_format
;
63 static int scaling_active
= 0;
64 static Notifier mouse_mode_notifier
;
65 static int idle_counter
;
67 #define SDL_REFRESH_INTERVAL_BUSY 10
68 #define SDL_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
69 / SDL_REFRESH_INTERVAL_BUSY + 1)
75 static void sdl_update(DisplayChangeListener
*dcl
,
76 int x
, int y
, int w
, int h
)
85 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
86 x
, y
, w
, h
, scaling_active
);
90 if (!scaling_active
) {
91 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
93 if (sdl_zoom_blit(guest_screen
, real_screen
, SMOOTHING_ON
, &rec
) < 0) {
94 fprintf(stderr
, "Zoom blit failed\n");
99 SDL_UpdateRect(real_screen
, rec
.x
, rec
.y
, rec
.w
, rec
.h
);
102 static void do_sdl_resize(int width
, int height
, int bpp
)
105 SDL_Surface
*tmp_screen
;
108 printf("SDL: Resizing to %dx%d bpp %d\n", width
, height
, bpp
);
111 flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
;
112 if (gui_fullscreen
) {
113 flags
|= SDL_FULLSCREEN
;
115 flags
|= SDL_RESIZABLE
;
118 flags
|= SDL_NOFRAME
;
120 tmp_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
123 fprintf(stderr
, "Could not open SDL display (%dx%dx%d): %s\n",
124 width
, height
, bpp
, SDL_GetError());
129 * Revert to the previous video mode if the change of resizing or
133 fprintf(stderr
, "Failed to set SDL display (%dx%dx%d): %s\n",
134 width
, height
, bpp
, SDL_GetError());
139 real_screen
= tmp_screen
;
142 static void sdl_switch(DisplayChangeListener
*dcl
,
143 DisplaySurface
*new_surface
)
147 /* temporary hack: allows to call sdl_switch to handle scaling changes */
149 surface
= new_surface
;
151 pf
= qemu_pixelformat_from_pixman(surface
->format
);
153 if (!scaling_active
) {
154 do_sdl_resize(surface_width(surface
), surface_height(surface
), 0);
155 } else if (real_screen
->format
->BitsPerPixel
!=
156 surface_bits_per_pixel(surface
)) {
157 do_sdl_resize(real_screen
->w
, real_screen
->h
,
158 surface_bits_per_pixel(surface
));
161 if (guest_screen
!= NULL
) {
162 SDL_FreeSurface(guest_screen
);
166 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
167 pf
.rmask
, pf
.gmask
, pf
.bmask
, pf
.amask
);
170 guest_screen
= SDL_CreateRGBSurfaceFrom
171 (surface_data(surface
),
172 surface_width(surface
), surface_height(surface
),
173 surface_bits_per_pixel(surface
), surface_stride(surface
),
178 static bool sdl_check_format(DisplayChangeListener
*dcl
,
179 pixman_format_code_t format
)
182 * We let SDL convert for us a few more formats than,
183 * the native ones. Thes are the ones I have tested.
185 return (format
== PIXMAN_x8r8g8b8
||
186 format
== PIXMAN_b8g8r8x8
||
187 format
== PIXMAN_x1r5g5b5
||
188 format
== PIXMAN_r5g6b5
);
191 /* generic keyboard conversion */
193 #include "sdl_keysym.h"
195 static kbd_layout_t
*kbd_layout
= NULL
;
197 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
200 /* workaround for X11+SDL bug with AltGR */
201 keysym
= ev
->keysym
.sym
;
202 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
204 /* For Japanese key '\' and '|' */
205 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
208 return keysym2scancode(kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
211 /* specific keyboard conversions from scan codes */
215 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
217 return ev
->keysym
.scancode
;
222 #if defined(SDL_VIDEO_DRIVER_X11)
223 #include <X11/XKBlib.h>
225 static int check_for_evdev(void)
228 XkbDescPtr desc
= NULL
;
230 char *keycodes
= NULL
;
232 SDL_VERSION(&info
.version
);
233 if (!SDL_GetWMInfo(&info
)) {
236 desc
= XkbGetKeyboard(info
.info
.x11
.display
,
237 XkbGBN_AllComponentsMask
,
239 if (desc
&& desc
->names
) {
240 keycodes
= XGetAtomName(info
.info
.x11
.display
, desc
->names
->keycodes
);
241 if (keycodes
== NULL
) {
242 fprintf(stderr
, "could not lookup keycode name\n");
243 } else if (strstart(keycodes
, "evdev", NULL
)) {
245 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
246 fprintf(stderr
, "unknown keycodes `%s', please report to "
247 "qemu-devel@nongnu.org\n", keycodes
);
252 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
260 static int check_for_evdev(void)
266 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
269 static int has_evdev
= -1;
272 has_evdev
= check_for_evdev();
274 keycode
= ev
->keysym
.scancode
;
278 } else if (keycode
< 97) {
279 keycode
-= 8; /* just an offset */
280 } else if (keycode
< 158) {
281 /* use conversion table */
283 keycode
= translate_evdev_keycode(keycode
- 97);
285 keycode
= translate_xfree86_keycode(keycode
- 97);
286 } else if (keycode
== 208) { /* Hiragana_Katakana */
288 } else if (keycode
== 211) { /* backslash */
298 static void reset_keys(void)
301 for(i
= 0; i
< 256; i
++) {
302 if (modifiers_state
[i
]) {
303 qemu_input_event_send_key_number(dcl
->con
, i
, false);
304 modifiers_state
[i
] = 0;
309 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
313 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
315 qemu_input_event_send_key_qcode(dcl
->con
, Q_KEY_CODE_PAUSE
,
316 ev
->type
== SDL_KEYDOWN
);
321 keycode
= sdl_keyevent_to_keycode_generic(ev
);
323 keycode
= sdl_keyevent_to_keycode(ev
);
328 /* sent when leaving window: reset the modifiers state */
331 case 0x2a: /* Left Shift */
332 case 0x36: /* Right Shift */
333 case 0x1d: /* Left CTRL */
334 case 0x9d: /* Right CTRL */
335 case 0x38: /* Left ALT */
336 case 0xb8: /* Right ALT */
337 if (ev
->type
== SDL_KEYUP
)
338 modifiers_state
[keycode
] = 0;
340 modifiers_state
[keycode
] = 1;
342 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
343 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
344 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
345 case 0x45: /* num lock */
346 case 0x3a: /* caps lock */
347 /* SDL does not send the key up event, so we generate it */
348 qemu_input_event_send_key_number(dcl
->con
, keycode
, true);
349 qemu_input_event_send_key_number(dcl
->con
, keycode
, false);
354 /* now send the key code */
355 qemu_input_event_send_key_number(dcl
->con
, keycode
,
356 ev
->type
== SDL_KEYDOWN
);
359 static void sdl_update_caption(void)
361 char win_title
[1024];
362 char icon_title
[1024];
363 const char *status
= "";
365 if (!runstate_is_running())
366 status
= " [Stopped]";
369 status
= " - Press Ctrl-Alt-Shift to exit mouse grab";
371 status
= " - Press Right-Ctrl to exit mouse grab";
373 status
= " - Press Ctrl-Alt to exit mouse grab";
377 snprintf(win_title
, sizeof(win_title
), "QEMU (%s)%s", qemu_name
, status
);
378 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
380 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
381 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
384 SDL_WM_SetCaption(win_title
, icon_title
);
387 static void sdl_hide_cursor(void)
392 if (qemu_input_is_absolute()) {
394 SDL_SetCursor(sdl_cursor_hidden
);
400 static void sdl_show_cursor(void)
405 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL
)) {
408 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
409 SDL_SetCursor(guest_sprite
);
411 SDL_SetCursor(sdl_cursor_normal
);
415 static void sdl_grab_start(void)
418 * If the application is not active, do not try to enter grab state. This
419 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
420 * application (SDL bug).
422 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS
)) {
426 SDL_SetCursor(guest_sprite
);
427 if (!qemu_input_is_absolute() && !absolute_enabled
) {
428 SDL_WarpMouse(guest_x
, guest_y
);
432 SDL_WM_GrabInput(SDL_GRAB_ON
);
434 sdl_update_caption();
437 static void sdl_grab_end(void)
439 SDL_WM_GrabInput(SDL_GRAB_OFF
);
442 sdl_update_caption();
445 static void absolute_mouse_grab(void)
447 int mouse_x
, mouse_y
;
449 SDL_GetMouseState(&mouse_x
, &mouse_y
);
450 if (mouse_x
> 0 && mouse_x
< real_screen
->w
- 1 &&
451 mouse_y
> 0 && mouse_y
< real_screen
->h
- 1) {
456 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
458 if (qemu_input_is_absolute()) {
459 if (!absolute_enabled
) {
460 absolute_enabled
= 1;
461 if (qemu_console_is_graphic(NULL
)) {
462 absolute_mouse_grab();
465 } else if (absolute_enabled
) {
466 if (!gui_fullscreen
) {
469 absolute_enabled
= 0;
473 static void sdl_send_mouse_event(int dx
, int dy
, int x
, int y
, int state
)
475 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
476 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
477 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
478 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
479 [INPUT_BUTTON_WHEEL_UP
] = SDL_BUTTON(SDL_BUTTON_WHEELUP
),
480 [INPUT_BUTTON_WHEEL_DOWN
] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN
),
482 static uint32_t prev_state
;
484 if (prev_state
!= state
) {
485 qemu_input_update_buttons(dcl
->con
, bmap
, prev_state
, state
);
489 if (qemu_input_is_absolute()) {
490 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_X
, x
,
492 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_Y
, y
,
503 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_X
, dx
);
504 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_Y
, dy
);
506 qemu_input_event_sync();
509 static void sdl_scale(int width
, int height
)
511 int bpp
= real_screen
->format
->BitsPerPixel
;
514 printf("SDL: Scaling to %dx%d bpp %d\n", width
, height
, bpp
);
517 if (bpp
!= 16 && bpp
!= 32) {
520 do_sdl_resize(width
, height
, bpp
);
524 static void toggle_full_screen(void)
526 int width
= surface_width(surface
);
527 int height
= surface_height(surface
);
528 int bpp
= surface_bits_per_pixel(surface
);
530 gui_fullscreen
= !gui_fullscreen
;
531 if (gui_fullscreen
) {
532 gui_saved_width
= real_screen
->w
;
533 gui_saved_height
= real_screen
->h
;
534 gui_saved_scaling
= scaling_active
;
536 do_sdl_resize(width
, height
, bpp
);
539 gui_saved_grab
= gui_grab
;
542 if (gui_saved_scaling
) {
543 sdl_scale(gui_saved_width
, gui_saved_height
);
545 do_sdl_resize(width
, height
, 0);
547 if (!gui_saved_grab
|| !qemu_console_is_graphic(NULL
)) {
551 graphic_hw_invalidate(NULL
);
552 graphic_hw_update(NULL
);
555 static void handle_keydown(SDL_Event
*ev
)
561 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
562 (gui_grab_code
| KMOD_LSHIFT
);
563 } else if (ctrl_grab
) {
564 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
566 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
568 gui_key_modifier_pressed
= mod_state
;
570 if (gui_key_modifier_pressed
) {
571 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
573 case 0x21: /* 'f' key on US keyboard */
574 toggle_full_screen();
577 case 0x16: /* 'u' key on US keyboard */
578 if (scaling_active
) {
580 sdl_switch(dcl
, NULL
);
581 graphic_hw_invalidate(NULL
);
582 graphic_hw_update(NULL
);
586 case 0x02 ... 0x0a: /* '1' to '9' keys */
587 /* Reset the modifiers sent to the current console */
589 console_select(keycode
- 0x02);
591 if (gui_fullscreen
) {
594 if (!qemu_console_is_graphic(NULL
)) {
595 /* release grab if going to a text console */
598 } else if (absolute_enabled
) {
601 } else if (absolute_enabled
) {
603 absolute_mouse_grab();
608 if (!gui_fullscreen
) {
609 int width
= MAX(real_screen
->w
+ (keycode
== 0x1b ? 50 : -50),
611 int height
= (surface_height(surface
) * width
) /
612 surface_width(surface
);
614 sdl_scale(width
, height
);
615 graphic_hw_invalidate(NULL
);
616 graphic_hw_update(NULL
);
622 } else if (!qemu_console_is_graphic(NULL
)) {
625 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
626 switch (ev
->key
.keysym
.sym
) {
628 keysym
= QEMU_KEY_CTRL_UP
;
631 keysym
= QEMU_KEY_CTRL_DOWN
;
634 keysym
= QEMU_KEY_CTRL_LEFT
;
637 keysym
= QEMU_KEY_CTRL_RIGHT
;
640 keysym
= QEMU_KEY_CTRL_HOME
;
643 keysym
= QEMU_KEY_CTRL_END
;
646 keysym
= QEMU_KEY_CTRL_PAGEUP
;
649 keysym
= QEMU_KEY_CTRL_PAGEDOWN
;
655 switch (ev
->key
.keysym
.sym
) {
657 keysym
= QEMU_KEY_UP
;
660 keysym
= QEMU_KEY_DOWN
;
663 keysym
= QEMU_KEY_LEFT
;
666 keysym
= QEMU_KEY_RIGHT
;
669 keysym
= QEMU_KEY_HOME
;
672 keysym
= QEMU_KEY_END
;
675 keysym
= QEMU_KEY_PAGEUP
;
678 keysym
= QEMU_KEY_PAGEDOWN
;
681 keysym
= QEMU_KEY_BACKSPACE
;
684 keysym
= QEMU_KEY_DELETE
;
691 kbd_put_keysym(keysym
);
692 } else if (ev
->key
.keysym
.unicode
!= 0) {
693 kbd_put_keysym(ev
->key
.keysym
.unicode
);
696 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
697 sdl_process_key(&ev
->key
);
701 static void handle_keyup(SDL_Event
*ev
)
706 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
708 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
710 if (!mod_state
&& gui_key_modifier_pressed
) {
711 gui_key_modifier_pressed
= 0;
712 if (gui_keysym
== 0) {
713 /* exit/enter grab if pressing Ctrl-Alt */
715 if (qemu_console_is_graphic(NULL
)) {
718 } else if (!gui_fullscreen
) {
721 /* SDL does not send back all the modifiers key, so we must
728 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
729 sdl_process_key(&ev
->key
);
733 static void handle_mousemotion(SDL_Event
*ev
)
737 if (qemu_console_is_graphic(NULL
) &&
738 (qemu_input_is_absolute() || absolute_enabled
)) {
739 max_x
= real_screen
->w
- 1;
740 max_y
= real_screen
->h
- 1;
741 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
742 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
746 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
747 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
751 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
752 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
,
753 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
757 static void handle_mousebutton(SDL_Event
*ev
)
759 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
760 SDL_MouseButtonEvent
*bev
;
762 if (!qemu_console_is_graphic(NULL
)) {
767 if (!gui_grab
&& !qemu_input_is_absolute()) {
768 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
769 /* start grabbing all events */
773 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
774 buttonstate
|= SDL_BUTTON(bev
->button
);
776 buttonstate
&= ~SDL_BUTTON(bev
->button
);
778 sdl_send_mouse_event(0, 0, bev
->x
, bev
->y
, buttonstate
);
782 static void handle_activation(SDL_Event
*ev
)
785 /* Disable grab if the window no longer has the focus
786 * (Windows-only workaround) */
787 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
788 !ev
->active
.gain
&& !gui_fullscreen
) {
792 if (!gui_grab
&& ev
->active
.gain
&& qemu_console_is_graphic(NULL
) &&
793 (qemu_input_is_absolute() || absolute_enabled
)) {
794 absolute_mouse_grab();
796 if (ev
->active
.state
& SDL_APPACTIVE
) {
797 if (ev
->active
.gain
) {
798 /* Back to default interval */
799 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
801 /* Sleeping interval. Not using the long default here as
802 * sdl_refresh does not only update the guest screen, but
803 * also checks for gui events. */
804 update_displaychangelistener(dcl
, 500);
809 static void sdl_refresh(DisplayChangeListener
*dcl
)
811 SDL_Event ev1
, *ev
= &ev1
;
814 if (last_vm_running
!= runstate_is_running()) {
815 last_vm_running
= runstate_is_running();
816 sdl_update_caption();
819 graphic_hw_update(NULL
);
820 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL
));
822 while (SDL_PollEvent(ev
)) {
824 case SDL_VIDEOEXPOSE
:
825 sdl_update(dcl
, 0, 0, real_screen
->w
, real_screen
->h
);
838 qemu_system_shutdown_request();
841 case SDL_MOUSEMOTION
:
843 handle_mousemotion(ev
);
845 case SDL_MOUSEBUTTONDOWN
:
846 case SDL_MOUSEBUTTONUP
:
848 handle_mousebutton(ev
);
850 case SDL_ACTIVEEVENT
:
851 handle_activation(ev
);
853 case SDL_VIDEORESIZE
:
854 sdl_scale(ev
->resize
.w
, ev
->resize
.h
);
855 graphic_hw_invalidate(NULL
);
856 graphic_hw_update(NULL
);
864 if (idle_counter
< SDL_MAX_IDLE_COUNT
) {
866 if (idle_counter
>= SDL_MAX_IDLE_COUNT
) {
867 dcl
->update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
872 dcl
->update_interval
= SDL_REFRESH_INTERVAL_BUSY
;
876 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
877 int x
, int y
, int on
)
882 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
883 SDL_SetCursor(guest_sprite
);
884 if (!qemu_input_is_absolute() && !absolute_enabled
) {
891 guest_x
= x
, guest_y
= y
;
894 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
897 uint8_t *image
, *mask
;
901 SDL_FreeCursor(guest_sprite
);
903 bpl
= cursor_get_mono_bpl(c
);
904 image
= g_malloc0(bpl
* c
->height
);
905 mask
= g_malloc0(bpl
* c
->height
);
906 cursor_get_mono_image(c
, 0x000000, image
);
907 cursor_get_mono_mask(c
, 0, mask
);
908 guest_sprite
= SDL_CreateCursor(image
, mask
, c
->width
, c
->height
,
914 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
915 SDL_SetCursor(guest_sprite
);
918 static void sdl_cleanup(void)
921 SDL_FreeCursor(guest_sprite
);
922 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
925 static const DisplayChangeListenerOps dcl_ops
= {
927 .dpy_gfx_update
= sdl_update
,
928 .dpy_gfx_switch
= sdl_switch
,
929 .dpy_gfx_check_format
= sdl_check_format
,
930 .dpy_refresh
= sdl_refresh
,
931 .dpy_mouse_set
= sdl_mouse_warp
,
932 .dpy_cursor_define
= sdl_mouse_define
,
935 void sdl_display_early_init(int opengl
)
937 if (opengl
== 1 /* on */) {
939 "SDL1 display code has no opengl support.\n"
940 "Please recompile qemu with SDL2, using\n"
941 "./configure --enable-sdl --with-sdlabi=2.0\n");
945 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
949 const SDL_VideoInfo
*vi
;
952 #if defined(__APPLE__)
953 /* always use generic keymaps */
954 if (!keyboard_layout
)
955 keyboard_layout
= "en-us";
957 if(keyboard_layout
) {
958 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
967 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
970 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
971 * accessible $DISPLAY to open X11 window. This is often the case
972 * when qemu is run using sudo. But in this case, and when actually
973 * run in X11 environment, SDL fights with X11 for the video card,
974 * making current display unavailable, often until reboot.
975 * So make x11 the default SDL video driver if this variable is unset.
976 * This is a bit hackish but saves us from bigger problem.
977 * Maybe it's a good idea to fix this in SDL instead.
979 setenv("SDL_VIDEODRIVER", "x11", 0);
982 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
983 * This requires SDL >= 1.2.14. */
984 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
986 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
987 if (SDL_Init (flags
)) {
988 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
992 vi
= SDL_GetVideoInfo();
993 host_format
= *(vi
->vfmt
);
995 /* Load a 32x32x4 image. White pixels are transparent. */
996 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
998 SDL_Surface
*image
= SDL_LoadBMP(filename
);
1000 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
1001 SDL_SetColorKey(image
, SDL_SRCCOLORKEY
, colorkey
);
1002 SDL_WM_SetIcon(image
, NULL
);
1012 dcl
= g_new0(DisplayChangeListener
, 1);
1013 dcl
->ops
= &dcl_ops
;
1014 register_displaychangelistener(dcl
);
1016 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
1017 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
1019 sdl_update_caption();
1020 SDL_EnableKeyRepeat(250, 50);
1023 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
1024 sdl_cursor_normal
= SDL_GetCursor();
1026 atexit(sdl_cleanup
);