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