]>
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 | 31 | |
e58d12ed FB |
32 | #if defined(__APPLE__) |
33 | #define CONFIG_SDL_GENERIC_KBD | |
34 | #endif | |
35 | ||
0f0b7264 FB |
36 | static SDL_Surface *screen; |
37 | static int gui_grab; /* if true, all keyboard/mouse events are grabbed */ | |
8a7ddc38 | 38 | static int last_vm_running; |
8e9c4afe FB |
39 | static int gui_saved_grab; |
40 | static int gui_fullscreen; | |
41 | static int gui_key_modifier_pressed; | |
42 | static int gui_keysym; | |
d63d307f | 43 | static int gui_fullscreen_initial_grab; |
0f0b7264 FB |
44 | |
45 | static void sdl_update(DisplayState *ds, int x, int y, int w, int h) | |
46 | { | |
898712a8 | 47 | // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h); |
0f0b7264 FB |
48 | SDL_UpdateRect(screen, x, y, w, h); |
49 | } | |
50 | ||
51 | static void sdl_resize(DisplayState *ds, int w, int h) | |
52 | { | |
53 | int flags; | |
54 | ||
55 | // printf("resizing to %d %d\n", w, h); | |
56 | ||
57 | flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL; | |
58 | flags |= SDL_RESIZABLE; | |
8e9c4afe FB |
59 | if (gui_fullscreen) |
60 | flags |= SDL_FULLSCREEN; | |
0f0b7264 FB |
61 | screen = SDL_SetVideoMode(w, h, 0, flags); |
62 | if (!screen) { | |
63 | fprintf(stderr, "Could not open SDL display\n"); | |
64 | exit(1); | |
65 | } | |
66 | ds->data = screen->pixels; | |
67 | ds->linesize = screen->pitch; | |
68 | ds->depth = screen->format->BitsPerPixel; | |
457831f4 FB |
69 | ds->width = w; |
70 | ds->height = h; | |
0f0b7264 FB |
71 | } |
72 | ||
e58d12ed FB |
73 | #ifdef CONFIG_SDL_GENERIC_KBD |
74 | ||
75 | /* XXX: use keymap tables defined in the VNC patch because the | |
76 | following code suppose you have a US keyboard. */ | |
77 | ||
78 | static const uint8_t scancodes[SDLK_LAST] = { | |
79 | [SDLK_ESCAPE] = 0x01, | |
80 | [SDLK_1] = 0x02, | |
81 | [SDLK_2] = 0x03, | |
82 | [SDLK_3] = 0x04, | |
83 | [SDLK_4] = 0x05, | |
84 | [SDLK_5] = 0x06, | |
85 | [SDLK_6] = 0x07, | |
86 | [SDLK_7] = 0x08, | |
87 | [SDLK_8] = 0x09, | |
88 | [SDLK_9] = 0x0a, | |
89 | [SDLK_0] = 0x0b, | |
90 | [SDLK_MINUS] = 0x0c, | |
91 | [SDLK_EQUALS] = 0x0d, | |
92 | [SDLK_BACKSPACE] = 0x0e, | |
93 | [SDLK_TAB] = 0x0f, | |
94 | [SDLK_q] = 0x10, | |
95 | [SDLK_w] = 0x11, | |
96 | [SDLK_e] = 0x12, | |
97 | [SDLK_r] = 0x13, | |
98 | [SDLK_t] = 0x14, | |
99 | [SDLK_y] = 0x15, | |
100 | [SDLK_u] = 0x16, | |
101 | [SDLK_i] = 0x17, | |
102 | [SDLK_o] = 0x18, | |
103 | [SDLK_p] = 0x19, | |
104 | [SDLK_LEFTBRACKET] = 0x1a, | |
105 | [SDLK_RIGHTBRACKET] = 0x1b, | |
106 | [SDLK_RETURN] = 0x1c, | |
107 | [SDLK_LCTRL] = 0x1d, | |
108 | [SDLK_a] = 0x1e, | |
109 | [SDLK_s] = 0x1f, | |
110 | [SDLK_d] = 0x20, | |
111 | [SDLK_f] = 0x21, | |
112 | [SDLK_g] = 0x22, | |
113 | [SDLK_h] = 0x23, | |
114 | [SDLK_j] = 0x24, | |
115 | [SDLK_k] = 0x25, | |
116 | [SDLK_l] = 0x26, | |
117 | [SDLK_SEMICOLON] = 0x27, | |
118 | [SDLK_QUOTE] = 0x28, | |
119 | [SDLK_BACKQUOTE] = 0x29, | |
120 | [SDLK_LSHIFT] = 0x2a, | |
121 | [SDLK_BACKSLASH] = 0x2b, | |
122 | [SDLK_z] = 0x2c, | |
123 | [SDLK_x] = 0x2d, | |
124 | [SDLK_c] = 0x2e, | |
125 | [SDLK_v] = 0x2f, | |
126 | [SDLK_b] = 0x30, | |
127 | [SDLK_n] = 0x31, | |
128 | [SDLK_m] = 0x32, | |
129 | [SDLK_COMMA] = 0x33, | |
130 | [SDLK_PERIOD] = 0x34, | |
131 | [SDLK_SLASH] = 0x35, | |
132 | [SDLK_KP_MULTIPLY] = 0x37, | |
133 | [SDLK_LALT] = 0x38, | |
134 | [SDLK_SPACE] = 0x39, | |
135 | [SDLK_CAPSLOCK] = 0x3a, | |
136 | [SDLK_F1] = 0x3b, | |
137 | [SDLK_F2] = 0x3c, | |
138 | [SDLK_F3] = 0x3d, | |
139 | [SDLK_F4] = 0x3e, | |
140 | [SDLK_F5] = 0x3f, | |
141 | [SDLK_F6] = 0x40, | |
142 | [SDLK_F7] = 0x41, | |
143 | [SDLK_F8] = 0x42, | |
144 | [SDLK_F9] = 0x43, | |
145 | [SDLK_F10] = 0x44, | |
146 | [SDLK_NUMLOCK] = 0x45, | |
147 | [SDLK_SCROLLOCK] = 0x46, | |
148 | [SDLK_KP7] = 0x47, | |
149 | [SDLK_KP8] = 0x48, | |
150 | [SDLK_KP9] = 0x49, | |
151 | [SDLK_KP_MINUS] = 0x4a, | |
152 | [SDLK_KP4] = 0x4b, | |
153 | [SDLK_KP5] = 0x4c, | |
154 | [SDLK_KP6] = 0x4d, | |
155 | [SDLK_KP_PLUS] = 0x4e, | |
156 | [SDLK_KP1] = 0x4f, | |
157 | [SDLK_KP2] = 0x50, | |
158 | [SDLK_KP3] = 0x51, | |
159 | [SDLK_KP0] = 0x52, | |
160 | [SDLK_KP_PERIOD] = 0x53, | |
161 | [SDLK_PRINT] = 0x54, | |
162 | [SDLK_LMETA] = 0x56, | |
163 | ||
164 | [SDLK_KP_ENTER] = 0x9c, | |
165 | [SDLK_KP_DIVIDE] = 0xb5, | |
166 | ||
167 | [SDLK_UP] = 0xc8, | |
168 | [SDLK_DOWN] = 0xd0, | |
169 | [SDLK_RIGHT] = 0xcd, | |
170 | [SDLK_LEFT] = 0xcb, | |
171 | [SDLK_INSERT] = 0xd2, | |
172 | [SDLK_HOME] = 0xc7, | |
173 | [SDLK_END] = 0xcf, | |
174 | [SDLK_PAGEUP] = 0xc9, | |
175 | [SDLK_PAGEDOWN] = 0xd1, | |
176 | [SDLK_DELETE] = 0xd3, | |
177 | }; | |
178 | ||
179 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) | |
180 | { | |
181 | return scancodes[ev->keysym.sym]; | |
182 | } | |
183 | ||
184 | #elif defined(_WIN32) | |
185 | ||
186 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) | |
187 | { | |
188 | return ev->keysym.scancode; | |
189 | } | |
190 | ||
191 | #else | |
192 | ||
de2200d3 FB |
193 | static const uint8_t x_keycode_to_pc_keycode[61] = { |
194 | 0xc7, /* 97 Home */ | |
195 | 0xc8, /* 98 Up */ | |
196 | 0xc9, /* 99 PgUp */ | |
197 | 0xcb, /* 100 Left */ | |
0f0b7264 | 198 | 0x4c, /* 101 KP-5 */ |
de2200d3 FB |
199 | 0xcd, /* 102 Right */ |
200 | 0xcf, /* 103 End */ | |
201 | 0xd0, /* 104 Down */ | |
202 | 0xd1, /* 105 PgDn */ | |
203 | 0xd2, /* 106 Ins */ | |
204 | 0xd3, /* 107 Del */ | |
205 | 0x9c, /* 108 Enter */ | |
206 | 0x9d, /* 109 Ctrl-R */ | |
22a56b8a | 207 | 0x0, /* 110 Pause */ |
de2200d3 FB |
208 | 0xb7, /* 111 Print */ |
209 | 0xb5, /* 112 Divide */ | |
210 | 0xb8, /* 113 Alt-R */ | |
211 | 0xc6, /* 114 Break */ | |
0f0b7264 FB |
212 | 0x0, /* 115 */ |
213 | 0x0, /* 116 */ | |
214 | 0x0, /* 117 */ | |
215 | 0x0, /* 118 */ | |
216 | 0x0, /* 119 */ | |
b71e95fc | 217 | 0x70, /* 120 Hiragana_Katakana */ |
0f0b7264 FB |
218 | 0x0, /* 121 */ |
219 | 0x0, /* 122 */ | |
b71e95fc | 220 | 0x73, /* 123 backslash */ |
0f0b7264 FB |
221 | 0x0, /* 124 */ |
222 | 0x0, /* 125 */ | |
223 | 0x0, /* 126 */ | |
224 | 0x0, /* 127 */ | |
225 | 0x0, /* 128 */ | |
b71e95fc | 226 | 0x79, /* 129 Henkan */ |
0f0b7264 | 227 | 0x0, /* 130 */ |
b71e95fc | 228 | 0x7b, /* 131 Muhenkan */ |
0f0b7264 | 229 | 0x0, /* 132 */ |
b71e95fc | 230 | 0x7d, /* 133 Yen */ |
0f0b7264 FB |
231 | 0x0, /* 134 */ |
232 | 0x0, /* 135 */ | |
233 | 0x47, /* 136 KP_7 */ | |
234 | 0x48, /* 137 KP_8 */ | |
235 | 0x49, /* 138 KP_9 */ | |
236 | 0x4b, /* 139 KP_4 */ | |
237 | 0x4c, /* 140 KP_5 */ | |
238 | 0x4d, /* 141 KP_6 */ | |
239 | 0x4f, /* 142 KP_1 */ | |
240 | 0x50, /* 143 KP_2 */ | |
241 | 0x51, /* 144 KP_3 */ | |
242 | 0x52, /* 145 KP_0 */ | |
243 | 0x53, /* 146 KP_. */ | |
244 | 0x47, /* 147 KP_HOME */ | |
245 | 0x48, /* 148 KP_UP */ | |
246 | 0x49, /* 149 KP_PgUp */ | |
247 | 0x4b, /* 150 KP_Left */ | |
248 | 0x4c, /* 151 KP_ */ | |
249 | 0x4d, /* 152 KP_Right */ | |
250 | 0x4f, /* 153 KP_End */ | |
251 | 0x50, /* 154 KP_Down */ | |
252 | 0x51, /* 155 KP_PgDn */ | |
253 | 0x52, /* 156 KP_Ins */ | |
254 | 0x53, /* 157 KP_Del */ | |
255 | }; | |
256 | ||
e58d12ed FB |
257 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
258 | { | |
259 | int keycode; | |
260 | ||
261 | keycode = ev->keysym.scancode; | |
262 | ||
263 | if (keycode < 9) { | |
264 | keycode = 0; | |
265 | } else if (keycode < 97) { | |
266 | keycode -= 8; /* just an offset */ | |
267 | } else if (keycode < 158) { | |
268 | /* use conversion table */ | |
269 | keycode = x_keycode_to_pc_keycode[keycode - 97]; | |
270 | } else { | |
271 | keycode = 0; | |
272 | } | |
273 | return keycode; | |
274 | } | |
275 | ||
276 | #endif | |
277 | ||
0f0b7264 FB |
278 | static void sdl_process_key(SDL_KeyboardEvent *ev) |
279 | { | |
de2200d3 FB |
280 | int keycode, v, i; |
281 | static uint8_t modifiers_state[256]; | |
282 | ||
283 | if (ev->keysym.sym == SDLK_PAUSE) { | |
284 | /* specific case */ | |
285 | v = 0; | |
286 | if (ev->type == SDL_KEYUP) | |
287 | v |= 0x80; | |
288 | kbd_put_keycode(0xe1); | |
289 | kbd_put_keycode(0x1d | v); | |
290 | kbd_put_keycode(0x45 | v); | |
291 | return; | |
292 | } | |
293 | ||
0f0b7264 | 294 | /* XXX: not portable, but avoids complicated mappings */ |
e58d12ed | 295 | keycode = sdl_keyevent_to_keycode(ev); |
de2200d3 FB |
296 | |
297 | switch(keycode) { | |
298 | case 0x00: | |
299 | /* sent when leaving window: reset the modifiers state */ | |
300 | for(i = 0; i < 256; i++) { | |
301 | if (modifiers_state[i]) { | |
302 | if (i & 0x80) | |
303 | kbd_put_keycode(0xe0); | |
304 | kbd_put_keycode(i | 0x80); | |
305 | } | |
306 | } | |
307 | return; | |
308 | case 0x2a: /* Left Shift */ | |
309 | case 0x36: /* Right Shift */ | |
310 | case 0x1d: /* Left CTRL */ | |
311 | case 0x9d: /* Right CTRL */ | |
312 | case 0x38: /* Left ALT */ | |
313 | case 0xb8: /* Right ALT */ | |
0f0b7264 | 314 | if (ev->type == SDL_KEYUP) |
de2200d3 FB |
315 | modifiers_state[keycode] = 0; |
316 | else | |
317 | modifiers_state[keycode] = 1; | |
318 | break; | |
319 | case 0x45: /* num lock */ | |
320 | case 0x3a: /* caps lock */ | |
321 | /* SDL does not send the key up event, so we generate it */ | |
322 | kbd_put_keycode(keycode); | |
323 | kbd_put_keycode(keycode | 0x80); | |
324 | return; | |
0f0b7264 | 325 | } |
de2200d3 FB |
326 | |
327 | /* now send the key code */ | |
328 | if (keycode & 0x80) | |
329 | kbd_put_keycode(0xe0); | |
330 | if (ev->type == SDL_KEYUP) | |
331 | kbd_put_keycode(keycode | 0x80); | |
332 | else | |
333 | kbd_put_keycode(keycode & 0x7f); | |
0f0b7264 FB |
334 | } |
335 | ||
8a7ddc38 FB |
336 | static void sdl_update_caption(void) |
337 | { | |
338 | char buf[1024]; | |
339 | strcpy(buf, "QEMU"); | |
340 | if (!vm_running) { | |
341 | strcat(buf, " [Stopped]"); | |
342 | } | |
343 | if (gui_grab) { | |
344 | strcat(buf, " - Press Ctrl-Shift to exit grab"); | |
345 | } | |
346 | SDL_WM_SetCaption(buf, "QEMU"); | |
347 | } | |
348 | ||
0f0b7264 FB |
349 | static void sdl_grab_start(void) |
350 | { | |
0f0b7264 FB |
351 | SDL_ShowCursor(0); |
352 | SDL_WM_GrabInput(SDL_GRAB_ON); | |
353 | /* dummy read to avoid moving the mouse */ | |
354 | SDL_GetRelativeMouseState(NULL, NULL); | |
355 | gui_grab = 1; | |
8a7ddc38 | 356 | sdl_update_caption(); |
0f0b7264 FB |
357 | } |
358 | ||
359 | static void sdl_grab_end(void) | |
360 | { | |
0f0b7264 FB |
361 | SDL_WM_GrabInput(SDL_GRAB_OFF); |
362 | SDL_ShowCursor(1); | |
363 | gui_grab = 0; | |
8a7ddc38 | 364 | sdl_update_caption(); |
0f0b7264 FB |
365 | } |
366 | ||
367 | static void sdl_send_mouse_event(void) | |
368 | { | |
369 | int dx, dy, dz, state, buttons; | |
370 | state = SDL_GetRelativeMouseState(&dx, &dy); | |
371 | buttons = 0; | |
372 | if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) | |
373 | buttons |= MOUSE_EVENT_LBUTTON; | |
374 | if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) | |
375 | buttons |= MOUSE_EVENT_RBUTTON; | |
376 | if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) | |
377 | buttons |= MOUSE_EVENT_MBUTTON; | |
378 | /* XXX: test wheel */ | |
379 | dz = 0; | |
8351d2d4 | 380 | #ifdef SDL_BUTTON_WHEELUP |
0f0b7264 FB |
381 | if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP)) |
382 | dz--; | |
383 | if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN)) | |
384 | dz++; | |
8351d2d4 | 385 | #endif |
0f0b7264 FB |
386 | kbd_mouse_event(dx, dy, dz, buttons); |
387 | } | |
388 | ||
8e9c4afe FB |
389 | static void toggle_full_screen(DisplayState *ds) |
390 | { | |
391 | gui_fullscreen = !gui_fullscreen; | |
392 | sdl_resize(ds, screen->w, screen->h); | |
393 | if (gui_fullscreen) { | |
394 | gui_saved_grab = gui_grab; | |
395 | sdl_grab_start(); | |
396 | } else { | |
397 | if (!gui_saved_grab) | |
398 | sdl_grab_end(); | |
399 | } | |
ee38b4c8 | 400 | vga_invalidate_display(); |
8e9c4afe | 401 | vga_update_display(); |
8e9c4afe FB |
402 | } |
403 | ||
0f0b7264 FB |
404 | static void sdl_refresh(DisplayState *ds) |
405 | { | |
406 | SDL_Event ev1, *ev = &ev1; | |
8e9c4afe FB |
407 | int mod_state; |
408 | ||
8a7ddc38 FB |
409 | if (last_vm_running != vm_running) { |
410 | last_vm_running = vm_running; | |
411 | sdl_update_caption(); | |
412 | } | |
413 | ||
457831f4 FB |
414 | if (is_active_console(vga_console)) |
415 | vga_update_display(); | |
416 | ||
0f0b7264 FB |
417 | while (SDL_PollEvent(ev)) { |
418 | switch (ev->type) { | |
419 | case SDL_VIDEOEXPOSE: | |
420 | sdl_update(ds, 0, 0, screen->w, screen->h); | |
421 | break; | |
422 | case SDL_KEYDOWN: | |
423 | case SDL_KEYUP: | |
424 | if (ev->type == SDL_KEYDOWN) { | |
8e9c4afe FB |
425 | mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) == |
426 | (KMOD_LSHIFT | KMOD_LCTRL); | |
427 | gui_key_modifier_pressed = mod_state; | |
457831f4 FB |
428 | if (gui_key_modifier_pressed) { |
429 | switch(ev->key.keysym.sym) { | |
430 | case SDLK_f: | |
431 | toggle_full_screen(ds); | |
432 | gui_keysym = 1; | |
433 | break; | |
434 | case SDLK_F1 ... SDLK_F12: | |
435 | console_select(ev->key.keysym.sym - SDLK_F1); | |
436 | if (is_active_console(vga_console)) { | |
437 | /* tell the vga console to redisplay itself */ | |
438 | vga_invalidate_display(); | |
439 | } else { | |
440 | /* display grab if going to a text console */ | |
441 | if (gui_grab) | |
442 | sdl_grab_end(); | |
443 | } | |
444 | gui_keysym = 1; | |
445 | break; | |
446 | default: | |
447 | break; | |
448 | } | |
449 | } | |
450 | if (!is_active_console(vga_console)) { | |
451 | int keysym; | |
452 | keysym = 0; | |
453 | if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) { | |
454 | switch(ev->key.keysym.sym) { | |
455 | case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break; | |
456 | case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break; | |
457 | case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break; | |
458 | case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break; | |
459 | case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break; | |
460 | case SDLK_END: keysym = QEMU_KEY_CTRL_END; break; | |
461 | case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break; | |
462 | case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break; | |
463 | default: break; | |
464 | } | |
465 | } else { | |
466 | switch(ev->key.keysym.sym) { | |
467 | case SDLK_UP: keysym = QEMU_KEY_UP; break; | |
468 | case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break; | |
469 | case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break; | |
470 | case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break; | |
471 | case SDLK_HOME: keysym = QEMU_KEY_HOME; break; | |
472 | case SDLK_END: keysym = QEMU_KEY_END; break; | |
473 | case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break; | |
474 | case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break; | |
475 | case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break; | |
476 | default: break; | |
477 | } | |
478 | } | |
479 | if (keysym) { | |
480 | kbd_put_keysym(keysym); | |
481 | } else if (ev->key.keysym.unicode != 0) { | |
482 | kbd_put_keysym(ev->key.keysym.unicode); | |
483 | } | |
8e9c4afe FB |
484 | } |
485 | } else if (ev->type == SDL_KEYUP) { | |
486 | mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)); | |
487 | if (!mod_state) { | |
488 | if (gui_key_modifier_pressed) { | |
457831f4 | 489 | if (gui_keysym == 0) { |
8e9c4afe FB |
490 | /* exit/enter grab if pressing Ctrl-Shift */ |
491 | if (!gui_grab) | |
492 | sdl_grab_start(); | |
493 | else | |
494 | sdl_grab_end(); | |
495 | break; | |
496 | } | |
497 | gui_key_modifier_pressed = 0; | |
498 | gui_keysym = 0; | |
499 | } | |
0f0b7264 FB |
500 | } |
501 | } | |
457831f4 FB |
502 | if (is_active_console(vga_console)) |
503 | sdl_process_key(&ev->key); | |
0f0b7264 FB |
504 | break; |
505 | case SDL_QUIT: | |
979a54fb | 506 | qemu_system_shutdown_request(); |
0f0b7264 FB |
507 | break; |
508 | case SDL_MOUSEMOTION: | |
509 | if (gui_grab) { | |
510 | sdl_send_mouse_event(); | |
511 | } | |
512 | break; | |
513 | case SDL_MOUSEBUTTONDOWN: | |
514 | case SDL_MOUSEBUTTONUP: | |
515 | { | |
516 | SDL_MouseButtonEvent *bev = &ev->button; | |
517 | if (!gui_grab) { | |
518 | if (ev->type == SDL_MOUSEBUTTONDOWN && | |
519 | (bev->state & SDL_BUTTON_LMASK)) { | |
520 | /* start grabbing all events */ | |
521 | sdl_grab_start(); | |
522 | } | |
523 | } else { | |
524 | sdl_send_mouse_event(); | |
525 | } | |
526 | } | |
527 | break; | |
0294ffb9 | 528 | case SDL_ACTIVEEVENT: |
d63d307f FB |
529 | if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0 && |
530 | !gui_fullscreen_initial_grab) { | |
0294ffb9 FB |
531 | sdl_grab_end(); |
532 | } | |
533 | break; | |
0f0b7264 FB |
534 | default: |
535 | break; | |
536 | } | |
537 | } | |
538 | } | |
539 | ||
898712a8 FB |
540 | static void sdl_cleanup(void) |
541 | { | |
542 | SDL_Quit(); | |
543 | } | |
544 | ||
d63d307f | 545 | void sdl_display_init(DisplayState *ds, int full_screen) |
0f0b7264 FB |
546 | { |
547 | int flags; | |
548 | ||
549 | flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE; | |
550 | if (SDL_Init (flags)) { | |
551 | fprintf(stderr, "Could not initialize SDL - exiting\n"); | |
552 | exit(1); | |
553 | } | |
67b915a5 | 554 | #ifndef _WIN32 |
0ae04d73 FB |
555 | /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */ |
556 | signal(SIGINT, SIG_DFL); | |
557 | signal(SIGQUIT, SIG_DFL); | |
67b915a5 | 558 | #endif |
0ae04d73 | 559 | |
0f0b7264 FB |
560 | ds->dpy_update = sdl_update; |
561 | ds->dpy_resize = sdl_resize; | |
562 | ds->dpy_refresh = sdl_refresh; | |
563 | ||
564 | sdl_resize(ds, 640, 400); | |
8a7ddc38 | 565 | sdl_update_caption(); |
0f0b7264 | 566 | SDL_EnableKeyRepeat(250, 50); |
457831f4 | 567 | SDL_EnableUNICODE(1); |
0f0b7264 | 568 | gui_grab = 0; |
898712a8 FB |
569 | |
570 | atexit(sdl_cleanup); | |
d63d307f FB |
571 | if (full_screen) { |
572 | gui_fullscreen = 1; | |
573 | gui_fullscreen_initial_grab = 1; | |
574 | sdl_grab_start(); | |
575 | } | |
0f0b7264 | 576 | } |