blob: 190b16f5759e36d1f4c82abd82867073d0188e45 [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
Fei Liab4f9312018-10-17 10:26:50 +020032#include "qapi/error.h"
blueswir1511d2b12009-03-07 15:32:56 +000033#include "qemu-common.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020034#include "qemu/cutils.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010035#include "ui/console.h"
Gerd Hoffmanna25f5452013-11-28 12:17:35 +010036#include "ui/input.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Daniel P. Berrange2ec78702018-01-17 16:47:15 +000038#ifndef WIN32
blueswir1511d2b12009-03-07 15:32:56 +000039#include "x_keymap.h"
Daniel P. Berrange2ec78702018-01-17 16:47:15 +000040#endif
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010041#include "sdl_zoom.h"
blueswir1511d2b12009-03-07 15:32:56 +000042
aliguori7d957bd2009-01-15 22:14:11 +000043static DisplayChangeListener *dcl;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +010044static DisplaySurface *surface;
Gerd Hoffmannfe91f362018-02-02 12:10:15 +010045static DisplayOptions *opts;
aliguori7d957bd2009-01-15 22:14:11 +000046static SDL_Surface *real_screen;
47static SDL_Surface *guest_screen = NULL;
bellard0f0b7262003-08-09 18:26:36 +000048static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000049static int last_vm_running;
Jan Kiszkaf9977892011-07-30 11:39:09 +020050static bool gui_saved_scaling;
51static int gui_saved_width;
52static int gui_saved_height;
bellard8e9c4af2004-04-28 19:33:40 +000053static int gui_saved_grab;
54static int gui_fullscreen;
55static int gui_key_modifier_pressed;
56static int gui_keysym;
bellard32ff25b2004-10-03 14:33:54 +000057static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
58static uint8_t modifiers_state[256];
bellard09b26c52006-04-12 21:09:08 +000059static SDL_Cursor *sdl_cursor_normal;
60static SDL_Cursor *sdl_cursor_hidden;
61static int absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +000062static int guest_cursor = 0;
63static int guest_x, guest_y;
Blue Swirl660f11b2009-07-31 21:16:51 +000064static SDL_Cursor *guest_sprite = NULL;
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010065static SDL_PixelFormat host_format;
66static int scaling_active = 0;
Anthony Liguori3af12c82010-03-10 09:42:58 -060067static Notifier mouse_mode_notifier;
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +010068static int idle_counter;
Daniel P. Berrange2ec78702018-01-17 16:47:15 +000069static const guint16 *keycode_map;
70static size_t keycode_maplen;
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +010071
72#define SDL_REFRESH_INTERVAL_BUSY 10
73#define SDL_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
74 / SDL_REFRESH_INTERVAL_BUSY + 1)
bellard0f0b7262003-08-09 18:26:36 +000075
Benjamin Herrenschmidt5f5d82d2014-07-07 15:10:12 +100076#if 0
77#define DEBUG_SDL
78#endif
79
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010080static void sdl_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010081 int x, int y, int w, int h)
bellard0f0b7262003-08-09 18:26:36 +000082{
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010083 SDL_Rect rec;
84 rec.x = x;
85 rec.y = y;
86 rec.w = w;
87 rec.h = h;
88
Benjamin Herrenschmidt5f5d82d2014-07-07 15:10:12 +100089#ifdef DEBUG_SDL
90 printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
91 x, y, w, h, scaling_active);
92#endif
93
aliguori7b5d76d2009-03-13 15:02:13 +000094 if (guest_screen) {
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010095 if (!scaling_active) {
96 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
97 } else {
98 if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) {
99 fprintf(stderr, "Zoom blit failed\n");
100 exit(1);
101 }
102 }
103 }
104 SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h);
bellard0f0b7262003-08-09 18:26:36 +0000105}
106
Jan Kiszka9510a482011-07-30 11:39:18 +0200107static void do_sdl_resize(int width, int height, int bpp)
bellard0f0b7262003-08-09 18:26:36 +0000108{
109 int flags;
Lei Li898ae282013-09-04 17:07:16 +0800110 SDL_Surface *tmp_screen;
bellard0f0b7262003-08-09 18:26:36 +0000111
Benjamin Herrenschmidt5f5d82d2014-07-07 15:10:12 +1000112#ifdef DEBUG_SDL
113 printf("SDL: Resizing to %dx%d bpp %d\n", width, height, bpp);
114#endif
bellard0f0b7262003-08-09 18:26:36 +0000115
Jan Kiszka91ada982011-07-30 11:39:05 +0200116 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
117 if (gui_fullscreen) {
bellard8e9c4af2004-04-28 19:33:40 +0000118 flags |= SDL_FULLSCREEN;
Jan Kiszka91ada982011-07-30 11:39:05 +0200119 } else {
120 flags |= SDL_RESIZABLE;
121 }
Gerd Hoffmann04ff1a32018-01-15 16:48:55 +0100122 if (no_frame) {
ths43523e92007-02-18 18:19:32 +0000123 flags |= SDL_NOFRAME;
Gerd Hoffmann04ff1a32018-01-15 16:48:55 +0100124 }
bellard9903da22005-10-30 23:19:10 +0000125
Lei Li898ae282013-09-04 17:07:16 +0800126 tmp_screen = SDL_SetVideoMode(width, height, bpp, flags);
aliguori7d957bd2009-01-15 22:14:11 +0000127 if (!real_screen) {
Lei Li898ae282013-09-04 17:07:16 +0800128 if (!tmp_screen) {
129 fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n",
130 width, height, bpp, SDL_GetError());
131 exit(1);
132 }
133 } else {
134 /*
135 * Revert to the previous video mode if the change of resizing or
136 * resolution failed.
137 */
138 if (!tmp_screen) {
139 fprintf(stderr, "Failed to set SDL display (%dx%dx%d): %s\n",
140 width, height, bpp, SDL_GetError());
141 return;
142 }
bellard0f0b7262003-08-09 18:26:36 +0000143 }
Lei Li898ae282013-09-04 17:07:16 +0800144
145 real_screen = tmp_screen;
aliguori7b5d76d2009-03-13 15:02:13 +0000146}
blueswir1749ecd92008-07-24 11:25:30 +0000147
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +0100148static void sdl_switch(DisplayChangeListener *dcl,
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100149 DisplaySurface *new_surface)
aliguori7b5d76d2009-03-13 15:02:13 +0000150{
Benjamin Herrenschmidtd28d6502014-07-07 15:11:22 +1000151 PixelFormat pf;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100152
153 /* temporary hack: allows to call sdl_switch to handle scaling changes */
154 if (new_surface) {
155 surface = new_surface;
aliguori7b5d76d2009-03-13 15:02:13 +0000156 }
Benjamin Herrenschmidtd28d6502014-07-07 15:11:22 +1000157 pf = qemu_pixelformat_from_pixman(surface->format);
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100158
159 if (!scaling_active) {
160 do_sdl_resize(surface_width(surface), surface_height(surface), 0);
161 } else if (real_screen->format->BitsPerPixel !=
162 surface_bits_per_pixel(surface)) {
163 do_sdl_resize(real_screen->w, real_screen->h,
164 surface_bits_per_pixel(surface));
165 }
166
167 if (guest_screen != NULL) {
168 SDL_FreeSurface(guest_screen);
169 }
Benjamin Herrenschmidt5f5d82d2014-07-07 15:10:12 +1000170
171#ifdef DEBUG_SDL
172 printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
173 pf.rmask, pf.gmask, pf.bmask, pf.amask);
174#endif
175
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100176 guest_screen = SDL_CreateRGBSurfaceFrom
177 (surface_data(surface),
178 surface_width(surface), surface_height(surface),
179 surface_bits_per_pixel(surface), surface_stride(surface),
Gerd Hoffmann30f1e662014-06-18 11:03:15 +0200180 pf.rmask, pf.gmask,
181 pf.bmask, pf.amask);
bellard0f0b7262003-08-09 18:26:36 +0000182}
183
Benjamin Herrenschmidt7dd93292014-07-07 17:24:42 +1000184static bool sdl_check_format(DisplayChangeListener *dcl,
185 pixman_format_code_t format)
186{
187 /*
188 * We let SDL convert for us a few more formats than,
189 * the native ones. Thes are the ones I have tested.
190 */
191 return (format == PIXMAN_x8r8g8b8 ||
192 format == PIXMAN_b8g8r8x8 ||
193 format == PIXMAN_x1r5g5b5 ||
194 format == PIXMAN_r5g6b5);
195}
196
bellard3d11d0e2004-12-12 16:56:30 +0000197/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +0000198
bellard3d11d0e2004-12-12 16:56:30 +0000199#include "sdl_keysym.h"
bellarde58d12e2004-07-05 22:13:07 +0000200
Anthony Liguoric227f092009-10-01 16:12:16 -0500201static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000202
bellard3d11d0e2004-12-12 16:56:30 +0000203static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000204{
Gerd Hoffmannabb4f2c2018-02-22 08:05:13 +0100205 bool shift = modifiers_state[0x2a] || modifiers_state[0x36];
206 bool altgr = modifiers_state[0xb8];
207 bool ctrl = modifiers_state[0x1d] || modifiers_state[0x9d];
bellard3d11d0e2004-12-12 16:56:30 +0000208 int keysym;
209 /* workaround for X11+SDL bug with AltGR */
210 keysym = ev->keysym.sym;
211 if (keysym == 0 && ev->keysym.scancode == 113)
212 keysym = SDLK_MODE;
bellard60659e32006-08-19 14:27:31 +0000213 /* For Japanese key '\' and '|' */
214 if (keysym == 92 && ev->keysym.scancode == 133) {
215 keysym = 0xa5;
216 }
Gerd Hoffmannabb4f2c2018-02-22 08:05:13 +0100217 return keysym2scancode(kbd_layout, keysym,
218 shift, altgr, ctrl) & SCANCODE_KEYMASK;
bellarde58d12e2004-07-05 22:13:07 +0000219}
220
bellard3d11d0e2004-12-12 16:56:30 +0000221
Daniel P. Berrange2ec78702018-01-17 16:47:15 +0000222static const guint16 *sdl_get_keymap(size_t *maplen)
bellarde58d12e2004-07-05 22:13:07 +0000223{
Daniel P. Berrange2ec78702018-01-17 16:47:15 +0000224#if defined(WIN32)
225 *maplen = qemu_input_map_atset1_to_qcode_len;
226 return qemu_input_map_atset1_to_qcode;
bellarde58d12e2004-07-05 22:13:07 +0000227#else
aliguori5368a422009-03-03 17:37:21 +0000228#if defined(SDL_VIDEO_DRIVER_X11)
aliguori5368a422009-03-03 17:37:21 +0000229 SDL_SysWMinfo info;
aliguori5368a422009-03-03 17:37:21 +0000230
231 SDL_VERSION(&info.version);
Daniel P. Berrange2ec78702018-01-17 16:47:15 +0000232 if (SDL_GetWMInfo(&info) > 0) {
233 return qemu_xkeymap_mapping_table(
234 info.info.x11.display, maplen);
Jan Kiszka229609d2009-06-27 09:59:40 +0200235 }
aliguori5368a422009-03-03 17:37:21 +0000236#endif
Daniel P. Berrange2ec78702018-01-17 16:47:15 +0000237 g_warning("Unsupported SDL video driver / platform.\n"
238 "Assuming Linux KBD scancodes, but probably wrong.\n"
239 "Please report to qemu-devel@nongnu.org\n"
240 "including the following information:\n"
241 "\n"
242 " - Operating system\n"
243 " - SDL video driver\n");
244 *maplen = qemu_input_map_xorgkbd_to_qcode_len;
245 return qemu_input_map_xorgkbd_to_qcode;
246#endif
247}
aliguori5368a422009-03-03 17:37:21 +0000248
bellarde58d12e2004-07-05 22:13:07 +0000249static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
250{
Daniel P. Berrangé8ea9c802018-02-01 18:00:33 +0000251 int qcode;
Daniel P. Berrange2ec78702018-01-17 16:47:15 +0000252 if (!keycode_map) {
253 return 0;
bellarde58d12e2004-07-05 22:13:07 +0000254 }
Daniel P. Berrange2ec78702018-01-17 16:47:15 +0000255 if (ev->keysym.scancode > keycode_maplen) {
256 return 0;
257 }
bellarde58d12e2004-07-05 22:13:07 +0000258
Daniel P. Berrangé8ea9c802018-02-01 18:00:33 +0000259 qcode = keycode_map[ev->keysym.scancode];
260
261 if (qcode > qemu_input_map_qcode_to_qnum_len) {
262 return 0;
263 }
264
265 return qemu_input_map_qcode_to_qnum[qcode];
Daniel P. Berrange2ec78702018-01-17 16:47:15 +0000266}
bellarde58d12e2004-07-05 22:13:07 +0000267
bellard32ff25b2004-10-03 14:33:54 +0000268static void reset_keys(void)
269{
270 int i;
271 for(i = 0; i < 256; i++) {
272 if (modifiers_state[i]) {
Gerd Hoffmanna25f5452013-11-28 12:17:35 +0100273 qemu_input_event_send_key_number(dcl->con, i, false);
bellard32ff25b2004-10-03 14:33:54 +0000274 modifiers_state[i] = 0;
275 }
276 }
277}
278
bellard0f0b7262003-08-09 18:26:36 +0000279static void sdl_process_key(SDL_KeyboardEvent *ev)
280{
Gerd Hoffmanna25f5452013-11-28 12:17:35 +0100281 int keycode;
bellardde2200d2004-06-04 13:15:06 +0000282
283 if (ev->keysym.sym == SDLK_PAUSE) {
284 /* specific case */
Gerd Hoffmanna25f5452013-11-28 12:17:35 +0100285 qemu_input_event_send_key_qcode(dcl->con, Q_KEY_CODE_PAUSE,
286 ev->type == SDL_KEYDOWN);
bellardde2200d2004-06-04 13:15:06 +0000287 return;
288 }
289
bellard3d11d0e2004-12-12 16:56:30 +0000290 if (kbd_layout) {
291 keycode = sdl_keyevent_to_keycode_generic(ev);
292 } else {
293 keycode = sdl_keyevent_to_keycode(ev);
294 }
bellardde2200d2004-06-04 13:15:06 +0000295
296 switch(keycode) {
297 case 0x00:
298 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000299 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000300 return;
301 case 0x2a: /* Left Shift */
302 case 0x36: /* Right Shift */
303 case 0x1d: /* Left CTRL */
304 case 0x9d: /* Right CTRL */
305 case 0x38: /* Left ALT */
306 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000307 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000308 modifiers_state[keycode] = 0;
309 else
310 modifiers_state[keycode] = 1;
311 break;
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100312#define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
313#if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
314 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
bellardde2200d2004-06-04 13:15:06 +0000315 case 0x45: /* num lock */
316 case 0x3a: /* caps lock */
317 /* SDL does not send the key up event, so we generate it */
Gerd Hoffmanna25f5452013-11-28 12:17:35 +0100318 qemu_input_event_send_key_number(dcl->con, keycode, true);
319 qemu_input_event_send_key_number(dcl->con, keycode, false);
bellardde2200d2004-06-04 13:15:06 +0000320 return;
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100321#endif
bellard0f0b7262003-08-09 18:26:36 +0000322 }
bellardde2200d2004-06-04 13:15:06 +0000323
324 /* now send the key code */
Gerd Hoffmanna25f5452013-11-28 12:17:35 +0100325 qemu_input_event_send_key_number(dcl->con, keycode,
326 ev->type == SDL_KEYDOWN);
bellard0f0b7262003-08-09 18:26:36 +0000327}
328
bellard8a7ddc32004-03-31 19:00:16 +0000329static void sdl_update_caption(void)
330{
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100331 char win_title[1024];
332 char icon_title[1024];
thsc35734b2007-03-19 15:17:08 +0000333 const char *status = "";
334
Luiz Capitulino13548692011-07-29 15:36:43 -0300335 if (!runstate_is_running())
thsc35734b2007-03-19 15:17:08 +0000336 status = " [Stopped]";
ths3780e192007-06-21 21:08:02 +0000337 else if (gui_grab) {
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500338 if (alt_grab)
Gerd Hoffmannf8d2c932018-01-15 16:48:54 +0100339 status = " - Press Ctrl-Alt-Shift-G to exit mouse grab";
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500340 else if (ctrl_grab)
Gerd Hoffmannf8d2c932018-01-15 16:48:54 +0100341 status = " - Press Right-Ctrl-G to exit mouse grab";
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500342 else
Gerd Hoffmannf8d2c932018-01-15 16:48:54 +0100343 status = " - Press Ctrl-Alt-G to exit mouse grab";
ths3780e192007-06-21 21:08:02 +0000344 }
thsc35734b2007-03-19 15:17:08 +0000345
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100346 if (qemu_name) {
347 snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
348 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
349 } else {
350 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
351 snprintf(icon_title, sizeof(icon_title), "QEMU");
352 }
thsc35734b2007-03-19 15:17:08 +0000353
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100354 SDL_WM_SetCaption(win_title, icon_title);
bellard8a7ddc32004-03-31 19:00:16 +0000355}
356
bellard09b26c52006-04-12 21:09:08 +0000357static void sdl_hide_cursor(void)
358{
balrog9467cd42007-05-01 01:34:14 +0000359 if (!cursor_hide)
360 return;
361
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100362 if (qemu_input_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000363 SDL_ShowCursor(1);
364 SDL_SetCursor(sdl_cursor_hidden);
365 } else {
366 SDL_ShowCursor(0);
367 }
bellard09b26c52006-04-12 21:09:08 +0000368}
369
370static void sdl_show_cursor(void)
371{
balrog9467cd42007-05-01 01:34:14 +0000372 if (!cursor_hide)
373 return;
374
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100375 if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL)) {
bellard8785a8d2006-06-13 10:49:12 +0000376 SDL_ShowCursor(1);
thsd34cab92007-04-02 01:10:46 +0000377 if (guest_cursor &&
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100378 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
thsd34cab92007-04-02 01:10:46 +0000379 SDL_SetCursor(guest_sprite);
380 else
381 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000382 }
383}
384
bellard0f0b7262003-08-09 18:26:36 +0000385static void sdl_grab_start(void)
386{
Jan Kiszka85f94f82012-01-31 13:45:28 +0100387 /*
388 * If the application is not active, do not try to enter grab state. This
389 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
390 * application (SDL bug).
391 */
392 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) {
393 return;
394 }
thsd34cab92007-04-02 01:10:46 +0000395 if (guest_cursor) {
396 SDL_SetCursor(guest_sprite);
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100397 if (!qemu_input_is_absolute() && !absolute_enabled) {
balrog08a2d4c2009-01-29 23:29:52 +0000398 SDL_WarpMouse(guest_x, guest_y);
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100399 }
thsd34cab92007-04-02 01:10:46 +0000400 } else
401 sdl_hide_cursor();
Jan Kiszkaeaa2e022012-01-31 13:45:29 +0100402 SDL_WM_GrabInput(SDL_GRAB_ON);
403 gui_grab = 1;
404 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000405}
406
407static void sdl_grab_end(void)
408{
bellard0f0b7262003-08-09 18:26:36 +0000409 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard0f0b7262003-08-09 18:26:36 +0000410 gui_grab = 0;
thsd34cab92007-04-02 01:10:46 +0000411 sdl_show_cursor();
bellard8a7ddc32004-03-31 19:00:16 +0000412 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000413}
414
Jan Kiszka66596352012-01-31 13:45:27 +0100415static void absolute_mouse_grab(void)
416{
417 int mouse_x, mouse_y;
418
Jan Kiszka85f94f82012-01-31 13:45:28 +0100419 SDL_GetMouseState(&mouse_x, &mouse_y);
420 if (mouse_x > 0 && mouse_x < real_screen->w - 1 &&
421 mouse_y > 0 && mouse_y < real_screen->h - 1) {
422 sdl_grab_start();
Jan Kiszka66596352012-01-31 13:45:27 +0100423 }
424}
425
Jan Kiszka9e8dd452011-06-20 14:06:26 +0200426static void sdl_mouse_mode_change(Notifier *notify, void *data)
Anthony Liguori3af12c82010-03-10 09:42:58 -0600427{
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100428 if (qemu_input_is_absolute()) {
Anthony Liguori3af12c82010-03-10 09:42:58 -0600429 if (!absolute_enabled) {
Anthony Liguori3af12c82010-03-10 09:42:58 -0600430 absolute_enabled = 1;
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100431 if (qemu_console_is_graphic(NULL)) {
Jan Kiszka66596352012-01-31 13:45:27 +0100432 absolute_mouse_grab();
433 }
Anthony Liguori3af12c82010-03-10 09:42:58 -0600434 }
435 } else if (absolute_enabled) {
Jan Kiszkaf8b8d632011-08-22 18:42:42 +0200436 if (!gui_fullscreen) {
437 sdl_grab_end();
438 }
Jan Kiszka35b0f232011-07-30 11:39:15 +0200439 absolute_enabled = 0;
Anthony Liguori3af12c82010-03-10 09:42:58 -0600440 }
441}
442
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100443static void sdl_send_mouse_event(int dx, int dy, int x, int y, int state)
bellard0f0b7262003-08-09 18:26:36 +0000444{
Eric Blake7fb1cf12015-11-18 01:52:57 -0700445 static uint32_t bmap[INPUT_BUTTON__MAX] = {
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100446 [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
447 [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
448 [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
Gerd Hoffmannf22d0af2016-01-12 12:14:12 +0100449 [INPUT_BUTTON_WHEEL_UP] = SDL_BUTTON(SDL_BUTTON_WHEELUP),
450 [INPUT_BUTTON_WHEEL_DOWN] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN),
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100451 };
452 static uint32_t prev_state;
Jan Kiszka9510a482011-07-30 11:39:18 +0200453
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100454 if (prev_state != state) {
455 qemu_input_update_buttons(dcl->con, bmap, prev_state, state);
456 prev_state = state;
Jan Kiszka9510a482011-07-30 11:39:18 +0200457 }
bellard09b26c52006-04-12 21:09:08 +0000458
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100459 if (qemu_input_is_absolute()) {
460 qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, x,
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200461 0, real_screen->w);
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100462 qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, y,
Philippe Voinov9cfa7ab2017-05-05 15:39:52 +0200463 0, real_screen->h);
Gerd Hoffmannc3aa84b2014-03-10 09:22:16 +0100464 } else {
465 if (guest_cursor) {
466 x -= guest_x;
467 y -= guest_y;
468 guest_x += x;
469 guest_y += y;
470 dx = x;
471 dy = y;
472 }
473 qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, dx);
474 qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, dy);
bellard09b26c52006-04-12 21:09:08 +0000475 }
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100476 qemu_input_event_sync();
bellard0f0b7262003-08-09 18:26:36 +0000477}
478
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100479static void sdl_scale(int width, int height)
Jan Kiszkaf9977892011-07-30 11:39:09 +0200480{
481 int bpp = real_screen->format->BitsPerPixel;
482
Benjamin Herrenschmidt5f5d82d2014-07-07 15:10:12 +1000483#ifdef DEBUG_SDL
484 printf("SDL: Scaling to %dx%d bpp %d\n", width, height, bpp);
485#endif
486
Jan Kiszkaf9977892011-07-30 11:39:09 +0200487 if (bpp != 16 && bpp != 32) {
488 bpp = 32;
489 }
490 do_sdl_resize(width, height, bpp);
491 scaling_active = 1;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200492}
493
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100494static void toggle_full_screen(void)
bellard8e9c4af2004-04-28 19:33:40 +0000495{
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100496 int width = surface_width(surface);
497 int height = surface_height(surface);
498 int bpp = surface_bits_per_pixel(surface);
499
bellard8e9c4af2004-04-28 19:33:40 +0000500 gui_fullscreen = !gui_fullscreen;
bellard8e9c4af2004-04-28 19:33:40 +0000501 if (gui_fullscreen) {
Jan Kiszkaf9977892011-07-30 11:39:09 +0200502 gui_saved_width = real_screen->w;
503 gui_saved_height = real_screen->h;
504 gui_saved_scaling = scaling_active;
505
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100506 do_sdl_resize(width, height, bpp);
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100507 scaling_active = 0;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200508
bellard8e9c4af2004-04-28 19:33:40 +0000509 gui_saved_grab = gui_grab;
510 sdl_grab_start();
511 } else {
Jan Kiszkaf9977892011-07-30 11:39:09 +0200512 if (gui_saved_scaling) {
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100513 sdl_scale(gui_saved_width, gui_saved_height);
Jan Kiszkaf9977892011-07-30 11:39:09 +0200514 } else {
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100515 do_sdl_resize(width, height, 0);
Jan Kiszkaf9977892011-07-30 11:39:09 +0200516 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100517 if (!gui_saved_grab || !qemu_console_is_graphic(NULL)) {
bellard8e9c4af2004-04-28 19:33:40 +0000518 sdl_grab_end();
Jan Kiszkaf8558102011-07-30 11:39:12 +0200519 }
bellard8e9c4af2004-04-28 19:33:40 +0000520 }
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100521 graphic_hw_invalidate(NULL);
522 graphic_hw_update(NULL);
bellard8e9c4af2004-04-28 19:33:40 +0000523}
524
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100525static void handle_keydown(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200526{
527 int mod_state;
528 int keycode;
529
530 if (alt_grab) {
531 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
532 (gui_grab_code | KMOD_LSHIFT);
533 } else if (ctrl_grab) {
534 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
535 } else {
536 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
537 }
538 gui_key_modifier_pressed = mod_state;
539
540 if (gui_key_modifier_pressed) {
541 keycode = sdl_keyevent_to_keycode(&ev->key);
542 switch (keycode) {
543 case 0x21: /* 'f' key on US keyboard */
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100544 toggle_full_screen();
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200545 gui_keysym = 1;
546 break;
Gerd Hoffmannf8d2c932018-01-15 16:48:54 +0100547 case 0x22: /* 'g' key */
548 if (!gui_grab) {
549 if (qemu_console_is_graphic(NULL)) {
550 sdl_grab_start();
551 }
552 } else if (!gui_fullscreen) {
553 sdl_grab_end();
554 }
555 gui_keysym = 1;
556 break;
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200557 case 0x16: /* 'u' key on US keyboard */
558 if (scaling_active) {
559 scaling_active = 0;
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100560 sdl_switch(dcl, NULL);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100561 graphic_hw_invalidate(NULL);
562 graphic_hw_update(NULL);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200563 }
564 gui_keysym = 1;
565 break;
566 case 0x02 ... 0x0a: /* '1' to '9' keys */
567 /* Reset the modifiers sent to the current console */
568 reset_keys();
569 console_select(keycode - 0x02);
570 gui_keysym = 1;
571 if (gui_fullscreen) {
572 break;
573 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100574 if (!qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200575 /* release grab if going to a text console */
576 if (gui_grab) {
577 sdl_grab_end();
578 } else if (absolute_enabled) {
579 sdl_show_cursor();
580 }
581 } else if (absolute_enabled) {
582 sdl_hide_cursor();
583 absolute_mouse_grab();
584 }
585 break;
586 case 0x1b: /* '+' */
587 case 0x35: /* '-' */
588 if (!gui_fullscreen) {
589 int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50),
590 160);
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100591 int height = (surface_height(surface) * width) /
592 surface_width(surface);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200593
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100594 sdl_scale(width, height);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100595 graphic_hw_invalidate(NULL);
596 graphic_hw_update(NULL);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200597 gui_keysym = 1;
598 }
599 default:
600 break;
601 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100602 } else if (!qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200603 int keysym = 0;
604
605 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
606 switch (ev->key.keysym.sym) {
607 case SDLK_UP:
608 keysym = QEMU_KEY_CTRL_UP;
609 break;
610 case SDLK_DOWN:
611 keysym = QEMU_KEY_CTRL_DOWN;
612 break;
613 case SDLK_LEFT:
614 keysym = QEMU_KEY_CTRL_LEFT;
615 break;
616 case SDLK_RIGHT:
617 keysym = QEMU_KEY_CTRL_RIGHT;
618 break;
619 case SDLK_HOME:
620 keysym = QEMU_KEY_CTRL_HOME;
621 break;
622 case SDLK_END:
623 keysym = QEMU_KEY_CTRL_END;
624 break;
625 case SDLK_PAGEUP:
626 keysym = QEMU_KEY_CTRL_PAGEUP;
627 break;
628 case SDLK_PAGEDOWN:
629 keysym = QEMU_KEY_CTRL_PAGEDOWN;
630 break;
631 default:
632 break;
633 }
634 } else {
635 switch (ev->key.keysym.sym) {
636 case SDLK_UP:
637 keysym = QEMU_KEY_UP;
638 break;
639 case SDLK_DOWN:
640 keysym = QEMU_KEY_DOWN;
641 break;
642 case SDLK_LEFT:
643 keysym = QEMU_KEY_LEFT;
644 break;
645 case SDLK_RIGHT:
646 keysym = QEMU_KEY_RIGHT;
647 break;
648 case SDLK_HOME:
649 keysym = QEMU_KEY_HOME;
650 break;
651 case SDLK_END:
652 keysym = QEMU_KEY_END;
653 break;
654 case SDLK_PAGEUP:
655 keysym = QEMU_KEY_PAGEUP;
656 break;
657 case SDLK_PAGEDOWN:
658 keysym = QEMU_KEY_PAGEDOWN;
659 break;
660 case SDLK_BACKSPACE:
661 keysym = QEMU_KEY_BACKSPACE;
662 break;
663 case SDLK_DELETE:
664 keysym = QEMU_KEY_DELETE;
665 break;
666 default:
667 break;
668 }
669 }
670 if (keysym) {
671 kbd_put_keysym(keysym);
672 } else if (ev->key.keysym.unicode != 0) {
673 kbd_put_keysym(ev->key.keysym.unicode);
674 }
675 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100676 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200677 sdl_process_key(&ev->key);
678 }
679}
680
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100681static void handle_keyup(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200682{
683 int mod_state;
684
685 if (!alt_grab) {
686 mod_state = (ev->key.keysym.mod & gui_grab_code);
687 } else {
688 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
689 }
690 if (!mod_state && gui_key_modifier_pressed) {
691 gui_key_modifier_pressed = 0;
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200692 gui_keysym = 0;
693 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100694 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200695 sdl_process_key(&ev->key);
696 }
697}
698
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100699static void handle_mousemotion(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200700{
701 int max_x, max_y;
702
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100703 if (qemu_console_is_graphic(NULL) &&
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100704 (qemu_input_is_absolute() || absolute_enabled)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200705 max_x = real_screen->w - 1;
706 max_y = real_screen->h - 1;
707 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
708 ev->motion.x == max_x || ev->motion.y == max_y)) {
709 sdl_grab_end();
710 }
Jan Kiszka85f94f82012-01-31 13:45:28 +0100711 if (!gui_grab &&
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200712 (ev->motion.x > 0 && ev->motion.x < max_x &&
713 ev->motion.y > 0 && ev->motion.y < max_y)) {
714 sdl_grab_start();
715 }
716 }
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100717 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
718 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel,
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200719 ev->motion.x, ev->motion.y, ev->motion.state);
720 }
721}
722
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100723static void handle_mousebutton(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200724{
725 int buttonstate = SDL_GetMouseState(NULL, NULL);
726 SDL_MouseButtonEvent *bev;
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200727
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100728 if (!qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200729 return;
730 }
731
732 bev = &ev->button;
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100733 if (!gui_grab && !qemu_input_is_absolute()) {
Jan Kiszka822f98d2012-01-31 13:45:30 +0100734 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200735 /* start grabbing all events */
736 sdl_grab_start();
737 }
738 } else {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200739 if (ev->type == SDL_MOUSEBUTTONDOWN) {
740 buttonstate |= SDL_BUTTON(bev->button);
741 } else {
742 buttonstate &= ~SDL_BUTTON(bev->button);
743 }
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100744 sdl_send_mouse_event(0, 0, bev->x, bev->y, buttonstate);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200745 }
746}
747
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100748static void handle_activation(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200749{
Jan Kiszka02df4d62012-01-31 13:45:31 +0100750#ifdef _WIN32
751 /* Disable grab if the window no longer has the focus
752 * (Windows-only workaround) */
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200753 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
754 !ev->active.gain && !gui_fullscreen) {
755 sdl_grab_end();
756 }
Jan Kiszka02df4d62012-01-31 13:45:31 +0100757#endif
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100758 if (!gui_grab && ev->active.gain && qemu_console_is_graphic(NULL) &&
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100759 (qemu_input_is_absolute() || absolute_enabled)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200760 absolute_mouse_grab();
761 }
762 if (ev->active.state & SDL_APPACTIVE) {
763 if (ev->active.gain) {
764 /* Back to default interval */
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100765 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200766 } else {
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100767 /* Sleeping interval. Not using the long default here as
768 * sdl_refresh does not only update the guest screen, but
769 * also checks for gui events. */
770 update_displaychangelistener(dcl, 500);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200771 }
772 }
773}
774
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100775static void sdl_refresh(DisplayChangeListener *dcl)
bellard0f0b7262003-08-09 18:26:36 +0000776{
777 SDL_Event ev1, *ev = &ev1;
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100778 bool allow_close = true;
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100779 int idle = 1;
ths3b46e622007-09-17 08:09:54 +0000780
Luiz Capitulino13548692011-07-29 15:36:43 -0300781 if (last_vm_running != runstate_is_running()) {
782 last_vm_running = runstate_is_running();
bellard8a7ddc32004-03-31 19:00:16 +0000783 sdl_update_caption();
784 }
785
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100786 graphic_hw_update(NULL);
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100787 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL));
bellard457831f2004-07-14 17:22:33 +0000788
bellard0f0b7262003-08-09 18:26:36 +0000789 while (SDL_PollEvent(ev)) {
790 switch (ev->type) {
791 case SDL_VIDEOEXPOSE:
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100792 sdl_update(dcl, 0, 0, real_screen->w, real_screen->h);
bellard0f0b7262003-08-09 18:26:36 +0000793 break;
794 case SDL_KEYDOWN:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100795 idle = 0;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100796 handle_keydown(ev);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200797 break;
bellard0f0b7262003-08-09 18:26:36 +0000798 case SDL_KEYUP:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100799 idle = 0;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100800 handle_keyup(ev);
bellard0f0b7262003-08-09 18:26:36 +0000801 break;
802 case SDL_QUIT:
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100803 if (opts->has_window_close && !opts->window_close) {
804 allow_close = false;
805 }
806 if (allow_close) {
Jan Kiszka941f5112011-07-30 11:39:04 +0200807 no_shutdown = 0;
Eric Blakecf83f142017-05-15 16:41:13 -0500808 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
Jan Kiszka941f5112011-07-30 11:39:04 +0200809 }
bellard0f0b7262003-08-09 18:26:36 +0000810 break;
811 case SDL_MOUSEMOTION:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100812 idle = 0;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100813 handle_mousemotion(ev);
bellard0f0b7262003-08-09 18:26:36 +0000814 break;
815 case SDL_MOUSEBUTTONDOWN:
816 case SDL_MOUSEBUTTONUP:
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100817 idle = 0;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100818 handle_mousebutton(ev);
bellard0f0b7262003-08-09 18:26:36 +0000819 break;
bellard0294ffb2004-04-29 22:15:15 +0000820 case SDL_ACTIVEEVENT:
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100821 handle_activation(ev);
bellard0294ffb2004-04-29 22:15:15 +0000822 break;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200823 case SDL_VIDEORESIZE:
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100824 sdl_scale(ev->resize.w, ev->resize.h);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100825 graphic_hw_invalidate(NULL);
826 graphic_hw_update(NULL);
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100827 break;
bellard0f0b7262003-08-09 18:26:36 +0000828 default:
829 break;
830 }
831 }
Jindřich Makovička56bdd4b2016-01-12 20:18:24 +0100832
833 if (idle) {
834 if (idle_counter < SDL_MAX_IDLE_COUNT) {
835 idle_counter++;
836 if (idle_counter >= SDL_MAX_IDLE_COUNT) {
837 dcl->update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
838 }
839 }
840 } else {
841 idle_counter = 0;
842 dcl->update_interval = SDL_REFRESH_INTERVAL_BUSY;
843 }
bellard0f0b7262003-08-09 18:26:36 +0000844}
845
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100846static void sdl_mouse_warp(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100847 int x, int y, int on)
thsd34cab92007-04-02 01:10:46 +0000848{
849 if (on) {
850 if (!guest_cursor)
851 sdl_show_cursor();
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100852 if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
thsd34cab92007-04-02 01:10:46 +0000853 SDL_SetCursor(guest_sprite);
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100854 if (!qemu_input_is_absolute() && !absolute_enabled) {
balrog08a2d4c2009-01-29 23:29:52 +0000855 SDL_WarpMouse(x, y);
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100856 }
thsd34cab92007-04-02 01:10:46 +0000857 }
858 } else if (gui_grab)
859 sdl_hide_cursor();
860 guest_cursor = on;
861 guest_x = x, guest_y = y;
862}
863
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100864static void sdl_mouse_define(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100865 QEMUCursor *c)
thsd34cab92007-04-02 01:10:46 +0000866{
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200867 uint8_t *image, *mask;
868 int bpl;
869
thsd34cab92007-04-02 01:10:46 +0000870 if (guest_sprite)
871 SDL_FreeCursor(guest_sprite);
872
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200873 bpl = cursor_get_mono_bpl(c);
Anthony Liguori7267c092011-08-20 22:09:37 -0500874 image = g_malloc0(bpl * c->height);
875 mask = g_malloc0(bpl * c->height);
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200876 cursor_get_mono_image(c, 0x000000, image);
877 cursor_get_mono_mask(c, 0, mask);
878 guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
879 c->hot_x, c->hot_y);
Anthony Liguori7267c092011-08-20 22:09:37 -0500880 g_free(image);
881 g_free(mask);
thsd34cab92007-04-02 01:10:46 +0000882
883 if (guest_cursor &&
Gerd Hoffmann3ab193e2013-11-28 12:27:40 +0100884 (gui_grab || qemu_input_is_absolute() || absolute_enabled))
thsd34cab92007-04-02 01:10:46 +0000885 SDL_SetCursor(guest_sprite);
886}
887
Anthony Liguori28695482010-03-21 14:13:02 -0500888static void sdl_cleanup(void)
bellard898712a2004-02-06 19:56:42 +0000889{
thsd34cab92007-04-02 01:10:46 +0000890 if (guest_sprite)
891 SDL_FreeCursor(guest_sprite);
malcd8ee7662009-05-17 18:26:05 +0400892 SDL_QuitSubSystem(SDL_INIT_VIDEO);
bellard898712a2004-02-06 19:56:42 +0000893}
894
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100895static const DisplayChangeListenerOps dcl_ops = {
Benjamin Herrenschmidt7dd93292014-07-07 17:24:42 +1000896 .dpy_name = "sdl",
897 .dpy_gfx_update = sdl_update,
898 .dpy_gfx_switch = sdl_switch,
899 .dpy_gfx_check_format = sdl_check_format,
900 .dpy_refresh = sdl_refresh,
901 .dpy_mouse_set = sdl_mouse_warp,
902 .dpy_cursor_define = sdl_mouse_define,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100903};
904
Gerd Hoffmann5ee17182018-03-01 11:05:36 +0100905static void sdl1_display_init(DisplayState *ds, DisplayOptions *o)
bellard0f0b7262003-08-09 18:26:36 +0000906{
907 int flags;
bellard09b26c52006-04-12 21:09:08 +0000908 uint8_t data = 0;
aliguori7b5d76d2009-03-13 15:02:13 +0000909 const SDL_VideoInfo *vi;
Samuel Thibaultf29b3432016-12-21 01:38:05 +0100910 SDL_SysWMinfo info;
Stefan Weil09cec712011-02-16 21:15:40 +0100911 char *filename;
bellard0f0b7262003-08-09 18:26:36 +0000912
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100913 assert(o->type == DISPLAY_TYPE_SDL);
914 opts = o;
bellard3d11d0e2004-12-12 16:56:30 +0000915#if defined(__APPLE__)
916 /* always use generic keymaps */
917 if (!keyboard_layout)
918 keyboard_layout = "en-us";
919#endif
920 if(keyboard_layout) {
Fei Liab4f9312018-10-17 10:26:50 +0200921 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout,
922 &error_fatal);
bellard3d11d0e2004-12-12 16:56:30 +0000923 }
924
Daniel P. Berrangee52c6ba2018-01-15 14:25:33 +0000925 g_printerr("Running QEMU with SDL 1.2 is deprecated, and will be removed\n"
926 "in a future release. Please switch to SDL 2.0 instead\n");
927
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100928 if (opts->has_full_screen && opts->full_screen) {
Jan Kiszka111f8ec2010-05-23 10:29:34 +0200929 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
930 }
Michael Tokarev1de97562011-05-08 01:18:30 +0400931#ifdef __linux__
932 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
933 * accessible $DISPLAY to open X11 window. This is often the case
934 * when qemu is run using sudo. But in this case, and when actually
935 * run in X11 environment, SDL fights with X11 for the video card,
936 * making current display unavailable, often until reboot.
937 * So make x11 the default SDL video driver if this variable is unset.
938 * This is a bit hackish but saves us from bigger problem.
939 * Maybe it's a good idea to fix this in SDL instead.
940 */
941 setenv("SDL_VIDEODRIVER", "x11", 0);
942#endif
Jan Kiszka111f8ec2010-05-23 10:29:34 +0200943
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100944 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
945 * This requires SDL >= 1.2.14. */
946 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
947
bellard0f0b7262003-08-09 18:26:36 +0000948 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
949 if (SDL_Init (flags)) {
malc3caf2562009-12-30 04:26:34 +0300950 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
951 SDL_GetError());
bellard0f0b7262003-08-09 18:26:36 +0000952 exit(1);
953 }
aliguori7b5d76d2009-03-13 15:02:13 +0000954 vi = SDL_GetVideoInfo();
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100955 host_format = *(vi->vfmt);
bellard0ae04d72003-09-30 21:09:16 +0000956
Daniel P. Berrange2ec78702018-01-17 16:47:15 +0000957 keycode_map = sdl_get_keymap(&keycode_maplen);
958
Stefan Weil09cec712011-02-16 21:15:40 +0100959 /* Load a 32x32x4 image. White pixels are transparent. */
960 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
961 if (filename) {
962 SDL_Surface *image = SDL_LoadBMP(filename);
963 if (image) {
964 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
965 SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
966 SDL_WM_SetIcon(image, NULL);
967 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500968 g_free(filename);
Stefan Weil09cec712011-02-16 21:15:40 +0100969 }
970
Gerd Hoffmannfe91f362018-02-02 12:10:15 +0100971 if (opts->has_full_screen && opts->full_screen) {
Jan Kiszka110defd2011-07-30 11:39:11 +0200972 gui_fullscreen = 1;
973 sdl_grab_start();
974 }
975
Markus Armbrusterfedf0d32015-11-03 17:12:03 +0100976 dcl = g_new0(DisplayChangeListener, 1);
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100977 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +0200978 register_displaychangelistener(dcl);
bellard0f0b7262003-08-09 18:26:36 +0000979
Anthony Liguori3af12c82010-03-10 09:42:58 -0600980 mouse_mode_notifier.notify = sdl_mouse_mode_change;
981 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
982
bellard8a7ddc32004-03-31 19:00:16 +0000983 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000984 SDL_EnableKeyRepeat(250, 50);
985 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000986
bellard09b26c52006-04-12 21:09:08 +0000987 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
988 sdl_cursor_normal = SDL_GetCursor();
989
Samuel Thibaultf29b3432016-12-21 01:38:05 +0100990 memset(&info, 0, sizeof(info));
991 SDL_VERSION(&info.version);
992 if (SDL_GetWMInfo(&info)) {
993 int i;
994 for (i = 0; ; i++) {
995 /* All consoles share the same window */
996 QemuConsole *con = qemu_console_lookup_by_index(i);
997 if (con) {
998#if defined(SDL_VIDEO_DRIVER_X11)
999 qemu_console_set_window_id(con, info.info.x11.wmwindow);
1000#elif defined(SDL_VIDEO_DRIVER_NANOX) || \
1001 defined(SDL_VIDEO_DRIVER_WINDIB) || defined(SDL_VIDEO_DRIVER_DDRAW) || \
1002 defined(SDL_VIDEO_DRIVER_GAPI) || \
1003 defined(SDL_VIDEO_DRIVER_RISCOS)
1004 qemu_console_set_window_id(con, (int) (uintptr_t) info.window);
1005#else
1006 qemu_console_set_window_id(con, info.data);
1007#endif
1008 } else {
1009 break;
1010 }
1011 }
1012 }
1013
Anthony Liguori28695482010-03-21 14:13:02 -05001014 atexit(sdl_cleanup);
bellard0f0b7262003-08-09 18:26:36 +00001015}
Gerd Hoffmann5ee17182018-03-01 11:05:36 +01001016
1017static QemuDisplay qemu_display_sdl1 = {
1018 .type = DISPLAY_TYPE_SDL,
1019 .init = sdl1_display_init,
1020};
1021
1022static void register_sdl1(void)
1023{
1024 qemu_display_register(&qemu_display_sdl1);
1025}
1026
1027type_init(register_sdl1);