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