Disable the vnc CopyRect encoding
[qemu-kvm/fedora.git] / sdl.c
blob8b7a1fef4f7da2fa218f26fa30a60e2dfd650cc9
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"
27 #include "x_keymap.h"
29 #include <SDL.h>
30 #include <SDL_syswm.h>
32 #ifndef _WIN32
33 #include <signal.h>
34 #endif
36 static DisplayChangeListener *dcl;
37 static SDL_Surface *real_screen;
38 static SDL_Surface *guest_screen = NULL;
39 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
40 static int last_vm_running;
41 static int gui_saved_grab;
42 static int gui_fullscreen;
43 static int gui_noframe;
44 static int gui_key_modifier_pressed;
45 static int gui_keysym;
46 static int gui_fullscreen_initial_grab;
47 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
48 static uint8_t modifiers_state[256];
49 static int width, height;
50 static SDL_Cursor *sdl_cursor_normal;
51 static SDL_Cursor *sdl_cursor_hidden;
52 static int absolute_enabled = 0;
53 static int guest_cursor = 0;
54 static int guest_x, guest_y;
55 static SDL_Cursor *guest_sprite = 0;
57 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
59 SDL_Rect rec;
60 rec.x = x;
61 rec.y = y;
62 rec.w = w;
63 rec.h = h;
64 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
66 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
67 SDL_UpdateRect(real_screen, x, y, w, h);
70 static void sdl_setdata(DisplayState *ds)
72 SDL_Rect rec;
73 rec.x = 0;
74 rec.y = 0;
75 rec.w = real_screen->w;
76 rec.h = real_screen->h;
78 if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
80 guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds),
81 ds_get_bits_per_pixel(ds), ds_get_linesize(ds),
82 ds->surface->pf.rmask, ds->surface->pf.gmask,
83 ds->surface->pf.bmask, ds->surface->pf.amask);
86 static void sdl_resize(DisplayState *ds)
88 int flags;
90 // printf("resizing to %d %d\n", w, h);
92 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
93 if (gui_fullscreen)
94 flags |= SDL_FULLSCREEN;
95 if (gui_noframe)
96 flags |= SDL_NOFRAME;
98 width = ds_get_width(ds);
99 height = ds_get_height(ds);
100 real_screen = SDL_SetVideoMode(width, height, 0, flags);
101 if (!real_screen) {
102 fprintf(stderr, "Could not open SDL display\n");
103 exit(1);
106 sdl_setdata(ds);
109 /* generic keyboard conversion */
111 #include "sdl_keysym.h"
113 static kbd_layout_t *kbd_layout = NULL;
115 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
117 int keysym;
118 /* workaround for X11+SDL bug with AltGR */
119 keysym = ev->keysym.sym;
120 if (keysym == 0 && ev->keysym.scancode == 113)
121 keysym = SDLK_MODE;
122 /* For Japanese key '\' and '|' */
123 if (keysym == 92 && ev->keysym.scancode == 133) {
124 keysym = 0xa5;
126 return keysym2scancode(kbd_layout, keysym);
129 /* specific keyboard conversions from scan codes */
131 #if defined(_WIN32)
133 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
135 return ev->keysym.scancode;
138 #else
140 #if defined(SDL_VIDEO_DRIVER_X11)
141 #include <X11/XKBlib.h>
143 static int check_for_evdev(void)
145 SDL_SysWMinfo info;
146 XkbDescPtr desc = NULL;
147 int has_evdev = 0;
148 char *keycodes = NULL;
150 SDL_VERSION(&info.version);
151 if (!SDL_GetWMInfo(&info)) {
152 return 0;
154 desc = XkbGetKeyboard(info.info.x11.display,
155 XkbGBN_AllComponentsMask,
156 XkbUseCoreKbd);
157 if (desc && desc->names) {
158 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
159 if (keycodes == NULL) {
160 fprintf(stderr, "could not lookup keycode name\n");
161 } else if (strstart(keycodes, "evdev", NULL)) {
162 has_evdev = 1;
163 } else if (!strstart(keycodes, "xfree86", NULL)) {
164 fprintf(stderr, "unknown keycodes `%s', please report to "
165 "qemu-devel@nongnu.org\n", keycodes);
169 if (desc) {
170 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
172 if (keycodes) {
173 XFree(keycodes);
175 return has_evdev;
177 #else
178 static int check_for_evdev(void)
180 return 0;
182 #endif
184 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
186 int keycode;
187 static int has_evdev = -1;
189 if (has_evdev == -1)
190 has_evdev = check_for_evdev();
192 keycode = ev->keysym.scancode;
194 if (keycode < 9) {
195 keycode = 0;
196 } else if (keycode < 97) {
197 keycode -= 8; /* just an offset */
198 } else if (keycode < 158) {
199 /* use conversion table */
200 if (has_evdev)
201 keycode = translate_evdev_keycode(keycode - 97);
202 else
203 keycode = translate_xfree86_keycode(keycode - 97);
204 } else if (keycode == 208) { /* Hiragana_Katakana */
205 keycode = 0x70;
206 } else if (keycode == 211) { /* backslash */
207 keycode = 0x73;
208 } else {
209 keycode = 0;
211 return keycode;
214 #endif
216 static void reset_keys(void)
218 int i;
219 for(i = 0; i < 256; i++) {
220 if (modifiers_state[i]) {
221 if (i & 0x80)
222 kbd_put_keycode(0xe0);
223 kbd_put_keycode(i | 0x80);
224 modifiers_state[i] = 0;
229 static void sdl_process_key(SDL_KeyboardEvent *ev)
231 int keycode, v;
233 if (ev->keysym.sym == SDLK_PAUSE) {
234 /* specific case */
235 v = 0;
236 if (ev->type == SDL_KEYUP)
237 v |= 0x80;
238 kbd_put_keycode(0xe1);
239 kbd_put_keycode(0x1d | v);
240 kbd_put_keycode(0x45 | v);
241 return;
244 if (kbd_layout) {
245 keycode = sdl_keyevent_to_keycode_generic(ev);
246 } else {
247 keycode = sdl_keyevent_to_keycode(ev);
250 switch(keycode) {
251 case 0x00:
252 /* sent when leaving window: reset the modifiers state */
253 reset_keys();
254 return;
255 case 0x2a: /* Left Shift */
256 case 0x36: /* Right Shift */
257 case 0x1d: /* Left CTRL */
258 case 0x9d: /* Right CTRL */
259 case 0x38: /* Left ALT */
260 case 0xb8: /* Right ALT */
261 if (ev->type == SDL_KEYUP)
262 modifiers_state[keycode] = 0;
263 else
264 modifiers_state[keycode] = 1;
265 break;
266 case 0x45: /* num lock */
267 case 0x3a: /* caps lock */
268 /* SDL does not send the key up event, so we generate it */
269 kbd_put_keycode(keycode);
270 kbd_put_keycode(keycode | 0x80);
271 return;
274 /* now send the key code */
275 if (keycode & 0x80)
276 kbd_put_keycode(0xe0);
277 if (ev->type == SDL_KEYUP)
278 kbd_put_keycode(keycode | 0x80);
279 else
280 kbd_put_keycode(keycode & 0x7f);
283 static void sdl_update_caption(void)
285 char buf[1024];
286 const char *status = "";
288 if (!vm_running)
289 status = " [Stopped]";
290 else if (gui_grab) {
291 if (!alt_grab)
292 status = " - Press Ctrl-Alt to exit grab";
293 else
294 status = " - Press Ctrl-Alt-Shift to exit grab";
297 if (qemu_name)
298 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
299 else
300 snprintf(buf, sizeof(buf), "QEMU%s", status);
302 SDL_WM_SetCaption(buf, "QEMU");
305 static void sdl_hide_cursor(void)
307 if (!cursor_hide)
308 return;
310 if (kbd_mouse_is_absolute()) {
311 SDL_ShowCursor(1);
312 SDL_SetCursor(sdl_cursor_hidden);
313 } else {
314 SDL_ShowCursor(0);
318 static void sdl_show_cursor(void)
320 if (!cursor_hide)
321 return;
323 if (!kbd_mouse_is_absolute()) {
324 SDL_ShowCursor(1);
325 if (guest_cursor &&
326 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
327 SDL_SetCursor(guest_sprite);
328 else
329 SDL_SetCursor(sdl_cursor_normal);
333 static void sdl_grab_start(void)
335 if (guest_cursor) {
336 SDL_SetCursor(guest_sprite);
337 if (!kbd_mouse_is_absolute() && !absolute_enabled)
338 SDL_WarpMouse(guest_x, guest_y);
339 } else
340 sdl_hide_cursor();
342 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
343 gui_grab = 1;
344 sdl_update_caption();
345 } else
346 sdl_show_cursor();
349 static void sdl_grab_end(void)
351 SDL_WM_GrabInput(SDL_GRAB_OFF);
352 gui_grab = 0;
353 sdl_show_cursor();
354 sdl_update_caption();
357 static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
359 int buttons;
360 buttons = 0;
361 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
362 buttons |= MOUSE_EVENT_LBUTTON;
363 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
364 buttons |= MOUSE_EVENT_RBUTTON;
365 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
366 buttons |= MOUSE_EVENT_MBUTTON;
368 if (kbd_mouse_is_absolute()) {
369 if (!absolute_enabled) {
370 sdl_hide_cursor();
371 if (gui_grab) {
372 sdl_grab_end();
374 absolute_enabled = 1;
377 dx = x * 0x7FFF / (width - 1);
378 dy = y * 0x7FFF / (height - 1);
379 } else if (absolute_enabled) {
380 sdl_show_cursor();
381 absolute_enabled = 0;
382 } else if (guest_cursor) {
383 x -= guest_x;
384 y -= guest_y;
385 guest_x += x;
386 guest_y += y;
387 dx = x;
388 dy = y;
391 kbd_mouse_event(dx, dy, dz, buttons);
394 static void toggle_full_screen(DisplayState *ds)
396 gui_fullscreen = !gui_fullscreen;
397 sdl_resize(ds);
398 if (gui_fullscreen) {
399 gui_saved_grab = gui_grab;
400 sdl_grab_start();
401 } else {
402 if (!gui_saved_grab)
403 sdl_grab_end();
405 vga_hw_invalidate();
406 vga_hw_update();
409 static void sdl_refresh(DisplayState *ds)
411 SDL_Event ev1, *ev = &ev1;
412 int mod_state;
413 int buttonstate = SDL_GetMouseState(NULL, NULL);
415 if (last_vm_running != vm_running) {
416 last_vm_running = vm_running;
417 sdl_update_caption();
420 vga_hw_update();
421 SDL_EnableUNICODE(!is_graphic_console());
423 while (SDL_PollEvent(ev)) {
424 switch (ev->type) {
425 case SDL_VIDEOEXPOSE:
426 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
427 break;
428 case SDL_KEYDOWN:
429 case SDL_KEYUP:
430 if (ev->type == SDL_KEYDOWN) {
431 if (!alt_grab) {
432 mod_state = (SDL_GetModState() & gui_grab_code) ==
433 gui_grab_code;
434 } else {
435 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
436 (gui_grab_code | KMOD_LSHIFT);
438 gui_key_modifier_pressed = mod_state;
439 if (gui_key_modifier_pressed) {
440 int keycode;
441 keycode = sdl_keyevent_to_keycode(&ev->key);
442 switch(keycode) {
443 case 0x21: /* 'f' key on US keyboard */
444 toggle_full_screen(ds);
445 gui_keysym = 1;
446 break;
447 case 0x02 ... 0x0a: /* '1' to '9' keys */
448 /* Reset the modifiers sent to the current console */
449 reset_keys();
450 console_select(keycode - 0x02);
451 if (!is_graphic_console()) {
452 /* display grab if going to a text console */
453 if (gui_grab)
454 sdl_grab_end();
456 gui_keysym = 1;
457 break;
458 default:
459 break;
461 } else if (!is_graphic_console()) {
462 int keysym;
463 keysym = 0;
464 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
465 switch(ev->key.keysym.sym) {
466 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
467 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
468 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
469 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
470 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
471 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
472 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
473 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
474 default: break;
476 } else {
477 switch(ev->key.keysym.sym) {
478 case SDLK_UP: keysym = QEMU_KEY_UP; break;
479 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
480 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
481 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
482 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
483 case SDLK_END: keysym = QEMU_KEY_END; break;
484 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
485 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
486 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
487 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
488 default: break;
491 if (keysym) {
492 kbd_put_keysym(keysym);
493 } else if (ev->key.keysym.unicode != 0) {
494 kbd_put_keysym(ev->key.keysym.unicode);
497 } else if (ev->type == SDL_KEYUP) {
498 if (!alt_grab) {
499 mod_state = (ev->key.keysym.mod & gui_grab_code);
500 } else {
501 mod_state = (ev->key.keysym.mod &
502 (gui_grab_code | KMOD_LSHIFT));
504 if (!mod_state) {
505 if (gui_key_modifier_pressed) {
506 gui_key_modifier_pressed = 0;
507 if (gui_keysym == 0) {
508 /* exit/enter grab if pressing Ctrl-Alt */
509 if (!gui_grab) {
510 /* if the application is not active,
511 do not try to enter grab state. It
512 prevents
513 'SDL_WM_GrabInput(SDL_GRAB_ON)'
514 from blocking all the application
515 (SDL bug). */
516 if (SDL_GetAppState() & SDL_APPACTIVE)
517 sdl_grab_start();
518 } else {
519 sdl_grab_end();
521 /* SDL does not send back all the
522 modifiers key, so we must correct it */
523 reset_keys();
524 break;
526 gui_keysym = 0;
530 if (is_graphic_console() && !gui_keysym)
531 sdl_process_key(&ev->key);
532 break;
533 case SDL_QUIT:
534 if (!no_quit)
535 qemu_system_shutdown_request();
536 break;
537 case SDL_MOUSEMOTION:
538 if (gui_grab || kbd_mouse_is_absolute() ||
539 absolute_enabled) {
540 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
541 ev->motion.x, ev->motion.y, ev->motion.state);
543 break;
544 case SDL_MOUSEBUTTONDOWN:
545 case SDL_MOUSEBUTTONUP:
547 SDL_MouseButtonEvent *bev = &ev->button;
548 if (!gui_grab && !kbd_mouse_is_absolute()) {
549 if (ev->type == SDL_MOUSEBUTTONDOWN &&
550 (bev->button == SDL_BUTTON_LEFT)) {
551 /* start grabbing all events */
552 sdl_grab_start();
554 } else {
555 int dz;
556 dz = 0;
557 if (ev->type == SDL_MOUSEBUTTONDOWN) {
558 buttonstate |= SDL_BUTTON(bev->button);
559 } else {
560 buttonstate &= ~SDL_BUTTON(bev->button);
562 #ifdef SDL_BUTTON_WHEELUP
563 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
564 dz = -1;
565 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
566 dz = 1;
568 #endif
569 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
572 break;
573 case SDL_ACTIVEEVENT:
574 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
575 !ev->active.gain && !gui_fullscreen_initial_grab) {
576 sdl_grab_end();
578 if (ev->active.state & SDL_APPACTIVE) {
579 if (ev->active.gain) {
580 /* Back to default interval */
581 dcl->gui_timer_interval = 0;
582 dcl->idle = 0;
583 } else {
584 /* Sleeping interval */
585 dcl->gui_timer_interval = 500;
586 dcl->idle = 1;
589 break;
590 default:
591 break;
596 static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
598 SDL_Rect dst = { x, y, w, h };
599 SDL_FillRect(real_screen, &dst, c);
602 static void sdl_mouse_warp(int x, int y, int on)
604 if (on) {
605 if (!guest_cursor)
606 sdl_show_cursor();
607 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
608 SDL_SetCursor(guest_sprite);
609 if (!kbd_mouse_is_absolute() && !absolute_enabled)
610 SDL_WarpMouse(x, y);
612 } else if (gui_grab)
613 sdl_hide_cursor();
614 guest_cursor = on;
615 guest_x = x, guest_y = y;
618 static void sdl_mouse_define(int width, int height, int bpp,
619 int hot_x, int hot_y,
620 uint8_t *image, uint8_t *mask)
622 uint8_t sprite[256], *line;
623 int x, y, dst, bypl, src = 0;
624 if (guest_sprite)
625 SDL_FreeCursor(guest_sprite);
627 memset(sprite, 0, 256);
628 bypl = ((width * bpp + 31) >> 5) << 2;
629 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
630 line = image;
631 for (x = 0; x < width; x ++, dst ++) {
632 switch (bpp) {
633 case 24:
634 src = *(line ++); src |= *(line ++); src |= *(line ++);
635 break;
636 case 16:
637 case 15:
638 src = *(line ++); src |= *(line ++);
639 break;
640 case 8:
641 src = *(line ++);
642 break;
643 case 4:
644 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
645 break;
646 case 2:
647 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
648 break;
649 case 1:
650 src = 1 & (line[x >> 3] >> (x & 7));
651 break;
653 if (!src)
654 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
657 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
659 if (guest_cursor &&
660 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
661 SDL_SetCursor(guest_sprite);
664 static void sdl_cleanup(void)
666 if (guest_sprite)
667 SDL_FreeCursor(guest_sprite);
668 SDL_Quit();
671 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
673 int flags;
674 uint8_t data = 0;
676 #if defined(__APPLE__)
677 /* always use generic keymaps */
678 if (!keyboard_layout)
679 keyboard_layout = "en-us";
680 #endif
681 if(keyboard_layout) {
682 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
683 if (!kbd_layout)
684 exit(1);
687 if (no_frame)
688 gui_noframe = 1;
690 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
691 if (SDL_Init (flags)) {
692 fprintf(stderr, "Could not initialize SDL - exiting\n");
693 exit(1);
696 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
697 dcl->dpy_update = sdl_update;
698 dcl->dpy_resize = sdl_resize;
699 dcl->dpy_refresh = sdl_refresh;
700 dcl->dpy_setdata = sdl_setdata;
701 dcl->dpy_fill = sdl_fill;
702 ds->mouse_set = sdl_mouse_warp;
703 ds->cursor_define = sdl_mouse_define;
704 register_displaychangelistener(ds, dcl);
706 sdl_update_caption();
707 SDL_EnableKeyRepeat(250, 50);
708 gui_grab = 0;
710 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
711 sdl_cursor_normal = SDL_GetCursor();
713 atexit(sdl_cleanup);
714 if (full_screen) {
715 gui_fullscreen = 1;
716 gui_fullscreen_initial_grab = 1;
717 sdl_grab_start();