Mercurial > sdl-ios-xcode
annotate src/joystick/linux/SDL_sysjoystick.c @ 889:eac8c69b5706
ate: Fri, 09 Apr 2004 20:07:10 +0200
From: Hibernatus
Subject: [SDL] BUG: changing video mode in opengl + windows
In WIN_GL_SetupWindow (SDL12/src/video/wincommon/SDL_wingl.c) you call
WIN_GL_ResetWindow when SetPixelFormat fails.
WIN_GL_ResetWindow cannot properly work in its current state, because there
are several things to update once you've created a new window. At least
DirectInput doesn't work anymore here (which is somewhat annoying for the
player :).
I think you can simply remove that function. Many games don't try to
recover from that exception, that's why the newest ATI drivers emulate 16
bpp display in 32 bpp mode so the error never occurs (but the games may run
much slower).
Having SDL_SetVideoMode return NULL would be better than actually setting
the video mode and losing focus, directinput and maybe directsound too. It
also made my previous drivers crash (when switching from 32 bpp windowed to
16 bpp fullscreen).
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 16 May 2004 18:02:48 +0000 |
parents | e7be95d758e8 |
children | dc29e5907694 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
589
diff
changeset
|
3 Copyright (C) 1997-2004 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 { | |
871 | 386 int i, t; |
0 | 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; | |
872
e7be95d758e8
Date: Fri, 05 Mar 2004 16:08:01 +0000
Sam Lantinga <slouken@libsdl.org>
parents:
871
diff
changeset
|
439 joystick->hwdata->abs_correct[i].coef[0] = |
e7be95d758e8
Date: Fri, 05 Mar 2004 16:08:01 +0000
Sam Lantinga <slouken@libsdl.org>
parents:
871
diff
changeset
|
440 (values[2] + values[1]) / 2 - values[4]; |
e7be95d758e8
Date: Fri, 05 Mar 2004 16:08:01 +0000
Sam Lantinga <slouken@libsdl.org>
parents:
871
diff
changeset
|
441 joystick->hwdata->abs_correct[i].coef[1] = |
e7be95d758e8
Date: Fri, 05 Mar 2004 16:08:01 +0000
Sam Lantinga <slouken@libsdl.org>
parents:
871
diff
changeset
|
442 (values[2] + values[1]) / 2 + values[4]; |
871 | 443 t = ((values[2] - values[1]) / 2 - 2 * values[4]); |
444 if ( t != 0 ) { | |
445 joystick->hwdata->abs_correct[i].coef[2] = (1 << 29) / t; | |
872
e7be95d758e8
Date: Fri, 05 Mar 2004 16:08:01 +0000
Sam Lantinga <slouken@libsdl.org>
parents:
871
diff
changeset
|
446 } else { |
e7be95d758e8
Date: Fri, 05 Mar 2004 16:08:01 +0000
Sam Lantinga <slouken@libsdl.org>
parents:
871
diff
changeset
|
447 joystick->hwdata->abs_correct[i].coef[2] = 0; |
871 | 448 } |
0 | 449 } |
450 ++joystick->naxes; | |
451 } | |
452 } | |
453 for ( i=ABS_HAT0X; i <= ABS_HAT3Y; i += 2 ) { | |
454 if ( test_bit(i, absbit) || test_bit(i+1, absbit) ) { | |
455 #ifdef DEBUG_INPUT_EVENTS | |
456 printf("Joystick has hat %d\n",(i-ABS_HAT0X)/2); | |
457 #endif | |
458 ++joystick->nhats; | |
459 } | |
460 } | |
461 if ( test_bit(REL_X, relbit) || test_bit(REL_Y, relbit) ) { | |
462 ++joystick->nballs; | |
463 } | |
464 | |
465 /* Allocate data to keep track of these thingamajigs */ | |
466 if ( joystick->nhats > 0 ) { | |
467 if ( allocate_hatdata(joystick) < 0 ) { | |
468 joystick->nhats = 0; | |
469 } | |
470 } | |
471 if ( joystick->nballs > 0 ) { | |
472 if ( allocate_balldata(joystick) < 0 ) { | |
473 joystick->nballs = 0; | |
474 } | |
475 } | |
476 } | |
477 return(joystick->hwdata->is_hid); | |
478 } | |
479 | |
480 #endif /* USE_INPUT_EVENTS */ | |
481 | |
482 /* Function to open a joystick for use. | |
483 The joystick to open is specified by the index field of the joystick. | |
484 This should fill the nbuttons and naxes fields of the joystick structure. | |
485 It returns 0, or -1 if there is an error. | |
486 */ | |
487 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
488 { | |
489 int fd; | |
490 | |
491 /* Open the joystick and set the joystick file descriptor */ | |
492 fd = open(SDL_joylist[joystick->index], O_RDONLY, 0); | |
493 if ( fd < 0 ) { | |
494 SDL_SetError("Unable to open %s\n", | |
495 SDL_joylist[joystick->index]); | |
496 return(-1); | |
497 } | |
498 joystick->hwdata = (struct joystick_hwdata *) | |
499 malloc(sizeof(*joystick->hwdata)); | |
500 if ( joystick->hwdata == NULL ) { | |
501 SDL_OutOfMemory(); | |
502 close(fd); | |
503 return(-1); | |
504 } | |
505 memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); | |
506 joystick->hwdata->fd = fd; | |
507 | |
508 /* Set the joystick to non-blocking read mode */ | |
509 fcntl(fd, F_SETFL, O_NONBLOCK); | |
510 | |
511 /* Get the number of buttons and axes on the joystick */ | |
512 #ifdef USE_INPUT_EVENTS | |
513 if ( ! EV_ConfigJoystick(joystick, fd) ) | |
514 #endif | |
554
38b1a98aeb11
Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents:
408
diff
changeset
|
515 JS_ConfigJoystick(joystick, fd); |
38b1a98aeb11
Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents:
408
diff
changeset
|
516 |
0 | 517 return(0); |
518 } | |
519 | |
520 static __inline__ | |
521 void HandleHat(SDL_Joystick *stick, Uint8 hat, int axis, int value) | |
522 { | |
523 struct hwdata_hat *the_hat; | |
524 const Uint8 position_map[3][3] = { | |
525 { SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP }, | |
526 { SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT }, | |
527 { SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN } | |
528 }; | |
529 | |
530 the_hat = &stick->hwdata->hats[hat]; | |
531 if ( value < 0 ) { | |
532 value = 0; | |
533 } else | |
534 if ( value == 0 ) { | |
535 value = 1; | |
536 } else | |
537 if ( value > 0 ) { | |
538 value = 2; | |
539 } | |
540 if ( value != the_hat->axis[axis] ) { | |
541 the_hat->axis[axis] = value; | |
542 SDL_PrivateJoystickHat(stick, hat, | |
543 position_map[the_hat->axis[1]][the_hat->axis[0]]); | |
544 } | |
545 } | |
546 | |
547 static __inline__ | |
548 void HandleBall(SDL_Joystick *stick, Uint8 ball, int axis, int value) | |
549 { | |
550 stick->hwdata->balls[ball].axis[axis] += value; | |
551 } | |
552 | |
553 /* Function to update the state of a joystick - called as a device poll. | |
554 * This function shouldn't update the joystick structure directly, | |
555 * but instead should call SDL_PrivateJoystick*() to deliver events | |
556 * and update joystick device state. | |
557 */ | |
558 static __inline__ void JS_HandleEvents(SDL_Joystick *joystick) | |
559 { | |
560 struct js_event events[32]; | |
561 int i, len; | |
562 Uint8 other_axis; | |
563 | |
564 while ((len=read(joystick->hwdata->fd, events, (sizeof events))) > 0) { | |
565 len /= sizeof(events[0]); | |
566 for ( i=0; i<len; ++i ) { | |
567 switch (events[i].type & ~JS_EVENT_INIT) { | |
568 case JS_EVENT_AXIS: | |
569 if ( events[i].number < joystick->naxes ) { | |
570 SDL_PrivateJoystickAxis(joystick, | |
571 events[i].number, events[i].value); | |
572 break; | |
573 } | |
574 events[i].number -= joystick->naxes; | |
589
2e58ece48b61
Removed obsolete Linux joystick code
Sam Lantinga <slouken@libsdl.org>
parents:
554
diff
changeset
|
575 other_axis = (events[i].number / 2); |
2e58ece48b61
Removed obsolete Linux joystick code
Sam Lantinga <slouken@libsdl.org>
parents:
554
diff
changeset
|
576 if ( other_axis < joystick->nhats ) { |
2e58ece48b61
Removed obsolete Linux joystick code
Sam Lantinga <slouken@libsdl.org>
parents:
554
diff
changeset
|
577 HandleHat(joystick, other_axis, |
2e58ece48b61
Removed obsolete Linux joystick code
Sam Lantinga <slouken@libsdl.org>
parents:
554
diff
changeset
|
578 events[i].number%2, |
2e58ece48b61
Removed obsolete Linux joystick code
Sam Lantinga <slouken@libsdl.org>
parents:
554
diff
changeset
|
579 events[i].value); |
2e58ece48b61
Removed obsolete Linux joystick code
Sam Lantinga <slouken@libsdl.org>
parents:
554
diff
changeset
|
580 break; |
0 | 581 } |
582 events[i].number -= joystick->nhats*2; | |
583 other_axis = (events[i].number / 2); | |
584 if ( other_axis < joystick->nballs ) { | |
585 HandleBall(joystick, other_axis, | |
586 events[i].number%2, | |
587 events[i].value); | |
588 break; | |
589 } | |
590 break; | |
591 case JS_EVENT_BUTTON: | |
592 SDL_PrivateJoystickButton(joystick, | |
593 events[i].number, events[i].value); | |
594 break; | |
595 default: | |
596 /* ?? */ | |
597 break; | |
598 } | |
599 } | |
600 } | |
601 } | |
602 #ifdef USE_INPUT_EVENTS | |
603 static __inline__ int EV_AxisCorrect(SDL_Joystick *joystick, int which, int value) | |
604 { | |
605 struct axis_correct *correct; | |
606 | |
607 correct = &joystick->hwdata->abs_correct[which]; | |
608 if ( correct->used ) { | |
609 if ( value > correct->coef[0] ) { | |
610 if ( value < correct->coef[1] ) { | |
611 return 0; | |
612 } | |
613 value -= correct->coef[1]; | |
614 } else { | |
615 value -= correct->coef[0]; | |
616 } | |
617 value *= correct->coef[2]; | |
618 value >>= 14; | |
619 } | |
554
38b1a98aeb11
Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents:
408
diff
changeset
|
620 |
0 | 621 /* Clamp and return */ |
554
38b1a98aeb11
Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents:
408
diff
changeset
|
622 if ( value < -32767 ) return -32767; |
38b1a98aeb11
Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents:
408
diff
changeset
|
623 if ( value > 32767 ) return 32767; |
38b1a98aeb11
Linux joystick cleanups from Alan Swanson
Sam Lantinga <slouken@libsdl.org>
parents:
408
diff
changeset
|
624 |
0 | 625 return value; |
626 } | |
627 | |
628 static __inline__ void EV_HandleEvents(SDL_Joystick *joystick) | |
629 { | |
630 struct input_event events[32]; | |
631 int i, len; | |
632 int code; | |
633 | |
634 while ((len=read(joystick->hwdata->fd, events, (sizeof events))) > 0) { | |
635 len /= sizeof(events[0]); | |
636 for ( i=0; i<len; ++i ) { | |
637 code = events[i].code; | |
638 switch (events[i].type) { | |
639 case EV_KEY: | |
640 if ( code >= BTN_MISC ) { | |
641 code -= BTN_MISC; | |
642 SDL_PrivateJoystickButton(joystick, | |
643 joystick->hwdata->key_map[code], | |
644 events[i].value); | |
645 } | |
646 break; | |
647 case EV_ABS: | |
648 switch (code) { | |
649 case ABS_HAT0X: | |
650 case ABS_HAT0Y: | |
651 case ABS_HAT1X: | |
652 case ABS_HAT1Y: | |
653 case ABS_HAT2X: | |
654 case ABS_HAT2Y: | |
655 case ABS_HAT3X: | |
656 case ABS_HAT3Y: | |
657 code -= ABS_HAT0X; | |
658 HandleHat(joystick, code/2, code%2, | |
659 events[i].value); | |
660 break; | |
661 default: | |
662 events[i].value = EV_AxisCorrect(joystick, code, events[i].value); | |
663 SDL_PrivateJoystickAxis(joystick, | |
664 joystick->hwdata->abs_map[code], | |
665 events[i].value); | |
666 break; | |
667 } | |
668 break; | |
669 case EV_REL: | |
670 switch (code) { | |
671 case REL_X: | |
672 case REL_Y: | |
673 code -= REL_X; | |
674 HandleBall(joystick, code/2, code%2, | |
675 events[i].value); | |
676 break; | |
677 default: | |
678 break; | |
679 } | |
680 break; | |
681 default: | |
682 break; | |
683 } | |
684 } | |
685 } | |
686 } | |
687 #endif /* USE_INPUT_EVENTS */ | |
688 | |
689 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
690 { | |
691 int i; | |
692 | |
693 #ifdef USE_INPUT_EVENTS | |
694 if ( joystick->hwdata->is_hid ) | |
695 EV_HandleEvents(joystick); | |
696 else | |
697 #endif | |
698 JS_HandleEvents(joystick); | |
699 | |
700 /* Deliver ball motion updates */ | |
701 for ( i=0; i<joystick->nballs; ++i ) { | |
702 int xrel, yrel; | |
703 | |
704 xrel = joystick->hwdata->balls[i].axis[0]; | |
705 yrel = joystick->hwdata->balls[i].axis[1]; | |
706 if ( xrel || yrel ) { | |
707 joystick->hwdata->balls[i].axis[0] = 0; | |
708 joystick->hwdata->balls[i].axis[1] = 0; | |
709 SDL_PrivateJoystickBall(joystick, (Uint8)i, xrel, yrel); | |
710 } | |
711 } | |
712 } | |
713 | |
714 /* Function to close a joystick after use */ | |
715 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
716 { | |
717 if ( joystick->hwdata ) { | |
718 close(joystick->hwdata->fd); | |
719 if ( joystick->hwdata->hats ) { | |
720 free(joystick->hwdata->hats); | |
721 } | |
722 if ( joystick->hwdata->balls ) { | |
723 free(joystick->hwdata->balls); | |
724 } | |
725 free(joystick->hwdata); | |
726 joystick->hwdata = NULL; | |
727 } | |
728 } | |
729 | |
730 /* Function to perform any system-specific joystick related cleanup */ | |
731 void SDL_SYS_JoystickQuit(void) | |
732 { | |
733 int i; | |
734 | |
735 for ( i=0; SDL_joylist[i]; ++i ) { | |
736 free(SDL_joylist[i]); | |
737 } | |
738 SDL_joylist[0] = NULL; | |
739 } | |
740 |