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