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