Added some preliminary support for haptic effect control. gsoc2008_force_feedback
authorEdgar Simo <bobbens@gmail.com>
Mon, 30 Jun 2008 21:38:29 +0000
branchgsoc2008_force_feedback
changeset 2479b9eb2cfe16cd
parent 2478 4fd783e0f34b
child 2480 b883974445fc
Added some preliminary support for haptic effect control.
src/haptic/SDL_haptic.c
src/haptic/SDL_syshaptic.h
src/haptic/linux/SDL_syshaptic.c
     1.1 --- a/src/haptic/SDL_haptic.c	Mon Jun 30 17:28:34 2008 +0000
     1.2 +++ b/src/haptic/SDL_haptic.c	Mon Jun 30 21:38:29 2008 +0000
     1.3 @@ -273,6 +273,13 @@
     1.4     if (!ValidHaptic(&haptic)) {
     1.5        return -1;
     1.6     }
     1.7 +
     1.8 +   /* Run the effect */
     1.9 +   if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect]) < 0) {
    1.10 +      return -1;
    1.11 +   }
    1.12 +
    1.13 +   return 0;
    1.14  }
    1.15  
    1.16  /*
    1.17 @@ -284,6 +291,13 @@
    1.18     if (!ValidHaptic(&haptic)) {
    1.19        return;
    1.20     }
    1.21 +
    1.22 +   /* Not allocated */
    1.23 +   if (haptic->effects[effect].hweffect == NULL) {
    1.24 +      return;
    1.25 +   }
    1.26 +
    1.27 +   SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
    1.28  }
    1.29  
    1.30  
     2.1 --- a/src/haptic/SDL_syshaptic.h	Mon Jun 30 17:28:34 2008 +0000
     2.2 +++ b/src/haptic/SDL_syshaptic.h	Mon Jun 30 21:38:29 2008 +0000
     2.3 @@ -52,4 +52,11 @@
     2.4  extern int SDL_SYS_HapticOpen(SDL_Haptic * haptic);
     2.5  extern void SDL_SYS_HapticClose(SDL_Haptic * haptic);
     2.6  extern void SDL_SYS_HapticQuit(void);
     2.7 +extern int SDL_SYS_HapticNewEffect(SDL_Haptic * haptic,
     2.8 +      struct haptic_effect * effect);
     2.9 +extern int SDL_SYS_HapticRunEffect(SDL_Haptic * haptic,
    2.10 +      struct haptic_effect * effect);
    2.11 +extern void SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic,
    2.12 +      struct haptic_effect * effect);
    2.13  
    2.14 +
     3.1 --- a/src/haptic/linux/SDL_syshaptic.c	Mon Jun 30 17:28:34 2008 +0000
     3.2 +++ b/src/haptic/linux/SDL_syshaptic.c	Mon Jun 30 21:38:29 2008 +0000
     3.3 @@ -64,7 +64,7 @@
     3.4   */
     3.5  struct haptic_hweffect
     3.6  {
     3.7 -   int id;
     3.8 +   struct ff_effect effect;
     3.9  };
    3.10  
    3.11  
    3.12 @@ -271,13 +271,90 @@
    3.13  }
    3.14  
    3.15  /*
    3.16 + * Initializes the linux effect struct from a haptic_effect.
    3.17 + */
    3.18 +static int
    3.19 +SDL_SYS_ToFFEffect( struct ff_effect * dest, struct haptic_effect * src )
    3.20 +{
    3.21 +   SDL_memset(dest, 0, sizeof(struct ff_effect));
    3.22 +   switch (src->effect.type) {
    3.23 +      case SDL_HAPTIC_CONSTANT:
    3.24 +         dest->type = FF_CONSTANT;
    3.25 +
    3.26 +         break;
    3.27 +
    3.28 +      default:
    3.29 +         SDL_SetError("Unknown haptic effect type.");
    3.30 +         return -1;
    3.31 +   }
    3.32 +
    3.33 +   return 0;
    3.34 +}
    3.35 +
    3.36 +/*
    3.37   * Creates a new haptic effect.
    3.38   */
    3.39  int
    3.40  SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
    3.41  {
    3.42 -   return -1;
    3.43 +   /* Allocate the hardware effect */
    3.44 +   effect->hweffect = (struct haptic_hweffect *) 
    3.45 +         SDL_malloc(sizeof(struct haptic_hweffect));
    3.46 +   if (effect->hweffect == NULL) {
    3.47 +      SDL_OutOfMemory();
    3.48 +      return -1;
    3.49 +   }
    3.50 +
    3.51 +   /* Prepare the ff_effect */
    3.52 +   if (SDL_SYS_ToFFEffect( &effect->hweffect->effect, effect ) != 0) {
    3.53 +      return -1;
    3.54 +   }
    3.55 +   effect->hweffect->effect.id = -1; /* Have the kernel give it an id */
    3.56 +
    3.57 +   /* Upload the effect */
    3.58 +   if (ioctl(haptic->hwdata->fd, EVIOCSFF, &effect->hweffect->effect) < 0) {
    3.59 +      SDL_SetError("Error uploading effect to the haptic device.");
    3.60 +      return -1;
    3.61 +   }
    3.62 +
    3.63 +   return 0;
    3.64  }
    3.65  
    3.66  
    3.67 +/*
    3.68 + * Runs an effect.
    3.69 + */
    3.70 +int
    3.71 +SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
    3.72 +{
    3.73 +   struct input_event run;
    3.74 +
    3.75 +   /* Prepare to run the effect */
    3.76 +   run.type = EV_FF;
    3.77 +   run.code = effect->hweffect->effect.id;
    3.78 +   run.value = 1;
    3.79 +
    3.80 +   if (write(haptic->hwdata->fd, (const void*) &run, sizeof(run)) < 0) {
    3.81 +      SDL_SetError("Unable to run the haptic effect.");
    3.82 +      return -1;
    3.83 +   }
    3.84 +
    3.85 +   return 0;
    3.86 +}
    3.87 +
    3.88 +
    3.89 +/*
    3.90 + * Frees the effect
    3.91 + */
    3.92 +void
    3.93 +SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
    3.94 +{
    3.95 +   if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
    3.96 +      SDL_SetError("Error removing the effect from the haptic device.");
    3.97 +   }
    3.98 +   SDL_free(effect->hweffect);
    3.99 +   effect->hweffect = NULL;
   3.100 +}
   3.101 +
   3.102 +
   3.103  #endif /* SDL_HAPTIC_LINUX */