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 | |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 32 | #if defined(__APPLE__) |
| 33 | #define CONFIG_SDL_GENERIC_KBD |
| 34 | #endif |
| 35 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 36 | static SDL_Surface *screen; |
| 37 | static int gui_grab; /* if true, all keyboard/mouse events are grabbed */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 38 | static int last_vm_running; |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 39 | static int gui_saved_grab; |
| 40 | static int gui_fullscreen; |
| 41 | static int gui_key_modifier_pressed; |
| 42 | static int gui_keysym; |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 43 | |
| 44 | static void sdl_update(DisplayState *ds, int x, int y, int w, int h) |
| 45 | { |
bellard | 898712a | 2004-02-06 19:56:42 +0000 | [diff] [blame] | 46 | // 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] | 47 | SDL_UpdateRect(screen, x, y, w, h); |
| 48 | } |
| 49 | |
| 50 | static void sdl_resize(DisplayState *ds, int w, int h) |
| 51 | { |
| 52 | int flags; |
| 53 | |
| 54 | // printf("resizing to %d %d\n", w, h); |
| 55 | |
| 56 | flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL; |
| 57 | flags |= SDL_RESIZABLE; |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 58 | if (gui_fullscreen) |
| 59 | flags |= SDL_FULLSCREEN; |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 60 | screen = SDL_SetVideoMode(w, h, 0, flags); |
| 61 | if (!screen) { |
| 62 | fprintf(stderr, "Could not open SDL display\n"); |
| 63 | exit(1); |
| 64 | } |
| 65 | ds->data = screen->pixels; |
| 66 | ds->linesize = screen->pitch; |
| 67 | ds->depth = screen->format->BitsPerPixel; |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 68 | ds->width = w; |
| 69 | ds->height = h; |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 70 | } |
| 71 | |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 72 | #ifdef CONFIG_SDL_GENERIC_KBD |
| 73 | |
| 74 | /* XXX: use keymap tables defined in the VNC patch because the |
| 75 | following code suppose you have a US keyboard. */ |
| 76 | |
| 77 | static const uint8_t scancodes[SDLK_LAST] = { |
| 78 | [SDLK_ESCAPE] = 0x01, |
| 79 | [SDLK_1] = 0x02, |
| 80 | [SDLK_2] = 0x03, |
| 81 | [SDLK_3] = 0x04, |
| 82 | [SDLK_4] = 0x05, |
| 83 | [SDLK_5] = 0x06, |
| 84 | [SDLK_6] = 0x07, |
| 85 | [SDLK_7] = 0x08, |
| 86 | [SDLK_8] = 0x09, |
| 87 | [SDLK_9] = 0x0a, |
| 88 | [SDLK_0] = 0x0b, |
| 89 | [SDLK_MINUS] = 0x0c, |
| 90 | [SDLK_EQUALS] = 0x0d, |
| 91 | [SDLK_BACKSPACE] = 0x0e, |
| 92 | [SDLK_TAB] = 0x0f, |
| 93 | [SDLK_q] = 0x10, |
| 94 | [SDLK_w] = 0x11, |
| 95 | [SDLK_e] = 0x12, |
| 96 | [SDLK_r] = 0x13, |
| 97 | [SDLK_t] = 0x14, |
| 98 | [SDLK_y] = 0x15, |
| 99 | [SDLK_u] = 0x16, |
| 100 | [SDLK_i] = 0x17, |
| 101 | [SDLK_o] = 0x18, |
| 102 | [SDLK_p] = 0x19, |
| 103 | [SDLK_LEFTBRACKET] = 0x1a, |
| 104 | [SDLK_RIGHTBRACKET] = 0x1b, |
| 105 | [SDLK_RETURN] = 0x1c, |
| 106 | [SDLK_LCTRL] = 0x1d, |
| 107 | [SDLK_a] = 0x1e, |
| 108 | [SDLK_s] = 0x1f, |
| 109 | [SDLK_d] = 0x20, |
| 110 | [SDLK_f] = 0x21, |
| 111 | [SDLK_g] = 0x22, |
| 112 | [SDLK_h] = 0x23, |
| 113 | [SDLK_j] = 0x24, |
| 114 | [SDLK_k] = 0x25, |
| 115 | [SDLK_l] = 0x26, |
| 116 | [SDLK_SEMICOLON] = 0x27, |
| 117 | [SDLK_QUOTE] = 0x28, |
| 118 | [SDLK_BACKQUOTE] = 0x29, |
| 119 | [SDLK_LSHIFT] = 0x2a, |
| 120 | [SDLK_BACKSLASH] = 0x2b, |
| 121 | [SDLK_z] = 0x2c, |
| 122 | [SDLK_x] = 0x2d, |
| 123 | [SDLK_c] = 0x2e, |
| 124 | [SDLK_v] = 0x2f, |
| 125 | [SDLK_b] = 0x30, |
| 126 | [SDLK_n] = 0x31, |
| 127 | [SDLK_m] = 0x32, |
| 128 | [SDLK_COMMA] = 0x33, |
| 129 | [SDLK_PERIOD] = 0x34, |
| 130 | [SDLK_SLASH] = 0x35, |
| 131 | [SDLK_KP_MULTIPLY] = 0x37, |
| 132 | [SDLK_LALT] = 0x38, |
| 133 | [SDLK_SPACE] = 0x39, |
| 134 | [SDLK_CAPSLOCK] = 0x3a, |
| 135 | [SDLK_F1] = 0x3b, |
| 136 | [SDLK_F2] = 0x3c, |
| 137 | [SDLK_F3] = 0x3d, |
| 138 | [SDLK_F4] = 0x3e, |
| 139 | [SDLK_F5] = 0x3f, |
| 140 | [SDLK_F6] = 0x40, |
| 141 | [SDLK_F7] = 0x41, |
| 142 | [SDLK_F8] = 0x42, |
| 143 | [SDLK_F9] = 0x43, |
| 144 | [SDLK_F10] = 0x44, |
| 145 | [SDLK_NUMLOCK] = 0x45, |
| 146 | [SDLK_SCROLLOCK] = 0x46, |
| 147 | [SDLK_KP7] = 0x47, |
| 148 | [SDLK_KP8] = 0x48, |
| 149 | [SDLK_KP9] = 0x49, |
| 150 | [SDLK_KP_MINUS] = 0x4a, |
| 151 | [SDLK_KP4] = 0x4b, |
| 152 | [SDLK_KP5] = 0x4c, |
| 153 | [SDLK_KP6] = 0x4d, |
| 154 | [SDLK_KP_PLUS] = 0x4e, |
| 155 | [SDLK_KP1] = 0x4f, |
| 156 | [SDLK_KP2] = 0x50, |
| 157 | [SDLK_KP3] = 0x51, |
| 158 | [SDLK_KP0] = 0x52, |
| 159 | [SDLK_KP_PERIOD] = 0x53, |
| 160 | [SDLK_PRINT] = 0x54, |
| 161 | [SDLK_LMETA] = 0x56, |
| 162 | |
| 163 | [SDLK_KP_ENTER] = 0x9c, |
| 164 | [SDLK_KP_DIVIDE] = 0xb5, |
| 165 | |
| 166 | [SDLK_UP] = 0xc8, |
| 167 | [SDLK_DOWN] = 0xd0, |
| 168 | [SDLK_RIGHT] = 0xcd, |
| 169 | [SDLK_LEFT] = 0xcb, |
| 170 | [SDLK_INSERT] = 0xd2, |
| 171 | [SDLK_HOME] = 0xc7, |
| 172 | [SDLK_END] = 0xcf, |
| 173 | [SDLK_PAGEUP] = 0xc9, |
| 174 | [SDLK_PAGEDOWN] = 0xd1, |
| 175 | [SDLK_DELETE] = 0xd3, |
| 176 | }; |
| 177 | |
| 178 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
| 179 | { |
| 180 | return scancodes[ev->keysym.sym]; |
| 181 | } |
| 182 | |
| 183 | #elif defined(_WIN32) |
| 184 | |
| 185 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
| 186 | { |
| 187 | return ev->keysym.scancode; |
| 188 | } |
| 189 | |
| 190 | #else |
| 191 | |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 192 | static const uint8_t x_keycode_to_pc_keycode[61] = { |
| 193 | 0xc7, /* 97 Home */ |
| 194 | 0xc8, /* 98 Up */ |
| 195 | 0xc9, /* 99 PgUp */ |
| 196 | 0xcb, /* 100 Left */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 197 | 0x4c, /* 101 KP-5 */ |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 198 | 0xcd, /* 102 Right */ |
| 199 | 0xcf, /* 103 End */ |
| 200 | 0xd0, /* 104 Down */ |
| 201 | 0xd1, /* 105 PgDn */ |
| 202 | 0xd2, /* 106 Ins */ |
| 203 | 0xd3, /* 107 Del */ |
| 204 | 0x9c, /* 108 Enter */ |
| 205 | 0x9d, /* 109 Ctrl-R */ |
bellard | 22a56b8 | 2004-06-05 08:32:36 +0000 | [diff] [blame] | 206 | 0x0, /* 110 Pause */ |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 207 | 0xb7, /* 111 Print */ |
| 208 | 0xb5, /* 112 Divide */ |
| 209 | 0xb8, /* 113 Alt-R */ |
| 210 | 0xc6, /* 114 Break */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 211 | 0x0, /* 115 */ |
| 212 | 0x0, /* 116 */ |
| 213 | 0x0, /* 117 */ |
| 214 | 0x0, /* 118 */ |
| 215 | 0x0, /* 119 */ |
bellard | b71e95f | 2004-05-20 13:08:06 +0000 | [diff] [blame] | 216 | 0x70, /* 120 Hiragana_Katakana */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 217 | 0x0, /* 121 */ |
| 218 | 0x0, /* 122 */ |
bellard | b71e95f | 2004-05-20 13:08:06 +0000 | [diff] [blame] | 219 | 0x73, /* 123 backslash */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 220 | 0x0, /* 124 */ |
| 221 | 0x0, /* 125 */ |
| 222 | 0x0, /* 126 */ |
| 223 | 0x0, /* 127 */ |
| 224 | 0x0, /* 128 */ |
bellard | b71e95f | 2004-05-20 13:08:06 +0000 | [diff] [blame] | 225 | 0x79, /* 129 Henkan */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 226 | 0x0, /* 130 */ |
bellard | b71e95f | 2004-05-20 13:08:06 +0000 | [diff] [blame] | 227 | 0x7b, /* 131 Muhenkan */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 228 | 0x0, /* 132 */ |
bellard | b71e95f | 2004-05-20 13:08:06 +0000 | [diff] [blame] | 229 | 0x7d, /* 133 Yen */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 230 | 0x0, /* 134 */ |
| 231 | 0x0, /* 135 */ |
| 232 | 0x47, /* 136 KP_7 */ |
| 233 | 0x48, /* 137 KP_8 */ |
| 234 | 0x49, /* 138 KP_9 */ |
| 235 | 0x4b, /* 139 KP_4 */ |
| 236 | 0x4c, /* 140 KP_5 */ |
| 237 | 0x4d, /* 141 KP_6 */ |
| 238 | 0x4f, /* 142 KP_1 */ |
| 239 | 0x50, /* 143 KP_2 */ |
| 240 | 0x51, /* 144 KP_3 */ |
| 241 | 0x52, /* 145 KP_0 */ |
| 242 | 0x53, /* 146 KP_. */ |
| 243 | 0x47, /* 147 KP_HOME */ |
| 244 | 0x48, /* 148 KP_UP */ |
| 245 | 0x49, /* 149 KP_PgUp */ |
| 246 | 0x4b, /* 150 KP_Left */ |
| 247 | 0x4c, /* 151 KP_ */ |
| 248 | 0x4d, /* 152 KP_Right */ |
| 249 | 0x4f, /* 153 KP_End */ |
| 250 | 0x50, /* 154 KP_Down */ |
| 251 | 0x51, /* 155 KP_PgDn */ |
| 252 | 0x52, /* 156 KP_Ins */ |
| 253 | 0x53, /* 157 KP_Del */ |
| 254 | }; |
| 255 | |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 256 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
| 257 | { |
| 258 | int keycode; |
| 259 | |
| 260 | keycode = ev->keysym.scancode; |
| 261 | |
| 262 | if (keycode < 9) { |
| 263 | keycode = 0; |
| 264 | } else if (keycode < 97) { |
| 265 | keycode -= 8; /* just an offset */ |
| 266 | } else if (keycode < 158) { |
| 267 | /* use conversion table */ |
| 268 | keycode = x_keycode_to_pc_keycode[keycode - 97]; |
| 269 | } else { |
| 270 | keycode = 0; |
| 271 | } |
| 272 | return keycode; |
| 273 | } |
| 274 | |
| 275 | #endif |
| 276 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 277 | static void sdl_process_key(SDL_KeyboardEvent *ev) |
| 278 | { |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 279 | int keycode, v, i; |
| 280 | static uint8_t modifiers_state[256]; |
| 281 | |
| 282 | if (ev->keysym.sym == SDLK_PAUSE) { |
| 283 | /* specific case */ |
| 284 | v = 0; |
| 285 | if (ev->type == SDL_KEYUP) |
| 286 | v |= 0x80; |
| 287 | kbd_put_keycode(0xe1); |
| 288 | kbd_put_keycode(0x1d | v); |
| 289 | kbd_put_keycode(0x45 | v); |
| 290 | return; |
| 291 | } |
| 292 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 293 | /* XXX: not portable, but avoids complicated mappings */ |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 294 | keycode = sdl_keyevent_to_keycode(ev); |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 295 | |
| 296 | switch(keycode) { |
| 297 | case 0x00: |
| 298 | /* sent when leaving window: reset the modifiers state */ |
| 299 | for(i = 0; i < 256; i++) { |
| 300 | if (modifiers_state[i]) { |
| 301 | if (i & 0x80) |
| 302 | kbd_put_keycode(0xe0); |
| 303 | kbd_put_keycode(i | 0x80); |
| 304 | } |
| 305 | } |
| 306 | return; |
| 307 | case 0x2a: /* Left Shift */ |
| 308 | case 0x36: /* Right Shift */ |
| 309 | case 0x1d: /* Left CTRL */ |
| 310 | case 0x9d: /* Right CTRL */ |
| 311 | case 0x38: /* Left ALT */ |
| 312 | case 0xb8: /* Right ALT */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 313 | if (ev->type == SDL_KEYUP) |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 314 | modifiers_state[keycode] = 0; |
| 315 | else |
| 316 | modifiers_state[keycode] = 1; |
| 317 | break; |
| 318 | case 0x45: /* num lock */ |
| 319 | case 0x3a: /* caps lock */ |
| 320 | /* SDL does not send the key up event, so we generate it */ |
| 321 | kbd_put_keycode(keycode); |
| 322 | kbd_put_keycode(keycode | 0x80); |
| 323 | return; |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 324 | } |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 325 | |
| 326 | /* now send the key code */ |
| 327 | if (keycode & 0x80) |
| 328 | kbd_put_keycode(0xe0); |
| 329 | if (ev->type == SDL_KEYUP) |
| 330 | kbd_put_keycode(keycode | 0x80); |
| 331 | else |
| 332 | kbd_put_keycode(keycode & 0x7f); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 333 | } |
| 334 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 335 | static void sdl_update_caption(void) |
| 336 | { |
| 337 | char buf[1024]; |
| 338 | strcpy(buf, "QEMU"); |
| 339 | if (!vm_running) { |
| 340 | strcat(buf, " [Stopped]"); |
| 341 | } |
| 342 | if (gui_grab) { |
| 343 | strcat(buf, " - Press Ctrl-Shift to exit grab"); |
| 344 | } |
| 345 | SDL_WM_SetCaption(buf, "QEMU"); |
| 346 | } |
| 347 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 348 | static void sdl_grab_start(void) |
| 349 | { |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 350 | SDL_ShowCursor(0); |
| 351 | SDL_WM_GrabInput(SDL_GRAB_ON); |
| 352 | /* dummy read to avoid moving the mouse */ |
| 353 | SDL_GetRelativeMouseState(NULL, NULL); |
| 354 | gui_grab = 1; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 355 | sdl_update_caption(); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static void sdl_grab_end(void) |
| 359 | { |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 360 | SDL_WM_GrabInput(SDL_GRAB_OFF); |
| 361 | SDL_ShowCursor(1); |
| 362 | gui_grab = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 363 | sdl_update_caption(); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static void sdl_send_mouse_event(void) |
| 367 | { |
| 368 | int dx, dy, dz, state, buttons; |
| 369 | state = SDL_GetRelativeMouseState(&dx, &dy); |
| 370 | buttons = 0; |
| 371 | if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) |
| 372 | buttons |= MOUSE_EVENT_LBUTTON; |
| 373 | if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) |
| 374 | buttons |= MOUSE_EVENT_RBUTTON; |
| 375 | if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) |
| 376 | buttons |= MOUSE_EVENT_MBUTTON; |
| 377 | /* XXX: test wheel */ |
| 378 | dz = 0; |
bellard | 8351d2d | 2004-01-04 18:15:01 +0000 | [diff] [blame] | 379 | #ifdef SDL_BUTTON_WHEELUP |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 380 | if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP)) |
| 381 | dz--; |
| 382 | if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN)) |
| 383 | dz++; |
bellard | 8351d2d | 2004-01-04 18:15:01 +0000 | [diff] [blame] | 384 | #endif |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 385 | kbd_mouse_event(dx, dy, dz, buttons); |
| 386 | } |
| 387 | |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 388 | static void toggle_full_screen(DisplayState *ds) |
| 389 | { |
| 390 | gui_fullscreen = !gui_fullscreen; |
| 391 | sdl_resize(ds, screen->w, screen->h); |
| 392 | if (gui_fullscreen) { |
| 393 | gui_saved_grab = gui_grab; |
| 394 | sdl_grab_start(); |
| 395 | } else { |
| 396 | if (!gui_saved_grab) |
| 397 | sdl_grab_end(); |
| 398 | } |
bellard | ee38b4c | 2004-06-08 00:56:42 +0000 | [diff] [blame] | 399 | vga_invalidate_display(); |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 400 | vga_update_display(); |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 401 | } |
| 402 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 403 | static void sdl_refresh(DisplayState *ds) |
| 404 | { |
| 405 | SDL_Event ev1, *ev = &ev1; |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 406 | int mod_state; |
| 407 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 408 | if (last_vm_running != vm_running) { |
| 409 | last_vm_running = vm_running; |
| 410 | sdl_update_caption(); |
| 411 | } |
| 412 | |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 413 | if (is_active_console(vga_console)) |
| 414 | vga_update_display(); |
| 415 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 416 | while (SDL_PollEvent(ev)) { |
| 417 | switch (ev->type) { |
| 418 | case SDL_VIDEOEXPOSE: |
| 419 | sdl_update(ds, 0, 0, screen->w, screen->h); |
| 420 | break; |
| 421 | case SDL_KEYDOWN: |
| 422 | case SDL_KEYUP: |
| 423 | if (ev->type == SDL_KEYDOWN) { |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 424 | mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) == |
| 425 | (KMOD_LSHIFT | KMOD_LCTRL); |
| 426 | gui_key_modifier_pressed = mod_state; |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 427 | if (gui_key_modifier_pressed) { |
| 428 | switch(ev->key.keysym.sym) { |
| 429 | case SDLK_f: |
| 430 | toggle_full_screen(ds); |
| 431 | gui_keysym = 1; |
| 432 | break; |
| 433 | case SDLK_F1 ... SDLK_F12: |
| 434 | console_select(ev->key.keysym.sym - SDLK_F1); |
| 435 | if (is_active_console(vga_console)) { |
| 436 | /* tell the vga console to redisplay itself */ |
| 437 | vga_invalidate_display(); |
| 438 | } else { |
| 439 | /* display grab if going to a text console */ |
| 440 | if (gui_grab) |
| 441 | sdl_grab_end(); |
| 442 | } |
| 443 | gui_keysym = 1; |
| 444 | break; |
| 445 | default: |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | if (!is_active_console(vga_console)) { |
| 450 | int keysym; |
| 451 | keysym = 0; |
| 452 | if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) { |
| 453 | switch(ev->key.keysym.sym) { |
| 454 | case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break; |
| 455 | case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break; |
| 456 | case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break; |
| 457 | case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break; |
| 458 | case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break; |
| 459 | case SDLK_END: keysym = QEMU_KEY_CTRL_END; break; |
| 460 | case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break; |
| 461 | case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break; |
| 462 | default: break; |
| 463 | } |
| 464 | } else { |
| 465 | switch(ev->key.keysym.sym) { |
| 466 | case SDLK_UP: keysym = QEMU_KEY_UP; break; |
| 467 | case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break; |
| 468 | case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break; |
| 469 | case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break; |
| 470 | case SDLK_HOME: keysym = QEMU_KEY_HOME; break; |
| 471 | case SDLK_END: keysym = QEMU_KEY_END; break; |
| 472 | case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break; |
| 473 | case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break; |
| 474 | case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break; |
| 475 | default: break; |
| 476 | } |
| 477 | } |
| 478 | if (keysym) { |
| 479 | kbd_put_keysym(keysym); |
| 480 | } else if (ev->key.keysym.unicode != 0) { |
| 481 | kbd_put_keysym(ev->key.keysym.unicode); |
| 482 | } |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 483 | } |
| 484 | } else if (ev->type == SDL_KEYUP) { |
| 485 | mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)); |
| 486 | if (!mod_state) { |
| 487 | if (gui_key_modifier_pressed) { |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 488 | if (gui_keysym == 0) { |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 489 | /* exit/enter grab if pressing Ctrl-Shift */ |
| 490 | if (!gui_grab) |
| 491 | sdl_grab_start(); |
| 492 | else |
| 493 | sdl_grab_end(); |
| 494 | break; |
| 495 | } |
| 496 | gui_key_modifier_pressed = 0; |
| 497 | gui_keysym = 0; |
| 498 | } |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 501 | if (is_active_console(vga_console)) |
| 502 | sdl_process_key(&ev->key); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 503 | break; |
| 504 | case SDL_QUIT: |
bellard | 979a54f | 2004-06-20 12:36:04 +0000 | [diff] [blame] | 505 | qemu_system_shutdown_request(); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 506 | break; |
| 507 | case SDL_MOUSEMOTION: |
| 508 | if (gui_grab) { |
| 509 | sdl_send_mouse_event(); |
| 510 | } |
| 511 | break; |
| 512 | case SDL_MOUSEBUTTONDOWN: |
| 513 | case SDL_MOUSEBUTTONUP: |
| 514 | { |
| 515 | SDL_MouseButtonEvent *bev = &ev->button; |
| 516 | if (!gui_grab) { |
| 517 | if (ev->type == SDL_MOUSEBUTTONDOWN && |
| 518 | (bev->state & SDL_BUTTON_LMASK)) { |
| 519 | /* start grabbing all events */ |
| 520 | sdl_grab_start(); |
| 521 | } |
| 522 | } else { |
| 523 | sdl_send_mouse_event(); |
| 524 | } |
| 525 | } |
| 526 | break; |
bellard | 0294ffb | 2004-04-29 22:15:15 +0000 | [diff] [blame] | 527 | case SDL_ACTIVEEVENT: |
| 528 | if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0) { |
| 529 | sdl_grab_end(); |
| 530 | } |
| 531 | break; |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 532 | default: |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
bellard | 898712a | 2004-02-06 19:56:42 +0000 | [diff] [blame] | 538 | static void sdl_cleanup(void) |
| 539 | { |
| 540 | SDL_Quit(); |
| 541 | } |
| 542 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 543 | void sdl_display_init(DisplayState *ds) |
| 544 | { |
| 545 | int flags; |
| 546 | |
| 547 | flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE; |
| 548 | if (SDL_Init (flags)) { |
| 549 | fprintf(stderr, "Could not initialize SDL - exiting\n"); |
| 550 | exit(1); |
| 551 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 552 | #ifndef _WIN32 |
bellard | 0ae04d7 | 2003-09-30 21:09:16 +0000 | [diff] [blame] | 553 | /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */ |
| 554 | signal(SIGINT, SIG_DFL); |
| 555 | signal(SIGQUIT, SIG_DFL); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 556 | #endif |
bellard | 0ae04d7 | 2003-09-30 21:09:16 +0000 | [diff] [blame] | 557 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 558 | ds->dpy_update = sdl_update; |
| 559 | ds->dpy_resize = sdl_resize; |
| 560 | ds->dpy_refresh = sdl_refresh; |
| 561 | |
| 562 | sdl_resize(ds, 640, 400); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 563 | sdl_update_caption(); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 564 | SDL_EnableKeyRepeat(250, 50); |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 565 | SDL_EnableUNICODE(1); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 566 | gui_grab = 0; |
bellard | 898712a | 2004-02-06 19:56:42 +0000 | [diff] [blame] | 567 | |
| 568 | atexit(sdl_cleanup); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 569 | } |