root / sdl.c @ 977d5710
History | View | Annotate | Download (15.4 kB)
1 | 0f0b7264 | bellard | /*
|
---|---|---|---|
2 | 0f0b7264 | bellard | * QEMU SDL display driver
|
3 | 0f0b7264 | bellard | *
|
4 | 0f0b7264 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 0f0b7264 | bellard | *
|
6 | 0f0b7264 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 0f0b7264 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 0f0b7264 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 0f0b7264 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 0f0b7264 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 0f0b7264 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 0f0b7264 | bellard | *
|
13 | 0f0b7264 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 0f0b7264 | bellard | * all copies or substantial portions of the Software.
|
15 | 0f0b7264 | bellard | *
|
16 | 0f0b7264 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 0f0b7264 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 0f0b7264 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 0f0b7264 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 0f0b7264 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 0f0b7264 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 0f0b7264 | bellard | * THE SOFTWARE.
|
23 | 0f0b7264 | bellard | */
|
24 | 67b915a5 | bellard | #include "vl.h" |
25 | 0f0b7264 | bellard | |
26 | 0f0b7264 | bellard | #include <SDL.h> |
27 | 0f0b7264 | bellard | |
28 | 67b915a5 | bellard | #ifndef _WIN32
|
29 | 67b915a5 | bellard | #include <signal.h> |
30 | 67b915a5 | bellard | #endif
|
31 | 0f0b7264 | bellard | |
32 | 0f0b7264 | bellard | static SDL_Surface *screen;
|
33 | 0f0b7264 | bellard | static int gui_grab; /* if true, all keyboard/mouse events are grabbed */ |
34 | 8a7ddc38 | bellard | static int last_vm_running; |
35 | 8e9c4afe | bellard | static int gui_saved_grab; |
36 | 8e9c4afe | bellard | static int gui_fullscreen; |
37 | 8e9c4afe | bellard | static int gui_key_modifier_pressed; |
38 | 8e9c4afe | bellard | static int gui_keysym; |
39 | d63d307f | bellard | static int gui_fullscreen_initial_grab; |
40 | 32ff25bf | bellard | static int gui_grab_code = KMOD_LALT | KMOD_LCTRL; |
41 | 32ff25bf | bellard | static uint8_t modifiers_state[256]; |
42 | 0f0b7264 | bellard | |
43 | 0f0b7264 | bellard | static void sdl_update(DisplayState *ds, int x, int y, int w, int h) |
44 | 0f0b7264 | bellard | { |
45 | 898712a8 | bellard | // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
|
46 | 0f0b7264 | bellard | SDL_UpdateRect(screen, x, y, w, h); |
47 | 0f0b7264 | bellard | } |
48 | 0f0b7264 | bellard | |
49 | 0f0b7264 | bellard | static void sdl_resize(DisplayState *ds, int w, int h) |
50 | 0f0b7264 | bellard | { |
51 | 0f0b7264 | bellard | int flags;
|
52 | 0f0b7264 | bellard | |
53 | 0f0b7264 | bellard | // printf("resizing to %d %d\n", w, h);
|
54 | 0f0b7264 | bellard | |
55 | 0f0b7264 | bellard | flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL; |
56 | 0f0b7264 | bellard | flags |= SDL_RESIZABLE; |
57 | 8e9c4afe | bellard | if (gui_fullscreen)
|
58 | 8e9c4afe | bellard | flags |= SDL_FULLSCREEN; |
59 | 0f0b7264 | bellard | screen = SDL_SetVideoMode(w, h, 0, flags);
|
60 | 0f0b7264 | bellard | if (!screen) {
|
61 | 0f0b7264 | bellard | fprintf(stderr, "Could not open SDL display\n");
|
62 | 0f0b7264 | bellard | exit(1);
|
63 | 0f0b7264 | bellard | } |
64 | 0f0b7264 | bellard | ds->data = screen->pixels; |
65 | 0f0b7264 | bellard | ds->linesize = screen->pitch; |
66 | 0f0b7264 | bellard | ds->depth = screen->format->BitsPerPixel; |
67 | 457831f4 | bellard | ds->width = w; |
68 | 457831f4 | bellard | ds->height = h; |
69 | 0f0b7264 | bellard | } |
70 | 0f0b7264 | bellard | |
71 | 3d11d0eb | bellard | /* generic keyboard conversion */
|
72 | e58d12ed | bellard | |
73 | 3d11d0eb | bellard | #include "sdl_keysym.h" |
74 | 3d11d0eb | bellard | #include "keymaps.c" |
75 | 3d11d0eb | bellard | |
76 | 3d11d0eb | bellard | static kbd_layout_t *kbd_layout = NULL; |
77 | 3d11d0eb | bellard | |
78 | 3d11d0eb | bellard | static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev) |
79 | e58d12ed | bellard | { |
80 | 3d11d0eb | bellard | int keysym;
|
81 | 3d11d0eb | bellard | /* workaround for X11+SDL bug with AltGR */
|
82 | 3d11d0eb | bellard | keysym = ev->keysym.sym; |
83 | 3d11d0eb | bellard | if (keysym == 0 && ev->keysym.scancode == 113) |
84 | 3d11d0eb | bellard | keysym = SDLK_MODE; |
85 | 3d11d0eb | bellard | return keysym2scancode(kbd_layout, keysym);
|
86 | e58d12ed | bellard | } |
87 | e58d12ed | bellard | |
88 | 3d11d0eb | bellard | /* specific keyboard conversions from scan codes */
|
89 | 3d11d0eb | bellard | |
90 | 3d11d0eb | bellard | #if defined(_WIN32)
|
91 | e58d12ed | bellard | |
92 | e58d12ed | bellard | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
93 | e58d12ed | bellard | { |
94 | e58d12ed | bellard | return ev->keysym.scancode;
|
95 | e58d12ed | bellard | } |
96 | e58d12ed | bellard | |
97 | e58d12ed | bellard | #else
|
98 | e58d12ed | bellard | |
99 | de2200d3 | bellard | static const uint8_t x_keycode_to_pc_keycode[61] = { |
100 | de2200d3 | bellard | 0xc7, /* 97 Home */ |
101 | de2200d3 | bellard | 0xc8, /* 98 Up */ |
102 | de2200d3 | bellard | 0xc9, /* 99 PgUp */ |
103 | de2200d3 | bellard | 0xcb, /* 100 Left */ |
104 | 0f0b7264 | bellard | 0x4c, /* 101 KP-5 */ |
105 | de2200d3 | bellard | 0xcd, /* 102 Right */ |
106 | de2200d3 | bellard | 0xcf, /* 103 End */ |
107 | de2200d3 | bellard | 0xd0, /* 104 Down */ |
108 | de2200d3 | bellard | 0xd1, /* 105 PgDn */ |
109 | de2200d3 | bellard | 0xd2, /* 106 Ins */ |
110 | de2200d3 | bellard | 0xd3, /* 107 Del */ |
111 | de2200d3 | bellard | 0x9c, /* 108 Enter */ |
112 | de2200d3 | bellard | 0x9d, /* 109 Ctrl-R */ |
113 | 22a56b8a | bellard | 0x0, /* 110 Pause */ |
114 | de2200d3 | bellard | 0xb7, /* 111 Print */ |
115 | de2200d3 | bellard | 0xb5, /* 112 Divide */ |
116 | de2200d3 | bellard | 0xb8, /* 113 Alt-R */ |
117 | de2200d3 | bellard | 0xc6, /* 114 Break */ |
118 | 0f0b7264 | bellard | 0x0, /* 115 */ |
119 | 0f0b7264 | bellard | 0x0, /* 116 */ |
120 | 0f0b7264 | bellard | 0x0, /* 117 */ |
121 | 0f0b7264 | bellard | 0x0, /* 118 */ |
122 | 0f0b7264 | bellard | 0x0, /* 119 */ |
123 | b71e95fc | bellard | 0x70, /* 120 Hiragana_Katakana */ |
124 | 0f0b7264 | bellard | 0x0, /* 121 */ |
125 | 0f0b7264 | bellard | 0x0, /* 122 */ |
126 | b71e95fc | bellard | 0x73, /* 123 backslash */ |
127 | 0f0b7264 | bellard | 0x0, /* 124 */ |
128 | 0f0b7264 | bellard | 0x0, /* 125 */ |
129 | 0f0b7264 | bellard | 0x0, /* 126 */ |
130 | 0f0b7264 | bellard | 0x0, /* 127 */ |
131 | 0f0b7264 | bellard | 0x0, /* 128 */ |
132 | b71e95fc | bellard | 0x79, /* 129 Henkan */ |
133 | 0f0b7264 | bellard | 0x0, /* 130 */ |
134 | b71e95fc | bellard | 0x7b, /* 131 Muhenkan */ |
135 | 0f0b7264 | bellard | 0x0, /* 132 */ |
136 | b71e95fc | bellard | 0x7d, /* 133 Yen */ |
137 | 0f0b7264 | bellard | 0x0, /* 134 */ |
138 | 0f0b7264 | bellard | 0x0, /* 135 */ |
139 | 0f0b7264 | bellard | 0x47, /* 136 KP_7 */ |
140 | 0f0b7264 | bellard | 0x48, /* 137 KP_8 */ |
141 | 0f0b7264 | bellard | 0x49, /* 138 KP_9 */ |
142 | 0f0b7264 | bellard | 0x4b, /* 139 KP_4 */ |
143 | 0f0b7264 | bellard | 0x4c, /* 140 KP_5 */ |
144 | 0f0b7264 | bellard | 0x4d, /* 141 KP_6 */ |
145 | 0f0b7264 | bellard | 0x4f, /* 142 KP_1 */ |
146 | 0f0b7264 | bellard | 0x50, /* 143 KP_2 */ |
147 | 0f0b7264 | bellard | 0x51, /* 144 KP_3 */ |
148 | 0f0b7264 | bellard | 0x52, /* 145 KP_0 */ |
149 | 0f0b7264 | bellard | 0x53, /* 146 KP_. */ |
150 | 0f0b7264 | bellard | 0x47, /* 147 KP_HOME */ |
151 | 0f0b7264 | bellard | 0x48, /* 148 KP_UP */ |
152 | 0f0b7264 | bellard | 0x49, /* 149 KP_PgUp */ |
153 | 0f0b7264 | bellard | 0x4b, /* 150 KP_Left */ |
154 | 0f0b7264 | bellard | 0x4c, /* 151 KP_ */ |
155 | 0f0b7264 | bellard | 0x4d, /* 152 KP_Right */ |
156 | 0f0b7264 | bellard | 0x4f, /* 153 KP_End */ |
157 | 0f0b7264 | bellard | 0x50, /* 154 KP_Down */ |
158 | 0f0b7264 | bellard | 0x51, /* 155 KP_PgDn */ |
159 | 0f0b7264 | bellard | 0x52, /* 156 KP_Ins */ |
160 | 0f0b7264 | bellard | 0x53, /* 157 KP_Del */ |
161 | 0f0b7264 | bellard | }; |
162 | 0f0b7264 | bellard | |
163 | e58d12ed | bellard | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
164 | e58d12ed | bellard | { |
165 | e58d12ed | bellard | int keycode;
|
166 | e58d12ed | bellard | |
167 | e58d12ed | bellard | keycode = ev->keysym.scancode; |
168 | e58d12ed | bellard | |
169 | e58d12ed | bellard | if (keycode < 9) { |
170 | e58d12ed | bellard | keycode = 0;
|
171 | e58d12ed | bellard | } else if (keycode < 97) { |
172 | e58d12ed | bellard | keycode -= 8; /* just an offset */ |
173 | e58d12ed | bellard | } else if (keycode < 158) { |
174 | e58d12ed | bellard | /* use conversion table */
|
175 | e58d12ed | bellard | keycode = x_keycode_to_pc_keycode[keycode - 97];
|
176 | e58d12ed | bellard | } else {
|
177 | e58d12ed | bellard | keycode = 0;
|
178 | e58d12ed | bellard | } |
179 | e58d12ed | bellard | return keycode;
|
180 | e58d12ed | bellard | } |
181 | e58d12ed | bellard | |
182 | e58d12ed | bellard | #endif
|
183 | e58d12ed | bellard | |
184 | 32ff25bf | bellard | static void reset_keys(void) |
185 | 32ff25bf | bellard | { |
186 | 32ff25bf | bellard | int i;
|
187 | 32ff25bf | bellard | for(i = 0; i < 256; i++) { |
188 | 32ff25bf | bellard | if (modifiers_state[i]) {
|
189 | 32ff25bf | bellard | if (i & 0x80) |
190 | 32ff25bf | bellard | kbd_put_keycode(0xe0);
|
191 | 32ff25bf | bellard | kbd_put_keycode(i | 0x80);
|
192 | 32ff25bf | bellard | modifiers_state[i] = 0;
|
193 | 32ff25bf | bellard | } |
194 | 32ff25bf | bellard | } |
195 | 32ff25bf | bellard | } |
196 | 32ff25bf | bellard | |
197 | 0f0b7264 | bellard | static void sdl_process_key(SDL_KeyboardEvent *ev) |
198 | 0f0b7264 | bellard | { |
199 | 32ff25bf | bellard | int keycode, v;
|
200 | de2200d3 | bellard | |
201 | de2200d3 | bellard | if (ev->keysym.sym == SDLK_PAUSE) {
|
202 | de2200d3 | bellard | /* specific case */
|
203 | de2200d3 | bellard | v = 0;
|
204 | de2200d3 | bellard | if (ev->type == SDL_KEYUP)
|
205 | de2200d3 | bellard | v |= 0x80;
|
206 | de2200d3 | bellard | kbd_put_keycode(0xe1);
|
207 | de2200d3 | bellard | kbd_put_keycode(0x1d | v);
|
208 | de2200d3 | bellard | kbd_put_keycode(0x45 | v);
|
209 | de2200d3 | bellard | return;
|
210 | de2200d3 | bellard | } |
211 | de2200d3 | bellard | |
212 | 3d11d0eb | bellard | if (kbd_layout) {
|
213 | 3d11d0eb | bellard | keycode = sdl_keyevent_to_keycode_generic(ev); |
214 | 3d11d0eb | bellard | } else {
|
215 | 3d11d0eb | bellard | keycode = sdl_keyevent_to_keycode(ev); |
216 | 3d11d0eb | bellard | } |
217 | de2200d3 | bellard | |
218 | de2200d3 | bellard | switch(keycode) {
|
219 | de2200d3 | bellard | case 0x00: |
220 | de2200d3 | bellard | /* sent when leaving window: reset the modifiers state */
|
221 | 32ff25bf | bellard | reset_keys(); |
222 | de2200d3 | bellard | return;
|
223 | de2200d3 | bellard | case 0x2a: /* Left Shift */ |
224 | de2200d3 | bellard | case 0x36: /* Right Shift */ |
225 | de2200d3 | bellard | case 0x1d: /* Left CTRL */ |
226 | de2200d3 | bellard | case 0x9d: /* Right CTRL */ |
227 | de2200d3 | bellard | case 0x38: /* Left ALT */ |
228 | de2200d3 | bellard | case 0xb8: /* Right ALT */ |
229 | 0f0b7264 | bellard | if (ev->type == SDL_KEYUP)
|
230 | de2200d3 | bellard | modifiers_state[keycode] = 0;
|
231 | de2200d3 | bellard | else
|
232 | de2200d3 | bellard | modifiers_state[keycode] = 1;
|
233 | de2200d3 | bellard | break;
|
234 | de2200d3 | bellard | case 0x45: /* num lock */ |
235 | de2200d3 | bellard | case 0x3a: /* caps lock */ |
236 | de2200d3 | bellard | /* SDL does not send the key up event, so we generate it */
|
237 | de2200d3 | bellard | kbd_put_keycode(keycode); |
238 | de2200d3 | bellard | kbd_put_keycode(keycode | 0x80);
|
239 | de2200d3 | bellard | return;
|
240 | 0f0b7264 | bellard | } |
241 | de2200d3 | bellard | |
242 | de2200d3 | bellard | /* now send the key code */
|
243 | de2200d3 | bellard | if (keycode & 0x80) |
244 | de2200d3 | bellard | kbd_put_keycode(0xe0);
|
245 | de2200d3 | bellard | if (ev->type == SDL_KEYUP)
|
246 | de2200d3 | bellard | kbd_put_keycode(keycode | 0x80);
|
247 | de2200d3 | bellard | else
|
248 | de2200d3 | bellard | kbd_put_keycode(keycode & 0x7f);
|
249 | 0f0b7264 | bellard | } |
250 | 0f0b7264 | bellard | |
251 | 8a7ddc38 | bellard | static void sdl_update_caption(void) |
252 | 8a7ddc38 | bellard | { |
253 | 8a7ddc38 | bellard | char buf[1024]; |
254 | 8a7ddc38 | bellard | strcpy(buf, "QEMU");
|
255 | 8a7ddc38 | bellard | if (!vm_running) {
|
256 | 8a7ddc38 | bellard | strcat(buf, " [Stopped]");
|
257 | 8a7ddc38 | bellard | } |
258 | 8a7ddc38 | bellard | if (gui_grab) {
|
259 | 32ff25bf | bellard | strcat(buf, " - Press Ctrl-Alt to exit grab");
|
260 | 8a7ddc38 | bellard | } |
261 | 8a7ddc38 | bellard | SDL_WM_SetCaption(buf, "QEMU");
|
262 | 8a7ddc38 | bellard | } |
263 | 8a7ddc38 | bellard | |
264 | 0f0b7264 | bellard | static void sdl_grab_start(void) |
265 | 0f0b7264 | bellard | { |
266 | 0f0b7264 | bellard | SDL_ShowCursor(0);
|
267 | 0f0b7264 | bellard | SDL_WM_GrabInput(SDL_GRAB_ON); |
268 | 0f0b7264 | bellard | /* dummy read to avoid moving the mouse */
|
269 | 0f0b7264 | bellard | SDL_GetRelativeMouseState(NULL, NULL); |
270 | 0f0b7264 | bellard | gui_grab = 1;
|
271 | 8a7ddc38 | bellard | sdl_update_caption(); |
272 | 0f0b7264 | bellard | } |
273 | 0f0b7264 | bellard | |
274 | 0f0b7264 | bellard | static void sdl_grab_end(void) |
275 | 0f0b7264 | bellard | { |
276 | 0f0b7264 | bellard | SDL_WM_GrabInput(SDL_GRAB_OFF); |
277 | 0f0b7264 | bellard | SDL_ShowCursor(1);
|
278 | 0f0b7264 | bellard | gui_grab = 0;
|
279 | 8a7ddc38 | bellard | sdl_update_caption(); |
280 | 0f0b7264 | bellard | } |
281 | 0f0b7264 | bellard | |
282 | 0f0b7264 | bellard | static void sdl_send_mouse_event(void) |
283 | 0f0b7264 | bellard | { |
284 | 0f0b7264 | bellard | int dx, dy, dz, state, buttons;
|
285 | 0f0b7264 | bellard | state = SDL_GetRelativeMouseState(&dx, &dy); |
286 | 0f0b7264 | bellard | buttons = 0;
|
287 | 0f0b7264 | bellard | if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
|
288 | 0f0b7264 | bellard | buttons |= MOUSE_EVENT_LBUTTON; |
289 | 0f0b7264 | bellard | if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
|
290 | 0f0b7264 | bellard | buttons |= MOUSE_EVENT_RBUTTON; |
291 | 0f0b7264 | bellard | if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
|
292 | 0f0b7264 | bellard | buttons |= MOUSE_EVENT_MBUTTON; |
293 | 0f0b7264 | bellard | /* XXX: test wheel */
|
294 | 0f0b7264 | bellard | dz = 0;
|
295 | 8351d2d4 | bellard | #ifdef SDL_BUTTON_WHEELUP
|
296 | 0f0b7264 | bellard | if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
|
297 | 0f0b7264 | bellard | dz--; |
298 | 0f0b7264 | bellard | if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
|
299 | 0f0b7264 | bellard | dz++; |
300 | 8351d2d4 | bellard | #endif
|
301 | 0f0b7264 | bellard | kbd_mouse_event(dx, dy, dz, buttons); |
302 | 0f0b7264 | bellard | } |
303 | 0f0b7264 | bellard | |
304 | 8e9c4afe | bellard | static void toggle_full_screen(DisplayState *ds) |
305 | 8e9c4afe | bellard | { |
306 | 8e9c4afe | bellard | gui_fullscreen = !gui_fullscreen; |
307 | 8e9c4afe | bellard | sdl_resize(ds, screen->w, screen->h); |
308 | 8e9c4afe | bellard | if (gui_fullscreen) {
|
309 | 8e9c4afe | bellard | gui_saved_grab = gui_grab; |
310 | 8e9c4afe | bellard | sdl_grab_start(); |
311 | 8e9c4afe | bellard | } else {
|
312 | 8e9c4afe | bellard | if (!gui_saved_grab)
|
313 | 8e9c4afe | bellard | sdl_grab_end(); |
314 | 8e9c4afe | bellard | } |
315 | ee38b4c8 | bellard | vga_invalidate_display(); |
316 | 8e9c4afe | bellard | vga_update_display(); |
317 | 8e9c4afe | bellard | } |
318 | 8e9c4afe | bellard | |
319 | 0f0b7264 | bellard | static void sdl_refresh(DisplayState *ds) |
320 | 0f0b7264 | bellard | { |
321 | 0f0b7264 | bellard | SDL_Event ev1, *ev = &ev1; |
322 | 8e9c4afe | bellard | int mod_state;
|
323 | 8e9c4afe | bellard | |
324 | 8a7ddc38 | bellard | if (last_vm_running != vm_running) {
|
325 | 8a7ddc38 | bellard | last_vm_running = vm_running; |
326 | 8a7ddc38 | bellard | sdl_update_caption(); |
327 | 8a7ddc38 | bellard | } |
328 | 8a7ddc38 | bellard | |
329 | 457831f4 | bellard | if (is_active_console(vga_console))
|
330 | 457831f4 | bellard | vga_update_display(); |
331 | 457831f4 | bellard | |
332 | 0f0b7264 | bellard | while (SDL_PollEvent(ev)) {
|
333 | 0f0b7264 | bellard | switch (ev->type) {
|
334 | 0f0b7264 | bellard | case SDL_VIDEOEXPOSE:
|
335 | 0f0b7264 | bellard | sdl_update(ds, 0, 0, screen->w, screen->h); |
336 | 0f0b7264 | bellard | break;
|
337 | 0f0b7264 | bellard | case SDL_KEYDOWN:
|
338 | 0f0b7264 | bellard | case SDL_KEYUP:
|
339 | 0f0b7264 | bellard | if (ev->type == SDL_KEYDOWN) {
|
340 | 32ff25bf | bellard | mod_state = (SDL_GetModState() & gui_grab_code) == |
341 | 32ff25bf | bellard | gui_grab_code; |
342 | 8e9c4afe | bellard | gui_key_modifier_pressed = mod_state; |
343 | 457831f4 | bellard | if (gui_key_modifier_pressed) {
|
344 | 32ff25bf | bellard | int keycode;
|
345 | 32ff25bf | bellard | keycode = sdl_keyevent_to_keycode(&ev->key); |
346 | 32ff25bf | bellard | switch(keycode) {
|
347 | 32ff25bf | bellard | case 0x21: /* 'f' key on US keyboard */ |
348 | 457831f4 | bellard | toggle_full_screen(ds); |
349 | 457831f4 | bellard | gui_keysym = 1;
|
350 | 457831f4 | bellard | break;
|
351 | 32ff25bf | bellard | case 0x02 ... 0x0a: /* '1' to '9' keys */ |
352 | 32ff25bf | bellard | console_select(keycode - 0x02);
|
353 | 457831f4 | bellard | if (is_active_console(vga_console)) {
|
354 | 457831f4 | bellard | /* tell the vga console to redisplay itself */
|
355 | 457831f4 | bellard | vga_invalidate_display(); |
356 | 457831f4 | bellard | } else {
|
357 | 457831f4 | bellard | /* display grab if going to a text console */
|
358 | 457831f4 | bellard | if (gui_grab)
|
359 | 457831f4 | bellard | sdl_grab_end(); |
360 | 457831f4 | bellard | } |
361 | 457831f4 | bellard | gui_keysym = 1;
|
362 | 457831f4 | bellard | break;
|
363 | 457831f4 | bellard | default:
|
364 | 457831f4 | bellard | break;
|
365 | 457831f4 | bellard | } |
366 | 32ff25bf | bellard | } else if (!is_active_console(vga_console)) { |
367 | 457831f4 | bellard | int keysym;
|
368 | 457831f4 | bellard | keysym = 0;
|
369 | 457831f4 | bellard | if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
|
370 | 457831f4 | bellard | switch(ev->key.keysym.sym) {
|
371 | 457831f4 | bellard | case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break; |
372 | 457831f4 | bellard | case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break; |
373 | 457831f4 | bellard | case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break; |
374 | 457831f4 | bellard | case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break; |
375 | 457831f4 | bellard | case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break; |
376 | 457831f4 | bellard | case SDLK_END: keysym = QEMU_KEY_CTRL_END; break; |
377 | 457831f4 | bellard | case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break; |
378 | 457831f4 | bellard | case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break; |
379 | 457831f4 | bellard | default: break; |
380 | 457831f4 | bellard | } |
381 | 457831f4 | bellard | } else {
|
382 | 457831f4 | bellard | switch(ev->key.keysym.sym) {
|
383 | 457831f4 | bellard | case SDLK_UP: keysym = QEMU_KEY_UP; break; |
384 | 457831f4 | bellard | case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break; |
385 | 457831f4 | bellard | case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break; |
386 | 457831f4 | bellard | case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break; |
387 | 457831f4 | bellard | case SDLK_HOME: keysym = QEMU_KEY_HOME; break; |
388 | 457831f4 | bellard | case SDLK_END: keysym = QEMU_KEY_END; break; |
389 | 457831f4 | bellard | case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break; |
390 | 457831f4 | bellard | case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break; |
391 | 457831f4 | bellard | case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break; |
392 | 457831f4 | bellard | default: break; |
393 | 457831f4 | bellard | } |
394 | 457831f4 | bellard | } |
395 | 457831f4 | bellard | if (keysym) {
|
396 | 457831f4 | bellard | kbd_put_keysym(keysym); |
397 | 457831f4 | bellard | } else if (ev->key.keysym.unicode != 0) { |
398 | 457831f4 | bellard | kbd_put_keysym(ev->key.keysym.unicode); |
399 | 457831f4 | bellard | } |
400 | 8e9c4afe | bellard | } |
401 | 8e9c4afe | bellard | } else if (ev->type == SDL_KEYUP) { |
402 | bf2b84e4 | bellard | mod_state = (ev->key.keysym.mod & gui_grab_code); |
403 | 8e9c4afe | bellard | if (!mod_state) {
|
404 | 8e9c4afe | bellard | if (gui_key_modifier_pressed) {
|
405 | 457831f4 | bellard | if (gui_keysym == 0) { |
406 | 32ff25bf | bellard | /* exit/enter grab if pressing Ctrl-Alt */
|
407 | 8e9c4afe | bellard | if (!gui_grab)
|
408 | 8e9c4afe | bellard | sdl_grab_start(); |
409 | 8e9c4afe | bellard | else
|
410 | 8e9c4afe | bellard | sdl_grab_end(); |
411 | 32ff25bf | bellard | /* SDL does not send back all the
|
412 | 32ff25bf | bellard | modifiers key, so we must correct it */
|
413 | 32ff25bf | bellard | reset_keys(); |
414 | 8e9c4afe | bellard | break;
|
415 | 8e9c4afe | bellard | } |
416 | 8e9c4afe | bellard | gui_key_modifier_pressed = 0;
|
417 | 8e9c4afe | bellard | gui_keysym = 0;
|
418 | 8e9c4afe | bellard | } |
419 | 0f0b7264 | bellard | } |
420 | 0f0b7264 | bellard | } |
421 | 457831f4 | bellard | if (is_active_console(vga_console))
|
422 | 457831f4 | bellard | sdl_process_key(&ev->key); |
423 | 0f0b7264 | bellard | break;
|
424 | 0f0b7264 | bellard | case SDL_QUIT:
|
425 | 979a54fb | bellard | qemu_system_shutdown_request(); |
426 | 0f0b7264 | bellard | break;
|
427 | 0f0b7264 | bellard | case SDL_MOUSEMOTION:
|
428 | 0f0b7264 | bellard | if (gui_grab) {
|
429 | 0f0b7264 | bellard | sdl_send_mouse_event(); |
430 | 0f0b7264 | bellard | } |
431 | 0f0b7264 | bellard | break;
|
432 | 0f0b7264 | bellard | case SDL_MOUSEBUTTONDOWN:
|
433 | 0f0b7264 | bellard | case SDL_MOUSEBUTTONUP:
|
434 | 0f0b7264 | bellard | { |
435 | 0f0b7264 | bellard | SDL_MouseButtonEvent *bev = &ev->button; |
436 | 0f0b7264 | bellard | if (!gui_grab) {
|
437 | 0f0b7264 | bellard | if (ev->type == SDL_MOUSEBUTTONDOWN &&
|
438 | 0f0b7264 | bellard | (bev->state & SDL_BUTTON_LMASK)) { |
439 | 0f0b7264 | bellard | /* start grabbing all events */
|
440 | 0f0b7264 | bellard | sdl_grab_start(); |
441 | 0f0b7264 | bellard | } |
442 | 0f0b7264 | bellard | } else {
|
443 | 0f0b7264 | bellard | sdl_send_mouse_event(); |
444 | 0f0b7264 | bellard | } |
445 | 0f0b7264 | bellard | } |
446 | 0f0b7264 | bellard | break;
|
447 | 0294ffb9 | bellard | case SDL_ACTIVEEVENT:
|
448 | d63d307f | bellard | if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0 && |
449 | d63d307f | bellard | !gui_fullscreen_initial_grab) { |
450 | 0294ffb9 | bellard | sdl_grab_end(); |
451 | 0294ffb9 | bellard | } |
452 | 0294ffb9 | bellard | break;
|
453 | 0f0b7264 | bellard | default:
|
454 | 0f0b7264 | bellard | break;
|
455 | 0f0b7264 | bellard | } |
456 | 0f0b7264 | bellard | } |
457 | 0f0b7264 | bellard | } |
458 | 0f0b7264 | bellard | |
459 | 898712a8 | bellard | static void sdl_cleanup(void) |
460 | 898712a8 | bellard | { |
461 | 898712a8 | bellard | SDL_Quit(); |
462 | 898712a8 | bellard | } |
463 | 898712a8 | bellard | |
464 | d63d307f | bellard | void sdl_display_init(DisplayState *ds, int full_screen) |
465 | 0f0b7264 | bellard | { |
466 | 0f0b7264 | bellard | int flags;
|
467 | 0f0b7264 | bellard | |
468 | 3d11d0eb | bellard | #if defined(__APPLE__)
|
469 | 3d11d0eb | bellard | /* always use generic keymaps */
|
470 | 3d11d0eb | bellard | if (!keyboard_layout)
|
471 | 3d11d0eb | bellard | keyboard_layout = "en-us";
|
472 | 3d11d0eb | bellard | #endif
|
473 | 3d11d0eb | bellard | if(keyboard_layout) {
|
474 | 3d11d0eb | bellard | kbd_layout = init_keyboard_layout(keyboard_layout); |
475 | 3d11d0eb | bellard | if (!kbd_layout)
|
476 | 3d11d0eb | bellard | exit(1);
|
477 | 3d11d0eb | bellard | } |
478 | 3d11d0eb | bellard | |
479 | 0f0b7264 | bellard | flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE; |
480 | 0f0b7264 | bellard | if (SDL_Init (flags)) {
|
481 | 0f0b7264 | bellard | fprintf(stderr, "Could not initialize SDL - exiting\n");
|
482 | 0f0b7264 | bellard | exit(1);
|
483 | 0f0b7264 | bellard | } |
484 | 67b915a5 | bellard | #ifndef _WIN32
|
485 | 0ae04d73 | bellard | /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
|
486 | 0ae04d73 | bellard | signal(SIGINT, SIG_DFL); |
487 | 0ae04d73 | bellard | signal(SIGQUIT, SIG_DFL); |
488 | 67b915a5 | bellard | #endif
|
489 | 0ae04d73 | bellard | |
490 | 0f0b7264 | bellard | ds->dpy_update = sdl_update; |
491 | 0f0b7264 | bellard | ds->dpy_resize = sdl_resize; |
492 | 0f0b7264 | bellard | ds->dpy_refresh = sdl_refresh; |
493 | 0f0b7264 | bellard | |
494 | 0f0b7264 | bellard | sdl_resize(ds, 640, 400); |
495 | 8a7ddc38 | bellard | sdl_update_caption(); |
496 | 0f0b7264 | bellard | SDL_EnableKeyRepeat(250, 50); |
497 | 457831f4 | bellard | SDL_EnableUNICODE(1);
|
498 | 0f0b7264 | bellard | gui_grab = 0;
|
499 | 898712a8 | bellard | |
500 | 898712a8 | bellard | atexit(sdl_cleanup); |
501 | d63d307f | bellard | if (full_screen) {
|
502 | d63d307f | bellard | gui_fullscreen = 1;
|
503 | d63d307f | bellard | gui_fullscreen_initial_grab = 1;
|
504 | d63d307f | bellard | sdl_grab_start(); |
505 | d63d307f | bellard | } |
506 | 0f0b7264 | bellard | } |