]>
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 | */ | |
cdfb017e SW |
24 | |
25 | /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */ | |
26 | #undef WIN32_LEAN_AND_MEAN | |
27 | ||
0f0b7264 | 28 | #include <SDL.h> |
c9985aa8 | 29 | #include <SDL_syswm.h> |
0f0b7264 | 30 | |
511d2b14 BS |
31 | #include "qemu-common.h" |
32 | #include "console.h" | |
33 | #include "sysemu.h" | |
34 | #include "x_keymap.h" | |
c18a2c36 | 35 | #include "sdl_zoom.h" |
511d2b14 | 36 | |
7d957bd8 AL |
37 | static DisplayChangeListener *dcl; |
38 | static SDL_Surface *real_screen; | |
39 | static SDL_Surface *guest_screen = NULL; | |
0f0b7264 | 40 | static int gui_grab; /* if true, all keyboard/mouse events are grabbed */ |
8a7ddc38 | 41 | static int last_vm_running; |
f9977897 JK |
42 | static bool gui_saved_scaling; |
43 | static int gui_saved_width; | |
44 | static int gui_saved_height; | |
8e9c4afe FB |
45 | static int gui_saved_grab; |
46 | static int gui_fullscreen; | |
43523e93 | 47 | static int gui_noframe; |
8e9c4afe FB |
48 | static int gui_key_modifier_pressed; |
49 | static int gui_keysym; | |
32ff25bf FB |
50 | static int gui_grab_code = KMOD_LALT | KMOD_LCTRL; |
51 | static uint8_t modifiers_state[256]; | |
09b26c5e FB |
52 | static SDL_Cursor *sdl_cursor_normal; |
53 | static SDL_Cursor *sdl_cursor_hidden; | |
54 | static int absolute_enabled = 0; | |
d34cab9f TS |
55 | static int guest_cursor = 0; |
56 | static int guest_x, guest_y; | |
660f11be | 57 | static SDL_Cursor *guest_sprite = NULL; |
c18a2c36 SS |
58 | static SDL_PixelFormat host_format; |
59 | static int scaling_active = 0; | |
3af12c86 | 60 | static Notifier mouse_mode_notifier; |
0f0b7264 FB |
61 | |
62 | static void sdl_update(DisplayState *ds, int x, int y, int w, int h) | |
63 | { | |
898712a8 | 64 | // printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h); |
c18a2c36 SS |
65 | SDL_Rect rec; |
66 | rec.x = x; | |
67 | rec.y = y; | |
68 | rec.w = w; | |
69 | rec.h = h; | |
70 | ||
7b5d76da | 71 | if (guest_screen) { |
c18a2c36 SS |
72 | if (!scaling_active) { |
73 | SDL_BlitSurface(guest_screen, &rec, real_screen, &rec); | |
74 | } else { | |
75 | if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) { | |
76 | fprintf(stderr, "Zoom blit failed\n"); | |
77 | exit(1); | |
78 | } | |
79 | } | |
80 | } | |
81 | SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h); | |
7d957bd8 AL |
82 | } |
83 | ||
84 | static void sdl_setdata(DisplayState *ds) | |
85 | { | |
7d957bd8 AL |
86 | if (guest_screen != NULL) SDL_FreeSurface(guest_screen); |
87 | ||
88 | guest_screen = SDL_CreateRGBSurfaceFrom(ds_get_data(ds), ds_get_width(ds), ds_get_height(ds), | |
89 | ds_get_bits_per_pixel(ds), ds_get_linesize(ds), | |
90 | ds->surface->pf.rmask, ds->surface->pf.gmask, | |
91 | ds->surface->pf.bmask, ds->surface->pf.amask); | |
0f0b7264 FB |
92 | } |
93 | ||
9510a486 | 94 | static void do_sdl_resize(int width, int height, int bpp) |
0f0b7264 FB |
95 | { |
96 | int flags; | |
97 | ||
98 | // printf("resizing to %d %d\n", w, h); | |
99 | ||
91ada980 JK |
100 | flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL; |
101 | if (gui_fullscreen) { | |
8e9c4afe | 102 | flags |= SDL_FULLSCREEN; |
91ada980 JK |
103 | } else { |
104 | flags |= SDL_RESIZABLE; | |
105 | } | |
43523e93 TS |
106 | if (gui_noframe) |
107 | flags |= SDL_NOFRAME; | |
9903da21 | 108 | |
7b5d76da | 109 | real_screen = SDL_SetVideoMode(width, height, bpp, flags); |
7d957bd8 | 110 | if (!real_screen) { |
b6034a39 BM |
111 | fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n", width, |
112 | height, bpp, SDL_GetError()); | |
0f0b7264 FB |
113 | exit(1); |
114 | } | |
7b5d76da AL |
115 | } |
116 | ||
117 | static void sdl_resize(DisplayState *ds) | |
118 | { | |
187cd1d9 GH |
119 | if (!scaling_active) { |
120 | do_sdl_resize(ds_get_width(ds), ds_get_height(ds), 0); | |
121 | } else if (real_screen->format->BitsPerPixel != ds_get_bits_per_pixel(ds)) { | |
122 | do_sdl_resize(real_screen->w, real_screen->h, | |
123 | ds_get_bits_per_pixel(ds)); | |
c18a2c36 | 124 | } |
187cd1d9 | 125 | sdl_setdata(ds); |
0f0b7264 FB |
126 | } |
127 | ||
3d11d0eb | 128 | /* generic keyboard conversion */ |
e58d12ed | 129 | |
3d11d0eb | 130 | #include "sdl_keysym.h" |
3d11d0eb | 131 | |
c227f099 | 132 | static kbd_layout_t *kbd_layout = NULL; |
3d11d0eb FB |
133 | |
134 | static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev) | |
e58d12ed | 135 | { |
3d11d0eb FB |
136 | int keysym; |
137 | /* workaround for X11+SDL bug with AltGR */ | |
138 | keysym = ev->keysym.sym; | |
139 | if (keysym == 0 && ev->keysym.scancode == 113) | |
140 | keysym = SDLK_MODE; | |
60659e3b FB |
141 | /* For Japanese key '\' and '|' */ |
142 | if (keysym == 92 && ev->keysym.scancode == 133) { | |
143 | keysym = 0xa5; | |
144 | } | |
44bb61c8 | 145 | return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK; |
e58d12ed FB |
146 | } |
147 | ||
3d11d0eb FB |
148 | /* specific keyboard conversions from scan codes */ |
149 | ||
150 | #if defined(_WIN32) | |
e58d12ed FB |
151 | |
152 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) | |
153 | { | |
154 | return ev->keysym.scancode; | |
155 | } | |
156 | ||
157 | #else | |
158 | ||
5368a422 AL |
159 | #if defined(SDL_VIDEO_DRIVER_X11) |
160 | #include <X11/XKBlib.h> | |
161 | ||
162 | static int check_for_evdev(void) | |
163 | { | |
164 | SDL_SysWMinfo info; | |
229609dd | 165 | XkbDescPtr desc = NULL; |
5368a422 | 166 | int has_evdev = 0; |
229609dd | 167 | char *keycodes = NULL; |
5368a422 AL |
168 | |
169 | SDL_VERSION(&info.version); | |
229609dd | 170 | if (!SDL_GetWMInfo(&info)) { |
5368a422 | 171 | return 0; |
229609dd | 172 | } |
5368a422 AL |
173 | desc = XkbGetKeyboard(info.info.x11.display, |
174 | XkbGBN_AllComponentsMask, | |
175 | XkbUseCoreKbd); | |
229609dd JK |
176 | if (desc && desc->names) { |
177 | keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes); | |
178 | if (keycodes == NULL) { | |
179 | fprintf(stderr, "could not lookup keycode name\n"); | |
180 | } else if (strstart(keycodes, "evdev", NULL)) { | |
181 | has_evdev = 1; | |
182 | } else if (!strstart(keycodes, "xfree86", NULL)) { | |
183 | fprintf(stderr, "unknown keycodes `%s', please report to " | |
184 | "[email protected]\n", keycodes); | |
185 | } | |
186 | } | |
5368a422 | 187 | |
229609dd JK |
188 | if (desc) { |
189 | XkbFreeKeyboard(desc, XkbGBN_AllComponentsMask, True); | |
190 | } | |
191 | if (keycodes) { | |
192 | XFree(keycodes); | |
193 | } | |
5368a422 AL |
194 | return has_evdev; |
195 | } | |
196 | #else | |
197 | static int check_for_evdev(void) | |
198 | { | |
199 | return 0; | |
200 | } | |
201 | #endif | |
202 | ||
e58d12ed FB |
203 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
204 | { | |
205 | int keycode; | |
5368a422 AL |
206 | static int has_evdev = -1; |
207 | ||
208 | if (has_evdev == -1) | |
209 | has_evdev = check_for_evdev(); | |
e58d12ed FB |
210 | |
211 | keycode = ev->keysym.scancode; | |
212 | ||
213 | if (keycode < 9) { | |
214 | keycode = 0; | |
215 | } else if (keycode < 97) { | |
216 | keycode -= 8; /* just an offset */ | |
5368a422 | 217 | } else if (keycode < 158) { |
e58d12ed | 218 | /* use conversion table */ |
5368a422 AL |
219 | if (has_evdev) |
220 | keycode = translate_evdev_keycode(keycode - 97); | |
221 | else | |
222 | keycode = translate_xfree86_keycode(keycode - 97); | |
223 | } else if (keycode == 208) { /* Hiragana_Katakana */ | |
224 | keycode = 0x70; | |
225 | } else if (keycode == 211) { /* backslash */ | |
226 | keycode = 0x73; | |
e58d12ed FB |
227 | } else { |
228 | keycode = 0; | |
229 | } | |
230 | return keycode; | |
231 | } | |
232 | ||
233 | #endif | |
234 | ||
32ff25bf FB |
235 | static void reset_keys(void) |
236 | { | |
237 | int i; | |
238 | for(i = 0; i < 256; i++) { | |
239 | if (modifiers_state[i]) { | |
44bb61c8 ST |
240 | if (i & SCANCODE_GREY) |
241 | kbd_put_keycode(SCANCODE_EMUL0); | |
242 | kbd_put_keycode(i | SCANCODE_UP); | |
32ff25bf FB |
243 | modifiers_state[i] = 0; |
244 | } | |
245 | } | |
246 | } | |
247 | ||
0f0b7264 FB |
248 | static void sdl_process_key(SDL_KeyboardEvent *ev) |
249 | { | |
32ff25bf | 250 | int keycode, v; |
de2200d3 FB |
251 | |
252 | if (ev->keysym.sym == SDLK_PAUSE) { | |
253 | /* specific case */ | |
254 | v = 0; | |
255 | if (ev->type == SDL_KEYUP) | |
44bb61c8 | 256 | v |= SCANCODE_UP; |
de2200d3 FB |
257 | kbd_put_keycode(0xe1); |
258 | kbd_put_keycode(0x1d | v); | |
259 | kbd_put_keycode(0x45 | v); | |
260 | return; | |
261 | } | |
262 | ||
3d11d0eb FB |
263 | if (kbd_layout) { |
264 | keycode = sdl_keyevent_to_keycode_generic(ev); | |
265 | } else { | |
266 | keycode = sdl_keyevent_to_keycode(ev); | |
267 | } | |
de2200d3 FB |
268 | |
269 | switch(keycode) { | |
270 | case 0x00: | |
271 | /* sent when leaving window: reset the modifiers state */ | |
32ff25bf | 272 | reset_keys(); |
de2200d3 FB |
273 | return; |
274 | case 0x2a: /* Left Shift */ | |
275 | case 0x36: /* Right Shift */ | |
276 | case 0x1d: /* Left CTRL */ | |
277 | case 0x9d: /* Right CTRL */ | |
278 | case 0x38: /* Left ALT */ | |
279 | case 0xb8: /* Right ALT */ | |
0f0b7264 | 280 | if (ev->type == SDL_KEYUP) |
de2200d3 FB |
281 | modifiers_state[keycode] = 0; |
282 | else | |
283 | modifiers_state[keycode] = 1; | |
284 | break; | |
4e79bcbb SW |
285 | #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION) |
286 | #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14 | |
287 | /* SDL versions before 1.2.14 don't support key up for caps/num lock. */ | |
de2200d3 FB |
288 | case 0x45: /* num lock */ |
289 | case 0x3a: /* caps lock */ | |
290 | /* SDL does not send the key up event, so we generate it */ | |
291 | kbd_put_keycode(keycode); | |
44bb61c8 | 292 | kbd_put_keycode(keycode | SCANCODE_UP); |
de2200d3 | 293 | return; |
4e79bcbb | 294 | #endif |
0f0b7264 | 295 | } |
de2200d3 FB |
296 | |
297 | /* now send the key code */ | |
44bb61c8 ST |
298 | if (keycode & SCANCODE_GREY) |
299 | kbd_put_keycode(SCANCODE_EMUL0); | |
de2200d3 | 300 | if (ev->type == SDL_KEYUP) |
44bb61c8 | 301 | kbd_put_keycode(keycode | SCANCODE_UP); |
de2200d3 | 302 | else |
44bb61c8 | 303 | kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK); |
0f0b7264 FB |
304 | } |
305 | ||
8a7ddc38 FB |
306 | static void sdl_update_caption(void) |
307 | { | |
b4ed5d18 DE |
308 | char win_title[1024]; |
309 | char icon_title[1024]; | |
c35734b2 TS |
310 | const char *status = ""; |
311 | ||
1354869c | 312 | if (!runstate_is_running()) |
c35734b2 | 313 | status = " [Stopped]"; |
3780e197 | 314 | else if (gui_grab) { |
0ca9f8a4 | 315 | if (alt_grab) |
4e75b342 | 316 | status = " - Press Ctrl-Alt-Shift to exit mouse grab"; |
0ca9f8a4 | 317 | else if (ctrl_grab) |
4e75b342 | 318 | status = " - Press Right-Ctrl to exit mouse grab"; |
0ca9f8a4 | 319 | else |
4e75b342 | 320 | status = " - Press Ctrl-Alt to exit mouse grab"; |
3780e197 | 321 | } |
c35734b2 | 322 | |
b4ed5d18 DE |
323 | if (qemu_name) { |
324 | snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status); | |
325 | snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name); | |
326 | } else { | |
327 | snprintf(win_title, sizeof(win_title), "QEMU%s", status); | |
328 | snprintf(icon_title, sizeof(icon_title), "QEMU"); | |
329 | } | |
c35734b2 | 330 | |
b4ed5d18 | 331 | SDL_WM_SetCaption(win_title, icon_title); |
8a7ddc38 FB |
332 | } |
333 | ||
09b26c5e FB |
334 | static void sdl_hide_cursor(void) |
335 | { | |
9467cd46 AZ |
336 | if (!cursor_hide) |
337 | return; | |
338 | ||
8785a8dd FB |
339 | if (kbd_mouse_is_absolute()) { |
340 | SDL_ShowCursor(1); | |
341 | SDL_SetCursor(sdl_cursor_hidden); | |
342 | } else { | |
343 | SDL_ShowCursor(0); | |
344 | } | |
09b26c5e FB |
345 | } |
346 | ||
347 | static void sdl_show_cursor(void) | |
348 | { | |
9467cd46 AZ |
349 | if (!cursor_hide) |
350 | return; | |
351 | ||
74d9dc69 | 352 | if (!kbd_mouse_is_absolute() || !is_graphic_console()) { |
8785a8dd | 353 | SDL_ShowCursor(1); |
d34cab9f TS |
354 | if (guest_cursor && |
355 | (gui_grab || kbd_mouse_is_absolute() || absolute_enabled)) | |
356 | SDL_SetCursor(guest_sprite); | |
357 | else | |
358 | SDL_SetCursor(sdl_cursor_normal); | |
09b26c5e FB |
359 | } |
360 | } | |
361 | ||
0f0b7264 FB |
362 | static void sdl_grab_start(void) |
363 | { | |
85f94f86 JK |
364 | /* |
365 | * If the application is not active, do not try to enter grab state. This | |
366 | * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the | |
367 | * application (SDL bug). | |
368 | */ | |
369 | if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) { | |
370 | return; | |
371 | } | |
d34cab9f TS |
372 | if (guest_cursor) { |
373 | SDL_SetCursor(guest_sprite); | |
08a2d4c4 AZ |
374 | if (!kbd_mouse_is_absolute() && !absolute_enabled) |
375 | SDL_WarpMouse(guest_x, guest_y); | |
d34cab9f TS |
376 | } else |
377 | sdl_hide_cursor(); | |
eaa2e027 JK |
378 | SDL_WM_GrabInput(SDL_GRAB_ON); |
379 | gui_grab = 1; | |
380 | sdl_update_caption(); | |
0f0b7264 FB |
381 | } |
382 | ||
383 | static void sdl_grab_end(void) | |
384 | { | |
0f0b7264 | 385 | SDL_WM_GrabInput(SDL_GRAB_OFF); |
0f0b7264 | 386 | gui_grab = 0; |
d34cab9f | 387 | sdl_show_cursor(); |
8a7ddc38 | 388 | sdl_update_caption(); |
0f0b7264 FB |
389 | } |
390 | ||
66596356 JK |
391 | static void absolute_mouse_grab(void) |
392 | { | |
393 | int mouse_x, mouse_y; | |
394 | ||
85f94f86 JK |
395 | SDL_GetMouseState(&mouse_x, &mouse_y); |
396 | if (mouse_x > 0 && mouse_x < real_screen->w - 1 && | |
397 | mouse_y > 0 && mouse_y < real_screen->h - 1) { | |
398 | sdl_grab_start(); | |
66596356 JK |
399 | } |
400 | } | |
401 | ||
9e8dd451 | 402 | static void sdl_mouse_mode_change(Notifier *notify, void *data) |
3af12c86 AL |
403 | { |
404 | if (kbd_mouse_is_absolute()) { | |
405 | if (!absolute_enabled) { | |
3af12c86 | 406 | absolute_enabled = 1; |
66596356 JK |
407 | if (is_graphic_console()) { |
408 | absolute_mouse_grab(); | |
409 | } | |
3af12c86 AL |
410 | } |
411 | } else if (absolute_enabled) { | |
f8b8d633 JK |
412 | if (!gui_fullscreen) { |
413 | sdl_grab_end(); | |
414 | } | |
35b0f237 | 415 | absolute_enabled = 0; |
3af12c86 AL |
416 | } |
417 | } | |
418 | ||
4c44bdcb | 419 | static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state) |
0f0b7264 | 420 | { |
9510a486 JK |
421 | int buttons = 0; |
422 | ||
423 | if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) { | |
0f0b7264 | 424 | buttons |= MOUSE_EVENT_LBUTTON; |
9510a486 JK |
425 | } |
426 | if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) { | |
0f0b7264 | 427 | buttons |= MOUSE_EVENT_RBUTTON; |
9510a486 JK |
428 | } |
429 | if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) { | |
0f0b7264 | 430 | buttons |= MOUSE_EVENT_MBUTTON; |
9510a486 | 431 | } |
09b26c5e FB |
432 | |
433 | if (kbd_mouse_is_absolute()) { | |
9510a486 JK |
434 | dx = x * 0x7FFF / (real_screen->w - 1); |
435 | dy = y * 0x7FFF / (real_screen->h - 1); | |
d34cab9f | 436 | } else if (guest_cursor) { |
4c44bdcb AJ |
437 | x -= guest_x; |
438 | y -= guest_y; | |
439 | guest_x += x; | |
440 | guest_y += y; | |
441 | dx = x; | |
442 | dy = y; | |
09b26c5e FB |
443 | } |
444 | ||
0f0b7264 FB |
445 | kbd_mouse_event(dx, dy, dz, buttons); |
446 | } | |
447 | ||
f9977897 JK |
448 | static void sdl_scale(DisplayState *ds, int width, int height) |
449 | { | |
450 | int bpp = real_screen->format->BitsPerPixel; | |
451 | ||
452 | if (bpp != 16 && bpp != 32) { | |
453 | bpp = 32; | |
454 | } | |
455 | do_sdl_resize(width, height, bpp); | |
456 | scaling_active = 1; | |
457 | if (!is_buffer_shared(ds->surface)) { | |
458 | ds->surface = qemu_resize_displaysurface(ds, ds_get_width(ds), | |
459 | ds_get_height(ds)); | |
a93a4a22 | 460 | dpy_gfx_resize(ds); |
f9977897 JK |
461 | } |
462 | } | |
463 | ||
8e9c4afe FB |
464 | static void toggle_full_screen(DisplayState *ds) |
465 | { | |
466 | gui_fullscreen = !gui_fullscreen; | |
8e9c4afe | 467 | if (gui_fullscreen) { |
f9977897 JK |
468 | gui_saved_width = real_screen->w; |
469 | gui_saved_height = real_screen->h; | |
470 | gui_saved_scaling = scaling_active; | |
471 | ||
472 | do_sdl_resize(ds_get_width(ds), ds_get_height(ds), | |
473 | ds_get_bits_per_pixel(ds)); | |
c18a2c36 | 474 | scaling_active = 0; |
f9977897 | 475 | |
8e9c4afe FB |
476 | gui_saved_grab = gui_grab; |
477 | sdl_grab_start(); | |
478 | } else { | |
f9977897 JK |
479 | if (gui_saved_scaling) { |
480 | sdl_scale(ds, gui_saved_width, gui_saved_height); | |
481 | } else { | |
482 | do_sdl_resize(ds_get_width(ds), ds_get_height(ds), 0); | |
483 | } | |
f8558100 | 484 | if (!gui_saved_grab || !is_graphic_console()) { |
8e9c4afe | 485 | sdl_grab_end(); |
f8558100 | 486 | } |
8e9c4afe | 487 | } |
95219897 PB |
488 | vga_hw_invalidate(); |
489 | vga_hw_update(); | |
8e9c4afe FB |
490 | } |
491 | ||
1ae1caf1 | 492 | static void handle_keydown(DisplayState *ds, SDL_Event *ev) |
0f0b7264 | 493 | { |
8e9c4afe | 494 | int mod_state; |
1ae1caf1 JK |
495 | int keycode; |
496 | ||
497 | if (alt_grab) { | |
498 | mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) == | |
499 | (gui_grab_code | KMOD_LSHIFT); | |
500 | } else if (ctrl_grab) { | |
501 | mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL; | |
502 | } else { | |
503 | mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code; | |
504 | } | |
505 | gui_key_modifier_pressed = mod_state; | |
506 | ||
507 | if (gui_key_modifier_pressed) { | |
508 | keycode = sdl_keyevent_to_keycode(&ev->key); | |
509 | switch (keycode) { | |
510 | case 0x21: /* 'f' key on US keyboard */ | |
511 | toggle_full_screen(ds); | |
512 | gui_keysym = 1; | |
513 | break; | |
514 | case 0x16: /* 'u' key on US keyboard */ | |
515 | if (scaling_active) { | |
516 | scaling_active = 0; | |
517 | sdl_resize(ds); | |
518 | vga_hw_invalidate(); | |
519 | vga_hw_update(); | |
520 | } | |
521 | gui_keysym = 1; | |
522 | break; | |
523 | case 0x02 ... 0x0a: /* '1' to '9' keys */ | |
524 | /* Reset the modifiers sent to the current console */ | |
525 | reset_keys(); | |
526 | console_select(keycode - 0x02); | |
527 | gui_keysym = 1; | |
528 | if (gui_fullscreen) { | |
529 | break; | |
530 | } | |
531 | if (!is_graphic_console()) { | |
532 | /* release grab if going to a text console */ | |
533 | if (gui_grab) { | |
534 | sdl_grab_end(); | |
535 | } else if (absolute_enabled) { | |
536 | sdl_show_cursor(); | |
537 | } | |
538 | } else if (absolute_enabled) { | |
539 | sdl_hide_cursor(); | |
540 | absolute_mouse_grab(); | |
541 | } | |
542 | break; | |
543 | case 0x1b: /* '+' */ | |
544 | case 0x35: /* '-' */ | |
545 | if (!gui_fullscreen) { | |
546 | int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50), | |
547 | 160); | |
548 | int height = (ds_get_height(ds) * width) / ds_get_width(ds); | |
549 | ||
550 | sdl_scale(ds, width, height); | |
551 | vga_hw_invalidate(); | |
552 | vga_hw_update(); | |
553 | gui_keysym = 1; | |
554 | } | |
555 | default: | |
556 | break; | |
557 | } | |
558 | } else if (!is_graphic_console()) { | |
559 | int keysym = 0; | |
560 | ||
561 | if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) { | |
562 | switch (ev->key.keysym.sym) { | |
563 | case SDLK_UP: | |
564 | keysym = QEMU_KEY_CTRL_UP; | |
565 | break; | |
566 | case SDLK_DOWN: | |
567 | keysym = QEMU_KEY_CTRL_DOWN; | |
568 | break; | |
569 | case SDLK_LEFT: | |
570 | keysym = QEMU_KEY_CTRL_LEFT; | |
571 | break; | |
572 | case SDLK_RIGHT: | |
573 | keysym = QEMU_KEY_CTRL_RIGHT; | |
574 | break; | |
575 | case SDLK_HOME: | |
576 | keysym = QEMU_KEY_CTRL_HOME; | |
577 | break; | |
578 | case SDLK_END: | |
579 | keysym = QEMU_KEY_CTRL_END; | |
580 | break; | |
581 | case SDLK_PAGEUP: | |
582 | keysym = QEMU_KEY_CTRL_PAGEUP; | |
583 | break; | |
584 | case SDLK_PAGEDOWN: | |
585 | keysym = QEMU_KEY_CTRL_PAGEDOWN; | |
586 | break; | |
587 | default: | |
588 | break; | |
589 | } | |
590 | } else { | |
591 | switch (ev->key.keysym.sym) { | |
592 | case SDLK_UP: | |
593 | keysym = QEMU_KEY_UP; | |
594 | break; | |
595 | case SDLK_DOWN: | |
596 | keysym = QEMU_KEY_DOWN; | |
597 | break; | |
598 | case SDLK_LEFT: | |
599 | keysym = QEMU_KEY_LEFT; | |
600 | break; | |
601 | case SDLK_RIGHT: | |
602 | keysym = QEMU_KEY_RIGHT; | |
603 | break; | |
604 | case SDLK_HOME: | |
605 | keysym = QEMU_KEY_HOME; | |
606 | break; | |
607 | case SDLK_END: | |
608 | keysym = QEMU_KEY_END; | |
609 | break; | |
610 | case SDLK_PAGEUP: | |
611 | keysym = QEMU_KEY_PAGEUP; | |
612 | break; | |
613 | case SDLK_PAGEDOWN: | |
614 | keysym = QEMU_KEY_PAGEDOWN; | |
615 | break; | |
616 | case SDLK_BACKSPACE: | |
617 | keysym = QEMU_KEY_BACKSPACE; | |
618 | break; | |
619 | case SDLK_DELETE: | |
620 | keysym = QEMU_KEY_DELETE; | |
621 | break; | |
622 | default: | |
623 | break; | |
624 | } | |
625 | } | |
626 | if (keysym) { | |
627 | kbd_put_keysym(keysym); | |
628 | } else if (ev->key.keysym.unicode != 0) { | |
629 | kbd_put_keysym(ev->key.keysym.unicode); | |
630 | } | |
631 | } | |
632 | if (is_graphic_console() && !gui_keysym) { | |
633 | sdl_process_key(&ev->key); | |
634 | } | |
635 | } | |
636 | ||
637 | static void handle_keyup(DisplayState *ds, SDL_Event *ev) | |
638 | { | |
639 | int mod_state; | |
640 | ||
641 | if (!alt_grab) { | |
642 | mod_state = (ev->key.keysym.mod & gui_grab_code); | |
643 | } else { | |
644 | mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT)); | |
645 | } | |
646 | if (!mod_state && gui_key_modifier_pressed) { | |
647 | gui_key_modifier_pressed = 0; | |
648 | if (gui_keysym == 0) { | |
649 | /* exit/enter grab if pressing Ctrl-Alt */ | |
650 | if (!gui_grab) { | |
85f94f86 | 651 | if (is_graphic_console()) { |
1ae1caf1 JK |
652 | sdl_grab_start(); |
653 | } | |
654 | } else if (!gui_fullscreen) { | |
655 | sdl_grab_end(); | |
656 | } | |
657 | /* SDL does not send back all the modifiers key, so we must | |
658 | * correct it. */ | |
659 | reset_keys(); | |
660 | return; | |
661 | } | |
662 | gui_keysym = 0; | |
663 | } | |
664 | if (is_graphic_console() && !gui_keysym) { | |
665 | sdl_process_key(&ev->key); | |
666 | } | |
667 | } | |
668 | ||
669 | static void handle_mousemotion(DisplayState *ds, SDL_Event *ev) | |
670 | { | |
671 | int max_x, max_y; | |
672 | ||
673 | if (is_graphic_console() && | |
674 | (kbd_mouse_is_absolute() || absolute_enabled)) { | |
675 | max_x = real_screen->w - 1; | |
676 | max_y = real_screen->h - 1; | |
677 | if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 || | |
678 | ev->motion.x == max_x || ev->motion.y == max_y)) { | |
679 | sdl_grab_end(); | |
680 | } | |
85f94f86 | 681 | if (!gui_grab && |
1ae1caf1 JK |
682 | (ev->motion.x > 0 && ev->motion.x < max_x && |
683 | ev->motion.y > 0 && ev->motion.y < max_y)) { | |
684 | sdl_grab_start(); | |
685 | } | |
686 | } | |
687 | if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) { | |
688 | sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0, | |
689 | ev->motion.x, ev->motion.y, ev->motion.state); | |
690 | } | |
691 | } | |
692 | ||
693 | static void handle_mousebutton(DisplayState *ds, SDL_Event *ev) | |
694 | { | |
4c44bdcb | 695 | int buttonstate = SDL_GetMouseState(NULL, NULL); |
1ae1caf1 JK |
696 | SDL_MouseButtonEvent *bev; |
697 | int dz; | |
698 | ||
699 | if (!is_graphic_console()) { | |
700 | return; | |
701 | } | |
702 | ||
703 | bev = &ev->button; | |
704 | if (!gui_grab && !kbd_mouse_is_absolute()) { | |
822f98d2 | 705 | if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) { |
1ae1caf1 JK |
706 | /* start grabbing all events */ |
707 | sdl_grab_start(); | |
708 | } | |
709 | } else { | |
710 | dz = 0; | |
711 | if (ev->type == SDL_MOUSEBUTTONDOWN) { | |
712 | buttonstate |= SDL_BUTTON(bev->button); | |
713 | } else { | |
714 | buttonstate &= ~SDL_BUTTON(bev->button); | |
715 | } | |
716 | #ifdef SDL_BUTTON_WHEELUP | |
717 | if (bev->button == SDL_BUTTON_WHEELUP && | |
718 | ev->type == SDL_MOUSEBUTTONDOWN) { | |
719 | dz = -1; | |
720 | } else if (bev->button == SDL_BUTTON_WHEELDOWN && | |
721 | ev->type == SDL_MOUSEBUTTONDOWN) { | |
722 | dz = 1; | |
723 | } | |
724 | #endif | |
725 | sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate); | |
726 | } | |
727 | } | |
728 | ||
729 | static void handle_activation(DisplayState *ds, SDL_Event *ev) | |
730 | { | |
02df4d6f JK |
731 | #ifdef _WIN32 |
732 | /* Disable grab if the window no longer has the focus | |
733 | * (Windows-only workaround) */ | |
1ae1caf1 JK |
734 | if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS && |
735 | !ev->active.gain && !gui_fullscreen) { | |
736 | sdl_grab_end(); | |
737 | } | |
02df4d6f | 738 | #endif |
1ae1caf1 JK |
739 | if (!gui_grab && ev->active.gain && is_graphic_console() && |
740 | (kbd_mouse_is_absolute() || absolute_enabled)) { | |
741 | absolute_mouse_grab(); | |
742 | } | |
743 | if (ev->active.state & SDL_APPACTIVE) { | |
744 | if (ev->active.gain) { | |
745 | /* Back to default interval */ | |
746 | dcl->gui_timer_interval = 0; | |
747 | dcl->idle = 0; | |
748 | } else { | |
749 | /* Sleeping interval */ | |
750 | dcl->gui_timer_interval = 500; | |
751 | dcl->idle = 1; | |
752 | } | |
753 | } | |
754 | } | |
755 | ||
756 | static void sdl_refresh(DisplayState *ds) | |
757 | { | |
758 | SDL_Event ev1, *ev = &ev1; | |
3b46e624 | 759 | |
1354869c LC |
760 | if (last_vm_running != runstate_is_running()) { |
761 | last_vm_running = runstate_is_running(); | |
8a7ddc38 FB |
762 | sdl_update_caption(); |
763 | } | |
764 | ||
95219897 | 765 | vga_hw_update(); |
3bee8bd0 | 766 | SDL_EnableUNICODE(!is_graphic_console()); |
457831f4 | 767 | |
0f0b7264 FB |
768 | while (SDL_PollEvent(ev)) { |
769 | switch (ev->type) { | |
770 | case SDL_VIDEOEXPOSE: | |
7d957bd8 | 771 | sdl_update(ds, 0, 0, real_screen->w, real_screen->h); |
0f0b7264 FB |
772 | break; |
773 | case SDL_KEYDOWN: | |
1ae1caf1 JK |
774 | handle_keydown(ds, ev); |
775 | break; | |
0f0b7264 | 776 | case SDL_KEYUP: |
1ae1caf1 | 777 | handle_keyup(ds, ev); |
0f0b7264 FB |
778 | break; |
779 | case SDL_QUIT: | |
941f511a JK |
780 | if (!no_quit) { |
781 | no_shutdown = 0; | |
731345e1 | 782 | qemu_system_shutdown_request(); |
941f511a | 783 | } |
0f0b7264 FB |
784 | break; |
785 | case SDL_MOUSEMOTION: | |
1ae1caf1 | 786 | handle_mousemotion(ds, ev); |
0f0b7264 FB |
787 | break; |
788 | case SDL_MOUSEBUTTONDOWN: | |
789 | case SDL_MOUSEBUTTONUP: | |
1ae1caf1 | 790 | handle_mousebutton(ds, ev); |
0f0b7264 | 791 | break; |
0294ffb9 | 792 | case SDL_ACTIVEEVENT: |
1ae1caf1 | 793 | handle_activation(ds, ev); |
0294ffb9 | 794 | break; |
f9977897 JK |
795 | case SDL_VIDEORESIZE: |
796 | sdl_scale(ds, ev->resize.w, ev->resize.h); | |
c18a2c36 SS |
797 | vga_hw_invalidate(); |
798 | vga_hw_update(); | |
799 | break; | |
0f0b7264 FB |
800 | default: |
801 | break; | |
802 | } | |
803 | } | |
804 | } | |
805 | ||
bf2fde70 | 806 | static void sdl_mouse_warp(DisplayState *ds, int x, int y, int on) |
d34cab9f TS |
807 | { |
808 | if (on) { | |
809 | if (!guest_cursor) | |
810 | sdl_show_cursor(); | |
811 | if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) { | |
812 | SDL_SetCursor(guest_sprite); | |
08a2d4c4 AZ |
813 | if (!kbd_mouse_is_absolute() && !absolute_enabled) |
814 | SDL_WarpMouse(x, y); | |
d34cab9f TS |
815 | } |
816 | } else if (gui_grab) | |
817 | sdl_hide_cursor(); | |
818 | guest_cursor = on; | |
819 | guest_x = x, guest_y = y; | |
820 | } | |
821 | ||
bf2fde70 | 822 | static void sdl_mouse_define(DisplayState *ds, QEMUCursor *c) |
d34cab9f | 823 | { |
fbe6d7a4 GH |
824 | uint8_t *image, *mask; |
825 | int bpl; | |
826 | ||
d34cab9f TS |
827 | if (guest_sprite) |
828 | SDL_FreeCursor(guest_sprite); | |
829 | ||
fbe6d7a4 | 830 | bpl = cursor_get_mono_bpl(c); |
7267c094 AL |
831 | image = g_malloc0(bpl * c->height); |
832 | mask = g_malloc0(bpl * c->height); | |
fbe6d7a4 GH |
833 | cursor_get_mono_image(c, 0x000000, image); |
834 | cursor_get_mono_mask(c, 0, mask); | |
835 | guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height, | |
836 | c->hot_x, c->hot_y); | |
7267c094 AL |
837 | g_free(image); |
838 | g_free(mask); | |
d34cab9f TS |
839 | |
840 | if (guest_cursor && | |
841 | (gui_grab || kbd_mouse_is_absolute() || absolute_enabled)) | |
842 | SDL_SetCursor(guest_sprite); | |
843 | } | |
844 | ||
28695489 | 845 | static void sdl_cleanup(void) |
898712a8 | 846 | { |
d34cab9f TS |
847 | if (guest_sprite) |
848 | SDL_FreeCursor(guest_sprite); | |
d8ee7665 | 849 | SDL_QuitSubSystem(SDL_INIT_VIDEO); |
898712a8 FB |
850 | } |
851 | ||
43523e93 | 852 | void sdl_display_init(DisplayState *ds, int full_screen, int no_frame) |
0f0b7264 FB |
853 | { |
854 | int flags; | |
09b26c5e | 855 | uint8_t data = 0; |
7b5d76da | 856 | const SDL_VideoInfo *vi; |
09cec717 | 857 | char *filename; |
0f0b7264 | 858 | |
3d11d0eb FB |
859 | #if defined(__APPLE__) |
860 | /* always use generic keymaps */ | |
861 | if (!keyboard_layout) | |
862 | keyboard_layout = "en-us"; | |
863 | #endif | |
864 | if(keyboard_layout) { | |
0483755a | 865 | kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); |
3d11d0eb FB |
866 | if (!kbd_layout) |
867 | exit(1); | |
868 | } | |
869 | ||
43523e93 TS |
870 | if (no_frame) |
871 | gui_noframe = 1; | |
872 | ||
111f8ec9 JK |
873 | if (!full_screen) { |
874 | setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0); | |
875 | } | |
1de9756b MT |
876 | #ifdef __linux__ |
877 | /* on Linux, SDL may use fbcon|directfb|svgalib when run without | |
878 | * accessible $DISPLAY to open X11 window. This is often the case | |
879 | * when qemu is run using sudo. But in this case, and when actually | |
880 | * run in X11 environment, SDL fights with X11 for the video card, | |
881 | * making current display unavailable, often until reboot. | |
882 | * So make x11 the default SDL video driver if this variable is unset. | |
883 | * This is a bit hackish but saves us from bigger problem. | |
884 | * Maybe it's a good idea to fix this in SDL instead. | |
885 | */ | |
886 | setenv("SDL_VIDEODRIVER", "x11", 0); | |
887 | #endif | |
111f8ec9 | 888 | |
4e79bcbb SW |
889 | /* Enable normal up/down events for Caps-Lock and Num-Lock keys. |
890 | * This requires SDL >= 1.2.14. */ | |
891 | setenv("SDL_DISABLE_LOCK_KEYS", "1", 1); | |
892 | ||
0f0b7264 FB |
893 | flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE; |
894 | if (SDL_Init (flags)) { | |
3caf2562 | 895 | fprintf(stderr, "Could not initialize SDL(%s) - exiting\n", |
896 | SDL_GetError()); | |
0f0b7264 FB |
897 | exit(1); |
898 | } | |
7b5d76da | 899 | vi = SDL_GetVideoInfo(); |
c18a2c36 | 900 | host_format = *(vi->vfmt); |
0ae04d73 | 901 | |
09cec717 SW |
902 | /* Load a 32x32x4 image. White pixels are transparent. */ |
903 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp"); | |
904 | if (filename) { | |
905 | SDL_Surface *image = SDL_LoadBMP(filename); | |
906 | if (image) { | |
907 | uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255); | |
908 | SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey); | |
909 | SDL_WM_SetIcon(image, NULL); | |
910 | } | |
7267c094 | 911 | g_free(filename); |
09cec717 SW |
912 | } |
913 | ||
110defd7 JK |
914 | if (full_screen) { |
915 | gui_fullscreen = 1; | |
916 | sdl_grab_start(); | |
917 | } | |
918 | ||
7267c094 | 919 | dcl = g_malloc0(sizeof(DisplayChangeListener)); |
a93a4a22 GH |
920 | dcl->dpy_gfx_update = sdl_update; |
921 | dcl->dpy_gfx_resize = sdl_resize; | |
7d957bd8 | 922 | dcl->dpy_refresh = sdl_refresh; |
a93a4a22 | 923 | dcl->dpy_gfx_setdata = sdl_setdata; |
bf2fde70 GH |
924 | dcl->dpy_mouse_set = sdl_mouse_warp; |
925 | dcl->dpy_cursor_define = sdl_mouse_define; | |
7d957bd8 | 926 | register_displaychangelistener(ds, dcl); |
0f0b7264 | 927 | |
3af12c86 AL |
928 | mouse_mode_notifier.notify = sdl_mouse_mode_change; |
929 | qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier); | |
930 | ||
8a7ddc38 | 931 | sdl_update_caption(); |
0f0b7264 FB |
932 | SDL_EnableKeyRepeat(250, 50); |
933 | gui_grab = 0; | |
898712a8 | 934 | |
09b26c5e FB |
935 | sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0); |
936 | sdl_cursor_normal = SDL_GetCursor(); | |
937 | ||
28695489 | 938 | atexit(sdl_cleanup); |
0f0b7264 | 939 | } |