comparison src/haptic/linux/SDL_syshaptic.c @ 2512:ef147ee4896c gsoc2008_force_feedback

Improved some ioctl handling. Implemented SDL_MouseIsHaptic and SDL_HapticOpenFromMouse. More code comments.
author Edgar Simo <bobbens@gmail.com>
date Thu, 10 Jul 2008 08:38:08 +0000
parents f12ae0bae468
children 55fd9103a330
comparison
equal deleted inserted replaced
2511:f12ae0bae468 2512:ef147ee4896c
89 EV_IsHaptic(int fd) 89 EV_IsHaptic(int fd)
90 { 90 {
91 unsigned int ret; 91 unsigned int ret;
92 unsigned long features[1 + FF_MAX/sizeof(unsigned long)]; 92 unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
93 93
94 /* Ask device for what it has. */
94 ret = 0; 95 ret = 0;
95 96 if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(features)), features) < 0) {
96 if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features) < 0) {
97 SDL_SetError("Unable to get device's haptic abilities: %s", strerror(errno)); 97 SDL_SetError("Unable to get device's haptic abilities: %s", strerror(errno));
98 return -1; 98 return -1;
99 } 99 }
100 100
101 /* Convert supported features to SDL_HAPTIC platform-neutral features. */
101 EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT); 102 EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT);
102 EV_TEST(FF_PERIODIC, SDL_HAPTIC_SINE | 103 EV_TEST(FF_PERIODIC, SDL_HAPTIC_SINE |
103 SDL_HAPTIC_SQUARE | 104 SDL_HAPTIC_SQUARE |
104 SDL_HAPTIC_TRIANGLE | 105 SDL_HAPTIC_TRIANGLE |
105 SDL_HAPTIC_SAWTOOTHUP | 106 SDL_HAPTIC_SAWTOOTHUP |
111 EV_TEST(FF_INERTIA, SDL_HAPTIC_INERTIA); 112 EV_TEST(FF_INERTIA, SDL_HAPTIC_INERTIA);
112 EV_TEST(FF_CUSTOM, SDL_HAPTIC_CUSTOM); 113 EV_TEST(FF_CUSTOM, SDL_HAPTIC_CUSTOM);
113 EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN); 114 EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN);
114 EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER); 115 EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
115 116
117 /* Return what it supports. */
116 return ret; 118 return ret;
119 }
120
121
122 /*
123 * Tests whether a device is a mouse or not.
124 */
125 static int
126 EV_IsMouse(int fd)
127 {
128 unsigned long argp[40];
129
130 /* Ask for supported features. */
131 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(argp)), argp) < 0) {
132 return -1;
133 }
134
135 /* Currently we only test for BTN_MOUSE which can give fake positives. */
136 if (test_bit(BTN_MOUSE,argp) != 0) {
137 return 1;
138 }
139
140 return 0;
117 } 141 }
118 142
119 /* 143 /*
120 * Initializes the haptic subsystem by finding available devices. 144 * Initializes the haptic subsystem by finding available devices.
121 */ 145 */
131 int duplicate; 155 int duplicate;
132 int numhaptics; 156 int numhaptics;
133 157
134 numhaptics = 0; 158 numhaptics = 0;
135 159
160 /*
161 * Limit amount of checks to MAX_HAPTICS since we may or may not have
162 * permission to some or all devices.
163 */
136 i = 0; 164 i = 0;
137 for (j = 0; j < MAX_HAPTICS; ++j) { 165 for (j = 0; j < MAX_HAPTICS; ++j) {
138 166
139 snprintf(path, PATH_MAX, joydev_pattern, i++); 167 snprintf(path, PATH_MAX, joydev_pattern, i++);
140 168
183 { 211 {
184 int fd; 212 int fd;
185 static char namebuf[128]; 213 static char namebuf[128];
186 char *name; 214 char *name;
187 215
216 /* Open the haptic device. */
188 name = NULL; 217 name = NULL;
189 fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0); 218 fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0);
219
190 if (fd >= 0) { 220 if (fd >= 0) {
221
222 /* Check for name ioctl. */
191 if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) { 223 if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
224
225 /* No name found, return device character device */
192 name = SDL_hapticlist[index].fname; 226 name = SDL_hapticlist[index].fname;
193 } 227 }
228 /* Name found, return name. */
194 else { 229 else {
195 name = namebuf; 230 name = namebuf;
196 } 231 }
197 } 232 }
198 close(fd); 233 close(fd);
213 if (haptic->hwdata == NULL) { 248 if (haptic->hwdata == NULL) {
214 SDL_OutOfMemory(); 249 SDL_OutOfMemory();
215 goto open_err; 250 goto open_err;
216 } 251 }
217 SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata)); 252 SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
253
218 /* Set the data */ 254 /* Set the data */
219 haptic->hwdata->fd = fd; 255 haptic->hwdata->fd = fd;
220 haptic->supported = EV_IsHaptic(fd); 256 haptic->supported = EV_IsHaptic(fd);
221 257
222 /* Set the effects */ 258 /* Set the effects */
262 SDL_hapticlist[haptic->index], strerror(errno)); 298 SDL_hapticlist[haptic->index], strerror(errno));
263 return -1; 299 return -1;
264 } 300 }
265 301
266 return SDL_SYS_HapticOpenFromFD(haptic,fd); 302 return SDL_SYS_HapticOpenFromFD(haptic,fd);
267 } 303 }
304
305
306 /*
307 * Opens a haptic device from first mouse it finds for usage.
308 */
309 int
310 SDL_SYS_HapticMouse(void)
311 {
312 int fd;
313 int i;
314
315 for (i=0; i<SDL_numhaptics; i++) {
316
317 /* Open the device. */
318 fd = open(SDL_hapticlist[i].fname, O_RDWR, 0);
319 if (fd < 0) {
320 SDL_SetError("Unable to open %s: %s",
321 SDL_hapticlist[i], strerror(errno));
322 return -1;
323 }
324
325 if (EV_IsMouse(fd)) {
326 close(fd);
327 return i;
328 }
329
330 close(fd);
331 }
332
333 return -1;
334 }
268 335
269 336
270 /* 337 /*
271 * Checks to see if a joystick has haptic features. 338 * Checks to see if a joystick has haptic features.
272 */ 339 */