blob: 178b5532b8d9dd2194a8662fbfdcd49b4bc04222 [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 */
bellard0f0b7262003-08-09 18:26:36 +000024#include <SDL.h>
aliguoric9985aa2009-03-08 15:04:07 +000025#include <SDL_syswm.h>
bellard0f0b7262003-08-09 18:26:36 +000026
bellard67b915a2004-03-31 23:37:16 +000027#ifndef _WIN32
28#include <signal.h>
29#endif
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"
35
aliguori7d957bd2009-01-15 22:14:11 +000036static DisplayChangeListener *dcl;
37static SDL_Surface *real_screen;
38static SDL_Surface *guest_screen = NULL;
bellard0f0b7262003-08-09 18:26:36 +000039static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000040static int last_vm_running;
bellard8e9c4af2004-04-28 19:33:40 +000041static int gui_saved_grab;
42static int gui_fullscreen;
ths43523e92007-02-18 18:19:32 +000043static int gui_noframe;
bellard8e9c4af2004-04-28 19:33:40 +000044static int gui_key_modifier_pressed;
45static int gui_keysym;
bellardd63d3072004-10-03 13:29:03 +000046static int gui_fullscreen_initial_grab;
bellard32ff25b2004-10-03 14:33:54 +000047static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
48static uint8_t modifiers_state[256];
bellard09b26c52006-04-12 21:09:08 +000049static int width, height;
50static SDL_Cursor *sdl_cursor_normal;
51static SDL_Cursor *sdl_cursor_hidden;
52static int absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +000053static int guest_cursor = 0;
54static int guest_x, guest_y;
55static SDL_Cursor *guest_sprite = 0;
aliguori7b5d76d2009-03-13 15:02:13 +000056static uint8_t allocator;
57static uint8_t hostbpp;
bellard0f0b7262003-08-09 18:26:36 +000058
59static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
60{
bellard898712a2004-02-06 19:56:42 +000061 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
aliguori7b5d76d2009-03-13 15:02:13 +000062 if (guest_screen) {
63 SDL_Rect rec;
64 rec.x = x;
65 rec.y = y;
66 rec.w = w;
67 rec.h = h;
68 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
69 }
pbrook6e600652009-01-21 01:50:17 +000070 SDL_UpdateRect(real_screen, x, y, w, h);
bellard0f0b7262003-08-09 18:26:36 +000071}
72
aliguori7d957bd2009-01-15 22:14:11 +000073static void sdl_setdata(DisplayState *ds)
74{
75 SDL_Rect rec;
76 rec.x = 0;
77 rec.y = 0;
78 rec.w = real_screen->w;
79 rec.h = real_screen->h;
80
81 if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
82
83 guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds),
84 ds_get_bits_per_pixel(ds), ds_get_linesize(ds),
85 ds->surface->pf.rmask, ds->surface->pf.gmask,
86 ds->surface->pf.bmask, ds->surface->pf.amask);
87}
88
balrog649c9072009-03-21 01:09:16 +000089static void do_sdl_resize(int new_width, int new_height, int bpp)
bellard0f0b7262003-08-09 18:26:36 +000090{
91 int flags;
92
93 // printf("resizing to %d %d\n", w, h);
94
95 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
bellard8e9c4af2004-04-28 19:33:40 +000096 if (gui_fullscreen)
97 flags |= SDL_FULLSCREEN;
ths43523e92007-02-18 18:19:32 +000098 if (gui_noframe)
99 flags |= SDL_NOFRAME;
bellard9903da22005-10-30 23:19:10 +0000100
balrog649c9072009-03-21 01:09:16 +0000101 width = new_width;
102 height = new_height;
aliguori7b5d76d2009-03-13 15:02:13 +0000103 real_screen = SDL_SetVideoMode(width, height, bpp, flags);
aliguori7d957bd2009-01-15 22:14:11 +0000104 if (!real_screen) {
bellard0f0b7262003-08-09 18:26:36 +0000105 fprintf(stderr, "Could not open SDL display\n");
106 exit(1);
107 }
aliguori7b5d76d2009-03-13 15:02:13 +0000108}
blueswir1749ecd92008-07-24 11:25:30 +0000109
aliguori7b5d76d2009-03-13 15:02:13 +0000110static void sdl_resize(DisplayState *ds)
111{
112 if (!allocator) {
113 do_sdl_resize(ds_get_width(ds), ds_get_height(ds), 0);
114 sdl_setdata(ds);
115 } else {
116 if (guest_screen != NULL) {
117 SDL_FreeSurface(guest_screen);
118 guest_screen = NULL;
119 }
120 }
121}
122
123static PixelFormat sdl_to_qemu_pixelformat(SDL_PixelFormat *sdl_pf)
124{
125 PixelFormat qemu_pf;
126
127 memset(&qemu_pf, 0x00, sizeof(PixelFormat));
128
129 qemu_pf.bits_per_pixel = sdl_pf->BitsPerPixel;
130 qemu_pf.bytes_per_pixel = sdl_pf->BytesPerPixel;
131 qemu_pf.depth = (qemu_pf.bits_per_pixel) == 32 ? 24 : (qemu_pf.bits_per_pixel);
132
133 qemu_pf.rmask = sdl_pf->Rmask;
134 qemu_pf.gmask = sdl_pf->Gmask;
135 qemu_pf.bmask = sdl_pf->Bmask;
136 qemu_pf.amask = sdl_pf->Amask;
137
138 qemu_pf.rshift = sdl_pf->Rshift;
139 qemu_pf.gshift = sdl_pf->Gshift;
140 qemu_pf.bshift = sdl_pf->Bshift;
141 qemu_pf.ashift = sdl_pf->Ashift;
142
143 qemu_pf.rbits = 8 - sdl_pf->Rloss;
144 qemu_pf.gbits = 8 - sdl_pf->Gloss;
145 qemu_pf.bbits = 8 - sdl_pf->Bloss;
146 qemu_pf.abits = 8 - sdl_pf->Aloss;
147
148 qemu_pf.rmax = ((1 << qemu_pf.rbits) - 1);
149 qemu_pf.gmax = ((1 << qemu_pf.gbits) - 1);
150 qemu_pf.bmax = ((1 << qemu_pf.bbits) - 1);
151 qemu_pf.amax = ((1 << qemu_pf.abits) - 1);
152
153 return qemu_pf;
154}
155
156static DisplaySurface* sdl_create_displaysurface(int width, int height)
157{
158 DisplaySurface *surface = (DisplaySurface*) qemu_mallocz(sizeof(DisplaySurface));
159 if (surface == NULL) {
160 fprintf(stderr, "sdl_create_displaysurface: malloc failed\n");
161 exit(1);
162 }
163
164 surface->width = width;
165 surface->height = height;
166
167 if (hostbpp == 16)
168 do_sdl_resize(width, height, 16);
169 else
170 do_sdl_resize(width, height, 32);
171
172 surface->pf = sdl_to_qemu_pixelformat(real_screen->format);
173 surface->linesize = real_screen->pitch;
174 surface->data = real_screen->pixels;
175
176#ifdef WORDS_BIGENDIAN
177 surface->flags = QEMU_ALLOCATED_FLAG | QEMU_BIG_ENDIAN_FLAG;
178#else
179 surface->flags = QEMU_ALLOCATED_FLAG;
180#endif
181 allocator = 1;
182
183 return surface;
184}
185
186static void sdl_free_displaysurface(DisplaySurface *surface)
187{
188 allocator = 0;
189 if (surface == NULL)
190 return;
191 qemu_free(surface);
192}
193
194static DisplaySurface* sdl_resize_displaysurface(DisplaySurface *surface, int width, int height)
195{
196 sdl_free_displaysurface(surface);
197 return sdl_create_displaysurface(width, height);
bellard0f0b7262003-08-09 18:26:36 +0000198}
199
bellard3d11d0e2004-12-12 16:56:30 +0000200/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +0000201
bellard3d11d0e2004-12-12 16:56:30 +0000202#include "sdl_keysym.h"
bellarde58d12e2004-07-05 22:13:07 +0000203
bellard3d11d0e2004-12-12 16:56:30 +0000204static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000205
bellard3d11d0e2004-12-12 16:56:30 +0000206static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000207{
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 }
bellard3d11d0e2004-12-12 16:56:30 +0000217 return keysym2scancode(kbd_layout, keysym);
bellarde58d12e2004-07-05 22:13:07 +0000218}
219
bellard3d11d0e2004-12-12 16:56:30 +0000220/* specific keyboard conversions from scan codes */
221
222#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000223
224static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
225{
226 return ev->keysym.scancode;
227}
228
229#else
230
aliguori5368a422009-03-03 17:37:21 +0000231#if defined(SDL_VIDEO_DRIVER_X11)
232#include <X11/XKBlib.h>
233
234static int check_for_evdev(void)
235{
236 SDL_SysWMinfo info;
237 XkbDescPtr desc;
238 int has_evdev = 0;
239 const char *keycodes;
240
241 SDL_VERSION(&info.version);
242 if (!SDL_GetWMInfo(&info))
243 return 0;
244
245 desc = XkbGetKeyboard(info.info.x11.display,
246 XkbGBN_AllComponentsMask,
247 XkbUseCoreKbd);
248 if (desc == NULL || desc->names == NULL)
249 return 0;
250
251 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
252 if (keycodes == NULL)
253 fprintf(stderr, "could not lookup keycode name\n");
balrogbb0574f2009-04-13 11:33:46 +0000254 else if (strstart(keycodes, "evdev", NULL))
aliguori5368a422009-03-03 17:37:21 +0000255 has_evdev = 1;
balrogbb0574f2009-04-13 11:33:46 +0000256 else if (!strstart(keycodes, "xfree86", NULL))
aliguori5368a422009-03-03 17:37:21 +0000257 fprintf(stderr,
258 "unknown keycodes `%s', please report to qemu-devel@nongnu.org\n",
259 keycodes);
260
261 XkbFreeClientMap(desc, XkbGBN_AllComponentsMask, True);
262
263 return has_evdev;
264}
265#else
266static int check_for_evdev(void)
267{
268 return 0;
269}
270#endif
271
bellarde58d12e2004-07-05 22:13:07 +0000272static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
273{
274 int keycode;
aliguori5368a422009-03-03 17:37:21 +0000275 static int has_evdev = -1;
276
277 if (has_evdev == -1)
278 has_evdev = check_for_evdev();
bellarde58d12e2004-07-05 22:13:07 +0000279
280 keycode = ev->keysym.scancode;
281
282 if (keycode < 9) {
283 keycode = 0;
284 } else if (keycode < 97) {
285 keycode -= 8; /* just an offset */
aliguori5368a422009-03-03 17:37:21 +0000286 } else if (keycode < 158) {
bellarde58d12e2004-07-05 22:13:07 +0000287 /* use conversion table */
aliguori5368a422009-03-03 17:37:21 +0000288 if (has_evdev)
289 keycode = translate_evdev_keycode(keycode - 97);
290 else
291 keycode = translate_xfree86_keycode(keycode - 97);
292 } else if (keycode == 208) { /* Hiragana_Katakana */
293 keycode = 0x70;
294 } else if (keycode == 211) { /* backslash */
295 keycode = 0x73;
bellarde58d12e2004-07-05 22:13:07 +0000296 } else {
297 keycode = 0;
298 }
299 return keycode;
300}
301
302#endif
303
bellard32ff25b2004-10-03 14:33:54 +0000304static void reset_keys(void)
305{
306 int i;
307 for(i = 0; i < 256; i++) {
308 if (modifiers_state[i]) {
309 if (i & 0x80)
310 kbd_put_keycode(0xe0);
311 kbd_put_keycode(i | 0x80);
312 modifiers_state[i] = 0;
313 }
314 }
315}
316
bellard0f0b7262003-08-09 18:26:36 +0000317static void sdl_process_key(SDL_KeyboardEvent *ev)
318{
bellard32ff25b2004-10-03 14:33:54 +0000319 int keycode, v;
bellardde2200d2004-06-04 13:15:06 +0000320
321 if (ev->keysym.sym == SDLK_PAUSE) {
322 /* specific case */
323 v = 0;
324 if (ev->type == SDL_KEYUP)
325 v |= 0x80;
326 kbd_put_keycode(0xe1);
327 kbd_put_keycode(0x1d | v);
328 kbd_put_keycode(0x45 | v);
329 return;
330 }
331
bellard3d11d0e2004-12-12 16:56:30 +0000332 if (kbd_layout) {
333 keycode = sdl_keyevent_to_keycode_generic(ev);
334 } else {
335 keycode = sdl_keyevent_to_keycode(ev);
336 }
bellardde2200d2004-06-04 13:15:06 +0000337
338 switch(keycode) {
339 case 0x00:
340 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000341 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000342 return;
343 case 0x2a: /* Left Shift */
344 case 0x36: /* Right Shift */
345 case 0x1d: /* Left CTRL */
346 case 0x9d: /* Right CTRL */
347 case 0x38: /* Left ALT */
348 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000349 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000350 modifiers_state[keycode] = 0;
351 else
352 modifiers_state[keycode] = 1;
353 break;
354 case 0x45: /* num lock */
355 case 0x3a: /* caps lock */
356 /* SDL does not send the key up event, so we generate it */
357 kbd_put_keycode(keycode);
358 kbd_put_keycode(keycode | 0x80);
359 return;
bellard0f0b7262003-08-09 18:26:36 +0000360 }
bellardde2200d2004-06-04 13:15:06 +0000361
362 /* now send the key code */
363 if (keycode & 0x80)
364 kbd_put_keycode(0xe0);
365 if (ev->type == SDL_KEYUP)
366 kbd_put_keycode(keycode | 0x80);
367 else
368 kbd_put_keycode(keycode & 0x7f);
bellard0f0b7262003-08-09 18:26:36 +0000369}
370
bellard8a7ddc32004-03-31 19:00:16 +0000371static void sdl_update_caption(void)
372{
373 char buf[1024];
thsc35734b2007-03-19 15:17:08 +0000374 const char *status = "";
375
376 if (!vm_running)
377 status = " [Stopped]";
ths3780e192007-06-21 21:08:02 +0000378 else if (gui_grab) {
379 if (!alt_grab)
380 status = " - Press Ctrl-Alt to exit grab";
381 else
382 status = " - Press Ctrl-Alt-Shift to exit grab";
383 }
thsc35734b2007-03-19 15:17:08 +0000384
385 if (qemu_name)
386 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
387 else
388 snprintf(buf, sizeof(buf), "QEMU%s", status);
389
bellard8a7ddc32004-03-31 19:00:16 +0000390 SDL_WM_SetCaption(buf, "QEMU");
391}
392
bellard09b26c52006-04-12 21:09:08 +0000393static void sdl_hide_cursor(void)
394{
balrog9467cd42007-05-01 01:34:14 +0000395 if (!cursor_hide)
396 return;
397
bellard8785a8d2006-06-13 10:49:12 +0000398 if (kbd_mouse_is_absolute()) {
399 SDL_ShowCursor(1);
400 SDL_SetCursor(sdl_cursor_hidden);
401 } else {
402 SDL_ShowCursor(0);
403 }
bellard09b26c52006-04-12 21:09:08 +0000404}
405
406static void sdl_show_cursor(void)
407{
balrog9467cd42007-05-01 01:34:14 +0000408 if (!cursor_hide)
409 return;
410
bellard09b26c52006-04-12 21:09:08 +0000411 if (!kbd_mouse_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000412 SDL_ShowCursor(1);
thsd34cab92007-04-02 01:10:46 +0000413 if (guest_cursor &&
414 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
415 SDL_SetCursor(guest_sprite);
416 else
417 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000418 }
419}
420
bellard0f0b7262003-08-09 18:26:36 +0000421static void sdl_grab_start(void)
422{
thsd34cab92007-04-02 01:10:46 +0000423 if (guest_cursor) {
424 SDL_SetCursor(guest_sprite);
balrog08a2d4c2009-01-29 23:29:52 +0000425 if (!kbd_mouse_is_absolute() && !absolute_enabled)
426 SDL_WarpMouse(guest_x, guest_y);
thsd34cab92007-04-02 01:10:46 +0000427 } else
428 sdl_hide_cursor();
aliguori6bb81602009-01-15 20:47:45 +0000429
430 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
431 gui_grab = 1;
432 sdl_update_caption();
433 } else
434 sdl_show_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000435}
436
437static void sdl_grab_end(void)
438{
bellard0f0b7262003-08-09 18:26:36 +0000439 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard0f0b7262003-08-09 18:26:36 +0000440 gui_grab = 0;
thsd34cab92007-04-02 01:10:46 +0000441 sdl_show_cursor();
bellard8a7ddc32004-03-31 19:00:16 +0000442 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000443}
444
aurel324c44bdc2008-03-13 19:20:18 +0000445static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
bellard0f0b7262003-08-09 18:26:36 +0000446{
aurel324c44bdc2008-03-13 19:20:18 +0000447 int buttons;
bellard0f0b7262003-08-09 18:26:36 +0000448 buttons = 0;
449 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
450 buttons |= MOUSE_EVENT_LBUTTON;
451 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
452 buttons |= MOUSE_EVENT_RBUTTON;
453 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
454 buttons |= MOUSE_EVENT_MBUTTON;
bellard09b26c52006-04-12 21:09:08 +0000455
456 if (kbd_mouse_is_absolute()) {
457 if (!absolute_enabled) {
458 sdl_hide_cursor();
459 if (gui_grab) {
460 sdl_grab_end();
461 }
462 absolute_enabled = 1;
463 }
464
aurel324c44bdc2008-03-13 19:20:18 +0000465 dx = x * 0x7FFF / (width - 1);
466 dy = y * 0x7FFF / (height - 1);
ths455204e2007-01-05 16:42:13 +0000467 } else if (absolute_enabled) {
468 sdl_show_cursor();
469 absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000470 } else if (guest_cursor) {
aurel324c44bdc2008-03-13 19:20:18 +0000471 x -= guest_x;
472 y -= guest_y;
473 guest_x += x;
474 guest_y += y;
475 dx = x;
476 dy = y;
bellard09b26c52006-04-12 21:09:08 +0000477 }
478
bellard0f0b7262003-08-09 18:26:36 +0000479 kbd_mouse_event(dx, dy, dz, buttons);
480}
481
bellard8e9c4af2004-04-28 19:33:40 +0000482static void toggle_full_screen(DisplayState *ds)
483{
484 gui_fullscreen = !gui_fullscreen;
aliguori7b5d76d2009-03-13 15:02:13 +0000485 do_sdl_resize(real_screen->w, real_screen->h, real_screen->format->BitsPerPixel);
bellard8e9c4af2004-04-28 19:33:40 +0000486 if (gui_fullscreen) {
487 gui_saved_grab = gui_grab;
488 sdl_grab_start();
489 } else {
490 if (!gui_saved_grab)
491 sdl_grab_end();
492 }
pbrook95219892006-04-09 01:06:34 +0000493 vga_hw_invalidate();
494 vga_hw_update();
bellard8e9c4af2004-04-28 19:33:40 +0000495}
496
bellard0f0b7262003-08-09 18:26:36 +0000497static void sdl_refresh(DisplayState *ds)
498{
499 SDL_Event ev1, *ev = &ev1;
bellard8e9c4af2004-04-28 19:33:40 +0000500 int mod_state;
aurel324c44bdc2008-03-13 19:20:18 +0000501 int buttonstate = SDL_GetMouseState(NULL, NULL);
ths3b46e622007-09-17 08:09:54 +0000502
bellard8a7ddc32004-03-31 19:00:16 +0000503 if (last_vm_running != vm_running) {
504 last_vm_running = vm_running;
505 sdl_update_caption();
506 }
507
pbrook95219892006-04-09 01:06:34 +0000508 vga_hw_update();
aurel323bee8bd2008-04-13 16:08:37 +0000509 SDL_EnableUNICODE(!is_graphic_console());
bellard457831f2004-07-14 17:22:33 +0000510
bellard0f0b7262003-08-09 18:26:36 +0000511 while (SDL_PollEvent(ev)) {
512 switch (ev->type) {
513 case SDL_VIDEOEXPOSE:
aliguori7d957bd2009-01-15 22:14:11 +0000514 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
bellard0f0b7262003-08-09 18:26:36 +0000515 break;
516 case SDL_KEYDOWN:
517 case SDL_KEYUP:
518 if (ev->type == SDL_KEYDOWN) {
ths3780e192007-06-21 21:08:02 +0000519 if (!alt_grab) {
520 mod_state = (SDL_GetModState() & gui_grab_code) ==
521 gui_grab_code;
522 } else {
523 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
524 (gui_grab_code | KMOD_LSHIFT);
525 }
bellard8e9c4af2004-04-28 19:33:40 +0000526 gui_key_modifier_pressed = mod_state;
bellard457831f2004-07-14 17:22:33 +0000527 if (gui_key_modifier_pressed) {
bellard32ff25b2004-10-03 14:33:54 +0000528 int keycode;
529 keycode = sdl_keyevent_to_keycode(&ev->key);
530 switch(keycode) {
531 case 0x21: /* 'f' key on US keyboard */
bellard457831f2004-07-14 17:22:33 +0000532 toggle_full_screen(ds);
533 gui_keysym = 1;
534 break;
ths5fafdf22007-09-16 21:08:06 +0000535 case 0x02 ... 0x0a: /* '1' to '9' keys */
bellarddfd92d32006-08-17 10:42:46 +0000536 /* Reset the modifiers sent to the current console */
537 reset_keys();
bellard32ff25b2004-10-03 14:33:54 +0000538 console_select(keycode - 0x02);
pbrook95219892006-04-09 01:06:34 +0000539 if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000540 /* display grab if going to a text console */
541 if (gui_grab)
542 sdl_grab_end();
543 }
544 gui_keysym = 1;
545 break;
546 default:
547 break;
548 }
pbrook95219892006-04-09 01:06:34 +0000549 } else if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000550 int keysym;
551 keysym = 0;
552 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
553 switch(ev->key.keysym.sym) {
554 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
555 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
556 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
557 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
558 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
559 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
560 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
561 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
562 default: break;
563 }
564 } else {
565 switch(ev->key.keysym.sym) {
566 case SDLK_UP: keysym = QEMU_KEY_UP; break;
567 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
568 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
569 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
570 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
571 case SDLK_END: keysym = QEMU_KEY_END; break;
572 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
573 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
thse91c8a72007-06-03 13:35:16 +0000574 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
575 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
bellard457831f2004-07-14 17:22:33 +0000576 default: break;
577 }
578 }
579 if (keysym) {
580 kbd_put_keysym(keysym);
581 } else if (ev->key.keysym.unicode != 0) {
582 kbd_put_keysym(ev->key.keysym.unicode);
583 }
bellard8e9c4af2004-04-28 19:33:40 +0000584 }
585 } else if (ev->type == SDL_KEYUP) {
ths3780e192007-06-21 21:08:02 +0000586 if (!alt_grab) {
587 mod_state = (ev->key.keysym.mod & gui_grab_code);
588 } else {
589 mod_state = (ev->key.keysym.mod &
590 (gui_grab_code | KMOD_LSHIFT));
591 }
bellard8e9c4af2004-04-28 19:33:40 +0000592 if (!mod_state) {
593 if (gui_key_modifier_pressed) {
pbrook5b311872006-03-11 20:07:45 +0000594 gui_key_modifier_pressed = 0;
bellard457831f2004-07-14 17:22:33 +0000595 if (gui_keysym == 0) {
bellard32ff25b2004-10-03 14:33:54 +0000596 /* exit/enter grab if pressing Ctrl-Alt */
bellardc66b0d42006-06-13 12:03:53 +0000597 if (!gui_grab) {
598 /* if the application is not active,
599 do not try to enter grab state. It
600 prevents
601 'SDL_WM_GrabInput(SDL_GRAB_ON)'
602 from blocking all the application
603 (SDL bug). */
604 if (SDL_GetAppState() & SDL_APPACTIVE)
605 sdl_grab_start();
606 } else {
bellard8e9c4af2004-04-28 19:33:40 +0000607 sdl_grab_end();
bellardc66b0d42006-06-13 12:03:53 +0000608 }
bellard32ff25b2004-10-03 14:33:54 +0000609 /* SDL does not send back all the
610 modifiers key, so we must correct it */
611 reset_keys();
bellard8e9c4af2004-04-28 19:33:40 +0000612 break;
613 }
bellard8e9c4af2004-04-28 19:33:40 +0000614 gui_keysym = 0;
615 }
bellard0f0b7262003-08-09 18:26:36 +0000616 }
617 }
ths5fafdf22007-09-16 21:08:06 +0000618 if (is_graphic_console() && !gui_keysym)
bellard457831f2004-07-14 17:22:33 +0000619 sdl_process_key(&ev->key);
bellard0f0b7262003-08-09 18:26:36 +0000620 break;
621 case SDL_QUIT:
aliguori5b08fc12008-08-21 20:08:03 +0000622 if (!no_quit)
balrog731345e2007-06-21 17:56:50 +0000623 qemu_system_shutdown_request();
bellard0f0b7262003-08-09 18:26:36 +0000624 break;
625 case SDL_MOUSEMOTION:
ths455204e2007-01-05 16:42:13 +0000626 if (gui_grab || kbd_mouse_is_absolute() ||
627 absolute_enabled) {
aurel324c44bdc2008-03-13 19:20:18 +0000628 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
629 ev->motion.x, ev->motion.y, ev->motion.state);
bellard0f0b7262003-08-09 18:26:36 +0000630 }
631 break;
632 case SDL_MOUSEBUTTONDOWN:
633 case SDL_MOUSEBUTTONUP:
634 {
635 SDL_MouseButtonEvent *bev = &ev->button;
bellard09b26c52006-04-12 21:09:08 +0000636 if (!gui_grab && !kbd_mouse_is_absolute()) {
bellard0f0b7262003-08-09 18:26:36 +0000637 if (ev->type == SDL_MOUSEBUTTONDOWN &&
aurel324c44bdc2008-03-13 19:20:18 +0000638 (bev->button == SDL_BUTTON_LEFT)) {
bellard0f0b7262003-08-09 18:26:36 +0000639 /* start grabbing all events */
640 sdl_grab_start();
641 }
642 } else {
bellard18a6d282005-01-17 22:32:23 +0000643 int dz;
644 dz = 0;
aurel324c44bdc2008-03-13 19:20:18 +0000645 if (ev->type == SDL_MOUSEBUTTONDOWN) {
646 buttonstate |= SDL_BUTTON(bev->button);
647 } else {
648 buttonstate &= ~SDL_BUTTON(bev->button);
649 }
bellard18a6d282005-01-17 22:32:23 +0000650#ifdef SDL_BUTTON_WHEELUP
bellard09b26c52006-04-12 21:09:08 +0000651 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000652 dz = -1;
bellard09b26c52006-04-12 21:09:08 +0000653 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000654 dz = 1;
655 }
ths3b46e622007-09-17 08:09:54 +0000656#endif
aurel324c44bdc2008-03-13 19:20:18 +0000657 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
bellard0f0b7262003-08-09 18:26:36 +0000658 }
659 }
660 break;
bellard0294ffb2004-04-29 22:15:15 +0000661 case SDL_ACTIVEEVENT:
pbrook5b311872006-03-11 20:07:45 +0000662 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
663 !ev->active.gain && !gui_fullscreen_initial_grab) {
bellard0294ffb2004-04-29 22:15:15 +0000664 sdl_grab_end();
665 }
aurel32f442e082008-03-13 19:20:33 +0000666 if (ev->active.state & SDL_APPACTIVE) {
667 if (ev->active.gain) {
668 /* Back to default interval */
aliguori7d957bd2009-01-15 22:14:11 +0000669 dcl->gui_timer_interval = 0;
670 dcl->idle = 0;
aurel32f442e082008-03-13 19:20:33 +0000671 } else {
672 /* Sleeping interval */
aliguori7d957bd2009-01-15 22:14:11 +0000673 dcl->gui_timer_interval = 500;
674 dcl->idle = 1;
aurel32f442e082008-03-13 19:20:33 +0000675 }
676 }
bellard0294ffb2004-04-29 22:15:15 +0000677 break;
bellard0f0b7262003-08-09 18:26:36 +0000678 default:
679 break;
680 }
681 }
682}
683
thsd34cab92007-04-02 01:10:46 +0000684static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
685{
686 SDL_Rect dst = { x, y, w, h };
aliguori7d957bd2009-01-15 22:14:11 +0000687 SDL_FillRect(real_screen, &dst, c);
thsd34cab92007-04-02 01:10:46 +0000688}
689
690static void sdl_mouse_warp(int x, int y, int on)
691{
692 if (on) {
693 if (!guest_cursor)
694 sdl_show_cursor();
695 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
696 SDL_SetCursor(guest_sprite);
balrog08a2d4c2009-01-29 23:29:52 +0000697 if (!kbd_mouse_is_absolute() && !absolute_enabled)
698 SDL_WarpMouse(x, y);
thsd34cab92007-04-02 01:10:46 +0000699 }
700 } else if (gui_grab)
701 sdl_hide_cursor();
702 guest_cursor = on;
703 guest_x = x, guest_y = y;
704}
705
706static void sdl_mouse_define(int width, int height, int bpp,
707 int hot_x, int hot_y,
708 uint8_t *image, uint8_t *mask)
709{
710 uint8_t sprite[256], *line;
711 int x, y, dst, bypl, src = 0;
712 if (guest_sprite)
713 SDL_FreeCursor(guest_sprite);
714
715 memset(sprite, 0, 256);
716 bypl = ((width * bpp + 31) >> 5) << 2;
717 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
718 line = image;
719 for (x = 0; x < width; x ++, dst ++) {
720 switch (bpp) {
721 case 24:
722 src = *(line ++); src |= *(line ++); src |= *(line ++);
723 break;
724 case 16:
725 case 15:
726 src = *(line ++); src |= *(line ++);
727 break;
728 case 8:
729 src = *(line ++);
730 break;
731 case 4:
732 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
733 break;
734 case 2:
735 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
736 break;
737 case 1:
738 src = 1 & (line[x >> 3] >> (x & 7));
739 break;
740 }
741 if (!src)
742 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
743 }
744 }
745 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
746
747 if (guest_cursor &&
748 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
749 SDL_SetCursor(guest_sprite);
750}
751
ths5fafdf22007-09-16 21:08:06 +0000752static void sdl_cleanup(void)
bellard898712a2004-02-06 19:56:42 +0000753{
thsd34cab92007-04-02 01:10:46 +0000754 if (guest_sprite)
755 SDL_FreeCursor(guest_sprite);
malcd8ee7662009-05-17 18:26:05 +0400756 SDL_QuitSubSystem(SDL_INIT_VIDEO);
bellard898712a2004-02-06 19:56:42 +0000757}
758
ths43523e92007-02-18 18:19:32 +0000759void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
bellard0f0b7262003-08-09 18:26:36 +0000760{
761 int flags;
bellard09b26c52006-04-12 21:09:08 +0000762 uint8_t data = 0;
aliguori7b5d76d2009-03-13 15:02:13 +0000763 DisplayAllocator *da;
764 const SDL_VideoInfo *vi;
bellard0f0b7262003-08-09 18:26:36 +0000765
bellard3d11d0e2004-12-12 16:56:30 +0000766#if defined(__APPLE__)
767 /* always use generic keymaps */
768 if (!keyboard_layout)
769 keyboard_layout = "en-us";
770#endif
771 if(keyboard_layout) {
aliguori04837552009-03-06 20:27:10 +0000772 kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
bellard3d11d0e2004-12-12 16:56:30 +0000773 if (!kbd_layout)
774 exit(1);
775 }
776
ths43523e92007-02-18 18:19:32 +0000777 if (no_frame)
778 gui_noframe = 1;
779
bellard0f0b7262003-08-09 18:26:36 +0000780 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
781 if (SDL_Init (flags)) {
782 fprintf(stderr, "Could not initialize SDL - exiting\n");
783 exit(1);
784 }
aliguori7b5d76d2009-03-13 15:02:13 +0000785 vi = SDL_GetVideoInfo();
786 hostbpp = vi->vfmt->BitsPerPixel;
bellard0ae04d72003-09-30 21:09:16 +0000787
aliguori7d957bd2009-01-15 22:14:11 +0000788 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
aliguori7d957bd2009-01-15 22:14:11 +0000789 dcl->dpy_update = sdl_update;
790 dcl->dpy_resize = sdl_resize;
791 dcl->dpy_refresh = sdl_refresh;
792 dcl->dpy_setdata = sdl_setdata;
793 dcl->dpy_fill = sdl_fill;
thsd34cab92007-04-02 01:10:46 +0000794 ds->mouse_set = sdl_mouse_warp;
795 ds->cursor_define = sdl_mouse_define;
aliguori7d957bd2009-01-15 22:14:11 +0000796 register_displaychangelistener(ds, dcl);
bellard0f0b7262003-08-09 18:26:36 +0000797
aliguori7b5d76d2009-03-13 15:02:13 +0000798 da = qemu_mallocz(sizeof(DisplayAllocator));
799 da->create_displaysurface = sdl_create_displaysurface;
800 da->resize_displaysurface = sdl_resize_displaysurface;
801 da->free_displaysurface = sdl_free_displaysurface;
802 if (register_displayallocator(ds, da) == da) {
803 DisplaySurface *surf;
804 surf = sdl_create_displaysurface(ds_get_width(ds), ds_get_height(ds));
805 defaultallocator_free_displaysurface(ds->surface);
806 ds->surface = surf;
807 dpy_resize(ds);
808 }
809
bellard8a7ddc32004-03-31 19:00:16 +0000810 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000811 SDL_EnableKeyRepeat(250, 50);
812 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000813
bellard09b26c52006-04-12 21:09:08 +0000814 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
815 sdl_cursor_normal = SDL_GetCursor();
816
bellard898712a2004-02-06 19:56:42 +0000817 atexit(sdl_cleanup);
bellardd63d3072004-10-03 13:29:03 +0000818 if (full_screen) {
819 gui_fullscreen = 1;
820 gui_fullscreen_initial_grab = 1;
821 sdl_grab_start();
822 }
bellard0f0b7262003-08-09 18:26:36 +0000823}