blob: aa5c669929421c12345a02cb2e17f97f894b6512 [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;
ths43523e92007-02-18 18:19:32 +000037static int gui_noframe;
bellard8e9c4af2004-04-28 19:33:40 +000038static int gui_key_modifier_pressed;
39static int gui_keysym;
bellardd63d3072004-10-03 13:29:03 +000040static int gui_fullscreen_initial_grab;
bellard32ff25b2004-10-03 14:33:54 +000041static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
42static uint8_t modifiers_state[256];
bellard09b26c52006-04-12 21:09:08 +000043static int width, height;
44static SDL_Cursor *sdl_cursor_normal;
45static SDL_Cursor *sdl_cursor_hidden;
46static int absolute_enabled = 0;
bellard0f0b7262003-08-09 18:26:36 +000047
48static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
49{
bellard898712a2004-02-06 19:56:42 +000050 // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
bellard0f0b7262003-08-09 18:26:36 +000051 SDL_UpdateRect(screen, x, y, w, h);
52}
53
54static void sdl_resize(DisplayState *ds, int w, int h)
55{
56 int flags;
57
58 // printf("resizing to %d %d\n", w, h);
59
60 flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
bellard8e9c4af2004-04-28 19:33:40 +000061 if (gui_fullscreen)
62 flags |= SDL_FULLSCREEN;
ths43523e92007-02-18 18:19:32 +000063 if (gui_noframe)
64 flags |= SDL_NOFRAME;
bellard9903da22005-10-30 23:19:10 +000065
bellard09b26c52006-04-12 21:09:08 +000066 width = w;
67 height = h;
68
bellard9903da22005-10-30 23:19:10 +000069 again:
bellard0f0b7262003-08-09 18:26:36 +000070 screen = SDL_SetVideoMode(w, h, 0, flags);
71 if (!screen) {
72 fprintf(stderr, "Could not open SDL display\n");
73 exit(1);
74 }
bellard9903da22005-10-30 23:19:10 +000075 if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
76 flags &= ~SDL_HWSURFACE;
77 goto again;
78 }
79
80 if (!screen->pixels) {
81 fprintf(stderr, "Could not open SDL display\n");
82 exit(1);
83 }
bellard0f0b7262003-08-09 18:26:36 +000084 ds->data = screen->pixels;
85 ds->linesize = screen->pitch;
86 ds->depth = screen->format->BitsPerPixel;
bellardd3079cd2006-05-10 22:17:36 +000087 if (ds->depth == 32 && screen->format->Rshift == 0) {
88 ds->bgr = 1;
89 } else {
90 ds->bgr = 0;
91 }
bellard457831f2004-07-14 17:22:33 +000092 ds->width = w;
93 ds->height = h;
bellard0f0b7262003-08-09 18:26:36 +000094}
95
bellard3d11d0e2004-12-12 16:56:30 +000096/* generic keyboard conversion */
bellarde58d12e2004-07-05 22:13:07 +000097
bellard3d11d0e2004-12-12 16:56:30 +000098#include "sdl_keysym.h"
99#include "keymaps.c"
bellarde58d12e2004-07-05 22:13:07 +0000100
bellard3d11d0e2004-12-12 16:56:30 +0000101static kbd_layout_t *kbd_layout = NULL;
bellarde58d12e2004-07-05 22:13:07 +0000102
bellard3d11d0e2004-12-12 16:56:30 +0000103static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
bellarde58d12e2004-07-05 22:13:07 +0000104{
bellard3d11d0e2004-12-12 16:56:30 +0000105 int keysym;
106 /* workaround for X11+SDL bug with AltGR */
107 keysym = ev->keysym.sym;
108 if (keysym == 0 && ev->keysym.scancode == 113)
109 keysym = SDLK_MODE;
bellard60659e32006-08-19 14:27:31 +0000110 /* For Japanese key '\' and '|' */
111 if (keysym == 92 && ev->keysym.scancode == 133) {
112 keysym = 0xa5;
113 }
bellard3d11d0e2004-12-12 16:56:30 +0000114 return keysym2scancode(kbd_layout, keysym);
bellarde58d12e2004-07-05 22:13:07 +0000115}
116
bellard3d11d0e2004-12-12 16:56:30 +0000117/* specific keyboard conversions from scan codes */
118
119#if defined(_WIN32)
bellarde58d12e2004-07-05 22:13:07 +0000120
121static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
122{
123 return ev->keysym.scancode;
124}
125
126#else
127
bellarde58d12e2004-07-05 22:13:07 +0000128static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
129{
130 int keycode;
131
132 keycode = ev->keysym.scancode;
133
134 if (keycode < 9) {
135 keycode = 0;
136 } else if (keycode < 97) {
137 keycode -= 8; /* just an offset */
bellard60659e32006-08-19 14:27:31 +0000138 } else if (keycode < 212) {
bellarde58d12e2004-07-05 22:13:07 +0000139 /* use conversion table */
ths6070dd02007-01-24 21:40:21 +0000140 keycode = _translate_keycode(keycode - 97);
bellarde58d12e2004-07-05 22:13:07 +0000141 } else {
142 keycode = 0;
143 }
144 return keycode;
145}
146
147#endif
148
bellard32ff25b2004-10-03 14:33:54 +0000149static void reset_keys(void)
150{
151 int i;
152 for(i = 0; i < 256; i++) {
153 if (modifiers_state[i]) {
154 if (i & 0x80)
155 kbd_put_keycode(0xe0);
156 kbd_put_keycode(i | 0x80);
157 modifiers_state[i] = 0;
158 }
159 }
160}
161
bellard0f0b7262003-08-09 18:26:36 +0000162static void sdl_process_key(SDL_KeyboardEvent *ev)
163{
bellard32ff25b2004-10-03 14:33:54 +0000164 int keycode, v;
bellardde2200d2004-06-04 13:15:06 +0000165
166 if (ev->keysym.sym == SDLK_PAUSE) {
167 /* specific case */
168 v = 0;
169 if (ev->type == SDL_KEYUP)
170 v |= 0x80;
171 kbd_put_keycode(0xe1);
172 kbd_put_keycode(0x1d | v);
173 kbd_put_keycode(0x45 | v);
174 return;
175 }
176
bellard3d11d0e2004-12-12 16:56:30 +0000177 if (kbd_layout) {
178 keycode = sdl_keyevent_to_keycode_generic(ev);
179 } else {
180 keycode = sdl_keyevent_to_keycode(ev);
181 }
bellardde2200d2004-06-04 13:15:06 +0000182
183 switch(keycode) {
184 case 0x00:
185 /* sent when leaving window: reset the modifiers state */
bellard32ff25b2004-10-03 14:33:54 +0000186 reset_keys();
bellardde2200d2004-06-04 13:15:06 +0000187 return;
188 case 0x2a: /* Left Shift */
189 case 0x36: /* Right Shift */
190 case 0x1d: /* Left CTRL */
191 case 0x9d: /* Right CTRL */
192 case 0x38: /* Left ALT */
193 case 0xb8: /* Right ALT */
bellard0f0b7262003-08-09 18:26:36 +0000194 if (ev->type == SDL_KEYUP)
bellardde2200d2004-06-04 13:15:06 +0000195 modifiers_state[keycode] = 0;
196 else
197 modifiers_state[keycode] = 1;
198 break;
199 case 0x45: /* num lock */
200 case 0x3a: /* caps lock */
201 /* SDL does not send the key up event, so we generate it */
202 kbd_put_keycode(keycode);
203 kbd_put_keycode(keycode | 0x80);
204 return;
bellard0f0b7262003-08-09 18:26:36 +0000205 }
bellardde2200d2004-06-04 13:15:06 +0000206
207 /* now send the key code */
208 if (keycode & 0x80)
209 kbd_put_keycode(0xe0);
210 if (ev->type == SDL_KEYUP)
211 kbd_put_keycode(keycode | 0x80);
212 else
213 kbd_put_keycode(keycode & 0x7f);
bellard0f0b7262003-08-09 18:26:36 +0000214}
215
bellard8a7ddc32004-03-31 19:00:16 +0000216static void sdl_update_caption(void)
217{
218 char buf[1024];
thsc35734b2007-03-19 15:17:08 +0000219 const char *status = "";
220
221 if (!vm_running)
222 status = " [Stopped]";
223 else if (gui_grab)
224 status = " - Press Ctrl-Alt to exit grab";
225
226 if (qemu_name)
227 snprintf(buf, sizeof(buf), "QEMU (%s)%s", qemu_name, status);
228 else
229 snprintf(buf, sizeof(buf), "QEMU%s", status);
230
bellard8a7ddc32004-03-31 19:00:16 +0000231 SDL_WM_SetCaption(buf, "QEMU");
232}
233
bellard09b26c52006-04-12 21:09:08 +0000234static void sdl_hide_cursor(void)
235{
bellard8785a8d2006-06-13 10:49:12 +0000236 if (kbd_mouse_is_absolute()) {
237 SDL_ShowCursor(1);
238 SDL_SetCursor(sdl_cursor_hidden);
239 } else {
240 SDL_ShowCursor(0);
241 }
bellard09b26c52006-04-12 21:09:08 +0000242}
243
244static void sdl_show_cursor(void)
245{
246 if (!kbd_mouse_is_absolute()) {
bellard8785a8d2006-06-13 10:49:12 +0000247 SDL_ShowCursor(1);
ths455204e2007-01-05 16:42:13 +0000248 SDL_SetCursor(sdl_cursor_normal);
bellard09b26c52006-04-12 21:09:08 +0000249 }
250}
251
bellard0f0b7262003-08-09 18:26:36 +0000252static void sdl_grab_start(void)
253{
bellard09b26c52006-04-12 21:09:08 +0000254 sdl_hide_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000255 SDL_WM_GrabInput(SDL_GRAB_ON);
256 /* dummy read to avoid moving the mouse */
257 SDL_GetRelativeMouseState(NULL, NULL);
258 gui_grab = 1;
bellard8a7ddc32004-03-31 19:00:16 +0000259 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000260}
261
262static void sdl_grab_end(void)
263{
bellard0f0b7262003-08-09 18:26:36 +0000264 SDL_WM_GrabInput(SDL_GRAB_OFF);
bellard09b26c52006-04-12 21:09:08 +0000265 sdl_show_cursor();
bellard0f0b7262003-08-09 18:26:36 +0000266 gui_grab = 0;
bellard8a7ddc32004-03-31 19:00:16 +0000267 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000268}
269
bellard18a6d282005-01-17 22:32:23 +0000270static void sdl_send_mouse_event(int dz)
bellard0f0b7262003-08-09 18:26:36 +0000271{
bellard18a6d282005-01-17 22:32:23 +0000272 int dx, dy, state, buttons;
bellard0f0b7262003-08-09 18:26:36 +0000273 state = SDL_GetRelativeMouseState(&dx, &dy);
274 buttons = 0;
275 if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
276 buttons |= MOUSE_EVENT_LBUTTON;
277 if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
278 buttons |= MOUSE_EVENT_RBUTTON;
279 if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
280 buttons |= MOUSE_EVENT_MBUTTON;
bellard09b26c52006-04-12 21:09:08 +0000281
282 if (kbd_mouse_is_absolute()) {
283 if (!absolute_enabled) {
284 sdl_hide_cursor();
285 if (gui_grab) {
286 sdl_grab_end();
287 }
288 absolute_enabled = 1;
289 }
290
291 SDL_GetMouseState(&dx, &dy);
292 dx = dx * 0x7FFF / width;
293 dy = dy * 0x7FFF / height;
ths455204e2007-01-05 16:42:13 +0000294 } else if (absolute_enabled) {
295 sdl_show_cursor();
296 absolute_enabled = 0;
bellard09b26c52006-04-12 21:09:08 +0000297 }
298
bellard0f0b7262003-08-09 18:26:36 +0000299 kbd_mouse_event(dx, dy, dz, buttons);
300}
301
bellard8e9c4af2004-04-28 19:33:40 +0000302static void toggle_full_screen(DisplayState *ds)
303{
304 gui_fullscreen = !gui_fullscreen;
305 sdl_resize(ds, screen->w, screen->h);
306 if (gui_fullscreen) {
307 gui_saved_grab = gui_grab;
308 sdl_grab_start();
309 } else {
310 if (!gui_saved_grab)
311 sdl_grab_end();
312 }
pbrook95219892006-04-09 01:06:34 +0000313 vga_hw_invalidate();
314 vga_hw_update();
bellard8e9c4af2004-04-28 19:33:40 +0000315}
316
bellard0f0b7262003-08-09 18:26:36 +0000317static void sdl_refresh(DisplayState *ds)
318{
319 SDL_Event ev1, *ev = &ev1;
bellard8e9c4af2004-04-28 19:33:40 +0000320 int mod_state;
321
bellard8a7ddc32004-03-31 19:00:16 +0000322 if (last_vm_running != vm_running) {
323 last_vm_running = vm_running;
324 sdl_update_caption();
325 }
326
pbrook95219892006-04-09 01:06:34 +0000327 vga_hw_update();
bellard457831f2004-07-14 17:22:33 +0000328
bellard0f0b7262003-08-09 18:26:36 +0000329 while (SDL_PollEvent(ev)) {
330 switch (ev->type) {
331 case SDL_VIDEOEXPOSE:
332 sdl_update(ds, 0, 0, screen->w, screen->h);
333 break;
334 case SDL_KEYDOWN:
335 case SDL_KEYUP:
336 if (ev->type == SDL_KEYDOWN) {
bellard32ff25b2004-10-03 14:33:54 +0000337 mod_state = (SDL_GetModState() & gui_grab_code) ==
338 gui_grab_code;
bellard8e9c4af2004-04-28 19:33:40 +0000339 gui_key_modifier_pressed = mod_state;
bellard457831f2004-07-14 17:22:33 +0000340 if (gui_key_modifier_pressed) {
bellard32ff25b2004-10-03 14:33:54 +0000341 int keycode;
342 keycode = sdl_keyevent_to_keycode(&ev->key);
343 switch(keycode) {
344 case 0x21: /* 'f' key on US keyboard */
bellard457831f2004-07-14 17:22:33 +0000345 toggle_full_screen(ds);
346 gui_keysym = 1;
347 break;
bellard32ff25b2004-10-03 14:33:54 +0000348 case 0x02 ... 0x0a: /* '1' to '9' keys */
bellarddfd92d32006-08-17 10:42:46 +0000349 /* Reset the modifiers sent to the current console */
350 reset_keys();
bellard32ff25b2004-10-03 14:33:54 +0000351 console_select(keycode - 0x02);
pbrook95219892006-04-09 01:06:34 +0000352 if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000353 /* display grab if going to a text console */
354 if (gui_grab)
355 sdl_grab_end();
356 }
357 gui_keysym = 1;
358 break;
359 default:
360 break;
361 }
pbrook95219892006-04-09 01:06:34 +0000362 } else if (!is_graphic_console()) {
bellard457831f2004-07-14 17:22:33 +0000363 int keysym;
364 keysym = 0;
365 if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
366 switch(ev->key.keysym.sym) {
367 case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break;
368 case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break;
369 case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break;
370 case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break;
371 case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break;
372 case SDLK_END: keysym = QEMU_KEY_CTRL_END; break;
373 case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break;
374 case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break;
375 default: break;
376 }
377 } else {
378 switch(ev->key.keysym.sym) {
379 case SDLK_UP: keysym = QEMU_KEY_UP; break;
380 case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break;
381 case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break;
382 case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break;
383 case SDLK_HOME: keysym = QEMU_KEY_HOME; break;
384 case SDLK_END: keysym = QEMU_KEY_END; break;
385 case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break;
386 case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break;
387 case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break;
388 default: break;
389 }
390 }
391 if (keysym) {
392 kbd_put_keysym(keysym);
393 } else if (ev->key.keysym.unicode != 0) {
394 kbd_put_keysym(ev->key.keysym.unicode);
395 }
bellard8e9c4af2004-04-28 19:33:40 +0000396 }
397 } else if (ev->type == SDL_KEYUP) {
bellardbf2b84e2004-11-14 19:46:35 +0000398 mod_state = (ev->key.keysym.mod & gui_grab_code);
bellard8e9c4af2004-04-28 19:33:40 +0000399 if (!mod_state) {
400 if (gui_key_modifier_pressed) {
pbrook5b311872006-03-11 20:07:45 +0000401 gui_key_modifier_pressed = 0;
bellard457831f2004-07-14 17:22:33 +0000402 if (gui_keysym == 0) {
bellard32ff25b2004-10-03 14:33:54 +0000403 /* exit/enter grab if pressing Ctrl-Alt */
bellardc66b0d42006-06-13 12:03:53 +0000404 if (!gui_grab) {
405 /* if the application is not active,
406 do not try to enter grab state. It
407 prevents
408 'SDL_WM_GrabInput(SDL_GRAB_ON)'
409 from blocking all the application
410 (SDL bug). */
411 if (SDL_GetAppState() & SDL_APPACTIVE)
412 sdl_grab_start();
413 } else {
bellard8e9c4af2004-04-28 19:33:40 +0000414 sdl_grab_end();
bellardc66b0d42006-06-13 12:03:53 +0000415 }
bellard32ff25b2004-10-03 14:33:54 +0000416 /* SDL does not send back all the
417 modifiers key, so we must correct it */
418 reset_keys();
bellard8e9c4af2004-04-28 19:33:40 +0000419 break;
420 }
bellard8e9c4af2004-04-28 19:33:40 +0000421 gui_keysym = 0;
422 }
bellard0f0b7262003-08-09 18:26:36 +0000423 }
424 }
bellarddfd92d32006-08-17 10:42:46 +0000425 if (is_graphic_console() && !gui_keysym)
bellard457831f2004-07-14 17:22:33 +0000426 sdl_process_key(&ev->key);
bellard0f0b7262003-08-09 18:26:36 +0000427 break;
428 case SDL_QUIT:
ths667acca2006-12-11 02:08:05 +0000429 if (!no_quit) {
430 qemu_system_shutdown_request();
431 }
bellard0f0b7262003-08-09 18:26:36 +0000432 break;
433 case SDL_MOUSEMOTION:
ths455204e2007-01-05 16:42:13 +0000434 if (gui_grab || kbd_mouse_is_absolute() ||
435 absolute_enabled) {
bellard18a6d282005-01-17 22:32:23 +0000436 sdl_send_mouse_event(0);
bellard0f0b7262003-08-09 18:26:36 +0000437 }
438 break;
439 case SDL_MOUSEBUTTONDOWN:
440 case SDL_MOUSEBUTTONUP:
441 {
442 SDL_MouseButtonEvent *bev = &ev->button;
bellard09b26c52006-04-12 21:09:08 +0000443 if (!gui_grab && !kbd_mouse_is_absolute()) {
bellard0f0b7262003-08-09 18:26:36 +0000444 if (ev->type == SDL_MOUSEBUTTONDOWN &&
445 (bev->state & SDL_BUTTON_LMASK)) {
446 /* start grabbing all events */
447 sdl_grab_start();
448 }
449 } else {
bellard18a6d282005-01-17 22:32:23 +0000450 int dz;
451 dz = 0;
452#ifdef SDL_BUTTON_WHEELUP
bellard09b26c52006-04-12 21:09:08 +0000453 if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000454 dz = -1;
bellard09b26c52006-04-12 21:09:08 +0000455 } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) {
bellard18a6d282005-01-17 22:32:23 +0000456 dz = 1;
457 }
458#endif
459 sdl_send_mouse_event(dz);
bellard0f0b7262003-08-09 18:26:36 +0000460 }
461 }
462 break;
bellard0294ffb2004-04-29 22:15:15 +0000463 case SDL_ACTIVEEVENT:
pbrook5b311872006-03-11 20:07:45 +0000464 if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
465 !ev->active.gain && !gui_fullscreen_initial_grab) {
bellard0294ffb2004-04-29 22:15:15 +0000466 sdl_grab_end();
467 }
468 break;
bellard0f0b7262003-08-09 18:26:36 +0000469 default:
470 break;
471 }
472 }
473}
474
bellard898712a2004-02-06 19:56:42 +0000475static void sdl_cleanup(void)
476{
477 SDL_Quit();
478}
479
ths43523e92007-02-18 18:19:32 +0000480void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
bellard0f0b7262003-08-09 18:26:36 +0000481{
482 int flags;
bellard09b26c52006-04-12 21:09:08 +0000483 uint8_t data = 0;
bellard0f0b7262003-08-09 18:26:36 +0000484
bellard3d11d0e2004-12-12 16:56:30 +0000485#if defined(__APPLE__)
486 /* always use generic keymaps */
487 if (!keyboard_layout)
488 keyboard_layout = "en-us";
489#endif
490 if(keyboard_layout) {
491 kbd_layout = init_keyboard_layout(keyboard_layout);
492 if (!kbd_layout)
493 exit(1);
494 }
495
ths43523e92007-02-18 18:19:32 +0000496 if (no_frame)
497 gui_noframe = 1;
498
bellard0f0b7262003-08-09 18:26:36 +0000499 flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
500 if (SDL_Init (flags)) {
501 fprintf(stderr, "Could not initialize SDL - exiting\n");
502 exit(1);
503 }
bellard67b915a2004-03-31 23:37:16 +0000504#ifndef _WIN32
bellard0ae04d72003-09-30 21:09:16 +0000505 /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
506 signal(SIGINT, SIG_DFL);
507 signal(SIGQUIT, SIG_DFL);
bellard67b915a2004-03-31 23:37:16 +0000508#endif
bellard0ae04d72003-09-30 21:09:16 +0000509
bellard0f0b7262003-08-09 18:26:36 +0000510 ds->dpy_update = sdl_update;
511 ds->dpy_resize = sdl_resize;
512 ds->dpy_refresh = sdl_refresh;
513
514 sdl_resize(ds, 640, 400);
bellard8a7ddc32004-03-31 19:00:16 +0000515 sdl_update_caption();
bellard0f0b7262003-08-09 18:26:36 +0000516 SDL_EnableKeyRepeat(250, 50);
bellard457831f2004-07-14 17:22:33 +0000517 SDL_EnableUNICODE(1);
bellard0f0b7262003-08-09 18:26:36 +0000518 gui_grab = 0;
bellard898712a2004-02-06 19:56:42 +0000519
bellard09b26c52006-04-12 21:09:08 +0000520 sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
521 sdl_cursor_normal = SDL_GetCursor();
522
bellard898712a2004-02-06 19:56:42 +0000523 atexit(sdl_cleanup);
bellardd63d3072004-10-03 13:29:03 +0000524 if (full_screen) {
525 gui_fullscreen = 1;
526 gui_fullscreen_initial_grab = 1;
527 sdl_grab_start();
528 }
bellard0f0b7262003-08-09 18:26:36 +0000529}