root / sdl.c @ d1d9f421
History | View | Annotate | Download (14.3 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 |
#if defined(__APPLE__)
|
33 |
#define CONFIG_SDL_GENERIC_KBD
|
34 |
#endif
|
35 |
|
36 |
static SDL_Surface *screen;
|
37 |
static int gui_grab; /* if true, all keyboard/mouse events are grabbed */ |
38 |
static int last_vm_running; |
39 |
static int gui_saved_grab; |
40 |
static int gui_fullscreen; |
41 |
static int gui_key_modifier_pressed; |
42 |
static int gui_keysym; |
43 |
|
44 |
static void sdl_update(DisplayState *ds, int x, int y, int w, int h) |
45 |
{ |
46 |
// printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
|
47 |
SDL_UpdateRect(screen, x, y, w, h); |
48 |
} |
49 |
|
50 |
static void sdl_resize(DisplayState *ds, int w, int h) |
51 |
{ |
52 |
int flags;
|
53 |
|
54 |
// printf("resizing to %d %d\n", w, h);
|
55 |
|
56 |
flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL; |
57 |
flags |= SDL_RESIZABLE; |
58 |
if (gui_fullscreen)
|
59 |
flags |= SDL_FULLSCREEN; |
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 |
ds->data = screen->pixels; |
66 |
ds->linesize = screen->pitch; |
67 |
ds->depth = screen->format->BitsPerPixel; |
68 |
} |
69 |
|
70 |
#ifdef CONFIG_SDL_GENERIC_KBD
|
71 |
|
72 |
/* XXX: use keymap tables defined in the VNC patch because the
|
73 |
following code suppose you have a US keyboard. */
|
74 |
|
75 |
static const uint8_t scancodes[SDLK_LAST] = { |
76 |
[SDLK_ESCAPE] = 0x01,
|
77 |
[SDLK_1] = 0x02,
|
78 |
[SDLK_2] = 0x03,
|
79 |
[SDLK_3] = 0x04,
|
80 |
[SDLK_4] = 0x05,
|
81 |
[SDLK_5] = 0x06,
|
82 |
[SDLK_6] = 0x07,
|
83 |
[SDLK_7] = 0x08,
|
84 |
[SDLK_8] = 0x09,
|
85 |
[SDLK_9] = 0x0a,
|
86 |
[SDLK_0] = 0x0b,
|
87 |
[SDLK_MINUS] = 0x0c,
|
88 |
[SDLK_EQUALS] = 0x0d,
|
89 |
[SDLK_BACKSPACE] = 0x0e,
|
90 |
[SDLK_TAB] = 0x0f,
|
91 |
[SDLK_q] = 0x10,
|
92 |
[SDLK_w] = 0x11,
|
93 |
[SDLK_e] = 0x12,
|
94 |
[SDLK_r] = 0x13,
|
95 |
[SDLK_t] = 0x14,
|
96 |
[SDLK_y] = 0x15,
|
97 |
[SDLK_u] = 0x16,
|
98 |
[SDLK_i] = 0x17,
|
99 |
[SDLK_o] = 0x18,
|
100 |
[SDLK_p] = 0x19,
|
101 |
[SDLK_LEFTBRACKET] = 0x1a,
|
102 |
[SDLK_RIGHTBRACKET] = 0x1b,
|
103 |
[SDLK_RETURN] = 0x1c,
|
104 |
[SDLK_LCTRL] = 0x1d,
|
105 |
[SDLK_a] = 0x1e,
|
106 |
[SDLK_s] = 0x1f,
|
107 |
[SDLK_d] = 0x20,
|
108 |
[SDLK_f] = 0x21,
|
109 |
[SDLK_g] = 0x22,
|
110 |
[SDLK_h] = 0x23,
|
111 |
[SDLK_j] = 0x24,
|
112 |
[SDLK_k] = 0x25,
|
113 |
[SDLK_l] = 0x26,
|
114 |
[SDLK_SEMICOLON] = 0x27,
|
115 |
[SDLK_QUOTE] = 0x28,
|
116 |
[SDLK_BACKQUOTE] = 0x29,
|
117 |
[SDLK_LSHIFT] = 0x2a,
|
118 |
[SDLK_BACKSLASH] = 0x2b,
|
119 |
[SDLK_z] = 0x2c,
|
120 |
[SDLK_x] = 0x2d,
|
121 |
[SDLK_c] = 0x2e,
|
122 |
[SDLK_v] = 0x2f,
|
123 |
[SDLK_b] = 0x30,
|
124 |
[SDLK_n] = 0x31,
|
125 |
[SDLK_m] = 0x32,
|
126 |
[SDLK_COMMA] = 0x33,
|
127 |
[SDLK_PERIOD] = 0x34,
|
128 |
[SDLK_SLASH] = 0x35,
|
129 |
[SDLK_KP_MULTIPLY] = 0x37,
|
130 |
[SDLK_LALT] = 0x38,
|
131 |
[SDLK_SPACE] = 0x39,
|
132 |
[SDLK_CAPSLOCK] = 0x3a,
|
133 |
[SDLK_F1] = 0x3b,
|
134 |
[SDLK_F2] = 0x3c,
|
135 |
[SDLK_F3] = 0x3d,
|
136 |
[SDLK_F4] = 0x3e,
|
137 |
[SDLK_F5] = 0x3f,
|
138 |
[SDLK_F6] = 0x40,
|
139 |
[SDLK_F7] = 0x41,
|
140 |
[SDLK_F8] = 0x42,
|
141 |
[SDLK_F9] = 0x43,
|
142 |
[SDLK_F10] = 0x44,
|
143 |
[SDLK_NUMLOCK] = 0x45,
|
144 |
[SDLK_SCROLLOCK] = 0x46,
|
145 |
[SDLK_KP7] = 0x47,
|
146 |
[SDLK_KP8] = 0x48,
|
147 |
[SDLK_KP9] = 0x49,
|
148 |
[SDLK_KP_MINUS] = 0x4a,
|
149 |
[SDLK_KP4] = 0x4b,
|
150 |
[SDLK_KP5] = 0x4c,
|
151 |
[SDLK_KP6] = 0x4d,
|
152 |
[SDLK_KP_PLUS] = 0x4e,
|
153 |
[SDLK_KP1] = 0x4f,
|
154 |
[SDLK_KP2] = 0x50,
|
155 |
[SDLK_KP3] = 0x51,
|
156 |
[SDLK_KP0] = 0x52,
|
157 |
[SDLK_KP_PERIOD] = 0x53,
|
158 |
[SDLK_PRINT] = 0x54,
|
159 |
[SDLK_LMETA] = 0x56,
|
160 |
|
161 |
[SDLK_KP_ENTER] = 0x9c,
|
162 |
[SDLK_KP_DIVIDE] = 0xb5,
|
163 |
|
164 |
[SDLK_UP] = 0xc8,
|
165 |
[SDLK_DOWN] = 0xd0,
|
166 |
[SDLK_RIGHT] = 0xcd,
|
167 |
[SDLK_LEFT] = 0xcb,
|
168 |
[SDLK_INSERT] = 0xd2,
|
169 |
[SDLK_HOME] = 0xc7,
|
170 |
[SDLK_END] = 0xcf,
|
171 |
[SDLK_PAGEUP] = 0xc9,
|
172 |
[SDLK_PAGEDOWN] = 0xd1,
|
173 |
[SDLK_DELETE] = 0xd3,
|
174 |
}; |
175 |
|
176 |
static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
177 |
{ |
178 |
return scancodes[ev->keysym.sym];
|
179 |
} |
180 |
|
181 |
#elif defined(_WIN32)
|
182 |
|
183 |
static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
184 |
{ |
185 |
return ev->keysym.scancode;
|
186 |
} |
187 |
|
188 |
#else
|
189 |
|
190 |
static const uint8_t x_keycode_to_pc_keycode[61] = { |
191 |
0xc7, /* 97 Home */ |
192 |
0xc8, /* 98 Up */ |
193 |
0xc9, /* 99 PgUp */ |
194 |
0xcb, /* 100 Left */ |
195 |
0x4c, /* 101 KP-5 */ |
196 |
0xcd, /* 102 Right */ |
197 |
0xcf, /* 103 End */ |
198 |
0xd0, /* 104 Down */ |
199 |
0xd1, /* 105 PgDn */ |
200 |
0xd2, /* 106 Ins */ |
201 |
0xd3, /* 107 Del */ |
202 |
0x9c, /* 108 Enter */ |
203 |
0x9d, /* 109 Ctrl-R */ |
204 |
0x0, /* 110 Pause */ |
205 |
0xb7, /* 111 Print */ |
206 |
0xb5, /* 112 Divide */ |
207 |
0xb8, /* 113 Alt-R */ |
208 |
0xc6, /* 114 Break */ |
209 |
0x0, /* 115 */ |
210 |
0x0, /* 116 */ |
211 |
0x0, /* 117 */ |
212 |
0x0, /* 118 */ |
213 |
0x0, /* 119 */ |
214 |
0x70, /* 120 Hiragana_Katakana */ |
215 |
0x0, /* 121 */ |
216 |
0x0, /* 122 */ |
217 |
0x73, /* 123 backslash */ |
218 |
0x0, /* 124 */ |
219 |
0x0, /* 125 */ |
220 |
0x0, /* 126 */ |
221 |
0x0, /* 127 */ |
222 |
0x0, /* 128 */ |
223 |
0x79, /* 129 Henkan */ |
224 |
0x0, /* 130 */ |
225 |
0x7b, /* 131 Muhenkan */ |
226 |
0x0, /* 132 */ |
227 |
0x7d, /* 133 Yen */ |
228 |
0x0, /* 134 */ |
229 |
0x0, /* 135 */ |
230 |
0x47, /* 136 KP_7 */ |
231 |
0x48, /* 137 KP_8 */ |
232 |
0x49, /* 138 KP_9 */ |
233 |
0x4b, /* 139 KP_4 */ |
234 |
0x4c, /* 140 KP_5 */ |
235 |
0x4d, /* 141 KP_6 */ |
236 |
0x4f, /* 142 KP_1 */ |
237 |
0x50, /* 143 KP_2 */ |
238 |
0x51, /* 144 KP_3 */ |
239 |
0x52, /* 145 KP_0 */ |
240 |
0x53, /* 146 KP_. */ |
241 |
0x47, /* 147 KP_HOME */ |
242 |
0x48, /* 148 KP_UP */ |
243 |
0x49, /* 149 KP_PgUp */ |
244 |
0x4b, /* 150 KP_Left */ |
245 |
0x4c, /* 151 KP_ */ |
246 |
0x4d, /* 152 KP_Right */ |
247 |
0x4f, /* 153 KP_End */ |
248 |
0x50, /* 154 KP_Down */ |
249 |
0x51, /* 155 KP_PgDn */ |
250 |
0x52, /* 156 KP_Ins */ |
251 |
0x53, /* 157 KP_Del */ |
252 |
}; |
253 |
|
254 |
static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
255 |
{ |
256 |
int keycode;
|
257 |
|
258 |
keycode = ev->keysym.scancode; |
259 |
|
260 |
if (keycode < 9) { |
261 |
keycode = 0;
|
262 |
} else if (keycode < 97) { |
263 |
keycode -= 8; /* just an offset */ |
264 |
} else if (keycode < 158) { |
265 |
/* use conversion table */
|
266 |
keycode = x_keycode_to_pc_keycode[keycode - 97];
|
267 |
} else {
|
268 |
keycode = 0;
|
269 |
} |
270 |
return keycode;
|
271 |
} |
272 |
|
273 |
#endif
|
274 |
|
275 |
static void sdl_process_key(SDL_KeyboardEvent *ev) |
276 |
{ |
277 |
int keycode, v, i;
|
278 |
static uint8_t modifiers_state[256]; |
279 |
|
280 |
if (ev->keysym.sym == SDLK_PAUSE) {
|
281 |
/* specific case */
|
282 |
v = 0;
|
283 |
if (ev->type == SDL_KEYUP)
|
284 |
v |= 0x80;
|
285 |
kbd_put_keycode(0xe1);
|
286 |
kbd_put_keycode(0x1d | v);
|
287 |
kbd_put_keycode(0x45 | v);
|
288 |
return;
|
289 |
} |
290 |
|
291 |
/* XXX: not portable, but avoids complicated mappings */
|
292 |
keycode = sdl_keyevent_to_keycode(ev); |
293 |
|
294 |
switch(keycode) {
|
295 |
case 0x00: |
296 |
/* sent when leaving window: reset the modifiers state */
|
297 |
for(i = 0; i < 256; i++) { |
298 |
if (modifiers_state[i]) {
|
299 |
if (i & 0x80) |
300 |
kbd_put_keycode(0xe0);
|
301 |
kbd_put_keycode(i | 0x80);
|
302 |
} |
303 |
} |
304 |
return;
|
305 |
case 0x2a: /* Left Shift */ |
306 |
case 0x36: /* Right Shift */ |
307 |
case 0x1d: /* Left CTRL */ |
308 |
case 0x9d: /* Right CTRL */ |
309 |
case 0x38: /* Left ALT */ |
310 |
case 0xb8: /* Right ALT */ |
311 |
if (ev->type == SDL_KEYUP)
|
312 |
modifiers_state[keycode] = 0;
|
313 |
else
|
314 |
modifiers_state[keycode] = 1;
|
315 |
break;
|
316 |
case 0x45: /* num lock */ |
317 |
case 0x3a: /* caps lock */ |
318 |
/* SDL does not send the key up event, so we generate it */
|
319 |
kbd_put_keycode(keycode); |
320 |
kbd_put_keycode(keycode | 0x80);
|
321 |
return;
|
322 |
} |
323 |
|
324 |
/* now send the key code */
|
325 |
if (keycode & 0x80) |
326 |
kbd_put_keycode(0xe0);
|
327 |
if (ev->type == SDL_KEYUP)
|
328 |
kbd_put_keycode(keycode | 0x80);
|
329 |
else
|
330 |
kbd_put_keycode(keycode & 0x7f);
|
331 |
} |
332 |
|
333 |
static void sdl_update_caption(void) |
334 |
{ |
335 |
char buf[1024]; |
336 |
strcpy(buf, "QEMU");
|
337 |
if (!vm_running) {
|
338 |
strcat(buf, " [Stopped]");
|
339 |
} |
340 |
if (gui_grab) {
|
341 |
strcat(buf, " - Press Ctrl-Shift to exit grab");
|
342 |
} |
343 |
SDL_WM_SetCaption(buf, "QEMU");
|
344 |
} |
345 |
|
346 |
static void sdl_grab_start(void) |
347 |
{ |
348 |
SDL_ShowCursor(0);
|
349 |
SDL_WM_GrabInput(SDL_GRAB_ON); |
350 |
/* dummy read to avoid moving the mouse */
|
351 |
SDL_GetRelativeMouseState(NULL, NULL); |
352 |
gui_grab = 1;
|
353 |
sdl_update_caption(); |
354 |
} |
355 |
|
356 |
static void sdl_grab_end(void) |
357 |
{ |
358 |
SDL_WM_GrabInput(SDL_GRAB_OFF); |
359 |
SDL_ShowCursor(1);
|
360 |
gui_grab = 0;
|
361 |
sdl_update_caption(); |
362 |
} |
363 |
|
364 |
static void sdl_send_mouse_event(void) |
365 |
{ |
366 |
int dx, dy, dz, state, buttons;
|
367 |
state = SDL_GetRelativeMouseState(&dx, &dy); |
368 |
buttons = 0;
|
369 |
if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
|
370 |
buttons |= MOUSE_EVENT_LBUTTON; |
371 |
if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
|
372 |
buttons |= MOUSE_EVENT_RBUTTON; |
373 |
if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
|
374 |
buttons |= MOUSE_EVENT_MBUTTON; |
375 |
/* XXX: test wheel */
|
376 |
dz = 0;
|
377 |
#ifdef SDL_BUTTON_WHEELUP
|
378 |
if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
|
379 |
dz--; |
380 |
if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
|
381 |
dz++; |
382 |
#endif
|
383 |
kbd_mouse_event(dx, dy, dz, buttons); |
384 |
} |
385 |
|
386 |
static void toggle_full_screen(DisplayState *ds) |
387 |
{ |
388 |
gui_fullscreen = !gui_fullscreen; |
389 |
sdl_resize(ds, screen->w, screen->h); |
390 |
if (gui_fullscreen) {
|
391 |
gui_saved_grab = gui_grab; |
392 |
sdl_grab_start(); |
393 |
} else {
|
394 |
if (!gui_saved_grab)
|
395 |
sdl_grab_end(); |
396 |
} |
397 |
vga_invalidate_display(); |
398 |
vga_update_display(); |
399 |
} |
400 |
|
401 |
static void sdl_refresh(DisplayState *ds) |
402 |
{ |
403 |
SDL_Event ev1, *ev = &ev1; |
404 |
int mod_state;
|
405 |
|
406 |
if (last_vm_running != vm_running) {
|
407 |
last_vm_running = vm_running; |
408 |
sdl_update_caption(); |
409 |
} |
410 |
|
411 |
vga_update_display(); |
412 |
while (SDL_PollEvent(ev)) {
|
413 |
switch (ev->type) {
|
414 |
case SDL_VIDEOEXPOSE:
|
415 |
sdl_update(ds, 0, 0, screen->w, screen->h); |
416 |
break;
|
417 |
case SDL_KEYDOWN:
|
418 |
case SDL_KEYUP:
|
419 |
if (ev->type == SDL_KEYDOWN) {
|
420 |
mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) == |
421 |
(KMOD_LSHIFT | KMOD_LCTRL); |
422 |
gui_key_modifier_pressed = mod_state; |
423 |
if (gui_key_modifier_pressed &&
|
424 |
ev->key.keysym.sym == SDLK_f) { |
425 |
gui_keysym = ev->key.keysym.sym; |
426 |
} |
427 |
} else if (ev->type == SDL_KEYUP) { |
428 |
mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)); |
429 |
if (!mod_state) {
|
430 |
if (gui_key_modifier_pressed) {
|
431 |
switch(gui_keysym) {
|
432 |
case SDLK_f:
|
433 |
toggle_full_screen(ds); |
434 |
break;
|
435 |
case 0: |
436 |
/* exit/enter grab if pressing Ctrl-Shift */
|
437 |
if (!gui_grab)
|
438 |
sdl_grab_start(); |
439 |
else
|
440 |
sdl_grab_end(); |
441 |
break;
|
442 |
} |
443 |
gui_key_modifier_pressed = 0;
|
444 |
gui_keysym = 0;
|
445 |
} |
446 |
} |
447 |
} |
448 |
sdl_process_key(&ev->key); |
449 |
break;
|
450 |
case SDL_QUIT:
|
451 |
qemu_system_shutdown_request(); |
452 |
break;
|
453 |
case SDL_MOUSEMOTION:
|
454 |
if (gui_grab) {
|
455 |
sdl_send_mouse_event(); |
456 |
} |
457 |
break;
|
458 |
case SDL_MOUSEBUTTONDOWN:
|
459 |
case SDL_MOUSEBUTTONUP:
|
460 |
{ |
461 |
SDL_MouseButtonEvent *bev = &ev->button; |
462 |
if (!gui_grab) {
|
463 |
if (ev->type == SDL_MOUSEBUTTONDOWN &&
|
464 |
(bev->state & SDL_BUTTON_LMASK)) { |
465 |
/* start grabbing all events */
|
466 |
sdl_grab_start(); |
467 |
} |
468 |
} else {
|
469 |
sdl_send_mouse_event(); |
470 |
} |
471 |
} |
472 |
break;
|
473 |
case SDL_ACTIVEEVENT:
|
474 |
if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0) { |
475 |
sdl_grab_end(); |
476 |
} |
477 |
break;
|
478 |
default:
|
479 |
break;
|
480 |
} |
481 |
} |
482 |
} |
483 |
|
484 |
static void sdl_cleanup(void) |
485 |
{ |
486 |
SDL_Quit(); |
487 |
} |
488 |
|
489 |
void sdl_display_init(DisplayState *ds)
|
490 |
{ |
491 |
int flags;
|
492 |
|
493 |
flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE; |
494 |
if (SDL_Init (flags)) {
|
495 |
fprintf(stderr, "Could not initialize SDL - exiting\n");
|
496 |
exit(1);
|
497 |
} |
498 |
|
499 |
#ifndef _WIN32
|
500 |
/* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
|
501 |
signal(SIGINT, SIG_DFL); |
502 |
signal(SIGQUIT, SIG_DFL); |
503 |
#endif
|
504 |
|
505 |
ds->dpy_update = sdl_update; |
506 |
ds->dpy_resize = sdl_resize; |
507 |
ds->dpy_refresh = sdl_refresh; |
508 |
|
509 |
sdl_resize(ds, 640, 400); |
510 |
sdl_update_caption(); |
511 |
SDL_EnableKeyRepeat(250, 50); |
512 |
gui_grab = 0;
|
513 |
|
514 |
atexit(sdl_cleanup); |
515 |
} |