]> Git Repo - qemu.git/blob - ui/sdl2.c
input: sdl2: Fix guest_cursor logic
[qemu.git] / ui / sdl2.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 /* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
25
26 /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
27 #undef WIN32_LEAN_AND_MEAN
28
29 #include <SDL.h>
30
31 #if SDL_MAJOR_VERSION == 2
32 #include <SDL_syswm.h>
33
34 #include "qemu-common.h"
35 #include "ui/console.h"
36 #include "ui/input.h"
37 #include "sysemu/sysemu.h"
38 #include "sdl_zoom.h"
39
40 #include "sdl2-keymap.h"
41
42 static int sdl2_num_outputs;
43 static struct sdl2_state {
44     DisplayChangeListener dcl;
45     DisplaySurface *surface;
46     SDL_Texture *texture;
47     SDL_Window *real_window;
48     SDL_Renderer *real_renderer;
49     int idx;
50     int last_vm_running; /* per console for caption reasons */
51     int x, y;
52 } *sdl2_console;
53
54 static SDL_Surface *guest_sprite_surface;
55 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
56
57 static bool gui_saved_scaling;
58 static int gui_saved_width;
59 static int gui_saved_height;
60 static int gui_saved_grab;
61 static int gui_fullscreen;
62 static int gui_noframe;
63 static int gui_key_modifier_pressed;
64 static int gui_keysym;
65 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
66 static uint8_t modifiers_state[SDL_NUM_SCANCODES];
67 static SDL_Cursor *sdl_cursor_normal;
68 static SDL_Cursor *sdl_cursor_hidden;
69 static int absolute_enabled;
70 static int guest_cursor;
71 static int guest_x, guest_y;
72 static SDL_Cursor *guest_sprite;
73 static int scaling_active;
74 static Notifier mouse_mode_notifier;
75
76 static void sdl_update_caption(struct sdl2_state *scon);
77
78 static struct sdl2_state *get_scon_from_window(uint32_t window_id)
79 {
80     int i;
81     for (i = 0; i < sdl2_num_outputs; i++) {
82         if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
83             return &sdl2_console[i];
84         }
85     }
86     return NULL;
87 }
88
89 static void sdl_update(DisplayChangeListener *dcl,
90                        int x, int y, int w, int h)
91 {
92     struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
93     SDL_Rect rect;
94     DisplaySurface *surf = qemu_console_surface(dcl->con);
95
96     if (!surf) {
97         return;
98     }
99     if (!scon->texture) {
100         return;
101     }
102
103     rect.x = x;
104     rect.y = y;
105     rect.w = w;
106     rect.h = h;
107
108     SDL_UpdateTexture(scon->texture, NULL, surface_data(surf),
109                       surface_stride(surf));
110     SDL_RenderCopy(scon->real_renderer, scon->texture, &rect, &rect);
111     SDL_RenderPresent(scon->real_renderer);
112 }
113
114 static void do_sdl_resize(struct sdl2_state *scon, int width, int height,
115                           int bpp)
116 {
117     int flags;
118
119     if (scon->real_window && scon->real_renderer) {
120         if (width && height) {
121             SDL_RenderSetLogicalSize(scon->real_renderer, width, height);
122             SDL_SetWindowSize(scon->real_window, width, height);
123         } else {
124             SDL_DestroyRenderer(scon->real_renderer);
125             SDL_DestroyWindow(scon->real_window);
126             scon->real_renderer = NULL;
127             scon->real_window = NULL;
128         }
129     } else {
130         if (!width || !height) {
131             return;
132         }
133         flags = 0;
134         if (gui_fullscreen) {
135             flags |= SDL_WINDOW_FULLSCREEN;
136         } else {
137             flags |= SDL_WINDOW_RESIZABLE;
138         }
139
140         scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
141                                              SDL_WINDOWPOS_UNDEFINED,
142                                              width, height, flags);
143         scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
144         sdl_update_caption(scon);
145     }
146 }
147
148 static void sdl_switch(DisplayChangeListener *dcl,
149                        DisplaySurface *new_surface)
150 {
151     struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
152     int format = 0;
153     int idx = scon->idx;
154     DisplaySurface *old_surface = scon->surface;
155
156     /* temporary hack: allows to call sdl_switch to handle scaling changes */
157     if (new_surface) {
158         scon->surface = new_surface;
159     }
160
161     if (!new_surface && idx > 0) {
162         scon->surface = NULL;
163     }
164
165     if (new_surface == NULL) {
166         do_sdl_resize(scon, 0, 0, 0);
167     } else {
168         do_sdl_resize(scon, surface_width(scon->surface),
169                       surface_height(scon->surface), 0);
170     }
171
172     if (old_surface && scon->texture) {
173         SDL_DestroyTexture(scon->texture);
174         scon->texture = NULL;
175     }
176
177     if (new_surface) {
178         if (!scon->texture) {
179             if (surface_bits_per_pixel(scon->surface) == 16) {
180                 format = SDL_PIXELFORMAT_RGB565;
181             } else if (surface_bits_per_pixel(scon->surface) == 32) {
182                 format = SDL_PIXELFORMAT_ARGB8888;
183             }
184
185             scon->texture = SDL_CreateTexture(scon->real_renderer, format,
186                                               SDL_TEXTUREACCESS_STREAMING,
187                                               surface_width(new_surface),
188                                               surface_height(new_surface));
189         }
190     }
191 }
192
193 static void reset_keys(void)
194 {
195     int i;
196
197     for (i = 0; i < 256; i++) {
198         if (modifiers_state[i]) {
199             int qcode = sdl2_scancode_to_qcode[i];
200             qemu_input_event_send_key_qcode(NULL, qcode, false);
201             modifiers_state[i] = 0;
202         }
203     }
204 }
205
206 static void sdl_process_key(SDL_KeyboardEvent *ev)
207 {
208     int qcode = sdl2_scancode_to_qcode[ev->keysym.scancode];
209
210     switch (ev->keysym.scancode) {
211 #if 0
212     case SDL_SCANCODE_NUMLOCKCLEAR:
213     case SDL_SCANCODE_CAPSLOCK:
214         /* SDL does not send the key up event, so we generate it */
215         qemu_input_event_send_key_qcode(NULL, qcode, true);
216         qemu_input_event_send_key_qcode(NULL, qcode, false);
217         return;
218 #endif
219     case SDL_SCANCODE_LCTRL:
220     case SDL_SCANCODE_LSHIFT:
221     case SDL_SCANCODE_LALT:
222     case SDL_SCANCODE_LGUI:
223     case SDL_SCANCODE_RCTRL:
224     case SDL_SCANCODE_RSHIFT:
225     case SDL_SCANCODE_RALT:
226     case SDL_SCANCODE_RGUI:
227         if (ev->type == SDL_KEYUP) {
228             modifiers_state[ev->keysym.scancode] = 0;
229         } else {
230             modifiers_state[ev->keysym.scancode] = 1;
231         }
232         /* fall though */
233     default:
234         qemu_input_event_send_key_qcode(NULL, qcode,
235                                         ev->type == SDL_KEYDOWN);
236     }
237 }
238
239 static void sdl_update_caption(struct sdl2_state *scon)
240 {
241     char win_title[1024];
242     char icon_title[1024];
243     const char *status = "";
244
245     if (!runstate_is_running()) {
246         status = " [Stopped]";
247     } else if (gui_grab) {
248         if (alt_grab) {
249             status = " - Press Ctrl-Alt-Shift to exit mouse grab";
250         } else if (ctrl_grab) {
251             status = " - Press Right-Ctrl to exit mouse grab";
252         } else {
253             status = " - Press Ctrl-Alt to exit mouse grab";
254         }
255     }
256
257     if (qemu_name) {
258         snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
259                  scon->idx, status);
260         snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
261     } else {
262         snprintf(win_title, sizeof(win_title), "QEMU%s", status);
263         snprintf(icon_title, sizeof(icon_title), "QEMU");
264     }
265
266     if (scon->real_window) {
267         SDL_SetWindowTitle(scon->real_window, win_title);
268     }
269 }
270
271 static void sdl_hide_cursor(void)
272 {
273     if (!cursor_hide) {
274         return;
275     }
276
277     if (qemu_input_is_absolute()) {
278         SDL_ShowCursor(1);
279         SDL_SetCursor(sdl_cursor_hidden);
280     } else {
281         SDL_ShowCursor(0);
282     }
283 }
284
285 static void sdl_show_cursor(void)
286 {
287     if (!cursor_hide) {
288         return;
289     }
290
291     if (!qemu_input_is_absolute()) {
292         SDL_ShowCursor(1);
293         if (guest_cursor &&
294             (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
295             SDL_SetCursor(guest_sprite);
296         } else {
297             SDL_SetCursor(sdl_cursor_normal);
298         }
299     }
300 }
301
302 static void sdl_grab_start(struct sdl2_state *scon)
303 {
304     /*
305      * If the application is not active, do not try to enter grab state. This
306      * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
307      * application (SDL bug).
308      */
309     if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
310         return;
311     }
312     if (guest_cursor) {
313         SDL_SetCursor(guest_sprite);
314         if (!qemu_input_is_absolute() && !absolute_enabled) {
315             SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
316         }
317     } else {
318         sdl_hide_cursor();
319     }
320     SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
321     gui_grab = 1;
322     sdl_update_caption(scon);
323 }
324
325 static void sdl_grab_end(struct sdl2_state *scon)
326 {
327     SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
328     gui_grab = 0;
329     sdl_show_cursor();
330     sdl_update_caption(scon);
331 }
332
333 static void absolute_mouse_grab(struct sdl2_state *scon)
334 {
335     int mouse_x, mouse_y;
336     int scr_w, scr_h;
337     SDL_GetMouseState(&mouse_x, &mouse_y);
338     SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
339     if (mouse_x > 0 && mouse_x < scr_w - 1 &&
340         mouse_y > 0 && mouse_y < scr_h - 1) {
341         sdl_grab_start(scon);
342     }
343 }
344
345 static void sdl_mouse_mode_change(Notifier *notify, void *data)
346 {
347     if (qemu_input_is_absolute()) {
348         if (!absolute_enabled) {
349             absolute_enabled = 1;
350             absolute_mouse_grab(&sdl2_console[0]);
351         }
352     } else if (absolute_enabled) {
353         if (!gui_fullscreen) {
354             sdl_grab_end(&sdl2_console[0]);
355         }
356         absolute_enabled = 0;
357     }
358 }
359
360 static void sdl_send_mouse_event(struct sdl2_state *scon, int dx, int dy,
361                                  int dz, int x, int y, int state)
362 {
363     static uint32_t bmap[INPUT_BUTTON_MAX] = {
364         [INPUT_BUTTON_LEFT]       = SDL_BUTTON(SDL_BUTTON_LEFT),
365         [INPUT_BUTTON_MIDDLE]     = SDL_BUTTON(SDL_BUTTON_MIDDLE),
366         [INPUT_BUTTON_RIGHT]      = SDL_BUTTON(SDL_BUTTON_RIGHT),
367 #if 0
368         [INPUT_BUTTON_WHEEL_UP]   = SDL_BUTTON(SDL_BUTTON_WHEELUP),
369         [INPUT_BUTTON_WHEEL_DOWN] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN),
370 #endif
371     };
372     static uint32_t prev_state;
373
374     if (prev_state != state) {
375         qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
376         prev_state = state;
377     }
378
379     if (qemu_input_is_absolute()) {
380         int scr_w, scr_h;
381         int max_w = 0, max_h = 0;
382         int off_x = 0, off_y = 0;
383         int cur_off_x = 0, cur_off_y = 0;
384         int i;
385
386         for (i = 0; i < sdl2_num_outputs; i++) {
387             struct sdl2_state *thiscon = &sdl2_console[i];
388             if (thiscon->real_window && thiscon->surface) {
389                 SDL_GetWindowSize(thiscon->real_window, &scr_w, &scr_h);
390                 cur_off_x = thiscon->x;
391                 cur_off_y = thiscon->y;
392                 if (scr_w + cur_off_x > max_w) {
393                     max_w = scr_w + cur_off_x;
394                 }
395                 if (scr_h + cur_off_y > max_h) {
396                     max_h = scr_h + cur_off_y;
397                 }
398                 if (i == scon->idx) {
399                     off_x = cur_off_x;
400                     off_y = cur_off_y;
401                 }
402             }
403         }
404         qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X, off_x + x, max_w);
405         qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y, off_y + y, max_h);
406     } else {
407         if (guest_cursor) {
408             x -= guest_x;
409             y -= guest_y;
410             guest_x += x;
411             guest_y += y;
412             dx = x;
413             dy = y;
414         }
415         qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
416         qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
417     }
418     qemu_input_event_sync();
419 }
420
421 static void sdl_scale(struct sdl2_state *scon, int width, int height)
422 {
423     int bpp = 0;
424     do_sdl_resize(scon, width, height, bpp);
425     scaling_active = 1;
426 }
427
428 static void toggle_full_screen(struct sdl2_state *scon)
429 {
430     int width = surface_width(scon->surface);
431     int height = surface_height(scon->surface);
432     int bpp = surface_bits_per_pixel(scon->surface);
433
434     gui_fullscreen = !gui_fullscreen;
435     if (gui_fullscreen) {
436         SDL_GetWindowSize(scon->real_window,
437                           &gui_saved_width, &gui_saved_height);
438         gui_saved_scaling = scaling_active;
439
440         do_sdl_resize(scon, width, height, bpp);
441         scaling_active = 0;
442
443         gui_saved_grab = gui_grab;
444         sdl_grab_start(scon);
445     } else {
446         if (gui_saved_scaling) {
447             sdl_scale(scon, gui_saved_width, gui_saved_height);
448         } else {
449             do_sdl_resize(scon, width, height, 0);
450         }
451         if (!gui_saved_grab) {
452             sdl_grab_end(scon);
453         }
454     }
455     graphic_hw_invalidate(scon->dcl.con);
456     graphic_hw_update(scon->dcl.con);
457 }
458
459 static void handle_keydown(SDL_Event *ev)
460 {
461     int mod_state;
462     struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
463
464     if (alt_grab) {
465         mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
466             (gui_grab_code | KMOD_LSHIFT);
467     } else if (ctrl_grab) {
468         mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
469     } else {
470         mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
471     }
472     gui_key_modifier_pressed = mod_state;
473
474     if (gui_key_modifier_pressed) {
475         switch (ev->key.keysym.scancode) {
476         case SDL_SCANCODE_F:
477             toggle_full_screen(scon);
478             gui_keysym = 1;
479             break;
480         case SDL_SCANCODE_U:
481             if (scaling_active) {
482                 scaling_active = 0;
483                 sdl_switch(&scon->dcl, NULL);
484                 graphic_hw_invalidate(scon->dcl.con);
485                 graphic_hw_update(scon->dcl.con);
486             }
487             gui_keysym = 1;
488             break;
489         case SDL_SCANCODE_KP_PLUS:
490         case SDL_SCANCODE_KP_MINUS:
491             if (!gui_fullscreen) {
492                 int scr_w, scr_h;
493                 int width, height;
494                 SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
495
496                 width = MAX(scr_w + (ev->key.keysym.scancode ==
497                                      SDL_SCANCODE_KP_PLUS ? 50 : -50),
498                             160);
499                 height = (surface_height(scon->surface) * width) /
500                     surface_width(scon->surface);
501
502                 sdl_scale(scon, width, height);
503                 graphic_hw_invalidate(NULL);
504                 graphic_hw_update(NULL);
505                 gui_keysym = 1;
506             }
507         default:
508             break;
509         }
510     }
511     if (!gui_keysym) {
512         sdl_process_key(&ev->key);
513     }
514 }
515
516 static void handle_keyup(SDL_Event *ev)
517 {
518     int mod_state;
519     struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
520
521     if (!alt_grab) {
522         mod_state = (ev->key.keysym.mod & gui_grab_code);
523     } else {
524         mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
525     }
526     if (!mod_state && gui_key_modifier_pressed) {
527         gui_key_modifier_pressed = 0;
528         if (gui_keysym == 0) {
529             /* exit/enter grab if pressing Ctrl-Alt */
530             if (!gui_grab) {
531                 sdl_grab_start(scon);
532             } else if (!gui_fullscreen) {
533                 sdl_grab_end(scon);
534             }
535             /* SDL does not send back all the modifiers key, so we must
536              * correct it. */
537             reset_keys();
538             return;
539         }
540         gui_keysym = 0;
541     }
542     if (!gui_keysym) {
543         sdl_process_key(&ev->key);
544     }
545 }
546
547 static void handle_mousemotion(SDL_Event *ev)
548 {
549     int max_x, max_y;
550     struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
551
552     if (qemu_input_is_absolute() || absolute_enabled) {
553         int scr_w, scr_h;
554         SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
555         max_x = scr_w - 1;
556         max_y = scr_h - 1;
557         if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
558                          ev->motion.x == max_x || ev->motion.y == max_y)) {
559             sdl_grab_end(scon);
560         }
561         if (!gui_grab &&
562             (ev->motion.x > 0 && ev->motion.x < max_x &&
563              ev->motion.y > 0 && ev->motion.y < max_y)) {
564             sdl_grab_start(scon);
565         }
566     }
567     if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
568         sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel, 0,
569                              ev->motion.x, ev->motion.y, ev->motion.state);
570     }
571 }
572
573 static void handle_mousebutton(SDL_Event *ev)
574 {
575     int buttonstate = SDL_GetMouseState(NULL, NULL);
576     SDL_MouseButtonEvent *bev;
577     struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
578     int dz;
579
580     bev = &ev->button;
581     if (!gui_grab && !qemu_input_is_absolute()) {
582         if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
583             /* start grabbing all events */
584             sdl_grab_start(scon);
585         }
586     } else {
587         dz = 0;
588         if (ev->type == SDL_MOUSEBUTTONDOWN) {
589             buttonstate |= SDL_BUTTON(bev->button);
590         } else {
591             buttonstate &= ~SDL_BUTTON(bev->button);
592         }
593 #ifdef SDL_BUTTON_WHEELUP
594         if (bev->button == SDL_BUTTON_WHEELUP &&
595             ev->type == SDL_MOUSEBUTTONDOWN) {
596             dz = -1;
597         } else if (bev->button == SDL_BUTTON_WHEELDOWN &&
598                    ev->type == SDL_MOUSEBUTTONDOWN) {
599             dz = 1;
600         }
601 #endif
602         sdl_send_mouse_event(scon, 0, 0, dz, bev->x, bev->y, buttonstate);
603     }
604 }
605
606 static void handle_windowevent(DisplayChangeListener *dcl, SDL_Event *ev)
607 {
608     int w, h;
609     struct sdl2_state *scon = get_scon_from_window(ev->key.windowID);
610
611     switch (ev->window.event) {
612     case SDL_WINDOWEVENT_RESIZED:
613         sdl_scale(scon, ev->window.data1, ev->window.data2);
614         graphic_hw_invalidate(scon->dcl.con);
615         graphic_hw_update(scon->dcl.con);
616         break;
617     case SDL_WINDOWEVENT_EXPOSED:
618         SDL_GetWindowSize(SDL_GetWindowFromID(ev->window.windowID), &w, &h);
619         sdl_update(dcl, 0, 0, w, h);
620         break;
621     case SDL_WINDOWEVENT_FOCUS_GAINED:
622     case SDL_WINDOWEVENT_ENTER:
623         if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
624             absolute_mouse_grab(scon);
625         }
626         break;
627     case SDL_WINDOWEVENT_FOCUS_LOST:
628         if (gui_grab && !gui_fullscreen) {
629             sdl_grab_end(scon);
630         }
631         break;
632     case SDL_WINDOWEVENT_RESTORED:
633         update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
634         break;
635     case SDL_WINDOWEVENT_MINIMIZED:
636         update_displaychangelistener(dcl, 500);
637         break;
638     case SDL_WINDOWEVENT_CLOSE:
639         if (!no_quit) {
640             no_shutdown = 0;
641             qemu_system_shutdown_request();
642         }
643         break;
644     }
645 }
646
647 static void sdl_refresh(DisplayChangeListener *dcl)
648 {
649     struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
650     SDL_Event ev1, *ev = &ev1;
651
652     if (scon->last_vm_running != runstate_is_running()) {
653         scon->last_vm_running = runstate_is_running();
654         sdl_update_caption(scon);
655     }
656
657     graphic_hw_update(dcl->con);
658
659     while (SDL_PollEvent(ev)) {
660         switch (ev->type) {
661         case SDL_KEYDOWN:
662             handle_keydown(ev);
663             break;
664         case SDL_KEYUP:
665             handle_keyup(ev);
666             break;
667         case SDL_QUIT:
668             if (!no_quit) {
669                 no_shutdown = 0;
670                 qemu_system_shutdown_request();
671             }
672             break;
673         case SDL_MOUSEMOTION:
674             handle_mousemotion(ev);
675             break;
676         case SDL_MOUSEBUTTONDOWN:
677         case SDL_MOUSEBUTTONUP:
678             handle_mousebutton(ev);
679             break;
680         case SDL_WINDOWEVENT:
681             handle_windowevent(dcl, ev);
682             break;
683         default:
684             break;
685         }
686     }
687 }
688
689 static void sdl_mouse_warp(DisplayChangeListener *dcl,
690                            int x, int y, int on)
691 {
692     struct sdl2_state *scon = container_of(dcl, struct sdl2_state, dcl);
693     if (on) {
694         if (!guest_cursor) {
695             sdl_show_cursor();
696         }
697         if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
698             SDL_SetCursor(guest_sprite);
699             if (!qemu_input_is_absolute() && !absolute_enabled) {
700                 SDL_WarpMouseInWindow(scon->real_window, x, y);
701             }
702         }
703     } else if (gui_grab) {
704         sdl_hide_cursor();
705     }
706     guest_cursor = on;
707     guest_x = x, guest_y = y;
708 }
709
710 static void sdl_mouse_define(DisplayChangeListener *dcl,
711                              QEMUCursor *c)
712 {
713
714     if (guest_sprite) {
715         SDL_FreeCursor(guest_sprite);
716     }
717
718     if (guest_sprite_surface) {
719         SDL_FreeSurface(guest_sprite_surface);
720     }
721
722     guest_sprite_surface =
723         SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
724                                  0xff0000, 0x00ff00, 0xff, 0xff000000);
725
726     if (!guest_sprite_surface) {
727         fprintf(stderr, "Failed to make rgb surface from %p\n", c);
728         return;
729     }
730     guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
731                                          c->hot_x, c->hot_y);
732     if (!guest_sprite) {
733         fprintf(stderr, "Failed to make color cursor from %p\n", c);
734         return;
735     }
736     if (guest_cursor &&
737         (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
738         SDL_SetCursor(guest_sprite);
739     }
740 }
741
742 static void sdl_cleanup(void)
743 {
744     if (guest_sprite) {
745         SDL_FreeCursor(guest_sprite);
746     }
747     SDL_QuitSubSystem(SDL_INIT_VIDEO);
748 }
749
750 static const DisplayChangeListenerOps dcl_ops = {
751     .dpy_name          = "sdl",
752     .dpy_gfx_update    = sdl_update,
753     .dpy_gfx_switch    = sdl_switch,
754     .dpy_refresh       = sdl_refresh,
755     .dpy_mouse_set     = sdl_mouse_warp,
756     .dpy_cursor_define = sdl_mouse_define,
757 };
758
759 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
760 {
761     int flags;
762     uint8_t data = 0;
763     char *filename;
764     int i;
765
766     if (no_frame) {
767         gui_noframe = 1;
768     }
769
770 #ifdef __linux__
771     /* on Linux, SDL may use fbcon|directfb|svgalib when run without
772      * accessible $DISPLAY to open X11 window.  This is often the case
773      * when qemu is run using sudo.  But in this case, and when actually
774      * run in X11 environment, SDL fights with X11 for the video card,
775      * making current display unavailable, often until reboot.
776      * So make x11 the default SDL video driver if this variable is unset.
777      * This is a bit hackish but saves us from bigger problem.
778      * Maybe it's a good idea to fix this in SDL instead.
779      */
780     setenv("SDL_VIDEODRIVER", "x11", 0);
781 #endif
782
783     flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
784     if (SDL_Init(flags)) {
785         fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
786                 SDL_GetError());
787         exit(1);
788     }
789
790     for (i = 0;; i++) {
791         QemuConsole *con = qemu_console_lookup_by_index(i);
792         if (!con || !qemu_console_is_graphic(con)) {
793             break;
794         }
795     }
796     sdl2_num_outputs = i;
797     sdl2_console = g_new0(struct sdl2_state, sdl2_num_outputs);
798     for (i = 0; i < sdl2_num_outputs; i++) {
799         QemuConsole *con = qemu_console_lookup_by_index(i);
800         sdl2_console[i].dcl.ops = &dcl_ops;
801         sdl2_console[i].dcl.con = con;
802         register_displaychangelistener(&sdl2_console[i].dcl);
803         sdl2_console[i].idx = i;
804     }
805
806     /* Load a 32x32x4 image. White pixels are transparent. */
807     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
808     if (filename) {
809         SDL_Surface *image = SDL_LoadBMP(filename);
810         if (image) {
811             uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
812             SDL_SetColorKey(image, SDL_TRUE, colorkey);
813             SDL_SetWindowIcon(sdl2_console[0].real_window, image);
814         }
815         g_free(filename);
816     }
817
818     if (full_screen) {
819         gui_fullscreen = 1;
820         sdl_grab_start(0);
821     }
822
823     mouse_mode_notifier.notify = sdl_mouse_mode_change;
824     qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
825
826     gui_grab = 0;
827
828     sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
829     sdl_cursor_normal = SDL_GetCursor();
830
831     atexit(sdl_cleanup);
832 }
833 #endif
This page took 0.06795 seconds and 4 git commands to generate.