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
;
63 static int idle_counter
;
65 #define SDL_REFRESH_INTERVAL_BUSY 10
66 #define SDL_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
67 / SDL_REFRESH_INTERVAL_BUSY + 1)
73 static void sdl_update(DisplayChangeListener
*dcl
,
74 int x
, int y
, int w
, int h
)
83 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
84 x
, y
, w
, h
, scaling_active
);
88 if (!scaling_active
) {
89 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
91 if (sdl_zoom_blit(guest_screen
, real_screen
, SMOOTHING_ON
, &rec
) < 0) {
92 fprintf(stderr
, "Zoom blit failed\n");
97 SDL_UpdateRect(real_screen
, rec
.x
, rec
.y
, rec
.w
, rec
.h
);
100 static void do_sdl_resize(int width
, int height
, int bpp
)
103 SDL_Surface
*tmp_screen
;
106 printf("SDL: Resizing to %dx%d bpp %d\n", width
, height
, bpp
);
109 flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
;
110 if (gui_fullscreen
) {
111 flags
|= SDL_FULLSCREEN
;
113 flags
|= SDL_RESIZABLE
;
116 flags
|= SDL_NOFRAME
;
118 tmp_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
121 fprintf(stderr
, "Could not open SDL display (%dx%dx%d): %s\n",
122 width
, height
, bpp
, SDL_GetError());
127 * Revert to the previous video mode if the change of resizing or
131 fprintf(stderr
, "Failed to set SDL display (%dx%dx%d): %s\n",
132 width
, height
, bpp
, SDL_GetError());
137 real_screen
= tmp_screen
;
140 static void sdl_switch(DisplayChangeListener
*dcl
,
141 DisplaySurface
*new_surface
)
145 /* temporary hack: allows to call sdl_switch to handle scaling changes */
147 surface
= new_surface
;
149 pf
= qemu_pixelformat_from_pixman(surface
->format
);
151 if (!scaling_active
) {
152 do_sdl_resize(surface_width(surface
), surface_height(surface
), 0);
153 } else if (real_screen
->format
->BitsPerPixel
!=
154 surface_bits_per_pixel(surface
)) {
155 do_sdl_resize(real_screen
->w
, real_screen
->h
,
156 surface_bits_per_pixel(surface
));
159 if (guest_screen
!= NULL
) {
160 SDL_FreeSurface(guest_screen
);
164 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
165 pf
.rmask
, pf
.gmask
, pf
.bmask
, pf
.amask
);
168 guest_screen
= SDL_CreateRGBSurfaceFrom
169 (surface_data(surface
),
170 surface_width(surface
), surface_height(surface
),
171 surface_bits_per_pixel(surface
), surface_stride(surface
),
176 static bool sdl_check_format(DisplayChangeListener
*dcl
,
177 pixman_format_code_t format
)
180 * We let SDL convert for us a few more formats than,
181 * the native ones. Thes are the ones I have tested.
183 return (format
== PIXMAN_x8r8g8b8
||
184 format
== PIXMAN_b8g8r8x8
||
185 format
== PIXMAN_x1r5g5b5
||
186 format
== PIXMAN_r5g6b5
);
189 /* generic keyboard conversion */
191 #include "sdl_keysym.h"
193 static kbd_layout_t
*kbd_layout
= NULL
;
195 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
198 /* workaround for X11+SDL bug with AltGR */
199 keysym
= ev
->keysym
.sym
;
200 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
202 /* For Japanese key '\' and '|' */
203 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
206 return keysym2scancode(kbd_layout
, keysym
) & SCANCODE_KEYMASK
;
209 /* specific keyboard conversions from scan codes */
213 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
215 return ev
->keysym
.scancode
;
220 #if defined(SDL_VIDEO_DRIVER_X11)
221 #include <X11/XKBlib.h>
223 static int check_for_evdev(void)
226 XkbDescPtr desc
= NULL
;
228 char *keycodes
= NULL
;
230 SDL_VERSION(&info
.version
);
231 if (!SDL_GetWMInfo(&info
)) {
234 desc
= XkbGetKeyboard(info
.info
.x11
.display
,
235 XkbGBN_AllComponentsMask
,
237 if (desc
&& desc
->names
) {
238 keycodes
= XGetAtomName(info
.info
.x11
.display
, desc
->names
->keycodes
);
239 if (keycodes
== NULL
) {
240 fprintf(stderr
, "could not lookup keycode name\n");
241 } else if (strstart(keycodes
, "evdev", NULL
)) {
243 } else if (!strstart(keycodes
, "xfree86", NULL
)) {
244 fprintf(stderr
, "unknown keycodes `%s', please report to "
245 "qemu-devel@nongnu.org\n", keycodes
);
250 XkbFreeKeyboard(desc
, XkbGBN_AllComponentsMask
, True
);
258 static int check_for_evdev(void)
264 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
267 static int has_evdev
= -1;
270 has_evdev
= check_for_evdev();
272 keycode
= ev
->keysym
.scancode
;
276 } else if (keycode
< 97) {
277 keycode
-= 8; /* just an offset */
278 } else if (keycode
< 158) {
279 /* use conversion table */
281 keycode
= translate_evdev_keycode(keycode
- 97);
283 keycode
= translate_xfree86_keycode(keycode
- 97);
284 } else if (keycode
== 208) { /* Hiragana_Katakana */
286 } else if (keycode
== 211) { /* backslash */
296 static void reset_keys(void)
299 for(i
= 0; i
< 256; i
++) {
300 if (modifiers_state
[i
]) {
301 qemu_input_event_send_key_number(dcl
->con
, i
, false);
302 modifiers_state
[i
] = 0;
307 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
311 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
313 qemu_input_event_send_key_qcode(dcl
->con
, Q_KEY_CODE_PAUSE
,
314 ev
->type
== SDL_KEYDOWN
);
319 keycode
= sdl_keyevent_to_keycode_generic(ev
);
321 keycode
= sdl_keyevent_to_keycode(ev
);
326 /* sent when leaving window: reset the modifiers state */
329 case 0x2a: /* Left Shift */
330 case 0x36: /* Right Shift */
331 case 0x1d: /* Left CTRL */
332 case 0x9d: /* Right CTRL */
333 case 0x38: /* Left ALT */
334 case 0xb8: /* Right ALT */
335 if (ev
->type
== SDL_KEYUP
)
336 modifiers_state
[keycode
] = 0;
338 modifiers_state
[keycode
] = 1;
340 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
341 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
342 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
343 case 0x45: /* num lock */
344 case 0x3a: /* caps lock */
345 /* SDL does not send the key up event, so we generate it */
346 qemu_input_event_send_key_number(dcl
->con
, keycode
, true);
347 qemu_input_event_send_key_number(dcl
->con
, keycode
, false);
352 /* now send the key code */
353 qemu_input_event_send_key_number(dcl
->con
, keycode
,
354 ev
->type
== SDL_KEYDOWN
);
357 static void sdl_update_caption(void)
359 char win_title
[1024];
360 char icon_title
[1024];
361 const char *status
= "";
363 if (!runstate_is_running())
364 status
= " [Stopped]";
367 status
= " - Press Ctrl-Alt-Shift to exit mouse grab";
369 status
= " - Press Right-Ctrl to exit mouse grab";
371 status
= " - Press Ctrl-Alt to exit mouse grab";
375 snprintf(win_title
, sizeof(win_title
), "QEMU (%s)%s", qemu_name
, status
);
376 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
378 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
379 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
382 SDL_WM_SetCaption(win_title
, icon_title
);
385 static void sdl_hide_cursor(void)
390 if (qemu_input_is_absolute()) {
392 SDL_SetCursor(sdl_cursor_hidden
);
398 static void sdl_show_cursor(void)
403 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL
)) {
406 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
407 SDL_SetCursor(guest_sprite
);
409 SDL_SetCursor(sdl_cursor_normal
);
413 static void sdl_grab_start(void)
416 * If the application is not active, do not try to enter grab state. This
417 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
418 * application (SDL bug).
420 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS
)) {
424 SDL_SetCursor(guest_sprite
);
425 if (!qemu_input_is_absolute() && !absolute_enabled
) {
426 SDL_WarpMouse(guest_x
, guest_y
);
430 SDL_WM_GrabInput(SDL_GRAB_ON
);
432 sdl_update_caption();
435 static void sdl_grab_end(void)
437 SDL_WM_GrabInput(SDL_GRAB_OFF
);
440 sdl_update_caption();
443 static void absolute_mouse_grab(void)
445 int mouse_x
, mouse_y
;
447 SDL_GetMouseState(&mouse_x
, &mouse_y
);
448 if (mouse_x
> 0 && mouse_x
< real_screen
->w
- 1 &&
449 mouse_y
> 0 && mouse_y
< real_screen
->h
- 1) {
454 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
456 if (qemu_input_is_absolute()) {
457 if (!absolute_enabled
) {
458 absolute_enabled
= 1;
459 if (qemu_console_is_graphic(NULL
)) {
460 absolute_mouse_grab();
463 } else if (absolute_enabled
) {
464 if (!gui_fullscreen
) {
467 absolute_enabled
= 0;
471 static void sdl_send_mouse_event(int dx
, int dy
, int x
, int y
, int state
)
473 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
474 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
475 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
476 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
477 [INPUT_BUTTON_WHEELUP
] = SDL_BUTTON(SDL_BUTTON_WHEELUP
),
478 [INPUT_BUTTON_WHEELDOWN
] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN
),
480 static uint32_t prev_state
;
482 if (prev_state
!= state
) {
483 qemu_input_update_buttons(dcl
->con
, bmap
, prev_state
, state
);
487 if (qemu_input_is_absolute()) {
488 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_X
, x
,
490 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_Y
, y
,
501 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_X
, dx
);
502 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_Y
, dy
);
504 qemu_input_event_sync();
507 static void sdl_scale(int width
, int height
)
509 int bpp
= real_screen
->format
->BitsPerPixel
;
512 printf("SDL: Scaling to %dx%d bpp %d\n", width
, height
, bpp
);
515 if (bpp
!= 16 && bpp
!= 32) {
518 do_sdl_resize(width
, height
, bpp
);
522 static void toggle_full_screen(void)
524 int width
= surface_width(surface
);
525 int height
= surface_height(surface
);
526 int bpp
= surface_bits_per_pixel(surface
);
528 gui_fullscreen
= !gui_fullscreen
;
529 if (gui_fullscreen
) {
530 gui_saved_width
= real_screen
->w
;
531 gui_saved_height
= real_screen
->h
;
532 gui_saved_scaling
= scaling_active
;
534 do_sdl_resize(width
, height
, bpp
);
537 gui_saved_grab
= gui_grab
;
540 if (gui_saved_scaling
) {
541 sdl_scale(gui_saved_width
, gui_saved_height
);
543 do_sdl_resize(width
, height
, 0);
545 if (!gui_saved_grab
|| !qemu_console_is_graphic(NULL
)) {
549 graphic_hw_invalidate(NULL
);
550 graphic_hw_update(NULL
);
553 static void handle_keydown(SDL_Event
*ev
)
559 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
560 (gui_grab_code
| KMOD_LSHIFT
);
561 } else if (ctrl_grab
) {
562 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
564 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
566 gui_key_modifier_pressed
= mod_state
;
568 if (gui_key_modifier_pressed
) {
569 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
571 case 0x21: /* 'f' key on US keyboard */
572 toggle_full_screen();
575 case 0x16: /* 'u' key on US keyboard */
576 if (scaling_active
) {
578 sdl_switch(dcl
, NULL
);
579 graphic_hw_invalidate(NULL
);
580 graphic_hw_update(NULL
);
584 case 0x02 ... 0x0a: /* '1' to '9' keys */
585 /* Reset the modifiers sent to the current console */
587 console_select(keycode
- 0x02);
589 if (gui_fullscreen
) {
592 if (!qemu_console_is_graphic(NULL
)) {
593 /* release grab if going to a text console */
596 } else if (absolute_enabled
) {
599 } else if (absolute_enabled
) {
601 absolute_mouse_grab();
606 if (!gui_fullscreen
) {
607 int width
= MAX(real_screen
->w
+ (keycode
== 0x1b ? 50 : -50),
609 int height
= (surface_height(surface
) * width
) /
610 surface_width(surface
);
612 sdl_scale(width
, height
);
613 graphic_hw_invalidate(NULL
);
614 graphic_hw_update(NULL
);
620 } else if (!qemu_console_is_graphic(NULL
)) {
623 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
624 switch (ev
->key
.keysym
.sym
) {
626 keysym
= QEMU_KEY_CTRL_UP
;
629 keysym
= QEMU_KEY_CTRL_DOWN
;
632 keysym
= QEMU_KEY_CTRL_LEFT
;
635 keysym
= QEMU_KEY_CTRL_RIGHT
;
638 keysym
= QEMU_KEY_CTRL_HOME
;
641 keysym
= QEMU_KEY_CTRL_END
;
644 keysym
= QEMU_KEY_CTRL_PAGEUP
;
647 keysym
= QEMU_KEY_CTRL_PAGEDOWN
;
653 switch (ev
->key
.keysym
.sym
) {
655 keysym
= QEMU_KEY_UP
;
658 keysym
= QEMU_KEY_DOWN
;
661 keysym
= QEMU_KEY_LEFT
;
664 keysym
= QEMU_KEY_RIGHT
;
667 keysym
= QEMU_KEY_HOME
;
670 keysym
= QEMU_KEY_END
;
673 keysym
= QEMU_KEY_PAGEUP
;
676 keysym
= QEMU_KEY_PAGEDOWN
;
679 keysym
= QEMU_KEY_BACKSPACE
;
682 keysym
= QEMU_KEY_DELETE
;
689 kbd_put_keysym(keysym
);
690 } else if (ev
->key
.keysym
.unicode
!= 0) {
691 kbd_put_keysym(ev
->key
.keysym
.unicode
);
694 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
695 sdl_process_key(&ev
->key
);
699 static void handle_keyup(SDL_Event
*ev
)
704 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
706 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
708 if (!mod_state
&& gui_key_modifier_pressed
) {
709 gui_key_modifier_pressed
= 0;
710 if (gui_keysym
== 0) {
711 /* exit/enter grab if pressing Ctrl-Alt */
713 if (qemu_console_is_graphic(NULL
)) {
716 } else if (!gui_fullscreen
) {
719 /* SDL does not send back all the modifiers key, so we must
726 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
727 sdl_process_key(&ev
->key
);
731 static void handle_mousemotion(SDL_Event
*ev
)
735 if (qemu_console_is_graphic(NULL
) &&
736 (qemu_input_is_absolute() || absolute_enabled
)) {
737 max_x
= real_screen
->w
- 1;
738 max_y
= real_screen
->h
- 1;
739 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
740 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
744 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
745 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
749 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
750 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
,
751 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
755 static void handle_mousebutton(SDL_Event
*ev
)
757 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
758 SDL_MouseButtonEvent
*bev
;
760 if (!qemu_console_is_graphic(NULL
)) {
765 if (!gui_grab
&& !qemu_input_is_absolute()) {
766 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
767 /* start grabbing all events */
771 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
772 buttonstate
|= SDL_BUTTON(bev
->button
);
774 buttonstate
&= ~SDL_BUTTON(bev
->button
);
776 sdl_send_mouse_event(0, 0, bev
->x
, bev
->y
, buttonstate
);
780 static void handle_activation(SDL_Event
*ev
)
783 /* Disable grab if the window no longer has the focus
784 * (Windows-only workaround) */
785 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
786 !ev
->active
.gain
&& !gui_fullscreen
) {
790 if (!gui_grab
&& ev
->active
.gain
&& qemu_console_is_graphic(NULL
) &&
791 (qemu_input_is_absolute() || absolute_enabled
)) {
792 absolute_mouse_grab();
794 if (ev
->active
.state
& SDL_APPACTIVE
) {
795 if (ev
->active
.gain
) {
796 /* Back to default interval */
797 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
799 /* Sleeping interval. Not using the long default here as
800 * sdl_refresh does not only update the guest screen, but
801 * also checks for gui events. */
802 update_displaychangelistener(dcl
, 500);
807 static void sdl_refresh(DisplayChangeListener
*dcl
)
809 SDL_Event ev1
, *ev
= &ev1
;
812 if (last_vm_running
!= runstate_is_running()) {
813 last_vm_running
= runstate_is_running();
814 sdl_update_caption();
817 graphic_hw_update(NULL
);
818 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL
));
820 while (SDL_PollEvent(ev
)) {
822 case SDL_VIDEOEXPOSE
:
823 sdl_update(dcl
, 0, 0, real_screen
->w
, real_screen
->h
);
836 qemu_system_shutdown_request();
839 case SDL_MOUSEMOTION
:
841 handle_mousemotion(ev
);
843 case SDL_MOUSEBUTTONDOWN
:
844 case SDL_MOUSEBUTTONUP
:
846 handle_mousebutton(ev
);
848 case SDL_ACTIVEEVENT
:
849 handle_activation(ev
);
851 case SDL_VIDEORESIZE
:
852 sdl_scale(ev
->resize
.w
, ev
->resize
.h
);
853 graphic_hw_invalidate(NULL
);
854 graphic_hw_update(NULL
);
862 if (idle_counter
< SDL_MAX_IDLE_COUNT
) {
864 if (idle_counter
>= SDL_MAX_IDLE_COUNT
) {
865 dcl
->update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
870 dcl
->update_interval
= SDL_REFRESH_INTERVAL_BUSY
;
874 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
875 int x
, int y
, int on
)
880 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
881 SDL_SetCursor(guest_sprite
);
882 if (!qemu_input_is_absolute() && !absolute_enabled
) {
889 guest_x
= x
, guest_y
= y
;
892 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
895 uint8_t *image
, *mask
;
899 SDL_FreeCursor(guest_sprite
);
901 bpl
= cursor_get_mono_bpl(c
);
902 image
= g_malloc0(bpl
* c
->height
);
903 mask
= g_malloc0(bpl
* c
->height
);
904 cursor_get_mono_image(c
, 0x000000, image
);
905 cursor_get_mono_mask(c
, 0, mask
);
906 guest_sprite
= SDL_CreateCursor(image
, mask
, c
->width
, c
->height
,
912 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
913 SDL_SetCursor(guest_sprite
);
916 static void sdl_cleanup(void)
919 SDL_FreeCursor(guest_sprite
);
920 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
923 static const DisplayChangeListenerOps dcl_ops
= {
925 .dpy_gfx_update
= sdl_update
,
926 .dpy_gfx_switch
= sdl_switch
,
927 .dpy_gfx_check_format
= sdl_check_format
,
928 .dpy_refresh
= sdl_refresh
,
929 .dpy_mouse_set
= sdl_mouse_warp
,
930 .dpy_cursor_define
= sdl_mouse_define
,
933 void sdl_display_early_init(int opengl
)
935 if (opengl
== 1 /* on */) {
937 "SDL1 display code has no opengl support.\n"
938 "Please recompile qemu with SDL2, using\n"
939 "./configure --enable-sdl --with-sdlabi=2.0\n");
943 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
947 const SDL_VideoInfo
*vi
;
950 #if defined(__APPLE__)
951 /* always use generic keymaps */
952 if (!keyboard_layout
)
953 keyboard_layout
= "en-us";
955 if(keyboard_layout
) {
956 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
965 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
968 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
969 * accessible $DISPLAY to open X11 window. This is often the case
970 * when qemu is run using sudo. But in this case, and when actually
971 * run in X11 environment, SDL fights with X11 for the video card,
972 * making current display unavailable, often until reboot.
973 * So make x11 the default SDL video driver if this variable is unset.
974 * This is a bit hackish but saves us from bigger problem.
975 * Maybe it's a good idea to fix this in SDL instead.
977 setenv("SDL_VIDEODRIVER", "x11", 0);
980 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
981 * This requires SDL >= 1.2.14. */
982 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
984 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
985 if (SDL_Init (flags
)) {
986 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
990 vi
= SDL_GetVideoInfo();
991 host_format
= *(vi
->vfmt
);
993 /* Load a 32x32x4 image. White pixels are transparent. */
994 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
996 SDL_Surface
*image
= SDL_LoadBMP(filename
);
998 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
999 SDL_SetColorKey(image
, SDL_SRCCOLORKEY
, colorkey
);
1000 SDL_WM_SetIcon(image
, NULL
);
1010 dcl
= g_new0(DisplayChangeListener
, 1);
1011 dcl
->ops
= &dcl_ops
;
1012 register_displaychangelistener(dcl
);
1014 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
1015 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
1017 sdl_update_caption();
1018 SDL_EnableKeyRepeat(250, 50);
1021 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
1022 sdl_cursor_normal
= SDL_GetCursor();
1024 atexit(sdl_cleanup
);