blob: b58e9d67949821e6a4da312f92bbf766d1b30931 [file] [log] [blame]
bellard0f0b7262003-08-09 18:26:36 +00001/*
2 * QEMU SDL display driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * 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 */
bellard67b915a2004-03-31 23:37:16 +000024#include "vl.h"
bellard0f0b7262003-08-09 18:26:36 +000025
26#include <SDL.h>
27
bellard67b915a2004-03-31 23:37:16 +000028#ifndef _WIN32
29#include <signal.h>
30#endif
bellard0f0b7262003-08-09 18:26:36 +000031
32static SDL_Surface *screen;
33static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
bellard8a7ddc32004-03-31 19:00:16 +000034static int last_vm_running;
bellard8e9c4af2004-04-28 19:33:40 +000035static int gui_saved_grab;
36static int gui_fullscreen;
37static int gui_key_modifier_pressed;
38static int gui_keysym;
bellardd63d3072004-10-03 13:29:03 +000039static int gui_fullscreen_initial_grab;
bellard32ff25b2004-10-03 14:33:54 +000040static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
41static uint8_t modifiers_state[256];
bellard09b26c52006-04-12 21:09:08 +000042static int width, height;
43static SDL_Cursor *sdl_cursor_normal;
44static SDL_Cursor *sdl_cursor_hidden;
45static int absolute_enabled = 0;
bellard0f0b7262003-08-09 18:26:36 +000046
47static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
48{
bellard898712a2004-02-06 19:56:42 +000049 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
bellard0f0b7262003-08-09 18:26:36 +000050 SDL_UpdateRect(screen, x, y, w, h);
51}
52
53static void sdl_resize(DisplayState *ds, int w, int h)
54{
55 int flags;
56
57 // printf("resizing to %d %d\n", w, h);
58
59 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
bellard8e9c4af2004-04-28 19:33:40 +000060 if (gui_fullscreen)
61 flags |= SDL_FULLSCREEN;
bellard9903da22005-10-30 23:19:10 +000062
bellard09b26c52006-04-12 21:09:08 +000063 width = w;
64 height = h;
65
bellard9903da22005-10-30 23:19:10 +000066 again:
bellard0f0b7262003-08-09 18:26:36 +000067 screen = SDL_SetVideoMode(w, h, 0, flags);
68 if (!screen) {
69 fprintf(stderr, "Could not open SDL display\n");
70 exit(1);
71 }
bellard9903da22005-10-30 23:19:10 +000072 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
73 flags &= ~SDL_HWSURFACE;
74 goto again;
75 }
76
77 if (!screen->pixels) {
78 fprintf(stderr, "Could not open SDL display\n");
79 exit(1);
80 }
bellard0f0b7262003-08-09 18:26:36 +000081 ds->data = screen->pixels;
82 ds->linesize = screen->pitch;
83 ds->depth = screen->format->BitsPerPixel;
bellardd3079cd2006-05-10 22:17:36 +000084 if (ds->depth == 32 && screen->format->Rshift == 0) {
85 ds->bgr = 1;
86 } else {
87 ds->bgr = 0;
88 }
bellard457831f2004-07-14 17:22:33 +000089 ds->width = w;
90 ds->height = h;
bellard0f0b7262003-08-09 18:26:36 +000091}
92
bellard3d11d0e2004-12-12 16:56:30 +000093/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +000094
bellard3d11d0e2004-12-12 16:56:30 +000095#include "sdl_keysym.h"
96#include "keymaps.c"
bellarde58d12e2004-07-05 22:13:07 +000097
bellard3d11d0e2004-12-12 16:56:30 +000098static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +000099
bellard3d11d0e2004-12-12 16:56:30 +0000100static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000101{
bellard3d11d0e2004-12-12 16:56:30 +0000102 int keysym;
103 /* workaround for X11+SDL bug with AltGR */
104 keysym = ev->keysym.sym;
105 if (keysym == 0 && ev->keysym.scancode == 113)
106 keysym = SDLK_MODE;
bellard60659e32006-08-19 14:27:31 +0000107 /* For Japanese key '\' and '|' */
108 if (keysym == 92 && ev->keysym.scancode == 133) {
109 keysym = 0xa5;
110 }
bellard3d11d0e2004-12-12 16:56:30 +0000111 return keysym2scancode(kbd_layout, keysym);
bellarde58d12e2004-07-05 22:13:07 +0000112}
113
bellard3d11d0e2004-12-12 16:56:30 +0000114/* specific keyboard conversions from scan codes */
115
116#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000117
118static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
119{
120 return ev->keysym.scancode;
121}
122
123#else
124
bellard60659e32006-08-19 14:27:31 +0000125static const uint8_t x_keycode_to_pc_keycode[115] = {
bellardde2200d2004-06-04 13:15:06 +0000126 0xc7, /* 97 Home */
127 0xc8, /* 98 Up */
128 0xc9, /* 99 PgUp */
129 0xcb, /* 100 Left */
bellard0f0b7262003-08-09 18:26:36 +0000130 0x4c, /* 101 KP-5 */
bellardde2200d2004-06-04 13:15:06 +0000131 0xcd, /* 102 Right */
132 0xcf, /* 103 End */
133 0xd0, /* 104 Down */
134 0xd1, /* 105 PgDn */
135 0xd2, /* 106 Ins */
136 0xd3, /* 107 Del */
137 0x9c, /* 108 Enter */
138 0x9d, /* 109 Ctrl-R */
bellard22a56b82004-06-05 08:32:36 +0000139 0x0, /* 110 Pause */
bellardde2200d2004-06-04 13:15:06 +0000140 0xb7, /* 111 Print */
141 0xb5, /* 112 Divide */
142 0xb8, /* 113 Alt-R */
143 0xc6, /* 114 Break */
bellard0f0b7262003-08-09 18:26:36 +0000144 0x0, /* 115 */
145 0x0, /* 116 */
146 0x0, /* 117 */
147 0x0, /* 118 */
148 0x0, /* 119 */
bellard60659e32006-08-19 14:27:31 +0000149 0x0, /* 120 */
bellard0f0b7262003-08-09 18:26:36 +0000150 0x0, /* 121 */
151 0x0, /* 122 */
bellard60659e32006-08-19 14:27:31 +0000152 0x0, /* 123 */
bellard0f0b7262003-08-09 18:26:36 +0000153 0x0, /* 124 */
154 0x0, /* 125 */
155 0x0, /* 126 */
156 0x0, /* 127 */
157 0x0, /* 128 */
bellardb71e95f2004-05-20 13:08:06 +0000158 0x79, /* 129 Henkan */
bellard0f0b7262003-08-09 18:26:36 +0000159 0x0, /* 130 */
bellardb71e95f2004-05-20 13:08:06 +0000160 0x7b, /* 131 Muhenkan */
bellard0f0b7262003-08-09 18:26:36 +0000161 0x0, /* 132 */
bellardb71e95f2004-05-20 13:08:06 +0000162 0x7d, /* 133 Yen */
bellard0f0b7262003-08-09 18:26:36 +0000163 0x0, /* 134 */
164 0x0, /* 135 */
165 0x47, /* 136 KP_7 */
166 0x48, /* 137 KP_8 */
167 0x49, /* 138 KP_9 */
168 0x4b, /* 139 KP_4 */
169 0x4c, /* 140 KP_5 */
170 0x4d, /* 141 KP_6 */
171 0x4f, /* 142 KP_1 */
172 0x50, /* 143 KP_2 */
173 0x51, /* 144 KP_3 */
174 0x52, /* 145 KP_0 */
175 0x53, /* 146 KP_. */
176 0x47, /* 147 KP_HOME */
177 0x48, /* 148 KP_UP */
178 0x49, /* 149 KP_PgUp */
179 0x4b, /* 150 KP_Left */
180 0x4c, /* 151 KP_ */
181 0x4d, /* 152 KP_Right */
182 0x4f, /* 153 KP_End */
183 0x50, /* 154 KP_Down */
184 0x51, /* 155 KP_PgDn */
185 0x52, /* 156 KP_Ins */
186 0x53, /* 157 KP_Del */
bellard60659e32006-08-19 14:27:31 +0000187 0x0, /* 158 */
188 0x0, /* 159 */
189 0x0, /* 160 */
190 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 170 */
191 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 180 */
192 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 190 */
193 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* 200 */
194 0x0, /* 201 */
195 0x0, /* 202 */
196 0x0, /* 203 */
197 0x0, /* 204 */
198 0x0, /* 205 */
199 0x0, /* 206 */
200 0x0, /* 207 */
201 0x70, /* 208 Hiragana_Katakana */
202 0x0, /* 209 */
203 0x0, /* 210 */
204 0x73, /* 211 backslash */
bellard0f0b7262003-08-09 18:26:36 +0000205};
206
bellarde58d12e2004-07-05 22:13:07 +0000207static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
208{
209 int keycode;
210
211 keycode = ev->keysym.scancode;
212
213 if (keycode < 9) {
214 keycode = 0;
215 } else if (keycode < 97) {
216 keycode -= 8; /* just an offset */
bellard60659e32006-08-19 14:27:31 +0000217 } else if (keycode < 212) {
bellarde58d12e2004-07-05 22:13:07 +0000218 /* use conversion table */
219 keycode = x_keycode_to_pc_keycode[keycode - 97];
220 } else {
221 keycode = 0;
222 }
223 return keycode;
224}
225
226#endif
227
bellard32ff25b2004-10-03 14:33:54 +0000228static void reset_keys(void)
229{
230 int i;
231 for(i = 0; i < 256; i++) {
232 if (modifiers_state[i]) {
233 if (i & 0x80)
234 kbd_put_keycode(0xe0);
235 kbd_put_keycode(i | 0x80);
236 modifiers_state[i] = 0;
237 }
238 }
239}
240
bellard0f0b7262003-08-09 18:26:36 +0000241static void sdl_process_key(SDL_KeyboardEvent *ev)
242{
bellard32ff25b2004-10-03 14:33:54 +0000243 int keycode, v;
bellardde2200d2004-06-04 13:15:06 +0000244
245 if (ev->keysym.sym == SDLK_PAUSE) {
246 /* specific case */
247 v = 0;
248 if (ev->type == SDL_KEYUP)
249 v |= 0x80;
250 kbd_put_keycode(0xe1);
251 kbd_put_keycode(0x1d | v);
252 kbd_put_keycode(0x45 | v);
253 return;
254 }
255
bellard3d11d0e2004-12-12 16:56:30 +0000256 if (kbd_layout) {
257 keycode = sdl_keyevent_to_keycode_generic(ev);
258 } else {
259 keycode = sdl_keyevent_to_keycode(ev);
260 }
bellardde2200d2004-06-04 13:15:06 +0000261
262 switch(keycode) {
263 case 0x00:
264 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000265 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000266 return;
267 case 0x2a: /* Left Shift */
268 case 0x36: /* Right Shift */
269 case 0x1d: /* Left CTRL */
270 case 0x9d: /* Right CTRL */
271 case 0x38: /* Left ALT */
272 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000273 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000274 modifiers_state[keycode] = 0;
275 else
276 modifiers_state[keycode] = 1;
277 break;
278 case 0x45: /* num lock */
279 case 0x3a: /* caps lock */
280 /* SDL does not send the key up event, so we generate it */
281 kbd_put_keycode(keycode);
282 kbd_put_keycode(keycode | 0x80);
283 return;
bellard0f0b7262003-08-09 18:26:36 +0000284 }
bellardde2200d2004-06-04 13:15:06 +0000285
286 /* now send the key code */
287 if (keycode & 0x80)
288 kbd_put_keycode(0xe0);
289 if (ev->type == SDL_KEYUP)
290 kbd_put_keycode(keycode | 0x80);
291 else
292 kbd_put_keycode(keycode & 0x7f);
bellard0f0b7262003-08-09 18:26:36 +0000293}
294
bellard8a7ddc32004-03-31 19:00:16 +0000295static void sdl_update_caption(void)
296{
297 char buf[1024];
298 strcpy(buf, "QEMU");
299 if (!vm_running) {
300 strcat(buf, " [Stopped]");
301 }
302 if (gui_grab) {
bellard32ff25b2004-10-03 14:33:54 +0000303 strcat(buf, " - Press Ctrl-Alt to exit grab");
bellard8a7ddc32004-03-31 19:00:16 +0000304 }
305 SDL_WM_SetCaption(buf, "QEMU");
306}
307
bellard09b26c52006-04-12 21:09:08 +0000308static void sdl_hide_cursor(void)
309{
bellard8785a8d2006-06-13 10:49:12 +0000310 if (kbd_mouse_is_absolute()) {
311 SDL_ShowCursor(1);
312 SDL_SetCursor(sdl_cursor_hidden);
313 } else {
314 SDL_ShowCursor(0);
315 }
bellard09b26c52006-04-12 21:09:08 +0000316}
317
318static void sdl_show_cursor(void)
319{
320 if (!kbd_mouse_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000321 SDL_ShowCursor(1);
bellard09b26c52006-04-12 21:09:08 +0000322 }
323}
324
bellard0f0b7262003-08-09 18:26:36 +0000325static void sdl_grab_start(void)
326{
bellard09b26c52006-04-12 21:09:08 +0000327 sdl_hide_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000328 SDL_WM_GrabInput(SDL_GRAB_ON);
329 /* dummy read to avoid moving the mouse */
330 SDL_GetRelativeMouseState(NULL, NULL);
331 gui_grab = 1;
bellard8a7ddc32004-03-31 19:00:16 +0000332 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000333}
334
335static void sdl_grab_end(void)
336{
bellard0f0b7262003-08-09 18:26:36 +0000337 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard09b26c52006-04-12 21:09:08 +0000338 sdl_show_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000339 gui_grab = 0;
bellard8a7ddc32004-03-31 19:00:16 +0000340 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000341}
342
bellard18a6d282005-01-17 22:32:23 +0000343static void sdl_send_mouse_event(int dz)
bellard0f0b7262003-08-09 18:26:36 +0000344{
bellard18a6d282005-01-17 22:32:23 +0000345 int dx, dy, state, buttons;
bellard0f0b7262003-08-09 18:26:36 +0000346 state = SDL_GetRelativeMouseState(&dx, &dy);
347 buttons = 0;
348 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
349 buttons |= MOUSE_EVENT_LBUTTON;
350 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
351 buttons |= MOUSE_EVENT_RBUTTON;
352 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
353 buttons |= MOUSE_EVENT_MBUTTON;
bellard09b26c52006-04-12 21:09:08 +0000354
355 if (kbd_mouse_is_absolute()) {
356 if (!absolute_enabled) {
357 sdl_hide_cursor();
358 if (gui_grab) {
359 sdl_grab_end();
360 }
361 absolute_enabled = 1;
362 }
363
364 SDL_GetMouseState(&dx, &dy);
365 dx = dx * 0x7FFF / width;
366 dy = dy * 0x7FFF / height;
367 }
368
bellard0f0b7262003-08-09 18:26:36 +0000369 kbd_mouse_event(dx, dy, dz, buttons);
370}
371
bellard8e9c4af2004-04-28 19:33:40 +0000372static void toggle_full_screen(DisplayState *ds)
373{
374 gui_fullscreen = !gui_fullscreen;
375 sdl_resize(ds, screen->w, screen->h);
376 if (gui_fullscreen) {
377 gui_saved_grab = gui_grab;
378 sdl_grab_start();
379 } else {
380 if (!gui_saved_grab)
381 sdl_grab_end();
382 }
pbrook95219892006-04-09 01:06:34 +0000383 vga_hw_invalidate();
384 vga_hw_update();
bellard8e9c4af2004-04-28 19:33:40 +0000385}
386
bellard0f0b7262003-08-09 18:26:36 +0000387static void sdl_refresh(DisplayState *ds)
388{
389 SDL_Event ev1, *ev = &ev1;
bellard8e9c4af2004-04-28 19:33:40 +0000390 int mod_state;
391
bellard8a7ddc32004-03-31 19:00:16 +0000392 if (last_vm_running != vm_running) {
393 last_vm_running = vm_running;
394 sdl_update_caption();
395 }
396
pbrook95219892006-04-09 01:06:34 +0000397 vga_hw_update();
bellard457831f2004-07-14 17:22:33 +0000398
bellard0f0b7262003-08-09 18:26:36 +0000399 while (SDL_PollEvent(ev)) {
400 switch (ev->type) {
401 case SDL_VIDEOEXPOSE:
402 sdl_update(ds, 0, 0, screen->w, screen->h);
403 break;
404 case SDL_KEYDOWN:
405 case SDL_KEYUP:
406 if (ev->type == SDL_KEYDOWN) {
bellard32ff25b2004-10-03 14:33:54 +0000407 mod_state = (SDL_GetModState() & gui_grab_code) ==
408 gui_grab_code;
bellard8e9c4af2004-04-28 19:33:40 +0000409 gui_key_modifier_pressed = mod_state;
bellard457831f2004-07-14 17:22:33 +0000410 if (gui_key_modifier_pressed) {
bellard32ff25b2004-10-03 14:33:54 +0000411 int keycode;
412 keycode = sdl_keyevent_to_keycode(&ev->key);
413 switch(keycode) {
414 case 0x21: /* 'f' key on US keyboard */
bellard457831f2004-07-14 17:22:33 +0000415 toggle_full_screen(ds);
416 gui_keysym = 1;
417 break;
bellard32ff25b2004-10-03 14:33:54 +0000418 case 0x02 ... 0x0a: /* '1' to '9' keys */
bellarddfd92d32006-08-17 10:42:46 +0000419 /* Reset the modifiers sent to the current console */
420 reset_keys();
bellard32ff25b2004-10-03 14:33:54 +0000421 console_select(keycode - 0x02);
pbrook95219892006-04-09 01:06:34 +0000422 if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000423 /* display grab if going to a text console */
424 if (gui_grab)
425 sdl_grab_end();
426 }
427 gui_keysym = 1;
428 break;
429 default:
430 break;
431 }
pbrook95219892006-04-09 01:06:34 +0000432 } else if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000433 int keysym;
434 keysym = 0;
435 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
436 switch(ev->key.keysym.sym) {
437 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
438 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
439 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
440 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
441 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
442 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
443 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
444 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
445 default: break;
446 }
447 } else {
448 switch(ev->key.keysym.sym) {
449 case SDLK_UP: keysym = QEMU_KEY_UP; break;
450 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
451 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
452 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
453 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
454 case SDLK_END: keysym = QEMU_KEY_END; break;
455 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
456 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
457 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
458 default: break;
459 }
460 }
461 if (keysym) {
462 kbd_put_keysym(keysym);
463 } else if (ev->key.keysym.unicode != 0) {
464 kbd_put_keysym(ev->key.keysym.unicode);
465 }
bellard8e9c4af2004-04-28 19:33:40 +0000466 }
467 } else if (ev->type == SDL_KEYUP) {
bellardbf2b84e2004-11-14 19:46:35 +0000468 mod_state = (ev->key.keysym.mod & gui_grab_code);
bellard8e9c4af2004-04-28 19:33:40 +0000469 if (!mod_state) {
470 if (gui_key_modifier_pressed) {
pbrook5b311872006-03-11 20:07:45 +0000471 gui_key_modifier_pressed = 0;
bellard457831f2004-07-14 17:22:33 +0000472 if (gui_keysym == 0) {
bellard32ff25b2004-10-03 14:33:54 +0000473 /* exit/enter grab if pressing Ctrl-Alt */
bellardc66b0d42006-06-13 12:03:53 +0000474 if (!gui_grab) {
475 /* if the application is not active,
476 do not try to enter grab state. It
477 prevents
478 'SDL_WM_GrabInput(SDL_GRAB_ON)'
479 from blocking all the application
480 (SDL bug). */
481 if (SDL_GetAppState() & SDL_APPACTIVE)
482 sdl_grab_start();
483 } else {
bellard8e9c4af2004-04-28 19:33:40 +0000484 sdl_grab_end();
bellardc66b0d42006-06-13 12:03:53 +0000485 }
bellard32ff25b2004-10-03 14:33:54 +0000486 /* SDL does not send back all the
487 modifiers key, so we must correct it */
488 reset_keys();
bellard8e9c4af2004-04-28 19:33:40 +0000489 break;
490 }
bellard8e9c4af2004-04-28 19:33:40 +0000491 gui_keysym = 0;
492 }
bellard0f0b7262003-08-09 18:26:36 +0000493 }
494 }
bellarddfd92d32006-08-17 10:42:46 +0000495 if (is_graphic_console() && !gui_keysym)
bellard457831f2004-07-14 17:22:33 +0000496 sdl_process_key(&ev->key);
bellard0f0b7262003-08-09 18:26:36 +0000497 break;
498 case SDL_QUIT:
ths667acca2006-12-11 02:08:05 +0000499 if (!no_quit) {
500 qemu_system_shutdown_request();
501 }
bellard0f0b7262003-08-09 18:26:36 +0000502 break;
503 case SDL_MOUSEMOTION:
bellard09b26c52006-04-12 21:09:08 +0000504 if (gui_grab || kbd_mouse_is_absolute()) {
bellard18a6d282005-01-17 22:32:23 +0000505 sdl_send_mouse_event(0);
bellard0f0b7262003-08-09 18:26:36 +0000506 }
507 break;
508 case SDL_MOUSEBUTTONDOWN:
509 case SDL_MOUSEBUTTONUP:
510 {
511 SDL_MouseButtonEvent *bev = &ev->button;
bellard09b26c52006-04-12 21:09:08 +0000512 if (!gui_grab && !kbd_mouse_is_absolute()) {
bellard0f0b7262003-08-09 18:26:36 +0000513 if (ev->type == SDL_MOUSEBUTTONDOWN &&
514 (bev->state & SDL_BUTTON_LMASK)) {
515 /* start grabbing all events */
516 sdl_grab_start();
517 }
518 } else {
bellard18a6d282005-01-17 22:32:23 +0000519 int dz;
520 dz = 0;
521#ifdef SDL_BUTTON_WHEELUP
bellard09b26c52006-04-12 21:09:08 +0000522 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000523 dz = -1;
bellard09b26c52006-04-12 21:09:08 +0000524 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000525 dz = 1;
526 }
527#endif
528 sdl_send_mouse_event(dz);
bellard0f0b7262003-08-09 18:26:36 +0000529 }
530 }
531 break;
bellard0294ffb2004-04-29 22:15:15 +0000532 case SDL_ACTIVEEVENT:
pbrook5b311872006-03-11 20:07:45 +0000533 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
534 !ev->active.gain && !gui_fullscreen_initial_grab) {
bellard0294ffb2004-04-29 22:15:15 +0000535 sdl_grab_end();
536 }
537 break;
bellard0f0b7262003-08-09 18:26:36 +0000538 default:
539 break;
540 }
541 }
542}
543
bellard898712a2004-02-06 19:56:42 +0000544static void sdl_cleanup(void)
545{
546 SDL_Quit();
547}
548
bellardd63d3072004-10-03 13:29:03 +0000549void sdl_display_init(DisplayState *ds, int full_screen)
bellard0f0b7262003-08-09 18:26:36 +0000550{
551 int flags;
bellard09b26c52006-04-12 21:09:08 +0000552 uint8_t data = 0;
bellard0f0b7262003-08-09 18:26:36 +0000553
bellard3d11d0e2004-12-12 16:56:30 +0000554#if defined(__APPLE__)
555 /* always use generic keymaps */
556 if (!keyboard_layout)
557 keyboard_layout = "en-us";
558#endif
559 if(keyboard_layout) {
560 kbd_layout = init_keyboard_layout(keyboard_layout);
561 if (!kbd_layout)
562 exit(1);
563 }
564
bellard0f0b7262003-08-09 18:26:36 +0000565 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
566 if (SDL_Init (flags)) {
567 fprintf(stderr, "Could not initialize SDL - exiting\n");
568 exit(1);
569 }
bellard67b915a2004-03-31 23:37:16 +0000570#ifndef _WIN32
bellard0ae04d72003-09-30 21:09:16 +0000571 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
572 signal(SIGINT, SIG_DFL);
573 signal(SIGQUIT, SIG_DFL);
bellard67b915a2004-03-31 23:37:16 +0000574#endif
bellard0ae04d72003-09-30 21:09:16 +0000575
bellard0f0b7262003-08-09 18:26:36 +0000576 ds->dpy_update = sdl_update;
577 ds->dpy_resize = sdl_resize;
578 ds->dpy_refresh = sdl_refresh;
579
580 sdl_resize(ds, 640, 400);
bellard8a7ddc32004-03-31 19:00:16 +0000581 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000582 SDL_EnableKeyRepeat(250, 50);
bellard457831f2004-07-14 17:22:33 +0000583 SDL_EnableUNICODE(1);
bellard0f0b7262003-08-09 18:26:36 +0000584 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000585
bellard09b26c52006-04-12 21:09:08 +0000586 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
587 sdl_cursor_normal = SDL_GetCursor();
588
bellard898712a2004-02-06 19:56:42 +0000589 atexit(sdl_cleanup);
bellardd63d3072004-10-03 13:29:03 +0000590 if (full_screen) {
591 gui_fullscreen = 1;
592 gui_fullscreen_initial_grab = 1;
593 sdl_grab_start();
594 }
bellard0f0b7262003-08-09 18:26:36 +0000595}