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