blob: c685b81f6fb4b87596f363e9384ff39f0a320143 [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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "qemu-common.h"
25#include "console.h"
26#include "sysemu.h"
aliguori5368a422009-03-03 17:37:21 +000027#include "x_keymap.h"
bellard0f0b7262003-08-09 18:26:36 +000028
29#include <SDL.h>
aliguori5368a422009-03-03 17:37:21 +000030#include <SDL/SDL_syswm.h>
bellard0f0b7262003-08-09 18:26:36 +000031
bellard67b915a2004-03-31 23:37:16 +000032#ifndef _WIN32
33#include <signal.h>
34#endif
bellard0f0b7262003-08-09 18:26:36 +000035
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;
bellard0f0b7262003-08-09 18:26:36 +000056
57static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
58{
aliguori7d957bd2009-01-15 22:14:11 +000059 SDL_Rect rec;
60 rec.x = x;
61 rec.y = y;
62 rec.w = w;
63 rec.h = h;
bellard898712a2004-02-06 19:56:42 +000064 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
aliguori7d957bd2009-01-15 22:14:11 +000065
66 SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
pbrook6e600652009-01-21 01:50:17 +000067 SDL_UpdateRect(real_screen, x, y, w, h);
bellard0f0b7262003-08-09 18:26:36 +000068}
69
aliguori7d957bd2009-01-15 22:14:11 +000070static void sdl_setdata(DisplayState *ds)
71{
72 SDL_Rect rec;
73 rec.x = 0;
74 rec.y = 0;
75 rec.w = real_screen->w;
76 rec.h = real_screen->h;
77
78 if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
79
80 guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds),
81 ds_get_bits_per_pixel(ds), ds_get_linesize(ds),
82 ds->surface->pf.rmask, ds->surface->pf.gmask,
83 ds->surface->pf.bmask, ds->surface->pf.amask);
84}
85
86static void sdl_resize(DisplayState *ds)
bellard0f0b7262003-08-09 18:26:36 +000087{
88 int flags;
89
90 // printf("resizing to %d %d\n", w, h);
91
92 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
bellard8e9c4af2004-04-28 19:33:40 +000093 if (gui_fullscreen)
94 flags |= SDL_FULLSCREEN;
ths43523e92007-02-18 18:19:32 +000095 if (gui_noframe)
96 flags |= SDL_NOFRAME;
bellard9903da22005-10-30 23:19:10 +000097
balrog8bf66d42009-01-29 23:19:20 +000098 width = ds_get_width(ds);
99 height = ds_get_height(ds);
100 real_screen = SDL_SetVideoMode(width, height, 0, flags);
aliguori7d957bd2009-01-15 22:14:11 +0000101 if (!real_screen) {
bellard0f0b7262003-08-09 18:26:36 +0000102 fprintf(stderr, "Could not open SDL display\n");
103 exit(1);
104 }
blueswir1749ecd92008-07-24 11:25:30 +0000105
aliguori7d957bd2009-01-15 22:14:11 +0000106 sdl_setdata(ds);
bellard0f0b7262003-08-09 18:26:36 +0000107}
108
bellard3d11d0e2004-12-12 16:56:30 +0000109/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +0000110
bellard3d11d0e2004-12-12 16:56:30 +0000111#include "sdl_keysym.h"
112#include "keymaps.c"
bellarde58d12e2004-07-05 22:13:07 +0000113
bellard3d11d0e2004-12-12 16:56:30 +0000114static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000115
bellard3d11d0e2004-12-12 16:56:30 +0000116static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000117{
bellard3d11d0e2004-12-12 16:56:30 +0000118 int keysym;
119 /* workaround for X11+SDL bug with AltGR */
120 keysym = ev->keysym.sym;
121 if (keysym == 0 && ev->keysym.scancode == 113)
122 keysym = SDLK_MODE;
bellard60659e32006-08-19 14:27:31 +0000123 /* For Japanese key '\' and '|' */
124 if (keysym == 92 && ev->keysym.scancode == 133) {
125 keysym = 0xa5;
126 }
bellard3d11d0e2004-12-12 16:56:30 +0000127 return keysym2scancode(kbd_layout, keysym);
bellarde58d12e2004-07-05 22:13:07 +0000128}
129
bellard3d11d0e2004-12-12 16:56:30 +0000130/* specific keyboard conversions from scan codes */
131
132#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000133
134static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
135{
136 return ev->keysym.scancode;
137}
138
139#else
140
aliguori5368a422009-03-03 17:37:21 +0000141#if defined(SDL_VIDEO_DRIVER_X11)
142#include <X11/XKBlib.h>
143
144static int check_for_evdev(void)
145{
146 SDL_SysWMinfo info;
147 XkbDescPtr desc;
148 int has_evdev = 0;
149 const char *keycodes;
150
151 SDL_VERSION(&info.version);
152 if (!SDL_GetWMInfo(&info))
153 return 0;
154
155 desc = XkbGetKeyboard(info.info.x11.display,
156 XkbGBN_AllComponentsMask,
157 XkbUseCoreKbd);
158 if (desc == NULL || desc->names == NULL)
159 return 0;
160
161 keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);
162 if (keycodes == NULL)
163 fprintf(stderr, "could not lookup keycode name\n");
164 else if (strstart(keycodes, "evdev_", NULL))
165 has_evdev = 1;
166 else if (!strstart(keycodes, "xfree86_", NULL))
167 fprintf(stderr,
168 "unknown keycodes `%s', please report to qemu-devel@nongnu.org\n",
169 keycodes);
170
171 XkbFreeClientMap(desc, XkbGBN_AllComponentsMask, True);
172
173 return has_evdev;
174}
175#else
176static int check_for_evdev(void)
177{
178 return 0;
179}
180#endif
181
bellarde58d12e2004-07-05 22:13:07 +0000182static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
183{
184 int keycode;
aliguori5368a422009-03-03 17:37:21 +0000185 static int has_evdev = -1;
186
187 if (has_evdev == -1)
188 has_evdev = check_for_evdev();
bellarde58d12e2004-07-05 22:13:07 +0000189
190 keycode = ev->keysym.scancode;
191
192 if (keycode < 9) {
193 keycode = 0;
194 } else if (keycode < 97) {
195 keycode -= 8; /* just an offset */
aliguori5368a422009-03-03 17:37:21 +0000196 } else if (keycode < 158) {
bellarde58d12e2004-07-05 22:13:07 +0000197 /* use conversion table */
aliguori5368a422009-03-03 17:37:21 +0000198 if (has_evdev)
199 keycode = translate_evdev_keycode(keycode - 97);
200 else
201 keycode = translate_xfree86_keycode(keycode - 97);
202 } else if (keycode == 208) { /* Hiragana_Katakana */
203 keycode = 0x70;
204 } else if (keycode == 211) { /* backslash */
205 keycode = 0x73;
bellarde58d12e2004-07-05 22:13:07 +0000206 } else {
207 keycode = 0;
208 }
209 return keycode;
210}
211
212#endif
213
bellard32ff25b2004-10-03 14:33:54 +0000214static void reset_keys(void)
215{
216 int i;
217 for(i = 0; i < 256; i++) {
218 if (modifiers_state[i]) {
219 if (i & 0x80)
220 kbd_put_keycode(0xe0);
221 kbd_put_keycode(i | 0x80);
222 modifiers_state[i] = 0;
223 }
224 }
225}
226
bellard0f0b7262003-08-09 18:26:36 +0000227static void sdl_process_key(SDL_KeyboardEvent *ev)
228{
bellard32ff25b2004-10-03 14:33:54 +0000229 int keycode, v;
bellardde2200d2004-06-04 13:15:06 +0000230
231 if (ev->keysym.sym == SDLK_PAUSE) {
232 /* specific case */
233 v = 0;
234 if (ev->type == SDL_KEYUP)
235 v |= 0x80;
236 kbd_put_keycode(0xe1);
237 kbd_put_keycode(0x1d | v);
238 kbd_put_keycode(0x45 | v);
239 return;
240 }
241
bellard3d11d0e2004-12-12 16:56:30 +0000242 if (kbd_layout) {
243 keycode = sdl_keyevent_to_keycode_generic(ev);
244 } else {
245 keycode = sdl_keyevent_to_keycode(ev);
246 }
bellardde2200d2004-06-04 13:15:06 +0000247
248 switch(keycode) {
249 case 0x00:
250 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000251 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000252 return;
253 case 0x2a: /* Left Shift */
254 case 0x36: /* Right Shift */
255 case 0x1d: /* Left CTRL */
256 case 0x9d: /* Right CTRL */
257 case 0x38: /* Left ALT */
258 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000259 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000260 modifiers_state[keycode] = 0;
261 else
262 modifiers_state[keycode] = 1;
263 break;
264 case 0x45: /* num lock */
265 case 0x3a: /* caps lock */
266 /* SDL does not send the key up event, so we generate it */
267 kbd_put_keycode(keycode);
268 kbd_put_keycode(keycode | 0x80);
269 return;
bellard0f0b7262003-08-09 18:26:36 +0000270 }
bellardde2200d2004-06-04 13:15:06 +0000271
272 /* now send the key code */
273 if (keycode & 0x80)
274 kbd_put_keycode(0xe0);
275 if (ev->type == SDL_KEYUP)
276 kbd_put_keycode(keycode | 0x80);
277 else
278 kbd_put_keycode(keycode & 0x7f);
bellard0f0b7262003-08-09 18:26:36 +0000279}
280
bellard8a7ddc32004-03-31 19:00:16 +0000281static void sdl_update_caption(void)
282{
283 char buf[1024];
thsc35734b2007-03-19 15:17:08 +0000284 const char *status = "";
285
286 if (!vm_running)
287 status = " [Stopped]";
ths3780e192007-06-21 21:08:02 +0000288 else if (gui_grab) {
289 if (!alt_grab)
290 status = " - Press Ctrl-Alt to exit grab";
291 else
292 status = " - Press Ctrl-Alt-Shift to exit grab";
293 }
thsc35734b2007-03-19 15:17:08 +0000294
295 if (qemu_name)
296 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
297 else
298 snprintf(buf, sizeof(buf), "QEMU%s", status);
299
bellard8a7ddc32004-03-31 19:00:16 +0000300 SDL_WM_SetCaption(buf, "QEMU");
301}
302
bellard09b26c52006-04-12 21:09:08 +0000303static void sdl_hide_cursor(void)
304{
balrog9467cd42007-05-01 01:34:14 +0000305 if (!cursor_hide)
306 return;
307
bellard8785a8d2006-06-13 10:49:12 +0000308 if (kbd_mouse_is_absolute()) {
309 SDL_ShowCursor(1);
310 SDL_SetCursor(sdl_cursor_hidden);
311 } else {
312 SDL_ShowCursor(0);
313 }
bellard09b26c52006-04-12 21:09:08 +0000314}
315
316static void sdl_show_cursor(void)
317{
balrog9467cd42007-05-01 01:34:14 +0000318 if (!cursor_hide)
319 return;
320
bellard09b26c52006-04-12 21:09:08 +0000321 if (!kbd_mouse_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000322 SDL_ShowCursor(1);
thsd34cab92007-04-02 01:10:46 +0000323 if (guest_cursor &&
324 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
325 SDL_SetCursor(guest_sprite);
326 else
327 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000328 }
329}
330
bellard0f0b7262003-08-09 18:26:36 +0000331static void sdl_grab_start(void)
332{
thsd34cab92007-04-02 01:10:46 +0000333 if (guest_cursor) {
334 SDL_SetCursor(guest_sprite);
balrog08a2d4c2009-01-29 23:29:52 +0000335 if (!kbd_mouse_is_absolute() && !absolute_enabled)
336 SDL_WarpMouse(guest_x, guest_y);
thsd34cab92007-04-02 01:10:46 +0000337 } else
338 sdl_hide_cursor();
aliguori6bb81602009-01-15 20:47:45 +0000339
340 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
341 gui_grab = 1;
342 sdl_update_caption();
343 } else
344 sdl_show_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000345}
346
347static void sdl_grab_end(void)
348{
bellard0f0b7262003-08-09 18:26:36 +0000349 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard0f0b7262003-08-09 18:26:36 +0000350 gui_grab = 0;
thsd34cab92007-04-02 01:10:46 +0000351 sdl_show_cursor();
bellard8a7ddc32004-03-31 19:00:16 +0000352 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000353}
354
aurel324c44bdc2008-03-13 19:20:18 +0000355static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
bellard0f0b7262003-08-09 18:26:36 +0000356{
aurel324c44bdc2008-03-13 19:20:18 +0000357 int buttons;
bellard0f0b7262003-08-09 18:26:36 +0000358 buttons = 0;
359 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
360 buttons |= MOUSE_EVENT_LBUTTON;
361 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
362 buttons |= MOUSE_EVENT_RBUTTON;
363 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
364 buttons |= MOUSE_EVENT_MBUTTON;
bellard09b26c52006-04-12 21:09:08 +0000365
366 if (kbd_mouse_is_absolute()) {
367 if (!absolute_enabled) {
368 sdl_hide_cursor();
369 if (gui_grab) {
370 sdl_grab_end();
371 }
372 absolute_enabled = 1;
373 }
374
aurel324c44bdc2008-03-13 19:20:18 +0000375 dx = x * 0x7FFF / (width - 1);
376 dy = y * 0x7FFF / (height - 1);
ths455204e2007-01-05 16:42:13 +0000377 } else if (absolute_enabled) {
378 sdl_show_cursor();
379 absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000380 } else if (guest_cursor) {
aurel324c44bdc2008-03-13 19:20:18 +0000381 x -= guest_x;
382 y -= guest_y;
383 guest_x += x;
384 guest_y += y;
385 dx = x;
386 dy = y;
bellard09b26c52006-04-12 21:09:08 +0000387 }
388
bellard0f0b7262003-08-09 18:26:36 +0000389 kbd_mouse_event(dx, dy, dz, buttons);
390}
391
bellard8e9c4af2004-04-28 19:33:40 +0000392static void toggle_full_screen(DisplayState *ds)
393{
394 gui_fullscreen = !gui_fullscreen;
aliguori7d957bd2009-01-15 22:14:11 +0000395 sdl_resize(ds);
bellard8e9c4af2004-04-28 19:33:40 +0000396 if (gui_fullscreen) {
397 gui_saved_grab = gui_grab;
398 sdl_grab_start();
399 } else {
400 if (!gui_saved_grab)
401 sdl_grab_end();
402 }
pbrook95219892006-04-09 01:06:34 +0000403 vga_hw_invalidate();
404 vga_hw_update();
bellard8e9c4af2004-04-28 19:33:40 +0000405}
406
bellard0f0b7262003-08-09 18:26:36 +0000407static void sdl_refresh(DisplayState *ds)
408{
409 SDL_Event ev1, *ev = &ev1;
bellard8e9c4af2004-04-28 19:33:40 +0000410 int mod_state;
aurel324c44bdc2008-03-13 19:20:18 +0000411 int buttonstate = SDL_GetMouseState(NULL, NULL);
ths3b46e622007-09-17 08:09:54 +0000412
bellard8a7ddc32004-03-31 19:00:16 +0000413 if (last_vm_running != vm_running) {
414 last_vm_running = vm_running;
415 sdl_update_caption();
416 }
417
pbrook95219892006-04-09 01:06:34 +0000418 vga_hw_update();
aurel323bee8bd2008-04-13 16:08:37 +0000419 SDL_EnableUNICODE(!is_graphic_console());
bellard457831f2004-07-14 17:22:33 +0000420
bellard0f0b7262003-08-09 18:26:36 +0000421 while (SDL_PollEvent(ev)) {
422 switch (ev->type) {
423 case SDL_VIDEOEXPOSE:
aliguori7d957bd2009-01-15 22:14:11 +0000424 sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
bellard0f0b7262003-08-09 18:26:36 +0000425 break;
426 case SDL_KEYDOWN:
427 case SDL_KEYUP:
428 if (ev->type == SDL_KEYDOWN) {
ths3780e192007-06-21 21:08:02 +0000429 if (!alt_grab) {
430 mod_state = (SDL_GetModState() & gui_grab_code) ==
431 gui_grab_code;
432 } else {
433 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
434 (gui_grab_code | KMOD_LSHIFT);
435 }
bellard8e9c4af2004-04-28 19:33:40 +0000436 gui_key_modifier_pressed = mod_state;
bellard457831f2004-07-14 17:22:33 +0000437 if (gui_key_modifier_pressed) {
bellard32ff25b2004-10-03 14:33:54 +0000438 int keycode;
439 keycode = sdl_keyevent_to_keycode(&ev->key);
440 switch(keycode) {
441 case 0x21: /* 'f' key on US keyboard */
bellard457831f2004-07-14 17:22:33 +0000442 toggle_full_screen(ds);
443 gui_keysym = 1;
444 break;
ths5fafdf22007-09-16 21:08:06 +0000445 case 0x02 ... 0x0a: /* '1' to '9' keys */
bellarddfd92d32006-08-17 10:42:46 +0000446 /* Reset the modifiers sent to the current console */
447 reset_keys();
bellard32ff25b2004-10-03 14:33:54 +0000448 console_select(keycode - 0x02);
pbrook95219892006-04-09 01:06:34 +0000449 if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000450 /* display grab if going to a text console */
451 if (gui_grab)
452 sdl_grab_end();
453 }
454 gui_keysym = 1;
455 break;
456 default:
457 break;
458 }
pbrook95219892006-04-09 01:06:34 +0000459 } else if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000460 int keysym;
461 keysym = 0;
462 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
463 switch(ev->key.keysym.sym) {
464 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
465 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
466 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
467 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
468 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
469 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
470 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
471 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
472 default: break;
473 }
474 } else {
475 switch(ev->key.keysym.sym) {
476 case SDLK_UP: keysym = QEMU_KEY_UP; break;
477 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
478 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
479 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
480 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
481 case SDLK_END: keysym = QEMU_KEY_END; break;
482 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
483 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
thse91c8a72007-06-03 13:35:16 +0000484 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
485 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
bellard457831f2004-07-14 17:22:33 +0000486 default: break;
487 }
488 }
489 if (keysym) {
490 kbd_put_keysym(keysym);
491 } else if (ev->key.keysym.unicode != 0) {
492 kbd_put_keysym(ev->key.keysym.unicode);
493 }
bellard8e9c4af2004-04-28 19:33:40 +0000494 }
495 } else if (ev->type == SDL_KEYUP) {
ths3780e192007-06-21 21:08:02 +0000496 if (!alt_grab) {
497 mod_state = (ev->key.keysym.mod & gui_grab_code);
498 } else {
499 mod_state = (ev->key.keysym.mod &
500 (gui_grab_code | KMOD_LSHIFT));
501 }
bellard8e9c4af2004-04-28 19:33:40 +0000502 if (!mod_state) {
503 if (gui_key_modifier_pressed) {
pbrook5b311872006-03-11 20:07:45 +0000504 gui_key_modifier_pressed = 0;
bellard457831f2004-07-14 17:22:33 +0000505 if (gui_keysym == 0) {
bellard32ff25b2004-10-03 14:33:54 +0000506 /* exit/enter grab if pressing Ctrl-Alt */
bellardc66b0d42006-06-13 12:03:53 +0000507 if (!gui_grab) {
508 /* if the application is not active,
509 do not try to enter grab state. It
510 prevents
511 'SDL_WM_GrabInput(SDL_GRAB_ON)'
512 from blocking all the application
513 (SDL bug). */
514 if (SDL_GetAppState() & SDL_APPACTIVE)
515 sdl_grab_start();
516 } else {
bellard8e9c4af2004-04-28 19:33:40 +0000517 sdl_grab_end();
bellardc66b0d42006-06-13 12:03:53 +0000518 }
bellard32ff25b2004-10-03 14:33:54 +0000519 /* SDL does not send back all the
520 modifiers key, so we must correct it */
521 reset_keys();
bellard8e9c4af2004-04-28 19:33:40 +0000522 break;
523 }
bellard8e9c4af2004-04-28 19:33:40 +0000524 gui_keysym = 0;
525 }
bellard0f0b7262003-08-09 18:26:36 +0000526 }
527 }
ths5fafdf22007-09-16 21:08:06 +0000528 if (is_graphic_console() && !gui_keysym)
bellard457831f2004-07-14 17:22:33 +0000529 sdl_process_key(&ev->key);
bellard0f0b7262003-08-09 18:26:36 +0000530 break;
531 case SDL_QUIT:
aliguori5b08fc12008-08-21 20:08:03 +0000532 if (!no_quit)
balrog731345e2007-06-21 17:56:50 +0000533 qemu_system_shutdown_request();
bellard0f0b7262003-08-09 18:26:36 +0000534 break;
535 case SDL_MOUSEMOTION:
ths455204e2007-01-05 16:42:13 +0000536 if (gui_grab || kbd_mouse_is_absolute() ||
537 absolute_enabled) {
aurel324c44bdc2008-03-13 19:20:18 +0000538 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
539 ev->motion.x, ev->motion.y, ev->motion.state);
bellard0f0b7262003-08-09 18:26:36 +0000540 }
541 break;
542 case SDL_MOUSEBUTTONDOWN:
543 case SDL_MOUSEBUTTONUP:
544 {
545 SDL_MouseButtonEvent *bev = &ev->button;
bellard09b26c52006-04-12 21:09:08 +0000546 if (!gui_grab && !kbd_mouse_is_absolute()) {
bellard0f0b7262003-08-09 18:26:36 +0000547 if (ev->type == SDL_MOUSEBUTTONDOWN &&
aurel324c44bdc2008-03-13 19:20:18 +0000548 (bev->button == SDL_BUTTON_LEFT)) {
bellard0f0b7262003-08-09 18:26:36 +0000549 /* start grabbing all events */
550 sdl_grab_start();
551 }
552 } else {
bellard18a6d282005-01-17 22:32:23 +0000553 int dz;
554 dz = 0;
aurel324c44bdc2008-03-13 19:20:18 +0000555 if (ev->type == SDL_MOUSEBUTTONDOWN) {
556 buttonstate |= SDL_BUTTON(bev->button);
557 } else {
558 buttonstate &= ~SDL_BUTTON(bev->button);
559 }
bellard18a6d282005-01-17 22:32:23 +0000560#ifdef SDL_BUTTON_WHEELUP
bellard09b26c52006-04-12 21:09:08 +0000561 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000562 dz = -1;
bellard09b26c52006-04-12 21:09:08 +0000563 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000564 dz = 1;
565 }
ths3b46e622007-09-17 08:09:54 +0000566#endif
aurel324c44bdc2008-03-13 19:20:18 +0000567 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
bellard0f0b7262003-08-09 18:26:36 +0000568 }
569 }
570 break;
bellard0294ffb2004-04-29 22:15:15 +0000571 case SDL_ACTIVEEVENT:
pbrook5b311872006-03-11 20:07:45 +0000572 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
573 !ev->active.gain && !gui_fullscreen_initial_grab) {
bellard0294ffb2004-04-29 22:15:15 +0000574 sdl_grab_end();
575 }
aurel32f442e082008-03-13 19:20:33 +0000576 if (ev->active.state & SDL_APPACTIVE) {
577 if (ev->active.gain) {
578 /* Back to default interval */
aliguori7d957bd2009-01-15 22:14:11 +0000579 dcl->gui_timer_interval = 0;
580 dcl->idle = 0;
aurel32f442e082008-03-13 19:20:33 +0000581 } else {
582 /* Sleeping interval */
aliguori7d957bd2009-01-15 22:14:11 +0000583 dcl->gui_timer_interval = 500;
584 dcl->idle = 1;
aurel32f442e082008-03-13 19:20:33 +0000585 }
586 }
bellard0294ffb2004-04-29 22:15:15 +0000587 break;
bellard0f0b7262003-08-09 18:26:36 +0000588 default:
589 break;
590 }
591 }
592}
593
thsd34cab92007-04-02 01:10:46 +0000594static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
595{
596 SDL_Rect dst = { x, y, w, h };
aliguori7d957bd2009-01-15 22:14:11 +0000597 SDL_FillRect(real_screen, &dst, c);
thsd34cab92007-04-02 01:10:46 +0000598}
599
600static void sdl_mouse_warp(int x, int y, int on)
601{
602 if (on) {
603 if (!guest_cursor)
604 sdl_show_cursor();
605 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
606 SDL_SetCursor(guest_sprite);
balrog08a2d4c2009-01-29 23:29:52 +0000607 if (!kbd_mouse_is_absolute() && !absolute_enabled)
608 SDL_WarpMouse(x, y);
thsd34cab92007-04-02 01:10:46 +0000609 }
610 } else if (gui_grab)
611 sdl_hide_cursor();
612 guest_cursor = on;
613 guest_x = x, guest_y = y;
614}
615
616static void sdl_mouse_define(int width, int height, int bpp,
617 int hot_x, int hot_y,
618 uint8_t *image, uint8_t *mask)
619{
620 uint8_t sprite[256], *line;
621 int x, y, dst, bypl, src = 0;
622 if (guest_sprite)
623 SDL_FreeCursor(guest_sprite);
624
625 memset(sprite, 0, 256);
626 bypl = ((width * bpp + 31) >> 5) << 2;
627 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
628 line = image;
629 for (x = 0; x < width; x ++, dst ++) {
630 switch (bpp) {
631 case 24:
632 src = *(line ++); src |= *(line ++); src |= *(line ++);
633 break;
634 case 16:
635 case 15:
636 src = *(line ++); src |= *(line ++);
637 break;
638 case 8:
639 src = *(line ++);
640 break;
641 case 4:
642 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
643 break;
644 case 2:
645 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
646 break;
647 case 1:
648 src = 1 & (line[x >> 3] >> (x & 7));
649 break;
650 }
651 if (!src)
652 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
653 }
654 }
655 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
656
657 if (guest_cursor &&
658 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
659 SDL_SetCursor(guest_sprite);
660}
661
ths5fafdf22007-09-16 21:08:06 +0000662static void sdl_cleanup(void)
bellard898712a2004-02-06 19:56:42 +0000663{
thsd34cab92007-04-02 01:10:46 +0000664 if (guest_sprite)
665 SDL_FreeCursor(guest_sprite);
bellard898712a2004-02-06 19:56:42 +0000666 SDL_Quit();
667}
668
ths43523e92007-02-18 18:19:32 +0000669void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
bellard0f0b7262003-08-09 18:26:36 +0000670{
671 int flags;
bellard09b26c52006-04-12 21:09:08 +0000672 uint8_t data = 0;
bellard0f0b7262003-08-09 18:26:36 +0000673
bellard3d11d0e2004-12-12 16:56:30 +0000674#if defined(__APPLE__)
675 /* always use generic keymaps */
676 if (!keyboard_layout)
677 keyboard_layout = "en-us";
678#endif
679 if(keyboard_layout) {
680 kbd_layout = init_keyboard_layout(keyboard_layout);
681 if (!kbd_layout)
682 exit(1);
683 }
684
ths43523e92007-02-18 18:19:32 +0000685 if (no_frame)
686 gui_noframe = 1;
687
bellard0f0b7262003-08-09 18:26:36 +0000688 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
689 if (SDL_Init (flags)) {
690 fprintf(stderr, "Could not initialize SDL - exiting\n");
691 exit(1);
692 }
bellard0ae04d72003-09-30 21:09:16 +0000693
aliguori7d957bd2009-01-15 22:14:11 +0000694 dcl = qemu_mallocz(sizeof(DisplayChangeListener));
aliguori7d957bd2009-01-15 22:14:11 +0000695 dcl->dpy_update = sdl_update;
696 dcl->dpy_resize = sdl_resize;
697 dcl->dpy_refresh = sdl_refresh;
698 dcl->dpy_setdata = sdl_setdata;
699 dcl->dpy_fill = sdl_fill;
thsd34cab92007-04-02 01:10:46 +0000700 ds->mouse_set = sdl_mouse_warp;
701 ds->cursor_define = sdl_mouse_define;
aliguori7d957bd2009-01-15 22:14:11 +0000702 register_displaychangelistener(ds, dcl);
bellard0f0b7262003-08-09 18:26:36 +0000703
bellard8a7ddc32004-03-31 19:00:16 +0000704 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000705 SDL_EnableKeyRepeat(250, 50);
706 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000707
bellard09b26c52006-04-12 21:09:08 +0000708 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
709 sdl_cursor_normal = SDL_GetCursor();
710
bellard898712a2004-02-06 19:56:42 +0000711 atexit(sdl_cleanup);
bellardd63d3072004-10-03 13:29:03 +0000712 if (full_screen) {
713 gui_fullscreen = 1;
714 gui_fullscreen_initial_grab = 1;
715 sdl_grab_start();
716 }
bellard0f0b7262003-08-09 18:26:36 +0000717}