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_UpdateRect(real_screen
, x
, y
, w
, h
);
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
;
96 real_screen
= SDL_SetVideoMode(ds_get_width(ds
), ds_get_height(ds
), 0, flags
);
98 fprintf(stderr
, "Could not open SDL display\n");
105 /* generic keyboard conversion */
107 #include "sdl_keysym.h"
110 static kbd_layout_t
*kbd_layout
= NULL
;
112 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
115 /* workaround for X11+SDL bug with AltGR */
116 keysym
= ev
->keysym
.sym
;
117 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
119 /* For Japanese key '\' and '|' */
120 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
123 return keysym2scancode(kbd_layout
, keysym
);
126 /* specific keyboard conversions from scan codes */
130 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
132 return ev
->keysym
.scancode
;
137 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
141 keycode
= ev
->keysym
.scancode
;
145 } else if (keycode
< 97) {
146 keycode
-= 8; /* just an offset */
147 } else if (keycode
< 212) {
148 /* use conversion table */
149 keycode
= _translate_keycode(keycode
- 97);
158 static void reset_keys(void)
161 for(i
= 0; i
< 256; i
++) {
162 if (modifiers_state
[i
]) {
164 kbd_put_keycode(0xe0);
165 kbd_put_keycode(i
| 0x80);
166 modifiers_state
[i
] = 0;
171 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
175 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
178 if (ev
->type
== SDL_KEYUP
)
180 kbd_put_keycode(0xe1);
181 kbd_put_keycode(0x1d | v
);
182 kbd_put_keycode(0x45 | v
);
187 keycode
= sdl_keyevent_to_keycode_generic(ev
);
189 keycode
= sdl_keyevent_to_keycode(ev
);
194 /* sent when leaving window: reset the modifiers state */
197 case 0x2a: /* Left Shift */
198 case 0x36: /* Right Shift */
199 case 0x1d: /* Left CTRL */
200 case 0x9d: /* Right CTRL */
201 case 0x38: /* Left ALT */
202 case 0xb8: /* Right ALT */
203 if (ev
->type
== SDL_KEYUP
)
204 modifiers_state
[keycode
] = 0;
206 modifiers_state
[keycode
] = 1;
208 case 0x45: /* num lock */
209 case 0x3a: /* caps lock */
210 /* SDL does not send the key up event, so we generate it */
211 kbd_put_keycode(keycode
);
212 kbd_put_keycode(keycode
| 0x80);
216 /* now send the key code */
218 kbd_put_keycode(0xe0);
219 if (ev
->type
== SDL_KEYUP
)
220 kbd_put_keycode(keycode
| 0x80);
222 kbd_put_keycode(keycode
& 0x7f);
225 static void sdl_update_caption(void)
228 const char *status
= "";
231 status
= " [Stopped]";
234 status
= " - Press Ctrl-Alt to exit grab";
236 status
= " - Press Ctrl-Alt-Shift to exit grab";
240 snprintf(buf
, sizeof(buf
), "QEMU (%s)%s", qemu_name
, status
);
242 snprintf(buf
, sizeof(buf
), "QEMU%s", status
);
244 SDL_WM_SetCaption(buf
, "QEMU");
247 static void sdl_hide_cursor(void)
252 if (kbd_mouse_is_absolute()) {
254 SDL_SetCursor(sdl_cursor_hidden
);
260 static void sdl_show_cursor(void)
265 if (!kbd_mouse_is_absolute()) {
268 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
269 SDL_SetCursor(guest_sprite
);
271 SDL_SetCursor(sdl_cursor_normal
);
275 static void sdl_grab_start(void)
278 SDL_SetCursor(guest_sprite
);
279 SDL_WarpMouse(guest_x
, guest_y
);
283 if (SDL_WM_GrabInput(SDL_GRAB_ON
) == SDL_GRAB_ON
) {
285 sdl_update_caption();
290 static void sdl_grab_end(void)
292 SDL_WM_GrabInput(SDL_GRAB_OFF
);
295 sdl_update_caption();
298 static void sdl_send_mouse_event(int dx
, int dy
, int dz
, int x
, int y
, int state
)
302 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
303 buttons
|= MOUSE_EVENT_LBUTTON
;
304 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
305 buttons
|= MOUSE_EVENT_RBUTTON
;
306 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
307 buttons
|= MOUSE_EVENT_MBUTTON
;
309 if (kbd_mouse_is_absolute()) {
310 if (!absolute_enabled
) {
315 absolute_enabled
= 1;
318 dx
= x
* 0x7FFF / (width
- 1);
319 dy
= y
* 0x7FFF / (height
- 1);
320 } else if (absolute_enabled
) {
322 absolute_enabled
= 0;
323 } else if (guest_cursor
) {
332 kbd_mouse_event(dx
, dy
, dz
, buttons
);
335 static void toggle_full_screen(DisplayState
*ds
)
337 gui_fullscreen
= !gui_fullscreen
;
339 if (gui_fullscreen
) {
340 gui_saved_grab
= gui_grab
;
350 static void sdl_refresh(DisplayState
*ds
)
352 SDL_Event ev1
, *ev
= &ev1
;
354 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
356 if (last_vm_running
!= vm_running
) {
357 last_vm_running
= vm_running
;
358 sdl_update_caption();
362 SDL_EnableUNICODE(!is_graphic_console());
364 while (SDL_PollEvent(ev
)) {
366 case SDL_VIDEOEXPOSE
:
367 sdl_update(ds
, 0, 0, real_screen
->w
, real_screen
->h
);
371 if (ev
->type
== SDL_KEYDOWN
) {
373 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
376 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
377 (gui_grab_code
| KMOD_LSHIFT
);
379 gui_key_modifier_pressed
= mod_state
;
380 if (gui_key_modifier_pressed
) {
382 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
384 case 0x21: /* 'f' key on US keyboard */
385 toggle_full_screen(ds
);
388 case 0x02 ... 0x0a: /* '1' to '9' keys */
389 /* Reset the modifiers sent to the current console */
391 console_select(keycode
- 0x02);
392 if (!is_graphic_console()) {
393 /* display grab if going to a text console */
402 } else if (!is_graphic_console()) {
405 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
406 switch(ev
->key
.keysym
.sym
) {
407 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
408 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
409 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
410 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
411 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
412 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
413 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
414 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
418 switch(ev
->key
.keysym
.sym
) {
419 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
420 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
421 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
422 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
423 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
424 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
425 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
426 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
427 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break;
428 case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
433 kbd_put_keysym(keysym
);
434 } else if (ev
->key
.keysym
.unicode
!= 0) {
435 kbd_put_keysym(ev
->key
.keysym
.unicode
);
438 } else if (ev
->type
== SDL_KEYUP
) {
440 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
442 mod_state
= (ev
->key
.keysym
.mod
&
443 (gui_grab_code
| KMOD_LSHIFT
));
446 if (gui_key_modifier_pressed
) {
447 gui_key_modifier_pressed
= 0;
448 if (gui_keysym
== 0) {
449 /* exit/enter grab if pressing Ctrl-Alt */
451 /* if the application is not active,
452 do not try to enter grab state. It
454 'SDL_WM_GrabInput(SDL_GRAB_ON)'
455 from blocking all the application
457 if (SDL_GetAppState() & SDL_APPACTIVE
)
462 /* SDL does not send back all the
463 modifiers key, so we must correct it */
471 if (is_graphic_console() && !gui_keysym
)
472 sdl_process_key(&ev
->key
);
476 qemu_system_shutdown_request();
478 case SDL_MOUSEMOTION
:
479 if (gui_grab
|| kbd_mouse_is_absolute() ||
481 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
, 0,
482 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
485 case SDL_MOUSEBUTTONDOWN
:
486 case SDL_MOUSEBUTTONUP
:
488 SDL_MouseButtonEvent
*bev
= &ev
->button
;
489 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
490 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
491 (bev
->button
== SDL_BUTTON_LEFT
)) {
492 /* start grabbing all events */
498 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
499 buttonstate
|= SDL_BUTTON(bev
->button
);
501 buttonstate
&= ~SDL_BUTTON(bev
->button
);
503 #ifdef SDL_BUTTON_WHEELUP
504 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
506 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
510 sdl_send_mouse_event(0, 0, dz
, bev
->x
, bev
->y
, buttonstate
);
514 case SDL_ACTIVEEVENT
:
515 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
516 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
519 if (ev
->active
.state
& SDL_APPACTIVE
) {
520 if (ev
->active
.gain
) {
521 /* Back to default interval */
522 dcl
->gui_timer_interval
= 0;
525 /* Sleeping interval */
526 dcl
->gui_timer_interval
= 500;
537 static void sdl_fill(DisplayState
*ds
, int x
, int y
, int w
, int h
, uint32_t c
)
539 SDL_Rect dst
= { x
, y
, w
, h
};
540 SDL_FillRect(real_screen
, &dst
, c
);
543 static void sdl_mouse_warp(int x
, int y
, int on
)
548 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
549 SDL_SetCursor(guest_sprite
);
555 guest_x
= x
, guest_y
= y
;
558 static void sdl_mouse_define(int width
, int height
, int bpp
,
559 int hot_x
, int hot_y
,
560 uint8_t *image
, uint8_t *mask
)
562 uint8_t sprite
[256], *line
;
563 int x
, y
, dst
, bypl
, src
= 0;
565 SDL_FreeCursor(guest_sprite
);
567 memset(sprite
, 0, 256);
568 bypl
= ((width
* bpp
+ 31) >> 5) << 2;
569 for (y
= 0, dst
= 0; y
< height
; y
++, image
+= bypl
) {
571 for (x
= 0; x
< width
; x
++, dst
++) {
574 src
= *(line
++); src
|= *(line
++); src
|= *(line
++);
578 src
= *(line
++); src
|= *(line
++);
584 src
= 0xf & (line
[x
>> 1] >> ((x
& 1)) << 2);
587 src
= 3 & (line
[x
>> 2] >> ((x
& 3)) << 1);
590 src
= 1 & (line
[x
>> 3] >> (x
& 7));
594 sprite
[dst
>> 3] |= (1 << (~dst
& 7)) & mask
[dst
>> 3];
597 guest_sprite
= SDL_CreateCursor(sprite
, mask
, width
, height
, hot_x
, hot_y
);
600 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
601 SDL_SetCursor(guest_sprite
);
604 static void sdl_cleanup(void)
607 SDL_FreeCursor(guest_sprite
);
611 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
616 #if defined(__APPLE__)
617 /* always use generic keymaps */
618 if (!keyboard_layout
)
619 keyboard_layout
= "en-us";
621 if(keyboard_layout
) {
622 kbd_layout
= init_keyboard_layout(keyboard_layout
);
630 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
631 if (SDL_Init (flags
)) {
632 fprintf(stderr
, "Could not initialize SDL - exiting\n");
636 dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
639 dcl
->dpy_update
= sdl_update
;
640 dcl
->dpy_resize
= sdl_resize
;
641 dcl
->dpy_refresh
= sdl_refresh
;
642 dcl
->dpy_setdata
= sdl_setdata
;
643 dcl
->dpy_fill
= sdl_fill
;
644 ds
->mouse_set
= sdl_mouse_warp
;
645 ds
->cursor_define
= sdl_mouse_define
;
646 register_displaychangelistener(ds
, dcl
);
648 sdl_update_caption();
649 SDL_EnableKeyRepeat(250, 50);
652 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
653 sdl_cursor_normal
= SDL_GetCursor();
658 gui_fullscreen_initial_grab
= 1;