Setting effects memory to 0. gsoc2008_force_feedback
authorEdgar Simo <bobbens@gmail.com>
Tue, 01 Jul 2008 14:09:53 +0000
branchgsoc2008_force_feedback
changeset 24839d52368ebcf5
parent 2482 b51ad78812d5
child 2484 666472fd4cb0
Setting effects memory to 0.
Added SDL_HapticSetGain().
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 11:21:36 2008 +0000
     1.2 +++ b/include/SDL_haptic.h	Tue Jul 01 14:09:53 2008 +0000
     1.3 @@ -232,11 +232,15 @@
     1.4  
     1.5  /*
     1.6   * Creates a new haptic effect on the device.
     1.7 + *
     1.8 + * Returns the id of the effect on success, -1 on failure.
     1.9   */
    1.10  extern DECLSPEC int SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect);
    1.11  
    1.12  /*
    1.13   * Runs the haptic effect on it's assosciated haptic device.
    1.14 + *
    1.15 + * Returns 0 on success or -1 on failure.
    1.16   */
    1.17  extern DECLSPEC int SDL_HapticRunEffect(SDL_Haptic * haptic, int effect);
    1.18  
    1.19 @@ -245,6 +249,13 @@
    1.20   */
    1.21  extern DECLSPEC void SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect);
    1.22  
    1.23 +/*
    1.24 + * Sets the global gain of the device.  Gain should be between 0 and 100.
    1.25 + *
    1.26 + * Returns 0 on success or -1 on failure.
    1.27 + */
    1.28 +extern DECLSPEC int SDL_HapticSetGain(SDL_Haptic * haptic, int gain);
    1.29 +
    1.30  
    1.31  /* Ends C function definitions when using C++ */
    1.32  #ifdef __cplusplus
     2.1 --- a/src/haptic/SDL_haptic.c	Tue Jul 01 11:21:36 2008 +0000
     2.2 +++ b/src/haptic/SDL_haptic.c	Tue Jul 01 14:09:53 2008 +0000
     2.3 @@ -324,4 +324,26 @@
     2.4     SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]);
     2.5  }
     2.6  
     2.7 +/*
     2.8 + * Sets the global gain of the device.
     2.9 + */
    2.10 +int
    2.11 +SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
    2.12 +{
    2.13 +   if (!ValidHaptic(&haptic)) {
    2.14 +      return -1;
    2.15 +   }
    2.16  
    2.17 +   if ((gain < 0) || (gain > 100)) {
    2.18 +      SDL_SetError("Haptic gain must be between 0 and 100.");
    2.19 +      return -1;
    2.20 +   }
    2.21 +
    2.22 +   if (SDL_SYS_HapticSetGain(haptic,gain) < 0) {
    2.23 +      return -1;
    2.24 +   }
    2.25 +
    2.26 +   return 0;
    2.27 +}
    2.28 +
    2.29 +
     3.1 --- a/src/haptic/SDL_syshaptic.h	Tue Jul 01 11:21:36 2008 +0000
     3.2 +++ b/src/haptic/SDL_syshaptic.h	Tue Jul 01 14:09:53 2008 +0000
     3.3 @@ -58,5 +58,6 @@
     3.4        struct haptic_effect * effect);
     3.5  extern void SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic,
     3.6        struct haptic_effect * effect);
     3.7 +extern int SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain);
     3.8  
     3.9  
     4.1 --- a/src/haptic/linux/SDL_syshaptic.c	Tue Jul 01 11:21:36 2008 +0000
     4.2 +++ b/src/haptic/linux/SDL_syshaptic.c	Tue Jul 01 14:09:53 2008 +0000
     4.3 @@ -35,7 +35,6 @@
     4.4  #include <fcntl.h>
     4.5  #include <linux/limits.h>
     4.6  #include <string.h>
     4.7 -#include <errno.h>
     4.8  
     4.9  
    4.10  #define MAX_HAPTICS  32
    4.11 @@ -222,6 +221,9 @@
    4.12        SDL_OutOfMemory();
    4.13        goto open_err;
    4.14     }
    4.15 +   /* Clear the memory */
    4.16 +   SDL_memset(haptic->effects, 0,
    4.17 +         sizeof(struct haptic_effect) * haptic->neffects);
    4.18  
    4.19     return 0;
    4.20  
    4.21 @@ -435,6 +437,7 @@
    4.22     return 0;
    4.23  }
    4.24  
    4.25 +
    4.26  /*
    4.27   * Creates a new haptic effect.
    4.28   */
    4.29 @@ -461,8 +464,7 @@
    4.30  
    4.31     /* Upload the effect */
    4.32     if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
    4.33 -      SDL_SetError("Error uploading effect to the haptic device: %s",
    4.34 -            strerror(errno));
    4.35 +      SDL_SetError("Error uploading effect to the haptic device.");
    4.36        return -1;
    4.37     }
    4.38  
    4.39 @@ -506,4 +508,24 @@
    4.40  }
    4.41  
    4.42  
    4.43 +/*
    4.44 + * Sets the gain.
    4.45 + */
    4.46 +int
    4.47 +SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
    4.48 +{
    4.49 +   struct input_event ie;
    4.50 +
    4.51 +   ie.type = EV_FF;
    4.52 +   ie.code = FF_GAIN;
    4.53 +   ie.value = (0xFFFFUL * gain) / 100;
    4.54 +   printf("%d\n",ie.value);
    4.55 +
    4.56 +   if (write(haptic->hwdata->fd, &ie, sizeof(ie)) == -1) {
    4.57 +      SDL_SetError("Error setting gain.");
    4.58 +   }
    4.59 +
    4.60 +}
    4.61 +
    4.62 +
    4.63  #endif /* SDL_HAPTIC_LINUX */