blob: 9d8583c4e6aacf44046c973655e7ee37b762a1db [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
bellard0f0b7262003-08-09 18:26:36 +000028#include <SDL.h>
aliguoric9985aa2009-03-08 15:04:07 +000029#include <SDL_syswm.h>
bellard0f0b7262003-08-09 18:26:36 +000030
blueswir1511d2b12009-03-07 15:32:56 +000031#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010032#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
blueswir1511d2b12009-03-07 15:32:56 +000034#include "x_keymap.h"
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010035#include "sdl_zoom.h"
blueswir1511d2b12009-03-07 15:32:56 +000036
aliguori7d957bd2009-01-15 22:14:11 +000037static DisplayChangeListener *dcl;
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +010038static DisplaySurface *surface;
aliguori7d957bd2009-01-15 22:14:11 +000039static SDL_Surface *real_screen;
40static SDL_Surface *guest_screen = NULL;
bellard0f0b7262003-08-09 18:26:36 +000041static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000042static int last_vm_running;
Jan Kiszkaf9977892011-07-30 11:39:09 +020043static bool gui_saved_scaling;
44static int gui_saved_width;
45static int gui_saved_height;
bellard8e9c4af2004-04-28 19:33:40 +000046static int gui_saved_grab;
47static int gui_fullscreen;
ths43523e92007-02-18 18:19:32 +000048static int gui_noframe;
bellard8e9c4af2004-04-28 19:33:40 +000049static int gui_key_modifier_pressed;
50static int gui_keysym;
bellard32ff25b2004-10-03 14:33:54 +000051static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
52static uint8_t modifiers_state[256];
bellard09b26c52006-04-12 21:09:08 +000053static SDL_Cursor *sdl_cursor_normal;
54static SDL_Cursor *sdl_cursor_hidden;
55static int absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +000056static int guest_cursor = 0;
57static int guest_x, guest_y;
Blue Swirl660f11b2009-07-31 21:16:51 +000058static SDL_Cursor *guest_sprite = NULL;
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010059static SDL_PixelFormat host_format;
60static int scaling_active = 0;
Anthony Liguori3af12c82010-03-10 09:42:58 -060061static Notifier mouse_mode_notifier;
bellard0f0b7262003-08-09 18:26:36 +000062
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010063static void sdl_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +010064 int x, int y, int w, int h)
bellard0f0b7262003-08-09 18:26:36 +000065{
bellard898712a2004-02-06 19:56:42 +000066 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010067 SDL_Rect rec;
68 rec.x = x;
69 rec.y = y;
70 rec.w = w;
71 rec.h = h;
72
aliguori7b5d76d2009-03-13 15:02:13 +000073 if (guest_screen) {
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010074 if (!scaling_active) {
75 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
76 } else {
77 if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) {
78 fprintf(stderr, "Zoom blit failed\n");
79 exit(1);
80 }
81 }
82 }
83 SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h);
bellard0f0b7262003-08-09 18:26:36 +000084}
85
Jan Kiszka9510a482011-07-30 11:39:18 +020086static void do_sdl_resize(int width, int height, int bpp)
bellard0f0b7262003-08-09 18:26:36 +000087{
88 int flags;
Lei Li898ae282013-09-04 17:07:16 +080089 SDL_Surface *tmp_screen;
bellard0f0b7262003-08-09 18:26:36 +000090
91 // printf("resizing to %d %d\n", w, h);
92
Jan Kiszka91ada982011-07-30 11:39:05 +020093 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
94 if (gui_fullscreen) {
bellard8e9c4af2004-04-28 19:33:40 +000095 flags |= SDL_FULLSCREEN;
Jan Kiszka91ada982011-07-30 11:39:05 +020096 } else {
97 flags |= SDL_RESIZABLE;
98 }
ths43523e92007-02-18 18:19:32 +000099 if (gui_noframe)
100 flags |= SDL_NOFRAME;
bellard9903da22005-10-30 23:19:10 +0000101
Lei Li898ae282013-09-04 17:07:16 +0800102 tmp_screen = SDL_SetVideoMode(width, height, bpp, flags);
aliguori7d957bd2009-01-15 22:14:11 +0000103 if (!real_screen) {
Lei Li898ae282013-09-04 17:07:16 +0800104 if (!tmp_screen) {
105 fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n",
106 width, height, bpp, SDL_GetError());
107 exit(1);
108 }
109 } else {
110 /*
111 * Revert to the previous video mode if the change of resizing or
112 * resolution failed.
113 */
114 if (!tmp_screen) {
115 fprintf(stderr, "Failed to set SDL display (%dx%dx%d): %s\n",
116 width, height, bpp, SDL_GetError());
117 return;
118 }
bellard0f0b7262003-08-09 18:26:36 +0000119 }
Lei Li898ae282013-09-04 17:07:16 +0800120
121 real_screen = tmp_screen;
aliguori7b5d76d2009-03-13 15:02:13 +0000122}
blueswir1749ecd92008-07-24 11:25:30 +0000123
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +0100124static void sdl_switch(DisplayChangeListener *dcl,
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100125 DisplaySurface *new_surface)
aliguori7b5d76d2009-03-13 15:02:13 +0000126{
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100127
128 /* temporary hack: allows to call sdl_switch to handle scaling changes */
129 if (new_surface) {
130 surface = new_surface;
aliguori7b5d76d2009-03-13 15:02:13 +0000131 }
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100132
133 if (!scaling_active) {
134 do_sdl_resize(surface_width(surface), surface_height(surface), 0);
135 } else if (real_screen->format->BitsPerPixel !=
136 surface_bits_per_pixel(surface)) {
137 do_sdl_resize(real_screen->w, real_screen->h,
138 surface_bits_per_pixel(surface));
139 }
140
141 if (guest_screen != NULL) {
142 SDL_FreeSurface(guest_screen);
143 }
144 guest_screen = SDL_CreateRGBSurfaceFrom
145 (surface_data(surface),
146 surface_width(surface), surface_height(surface),
147 surface_bits_per_pixel(surface), surface_stride(surface),
148 surface->pf.rmask, surface->pf.gmask,
149 surface->pf.bmask, surface->pf.amask);
bellard0f0b7262003-08-09 18:26:36 +0000150}
151
bellard3d11d0e2004-12-12 16:56:30 +0000152/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +0000153
bellard3d11d0e2004-12-12 16:56:30 +0000154#include "sdl_keysym.h"
bellarde58d12e2004-07-05 22:13:07 +0000155
Anthony Liguoric227f092009-10-01 16:12:16 -0500156static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000157
bellard3d11d0e2004-12-12 16:56:30 +0000158static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000159{
bellard3d11d0e2004-12-12 16:56:30 +0000160 int keysym;
161 /* workaround for X11+SDL bug with AltGR */
162 keysym = ev->keysym.sym;
163 if (keysym == 0 && ev->keysym.scancode == 113)
164 keysym = SDLK_MODE;
bellard60659e32006-08-19 14:27:31 +0000165 /* For Japanese key '\' and '|' */
166 if (keysym == 92 && ev->keysym.scancode == 133) {
167 keysym = 0xa5;
168 }
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100169 return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
bellarde58d12e2004-07-05 22:13:07 +0000170}
171
bellard3d11d0e2004-12-12 16:56:30 +0000172/* specific keyboard conversions from scan codes */
173
174#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000175
176static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
177{
178 return ev->keysym.scancode;
179}
180
181#else
182
aliguori5368a422009-03-03 17:37:21 +0000183#if defined(SDL_VIDEO_DRIVER_X11)
184#include <X11/XKBlib.h>
185
186static int check_for_evdev(void)
187{
188 SDL_SysWMinfo info;
Jan Kiszka229609d2009-06-27 09:59:40 +0200189 XkbDescPtr desc = NULL;
aliguori5368a422009-03-03 17:37:21 +0000190 int has_evdev = 0;
Jan Kiszka229609d2009-06-27 09:59:40 +0200191 char *keycodes = NULL;
aliguori5368a422009-03-03 17:37:21 +0000192
193 SDL_VERSION(&info.version);
Jan Kiszka229609d2009-06-27 09:59:40 +0200194 if (!SDL_GetWMInfo(&info)) {
aliguori5368a422009-03-03 17:37:21 +0000195 return 0;
Jan Kiszka229609d2009-06-27 09:59:40 +0200196 }
aliguori5368a422009-03-03 17:37:21 +0000197 desc = XkbGetKeyboard(info.info.x11.display,
198 XkbGBN_AllComponentsMask,
199 XkbUseCoreKbd);
Jan Kiszka229609d2009-06-27 09:59:40 +0200200 if (desc && desc->names) {
201 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
202 if (keycodes == NULL) {
203 fprintf(stderr, "could not lookup keycode name\n");
204 } else if (strstart(keycodes, "evdev", NULL)) {
205 has_evdev = 1;
206 } else if (!strstart(keycodes, "xfree86", NULL)) {
207 fprintf(stderr, "unknown keycodes `%s', please report to "
208 "qemu-devel@nongnu.org\n", keycodes);
209 }
210 }
aliguori5368a422009-03-03 17:37:21 +0000211
Jan Kiszka229609d2009-06-27 09:59:40 +0200212 if (desc) {
213 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
214 }
215 if (keycodes) {
216 XFree(keycodes);
217 }
aliguori5368a422009-03-03 17:37:21 +0000218 return has_evdev;
219}
220#else
221static int check_for_evdev(void)
222{
223 return 0;
224}
225#endif
226
bellarde58d12e2004-07-05 22:13:07 +0000227static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
228{
229 int keycode;
aliguori5368a422009-03-03 17:37:21 +0000230 static int has_evdev = -1;
231
232 if (has_evdev == -1)
233 has_evdev = check_for_evdev();
bellarde58d12e2004-07-05 22:13:07 +0000234
235 keycode = ev->keysym.scancode;
236
237 if (keycode < 9) {
238 keycode = 0;
239 } else if (keycode < 97) {
240 keycode -= 8; /* just an offset */
aliguori5368a422009-03-03 17:37:21 +0000241 } else if (keycode < 158) {
bellarde58d12e2004-07-05 22:13:07 +0000242 /* use conversion table */
aliguori5368a422009-03-03 17:37:21 +0000243 if (has_evdev)
244 keycode = translate_evdev_keycode(keycode - 97);
245 else
246 keycode = translate_xfree86_keycode(keycode - 97);
247 } else if (keycode == 208) { /* Hiragana_Katakana */
248 keycode = 0x70;
249 } else if (keycode == 211) { /* backslash */
250 keycode = 0x73;
bellarde58d12e2004-07-05 22:13:07 +0000251 } else {
252 keycode = 0;
253 }
254 return keycode;
255}
256
257#endif
258
bellard32ff25b2004-10-03 14:33:54 +0000259static void reset_keys(void)
260{
261 int i;
262 for(i = 0; i < 256; i++) {
263 if (modifiers_state[i]) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100264 if (i & SCANCODE_GREY)
265 kbd_put_keycode(SCANCODE_EMUL0);
266 kbd_put_keycode(i | SCANCODE_UP);
bellard32ff25b2004-10-03 14:33:54 +0000267 modifiers_state[i] = 0;
268 }
269 }
270}
271
bellard0f0b7262003-08-09 18:26:36 +0000272static void sdl_process_key(SDL_KeyboardEvent *ev)
273{
bellard32ff25b2004-10-03 14:33:54 +0000274 int keycode, v;
bellardde2200d2004-06-04 13:15:06 +0000275
276 if (ev->keysym.sym == SDLK_PAUSE) {
277 /* specific case */
278 v = 0;
279 if (ev->type == SDL_KEYUP)
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100280 v |= SCANCODE_UP;
bellardde2200d2004-06-04 13:15:06 +0000281 kbd_put_keycode(0xe1);
282 kbd_put_keycode(0x1d | v);
283 kbd_put_keycode(0x45 | v);
284 return;
285 }
286
bellard3d11d0e2004-12-12 16:56:30 +0000287 if (kbd_layout) {
288 keycode = sdl_keyevent_to_keycode_generic(ev);
289 } else {
290 keycode = sdl_keyevent_to_keycode(ev);
291 }
bellardde2200d2004-06-04 13:15:06 +0000292
293 switch(keycode) {
294 case 0x00:
295 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000296 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000297 return;
298 case 0x2a: /* Left Shift */
299 case 0x36: /* Right Shift */
300 case 0x1d: /* Left CTRL */
301 case 0x9d: /* Right CTRL */
302 case 0x38: /* Left ALT */
303 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000304 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000305 modifiers_state[keycode] = 0;
306 else
307 modifiers_state[keycode] = 1;
308 break;
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100309#define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
310#if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
311 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
bellardde2200d2004-06-04 13:15:06 +0000312 case 0x45: /* num lock */
313 case 0x3a: /* caps lock */
314 /* SDL does not send the key up event, so we generate it */
315 kbd_put_keycode(keycode);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100316 kbd_put_keycode(keycode | SCANCODE_UP);
bellardde2200d2004-06-04 13:15:06 +0000317 return;
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100318#endif
bellard0f0b7262003-08-09 18:26:36 +0000319 }
bellardde2200d2004-06-04 13:15:06 +0000320
321 /* now send the key code */
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100322 if (keycode & SCANCODE_GREY)
323 kbd_put_keycode(SCANCODE_EMUL0);
bellardde2200d2004-06-04 13:15:06 +0000324 if (ev->type == SDL_KEYUP)
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100325 kbd_put_keycode(keycode | SCANCODE_UP);
bellardde2200d2004-06-04 13:15:06 +0000326 else
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100327 kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
bellard0f0b7262003-08-09 18:26:36 +0000328}
329
bellard8a7ddc32004-03-31 19:00:16 +0000330static void sdl_update_caption(void)
331{
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100332 char win_title[1024];
333 char icon_title[1024];
thsc35734b2007-03-19 15:17:08 +0000334 const char *status = "";
335
Luiz Capitulino13548692011-07-29 15:36:43 -0300336 if (!runstate_is_running())
thsc35734b2007-03-19 15:17:08 +0000337 status = " [Stopped]";
ths3780e192007-06-21 21:08:02 +0000338 else if (gui_grab) {
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500339 if (alt_grab)
Anthony Liguori4e75b342010-03-01 08:47:28 -0600340 status = " - Press Ctrl-Alt-Shift to exit mouse grab";
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500341 else if (ctrl_grab)
Anthony Liguori4e75b342010-03-01 08:47:28 -0600342 status = " - Press Right-Ctrl to exit mouse grab";
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500343 else
Anthony Liguori4e75b342010-03-01 08:47:28 -0600344 status = " - Press Ctrl-Alt to exit mouse grab";
ths3780e192007-06-21 21:08:02 +0000345 }
thsc35734b2007-03-19 15:17:08 +0000346
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100347 if (qemu_name) {
348 snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
349 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
350 } else {
351 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
352 snprintf(icon_title, sizeof(icon_title), "QEMU");
353 }
thsc35734b2007-03-19 15:17:08 +0000354
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100355 SDL_WM_SetCaption(win_title, icon_title);
bellard8a7ddc32004-03-31 19:00:16 +0000356}
357
bellard09b26c52006-04-12 21:09:08 +0000358static void sdl_hide_cursor(void)
359{
balrog9467cd42007-05-01 01:34:14 +0000360 if (!cursor_hide)
361 return;
362
bellard8785a8d2006-06-13 10:49:12 +0000363 if (kbd_mouse_is_absolute()) {
364 SDL_ShowCursor(1);
365 SDL_SetCursor(sdl_cursor_hidden);
366 } else {
367 SDL_ShowCursor(0);
368 }
bellard09b26c52006-04-12 21:09:08 +0000369}
370
371static void sdl_show_cursor(void)
372{
balrog9467cd42007-05-01 01:34:14 +0000373 if (!cursor_hide)
374 return;
375
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100376 if (!kbd_mouse_is_absolute() || !qemu_console_is_graphic(NULL)) {
bellard8785a8d2006-06-13 10:49:12 +0000377 SDL_ShowCursor(1);
thsd34cab92007-04-02 01:10:46 +0000378 if (guest_cursor &&
379 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
380 SDL_SetCursor(guest_sprite);
381 else
382 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000383 }
384}
385
bellard0f0b7262003-08-09 18:26:36 +0000386static void sdl_grab_start(void)
387{
Jan Kiszka85f94f82012-01-31 13:45:28 +0100388 /*
389 * If the application is not active, do not try to enter grab state. This
390 * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
391 * application (SDL bug).
392 */
393 if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) {
394 return;
395 }
thsd34cab92007-04-02 01:10:46 +0000396 if (guest_cursor) {
397 SDL_SetCursor(guest_sprite);
balrog08a2d4c2009-01-29 23:29:52 +0000398 if (!kbd_mouse_is_absolute() && !absolute_enabled)
399 SDL_WarpMouse(guest_x, guest_y);
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{
428 if (kbd_mouse_is_absolute()) {
429 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
aurel324c44bdc2008-03-13 19:20:18 +0000443static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
bellard0f0b7262003-08-09 18:26:36 +0000444{
Jan Kiszka9510a482011-07-30 11:39:18 +0200445 int buttons = 0;
446
447 if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) {
bellard0f0b7262003-08-09 18:26:36 +0000448 buttons |= MOUSE_EVENT_LBUTTON;
Jan Kiszka9510a482011-07-30 11:39:18 +0200449 }
450 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
bellard0f0b7262003-08-09 18:26:36 +0000451 buttons |= MOUSE_EVENT_RBUTTON;
Jan Kiszka9510a482011-07-30 11:39:18 +0200452 }
453 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) {
bellard0f0b7262003-08-09 18:26:36 +0000454 buttons |= MOUSE_EVENT_MBUTTON;
Jan Kiszka9510a482011-07-30 11:39:18 +0200455 }
bellard09b26c52006-04-12 21:09:08 +0000456
457 if (kbd_mouse_is_absolute()) {
Jan Kiszka9510a482011-07-30 11:39:18 +0200458 dx = x * 0x7FFF / (real_screen->w - 1);
459 dy = y * 0x7FFF / (real_screen->h - 1);
thsd34cab92007-04-02 01:10:46 +0000460 } else if (guest_cursor) {
aurel324c44bdc2008-03-13 19:20:18 +0000461 x -= guest_x;
462 y -= guest_y;
463 guest_x += x;
464 guest_y += y;
465 dx = x;
466 dy = y;
bellard09b26c52006-04-12 21:09:08 +0000467 }
468
bellard0f0b7262003-08-09 18:26:36 +0000469 kbd_mouse_event(dx, dy, dz, buttons);
470}
471
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100472static void sdl_scale(int width, int height)
Jan Kiszkaf9977892011-07-30 11:39:09 +0200473{
474 int bpp = real_screen->format->BitsPerPixel;
475
476 if (bpp != 16 && bpp != 32) {
477 bpp = 32;
478 }
479 do_sdl_resize(width, height, bpp);
480 scaling_active = 1;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200481}
482
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100483static void toggle_full_screen(void)
bellard8e9c4af2004-04-28 19:33:40 +0000484{
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100485 int width = surface_width(surface);
486 int height = surface_height(surface);
487 int bpp = surface_bits_per_pixel(surface);
488
bellard8e9c4af2004-04-28 19:33:40 +0000489 gui_fullscreen = !gui_fullscreen;
bellard8e9c4af2004-04-28 19:33:40 +0000490 if (gui_fullscreen) {
Jan Kiszkaf9977892011-07-30 11:39:09 +0200491 gui_saved_width = real_screen->w;
492 gui_saved_height = real_screen->h;
493 gui_saved_scaling = scaling_active;
494
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100495 do_sdl_resize(width, height, bpp);
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100496 scaling_active = 0;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200497
bellard8e9c4af2004-04-28 19:33:40 +0000498 gui_saved_grab = gui_grab;
499 sdl_grab_start();
500 } else {
Jan Kiszkaf9977892011-07-30 11:39:09 +0200501 if (gui_saved_scaling) {
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100502 sdl_scale(gui_saved_width, gui_saved_height);
Jan Kiszkaf9977892011-07-30 11:39:09 +0200503 } else {
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100504 do_sdl_resize(width, height, 0);
Jan Kiszkaf9977892011-07-30 11:39:09 +0200505 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100506 if (!gui_saved_grab || !qemu_console_is_graphic(NULL)) {
bellard8e9c4af2004-04-28 19:33:40 +0000507 sdl_grab_end();
Jan Kiszkaf8558102011-07-30 11:39:12 +0200508 }
bellard8e9c4af2004-04-28 19:33:40 +0000509 }
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100510 graphic_hw_invalidate(NULL);
511 graphic_hw_update(NULL);
bellard8e9c4af2004-04-28 19:33:40 +0000512}
513
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100514static void handle_keydown(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200515{
516 int mod_state;
517 int keycode;
518
519 if (alt_grab) {
520 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
521 (gui_grab_code | KMOD_LSHIFT);
522 } else if (ctrl_grab) {
523 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
524 } else {
525 mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
526 }
527 gui_key_modifier_pressed = mod_state;
528
529 if (gui_key_modifier_pressed) {
530 keycode = sdl_keyevent_to_keycode(&ev->key);
531 switch (keycode) {
532 case 0x21: /* 'f' key on US keyboard */
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100533 toggle_full_screen();
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200534 gui_keysym = 1;
535 break;
536 case 0x16: /* 'u' key on US keyboard */
537 if (scaling_active) {
538 scaling_active = 0;
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100539 sdl_switch(dcl, NULL);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100540 graphic_hw_invalidate(NULL);
541 graphic_hw_update(NULL);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200542 }
543 gui_keysym = 1;
544 break;
545 case 0x02 ... 0x0a: /* '1' to '9' keys */
546 /* Reset the modifiers sent to the current console */
547 reset_keys();
548 console_select(keycode - 0x02);
549 gui_keysym = 1;
550 if (gui_fullscreen) {
551 break;
552 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100553 if (!qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200554 /* release grab if going to a text console */
555 if (gui_grab) {
556 sdl_grab_end();
557 } else if (absolute_enabled) {
558 sdl_show_cursor();
559 }
560 } else if (absolute_enabled) {
561 sdl_hide_cursor();
562 absolute_mouse_grab();
563 }
564 break;
565 case 0x1b: /* '+' */
566 case 0x35: /* '-' */
567 if (!gui_fullscreen) {
568 int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50),
569 160);
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100570 int height = (surface_height(surface) * width) /
571 surface_width(surface);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200572
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100573 sdl_scale(width, height);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100574 graphic_hw_invalidate(NULL);
575 graphic_hw_update(NULL);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200576 gui_keysym = 1;
577 }
578 default:
579 break;
580 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100581 } else if (!qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200582 int keysym = 0;
583
584 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
585 switch (ev->key.keysym.sym) {
586 case SDLK_UP:
587 keysym = QEMU_KEY_CTRL_UP;
588 break;
589 case SDLK_DOWN:
590 keysym = QEMU_KEY_CTRL_DOWN;
591 break;
592 case SDLK_LEFT:
593 keysym = QEMU_KEY_CTRL_LEFT;
594 break;
595 case SDLK_RIGHT:
596 keysym = QEMU_KEY_CTRL_RIGHT;
597 break;
598 case SDLK_HOME:
599 keysym = QEMU_KEY_CTRL_HOME;
600 break;
601 case SDLK_END:
602 keysym = QEMU_KEY_CTRL_END;
603 break;
604 case SDLK_PAGEUP:
605 keysym = QEMU_KEY_CTRL_PAGEUP;
606 break;
607 case SDLK_PAGEDOWN:
608 keysym = QEMU_KEY_CTRL_PAGEDOWN;
609 break;
610 default:
611 break;
612 }
613 } else {
614 switch (ev->key.keysym.sym) {
615 case SDLK_UP:
616 keysym = QEMU_KEY_UP;
617 break;
618 case SDLK_DOWN:
619 keysym = QEMU_KEY_DOWN;
620 break;
621 case SDLK_LEFT:
622 keysym = QEMU_KEY_LEFT;
623 break;
624 case SDLK_RIGHT:
625 keysym = QEMU_KEY_RIGHT;
626 break;
627 case SDLK_HOME:
628 keysym = QEMU_KEY_HOME;
629 break;
630 case SDLK_END:
631 keysym = QEMU_KEY_END;
632 break;
633 case SDLK_PAGEUP:
634 keysym = QEMU_KEY_PAGEUP;
635 break;
636 case SDLK_PAGEDOWN:
637 keysym = QEMU_KEY_PAGEDOWN;
638 break;
639 case SDLK_BACKSPACE:
640 keysym = QEMU_KEY_BACKSPACE;
641 break;
642 case SDLK_DELETE:
643 keysym = QEMU_KEY_DELETE;
644 break;
645 default:
646 break;
647 }
648 }
649 if (keysym) {
650 kbd_put_keysym(keysym);
651 } else if (ev->key.keysym.unicode != 0) {
652 kbd_put_keysym(ev->key.keysym.unicode);
653 }
654 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100655 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200656 sdl_process_key(&ev->key);
657 }
658}
659
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100660static void handle_keyup(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200661{
662 int mod_state;
663
664 if (!alt_grab) {
665 mod_state = (ev->key.keysym.mod & gui_grab_code);
666 } else {
667 mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
668 }
669 if (!mod_state && gui_key_modifier_pressed) {
670 gui_key_modifier_pressed = 0;
671 if (gui_keysym == 0) {
672 /* exit/enter grab if pressing Ctrl-Alt */
673 if (!gui_grab) {
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100674 if (qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200675 sdl_grab_start();
676 }
677 } else if (!gui_fullscreen) {
678 sdl_grab_end();
679 }
680 /* SDL does not send back all the modifiers key, so we must
681 * correct it. */
682 reset_keys();
683 return;
684 }
685 gui_keysym = 0;
686 }
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100687 if (qemu_console_is_graphic(NULL) && !gui_keysym) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200688 sdl_process_key(&ev->key);
689 }
690}
691
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100692static void handle_mousemotion(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200693{
694 int max_x, max_y;
695
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100696 if (qemu_console_is_graphic(NULL) &&
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200697 (kbd_mouse_is_absolute() || absolute_enabled)) {
698 max_x = real_screen->w - 1;
699 max_y = real_screen->h - 1;
700 if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
701 ev->motion.x == max_x || ev->motion.y == max_y)) {
702 sdl_grab_end();
703 }
Jan Kiszka85f94f82012-01-31 13:45:28 +0100704 if (!gui_grab &&
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200705 (ev->motion.x > 0 && ev->motion.x < max_x &&
706 ev->motion.y > 0 && ev->motion.y < max_y)) {
707 sdl_grab_start();
708 }
709 }
710 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
711 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
712 ev->motion.x, ev->motion.y, ev->motion.state);
713 }
714}
715
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100716static void handle_mousebutton(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200717{
718 int buttonstate = SDL_GetMouseState(NULL, NULL);
719 SDL_MouseButtonEvent *bev;
720 int dz;
721
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100722 if (!qemu_console_is_graphic(NULL)) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200723 return;
724 }
725
726 bev = &ev->button;
727 if (!gui_grab && !kbd_mouse_is_absolute()) {
Jan Kiszka822f98d2012-01-31 13:45:30 +0100728 if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200729 /* start grabbing all events */
730 sdl_grab_start();
731 }
732 } else {
733 dz = 0;
734 if (ev->type == SDL_MOUSEBUTTONDOWN) {
735 buttonstate |= SDL_BUTTON(bev->button);
736 } else {
737 buttonstate &= ~SDL_BUTTON(bev->button);
738 }
739#ifdef SDL_BUTTON_WHEELUP
740 if (bev->button == SDL_BUTTON_WHEELUP &&
741 ev->type == SDL_MOUSEBUTTONDOWN) {
742 dz = -1;
743 } else if (bev->button == SDL_BUTTON_WHEELDOWN &&
744 ev->type == SDL_MOUSEBUTTONDOWN) {
745 dz = 1;
746 }
747#endif
748 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
749 }
750}
751
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100752static void handle_activation(SDL_Event *ev)
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200753{
Jan Kiszka02df4d62012-01-31 13:45:31 +0100754#ifdef _WIN32
755 /* Disable grab if the window no longer has the focus
756 * (Windows-only workaround) */
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200757 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
758 !ev->active.gain && !gui_fullscreen) {
759 sdl_grab_end();
760 }
Jan Kiszka02df4d62012-01-31 13:45:31 +0100761#endif
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100762 if (!gui_grab && ev->active.gain && qemu_console_is_graphic(NULL) &&
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200763 (kbd_mouse_is_absolute() || absolute_enabled)) {
764 absolute_mouse_grab();
765 }
766 if (ev->active.state & SDL_APPACTIVE) {
767 if (ev->active.gain) {
768 /* Back to default interval */
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100769 update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200770 } else {
Gerd Hoffmann0f7b2862013-03-14 11:56:16 +0100771 /* Sleeping interval. Not using the long default here as
772 * sdl_refresh does not only update the guest screen, but
773 * also checks for gui events. */
774 update_displaychangelistener(dcl, 500);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200775 }
776 }
777}
778
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100779static void sdl_refresh(DisplayChangeListener *dcl)
bellard0f0b7262003-08-09 18:26:36 +0000780{
781 SDL_Event ev1, *ev = &ev1;
ths3b46e622007-09-17 08:09:54 +0000782
Luiz Capitulino13548692011-07-29 15:36:43 -0300783 if (last_vm_running != runstate_is_running()) {
784 last_vm_running = runstate_is_running();
bellard8a7ddc32004-03-31 19:00:16 +0000785 sdl_update_caption();
786 }
787
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100788 graphic_hw_update(NULL);
Gerd Hoffmann81c0d5a2013-03-14 14:27:08 +0100789 SDL_EnableUNICODE(!qemu_console_is_graphic(NULL));
bellard457831f2004-07-14 17:22:33 +0000790
bellard0f0b7262003-08-09 18:26:36 +0000791 while (SDL_PollEvent(ev)) {
792 switch (ev->type) {
793 case SDL_VIDEOEXPOSE:
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100794 sdl_update(dcl, 0, 0, real_screen->w, real_screen->h);
bellard0f0b7262003-08-09 18:26:36 +0000795 break;
796 case SDL_KEYDOWN:
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100797 handle_keydown(ev);
Jan Kiszka1ae1caf2011-07-30 11:39:17 +0200798 break;
bellard0f0b7262003-08-09 18:26:36 +0000799 case SDL_KEYUP:
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100800 handle_keyup(ev);
bellard0f0b7262003-08-09 18:26:36 +0000801 break;
802 case SDL_QUIT:
Jan Kiszka941f5112011-07-30 11:39:04 +0200803 if (!no_quit) {
804 no_shutdown = 0;
balrog731345e2007-06-21 17:56:50 +0000805 qemu_system_shutdown_request();
Jan Kiszka941f5112011-07-30 11:39:04 +0200806 }
bellard0f0b7262003-08-09 18:26:36 +0000807 break;
808 case SDL_MOUSEMOTION:
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100809 handle_mousemotion(ev);
bellard0f0b7262003-08-09 18:26:36 +0000810 break;
811 case SDL_MOUSEBUTTONDOWN:
812 case SDL_MOUSEBUTTONUP:
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100813 handle_mousebutton(ev);
bellard0f0b7262003-08-09 18:26:36 +0000814 break;
bellard0294ffb2004-04-29 22:15:15 +0000815 case SDL_ACTIVEEVENT:
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100816 handle_activation(ev);
bellard0294ffb2004-04-29 22:15:15 +0000817 break;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200818 case SDL_VIDEORESIZE:
Gerd Hoffmann8db9bae2013-03-01 09:01:13 +0100819 sdl_scale(ev->resize.w, ev->resize.h);
Gerd Hoffmann1dbfa002013-03-12 13:44:38 +0100820 graphic_hw_invalidate(NULL);
821 graphic_hw_update(NULL);
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100822 break;
bellard0f0b7262003-08-09 18:26:36 +0000823 default:
824 break;
825 }
826 }
827}
828
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100829static void sdl_mouse_warp(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100830 int x, int y, int on)
thsd34cab92007-04-02 01:10:46 +0000831{
832 if (on) {
833 if (!guest_cursor)
834 sdl_show_cursor();
835 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
836 SDL_SetCursor(guest_sprite);
balrog08a2d4c2009-01-29 23:29:52 +0000837 if (!kbd_mouse_is_absolute() && !absolute_enabled)
838 SDL_WarpMouse(x, y);
thsd34cab92007-04-02 01:10:46 +0000839 }
840 } else if (gui_grab)
841 sdl_hide_cursor();
842 guest_cursor = on;
843 guest_x = x, guest_y = y;
844}
845
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100846static void sdl_mouse_define(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100847 QEMUCursor *c)
thsd34cab92007-04-02 01:10:46 +0000848{
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200849 uint8_t *image, *mask;
850 int bpl;
851
thsd34cab92007-04-02 01:10:46 +0000852 if (guest_sprite)
853 SDL_FreeCursor(guest_sprite);
854
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200855 bpl = cursor_get_mono_bpl(c);
Anthony Liguori7267c092011-08-20 22:09:37 -0500856 image = g_malloc0(bpl * c->height);
857 mask = g_malloc0(bpl * c->height);
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200858 cursor_get_mono_image(c, 0x000000, image);
859 cursor_get_mono_mask(c, 0, mask);
860 guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
861 c->hot_x, c->hot_y);
Anthony Liguori7267c092011-08-20 22:09:37 -0500862 g_free(image);
863 g_free(mask);
thsd34cab92007-04-02 01:10:46 +0000864
865 if (guest_cursor &&
866 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
867 SDL_SetCursor(guest_sprite);
868}
869
Anthony Liguori28695482010-03-21 14:13:02 -0500870static void sdl_cleanup(void)
bellard898712a2004-02-06 19:56:42 +0000871{
thsd34cab92007-04-02 01:10:46 +0000872 if (guest_sprite)
873 SDL_FreeCursor(guest_sprite);
malcd8ee7662009-05-17 18:26:05 +0400874 SDL_QuitSubSystem(SDL_INIT_VIDEO);
bellard898712a2004-02-06 19:56:42 +0000875}
876
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100877static const DisplayChangeListenerOps dcl_ops = {
878 .dpy_name = "sdl",
879 .dpy_gfx_update = sdl_update,
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +0100880 .dpy_gfx_switch = sdl_switch,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100881 .dpy_refresh = sdl_refresh,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100882 .dpy_mouse_set = sdl_mouse_warp,
883 .dpy_cursor_define = sdl_mouse_define,
884};
885
ths43523e92007-02-18 18:19:32 +0000886void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
bellard0f0b7262003-08-09 18:26:36 +0000887{
888 int flags;
bellard09b26c52006-04-12 21:09:08 +0000889 uint8_t data = 0;
aliguori7b5d76d2009-03-13 15:02:13 +0000890 const SDL_VideoInfo *vi;
Stefan Weil09cec712011-02-16 21:15:40 +0100891 char *filename;
bellard0f0b7262003-08-09 18:26:36 +0000892
bellard3d11d0e2004-12-12 16:56:30 +0000893#if defined(__APPLE__)
894 /* always use generic keymaps */
895 if (!keyboard_layout)
896 keyboard_layout = "en-us";
897#endif
898 if(keyboard_layout) {
aliguori04837552009-03-06 20:27:10 +0000899 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
bellard3d11d0e2004-12-12 16:56:30 +0000900 if (!kbd_layout)
901 exit(1);
902 }
903
ths43523e92007-02-18 18:19:32 +0000904 if (no_frame)
905 gui_noframe = 1;
906
Jan Kiszka111f8ec2010-05-23 10:29:34 +0200907 if (!full_screen) {
908 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
909 }
Michael Tokarev1de97562011-05-08 01:18:30 +0400910#ifdef __linux__
911 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
912 * accessible $DISPLAY to open X11 window. This is often the case
913 * when qemu is run using sudo. But in this case, and when actually
914 * run in X11 environment, SDL fights with X11 for the video card,
915 * making current display unavailable, often until reboot.
916 * So make x11 the default SDL video driver if this variable is unset.
917 * This is a bit hackish but saves us from bigger problem.
918 * Maybe it's a good idea to fix this in SDL instead.
919 */
920 setenv("SDL_VIDEODRIVER", "x11", 0);
921#endif
Jan Kiszka111f8ec2010-05-23 10:29:34 +0200922
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100923 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
924 * This requires SDL >= 1.2.14. */
925 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
926
bellard0f0b7262003-08-09 18:26:36 +0000927 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
928 if (SDL_Init (flags)) {
malc3caf2562009-12-30 04:26:34 +0300929 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
930 SDL_GetError());
bellard0f0b7262003-08-09 18:26:36 +0000931 exit(1);
932 }
aliguori7b5d76d2009-03-13 15:02:13 +0000933 vi = SDL_GetVideoInfo();
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100934 host_format = *(vi->vfmt);
bellard0ae04d72003-09-30 21:09:16 +0000935
Stefan Weil09cec712011-02-16 21:15:40 +0100936 /* Load a 32x32x4 image. White pixels are transparent. */
937 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
938 if (filename) {
939 SDL_Surface *image = SDL_LoadBMP(filename);
940 if (image) {
941 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
942 SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
943 SDL_WM_SetIcon(image, NULL);
944 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500945 g_free(filename);
Stefan Weil09cec712011-02-16 21:15:40 +0100946 }
947
Jan Kiszka110defd2011-07-30 11:39:11 +0200948 if (full_screen) {
949 gui_fullscreen = 1;
950 sdl_grab_start();
951 }
952
Anthony Liguori7267c092011-08-20 22:09:37 -0500953 dcl = g_malloc0(sizeof(DisplayChangeListener));
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100954 dcl->ops = &dcl_ops;
Gerd Hoffmann52090892013-04-23 15:44:31 +0200955 register_displaychangelistener(dcl);
bellard0f0b7262003-08-09 18:26:36 +0000956
Anthony Liguori3af12c82010-03-10 09:42:58 -0600957 mouse_mode_notifier.notify = sdl_mouse_mode_change;
958 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
959
bellard8a7ddc32004-03-31 19:00:16 +0000960 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000961 SDL_EnableKeyRepeat(250, 50);
962 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000963
bellard09b26c52006-04-12 21:09:08 +0000964 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
965 sdl_cursor_normal = SDL_GetCursor();
966
Anthony Liguori28695482010-03-21 14:13:02 -0500967 atexit(sdl_cleanup);
bellard0f0b7262003-08-09 18:26:36 +0000968}