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_key_modifier_pressed
;
38 static int gui_keysym
;
40 static void sdl_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
42 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
43 SDL_UpdateRect(screen
, x
, y
, w
, h
);
46 static void sdl_resize(DisplayState
*ds
, int w
, int h
)
50 // printf("resizing to %d %d\n", w, h);
52 flags
= SDL_HWSURFACE
|SDL_ASYNCBLIT
|SDL_HWACCEL
;
53 flags
|= SDL_RESIZABLE
;
55 flags
|= SDL_FULLSCREEN
;
56 screen
= SDL_SetVideoMode(w
, h
, 0, flags
);
58 fprintf(stderr
, "Could not open SDL display\n");
61 ds
->data
= screen
->pixels
;
62 ds
->linesize
= screen
->pitch
;
63 ds
->depth
= screen
->format
->BitsPerPixel
;
66 static const uint8_t x_keycode_to_pc_keycode
[61] = {
79 0x9d, /* 109 Ctrl-R */
82 0xb5, /* 112 Divide */
90 0x70, /* 120 Hiragana_Katakana */
93 0x73, /* 123 backslash */
99 0x79, /* 129 Henkan */
101 0x7b, /* 131 Muhenkan */
117 0x47, /* 147 KP_HOME */
118 0x48, /* 148 KP_UP */
119 0x49, /* 149 KP_PgUp */
120 0x4b, /* 150 KP_Left */
122 0x4d, /* 152 KP_Right */
123 0x4f, /* 153 KP_End */
124 0x50, /* 154 KP_Down */
125 0x51, /* 155 KP_PgDn */
126 0x52, /* 156 KP_Ins */
127 0x53, /* 157 KP_Del */
130 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
133 static uint8_t modifiers_state
[256];
135 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
138 if (ev
->type
== SDL_KEYUP
)
140 kbd_put_keycode(0xe1);
141 kbd_put_keycode(0x1d | v
);
142 kbd_put_keycode(0x45 | v
);
146 /* XXX: not portable, but avoids complicated mappings */
147 keycode
= ev
->keysym
.scancode
;
149 /* XXX: windows version may not work: 0xe0/0xe1 should be trapped
154 } else if (keycode
< 97) {
155 keycode
-= 8; /* just an offset */
156 } else if (keycode
< 158) {
157 /* use conversion table */
158 keycode
= x_keycode_to_pc_keycode
[keycode
- 97];
166 /* sent when leaving window: reset the modifiers state */
167 for(i
= 0; i
< 256; i
++) {
168 if (modifiers_state
[i
]) {
170 kbd_put_keycode(0xe0);
171 kbd_put_keycode(i
| 0x80);
175 case 0x2a: /* Left Shift */
176 case 0x36: /* Right Shift */
177 case 0x1d: /* Left CTRL */
178 case 0x9d: /* Right CTRL */
179 case 0x38: /* Left ALT */
180 case 0xb8: /* Right ALT */
181 if (ev
->type
== SDL_KEYUP
)
182 modifiers_state
[keycode
] = 0;
184 modifiers_state
[keycode
] = 1;
186 case 0x45: /* num lock */
187 case 0x3a: /* caps lock */
188 /* SDL does not send the key up event, so we generate it */
189 kbd_put_keycode(keycode
);
190 kbd_put_keycode(keycode
| 0x80);
194 /* now send the key code */
196 kbd_put_keycode(0xe0);
197 if (ev
->type
== SDL_KEYUP
)
198 kbd_put_keycode(keycode
| 0x80);
200 kbd_put_keycode(keycode
& 0x7f);
203 static void sdl_update_caption(void)
208 strcat(buf
, " [Stopped]");
211 strcat(buf
, " - Press Ctrl-Shift to exit grab");
213 SDL_WM_SetCaption(buf
, "QEMU");
216 static void sdl_grab_start(void)
219 SDL_WM_GrabInput(SDL_GRAB_ON
);
220 /* dummy read to avoid moving the mouse */
221 SDL_GetRelativeMouseState(NULL
, NULL
);
223 sdl_update_caption();
226 static void sdl_grab_end(void)
228 SDL_WM_GrabInput(SDL_GRAB_OFF
);
231 sdl_update_caption();
234 static void sdl_send_mouse_event(void)
236 int dx
, dy
, dz
, state
, buttons
;
237 state
= SDL_GetRelativeMouseState(&dx
, &dy
);
239 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
240 buttons
|= MOUSE_EVENT_LBUTTON
;
241 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
242 buttons
|= MOUSE_EVENT_RBUTTON
;
243 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
244 buttons
|= MOUSE_EVENT_MBUTTON
;
245 /* XXX: test wheel */
247 #ifdef SDL_BUTTON_WHEELUP
248 if (state
& SDL_BUTTON(SDL_BUTTON_WHEELUP
))
250 if (state
& SDL_BUTTON(SDL_BUTTON_WHEELDOWN
))
253 kbd_mouse_event(dx
, dy
, dz
, buttons
);
256 static void toggle_full_screen(DisplayState
*ds
)
258 gui_fullscreen
= !gui_fullscreen
;
259 sdl_resize(ds
, screen
->w
, screen
->h
);
260 if (gui_fullscreen
) {
261 gui_saved_grab
= gui_grab
;
267 vga_update_display();
268 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
271 static void sdl_refresh(DisplayState
*ds
)
273 SDL_Event ev1
, *ev
= &ev1
;
276 if (last_vm_running
!= vm_running
) {
277 last_vm_running
= vm_running
;
278 sdl_update_caption();
281 vga_update_display();
282 while (SDL_PollEvent(ev
)) {
284 case SDL_VIDEOEXPOSE
:
285 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
289 if (ev
->type
== SDL_KEYDOWN
) {
290 mod_state
= (SDL_GetModState() & (KMOD_LSHIFT
| KMOD_LCTRL
)) ==
291 (KMOD_LSHIFT
| KMOD_LCTRL
);
292 gui_key_modifier_pressed
= mod_state
;
293 if (gui_key_modifier_pressed
&&
294 ev
->key
.keysym
.sym
== SDLK_f
) {
295 gui_keysym
= ev
->key
.keysym
.sym
;
297 } else if (ev
->type
== SDL_KEYUP
) {
298 mod_state
= (SDL_GetModState() & (KMOD_LSHIFT
| KMOD_LCTRL
));
300 if (gui_key_modifier_pressed
) {
303 toggle_full_screen(ds
);
306 /* exit/enter grab if pressing Ctrl-Shift */
313 gui_key_modifier_pressed
= 0;
318 sdl_process_key(&ev
->key
);
323 case SDL_MOUSEMOTION
:
325 sdl_send_mouse_event();
328 case SDL_MOUSEBUTTONDOWN
:
329 case SDL_MOUSEBUTTONUP
:
331 SDL_MouseButtonEvent
*bev
= &ev
->button
;
333 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
334 (bev
->state
& SDL_BUTTON_LMASK
)) {
335 /* start grabbing all events */
339 sdl_send_mouse_event();
343 case SDL_ACTIVEEVENT
:
344 if (gui_grab
&& (ev
->active
.gain
& SDL_ACTIVEEVENTMASK
) == 0) {
354 static void sdl_cleanup(void)
359 void sdl_display_init(DisplayState
*ds
)
363 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
364 if (SDL_Init (flags
)) {
365 fprintf(stderr
, "Could not initialize SDL - exiting\n");
370 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
371 signal(SIGINT
, SIG_DFL
);
372 signal(SIGQUIT
, SIG_DFL
);
375 ds
->dpy_update
= sdl_update
;
376 ds
->dpy_resize
= sdl_resize
;
377 ds
->dpy_refresh
= sdl_refresh
;
379 sdl_resize(ds
, 640, 400);
380 sdl_update_caption();
381 SDL_EnableKeyRepeat(250, 50);