comparison src/video/ps2gs/SDL_gsevents.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents d910939febfa
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
54 /* The translation tables from a console scancode to a SDL keysym */ 54 /* The translation tables from a console scancode to a SDL keysym */
55 #define NUM_VGAKEYMAPS (1<<KG_CAPSSHIFT) 55 #define NUM_VGAKEYMAPS (1<<KG_CAPSSHIFT)
56 static Uint16 vga_keymap[NUM_VGAKEYMAPS][NR_KEYS]; 56 static Uint16 vga_keymap[NUM_VGAKEYMAPS][NR_KEYS];
57 static SDLKey keymap[128]; 57 static SDLKey keymap[128];
58 static Uint16 keymap_temp[128]; /* only used at startup */ 58 static Uint16 keymap_temp[128]; /* only used at startup */
59 static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym); 59 static SDL_keysym *TranslateKey (int scancode, SDL_keysym * keysym);
60 60
61 /* Ugh, we have to duplicate the kernel's keysym mapping code... 61 /* Ugh, we have to duplicate the kernel's keysym mapping code...
62 Oh, it's not so bad. :-) 62 Oh, it's not so bad. :-)
63 63
64 FIXME: Add keyboard LED handling code 64 FIXME: Add keyboard LED handling code
65 */ 65 */
66 static void GS_vgainitkeymaps(int fd) 66 static void
67 { 67 GS_vgainitkeymaps (int fd)
68 struct kbentry entry; 68 {
69 int map, i; 69 struct kbentry entry;
70 70 int map, i;
71 /* Don't do anything if we are passed a closed keyboard */ 71
72 if ( fd < 0 ) { 72 /* Don't do anything if we are passed a closed keyboard */
73 return; 73 if (fd < 0) {
74 } 74 return;
75 75 }
76 /* Load all the keysym mappings */ 76
77 for ( map=0; map<NUM_VGAKEYMAPS; ++map ) { 77 /* Load all the keysym mappings */
78 SDL_memset(vga_keymap[map], 0, NR_KEYS*sizeof(Uint16)); 78 for (map = 0; map < NUM_VGAKEYMAPS; ++map) {
79 for ( i=0; i<NR_KEYS; ++i ) { 79 SDL_memset (vga_keymap[map], 0, NR_KEYS * sizeof (Uint16));
80 entry.kb_table = map; 80 for (i = 0; i < NR_KEYS; ++i) {
81 entry.kb_index = i; 81 entry.kb_table = map;
82 if ( ioctl(fd, KDGKBENT, &entry) == 0 ) { 82 entry.kb_index = i;
83 /* fill keytemp. This replaces SDL_fbkeys.h */ 83 if (ioctl (fd, KDGKBENT, &entry) == 0) {
84 if ( (map == 0) && (i<128) ) { 84 /* fill keytemp. This replaces SDL_fbkeys.h */
85 keymap_temp[i] = entry.kb_value; 85 if ((map == 0) && (i < 128)) {
86 } 86 keymap_temp[i] = entry.kb_value;
87 /* The "Enter" key is a special case */ 87 }
88 if ( entry.kb_value == K_ENTER ) { 88 /* The "Enter" key is a special case */
89 entry.kb_value = K(KT_ASCII,13); 89 if (entry.kb_value == K_ENTER) {
90 } 90 entry.kb_value = K (KT_ASCII, 13);
91 /* Handle numpad specially as well */ 91 }
92 if ( KTYP(entry.kb_value) == KT_PAD ) { 92 /* Handle numpad specially as well */
93 switch ( entry.kb_value ) { 93 if (KTYP (entry.kb_value) == KT_PAD) {
94 case K_P0: 94 switch (entry.kb_value) {
95 case K_P1: 95 case K_P0:
96 case K_P2: 96 case K_P1:
97 case K_P3: 97 case K_P2:
98 case K_P4: 98 case K_P3:
99 case K_P5: 99 case K_P4:
100 case K_P6: 100 case K_P5:
101 case K_P7: 101 case K_P6:
102 case K_P8: 102 case K_P7:
103 case K_P9: 103 case K_P8:
104 vga_keymap[map][i]=entry.kb_value; 104 case K_P9:
105 vga_keymap[map][i]+= '0'; 105 vga_keymap[map][i] = entry.kb_value;
106 break; 106 vga_keymap[map][i] += '0';
107 case K_PPLUS: 107 break;
108 vga_keymap[map][i]=K(KT_ASCII,'+'); 108 case K_PPLUS:
109 break; 109 vga_keymap[map][i] = K (KT_ASCII, '+');
110 case K_PMINUS: 110 break;
111 vga_keymap[map][i]=K(KT_ASCII,'-'); 111 case K_PMINUS:
112 break; 112 vga_keymap[map][i] = K (KT_ASCII, '-');
113 case K_PSTAR: 113 break;
114 vga_keymap[map][i]=K(KT_ASCII,'*'); 114 case K_PSTAR:
115 break; 115 vga_keymap[map][i] = K (KT_ASCII, '*');
116 case K_PSLASH: 116 break;
117 vga_keymap[map][i]=K(KT_ASCII,'/'); 117 case K_PSLASH:
118 break; 118 vga_keymap[map][i] = K (KT_ASCII, '/');
119 case K_PENTER: 119 break;
120 vga_keymap[map][i]=K(KT_ASCII,'\r'); 120 case K_PENTER:
121 break; 121 vga_keymap[map][i] = K (KT_ASCII, '\r');
122 case K_PCOMMA: 122 break;
123 vga_keymap[map][i]=K(KT_ASCII,','); 123 case K_PCOMMA:
124 break; 124 vga_keymap[map][i] = K (KT_ASCII, ',');
125 case K_PDOT: 125 break;
126 vga_keymap[map][i]=K(KT_ASCII,'.'); 126 case K_PDOT:
127 break; 127 vga_keymap[map][i] = K (KT_ASCII, '.');
128 default: 128 break;
129 break; 129 default:
130 } 130 break;
131 } 131 }
132 /* Do the normal key translation */ 132 }
133 if ( (KTYP(entry.kb_value) == KT_LATIN) || 133 /* Do the normal key translation */
134 (KTYP(entry.kb_value) == KT_ASCII) || 134 if ((KTYP (entry.kb_value) == KT_LATIN) ||
135 (KTYP(entry.kb_value) == KT_LETTER) ) { 135 (KTYP (entry.kb_value) == KT_ASCII) ||
136 vga_keymap[map][i] = entry.kb_value; 136 (KTYP (entry.kb_value) == KT_LETTER)) {
137 } 137 vga_keymap[map][i] = entry.kb_value;
138 } 138 }
139 } 139 }
140 } 140 }
141 } 141 }
142 142 }
143 int GS_InGraphicsMode(_THIS) 143
144 { 144 int
145 return((keyboard_fd >= 0) && (saved_kbd_mode >= 0)); 145 GS_InGraphicsMode (_THIS)
146 } 146 {
147 147 return ((keyboard_fd >= 0) && (saved_kbd_mode >= 0));
148 int GS_EnterGraphicsMode(_THIS) 148 }
149 { 149
150 struct termios keyboard_termios; 150 int
151 151 GS_EnterGraphicsMode (_THIS)
152 /* Set medium-raw keyboard mode */ 152 {
153 if ( (keyboard_fd >= 0) && !GS_InGraphicsMode(this) ) { 153 struct termios keyboard_termios;
154 154
155 /* Switch to the correct virtual terminal */ 155 /* Set medium-raw keyboard mode */
156 if ( current_vt > 0 ) { 156 if ((keyboard_fd >= 0) && !GS_InGraphicsMode (this)) {
157 struct vt_stat vtstate; 157
158 158 /* Switch to the correct virtual terminal */
159 if ( ioctl(keyboard_fd, VT_GETSTATE, &vtstate) == 0 ) { 159 if (current_vt > 0) {
160 saved_vt = vtstate.v_active; 160 struct vt_stat vtstate;
161 } 161
162 if ( ioctl(keyboard_fd, VT_ACTIVATE, current_vt) == 0 ) { 162 if (ioctl (keyboard_fd, VT_GETSTATE, &vtstate) == 0) {
163 ioctl(keyboard_fd, VT_WAITACTIVE, current_vt); 163 saved_vt = vtstate.v_active;
164 } 164 }
165 } 165 if (ioctl (keyboard_fd, VT_ACTIVATE, current_vt) == 0) {
166 166 ioctl (keyboard_fd, VT_WAITACTIVE, current_vt);
167 /* Set the terminal input mode */ 167 }
168 if ( tcgetattr(keyboard_fd, &saved_kbd_termios) < 0 ) { 168 }
169 SDL_SetError("Unable to get terminal attributes"); 169
170 if ( keyboard_fd > 0 ) { 170 /* Set the terminal input mode */
171 close(keyboard_fd); 171 if (tcgetattr (keyboard_fd, &saved_kbd_termios) < 0) {
172 } 172 SDL_SetError ("Unable to get terminal attributes");
173 keyboard_fd = -1; 173 if (keyboard_fd > 0) {
174 return(-1); 174 close (keyboard_fd);
175 } 175 }
176 if ( ioctl(keyboard_fd, KDGKBMODE, &saved_kbd_mode) < 0 ) { 176 keyboard_fd = -1;
177 SDL_SetError("Unable to get current keyboard mode"); 177 return (-1);
178 if ( keyboard_fd > 0 ) { 178 }
179 close(keyboard_fd); 179 if (ioctl (keyboard_fd, KDGKBMODE, &saved_kbd_mode) < 0) {
180 } 180 SDL_SetError ("Unable to get current keyboard mode");
181 keyboard_fd = -1; 181 if (keyboard_fd > 0) {
182 return(-1); 182 close (keyboard_fd);
183 } 183 }
184 keyboard_termios = saved_kbd_termios; 184 keyboard_fd = -1;
185 keyboard_termios.c_lflag &= ~(ICANON | ECHO | ISIG); 185 return (-1);
186 keyboard_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON); 186 }
187 keyboard_termios.c_cc[VMIN] = 0; 187 keyboard_termios = saved_kbd_termios;
188 keyboard_termios.c_cc[VTIME] = 0; 188 keyboard_termios.c_lflag &= ~(ICANON | ECHO | ISIG);
189 if (tcsetattr(keyboard_fd, TCSAFLUSH, &keyboard_termios) < 0) { 189 keyboard_termios.c_iflag &=
190 GS_CloseKeyboard(this); 190 ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
191 SDL_SetError("Unable to set terminal attributes"); 191 keyboard_termios.c_cc[VMIN] = 0;
192 return(-1); 192 keyboard_termios.c_cc[VTIME] = 0;
193 } 193 if (tcsetattr (keyboard_fd, TCSAFLUSH, &keyboard_termios) < 0) {
194 /* This will fail if we aren't root or this isn't our tty */ 194 GS_CloseKeyboard (this);
195 if ( ioctl(keyboard_fd, KDSKBMODE, K_MEDIUMRAW) < 0 ) { 195 SDL_SetError ("Unable to set terminal attributes");
196 GS_CloseKeyboard(this); 196 return (-1);
197 SDL_SetError("Unable to set keyboard in raw mode"); 197 }
198 return(-1); 198 /* This will fail if we aren't root or this isn't our tty */
199 } 199 if (ioctl (keyboard_fd, KDSKBMODE, K_MEDIUMRAW) < 0) {
200 if ( ioctl(keyboard_fd, KDSETMODE, KD_GRAPHICS) < 0 ) { 200 GS_CloseKeyboard (this);
201 GS_CloseKeyboard(this); 201 SDL_SetError ("Unable to set keyboard in raw mode");
202 SDL_SetError("Unable to set keyboard in graphics mode"); 202 return (-1);
203 return(-1); 203 }
204 } 204 if (ioctl (keyboard_fd, KDSETMODE, KD_GRAPHICS) < 0) {
205 } 205 GS_CloseKeyboard (this);
206 return(keyboard_fd); 206 SDL_SetError ("Unable to set keyboard in graphics mode");
207 } 207 return (-1);
208 208 }
209 void GS_LeaveGraphicsMode(_THIS) 209 }
210 { 210 return (keyboard_fd);
211 if ( GS_InGraphicsMode(this) ) { 211 }
212 ioctl(keyboard_fd, KDSETMODE, KD_TEXT); 212
213 ioctl(keyboard_fd, KDSKBMODE, saved_kbd_mode); 213 void
214 tcsetattr(keyboard_fd, TCSAFLUSH, &saved_kbd_termios); 214 GS_LeaveGraphicsMode (_THIS)
215 saved_kbd_mode = -1; 215 {
216 216 if (GS_InGraphicsMode (this)) {
217 /* Head back over to the original virtual terminal */ 217 ioctl (keyboard_fd, KDSETMODE, KD_TEXT);
218 if ( saved_vt > 0 ) { 218 ioctl (keyboard_fd, KDSKBMODE, saved_kbd_mode);
219 ioctl(keyboard_fd, VT_ACTIVATE, saved_vt); 219 tcsetattr (keyboard_fd, TCSAFLUSH, &saved_kbd_termios);
220 } 220 saved_kbd_mode = -1;
221 } 221
222 } 222 /* Head back over to the original virtual terminal */
223 223 if (saved_vt > 0) {
224 void GS_CloseKeyboard(_THIS) 224 ioctl (keyboard_fd, VT_ACTIVATE, saved_vt);
225 { 225 }
226 if ( keyboard_fd >= 0 ) { 226 }
227 GS_LeaveGraphicsMode(this); 227 }
228 if ( keyboard_fd > 0 ) { 228
229 close(keyboard_fd); 229 void
230 } 230 GS_CloseKeyboard (_THIS)
231 } 231 {
232 keyboard_fd = -1; 232 if (keyboard_fd >= 0) {
233 } 233 GS_LeaveGraphicsMode (this);
234 234 if (keyboard_fd > 0) {
235 int GS_OpenKeyboard(_THIS) 235 close (keyboard_fd);
236 { 236 }
237 /* Open only if not already opened */ 237 }
238 if ( keyboard_fd < 0 ) { 238 keyboard_fd = -1;
239 char *tty0[] = { "/dev/tty0", "/dev/vc/0", NULL }; 239 }
240 char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL }; 240
241 int i, tty0_fd; 241 int
242 242 GS_OpenKeyboard (_THIS)
243 /* Try to query for a free virtual terminal */ 243 {
244 tty0_fd = -1; 244 /* Open only if not already opened */
245 for ( i=0; tty0[i] && (tty0_fd < 0); ++i ) { 245 if (keyboard_fd < 0) {
246 tty0_fd = open(tty0[i], O_WRONLY, 0); 246 char *tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
247 } 247 char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };
248 if ( tty0_fd < 0 ) { 248 int i, tty0_fd;
249 tty0_fd = dup(0); /* Maybe stdin is a VT? */ 249
250 } 250 /* Try to query for a free virtual terminal */
251 ioctl(tty0_fd, VT_OPENQRY, &current_vt); 251 tty0_fd = -1;
252 close(tty0_fd); 252 for (i = 0; tty0[i] && (tty0_fd < 0); ++i) {
253 if ( (geteuid() == 0) && (current_vt > 0) ) { 253 tty0_fd = open (tty0[i], O_WRONLY, 0);
254 for ( i=0; vcs[i] && (keyboard_fd < 0); ++i ) { 254 }
255 char vtpath[12]; 255 if (tty0_fd < 0) {
256 256 tty0_fd = dup (0); /* Maybe stdin is a VT? */
257 SDL_snprintf(vtpath, SDL_arraysize(vtpath), vcs[i], current_vt); 257 }
258 keyboard_fd = open(vtpath, O_RDWR, 0); 258 ioctl (tty0_fd, VT_OPENQRY, &current_vt);
259 close (tty0_fd);
260 if ((geteuid () == 0) && (current_vt > 0)) {
261 for (i = 0; vcs[i] && (keyboard_fd < 0); ++i) {
262 char vtpath[12];
263
264 SDL_snprintf (vtpath, SDL_arraysize (vtpath), vcs[i],
265 current_vt);
266 keyboard_fd = open (vtpath, O_RDWR, 0);
259 #ifdef DEBUG_KEYBOARD 267 #ifdef DEBUG_KEYBOARD
260 fprintf(stderr, "vtpath = %s, fd = %d\n", 268 fprintf (stderr, "vtpath = %s, fd = %d\n",
261 vtpath, keyboard_fd); 269 vtpath, keyboard_fd);
262 #endif /* DEBUG_KEYBOARD */ 270 #endif /* DEBUG_KEYBOARD */
263 271
264 /* This needs to be our controlling tty 272 /* This needs to be our controlling tty
265 so that the kernel ioctl() calls work 273 so that the kernel ioctl() calls work
266 */ 274 */
267 if ( keyboard_fd >= 0 ) { 275 if (keyboard_fd >= 0) {
268 tty0_fd = open("/dev/tty", O_RDWR, 0); 276 tty0_fd = open ("/dev/tty", O_RDWR, 0);
269 if ( tty0_fd >= 0 ) { 277 if (tty0_fd >= 0) {
270 ioctl(tty0_fd, TIOCNOTTY, 0); 278 ioctl (tty0_fd, TIOCNOTTY, 0);
271 close(tty0_fd); 279 close (tty0_fd);
272 } 280 }
273 } 281 }
274 } 282 }
275 } 283 }
276 if ( keyboard_fd < 0 ) { 284 if (keyboard_fd < 0) {
277 /* Last resort, maybe our tty is a usable VT */ 285 /* Last resort, maybe our tty is a usable VT */
278 current_vt = 0; 286 current_vt = 0;
279 keyboard_fd = open("/dev/tty", O_RDWR); 287 keyboard_fd = open ("/dev/tty", O_RDWR);
280 } 288 }
281 #ifdef DEBUG_KEYBOARD 289 #ifdef DEBUG_KEYBOARD
282 fprintf(stderr, "Current VT: %d\n", current_vt); 290 fprintf (stderr, "Current VT: %d\n", current_vt);
283 #endif 291 #endif
284 saved_kbd_mode = -1; 292 saved_kbd_mode = -1;
285 293
286 /* Make sure that our input is a console terminal */ 294 /* Make sure that our input is a console terminal */
287 { int dummy; 295 {
288 if ( ioctl(keyboard_fd, KDGKBMODE, &dummy) < 0 ) { 296 int dummy;
289 close(keyboard_fd); 297 if (ioctl (keyboard_fd, KDGKBMODE, &dummy) < 0) {
290 keyboard_fd = -1; 298 close (keyboard_fd);
291 SDL_SetError("Unable to open a console terminal"); 299 keyboard_fd = -1;
292 } 300 SDL_SetError ("Unable to open a console terminal");
293 } 301 }
294 302 }
295 /* Set up keymap */ 303
296 GS_vgainitkeymaps(keyboard_fd); 304 /* Set up keymap */
297 } 305 GS_vgainitkeymaps (keyboard_fd);
298 return(keyboard_fd); 306 }
299 } 307 return (keyboard_fd);
300 308 }
301 static enum { 309
302 MOUSE_NONE = -1, 310 static enum
303 MOUSE_GPM, /* Note: GPM uses the MSC protocol */ 311 {
304 MOUSE_PS2, 312 MOUSE_NONE = -1,
305 MOUSE_IMPS2, 313 MOUSE_GPM, /* Note: GPM uses the MSC protocol */
306 MOUSE_MS, 314 MOUSE_PS2,
307 MOUSE_BM, 315 MOUSE_IMPS2,
308 NUM_MOUSE_DRVS 316 MOUSE_MS,
317 MOUSE_BM,
318 NUM_MOUSE_DRVS
309 } mouse_drv = MOUSE_NONE; 319 } mouse_drv = MOUSE_NONE;
310 320
311 void GS_CloseMouse(_THIS) 321 void
312 { 322 GS_CloseMouse (_THIS)
313 if ( mouse_fd > 0 ) { 323 {
314 close(mouse_fd); 324 if (mouse_fd > 0) {
315 } 325 close (mouse_fd);
316 mouse_fd = -1; 326 }
327 mouse_fd = -1;
317 } 328 }
318 329
319 /* Returns processes listed in /proc with the desired name */ 330 /* Returns processes listed in /proc with the desired name */
320 static int find_pid(DIR *proc, const char *wanted_name) 331 static int
321 { 332 find_pid (DIR * proc, const char *wanted_name)
322 struct dirent *entry; 333 {
323 int pid; 334 struct dirent *entry;
324 335 int pid;
325 /* First scan proc for the gpm process */ 336
326 pid = 0; 337 /* First scan proc for the gpm process */
327 while ( (pid == 0) && ((entry=readdir(proc)) != NULL) ) { 338 pid = 0;
328 if ( isdigit(entry->d_name[0]) ) { 339 while ((pid == 0) && ((entry = readdir (proc)) != NULL)) {
329 FILE *status; 340 if (isdigit (entry->d_name[0])) {
330 char path[PATH_MAX]; 341 FILE *status;
331 char name[PATH_MAX]; 342 char path[PATH_MAX];
332 343 char name[PATH_MAX];
333 SDL_snprintf(path, SDL_arraysize(path), "/proc/%s/status", entry->d_name); 344
334 status=fopen(path, "r"); 345 SDL_snprintf (path, SDL_arraysize (path), "/proc/%s/status",
335 if ( status ) { 346 entry->d_name);
336 name[0] = '\0'; 347 status = fopen (path, "r");
337 fscanf(status, "Name: %s", name); 348 if (status) {
338 if ( SDL_strcmp(name, wanted_name) == 0 ) { 349 name[0] = '\0';
339 pid = atoi(entry->d_name); 350 fscanf (status, "Name: %s", name);
340 } 351 if (SDL_strcmp (name, wanted_name) == 0) {
341 fclose(status); 352 pid = atoi (entry->d_name);
342 } 353 }
343 } 354 fclose (status);
344 } 355 }
345 return pid; 356 }
357 }
358 return pid;
346 } 359 }
347 360
348 /* Returns true if /dev/gpmdata is being written to by gpm */ 361 /* Returns true if /dev/gpmdata is being written to by gpm */
349 static int gpm_available(void) 362 static int
350 { 363 gpm_available (void)
351 int available; 364 {
352 DIR *proc; 365 int available;
353 int pid; 366 DIR *proc;
354 int cmdline, len, arglen; 367 int pid;
355 char path[PATH_MAX]; 368 int cmdline, len, arglen;
356 char args[PATH_MAX], *arg; 369 char path[PATH_MAX];
357 370 char args[PATH_MAX], *arg;
358 /* Don't bother looking if the fifo isn't there */ 371
359 if ( access(GPM_NODE_FIFO, F_OK) < 0 ) { 372 /* Don't bother looking if the fifo isn't there */
360 return(0); 373 if (access (GPM_NODE_FIFO, F_OK) < 0) {
361 } 374 return (0);
362 375 }
363 available = 0; 376
364 proc = opendir("/proc"); 377 available = 0;
365 if ( proc ) { 378 proc = opendir ("/proc");
366 while ( (pid=find_pid(proc, "gpm")) > 0 ) { 379 if (proc) {
367 SDL_snprintf(path, SDL_arraysize(path), "/proc/%d/cmdline", pid); 380 while ((pid = find_pid (proc, "gpm")) > 0) {
368 cmdline = open(path, O_RDONLY, 0); 381 SDL_snprintf (path, SDL_arraysize (path), "/proc/%d/cmdline",
369 if ( cmdline >= 0 ) { 382 pid);
370 len = read(cmdline, args, sizeof(args)); 383 cmdline = open (path, O_RDONLY, 0);
371 arg = args; 384 if (cmdline >= 0) {
372 while ( len > 0 ) { 385 len = read (cmdline, args, sizeof (args));
373 if ( SDL_strcmp(arg, "-R") == 0 ) { 386 arg = args;
374 available = 1; 387 while (len > 0) {
375 } 388 if (SDL_strcmp (arg, "-R") == 0) {
376 arglen = SDL_strlen(arg)+1; 389 available = 1;
377 len -= arglen; 390 }
378 arg += arglen; 391 arglen = SDL_strlen (arg) + 1;
379 } 392 len -= arglen;
380 close(cmdline); 393 arg += arglen;
381 } 394 }
382 } 395 close (cmdline);
383 closedir(proc); 396 }
384 } 397 }
385 return available; 398 closedir (proc);
399 }
400 return available;
386 } 401 }
387 402
388 403
389 /* rcg06112001 Set up IMPS/2 mode, if possible. This gives 404 /* rcg06112001 Set up IMPS/2 mode, if possible. This gives
390 * us access to the mousewheel, etc. Returns zero if 405 * us access to the mousewheel, etc. Returns zero if
391 * writes to device failed, but you still need to query the 406 * writes to device failed, but you still need to query the
392 * device to see which mode it's actually in. 407 * device to see which mode it's actually in.
393 */ 408 */
394 static int set_imps2_mode(int fd) 409 static int
395 { 410 set_imps2_mode (int fd)
396 /* If you wanted to control the mouse mode (and we do :) ) ... 411 {
397 Set IMPS/2 protocol: 412 /* If you wanted to control the mouse mode (and we do :) ) ...
398 {0xf3,200,0xf3,100,0xf3,80} 413 Set IMPS/2 protocol:
399 Reset mouse device: 414 {0xf3,200,0xf3,100,0xf3,80}
400 {0xFF} 415 Reset mouse device:
401 */ 416 {0xFF}
402 Uint8 set_imps2[] = {0xf3, 200, 0xf3, 100, 0xf3, 80}; 417 */
403 Uint8 reset = 0xff; 418 Uint8 set_imps2[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
404 fd_set fdset; 419 Uint8 reset = 0xff;
405 struct timeval tv; 420 fd_set fdset;
406 int retval = 0; 421 struct timeval tv;
407 422 int retval = 0;
408 if ( write(fd, &set_imps2, sizeof(set_imps2)) == sizeof(set_imps2) ) { 423
409 if (write(fd, &reset, sizeof (reset)) == sizeof (reset) ) { 424 if (write (fd, &set_imps2, sizeof (set_imps2)) == sizeof (set_imps2)) {
410 retval = 1; 425 if (write (fd, &reset, sizeof (reset)) == sizeof (reset)) {
411 } 426 retval = 1;
412 } 427 }
413 428 }
414 /* Get rid of any chatter from the above */ 429
415 FD_ZERO(&fdset); 430 /* Get rid of any chatter from the above */
416 FD_SET(fd, &fdset); 431 FD_ZERO (&fdset);
417 tv.tv_sec = 0; 432 FD_SET (fd, &fdset);
418 tv.tv_usec = 0; 433 tv.tv_sec = 0;
419 while ( select(fd+1, &fdset, 0, 0, &tv) > 0 ) { 434 tv.tv_usec = 0;
420 char temp[32]; 435 while (select (fd + 1, &fdset, 0, 0, &tv) > 0) {
421 read(fd, temp, sizeof(temp)); 436 char temp[32];
422 } 437 read (fd, temp, sizeof (temp));
423 438 }
424 return retval; 439
440 return retval;
425 } 441 }
426 442
427 443
428 /* Returns true if the mouse uses the IMPS/2 protocol */ 444 /* Returns true if the mouse uses the IMPS/2 protocol */
429 static int detect_imps2(int fd) 445 static int
430 { 446 detect_imps2 (int fd)
431 int imps2; 447 {
432 448 int imps2;
433 imps2 = 0; 449
434 450 imps2 = 0;
435 if ( SDL_getenv("SDL_MOUSEDEV_IMPS2") ) { 451
436 imps2 = 1; 452 if (SDL_getenv ("SDL_MOUSEDEV_IMPS2")) {
437 } 453 imps2 = 1;
438 if ( ! imps2 ) { 454 }
439 Uint8 query_ps2 = 0xF2; 455 if (!imps2) {
440 fd_set fdset; 456 Uint8 query_ps2 = 0xF2;
441 struct timeval tv; 457 fd_set fdset;
442 458 struct timeval tv;
443 /* Get rid of any mouse motion noise */ 459
444 FD_ZERO(&fdset); 460 /* Get rid of any mouse motion noise */
445 FD_SET(fd, &fdset); 461 FD_ZERO (&fdset);
446 tv.tv_sec = 0; 462 FD_SET (fd, &fdset);
447 tv.tv_usec = 0; 463 tv.tv_sec = 0;
448 while ( select(fd+1, &fdset, 0, 0, &tv) > 0 ) { 464 tv.tv_usec = 0;
449 char temp[32]; 465 while (select (fd + 1, &fdset, 0, 0, &tv) > 0) {
450 read(fd, temp, sizeof(temp)); 466 char temp[32];
451 } 467 read (fd, temp, sizeof (temp));
452 468 }
453 /* Query for the type of mouse protocol */ 469
454 if ( write(fd, &query_ps2, sizeof (query_ps2)) == sizeof (query_ps2)) { 470 /* Query for the type of mouse protocol */
455 Uint8 ch = 0; 471 if (write (fd, &query_ps2, sizeof (query_ps2)) == sizeof (query_ps2)) {
456 472 Uint8 ch = 0;
457 /* Get the mouse protocol response */ 473
458 do { 474 /* Get the mouse protocol response */
459 FD_ZERO(&fdset); 475 do {
460 FD_SET(fd, &fdset); 476 FD_ZERO (&fdset);
461 tv.tv_sec = 1; 477 FD_SET (fd, &fdset);
462 tv.tv_usec = 0; 478 tv.tv_sec = 1;
463 if ( select(fd+1, &fdset, 0, 0, &tv) < 1 ) { 479 tv.tv_usec = 0;
464 break; 480 if (select (fd + 1, &fdset, 0, 0, &tv) < 1) {
465 } 481 break;
466 } while ( (read(fd, &ch, sizeof (ch)) == sizeof (ch)) && 482 }
467 ((ch == 0xFA) || (ch == 0xAA)) ); 483 }
468 484 while ((read (fd, &ch, sizeof (ch)) == sizeof (ch)) &&
469 /* Experimental values (Logitech wheelmouse) */ 485 ((ch == 0xFA) || (ch == 0xAA)));
486
487 /* Experimental values (Logitech wheelmouse) */
470 #ifdef DEBUG_MOUSE 488 #ifdef DEBUG_MOUSE
471 fprintf(stderr, "Last mouse mode: 0x%x\n", ch); 489 fprintf (stderr, "Last mouse mode: 0x%x\n", ch);
472 #endif 490 #endif
473 if ( ch == 3 ) { 491 if (ch == 3) {
474 imps2 = 1; 492 imps2 = 1;
475 } 493 }
476 } 494 }
477 } 495 }
478 return imps2; 496 return imps2;
479 } 497 }
480 498
481 int GS_OpenMouse(_THIS) 499 int
482 { 500 GS_OpenMouse (_THIS)
483 int i; 501 {
484 const char *mousedev; 502 int i;
485 const char *mousedrv; 503 const char *mousedev;
486 504 const char *mousedrv;
487 mousedrv = SDL_getenv("SDL_MOUSEDRV"); 505
488 mousedev = SDL_getenv("SDL_MOUSEDEV"); 506 mousedrv = SDL_getenv ("SDL_MOUSEDRV");
489 mouse_fd = -1; 507 mousedev = SDL_getenv ("SDL_MOUSEDEV");
490 508 mouse_fd = -1;
491 /* STD MICE */ 509
492 510 /* STD MICE */
493 if ( mousedev == NULL ) { 511
494 /* FIXME someday... allow multiple mice in this driver */ 512 if (mousedev == NULL) {
495 char *ps2mice[] = { 513 /* FIXME someday... allow multiple mice in this driver */
496 "/dev/input/mice", "/dev/usbmouse", "/dev/psaux", NULL 514 char *ps2mice[] = {
497 }; 515 "/dev/input/mice", "/dev/usbmouse", "/dev/psaux", NULL
498 /* First try to use GPM in repeater mode */ 516 };
499 if ( mouse_fd < 0 ) { 517 /* First try to use GPM in repeater mode */
500 if ( gpm_available() ) { 518 if (mouse_fd < 0) {
501 mouse_fd = open(GPM_NODE_FIFO, O_RDONLY, 0); 519 if (gpm_available ()) {
502 if ( mouse_fd >= 0 ) { 520 mouse_fd = open (GPM_NODE_FIFO, O_RDONLY, 0);
521 if (mouse_fd >= 0) {
503 #ifdef DEBUG_MOUSE 522 #ifdef DEBUG_MOUSE
504 fprintf(stderr, "Using GPM mouse\n"); 523 fprintf (stderr, "Using GPM mouse\n");
505 #endif 524 #endif
506 mouse_drv = MOUSE_GPM; 525 mouse_drv = MOUSE_GPM;
507 } 526 }
508 } 527 }
509 } 528 }
510 /* Now try to use a modern PS/2 mouse */ 529 /* Now try to use a modern PS/2 mouse */
511 for ( i=0; (mouse_fd < 0) && ps2mice[i]; ++i ) { 530 for (i = 0; (mouse_fd < 0) && ps2mice[i]; ++i) {
512 mouse_fd = open(ps2mice[i], O_RDWR, 0); 531 mouse_fd = open (ps2mice[i], O_RDWR, 0);
513 if (mouse_fd < 0) { 532 if (mouse_fd < 0) {
514 mouse_fd = open(ps2mice[i], O_RDONLY, 0); 533 mouse_fd = open (ps2mice[i], O_RDONLY, 0);
515 } 534 }
516 if (mouse_fd >= 0) { 535 if (mouse_fd >= 0) {
517 /* rcg06112001 Attempt to set IMPS/2 mode */ 536 /* rcg06112001 Attempt to set IMPS/2 mode */
518 if ( i == 0 ) { 537 if (i == 0) {
519 set_imps2_mode(mouse_fd); 538 set_imps2_mode (mouse_fd);
520 } 539 }
521 if (detect_imps2(mouse_fd)) { 540 if (detect_imps2 (mouse_fd)) {
522 #ifdef DEBUG_MOUSE 541 #ifdef DEBUG_MOUSE
523 fprintf(stderr, "Using IMPS2 mouse\n"); 542 fprintf (stderr, "Using IMPS2 mouse\n");
524 #endif 543 #endif
525 mouse_drv = MOUSE_IMPS2; 544 mouse_drv = MOUSE_IMPS2;
526 } else { 545 } else {
527 mouse_drv = MOUSE_PS2; 546 mouse_drv = MOUSE_PS2;
528 #ifdef DEBUG_MOUSE 547 #ifdef DEBUG_MOUSE
529 fprintf(stderr, "Using PS2 mouse\n"); 548 fprintf (stderr, "Using PS2 mouse\n");
530 #endif 549 #endif
531 } 550 }
532 } 551 }
533 } 552 }
534 /* Next try to use a PPC ADB port mouse */ 553 /* Next try to use a PPC ADB port mouse */
535 if ( mouse_fd < 0 ) { 554 if (mouse_fd < 0) {
536 mouse_fd = open("/dev/adbmouse", O_RDONLY, 0); 555 mouse_fd = open ("/dev/adbmouse", O_RDONLY, 0);
537 if ( mouse_fd >= 0 ) { 556 if (mouse_fd >= 0) {
538 #ifdef DEBUG_MOUSE 557 #ifdef DEBUG_MOUSE
539 fprintf(stderr, "Using ADB mouse\n"); 558 fprintf (stderr, "Using ADB mouse\n");
540 #endif 559 #endif
541 mouse_drv = MOUSE_BM; 560 mouse_drv = MOUSE_BM;
542 } 561 }
543 } 562 }
544 } 563 }
545 /* Default to a serial Microsoft mouse */ 564 /* Default to a serial Microsoft mouse */
546 if ( mouse_fd < 0 ) { 565 if (mouse_fd < 0) {
547 if ( mousedev == NULL ) { 566 if (mousedev == NULL) {
548 mousedev = "/dev/mouse"; 567 mousedev = "/dev/mouse";
549 } 568 }
550 mouse_fd = open(mousedev, O_RDONLY, 0); 569 mouse_fd = open (mousedev, O_RDONLY, 0);
551 if ( mouse_fd >= 0 ) { 570 if (mouse_fd >= 0) {
552 struct termios mouse_termios; 571 struct termios mouse_termios;
553 572
554 /* Set the sampling speed to 1200 baud */ 573 /* Set the sampling speed to 1200 baud */
555 tcgetattr(mouse_fd, &mouse_termios); 574 tcgetattr (mouse_fd, &mouse_termios);
556 mouse_termios.c_iflag = IGNBRK | IGNPAR; 575 mouse_termios.c_iflag = IGNBRK | IGNPAR;
557 mouse_termios.c_oflag = 0; 576 mouse_termios.c_oflag = 0;
558 mouse_termios.c_lflag = 0; 577 mouse_termios.c_lflag = 0;
559 mouse_termios.c_line = 0; 578 mouse_termios.c_line = 0;
560 mouse_termios.c_cc[VTIME] = 0; 579 mouse_termios.c_cc[VTIME] = 0;
561 mouse_termios.c_cc[VMIN] = 1; 580 mouse_termios.c_cc[VMIN] = 1;
562 mouse_termios.c_cflag = CREAD | CLOCAL | HUPCL; 581 mouse_termios.c_cflag = CREAD | CLOCAL | HUPCL;
563 mouse_termios.c_cflag |= CS8; 582 mouse_termios.c_cflag |= CS8;
564 mouse_termios.c_cflag |= B1200; 583 mouse_termios.c_cflag |= B1200;
565 tcsetattr(mouse_fd, TCSAFLUSH, &mouse_termios); 584 tcsetattr (mouse_fd, TCSAFLUSH, &mouse_termios);
566 #ifdef DEBUG_MOUSE 585 #ifdef DEBUG_MOUSE
567 fprintf(stderr, "Using Microsoft mouse on %s\n", mousedev); 586 fprintf (stderr, "Using Microsoft mouse on %s\n", mousedev);
568 #endif 587 #endif
569 mouse_drv = MOUSE_MS; 588 mouse_drv = MOUSE_MS;
570 } 589 }
571 } 590 }
572 if ( mouse_fd < 0 ) { 591 if (mouse_fd < 0) {
573 mouse_drv = MOUSE_NONE; 592 mouse_drv = MOUSE_NONE;
574 } 593 }
575 return(mouse_fd); 594 return (mouse_fd);
576 } 595 }
577 596
578 static int posted = 0; 597 static int posted = 0;
579 598
580 void GS_vgamousecallback(int button, int dx, int dy) 599 void
581 { 600 GS_vgamousecallback (int button, int dx, int dy)
582 int button_1, button_3; 601 {
583 int button_state; 602 int button_1, button_3;
584 int state_changed; 603 int button_state;
585 int i; 604 int state_changed;
586 Uint8 state; 605 int i;
587 606 Uint8 state;
588 if ( dx || dy ) { 607
589 posted += SDL_PrivateMouseMotion(0, 1, dx, dy); 608 if (dx || dy) {
590 } 609 posted += SDL_PrivateMouseMotion (0, 1, dx, dy);
591 610 }
592 /* Swap button 1 and 3 */ 611
593 button_1 = (button & 0x04) >> 2; 612 /* Swap button 1 and 3 */
594 button_3 = (button & 0x01) << 2; 613 button_1 = (button & 0x04) >> 2;
595 button &= ~0x05; 614 button_3 = (button & 0x01) << 2;
596 button |= (button_1|button_3); 615 button &= ~0x05;
597 616 button |= (button_1 | button_3);
598 /* See what changed */ 617
599 button_state = SDL_GetMouseState(NULL, NULL); 618 /* See what changed */
600 state_changed = button_state ^ button; 619 button_state = SDL_GetMouseState (NULL, NULL);
601 for ( i=0; i<8; ++i ) { 620 state_changed = button_state ^ button;
602 if ( state_changed & (1<<i) ) { 621 for (i = 0; i < 8; ++i) {
603 if ( button & (1<<i) ) { 622 if (state_changed & (1 << i)) {
604 state = SDL_PRESSED; 623 if (button & (1 << i)) {
605 } else { 624 state = SDL_PRESSED;
606 state = SDL_RELEASED; 625 } else {
607 } 626 state = SDL_RELEASED;
608 posted += SDL_PrivateMouseButton(state, i+1, 0, 0); 627 }
609 } 628 posted += SDL_PrivateMouseButton (state, i + 1, 0, 0);
610 } 629 }
630 }
611 } 631 }
612 632
613 /* For now, use GPM, PS/2, and MS protocols 633 /* For now, use GPM, PS/2, and MS protocols
614 Driver adapted from the SVGAlib mouse driver code (taken from gpm, etc.) 634 Driver adapted from the SVGAlib mouse driver code (taken from gpm, etc.)
615 */ 635 */
616 static void handle_mouse(_THIS) 636 static void
617 { 637 handle_mouse (_THIS)
618 static int start = 0; 638 {
619 static unsigned char mousebuf[BUFSIZ]; 639 static int start = 0;
620 int i, nread; 640 static unsigned char mousebuf[BUFSIZ];
621 int button = 0; 641 int i, nread;
622 int dx = 0, dy = 0; 642 int button = 0;
623 int packetsize = 0; 643 int dx = 0, dy = 0;
624 644 int packetsize = 0;
625 /* Figure out the mouse packet size */ 645
626 switch (mouse_drv) { 646 /* Figure out the mouse packet size */
627 case MOUSE_NONE: 647 switch (mouse_drv) {
628 /* Ack! */ 648 case MOUSE_NONE:
629 read(mouse_fd, mousebuf, BUFSIZ); 649 /* Ack! */
630 return; 650 read (mouse_fd, mousebuf, BUFSIZ);
631 case MOUSE_GPM: 651 return;
632 packetsize = 5; 652 case MOUSE_GPM:
633 break; 653 packetsize = 5;
634 case MOUSE_IMPS2: 654 break;
635 packetsize = 4; 655 case MOUSE_IMPS2:
636 break; 656 packetsize = 4;
637 case MOUSE_PS2: 657 break;
638 case MOUSE_MS: 658 case MOUSE_PS2:
639 case MOUSE_BM: 659 case MOUSE_MS:
640 packetsize = 3; 660 case MOUSE_BM:
641 break; 661 packetsize = 3;
642 case NUM_MOUSE_DRVS: 662 break;
643 /* Uh oh.. */ 663 case NUM_MOUSE_DRVS:
644 packetsize = 0; 664 /* Uh oh.. */
645 break; 665 packetsize = 0;
646 } 666 break;
647 667 }
648 /* Read as many packets as possible */ 668
649 nread = read(mouse_fd, &mousebuf[start], BUFSIZ-start); 669 /* Read as many packets as possible */
650 if ( nread < 0 ) { 670 nread = read (mouse_fd, &mousebuf[start], BUFSIZ - start);
651 return; 671 if (nread < 0) {
652 } 672 return;
653 nread += start; 673 }
674 nread += start;
654 #ifdef DEBUG_MOUSE 675 #ifdef DEBUG_MOUSE
655 fprintf(stderr, "Read %d bytes from mouse, start = %d\n", nread, start); 676 fprintf (stderr, "Read %d bytes from mouse, start = %d\n", nread, start);
656 #endif 677 #endif
657 for ( i=0; i<(nread-(packetsize-1)); i += packetsize ) { 678 for (i = 0; i < (nread - (packetsize - 1)); i += packetsize) {
658 switch (mouse_drv) { 679 switch (mouse_drv) {
659 case MOUSE_NONE: 680 case MOUSE_NONE:
660 break; 681 break;
661 case MOUSE_GPM: 682 case MOUSE_GPM:
662 /* GPM protocol has 0x80 in high byte */ 683 /* GPM protocol has 0x80 in high byte */
663 if ( (mousebuf[i] & 0xF8) != 0x80 ) { 684 if ((mousebuf[i] & 0xF8) != 0x80) {
664 /* Go to next byte */ 685 /* Go to next byte */
665 i -= (packetsize-1); 686 i -= (packetsize - 1);
666 continue; 687 continue;
667 } 688 }
668 /* Get current mouse state */ 689 /* Get current mouse state */
669 button = (~mousebuf[i]) & 0x07; 690 button = (~mousebuf[i]) & 0x07;
670 dx = (signed char)(mousebuf[i+1]) + 691 dx = (signed char) (mousebuf[i + 1]) +
671 (signed char)(mousebuf[i+3]); 692 (signed char) (mousebuf[i + 3]);
672 dy = -((signed char)(mousebuf[i+2]) + 693 dy = -((signed char) (mousebuf[i + 2]) +
673 (signed char)(mousebuf[i+4])); 694 (signed char) (mousebuf[i + 4]));
674 break; 695 break;
675 case MOUSE_PS2: 696 case MOUSE_PS2:
676 /* PS/2 protocol has nothing in high byte */ 697 /* PS/2 protocol has nothing in high byte */
677 if ( (mousebuf[i] & 0xC0) != 0 ) { 698 if ((mousebuf[i] & 0xC0) != 0) {
678 /* Go to next byte */ 699 /* Go to next byte */
679 i -= (packetsize-1); 700 i -= (packetsize - 1);
680 continue; 701 continue;
681 } 702 }
682 /* Get current mouse state */ 703 /* Get current mouse state */
683 button = (mousebuf[i] & 0x04) >> 1 | /*Middle*/ 704 button = (mousebuf[i] & 0x04) >> 1 | /*Middle */
684 (mousebuf[i] & 0x02) >> 1 | /*Right*/ 705 (mousebuf[i] & 0x02) >> 1 | /*Right */
685 (mousebuf[i] & 0x01) << 2; /*Left*/ 706 (mousebuf[i] & 0x01) << 2; /*Left */
686 dx = (mousebuf[i] & 0x10) ? 707 dx = (mousebuf[i] & 0x10) ?
687 mousebuf[i+1] - 256 : mousebuf[i+1]; 708 mousebuf[i + 1] - 256 : mousebuf[i + 1];
688 dy = (mousebuf[i] & 0x20) ? 709 dy = (mousebuf[i] & 0x20) ?
689 -(mousebuf[i+2] - 256) : -mousebuf[i+2]; 710 -(mousebuf[i + 2] - 256) : -mousebuf[i + 2];
690 break; 711 break;
691 case MOUSE_IMPS2: 712 case MOUSE_IMPS2:
692 /* Get current mouse state */ 713 /* Get current mouse state */
693 button = (mousebuf[i] & 0x04) >> 1 | /*Middle*/ 714 button = (mousebuf[i] & 0x04) >> 1 | /*Middle */
694 (mousebuf[i] & 0x02) >> 1 | /*Right*/ 715 (mousebuf[i] & 0x02) >> 1 | /*Right */
695 (mousebuf[i] & 0x01) << 2 | /*Left*/ 716 (mousebuf[i] & 0x01) << 2 | /*Left */
696 (mousebuf[i] & 0x40) >> 3 | /* 4 */ 717 (mousebuf[i] & 0x40) >> 3 | /* 4 */
697 (mousebuf[i] & 0x80) >> 3; /* 5 */ 718 (mousebuf[i] & 0x80) >> 3; /* 5 */
698 dx = (mousebuf[i] & 0x10) ? 719 dx = (mousebuf[i] & 0x10) ?
699 mousebuf[i+1] - 256 : mousebuf[i+1]; 720 mousebuf[i + 1] - 256 : mousebuf[i + 1];
700 dy = (mousebuf[i] & 0x20) ? 721 dy = (mousebuf[i] & 0x20) ?
701 -(mousebuf[i+2] - 256) : -mousebuf[i+2]; 722 -(mousebuf[i + 2] - 256) : -mousebuf[i + 2];
702 switch (mousebuf[i+3]&0x0F) { 723 switch (mousebuf[i + 3] & 0x0F) {
703 case 0x0E: /* DX = +1 */ 724 case 0x0E: /* DX = +1 */
704 case 0x02: /* DX = -1 */ 725 case 0x02: /* DX = -1 */
705 break; 726 break;
706 case 0x0F: /* DY = +1 (map button 4) */ 727 case 0x0F: /* DY = +1 (map button 4) */
707 FB_vgamousecallback(button | (1<<3), 728 FB_vgamousecallback (button | (1 << 3), 1, 0, 0);
708 1, 0, 0); 729 break;
709 break; 730 case 0x01: /* DY = -1 (map button 5) */
710 case 0x01: /* DY = -1 (map button 5) */ 731 FB_vgamousecallback (button | (1 << 4), 1, 0, 0);
711 FB_vgamousecallback(button | (1<<4), 732 break;
712 1, 0, 0); 733 }
713 break; 734 break;
714 } 735 case MOUSE_MS:
715 break; 736 /* Microsoft protocol has 0x40 in high byte */
716 case MOUSE_MS: 737 if ((mousebuf[i] & 0x40) != 0x40) {
717 /* Microsoft protocol has 0x40 in high byte */ 738 /* Go to next byte */
718 if ( (mousebuf[i] & 0x40) != 0x40 ) { 739 i -= (packetsize - 1);
719 /* Go to next byte */ 740 continue;
720 i -= (packetsize-1); 741 }
721 continue; 742 /* Get current mouse state */
722 } 743 button = ((mousebuf[i] & 0x20) >> 3) |
723 /* Get current mouse state */ 744 ((mousebuf[i] & 0x10) >> 4);
724 button = ((mousebuf[i] & 0x20) >> 3) | 745 dx = (signed char) (((mousebuf[i] & 0x03) << 6) |
725 ((mousebuf[i] & 0x10) >> 4); 746 (mousebuf[i + 1] & 0x3F));
726 dx = (signed char)(((mousebuf[i] & 0x03) << 6) | 747 dy = (signed char) (((mousebuf[i] & 0x0C) << 4) |
727 (mousebuf[i + 1] & 0x3F)); 748 (mousebuf[i + 2] & 0x3F));
728 dy = (signed char)(((mousebuf[i] & 0x0C) << 4) | 749 break;
729 (mousebuf[i + 2] & 0x3F)); 750 case MOUSE_BM:
730 break; 751 /* BusMouse protocol has 0xF8 in high byte */
731 case MOUSE_BM: 752 if ((mousebuf[i] & 0xF8) != 0x80) {
732 /* BusMouse protocol has 0xF8 in high byte */ 753 /* Go to next byte */
733 if ( (mousebuf[i] & 0xF8) != 0x80 ) { 754 i -= (packetsize - 1);
734 /* Go to next byte */ 755 continue;
735 i -= (packetsize-1); 756 }
736 continue; 757 /* Get current mouse state */
737 } 758 button = (~mousebuf[i]) & 0x07;
738 /* Get current mouse state */ 759 dx = (signed char) mousebuf[i + 1];
739 button = (~mousebuf[i]) & 0x07; 760 dy = -(signed char) mousebuf[i + 2];
740 dx = (signed char)mousebuf[i+1]; 761 break;
741 dy = -(signed char)mousebuf[i+2]; 762 case NUM_MOUSE_DRVS:
742 break; 763 /* Uh oh.. */
743 case NUM_MOUSE_DRVS: 764 dx = 0;
744 /* Uh oh.. */ 765 dy = 0;
745 dx = 0; 766 break;
746 dy = 0; 767 }
747 break; 768 GS_vgamousecallback (button, dx, dy);
748 } 769 }
749 GS_vgamousecallback(button, dx, dy); 770 if (i < nread) {
750 } 771 SDL_memcpy (mousebuf, &mousebuf[i], (nread - i));
751 if ( i < nread ) { 772 start = (nread - i);
752 SDL_memcpy(mousebuf, &mousebuf[i], (nread-i)); 773 } else {
753 start = (nread-i); 774 start = 0;
754 } else { 775 }
755 start = 0; 776 return;
756 } 777 }
757 return; 778
758 } 779 static void
759 780 handle_keyboard (_THIS)
760 static void handle_keyboard(_THIS) 781 {
761 { 782 unsigned char keybuf[BUFSIZ];
762 unsigned char keybuf[BUFSIZ]; 783 int i, nread;
763 int i, nread; 784 int pressed;
764 int pressed; 785 int scancode;
765 int scancode; 786 SDL_keysym keysym;
766 SDL_keysym keysym; 787
767 788 nread = read (keyboard_fd, keybuf, BUFSIZ);
768 nread = read(keyboard_fd, keybuf, BUFSIZ); 789 for (i = 0; i < nread; ++i) {
769 for ( i=0; i<nread; ++i ) { 790 scancode = keybuf[i] & 0x7F;
770 scancode = keybuf[i] & 0x7F; 791 if (keybuf[i] & 0x80) {
771 if ( keybuf[i] & 0x80 ) { 792 pressed = SDL_RELEASED;
772 pressed = SDL_RELEASED; 793 } else {
773 } else { 794 pressed = SDL_PRESSED;
774 pressed = SDL_PRESSED; 795 }
775 } 796 TranslateKey (scancode, &keysym);
776 TranslateKey(scancode, &keysym); 797 posted += SDL_PrivateKeyboard (pressed, &keysym);
777 posted += SDL_PrivateKeyboard(pressed, &keysym); 798 }
778 } 799 }
779 } 800
780 801 void
781 void GS_PumpEvents(_THIS) 802 GS_PumpEvents (_THIS)
782 { 803 {
783 fd_set fdset; 804 fd_set fdset;
784 int max_fd; 805 int max_fd;
785 static struct timeval zero; 806 static struct timeval zero;
786 807
787 do { 808 do {
788 posted = 0; 809 posted = 0;
789 810
790 FD_ZERO(&fdset); 811 FD_ZERO (&fdset);
791 max_fd = 0; 812 max_fd = 0;
792 if ( keyboard_fd >= 0 ) { 813 if (keyboard_fd >= 0) {
793 FD_SET(keyboard_fd, &fdset); 814 FD_SET (keyboard_fd, &fdset);
794 if ( max_fd < keyboard_fd ) { 815 if (max_fd < keyboard_fd) {
795 max_fd = keyboard_fd; 816 max_fd = keyboard_fd;
796 } 817 }
797 } 818 }
798 if ( mouse_fd >= 0 ) { 819 if (mouse_fd >= 0) {
799 FD_SET(mouse_fd, &fdset); 820 FD_SET (mouse_fd, &fdset);
800 if ( max_fd < mouse_fd ) { 821 if (max_fd < mouse_fd) {
801 max_fd = mouse_fd; 822 max_fd = mouse_fd;
802 } 823 }
803 } 824 }
804 if ( select(max_fd+1, &fdset, NULL, NULL, &zero) > 0 ) { 825 if (select (max_fd + 1, &fdset, NULL, NULL, &zero) > 0) {
805 if ( keyboard_fd >= 0 ) { 826 if (keyboard_fd >= 0) {
806 if ( FD_ISSET(keyboard_fd, &fdset) ) { 827 if (FD_ISSET (keyboard_fd, &fdset)) {
807 handle_keyboard(this); 828 handle_keyboard (this);
808 } 829 }
809 } 830 }
810 if ( mouse_fd >= 0 ) { 831 if (mouse_fd >= 0) {
811 if ( FD_ISSET(mouse_fd, &fdset) ) { 832 if (FD_ISSET (mouse_fd, &fdset)) {
812 handle_mouse(this); 833 handle_mouse (this);
813 } 834 }
814 } 835 }
815 } 836 }
816 } while ( posted ); 837 }
817 } 838 while (posted);
818 839 }
819 void GS_InitOSKeymap(_THIS) 840
820 { 841 void
821 int i; 842 GS_InitOSKeymap (_THIS)
822 843 {
823 /* Initialize the Linux key translation table */ 844 int i;
824 845
825 /* First get the ascii keys and others not well handled */ 846 /* Initialize the Linux key translation table */
826 for (i=0; i<SDL_arraysize(keymap); ++i) { 847
827 switch(i) { 848 /* First get the ascii keys and others not well handled */
828 /* These aren't handled by the x86 kernel keymapping (?) */ 849 for (i = 0; i < SDL_arraysize (keymap); ++i) {
829 case SCANCODE_PRINTSCREEN: 850 switch (i) {
830 keymap[i] = SDLK_PRINT; 851 /* These aren't handled by the x86 kernel keymapping (?) */
831 break; 852 case SCANCODE_PRINTSCREEN:
832 case SCANCODE_BREAK: 853 keymap[i] = SDLK_PRINT;
833 keymap[i] = SDLK_BREAK; 854 break;
834 break; 855 case SCANCODE_BREAK:
835 case SCANCODE_BREAK_ALTERNATIVE: 856 keymap[i] = SDLK_BREAK;
836 keymap[i] = SDLK_PAUSE; 857 break;
837 break; 858 case SCANCODE_BREAK_ALTERNATIVE:
838 case SCANCODE_LEFTSHIFT: 859 keymap[i] = SDLK_PAUSE;
839 keymap[i] = SDLK_LSHIFT; 860 break;
840 break; 861 case SCANCODE_LEFTSHIFT:
841 case SCANCODE_RIGHTSHIFT: 862 keymap[i] = SDLK_LSHIFT;
842 keymap[i] = SDLK_RSHIFT; 863 break;
843 break; 864 case SCANCODE_RIGHTSHIFT:
844 case SCANCODE_LEFTCONTROL: 865 keymap[i] = SDLK_RSHIFT;
845 keymap[i] = SDLK_LCTRL; 866 break;
846 break; 867 case SCANCODE_LEFTCONTROL:
847 case SCANCODE_RIGHTCONTROL: 868 keymap[i] = SDLK_LCTRL;
848 keymap[i] = SDLK_RCTRL; 869 break;
849 break; 870 case SCANCODE_RIGHTCONTROL:
850 case SCANCODE_RIGHTWIN: 871 keymap[i] = SDLK_RCTRL;
851 keymap[i] = SDLK_RSUPER; 872 break;
852 break; 873 case SCANCODE_RIGHTWIN:
853 case SCANCODE_LEFTWIN: 874 keymap[i] = SDLK_RSUPER;
854 keymap[i] = SDLK_LSUPER; 875 break;
855 break; 876 case SCANCODE_LEFTWIN:
856 case 127: 877 keymap[i] = SDLK_LSUPER;
857 keymap[i] = SDLK_MENU; 878 break;
858 break; 879 case 127:
859 /* this should take care of all standard ascii keys */ 880 keymap[i] = SDLK_MENU;
860 default: 881 break;
861 keymap[i] = KVAL(vga_keymap[0][i]); 882 /* this should take care of all standard ascii keys */
862 break; 883 default:
863 } 884 keymap[i] = KVAL (vga_keymap[0][i]);
864 } 885 break;
865 for (i=0; i<SDL_arraysize(keymap); ++i) { 886 }
866 switch(keymap_temp[i]) { 887 }
867 case K_F1: keymap[i] = SDLK_F1; break; 888 for (i = 0; i < SDL_arraysize (keymap); ++i) {
868 case K_F2: keymap[i] = SDLK_F2; break; 889 switch (keymap_temp[i]) {
869 case K_F3: keymap[i] = SDLK_F3; break; 890 case K_F1:
870 case K_F4: keymap[i] = SDLK_F4; break; 891 keymap[i] = SDLK_F1;
871 case K_F5: keymap[i] = SDLK_F5; break; 892 break;
872 case K_F6: keymap[i] = SDLK_F6; break; 893 case K_F2:
873 case K_F7: keymap[i] = SDLK_F7; break; 894 keymap[i] = SDLK_F2;
874 case K_F8: keymap[i] = SDLK_F8; break; 895 break;
875 case K_F9: keymap[i] = SDLK_F9; break; 896 case K_F3:
876 case K_F10: keymap[i] = SDLK_F10; break; 897 keymap[i] = SDLK_F3;
877 case K_F11: keymap[i] = SDLK_F11; break; 898 break;
878 case K_F12: keymap[i] = SDLK_F12; break; 899 case K_F4:
879 900 keymap[i] = SDLK_F4;
880 case K_DOWN: keymap[i] = SDLK_DOWN; break; 901 break;
881 case K_LEFT: keymap[i] = SDLK_LEFT; break; 902 case K_F5:
882 case K_RIGHT: keymap[i] = SDLK_RIGHT; break; 903 keymap[i] = SDLK_F5;
883 case K_UP: keymap[i] = SDLK_UP; break; 904 break;
884 905 case K_F6:
885 case K_P0: keymap[i] = SDLK_KP0; break; 906 keymap[i] = SDLK_F6;
886 case K_P1: keymap[i] = SDLK_KP1; break; 907 break;
887 case K_P2: keymap[i] = SDLK_KP2; break; 908 case K_F7:
888 case K_P3: keymap[i] = SDLK_KP3; break; 909 keymap[i] = SDLK_F7;
889 case K_P4: keymap[i] = SDLK_KP4; break; 910 break;
890 case K_P5: keymap[i] = SDLK_KP5; break; 911 case K_F8:
891 case K_P6: keymap[i] = SDLK_KP6; break; 912 keymap[i] = SDLK_F8;
892 case K_P7: keymap[i] = SDLK_KP7; break; 913 break;
893 case K_P8: keymap[i] = SDLK_KP8; break; 914 case K_F9:
894 case K_P9: keymap[i] = SDLK_KP9; break; 915 keymap[i] = SDLK_F9;
895 case K_PPLUS: keymap[i] = SDLK_KP_PLUS; break; 916 break;
896 case K_PMINUS: keymap[i] = SDLK_KP_MINUS; break; 917 case K_F10:
897 case K_PSTAR: keymap[i] = SDLK_KP_MULTIPLY; break; 918 keymap[i] = SDLK_F10;
898 case K_PSLASH: keymap[i] = SDLK_KP_DIVIDE; break; 919 break;
899 case K_PENTER: keymap[i] = SDLK_KP_ENTER; break; 920 case K_F11:
900 case K_PDOT: keymap[i] = SDLK_KP_PERIOD; break; 921 keymap[i] = SDLK_F11;
901 922 break;
902 case K_SHIFT: if ( keymap[i] != SDLK_RSHIFT ) 923 case K_F12:
903 keymap[i] = SDLK_LSHIFT; 924 keymap[i] = SDLK_F12;
904 break; 925 break;
905 case K_SHIFTL: keymap[i] = SDLK_LSHIFT; break; 926
906 case K_SHIFTR: keymap[i] = SDLK_RSHIFT; break; 927 case K_DOWN:
907 case K_CTRL: if ( keymap[i] != SDLK_RCTRL ) 928 keymap[i] = SDLK_DOWN;
908 keymap[i] = SDLK_LCTRL; 929 break;
909 break; 930 case K_LEFT:
910 case K_CTRLL: keymap[i] = SDLK_LCTRL; break; 931 keymap[i] = SDLK_LEFT;
911 case K_CTRLR: keymap[i] = SDLK_RCTRL; break; 932 break;
912 case K_ALT: keymap[i] = SDLK_LALT; break; 933 case K_RIGHT:
913 case K_ALTGR: keymap[i] = SDLK_RALT; break; 934 keymap[i] = SDLK_RIGHT;
914 935 break;
915 case K_INSERT: keymap[i] = SDLK_INSERT; break; 936 case K_UP:
916 case K_REMOVE: keymap[i] = SDLK_DELETE; break; 937 keymap[i] = SDLK_UP;
917 case K_PGUP: keymap[i] = SDLK_PAGEUP; break; 938 break;
918 case K_PGDN: keymap[i] = SDLK_PAGEDOWN; break; 939
919 case K_FIND: keymap[i] = SDLK_HOME; break; 940 case K_P0:
920 case K_SELECT: keymap[i] = SDLK_END; break; 941 keymap[i] = SDLK_KP0;
921 942 break;
922 case K_NUM: keymap[i] = SDLK_NUMLOCK; break; 943 case K_P1:
923 case K_CAPS: keymap[i] = SDLK_CAPSLOCK; break; 944 keymap[i] = SDLK_KP1;
924 945 break;
925 case K_F13: keymap[i] = SDLK_PRINT; break; 946 case K_P2:
926 case K_HOLD: keymap[i] = SDLK_SCROLLOCK; break; 947 keymap[i] = SDLK_KP2;
927 case K_PAUSE: keymap[i] = SDLK_PAUSE; break; 948 break;
928 949 case K_P3:
929 case 127: keymap[i] = SDLK_BACKSPACE; break; 950 keymap[i] = SDLK_KP3;
930 951 break;
931 default: break; 952 case K_P4:
932 } 953 keymap[i] = SDLK_KP4;
933 } 954 break;
934 } 955 case K_P5:
935 956 keymap[i] = SDLK_KP5;
936 static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym) 957 break;
937 { 958 case K_P6:
938 /* Set the keysym information */ 959 keymap[i] = SDLK_KP6;
939 keysym->scancode = scancode; 960 break;
940 keysym->sym = keymap[scancode]; 961 case K_P7:
941 keysym->mod = KMOD_NONE; 962 keymap[i] = SDLK_KP7;
942 963 break;
943 /* If UNICODE is on, get the UNICODE value for the key */ 964 case K_P8:
944 keysym->unicode = 0; 965 keymap[i] = SDLK_KP8;
945 if ( SDL_TranslateUNICODE ) { 966 break;
946 int map; 967 case K_P9:
947 SDLMod modstate; 968 keymap[i] = SDLK_KP9;
948 969 break;
949 modstate = SDL_GetModState(); 970 case K_PPLUS:
950 map = 0; 971 keymap[i] = SDLK_KP_PLUS;
951 if ( modstate & KMOD_SHIFT ) { 972 break;
952 map |= (1<<KG_SHIFT); 973 case K_PMINUS:
953 } 974 keymap[i] = SDLK_KP_MINUS;
954 if ( modstate & KMOD_CTRL ) { 975 break;
955 map |= (1<<KG_CTRL); 976 case K_PSTAR:
956 } 977 keymap[i] = SDLK_KP_MULTIPLY;
957 if ( modstate & KMOD_ALT ) { 978 break;
958 map |= (1<<KG_ALT); 979 case K_PSLASH:
959 } 980 keymap[i] = SDLK_KP_DIVIDE;
960 if ( modstate & KMOD_MODE ) { 981 break;
961 map |= (1<<KG_ALTGR); 982 case K_PENTER:
962 } 983 keymap[i] = SDLK_KP_ENTER;
963 if ( KTYP(vga_keymap[map][scancode]) == KT_LETTER ) { 984 break;
964 if ( modstate & KMOD_CAPS ) { 985 case K_PDOT:
965 map ^= (1<<KG_SHIFT); 986 keymap[i] = SDLK_KP_PERIOD;
966 } 987 break;
967 } 988
968 if ( KTYP(vga_keymap[map][scancode]) == KT_PAD ) { 989 case K_SHIFT:
969 if ( modstate & KMOD_NUM ) { 990 if (keymap[i] != SDLK_RSHIFT)
970 keysym->unicode=KVAL(vga_keymap[map][scancode]); 991 keymap[i] = SDLK_LSHIFT;
971 } 992 break;
972 } else { 993 case K_SHIFTL:
973 keysym->unicode = KVAL(vga_keymap[map][scancode]); 994 keymap[i] = SDLK_LSHIFT;
974 } 995 break;
975 } 996 case K_SHIFTR:
976 return(keysym); 997 keymap[i] = SDLK_RSHIFT;
977 } 998 break;
999 case K_CTRL:
1000 if (keymap[i] != SDLK_RCTRL)
1001 keymap[i] = SDLK_LCTRL;
1002 break;
1003 case K_CTRLL:
1004 keymap[i] = SDLK_LCTRL;
1005 break;
1006 case K_CTRLR:
1007 keymap[i] = SDLK_RCTRL;
1008 break;
1009 case K_ALT:
1010 keymap[i] = SDLK_LALT;
1011 break;
1012 case K_ALTGR:
1013 keymap[i] = SDLK_RALT;
1014 break;
1015
1016 case K_INSERT:
1017 keymap[i] = SDLK_INSERT;
1018 break;
1019 case K_REMOVE:
1020 keymap[i] = SDLK_DELETE;
1021 break;
1022 case K_PGUP:
1023 keymap[i] = SDLK_PAGEUP;
1024 break;
1025 case K_PGDN:
1026 keymap[i] = SDLK_PAGEDOWN;
1027 break;
1028 case K_FIND:
1029 keymap[i] = SDLK_HOME;
1030 break;
1031 case K_SELECT:
1032 keymap[i] = SDLK_END;
1033 break;
1034
1035 case K_NUM:
1036 keymap[i] = SDLK_NUMLOCK;
1037 break;
1038 case K_CAPS:
1039 keymap[i] = SDLK_CAPSLOCK;
1040 break;
1041
1042 case K_F13:
1043 keymap[i] = SDLK_PRINT;
1044 break;
1045 case K_HOLD:
1046 keymap[i] = SDLK_SCROLLOCK;
1047 break;
1048 case K_PAUSE:
1049 keymap[i] = SDLK_PAUSE;
1050 break;
1051
1052 case 127:
1053 keymap[i] = SDLK_BACKSPACE;
1054 break;
1055
1056 default:
1057 break;
1058 }
1059 }
1060 }
1061
1062 static SDL_keysym *
1063 TranslateKey (int scancode, SDL_keysym * keysym)
1064 {
1065 /* Set the keysym information */
1066 keysym->scancode = scancode;
1067 keysym->sym = keymap[scancode];
1068 keysym->mod = KMOD_NONE;
1069
1070 /* If UNICODE is on, get the UNICODE value for the key */
1071 keysym->unicode = 0;
1072 if (SDL_TranslateUNICODE) {
1073 int map;
1074 SDLMod modstate;
1075
1076 modstate = SDL_GetModState ();
1077 map = 0;
1078 if (modstate & KMOD_SHIFT) {
1079 map |= (1 << KG_SHIFT);
1080 }
1081 if (modstate & KMOD_CTRL) {
1082 map |= (1 << KG_CTRL);
1083 }
1084 if (modstate & KMOD_ALT) {
1085 map |= (1 << KG_ALT);
1086 }
1087 if (modstate & KMOD_MODE) {
1088 map |= (1 << KG_ALTGR);
1089 }
1090 if (KTYP (vga_keymap[map][scancode]) == KT_LETTER) {
1091 if (modstate & KMOD_CAPS) {
1092 map ^= (1 << KG_SHIFT);
1093 }
1094 }
1095 if (KTYP (vga_keymap[map][scancode]) == KT_PAD) {
1096 if (modstate & KMOD_NUM) {
1097 keysym->unicode = KVAL (vga_keymap[map][scancode]);
1098 }
1099 } else {
1100 keysym->unicode = KVAL (vga_keymap[map][scancode]);
1101 }
1102 }
1103 return (keysym);
1104 }
1105
1106 /* vi: set ts=4 sw=4 expandtab: */