root / sdl.c @ 1247c5f7
History | View | Annotate | Download (15.7 kB)
1 |
/*
|
---|---|
2 |
* QEMU SDL display driver
|
3 |
*
|
4 |
* Copyright (c) 2003 Fabrice Bellard
|
5 |
*
|
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:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
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
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
#include "vl.h" |
25 |
|
26 |
#include <SDL.h> |
27 |
|
28 |
#ifndef _WIN32
|
29 |
#include <signal.h> |
30 |
#endif
|
31 |
|
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; |
39 |
static int gui_fullscreen_initial_grab; |
40 |
static int gui_grab_code = KMOD_LALT | KMOD_LCTRL; |
41 |
static uint8_t modifiers_state[256]; |
42 |
|
43 |
static void sdl_update(DisplayState *ds, int x, int y, int w, int h) |
44 |
{ |
45 |
// printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
|
46 |
SDL_UpdateRect(screen, x, y, w, h); |
47 |
} |
48 |
|
49 |
static void sdl_resize(DisplayState *ds, int w, int h) |
50 |
{ |
51 |
int flags;
|
52 |
|
53 |
// printf("resizing to %d %d\n", w, h);
|
54 |
|
55 |
flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL; |
56 |
if (gui_fullscreen)
|
57 |
flags |= SDL_FULLSCREEN; |
58 |
|
59 |
again:
|
60 |
screen = SDL_SetVideoMode(w, h, 0, flags);
|
61 |
if (!screen) {
|
62 |
fprintf(stderr, "Could not open SDL display\n");
|
63 |
exit(1);
|
64 |
} |
65 |
if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
|
66 |
flags &= ~SDL_HWSURFACE; |
67 |
goto again;
|
68 |
} |
69 |
|
70 |
if (!screen->pixels) {
|
71 |
fprintf(stderr, "Could not open SDL display\n");
|
72 |
exit(1);
|
73 |
} |
74 |
ds->data = screen->pixels; |
75 |
ds->linesize = screen->pitch; |
76 |
ds->depth = screen->format->BitsPerPixel; |
77 |
ds->width = w; |
78 |
ds->height = h; |
79 |
} |
80 |
|
81 |
/* generic keyboard conversion */
|
82 |
|
83 |
#include "sdl_keysym.h" |
84 |
#include "keymaps.c" |
85 |
|
86 |
static kbd_layout_t *kbd_layout = NULL; |
87 |
|
88 |
static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev) |
89 |
{ |
90 |
int keysym;
|
91 |
/* workaround for X11+SDL bug with AltGR */
|
92 |
keysym = ev->keysym.sym; |
93 |
if (keysym == 0 && ev->keysym.scancode == 113) |
94 |
keysym = SDLK_MODE; |
95 |
return keysym2scancode(kbd_layout, keysym);
|
96 |
} |
97 |
|
98 |
/* specific keyboard conversions from scan codes */
|
99 |
|
100 |
#if defined(_WIN32)
|
101 |
|
102 |
static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
103 |
{ |
104 |
return ev->keysym.scancode;
|
105 |
} |
106 |
|
107 |
#else
|
108 |
|
109 |
static const uint8_t x_keycode_to_pc_keycode[61] = { |
110 |
0xc7, /* 97 Home */ |
111 |
0xc8, /* 98 Up */ |
112 |
0xc9, /* 99 PgUp */ |
113 |
0xcb, /* 100 Left */ |
114 |
0x4c, /* 101 KP-5 */ |
115 |
0xcd, /* 102 Right */ |
116 |
0xcf, /* 103 End */ |
117 |
0xd0, /* 104 Down */ |
118 |
0xd1, /* 105 PgDn */ |
119 |
0xd2, /* 106 Ins */ |
120 |
0xd3, /* 107 Del */ |
121 |
0x9c, /* 108 Enter */ |
122 |
0x9d, /* 109 Ctrl-R */ |
123 |
0x0, /* 110 Pause */ |
124 |
0xb7, /* 111 Print */ |
125 |
0xb5, /* 112 Divide */ |
126 |
0xb8, /* 113 Alt-R */ |
127 |
0xc6, /* 114 Break */ |
128 |
0x0, /* 115 */ |
129 |
0x0, /* 116 */ |
130 |
0x0, /* 117 */ |
131 |
0x0, /* 118 */ |
132 |
0x0, /* 119 */ |
133 |
0x70, /* 120 Hiragana_Katakana */ |
134 |
0x0, /* 121 */ |
135 |
0x0, /* 122 */ |
136 |
0x73, /* 123 backslash */ |
137 |
0x0, /* 124 */ |
138 |
0x0, /* 125 */ |
139 |
0x0, /* 126 */ |
140 |
0x0, /* 127 */ |
141 |
0x0, /* 128 */ |
142 |
0x79, /* 129 Henkan */ |
143 |
0x0, /* 130 */ |
144 |
0x7b, /* 131 Muhenkan */ |
145 |
0x0, /* 132 */ |
146 |
0x7d, /* 133 Yen */ |
147 |
0x0, /* 134 */ |
148 |
0x0, /* 135 */ |
149 |
0x47, /* 136 KP_7 */ |
150 |
0x48, /* 137 KP_8 */ |
151 |
0x49, /* 138 KP_9 */ |
152 |
0x4b, /* 139 KP_4 */ |
153 |
0x4c, /* 140 KP_5 */ |
154 |
0x4d, /* 141 KP_6 */ |
155 |
0x4f, /* 142 KP_1 */ |
156 |
0x50, /* 143 KP_2 */ |
157 |
0x51, /* 144 KP_3 */ |
158 |
0x52, /* 145 KP_0 */ |
159 |
0x53, /* 146 KP_. */ |
160 |
0x47, /* 147 KP_HOME */ |
161 |
0x48, /* 148 KP_UP */ |
162 |
0x49, /* 149 KP_PgUp */ |
163 |
0x4b, /* 150 KP_Left */ |
164 |
0x4c, /* 151 KP_ */ |
165 |
0x4d, /* 152 KP_Right */ |
166 |
0x4f, /* 153 KP_End */ |
167 |
0x50, /* 154 KP_Down */ |
168 |
0x51, /* 155 KP_PgDn */ |
169 |
0x52, /* 156 KP_Ins */ |
170 |
0x53, /* 157 KP_Del */ |
171 |
}; |
172 |
|
173 |
static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
174 |
{ |
175 |
int keycode;
|
176 |
|
177 |
keycode = ev->keysym.scancode; |
178 |
|
179 |
if (keycode < 9) { |
180 |
keycode = 0;
|
181 |
} else if (keycode < 97) { |
182 |
keycode -= 8; /* just an offset */ |
183 |
} else if (keycode < 158) { |
184 |
/* use conversion table */
|
185 |
keycode = x_keycode_to_pc_keycode[keycode - 97];
|
186 |
} else {
|
187 |
keycode = 0;
|
188 |
} |
189 |
return keycode;
|
190 |
} |
191 |
|
192 |
#endif
|
193 |
|
194 |
static void reset_keys(void) |
195 |
{ |
196 |
int i;
|
197 |
for(i = 0; i < 256; i++) { |
198 |
if (modifiers_state[i]) {
|
199 |
if (i & 0x80) |
200 |
kbd_put_keycode(0xe0);
|
201 |
kbd_put_keycode(i | 0x80);
|
202 |
modifiers_state[i] = 0;
|
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
static void sdl_process_key(SDL_KeyboardEvent *ev) |
208 |
{ |
209 |
int keycode, v;
|
210 |
|
211 |
if (ev->keysym.sym == SDLK_PAUSE) {
|
212 |
/* specific case */
|
213 |
v = 0;
|
214 |
if (ev->type == SDL_KEYUP)
|
215 |
v |= 0x80;
|
216 |
kbd_put_keycode(0xe1);
|
217 |
kbd_put_keycode(0x1d | v);
|
218 |
kbd_put_keycode(0x45 | v);
|
219 |
return;
|
220 |
} |
221 |
|
222 |
if (kbd_layout) {
|
223 |
keycode = sdl_keyevent_to_keycode_generic(ev); |
224 |
} else {
|
225 |
keycode = sdl_keyevent_to_keycode(ev); |
226 |
} |
227 |
|
228 |
switch(keycode) {
|
229 |
case 0x00: |
230 |
/* sent when leaving window: reset the modifiers state */
|
231 |
reset_keys(); |
232 |
return;
|
233 |
case 0x2a: /* Left Shift */ |
234 |
case 0x36: /* Right Shift */ |
235 |
case 0x1d: /* Left CTRL */ |
236 |
case 0x9d: /* Right CTRL */ |
237 |
case 0x38: /* Left ALT */ |
238 |
case 0xb8: /* Right ALT */ |
239 |
if (ev->type == SDL_KEYUP)
|
240 |
modifiers_state[keycode] = 0;
|
241 |
else
|
242 |
modifiers_state[keycode] = 1;
|
243 |
break;
|
244 |
case 0x45: /* num lock */ |
245 |
case 0x3a: /* caps lock */ |
246 |
/* SDL does not send the key up event, so we generate it */
|
247 |
kbd_put_keycode(keycode); |
248 |
kbd_put_keycode(keycode | 0x80);
|
249 |
return;
|
250 |
} |
251 |
|
252 |
/* now send the key code */
|
253 |
if (keycode & 0x80) |
254 |
kbd_put_keycode(0xe0);
|
255 |
if (ev->type == SDL_KEYUP)
|
256 |
kbd_put_keycode(keycode | 0x80);
|
257 |
else
|
258 |
kbd_put_keycode(keycode & 0x7f);
|
259 |
} |
260 |
|
261 |
static void sdl_update_caption(void) |
262 |
{ |
263 |
char buf[1024]; |
264 |
strcpy(buf, "QEMU");
|
265 |
if (!vm_running) {
|
266 |
strcat(buf, " [Stopped]");
|
267 |
} |
268 |
if (gui_grab) {
|
269 |
strcat(buf, " - Press Ctrl-Alt to exit grab");
|
270 |
} |
271 |
SDL_WM_SetCaption(buf, "QEMU");
|
272 |
} |
273 |
|
274 |
static void sdl_grab_start(void) |
275 |
{ |
276 |
SDL_ShowCursor(0);
|
277 |
SDL_WM_GrabInput(SDL_GRAB_ON); |
278 |
/* dummy read to avoid moving the mouse */
|
279 |
SDL_GetRelativeMouseState(NULL, NULL); |
280 |
gui_grab = 1;
|
281 |
sdl_update_caption(); |
282 |
} |
283 |
|
284 |
static void sdl_grab_end(void) |
285 |
{ |
286 |
SDL_WM_GrabInput(SDL_GRAB_OFF); |
287 |
SDL_ShowCursor(1);
|
288 |
gui_grab = 0;
|
289 |
sdl_update_caption(); |
290 |
} |
291 |
|
292 |
static void sdl_send_mouse_event(int dz) |
293 |
{ |
294 |
int dx, dy, state, buttons;
|
295 |
state = SDL_GetRelativeMouseState(&dx, &dy); |
296 |
buttons = 0;
|
297 |
if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
|
298 |
buttons |= MOUSE_EVENT_LBUTTON; |
299 |
if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
|
300 |
buttons |= MOUSE_EVENT_RBUTTON; |
301 |
if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
|
302 |
buttons |= MOUSE_EVENT_MBUTTON; |
303 |
kbd_mouse_event(dx, dy, dz, buttons); |
304 |
} |
305 |
|
306 |
static void toggle_full_screen(DisplayState *ds) |
307 |
{ |
308 |
gui_fullscreen = !gui_fullscreen; |
309 |
sdl_resize(ds, screen->w, screen->h); |
310 |
if (gui_fullscreen) {
|
311 |
gui_saved_grab = gui_grab; |
312 |
sdl_grab_start(); |
313 |
} else {
|
314 |
if (!gui_saved_grab)
|
315 |
sdl_grab_end(); |
316 |
} |
317 |
vga_invalidate_display(); |
318 |
vga_update_display(); |
319 |
} |
320 |
|
321 |
static void sdl_refresh(DisplayState *ds) |
322 |
{ |
323 |
SDL_Event ev1, *ev = &ev1; |
324 |
int mod_state;
|
325 |
|
326 |
if (last_vm_running != vm_running) {
|
327 |
last_vm_running = vm_running; |
328 |
sdl_update_caption(); |
329 |
} |
330 |
|
331 |
if (is_active_console(vga_console))
|
332 |
vga_update_display(); |
333 |
|
334 |
while (SDL_PollEvent(ev)) {
|
335 |
switch (ev->type) {
|
336 |
case SDL_VIDEOEXPOSE:
|
337 |
sdl_update(ds, 0, 0, screen->w, screen->h); |
338 |
break;
|
339 |
case SDL_KEYDOWN:
|
340 |
case SDL_KEYUP:
|
341 |
if (ev->type == SDL_KEYDOWN) {
|
342 |
mod_state = (SDL_GetModState() & gui_grab_code) == |
343 |
gui_grab_code; |
344 |
gui_key_modifier_pressed = mod_state; |
345 |
if (gui_key_modifier_pressed) {
|
346 |
int keycode;
|
347 |
keycode = sdl_keyevent_to_keycode(&ev->key); |
348 |
switch(keycode) {
|
349 |
case 0x21: /* 'f' key on US keyboard */ |
350 |
toggle_full_screen(ds); |
351 |
gui_keysym = 1;
|
352 |
break;
|
353 |
case 0x02 ... 0x0a: /* '1' to '9' keys */ |
354 |
console_select(keycode - 0x02);
|
355 |
if (is_active_console(vga_console)) {
|
356 |
/* tell the vga console to redisplay itself */
|
357 |
vga_invalidate_display(); |
358 |
} else {
|
359 |
/* display grab if going to a text console */
|
360 |
if (gui_grab)
|
361 |
sdl_grab_end(); |
362 |
} |
363 |
gui_keysym = 1;
|
364 |
break;
|
365 |
default:
|
366 |
break;
|
367 |
} |
368 |
} else if (!is_active_console(vga_console)) { |
369 |
int keysym;
|
370 |
keysym = 0;
|
371 |
if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
|
372 |
switch(ev->key.keysym.sym) {
|
373 |
case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break; |
374 |
case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break; |
375 |
case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break; |
376 |
case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break; |
377 |
case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break; |
378 |
case SDLK_END: keysym = QEMU_KEY_CTRL_END; break; |
379 |
case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break; |
380 |
case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break; |
381 |
default: break; |
382 |
} |
383 |
} else {
|
384 |
switch(ev->key.keysym.sym) {
|
385 |
case SDLK_UP: keysym = QEMU_KEY_UP; break; |
386 |
case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break; |
387 |
case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break; |
388 |
case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break; |
389 |
case SDLK_HOME: keysym = QEMU_KEY_HOME; break; |
390 |
case SDLK_END: keysym = QEMU_KEY_END; break; |
391 |
case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break; |
392 |
case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break; |
393 |
case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break; |
394 |
default: break; |
395 |
} |
396 |
} |
397 |
if (keysym) {
|
398 |
kbd_put_keysym(keysym); |
399 |
} else if (ev->key.keysym.unicode != 0) { |
400 |
kbd_put_keysym(ev->key.keysym.unicode); |
401 |
} |
402 |
} |
403 |
} else if (ev->type == SDL_KEYUP) { |
404 |
mod_state = (ev->key.keysym.mod & gui_grab_code); |
405 |
if (!mod_state) {
|
406 |
if (gui_key_modifier_pressed) {
|
407 |
if (gui_keysym == 0) { |
408 |
/* exit/enter grab if pressing Ctrl-Alt */
|
409 |
if (!gui_grab)
|
410 |
sdl_grab_start(); |
411 |
else
|
412 |
sdl_grab_end(); |
413 |
/* SDL does not send back all the
|
414 |
modifiers key, so we must correct it */
|
415 |
reset_keys(); |
416 |
break;
|
417 |
} |
418 |
gui_key_modifier_pressed = 0;
|
419 |
gui_keysym = 0;
|
420 |
} |
421 |
} |
422 |
} |
423 |
if (is_active_console(vga_console))
|
424 |
sdl_process_key(&ev->key); |
425 |
break;
|
426 |
case SDL_QUIT:
|
427 |
qemu_system_shutdown_request(); |
428 |
break;
|
429 |
case SDL_MOUSEMOTION:
|
430 |
if (gui_grab) {
|
431 |
sdl_send_mouse_event(0);
|
432 |
} |
433 |
break;
|
434 |
case SDL_MOUSEBUTTONDOWN:
|
435 |
case SDL_MOUSEBUTTONUP:
|
436 |
{ |
437 |
SDL_MouseButtonEvent *bev = &ev->button; |
438 |
if (!gui_grab) {
|
439 |
if (ev->type == SDL_MOUSEBUTTONDOWN &&
|
440 |
(bev->state & SDL_BUTTON_LMASK)) { |
441 |
/* start grabbing all events */
|
442 |
sdl_grab_start(); |
443 |
} |
444 |
} else {
|
445 |
int dz;
|
446 |
dz = 0;
|
447 |
#ifdef SDL_BUTTON_WHEELUP
|
448 |
if (bev->button == SDL_BUTTON_WHEELUP) {
|
449 |
dz = -1;
|
450 |
} else if (bev->button == SDL_BUTTON_WHEELDOWN) { |
451 |
dz = 1;
|
452 |
} |
453 |
#endif
|
454 |
sdl_send_mouse_event(dz); |
455 |
} |
456 |
} |
457 |
break;
|
458 |
case SDL_ACTIVEEVENT:
|
459 |
if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0 && |
460 |
!gui_fullscreen_initial_grab) { |
461 |
sdl_grab_end(); |
462 |
} |
463 |
break;
|
464 |
default:
|
465 |
break;
|
466 |
} |
467 |
} |
468 |
} |
469 |
|
470 |
static void sdl_cleanup(void) |
471 |
{ |
472 |
SDL_Quit(); |
473 |
} |
474 |
|
475 |
void sdl_display_init(DisplayState *ds, int full_screen) |
476 |
{ |
477 |
int flags;
|
478 |
|
479 |
#if defined(__APPLE__)
|
480 |
/* always use generic keymaps */
|
481 |
if (!keyboard_layout)
|
482 |
keyboard_layout = "en-us";
|
483 |
#endif
|
484 |
if(keyboard_layout) {
|
485 |
kbd_layout = init_keyboard_layout(keyboard_layout); |
486 |
if (!kbd_layout)
|
487 |
exit(1);
|
488 |
} |
489 |
|
490 |
flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE; |
491 |
if (SDL_Init (flags)) {
|
492 |
fprintf(stderr, "Could not initialize SDL - exiting\n");
|
493 |
exit(1);
|
494 |
} |
495 |
#ifndef _WIN32
|
496 |
/* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
|
497 |
signal(SIGINT, SIG_DFL); |
498 |
signal(SIGQUIT, SIG_DFL); |
499 |
#endif
|
500 |
|
501 |
ds->dpy_update = sdl_update; |
502 |
ds->dpy_resize = sdl_resize; |
503 |
ds->dpy_refresh = sdl_refresh; |
504 |
|
505 |
sdl_resize(ds, 640, 400); |
506 |
sdl_update_caption(); |
507 |
SDL_EnableKeyRepeat(250, 50); |
508 |
SDL_EnableUNICODE(1);
|
509 |
gui_grab = 0;
|
510 |
|
511 |
atexit(sdl_cleanup); |
512 |
if (full_screen) {
|
513 |
gui_fullscreen = 1;
|
514 |
gui_fullscreen_initial_grab = 1;
|
515 |
sdl_grab_start(); |
516 |
} |
517 |
} |