comparison src/haptic/linux/SDL_syshaptic.c @ 2490:be9b206d44af gsoc2008_force_feedback

Some more error checking. Implemented SDL_HapticOpenFromJoystick().
author Edgar Simo <bobbens@gmail.com>
date Wed, 02 Jul 2008 09:52:44 +0000
parents 96adc8025331
children 10bc7aaf5114
comparison
equal deleted inserted replaced
2489:96adc8025331 2490:be9b206d44af
86 unsigned int ret; 86 unsigned int ret;
87 unsigned long features[1 + FF_MAX/sizeof(unsigned long)]; 87 unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
88 88
89 ret = 0; 89 ret = 0;
90 90
91 ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features); 91 if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features) < 0) {
92 SDL_SetError("Unable to get device's haptic abilities.");
93 return -1;
94 }
92 95
93 EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT); 96 EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT);
94 EV_TEST(FF_PERIODIC, SDL_HAPTIC_SINE | 97 EV_TEST(FF_PERIODIC, SDL_HAPTIC_SINE |
95 SDL_HAPTIC_SQUARE | 98 SDL_HAPTIC_SQUARE |
96 SDL_HAPTIC_TRIANGLE | 99 SDL_HAPTIC_TRIANGLE |
149 #ifdef DEBUG_INPUT_EVENTS 152 #ifdef DEBUG_INPUT_EVENTS
150 printf("Checking %s\n", path); 153 printf("Checking %s\n", path);
151 #endif 154 #endif
152 155
153 /* see if it works */ 156 /* see if it works */
154 if (EV_IsHaptic(fd)!=0) { 157 if (EV_IsHaptic(fd) > 0) {
155 SDL_hapticlist[numhaptics].fname = SDL_strdup(path); 158 SDL_hapticlist[numhaptics].fname = SDL_strdup(path);
156 SDL_hapticlist[numhaptics].haptic = NULL; 159 SDL_hapticlist[numhaptics].haptic = NULL;
157 dev_nums[numhaptics] = sb.st_rdev; 160 dev_nums[numhaptics] = sb.st_rdev;
158 ++numhaptics; 161 ++numhaptics;
159 } 162 }
189 return name; 192 return name;
190 } 193 }
191 194
192 195
193 /* 196 /*
194 * Opens a haptic device for usage. 197 * Opens the haptic device from the file descriptor.
195 */ 198 */
196 int 199 static int
197 SDL_SYS_HapticOpen(SDL_Haptic * haptic) 200 SDL_SYS_HapticOpenFromFD(SDL_Haptic * haptic, int fd)
198 { 201 {
199 int i;
200 int fd;
201
202 /* Open the character device */
203 fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
204 if (fd < 0) {
205 SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
206 return -1;
207 }
208
209 /* Allocate the hwdata */ 202 /* Allocate the hwdata */
210 haptic->hwdata = (struct haptic_hwdata *) 203 haptic->hwdata = (struct haptic_hwdata *)
211 SDL_malloc(sizeof(*haptic->hwdata)); 204 SDL_malloc(sizeof(*haptic->hwdata));
212 if (haptic->hwdata == NULL) { 205 if (haptic->hwdata == NULL) {
213 SDL_OutOfMemory(); 206 SDL_OutOfMemory();
245 return -1; 238 return -1;
246 } 239 }
247 240
248 241
249 /* 242 /*
243 * Opens a haptic device for usage.
244 */
245 int
246 SDL_SYS_HapticOpen(SDL_Haptic * haptic)
247 {
248 int fd;
249
250 /* Open the character device */
251 fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
252 if (fd < 0) {
253 SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
254 return -1;
255 }
256
257 return SDL_SYS_HapticOpenFromFD(haptic,fd);
258 }
259
260
261 /*
250 * Checks to see if a joystick has haptic features. 262 * Checks to see if a joystick has haptic features.
251 */ 263 */
252 int 264 int
253 SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick) 265 SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
254 { 266 {
255 if (EV_IsHaptic(joystick->hwdata->fd) > 0) 267 return EV_IsHaptic(joystick->hwdata->fd);
268 }
269
270
271 /*
272 * Checks to see if the haptic device and joystick and in reality the same.
273 */
274 int
275 SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
276 {
277 if (SDL_strcmp(joystick->name,haptic->name)==0) {
256 return 1; 278 return 1;
279 }
257 return 0; 280 return 0;
258 } 281 }
259 282
260 283
261 /* 284 /*
262 * Opens a SDL_Haptic from a SDL_Joystick. 285 * Opens a SDL_Haptic from a SDL_Joystick.
263 */ 286 */
264 int 287 int
265 SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick) 288 SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
266 { 289 {
290 int fd;
291 fd = open(joystick->hwdata->fname, O_RDWR, 0);
292 return SDL_SYS_HapticOpenFromFD(haptic,fd);
267 } 293 }
268 294
269 295
270 /* 296 /*
271 * Closes the haptic device. 297 * Closes the haptic device.