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 SDL_Surface
*screen
;
35 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
36 static int last_vm_running
;
37 static int gui_saved_grab
;
38 static int gui_fullscreen
;
39 static int gui_noframe
;
40 static int gui_key_modifier_pressed
;
41 static int gui_keysym
;
42 static int gui_fullscreen_initial_grab
;
43 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
44 static uint8_t modifiers_state
[256];
45 static int width
, height
;
46 static SDL_Cursor
*sdl_cursor_normal
;
47 static SDL_Cursor
*sdl_cursor_hidden
;
48 static int absolute_enabled
= 0;
49 static int guest_cursor
= 0;
50 static int guest_x
, guest_y
;
51 static SDL_Cursor
*guest_sprite
= 0;
53 static void sdl_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
55 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
56 SDL_UpdateRect(screen
, x
, y
, w
, h
);
59 static void sdl_resize(DisplayState
*ds
, int w
, int h
)
63 // printf("resizing to %d %d\n", w, h);
65 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
67 flags
|= SDL_FULLSCREEN
;
75 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
77 fprintf(stderr
, "Could not open SDL display\n");
80 if (!screen
->pixels
&& (flags
& SDL_HWSURFACE
) && (flags
& SDL_FULLSCREEN
)) {
81 flags
&= ~SDL_HWSURFACE
;
85 if (!screen
->pixels
) {
86 fprintf(stderr
, "Could not open SDL display\n");
89 ds
->data
= screen
->pixels
;
90 ds
->linesize
= screen
->pitch
;
91 ds
->depth
= screen
->format
->BitsPerPixel
;
92 /* SDL BitsPerPixel never indicates any values other than
93 multiples of 8, so we need to check for strange depths. */
94 if (ds
->depth
== 16) {
97 mask
= screen
->format
->Rmask
;
98 mask
|= screen
->format
->Gmask
;
99 mask
|= screen
->format
->Bmask
;
100 if ((mask
& 0x8000) == 0)
107 /* generic keyboard conversion */
109 #include "sdl_keysym.h"
112 static kbd_layout_t
*kbd_layout
= NULL
;
114 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
117 /* workaround for X11+SDL bug with AltGR */
118 keysym
= ev
->keysym
.sym
;
119 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
121 /* For Japanese key '\' and '|' */
122 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
125 return keysym2scancode(kbd_layout
, keysym
);
128 /* specific keyboard conversions from scan codes */
132 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
134 return ev
->keysym
.scancode
;
139 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
143 keycode
= ev
->keysym
.scancode
;
147 } else if (keycode
< 97) {
148 keycode
-= 8; /* just an offset */
149 } else if (keycode
< 212) {
150 /* use conversion table */
151 keycode
= _translate_keycode(keycode
- 97);
160 static void reset_keys(void)
163 for(i
= 0; i
< 256; i
++) {
164 if (modifiers_state
[i
]) {
166 kbd_put_keycode(0xe0);
167 kbd_put_keycode(i
| 0x80);
168 modifiers_state
[i
] = 0;
173 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
177 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
180 if (ev
->type
== SDL_KEYUP
)
182 kbd_put_keycode(0xe1);
183 kbd_put_keycode(0x1d | v
);
184 kbd_put_keycode(0x45 | v
);
189 keycode
= sdl_keyevent_to_keycode_generic(ev
);
191 keycode
= sdl_keyevent_to_keycode(ev
);
196 /* sent when leaving window: reset the modifiers state */
199 case 0x2a: /* Left Shift */
200 case 0x36: /* Right Shift */
201 case 0x1d: /* Left CTRL */
202 case 0x9d: /* Right CTRL */
203 case 0x38: /* Left ALT */
204 case 0xb8: /* Right ALT */
205 if (ev
->type
== SDL_KEYUP
)
206 modifiers_state
[keycode
] = 0;
208 modifiers_state
[keycode
] = 1;
210 case 0x45: /* num lock */
211 case 0x3a: /* caps lock */
212 /* SDL does not send the key up event, so we generate it */
213 kbd_put_keycode(keycode
);
214 kbd_put_keycode(keycode
| 0x80);
218 /* now send the key code */
220 kbd_put_keycode(0xe0);
221 if (ev
->type
== SDL_KEYUP
)
222 kbd_put_keycode(keycode
| 0x80);
224 kbd_put_keycode(keycode
& 0x7f);
227 static void sdl_update_caption(void)
230 const char *status
= "";
233 status
= " [Stopped]";
236 status
= " - Press Ctrl-Alt to exit grab";
238 status
= " - Press Ctrl-Alt-Shift to exit grab";
242 snprintf(buf
, sizeof(buf
), "QEMU (%s)%s", qemu_name
, status
);
244 snprintf(buf
, sizeof(buf
), "QEMU%s", status
);
246 SDL_WM_SetCaption(buf
, "QEMU");
249 static void sdl_hide_cursor(void)
254 if (kbd_mouse_is_absolute()) {
256 SDL_SetCursor(sdl_cursor_hidden
);
262 static void sdl_show_cursor(void)
267 if (!kbd_mouse_is_absolute()) {
270 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
271 SDL_SetCursor(guest_sprite
);
273 SDL_SetCursor(sdl_cursor_normal
);
277 static void sdl_grab_start(void)
280 SDL_SetCursor(guest_sprite
);
281 SDL_WarpMouse(guest_x
, guest_y
);
285 if (SDL_WM_GrabInput(SDL_GRAB_ON
) == SDL_GRAB_ON
) {
287 sdl_update_caption();
292 static void sdl_grab_end(void)
294 SDL_WM_GrabInput(SDL_GRAB_OFF
);
297 sdl_update_caption();
300 static void sdl_send_mouse_event(int dx
, int dy
, int dz
, int x
, int y
, int state
)
304 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
305 buttons
|= MOUSE_EVENT_LBUTTON
;
306 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
307 buttons
|= MOUSE_EVENT_RBUTTON
;
308 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
309 buttons
|= MOUSE_EVENT_MBUTTON
;
311 if (kbd_mouse_is_absolute()) {
312 if (!absolute_enabled
) {
317 absolute_enabled
= 1;
320 dx
= x
* 0x7FFF / (width
- 1);
321 dy
= y
* 0x7FFF / (height
- 1);
322 } else if (absolute_enabled
) {
324 absolute_enabled
= 0;
325 } else if (guest_cursor
) {
334 kbd_mouse_event(dx
, dy
, dz
, buttons
);
337 static void toggle_full_screen(DisplayState
*ds
)
339 gui_fullscreen
= !gui_fullscreen
;
340 sdl_resize(ds
, screen
->w
, screen
->h
);
341 if (gui_fullscreen
) {
342 gui_saved_grab
= gui_grab
;
352 static void sdl_refresh(DisplayState
*ds
)
354 SDL_Event ev1
, *ev
= &ev1
;
356 int buttonstate
= SDL_GetMouseState(NULL
, NULL
);
358 if (last_vm_running
!= vm_running
) {
359 last_vm_running
= vm_running
;
360 sdl_update_caption();
364 SDL_EnableUNICODE(!is_graphic_console());
366 while (SDL_PollEvent(ev
)) {
368 case SDL_VIDEOEXPOSE
:
369 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
373 if (ev
->type
== SDL_KEYDOWN
) {
375 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
378 mod_state
= (SDL_GetModState() & (gui_grab_code
| KMOD_LSHIFT
)) ==
379 (gui_grab_code
| KMOD_LSHIFT
);
381 gui_key_modifier_pressed
= mod_state
;
382 if (gui_key_modifier_pressed
) {
384 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
386 case 0x21: /* 'f' key on US keyboard */
387 toggle_full_screen(ds
);
390 case 0x02 ... 0x0a: /* '1' to '9' keys */
391 /* Reset the modifiers sent to the current console */
393 console_select(keycode
- 0x02);
394 if (!is_graphic_console()) {
395 /* display grab if going to a text console */
404 } else if (!is_graphic_console()) {
407 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
408 switch(ev
->key
.keysym
.sym
) {
409 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
410 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
411 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
412 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
413 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
414 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
415 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
416 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
420 switch(ev
->key
.keysym
.sym
) {
421 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
422 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
423 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
424 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
425 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
426 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
427 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
428 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
429 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break;
430 case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
435 kbd_put_keysym(keysym
);
436 } else if (ev
->key
.keysym
.unicode
!= 0) {
437 kbd_put_keysym(ev
->key
.keysym
.unicode
);
440 } else if (ev
->type
== SDL_KEYUP
) {
442 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
444 mod_state
= (ev
->key
.keysym
.mod
&
445 (gui_grab_code
| KMOD_LSHIFT
));
448 if (gui_key_modifier_pressed
) {
449 gui_key_modifier_pressed
= 0;
450 if (gui_keysym
== 0) {
451 /* exit/enter grab if pressing Ctrl-Alt */
453 /* if the application is not active,
454 do not try to enter grab state. It
456 'SDL_WM_GrabInput(SDL_GRAB_ON)'
457 from blocking all the application
459 if (SDL_GetAppState() & SDL_APPACTIVE
)
464 /* SDL does not send back all the
465 modifiers key, so we must correct it */
473 if (is_graphic_console() && !gui_keysym
)
474 sdl_process_key(&ev
->key
);
478 qemu_system_shutdown_request();
480 case SDL_MOUSEMOTION
:
481 if (gui_grab
|| kbd_mouse_is_absolute() ||
483 sdl_send_mouse_event(ev
->motion
.xrel
, ev
->motion
.yrel
, 0,
484 ev
->motion
.x
, ev
->motion
.y
, ev
->motion
.state
);
487 case SDL_MOUSEBUTTONDOWN
:
488 case SDL_MOUSEBUTTONUP
:
490 SDL_MouseButtonEvent
*bev
= &ev
->button
;
491 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
492 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
493 (bev
->button
== SDL_BUTTON_LEFT
)) {
494 /* start grabbing all events */
500 if (ev
->type
== SDL_MOUSEBUTTONDOWN
) {
501 buttonstate
|= SDL_BUTTON(bev
->button
);
503 buttonstate
&= ~SDL_BUTTON(bev
->button
);
505 #ifdef SDL_BUTTON_WHEELUP
506 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
508 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
512 sdl_send_mouse_event(0, 0, dz
, bev
->x
, bev
->y
, buttonstate
);
516 case SDL_ACTIVEEVENT
:
517 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
518 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
521 if (ev
->active
.state
& SDL_APPACTIVE
) {
522 if (ev
->active
.gain
) {
523 /* Back to default interval */
524 ds
->gui_timer_interval
= 0;
527 /* Sleeping interval */
528 ds
->gui_timer_interval
= 500;
539 static void sdl_fill(DisplayState
*ds
, int x
, int y
, int w
, int h
, uint32_t c
)
541 SDL_Rect dst
= { x
, y
, w
, h
};
542 SDL_FillRect(screen
, &dst
, c
);
545 static void sdl_mouse_warp(int x
, int y
, int on
)
550 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
551 SDL_SetCursor(guest_sprite
);
557 guest_x
= x
, guest_y
= y
;
560 static void sdl_mouse_define(int width
, int height
, int bpp
,
561 int hot_x
, int hot_y
,
562 uint8_t *image
, uint8_t *mask
)
564 uint8_t sprite
[256], *line
;
565 int x
, y
, dst
, bypl
, src
= 0;
567 SDL_FreeCursor(guest_sprite
);
569 memset(sprite
, 0, 256);
570 bypl
= ((width
* bpp
+ 31) >> 5) << 2;
571 for (y
= 0, dst
= 0; y
< height
; y
++, image
+= bypl
) {
573 for (x
= 0; x
< width
; x
++, dst
++) {
576 src
= *(line
++); src
|= *(line
++); src
|= *(line
++);
580 src
= *(line
++); src
|= *(line
++);
586 src
= 0xf & (line
[x
>> 1] >> ((x
& 1)) << 2);
589 src
= 3 & (line
[x
>> 2] >> ((x
& 3)) << 1);
592 src
= 1 & (line
[x
>> 3] >> (x
& 7));
596 sprite
[dst
>> 3] |= (1 << (~dst
& 7)) & mask
[dst
>> 3];
599 guest_sprite
= SDL_CreateCursor(sprite
, mask
, width
, height
, hot_x
, hot_y
);
602 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
603 SDL_SetCursor(guest_sprite
);
606 static void sdl_cleanup(void)
609 SDL_FreeCursor(guest_sprite
);
613 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
618 #if defined(__APPLE__)
619 /* always use generic keymaps */
620 if (!keyboard_layout
)
621 keyboard_layout
= "en-us";
623 if(keyboard_layout
) {
624 kbd_layout
= init_keyboard_layout(keyboard_layout
);
632 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
633 if (SDL_Init (flags
)) {
634 fprintf(stderr
, "Could not initialize SDL - exiting\n");
638 ds
->dpy_update
= sdl_update
;
639 ds
->dpy_resize
= sdl_resize
;
640 ds
->dpy_refresh
= sdl_refresh
;
641 ds
->dpy_fill
= sdl_fill
;
642 ds
->mouse_set
= sdl_mouse_warp
;
643 ds
->cursor_define
= sdl_mouse_define
;
645 sdl_resize(ds
, 640, 400);
646 sdl_update_caption();
647 SDL_EnableKeyRepeat(250, 50);
650 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
651 sdl_cursor_normal
= SDL_GetCursor();
656 gui_fullscreen_initial_grab
= 1;