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
24 #include "qemu-common.h"
34 static DisplayChangeListener
*dcl
;
35 static SDL_Surface
*real_screen
;
36 static SDL_Surface
*guest_screen
= NULL
;
37 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
38 static int last_vm_running
;
39 static int gui_saved_grab
;
40 static int gui_fullscreen
;
41 static int gui_noframe
;
42 static int gui_key_modifier_pressed
;
43 static int gui_keysym
;
44 static int gui_fullscreen_initial_grab
;
45 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
46 static uint8_t modifiers_state
[256];
47 static int width
, height
;
48 static SDL_Cursor
*sdl_cursor_normal
;
49 static SDL_Cursor
*sdl_cursor_hidden
;
50 static int absolute_enabled
= 0;
51 static int guest_cursor
= 0;
52 static int guest_x
, guest_y
;
53 static SDL_Cursor
*guest_sprite
= 0;
55 static void sdl_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
62 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
64 SDL_BlitSurface(guest_screen
, &rec
, real_screen
, &rec
);
65 SDL_Flip(real_screen
);
68 static void sdl_setdata(DisplayState
*ds
)
73 rec
.w
= real_screen
->w
;
74 rec
.h
= real_screen
->h
;
76 if (guest_screen
!= NULL
) SDL_FreeSurface(guest_screen
);
78 guest_screen
= SDL_CreateRGBSurfaceFrom(ds_get_data(ds
), ds_get_width(ds
), ds_get_height(ds
),
79 ds_get_bits_per_pixel(ds
), ds_get_linesize(ds
),
80 ds
->surface
->pf
.rmask
, ds
->surface
->pf
.gmask
,
81 ds
->surface
->pf
.bmask
, ds
->surface
->pf
.amask
);
84 static void sdl_resize(DisplayState
*ds
)
88 // printf("resizing to %d %d\n", w, h);
90 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
92 flags
|= SDL_FULLSCREEN
;
97 real_screen
= SDL_SetVideoMode(ds_get_width(ds
), ds_get_height(ds
), 0, flags
);
99 fprintf(stderr
, "Could not open SDL display\n");
102 if (!real_screen
->pixels
&& (flags
& SDL_HWSURFACE
) && (flags
& SDL_FULLSCREEN
)) {
103 flags
&= ~SDL_HWSURFACE
;
107 if (!real_screen
->pixels
) {
108 fprintf(stderr
, "Could not open SDL display\n");
115 /* generic keyboard conversion */
117 #include "sdl_keysym.h"
120 static kbd_layout_t
*kbd_layout
= NULL
;
122 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
125 /* workaround for X11+SDL bug with AltGR */
126 keysym
= ev
->keysym
.sym
;
127 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
129 /* For Japanese key '\' and '|' */
130 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
133 return keysym2scancode(kbd_layout
, keysym
);
136 /* specific keyboard conversions from scan codes */
140 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
142 return ev
->keysym
.scancode
;
147 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
151 keycode
= ev
->keysym
.scancode
;
155 } else if (keycode
< 97) {
156 keycode
-= 8; /* just an offset */
157 } else if (keycode
< 212) {
158 /* use conversion table */
159 keycode
= _translate_keycode(keycode
- 97);
168 static void reset_keys(void)
171 for(i
= 0; i
< 256; i
++) {
172 if (modifiers_state
[i
]) {
174 kbd_put_keycode(0xe0);
175 kbd_put_keycode(i
| 0x80);
176 modifiers_state
[i
] = 0;
181 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
185 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
188 if (ev
->type
== SDL_KEYUP
)
190 kbd_put_keycode(0xe1);
191 kbd_put_keycode(0x1d | v
);
192 kbd_put_keycode(0x45 | v
);
197 keycode
= sdl_keyevent_to_keycode_generic(ev
);
199 keycode
= sdl_keyevent_to_keycode(ev
);
204 /* sent when leaving window: reset the modifiers state */
207 case 0x2a: /* Left Shift */
208 case 0x36: /* Right Shift */
209 case 0x1d: /* Left CTRL */
210 case 0x9d: /* Right CTRL */
211 case 0x38: /* Left ALT */
212 case 0xb8: /* Right ALT */
213 if (ev
->type
== SDL_KEYUP
)
214 modifiers_state
[keycode
] = 0;
216 modifiers_state
[keycode
] = 1;
218 case 0x45: /* num lock */
219 case 0x3a: /* caps lock */
220 /* SDL does not send the key up event, so we generate it */
221 kbd_put_keycode(keycode
);
222 kbd_put_keycode(keycode
| 0x80);
226 /* now send the key code */
228 kbd_put_keycode(0xe0);
229 if (ev
->type
== SDL_KEYUP
)
230 kbd_put_keycode(keycode
| 0x80);
232 kbd_put_keycode(keycode
& 0x7f);
235 static void sdl_update_caption(void)
238 const char *status
= "";
241 status
= " [Stopped]";
244 status
= " - Press Ctrl-Alt to exit grab";
246 status
= " - Press Ctrl-Alt-Shift to exit grab";
250 snprintf(buf
, sizeof(buf
), "QEMU (%s)%s", qemu_name
, status
);
252 snprintf(buf
, sizeof(buf
), "QEMU%s", status
);
254 SDL_WM_SetCaption(buf
, "QEMU");
257 static void sdl_hide_cursor(void)
262 if (kbd_mouse_is_absolute()) {
264 SDL_SetCursor(sdl_cursor_hidden
);
270 static void sdl_show_cursor(void)
275 if (!kbd_mouse_is_absolute()) {
278 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
279 SDL_SetCursor(guest_sprite
);
281 SDL_SetCursor(sdl_cursor_normal
);
285 static void sdl_grab_start(void)
288 SDL_SetCursor(guest_sprite
);
289 SDL_WarpMouse(guest_x
, guest_y
);
293 if (SDL_WM_GrabInput(SDL_GRAB_ON
) == SDL_GRAB_ON
) {
295 sdl_update_caption();
300 static void sdl_grab_end(void)
302 SDL_WM_GrabInput(SDL_GRAB_OFF
);
305 sdl_update_caption();
308 static void sdl_send_mouse_event(int dx
, int dy
, int dz
, int x
, int y
, int state
)
312 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
313 buttons
|= MOUSE_EVENT_LBUTTON
;
314 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
315 buttons
|= MOUSE_EVENT_RBUTTON
;
316 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
317 buttons
|= MOUSE_EVENT_MBUTTON
;
319 if (kbd_mouse_is_absolute()) {
320 if (!absolute_enabled
) {
325 absolute_enabled
= 1;
328 dx
= x
* 0x7FFF / (width
- 1);
329 dy
= y
* 0x7FFF / (height
- 1);
330 } else if (absolute_enabled
) {
332 absolute_enabled
= 0;
333 } else if (guest_cursor
) {
342 kbd_mouse_event(dx
, dy
, dz
, buttons
);
345 static void toggle_full_screen(DisplayState
*ds
)
347 gui_fullscreen
= !gui_fullscreen
;
349 if (gui_fullscreen
) {
350 gui_saved_grab
= gui_grab
;
360 static void sdl_refresh(DisplayState
*ds
)
362 SDL_Event ev1
, *ev
= &ev1
;
364 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
366 if (last_vm_running
!= vm_running
) {
367 last_vm_running
= vm_running
;
368 sdl_update_caption();
372 SDL_EnableUNICODE(!is_graphic_console());
374 while (SDL_PollEvent(ev
)) {
376 case SDL_VIDEOEXPOSE
:
377 sdl_update(ds
, 0, 0, real_screen
->w
, real_screen
->h
);
381 if (ev
->type
== SDL_KEYDOWN
) {
383 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
386 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
387 (gui_grab_code
| KMOD_LSHIFT
);
389 gui_key_modifier_pressed
= mod_state
;
390 if (gui_key_modifier_pressed
) {
392 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
394 case 0x21: /* 'f' key on US keyboard */
395 toggle_full_screen(ds
);
398 case 0x02 ... 0x0a: /* '1' to '9' keys */
399 /* Reset the modifiers sent to the current console */
401 console_select(keycode
- 0x02);
402 if (!is_graphic_console()) {
403 /* display grab if going to a text console */
412 } else if (!is_graphic_console()) {
415 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
416 switch(ev
->key
.keysym
.sym
) {
417 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
418 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
419 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
420 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
421 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
422 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
423 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
424 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
428 switch(ev
->key
.keysym
.sym
) {
429 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
430 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
431 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
432 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
433 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
434 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
435 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
436 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
437 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break;
438 case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
443 kbd_put_keysym(keysym
);
444 } else if (ev
->key
.keysym
.unicode
!= 0) {
445 kbd_put_keysym(ev
->key
.keysym
.unicode
);
448 } else if (ev
->type
== SDL_KEYUP
) {
450 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
452 mod_state
= (ev
->key
.keysym
.mod
&
453 (gui_grab_code
| KMOD_LSHIFT
));
456 if (gui_key_modifier_pressed
) {
457 gui_key_modifier_pressed
= 0;
458 if (gui_keysym
== 0) {
459 /* exit/enter grab if pressing Ctrl-Alt */
461 /* if the application is not active,
462 do not try to enter grab state. It
464 'SDL_WM_GrabInput(SDL_GRAB_ON)'
465 from blocking all the application
467 if (SDL_GetAppState() & SDL_APPACTIVE
)
472 /* SDL does not send back all the
473 modifiers key, so we must correct it */
481 if (is_graphic_console() && !gui_keysym
)
482 sdl_process_key(&ev
->key
);
486 qemu_system_shutdown_request();
488 case SDL_MOUSEMOTION
:
489 if (gui_grab
|| kbd_mouse_is_absolute() ||
491 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
, 0,
492 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
495 case SDL_MOUSEBUTTONDOWN
:
496 case SDL_MOUSEBUTTONUP
:
498 SDL_MouseButtonEvent
*bev
= &ev
->button
;
499 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
500 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
501 (bev
->button
== SDL_BUTTON_LEFT
)) {
502 /* start grabbing all events */
508 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
509 buttonstate
|= SDL_BUTTON(bev
->button
);
511 buttonstate
&= ~SDL_BUTTON(bev
->button
);
513 #ifdef SDL_BUTTON_WHEELUP
514 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
516 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
520 sdl_send_mouse_event(0, 0, dz
, bev
->x
, bev
->y
, buttonstate
);
524 case SDL_ACTIVEEVENT
:
525 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
526 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
529 if (ev
->active
.state
& SDL_APPACTIVE
) {
530 if (ev
->active
.gain
) {
531 /* Back to default interval */
532 dcl
->gui_timer_interval
= 0;
535 /* Sleeping interval */
536 dcl
->gui_timer_interval
= 500;
547 static void sdl_fill(DisplayState
*ds
, int x
, int y
, int w
, int h
, uint32_t c
)
549 SDL_Rect dst
= { x
, y
, w
, h
};
550 SDL_FillRect(real_screen
, &dst
, c
);
553 static void sdl_mouse_warp(int x
, int y
, int on
)
558 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
559 SDL_SetCursor(guest_sprite
);
565 guest_x
= x
, guest_y
= y
;
568 static void sdl_mouse_define(int width
, int height
, int bpp
,
569 int hot_x
, int hot_y
,
570 uint8_t *image
, uint8_t *mask
)
572 uint8_t sprite
[256], *line
;
573 int x
, y
, dst
, bypl
, src
= 0;
575 SDL_FreeCursor(guest_sprite
);
577 memset(sprite
, 0, 256);
578 bypl
= ((width
* bpp
+ 31) >> 5) << 2;
579 for (y
= 0, dst
= 0; y
< height
; y
++, image
+= bypl
) {
581 for (x
= 0; x
< width
; x
++, dst
++) {
584 src
= *(line
++); src
|= *(line
++); src
|= *(line
++);
588 src
= *(line
++); src
|= *(line
++);
594 src
= 0xf & (line
[x
>> 1] >> ((x
& 1)) << 2);
597 src
= 3 & (line
[x
>> 2] >> ((x
& 3)) << 1);
600 src
= 1 & (line
[x
>> 3] >> (x
& 7));
604 sprite
[dst
>> 3] |= (1 << (~dst
& 7)) & mask
[dst
>> 3];
607 guest_sprite
= SDL_CreateCursor(sprite
, mask
, width
, height
, hot_x
, hot_y
);
610 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
611 SDL_SetCursor(guest_sprite
);
614 static void sdl_cleanup(void)
617 SDL_FreeCursor(guest_sprite
);
621 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
626 #if defined(__APPLE__)
627 /* always use generic keymaps */
628 if (!keyboard_layout
)
629 keyboard_layout
= "en-us";
631 if(keyboard_layout
) {
632 kbd_layout
= init_keyboard_layout(keyboard_layout
);
640 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
641 if (SDL_Init (flags
)) {
642 fprintf(stderr
, "Could not initialize SDL - exiting\n");
646 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
649 dcl
->dpy_update
= sdl_update
;
650 dcl
->dpy_resize
= sdl_resize
;
651 dcl
->dpy_refresh
= sdl_refresh
;
652 dcl
->dpy_setdata
= sdl_setdata
;
653 dcl
->dpy_fill
= sdl_fill
;
654 ds
->mouse_set
= sdl_mouse_warp
;
655 ds
->cursor_define
= sdl_mouse_define
;
656 register_displaychangelistener(ds
, dcl
);
658 sdl_update_caption();
659 SDL_EnableKeyRepeat(250, 50);
662 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
663 sdl_cursor_normal
= SDL_GetCursor();
668 gui_fullscreen_initial_grab
= 1;