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