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