Enhance 'info vnc' monitor output ("Daniel P. Berrange")
[qemu-kvm/fedora.git] / sdl.c
blob95efe8df1219de306ef07758cbf0c4025db31323
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"
112 #include "keymaps.c"
114 static kbd_layout_t *kbd_layout = NULL;
116 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
118 int keysym;
119 /* workaround for X11+SDL bug with AltGR */
120 keysym = ev->keysym.sym;
121 if (keysym == 0 && ev->keysym.scancode == 113)
122 keysym = SDLK_MODE;
123 /* For Japanese key '\' and '|' */
124 if (keysym == 92 && ev->keysym.scancode == 133) {
125 keysym = 0xa5;
127 return keysym2scancode(kbd_layout, keysym);
130 /* specific keyboard conversions from scan codes */
132 #if defined(_WIN32)
134 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
136 return ev->keysym.scancode;
139 #else
141 #if defined(SDL_VIDEO_DRIVER_X11)
142 #include <X11/XKBlib.h>
144 static int check_for_evdev(void)
146 SDL_SysWMinfo info;
147 XkbDescPtr desc = NULL;
148 int has_evdev = 0;
149 char *keycodes = NULL;
151 SDL_VERSION(&info.version);
152 if (!SDL_GetWMInfo(&info)) {
153 return 0;
155 desc = XkbGetKeyboard(info.info.x11.display,
156 XkbGBN_AllComponentsMask,
157 XkbUseCoreKbd);
158 if (desc && desc->names) {
159 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
160 if (keycodes == NULL) {
161 fprintf(stderr, "could not lookup keycode name\n");
162 } else if (strstart(keycodes, "evdev", NULL)) {
163 has_evdev = 1;
164 } else if (!strstart(keycodes, "xfree86", NULL)) {
165 fprintf(stderr, "unknown keycodes `%s', please report to "
166 "qemu-devel@nongnu.org\n", keycodes);
170 if (desc) {
171 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
173 if (keycodes) {
174 XFree(keycodes);
176 return has_evdev;
178 #else
179 static int check_for_evdev(void)
181 return 0;
183 #endif
185 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
187 int keycode;
188 static int has_evdev = -1;
190 if (has_evdev == -1)
191 has_evdev = check_for_evdev();
193 keycode = ev->keysym.scancode;
195 if (keycode < 9) {
196 keycode = 0;
197 } else if (keycode < 97) {
198 keycode -= 8; /* just an offset */
199 } else if (keycode < 158) {
200 /* use conversion table */
201 if (has_evdev)
202 keycode = translate_evdev_keycode(keycode - 97);
203 else
204 keycode = translate_xfree86_keycode(keycode - 97);
205 } else if (keycode == 208) { /* Hiragana_Katakana */
206 keycode = 0x70;
207 } else if (keycode == 211) { /* backslash */
208 keycode = 0x73;
209 } else {
210 keycode = 0;
212 return keycode;
215 #endif
217 static void reset_keys(void)
219 int i;
220 for(i = 0; i < 256; i++) {
221 if (modifiers_state[i]) {
222 if (i & 0x80)
223 kbd_put_keycode(0xe0);
224 kbd_put_keycode(i | 0x80);
225 modifiers_state[i] = 0;
230 static void sdl_process_key(SDL_KeyboardEvent *ev)
232 int keycode, v;
234 if (ev->keysym.sym == SDLK_PAUSE) {
235 /* specific case */
236 v = 0;
237 if (ev->type == SDL_KEYUP)
238 v |= 0x80;
239 kbd_put_keycode(0xe1);
240 kbd_put_keycode(0x1d | v);
241 kbd_put_keycode(0x45 | v);
242 return;
245 if (kbd_layout) {
246 keycode = sdl_keyevent_to_keycode_generic(ev);
247 } else {
248 keycode = sdl_keyevent_to_keycode(ev);
251 switch(keycode) {
252 case 0x00:
253 /* sent when leaving window: reset the modifiers state */
254 reset_keys();
255 return;
256 case 0x2a: /* Left Shift */
257 case 0x36: /* Right Shift */
258 case 0x1d: /* Left CTRL */
259 case 0x9d: /* Right CTRL */
260 case 0x38: /* Left ALT */
261 case 0xb8: /* Right ALT */
262 if (ev->type == SDL_KEYUP)
263 modifiers_state[keycode] = 0;
264 else
265 modifiers_state[keycode] = 1;
266 break;
267 case 0x45: /* num lock */
268 case 0x3a: /* caps lock */
269 /* SDL does not send the key up event, so we generate it */
270 kbd_put_keycode(keycode);
271 kbd_put_keycode(keycode | 0x80);
272 return;
275 /* now send the key code */
276 if (keycode & 0x80)
277 kbd_put_keycode(0xe0);
278 if (ev->type == SDL_KEYUP)
279 kbd_put_keycode(keycode | 0x80);
280 else
281 kbd_put_keycode(keycode & 0x7f);
284 static void sdl_update_caption(void)
286 char buf[1024];
287 const char *status = "";
289 if (!vm_running)
290 status = " [Stopped]";
291 else if (gui_grab) {
292 if (!alt_grab)
293 status = " - Press Ctrl-Alt to exit grab";
294 else
295 status = " - Press Ctrl-Alt-Shift to exit grab";
298 if (qemu_name)
299 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
300 else
301 snprintf(buf, sizeof(buf), "QEMU%s", status);
303 SDL_WM_SetCaption(buf, "QEMU");
306 static void sdl_hide_cursor(void)
308 if (!cursor_hide)
309 return;
311 if (kbd_mouse_is_absolute()) {
312 SDL_ShowCursor(1);
313 SDL_SetCursor(sdl_cursor_hidden);
314 } else {
315 SDL_ShowCursor(0);
319 static void sdl_show_cursor(void)
321 if (!cursor_hide)
322 return;
324 if (!kbd_mouse_is_absolute()) {
325 SDL_ShowCursor(1);
326 if (guest_cursor &&
327 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
328 SDL_SetCursor(guest_sprite);
329 else
330 SDL_SetCursor(sdl_cursor_normal);
334 static void sdl_grab_start(void)
336 if (guest_cursor) {
337 SDL_SetCursor(guest_sprite);
338 if (!kbd_mouse_is_absolute() && !absolute_enabled)
339 SDL_WarpMouse(guest_x, guest_y);
340 } else
341 sdl_hide_cursor();
343 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
344 gui_grab = 1;
345 sdl_update_caption();
346 } else
347 sdl_show_cursor();
350 static void sdl_grab_end(void)
352 SDL_WM_GrabInput(SDL_GRAB_OFF);
353 gui_grab = 0;
354 sdl_show_cursor();
355 sdl_update_caption();
358 static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
360 int buttons;
361 buttons = 0;
362 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
363 buttons |= MOUSE_EVENT_LBUTTON;
364 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
365 buttons |= MOUSE_EVENT_RBUTTON;
366 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
367 buttons |= MOUSE_EVENT_MBUTTON;
369 if (kbd_mouse_is_absolute()) {
370 if (!absolute_enabled) {
371 sdl_hide_cursor();
372 if (gui_grab) {
373 sdl_grab_end();
375 absolute_enabled = 1;
378 dx = x * 0x7FFF / (width - 1);
379 dy = y * 0x7FFF / (height - 1);
380 } else if (absolute_enabled) {
381 sdl_show_cursor();
382 absolute_enabled = 0;
383 } else if (guest_cursor) {
384 x -= guest_x;
385 y -= guest_y;
386 guest_x += x;
387 guest_y += y;
388 dx = x;
389 dy = y;
392 kbd_mouse_event(dx, dy, dz, buttons);
395 static void toggle_full_screen(DisplayState *ds)
397 gui_fullscreen = !gui_fullscreen;
398 sdl_resize(ds);
399 if (gui_fullscreen) {
400 gui_saved_grab = gui_grab;
401 sdl_grab_start();
402 } else {
403 if (!gui_saved_grab)
404 sdl_grab_end();
406 vga_hw_invalidate();
407 vga_hw_update();
410 static void sdl_refresh(DisplayState *ds)
412 SDL_Event ev1, *ev = &ev1;
413 int mod_state;
414 int buttonstate = SDL_GetMouseState(NULL, NULL);
416 if (last_vm_running != vm_running) {
417 last_vm_running = vm_running;
418 sdl_update_caption();
421 vga_hw_update();
422 SDL_EnableUNICODE(!is_graphic_console());
424 while (SDL_PollEvent(ev)) {
425 switch (ev->type) {
426 case SDL_VIDEOEXPOSE:
427 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
428 break;
429 case SDL_KEYDOWN:
430 case SDL_KEYUP:
431 if (ev->type == SDL_KEYDOWN) {
432 if (!alt_grab) {
433 mod_state = (SDL_GetModState() & gui_grab_code) ==
434 gui_grab_code;
435 } else {
436 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
437 (gui_grab_code | KMOD_LSHIFT);
439 gui_key_modifier_pressed = mod_state;
440 if (gui_key_modifier_pressed) {
441 int keycode;
442 keycode = sdl_keyevent_to_keycode(&ev->key);
443 switch(keycode) {
444 case 0x21: /* 'f' key on US keyboard */
445 toggle_full_screen(ds);
446 gui_keysym = 1;
447 break;
448 case 0x02 ... 0x0a: /* '1' to '9' keys */
449 /* Reset the modifiers sent to the current console */
450 reset_keys();
451 console_select(keycode - 0x02);
452 if (!is_graphic_console()) {
453 /* display grab if going to a text console */
454 if (gui_grab)
455 sdl_grab_end();
457 gui_keysym = 1;
458 break;
459 default:
460 break;
462 } else if (!is_graphic_console()) {
463 int keysym;
464 keysym = 0;
465 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
466 switch(ev->key.keysym.sym) {
467 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
468 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
469 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
470 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
471 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
472 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
473 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
474 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
475 default: break;
477 } else {
478 switch(ev->key.keysym.sym) {
479 case SDLK_UP: keysym = QEMU_KEY_UP; break;
480 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
481 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
482 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
483 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
484 case SDLK_END: keysym = QEMU_KEY_END; break;
485 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
486 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
487 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
488 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
489 default: break;
492 if (keysym) {
493 kbd_put_keysym(keysym);
494 } else if (ev->key.keysym.unicode != 0) {
495 kbd_put_keysym(ev->key.keysym.unicode);
498 } else if (ev->type == SDL_KEYUP) {
499 if (!alt_grab) {
500 mod_state = (ev->key.keysym.mod & gui_grab_code);
501 } else {
502 mod_state = (ev->key.keysym.mod &
503 (gui_grab_code | KMOD_LSHIFT));
505 if (!mod_state) {
506 if (gui_key_modifier_pressed) {
507 gui_key_modifier_pressed = 0;
508 if (gui_keysym == 0) {
509 /* exit/enter grab if pressing Ctrl-Alt */
510 if (!gui_grab) {
511 /* if the application is not active,
512 do not try to enter grab state. It
513 prevents
514 'SDL_WM_GrabInput(SDL_GRAB_ON)'
515 from blocking all the application
516 (SDL bug). */
517 if (SDL_GetAppState() & SDL_APPACTIVE)
518 sdl_grab_start();
519 } else {
520 sdl_grab_end();
522 /* SDL does not send back all the
523 modifiers key, so we must correct it */
524 reset_keys();
525 break;
527 gui_keysym = 0;
531 if (is_graphic_console() && !gui_keysym)
532 sdl_process_key(&ev->key);
533 break;
534 case SDL_QUIT:
535 if (!no_quit)
536 qemu_system_shutdown_request();
537 break;
538 case SDL_MOUSEMOTION:
539 if (gui_grab || kbd_mouse_is_absolute() ||
540 absolute_enabled) {
541 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
542 ev->motion.x, ev->motion.y, ev->motion.state);
544 break;
545 case SDL_MOUSEBUTTONDOWN:
546 case SDL_MOUSEBUTTONUP:
548 SDL_MouseButtonEvent *bev = &ev->button;
549 if (!gui_grab && !kbd_mouse_is_absolute()) {
550 if (ev->type == SDL_MOUSEBUTTONDOWN &&
551 (bev->button == SDL_BUTTON_LEFT)) {
552 /* start grabbing all events */
553 sdl_grab_start();
555 } else {
556 int dz;
557 dz = 0;
558 if (ev->type == SDL_MOUSEBUTTONDOWN) {
559 buttonstate |= SDL_BUTTON(bev->button);
560 } else {
561 buttonstate &= ~SDL_BUTTON(bev->button);
563 #ifdef SDL_BUTTON_WHEELUP
564 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
565 dz = -1;
566 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
567 dz = 1;
569 #endif
570 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
573 break;
574 case SDL_ACTIVEEVENT:
575 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
576 !ev->active.gain && !gui_fullscreen_initial_grab) {
577 sdl_grab_end();
579 if (ev->active.state & SDL_APPACTIVE) {
580 if (ev->active.gain) {
581 /* Back to default interval */
582 dcl->gui_timer_interval = 0;
583 dcl->idle = 0;
584 } else {
585 /* Sleeping interval */
586 dcl->gui_timer_interval = 500;
587 dcl->idle = 1;
590 break;
591 default:
592 break;
597 static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
599 SDL_Rect dst = { x, y, w, h };
600 SDL_FillRect(real_screen, &dst, c);
603 static void sdl_mouse_warp(int x, int y, int on)
605 if (on) {
606 if (!guest_cursor)
607 sdl_show_cursor();
608 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
609 SDL_SetCursor(guest_sprite);
610 if (!kbd_mouse_is_absolute() && !absolute_enabled)
611 SDL_WarpMouse(x, y);
613 } else if (gui_grab)
614 sdl_hide_cursor();
615 guest_cursor = on;
616 guest_x = x, guest_y = y;
619 static void sdl_mouse_define(int width, int height, int bpp,
620 int hot_x, int hot_y,
621 uint8_t *image, uint8_t *mask)
623 uint8_t sprite[256], *line;
624 int x, y, dst, bypl, src = 0;
625 if (guest_sprite)
626 SDL_FreeCursor(guest_sprite);
628 memset(sprite, 0, 256);
629 bypl = ((width * bpp + 31) >> 5) << 2;
630 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
631 line = image;
632 for (x = 0; x < width; x ++, dst ++) {
633 switch (bpp) {
634 case 24:
635 src = *(line ++); src |= *(line ++); src |= *(line ++);
636 break;
637 case 16:
638 case 15:
639 src = *(line ++); src |= *(line ++);
640 break;
641 case 8:
642 src = *(line ++);
643 break;
644 case 4:
645 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
646 break;
647 case 2:
648 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
649 break;
650 case 1:
651 src = 1 & (line[x >> 3] >> (x & 7));
652 break;
654 if (!src)
655 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
658 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
660 if (guest_cursor &&
661 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
662 SDL_SetCursor(guest_sprite);
665 static void sdl_cleanup(void)
667 if (guest_sprite)
668 SDL_FreeCursor(guest_sprite);
669 SDL_Quit();
672 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
674 int flags;
675 uint8_t data = 0;
677 #if defined(__APPLE__)
678 /* always use generic keymaps */
679 if (!keyboard_layout)
680 keyboard_layout = "en-us";
681 #endif
682 if(keyboard_layout) {
683 kbd_layout = init_keyboard_layout(keyboard_layout);
684 if (!kbd_layout)
685 exit(1);
688 if (no_frame)
689 gui_noframe = 1;
691 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
692 if (SDL_Init (flags)) {
693 fprintf(stderr, "Could not initialize SDL - exiting\n");
694 exit(1);
697 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
698 dcl->dpy_update = sdl_update;
699 dcl->dpy_resize = sdl_resize;
700 dcl->dpy_refresh = sdl_refresh;
701 dcl->dpy_setdata = sdl_setdata;
702 dcl->dpy_fill = sdl_fill;
703 ds->mouse_set = sdl_mouse_warp;
704 ds->cursor_define = sdl_mouse_define;
705 register_displaychangelistener(ds, dcl);
707 sdl_update_caption();
708 SDL_EnableKeyRepeat(250, 50);
709 gui_grab = 0;
711 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
712 sdl_cursor_normal = SDL_GetCursor();
714 atexit(sdl_cleanup);
715 if (full_screen) {
716 gui_fullscreen = 1;
717 gui_fullscreen_initial_grab = 1;
718 sdl_grab_start();