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