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