sdl_cleanup fix (Martin Garton)
[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 <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <getopt.h>
29 #include <inttypes.h>
30 #include <unistd.h>
31 #include <sys/mman.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <sys/time.h>
36 #include <malloc.h>
37 #include <termios.h>
38 #include <sys/poll.h>
39 #include <errno.h>
40 #include <sys/wait.h>
41 #include <netinet/in.h>
42
43 #include <SDL.h>
44
45 #include "cpu.h"
46 #include "exec-all.h"
47
48 #include "vl.h"
49
50 static SDL_Surface *screen;
51 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
52
53 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
54 {
55     //    printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
56     SDL_UpdateRect(screen, x, y, w, h);
57 }
58
59 static void sdl_resize(DisplayState *ds, int w, int h)
60 {
61     int flags;
62
63     //    printf("resizing to %d %d\n", w, h);
64
65     flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL;
66     flags |= SDL_RESIZABLE;
67     screen = SDL_SetVideoMode(w, h, 0, flags);
68     if (!screen) {
69         fprintf(stderr, "Could not open SDL display\n");
70         exit(1);
71     }
72     ds->data = screen->pixels;
73     ds->linesize = screen->pitch;
74     ds->depth = screen->format->BitsPerPixel;
75 }
76
77 static const uint32_t x_keycode_to_pc_keycode[61] = {
78    0x47e0,      /*  97  Home   */
79    0x48e0,      /*  98  Up     */
80    0x49e0,      /*  99  PgUp   */
81    0x4be0,      /* 100  Left   */
82    0x4c,        /* 101  KP-5   */
83    0x4de0,      /* 102  Right  */
84    0x4fe0,      /* 103  End    */
85    0x50e0,      /* 104  Down   */
86    0x51e0,      /* 105  PgDn   */
87    0x52e0,      /* 106  Ins    */
88    0x53e0,      /* 107  Del    */
89    0x1ce0,      /* 108  Enter  */
90    0x1de0,      /* 109  Ctrl-R */
91    0x451de1,    /* 110  Pause  */
92    0x37e0,      /* 111  Print  */
93    0x35e0,      /* 112  Divide */
94    0x38e0,      /* 113  Alt-R  */
95    0x46e0,      /* 114  Break  */   
96    0x0,         /* 115 */
97    0x0,         /* 116 */
98    0x0,         /* 117 */
99    0x0,         /* 118 */
100    0x0,         /* 119 */
101    0x0,         /* 120 */
102    0x0,         /* 121 */
103    0x0,         /* 122 */
104    0x0,         /* 123 */
105    0x0,         /* 124 */
106    0x0,         /* 125 */
107    0x0,         /* 126 */
108    0x0,         /* 127 */
109    0x0,         /* 128 */
110    0x0,         /* 129 */
111    0x0,         /* 130 */
112    0x0,         /* 131 */
113    0x0,         /* 132 */
114    0x0,         /* 133 */
115    0x0,         /* 134 */
116    0x0,         /* 135 */
117    0x47,         /* 136 KP_7 */
118    0x48,         /* 137 KP_8 */
119    0x49,         /* 138 KP_9 */
120    0x4b,         /* 139 KP_4 */
121    0x4c,         /* 140 KP_5 */
122    0x4d,         /* 141 KP_6 */
123    0x4f,         /* 142 KP_1 */
124    0x50,         /* 143 KP_2 */
125    0x51,         /* 144 KP_3 */
126    0x52,         /* 145 KP_0 */
127    0x53,         /* 146 KP_. */
128    0x47,         /* 147 KP_HOME */
129    0x48,         /* 148 KP_UP */
130    0x49,         /* 149 KP_PgUp */
131    0x4b,         /* 150 KP_Left */
132    0x4c,         /* 151 KP_ */
133    0x4d,         /* 152 KP_Right */
134    0x4f,         /* 153 KP_End */
135    0x50,         /* 154 KP_Down */
136    0x51,         /* 155 KP_PgDn */
137    0x52,         /* 156 KP_Ins */
138    0x53,         /* 157 KP_Del */
139 };
140
141 static void sdl_process_key(SDL_KeyboardEvent *ev)
142 {
143     int keycode, v;
144     
145     /* XXX: not portable, but avoids complicated mappings */
146     keycode = ev->keysym.scancode;
147     if (keycode < 9) {
148         keycode = 0;
149     } else if (keycode < 97) {
150         keycode -= 8; /* just an offset */
151     } else if (keycode < 158) {
152         /* use conversion table */
153         keycode = x_keycode_to_pc_keycode[keycode - 97];
154     } else {
155         keycode = 0;
156     }
157     
158     /* now send the key code */
159     while (keycode != 0) {
160         v = keycode & 0xff;
161         if (ev->type == SDL_KEYUP)
162             v |= 0x80;
163         kbd_put_keycode(v);
164         keycode >>= 8;
165     }
166 }
167
168 static void sdl_grab_start(void)
169 {
170     SDL_WM_SetCaption("QEMU - Press Ctrl-Shift to exit grab", "QEMU");
171     SDL_ShowCursor(0);
172     SDL_WM_GrabInput(SDL_GRAB_ON);
173     /* dummy read to avoid moving the mouse */
174     SDL_GetRelativeMouseState(NULL, NULL);
175     gui_grab = 1;
176 }
177
178 static void sdl_grab_end(void)
179 {
180     SDL_WM_SetCaption("QEMU", "QEMU");
181     SDL_WM_GrabInput(SDL_GRAB_OFF);
182     SDL_ShowCursor(1);
183     gui_grab = 0;
184 }
185
186 static void sdl_send_mouse_event(void)
187 {
188     int dx, dy, dz, state, buttons;
189     state = SDL_GetRelativeMouseState(&dx, &dy);
190     buttons = 0;
191     if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
192         buttons |= MOUSE_EVENT_LBUTTON;
193     if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
194         buttons |= MOUSE_EVENT_RBUTTON;
195     if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE))
196         buttons |= MOUSE_EVENT_MBUTTON;
197     /* XXX: test wheel */
198     dz = 0;
199 #ifdef SDL_BUTTON_WHEELUP
200     if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP))
201         dz--;
202     if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN))
203         dz++;
204 #endif
205     kbd_mouse_event(dx, dy, dz, buttons);
206 }
207
208 static void sdl_refresh(DisplayState *ds)
209 {
210     SDL_Event ev1, *ev = &ev1;
211
212     vga_update_display();
213     while (SDL_PollEvent(ev)) {
214         switch (ev->type) {
215         case SDL_VIDEOEXPOSE:
216             sdl_update(ds, 0, 0, screen->w, screen->h);
217             break;
218         case SDL_KEYDOWN:
219         case SDL_KEYUP:
220             if (ev->type == SDL_KEYDOWN) {
221                 if ((SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) ==
222                     (KMOD_LSHIFT | KMOD_LCTRL)) {
223                     /* exit/enter grab if pressing Ctrl-Shift */
224                     if (!gui_grab)
225                         sdl_grab_start();
226                     else
227                         sdl_grab_end();
228                 }
229             }
230             sdl_process_key(&ev->key);
231             break;
232         case SDL_QUIT:
233             reset_requested = 1;
234             break;
235         case SDL_MOUSEMOTION:
236             if (gui_grab) {
237                 sdl_send_mouse_event();
238             }
239             break;
240         case SDL_MOUSEBUTTONDOWN:
241         case SDL_MOUSEBUTTONUP:
242             {
243                 SDL_MouseButtonEvent *bev = &ev->button;
244                 if (!gui_grab) {
245                     if (ev->type == SDL_MOUSEBUTTONDOWN &&
246                         (bev->state & SDL_BUTTON_LMASK)) {
247                         /* start grabbing all events */
248                         sdl_grab_start();
249                     }
250                 } else {
251                     sdl_send_mouse_event();
252                 }
253             }
254             break;
255         default:
256             break;
257         }
258     }
259 }
260
261 static void sdl_cleanup(void) 
262 {
263     SDL_Quit();
264 }
265
266 void sdl_display_init(DisplayState *ds)
267 {
268     int flags;
269
270     flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
271     if (SDL_Init (flags)) {
272         fprintf(stderr, "Could not initialize SDL - exiting\n");
273         exit(1);
274     }
275     /* NOTE: we still want Ctrl-C to work, so we undo the SDL redirections */
276     signal(SIGINT, SIG_DFL);
277     signal(SIGQUIT, SIG_DFL);
278
279     ds->dpy_update = sdl_update;
280     ds->dpy_resize = sdl_resize;
281     ds->dpy_refresh = sdl_refresh;
282
283     sdl_resize(ds, 640, 400);
284     SDL_WM_SetCaption("QEMU", "QEMU");
285     SDL_EnableKeyRepeat(250, 50);
286     gui_grab = 0;
287
288     atexit(sdl_cleanup);
289 }