Make mixer emulation a configure option (Jan Kiszka)
[qemu-kvm/fedora.git] / sdl.c
blobbac60ea4654b9d6a6d712a5847806786251a7d6c
1 /*
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
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "console.h"
26 #include "sysemu.h"
28 #include <SDL.h>
30 #ifndef _WIN32
31 #include <signal.h>
32 #endif
34 static SDL_Surface *screen;
35 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
36 static int last_vm_running;
37 static int gui_saved_grab;
38 static int gui_fullscreen;
39 static int gui_noframe;
40 static int gui_key_modifier_pressed;
41 static int gui_keysym;
42 static int gui_fullscreen_initial_grab;
43 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
44 static uint8_t modifiers_state[256];
45 static int width, height;
46 static SDL_Cursor *sdl_cursor_normal;
47 static SDL_Cursor *sdl_cursor_hidden;
48 static int absolute_enabled = 0;
49 static int guest_cursor = 0;
50 static int guest_x, guest_y;
51 static SDL_Cursor *guest_sprite = 0;
53 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
55 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
56 SDL_UpdateRect(screen, x, y, w, h);
59 static void sdl_resize(DisplayState *ds, int w, int h)
61 int flags;
63 // printf("resizing to %d %d\n", w, h);
65 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
66 if (gui_fullscreen)
67 flags |= SDL_FULLSCREEN;
68 if (gui_noframe)
69 flags |= SDL_NOFRAME;
71 width = w;
72 height = h;
74 again:
75 screen = SDL_SetVideoMode(w, h, 0, flags);
76 if (!screen) {
77 fprintf(stderr, "Could not open SDL display\n");
78 exit(1);
80 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
81 flags &= ~SDL_HWSURFACE;
82 goto again;
85 if (!screen->pixels) {
86 fprintf(stderr, "Could not open SDL display\n");
87 exit(1);
89 ds->data = screen->pixels;
90 ds->linesize = screen->pitch;
91 ds->depth = screen->format->BitsPerPixel;
92 if (screen->format->Bshift > screen->format->Rshift) {
93 ds->bgr = 1;
94 } else {
95 ds->bgr = 0;
97 ds->width = w;
98 ds->height = h;
101 /* generic keyboard conversion */
103 #include "sdl_keysym.h"
104 #include "keymaps.c"
106 static kbd_layout_t *kbd_layout = NULL;
108 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
110 int keysym;
111 /* workaround for X11+SDL bug with AltGR */
112 keysym = ev->keysym.sym;
113 if (keysym == 0 && ev->keysym.scancode == 113)
114 keysym = SDLK_MODE;
115 /* For Japanese key '\' and '|' */
116 if (keysym == 92 && ev->keysym.scancode == 133) {
117 keysym = 0xa5;
119 return keysym2scancode(kbd_layout, keysym);
122 /* specific keyboard conversions from scan codes */
124 #if defined(_WIN32)
126 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
128 return ev->keysym.scancode;
131 #else
133 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
135 int keycode;
137 keycode = ev->keysym.scancode;
139 if (keycode < 9) {
140 keycode = 0;
141 } else if (keycode < 97) {
142 keycode -= 8; /* just an offset */
143 } else if (keycode < 212) {
144 /* use conversion table */
145 keycode = _translate_keycode(keycode - 97);
146 } else {
147 keycode = 0;
149 return keycode;
152 #endif
154 static void reset_keys(void)
156 int i;
157 for(i = 0; i < 256; i++) {
158 if (modifiers_state[i]) {
159 if (i & 0x80)
160 kbd_put_keycode(0xe0);
161 kbd_put_keycode(i | 0x80);
162 modifiers_state[i] = 0;
167 static void sdl_process_key(SDL_KeyboardEvent *ev)
169 int keycode, v;
171 if (ev->keysym.sym == SDLK_PAUSE) {
172 /* specific case */
173 v = 0;
174 if (ev->type == SDL_KEYUP)
175 v |= 0x80;
176 kbd_put_keycode(0xe1);
177 kbd_put_keycode(0x1d | v);
178 kbd_put_keycode(0x45 | v);
179 return;
182 if (kbd_layout) {
183 keycode = sdl_keyevent_to_keycode_generic(ev);
184 } else {
185 keycode = sdl_keyevent_to_keycode(ev);
188 switch(keycode) {
189 case 0x00:
190 /* sent when leaving window: reset the modifiers state */
191 reset_keys();
192 return;
193 case 0x2a: /* Left Shift */
194 case 0x36: /* Right Shift */
195 case 0x1d: /* Left CTRL */
196 case 0x9d: /* Right CTRL */
197 case 0x38: /* Left ALT */
198 case 0xb8: /* Right ALT */
199 if (ev->type == SDL_KEYUP)
200 modifiers_state[keycode] = 0;
201 else
202 modifiers_state[keycode] = 1;
203 break;
204 case 0x45: /* num lock */
205 case 0x3a: /* caps lock */
206 /* SDL does not send the key up event, so we generate it */
207 kbd_put_keycode(keycode);
208 kbd_put_keycode(keycode | 0x80);
209 return;
212 /* now send the key code */
213 if (keycode & 0x80)
214 kbd_put_keycode(0xe0);
215 if (ev->type == SDL_KEYUP)
216 kbd_put_keycode(keycode | 0x80);
217 else
218 kbd_put_keycode(keycode & 0x7f);
221 static void sdl_update_caption(void)
223 char buf[1024];
224 const char *status = "";
226 if (!vm_running)
227 status = " [Stopped]";
228 else if (gui_grab) {
229 if (!alt_grab)
230 status = " - Press Ctrl-Alt to exit grab";
231 else
232 status = " - Press Ctrl-Alt-Shift to exit grab";
235 if (qemu_name)
236 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
237 else
238 snprintf(buf, sizeof(buf), "QEMU%s", status);
240 SDL_WM_SetCaption(buf, "QEMU");
243 static void sdl_hide_cursor(void)
245 if (!cursor_hide)
246 return;
248 if (kbd_mouse_is_absolute()) {
249 SDL_ShowCursor(1);
250 SDL_SetCursor(sdl_cursor_hidden);
251 } else {
252 SDL_ShowCursor(0);
256 static void sdl_show_cursor(void)
258 if (!cursor_hide)
259 return;
261 if (!kbd_mouse_is_absolute()) {
262 SDL_ShowCursor(1);
263 if (guest_cursor &&
264 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
265 SDL_SetCursor(guest_sprite);
266 else
267 SDL_SetCursor(sdl_cursor_normal);
271 static void sdl_grab_start(void)
273 if (guest_cursor) {
274 SDL_SetCursor(guest_sprite);
275 SDL_WarpMouse(guest_x, guest_y);
276 } else
277 sdl_hide_cursor();
278 SDL_WM_GrabInput(SDL_GRAB_ON);
279 gui_grab = 1;
280 sdl_update_caption();
283 static void sdl_grab_end(void)
285 SDL_WM_GrabInput(SDL_GRAB_OFF);
286 gui_grab = 0;
287 sdl_show_cursor();
288 sdl_update_caption();
291 static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
293 int buttons;
294 buttons = 0;
295 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
296 buttons |= MOUSE_EVENT_LBUTTON;
297 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
298 buttons |= MOUSE_EVENT_RBUTTON;
299 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
300 buttons |= MOUSE_EVENT_MBUTTON;
302 if (kbd_mouse_is_absolute()) {
303 if (!absolute_enabled) {
304 sdl_hide_cursor();
305 if (gui_grab) {
306 sdl_grab_end();
308 absolute_enabled = 1;
311 dx = x * 0x7FFF / (width - 1);
312 dy = y * 0x7FFF / (height - 1);
313 } else if (absolute_enabled) {
314 sdl_show_cursor();
315 absolute_enabled = 0;
316 } else if (guest_cursor) {
317 x -= guest_x;
318 y -= guest_y;
319 guest_x += x;
320 guest_y += y;
321 dx = x;
322 dy = y;
325 kbd_mouse_event(dx, dy, dz, buttons);
328 static void toggle_full_screen(DisplayState *ds)
330 gui_fullscreen = !gui_fullscreen;
331 sdl_resize(ds, screen->w, screen->h);
332 if (gui_fullscreen) {
333 gui_saved_grab = gui_grab;
334 sdl_grab_start();
335 } else {
336 if (!gui_saved_grab)
337 sdl_grab_end();
339 vga_hw_invalidate();
340 vga_hw_update();
343 static void sdl_refresh(DisplayState *ds)
345 SDL_Event ev1, *ev = &ev1;
346 int mod_state;
347 int buttonstate = SDL_GetMouseState(NULL, NULL);
349 if (last_vm_running != vm_running) {
350 last_vm_running = vm_running;
351 sdl_update_caption();
354 vga_hw_update();
355 SDL_EnableUNICODE(!is_graphic_console());
357 while (SDL_PollEvent(ev)) {
358 switch (ev->type) {
359 case SDL_VIDEOEXPOSE:
360 sdl_update(ds, 0, 0, screen->w, screen->h);
361 break;
362 case SDL_KEYDOWN:
363 case SDL_KEYUP:
364 if (ev->type == SDL_KEYDOWN) {
365 if (!alt_grab) {
366 mod_state = (SDL_GetModState() & gui_grab_code) ==
367 gui_grab_code;
368 } else {
369 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
370 (gui_grab_code | KMOD_LSHIFT);
372 gui_key_modifier_pressed = mod_state;
373 if (gui_key_modifier_pressed) {
374 int keycode;
375 keycode = sdl_keyevent_to_keycode(&ev->key);
376 switch(keycode) {
377 case 0x21: /* 'f' key on US keyboard */
378 toggle_full_screen(ds);
379 gui_keysym = 1;
380 break;
381 case 0x02 ... 0x0a: /* '1' to '9' keys */
382 /* Reset the modifiers sent to the current console */
383 reset_keys();
384 console_select(keycode - 0x02);
385 if (!is_graphic_console()) {
386 /* display grab if going to a text console */
387 if (gui_grab)
388 sdl_grab_end();
390 gui_keysym = 1;
391 break;
392 default:
393 break;
395 } else if (!is_graphic_console()) {
396 int keysym;
397 keysym = 0;
398 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
399 switch(ev->key.keysym.sym) {
400 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
401 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
402 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
403 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
404 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
405 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
406 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
407 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
408 default: break;
410 } else {
411 switch(ev->key.keysym.sym) {
412 case SDLK_UP: keysym = QEMU_KEY_UP; break;
413 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
414 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
415 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
416 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
417 case SDLK_END: keysym = QEMU_KEY_END; break;
418 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
419 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
420 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
421 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
422 default: break;
425 if (keysym) {
426 kbd_put_keysym(keysym);
427 } else if (ev->key.keysym.unicode != 0) {
428 kbd_put_keysym(ev->key.keysym.unicode);
431 } else if (ev->type == SDL_KEYUP) {
432 if (!alt_grab) {
433 mod_state = (ev->key.keysym.mod & gui_grab_code);
434 } else {
435 mod_state = (ev->key.keysym.mod &
436 (gui_grab_code | KMOD_LSHIFT));
438 if (!mod_state) {
439 if (gui_key_modifier_pressed) {
440 gui_key_modifier_pressed = 0;
441 if (gui_keysym == 0) {
442 /* exit/enter grab if pressing Ctrl-Alt */
443 if (!gui_grab) {
444 /* if the application is not active,
445 do not try to enter grab state. It
446 prevents
447 'SDL_WM_GrabInput(SDL_GRAB_ON)'
448 from blocking all the application
449 (SDL bug). */
450 if (SDL_GetAppState() & SDL_APPACTIVE)
451 sdl_grab_start();
452 } else {
453 sdl_grab_end();
455 /* SDL does not send back all the
456 modifiers key, so we must correct it */
457 reset_keys();
458 break;
460 gui_keysym = 0;
464 if (is_graphic_console() && !gui_keysym)
465 sdl_process_key(&ev->key);
466 break;
467 case SDL_QUIT:
468 if (!no_quit) {
469 qemu_system_shutdown_request();
470 vm_start(); /* In case we're paused */
472 break;
473 case SDL_MOUSEMOTION:
474 if (gui_grab || kbd_mouse_is_absolute() ||
475 absolute_enabled) {
476 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
477 ev->motion.x, ev->motion.y, ev->motion.state);
479 break;
480 case SDL_MOUSEBUTTONDOWN:
481 case SDL_MOUSEBUTTONUP:
483 SDL_MouseButtonEvent *bev = &ev->button;
484 if (!gui_grab && !kbd_mouse_is_absolute()) {
485 if (ev->type == SDL_MOUSEBUTTONDOWN &&
486 (bev->button == SDL_BUTTON_LEFT)) {
487 /* start grabbing all events */
488 sdl_grab_start();
490 } else {
491 int dz;
492 dz = 0;
493 if (ev->type == SDL_MOUSEBUTTONDOWN) {
494 buttonstate |= SDL_BUTTON(bev->button);
495 } else {
496 buttonstate &= ~SDL_BUTTON(bev->button);
498 #ifdef SDL_BUTTON_WHEELUP
499 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
500 dz = -1;
501 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
502 dz = 1;
504 #endif
505 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
508 break;
509 case SDL_ACTIVEEVENT:
510 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
511 !ev->active.gain && !gui_fullscreen_initial_grab) {
512 sdl_grab_end();
514 if (ev->active.state & SDL_APPACTIVE) {
515 if (ev->active.gain) {
516 /* Back to default interval */
517 ds->gui_timer_interval = 0;
518 } else {
519 /* Sleeping interval */
520 ds->gui_timer_interval = 500;
523 break;
524 default:
525 break;
530 static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
532 SDL_Rect dst = { x, y, w, h };
533 SDL_FillRect(screen, &dst, c);
536 static void sdl_mouse_warp(int x, int y, int on)
538 if (on) {
539 if (!guest_cursor)
540 sdl_show_cursor();
541 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
542 SDL_SetCursor(guest_sprite);
543 SDL_WarpMouse(x, y);
545 } else if (gui_grab)
546 sdl_hide_cursor();
547 guest_cursor = on;
548 guest_x = x, guest_y = y;
551 static void sdl_mouse_define(int width, int height, int bpp,
552 int hot_x, int hot_y,
553 uint8_t *image, uint8_t *mask)
555 uint8_t sprite[256], *line;
556 int x, y, dst, bypl, src = 0;
557 if (guest_sprite)
558 SDL_FreeCursor(guest_sprite);
560 memset(sprite, 0, 256);
561 bypl = ((width * bpp + 31) >> 5) << 2;
562 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
563 line = image;
564 for (x = 0; x < width; x ++, dst ++) {
565 switch (bpp) {
566 case 24:
567 src = *(line ++); src |= *(line ++); src |= *(line ++);
568 break;
569 case 16:
570 case 15:
571 src = *(line ++); src |= *(line ++);
572 break;
573 case 8:
574 src = *(line ++);
575 break;
576 case 4:
577 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
578 break;
579 case 2:
580 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
581 break;
582 case 1:
583 src = 1 & (line[x >> 3] >> (x & 7));
584 break;
586 if (!src)
587 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
590 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
592 if (guest_cursor &&
593 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
594 SDL_SetCursor(guest_sprite);
597 static void sdl_cleanup(void)
599 if (guest_sprite)
600 SDL_FreeCursor(guest_sprite);
601 SDL_Quit();
604 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
606 int flags;
607 uint8_t data = 0;
609 #if defined(__APPLE__)
610 /* always use generic keymaps */
611 if (!keyboard_layout)
612 keyboard_layout = "en-us";
613 #endif
614 if(keyboard_layout) {
615 kbd_layout = init_keyboard_layout(keyboard_layout);
616 if (!kbd_layout)
617 exit(1);
620 if (no_frame)
621 gui_noframe = 1;
623 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
624 if (SDL_Init (flags)) {
625 fprintf(stderr, "Could not initialize SDL - exiting\n");
626 exit(1);
628 #ifndef _WIN32
629 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
630 signal(SIGINT, SIG_DFL);
631 signal(SIGQUIT, SIG_DFL);
632 #endif
634 ds->dpy_update = sdl_update;
635 ds->dpy_resize = sdl_resize;
636 ds->dpy_refresh = sdl_refresh;
637 ds->dpy_fill = sdl_fill;
638 ds->mouse_set = sdl_mouse_warp;
639 ds->cursor_define = sdl_mouse_define;
641 sdl_resize(ds, 640, 400);
642 sdl_update_caption();
643 SDL_EnableKeyRepeat(250, 50);
644 gui_grab = 0;
646 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
647 sdl_cursor_normal = SDL_GetCursor();
649 atexit(sdl_cleanup);
650 if (full_screen) {
651 gui_fullscreen = 1;
652 gui_fullscreen_initial_grab = 1;
653 sdl_grab_start();