blob: 2ce6e815224dd28c908e30fa47e1ec991c158a92 [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"
bellard0f0b7262003-08-09 18:26:36 +000027
28#include <SDL.h>
29
bellard67b915a2004-03-31 23:37:16 +000030#ifndef _WIN32
31#include <signal.h>
32#endif
bellard0f0b7262003-08-09 18:26:36 +000033
34static SDL_Surface *screen;
35static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000036static int last_vm_running;
bellard8e9c4af2004-04-28 19:33:40 +000037static int gui_saved_grab;
38static int gui_fullscreen;
ths43523e92007-02-18 18:19:32 +000039static int gui_noframe;
bellard8e9c4af2004-04-28 19:33:40 +000040static int gui_key_modifier_pressed;
41static int gui_keysym;
bellardd63d3072004-10-03 13:29:03 +000042static int gui_fullscreen_initial_grab;
bellard32ff25b2004-10-03 14:33:54 +000043static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
44static uint8_t modifiers_state[256];
bellard09b26c52006-04-12 21:09:08 +000045static int width, height;
46static SDL_Cursor *sdl_cursor_normal;
47static SDL_Cursor *sdl_cursor_hidden;
48static int absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +000049static int guest_cursor = 0;
50static int guest_x, guest_y;
51static SDL_Cursor *guest_sprite = 0;
bellard0f0b7262003-08-09 18:26:36 +000052
53static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
54{
bellard898712a2004-02-06 19:56:42 +000055 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
bellard0f0b7262003-08-09 18:26:36 +000056 SDL_UpdateRect(screen, x, y, w, h);
57}
58
59static void sdl_resize(DisplayState *ds, int w, int h)
60{
61 int flags;
62
63 // printf("resizing to %d %d\n", w, h);
64
65 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
bellard8e9c4af2004-04-28 19:33:40 +000066 if (gui_fullscreen)
67 flags |= SDL_FULLSCREEN;
ths43523e92007-02-18 18:19:32 +000068 if (gui_noframe)
69 flags |= SDL_NOFRAME;
bellard9903da22005-10-30 23:19:10 +000070
bellard09b26c52006-04-12 21:09:08 +000071 width = w;
72 height = h;
73
bellard9903da22005-10-30 23:19:10 +000074 again:
bellard0f0b7262003-08-09 18:26:36 +000075 screen = SDL_SetVideoMode(w, h, 0, flags);
76 if (!screen) {
77 fprintf(stderr, "Could not open SDL display\n");
78 exit(1);
79 }
bellard9903da22005-10-30 23:19:10 +000080 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
81 flags &= ~SDL_HWSURFACE;
82 goto again;
83 }
84
85 if (!screen->pixels) {
86 fprintf(stderr, "Could not open SDL display\n");
87 exit(1);
88 }
bellard0f0b7262003-08-09 18:26:36 +000089 ds->data = screen->pixels;
90 ds->linesize = screen->pitch;
91 ds->depth = screen->format->BitsPerPixel;
blueswir1749ecd92008-07-24 11:25:30 +000092 /* SDL BitsPerPixel never indicates any values other than
93 multiples of 8, so we need to check for strange depths. */
94 if (ds->depth == 16) {
95 uint32_t mask;
96
97 mask = screen->format->Rmask;
98 mask |= screen->format->Gmask;
99 mask |= screen->format->Bmask;
100 if ((mask & 0x8000) == 0)
101 ds->depth = 15;
102 }
aliguoric7bd7be2008-07-23 13:24:33 +0000103 if (ds->depth == 32 && screen->format->Rshift == 0) {
bellardd3079cd2006-05-10 22:17:36 +0000104 ds->bgr = 1;
105 } else {
106 ds->bgr = 0;
107 }
bellard457831f2004-07-14 17:22:33 +0000108 ds->width = w;
109 ds->height = h;
bellard0f0b7262003-08-09 18:26:36 +0000110}
111
bellard3d11d0e2004-12-12 16:56:30 +0000112/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +0000113
bellard3d11d0e2004-12-12 16:56:30 +0000114#include "sdl_keysym.h"
115#include "keymaps.c"
bellarde58d12e2004-07-05 22:13:07 +0000116
bellard3d11d0e2004-12-12 16:56:30 +0000117static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000118
bellard3d11d0e2004-12-12 16:56:30 +0000119static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000120{
bellard3d11d0e2004-12-12 16:56:30 +0000121 int keysym;
122 /* workaround for X11+SDL bug with AltGR */
123 keysym = ev->keysym.sym;
124 if (keysym == 0 && ev->keysym.scancode == 113)
125 keysym = SDLK_MODE;
bellard60659e32006-08-19 14:27:31 +0000126 /* For Japanese key '\' and '|' */
127 if (keysym == 92 && ev->keysym.scancode == 133) {
128 keysym = 0xa5;
129 }
bellard3d11d0e2004-12-12 16:56:30 +0000130 return keysym2scancode(kbd_layout, keysym);
bellarde58d12e2004-07-05 22:13:07 +0000131}
132
bellard3d11d0e2004-12-12 16:56:30 +0000133/* specific keyboard conversions from scan codes */
134
135#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000136
137static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
138{
139 return ev->keysym.scancode;
140}
141
142#else
143
bellarde58d12e2004-07-05 22:13:07 +0000144static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
145{
146 int keycode;
147
148 keycode = ev->keysym.scancode;
149
150 if (keycode < 9) {
151 keycode = 0;
152 } else if (keycode < 97) {
153 keycode -= 8; /* just an offset */
bellard60659e32006-08-19 14:27:31 +0000154 } else if (keycode < 212) {
bellarde58d12e2004-07-05 22:13:07 +0000155 /* use conversion table */
ths6070dd02007-01-24 21:40:21 +0000156 keycode = _translate_keycode(keycode - 97);
bellarde58d12e2004-07-05 22:13:07 +0000157 } else {
158 keycode = 0;
159 }
160 return keycode;
161}
162
163#endif
164
bellard32ff25b2004-10-03 14:33:54 +0000165static void reset_keys(void)
166{
167 int i;
168 for(i = 0; i < 256; i++) {
169 if (modifiers_state[i]) {
170 if (i & 0x80)
171 kbd_put_keycode(0xe0);
172 kbd_put_keycode(i | 0x80);
173 modifiers_state[i] = 0;
174 }
175 }
176}
177
bellard0f0b7262003-08-09 18:26:36 +0000178static void sdl_process_key(SDL_KeyboardEvent *ev)
179{
bellard32ff25b2004-10-03 14:33:54 +0000180 int keycode, v;
bellardde2200d2004-06-04 13:15:06 +0000181
182 if (ev->keysym.sym == SDLK_PAUSE) {
183 /* specific case */
184 v = 0;
185 if (ev->type == SDL_KEYUP)
186 v |= 0x80;
187 kbd_put_keycode(0xe1);
188 kbd_put_keycode(0x1d | v);
189 kbd_put_keycode(0x45 | v);
190 return;
191 }
192
bellard3d11d0e2004-12-12 16:56:30 +0000193 if (kbd_layout) {
194 keycode = sdl_keyevent_to_keycode_generic(ev);
195 } else {
196 keycode = sdl_keyevent_to_keycode(ev);
197 }
bellardde2200d2004-06-04 13:15:06 +0000198
199 switch(keycode) {
200 case 0x00:
201 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000202 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000203 return;
204 case 0x2a: /* Left Shift */
205 case 0x36: /* Right Shift */
206 case 0x1d: /* Left CTRL */
207 case 0x9d: /* Right CTRL */
208 case 0x38: /* Left ALT */
209 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000210 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000211 modifiers_state[keycode] = 0;
212 else
213 modifiers_state[keycode] = 1;
214 break;
215 case 0x45: /* num lock */
216 case 0x3a: /* caps lock */
217 /* SDL does not send the key up event, so we generate it */
218 kbd_put_keycode(keycode);
219 kbd_put_keycode(keycode | 0x80);
220 return;
bellard0f0b7262003-08-09 18:26:36 +0000221 }
bellardde2200d2004-06-04 13:15:06 +0000222
223 /* now send the key code */
224 if (keycode & 0x80)
225 kbd_put_keycode(0xe0);
226 if (ev->type == SDL_KEYUP)
227 kbd_put_keycode(keycode | 0x80);
228 else
229 kbd_put_keycode(keycode & 0x7f);
bellard0f0b7262003-08-09 18:26:36 +0000230}
231
bellard8a7ddc32004-03-31 19:00:16 +0000232static void sdl_update_caption(void)
233{
234 char buf[1024];
thsc35734b2007-03-19 15:17:08 +0000235 const char *status = "";
236
237 if (!vm_running)
238 status = " [Stopped]";
ths3780e192007-06-21 21:08:02 +0000239 else if (gui_grab) {
240 if (!alt_grab)
241 status = " - Press Ctrl-Alt to exit grab";
242 else
243 status = " - Press Ctrl-Alt-Shift to exit grab";
244 }
thsc35734b2007-03-19 15:17:08 +0000245
246 if (qemu_name)
247 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
248 else
249 snprintf(buf, sizeof(buf), "QEMU%s", status);
250
bellard8a7ddc32004-03-31 19:00:16 +0000251 SDL_WM_SetCaption(buf, "QEMU");
252}
253
bellard09b26c52006-04-12 21:09:08 +0000254static void sdl_hide_cursor(void)
255{
balrog9467cd42007-05-01 01:34:14 +0000256 if (!cursor_hide)
257 return;
258
bellard8785a8d2006-06-13 10:49:12 +0000259 if (kbd_mouse_is_absolute()) {
260 SDL_ShowCursor(1);
261 SDL_SetCursor(sdl_cursor_hidden);
262 } else {
263 SDL_ShowCursor(0);
264 }
bellard09b26c52006-04-12 21:09:08 +0000265}
266
267static void sdl_show_cursor(void)
268{
balrog9467cd42007-05-01 01:34:14 +0000269 if (!cursor_hide)
270 return;
271
bellard09b26c52006-04-12 21:09:08 +0000272 if (!kbd_mouse_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000273 SDL_ShowCursor(1);
thsd34cab92007-04-02 01:10:46 +0000274 if (guest_cursor &&
275 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
276 SDL_SetCursor(guest_sprite);
277 else
278 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000279 }
280}
281
bellard0f0b7262003-08-09 18:26:36 +0000282static void sdl_grab_start(void)
283{
thsd34cab92007-04-02 01:10:46 +0000284 if (guest_cursor) {
285 SDL_SetCursor(guest_sprite);
286 SDL_WarpMouse(guest_x, guest_y);
287 } else
288 sdl_hide_cursor();
aliguori6bb81602009-01-15 20:47:45 +0000289
290 if (SDL_WM_GrabInput(SDL_GRAB_ON) == SDL_GRAB_ON) {
291 gui_grab = 1;
292 sdl_update_caption();
293 } else
294 sdl_show_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000295}
296
297static void sdl_grab_end(void)
298{
bellard0f0b7262003-08-09 18:26:36 +0000299 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard0f0b7262003-08-09 18:26:36 +0000300 gui_grab = 0;
thsd34cab92007-04-02 01:10:46 +0000301 sdl_show_cursor();
bellard8a7ddc32004-03-31 19:00:16 +0000302 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000303}
304
aurel324c44bdc2008-03-13 19:20:18 +0000305static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
bellard0f0b7262003-08-09 18:26:36 +0000306{
aurel324c44bdc2008-03-13 19:20:18 +0000307 int buttons;
bellard0f0b7262003-08-09 18:26:36 +0000308 buttons = 0;
309 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
310 buttons |= MOUSE_EVENT_LBUTTON;
311 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
312 buttons |= MOUSE_EVENT_RBUTTON;
313 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
314 buttons |= MOUSE_EVENT_MBUTTON;
bellard09b26c52006-04-12 21:09:08 +0000315
316 if (kbd_mouse_is_absolute()) {
317 if (!absolute_enabled) {
318 sdl_hide_cursor();
319 if (gui_grab) {
320 sdl_grab_end();
321 }
322 absolute_enabled = 1;
323 }
324
aurel324c44bdc2008-03-13 19:20:18 +0000325 dx = x * 0x7FFF / (width - 1);
326 dy = y * 0x7FFF / (height - 1);
ths455204e2007-01-05 16:42:13 +0000327 } else if (absolute_enabled) {
328 sdl_show_cursor();
329 absolute_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000330 } else if (guest_cursor) {
aurel324c44bdc2008-03-13 19:20:18 +0000331 x -= guest_x;
332 y -= guest_y;
333 guest_x += x;
334 guest_y += y;
335 dx = x;
336 dy = y;
bellard09b26c52006-04-12 21:09:08 +0000337 }
338
bellard0f0b7262003-08-09 18:26:36 +0000339 kbd_mouse_event(dx, dy, dz, buttons);
340}
341
bellard8e9c4af2004-04-28 19:33:40 +0000342static void toggle_full_screen(DisplayState *ds)
343{
344 gui_fullscreen = !gui_fullscreen;
345 sdl_resize(ds, screen->w, screen->h);
346 if (gui_fullscreen) {
347 gui_saved_grab = gui_grab;
348 sdl_grab_start();
349 } else {
350 if (!gui_saved_grab)
351 sdl_grab_end();
352 }
pbrook95219892006-04-09 01:06:34 +0000353 vga_hw_invalidate();
354 vga_hw_update();
bellard8e9c4af2004-04-28 19:33:40 +0000355}
356
bellard0f0b7262003-08-09 18:26:36 +0000357static void sdl_refresh(DisplayState *ds)
358{
359 SDL_Event ev1, *ev = &ev1;
bellard8e9c4af2004-04-28 19:33:40 +0000360 int mod_state;
aurel324c44bdc2008-03-13 19:20:18 +0000361 int buttonstate = SDL_GetMouseState(NULL, NULL);
ths3b46e622007-09-17 08:09:54 +0000362
bellard8a7ddc32004-03-31 19:00:16 +0000363 if (last_vm_running != vm_running) {
364 last_vm_running = vm_running;
365 sdl_update_caption();
366 }
367
pbrook95219892006-04-09 01:06:34 +0000368 vga_hw_update();
aurel323bee8bd2008-04-13 16:08:37 +0000369 SDL_EnableUNICODE(!is_graphic_console());
bellard457831f2004-07-14 17:22:33 +0000370
bellard0f0b7262003-08-09 18:26:36 +0000371 while (SDL_PollEvent(ev)) {
372 switch (ev->type) {
373 case SDL_VIDEOEXPOSE:
374 sdl_update(ds, 0, 0, screen->w, screen->h);
375 break;
376 case SDL_KEYDOWN:
377 case SDL_KEYUP:
378 if (ev->type == SDL_KEYDOWN) {
ths3780e192007-06-21 21:08:02 +0000379 if (!alt_grab) {
380 mod_state = (SDL_GetModState() & gui_grab_code) ==
381 gui_grab_code;
382 } else {
383 mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
384 (gui_grab_code | KMOD_LSHIFT);
385 }
bellard8e9c4af2004-04-28 19:33:40 +0000386 gui_key_modifier_pressed = mod_state;
bellard457831f2004-07-14 17:22:33 +0000387 if (gui_key_modifier_pressed) {
bellard32ff25b2004-10-03 14:33:54 +0000388 int keycode;
389 keycode = sdl_keyevent_to_keycode(&ev->key);
390 switch(keycode) {
391 case 0x21: /* 'f' key on US keyboard */
bellard457831f2004-07-14 17:22:33 +0000392 toggle_full_screen(ds);
393 gui_keysym = 1;
394 break;
ths5fafdf22007-09-16 21:08:06 +0000395 case 0x02 ... 0x0a: /* '1' to '9' keys */
bellarddfd92d32006-08-17 10:42:46 +0000396 /* Reset the modifiers sent to the current console */
397 reset_keys();
bellard32ff25b2004-10-03 14:33:54 +0000398 console_select(keycode - 0x02);
pbrook95219892006-04-09 01:06:34 +0000399 if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000400 /* display grab if going to a text console */
401 if (gui_grab)
402 sdl_grab_end();
403 }
404 gui_keysym = 1;
405 break;
406 default:
407 break;
408 }
pbrook95219892006-04-09 01:06:34 +0000409 } else if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000410 int keysym;
411 keysym = 0;
412 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
413 switch(ev->key.keysym.sym) {
414 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
415 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
416 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
417 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
418 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
419 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
420 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
421 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
422 default: break;
423 }
424 } else {
425 switch(ev->key.keysym.sym) {
426 case SDLK_UP: keysym = QEMU_KEY_UP; break;
427 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
428 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
429 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
430 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
431 case SDLK_END: keysym = QEMU_KEY_END; break;
432 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
433 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
thse91c8a72007-06-03 13:35:16 +0000434 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break;
435 case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
bellard457831f2004-07-14 17:22:33 +0000436 default: break;
437 }
438 }
439 if (keysym) {
440 kbd_put_keysym(keysym);
441 } else if (ev->key.keysym.unicode != 0) {
442 kbd_put_keysym(ev->key.keysym.unicode);
443 }
bellard8e9c4af2004-04-28 19:33:40 +0000444 }
445 } else if (ev->type == SDL_KEYUP) {
ths3780e192007-06-21 21:08:02 +0000446 if (!alt_grab) {
447 mod_state = (ev->key.keysym.mod & gui_grab_code);
448 } else {
449 mod_state = (ev->key.keysym.mod &
450 (gui_grab_code | KMOD_LSHIFT));
451 }
bellard8e9c4af2004-04-28 19:33:40 +0000452 if (!mod_state) {
453 if (gui_key_modifier_pressed) {
pbrook5b311872006-03-11 20:07:45 +0000454 gui_key_modifier_pressed = 0;
bellard457831f2004-07-14 17:22:33 +0000455 if (gui_keysym == 0) {
bellard32ff25b2004-10-03 14:33:54 +0000456 /* exit/enter grab if pressing Ctrl-Alt */
bellardc66b0d42006-06-13 12:03:53 +0000457 if (!gui_grab) {
458 /* if the application is not active,
459 do not try to enter grab state. It
460 prevents
461 'SDL_WM_GrabInput(SDL_GRAB_ON)'
462 from blocking all the application
463 (SDL bug). */
464 if (SDL_GetAppState() & SDL_APPACTIVE)
465 sdl_grab_start();
466 } else {
bellard8e9c4af2004-04-28 19:33:40 +0000467 sdl_grab_end();
bellardc66b0d42006-06-13 12:03:53 +0000468 }
bellard32ff25b2004-10-03 14:33:54 +0000469 /* SDL does not send back all the
470 modifiers key, so we must correct it */
471 reset_keys();
bellard8e9c4af2004-04-28 19:33:40 +0000472 break;
473 }
bellard8e9c4af2004-04-28 19:33:40 +0000474 gui_keysym = 0;
475 }
bellard0f0b7262003-08-09 18:26:36 +0000476 }
477 }
ths5fafdf22007-09-16 21:08:06 +0000478 if (is_graphic_console() && !gui_keysym)
bellard457831f2004-07-14 17:22:33 +0000479 sdl_process_key(&ev->key);
bellard0f0b7262003-08-09 18:26:36 +0000480 break;
481 case SDL_QUIT:
aliguori5b08fc12008-08-21 20:08:03 +0000482 if (!no_quit)
balrog731345e2007-06-21 17:56:50 +0000483 qemu_system_shutdown_request();
bellard0f0b7262003-08-09 18:26:36 +0000484 break;
485 case SDL_MOUSEMOTION:
ths455204e2007-01-05 16:42:13 +0000486 if (gui_grab || kbd_mouse_is_absolute() ||
487 absolute_enabled) {
aurel324c44bdc2008-03-13 19:20:18 +0000488 sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
489 ev->motion.x, ev->motion.y, ev->motion.state);
bellard0f0b7262003-08-09 18:26:36 +0000490 }
491 break;
492 case SDL_MOUSEBUTTONDOWN:
493 case SDL_MOUSEBUTTONUP:
494 {
495 SDL_MouseButtonEvent *bev = &ev->button;
bellard09b26c52006-04-12 21:09:08 +0000496 if (!gui_grab && !kbd_mouse_is_absolute()) {
bellard0f0b7262003-08-09 18:26:36 +0000497 if (ev->type == SDL_MOUSEBUTTONDOWN &&
aurel324c44bdc2008-03-13 19:20:18 +0000498 (bev->button == SDL_BUTTON_LEFT)) {
bellard0f0b7262003-08-09 18:26:36 +0000499 /* start grabbing all events */
500 sdl_grab_start();
501 }
502 } else {
bellard18a6d282005-01-17 22:32:23 +0000503 int dz;
504 dz = 0;
aurel324c44bdc2008-03-13 19:20:18 +0000505 if (ev->type == SDL_MOUSEBUTTONDOWN) {
506 buttonstate |= SDL_BUTTON(bev->button);
507 } else {
508 buttonstate &= ~SDL_BUTTON(bev->button);
509 }
bellard18a6d282005-01-17 22:32:23 +0000510#ifdef SDL_BUTTON_WHEELUP
bellard09b26c52006-04-12 21:09:08 +0000511 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000512 dz = -1;
bellard09b26c52006-04-12 21:09:08 +0000513 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000514 dz = 1;
515 }
ths3b46e622007-09-17 08:09:54 +0000516#endif
aurel324c44bdc2008-03-13 19:20:18 +0000517 sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
bellard0f0b7262003-08-09 18:26:36 +0000518 }
519 }
520 break;
bellard0294ffb2004-04-29 22:15:15 +0000521 case SDL_ACTIVEEVENT:
pbrook5b311872006-03-11 20:07:45 +0000522 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
523 !ev->active.gain && !gui_fullscreen_initial_grab) {
bellard0294ffb2004-04-29 22:15:15 +0000524 sdl_grab_end();
525 }
aurel32f442e082008-03-13 19:20:33 +0000526 if (ev->active.state & SDL_APPACTIVE) {
527 if (ev->active.gain) {
528 /* Back to default interval */
529 ds->gui_timer_interval = 0;
aliguoribcfad702008-08-21 20:08:55 +0000530 ds->idle = 0;
aurel32f442e082008-03-13 19:20:33 +0000531 } else {
532 /* Sleeping interval */
533 ds->gui_timer_interval = 500;
aliguoribcfad702008-08-21 20:08:55 +0000534 ds->idle = 1;
aurel32f442e082008-03-13 19:20:33 +0000535 }
536 }
bellard0294ffb2004-04-29 22:15:15 +0000537 break;
bellard0f0b7262003-08-09 18:26:36 +0000538 default:
539 break;
540 }
541 }
542}
543
thsd34cab92007-04-02 01:10:46 +0000544static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
545{
546 SDL_Rect dst = { x, y, w, h };
547 SDL_FillRect(screen, &dst, c);
548}
549
550static void sdl_mouse_warp(int x, int y, int on)
551{
552 if (on) {
553 if (!guest_cursor)
554 sdl_show_cursor();
555 if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
556 SDL_SetCursor(guest_sprite);
557 SDL_WarpMouse(x, y);
558 }
559 } else if (gui_grab)
560 sdl_hide_cursor();
561 guest_cursor = on;
562 guest_x = x, guest_y = y;
563}
564
565static void sdl_mouse_define(int width, int height, int bpp,
566 int hot_x, int hot_y,
567 uint8_t *image, uint8_t *mask)
568{
569 uint8_t sprite[256], *line;
570 int x, y, dst, bypl, src = 0;
571 if (guest_sprite)
572 SDL_FreeCursor(guest_sprite);
573
574 memset(sprite, 0, 256);
575 bypl = ((width * bpp + 31) >> 5) << 2;
576 for (y = 0, dst = 0; y < height; y ++, image += bypl) {
577 line = image;
578 for (x = 0; x < width; x ++, dst ++) {
579 switch (bpp) {
580 case 24:
581 src = *(line ++); src |= *(line ++); src |= *(line ++);
582 break;
583 case 16:
584 case 15:
585 src = *(line ++); src |= *(line ++);
586 break;
587 case 8:
588 src = *(line ++);
589 break;
590 case 4:
591 src = 0xf & (line[x >> 1] >> ((x & 1)) << 2);
592 break;
593 case 2:
594 src = 3 & (line[x >> 2] >> ((x & 3)) << 1);
595 break;
596 case 1:
597 src = 1 & (line[x >> 3] >> (x & 7));
598 break;
599 }
600 if (!src)
601 sprite[dst >> 3] |= (1 << (~dst & 7)) & mask[dst >> 3];
602 }
603 }
604 guest_sprite = SDL_CreateCursor(sprite, mask, width, height, hot_x, hot_y);
605
606 if (guest_cursor &&
607 (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
608 SDL_SetCursor(guest_sprite);
609}
610
ths5fafdf22007-09-16 21:08:06 +0000611static void sdl_cleanup(void)
bellard898712a2004-02-06 19:56:42 +0000612{
thsd34cab92007-04-02 01:10:46 +0000613 if (guest_sprite)
614 SDL_FreeCursor(guest_sprite);
bellard898712a2004-02-06 19:56:42 +0000615 SDL_Quit();
616}
617
ths43523e92007-02-18 18:19:32 +0000618void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
bellard0f0b7262003-08-09 18:26:36 +0000619{
620 int flags;
bellard09b26c52006-04-12 21:09:08 +0000621 uint8_t data = 0;
bellard0f0b7262003-08-09 18:26:36 +0000622
bellard3d11d0e2004-12-12 16:56:30 +0000623#if defined(__APPLE__)
624 /* always use generic keymaps */
625 if (!keyboard_layout)
626 keyboard_layout = "en-us";
627#endif
628 if(keyboard_layout) {
629 kbd_layout = init_keyboard_layout(keyboard_layout);
630 if (!kbd_layout)
631 exit(1);
632 }
633
ths43523e92007-02-18 18:19:32 +0000634 if (no_frame)
635 gui_noframe = 1;
636
bellard0f0b7262003-08-09 18:26:36 +0000637 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
638 if (SDL_Init (flags)) {
639 fprintf(stderr, "Could not initialize SDL - exiting\n");
640 exit(1);
641 }
bellard0ae04d72003-09-30 21:09:16 +0000642
bellard0f0b7262003-08-09 18:26:36 +0000643 ds->dpy_update = sdl_update;
644 ds->dpy_resize = sdl_resize;
645 ds->dpy_refresh = sdl_refresh;
thsd34cab92007-04-02 01:10:46 +0000646 ds->dpy_fill = sdl_fill;
647 ds->mouse_set = sdl_mouse_warp;
648 ds->cursor_define = sdl_mouse_define;
bellard0f0b7262003-08-09 18:26:36 +0000649
650 sdl_resize(ds, 640, 400);
bellard8a7ddc32004-03-31 19:00:16 +0000651 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000652 SDL_EnableKeyRepeat(250, 50);
653 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000654
bellard09b26c52006-04-12 21:09:08 +0000655 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
656 sdl_cursor_normal = SDL_GetCursor();
657
bellard898712a2004-02-06 19:56:42 +0000658 atexit(sdl_cleanup);
bellardd63d3072004-10-03 13:29:03 +0000659 if (full_screen) {
660 gui_fullscreen = 1;
661 gui_fullscreen_initial_grab = 1;
662 sdl_grab_start();
663 }
bellard0f0b7262003-08-09 18:26:36 +0000664}