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