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