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