blob: d8cf5bcf740317a5675bbb4b09fe463af3e0c27c [file] [log] [blame]
bellard0f0b7262003-08-09 18:26:36 +00001/*
2 * QEMU SDL display driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard0f0b7262003-08-09 18:26:36 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard0f0b7262003-08-09 18:26:36 +00006 * 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 Weilcdfb0172010-04-01 04:20:07 +000024
25/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
26#undef WIN32_LEAN_AND_MEAN
27
Peter Maydelle16f4c82016-01-29 17:49:51 +000028#include "qemu/osdep.h"
bellard0f0b7262003-08-09 18:26:36 +000029#include <SDL.h>
aliguoric9985aa2009-03-08 15:04:07 +000030#include <SDL_syswm.h>
bellard0f0b7262003-08-09 18:26:36 +000031
blueswir1511d2b12009-03-07 15:32:56 +000032#include "qemu-common.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020033#include "qemu/cutils.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010034#include "ui/console.h"
Gerd Hoffmanna25f5452013-11-28 12:17:35 +010035#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/sysemu.h"
blueswir1511d2b12009-03-07 15:32:56 +000037#include "x_keymap.h"
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010038#include "sdl_zoom.h"
blueswir1511d2b12009-03-07 15:32:56 +000039
aliguori7d957bd2009-01-15 22:14:11 +000040static DisplayChangeListener *dcl;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +010041static DisplaySurface *surface;
aliguori7d957bd2009-01-15 22:14:11 +000042static SDL_Surface *real_screen;
43static SDL_Surface *guest_screen = NULL;
bellard0f0b7262003-08-09 18:26:36 +000044static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000045static int last_vm_running;
Jan Kiszkaf9977892011-07-30 11:39:09 +020046static bool gui_saved_scaling;
47static int gui_saved_width;
48static int gui_saved_height;
bellard8e9c4af2004-04-28 19:33:40 +000049static int gui_saved_grab;
50static int gui_fullscreen;
ths43523e92007-02-18 18:19:32 +000051static int gui_noframe;
bellard8e9c4af2004-04-28 19:33:40 +000052static int gui_key_modifier_pressed;
53static int gui_keysym;
bellard32ff25b2004-10-03 14:33:54 +000054static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
55static uint8_t modifiers_state[256];
bellard09b26c52006-04-12 21:09:08 +000056static SDL_Cursor *sdl_cursor_normal;
57static SDL_Cursor *sdl_cursor_hidden;
58static int absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +000059static int guest_cursor = 0;
60static int guest_x, guest_y;
Blue Swirl660f11b2009-07-31 21:16:51 +000061static SDL_Cursor *guest_sprite = NULL;
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010062static SDL_PixelFormat host_format;
63static int scaling_active = 0;
Anthony Liguori3af12c82010-03-10 09:42:58 -060064static Notifier mouse_mode_notifier;
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +010065static 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)
bellard0f0b7262003-08-09 18:26:36 +000070
Benjamin Herrenschmidt5f5d82d2014-07-07 15:10:12 +100071#if 0
72#define DEBUG_SDL
73#endif
74
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010075static void sdl_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010076 int x, int y, int w, int h)
bellard0f0b7262003-08-09 18:26:36 +000077{
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010078 SDL_Rect rec;
79 rec.x = x;
80 rec.y = y;
81 rec.w = w;
82 rec.h = h;
83
Benjamin Herrenschmidt5f5d82d2014-07-07 15:10:12 +100084#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
aliguori7b5d76d2009-03-13 15:02:13 +000089 if (guest_screen) {
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010090 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);
bellard0f0b7262003-08-09 18:26:36 +0000100}
101
Jan Kiszka9510a482011-07-30 11:39:18 +0200102static void do_sdl_resize(int width, int height, int bpp)
bellard0f0b7262003-08-09 18:26:36 +0000103{
104 int flags;
Lei Li898ae282013-09-04 17:07:16 +0800105 SDL_Surface *tmp_screen;
bellard0f0b7262003-08-09 18:26:36 +0000106
Benjamin Herrenschmidt5f5d82d2014-07-07 15:10:12 +1000107#ifdef DEBUG_SDL
108 printf("SDL: Resizing to %dx%d bpp %d\n", width, height, bpp);
109#endif
bellard0f0b7262003-08-09 18:26:36 +0000110
Jan Kiszka91ada982011-07-30 11:39:05 +0200111 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
112 if (gui_fullscreen) {
bellard8e9c4af2004-04-28 19:33:40 +0000113 flags |= SDL_FULLSCREEN;
Jan Kiszka91ada982011-07-30 11:39:05 +0200114 } else {
115 flags |= SDL_RESIZABLE;
116 }
ths43523e92007-02-18 18:19:32 +0000117 if (gui_noframe)
118 flags |= SDL_NOFRAME;
bellard9903da22005-10-30 23:19:10 +0000119
Lei Li898ae282013-09-04 17:07:16 +0800120 tmp_screen = SDL_SetVideoMode(width, height, bpp, flags);
aliguori7d957bd2009-01-15 22:14:11 +0000121 if (!real_screen) {
Lei Li898ae282013-09-04 17:07:16 +0800122 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 }
bellard0f0b7262003-08-09 18:26:36 +0000137 }
Lei Li898ae282013-09-04 17:07:16 +0800138
139 real_screen = tmp_screen;
aliguori7b5d76d2009-03-13 15:02:13 +0000140}
blueswir1749ecd92008-07-24 11:25:30 +0000141
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +0100142static void sdl_switch(DisplayChangeListener *dcl,
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100143 DisplaySurface *new_surface)
aliguori7b5d76d2009-03-13 15:02:13 +0000144{
Benjamin Herrenschmidtd28d6502014-07-07 15:11:22 +1000145 PixelFormat pf;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100146
147 /* temporary hack: allows to call sdl_switch to handle scaling changes */
148 if (new_surface) {
149 surface = new_surface;
aliguori7b5d76d2009-03-13 15:02:13 +0000150 }
Benjamin Herrenschmidtd28d6502014-07-07 15:11:22 +1000151 pf = qemu_pixelformat_from_pixman(surface->format);
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100152
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 Herrenschmidt5f5d82d2014-07-07 15:10:12 +1000164
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 Hoffmann8db9bae2013-03-01 09:01:13 +0100170 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 Hoffmann30f1e662014-06-18 11:03:15 +0200174 pf.rmask, pf.gmask,
175 pf.bmask, pf.amask);
bellard0f0b7262003-08-09 18:26:36 +0000176}
177
Benjamin Herrenschmidt7dd93292014-07-07 17:24:42 +1000178static 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
bellard3d11d0e2004-12-12 16:56:30 +0000191/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +0000192
bellard3d11d0e2004-12-12 16:56:30 +0000193#include "sdl_keysym.h"
bellarde58d12e2004-07-05 22:13:07 +0000194
Anthony Liguoric227f092009-10-01 16:12:16 -0500195static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000196
bellard3d11d0e2004-12-12 16:56:30 +0000197static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000198{
bellard3d11d0e2004-12-12 16:56:30 +0000199 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;
bellard60659e32006-08-19 14:27:31 +0000204 /* For Japanese key '\' and '|' */
205 if (keysym == 92 && ev->keysym.scancode == 133) {
206 keysym = 0xa5;
207 }
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100208 return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
bellarde58d12e2004-07-05 22:13:07 +0000209}
210
bellard3d11d0e2004-12-12 16:56:30 +0000211/* specific keyboard conversions from scan codes */
212
213#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000214
215static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
216{
217 return ev->keysym.scancode;
218}
219
220#else
221
aliguori5368a422009-03-03 17:37:21 +0000222#if defined(SDL_VIDEO_DRIVER_X11)
223#include <X11/XKBlib.h>
224
225static int check_for_evdev(void)
226{
227 SDL_SysWMinfo info;
Jan Kiszka229609d2009-06-27 09:59:40 +0200228 XkbDescPtr desc = NULL;
aliguori5368a422009-03-03 17:37:21 +0000229 int has_evdev = 0;
Jan Kiszka229609d2009-06-27 09:59:40 +0200230 char *keycodes = NULL;
aliguori5368a422009-03-03 17:37:21 +0000231
232 SDL_VERSION(&info.version);
Jan Kiszka229609d2009-06-27 09:59:40 +0200233 if (!SDL_GetWMInfo(&info)) {
aliguori5368a422009-03-03 17:37:21 +0000234 return 0;
Jan Kiszka229609d2009-06-27 09:59:40 +0200235 }
aliguori5368a422009-03-03 17:37:21 +0000236 desc = XkbGetKeyboard(info.info.x11.display,
237 XkbGBN_AllComponentsMask,
238 XkbUseCoreKbd);
Jan Kiszka229609d2009-06-27 09:59:40 +0200239 if (desc && desc->names) {
240 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
241 if (keycodes == NULL) {
242 fprintf(stderr, "could not lookup keycode name\n");
243 } else if (strstart(keycodes, "evdev", NULL)) {
244 has_evdev = 1;
245 } else if (!strstart(keycodes, "xfree86", NULL)) {
246 fprintf(stderr, "unknown keycodes `%s', please report to "
247 "qemu-devel@nongnu.org\n", keycodes);
248 }
249 }
aliguori5368a422009-03-03 17:37:21 +0000250
Jan Kiszka229609d2009-06-27 09:59:40 +0200251 if (desc) {
252 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
253 }
254 if (keycodes) {
255 XFree(keycodes);
256 }
aliguori5368a422009-03-03 17:37:21 +0000257 return has_evdev;
258}
259#else
260static int check_for_evdev(void)
261{
262 return 0;
263}
264#endif
265
bellarde58d12e2004-07-05 22:13:07 +0000266static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
267{
268 int keycode;
aliguori5368a422009-03-03 17:37:21 +0000269 static int has_evdev = -1;
270
271 if (has_evdev == -1)
272 has_evdev = check_for_evdev();
bellarde58d12e2004-07-05 22:13:07 +0000273
274 keycode = ev->keysym.scancode;
275
276 if (keycode < 9) {
277 keycode = 0;
278 } else if (keycode < 97) {
279 keycode -= 8; /* just an offset */
aliguori5368a422009-03-03 17:37:21 +0000280 } else if (keycode < 158) {
bellarde58d12e2004-07-05 22:13:07 +0000281 /* use conversion table */
aliguori5368a422009-03-03 17:37:21 +0000282 if (has_evdev)
283 keycode = translate_evdev_keycode(keycode - 97);
284 else
285 keycode = translate_xfree86_keycode(keycode - 97);
286 } else if (keycode == 208) { /* Hiragana_Katakana */
287 keycode = 0x70;
288 } else if (keycode == 211) { /* backslash */
289 keycode = 0x73;
bellarde58d12e2004-07-05 22:13:07 +0000290 } else {
291 keycode = 0;
292 }
293 return keycode;
294}
295
296#endif
297
bellard32ff25b2004-10-03 14:33:54 +0000298static void reset_keys(void)
299{
300 int i;
301 for(i = 0; i < 256; i++) {
302 if (modifiers_state[i]) {
Gerd Hoffmanna25f5452013-11-28 12:17:35 +0100303 qemu_input_event_send_key_number(dcl->con, i, false);
bellard32ff25b2004-10-03 14:33:54 +0000304 modifiers_state[i] = 0;
305 }
306 }
307}
308
bellard0f0b7262003-08-09 18:26:36 +0000309static void sdl_process_key(SDL_KeyboardEvent *ev)
310{
Gerd Hoffmanna25f5452013-11-28 12:17:35 +0100311 int keycode;
bellardde2200d2004-06-04 13:15:06 +0000312
313 if (ev->keysym.sym == SDLK_PAUSE) {
314 /* specific case */
Gerd Hoffmanna25f5452013-11-28 12:17:35 +0100315 qemu_input_event_send_key_qcode(dcl->con, Q_KEY_CODE_PAUSE,
316 ev->type == SDL_KEYDOWN);
bellardde2200d2004-06-04 13:15:06 +0000317 return;
318 }
319
bellard3d11d0e2004-12-12 16:56:30 +0000320 if (kbd_layout) {
321 keycode = sdl_keyevent_to_keycode_generic(ev);
322 } else {
323 keycode = sdl_keyevent_to_keycode(ev);
324 }
bellardde2200d2004-06-04 13:15:06 +0000325
326 switch(keycode) {
327 case 0x00:
328 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000329 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000330 return;
331 case 0x2a: /* Left Shift */
332 case 0x36: /* Right Shift */
333 case 0x1d: /* Left CTRL */
334 case 0x9d: /* Right CTRL */
335 case 0x38: /* Left ALT */
336 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000337 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000338 modifiers_state[keycode] = 0;
339 else
340 modifiers_state[keycode] = 1;
341 break;
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100342#define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
343#if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
344 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
bellardde2200d2004-06-04 13:15:06 +0000345 case 0x45: /* num lock */
346 case 0x3a: /* caps lock */
347 /* SDL does not send the key up event, so we generate it */
Gerd Hoffmanna25f5452013-11-28 12:17:35 +0100348 qemu_input_event_send_key_number(dcl->con, keycode, true);
349 qemu_input_event_send_key_number(dcl->con, keycode, false);
bellardde2200d2004-06-04 13:15:06 +0000350 return;
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100351#endif
bellard0f0b7262003-08-09 18:26:36 +0000352 }
bellardde2200d2004-06-04 13:15:06 +0000353
354 /* now send the key code */
Gerd Hoffmanna25f5452013-11-28 12:17:35 +0100355 qemu_input_event_send_key_number(dcl->con, keycode,
356 ev->type == SDL_KEYDOWN);
bellard0f0b7262003-08-09 18:26:36 +0000357}
358
bellard8a7ddc32004-03-31 19:00:16 +0000359static void sdl_update_caption(void)
360{
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100361 char win_title[1024];
362 char icon_title[1024];
thsc35734b2007-03-19 15:17:08 +0000363 const char *status = "";
364
Luiz Capitulino13548692011-07-29 15:36:43 -0300365 if (!runstate_is_running())
thsc35734b2007-03-19 15:17:08 +0000366 status = " [Stopped]";
ths3780e192007-06-21 21:08:02 +0000367 else if (gui_grab) {
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500368 if (alt_grab)
Anthony Liguori4e75b342010-03-01 08:47:28 -0600369 status = " - Press Ctrl-Alt-Shift to exit mouse grab";
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500370 else if (ctrl_grab)
Anthony Liguori4e75b342010-03-01 08:47:28 -0600371 status = " - Press Right-Ctrl to exit mouse grab";
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500372 else
Anthony Liguori4e75b342010-03-01 08:47:28 -0600373 status = " - Press Ctrl-Alt to exit mouse grab";
ths3780e192007-06-21 21:08:02 +0000374 }
thsc35734b2007-03-19 15:17:08 +0000375
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100376 if (qemu_name) {
377 snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
378 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
379 } else {
380 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
381 snprintf(icon_title, sizeof(icon_title), "QEMU");
382 }
thsc35734b2007-03-19 15:17:08 +0000383
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100384 SDL_WM_SetCaption(win_title, icon_title);
bellard8a7ddc32004-03-31 19:00:16 +0000385}
386
bellard09b26c52006-04-12 21:09:08 +0000387static void sdl_hide_cursor(void)
388{
balrog9467cd42007-05-01 01:34:14 +0000389 if (!cursor_hide)
390 return;
391
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100392 if (qemu_input_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000393 SDL_ShowCursor(1);
394 SDL_SetCursor(sdl_cursor_hidden);
395 } else {
396 SDL_ShowCursor(0);
397 }
bellard09b26c52006-04-12 21:09:08 +0000398}
399
400static void sdl_show_cursor(void)
401{
balrog9467cd42007-05-01 01:34:14 +0000402 if (!cursor_hide)
403 return;
404
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100405 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL)) {
bellard8785a8d2006-06-13 10:49:12 +0000406 SDL_ShowCursor(1);
thsd34cab92007-04-02 01:10:46 +0000407 if (guest_cursor &&
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100408 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
thsd34cab92007-04-02 01:10:46 +0000409 SDL_SetCursor(guest_sprite);
410 else
411 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000412 }
413}
414
bellard0f0b7262003-08-09 18:26:36 +0000415static void sdl_grab_start(void)
416{
Jan Kiszka85f94f82012-01-31 13:45:28 +0100417 /*
418 * If the application is not active, do not try to enter grab state. This
419 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
420 * application (SDL bug).
421 */
422 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) {
423 return;
424 }
thsd34cab92007-04-02 01:10:46 +0000425 if (guest_cursor) {
426 SDL_SetCursor(guest_sprite);
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100427 if (!qemu_input_is_absolute() && !absolute_enabled) {
balrog08a2d4c2009-01-29 23:29:52 +0000428 SDL_WarpMouse(guest_x, guest_y);
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100429 }
thsd34cab92007-04-02 01:10:46 +0000430 } else
431 sdl_hide_cursor();
Jan Kiszkaeaa2e022012-01-31 13:45:29 +0100432 SDL_WM_GrabInput(SDL_GRAB_ON);
433 gui_grab = 1;
434 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000435}
436
437static void sdl_grab_end(void)
438{
bellard0f0b7262003-08-09 18:26:36 +0000439 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard0f0b7262003-08-09 18:26:36 +0000440 gui_grab = 0;
thsd34cab92007-04-02 01:10:46 +0000441 sdl_show_cursor();
bellard8a7ddc32004-03-31 19:00:16 +0000442 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000443}
444
Jan Kiszka66596352012-01-31 13:45:27 +0100445static void absolute_mouse_grab(void)
446{
447 int mouse_x, mouse_y;
448
Jan Kiszka85f94f82012-01-31 13:45:28 +0100449 SDL_GetMouseState(&mouse_x, &mouse_y);
450 if (mouse_x > 0 && mouse_x < real_screen->w - 1 &&
451 mouse_y > 0 && mouse_y < real_screen->h - 1) {
452 sdl_grab_start();
Jan Kiszka66596352012-01-31 13:45:27 +0100453 }
454}
455
Jan Kiszka9e8dd452011-06-20 14:06:26 +0200456static void sdl_mouse_mode_change(Notifier *notify, void *data)
Anthony Liguori3af12c82010-03-10 09:42:58 -0600457{
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100458 if (qemu_input_is_absolute()) {
Anthony Liguori3af12c82010-03-10 09:42:58 -0600459 if (!absolute_enabled) {
Anthony Liguori3af12c82010-03-10 09:42:58 -0600460 absolute_enabled = 1;
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100461 if (qemu_console_is_graphic(NULL)) {
Jan Kiszka66596352012-01-31 13:45:27 +0100462 absolute_mouse_grab();
463 }
Anthony Liguori3af12c82010-03-10 09:42:58 -0600464 }
465 } else if (absolute_enabled) {
Jan Kiszkaf8b8d632011-08-22 18:42:42 +0200466 if (!gui_fullscreen) {
467 sdl_grab_end();
468 }
Jan Kiszka35b0f232011-07-30 11:39:15 +0200469 absolute_enabled = 0;
Anthony Liguori3af12c82010-03-10 09:42:58 -0600470 }
471}
472
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100473static void sdl_send_mouse_event(int dx, int dy, int x, int y, int state)
bellard0f0b7262003-08-09 18:26:36 +0000474{
Eric Blake7fb1cf12015-11-18 01:52:57 -0700475 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100476 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
477 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
478 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100479 [INPUT_BUTTON_WHEEL_UP] = SDL_BUTTON(SDL_BUTTON_WHEELUP),
480 [INPUT_BUTTON_WHEEL_DOWN] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN),
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100481 };
482 static uint32_t prev_state;
Jan Kiszka9510a482011-07-30 11:39:18 +0200483
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100484 if (prev_state != state) {
485 qemu_input_update_buttons(dcl->con, bmap, prev_state, state);
486 prev_state = state;
Jan Kiszka9510a482011-07-30 11:39:18 +0200487 }
bellard09b26c52006-04-12 21:09:08 +0000488
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100489 if (qemu_input_is_absolute()) {
490 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, x,
491 real_screen->w);
492 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, y,
493 real_screen->h);
Gerd Hoffmannc3aa84b2014-03-10 09:22:16 +0100494 } else {
495 if (guest_cursor) {
496 x -= guest_x;
497 y -= guest_y;
498 guest_x += x;
499 guest_y += y;
500 dx = x;
501 dy = y;
502 }
503 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, dx);
504 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, dy);
bellard09b26c52006-04-12 21:09:08 +0000505 }
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100506 qemu_input_event_sync();
bellard0f0b7262003-08-09 18:26:36 +0000507}
508
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100509static void sdl_scale(int width, int height)
Jan Kiszkaf9977892011-07-30 11:39:09 +0200510{
511 int bpp = real_screen->format->BitsPerPixel;
512
Benjamin Herrenschmidt5f5d82d2014-07-07 15:10:12 +1000513#ifdef DEBUG_SDL
514 printf("SDL: Scaling to %dx%d bpp %d\n", width, height, bpp);
515#endif
516
Jan Kiszkaf9977892011-07-30 11:39:09 +0200517 if (bpp != 16 && bpp != 32) {
518 bpp = 32;
519 }
520 do_sdl_resize(width, height, bpp);
521 scaling_active = 1;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200522}
523
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100524static void toggle_full_screen(void)
bellard8e9c4af2004-04-28 19:33:40 +0000525{
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100526 int width = surface_width(surface);
527 int height = surface_height(surface);
528 int bpp = surface_bits_per_pixel(surface);
529
bellard8e9c4af2004-04-28 19:33:40 +0000530 gui_fullscreen = !gui_fullscreen;
bellard8e9c4af2004-04-28 19:33:40 +0000531 if (gui_fullscreen) {
Jan Kiszkaf9977892011-07-30 11:39:09 +0200532 gui_saved_width = real_screen->w;
533 gui_saved_height = real_screen->h;
534 gui_saved_scaling = scaling_active;
535
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100536 do_sdl_resize(width, height, bpp);
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100537 scaling_active = 0;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200538
bellard8e9c4af2004-04-28 19:33:40 +0000539 gui_saved_grab = gui_grab;
540 sdl_grab_start();
541 } else {
Jan Kiszkaf9977892011-07-30 11:39:09 +0200542 if (gui_saved_scaling) {
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100543 sdl_scale(gui_saved_width, gui_saved_height);
Jan Kiszkaf9977892011-07-30 11:39:09 +0200544 } else {
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100545 do_sdl_resize(width, height, 0);
Jan Kiszkaf9977892011-07-30 11:39:09 +0200546 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100547 if (!gui_saved_grab || !qemu_console_is_graphic(NULL)) {
bellard8e9c4af2004-04-28 19:33:40 +0000548 sdl_grab_end();
Jan Kiszkaf8558102011-07-30 11:39:12 +0200549 }
bellard8e9c4af2004-04-28 19:33:40 +0000550 }
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100551 graphic_hw_invalidate(NULL);
552 graphic_hw_update(NULL);
bellard8e9c4af2004-04-28 19:33:40 +0000553}
554
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100555static void handle_keydown(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200556{
557 int mod_state;
558 int keycode;
559
560 if (alt_grab) {
561 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
562 (gui_grab_code | KMOD_LSHIFT);
563 } else if (ctrl_grab) {
564 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
565 } else {
566 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
567 }
568 gui_key_modifier_pressed = mod_state;
569
570 if (gui_key_modifier_pressed) {
571 keycode = sdl_keyevent_to_keycode(&ev->key);
572 switch (keycode) {
573 case 0x21: /* 'f' key on US keyboard */
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100574 toggle_full_screen();
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200575 gui_keysym = 1;
576 break;
577 case 0x16: /* 'u' key on US keyboard */
578 if (scaling_active) {
579 scaling_active = 0;
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100580 sdl_switch(dcl, NULL);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100581 graphic_hw_invalidate(NULL);
582 graphic_hw_update(NULL);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200583 }
584 gui_keysym = 1;
585 break;
586 case 0x02 ... 0x0a: /* '1' to '9' keys */
587 /* Reset the modifiers sent to the current console */
588 reset_keys();
589 console_select(keycode - 0x02);
590 gui_keysym = 1;
591 if (gui_fullscreen) {
592 break;
593 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100594 if (!qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200595 /* release grab if going to a text console */
596 if (gui_grab) {
597 sdl_grab_end();
598 } else if (absolute_enabled) {
599 sdl_show_cursor();
600 }
601 } else if (absolute_enabled) {
602 sdl_hide_cursor();
603 absolute_mouse_grab();
604 }
605 break;
606 case 0x1b: /* '+' */
607 case 0x35: /* '-' */
608 if (!gui_fullscreen) {
609 int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50),
610 160);
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100611 int height = (surface_height(surface) * width) /
612 surface_width(surface);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200613
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100614 sdl_scale(width, height);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100615 graphic_hw_invalidate(NULL);
616 graphic_hw_update(NULL);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200617 gui_keysym = 1;
618 }
619 default:
620 break;
621 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100622 } else if (!qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200623 int keysym = 0;
624
625 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
626 switch (ev->key.keysym.sym) {
627 case SDLK_UP:
628 keysym = QEMU_KEY_CTRL_UP;
629 break;
630 case SDLK_DOWN:
631 keysym = QEMU_KEY_CTRL_DOWN;
632 break;
633 case SDLK_LEFT:
634 keysym = QEMU_KEY_CTRL_LEFT;
635 break;
636 case SDLK_RIGHT:
637 keysym = QEMU_KEY_CTRL_RIGHT;
638 break;
639 case SDLK_HOME:
640 keysym = QEMU_KEY_CTRL_HOME;
641 break;
642 case SDLK_END:
643 keysym = QEMU_KEY_CTRL_END;
644 break;
645 case SDLK_PAGEUP:
646 keysym = QEMU_KEY_CTRL_PAGEUP;
647 break;
648 case SDLK_PAGEDOWN:
649 keysym = QEMU_KEY_CTRL_PAGEDOWN;
650 break;
651 default:
652 break;
653 }
654 } else {
655 switch (ev->key.keysym.sym) {
656 case SDLK_UP:
657 keysym = QEMU_KEY_UP;
658 break;
659 case SDLK_DOWN:
660 keysym = QEMU_KEY_DOWN;
661 break;
662 case SDLK_LEFT:
663 keysym = QEMU_KEY_LEFT;
664 break;
665 case SDLK_RIGHT:
666 keysym = QEMU_KEY_RIGHT;
667 break;
668 case SDLK_HOME:
669 keysym = QEMU_KEY_HOME;
670 break;
671 case SDLK_END:
672 keysym = QEMU_KEY_END;
673 break;
674 case SDLK_PAGEUP:
675 keysym = QEMU_KEY_PAGEUP;
676 break;
677 case SDLK_PAGEDOWN:
678 keysym = QEMU_KEY_PAGEDOWN;
679 break;
680 case SDLK_BACKSPACE:
681 keysym = QEMU_KEY_BACKSPACE;
682 break;
683 case SDLK_DELETE:
684 keysym = QEMU_KEY_DELETE;
685 break;
686 default:
687 break;
688 }
689 }
690 if (keysym) {
691 kbd_put_keysym(keysym);
692 } else if (ev->key.keysym.unicode != 0) {
693 kbd_put_keysym(ev->key.keysym.unicode);
694 }
695 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100696 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200697 sdl_process_key(&ev->key);
698 }
699}
700
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100701static void handle_keyup(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200702{
703 int mod_state;
704
705 if (!alt_grab) {
706 mod_state = (ev->key.keysym.mod & gui_grab_code);
707 } else {
708 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
709 }
710 if (!mod_state && gui_key_modifier_pressed) {
711 gui_key_modifier_pressed = 0;
712 if (gui_keysym == 0) {
713 /* exit/enter grab if pressing Ctrl-Alt */
714 if (!gui_grab) {
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100715 if (qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200716 sdl_grab_start();
717 }
718 } else if (!gui_fullscreen) {
719 sdl_grab_end();
720 }
721 /* SDL does not send back all the modifiers key, so we must
722 * correct it. */
723 reset_keys();
724 return;
725 }
726 gui_keysym = 0;
727 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100728 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200729 sdl_process_key(&ev->key);
730 }
731}
732
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100733static void handle_mousemotion(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200734{
735 int max_x, max_y;
736
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100737 if (qemu_console_is_graphic(NULL) &&
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100738 (qemu_input_is_absolute() || absolute_enabled)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200739 max_x = real_screen->w - 1;
740 max_y = real_screen->h - 1;
741 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
742 ev->motion.x == max_x || ev->motion.y == max_y)) {
743 sdl_grab_end();
744 }
Jan Kiszka85f94f82012-01-31 13:45:28 +0100745 if (!gui_grab &&
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200746 (ev->motion.x > 0 && ev->motion.x < max_x &&
747 ev->motion.y > 0 && ev->motion.y < max_y)) {
748 sdl_grab_start();
749 }
750 }
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100751 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
752 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel,
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200753 ev->motion.x, ev->motion.y, ev->motion.state);
754 }
755}
756
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100757static void handle_mousebutton(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200758{
759 int buttonstate = SDL_GetMouseState(NULL, NULL);
760 SDL_MouseButtonEvent *bev;
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200761
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100762 if (!qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200763 return;
764 }
765
766 bev = &ev->button;
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100767 if (!gui_grab && !qemu_input_is_absolute()) {
Jan Kiszka822f98d2012-01-31 13:45:30 +0100768 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200769 /* start grabbing all events */
770 sdl_grab_start();
771 }
772 } else {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200773 if (ev->type == SDL_MOUSEBUTTONDOWN) {
774 buttonstate |= SDL_BUTTON(bev->button);
775 } else {
776 buttonstate &= ~SDL_BUTTON(bev->button);
777 }
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100778 sdl_send_mouse_event(0, 0, bev->x, bev->y, buttonstate);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200779 }
780}
781
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100782static void handle_activation(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200783{
Jan Kiszka02df4d62012-01-31 13:45:31 +0100784#ifdef _WIN32
785 /* Disable grab if the window no longer has the focus
786 * (Windows-only workaround) */
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200787 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
788 !ev->active.gain && !gui_fullscreen) {
789 sdl_grab_end();
790 }
Jan Kiszka02df4d62012-01-31 13:45:31 +0100791#endif
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100792 if (!gui_grab && ev->active.gain && qemu_console_is_graphic(NULL) &&
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100793 (qemu_input_is_absolute() || absolute_enabled)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200794 absolute_mouse_grab();
795 }
796 if (ev->active.state & SDL_APPACTIVE) {
797 if (ev->active.gain) {
798 /* Back to default interval */
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100799 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200800 } else {
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100801 /* Sleeping interval. Not using the long default here as
802 * sdl_refresh does not only update the guest screen, but
803 * also checks for gui events. */
804 update_displaychangelistener(dcl, 500);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200805 }
806 }
807}
808
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100809static void sdl_refresh(DisplayChangeListener *dcl)
bellard0f0b7262003-08-09 18:26:36 +0000810{
811 SDL_Event ev1, *ev = &ev1;
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100812 int idle = 1;
ths3b46e622007-09-17 08:09:54 +0000813
Luiz Capitulino13548692011-07-29 15:36:43 -0300814 if (last_vm_running != runstate_is_running()) {
815 last_vm_running = runstate_is_running();
bellard8a7ddc32004-03-31 19:00:16 +0000816 sdl_update_caption();
817 }
818
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100819 graphic_hw_update(NULL);
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100820 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL));
bellard457831f2004-07-14 17:22:33 +0000821
bellard0f0b7262003-08-09 18:26:36 +0000822 while (SDL_PollEvent(ev)) {
823 switch (ev->type) {
824 case SDL_VIDEOEXPOSE:
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100825 sdl_update(dcl, 0, 0, real_screen->w, real_screen->h);
bellard0f0b7262003-08-09 18:26:36 +0000826 break;
827 case SDL_KEYDOWN:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100828 idle = 0;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100829 handle_keydown(ev);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200830 break;
bellard0f0b7262003-08-09 18:26:36 +0000831 case SDL_KEYUP:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100832 idle = 0;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100833 handle_keyup(ev);
bellard0f0b7262003-08-09 18:26:36 +0000834 break;
835 case SDL_QUIT:
Jan Kiszka941f5112011-07-30 11:39:04 +0200836 if (!no_quit) {
837 no_shutdown = 0;
balrog731345e2007-06-21 17:56:50 +0000838 qemu_system_shutdown_request();
Jan Kiszka941f5112011-07-30 11:39:04 +0200839 }
bellard0f0b7262003-08-09 18:26:36 +0000840 break;
841 case SDL_MOUSEMOTION:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100842 idle = 0;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100843 handle_mousemotion(ev);
bellard0f0b7262003-08-09 18:26:36 +0000844 break;
845 case SDL_MOUSEBUTTONDOWN:
846 case SDL_MOUSEBUTTONUP:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100847 idle = 0;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100848 handle_mousebutton(ev);
bellard0f0b7262003-08-09 18:26:36 +0000849 break;
bellard0294ffb2004-04-29 22:15:15 +0000850 case SDL_ACTIVEEVENT:
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100851 handle_activation(ev);
bellard0294ffb2004-04-29 22:15:15 +0000852 break;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200853 case SDL_VIDEORESIZE:
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100854 sdl_scale(ev->resize.w, ev->resize.h);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100855 graphic_hw_invalidate(NULL);
856 graphic_hw_update(NULL);
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100857 break;
bellard0f0b7262003-08-09 18:26:36 +0000858 default:
859 break;
860 }
861 }
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100862
863 if (idle) {
864 if (idle_counter < SDL_MAX_IDLE_COUNT) {
865 idle_counter++;
866 if (idle_counter >= SDL_MAX_IDLE_COUNT) {
867 dcl->update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
868 }
869 }
870 } else {
871 idle_counter = 0;
872 dcl->update_interval = SDL_REFRESH_INTERVAL_BUSY;
873 }
bellard0f0b7262003-08-09 18:26:36 +0000874}
875
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100876static void sdl_mouse_warp(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100877 int x, int y, int on)
thsd34cab92007-04-02 01:10:46 +0000878{
879 if (on) {
880 if (!guest_cursor)
881 sdl_show_cursor();
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100882 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
thsd34cab92007-04-02 01:10:46 +0000883 SDL_SetCursor(guest_sprite);
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100884 if (!qemu_input_is_absolute() && !absolute_enabled) {
balrog08a2d4c2009-01-29 23:29:52 +0000885 SDL_WarpMouse(x, y);
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100886 }
thsd34cab92007-04-02 01:10:46 +0000887 }
888 } else if (gui_grab)
889 sdl_hide_cursor();
890 guest_cursor = on;
891 guest_x = x, guest_y = y;
892}
893
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100894static void sdl_mouse_define(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100895 QEMUCursor *c)
thsd34cab92007-04-02 01:10:46 +0000896{
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200897 uint8_t *image, *mask;
898 int bpl;
899
thsd34cab92007-04-02 01:10:46 +0000900 if (guest_sprite)
901 SDL_FreeCursor(guest_sprite);
902
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200903 bpl = cursor_get_mono_bpl(c);
Anthony Liguori7267c092011-08-20 22:09:37 -0500904 image = g_malloc0(bpl * c->height);
905 mask = g_malloc0(bpl * c->height);
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200906 cursor_get_mono_image(c, 0x000000, image);
907 cursor_get_mono_mask(c, 0, mask);
908 guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
909 c->hot_x, c->hot_y);
Anthony Liguori7267c092011-08-20 22:09:37 -0500910 g_free(image);
911 g_free(mask);
thsd34cab92007-04-02 01:10:46 +0000912
913 if (guest_cursor &&
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100914 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
thsd34cab92007-04-02 01:10:46 +0000915 SDL_SetCursor(guest_sprite);
916}
917
Anthony Liguori28695482010-03-21 14:13:02 -0500918static void sdl_cleanup(void)
bellard898712a2004-02-06 19:56:42 +0000919{
thsd34cab92007-04-02 01:10:46 +0000920 if (guest_sprite)
921 SDL_FreeCursor(guest_sprite);
malcd8ee7662009-05-17 18:26:05 +0400922 SDL_QuitSubSystem(SDL_INIT_VIDEO);
bellard898712a2004-02-06 19:56:42 +0000923}
924
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100925static const DisplayChangeListenerOps dcl_ops = {
Benjamin Herrenschmidt7dd93292014-07-07 17:24:42 +1000926 .dpy_name = "sdl",
927 .dpy_gfx_update = sdl_update,
928 .dpy_gfx_switch = sdl_switch,
929 .dpy_gfx_check_format = sdl_check_format,
930 .dpy_refresh = sdl_refresh,
931 .dpy_mouse_set = sdl_mouse_warp,
932 .dpy_cursor_define = sdl_mouse_define,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100933};
934
Gerd Hoffmann0b71a5d2014-11-11 16:54:45 +0100935void sdl_display_early_init(int opengl)
936{
937 if (opengl == 1 /* on */) {
938 fprintf(stderr,
939 "SDL1 display code has no opengl support.\n"
940 "Please recompile qemu with SDL2, using\n"
941 "./configure --enable-sdl --with-sdlabi=2.0\n");
942 }
943}
944
ths43523e92007-02-18 18:19:32 +0000945void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
bellard0f0b7262003-08-09 18:26:36 +0000946{
947 int flags;
bellard09b26c52006-04-12 21:09:08 +0000948 uint8_t data = 0;
aliguori7b5d76d2009-03-13 15:02:13 +0000949 const SDL_VideoInfo *vi;
Stefan Weil09cec712011-02-16 21:15:40 +0100950 char *filename;
bellard0f0b7262003-08-09 18:26:36 +0000951
bellard3d11d0e2004-12-12 16:56:30 +0000952#if defined(__APPLE__)
953 /* always use generic keymaps */
954 if (!keyboard_layout)
955 keyboard_layout = "en-us";
956#endif
957 if(keyboard_layout) {
aliguori04837552009-03-06 20:27:10 +0000958 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
bellard3d11d0e2004-12-12 16:56:30 +0000959 if (!kbd_layout)
960 exit(1);
961 }
962
ths43523e92007-02-18 18:19:32 +0000963 if (no_frame)
964 gui_noframe = 1;
965
Jan Kiszka111f8ec2010-05-23 10:29:34 +0200966 if (!full_screen) {
967 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
968 }
Michael Tokarev1de97562011-05-08 01:18:30 +0400969#ifdef __linux__
970 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
971 * accessible $DISPLAY to open X11 window. This is often the case
972 * when qemu is run using sudo. But in this case, and when actually
973 * run in X11 environment, SDL fights with X11 for the video card,
974 * making current display unavailable, often until reboot.
975 * So make x11 the default SDL video driver if this variable is unset.
976 * This is a bit hackish but saves us from bigger problem.
977 * Maybe it's a good idea to fix this in SDL instead.
978 */
979 setenv("SDL_VIDEODRIVER", "x11", 0);
980#endif
Jan Kiszka111f8ec2010-05-23 10:29:34 +0200981
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100982 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
983 * This requires SDL >= 1.2.14. */
984 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
985
bellard0f0b7262003-08-09 18:26:36 +0000986 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
987 if (SDL_Init (flags)) {
malc3caf2562009-12-30 04:26:34 +0300988 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
989 SDL_GetError());
bellard0f0b7262003-08-09 18:26:36 +0000990 exit(1);
991 }
aliguori7b5d76d2009-03-13 15:02:13 +0000992 vi = SDL_GetVideoInfo();
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100993 host_format = *(vi->vfmt);
bellard0ae04d72003-09-30 21:09:16 +0000994
Stefan Weil09cec712011-02-16 21:15:40 +0100995 /* Load a 32x32x4 image. White pixels are transparent. */
996 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
997 if (filename) {
998 SDL_Surface *image = SDL_LoadBMP(filename);
999 if (image) {
1000 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
1001 SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
1002 SDL_WM_SetIcon(image, NULL);
1003 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001004 g_free(filename);
Stefan Weil09cec712011-02-16 21:15:40 +01001005 }
1006
Jan Kiszka110defd2011-07-30 11:39:11 +02001007 if (full_screen) {
1008 gui_fullscreen = 1;
1009 sdl_grab_start();
1010 }
1011
Markus Armbrusterfedf0d32015-11-03 17:12:03 +01001012 dcl = g_new0(DisplayChangeListener, 1);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +01001013 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +02001014 register_displaychangelistener(dcl);
bellard0f0b7262003-08-09 18:26:36 +00001015
Anthony Liguori3af12c82010-03-10 09:42:58 -06001016 mouse_mode_notifier.notify = sdl_mouse_mode_change;
1017 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
1018
bellard8a7ddc32004-03-31 19:00:16 +00001019 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +00001020 SDL_EnableKeyRepeat(250, 50);
1021 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +00001022
bellard09b26c52006-04-12 21:09:08 +00001023 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
1024 sdl_cursor_normal = SDL_GetCursor();
1025
Anthony Liguori28695482010-03-21 14:13:02 -05001026 atexit(sdl_cleanup);
bellard0f0b7262003-08-09 18:26:36 +00001027}