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