Commit 0f0b726444c45004910617da0c5bce666caa07b3
1 parent
b92e5a22
SDL support for VGA, keyboard and mouse
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@348 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
1 changed file
with
275 additions
and
0 deletions
sdl.c
0 → 100644
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-i386.h" | ||
46 | +#include "exec.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 | + SDL_UpdateRect(screen, x, y, w, h); | ||
56 | +} | ||
57 | + | ||
58 | +static void sdl_resize(DisplayState *ds, int w, int h) | ||
59 | +{ | ||
60 | + int flags; | ||
61 | + | ||
62 | + // printf("resizing to %d %d\n", w, h); | ||
63 | + | ||
64 | + flags = SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL; | ||
65 | + flags |= SDL_RESIZABLE; | ||
66 | + screen = SDL_SetVideoMode(w, h, 0, flags); | ||
67 | + if (!screen) { | ||
68 | + fprintf(stderr, "Could not open SDL display\n"); | ||
69 | + exit(1); | ||
70 | + } | ||
71 | + ds->data = screen->pixels; | ||
72 | + ds->linesize = screen->pitch; | ||
73 | + ds->depth = screen->format->BitsPerPixel; | ||
74 | +} | ||
75 | + | ||
76 | +static const uint32_t x_keycode_to_pc_keycode[61] = { | ||
77 | + 0x47e0, /* 97 Home */ | ||
78 | + 0x48e0, /* 98 Up */ | ||
79 | + 0x49e0, /* 99 PgUp */ | ||
80 | + 0x4be0, /* 100 Left */ | ||
81 | + 0x4c, /* 101 KP-5 */ | ||
82 | + 0x4de0, /* 102 Right */ | ||
83 | + 0x4fe0, /* 103 End */ | ||
84 | + 0x50e0, /* 104 Down */ | ||
85 | + 0x51e0, /* 105 PgDn */ | ||
86 | + 0x52e0, /* 106 Ins */ | ||
87 | + 0x53e0, /* 107 Del */ | ||
88 | + 0x1ce0, /* 108 Enter */ | ||
89 | + 0x1de0, /* 109 Ctrl-R */ | ||
90 | + 0x451de1, /* 110 Pause */ | ||
91 | + 0x37e0, /* 111 Print */ | ||
92 | + 0x35e0, /* 112 Divide */ | ||
93 | + 0x38e0, /* 113 Alt-R */ | ||
94 | + 0x46e0, /* 114 Break */ | ||
95 | + 0x0, /* 115 */ | ||
96 | + 0x0, /* 116 */ | ||
97 | + 0x0, /* 117 */ | ||
98 | + 0x0, /* 118 */ | ||
99 | + 0x0, /* 119 */ | ||
100 | + 0x0, /* 120 */ | ||
101 | + 0x0, /* 121 */ | ||
102 | + 0x0, /* 122 */ | ||
103 | + 0x0, /* 123 */ | ||
104 | + 0x0, /* 124 */ | ||
105 | + 0x0, /* 125 */ | ||
106 | + 0x0, /* 126 */ | ||
107 | + 0x0, /* 127 */ | ||
108 | + 0x0, /* 128 */ | ||
109 | + 0x0, /* 129 */ | ||
110 | + 0x0, /* 130 */ | ||
111 | + 0x0, /* 131 */ | ||
112 | + 0x0, /* 132 */ | ||
113 | + 0x0, /* 133 */ | ||
114 | + 0x0, /* 134 */ | ||
115 | + 0x0, /* 135 */ | ||
116 | + 0x47, /* 136 KP_7 */ | ||
117 | + 0x48, /* 137 KP_8 */ | ||
118 | + 0x49, /* 138 KP_9 */ | ||
119 | + 0x4b, /* 139 KP_4 */ | ||
120 | + 0x4c, /* 140 KP_5 */ | ||
121 | + 0x4d, /* 141 KP_6 */ | ||
122 | + 0x4f, /* 142 KP_1 */ | ||
123 | + 0x50, /* 143 KP_2 */ | ||
124 | + 0x51, /* 144 KP_3 */ | ||
125 | + 0x52, /* 145 KP_0 */ | ||
126 | + 0x53, /* 146 KP_. */ | ||
127 | + 0x47, /* 147 KP_HOME */ | ||
128 | + 0x48, /* 148 KP_UP */ | ||
129 | + 0x49, /* 149 KP_PgUp */ | ||
130 | + 0x4b, /* 150 KP_Left */ | ||
131 | + 0x4c, /* 151 KP_ */ | ||
132 | + 0x4d, /* 152 KP_Right */ | ||
133 | + 0x4f, /* 153 KP_End */ | ||
134 | + 0x50, /* 154 KP_Down */ | ||
135 | + 0x51, /* 155 KP_PgDn */ | ||
136 | + 0x52, /* 156 KP_Ins */ | ||
137 | + 0x53, /* 157 KP_Del */ | ||
138 | +}; | ||
139 | + | ||
140 | +static void sdl_process_key(SDL_KeyboardEvent *ev) | ||
141 | +{ | ||
142 | + int keycode, v; | ||
143 | + | ||
144 | + /* XXX: not portable, but avoids complicated mappings */ | ||
145 | + keycode = ev->keysym.scancode; | ||
146 | + if (keycode < 9) { | ||
147 | + keycode = 0; | ||
148 | + } else if (keycode < 97) { | ||
149 | + keycode -= 8; /* just an offset */ | ||
150 | + } else if (keycode < 158) { | ||
151 | + /* use conversion table */ | ||
152 | + keycode = x_keycode_to_pc_keycode[keycode - 97]; | ||
153 | + } else { | ||
154 | + keycode = 0; | ||
155 | + } | ||
156 | + | ||
157 | + /* now send the key code */ | ||
158 | + while (keycode != 0) { | ||
159 | + v = keycode & 0xff; | ||
160 | + if (ev->type == SDL_KEYUP) | ||
161 | + v |= 0x80; | ||
162 | + kbd_put_keycode(v); | ||
163 | + keycode >>= 8; | ||
164 | + } | ||
165 | +} | ||
166 | + | ||
167 | +static void sdl_grab_start(void) | ||
168 | +{ | ||
169 | + SDL_WM_SetCaption("QEMU - Press Ctrl-Shift to exit grab", "QEMU"); | ||
170 | + SDL_ShowCursor(0); | ||
171 | + SDL_WM_GrabInput(SDL_GRAB_ON); | ||
172 | + /* dummy read to avoid moving the mouse */ | ||
173 | + SDL_GetRelativeMouseState(NULL, NULL); | ||
174 | + gui_grab = 1; | ||
175 | +} | ||
176 | + | ||
177 | +static void sdl_grab_end(void) | ||
178 | +{ | ||
179 | + SDL_WM_SetCaption("QEMU", "QEMU"); | ||
180 | + SDL_WM_GrabInput(SDL_GRAB_OFF); | ||
181 | + SDL_ShowCursor(1); | ||
182 | + gui_grab = 0; | ||
183 | +} | ||
184 | + | ||
185 | +static void sdl_send_mouse_event(void) | ||
186 | +{ | ||
187 | + int dx, dy, dz, state, buttons; | ||
188 | + state = SDL_GetRelativeMouseState(&dx, &dy); | ||
189 | + buttons = 0; | ||
190 | + if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) | ||
191 | + buttons |= MOUSE_EVENT_LBUTTON; | ||
192 | + if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) | ||
193 | + buttons |= MOUSE_EVENT_RBUTTON; | ||
194 | + if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) | ||
195 | + buttons |= MOUSE_EVENT_MBUTTON; | ||
196 | + /* XXX: test wheel */ | ||
197 | + dz = 0; | ||
198 | + if (state & SDL_BUTTON(SDL_BUTTON_WHEELUP)) | ||
199 | + dz--; | ||
200 | + if (state & SDL_BUTTON(SDL_BUTTON_WHEELDOWN)) | ||
201 | + dz++; | ||
202 | + kbd_mouse_event(dx, dy, dz, buttons); | ||
203 | +} | ||
204 | + | ||
205 | +static void sdl_refresh(DisplayState *ds) | ||
206 | +{ | ||
207 | + SDL_Event ev1, *ev = &ev1; | ||
208 | + | ||
209 | + vga_update_display(); | ||
210 | + while (SDL_PollEvent(ev)) { | ||
211 | + switch (ev->type) { | ||
212 | + case SDL_VIDEOEXPOSE: | ||
213 | + sdl_update(ds, 0, 0, screen->w, screen->h); | ||
214 | + break; | ||
215 | + case SDL_KEYDOWN: | ||
216 | + case SDL_KEYUP: | ||
217 | + if (ev->type == SDL_KEYDOWN) { | ||
218 | + if ((SDL_GetModState() & (KMOD_LSHIFT | KMOD_LCTRL)) == | ||
219 | + (KMOD_LSHIFT | KMOD_LCTRL)) { | ||
220 | + /* exit/enter grab if pressing Ctrl-Shift */ | ||
221 | + if (!gui_grab) | ||
222 | + sdl_grab_start(); | ||
223 | + else | ||
224 | + sdl_grab_end(); | ||
225 | + } | ||
226 | + } | ||
227 | + sdl_process_key(&ev->key); | ||
228 | + break; | ||
229 | + case SDL_QUIT: | ||
230 | + reset_requested = 1; | ||
231 | + break; | ||
232 | + case SDL_MOUSEMOTION: | ||
233 | + if (gui_grab) { | ||
234 | + sdl_send_mouse_event(); | ||
235 | + } | ||
236 | + break; | ||
237 | + case SDL_MOUSEBUTTONDOWN: | ||
238 | + case SDL_MOUSEBUTTONUP: | ||
239 | + { | ||
240 | + SDL_MouseButtonEvent *bev = &ev->button; | ||
241 | + if (!gui_grab) { | ||
242 | + if (ev->type == SDL_MOUSEBUTTONDOWN && | ||
243 | + (bev->state & SDL_BUTTON_LMASK)) { | ||
244 | + /* start grabbing all events */ | ||
245 | + sdl_grab_start(); | ||
246 | + } | ||
247 | + } else { | ||
248 | + sdl_send_mouse_event(); | ||
249 | + } | ||
250 | + } | ||
251 | + break; | ||
252 | + default: | ||
253 | + break; | ||
254 | + } | ||
255 | + } | ||
256 | +} | ||
257 | + | ||
258 | +void sdl_display_init(DisplayState *ds) | ||
259 | +{ | ||
260 | + int flags; | ||
261 | + | ||
262 | + flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE; | ||
263 | + if (SDL_Init (flags)) { | ||
264 | + fprintf(stderr, "Could not initialize SDL - exiting\n"); | ||
265 | + exit(1); | ||
266 | + } | ||
267 | + ds->dpy_update = sdl_update; | ||
268 | + ds->dpy_resize = sdl_resize; | ||
269 | + ds->dpy_refresh = sdl_refresh; | ||
270 | + | ||
271 | + sdl_resize(ds, 640, 400); | ||
272 | + SDL_WM_SetCaption("QEMU", "QEMU"); | ||
273 | + SDL_EnableKeyRepeat(250, 50); | ||
274 | + gui_grab = 0; | ||
275 | +} |