Mercurial > sdl-ios-xcode
annotate src/joystick/linux/SDL_sysjoystick.c @ 232:678e3ed01019
Don't generate a spurious error on SDL_InitVideo()
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 06 Nov 2001 00:15:04 +0000 |
parents | 50620ec9c86a |
children | ab781a7dd82f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@devolution.com | |
21 */ | |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* This is the system specific header for the SDL joystick API */ | |
29 | |
30 #include <stdio.h> /* For the definition of NULL */ | |
31 #include <stdlib.h> /* For getenv() prototype */ | |
32 #include <string.h> | |
33 #include <sys/stat.h> | |
34 #include <unistd.h> | |
35 #include <fcntl.h> | |
36 #include <sys/ioctl.h> | |
37 #include <limits.h> /* For the definition of PATH_MAX */ | |
38 | |
39 #include <linux/joystick.h> | |
40 #ifdef USE_INPUT_EVENTS | |
41 #include <linux/input.h> | |
42 #endif | |
43 | |
44 #include "SDL_error.h" | |
45 #include "SDL_joystick.h" | |
46 #include "SDL_sysjoystick.h" | |
47 #include "SDL_joystick_c.h" | |
48 | |
49 /* Define this if you want to map axes to hats and trackballs */ | |
50 #define FANCY_HATS_AND_BALLS | |
51 | |
52 #ifdef FANCY_HATS_AND_BALLS | |
53 /* Special joystick configurations: | |
54 'JoystickName' Naxes Nhats Nballs | |
55 */ | |
56 static const char *special_joysticks[] = { | |
57 "'MadCatz Panther XL' 3 2 1", /* We don't handle a rudder (axis 8) */ | |
58 "'SideWinder Precision Pro' 4 1 0", | |
59 "'SideWinder 3D Pro' 4 1 0", | |
60 "'Microsoft SideWinder 3D Pro' 4 1 0", | |
61 "'Microsoft SideWinder Dual Strike USB version 1.0' 2 1 0", | |
62 "'WingMan Interceptor' 3 3 0", | |
63 /* WingMan Extreme Analog - not recognized by default | |
221
50620ec9c86a
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
211
diff
changeset
|
64 "'Analog 3-axis 4-button joystick' 2 1 0", |
0 | 65 */ |
66 "'WingMan Extreme Digital 3D' 4 1 0", | |
221
50620ec9c86a
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
211
diff
changeset
|
67 "'Analog 2-axis 4-button 1-hat FCS joystick' 2 1 0", |
0 | 68 NULL |
69 }; | |
70 #else | |
71 #undef USE_INPUT_EVENTS | |
72 #endif | |
73 | |
74 /* The maximum number of joysticks we'll detect */ | |
75 #define MAX_JOYSTICKS 32 | |
76 | |
77 /* A list of available joysticks */ | |
78 static char *SDL_joylist[MAX_JOYSTICKS]; | |
79 | |
80 /* The private structure used to keep track of a joystick */ | |
81 struct joystick_hwdata { | |
82 int fd; | |
83 /* The current linux joystick driver maps hats to two axes */ | |
84 int analog_hat; /* Well, except for analog hats */ | |
85 struct hwdata_hat { | |
86 int axis[2]; | |
87 } *hats; | |
88 /* The current linux joystick driver maps balls to two axes */ | |
89 struct hwdata_ball { | |
90 int axis[2]; | |
91 } *balls; | |
92 | |
93 /* Support for the Linux 2.4 unified input interface */ | |
94 SDL_bool is_hid; | |
95 #ifdef USE_INPUT_EVENTS | |
96 Uint8 key_map[KEY_MAX-BTN_MISC]; | |
97 Uint8 abs_map[ABS_MAX]; | |
98 struct axis_correct { | |
99 int used; | |
100 int coef[3]; | |
101 } abs_correct[ABS_MAX]; | |
102 #endif | |
103 }; | |
104 | |
105 static char *mystrdup(const char *string) | |
106 { | |
107 char *newstring; | |
108 | |
109 newstring = (char *)malloc(strlen(string)+1); | |
110 if ( newstring ) { | |
111 strcpy(newstring, string); | |
112 } | |
113 return(newstring); | |
114 } | |
115 | |
116 #ifdef USE_INPUT_EVENTS | |
117 #define test_bit(nr, addr) \ | |
118 (((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0) | |
119 | |
120 static int EV_IsJoystick(int fd) | |
121 { | |
122 unsigned long evbit[40]; | |
123 unsigned long keybit[40]; | |
124 unsigned long absbit[40]; | |
125 | |
126 if ( (ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) || | |
127 (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) || | |
128 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0) ) { | |
129 return(0); | |
130 } | |
131 if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) && | |
132 test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit) && | |
133 (test_bit(BTN_TRIGGER, keybit) || test_bit(BTN_A, keybit) || test_bit(BTN_1, keybit)))) return 0; | |
134 return(1); | |
135 } | |
136 | |
137 #endif /* USE_INPUT_EVENTS */ | |
138 | |
139 /* Function to scan the system for joysticks */ | |
140 int SDL_SYS_JoystickInit(void) | |
141 { | |
142 /* The base path of the joystick devices */ | |
143 const char *joydev_pattern[2] = { | |
144 "/dev/js%d", | |
145 #ifdef USE_INPUT_EVENTS | |
146 "/dev/input/event%d" | |
211
0cc95f442f3a
If we're looking at the /dev/input event devices, and we found
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
147 #endif |
0 | 148 "/dev/input/js%d" |
149 }; | |
150 int numjoysticks; | |
151 int i, j, done; | |
152 int fd; | |
153 char path[PATH_MAX]; | |
154 dev_t dev_nums[MAX_JOYSTICKS]; /* major/minor device numbers */ | |
155 struct stat sb; | |
156 int n, duplicate; | |
157 | |
158 numjoysticks = 0; | |
159 | |
160 /* First see if the user specified a joystick to use */ | |
161 if ( getenv("SDL_JOYSTICK_DEVICE") != NULL ) { | |
162 strncpy(path, getenv("SDL_JOYSTICK_DEVICE"), sizeof(path)); | |
163 path[sizeof(path)-1] = '\0'; | |
164 if ( stat(path, &sb) == 0 ) { | |
165 fd = open(path, O_RDONLY, 0); | |
166 if ( fd >= 0 ) { | |
167 /* Assume the user knows what they're doing. */ | |
168 SDL_joylist[numjoysticks] = mystrdup(path); | |
169 if ( SDL_joylist[numjoysticks] ) { | |
170 dev_nums[numjoysticks] = sb.st_rdev; | |
171 ++numjoysticks; | |
172 } | |
173 close(fd); | |
174 } | |
175 } | |
176 } | |
177 for ( i=0; i<SDL_TABLESIZE(joydev_pattern); ++i ) { | |
178 done = 0; | |
179 for ( j=0; (j < MAX_JOYSTICKS) && !done; ++j ) { | |
180 sprintf(path, joydev_pattern[i], j); | |
181 | |
182 /* rcg06302000 replaced access(F_OK) call with stat(). | |
183 * stat() will fail if the file doesn't exist, so it's | |
184 * equivalent behaviour. | |
185 */ | |
186 if ( stat(path, &sb) == 0 ) { | |
187 /* Check to make sure it's not already in list. | |
188 * This happens when we see a stick via symlink. | |
189 */ | |
190 duplicate = 0; | |
191 for (n=0; (n<numjoysticks) && !duplicate; ++n) { | |
192 if ( sb.st_rdev == dev_nums[n] ) { | |
193 duplicate = 1; | |
194 } | |
195 } | |
196 if (duplicate) { | |
197 continue; | |
198 } | |
199 | |
200 fd = open(path, O_RDONLY, 0); | |
201 if ( fd < 0 ) { | |
202 continue; | |
203 } | |
204 #ifdef USE_INPUT_EVENTS | |
205 #ifdef DEBUG_INPUT_EVENTS | |
206 printf("Checking %s\n", path); | |
207 #endif | |
208 if ( (i > 0) && ! EV_IsJoystick(fd) ) { | |
209 close(fd); | |
210 continue; | |
211 } | |
212 #endif | |
213 close(fd); | |
214 | |
215 /* We're fine, add this joystick */ | |
216 SDL_joylist[numjoysticks] = mystrdup(path); | |
217 if ( SDL_joylist[numjoysticks] ) { | |
218 dev_nums[numjoysticks] = sb.st_rdev; | |
219 ++numjoysticks; | |
220 } | |
221 } else { | |
222 done = 1; | |
223 } | |
224 } | |
211
0cc95f442f3a
If we're looking at the /dev/input event devices, and we found
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
225 /* This is a special case... |
0cc95f442f3a
If we're looking at the /dev/input event devices, and we found
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
226 If we're looking at the /dev/input event devices, and we found |
0cc95f442f3a
If we're looking at the /dev/input event devices, and we found
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
227 at least one, then we don't want to look at the input joystick |
0cc95f442f3a
If we're looking at the /dev/input event devices, and we found
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
228 devices, since they're built on top of devices that we've already |
0cc95f442f3a
If we're looking at the /dev/input event devices, and we found
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
229 seen, so we're done. |
0cc95f442f3a
If we're looking at the /dev/input event devices, and we found
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
230 */ |
0cc95f442f3a
If we're looking at the /dev/input event devices, and we found
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
231 if ( i > 0 && j > 0 ) { |
0cc95f442f3a
If we're looking at the /dev/input event devices, and we found
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
232 done = 1; |
0cc95f442f3a
If we're looking at the /dev/input event devices, and we found
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
233 } |
0 | 234 } |
235 return(numjoysticks); | |
236 } | |
237 | |
238 /* Function to get the device-dependent name of a joystick */ | |
239 const char *SDL_SYS_JoystickName(int index) | |
240 { | |
241 int fd; | |
242 static char namebuf[128]; | |
243 char *name; | |
244 | |
245 name = NULL; | |
246 fd = open(SDL_joylist[index], O_RDONLY, 0); | |
247 if ( fd >= 0 ) { | |
248 if ( | |
249 #ifdef USE_INPUT_EVENTS | |
250 (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) && | |
251 #endif | |
252 (ioctl(fd, JSIOCGNAME(sizeof(namebuf)), namebuf) <= 0) ) { | |
253 name = SDL_joylist[index]; | |
254 } else { | |
255 name = namebuf; | |
256 } | |
257 close(fd); | |
258 } | |
259 return name; | |
260 } | |
261 | |
262 #ifdef FANCY_HATS_AND_BALLS | |
263 | |
264 static int allocate_hatdata(SDL_Joystick *joystick) | |
265 { | |
266 int i; | |
267 | |
268 joystick->hwdata->hats = (struct hwdata_hat *)malloc( | |
269 joystick->nhats * sizeof(struct hwdata_hat)); | |
270 if ( joystick->hwdata->hats == NULL ) { | |
271 return(-1); | |
272 } | |
273 for ( i=0; i<joystick->nhats; ++i ) { | |
274 joystick->hwdata->hats[i].axis[0] = 1; | |
275 joystick->hwdata->hats[i].axis[1] = 1; | |
276 } | |
277 return(0); | |
278 } | |
279 | |
280 static int allocate_balldata(SDL_Joystick *joystick) | |
281 { | |
282 int i; | |
283 | |
284 joystick->hwdata->balls = (struct hwdata_ball *)malloc( | |
285 joystick->nballs * sizeof(struct hwdata_ball)); | |
286 if ( joystick->hwdata->balls == NULL ) { | |
287 return(-1); | |
288 } | |
289 for ( i=0; i<joystick->nballs; ++i ) { | |
290 joystick->hwdata->balls[i].axis[0] = 0; | |
291 joystick->hwdata->balls[i].axis[1] = 0; | |
292 } | |
293 return(0); | |
294 } | |
295 | |
296 static SDL_bool ConfigJoystick(SDL_Joystick *joystick, | |
297 const char *name, const char *config) | |
298 { | |
299 char cfg_name[128]; | |
300 SDL_bool handled; | |
301 | |
302 if ( config == NULL ) { | |
303 return(SDL_FALSE); | |
304 } | |
305 strcpy(cfg_name, ""); | |
306 if ( *config == '\'' ) { | |
307 sscanf(config, "'%[^']s'", cfg_name); | |
308 config += strlen(cfg_name)+2; | |
309 } else { | |
310 sscanf(config, "%s", cfg_name); | |
311 config += strlen(cfg_name); | |
312 } | |
313 handled = SDL_FALSE; | |
314 if ( strcmp(cfg_name, name) == 0 ) { | |
315 /* Get the number of axes, hats and balls for this joystick */ | |
316 int joystick_axes = joystick->naxes; | |
317 sscanf(config, "%d %d %d", | |
318 &joystick->naxes, &joystick->nhats, &joystick->nballs); | |
319 | |
320 /* Allocate the extra data for mapping them */ | |
321 if ( joystick->nhats > 0 ) { | |
322 /* HACK: Analog hats map to only one axis */ | |
323 if (joystick_axes == (joystick->naxes+joystick->nhats)){ | |
324 joystick->hwdata->analog_hat = 1; | |
325 } else { | |
326 if ( allocate_hatdata(joystick) < 0 ) { | |
327 joystick->nhats = 0; | |
328 } | |
329 joystick->hwdata->analog_hat = 0; | |
330 } | |
331 } | |
332 if ( joystick->nballs > 0 ) { | |
333 if ( allocate_balldata(joystick) < 0 ) { | |
334 joystick->nballs = 0; | |
335 } | |
336 } | |
337 handled = SDL_TRUE; | |
338 } | |
339 return(handled); | |
340 } | |
341 | |
342 #ifdef USE_INPUT_EVENTS | |
343 | |
344 static SDL_bool EV_ConfigJoystick(SDL_Joystick *joystick, int fd) | |
345 { | |
346 int i; | |
347 unsigned long keybit[40]; | |
348 unsigned long absbit[40]; | |
349 unsigned long relbit[40]; | |
350 | |
351 /* See if this device uses the new unified event API */ | |
352 if ( (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) && | |
353 (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) && | |
354 (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0) ) { | |
355 joystick->hwdata->is_hid = SDL_TRUE; | |
356 | |
357 /* Get the number of buttons, axes, and other thingamajigs */ | |
358 for ( i=BTN_JOYSTICK; i < KEY_MAX; ++i ) { | |
359 if ( test_bit(i, keybit) ) { | |
360 #ifdef DEBUG_INPUT_EVENTS | |
361 printf("Joystick has button: 0x%x\n", i); | |
362 #endif | |
363 joystick->hwdata->key_map[i-BTN_MISC] = | |
364 joystick->nbuttons; | |
365 ++joystick->nbuttons; | |
366 } | |
367 } | |
368 for ( i=BTN_MISC; i < BTN_JOYSTICK; ++i ) { | |
369 if ( test_bit(i, keybit) ) { | |
370 #ifdef DEBUG_INPUT_EVENTS | |
371 printf("Joystick has button: 0x%x\n", i); | |
372 #endif | |
373 joystick->hwdata->key_map[i-BTN_MISC] = | |
374 joystick->nbuttons; | |
375 ++joystick->nbuttons; | |
376 } | |
377 } | |
378 for ( i=0; i<ABS_MAX; ++i ) { | |
379 /* Skip hats */ | |
380 if ( i == ABS_HAT0X ) { | |
381 i = ABS_HAT3Y; | |
382 continue; | |
383 } | |
384 if ( test_bit(i, absbit) ) { | |
385 int values[5]; | |
386 | |
387 ioctl(fd, EVIOCGABS(i), values); | |
388 #ifdef DEBUG_INPUT_EVENTS | |
389 printf("Joystick has absolute axis: %x\n", i); | |
390 printf("Values = { %d, %d, %d, %d, %d }\n", | |
391 values[0], values[1], | |
392 values[2], values[3], values[4]); | |
393 #endif /* DEBUG_INPUT_EVENTS */ | |
394 joystick->hwdata->abs_map[i] = joystick->naxes; | |
395 if ( values[1] == values[2] ) { | |
396 joystick->hwdata->abs_correct[i].used = 0; | |
397 } else { | |
398 joystick->hwdata->abs_correct[i].used = 1; | |
399 joystick->hwdata->abs_correct[i].coef[0] = | |
400 (values[2] + values[1]) / 2 - values[4]; | |
401 joystick->hwdata->abs_correct[i].coef[1] = | |
402 (values[2] + values[1]) / 2 + values[4]; | |
403 joystick->hwdata->abs_correct[i].coef[2] = | |
404 (1 << 29) / ((values[2] - values[1]) / 2 - 2 * values[4]); | |
405 } | |
406 ++joystick->naxes; | |
407 } | |
408 } | |
409 for ( i=ABS_HAT0X; i <= ABS_HAT3Y; i += 2 ) { | |
410 if ( test_bit(i, absbit) || test_bit(i+1, absbit) ) { | |
411 #ifdef DEBUG_INPUT_EVENTS | |
412 printf("Joystick has hat %d\n",(i-ABS_HAT0X)/2); | |
413 #endif | |
414 ++joystick->nhats; | |
415 } | |
416 } | |
417 if ( test_bit(REL_X, relbit) || test_bit(REL_Y, relbit) ) { | |
418 ++joystick->nballs; | |
419 } | |
420 | |
421 /* Allocate data to keep track of these thingamajigs */ | |
422 if ( joystick->nhats > 0 ) { | |
423 if ( allocate_hatdata(joystick) < 0 ) { | |
424 joystick->nhats = 0; | |
425 } | |
426 } | |
427 if ( joystick->nballs > 0 ) { | |
428 if ( allocate_balldata(joystick) < 0 ) { | |
429 joystick->nballs = 0; | |
430 } | |
431 } | |
432 } | |
433 return(joystick->hwdata->is_hid); | |
434 } | |
435 | |
436 #endif /* USE_INPUT_EVENTS */ | |
437 | |
438 #endif /* FANCY_HATS_AND_BALLS */ | |
439 | |
440 /* Function to open a joystick for use. | |
441 The joystick to open is specified by the index field of the joystick. | |
442 This should fill the nbuttons and naxes fields of the joystick structure. | |
443 It returns 0, or -1 if there is an error. | |
444 */ | |
445 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
446 { | |
447 #ifdef FANCY_HATS_AND_BALLS | |
448 const char *name; | |
449 int i; | |
450 #endif | |
451 int fd; | |
452 unsigned char n; | |
453 | |
454 /* Open the joystick and set the joystick file descriptor */ | |
455 fd = open(SDL_joylist[joystick->index], O_RDONLY, 0); | |
456 if ( fd < 0 ) { | |
457 SDL_SetError("Unable to open %s\n", | |
458 SDL_joylist[joystick->index]); | |
459 return(-1); | |
460 } | |
461 joystick->hwdata = (struct joystick_hwdata *) | |
462 malloc(sizeof(*joystick->hwdata)); | |
463 if ( joystick->hwdata == NULL ) { | |
464 SDL_OutOfMemory(); | |
465 close(fd); | |
466 return(-1); | |
467 } | |
468 memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); | |
469 joystick->hwdata->fd = fd; | |
470 | |
471 /* Set the joystick to non-blocking read mode */ | |
472 fcntl(fd, F_SETFL, O_NONBLOCK); | |
473 | |
474 /* Get the number of buttons and axes on the joystick */ | |
475 #ifdef USE_INPUT_EVENTS | |
476 if ( ! EV_ConfigJoystick(joystick, fd) ) | |
477 #endif | |
478 { | |
479 if ( ioctl(fd, JSIOCGAXES, &n) < 0 ) { | |
480 joystick->naxes = 2; | |
481 } else { | |
482 joystick->naxes = n; | |
483 } | |
484 if ( ioctl(fd, JSIOCGBUTTONS, &n) < 0 ) { | |
485 joystick->nbuttons = 2; | |
486 } else { | |
487 joystick->nbuttons = n; | |
488 } | |
489 #ifdef FANCY_HATS_AND_BALLS | |
490 /* Check for special joystick support */ | |
491 name = SDL_SYS_JoystickName(joystick->index); | |
492 for ( i=0; special_joysticks[i]; ++i ) { | |
493 if (ConfigJoystick(joystick,name,special_joysticks[i])){ | |
494 break; | |
495 } | |
496 } | |
497 if ( special_joysticks[i] == NULL ) { | |
498 ConfigJoystick(joystick, name, | |
499 getenv("SDL_LINUX_JOYSTICK")); | |
500 } | |
501 #endif /* FANCY_HATS_AND_BALLS */ | |
502 } | |
503 return(0); | |
504 } | |
505 | |
506 static __inline__ | |
507 void HandleHat(SDL_Joystick *stick, Uint8 hat, int axis, int value) | |
508 { | |
509 struct hwdata_hat *the_hat; | |
510 const Uint8 position_map[3][3] = { | |
511 { SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP }, | |
512 { SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT }, | |
513 { SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN } | |
514 }; | |
515 | |
516 the_hat = &stick->hwdata->hats[hat]; | |
517 if ( value < 0 ) { | |
518 value = 0; | |
519 } else | |
520 if ( value == 0 ) { | |
521 value = 1; | |
522 } else | |
523 if ( value > 0 ) { | |
524 value = 2; | |
525 } | |
526 if ( value != the_hat->axis[axis] ) { | |
527 the_hat->axis[axis] = value; | |
528 SDL_PrivateJoystickHat(stick, hat, | |
529 position_map[the_hat->axis[1]][the_hat->axis[0]]); | |
530 } | |
531 } | |
532 | |
533 /* This was necessary for the Wingman Extreme Analog joystick */ | |
534 static __inline__ | |
535 void HandleAnalogHat(SDL_Joystick *stick, Uint8 hat, int value) | |
536 { | |
537 const Uint8 position_map[] = { | |
538 SDL_HAT_UP, | |
539 SDL_HAT_RIGHT, | |
540 SDL_HAT_DOWN, | |
541 SDL_HAT_LEFT, | |
542 SDL_HAT_CENTERED | |
543 }; | |
544 SDL_PrivateJoystickHat(stick, hat, position_map[(value/16000)+2]); | |
545 } | |
546 | |
547 static __inline__ | |
548 void HandleBall(SDL_Joystick *stick, Uint8 ball, int axis, int value) | |
549 { | |
550 stick->hwdata->balls[ball].axis[axis] += value; | |
551 } | |
552 | |
553 /* Function to update the state of a joystick - called as a device poll. | |
554 * This function shouldn't update the joystick structure directly, | |
555 * but instead should call SDL_PrivateJoystick*() to deliver events | |
556 * and update joystick device state. | |
557 */ | |
558 static __inline__ void JS_HandleEvents(SDL_Joystick *joystick) | |
559 { | |
560 struct js_event events[32]; | |
561 int i, len; | |
562 Uint8 other_axis; | |
563 | |
564 while ((len=read(joystick->hwdata->fd, events, (sizeof events))) > 0) { | |
565 len /= sizeof(events[0]); | |
566 for ( i=0; i<len; ++i ) { | |
567 switch (events[i].type & ~JS_EVENT_INIT) { | |
568 case JS_EVENT_AXIS: | |
569 if ( events[i].number < joystick->naxes ) { | |
570 SDL_PrivateJoystickAxis(joystick, | |
571 events[i].number, events[i].value); | |
572 break; | |
573 } | |
574 events[i].number -= joystick->naxes; | |
575 if ( joystick->hwdata->analog_hat ) { | |
576 other_axis = events[i].number; | |
577 if ( other_axis < joystick->nhats ) { | |
578 HandleAnalogHat(joystick, other_axis, | |
579 events[i].value); | |
580 break; | |
581 } | |
582 } else { | |
583 other_axis = (events[i].number / 2); | |
584 if ( other_axis < joystick->nhats ) { | |
585 HandleHat(joystick, other_axis, | |
586 events[i].number%2, | |
587 events[i].value); | |
588 break; | |
589 } | |
590 } | |
591 events[i].number -= joystick->nhats*2; | |
592 other_axis = (events[i].number / 2); | |
593 if ( other_axis < joystick->nballs ) { | |
594 HandleBall(joystick, other_axis, | |
595 events[i].number%2, | |
596 events[i].value); | |
597 break; | |
598 } | |
599 break; | |
600 case JS_EVENT_BUTTON: | |
601 SDL_PrivateJoystickButton(joystick, | |
602 events[i].number, events[i].value); | |
603 break; | |
604 default: | |
605 /* ?? */ | |
606 break; | |
607 } | |
608 } | |
609 } | |
610 } | |
611 #ifdef USE_INPUT_EVENTS | |
612 static __inline__ int EV_AxisCorrect(SDL_Joystick *joystick, int which, int value) | |
613 { | |
614 struct axis_correct *correct; | |
615 | |
616 correct = &joystick->hwdata->abs_correct[which]; | |
617 if ( correct->used ) { | |
618 if ( value > correct->coef[0] ) { | |
619 if ( value < correct->coef[1] ) { | |
620 return 0; | |
621 } | |
622 value -= correct->coef[1]; | |
623 } else { | |
624 value -= correct->coef[0]; | |
625 } | |
626 value *= correct->coef[2]; | |
627 value >>= 14; | |
628 } | |
629 /* Clamp and return */ | |
630 if ( value < -32767 ) { | |
631 value = -32767; | |
632 } else | |
633 if ( value > 32767 ) { | |
634 value = 32767; | |
635 } | |
636 return value; | |
637 } | |
638 | |
639 static __inline__ void EV_HandleEvents(SDL_Joystick *joystick) | |
640 { | |
641 struct input_event events[32]; | |
642 int i, len; | |
643 int code; | |
644 | |
645 while ((len=read(joystick->hwdata->fd, events, (sizeof events))) > 0) { | |
646 len /= sizeof(events[0]); | |
647 for ( i=0; i<len; ++i ) { | |
648 code = events[i].code; | |
649 switch (events[i].type) { | |
650 case EV_KEY: | |
651 if ( code >= BTN_MISC ) { | |
652 code -= BTN_MISC; | |
653 SDL_PrivateJoystickButton(joystick, | |
654 joystick->hwdata->key_map[code], | |
655 events[i].value); | |
656 } | |
657 break; | |
658 case EV_ABS: | |
659 switch (code) { | |
660 case ABS_HAT0X: | |
661 case ABS_HAT0Y: | |
662 case ABS_HAT1X: | |
663 case ABS_HAT1Y: | |
664 case ABS_HAT2X: | |
665 case ABS_HAT2Y: | |
666 case ABS_HAT3X: | |
667 case ABS_HAT3Y: | |
668 code -= ABS_HAT0X; | |
669 HandleHat(joystick, code/2, code%2, | |
670 events[i].value); | |
671 break; | |
672 default: | |
673 events[i].value = EV_AxisCorrect(joystick, code, events[i].value); | |
674 SDL_PrivateJoystickAxis(joystick, | |
675 joystick->hwdata->abs_map[code], | |
676 events[i].value); | |
677 break; | |
678 } | |
679 break; | |
680 case EV_REL: | |
681 switch (code) { | |
682 case REL_X: | |
683 case REL_Y: | |
684 code -= REL_X; | |
685 HandleBall(joystick, code/2, code%2, | |
686 events[i].value); | |
687 break; | |
688 default: | |
689 break; | |
690 } | |
691 break; | |
692 default: | |
693 break; | |
694 } | |
695 } | |
696 } | |
697 } | |
698 #endif /* USE_INPUT_EVENTS */ | |
699 | |
700 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
701 { | |
702 int i; | |
703 | |
704 #ifdef USE_INPUT_EVENTS | |
705 if ( joystick->hwdata->is_hid ) | |
706 EV_HandleEvents(joystick); | |
707 else | |
708 #endif | |
709 JS_HandleEvents(joystick); | |
710 | |
711 /* Deliver ball motion updates */ | |
712 for ( i=0; i<joystick->nballs; ++i ) { | |
713 int xrel, yrel; | |
714 | |
715 xrel = joystick->hwdata->balls[i].axis[0]; | |
716 yrel = joystick->hwdata->balls[i].axis[1]; | |
717 if ( xrel || yrel ) { | |
718 joystick->hwdata->balls[i].axis[0] = 0; | |
719 joystick->hwdata->balls[i].axis[1] = 0; | |
720 SDL_PrivateJoystickBall(joystick, (Uint8)i, xrel, yrel); | |
721 } | |
722 } | |
723 } | |
724 | |
725 /* Function to close a joystick after use */ | |
726 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
727 { | |
728 if ( joystick->hwdata ) { | |
729 close(joystick->hwdata->fd); | |
730 if ( joystick->hwdata->hats ) { | |
731 free(joystick->hwdata->hats); | |
732 } | |
733 if ( joystick->hwdata->balls ) { | |
734 free(joystick->hwdata->balls); | |
735 } | |
736 free(joystick->hwdata); | |
737 joystick->hwdata = NULL; | |
738 } | |
739 } | |
740 | |
741 /* Function to perform any system-specific joystick related cleanup */ | |
742 void SDL_SYS_JoystickQuit(void) | |
743 { | |
744 int i; | |
745 | |
746 for ( i=0; SDL_joylist[i]; ++i ) { | |
747 free(SDL_joylist[i]); | |
748 } | |
749 SDL_joylist[0] = NULL; | |
750 } | |
751 |