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"
42 static DisplayChangeListener
*dcl
;
43 static DisplaySurface
*surface
;
44 static DisplayOptions
*opts
;
45 static SDL_Surface
*real_screen
;
46 static SDL_Surface
*guest_screen
= NULL
;
47 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
48 static int last_vm_running
;
49 static bool gui_saved_scaling
;
50 static int gui_saved_width
;
51 static int gui_saved_height
;
52 static int gui_saved_grab
;
53 static int gui_fullscreen
;
54 static int gui_key_modifier_pressed
;
55 static int gui_keysym
;
56 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
57 static uint8_t modifiers_state
[256];
58 static SDL_Cursor
*sdl_cursor_normal
;
59 static SDL_Cursor
*sdl_cursor_hidden
;
60 static int absolute_enabled
= 0;
61 static int guest_cursor
= 0;
62 static int guest_x
, guest_y
;
63 static SDL_Cursor
*guest_sprite
= NULL
;
64 static SDL_PixelFormat host_format
;
65 static int scaling_active
= 0;
66 static Notifier mouse_mode_notifier
;
67 static int idle_counter
;
68 static const guint16
*keycode_map
;
69 static size_t keycode_maplen
;
71 #define SDL_REFRESH_INTERVAL_BUSY 10
72 #define SDL_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
73 / SDL_REFRESH_INTERVAL_BUSY + 1)
79 static void sdl_update(DisplayChangeListener
*dcl
,
80 int x
, int y
, int w
, int h
)
89 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
90 x
, y
, w
, h
, scaling_active
);
94 if (!scaling_active
) {
95 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
97 if (sdl_zoom_blit(guest_screen
, real_screen
, SMOOTHING_ON
, &rec
) < 0) {
98 fprintf(stderr
, "Zoom blit failed\n");
103 SDL_UpdateRect(real_screen
, rec
.x
, rec
.y
, rec
.w
, rec
.h
);
106 static void do_sdl_resize(int width
, int height
, int bpp
)
109 SDL_Surface
*tmp_screen
;
112 printf("SDL: Resizing to %dx%d bpp %d\n", width
, height
, bpp
);
115 flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
;
116 if (gui_fullscreen
) {
117 flags
|= SDL_FULLSCREEN
;
119 flags
|= SDL_RESIZABLE
;
122 flags
|= SDL_NOFRAME
;
125 tmp_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
128 fprintf(stderr
, "Could not open SDL display (%dx%dx%d): %s\n",
129 width
, height
, bpp
, SDL_GetError());
134 * Revert to the previous video mode if the change of resizing or
138 fprintf(stderr
, "Failed to set SDL display (%dx%dx%d): %s\n",
139 width
, height
, bpp
, SDL_GetError());
144 real_screen
= tmp_screen
;
147 static void sdl_switch(DisplayChangeListener
*dcl
,
148 DisplaySurface
*new_surface
)
152 /* temporary hack: allows to call sdl_switch to handle scaling changes */
154 surface
= new_surface
;
156 pf
= qemu_pixelformat_from_pixman(surface
->format
);
158 if (!scaling_active
) {
159 do_sdl_resize(surface_width(surface
), surface_height(surface
), 0);
160 } else if (real_screen
->format
->BitsPerPixel
!=
161 surface_bits_per_pixel(surface
)) {
162 do_sdl_resize(real_screen
->w
, real_screen
->h
,
163 surface_bits_per_pixel(surface
));
166 if (guest_screen
!= NULL
) {
167 SDL_FreeSurface(guest_screen
);
171 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
172 pf
.rmask
, pf
.gmask
, pf
.bmask
, pf
.amask
);
175 guest_screen
= SDL_CreateRGBSurfaceFrom
176 (surface_data(surface
),
177 surface_width(surface
), surface_height(surface
),
178 surface_bits_per_pixel(surface
), surface_stride(surface
),
183 static bool sdl_check_format(DisplayChangeListener
*dcl
,
184 pixman_format_code_t format
)
187 * We let SDL convert for us a few more formats than,
188 * the native ones. Thes are the ones I have tested.
190 return (format
== PIXMAN_x8r8g8b8
||
191 format
== PIXMAN_b8g8r8x8
||
192 format
== PIXMAN_x1r5g5b5
||
193 format
== PIXMAN_r5g6b5
);
196 /* generic keyboard conversion */
198 #include "sdl_keysym.h"
200 static kbd_layout_t
*kbd_layout
= NULL
;
202 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
204 bool shift
= modifiers_state
[0x2a] || modifiers_state
[0x36];
205 bool altgr
= modifiers_state
[0xb8];
206 bool ctrl
= modifiers_state
[0x1d] || modifiers_state
[0x9d];
208 /* workaround for X11+SDL bug with AltGR */
209 keysym
= ev
->keysym
.sym
;
210 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
212 /* For Japanese key '\' and '|' */
213 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
216 return keysym2scancode(kbd_layout
, keysym
,
217 shift
, altgr
, ctrl
) & SCANCODE_KEYMASK
;
221 static const guint16
*sdl_get_keymap(size_t *maplen
)
224 *maplen
= qemu_input_map_atset1_to_qcode_len
;
225 return qemu_input_map_atset1_to_qcode
;
227 #if defined(SDL_VIDEO_DRIVER_X11)
230 SDL_VERSION(&info
.version
);
231 if (SDL_GetWMInfo(&info
) > 0) {
232 return qemu_xkeymap_mapping_table(
233 info
.info
.x11
.display
, maplen
);
236 g_warning("Unsupported SDL video driver / platform.\n"
237 "Assuming Linux KBD scancodes, but probably wrong.\n"
238 "Please report to qemu-devel@nongnu.org\n"
239 "including the following information:\n"
241 " - Operating system\n"
242 " - SDL video driver\n");
243 *maplen
= qemu_input_map_xorgkbd_to_qcode_len
;
244 return qemu_input_map_xorgkbd_to_qcode
;
248 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
254 if (ev
->keysym
.scancode
> keycode_maplen
) {
258 qcode
= keycode_map
[ev
->keysym
.scancode
];
260 if (qcode
> qemu_input_map_qcode_to_qnum_len
) {
264 return qemu_input_map_qcode_to_qnum
[qcode
];
267 static void reset_keys(void)
270 for(i
= 0; i
< 256; i
++) {
271 if (modifiers_state
[i
]) {
272 qemu_input_event_send_key_number(dcl
->con
, i
, false);
273 modifiers_state
[i
] = 0;
278 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
282 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
284 qemu_input_event_send_key_qcode(dcl
->con
, Q_KEY_CODE_PAUSE
,
285 ev
->type
== SDL_KEYDOWN
);
290 keycode
= sdl_keyevent_to_keycode_generic(ev
);
292 keycode
= sdl_keyevent_to_keycode(ev
);
297 /* sent when leaving window: reset the modifiers state */
300 case 0x2a: /* Left Shift */
301 case 0x36: /* Right Shift */
302 case 0x1d: /* Left CTRL */
303 case 0x9d: /* Right CTRL */
304 case 0x38: /* Left ALT */
305 case 0xb8: /* Right ALT */
306 if (ev
->type
== SDL_KEYUP
)
307 modifiers_state
[keycode
] = 0;
309 modifiers_state
[keycode
] = 1;
311 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
312 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
313 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
314 case 0x45: /* num lock */
315 case 0x3a: /* caps lock */
316 /* SDL does not send the key up event, so we generate it */
317 qemu_input_event_send_key_number(dcl
->con
, keycode
, true);
318 qemu_input_event_send_key_number(dcl
->con
, keycode
, false);
323 /* now send the key code */
324 qemu_input_event_send_key_number(dcl
->con
, keycode
,
325 ev
->type
== SDL_KEYDOWN
);
328 static void sdl_update_caption(void)
330 char win_title
[1024];
331 char icon_title
[1024];
332 const char *status
= "";
334 if (!runstate_is_running())
335 status
= " [Stopped]";
338 status
= " - Press Ctrl-Alt-Shift-G to exit mouse grab";
340 status
= " - Press Right-Ctrl-G to exit mouse grab";
342 status
= " - Press Ctrl-Alt-G to exit mouse grab";
346 snprintf(win_title
, sizeof(win_title
), "QEMU (%s)%s", qemu_name
, status
);
347 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
349 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
350 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
353 SDL_WM_SetCaption(win_title
, icon_title
);
356 static void sdl_hide_cursor(void)
361 if (qemu_input_is_absolute()) {
363 SDL_SetCursor(sdl_cursor_hidden
);
369 static void sdl_show_cursor(void)
374 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL
)) {
377 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
378 SDL_SetCursor(guest_sprite
);
380 SDL_SetCursor(sdl_cursor_normal
);
384 static void sdl_grab_start(void)
387 * If the application is not active, do not try to enter grab state. This
388 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
389 * application (SDL bug).
391 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS
)) {
395 SDL_SetCursor(guest_sprite
);
396 if (!qemu_input_is_absolute() && !absolute_enabled
) {
397 SDL_WarpMouse(guest_x
, guest_y
);
401 SDL_WM_GrabInput(SDL_GRAB_ON
);
403 sdl_update_caption();
406 static void sdl_grab_end(void)
408 SDL_WM_GrabInput(SDL_GRAB_OFF
);
411 sdl_update_caption();
414 static void absolute_mouse_grab(void)
416 int mouse_x
, mouse_y
;
418 SDL_GetMouseState(&mouse_x
, &mouse_y
);
419 if (mouse_x
> 0 && mouse_x
< real_screen
->w
- 1 &&
420 mouse_y
> 0 && mouse_y
< real_screen
->h
- 1) {
425 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
427 if (qemu_input_is_absolute()) {
428 if (!absolute_enabled
) {
429 absolute_enabled
= 1;
430 if (qemu_console_is_graphic(NULL
)) {
431 absolute_mouse_grab();
434 } else if (absolute_enabled
) {
435 if (!gui_fullscreen
) {
438 absolute_enabled
= 0;
442 static void sdl_send_mouse_event(int dx
, int dy
, int x
, int y
, int state
)
444 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
445 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
446 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
447 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
448 [INPUT_BUTTON_WHEEL_UP
] = SDL_BUTTON(SDL_BUTTON_WHEELUP
),
449 [INPUT_BUTTON_WHEEL_DOWN
] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN
),
451 static uint32_t prev_state
;
453 if (prev_state
!= state
) {
454 qemu_input_update_buttons(dcl
->con
, bmap
, prev_state
, state
);
458 if (qemu_input_is_absolute()) {
459 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_X
, x
,
461 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_Y
, y
,
472 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_X
, dx
);
473 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_Y
, dy
);
475 qemu_input_event_sync();
478 static void sdl_scale(int width
, int height
)
480 int bpp
= real_screen
->format
->BitsPerPixel
;
483 printf("SDL: Scaling to %dx%d bpp %d\n", width
, height
, bpp
);
486 if (bpp
!= 16 && bpp
!= 32) {
489 do_sdl_resize(width
, height
, bpp
);
493 static void toggle_full_screen(void)
495 int width
= surface_width(surface
);
496 int height
= surface_height(surface
);
497 int bpp
= surface_bits_per_pixel(surface
);
499 gui_fullscreen
= !gui_fullscreen
;
500 if (gui_fullscreen
) {
501 gui_saved_width
= real_screen
->w
;
502 gui_saved_height
= real_screen
->h
;
503 gui_saved_scaling
= scaling_active
;
505 do_sdl_resize(width
, height
, bpp
);
508 gui_saved_grab
= gui_grab
;
511 if (gui_saved_scaling
) {
512 sdl_scale(gui_saved_width
, gui_saved_height
);
514 do_sdl_resize(width
, height
, 0);
516 if (!gui_saved_grab
|| !qemu_console_is_graphic(NULL
)) {
520 graphic_hw_invalidate(NULL
);
521 graphic_hw_update(NULL
);
524 static void handle_keydown(SDL_Event
*ev
)
530 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
531 (gui_grab_code
| KMOD_LSHIFT
);
532 } else if (ctrl_grab
) {
533 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
535 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
537 gui_key_modifier_pressed
= mod_state
;
539 if (gui_key_modifier_pressed
) {
540 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
542 case 0x21: /* 'f' key on US keyboard */
543 toggle_full_screen();
546 case 0x22: /* 'g' key */
548 if (qemu_console_is_graphic(NULL
)) {
551 } else if (!gui_fullscreen
) {
556 case 0x16: /* 'u' key on US keyboard */
557 if (scaling_active
) {
559 sdl_switch(dcl
, NULL
);
560 graphic_hw_invalidate(NULL
);
561 graphic_hw_update(NULL
);
565 case 0x02 ... 0x0a: /* '1' to '9' keys */
566 /* Reset the modifiers sent to the current console */
568 console_select(keycode
- 0x02);
570 if (gui_fullscreen
) {
573 if (!qemu_console_is_graphic(NULL
)) {
574 /* release grab if going to a text console */
577 } else if (absolute_enabled
) {
580 } else if (absolute_enabled
) {
582 absolute_mouse_grab();
587 if (!gui_fullscreen
) {
588 int width
= MAX(real_screen
->w
+ (keycode
== 0x1b ? 50 : -50),
590 int height
= (surface_height(surface
) * width
) /
591 surface_width(surface
);
593 sdl_scale(width
, height
);
594 graphic_hw_invalidate(NULL
);
595 graphic_hw_update(NULL
);
601 } else if (!qemu_console_is_graphic(NULL
)) {
604 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
605 switch (ev
->key
.keysym
.sym
) {
607 keysym
= QEMU_KEY_CTRL_UP
;
610 keysym
= QEMU_KEY_CTRL_DOWN
;
613 keysym
= QEMU_KEY_CTRL_LEFT
;
616 keysym
= QEMU_KEY_CTRL_RIGHT
;
619 keysym
= QEMU_KEY_CTRL_HOME
;
622 keysym
= QEMU_KEY_CTRL_END
;
625 keysym
= QEMU_KEY_CTRL_PAGEUP
;
628 keysym
= QEMU_KEY_CTRL_PAGEDOWN
;
634 switch (ev
->key
.keysym
.sym
) {
636 keysym
= QEMU_KEY_UP
;
639 keysym
= QEMU_KEY_DOWN
;
642 keysym
= QEMU_KEY_LEFT
;
645 keysym
= QEMU_KEY_RIGHT
;
648 keysym
= QEMU_KEY_HOME
;
651 keysym
= QEMU_KEY_END
;
654 keysym
= QEMU_KEY_PAGEUP
;
657 keysym
= QEMU_KEY_PAGEDOWN
;
660 keysym
= QEMU_KEY_BACKSPACE
;
663 keysym
= QEMU_KEY_DELETE
;
670 kbd_put_keysym(keysym
);
671 } else if (ev
->key
.keysym
.unicode
!= 0) {
672 kbd_put_keysym(ev
->key
.keysym
.unicode
);
675 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
676 sdl_process_key(&ev
->key
);
680 static void handle_keyup(SDL_Event
*ev
)
685 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
687 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
689 if (!mod_state
&& gui_key_modifier_pressed
) {
690 gui_key_modifier_pressed
= 0;
693 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
694 sdl_process_key(&ev
->key
);
698 static void handle_mousemotion(SDL_Event
*ev
)
702 if (qemu_console_is_graphic(NULL
) &&
703 (qemu_input_is_absolute() || absolute_enabled
)) {
704 max_x
= real_screen
->w
- 1;
705 max_y
= real_screen
->h
- 1;
706 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
707 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
711 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
712 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
716 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
717 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
,
718 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
722 static void handle_mousebutton(SDL_Event
*ev
)
724 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
725 SDL_MouseButtonEvent
*bev
;
727 if (!qemu_console_is_graphic(NULL
)) {
732 if (!gui_grab
&& !qemu_input_is_absolute()) {
733 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
734 /* start grabbing all events */
738 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
739 buttonstate
|= SDL_BUTTON(bev
->button
);
741 buttonstate
&= ~SDL_BUTTON(bev
->button
);
743 sdl_send_mouse_event(0, 0, bev
->x
, bev
->y
, buttonstate
);
747 static void handle_activation(SDL_Event
*ev
)
750 /* Disable grab if the window no longer has the focus
751 * (Windows-only workaround) */
752 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
753 !ev
->active
.gain
&& !gui_fullscreen
) {
757 if (!gui_grab
&& ev
->active
.gain
&& qemu_console_is_graphic(NULL
) &&
758 (qemu_input_is_absolute() || absolute_enabled
)) {
759 absolute_mouse_grab();
761 if (ev
->active
.state
& SDL_APPACTIVE
) {
762 if (ev
->active
.gain
) {
763 /* Back to default interval */
764 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
766 /* Sleeping interval. Not using the long default here as
767 * sdl_refresh does not only update the guest screen, but
768 * also checks for gui events. */
769 update_displaychangelistener(dcl
, 500);
774 static void sdl_refresh(DisplayChangeListener
*dcl
)
776 SDL_Event ev1
, *ev
= &ev1
;
777 bool allow_close
= true;
780 if (last_vm_running
!= runstate_is_running()) {
781 last_vm_running
= runstate_is_running();
782 sdl_update_caption();
785 graphic_hw_update(NULL
);
786 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL
));
788 while (SDL_PollEvent(ev
)) {
790 case SDL_VIDEOEXPOSE
:
791 sdl_update(dcl
, 0, 0, real_screen
->w
, real_screen
->h
);
802 if (opts
->has_window_close
&& !opts
->window_close
) {
807 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
810 case SDL_MOUSEMOTION
:
812 handle_mousemotion(ev
);
814 case SDL_MOUSEBUTTONDOWN
:
815 case SDL_MOUSEBUTTONUP
:
817 handle_mousebutton(ev
);
819 case SDL_ACTIVEEVENT
:
820 handle_activation(ev
);
822 case SDL_VIDEORESIZE
:
823 sdl_scale(ev
->resize
.w
, ev
->resize
.h
);
824 graphic_hw_invalidate(NULL
);
825 graphic_hw_update(NULL
);
833 if (idle_counter
< SDL_MAX_IDLE_COUNT
) {
835 if (idle_counter
>= SDL_MAX_IDLE_COUNT
) {
836 dcl
->update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
841 dcl
->update_interval
= SDL_REFRESH_INTERVAL_BUSY
;
845 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
846 int x
, int y
, int on
)
851 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
852 SDL_SetCursor(guest_sprite
);
853 if (!qemu_input_is_absolute() && !absolute_enabled
) {
860 guest_x
= x
, guest_y
= y
;
863 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
866 uint8_t *image
, *mask
;
870 SDL_FreeCursor(guest_sprite
);
872 bpl
= cursor_get_mono_bpl(c
);
873 image
= g_malloc0(bpl
* c
->height
);
874 mask
= g_malloc0(bpl
* c
->height
);
875 cursor_get_mono_image(c
, 0x000000, image
);
876 cursor_get_mono_mask(c
, 0, mask
);
877 guest_sprite
= SDL_CreateCursor(image
, mask
, c
->width
, c
->height
,
883 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
884 SDL_SetCursor(guest_sprite
);
887 static void sdl_cleanup(void)
890 SDL_FreeCursor(guest_sprite
);
891 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
894 static const DisplayChangeListenerOps dcl_ops
= {
896 .dpy_gfx_update
= sdl_update
,
897 .dpy_gfx_switch
= sdl_switch
,
898 .dpy_gfx_check_format
= sdl_check_format
,
899 .dpy_refresh
= sdl_refresh
,
900 .dpy_mouse_set
= sdl_mouse_warp
,
901 .dpy_cursor_define
= sdl_mouse_define
,
904 static void sdl1_display_init(DisplayState
*ds
, DisplayOptions
*o
)
908 const SDL_VideoInfo
*vi
;
912 assert(o
->type
== DISPLAY_TYPE_SDL
);
914 #if defined(__APPLE__)
915 /* always use generic keymaps */
916 if (!keyboard_layout
)
917 keyboard_layout
= "en-us";
919 if(keyboard_layout
) {
920 kbd_layout
= init_keyboard_layout(name2keysym
, keyboard_layout
);
925 g_printerr("Running QEMU with SDL 1.2 is deprecated, and will be removed\n"
926 "in a future release. Please switch to SDL 2.0 instead\n");
928 if (opts
->has_full_screen
&& opts
->full_screen
) {
929 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
932 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
933 * accessible $DISPLAY to open X11 window. This is often the case
934 * when qemu is run using sudo. But in this case, and when actually
935 * run in X11 environment, SDL fights with X11 for the video card,
936 * making current display unavailable, often until reboot.
937 * So make x11 the default SDL video driver if this variable is unset.
938 * This is a bit hackish but saves us from bigger problem.
939 * Maybe it's a good idea to fix this in SDL instead.
941 setenv("SDL_VIDEODRIVER", "x11", 0);
944 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
945 * This requires SDL >= 1.2.14. */
946 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
948 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
949 if (SDL_Init (flags
)) {
950 fprintf(stderr
, "Could not initialize SDL(%s) - exiting\n",
954 vi
= SDL_GetVideoInfo();
955 host_format
= *(vi
->vfmt
);
957 keycode_map
= sdl_get_keymap(&keycode_maplen
);
959 /* Load a 32x32x4 image. White pixels are transparent. */
960 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, "qemu-icon.bmp");
962 SDL_Surface
*image
= SDL_LoadBMP(filename
);
964 uint32_t colorkey
= SDL_MapRGB(image
->format
, 255, 255, 255);
965 SDL_SetColorKey(image
, SDL_SRCCOLORKEY
, colorkey
);
966 SDL_WM_SetIcon(image
, NULL
);
971 if (opts
->has_full_screen
&& opts
->full_screen
) {
976 dcl
= g_new0(DisplayChangeListener
, 1);
978 register_displaychangelistener(dcl
);
980 mouse_mode_notifier
.notify
= sdl_mouse_mode_change
;
981 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier
);
983 sdl_update_caption();
984 SDL_EnableKeyRepeat(250, 50);
987 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
988 sdl_cursor_normal
= SDL_GetCursor();
990 memset(&info
, 0, sizeof(info
));
991 SDL_VERSION(&info
.version
);
992 if (SDL_GetWMInfo(&info
)) {
995 /* All consoles share the same window */
996 QemuConsole
*con
= qemu_console_lookup_by_index(i
);
998 #if defined(SDL_VIDEO_DRIVER_X11)
999 qemu_console_set_window_id(con
, info
.info
.x11
.wmwindow
);
1000 #elif defined(SDL_VIDEO_DRIVER_NANOX) || \
1001 defined(SDL_VIDEO_DRIVER_WINDIB) || defined(SDL_VIDEO_DRIVER_DDRAW) || \
1002 defined(SDL_VIDEO_DRIVER_GAPI) || \
1003 defined(SDL_VIDEO_DRIVER_RISCOS)
1004 qemu_console_set_window_id(con
, (int) (uintptr_t) info
.window
);
1006 qemu_console_set_window_id(con
, info
.data
);
1014 atexit(sdl_cleanup
);
1017 static QemuDisplay qemu_display_sdl1
= {
1018 .type
= DISPLAY_TYPE_SDL
,
1019 .init
= sdl1_display_init
,
1022 static void register_sdl1(void)
1024 qemu_display_register(&qemu_display_sdl1
);
1027 type_init(register_sdl1
);