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 "qapi/error.h"
33 #include "qemu-common.h"
34 #include "qemu/cutils.h"
35 #include "ui/console.h"
37 #include "sysemu/sysemu.h"
43 static DisplayChangeListener
*dcl
;
44 static DisplaySurface
*surface
;
45 static DisplayOptions
*opts
;
46 static SDL_Surface
*real_screen
;
47 static SDL_Surface
*guest_screen
= NULL
;
48 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
49 static int last_vm_running
;
50 static bool gui_saved_scaling
;
51 static int gui_saved_width
;
52 static int gui_saved_height
;
53 static int gui_saved_grab
;
54 static int gui_fullscreen
;
55 static int gui_key_modifier_pressed
;
56 static int gui_keysym
;
57 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
58 static uint8_t modifiers_state
[256];
59 static SDL_Cursor
*sdl_cursor_normal
;
60 static SDL_Cursor
*sdl_cursor_hidden
;
61 static int absolute_enabled
= 0;
62 static int guest_cursor
= 0;
63 static int guest_x
, guest_y
;
64 static SDL_Cursor
*guest_sprite
= NULL
;
65 static SDL_PixelFormat host_format
;
66 static int scaling_active
= 0;
67 static Notifier mouse_mode_notifier
;
68 static int idle_counter
;
69 static const guint16
*keycode_map
;
70 static size_t keycode_maplen
;
72 #define SDL_REFRESH_INTERVAL_BUSY 10
73 #define SDL_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
74 / SDL_REFRESH_INTERVAL_BUSY + 1)
80 static void sdl_update(DisplayChangeListener
*dcl
,
81 int x
, int y
, int w
, int h
)
90 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
91 x
, y
, w
, h
, scaling_active
);
95 if (!scaling_active
) {
96 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
98 if (sdl_zoom_blit(guest_screen
, real_screen
, SMOOTHING_ON
, &rec
) < 0) {
99 fprintf(stderr
, "Zoom blit failed\n");
104 SDL_UpdateRect(real_screen
, rec
.x
, rec
.y
, rec
.w
, rec
.h
);
107 static void do_sdl_resize(int width
, int height
, int bpp
)
110 SDL_Surface
*tmp_screen
;
113 printf("SDL: Resizing to %dx%d bpp %d\n", width
, height
, bpp
);
116 flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
;
117 if (gui_fullscreen
) {
118 flags
|= SDL_FULLSCREEN
;
120 flags
|= SDL_RESIZABLE
;
123 flags
|= SDL_NOFRAME
;
126 tmp_screen
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
129 fprintf(stderr
, "Could not open SDL display (%dx%dx%d): %s\n",
130 width
, height
, bpp
, SDL_GetError());
135 * Revert to the previous video mode if the change of resizing or
139 fprintf(stderr
, "Failed to set SDL display (%dx%dx%d): %s\n",
140 width
, height
, bpp
, SDL_GetError());
145 real_screen
= tmp_screen
;
148 static void sdl_switch(DisplayChangeListener
*dcl
,
149 DisplaySurface
*new_surface
)
153 /* temporary hack: allows to call sdl_switch to handle scaling changes */
155 surface
= new_surface
;
157 pf
= qemu_pixelformat_from_pixman(surface
->format
);
159 if (!scaling_active
) {
160 do_sdl_resize(surface_width(surface
), surface_height(surface
), 0);
161 } else if (real_screen
->format
->BitsPerPixel
!=
162 surface_bits_per_pixel(surface
)) {
163 do_sdl_resize(real_screen
->w
, real_screen
->h
,
164 surface_bits_per_pixel(surface
));
167 if (guest_screen
!= NULL
) {
168 SDL_FreeSurface(guest_screen
);
172 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
173 pf
.rmask
, pf
.gmask
, pf
.bmask
, pf
.amask
);
176 guest_screen
= SDL_CreateRGBSurfaceFrom
177 (surface_data(surface
),
178 surface_width(surface
), surface_height(surface
),
179 surface_bits_per_pixel(surface
), surface_stride(surface
),
184 static bool sdl_check_format(DisplayChangeListener
*dcl
,
185 pixman_format_code_t format
)
188 * We let SDL convert for us a few more formats than,
189 * the native ones. Thes are the ones I have tested.
191 return (format
== PIXMAN_x8r8g8b8
||
192 format
== PIXMAN_b8g8r8x8
||
193 format
== PIXMAN_x1r5g5b5
||
194 format
== PIXMAN_r5g6b5
);
197 /* generic keyboard conversion */
199 #include "sdl_keysym.h"
201 static kbd_layout_t
*kbd_layout
= NULL
;
203 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
205 bool shift
= modifiers_state
[0x2a] || modifiers_state
[0x36];
206 bool altgr
= modifiers_state
[0xb8];
207 bool ctrl
= modifiers_state
[0x1d] || modifiers_state
[0x9d];
209 /* workaround for X11+SDL bug with AltGR */
210 keysym
= ev
->keysym
.sym
;
211 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
213 /* For Japanese key '\' and '|' */
214 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
217 return keysym2scancode(kbd_layout
, keysym
,
218 shift
, altgr
, ctrl
) & SCANCODE_KEYMASK
;
222 static const guint16
*sdl_get_keymap(size_t *maplen
)
225 *maplen
= qemu_input_map_atset1_to_qcode_len
;
226 return qemu_input_map_atset1_to_qcode
;
228 #if defined(SDL_VIDEO_DRIVER_X11)
231 SDL_VERSION(&info
.version
);
232 if (SDL_GetWMInfo(&info
) > 0) {
233 return qemu_xkeymap_mapping_table(
234 info
.info
.x11
.display
, maplen
);
237 g_warning("Unsupported SDL video driver / platform.\n"
238 "Assuming Linux KBD scancodes, but probably wrong.\n"
239 "Please report to qemu-devel@nongnu.org\n"
240 "including the following information:\n"
242 " - Operating system\n"
243 " - SDL video driver\n");
244 *maplen
= qemu_input_map_xorgkbd_to_qcode_len
;
245 return qemu_input_map_xorgkbd_to_qcode
;
249 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
255 if (ev
->keysym
.scancode
> keycode_maplen
) {
259 qcode
= keycode_map
[ev
->keysym
.scancode
];
261 if (qcode
> qemu_input_map_qcode_to_qnum_len
) {
265 return qemu_input_map_qcode_to_qnum
[qcode
];
268 static void reset_keys(void)
271 for(i
= 0; i
< 256; i
++) {
272 if (modifiers_state
[i
]) {
273 qemu_input_event_send_key_number(dcl
->con
, i
, false);
274 modifiers_state
[i
] = 0;
279 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
283 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
285 qemu_input_event_send_key_qcode(dcl
->con
, Q_KEY_CODE_PAUSE
,
286 ev
->type
== SDL_KEYDOWN
);
291 keycode
= sdl_keyevent_to_keycode_generic(ev
);
293 keycode
= sdl_keyevent_to_keycode(ev
);
298 /* sent when leaving window: reset the modifiers state */
301 case 0x2a: /* Left Shift */
302 case 0x36: /* Right Shift */
303 case 0x1d: /* Left CTRL */
304 case 0x9d: /* Right CTRL */
305 case 0x38: /* Left ALT */
306 case 0xb8: /* Right ALT */
307 if (ev
->type
== SDL_KEYUP
)
308 modifiers_state
[keycode
] = 0;
310 modifiers_state
[keycode
] = 1;
312 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
313 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
314 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
315 case 0x45: /* num lock */
316 case 0x3a: /* caps lock */
317 /* SDL does not send the key up event, so we generate it */
318 qemu_input_event_send_key_number(dcl
->con
, keycode
, true);
319 qemu_input_event_send_key_number(dcl
->con
, keycode
, false);
324 /* now send the key code */
325 qemu_input_event_send_key_number(dcl
->con
, keycode
,
326 ev
->type
== SDL_KEYDOWN
);
329 static void sdl_update_caption(void)
331 char win_title
[1024];
332 char icon_title
[1024];
333 const char *status
= "";
335 if (!runstate_is_running())
336 status
= " [Stopped]";
339 status
= " - Press Ctrl-Alt-Shift-G to exit mouse grab";
341 status
= " - Press Right-Ctrl-G to exit mouse grab";
343 status
= " - Press Ctrl-Alt-G to exit mouse grab";
347 snprintf(win_title
, sizeof(win_title
), "QEMU (%s)%s", qemu_name
, status
);
348 snprintf(icon_title
, sizeof(icon_title
), "QEMU (%s)", qemu_name
);
350 snprintf(win_title
, sizeof(win_title
), "QEMU%s", status
);
351 snprintf(icon_title
, sizeof(icon_title
), "QEMU");
354 SDL_WM_SetCaption(win_title
, icon_title
);
357 static void sdl_hide_cursor(void)
362 if (qemu_input_is_absolute()) {
364 SDL_SetCursor(sdl_cursor_hidden
);
370 static void sdl_show_cursor(void)
375 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL
)) {
378 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
379 SDL_SetCursor(guest_sprite
);
381 SDL_SetCursor(sdl_cursor_normal
);
385 static void sdl_grab_start(void)
388 * If the application is not active, do not try to enter grab state. This
389 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
390 * application (SDL bug).
392 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS
)) {
396 SDL_SetCursor(guest_sprite
);
397 if (!qemu_input_is_absolute() && !absolute_enabled
) {
398 SDL_WarpMouse(guest_x
, guest_y
);
402 SDL_WM_GrabInput(SDL_GRAB_ON
);
404 sdl_update_caption();
407 static void sdl_grab_end(void)
409 SDL_WM_GrabInput(SDL_GRAB_OFF
);
412 sdl_update_caption();
415 static void absolute_mouse_grab(void)
417 int mouse_x
, mouse_y
;
419 SDL_GetMouseState(&mouse_x
, &mouse_y
);
420 if (mouse_x
> 0 && mouse_x
< real_screen
->w
- 1 &&
421 mouse_y
> 0 && mouse_y
< real_screen
->h
- 1) {
426 static void sdl_mouse_mode_change(Notifier
*notify
, void *data
)
428 if (qemu_input_is_absolute()) {
429 if (!absolute_enabled
) {
430 absolute_enabled
= 1;
431 if (qemu_console_is_graphic(NULL
)) {
432 absolute_mouse_grab();
435 } else if (absolute_enabled
) {
436 if (!gui_fullscreen
) {
439 absolute_enabled
= 0;
443 static void sdl_send_mouse_event(int dx
, int dy
, int x
, int y
, int state
)
445 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
446 [INPUT_BUTTON_LEFT
] = SDL_BUTTON(SDL_BUTTON_LEFT
),
447 [INPUT_BUTTON_MIDDLE
] = SDL_BUTTON(SDL_BUTTON_MIDDLE
),
448 [INPUT_BUTTON_RIGHT
] = SDL_BUTTON(SDL_BUTTON_RIGHT
),
449 [INPUT_BUTTON_WHEEL_UP
] = SDL_BUTTON(SDL_BUTTON_WHEELUP
),
450 [INPUT_BUTTON_WHEEL_DOWN
] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN
),
452 static uint32_t prev_state
;
454 if (prev_state
!= state
) {
455 qemu_input_update_buttons(dcl
->con
, bmap
, prev_state
, state
);
459 if (qemu_input_is_absolute()) {
460 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_X
, x
,
462 qemu_input_queue_abs(dcl
->con
, INPUT_AXIS_Y
, y
,
473 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_X
, dx
);
474 qemu_input_queue_rel(dcl
->con
, INPUT_AXIS_Y
, dy
);
476 qemu_input_event_sync();
479 static void sdl_scale(int width
, int height
)
481 int bpp
= real_screen
->format
->BitsPerPixel
;
484 printf("SDL: Scaling to %dx%d bpp %d\n", width
, height
, bpp
);
487 if (bpp
!= 16 && bpp
!= 32) {
490 do_sdl_resize(width
, height
, bpp
);
494 static void toggle_full_screen(void)
496 int width
= surface_width(surface
);
497 int height
= surface_height(surface
);
498 int bpp
= surface_bits_per_pixel(surface
);
500 gui_fullscreen
= !gui_fullscreen
;
501 if (gui_fullscreen
) {
502 gui_saved_width
= real_screen
->w
;
503 gui_saved_height
= real_screen
->h
;
504 gui_saved_scaling
= scaling_active
;
506 do_sdl_resize(width
, height
, bpp
);
509 gui_saved_grab
= gui_grab
;
512 if (gui_saved_scaling
) {
513 sdl_scale(gui_saved_width
, gui_saved_height
);
515 do_sdl_resize(width
, height
, 0);
517 if (!gui_saved_grab
|| !qemu_console_is_graphic(NULL
)) {
521 graphic_hw_invalidate(NULL
);
522 graphic_hw_update(NULL
);
525 static void handle_keydown(SDL_Event
*ev
)
531 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
532 (gui_grab_code
| KMOD_LSHIFT
);
533 } else if (ctrl_grab
) {
534 mod_state
= (SDL_GetModState() & KMOD_RCTRL
) == KMOD_RCTRL
;
536 mod_state
= (SDL_GetModState() & gui_grab_code
) == gui_grab_code
;
538 gui_key_modifier_pressed
= mod_state
;
540 if (gui_key_modifier_pressed
) {
541 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
543 case 0x21: /* 'f' key on US keyboard */
544 toggle_full_screen();
547 case 0x22: /* 'g' key */
549 if (qemu_console_is_graphic(NULL
)) {
552 } else if (!gui_fullscreen
) {
557 case 0x16: /* 'u' key on US keyboard */
558 if (scaling_active
) {
560 sdl_switch(dcl
, NULL
);
561 graphic_hw_invalidate(NULL
);
562 graphic_hw_update(NULL
);
566 case 0x02 ... 0x0a: /* '1' to '9' keys */
567 /* Reset the modifiers sent to the current console */
569 console_select(keycode
- 0x02);
571 if (gui_fullscreen
) {
574 if (!qemu_console_is_graphic(NULL
)) {
575 /* release grab if going to a text console */
578 } else if (absolute_enabled
) {
581 } else if (absolute_enabled
) {
583 absolute_mouse_grab();
588 if (!gui_fullscreen
) {
589 int width
= MAX(real_screen
->w
+ (keycode
== 0x1b ? 50 : -50),
591 int height
= (surface_height(surface
) * width
) /
592 surface_width(surface
);
594 sdl_scale(width
, height
);
595 graphic_hw_invalidate(NULL
);
596 graphic_hw_update(NULL
);
602 } else if (!qemu_console_is_graphic(NULL
)) {
605 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
606 switch (ev
->key
.keysym
.sym
) {
608 keysym
= QEMU_KEY_CTRL_UP
;
611 keysym
= QEMU_KEY_CTRL_DOWN
;
614 keysym
= QEMU_KEY_CTRL_LEFT
;
617 keysym
= QEMU_KEY_CTRL_RIGHT
;
620 keysym
= QEMU_KEY_CTRL_HOME
;
623 keysym
= QEMU_KEY_CTRL_END
;
626 keysym
= QEMU_KEY_CTRL_PAGEUP
;
629 keysym
= QEMU_KEY_CTRL_PAGEDOWN
;
635 switch (ev
->key
.keysym
.sym
) {
637 keysym
= QEMU_KEY_UP
;
640 keysym
= QEMU_KEY_DOWN
;
643 keysym
= QEMU_KEY_LEFT
;
646 keysym
= QEMU_KEY_RIGHT
;
649 keysym
= QEMU_KEY_HOME
;
652 keysym
= QEMU_KEY_END
;
655 keysym
= QEMU_KEY_PAGEUP
;
658 keysym
= QEMU_KEY_PAGEDOWN
;
661 keysym
= QEMU_KEY_BACKSPACE
;
664 keysym
= QEMU_KEY_DELETE
;
671 kbd_put_keysym(keysym
);
672 } else if (ev
->key
.keysym
.unicode
!= 0) {
673 kbd_put_keysym(ev
->key
.keysym
.unicode
);
676 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
677 sdl_process_key(&ev
->key
);
681 static void handle_keyup(SDL_Event
*ev
)
686 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
688 mod_state
= (ev
->key
.keysym
.mod
& (gui_grab_code
| KMOD_LSHIFT
));
690 if (!mod_state
&& gui_key_modifier_pressed
) {
691 gui_key_modifier_pressed
= 0;
694 if (qemu_console_is_graphic(NULL
) && !gui_keysym
) {
695 sdl_process_key(&ev
->key
);
699 static void handle_mousemotion(SDL_Event
*ev
)
703 if (qemu_console_is_graphic(NULL
) &&
704 (qemu_input_is_absolute() || absolute_enabled
)) {
705 max_x
= real_screen
->w
- 1;
706 max_y
= real_screen
->h
- 1;
707 if (gui_grab
&& (ev
->motion
.x
== 0 || ev
->motion
.y
== 0 ||
708 ev
->motion
.x
== max_x
|| ev
->motion
.y
== max_y
)) {
712 (ev
->motion
.x
> 0 && ev
->motion
.x
< max_x
&&
713 ev
->motion
.y
> 0 && ev
->motion
.y
< max_y
)) {
717 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
718 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
,
719 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
723 static void handle_mousebutton(SDL_Event
*ev
)
725 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
726 SDL_MouseButtonEvent
*bev
;
728 if (!qemu_console_is_graphic(NULL
)) {
733 if (!gui_grab
&& !qemu_input_is_absolute()) {
734 if (ev
->type
== SDL_MOUSEBUTTONUP
&& bev
->button
== SDL_BUTTON_LEFT
) {
735 /* start grabbing all events */
739 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
740 buttonstate
|= SDL_BUTTON(bev
->button
);
742 buttonstate
&= ~SDL_BUTTON(bev
->button
);
744 sdl_send_mouse_event(0, 0, bev
->x
, bev
->y
, buttonstate
);
748 static void handle_activation(SDL_Event
*ev
)
751 /* Disable grab if the window no longer has the focus
752 * (Windows-only workaround) */
753 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
754 !ev
->active
.gain
&& !gui_fullscreen
) {
758 if (!gui_grab
&& ev
->active
.gain
&& qemu_console_is_graphic(NULL
) &&
759 (qemu_input_is_absolute() || absolute_enabled
)) {
760 absolute_mouse_grab();
762 if (ev
->active
.state
& SDL_APPACTIVE
) {
763 if (ev
->active
.gain
) {
764 /* Back to default interval */
765 update_displaychangelistener(dcl
, GUI_REFRESH_INTERVAL_DEFAULT
);
767 /* Sleeping interval. Not using the long default here as
768 * sdl_refresh does not only update the guest screen, but
769 * also checks for gui events. */
770 update_displaychangelistener(dcl
, 500);
775 static void sdl_refresh(DisplayChangeListener
*dcl
)
777 SDL_Event ev1
, *ev
= &ev1
;
778 bool allow_close
= true;
781 if (last_vm_running
!= runstate_is_running()) {
782 last_vm_running
= runstate_is_running();
783 sdl_update_caption();
786 graphic_hw_update(NULL
);
787 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL
));
789 while (SDL_PollEvent(ev
)) {
791 case SDL_VIDEOEXPOSE
:
792 sdl_update(dcl
, 0, 0, real_screen
->w
, real_screen
->h
);
803 if (opts
->has_window_close
&& !opts
->window_close
) {
808 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI
);
811 case SDL_MOUSEMOTION
:
813 handle_mousemotion(ev
);
815 case SDL_MOUSEBUTTONDOWN
:
816 case SDL_MOUSEBUTTONUP
:
818 handle_mousebutton(ev
);
820 case SDL_ACTIVEEVENT
:
821 handle_activation(ev
);
823 case SDL_VIDEORESIZE
:
824 sdl_scale(ev
->resize
.w
, ev
->resize
.h
);
825 graphic_hw_invalidate(NULL
);
826 graphic_hw_update(NULL
);
834 if (idle_counter
< SDL_MAX_IDLE_COUNT
) {
836 if (idle_counter
>= SDL_MAX_IDLE_COUNT
) {
837 dcl
->update_interval
= GUI_REFRESH_INTERVAL_DEFAULT
;
842 dcl
->update_interval
= SDL_REFRESH_INTERVAL_BUSY
;
846 static void sdl_mouse_warp(DisplayChangeListener
*dcl
,
847 int x
, int y
, int on
)
852 if (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
) {
853 SDL_SetCursor(guest_sprite
);
854 if (!qemu_input_is_absolute() && !absolute_enabled
) {
861 guest_x
= x
, guest_y
= y
;
864 static void sdl_mouse_define(DisplayChangeListener
*dcl
,
867 uint8_t *image
, *mask
;
871 SDL_FreeCursor(guest_sprite
);
873 bpl
= cursor_get_mono_bpl(c
);
874 image
= g_malloc0(bpl
* c
->height
);
875 mask
= g_malloc0(bpl
* c
->height
);
876 cursor_get_mono_image(c
, 0x000000, image
);
877 cursor_get_mono_mask(c
, 0, mask
);
878 guest_sprite
= SDL_CreateCursor(image
, mask
, c
->width
, c
->height
,
884 (gui_grab
|| qemu_input_is_absolute() || absolute_enabled
))
885 SDL_SetCursor(guest_sprite
);
888 static void sdl_cleanup(void)
891 SDL_FreeCursor(guest_sprite
);
892 SDL_QuitSubSystem(SDL_INIT_VIDEO
);
895 static const DisplayChangeListenerOps dcl_ops
= {
897 .dpy_gfx_update
= sdl_update
,
898 .dpy_gfx_switch
= sdl_switch
,
899 .dpy_gfx_check_format
= sdl_check_format
,
900 .dpy_refresh
= sdl_refresh
,
901 .dpy_mouse_set
= sdl_mouse_warp
,
902 .dpy_cursor_define
= sdl_mouse_define
,
905 static void sdl1_display_init(DisplayState
*ds
, DisplayOptions
*o
)
909 const SDL_VideoInfo
*vi
;
913 assert(o
->type
== DISPLAY_TYPE_SDL
);
915 #if defined(__APPLE__)
916 /* always use generic keymaps */
917 if (!keyboard_layout
)
918 keyboard_layout
= "en-us";
920 if(keyboard_layout
) {
921 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
);