blob: e18c59ac4e8b64d34b55eeb139f426092fa4431e [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"
32#include "console.h"
33#include "sysemu.h"
34#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;
38static SDL_Surface *real_screen;
39static SDL_Surface *guest_screen = NULL;
bellard0f0b7262003-08-09 18:26:36 +000040static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000041static int last_vm_running;
Jan Kiszkaf9977892011-07-30 11:39:09 +020042static bool gui_saved_scaling;
43static int gui_saved_width;
44static int gui_saved_height;
bellard8e9c4af2004-04-28 19:33:40 +000045static int gui_saved_grab;
46static int gui_fullscreen;
ths43523e92007-02-18 18:19:32 +000047static int gui_noframe;
bellard8e9c4af2004-04-28 19:33:40 +000048static int gui_key_modifier_pressed;
49static int gui_keysym;
bellardd63d3072004-10-03 13:29:03 +000050static int gui_fullscreen_initial_grab;
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 int width, height;
54static SDL_Cursor *sdl_cursor_normal;
55static SDL_Cursor *sdl_cursor_hidden;
56static int absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +000057static int guest_cursor = 0;
58static int guest_x, guest_y;
Blue Swirl660f11b2009-07-31 21:16:51 +000059static SDL_Cursor *guest_sprite = NULL;
aliguori7b5d76d2009-03-13 15:02:13 +000060static uint8_t allocator;
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010061static SDL_PixelFormat host_format;
62static int scaling_active = 0;
Anthony Liguori3af12c82010-03-10 09:42:58 -060063static Notifier mouse_mode_notifier;
bellard0f0b7262003-08-09 18:26:36 +000064
65static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
66{
bellard898712a2004-02-06 19:56:42 +000067 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010068 SDL_Rect rec;
69 rec.x = x;
70 rec.y = y;
71 rec.w = w;
72 rec.h = h;
73
aliguori7b5d76d2009-03-13 15:02:13 +000074 if (guest_screen) {
Stefano Stabellinic18a2c32009-06-24 11:58:25 +010075 if (!scaling_active) {
76 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
77 } else {
78 if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) {
79 fprintf(stderr, "Zoom blit failed\n");
80 exit(1);
81 }
82 }
83 }
84 SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h);
bellard0f0b7262003-08-09 18:26:36 +000085}
86
aliguori7d957bd2009-01-15 22:14:11 +000087static void sdl_setdata(DisplayState *ds)
88{
aliguori7d957bd2009-01-15 22:14:11 +000089 if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
90
91 guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds),
92 ds_get_bits_per_pixel(ds), ds_get_linesize(ds),
93 ds->surface->pf.rmask, ds->surface->pf.gmask,
94 ds->surface->pf.bmask, ds->surface->pf.amask);
95}
96
balrog649c9072009-03-21 01:09:16 +000097static void do_sdl_resize(int new_width, int new_height, int bpp)
bellard0f0b7262003-08-09 18:26:36 +000098{
99 int flags;
100
101 // printf("resizing to %d %d\n", w, h);
102
Jan Kiszka91ada982011-07-30 11:39:05 +0200103 flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
104 if (gui_fullscreen) {
bellard8e9c4af2004-04-28 19:33:40 +0000105 flags |= SDL_FULLSCREEN;
Jan Kiszka91ada982011-07-30 11:39:05 +0200106 } else {
107 flags |= SDL_RESIZABLE;
108 }
ths43523e92007-02-18 18:19:32 +0000109 if (gui_noframe)
110 flags |= SDL_NOFRAME;
bellard9903da22005-10-30 23:19:10 +0000111
balrog649c9072009-03-21 01:09:16 +0000112 width = new_width;
113 height = new_height;
aliguori7b5d76d2009-03-13 15:02:13 +0000114 real_screen = SDL_SetVideoMode(width, height, bpp, flags);
aliguori7d957bd2009-01-15 22:14:11 +0000115 if (!real_screen) {
Bjørn Morkb6034a32010-03-08 13:07:14 +0100116 fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n", width,
117 height, bpp, SDL_GetError());
bellard0f0b7262003-08-09 18:26:36 +0000118 exit(1);
119 }
aliguori7b5d76d2009-03-13 15:02:13 +0000120}
blueswir1749ecd92008-07-24 11:25:30 +0000121
aliguori7b5d76d2009-03-13 15:02:13 +0000122static void sdl_resize(DisplayState *ds)
123{
124 if (!allocator) {
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100125 if (!scaling_active)
126 do_sdl_resize(ds_get_width(ds), ds_get_height(ds), 0);
127 else if (real_screen->format->BitsPerPixel != ds_get_bits_per_pixel(ds))
128 do_sdl_resize(real_screen->w, real_screen->h, ds_get_bits_per_pixel(ds));
aliguori7b5d76d2009-03-13 15:02:13 +0000129 sdl_setdata(ds);
130 } else {
131 if (guest_screen != NULL) {
132 SDL_FreeSurface(guest_screen);
133 guest_screen = NULL;
134 }
135 }
136}
137
138static PixelFormat sdl_to_qemu_pixelformat(SDL_PixelFormat *sdl_pf)
139{
140 PixelFormat qemu_pf;
141
142 memset(&qemu_pf, 0x00, sizeof(PixelFormat));
143
144 qemu_pf.bits_per_pixel = sdl_pf->BitsPerPixel;
145 qemu_pf.bytes_per_pixel = sdl_pf->BytesPerPixel;
146 qemu_pf.depth = (qemu_pf.bits_per_pixel) == 32 ? 24 : (qemu_pf.bits_per_pixel);
147
148 qemu_pf.rmask = sdl_pf->Rmask;
149 qemu_pf.gmask = sdl_pf->Gmask;
150 qemu_pf.bmask = sdl_pf->Bmask;
151 qemu_pf.amask = sdl_pf->Amask;
152
153 qemu_pf.rshift = sdl_pf->Rshift;
154 qemu_pf.gshift = sdl_pf->Gshift;
155 qemu_pf.bshift = sdl_pf->Bshift;
156 qemu_pf.ashift = sdl_pf->Ashift;
157
158 qemu_pf.rbits = 8 - sdl_pf->Rloss;
159 qemu_pf.gbits = 8 - sdl_pf->Gloss;
160 qemu_pf.bbits = 8 - sdl_pf->Bloss;
161 qemu_pf.abits = 8 - sdl_pf->Aloss;
162
163 qemu_pf.rmax = ((1 << qemu_pf.rbits) - 1);
164 qemu_pf.gmax = ((1 << qemu_pf.gbits) - 1);
165 qemu_pf.bmax = ((1 << qemu_pf.bbits) - 1);
166 qemu_pf.amax = ((1 << qemu_pf.abits) - 1);
167
168 return qemu_pf;
169}
170
171static DisplaySurface* sdl_create_displaysurface(int width, int height)
172{
173 DisplaySurface *surface = (DisplaySurface*) qemu_mallocz(sizeof(DisplaySurface));
174 if (surface == NULL) {
175 fprintf(stderr, "sdl_create_displaysurface: malloc failed\n");
176 exit(1);
177 }
178
179 surface->width = width;
180 surface->height = height;
181
Jes Sorensenffe8b822011-03-16 13:33:30 +0100182 if (scaling_active) {
183 int linesize;
184 PixelFormat pf;
185 if (host_format.BytesPerPixel != 2 && host_format.BytesPerPixel != 4) {
186 linesize = width * 4;
187 pf = qemu_default_pixelformat(32);
188 } else {
189 linesize = width * host_format.BytesPerPixel;
190 pf = sdl_to_qemu_pixelformat(&host_format);
191 }
192 qemu_alloc_display(surface, width, height, linesize, pf, 0);
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100193 return surface;
194 }
195
196 if (host_format.BitsPerPixel == 16)
aliguori7b5d76d2009-03-13 15:02:13 +0000197 do_sdl_resize(width, height, 16);
198 else
199 do_sdl_resize(width, height, 32);
200
201 surface->pf = sdl_to_qemu_pixelformat(real_screen->format);
202 surface->linesize = real_screen->pitch;
203 surface->data = real_screen->pixels;
204
Juan Quintelae2542fe2009-07-27 16:13:06 +0200205#ifdef HOST_WORDS_BIGENDIAN
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100206 surface->flags = QEMU_REALPIXELS_FLAG | QEMU_BIG_ENDIAN_FLAG;
aliguori7b5d76d2009-03-13 15:02:13 +0000207#else
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100208 surface->flags = QEMU_REALPIXELS_FLAG;
aliguori7b5d76d2009-03-13 15:02:13 +0000209#endif
210 allocator = 1;
211
212 return surface;
213}
214
215static void sdl_free_displaysurface(DisplaySurface *surface)
216{
217 allocator = 0;
218 if (surface == NULL)
219 return;
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100220
221 if (surface->flags & QEMU_ALLOCATED_FLAG)
222 qemu_free(surface->data);
aliguori7b5d76d2009-03-13 15:02:13 +0000223 qemu_free(surface);
224}
225
226static DisplaySurface* sdl_resize_displaysurface(DisplaySurface *surface, int width, int height)
227{
228 sdl_free_displaysurface(surface);
229 return sdl_create_displaysurface(width, height);
bellard0f0b7262003-08-09 18:26:36 +0000230}
231
bellard3d11d0e2004-12-12 16:56:30 +0000232/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +0000233
bellard3d11d0e2004-12-12 16:56:30 +0000234#include "sdl_keysym.h"
bellarde58d12e2004-07-05 22:13:07 +0000235
Anthony Liguoric227f092009-10-01 16:12:16 -0500236static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000237
bellard3d11d0e2004-12-12 16:56:30 +0000238static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000239{
bellard3d11d0e2004-12-12 16:56:30 +0000240 int keysym;
241 /* workaround for X11+SDL bug with AltGR */
242 keysym = ev->keysym.sym;
243 if (keysym == 0 && ev->keysym.scancode == 113)
244 keysym = SDLK_MODE;
bellard60659e32006-08-19 14:27:31 +0000245 /* For Japanese key '\' and '|' */
246 if (keysym == 92 && ev->keysym.scancode == 133) {
247 keysym = 0xa5;
248 }
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100249 return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
bellarde58d12e2004-07-05 22:13:07 +0000250}
251
bellard3d11d0e2004-12-12 16:56:30 +0000252/* specific keyboard conversions from scan codes */
253
254#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000255
256static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
257{
258 return ev->keysym.scancode;
259}
260
261#else
262
aliguori5368a422009-03-03 17:37:21 +0000263#if defined(SDL_VIDEO_DRIVER_X11)
264#include <X11/XKBlib.h>
265
266static int check_for_evdev(void)
267{
268 SDL_SysWMinfo info;
Jan Kiszka229609d2009-06-27 09:59:40 +0200269 XkbDescPtr desc = NULL;
aliguori5368a422009-03-03 17:37:21 +0000270 int has_evdev = 0;
Jan Kiszka229609d2009-06-27 09:59:40 +0200271 char *keycodes = NULL;
aliguori5368a422009-03-03 17:37:21 +0000272
273 SDL_VERSION(&info.version);
Jan Kiszka229609d2009-06-27 09:59:40 +0200274 if (!SDL_GetWMInfo(&info)) {
aliguori5368a422009-03-03 17:37:21 +0000275 return 0;
Jan Kiszka229609d2009-06-27 09:59:40 +0200276 }
aliguori5368a422009-03-03 17:37:21 +0000277 desc = XkbGetKeyboard(info.info.x11.display,
278 XkbGBN_AllComponentsMask,
279 XkbUseCoreKbd);
Jan Kiszka229609d2009-06-27 09:59:40 +0200280 if (desc && desc->names) {
281 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
282 if (keycodes == NULL) {
283 fprintf(stderr, "could not lookup keycode name\n");
284 } else if (strstart(keycodes, "evdev", NULL)) {
285 has_evdev = 1;
286 } else if (!strstart(keycodes, "xfree86", NULL)) {
287 fprintf(stderr, "unknown keycodes `%s', please report to "
288 "qemu-devel@nongnu.org\n", keycodes);
289 }
290 }
aliguori5368a422009-03-03 17:37:21 +0000291
Jan Kiszka229609d2009-06-27 09:59:40 +0200292 if (desc) {
293 XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True);
294 }
295 if (keycodes) {
296 XFree(keycodes);
297 }
aliguori5368a422009-03-03 17:37:21 +0000298 return has_evdev;
299}
300#else
301static int check_for_evdev(void)
302{
303 return 0;
304}
305#endif
306
bellarde58d12e2004-07-05 22:13:07 +0000307static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
308{
309 int keycode;
aliguori5368a422009-03-03 17:37:21 +0000310 static int has_evdev = -1;
311
312 if (has_evdev == -1)
313 has_evdev = check_for_evdev();
bellarde58d12e2004-07-05 22:13:07 +0000314
315 keycode = ev->keysym.scancode;
316
317 if (keycode < 9) {
318 keycode = 0;
319 } else if (keycode < 97) {
320 keycode -= 8; /* just an offset */
aliguori5368a422009-03-03 17:37:21 +0000321 } else if (keycode < 158) {
bellarde58d12e2004-07-05 22:13:07 +0000322 /* use conversion table */
aliguori5368a422009-03-03 17:37:21 +0000323 if (has_evdev)
324 keycode = translate_evdev_keycode(keycode - 97);
325 else
326 keycode = translate_xfree86_keycode(keycode - 97);
327 } else if (keycode == 208) { /* Hiragana_Katakana */
328 keycode = 0x70;
329 } else if (keycode == 211) { /* backslash */
330 keycode = 0x73;
bellarde58d12e2004-07-05 22:13:07 +0000331 } else {
332 keycode = 0;
333 }
334 return keycode;
335}
336
337#endif
338
bellard32ff25b2004-10-03 14:33:54 +0000339static void reset_keys(void)
340{
341 int i;
342 for(i = 0; i < 256; i++) {
343 if (modifiers_state[i]) {
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100344 if (i & SCANCODE_GREY)
345 kbd_put_keycode(SCANCODE_EMUL0);
346 kbd_put_keycode(i | SCANCODE_UP);
bellard32ff25b2004-10-03 14:33:54 +0000347 modifiers_state[i] = 0;
348 }
349 }
350}
351
bellard0f0b7262003-08-09 18:26:36 +0000352static void sdl_process_key(SDL_KeyboardEvent *ev)
353{
bellard32ff25b2004-10-03 14:33:54 +0000354 int keycode, v;
bellardde2200d2004-06-04 13:15:06 +0000355
356 if (ev->keysym.sym == SDLK_PAUSE) {
357 /* specific case */
358 v = 0;
359 if (ev->type == SDL_KEYUP)
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100360 v |= SCANCODE_UP;
bellardde2200d2004-06-04 13:15:06 +0000361 kbd_put_keycode(0xe1);
362 kbd_put_keycode(0x1d | v);
363 kbd_put_keycode(0x45 | v);
364 return;
365 }
366
bellard3d11d0e2004-12-12 16:56:30 +0000367 if (kbd_layout) {
368 keycode = sdl_keyevent_to_keycode_generic(ev);
369 } else {
370 keycode = sdl_keyevent_to_keycode(ev);
371 }
bellardde2200d2004-06-04 13:15:06 +0000372
373 switch(keycode) {
374 case 0x00:
375 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000376 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000377 return;
378 case 0x2a: /* Left Shift */
379 case 0x36: /* Right Shift */
380 case 0x1d: /* Left CTRL */
381 case 0x9d: /* Right CTRL */
382 case 0x38: /* Left ALT */
383 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000384 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000385 modifiers_state[keycode] = 0;
386 else
387 modifiers_state[keycode] = 1;
388 break;
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100389#define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
390#if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
391 /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
bellardde2200d2004-06-04 13:15:06 +0000392 case 0x45: /* num lock */
393 case 0x3a: /* caps lock */
394 /* SDL does not send the key up event, so we generate it */
395 kbd_put_keycode(keycode);
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100396 kbd_put_keycode(keycode | SCANCODE_UP);
bellardde2200d2004-06-04 13:15:06 +0000397 return;
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100398#endif
bellard0f0b7262003-08-09 18:26:36 +0000399 }
bellardde2200d2004-06-04 13:15:06 +0000400
401 /* now send the key code */
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100402 if (keycode & SCANCODE_GREY)
403 kbd_put_keycode(SCANCODE_EMUL0);
bellardde2200d2004-06-04 13:15:06 +0000404 if (ev->type == SDL_KEYUP)
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100405 kbd_put_keycode(keycode | SCANCODE_UP);
bellardde2200d2004-06-04 13:15:06 +0000406 else
Samuel Thibault44bb61c2010-02-28 21:03:00 +0100407 kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
bellard0f0b7262003-08-09 18:26:36 +0000408}
409
bellard8a7ddc32004-03-31 19:00:16 +0000410static void sdl_update_caption(void)
411{
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100412 char win_title[1024];
413 char icon_title[1024];
thsc35734b2007-03-19 15:17:08 +0000414 const char *status = "";
415
416 if (!vm_running)
417 status = " [Stopped]";
ths3780e192007-06-21 21:08:02 +0000418 else if (gui_grab) {
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500419 if (alt_grab)
Anthony Liguori4e75b342010-03-01 08:47:28 -0600420 status = " - Press Ctrl-Alt-Shift to exit mouse grab";
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500421 else if (ctrl_grab)
Anthony Liguori4e75b342010-03-01 08:47:28 -0600422 status = " - Press Right-Ctrl to exit mouse grab";
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500423 else
Anthony Liguori4e75b342010-03-01 08:47:28 -0600424 status = " - Press Ctrl-Alt to exit mouse grab";
ths3780e192007-06-21 21:08:02 +0000425 }
thsc35734b2007-03-19 15:17:08 +0000426
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100427 if (qemu_name) {
428 snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
429 snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
430 } else {
431 snprintf(win_title, sizeof(win_title), "QEMU%s", status);
432 snprintf(icon_title, sizeof(icon_title), "QEMU");
433 }
thsc35734b2007-03-19 15:17:08 +0000434
Dominic Evansb4ed5d12009-09-24 12:13:56 +0100435 SDL_WM_SetCaption(win_title, icon_title);
bellard8a7ddc32004-03-31 19:00:16 +0000436}
437
bellard09b26c52006-04-12 21:09:08 +0000438static void sdl_hide_cursor(void)
439{
balrog9467cd42007-05-01 01:34:14 +0000440 if (!cursor_hide)
441 return;
442
bellard8785a8d2006-06-13 10:49:12 +0000443 if (kbd_mouse_is_absolute()) {
444 SDL_ShowCursor(1);
445 SDL_SetCursor(sdl_cursor_hidden);
446 } else {
447 SDL_ShowCursor(0);
448 }
bellard09b26c52006-04-12 21:09:08 +0000449}
450
451static void sdl_show_cursor(void)
452{
balrog9467cd42007-05-01 01:34:14 +0000453 if (!cursor_hide)
454 return;
455
bellard09b26c52006-04-12 21:09:08 +0000456 if (!kbd_mouse_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000457 SDL_ShowCursor(1);
thsd34cab92007-04-02 01:10:46 +0000458 if (guest_cursor &&
459 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
460 SDL_SetCursor(guest_sprite);
461 else
462 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000463 }
464}
465
bellard0f0b7262003-08-09 18:26:36 +0000466static void sdl_grab_start(void)
467{
thsd34cab92007-04-02 01:10:46 +0000468 if (guest_cursor) {
469 SDL_SetCursor(guest_sprite);
balrog08a2d4c2009-01-29 23:29:52 +0000470 if (!kbd_mouse_is_absolute() && !absolute_enabled)
471 SDL_WarpMouse(guest_x, guest_y);
thsd34cab92007-04-02 01:10:46 +0000472 } else
473 sdl_hide_cursor();
aliguori6bb81602009-01-15 20:47:45 +0000474
475 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
476 gui_grab = 1;
477 sdl_update_caption();
478 } else
479 sdl_show_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000480}
481
482static void sdl_grab_end(void)
483{
bellard0f0b7262003-08-09 18:26:36 +0000484 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard0f0b7262003-08-09 18:26:36 +0000485 gui_grab = 0;
thsd34cab92007-04-02 01:10:46 +0000486 sdl_show_cursor();
bellard8a7ddc32004-03-31 19:00:16 +0000487 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000488}
489
Jan Kiszka9e8dd452011-06-20 14:06:26 +0200490static void sdl_mouse_mode_change(Notifier *notify, void *data)
Anthony Liguori3af12c82010-03-10 09:42:58 -0600491{
492 if (kbd_mouse_is_absolute()) {
493 if (!absolute_enabled) {
494 sdl_hide_cursor();
495 if (gui_grab) {
496 sdl_grab_end();
497 }
498 absolute_enabled = 1;
499 }
500 } else if (absolute_enabled) {
501 sdl_show_cursor();
502 absolute_enabled = 0;
503 }
504}
505
aurel324c44bdc2008-03-13 19:20:18 +0000506static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
bellard0f0b7262003-08-09 18:26:36 +0000507{
aurel324c44bdc2008-03-13 19:20:18 +0000508 int buttons;
bellard0f0b7262003-08-09 18:26:36 +0000509 buttons = 0;
510 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
511 buttons |= MOUSE_EVENT_LBUTTON;
512 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
513 buttons |= MOUSE_EVENT_RBUTTON;
514 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
515 buttons |= MOUSE_EVENT_MBUTTON;
bellard09b26c52006-04-12 21:09:08 +0000516
517 if (kbd_mouse_is_absolute()) {
aurel324c44bdc2008-03-13 19:20:18 +0000518 dx = x * 0x7FFF / (width - 1);
519 dy = y * 0x7FFF / (height - 1);
thsd34cab92007-04-02 01:10:46 +0000520 } else if (guest_cursor) {
aurel324c44bdc2008-03-13 19:20:18 +0000521 x -= guest_x;
522 y -= guest_y;
523 guest_x += x;
524 guest_y += y;
525 dx = x;
526 dy = y;
bellard09b26c52006-04-12 21:09:08 +0000527 }
528
bellard0f0b7262003-08-09 18:26:36 +0000529 kbd_mouse_event(dx, dy, dz, buttons);
530}
531
Jan Kiszkaf9977892011-07-30 11:39:09 +0200532static void sdl_scale(DisplayState *ds, int width, int height)
533{
534 int bpp = real_screen->format->BitsPerPixel;
535
536 if (bpp != 16 && bpp != 32) {
537 bpp = 32;
538 }
539 do_sdl_resize(width, height, bpp);
540 scaling_active = 1;
541 if (!is_buffer_shared(ds->surface)) {
542 ds->surface = qemu_resize_displaysurface(ds, ds_get_width(ds),
543 ds_get_height(ds));
544 dpy_resize(ds);
545 }
546}
547
bellard8e9c4af2004-04-28 19:33:40 +0000548static void toggle_full_screen(DisplayState *ds)
549{
550 gui_fullscreen = !gui_fullscreen;
bellard8e9c4af2004-04-28 19:33:40 +0000551 if (gui_fullscreen) {
Jan Kiszkaf9977892011-07-30 11:39:09 +0200552 gui_saved_width = real_screen->w;
553 gui_saved_height = real_screen->h;
554 gui_saved_scaling = scaling_active;
555
556 do_sdl_resize(ds_get_width(ds), ds_get_height(ds),
557 ds_get_bits_per_pixel(ds));
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100558 scaling_active = 0;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200559
bellard8e9c4af2004-04-28 19:33:40 +0000560 gui_saved_grab = gui_grab;
561 sdl_grab_start();
562 } else {
Jan Kiszkaf9977892011-07-30 11:39:09 +0200563 if (gui_saved_scaling) {
564 sdl_scale(ds, gui_saved_width, gui_saved_height);
565 } else {
566 do_sdl_resize(ds_get_width(ds), ds_get_height(ds), 0);
567 }
bellard8e9c4af2004-04-28 19:33:40 +0000568 if (!gui_saved_grab)
569 sdl_grab_end();
570 }
pbrook95219892006-04-09 01:06:34 +0000571 vga_hw_invalidate();
572 vga_hw_update();
bellard8e9c4af2004-04-28 19:33:40 +0000573}
574
bellard0f0b7262003-08-09 18:26:36 +0000575static void sdl_refresh(DisplayState *ds)
576{
577 SDL_Event ev1, *ev = &ev1;
bellard8e9c4af2004-04-28 19:33:40 +0000578 int mod_state;
aurel324c44bdc2008-03-13 19:20:18 +0000579 int buttonstate = SDL_GetMouseState(NULL, NULL);
ths3b46e622007-09-17 08:09:54 +0000580
bellard8a7ddc32004-03-31 19:00:16 +0000581 if (last_vm_running != vm_running) {
582 last_vm_running = vm_running;
583 sdl_update_caption();
584 }
585
pbrook95219892006-04-09 01:06:34 +0000586 vga_hw_update();
aurel323bee8bd2008-04-13 16:08:37 +0000587 SDL_EnableUNICODE(!is_graphic_console());
bellard457831f2004-07-14 17:22:33 +0000588
bellard0f0b7262003-08-09 18:26:36 +0000589 while (SDL_PollEvent(ev)) {
590 switch (ev->type) {
591 case SDL_VIDEOEXPOSE:
aliguori7d957bd2009-01-15 22:14:11 +0000592 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
bellard0f0b7262003-08-09 18:26:36 +0000593 break;
594 case SDL_KEYDOWN:
595 case SDL_KEYUP:
596 if (ev->type == SDL_KEYDOWN) {
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500597 if (alt_grab) {
ths3780e192007-06-21 21:08:02 +0000598 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
599 (gui_grab_code | KMOD_LSHIFT);
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500600 } else if (ctrl_grab) {
601 mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
602 } else {
603 mod_state = (SDL_GetModState() & gui_grab_code) ==
604 gui_grab_code;
ths3780e192007-06-21 21:08:02 +0000605 }
bellard8e9c4af2004-04-28 19:33:40 +0000606 gui_key_modifier_pressed = mod_state;
bellard457831f2004-07-14 17:22:33 +0000607 if (gui_key_modifier_pressed) {
bellard32ff25b2004-10-03 14:33:54 +0000608 int keycode;
609 keycode = sdl_keyevent_to_keycode(&ev->key);
610 switch(keycode) {
611 case 0x21: /* 'f' key on US keyboard */
bellard457831f2004-07-14 17:22:33 +0000612 toggle_full_screen(ds);
613 gui_keysym = 1;
614 break;
malcc4a735f2009-09-10 05:15:07 +0400615 case 0x16: /* 'u' key on US keyboard */
Jan Kiszka97ad1c22011-07-30 11:39:06 +0200616 if (scaling_active) {
617 scaling_active = 0;
618 sdl_resize(ds);
619 vga_hw_invalidate();
620 vga_hw_update();
621 }
Jan Kiszkad71680c2011-07-30 11:39:07 +0200622 gui_keysym = 1;
malcc4a735f2009-09-10 05:15:07 +0400623 break;
ths5fafdf22007-09-16 21:08:06 +0000624 case 0x02 ... 0x0a: /* '1' to '9' keys */
bellarddfd92d32006-08-17 10:42:46 +0000625 /* Reset the modifiers sent to the current console */
626 reset_keys();
bellard32ff25b2004-10-03 14:33:54 +0000627 console_select(keycode - 0x02);
pbrook95219892006-04-09 01:06:34 +0000628 if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000629 /* display grab if going to a text console */
630 if (gui_grab)
631 sdl_grab_end();
632 }
633 gui_keysym = 1;
634 break;
635 default:
636 break;
637 }
pbrook95219892006-04-09 01:06:34 +0000638 } else if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000639 int keysym;
640 keysym = 0;
641 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
642 switch(ev->key.keysym.sym) {
643 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
644 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
645 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
646 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
647 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
648 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
649 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
650 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
651 default: break;
652 }
653 } else {
654 switch(ev->key.keysym.sym) {
655 case SDLK_UP: keysym = QEMU_KEY_UP; break;
656 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
657 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
658 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
659 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
660 case SDLK_END: keysym = QEMU_KEY_END; break;
661 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
662 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
thse91c8a72007-06-03 13:35:16 +0000663 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
664 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
bellard457831f2004-07-14 17:22:33 +0000665 default: break;
666 }
667 }
668 if (keysym) {
669 kbd_put_keysym(keysym);
670 } else if (ev->key.keysym.unicode != 0) {
671 kbd_put_keysym(ev->key.keysym.unicode);
672 }
bellard8e9c4af2004-04-28 19:33:40 +0000673 }
674 } else if (ev->type == SDL_KEYUP) {
ths3780e192007-06-21 21:08:02 +0000675 if (!alt_grab) {
676 mod_state = (ev->key.keysym.mod & gui_grab_code);
677 } else {
678 mod_state = (ev->key.keysym.mod &
679 (gui_grab_code | KMOD_LSHIFT));
680 }
bellard8e9c4af2004-04-28 19:33:40 +0000681 if (!mod_state) {
682 if (gui_key_modifier_pressed) {
pbrook5b311872006-03-11 20:07:45 +0000683 gui_key_modifier_pressed = 0;
bellard457831f2004-07-14 17:22:33 +0000684 if (gui_keysym == 0) {
bellard32ff25b2004-10-03 14:33:54 +0000685 /* exit/enter grab if pressing Ctrl-Alt */
bellardc66b0d42006-06-13 12:03:53 +0000686 if (!gui_grab) {
687 /* if the application is not active,
688 do not try to enter grab state. It
689 prevents
690 'SDL_WM_GrabInput(SDL_GRAB_ON)'
691 from blocking all the application
692 (SDL bug). */
693 if (SDL_GetAppState() & SDL_APPACTIVE)
694 sdl_grab_start();
695 } else {
bellard8e9c4af2004-04-28 19:33:40 +0000696 sdl_grab_end();
bellardc66b0d42006-06-13 12:03:53 +0000697 }
bellard32ff25b2004-10-03 14:33:54 +0000698 /* SDL does not send back all the
699 modifiers key, so we must correct it */
700 reset_keys();
bellard8e9c4af2004-04-28 19:33:40 +0000701 break;
702 }
bellard8e9c4af2004-04-28 19:33:40 +0000703 gui_keysym = 0;
704 }
bellard0f0b7262003-08-09 18:26:36 +0000705 }
706 }
ths5fafdf22007-09-16 21:08:06 +0000707 if (is_graphic_console() && !gui_keysym)
bellard457831f2004-07-14 17:22:33 +0000708 sdl_process_key(&ev->key);
bellard0f0b7262003-08-09 18:26:36 +0000709 break;
710 case SDL_QUIT:
Jan Kiszka941f5112011-07-30 11:39:04 +0200711 if (!no_quit) {
712 no_shutdown = 0;
balrog731345e2007-06-21 17:56:50 +0000713 qemu_system_shutdown_request();
Jan Kiszka941f5112011-07-30 11:39:04 +0200714 }
bellard0f0b7262003-08-09 18:26:36 +0000715 break;
716 case SDL_MOUSEMOTION:
ths455204e2007-01-05 16:42:13 +0000717 if (gui_grab || kbd_mouse_is_absolute() ||
718 absolute_enabled) {
aurel324c44bdc2008-03-13 19:20:18 +0000719 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
720 ev->motion.x, ev->motion.y, ev->motion.state);
bellard0f0b7262003-08-09 18:26:36 +0000721 }
722 break;
723 case SDL_MOUSEBUTTONDOWN:
724 case SDL_MOUSEBUTTONUP:
725 {
726 SDL_MouseButtonEvent *bev = &ev->button;
bellard09b26c52006-04-12 21:09:08 +0000727 if (!gui_grab && !kbd_mouse_is_absolute()) {
bellard0f0b7262003-08-09 18:26:36 +0000728 if (ev->type == SDL_MOUSEBUTTONDOWN &&
aurel324c44bdc2008-03-13 19:20:18 +0000729 (bev->button == SDL_BUTTON_LEFT)) {
bellard0f0b7262003-08-09 18:26:36 +0000730 /* start grabbing all events */
731 sdl_grab_start();
732 }
733 } else {
bellard18a6d282005-01-17 22:32:23 +0000734 int dz;
735 dz = 0;
aurel324c44bdc2008-03-13 19:20:18 +0000736 if (ev->type == SDL_MOUSEBUTTONDOWN) {
737 buttonstate |= SDL_BUTTON(bev->button);
738 } else {
739 buttonstate &= ~SDL_BUTTON(bev->button);
740 }
bellard18a6d282005-01-17 22:32:23 +0000741#ifdef SDL_BUTTON_WHEELUP
bellard09b26c52006-04-12 21:09:08 +0000742 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000743 dz = -1;
bellard09b26c52006-04-12 21:09:08 +0000744 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000745 dz = 1;
746 }
ths3b46e622007-09-17 08:09:54 +0000747#endif
aurel324c44bdc2008-03-13 19:20:18 +0000748 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
bellard0f0b7262003-08-09 18:26:36 +0000749 }
750 }
751 break;
bellard0294ffb2004-04-29 22:15:15 +0000752 case SDL_ACTIVEEVENT:
pbrook5b311872006-03-11 20:07:45 +0000753 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
754 !ev->active.gain && !gui_fullscreen_initial_grab) {
bellard0294ffb2004-04-29 22:15:15 +0000755 sdl_grab_end();
756 }
aurel32f442e082008-03-13 19:20:33 +0000757 if (ev->active.state & SDL_APPACTIVE) {
758 if (ev->active.gain) {
759 /* Back to default interval */
aliguori7d957bd2009-01-15 22:14:11 +0000760 dcl->gui_timer_interval = 0;
761 dcl->idle = 0;
aurel32f442e082008-03-13 19:20:33 +0000762 } else {
763 /* Sleeping interval */
aliguori7d957bd2009-01-15 22:14:11 +0000764 dcl->gui_timer_interval = 500;
765 dcl->idle = 1;
aurel32f442e082008-03-13 19:20:33 +0000766 }
767 }
bellard0294ffb2004-04-29 22:15:15 +0000768 break;
Jan Kiszkaf9977892011-07-30 11:39:09 +0200769 case SDL_VIDEORESIZE:
770 sdl_scale(ds, ev->resize.w, ev->resize.h);
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100771 vga_hw_invalidate();
772 vga_hw_update();
773 break;
bellard0f0b7262003-08-09 18:26:36 +0000774 default:
775 break;
776 }
777 }
778}
779
thsd34cab92007-04-02 01:10:46 +0000780static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
781{
782 SDL_Rect dst = { x, y, w, h };
aliguori7d957bd2009-01-15 22:14:11 +0000783 SDL_FillRect(real_screen, &dst, c);
thsd34cab92007-04-02 01:10:46 +0000784}
785
786static void sdl_mouse_warp(int x, int y, int on)
787{
788 if (on) {
789 if (!guest_cursor)
790 sdl_show_cursor();
791 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
792 SDL_SetCursor(guest_sprite);
balrog08a2d4c2009-01-29 23:29:52 +0000793 if (!kbd_mouse_is_absolute() && !absolute_enabled)
794 SDL_WarpMouse(x, y);
thsd34cab92007-04-02 01:10:46 +0000795 }
796 } else if (gui_grab)
797 sdl_hide_cursor();
798 guest_cursor = on;
799 guest_x = x, guest_y = y;
800}
801
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200802static void sdl_mouse_define(QEMUCursor *c)
thsd34cab92007-04-02 01:10:46 +0000803{
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200804 uint8_t *image, *mask;
805 int bpl;
806
thsd34cab92007-04-02 01:10:46 +0000807 if (guest_sprite)
808 SDL_FreeCursor(guest_sprite);
809
Gerd Hoffmannfbe6d7a2010-05-21 11:54:33 +0200810 bpl = cursor_get_mono_bpl(c);
811 image = qemu_mallocz(bpl * c->height);
812 mask = qemu_mallocz(bpl * c->height);
813 cursor_get_mono_image(c, 0x000000, image);
814 cursor_get_mono_mask(c, 0, mask);
815 guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
816 c->hot_x, c->hot_y);
817 qemu_free(image);
818 qemu_free(mask);
thsd34cab92007-04-02 01:10:46 +0000819
820 if (guest_cursor &&
821 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
822 SDL_SetCursor(guest_sprite);
823}
824
Anthony Liguori28695482010-03-21 14:13:02 -0500825static void sdl_cleanup(void)
bellard898712a2004-02-06 19:56:42 +0000826{
thsd34cab92007-04-02 01:10:46 +0000827 if (guest_sprite)
828 SDL_FreeCursor(guest_sprite);
malcd8ee7662009-05-17 18:26:05 +0400829 SDL_QuitSubSystem(SDL_INIT_VIDEO);
bellard898712a2004-02-06 19:56:42 +0000830}
831
ths43523e92007-02-18 18:19:32 +0000832void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
bellard0f0b7262003-08-09 18:26:36 +0000833{
834 int flags;
bellard09b26c52006-04-12 21:09:08 +0000835 uint8_t data = 0;
aliguori7b5d76d2009-03-13 15:02:13 +0000836 DisplayAllocator *da;
837 const SDL_VideoInfo *vi;
Stefan Weil09cec712011-02-16 21:15:40 +0100838 char *filename;
bellard0f0b7262003-08-09 18:26:36 +0000839
bellard3d11d0e2004-12-12 16:56:30 +0000840#if defined(__APPLE__)
841 /* always use generic keymaps */
842 if (!keyboard_layout)
843 keyboard_layout = "en-us";
844#endif
845 if(keyboard_layout) {
aliguori04837552009-03-06 20:27:10 +0000846 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
bellard3d11d0e2004-12-12 16:56:30 +0000847 if (!kbd_layout)
848 exit(1);
849 }
850
ths43523e92007-02-18 18:19:32 +0000851 if (no_frame)
852 gui_noframe = 1;
853
Jan Kiszka111f8ec2010-05-23 10:29:34 +0200854 if (!full_screen) {
855 setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
856 }
Michael Tokarev1de97562011-05-08 01:18:30 +0400857#ifdef __linux__
858 /* on Linux, SDL may use fbcon|directfb|svgalib when run without
859 * accessible $DISPLAY to open X11 window. This is often the case
860 * when qemu is run using sudo. But in this case, and when actually
861 * run in X11 environment, SDL fights with X11 for the video card,
862 * making current display unavailable, often until reboot.
863 * So make x11 the default SDL video driver if this variable is unset.
864 * This is a bit hackish but saves us from bigger problem.
865 * Maybe it's a good idea to fix this in SDL instead.
866 */
867 setenv("SDL_VIDEODRIVER", "x11", 0);
868#endif
Jan Kiszka111f8ec2010-05-23 10:29:34 +0200869
Stefan Weil4e79bcb2011-02-03 22:35:07 +0100870 /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
871 * This requires SDL >= 1.2.14. */
872 setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
873
bellard0f0b7262003-08-09 18:26:36 +0000874 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
875 if (SDL_Init (flags)) {
malc3caf2562009-12-30 04:26:34 +0300876 fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
877 SDL_GetError());
bellard0f0b7262003-08-09 18:26:36 +0000878 exit(1);
879 }
aliguori7b5d76d2009-03-13 15:02:13 +0000880 vi = SDL_GetVideoInfo();
Stefano Stabellinic18a2c32009-06-24 11:58:25 +0100881 host_format = *(vi->vfmt);
bellard0ae04d72003-09-30 21:09:16 +0000882
Stefan Weil09cec712011-02-16 21:15:40 +0100883 /* Load a 32x32x4 image. White pixels are transparent. */
884 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
885 if (filename) {
886 SDL_Surface *image = SDL_LoadBMP(filename);
887 if (image) {
888 uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
889 SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
890 SDL_WM_SetIcon(image, NULL);
891 }
892 qemu_free(filename);
893 }
894
aliguori7d957bd2009-01-15 22:14:11 +0000895 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
aliguori7d957bd2009-01-15 22:14:11 +0000896 dcl->dpy_update = sdl_update;
897 dcl->dpy_resize = sdl_resize;
898 dcl->dpy_refresh = sdl_refresh;
899 dcl->dpy_setdata = sdl_setdata;
900 dcl->dpy_fill = sdl_fill;
thsd34cab92007-04-02 01:10:46 +0000901 ds->mouse_set = sdl_mouse_warp;
902 ds->cursor_define = sdl_mouse_define;
aliguori7d957bd2009-01-15 22:14:11 +0000903 register_displaychangelistener(ds, dcl);
bellard0f0b7262003-08-09 18:26:36 +0000904
aliguori7b5d76d2009-03-13 15:02:13 +0000905 da = qemu_mallocz(sizeof(DisplayAllocator));
906 da->create_displaysurface = sdl_create_displaysurface;
907 da->resize_displaysurface = sdl_resize_displaysurface;
908 da->free_displaysurface = sdl_free_displaysurface;
909 if (register_displayallocator(ds, da) == da) {
aliguori7b5d76d2009-03-13 15:02:13 +0000910 dpy_resize(ds);
911 }
912
Anthony Liguori3af12c82010-03-10 09:42:58 -0600913 mouse_mode_notifier.notify = sdl_mouse_mode_change;
914 qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
915
bellard8a7ddc32004-03-31 19:00:16 +0000916 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000917 SDL_EnableKeyRepeat(250, 50);
918 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000919
bellard09b26c52006-04-12 21:09:08 +0000920 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
921 sdl_cursor_normal = SDL_GetCursor();
922
Anthony Liguori28695482010-03-21 14:13:02 -0500923 atexit(sdl_cleanup);
bellardd63d3072004-10-03 13:29:03 +0000924 if (full_screen) {
925 gui_fullscreen = 1;
926 gui_fullscreen_initial_grab = 1;
927 sdl_grab_start();
928 }
bellard0f0b7262003-08-09 18:26:36 +0000929}