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