]>
Commit | Line | Data |
---|---|---|
0f0b7264 FB |
1 | /* |
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 | */ | |
67b915a5 | 24 | #include "vl.h" |
0f0b7264 FB |
25 | |
26 | #include <SDL.h> | |
27 | ||
67b915a5 FB |
28 | #ifndef _WIN32 |
29 | #include <signal.h> | |
30 | #endif | |
0f0b7264 FB |
31 | |
32 | static SDL_Surface *screen; | |
33 | static int gui_grab; /* if true, all keyboard/mouse events are grabbed */ | |
8a7ddc38 | 34 | static int last_vm_running; |
8e9c4afe FB |
35 | static int gui_saved_grab; |
36 | static int gui_fullscreen; | |
37 | static int gui_key_modifier_pressed; | |
38 | static int gui_keysym; | |
d63d307f | 39 | static int gui_fullscreen_initial_grab; |
32ff25bf FB |
40 | static int gui_grab_code = KMOD_LALT | KMOD_LCTRL; |
41 | static uint8_t modifiers_state[256]; | |
09b26c5e FB |
42 | static int width, height; |
43 | static SDL_Cursor *sdl_cursor_normal; | |
44 | static SDL_Cursor *sdl_cursor_hidden; | |
45 | static int absolute_enabled = 0; | |
0f0b7264 FB |
46 | |
47 | static void sdl_update(DisplayState *ds, int x, int y, int w, int h) | |
48 | { | |
898712a8 | 49 | // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h); |
0f0b7264 FB |
50 | SDL_UpdateRect(screen, x, y, w, h); |
51 | } | |
52 | ||
53 | static 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; | |
8e9c4afe FB |
60 | if (gui_fullscreen) |
61 | flags |= SDL_FULLSCREEN; | |
9903da21 | 62 | |
09b26c5e FB |
63 | width = w; |
64 | height = h; | |
65 | ||
9903da21 | 66 | again: |
0f0b7264 FB |
67 | screen = SDL_SetVideoMode(w, h, 0, flags); |
68 | if (!screen) { | |
69 | fprintf(stderr, "Could not open SDL display\n"); | |
70 | exit(1); | |
71 | } | |
9903da21 FB |
72 | 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 | } | |
0f0b7264 FB |
81 | ds->data = screen->pixels; |
82 | ds->linesize = screen->pitch; | |
83 | ds->depth = screen->format->BitsPerPixel; | |
d3079cd2 FB |
84 | if (ds->depth == 32 && screen->format->Rshift == 0) { |
85 | ds->bgr = 1; | |
86 | } else { | |
87 | ds->bgr = 0; | |
88 | } | |
457831f4 FB |
89 | ds->width = w; |
90 | ds->height = h; | |
0f0b7264 FB |
91 | } |
92 | ||
3d11d0eb | 93 | /* generic keyboard conversion */ |
e58d12ed | 94 | |
3d11d0eb FB |
95 | #include "sdl_keysym.h" |
96 | #include "keymaps.c" | |
97 | ||
98 | static kbd_layout_t *kbd_layout = NULL; | |
99 | ||
100 | static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev) | |
e58d12ed | 101 | { |
3d11d0eb FB |
102 | 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; | |
107 | return keysym2scancode(kbd_layout, keysym); | |
e58d12ed FB |
108 | } |
109 | ||
3d11d0eb FB |
110 | /* specific keyboard conversions from scan codes */ |
111 | ||
112 | #if defined(_WIN32) | |
e58d12ed FB |
113 | |
114 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) | |
115 | { | |
116 | return ev->keysym.scancode; | |
117 | } | |
118 | ||
119 | #else | |
120 | ||
de2200d3 FB |
121 | static const uint8_t x_keycode_to_pc_keycode[61] = { |
122 | 0xc7, /* 97 Home */ | |
123 | 0xc8, /* 98 Up */ | |
124 | 0xc9, /* 99 PgUp */ | |
125 | 0xcb, /* 100 Left */ | |
0f0b7264 | 126 | 0x4c, /* 101 KP-5 */ |
de2200d3 FB |
127 | 0xcd, /* 102 Right */ |
128 | 0xcf, /* 103 End */ | |
129 | 0xd0, /* 104 Down */ | |
130 | 0xd1, /* 105 PgDn */ | |
131 | 0xd2, /* 106 Ins */ | |
132 | 0xd3, /* 107 Del */ | |
133 | 0x9c, /* 108 Enter */ | |
134 | 0x9d, /* 109 Ctrl-R */ | |
22a56b8a | 135 | 0x0, /* 110 Pause */ |
de2200d3 FB |
136 | 0xb7, /* 111 Print */ |
137 | 0xb5, /* 112 Divide */ | |
138 | 0xb8, /* 113 Alt-R */ | |
139 | 0xc6, /* 114 Break */ | |
0f0b7264 FB |
140 | 0x0, /* 115 */ |
141 | 0x0, /* 116 */ | |
142 | 0x0, /* 117 */ | |
143 | 0x0, /* 118 */ | |
144 | 0x0, /* 119 */ | |
b71e95fc | 145 | 0x70, /* 120 Hiragana_Katakana */ |
0f0b7264 FB |
146 | 0x0, /* 121 */ |
147 | 0x0, /* 122 */ | |
b71e95fc | 148 | 0x73, /* 123 backslash */ |
0f0b7264 FB |
149 | 0x0, /* 124 */ |
150 | 0x0, /* 125 */ | |
151 | 0x0, /* 126 */ | |
152 | 0x0, /* 127 */ | |
153 | 0x0, /* 128 */ | |
b71e95fc | 154 | 0x79, /* 129 Henkan */ |
0f0b7264 | 155 | 0x0, /* 130 */ |
b71e95fc | 156 | 0x7b, /* 131 Muhenkan */ |
0f0b7264 | 157 | 0x0, /* 132 */ |
b71e95fc | 158 | 0x7d, /* 133 Yen */ |
0f0b7264 FB |
159 | 0x0, /* 134 */ |
160 | 0x0, /* 135 */ | |
161 | 0x47, /* 136 KP_7 */ | |
162 | 0x48, /* 137 KP_8 */ | |
163 | 0x49, /* 138 KP_9 */ | |
164 | 0x4b, /* 139 KP_4 */ | |
165 | 0x4c, /* 140 KP_5 */ | |
166 | 0x4d, /* 141 KP_6 */ | |
167 | 0x4f, /* 142 KP_1 */ | |
168 | 0x50, /* 143 KP_2 */ | |
169 | 0x51, /* 144 KP_3 */ | |
170 | 0x52, /* 145 KP_0 */ | |
171 | 0x53, /* 146 KP_. */ | |
172 | 0x47, /* 147 KP_HOME */ | |
173 | 0x48, /* 148 KP_UP */ | |
174 | 0x49, /* 149 KP_PgUp */ | |
175 | 0x4b, /* 150 KP_Left */ | |
176 | 0x4c, /* 151 KP_ */ | |
177 | 0x4d, /* 152 KP_Right */ | |
178 | 0x4f, /* 153 KP_End */ | |
179 | 0x50, /* 154 KP_Down */ | |
180 | 0x51, /* 155 KP_PgDn */ | |
181 | 0x52, /* 156 KP_Ins */ | |
182 | 0x53, /* 157 KP_Del */ | |
183 | }; | |
184 | ||
e58d12ed FB |
185 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
186 | { | |
187 | int keycode; | |
188 | ||
189 | keycode = ev->keysym.scancode; | |
190 | ||
191 | if (keycode < 9) { | |
192 | keycode = 0; | |
193 | } else if (keycode < 97) { | |
194 | keycode -= 8; /* just an offset */ | |
195 | } else if (keycode < 158) { | |
196 | /* use conversion table */ | |
197 | keycode = x_keycode_to_pc_keycode[keycode - 97]; | |
198 | } else { | |
199 | keycode = 0; | |
200 | } | |
201 | return keycode; | |
202 | } | |
203 | ||
204 | #endif | |
205 | ||
32ff25bf FB |
206 | static void reset_keys(void) |
207 | { | |
208 | int i; | |
209 | for(i = 0; i < 256; i++) { | |
210 | if (modifiers_state[i]) { | |
211 | if (i & 0x80) | |
212 | kbd_put_keycode(0xe0); | |
213 | kbd_put_keycode(i | 0x80); | |
214 | modifiers_state[i] = 0; | |
215 | } | |
216 | } | |
217 | } | |
218 | ||
0f0b7264 FB |
219 | static void sdl_process_key(SDL_KeyboardEvent *ev) |
220 | { | |
32ff25bf | 221 | int keycode, v; |
de2200d3 FB |
222 | |
223 | if (ev->keysym.sym == SDLK_PAUSE) { | |
224 | /* specific case */ | |
225 | v = 0; | |
226 | if (ev->type == SDL_KEYUP) | |
227 | v |= 0x80; | |
228 | kbd_put_keycode(0xe1); | |
229 | kbd_put_keycode(0x1d | v); | |
230 | kbd_put_keycode(0x45 | v); | |
231 | return; | |
232 | } | |
233 | ||
3d11d0eb FB |
234 | if (kbd_layout) { |
235 | keycode = sdl_keyevent_to_keycode_generic(ev); | |
236 | } else { | |
237 | keycode = sdl_keyevent_to_keycode(ev); | |
238 | } | |
de2200d3 FB |
239 | |
240 | switch(keycode) { | |
241 | case 0x00: | |
242 | /* sent when leaving window: reset the modifiers state */ | |
32ff25bf | 243 | reset_keys(); |
de2200d3 FB |
244 | return; |
245 | case 0x2a: /* Left Shift */ | |
246 | case 0x36: /* Right Shift */ | |
247 | case 0x1d: /* Left CTRL */ | |
248 | case 0x9d: /* Right CTRL */ | |
249 | case 0x38: /* Left ALT */ | |
250 | case 0xb8: /* Right ALT */ | |
0f0b7264 | 251 | if (ev->type == SDL_KEYUP) |
de2200d3 FB |
252 | modifiers_state[keycode] = 0; |
253 | else | |
254 | modifiers_state[keycode] = 1; | |
255 | break; | |
256 | case 0x45: /* num lock */ | |
257 | case 0x3a: /* caps lock */ | |
258 | /* SDL does not send the key up event, so we generate it */ | |
259 | kbd_put_keycode(keycode); | |
260 | kbd_put_keycode(keycode | 0x80); | |
261 | return; | |
0f0b7264 | 262 | } |
de2200d3 FB |
263 | |
264 | /* now send the key code */ | |
265 | if (keycode & 0x80) | |
266 | kbd_put_keycode(0xe0); | |
267 | if (ev->type == SDL_KEYUP) | |
268 | kbd_put_keycode(keycode | 0x80); | |
269 | else | |
270 | kbd_put_keycode(keycode & 0x7f); | |
0f0b7264 FB |
271 | } |
272 | ||
8a7ddc38 FB |
273 | static void sdl_update_caption(void) |
274 | { | |
275 | char buf[1024]; | |
276 | strcpy(buf, "QEMU"); | |
277 | if (!vm_running) { | |
278 | strcat(buf, " [Stopped]"); | |
279 | } | |
280 | if (gui_grab) { | |
32ff25bf | 281 | strcat(buf, " - Press Ctrl-Alt to exit grab"); |
8a7ddc38 FB |
282 | } |
283 | SDL_WM_SetCaption(buf, "QEMU"); | |
284 | } | |
285 | ||
09b26c5e FB |
286 | static void sdl_hide_cursor(void) |
287 | { | |
288 | SDL_SetCursor(sdl_cursor_hidden); | |
289 | } | |
290 | ||
291 | static void sdl_show_cursor(void) | |
292 | { | |
293 | if (!kbd_mouse_is_absolute()) { | |
294 | SDL_SetCursor(sdl_cursor_normal); | |
295 | } | |
296 | } | |
297 | ||
0f0b7264 FB |
298 | static void sdl_grab_start(void) |
299 | { | |
09b26c5e | 300 | sdl_hide_cursor(); |
0f0b7264 FB |
301 | SDL_WM_GrabInput(SDL_GRAB_ON); |
302 | /* dummy read to avoid moving the mouse */ | |
303 | SDL_GetRelativeMouseState(NULL, NULL); | |
304 | gui_grab = 1; | |
8a7ddc38 | 305 | sdl_update_caption(); |
0f0b7264 FB |
306 | } |
307 | ||
308 | static void sdl_grab_end(void) | |
309 | { | |
0f0b7264 | 310 | SDL_WM_GrabInput(SDL_GRAB_OFF); |
09b26c5e | 311 | sdl_show_cursor(); |
0f0b7264 | 312 | gui_grab = 0; |
8a7ddc38 | 313 | sdl_update_caption(); |
0f0b7264 FB |
314 | } |
315 | ||
18a6d284 | 316 | static void sdl_send_mouse_event(int dz) |
0f0b7264 | 317 | { |
18a6d284 | 318 | int dx, dy, state, buttons; |
0f0b7264 FB |
319 | state = SDL_GetRelativeMouseState(&dx, &dy); |
320 | buttons = 0; | |
321 | if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) | |
322 | buttons |= MOUSE_EVENT_LBUTTON; | |
323 | if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) | |
324 | buttons |= MOUSE_EVENT_RBUTTON; | |
325 | if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) | |
326 | buttons |= MOUSE_EVENT_MBUTTON; | |
09b26c5e FB |
327 | |
328 | if (kbd_mouse_is_absolute()) { | |
329 | if (!absolute_enabled) { | |
330 | sdl_hide_cursor(); | |
331 | if (gui_grab) { | |
332 | sdl_grab_end(); | |
333 | } | |
334 | absolute_enabled = 1; | |
335 | } | |
336 | ||
337 | SDL_GetMouseState(&dx, &dy); | |
338 | dx = dx * 0x7FFF / width; | |
339 | dy = dy * 0x7FFF / height; | |
340 | } | |
341 | ||
0f0b7264 FB |
342 | kbd_mouse_event(dx, dy, dz, buttons); |
343 | } | |
344 | ||
8e9c4afe FB |
345 | static void toggle_full_screen(DisplayState *ds) |
346 | { | |
347 | gui_fullscreen = !gui_fullscreen; | |
348 | sdl_resize(ds, screen->w, screen->h); | |
349 | if (gui_fullscreen) { | |
350 | gui_saved_grab = gui_grab; | |
351 | sdl_grab_start(); | |
352 | } else { | |
353 | if (!gui_saved_grab) | |
354 | sdl_grab_end(); | |
355 | } | |
95219897 PB |
356 | vga_hw_invalidate(); |
357 | vga_hw_update(); | |
8e9c4afe FB |
358 | } |
359 | ||
0f0b7264 FB |
360 | static void sdl_refresh(DisplayState *ds) |
361 | { | |
362 | SDL_Event ev1, *ev = &ev1; | |
8e9c4afe FB |
363 | int mod_state; |
364 | ||
8a7ddc38 FB |
365 | if (last_vm_running != vm_running) { |
366 | last_vm_running = vm_running; | |
367 | sdl_update_caption(); | |
368 | } | |
369 | ||
95219897 | 370 | vga_hw_update(); |
457831f4 | 371 | |
0f0b7264 FB |
372 | while (SDL_PollEvent(ev)) { |
373 | switch (ev->type) { | |
374 | case SDL_VIDEOEXPOSE: | |
375 | sdl_update(ds, 0, 0, screen->w, screen->h); | |
376 | break; | |
377 | case SDL_KEYDOWN: | |
378 | case SDL_KEYUP: | |
379 | if (ev->type == SDL_KEYDOWN) { | |
32ff25bf FB |
380 | mod_state = (SDL_GetModState() & gui_grab_code) == |
381 | gui_grab_code; | |
8e9c4afe | 382 | gui_key_modifier_pressed = mod_state; |
457831f4 | 383 | if (gui_key_modifier_pressed) { |
32ff25bf FB |
384 | int keycode; |
385 | keycode = sdl_keyevent_to_keycode(&ev->key); | |
386 | switch(keycode) { | |
387 | case 0x21: /* 'f' key on US keyboard */ | |
457831f4 FB |
388 | toggle_full_screen(ds); |
389 | gui_keysym = 1; | |
390 | break; | |
32ff25bf FB |
391 | case 0x02 ... 0x0a: /* '1' to '9' keys */ |
392 | console_select(keycode - 0x02); | |
95219897 | 393 | if (!is_graphic_console()) { |
457831f4 FB |
394 | /* display grab if going to a text console */ |
395 | if (gui_grab) | |
396 | sdl_grab_end(); | |
397 | } | |
398 | gui_keysym = 1; | |
399 | break; | |
400 | default: | |
401 | break; | |
402 | } | |
95219897 | 403 | } else if (!is_graphic_console()) { |
457831f4 FB |
404 | int keysym; |
405 | keysym = 0; | |
406 | if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) { | |
407 | switch(ev->key.keysym.sym) { | |
408 | case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break; | |
409 | case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break; | |
410 | case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break; | |
411 | case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break; | |
412 | case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break; | |
413 | case SDLK_END: keysym = QEMU_KEY_CTRL_END; break; | |
414 | case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break; | |
415 | case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break; | |
416 | default: break; | |
417 | } | |
418 | } else { | |
419 | switch(ev->key.keysym.sym) { | |
420 | case SDLK_UP: keysym = QEMU_KEY_UP; break; | |
421 | case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break; | |
422 | case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break; | |
423 | case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break; | |
424 | case SDLK_HOME: keysym = QEMU_KEY_HOME; break; | |
425 | case SDLK_END: keysym = QEMU_KEY_END; break; | |
426 | case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break; | |
427 | case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break; | |
428 | case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break; | |
429 | default: break; | |
430 | } | |
431 | } | |
432 | if (keysym) { | |
433 | kbd_put_keysym(keysym); | |
434 | } else if (ev->key.keysym.unicode != 0) { | |
435 | kbd_put_keysym(ev->key.keysym.unicode); | |
436 | } | |
8e9c4afe FB |
437 | } |
438 | } else if (ev->type == SDL_KEYUP) { | |
bf2b84e4 | 439 | mod_state = (ev->key.keysym.mod & gui_grab_code); |
8e9c4afe FB |
440 | if (!mod_state) { |
441 | if (gui_key_modifier_pressed) { | |
5b311878 | 442 | gui_key_modifier_pressed = 0; |
457831f4 | 443 | if (gui_keysym == 0) { |
32ff25bf | 444 | /* exit/enter grab if pressing Ctrl-Alt */ |
8e9c4afe FB |
445 | if (!gui_grab) |
446 | sdl_grab_start(); | |
447 | else | |
448 | sdl_grab_end(); | |
32ff25bf FB |
449 | /* SDL does not send back all the |
450 | modifiers key, so we must correct it */ | |
451 | reset_keys(); | |
8e9c4afe FB |
452 | break; |
453 | } | |
8e9c4afe FB |
454 | gui_keysym = 0; |
455 | } | |
0f0b7264 FB |
456 | } |
457 | } | |
95219897 | 458 | if (is_graphic_console()) |
457831f4 | 459 | sdl_process_key(&ev->key); |
0f0b7264 FB |
460 | break; |
461 | case SDL_QUIT: | |
979a54fb | 462 | qemu_system_shutdown_request(); |
0f0b7264 FB |
463 | break; |
464 | case SDL_MOUSEMOTION: | |
09b26c5e | 465 | if (gui_grab || kbd_mouse_is_absolute()) { |
18a6d284 | 466 | sdl_send_mouse_event(0); |
0f0b7264 FB |
467 | } |
468 | break; | |
469 | case SDL_MOUSEBUTTONDOWN: | |
470 | case SDL_MOUSEBUTTONUP: | |
471 | { | |
472 | SDL_MouseButtonEvent *bev = &ev->button; | |
09b26c5e | 473 | if (!gui_grab && !kbd_mouse_is_absolute()) { |
0f0b7264 FB |
474 | if (ev->type == SDL_MOUSEBUTTONDOWN && |
475 | (bev->state & SDL_BUTTON_LMASK)) { | |
476 | /* start grabbing all events */ | |
477 | sdl_grab_start(); | |
478 | } | |
479 | } else { | |
18a6d284 FB |
480 | int dz; |
481 | dz = 0; | |
482 | #ifdef SDL_BUTTON_WHEELUP | |
09b26c5e | 483 | if (bev->button == SDL_BUTTON_WHEELUP && ev->type == SDL_MOUSEBUTTONDOWN) { |
18a6d284 | 484 | dz = -1; |
09b26c5e | 485 | } else if (bev->button == SDL_BUTTON_WHEELDOWN && ev->type == SDL_MOUSEBUTTONDOWN) { |
18a6d284 FB |
486 | dz = 1; |
487 | } | |
488 | #endif | |
489 | sdl_send_mouse_event(dz); | |
0f0b7264 FB |
490 | } |
491 | } | |
492 | break; | |
0294ffb9 | 493 | case SDL_ACTIVEEVENT: |
5b311878 PB |
494 | if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS && |
495 | !ev->active.gain && !gui_fullscreen_initial_grab) { | |
0294ffb9 FB |
496 | sdl_grab_end(); |
497 | } | |
498 | break; | |
0f0b7264 FB |
499 | default: |
500 | break; | |
501 | } | |
502 | } | |
503 | } | |
504 | ||
898712a8 FB |
505 | static void sdl_cleanup(void) |
506 | { | |
507 | SDL_Quit(); | |
508 | } | |
509 | ||
d63d307f | 510 | void sdl_display_init(DisplayState *ds, int full_screen) |
0f0b7264 FB |
511 | { |
512 | int flags; | |
09b26c5e | 513 | uint8_t data = 0; |
0f0b7264 | 514 | |
3d11d0eb FB |
515 | #if defined(__APPLE__) |
516 | /* always use generic keymaps */ | |
517 | if (!keyboard_layout) | |
518 | keyboard_layout = "en-us"; | |
519 | #endif | |
520 | if(keyboard_layout) { | |
521 | kbd_layout = init_keyboard_layout(keyboard_layout); | |
522 | if (!kbd_layout) | |
523 | exit(1); | |
524 | } | |
525 | ||
0f0b7264 FB |
526 | flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE; |
527 | if (SDL_Init (flags)) { | |
528 | fprintf(stderr, "Could not initialize SDL - exiting\n"); | |
529 | exit(1); | |
530 | } | |
67b915a5 | 531 | #ifndef _WIN32 |
0ae04d73 FB |
532 | /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */ |
533 | signal(SIGINT, SIG_DFL); | |
534 | signal(SIGQUIT, SIG_DFL); | |
67b915a5 | 535 | #endif |
0ae04d73 | 536 | |
0f0b7264 FB |
537 | ds->dpy_update = sdl_update; |
538 | ds->dpy_resize = sdl_resize; | |
539 | ds->dpy_refresh = sdl_refresh; | |
540 | ||
541 | sdl_resize(ds, 640, 400); | |
8a7ddc38 | 542 | sdl_update_caption(); |
0f0b7264 | 543 | SDL_EnableKeyRepeat(250, 50); |
457831f4 | 544 | SDL_EnableUNICODE(1); |
0f0b7264 | 545 | gui_grab = 0; |
898712a8 | 546 | |
09b26c5e FB |
547 | sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0); |
548 | sdl_cursor_normal = SDL_GetCursor(); | |
549 | ||
898712a8 | 550 | atexit(sdl_cleanup); |
d63d307f FB |
551 | if (full_screen) { |
552 | gui_fullscreen = 1; | |
553 | gui_fullscreen_initial_grab = 1; | |
554 | sdl_grab_start(); | |
555 | } | |
0f0b7264 | 556 | } |