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
32 static SDL_Surface
*screen
;
33 static int gui_grab
; /* if true, all keyboard/mouse events are grabbed */
34 static int last_vm_running
;
35 static int gui_saved_grab
;
36 static int gui_fullscreen
;
37 static int gui_noframe
;
38 static int gui_key_modifier_pressed
;
39 static int gui_keysym
;
40 static int gui_fullscreen_initial_grab
;
41 static int gui_grab_code
= KMOD_LALT
| KMOD_LCTRL
;
42 static uint8_t modifiers_state
[256];
43 static int width
, height
;
44 static SDL_Cursor
*sdl_cursor_normal
;
45 static SDL_Cursor
*sdl_cursor_hidden
;
46 static int absolute_enabled
= 0;
47 static int guest_cursor
= 0;
48 static int guest_x
, guest_y
;
49 static SDL_Cursor
*guest_sprite
= 0;
51 static void sdl_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
53 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
54 SDL_UpdateRect(screen
, x
, y
, w
, h
);
57 static void sdl_resize(DisplayState
*ds
, int w
, int h
)
61 // printf("resizing to %d %d\n", w, h);
63 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
65 flags
|= SDL_FULLSCREEN
;
73 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
75 fprintf(stderr
, "Could not open SDL display\n");
78 if (!screen
->pixels
&& (flags
& SDL_HWSURFACE
) && (flags
& SDL_FULLSCREEN
)) {
79 flags
&= ~SDL_HWSURFACE
;
83 if (!screen
->pixels
) {
84 fprintf(stderr
, "Could not open SDL display\n");
87 ds
->data
= screen
->pixels
;
88 ds
->linesize
= screen
->pitch
;
89 ds
->depth
= screen
->format
->BitsPerPixel
;
90 if (ds
->depth
== 32 && screen
->format
->Rshift
== 0) {
99 /* generic keyboard conversion */
101 #include "sdl_keysym.h"
104 static kbd_layout_t
*kbd_layout
= NULL
;
106 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent
*ev
)
109 /* workaround for X11+SDL bug with AltGR */
110 keysym
= ev
->keysym
.sym
;
111 if (keysym
== 0 && ev
->keysym
.scancode
== 113)
113 /* For Japanese key '\' and '|' */
114 if (keysym
== 92 && ev
->keysym
.scancode
== 133) {
117 return keysym2scancode(kbd_layout
, keysym
);
120 /* specific keyboard conversions from scan codes */
124 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
126 return ev
->keysym
.scancode
;
131 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent
*ev
)
135 keycode
= ev
->keysym
.scancode
;
139 } else if (keycode
< 97) {
140 keycode
-= 8; /* just an offset */
141 } else if (keycode
< 212) {
142 /* use conversion table */
143 keycode
= _translate_keycode(keycode
- 97);
152 static void reset_keys(void)
155 for(i
= 0; i
< 256; i
++) {
156 if (modifiers_state
[i
]) {
158 kbd_put_keycode(0xe0);
159 kbd_put_keycode(i
| 0x80);
160 modifiers_state
[i
] = 0;
165 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
169 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
172 if (ev
->type
== SDL_KEYUP
)
174 kbd_put_keycode(0xe1);
175 kbd_put_keycode(0x1d | v
);
176 kbd_put_keycode(0x45 | v
);
181 keycode
= sdl_keyevent_to_keycode_generic(ev
);
183 keycode
= sdl_keyevent_to_keycode(ev
);
188 /* sent when leaving window: reset the modifiers state */
191 case 0x2a: /* Left Shift */
192 case 0x36: /* Right Shift */
193 case 0x1d: /* Left CTRL */
194 case 0x9d: /* Right CTRL */
195 case 0x38: /* Left ALT */
196 case 0xb8: /* Right ALT */
197 if (ev
->type
== SDL_KEYUP
)
198 modifiers_state
[keycode
] = 0;
200 modifiers_state
[keycode
] = 1;
202 case 0x45: /* num lock */
203 case 0x3a: /* caps lock */
204 /* SDL does not send the key up event, so we generate it */
205 kbd_put_keycode(keycode
);
206 kbd_put_keycode(keycode
| 0x80);
210 /* now send the key code */
212 kbd_put_keycode(0xe0);
213 if (ev
->type
== SDL_KEYUP
)
214 kbd_put_keycode(keycode
| 0x80);
216 kbd_put_keycode(keycode
& 0x7f);
219 static void sdl_update_caption(void)
222 const char *status
= "";
225 status
= " [Stopped]";
227 status
= " - Press Ctrl-Alt to exit grab";
230 snprintf(buf
, sizeof(buf
), "QEMU (%s)%s", qemu_name
, status
);
232 snprintf(buf
, sizeof(buf
), "QEMU%s", status
);
234 SDL_WM_SetCaption(buf
, "QEMU");
237 static void sdl_hide_cursor(void)
239 if (kbd_mouse_is_absolute()) {
241 SDL_SetCursor(sdl_cursor_hidden
);
247 static void sdl_show_cursor(void)
249 if (!kbd_mouse_is_absolute()) {
252 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
253 SDL_SetCursor(guest_sprite
);
255 SDL_SetCursor(sdl_cursor_normal
);
259 static void sdl_grab_start(void)
262 SDL_SetCursor(guest_sprite
);
263 SDL_WarpMouse(guest_x
, guest_y
);
266 SDL_WM_GrabInput(SDL_GRAB_ON
);
267 /* dummy read to avoid moving the mouse */
268 SDL_GetRelativeMouseState(NULL
, NULL
);
270 sdl_update_caption();
273 static void sdl_grab_end(void)
275 SDL_WM_GrabInput(SDL_GRAB_OFF
);
278 sdl_update_caption();
281 static void sdl_send_mouse_event(int dz
)
283 int dx
, dy
, state
, buttons
;
284 state
= SDL_GetRelativeMouseState(&dx
, &dy
);
286 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
287 buttons
|= MOUSE_EVENT_LBUTTON
;
288 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
289 buttons
|= MOUSE_EVENT_RBUTTON
;
290 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
291 buttons
|= MOUSE_EVENT_MBUTTON
;
293 if (kbd_mouse_is_absolute()) {
294 if (!absolute_enabled
) {
299 absolute_enabled
= 1;
302 SDL_GetMouseState(&dx
, &dy
);
303 dx
= dx
* 0x7FFF / width
;
304 dy
= dy
* 0x7FFF / height
;
305 } else if (absolute_enabled
) {
307 absolute_enabled
= 0;
308 } else if (guest_cursor
) {
309 SDL_GetMouseState(&dx
, &dy
);
316 kbd_mouse_event(dx
, dy
, dz
, buttons
);
319 static void toggle_full_screen(DisplayState
*ds
)
321 gui_fullscreen
= !gui_fullscreen
;
322 sdl_resize(ds
, screen
->w
, screen
->h
);
323 if (gui_fullscreen
) {
324 gui_saved_grab
= gui_grab
;
334 static void sdl_refresh(DisplayState
*ds
)
336 SDL_Event ev1
, *ev
= &ev1
;
339 if (last_vm_running
!= vm_running
) {
340 last_vm_running
= vm_running
;
341 sdl_update_caption();
346 while (SDL_PollEvent(ev
)) {
348 case SDL_VIDEOEXPOSE
:
349 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
353 if (ev
->type
== SDL_KEYDOWN
) {
354 mod_state
= (SDL_GetModState() & gui_grab_code
) ==
356 gui_key_modifier_pressed
= mod_state
;
357 if (gui_key_modifier_pressed
) {
359 keycode
= sdl_keyevent_to_keycode(&ev
->key
);
361 case 0x21: /* 'f' key on US keyboard */
362 toggle_full_screen(ds
);
365 case 0x02 ... 0x0a: /* '1' to '9' keys */
366 /* Reset the modifiers sent to the current console */
368 console_select(keycode
- 0x02);
369 if (!is_graphic_console()) {
370 /* display grab if going to a text console */
379 } else if (!is_graphic_console()) {
382 if (ev
->key
.keysym
.mod
& (KMOD_LCTRL
| KMOD_RCTRL
)) {
383 switch(ev
->key
.keysym
.sym
) {
384 case SDLK_UP
: keysym
= QEMU_KEY_CTRL_UP
; break;
385 case SDLK_DOWN
: keysym
= QEMU_KEY_CTRL_DOWN
; break;
386 case SDLK_LEFT
: keysym
= QEMU_KEY_CTRL_LEFT
; break;
387 case SDLK_RIGHT
: keysym
= QEMU_KEY_CTRL_RIGHT
; break;
388 case SDLK_HOME
: keysym
= QEMU_KEY_CTRL_HOME
; break;
389 case SDLK_END
: keysym
= QEMU_KEY_CTRL_END
; break;
390 case SDLK_PAGEUP
: keysym
= QEMU_KEY_CTRL_PAGEUP
; break;
391 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_CTRL_PAGEDOWN
; break;
395 switch(ev
->key
.keysym
.sym
) {
396 case SDLK_UP
: keysym
= QEMU_KEY_UP
; break;
397 case SDLK_DOWN
: keysym
= QEMU_KEY_DOWN
; break;
398 case SDLK_LEFT
: keysym
= QEMU_KEY_LEFT
; break;
399 case SDLK_RIGHT
: keysym
= QEMU_KEY_RIGHT
; break;
400 case SDLK_HOME
: keysym
= QEMU_KEY_HOME
; break;
401 case SDLK_END
: keysym
= QEMU_KEY_END
; break;
402 case SDLK_PAGEUP
: keysym
= QEMU_KEY_PAGEUP
; break;
403 case SDLK_PAGEDOWN
: keysym
= QEMU_KEY_PAGEDOWN
; break;
404 case SDLK_BACKSPACE
: keysym
= QEMU_KEY_BACKSPACE
; break; case SDLK_DELETE
: keysym
= QEMU_KEY_DELETE
; break;
409 kbd_put_keysym(keysym
);
410 } else if (ev
->key
.keysym
.unicode
!= 0) {
411 kbd_put_keysym(ev
->key
.keysym
.unicode
);
414 } else if (ev
->type
== SDL_KEYUP
) {
415 mod_state
= (ev
->key
.keysym
.mod
& gui_grab_code
);
417 if (gui_key_modifier_pressed
) {
418 gui_key_modifier_pressed
= 0;
419 if (gui_keysym
== 0) {
420 /* exit/enter grab if pressing Ctrl-Alt */
422 /* if the application is not active,
423 do not try to enter grab state. It
425 'SDL_WM_GrabInput(SDL_GRAB_ON)'
426 from blocking all the application
428 if (SDL_GetAppState() & SDL_APPACTIVE
)
433 /* SDL does not send back all the
434 modifiers key, so we must correct it */
442 if (is_graphic_console() && !gui_keysym
)
443 sdl_process_key(&ev
->key
);
447 qemu_system_shutdown_request();
450 case SDL_MOUSEMOTION
:
451 if (gui_grab
|| kbd_mouse_is_absolute() ||
453 sdl_send_mouse_event(0);
456 case SDL_MOUSEBUTTONDOWN
:
457 case SDL_MOUSEBUTTONUP
:
459 SDL_MouseButtonEvent
*bev
= &ev
->button
;
460 if (!gui_grab
&& !kbd_mouse_is_absolute()) {
461 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
462 (bev
->state
& SDL_BUTTON_LMASK
)) {
463 /* start grabbing all events */
469 #ifdef SDL_BUTTON_WHEELUP
470 if (bev
->button
== SDL_BUTTON_WHEELUP
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
472 } else if (bev
->button
== SDL_BUTTON_WHEELDOWN
&& ev
->type
== SDL_MOUSEBUTTONDOWN
) {
476 sdl_send_mouse_event(dz
);
480 case SDL_ACTIVEEVENT
:
481 if (gui_grab
&& ev
->active
.state
== SDL_APPINPUTFOCUS
&&
482 !ev
->active
.gain
&& !gui_fullscreen_initial_grab
) {
492 static void sdl_fill(DisplayState
*ds
, int x
, int y
, int w
, int h
, uint32_t c
)
494 SDL_Rect dst
= { x
, y
, w
, h
};
495 SDL_FillRect(screen
, &dst
, c
);
498 static void sdl_mouse_warp(int x
, int y
, int on
)
503 if (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
) {
504 SDL_SetCursor(guest_sprite
);
510 guest_x
= x
, guest_y
= y
;
513 static void sdl_mouse_define(int width
, int height
, int bpp
,
514 int hot_x
, int hot_y
,
515 uint8_t *image
, uint8_t *mask
)
517 uint8_t sprite
[256], *line
;
518 int x
, y
, dst
, bypl
, src
= 0;
520 SDL_FreeCursor(guest_sprite
);
522 memset(sprite
, 0, 256);
523 bypl
= ((width
* bpp
+ 31) >> 5) << 2;
524 for (y
= 0, dst
= 0; y
< height
; y
++, image
+= bypl
) {
526 for (x
= 0; x
< width
; x
++, dst
++) {
529 src
= *(line
++); src
|= *(line
++); src
|= *(line
++);
533 src
= *(line
++); src
|= *(line
++);
539 src
= 0xf & (line
[x
>> 1] >> ((x
& 1)) << 2);
542 src
= 3 & (line
[x
>> 2] >> ((x
& 3)) << 1);
545 src
= 1 & (line
[x
>> 3] >> (x
& 7));
549 sprite
[dst
>> 3] |= (1 << (~dst
& 7)) & mask
[dst
>> 3];
552 guest_sprite
= SDL_CreateCursor(sprite
, mask
, width
, height
, hot_x
, hot_y
);
555 (gui_grab
|| kbd_mouse_is_absolute() || absolute_enabled
))
556 SDL_SetCursor(guest_sprite
);
559 static void sdl_cleanup(void)
562 SDL_FreeCursor(guest_sprite
);
566 void sdl_display_init(DisplayState
*ds
, int full_screen
, int no_frame
)
571 #if defined(__APPLE__)
572 /* always use generic keymaps */
573 if (!keyboard_layout
)
574 keyboard_layout
= "en-us";
576 if(keyboard_layout
) {
577 kbd_layout
= init_keyboard_layout(keyboard_layout
);
585 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
586 if (SDL_Init (flags
)) {
587 fprintf(stderr
, "Could not initialize SDL - exiting\n");
591 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
592 signal(SIGINT
, SIG_DFL
);
593 signal(SIGQUIT
, SIG_DFL
);
596 ds
->dpy_update
= sdl_update
;
597 ds
->dpy_resize
= sdl_resize
;
598 ds
->dpy_refresh
= sdl_refresh
;
599 ds
->dpy_fill
= sdl_fill
;
600 ds
->mouse_set
= sdl_mouse_warp
;
601 ds
->cursor_define
= sdl_mouse_define
;
603 sdl_resize(ds
, 640, 400);
604 sdl_update_caption();
605 SDL_EnableKeyRepeat(250, 50);
606 SDL_EnableUNICODE(1);
609 sdl_cursor_hidden
= SDL_CreateCursor(&data
, &data
, 8, 1, 0, 0);
610 sdl_cursor_normal
= SDL_GetCursor();
615 gui_fullscreen_initial_grab
= 1;