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