Coalesced MMIO support (core)
[qemu-kvm/markmc.git] / sdl.c
blob4b7229e50c01d1ef16aabdab8b08ec8d6ef8d6fb
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 (ds->depth == 32 && screen->format->Rshift == 0) {
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], appname[20] = "QEMU";
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 decorate_application_name(appname, sizeof appname);
237 if (qemu_name)
238 snprintf(buf, sizeof(buf), "%s (%s)%s", appname, qemu_name, status);
239 else
240 snprintf(buf, sizeof(buf), "%s%s", appname, status);
242 SDL_WM_SetCaption(buf, appname);
245 static void sdl_hide_cursor(void)
247 if (!cursor_hide)
248 return;
250 if (kbd_mouse_is_absolute()) {
251 SDL_ShowCursor(1);
252 SDL_SetCursor(sdl_cursor_hidden);
253 } else {
254 SDL_ShowCursor(0);
258 static void sdl_show_cursor(void)
260 if (!cursor_hide)
261 return;
263 if (!kbd_mouse_is_absolute()) {
264 SDL_ShowCursor(1);
265 if (guest_cursor &&
266 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
267 SDL_SetCursor(guest_sprite);
268 else
269 SDL_SetCursor(sdl_cursor_normal);
273 static void sdl_grab_start(void)
275 if (guest_cursor) {
276 SDL_SetCursor(guest_sprite);
277 SDL_WarpMouse(guest_x, guest_y);
278 } else
279 sdl_hide_cursor();
280 SDL_WM_GrabInput(SDL_GRAB_ON);
281 gui_grab = 1;
282 sdl_update_caption();
285 static void sdl_grab_end(void)
287 SDL_WM_GrabInput(SDL_GRAB_OFF);
288 gui_grab = 0;
289 sdl_show_cursor();
290 sdl_update_caption();
293 static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
295 int buttons;
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;
304 if (kbd_mouse_is_absolute()) {
305 if (!absolute_enabled) {
306 sdl_hide_cursor();
307 if (gui_grab) {
308 sdl_grab_end();
310 absolute_enabled = 1;
313 dx = x * 0x7FFF / (width - 1);
314 dy = y * 0x7FFF / (height - 1);
315 } else if (absolute_enabled) {
316 sdl_show_cursor();
317 absolute_enabled = 0;
318 } else if (guest_cursor) {
319 x -= guest_x;
320 y -= guest_y;
321 guest_x += x;
322 guest_y += y;
323 dx = x;
324 dy = y;
327 kbd_mouse_event(dx, dy, dz, buttons);
330 static void toggle_full_screen(DisplayState *ds)
332 gui_fullscreen = !gui_fullscreen;
333 sdl_resize(ds, screen->w, screen->h);
334 if (gui_fullscreen) {
335 gui_saved_grab = gui_grab;
336 sdl_grab_start();
337 } else {
338 if (!gui_saved_grab)
339 sdl_grab_end();
341 vga_hw_invalidate();
342 vga_hw_update();
345 static void sdl_refresh(DisplayState *ds)
347 SDL_Event ev1, *ev = &ev1;
348 int mod_state;
349 int buttonstate = SDL_GetMouseState(NULL, NULL);
351 if (last_vm_running != vm_running) {
352 last_vm_running = vm_running;
353 sdl_update_caption();
356 vga_hw_update();
357 SDL_EnableUNICODE(!is_graphic_console());
359 while (SDL_PollEvent(ev)) {
360 switch (ev->type) {
361 case SDL_VIDEOEXPOSE:
362 sdl_update(ds, 0, 0, screen->w, screen->h);
363 break;
364 case SDL_KEYDOWN:
365 case SDL_KEYUP:
366 if (ev->type == SDL_KEYDOWN) {
367 if (!alt_grab) {
368 mod_state = (SDL_GetModState() & gui_grab_code) ==
369 gui_grab_code;
370 } else {
371 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
372 (gui_grab_code | KMOD_LSHIFT);
374 gui_key_modifier_pressed = mod_state;
375 if (gui_key_modifier_pressed) {
376 int keycode;
377 keycode = sdl_keyevent_to_keycode(&ev->key);
378 switch(keycode) {
379 case 0x21: /* 'f' key on US keyboard */
380 toggle_full_screen(ds);
381 gui_keysym = 1;
382 break;
383 case 0x02 ... 0x0a: /* '1' to '9' keys */
384 /* Reset the modifiers sent to the current console */
385 reset_keys();
386 console_select(keycode - 0x02);
387 if (!is_graphic_console()) {
388 /* display grab if going to a text console */
389 if (gui_grab)
390 sdl_grab_end();
392 gui_keysym = 1;
393 break;
394 default:
395 break;
397 } else if (!is_graphic_console()) {
398 int keysym;
399 keysym = 0;
400 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
401 switch(ev->key.keysym.sym) {
402 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
403 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
404 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
405 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
406 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
407 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
408 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
409 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
410 default: break;
412 } else {
413 switch(ev->key.keysym.sym) {
414 case SDLK_UP: keysym = QEMU_KEY_UP; break;
415 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
416 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
417 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
418 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
419 case SDLK_END: keysym = QEMU_KEY_END; break;
420 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
421 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
422 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
423 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
424 default: break;
427 if (keysym) {
428 kbd_put_keysym(keysym);
429 } else if (ev->key.keysym.unicode != 0) {
430 kbd_put_keysym(ev->key.keysym.unicode);
433 } else if (ev->type == SDL_KEYUP) {
434 if (!alt_grab) {
435 mod_state = (ev->key.keysym.mod & gui_grab_code);
436 } else {
437 mod_state = (ev->key.keysym.mod &
438 (gui_grab_code | KMOD_LSHIFT));
440 if (!mod_state) {
441 if (gui_key_modifier_pressed) {
442 gui_key_modifier_pressed = 0;
443 if (gui_keysym == 0) {
444 /* exit/enter grab if pressing Ctrl-Alt */
445 if (!gui_grab) {
446 /* if the application is not active,
447 do not try to enter grab state. It
448 prevents
449 'SDL_WM_GrabInput(SDL_GRAB_ON)'
450 from blocking all the application
451 (SDL bug). */
452 if (SDL_GetAppState() & SDL_APPACTIVE)
453 sdl_grab_start();
454 } else {
455 sdl_grab_end();
457 /* SDL does not send back all the
458 modifiers key, so we must correct it */
459 reset_keys();
460 break;
462 gui_keysym = 0;
466 if (is_graphic_console() && !gui_keysym)
467 sdl_process_key(&ev->key);
468 break;
469 case SDL_QUIT:
470 if (!no_quit) {
471 qemu_system_shutdown_request();
472 vm_start(); /* In case we're paused */
474 break;
475 case SDL_MOUSEMOTION:
476 if (gui_grab || kbd_mouse_is_absolute() ||
477 absolute_enabled) {
478 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
479 ev->motion.x, ev->motion.y, ev->motion.state);
481 break;
482 case SDL_MOUSEBUTTONDOWN:
483 case SDL_MOUSEBUTTONUP:
485 SDL_MouseButtonEvent *bev = &ev->button;
486 if (!gui_grab && !kbd_mouse_is_absolute()) {
487 if (ev->type == SDL_MOUSEBUTTONDOWN &&
488 (bev->button == SDL_BUTTON_LEFT)) {
489 /* start grabbing all events */
490 sdl_grab_start();
492 } else {
493 int dz;
494 dz = 0;
495 if (ev->type == SDL_MOUSEBUTTONDOWN) {
496 buttonstate |= SDL_BUTTON(bev->button);
497 } else {
498 buttonstate &= ~SDL_BUTTON(bev->button);
500 #ifdef SDL_BUTTON_WHEELUP
501 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
502 dz = -1;
503 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
504 dz = 1;
506 #endif
507 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
510 break;
511 case SDL_ACTIVEEVENT:
512 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
513 !ev->active.gain && !gui_fullscreen_initial_grab) {
514 sdl_grab_end();
516 if (ev->active.state & SDL_APPACTIVE) {
517 if (ev->active.gain) {
518 /* Back to default interval */
519 ds->gui_timer_interval = 0;
520 } else {
521 /* Sleeping interval */
522 ds->gui_timer_interval = 500;
525 break;
526 default:
527 break;
532 static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
534 SDL_Rect dst = { x, y, w, h };
535 SDL_FillRect(screen, &dst, c);
538 static void sdl_mouse_warp(int x, int y, int on)
540 if (on) {
541 if (!guest_cursor)
542 sdl_show_cursor();
543 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
544 SDL_SetCursor(guest_sprite);
545 SDL_WarpMouse(x, y);
547 } else if (gui_grab)
548 sdl_hide_cursor();
549 guest_cursor = on;
550 guest_x = x, guest_y = y;
553 static void sdl_mouse_define(int width, int height, int bpp,
554 int hot_x, int hot_y,
555 uint8_t *image, uint8_t *mask)
557 uint8_t sprite[256], *line;
558 int x, y, dst, bypl, src = 0;
559 if (guest_sprite)
560 SDL_FreeCursor(guest_sprite);
562 memset(sprite, 0, 256);
563 bypl = ((width * bpp + 31) >> 5) << 2;
564 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
565 line = image;
566 for (x = 0; x < width; x ++, dst ++) {
567 switch (bpp) {
568 case 24:
569 src = *(line ++); src |= *(line ++); src |= *(line ++);
570 break;
571 case 16:
572 case 15:
573 src = *(line ++); src |= *(line ++);
574 break;
575 case 8:
576 src = *(line ++);
577 break;
578 case 4:
579 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
580 break;
581 case 2:
582 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
583 break;
584 case 1:
585 src = 1 & (line[x >> 3] >> (x & 7));
586 break;
588 if (!src)
589 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
592 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
594 if (guest_cursor &&
595 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
596 SDL_SetCursor(guest_sprite);
599 static void sdl_cleanup(void)
601 if (guest_sprite)
602 SDL_FreeCursor(guest_sprite);
603 SDL_Quit();
606 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
608 int flags;
609 uint8_t data = 0;
611 #if defined(__APPLE__)
612 /* always use generic keymaps */
613 if (!keyboard_layout)
614 keyboard_layout = "en-us";
615 #endif
616 if(keyboard_layout) {
617 kbd_layout = init_keyboard_layout(keyboard_layout);
618 if (!kbd_layout)
619 exit(1);
622 if (no_frame)
623 gui_noframe = 1;
625 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
626 if (SDL_Init (flags)) {
627 fprintf(stderr, "Could not initialize SDL - exiting\n");
628 exit(1);
630 #ifndef _WIN32
631 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
632 signal(SIGINT, SIG_DFL);
633 signal(SIGQUIT, SIG_DFL);
634 #endif
636 ds->dpy_update = sdl_update;
637 ds->dpy_resize = sdl_resize;
638 ds->dpy_refresh = sdl_refresh;
639 ds->dpy_fill = sdl_fill;
640 ds->mouse_set = sdl_mouse_warp;
641 ds->cursor_define = sdl_mouse_define;
643 sdl_resize(ds, 640, 400);
644 sdl_update_caption();
645 SDL_EnableKeyRepeat(250, 50);
646 gui_grab = 0;
648 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
649 sdl_cursor_normal = SDL_GetCursor();
651 atexit(sdl_cleanup);
652 if (full_screen) {
653 gui_fullscreen = 1;
654 gui_fullscreen_initial_grab = 1;
655 sdl_grab_start();