fixed window switch - fixed caps lock and num lock - simplified keycodes (initial...
[qemu] / sdl.c
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  */
24 #include "vl.h"
25
26 #include <SDL.h>
27
28 #ifndef _WIN32
29 #include <signal.h>
30 #endif
31
32 static SDL_Surface *screen;
33 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
34 static int last_vm_running;
35 static int gui_saved_grab;
36 static int gui_fullscreen;
37 static int gui_key_modifier_pressed;
38 static int gui_keysym;
39
40 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
41 {
42     //    printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
43     SDL_UpdateRect(screen, x, y, w, h);
44 }
45
46 static void sdl_resize(DisplayState *ds, int w, int h)
47 {
48     int flags;
49
50     //    printf("resizing to %d %d\n", w, h);
51
52     flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
53     flags |= SDL_RESIZABLE;
54     if (gui_fullscreen)
55         flags |= SDL_FULLSCREEN;
56     screen = SDL_SetVideoMode(w, h, 0, flags);
57     if (!screen) {
58         fprintf(stderr, "Could not open SDL display\n");
59         exit(1);
60     }
61     ds->data = screen->pixels;
62     ds->linesize = screen->pitch;
63     ds->depth = screen->format->BitsPerPixel;
64 }
65
66 static const uint8_t x_keycode_to_pc_keycode[61] = {
67    0xc7,      /*  97  Home   */
68    0xc8,      /*  98  Up     */
69    0xc9,      /*  99  PgUp   */
70    0xcb,      /* 100  Left   */
71    0x4c,        /* 101  KP-5   */
72    0xcd,      /* 102  Right  */
73    0xcf,      /* 103  End    */
74    0xd0,      /* 104  Down   */
75    0xd1,      /* 105  PgDn   */
76    0xd2,      /* 106  Ins    */
77    0xd3,      /* 107  Del    */
78    0x9c,      /* 108  Enter  */
79    0x9d,      /* 109  Ctrl-R */
80    0xb7,      /* 111  Print  */
81    0xb5,      /* 112  Divide */
82    0xb8,      /* 113  Alt-R  */
83    0xc6,      /* 114  Break  */   
84    0x0,         /* 115 */
85    0x0,         /* 116 */
86    0x0,         /* 117 */
87    0x0,         /* 118 */
88    0x0,         /* 119 */
89    0x70,         /* 120 Hiragana_Katakana */
90    0x0,         /* 121 */
91    0x0,         /* 122 */
92    0x73,         /* 123 backslash */
93    0x0,         /* 124 */
94    0x0,         /* 125 */
95    0x0,         /* 126 */
96    0x0,         /* 127 */
97    0x0,         /* 128 */
98    0x79,         /* 129 Henkan */
99    0x0,         /* 130 */
100    0x7b,         /* 131 Muhenkan */
101    0x0,         /* 132 */
102    0x7d,         /* 133 Yen */
103    0x0,         /* 134 */
104    0x0,         /* 135 */
105    0x47,         /* 136 KP_7 */
106    0x48,         /* 137 KP_8 */
107    0x49,         /* 138 KP_9 */
108    0x4b,         /* 139 KP_4 */
109    0x4c,         /* 140 KP_5 */
110    0x4d,         /* 141 KP_6 */
111    0x4f,         /* 142 KP_1 */
112    0x50,         /* 143 KP_2 */
113    0x51,         /* 144 KP_3 */
114    0x52,         /* 145 KP_0 */
115    0x53,         /* 146 KP_. */
116    0x47,         /* 147 KP_HOME */
117    0x48,         /* 148 KP_UP */
118    0x49,         /* 149 KP_PgUp */
119    0x4b,         /* 150 KP_Left */
120    0x4c,         /* 151 KP_ */
121    0x4d,         /* 152 KP_Right */
122    0x4f,         /* 153 KP_End */
123    0x50,         /* 154 KP_Down */
124    0x51,         /* 155 KP_PgDn */
125    0x52,         /* 156 KP_Ins */
126    0x53,         /* 157 KP_Del */
127 };
128
129 static void sdl_process_key(SDL_KeyboardEvent *ev)
130 {
131     int keycode, v, i;
132     static uint8_t modifiers_state[256];
133
134     if (ev->keysym.sym == SDLK_PAUSE) {
135         /* specific case */
136         v = 0;
137         if (ev->type == SDL_KEYUP)
138             v |= 0x80;
139         kbd_put_keycode(0xe1);
140         kbd_put_keycode(0x1d | v);
141         kbd_put_keycode(0x45 | v);
142         return;
143     }
144
145     /* XXX: not portable, but avoids complicated mappings */
146     keycode = ev->keysym.scancode;
147
148     /* XXX: windows version may not work: 0xe0/0xe1 should be trapped
149        ? */
150 #ifndef _WIN32
151     if (keycode < 9) {
152         keycode = 0;
153     } else if (keycode < 97) {
154         keycode -= 8; /* just an offset */
155     } else if (keycode < 158) {
156         /* use conversion table */
157         keycode = x_keycode_to_pc_keycode[keycode - 97];
158     } else {
159         keycode = 0;
160     }
161 #endif
162
163     switch(keycode) {
164     case 0x00:
165         /* sent when leaving window: reset the modifiers state */
166         for(i = 0; i < 256; i++) {
167             if (modifiers_state[i]) {
168                 if (i & 0x80)
169                     kbd_put_keycode(0xe0);
170                 kbd_put_keycode(i | 0x80);
171             }
172         }
173         return;
174     case 0x2a:                          /* Left Shift */
175     case 0x36:                          /* Right Shift */
176     case 0x1d:                          /* Left CTRL */
177     case 0x9d:                          /* Right CTRL */
178     case 0x38:                          /* Left ALT */
179     case 0xb8:                         /* Right ALT */
180         if (ev->type == SDL_KEYUP)
181             modifiers_state[keycode] = 0;
182         else
183             modifiers_state[keycode] = 1;
184         break;
185     case 0x45: /* num lock */
186     case 0x3a: /* caps lock */
187         /* SDL does not send the key up event, so we generate it */
188         kbd_put_keycode(keycode);
189         kbd_put_keycode(keycode | 0x80);
190         return;
191     }
192
193     /* now send the key code */
194     if (keycode & 0x80)
195         kbd_put_keycode(0xe0);
196     if (ev->type == SDL_KEYUP)
197         kbd_put_keycode(keycode | 0x80);
198     else
199         kbd_put_keycode(keycode & 0x7f);
200 }
201
202 static void sdl_update_caption(void)
203 {
204     char buf[1024];
205     strcpy(buf, "QEMU");
206     if (!vm_running) {
207         strcat(buf, " [Stopped]");
208     }
209     if (gui_grab) {
210         strcat(buf, " - Press Ctrl-Shift to exit grab");
211     }
212     SDL_WM_SetCaption(buf, "QEMU");
213 }
214
215 static void sdl_grab_start(void)
216 {
217     SDL_ShowCursor(0);
218     SDL_WM_GrabInput(SDL_GRAB_ON);
219     /* dummy read to avoid moving the mouse */
220     SDL_GetRelativeMouseState(NULL, NULL);
221     gui_grab = 1;
222     sdl_update_caption();
223 }
224
225 static void sdl_grab_end(void)
226 {
227     SDL_WM_GrabInput(SDL_GRAB_OFF);
228     SDL_ShowCursor(1);
229     gui_grab = 0;
230     sdl_update_caption();
231 }
232
233 static void sdl_send_mouse_event(void)
234 {
235     int dx, dy, dz, state, buttons;
236     state = SDL_GetRelativeMouseState(&dx, &dy);
237     buttons = 0;
238     if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
239         buttons |= MOUSE_EVENT_LBUTTON;
240     if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
241         buttons |= MOUSE_EVENT_RBUTTON;
242     if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
243         buttons |= MOUSE_EVENT_MBUTTON;
244     /* XXX: test wheel */
245     dz = 0;
246 #ifdef SDL_BUTTON_WHEELUP
247     if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
248         dz--;
249     if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
250         dz++;
251 #endif
252     kbd_mouse_event(dx, dy, dz, buttons);
253 }
254
255 static void toggle_full_screen(DisplayState *ds)
256 {
257     gui_fullscreen = !gui_fullscreen;
258     sdl_resize(ds, screen->w, screen->h);
259     if (gui_fullscreen) {
260         gui_saved_grab = gui_grab;
261         sdl_grab_start();
262     } else {
263         if (!gui_saved_grab)
264             sdl_grab_end();
265     }
266     vga_update_display();
267     sdl_update(ds, 0, 0, screen->w, screen->h);
268 }
269
270 static void sdl_refresh(DisplayState *ds)
271 {
272     SDL_Event ev1, *ev = &ev1;
273     int mod_state;
274                      
275     if (last_vm_running != vm_running) {
276         last_vm_running = vm_running;
277         sdl_update_caption();
278     }
279
280     vga_update_display();
281     while (SDL_PollEvent(ev)) {
282         switch (ev->type) {
283         case SDL_VIDEOEXPOSE:
284             sdl_update(ds, 0, 0, screen->w, screen->h);
285             break;
286         case SDL_KEYDOWN:
287         case SDL_KEYUP:
288             if (ev->type == SDL_KEYDOWN) {
289                 mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
290                     (KMOD_LSHIFT | KMOD_LCTRL);
291                 gui_key_modifier_pressed = mod_state;
292                 if (gui_key_modifier_pressed && 
293                     ev->key.keysym.sym == SDLK_f) {
294                     gui_keysym = ev->key.keysym.sym;
295                 }
296             } else if (ev->type == SDL_KEYUP) {
297                 mod_state = (SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL));
298                 if (!mod_state) {
299                     if (gui_key_modifier_pressed) {
300                         switch(gui_keysym) {
301                         case SDLK_f:
302                             toggle_full_screen(ds);
303                             break;
304                         case 0:
305                             /* exit/enter grab if pressing Ctrl-Shift */
306                             if (!gui_grab)
307                                 sdl_grab_start();
308                             else
309                                 sdl_grab_end();
310                             break;
311                         }
312                         gui_key_modifier_pressed = 0;
313                         gui_keysym = 0;
314                     }
315                 }
316             }
317             sdl_process_key(&ev->key);
318             break;
319         case SDL_QUIT:
320             reset_requested = 1;
321             break;
322         case SDL_MOUSEMOTION:
323             if (gui_grab) {
324                 sdl_send_mouse_event();
325             }
326             break;
327         case SDL_MOUSEBUTTONDOWN:
328         case SDL_MOUSEBUTTONUP:
329             {
330                 SDL_MouseButtonEvent *bev = &ev->button;
331                 if (!gui_grab) {
332                     if (ev->type == SDL_MOUSEBUTTONDOWN &&
333                         (bev->state & SDL_BUTTON_LMASK)) {
334                         /* start grabbing all events */
335                         sdl_grab_start();
336                     }
337                 } else {
338                     sdl_send_mouse_event();
339                 }
340             }
341             break;
342         case SDL_ACTIVEEVENT:
343             if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0) {
344                 sdl_grab_end();
345             }
346             break;
347         default:
348             break;
349         }
350     }
351 }
352
353 static void sdl_cleanup(void) 
354 {
355     SDL_Quit();
356 }
357
358 void sdl_display_init(DisplayState *ds)
359 {
360     int flags;
361
362     flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
363     if (SDL_Init (flags)) {
364         fprintf(stderr, "Could not initialize SDL - exiting\n");
365         exit(1);
366     }
367
368 #ifndef _WIN32
369     /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
370     signal(SIGINT, SIG_DFL);
371     signal(SIGQUIT, SIG_DFL);
372 #endif
373
374     ds->dpy_update = sdl_update;
375     ds->dpy_resize = sdl_resize;
376     ds->dpy_refresh = sdl_refresh;
377
378     sdl_resize(ds, 640, 400);
379     sdl_update_caption();
380     SDL_EnableKeyRepeat(250, 50);
381     gui_grab = 0;
382
383     atexit(sdl_cleanup);
384 }