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