]>
git.proxmox.com Git - qemu.git/blob - sdl.c
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 */
81 0xb5, /* 112 Divide */
89 0x70, /* 120 Hiragana_Katakana */
92 0x73, /* 123 backslash */
98 0x79, /* 129 Henkan */
100 0x7b, /* 131 Muhenkan */
116 0x47, /* 147 KP_HOME */
117 0x48, /* 148 KP_UP */
118 0x49, /* 149 KP_PgUp */
119 0x4b, /* 150 KP_Left */
121 0x4d, /* 152 KP_Right */
122 0x4f, /* 153 KP_End */
123 0x50, /* 154 KP_Down */
124 0x51, /* 155 KP_PgDn */
125 0x52, /* 156 KP_Ins */
126 0x53, /* 157 KP_Del */
129 static void sdl_process_key(SDL_KeyboardEvent
*ev
)
132 static uint8_t modifiers_state
[256];
134 if (ev
->keysym
.sym
== SDLK_PAUSE
) {
137 if (ev
->type
== SDL_KEYUP
)
139 kbd_put_keycode(0xe1);
140 kbd_put_keycode(0x1d | v
);
141 kbd_put_keycode(0x45 | v
);
145 /* XXX: not portable, but avoids complicated mappings */
146 keycode
= ev
->keysym
.scancode
;
148 /* XXX: windows version may not work: 0xe0/0xe1 should be trapped
153 } else if (keycode
< 97) {
154 keycode
-= 8; /* just an offset */
155 } else if (keycode
< 158) {
156 /* use conversion table */
157 keycode
= x_keycode_to_pc_keycode
[keycode
- 97];
165 /* sent when leaving window: reset the modifiers state */
166 for(i
= 0; i
< 256; i
++) {
167 if (modifiers_state
[i
]) {
169 kbd_put_keycode(0xe0);
170 kbd_put_keycode(i
| 0x80);
174 case 0x2a: /* Left Shift */
175 case 0x36: /* Right Shift */
176 case 0x1d: /* Left CTRL */
177 case 0x9d: /* Right CTRL */
178 case 0x38: /* Left ALT */
179 case 0xb8: /* Right ALT */
180 if (ev
->type
== SDL_KEYUP
)
181 modifiers_state
[keycode
] = 0;
183 modifiers_state
[keycode
] = 1;
185 case 0x45: /* num lock */
186 case 0x3a: /* caps lock */
187 /* SDL does not send the key up event, so we generate it */
188 kbd_put_keycode(keycode
);
189 kbd_put_keycode(keycode
| 0x80);
193 /* now send the key code */
195 kbd_put_keycode(0xe0);
196 if (ev
->type
== SDL_KEYUP
)
197 kbd_put_keycode(keycode
| 0x80);
199 kbd_put_keycode(keycode
& 0x7f);
202 static void sdl_update_caption(void)
207 strcat(buf
, " [Stopped]");
210 strcat(buf
, " - Press Ctrl-Shift to exit grab");
212 SDL_WM_SetCaption(buf
, "QEMU");
215 static void sdl_grab_start(void)
218 SDL_WM_GrabInput(SDL_GRAB_ON
);
219 /* dummy read to avoid moving the mouse */
220 SDL_GetRelativeMouseState(NULL
, NULL
);
222 sdl_update_caption();
225 static void sdl_grab_end(void)
227 SDL_WM_GrabInput(SDL_GRAB_OFF
);
230 sdl_update_caption();
233 static void sdl_send_mouse_event(void)
235 int dx
, dy
, dz
, state
, buttons
;
236 state
= SDL_GetRelativeMouseState(&dx
, &dy
);
238 if (state
& SDL_BUTTON(SDL_BUTTON_LEFT
))
239 buttons
|= MOUSE_EVENT_LBUTTON
;
240 if (state
& SDL_BUTTON(SDL_BUTTON_RIGHT
))
241 buttons
|= MOUSE_EVENT_RBUTTON
;
242 if (state
& SDL_BUTTON(SDL_BUTTON_MIDDLE
))
243 buttons
|= MOUSE_EVENT_MBUTTON
;
244 /* XXX: test wheel */
246 #ifdef SDL_BUTTON_WHEELUP
247 if (state
& SDL_BUTTON(SDL_BUTTON_WHEELUP
))
249 if (state
& SDL_BUTTON(SDL_BUTTON_WHEELDOWN
))
252 kbd_mouse_event(dx
, dy
, dz
, buttons
);
255 static void toggle_full_screen(DisplayState
*ds
)
257 gui_fullscreen
= !gui_fullscreen
;
258 sdl_resize(ds
, screen
->w
, screen
->h
);
259 if (gui_fullscreen
) {
260 gui_saved_grab
= gui_grab
;
266 vga_update_display();
267 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
270 static void sdl_refresh(DisplayState
*ds
)
272 SDL_Event ev1
, *ev
= &ev1
;
275 if (last_vm_running
!= vm_running
) {
276 last_vm_running
= vm_running
;
277 sdl_update_caption();
280 vga_update_display();
281 while (SDL_PollEvent(ev
)) {
283 case SDL_VIDEOEXPOSE
:
284 sdl_update(ds
, 0, 0, screen
->w
, screen
->h
);
288 if (ev
->type
== SDL_KEYDOWN
) {
289 mod_state
= (SDL_GetModState() & (KMOD_LSHIFT
| KMOD_LCTRL
)) ==
290 (KMOD_LSHIFT
| KMOD_LCTRL
);
291 gui_key_modifier_pressed
= mod_state
;
292 if (gui_key_modifier_pressed
&&
293 ev
->key
.keysym
.sym
== SDLK_f
) {
294 gui_keysym
= ev
->key
.keysym
.sym
;
296 } else if (ev
->type
== SDL_KEYUP
) {
297 mod_state
= (SDL_GetModState() & (KMOD_LSHIFT
| KMOD_LCTRL
));
299 if (gui_key_modifier_pressed
) {
302 toggle_full_screen(ds
);
305 /* exit/enter grab if pressing Ctrl-Shift */
312 gui_key_modifier_pressed
= 0;
317 sdl_process_key(&ev
->key
);
322 case SDL_MOUSEMOTION
:
324 sdl_send_mouse_event();
327 case SDL_MOUSEBUTTONDOWN
:
328 case SDL_MOUSEBUTTONUP
:
330 SDL_MouseButtonEvent
*bev
= &ev
->button
;
332 if (ev
->type
== SDL_MOUSEBUTTONDOWN
&&
333 (bev
->state
& SDL_BUTTON_LMASK
)) {
334 /* start grabbing all events */
338 sdl_send_mouse_event();
342 case SDL_ACTIVEEVENT
:
343 if (gui_grab
&& (ev
->active
.gain
& SDL_ACTIVEEVENTMASK
) == 0) {
353 static void sdl_cleanup(void)
358 void sdl_display_init(DisplayState
*ds
)
362 flags
= SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
;
363 if (SDL_Init (flags
)) {
364 fprintf(stderr
, "Could not initialize SDL - exiting\n");
369 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
370 signal(SIGINT
, SIG_DFL
);
371 signal(SIGQUIT
, SIG_DFL
);
374 ds
->dpy_update
= sdl_update
;
375 ds
->dpy_resize
= sdl_resize
;
376 ds
->dpy_refresh
= sdl_refresh
;
378 sdl_resize(ds
, 640, 400);
379 sdl_update_caption();
380 SDL_EnableKeyRepeat(250, 50);