]> git.proxmox.com Git - mirror_qemu.git/blob - ui/sdl.c
input: keyboard: switch sdl ui to new core
[mirror_qemu.git] / ui / sdl.c
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 */
24
25 /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
26 #undef WIN32_LEAN_AND_MEAN
27
28 #include <SDL.h>
29 #include <SDL_syswm.h>
30
31 #include "qemu-common.h"
32 #include "ui/console.h"
33 #include "ui/input.h"
34 #include "sysemu/sysemu.h"
35 #include "x_keymap.h"
36 #include "sdl_zoom.h"
37
38 static DisplayChangeListener *dcl;
39 static DisplaySurface *surface;
40 static SDL_Surface *real_screen;
41 static SDL_Surface *guest_screen = NULL;
42 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
43 static int last_vm_running;
44 static bool gui_saved_scaling;
45 static int gui_saved_width;
46 static int gui_saved_height;
47 static int gui_saved_grab;
48 static int gui_fullscreen;
49 static int gui_noframe;
50 static int gui_key_modifier_pressed;
51 static int gui_keysym;
52 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
53 static uint8_t modifiers_state[256];
54 static SDL_Cursor *sdl_cursor_normal;
55 static SDL_Cursor *sdl_cursor_hidden;
56 static int absolute_enabled = 0;
57 static int guest_cursor = 0;
58 static int guest_x, guest_y;
59 static SDL_Cursor *guest_sprite = NULL;
60 static SDL_PixelFormat host_format;
61 static int scaling_active = 0;
62 static Notifier mouse_mode_notifier;
63
64 static void sdl_update(DisplayChangeListener *dcl,
65 int x, int y, int w, int h)
66 {
67 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
68 SDL_Rect rec;
69 rec.x = x;
70 rec.y = y;
71 rec.w = w;
72 rec.h = h;
73
74 if (guest_screen) {
75 if (!scaling_active) {
76 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
77 } else {
78 if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) {
79 fprintf(stderr, "Zoom blit failed\n");
80 exit(1);
81 }
82 }
83 }
84 SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h);
85 }
86
87 static void do_sdl_resize(int width, int height, int bpp)
88 {
89 int flags;
90 SDL_Surface *tmp_screen;
91
92 // printf("resizing to %d %d\n", w, h);
93
94 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
95 if (gui_fullscreen) {
96 flags |= SDL_FULLSCREEN;
97 } else {
98 flags |= SDL_RESIZABLE;
99 }
100 if (gui_noframe)
101 flags |= SDL_NOFRAME;
102
103 tmp_screen = SDL_SetVideoMode(width, height, bpp, flags);
104 if (!real_screen) {
105 if (!tmp_screen) {
106 fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n",
107 width, height, bpp, SDL_GetError());
108 exit(1);
109 }
110 } else {
111 /*
112 * Revert to the previous video mode if the change of resizing or
113 * resolution failed.
114 */
115 if (!tmp_screen) {
116 fprintf(stderr, "Failed to set SDL display (%dx%dx%d): %s\n",
117 width, height, bpp, SDL_GetError());
118 return;
119 }
120 }
121
122 real_screen = tmp_screen;
123 }
124
125 static void sdl_switch(DisplayChangeListener *dcl,
126 DisplaySurface *new_surface)
127 {
128
129 /* temporary hack: allows to call sdl_switch to handle scaling changes */
130 if (new_surface) {
131 surface = new_surface;
132 }
133
134 if (!scaling_active) {
135 do_sdl_resize(surface_width(surface), surface_height(surface), 0);
136 } else if (real_screen->format->BitsPerPixel !=
137 surface_bits_per_pixel(surface)) {
138 do_sdl_resize(real_screen->w, real_screen->h,
139 surface_bits_per_pixel(surface));
140 }
141
142 if (guest_screen != NULL) {
143 SDL_FreeSurface(guest_screen);
144 }
145 guest_screen = SDL_CreateRGBSurfaceFrom
146 (surface_data(surface),
147 surface_width(surface), surface_height(surface),
148 surface_bits_per_pixel(surface), surface_stride(surface),
149 surface->pf.rmask, surface->pf.gmask,
150 surface->pf.bmask, surface->pf.amask);
151 }
152
153 /* generic keyboard conversion */
154
155 #include "sdl_keysym.h"
156
157 static kbd_layout_t *kbd_layout = NULL;
158
159 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
160 {
161 int keysym;
162 /* workaround for X11+SDL bug with AltGR */
163 keysym = ev->keysym.sym;
164 if (keysym == 0 && ev->keysym.scancode == 113)
165 keysym = SDLK_MODE;
166 /* For Japanese key '\' and '|' */
167 if (keysym == 92 && ev->keysym.scancode == 133) {
168 keysym = 0xa5;
169 }
170 return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
171 }
172
173 /* specific keyboard conversions from scan codes */
174
175 #if defined(_WIN32)
176
177 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
178 {
179 return ev->keysym.scancode;
180 }
181
182 #else
183
184 #if defined(SDL_VIDEO_DRIVER_X11)
185 #include <X11/XKBlib.h>
186
187 static int check_for_evdev(void)
188 {
189 SDL_SysWMinfo info;
190 XkbDescPtr desc = NULL;
191 int has_evdev = 0;
192 char *keycodes = NULL;
193
194 SDL_VERSION(&info.version);
195 if (!SDL_GetWMInfo(&info)) {
196 return 0;
197 }
198 desc = XkbGetKeyboard(info.info.x11.display,
199 XkbGBN_AllComponentsMask,
200 XkbUseCoreKbd);
201 if (desc && desc->names) {
202 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
203 if (keycodes == NULL) {
204 fprintf(stderr, "could not lookup keycode name\n");
205 } else if (strstart(keycodes, "evdev", NULL)) {
206 has_evdev = 1;
207 } else if (!strstart(keycodes, "xfree86", NULL)) {
208 fprintf(stderr, "unknown keycodes `%s', please report to "
209 "qemu-devel@nongnu.org\n", keycodes);
210 }
211 }
212
213 if (desc) {
214 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
215 }
216 if (keycodes) {
217 XFree(keycodes);
218 }
219 return has_evdev;
220 }
221 #else
222 static int check_for_evdev(void)
223 {
224 return 0;
225 }
226 #endif
227
228 static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
229 {
230 int keycode;
231 static int has_evdev = -1;
232
233 if (has_evdev == -1)
234 has_evdev = check_for_evdev();
235
236 keycode = ev->keysym.scancode;
237
238 if (keycode < 9) {
239 keycode = 0;
240 } else if (keycode < 97) {
241 keycode -= 8; /* just an offset */
242 } else if (keycode < 158) {
243 /* use conversion table */
244 if (has_evdev)
245 keycode = translate_evdev_keycode(keycode - 97);
246 else
247 keycode = translate_xfree86_keycode(keycode - 97);
248 } else if (keycode == 208) { /* Hiragana_Katakana */
249 keycode = 0x70;
250 } else if (keycode == 211) { /* backslash */
251 keycode = 0x73;
252 } else {
253 keycode = 0;
254 }
255 return keycode;
256 }
257
258 #endif
259
260 static void reset_keys(void)
261 {
262 int i;
263 for(i = 0; i < 256; i++) {
264 if (modifiers_state[i]) {
265 qemu_input_event_send_key_number(dcl->con, i, false);
266 modifiers_state[i] = 0;
267 }
268 }
269 }
270
271 static void sdl_process_key(SDL_KeyboardEvent *ev)
272 {
273 int keycode;
274
275 if (ev->keysym.sym == SDLK_PAUSE) {
276 /* specific case */
277 qemu_input_event_send_key_qcode(dcl->con, Q_KEY_CODE_PAUSE,
278 ev->type == SDL_KEYDOWN);
279 return;
280 }
281
282 if (kbd_layout) {
283 keycode = sdl_keyevent_to_keycode_generic(ev);
284 } else {
285 keycode = sdl_keyevent_to_keycode(ev);
286 }
287
288 switch(keycode) {
289 case 0x00:
290 /* sent when leaving window: reset the modifiers state */
291 reset_keys();
292 return;
293 case 0x2a: /* Left Shift */
294 case 0x36: /* Right Shift */
295 case 0x1d: /* Left CTRL */
296 case 0x9d: /* Right CTRL */
297 case 0x38: /* Left ALT */
298 case 0xb8: /* Right ALT */
299 if (ev->type == SDL_KEYUP)
300 modifiers_state[keycode] = 0;
301 else
302 modifiers_state[keycode] = 1;
303 break;
304 #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
305 #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
306 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
307 case 0x45: /* num lock */
308 case 0x3a: /* caps lock */
309 /* SDL does not send the key up event, so we generate it */
310 qemu_input_event_send_key_number(dcl->con, keycode, true);
311 qemu_input_event_send_key_number(dcl->con, keycode, false);
312 return;
313 #endif
314 }
315
316 /* now send the key code */
317 qemu_input_event_send_key_number(dcl->con, keycode,
318 ev->type == SDL_KEYDOWN);
319 }
320
321 static void sdl_update_caption(void)
322 {
323 char win_title[1024];
324 char icon_title[1024];
325 const char *status = "";
326
327 if (!runstate_is_running())
328 status = " [Stopped]";
329 else if (gui_grab) {
330 if (alt_grab)
331 status = " - Press Ctrl-Alt-Shift to exit mouse grab";
332 else if (ctrl_grab)
333 status = " - Press Right-Ctrl to exit mouse grab";
334 else
335 status = " - Press Ctrl-Alt to exit mouse grab";
336 }
337
338 if (qemu_name) {
339 snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
340 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
341 } else {
342 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
343 snprintf(icon_title, sizeof(icon_title), "QEMU");
344 }
345
346 SDL_WM_SetCaption(win_title, icon_title);
347 }
348
349 static void sdl_hide_cursor(void)
350 {
351 if (!cursor_hide)
352 return;
353
354 if (kbd_mouse_is_absolute()) {
355 SDL_ShowCursor(1);
356 SDL_SetCursor(sdl_cursor_hidden);
357 } else {
358 SDL_ShowCursor(0);
359 }
360 }
361
362 static void sdl_show_cursor(void)
363 {
364 if (!cursor_hide)
365 return;
366
367 if (!kbd_mouse_is_absolute() || !qemu_console_is_graphic(NULL)) {
368 SDL_ShowCursor(1);
369 if (guest_cursor &&
370 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
371 SDL_SetCursor(guest_sprite);
372 else
373 SDL_SetCursor(sdl_cursor_normal);
374 }
375 }
376
377 static void sdl_grab_start(void)
378 {
379 /*
380 * If the application is not active, do not try to enter grab state. This
381 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
382 * application (SDL bug).
383 */
384 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) {
385 return;
386 }
387 if (guest_cursor) {
388 SDL_SetCursor(guest_sprite);
389 if (!kbd_mouse_is_absolute() && !absolute_enabled)
390 SDL_WarpMouse(guest_x, guest_y);
391 } else
392 sdl_hide_cursor();
393 SDL_WM_GrabInput(SDL_GRAB_ON);
394 gui_grab = 1;
395 sdl_update_caption();
396 }
397
398 static void sdl_grab_end(void)
399 {
400 SDL_WM_GrabInput(SDL_GRAB_OFF);
401 gui_grab = 0;
402 sdl_show_cursor();
403 sdl_update_caption();
404 }
405
406 static void absolute_mouse_grab(void)
407 {
408 int mouse_x, mouse_y;
409
410 SDL_GetMouseState(&mouse_x, &mouse_y);
411 if (mouse_x > 0 && mouse_x < real_screen->w - 1 &&
412 mouse_y > 0 && mouse_y < real_screen->h - 1) {
413 sdl_grab_start();
414 }
415 }
416
417 static void sdl_mouse_mode_change(Notifier *notify, void *data)
418 {
419 if (kbd_mouse_is_absolute()) {
420 if (!absolute_enabled) {
421 absolute_enabled = 1;
422 if (qemu_console_is_graphic(NULL)) {
423 absolute_mouse_grab();
424 }
425 }
426 } else if (absolute_enabled) {
427 if (!gui_fullscreen) {
428 sdl_grab_end();
429 }
430 absolute_enabled = 0;
431 }
432 }
433
434 static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
435 {
436 int buttons = 0;
437
438 if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) {
439 buttons |= MOUSE_EVENT_LBUTTON;
440 }
441 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
442 buttons |= MOUSE_EVENT_RBUTTON;
443 }
444 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) {
445 buttons |= MOUSE_EVENT_MBUTTON;
446 }
447
448 if (kbd_mouse_is_absolute()) {
449 dx = x * 0x7FFF / (real_screen->w - 1);
450 dy = y * 0x7FFF / (real_screen->h - 1);
451 } else if (guest_cursor) {
452 x -= guest_x;
453 y -= guest_y;
454 guest_x += x;
455 guest_y += y;
456 dx = x;
457 dy = y;
458 }
459
460 kbd_mouse_event(dx, dy, dz, buttons);
461 }
462
463 static void sdl_scale(int width, int height)
464 {
465 int bpp = real_screen->format->BitsPerPixel;
466
467 if (bpp != 16 && bpp != 32) {
468 bpp = 32;
469 }
470 do_sdl_resize(width, height, bpp);
471 scaling_active = 1;
472 }
473
474 static void toggle_full_screen(void)
475 {
476 int width = surface_width(surface);
477 int height = surface_height(surface);
478 int bpp = surface_bits_per_pixel(surface);
479
480 gui_fullscreen = !gui_fullscreen;
481 if (gui_fullscreen) {
482 gui_saved_width = real_screen->w;
483 gui_saved_height = real_screen->h;
484 gui_saved_scaling = scaling_active;
485
486 do_sdl_resize(width, height, bpp);
487 scaling_active = 0;
488
489 gui_saved_grab = gui_grab;
490 sdl_grab_start();
491 } else {
492 if (gui_saved_scaling) {
493 sdl_scale(gui_saved_width, gui_saved_height);
494 } else {
495 do_sdl_resize(width, height, 0);
496 }
497 if (!gui_saved_grab || !qemu_console_is_graphic(NULL)) {
498 sdl_grab_end();
499 }
500 }
501 graphic_hw_invalidate(NULL);
502 graphic_hw_update(NULL);
503 }
504
505 static void handle_keydown(SDL_Event *ev)
506 {
507 int mod_state;
508 int keycode;
509
510 if (alt_grab) {
511 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
512 (gui_grab_code | KMOD_LSHIFT);
513 } else if (ctrl_grab) {
514 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
515 } else {
516 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
517 }
518 gui_key_modifier_pressed = mod_state;
519
520 if (gui_key_modifier_pressed) {
521 keycode = sdl_keyevent_to_keycode(&ev->key);
522 switch (keycode) {
523 case 0x21: /* 'f' key on US keyboard */
524 toggle_full_screen();
525 gui_keysym = 1;
526 break;
527 case 0x16: /* 'u' key on US keyboard */
528 if (scaling_active) {
529 scaling_active = 0;
530 sdl_switch(dcl, NULL);
531 graphic_hw_invalidate(NULL);
532 graphic_hw_update(NULL);
533 }
534 gui_keysym = 1;
535 break;
536 case 0x02 ... 0x0a: /* '1' to '9' keys */
537 /* Reset the modifiers sent to the current console */
538 reset_keys();
539 console_select(keycode - 0x02);
540 gui_keysym = 1;
541 if (gui_fullscreen) {
542 break;
543 }
544 if (!qemu_console_is_graphic(NULL)) {
545 /* release grab if going to a text console */
546 if (gui_grab) {
547 sdl_grab_end();
548 } else if (absolute_enabled) {
549 sdl_show_cursor();
550 }
551 } else if (absolute_enabled) {
552 sdl_hide_cursor();
553 absolute_mouse_grab();
554 }
555 break;
556 case 0x1b: /* '+' */
557 case 0x35: /* '-' */
558 if (!gui_fullscreen) {
559 int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50),
560 160);
561 int height = (surface_height(surface) * width) /
562 surface_width(surface);
563
564 sdl_scale(width, height);
565 graphic_hw_invalidate(NULL);
566 graphic_hw_update(NULL);
567 gui_keysym = 1;
568 }
569 default:
570 break;
571 }
572 } else if (!qemu_console_is_graphic(NULL)) {
573 int keysym = 0;
574
575 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
576 switch (ev->key.keysym.sym) {
577 case SDLK_UP:
578 keysym = QEMU_KEY_CTRL_UP;
579 break;
580 case SDLK_DOWN:
581 keysym = QEMU_KEY_CTRL_DOWN;
582 break;
583 case SDLK_LEFT:
584 keysym = QEMU_KEY_CTRL_LEFT;
585 break;
586 case SDLK_RIGHT:
587 keysym = QEMU_KEY_CTRL_RIGHT;
588 break;
589 case SDLK_HOME:
590 keysym = QEMU_KEY_CTRL_HOME;
591 break;
592 case SDLK_END:
593 keysym = QEMU_KEY_CTRL_END;
594 break;
595 case SDLK_PAGEUP:
596 keysym = QEMU_KEY_CTRL_PAGEUP;
597 break;
598 case SDLK_PAGEDOWN:
599 keysym = QEMU_KEY_CTRL_PAGEDOWN;
600 break;
601 default:
602 break;
603 }
604 } else {
605 switch (ev->key.keysym.sym) {
606 case SDLK_UP:
607 keysym = QEMU_KEY_UP;
608 break;
609 case SDLK_DOWN:
610 keysym = QEMU_KEY_DOWN;
611 break;
612 case SDLK_LEFT:
613 keysym = QEMU_KEY_LEFT;
614 break;
615 case SDLK_RIGHT:
616 keysym = QEMU_KEY_RIGHT;
617 break;
618 case SDLK_HOME:
619 keysym = QEMU_KEY_HOME;
620 break;
621 case SDLK_END:
622 keysym = QEMU_KEY_END;
623 break;
624 case SDLK_PAGEUP:
625 keysym = QEMU_KEY_PAGEUP;
626 break;
627 case SDLK_PAGEDOWN:
628 keysym = QEMU_KEY_PAGEDOWN;
629 break;
630 case SDLK_BACKSPACE:
631 keysym = QEMU_KEY_BACKSPACE;
632 break;
633 case SDLK_DELETE:
634 keysym = QEMU_KEY_DELETE;
635 break;
636 default:
637 break;
638 }
639 }
640 if (keysym) {
641 kbd_put_keysym(keysym);
642 } else if (ev->key.keysym.unicode != 0) {
643 kbd_put_keysym(ev->key.keysym.unicode);
644 }
645 }
646 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
647 sdl_process_key(&ev->key);
648 }
649 }
650
651 static void handle_keyup(SDL_Event *ev)
652 {
653 int mod_state;
654
655 if (!alt_grab) {
656 mod_state = (ev->key.keysym.mod & gui_grab_code);
657 } else {
658 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
659 }
660 if (!mod_state && gui_key_modifier_pressed) {
661 gui_key_modifier_pressed = 0;
662 if (gui_keysym == 0) {
663 /* exit/enter grab if pressing Ctrl-Alt */
664 if (!gui_grab) {
665 if (qemu_console_is_graphic(NULL)) {
666 sdl_grab_start();
667 }
668 } else if (!gui_fullscreen) {
669 sdl_grab_end();
670 }
671 /* SDL does not send back all the modifiers key, so we must
672 * correct it. */
673 reset_keys();
674 return;
675 }
676 gui_keysym = 0;
677 }
678 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
679 sdl_process_key(&ev->key);
680 }
681 }
682
683 static void handle_mousemotion(SDL_Event *ev)
684 {
685 int max_x, max_y;
686
687 if (qemu_console_is_graphic(NULL) &&
688 (kbd_mouse_is_absolute() || absolute_enabled)) {
689 max_x = real_screen->w - 1;
690 max_y = real_screen->h - 1;
691 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
692 ev->motion.x == max_x || ev->motion.y == max_y)) {
693 sdl_grab_end();
694 }
695 if (!gui_grab &&
696 (ev->motion.x > 0 && ev->motion.x < max_x &&
697 ev->motion.y > 0 && ev->motion.y < max_y)) {
698 sdl_grab_start();
699 }
700 }
701 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
702 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
703 ev->motion.x, ev->motion.y, ev->motion.state);
704 }
705 }
706
707 static void handle_mousebutton(SDL_Event *ev)
708 {
709 int buttonstate = SDL_GetMouseState(NULL, NULL);
710 SDL_MouseButtonEvent *bev;
711 int dz;
712
713 if (!qemu_console_is_graphic(NULL)) {
714 return;
715 }
716
717 bev = &ev->button;
718 if (!gui_grab && !kbd_mouse_is_absolute()) {
719 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
720 /* start grabbing all events */
721 sdl_grab_start();
722 }
723 } else {
724 dz = 0;
725 if (ev->type == SDL_MOUSEBUTTONDOWN) {
726 buttonstate |= SDL_BUTTON(bev->button);
727 } else {
728 buttonstate &= ~SDL_BUTTON(bev->button);
729 }
730 #ifdef SDL_BUTTON_WHEELUP
731 if (bev->button == SDL_BUTTON_WHEELUP &&
732 ev->type == SDL_MOUSEBUTTONDOWN) {
733 dz = -1;
734 } else if (bev->button == SDL_BUTTON_WHEELDOWN &&
735 ev->type == SDL_MOUSEBUTTONDOWN) {
736 dz = 1;
737 }
738 #endif
739 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
740 }
741 }
742
743 static void handle_activation(SDL_Event *ev)
744 {
745 #ifdef _WIN32
746 /* Disable grab if the window no longer has the focus
747 * (Windows-only workaround) */
748 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
749 !ev->active.gain && !gui_fullscreen) {
750 sdl_grab_end();
751 }
752 #endif
753 if (!gui_grab && ev->active.gain && qemu_console_is_graphic(NULL) &&
754 (kbd_mouse_is_absolute() || absolute_enabled)) {
755 absolute_mouse_grab();
756 }
757 if (ev->active.state & SDL_APPACTIVE) {
758 if (ev->active.gain) {
759 /* Back to default interval */
760 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
761 } else {
762 /* Sleeping interval. Not using the long default here as
763 * sdl_refresh does not only update the guest screen, but
764 * also checks for gui events. */
765 update_displaychangelistener(dcl, 500);
766 }
767 }
768 }
769
770 static void sdl_refresh(DisplayChangeListener *dcl)
771 {
772 SDL_Event ev1, *ev = &ev1;
773
774 if (last_vm_running != runstate_is_running()) {
775 last_vm_running = runstate_is_running();
776 sdl_update_caption();
777 }
778
779 graphic_hw_update(NULL);
780 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL));
781
782 while (SDL_PollEvent(ev)) {
783 switch (ev->type) {
784 case SDL_VIDEOEXPOSE:
785 sdl_update(dcl, 0, 0, real_screen->w, real_screen->h);
786 break;
787 case SDL_KEYDOWN:
788 handle_keydown(ev);
789 break;
790 case SDL_KEYUP:
791 handle_keyup(ev);
792 break;
793 case SDL_QUIT:
794 if (!no_quit) {
795 no_shutdown = 0;
796 qemu_system_shutdown_request();
797 }
798 break;
799 case SDL_MOUSEMOTION:
800 handle_mousemotion(ev);
801 break;
802 case SDL_MOUSEBUTTONDOWN:
803 case SDL_MOUSEBUTTONUP:
804 handle_mousebutton(ev);
805 break;
806 case SDL_ACTIVEEVENT:
807 handle_activation(ev);
808 break;
809 case SDL_VIDEORESIZE:
810 sdl_scale(ev->resize.w, ev->resize.h);
811 graphic_hw_invalidate(NULL);
812 graphic_hw_update(NULL);
813 break;
814 default:
815 break;
816 }
817 }
818 }
819
820 static void sdl_mouse_warp(DisplayChangeListener *dcl,
821 int x, int y, int on)
822 {
823 if (on) {
824 if (!guest_cursor)
825 sdl_show_cursor();
826 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
827 SDL_SetCursor(guest_sprite);
828 if (!kbd_mouse_is_absolute() && !absolute_enabled)
829 SDL_WarpMouse(x, y);
830 }
831 } else if (gui_grab)
832 sdl_hide_cursor();
833 guest_cursor = on;
834 guest_x = x, guest_y = y;
835 }
836
837 static void sdl_mouse_define(DisplayChangeListener *dcl,
838 QEMUCursor *c)
839 {
840 uint8_t *image, *mask;
841 int bpl;
842
843 if (guest_sprite)
844 SDL_FreeCursor(guest_sprite);
845
846 bpl = cursor_get_mono_bpl(c);
847 image = g_malloc0(bpl * c->height);
848 mask = g_malloc0(bpl * c->height);
849 cursor_get_mono_image(c, 0x000000, image);
850 cursor_get_mono_mask(c, 0, mask);
851 guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
852 c->hot_x, c->hot_y);
853 g_free(image);
854 g_free(mask);
855
856 if (guest_cursor &&
857 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
858 SDL_SetCursor(guest_sprite);
859 }
860
861 static void sdl_cleanup(void)
862 {
863 if (guest_sprite)
864 SDL_FreeCursor(guest_sprite);
865 SDL_QuitSubSystem(SDL_INIT_VIDEO);
866 }
867
868 static const DisplayChangeListenerOps dcl_ops = {
869 .dpy_name = "sdl",
870 .dpy_gfx_update = sdl_update,
871 .dpy_gfx_switch = sdl_switch,
872 .dpy_refresh = sdl_refresh,
873 .dpy_mouse_set = sdl_mouse_warp,
874 .dpy_cursor_define = sdl_mouse_define,
875 };
876
877 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
878 {
879 int flags;
880 uint8_t data = 0;
881 const SDL_VideoInfo *vi;
882 char *filename;
883
884 #if defined(__APPLE__)
885 /* always use generic keymaps */
886 if (!keyboard_layout)
887 keyboard_layout = "en-us";
888 #endif
889 if(keyboard_layout) {
890 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
891 if (!kbd_layout)
892 exit(1);
893 }
894
895 if (no_frame)
896 gui_noframe = 1;
897
898 if (!full_screen) {
899 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
900 }
901 #ifdef __linux__
902 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
903 * accessible $DISPLAY to open X11 window. This is often the case
904 * when qemu is run using sudo. But in this case, and when actually
905 * run in X11 environment, SDL fights with X11 for the video card,
906 * making current display unavailable, often until reboot.
907 * So make x11 the default SDL video driver if this variable is unset.
908 * This is a bit hackish but saves us from bigger problem.
909 * Maybe it's a good idea to fix this in SDL instead.
910 */
911 setenv("SDL_VIDEODRIVER", "x11", 0);
912 #endif
913
914 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
915 * This requires SDL >= 1.2.14. */
916 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
917
918 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
919 if (SDL_Init (flags)) {
920 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
921 SDL_GetError());
922 exit(1);
923 }
924 vi = SDL_GetVideoInfo();
925 host_format = *(vi->vfmt);
926
927 /* Load a 32x32x4 image. White pixels are transparent. */
928 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
929 if (filename) {
930 SDL_Surface *image = SDL_LoadBMP(filename);
931 if (image) {
932 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
933 SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
934 SDL_WM_SetIcon(image, NULL);
935 }
936 g_free(filename);
937 }
938
939 if (full_screen) {
940 gui_fullscreen = 1;
941 sdl_grab_start();
942 }
943
944 dcl = g_malloc0(sizeof(DisplayChangeListener));
945 dcl->ops = &dcl_ops;
946 register_displaychangelistener(dcl);
947
948 mouse_mode_notifier.notify = sdl_mouse_mode_change;
949 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
950
951 sdl_update_caption();
952 SDL_EnableKeyRepeat(250, 50);
953 gui_grab = 0;
954
955 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
956 sdl_cursor_normal = SDL_GetCursor();
957
958 atexit(sdl_cleanup);
959 }