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