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