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; |
| 37 | static int gui_key_modifier_pressed; |
| 38 | static int gui_keysym; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 39 | static int gui_fullscreen_initial_grab; |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 40 | static int gui_grab_code = KMOD_LALT | KMOD_LCTRL; |
| 41 | static uint8_t modifiers_state[256]; |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 42 | |
| 43 | static void sdl_update(DisplayState *ds, int x, int y, int w, int h) |
| 44 | { |
bellard | 898712a | 2004-02-06 19:56:42 +0000 | [diff] [blame] | 45 | // 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] | 46 | SDL_UpdateRect(screen, x, y, w, h); |
| 47 | } |
| 48 | |
| 49 | static void sdl_resize(DisplayState *ds, int w, int h) |
| 50 | { |
| 51 | int flags; |
| 52 | |
| 53 | // printf("resizing to %d %d\n", w, h); |
| 54 | |
| 55 | flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL; |
| 56 | flags |= SDL_RESIZABLE; |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 57 | if (gui_fullscreen) |
| 58 | flags |= SDL_FULLSCREEN; |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 59 | screen = SDL_SetVideoMode(w, h, 0, flags); |
| 60 | if (!screen) { |
| 61 | fprintf(stderr, "Could not open SDL display\n"); |
| 62 | exit(1); |
| 63 | } |
| 64 | ds->data = screen->pixels; |
| 65 | ds->linesize = screen->pitch; |
| 66 | ds->depth = screen->format->BitsPerPixel; |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 67 | ds->width = w; |
| 68 | ds->height = h; |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 69 | } |
| 70 | |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 71 | /* generic keyboard conversion */ |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 72 | |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 73 | #include "sdl_keysym.h" |
| 74 | #include "keymaps.c" |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 75 | |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 76 | static kbd_layout_t *kbd_layout = NULL; |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 77 | |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 78 | static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev) |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 79 | { |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 80 | int keysym; |
| 81 | /* workaround for X11+SDL bug with AltGR */ |
| 82 | keysym = ev->keysym.sym; |
| 83 | if (keysym == 0 && ev->keysym.scancode == 113) |
| 84 | keysym = SDLK_MODE; |
| 85 | return keysym2scancode(kbd_layout, keysym); |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 86 | } |
| 87 | |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 88 | /* specific keyboard conversions from scan codes */ |
| 89 | |
| 90 | #if defined(_WIN32) |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 91 | |
| 92 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
| 93 | { |
| 94 | return ev->keysym.scancode; |
| 95 | } |
| 96 | |
| 97 | #else |
| 98 | |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 99 | static const uint8_t x_keycode_to_pc_keycode[61] = { |
| 100 | 0xc7, /* 97 Home */ |
| 101 | 0xc8, /* 98 Up */ |
| 102 | 0xc9, /* 99 PgUp */ |
| 103 | 0xcb, /* 100 Left */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 104 | 0x4c, /* 101 KP-5 */ |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 105 | 0xcd, /* 102 Right */ |
| 106 | 0xcf, /* 103 End */ |
| 107 | 0xd0, /* 104 Down */ |
| 108 | 0xd1, /* 105 PgDn */ |
| 109 | 0xd2, /* 106 Ins */ |
| 110 | 0xd3, /* 107 Del */ |
| 111 | 0x9c, /* 108 Enter */ |
| 112 | 0x9d, /* 109 Ctrl-R */ |
bellard | 22a56b8 | 2004-06-05 08:32:36 +0000 | [diff] [blame] | 113 | 0x0, /* 110 Pause */ |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 114 | 0xb7, /* 111 Print */ |
| 115 | 0xb5, /* 112 Divide */ |
| 116 | 0xb8, /* 113 Alt-R */ |
| 117 | 0xc6, /* 114 Break */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 118 | 0x0, /* 115 */ |
| 119 | 0x0, /* 116 */ |
| 120 | 0x0, /* 117 */ |
| 121 | 0x0, /* 118 */ |
| 122 | 0x0, /* 119 */ |
bellard | b71e95f | 2004-05-20 13:08:06 +0000 | [diff] [blame] | 123 | 0x70, /* 120 Hiragana_Katakana */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 124 | 0x0, /* 121 */ |
| 125 | 0x0, /* 122 */ |
bellard | b71e95f | 2004-05-20 13:08:06 +0000 | [diff] [blame] | 126 | 0x73, /* 123 backslash */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 127 | 0x0, /* 124 */ |
| 128 | 0x0, /* 125 */ |
| 129 | 0x0, /* 126 */ |
| 130 | 0x0, /* 127 */ |
| 131 | 0x0, /* 128 */ |
bellard | b71e95f | 2004-05-20 13:08:06 +0000 | [diff] [blame] | 132 | 0x79, /* 129 Henkan */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 133 | 0x0, /* 130 */ |
bellard | b71e95f | 2004-05-20 13:08:06 +0000 | [diff] [blame] | 134 | 0x7b, /* 131 Muhenkan */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 135 | 0x0, /* 132 */ |
bellard | b71e95f | 2004-05-20 13:08:06 +0000 | [diff] [blame] | 136 | 0x7d, /* 133 Yen */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 137 | 0x0, /* 134 */ |
| 138 | 0x0, /* 135 */ |
| 139 | 0x47, /* 136 KP_7 */ |
| 140 | 0x48, /* 137 KP_8 */ |
| 141 | 0x49, /* 138 KP_9 */ |
| 142 | 0x4b, /* 139 KP_4 */ |
| 143 | 0x4c, /* 140 KP_5 */ |
| 144 | 0x4d, /* 141 KP_6 */ |
| 145 | 0x4f, /* 142 KP_1 */ |
| 146 | 0x50, /* 143 KP_2 */ |
| 147 | 0x51, /* 144 KP_3 */ |
| 148 | 0x52, /* 145 KP_0 */ |
| 149 | 0x53, /* 146 KP_. */ |
| 150 | 0x47, /* 147 KP_HOME */ |
| 151 | 0x48, /* 148 KP_UP */ |
| 152 | 0x49, /* 149 KP_PgUp */ |
| 153 | 0x4b, /* 150 KP_Left */ |
| 154 | 0x4c, /* 151 KP_ */ |
| 155 | 0x4d, /* 152 KP_Right */ |
| 156 | 0x4f, /* 153 KP_End */ |
| 157 | 0x50, /* 154 KP_Down */ |
| 158 | 0x51, /* 155 KP_PgDn */ |
| 159 | 0x52, /* 156 KP_Ins */ |
| 160 | 0x53, /* 157 KP_Del */ |
| 161 | }; |
| 162 | |
bellard | e58d12e | 2004-07-05 22:13:07 +0000 | [diff] [blame] | 163 | static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev) |
| 164 | { |
| 165 | int keycode; |
| 166 | |
| 167 | keycode = ev->keysym.scancode; |
| 168 | |
| 169 | if (keycode < 9) { |
| 170 | keycode = 0; |
| 171 | } else if (keycode < 97) { |
| 172 | keycode -= 8; /* just an offset */ |
| 173 | } else if (keycode < 158) { |
| 174 | /* use conversion table */ |
| 175 | keycode = x_keycode_to_pc_keycode[keycode - 97]; |
| 176 | } else { |
| 177 | keycode = 0; |
| 178 | } |
| 179 | return keycode; |
| 180 | } |
| 181 | |
| 182 | #endif |
| 183 | |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 184 | static void reset_keys(void) |
| 185 | { |
| 186 | int i; |
| 187 | for(i = 0; i < 256; i++) { |
| 188 | if (modifiers_state[i]) { |
| 189 | if (i & 0x80) |
| 190 | kbd_put_keycode(0xe0); |
| 191 | kbd_put_keycode(i | 0x80); |
| 192 | modifiers_state[i] = 0; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 197 | static void sdl_process_key(SDL_KeyboardEvent *ev) |
| 198 | { |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 199 | int keycode, v; |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 200 | |
| 201 | if (ev->keysym.sym == SDLK_PAUSE) { |
| 202 | /* specific case */ |
| 203 | v = 0; |
| 204 | if (ev->type == SDL_KEYUP) |
| 205 | v |= 0x80; |
| 206 | kbd_put_keycode(0xe1); |
| 207 | kbd_put_keycode(0x1d | v); |
| 208 | kbd_put_keycode(0x45 | v); |
| 209 | return; |
| 210 | } |
| 211 | |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 212 | if (kbd_layout) { |
| 213 | keycode = sdl_keyevent_to_keycode_generic(ev); |
| 214 | } else { |
| 215 | keycode = sdl_keyevent_to_keycode(ev); |
| 216 | } |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 217 | |
| 218 | switch(keycode) { |
| 219 | case 0x00: |
| 220 | /* sent when leaving window: reset the modifiers state */ |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 221 | reset_keys(); |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 222 | return; |
| 223 | case 0x2a: /* Left Shift */ |
| 224 | case 0x36: /* Right Shift */ |
| 225 | case 0x1d: /* Left CTRL */ |
| 226 | case 0x9d: /* Right CTRL */ |
| 227 | case 0x38: /* Left ALT */ |
| 228 | case 0xb8: /* Right ALT */ |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 229 | if (ev->type == SDL_KEYUP) |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 230 | modifiers_state[keycode] = 0; |
| 231 | else |
| 232 | modifiers_state[keycode] = 1; |
| 233 | break; |
| 234 | case 0x45: /* num lock */ |
| 235 | case 0x3a: /* caps lock */ |
| 236 | /* SDL does not send the key up event, so we generate it */ |
| 237 | kbd_put_keycode(keycode); |
| 238 | kbd_put_keycode(keycode | 0x80); |
| 239 | return; |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 240 | } |
bellard | de2200d | 2004-06-04 13:15:06 +0000 | [diff] [blame] | 241 | |
| 242 | /* now send the key code */ |
| 243 | if (keycode & 0x80) |
| 244 | kbd_put_keycode(0xe0); |
| 245 | if (ev->type == SDL_KEYUP) |
| 246 | kbd_put_keycode(keycode | 0x80); |
| 247 | else |
| 248 | kbd_put_keycode(keycode & 0x7f); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 249 | } |
| 250 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 251 | static void sdl_update_caption(void) |
| 252 | { |
| 253 | char buf[1024]; |
| 254 | strcpy(buf, "QEMU"); |
| 255 | if (!vm_running) { |
| 256 | strcat(buf, " [Stopped]"); |
| 257 | } |
| 258 | if (gui_grab) { |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 259 | strcat(buf, " - Press Ctrl-Alt to exit grab"); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 260 | } |
| 261 | SDL_WM_SetCaption(buf, "QEMU"); |
| 262 | } |
| 263 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 264 | static void sdl_grab_start(void) |
| 265 | { |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 266 | SDL_ShowCursor(0); |
| 267 | SDL_WM_GrabInput(SDL_GRAB_ON); |
| 268 | /* dummy read to avoid moving the mouse */ |
| 269 | SDL_GetRelativeMouseState(NULL, NULL); |
| 270 | gui_grab = 1; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 271 | sdl_update_caption(); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static void sdl_grab_end(void) |
| 275 | { |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 276 | SDL_WM_GrabInput(SDL_GRAB_OFF); |
| 277 | SDL_ShowCursor(1); |
| 278 | gui_grab = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 279 | sdl_update_caption(); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static void sdl_send_mouse_event(void) |
| 283 | { |
| 284 | int dx, dy, dz, state, buttons; |
| 285 | state = SDL_GetRelativeMouseState(&dx, &dy); |
| 286 | buttons = 0; |
| 287 | if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) |
| 288 | buttons |= MOUSE_EVENT_LBUTTON; |
| 289 | if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) |
| 290 | buttons |= MOUSE_EVENT_RBUTTON; |
| 291 | if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) |
| 292 | buttons |= MOUSE_EVENT_MBUTTON; |
| 293 | /* XXX: test wheel */ |
| 294 | dz = 0; |
bellard | 8351d2d | 2004-01-04 18:15:01 +0000 | [diff] [blame] | 295 | #ifdef SDL_BUTTON_WHEELUP |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 296 | if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP)) |
| 297 | dz--; |
| 298 | if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN)) |
| 299 | dz++; |
bellard | 8351d2d | 2004-01-04 18:15:01 +0000 | [diff] [blame] | 300 | #endif |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 301 | kbd_mouse_event(dx, dy, dz, buttons); |
| 302 | } |
| 303 | |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 304 | static void toggle_full_screen(DisplayState *ds) |
| 305 | { |
| 306 | gui_fullscreen = !gui_fullscreen; |
| 307 | sdl_resize(ds, screen->w, screen->h); |
| 308 | if (gui_fullscreen) { |
| 309 | gui_saved_grab = gui_grab; |
| 310 | sdl_grab_start(); |
| 311 | } else { |
| 312 | if (!gui_saved_grab) |
| 313 | sdl_grab_end(); |
| 314 | } |
bellard | ee38b4c | 2004-06-08 00:56:42 +0000 | [diff] [blame] | 315 | vga_invalidate_display(); |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 316 | vga_update_display(); |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 317 | } |
| 318 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 319 | static void sdl_refresh(DisplayState *ds) |
| 320 | { |
| 321 | SDL_Event ev1, *ev = &ev1; |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 322 | int mod_state; |
| 323 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 324 | if (last_vm_running != vm_running) { |
| 325 | last_vm_running = vm_running; |
| 326 | sdl_update_caption(); |
| 327 | } |
| 328 | |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 329 | if (is_active_console(vga_console)) |
| 330 | vga_update_display(); |
| 331 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 332 | while (SDL_PollEvent(ev)) { |
| 333 | switch (ev->type) { |
| 334 | case SDL_VIDEOEXPOSE: |
| 335 | sdl_update(ds, 0, 0, screen->w, screen->h); |
| 336 | break; |
| 337 | case SDL_KEYDOWN: |
| 338 | case SDL_KEYUP: |
| 339 | if (ev->type == SDL_KEYDOWN) { |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 340 | mod_state = (SDL_GetModState() & gui_grab_code) == |
| 341 | gui_grab_code; |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 342 | gui_key_modifier_pressed = mod_state; |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 343 | if (gui_key_modifier_pressed) { |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 344 | int keycode; |
| 345 | keycode = sdl_keyevent_to_keycode(&ev->key); |
| 346 | switch(keycode) { |
| 347 | case 0x21: /* 'f' key on US keyboard */ |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 348 | toggle_full_screen(ds); |
| 349 | gui_keysym = 1; |
| 350 | break; |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 351 | case 0x02 ... 0x0a: /* '1' to '9' keys */ |
| 352 | console_select(keycode - 0x02); |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 353 | if (is_active_console(vga_console)) { |
| 354 | /* tell the vga console to redisplay itself */ |
| 355 | vga_invalidate_display(); |
| 356 | } else { |
| 357 | /* display grab if going to a text console */ |
| 358 | if (gui_grab) |
| 359 | sdl_grab_end(); |
| 360 | } |
| 361 | gui_keysym = 1; |
| 362 | break; |
| 363 | default: |
| 364 | break; |
| 365 | } |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 366 | } else if (!is_active_console(vga_console)) { |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 367 | int keysym; |
| 368 | keysym = 0; |
| 369 | if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) { |
| 370 | switch(ev->key.keysym.sym) { |
| 371 | case SDLK_UP: keysym = QEMU_KEY_CTRL_UP; break; |
| 372 | case SDLK_DOWN: keysym = QEMU_KEY_CTRL_DOWN; break; |
| 373 | case SDLK_LEFT: keysym = QEMU_KEY_CTRL_LEFT; break; |
| 374 | case SDLK_RIGHT: keysym = QEMU_KEY_CTRL_RIGHT; break; |
| 375 | case SDLK_HOME: keysym = QEMU_KEY_CTRL_HOME; break; |
| 376 | case SDLK_END: keysym = QEMU_KEY_CTRL_END; break; |
| 377 | case SDLK_PAGEUP: keysym = QEMU_KEY_CTRL_PAGEUP; break; |
| 378 | case SDLK_PAGEDOWN: keysym = QEMU_KEY_CTRL_PAGEDOWN; break; |
| 379 | default: break; |
| 380 | } |
| 381 | } else { |
| 382 | switch(ev->key.keysym.sym) { |
| 383 | case SDLK_UP: keysym = QEMU_KEY_UP; break; |
| 384 | case SDLK_DOWN: keysym = QEMU_KEY_DOWN; break; |
| 385 | case SDLK_LEFT: keysym = QEMU_KEY_LEFT; break; |
| 386 | case SDLK_RIGHT: keysym = QEMU_KEY_RIGHT; break; |
| 387 | case SDLK_HOME: keysym = QEMU_KEY_HOME; break; |
| 388 | case SDLK_END: keysym = QEMU_KEY_END; break; |
| 389 | case SDLK_PAGEUP: keysym = QEMU_KEY_PAGEUP; break; |
| 390 | case SDLK_PAGEDOWN: keysym = QEMU_KEY_PAGEDOWN; break; |
| 391 | case SDLK_BACKSPACE: keysym = QEMU_KEY_BACKSPACE; break; case SDLK_DELETE: keysym = QEMU_KEY_DELETE; break; |
| 392 | default: break; |
| 393 | } |
| 394 | } |
| 395 | if (keysym) { |
| 396 | kbd_put_keysym(keysym); |
| 397 | } else if (ev->key.keysym.unicode != 0) { |
| 398 | kbd_put_keysym(ev->key.keysym.unicode); |
| 399 | } |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 400 | } |
| 401 | } else if (ev->type == SDL_KEYUP) { |
bellard | bf2b84e | 2004-11-14 19:46:35 +0000 | [diff] [blame] | 402 | mod_state = (ev->key.keysym.mod & gui_grab_code); |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 403 | if (!mod_state) { |
| 404 | if (gui_key_modifier_pressed) { |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 405 | if (gui_keysym == 0) { |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 406 | /* exit/enter grab if pressing Ctrl-Alt */ |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 407 | if (!gui_grab) |
| 408 | sdl_grab_start(); |
| 409 | else |
| 410 | sdl_grab_end(); |
bellard | 32ff25b | 2004-10-03 14:33:54 +0000 | [diff] [blame] | 411 | /* SDL does not send back all the |
| 412 | modifiers key, so we must correct it */ |
| 413 | reset_keys(); |
bellard | 8e9c4af | 2004-04-28 19:33:40 +0000 | [diff] [blame] | 414 | break; |
| 415 | } |
| 416 | gui_key_modifier_pressed = 0; |
| 417 | gui_keysym = 0; |
| 418 | } |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 421 | if (is_active_console(vga_console)) |
| 422 | sdl_process_key(&ev->key); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 423 | break; |
| 424 | case SDL_QUIT: |
bellard | 979a54f | 2004-06-20 12:36:04 +0000 | [diff] [blame] | 425 | qemu_system_shutdown_request(); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 426 | break; |
| 427 | case SDL_MOUSEMOTION: |
| 428 | if (gui_grab) { |
| 429 | sdl_send_mouse_event(); |
| 430 | } |
| 431 | break; |
| 432 | case SDL_MOUSEBUTTONDOWN: |
| 433 | case SDL_MOUSEBUTTONUP: |
| 434 | { |
| 435 | SDL_MouseButtonEvent *bev = &ev->button; |
| 436 | if (!gui_grab) { |
| 437 | if (ev->type == SDL_MOUSEBUTTONDOWN && |
| 438 | (bev->state & SDL_BUTTON_LMASK)) { |
| 439 | /* start grabbing all events */ |
| 440 | sdl_grab_start(); |
| 441 | } |
| 442 | } else { |
| 443 | sdl_send_mouse_event(); |
| 444 | } |
| 445 | } |
| 446 | break; |
bellard | 0294ffb | 2004-04-29 22:15:15 +0000 | [diff] [blame] | 447 | case SDL_ACTIVEEVENT: |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 448 | if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0 && |
| 449 | !gui_fullscreen_initial_grab) { |
bellard | 0294ffb | 2004-04-29 22:15:15 +0000 | [diff] [blame] | 450 | sdl_grab_end(); |
| 451 | } |
| 452 | break; |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 453 | default: |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
bellard | 898712a | 2004-02-06 19:56:42 +0000 | [diff] [blame] | 459 | static void sdl_cleanup(void) |
| 460 | { |
| 461 | SDL_Quit(); |
| 462 | } |
| 463 | |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 464 | void sdl_display_init(DisplayState *ds, int full_screen) |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 465 | { |
| 466 | int flags; |
| 467 | |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 468 | #if defined(__APPLE__) |
| 469 | /* always use generic keymaps */ |
| 470 | if (!keyboard_layout) |
| 471 | keyboard_layout = "en-us"; |
| 472 | #endif |
| 473 | if(keyboard_layout) { |
| 474 | kbd_layout = init_keyboard_layout(keyboard_layout); |
| 475 | if (!kbd_layout) |
| 476 | exit(1); |
| 477 | } |
| 478 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 479 | flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE; |
| 480 | if (SDL_Init (flags)) { |
| 481 | fprintf(stderr, "Could not initialize SDL - exiting\n"); |
| 482 | exit(1); |
| 483 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 484 | #ifndef _WIN32 |
bellard | 0ae04d7 | 2003-09-30 21:09:16 +0000 | [diff] [blame] | 485 | /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */ |
| 486 | signal(SIGINT, SIG_DFL); |
| 487 | signal(SIGQUIT, SIG_DFL); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 488 | #endif |
bellard | 0ae04d7 | 2003-09-30 21:09:16 +0000 | [diff] [blame] | 489 | |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 490 | ds->dpy_update = sdl_update; |
| 491 | ds->dpy_resize = sdl_resize; |
| 492 | ds->dpy_refresh = sdl_refresh; |
| 493 | |
| 494 | sdl_resize(ds, 640, 400); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 495 | sdl_update_caption(); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 496 | SDL_EnableKeyRepeat(250, 50); |
bellard | 457831f | 2004-07-14 17:22:33 +0000 | [diff] [blame] | 497 | SDL_EnableUNICODE(1); |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 498 | gui_grab = 0; |
bellard | 898712a | 2004-02-06 19:56:42 +0000 | [diff] [blame] | 499 | |
| 500 | atexit(sdl_cleanup); |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 501 | if (full_screen) { |
| 502 | gui_fullscreen = 1; |
| 503 | gui_fullscreen_initial_grab = 1; |
| 504 | sdl_grab_start(); |
| 505 | } |
bellard | 0f0b726 | 2003-08-09 18:26:36 +0000 | [diff] [blame] | 506 | } |