Added SDL_HapticUpdateEffect(). gsoc2008_force_feedback
authorEdgar Simo <bobbens@gmail.com>
Tue, 01 Jul 2008 18:35:05 +0000
branchgsoc2008_force_feedback
changeset 24888e2bdbccf7ff
parent 2487 4c8e25ef2d97
child 2489 96adc8025331
Added SDL_HapticUpdateEffect().
include/SDL_haptic.h
src/haptic/SDL_haptic.c
src/haptic/SDL_syshaptic.h
src/haptic/linux/SDL_syshaptic.c
     1.1 --- a/include/SDL_haptic.h	Tue Jul 01 16:42:12 2008 +0000
     1.2 +++ b/include/SDL_haptic.h	Tue Jul 01 18:35:05 2008 +0000
     1.3 @@ -241,6 +241,16 @@
     1.4  extern DECLSPEC int SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect);
     1.5  
     1.6  /*
     1.7 + * Uploads an effect.  Can be used dynamically, although behaviour when
     1.8 + * dynamically changing direction may be strange.  Specifically the effect
     1.9 + * may reupload itself and start playing from the start.  You cannot change
    1.10 + * the type either when running UpdateEffect.
    1.11 + *
    1.12 + * Returns the id of the effect on success, -1 on failure.
    1.13 + */
    1.14 +extern DECLSPEC int SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data);
    1.15 +
    1.16 +/*
    1.17   * Runs the haptic effect on it's assosciated haptic device.
    1.18   *
    1.19   * Returns 0 on success or -1 on failure.
     2.1 --- a/src/haptic/SDL_haptic.c	Tue Jul 01 16:42:12 2008 +0000
     2.2 +++ b/src/haptic/SDL_haptic.c	Tue Jul 01 18:35:05 2008 +0000
     2.3 @@ -292,6 +292,25 @@
     2.4  }
     2.5  
     2.6  /*
     2.7 + * Updates an effect.
     2.8 + */
     2.9 +int
    2.10 +SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data)
    2.11 +{
    2.12 +   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
    2.13 +      return -1;
    2.14 +   }
    2.15 +
    2.16 +   /* Updates the effect */
    2.17 +   if (SDL_SYS_HapticUpdateEffect(haptic,&haptic->effects[effect],data) < 0) {
    2.18 +      return -1;
    2.19 +   }
    2.20 +
    2.21 +   return 0;
    2.22 +}
    2.23 +
    2.24 +
    2.25 +/*
    2.26   * Runs the haptic effect on the device.
    2.27   */
    2.28  int
     3.1 --- a/src/haptic/SDL_syshaptic.h	Tue Jul 01 16:42:12 2008 +0000
     3.2 +++ b/src/haptic/SDL_syshaptic.h	Tue Jul 01 18:35:05 2008 +0000
     3.3 @@ -54,6 +54,8 @@
     3.4  extern void SDL_SYS_HapticQuit(void);
     3.5  extern int SDL_SYS_HapticNewEffect(SDL_Haptic * haptic,
     3.6        struct haptic_effect * effect, SDL_HapticEffect * base);
     3.7 +extern int SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
     3.8 +      struct haptic_effect * effect, SDL_HapticEffect * data);
     3.9  extern int SDL_SYS_HapticRunEffect(SDL_Haptic * haptic,
    3.10        struct haptic_effect * effect);
    3.11  extern int SDL_SYS_HapticStopEffect(SDL_Haptic * haptic,
     4.1 --- a/src/haptic/linux/SDL_syshaptic.c	Tue Jul 01 16:42:12 2008 +0000
     4.2 +++ b/src/haptic/linux/SDL_syshaptic.c	Tue Jul 01 18:35:05 2008 +0000
     4.3 @@ -456,17 +456,52 @@
     4.4     /* Prepare the ff_effect */
     4.5     linux_effect = &effect->hweffect->effect;
     4.6     if (SDL_SYS_ToFFEffect( linux_effect, base ) != 0) {
     4.7 -      return -1;
     4.8 +      goto new_effect_err;
     4.9     }
    4.10     linux_effect->id = -1; /* Have the kernel give it an id */
    4.11  
    4.12     /* Upload the effect */
    4.13     if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
    4.14        SDL_SetError("Error uploading effect to the haptic device.");
    4.15 +      goto new_effect_err;
    4.16 +   }
    4.17 +
    4.18 +   return 0;
    4.19 +
    4.20 +new_effect_err:
    4.21 +   free(effect->hweffect);
    4.22 +   effect->hweffect = NULL;
    4.23 +   return -1;
    4.24 +}
    4.25 +
    4.26 +
    4.27 +/*
    4.28 + * Updates an effect.
    4.29 + *
    4.30 + * Note: Dynamically updating the direction can in some cases force
    4.31 + * the effect to restart and run once.
    4.32 + */
    4.33 +int SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
    4.34 +      struct haptic_effect * effect, SDL_HapticEffect * data)
    4.35 +{
    4.36 +   struct ff_effect linux_effect;
    4.37 +
    4.38 +   /* Create the new effect */
    4.39 +   if (SDL_SYS_ToFFEffect( &linux_effect, data ) != 0) {
    4.40 +      return -1;
    4.41 +   }
    4.42 +   linux_effect.id = effect->hweffect->effect.id;
    4.43 +
    4.44 +   /* See if it can be uploaded. */
    4.45 +   if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) {
    4.46 +      SDL_SetError("Error updating the haptic effect.");
    4.47        return -1;
    4.48     }
    4.49  
    4.50 -   return 0;
    4.51 +   /* Copy the new effect into memory. */
    4.52 +   SDL_memcpy( &effect->hweffect->effect, &linux_effect, sizeof(struct ff_effect) );
    4.53 +
    4.54 +   return effect->hweffect->effect.id;
    4.55  }
    4.56  
    4.57