comparison src/haptic/linux/SDL_syshaptic.c @ 2479:b9eb2cfe16cd gsoc2008_force_feedback

Added some preliminary support for haptic effect control.
author Edgar Simo <bobbens@gmail.com>
date Mon, 30 Jun 2008 21:38:29 +0000
parents 4fd783e0f34b
children b883974445fc
comparison
equal deleted inserted replaced
2478:4fd783e0f34b 2479:b9eb2cfe16cd
62 /* 62 /*
63 * Haptic system effect data. 63 * Haptic system effect data.
64 */ 64 */
65 struct haptic_hweffect 65 struct haptic_hweffect
66 { 66 {
67 int id; 67 struct ff_effect effect;
68 }; 68 };
69 69
70 70
71 71
72 #define test_bit(nr, addr) \ 72 #define test_bit(nr, addr) \
269 } 269 }
270 SDL_hapticlist[0].fname = NULL; 270 SDL_hapticlist[0].fname = NULL;
271 } 271 }
272 272
273 /* 273 /*
274 * Initializes the linux effect struct from a haptic_effect.
275 */
276 static int
277 SDL_SYS_ToFFEffect( struct ff_effect * dest, struct haptic_effect * src )
278 {
279 SDL_memset(dest, 0, sizeof(struct ff_effect));
280 switch (src->effect.type) {
281 case SDL_HAPTIC_CONSTANT:
282 dest->type = FF_CONSTANT;
283
284 break;
285
286 default:
287 SDL_SetError("Unknown haptic effect type.");
288 return -1;
289 }
290
291 return 0;
292 }
293
294 /*
274 * Creates a new haptic effect. 295 * Creates a new haptic effect.
275 */ 296 */
276 int 297 int
277 SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect) 298 SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
278 { 299 {
279 return -1; 300 /* Allocate the hardware effect */
301 effect->hweffect = (struct haptic_hweffect *)
302 SDL_malloc(sizeof(struct haptic_hweffect));
303 if (effect->hweffect == NULL) {
304 SDL_OutOfMemory();
305 return -1;
306 }
307
308 /* Prepare the ff_effect */
309 if (SDL_SYS_ToFFEffect( &effect->hweffect->effect, effect ) != 0) {
310 return -1;
311 }
312 effect->hweffect->effect.id = -1; /* Have the kernel give it an id */
313
314 /* Upload the effect */
315 if (ioctl(haptic->hwdata->fd, EVIOCSFF, &effect->hweffect->effect) < 0) {
316 SDL_SetError("Error uploading effect to the haptic device.");
317 return -1;
318 }
319
320 return 0;
321 }
322
323
324 /*
325 * Runs an effect.
326 */
327 int
328 SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
329 {
330 struct input_event run;
331
332 /* Prepare to run the effect */
333 run.type = EV_FF;
334 run.code = effect->hweffect->effect.id;
335 run.value = 1;
336
337 if (write(haptic->hwdata->fd, (const void*) &run, sizeof(run)) < 0) {
338 SDL_SetError("Unable to run the haptic effect.");
339 return -1;
340 }
341
342 return 0;
343 }
344
345
346 /*
347 * Frees the effect
348 */
349 void
350 SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
351 {
352 if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
353 SDL_SetError("Error removing the effect from the haptic device.");
354 }
355 SDL_free(effect->hweffect);
356 effect->hweffect = NULL;
280 } 357 }
281 358
282 359
283 #endif /* SDL_HAPTIC_LINUX */ 360 #endif /* SDL_HAPTIC_LINUX */