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