Gain is set to max on haptic device open. gsoc2008_force_feedback
authorEdgar Simo <bobbens@gmail.com>
Tue, 08 Jul 2008 19:55:12 +0000
branchgsoc2008_force_feedback
changeset 25078ef1d0f4d0c1
parent 2506 ba8e99fe92c1
child 2508 f23cee9e16fc
Gain is set to max on haptic device open.
Autocenter is disabled on haptic device open.
Maximum gain can be set by SDL_HAPTIC_GAIN_MAX.
include/SDL_haptic.h
src/haptic/SDL_haptic.c
     1.1 --- a/include/SDL_haptic.h	Tue Jul 08 19:35:10 2008 +0000
     1.2 +++ b/include/SDL_haptic.h	Tue Jul 08 19:55:12 2008 +0000
     1.3 @@ -630,8 +630,9 @@
     1.4   * \brief Opens a Haptic device for usage - the index passed as an
     1.5   * argument refers to the N'th Haptic device on this system.
     1.6   *
     1.7 - * This function returns a Haptic device identifier, or Null
     1.8 - * if an error occurred.
     1.9 + * When opening a haptic device, it's gain will be set to maximum and
    1.10 + *  autocenter will be disabled.  To modify these values use
    1.11 + *  SDL_HapticSetGain and SDL_HapticSetAutocenter
    1.12   *
    1.13   *    \param device_index Index of the device to open.
    1.14   *    \return Device identifier or NULL on error.
    1.15 @@ -639,6 +640,8 @@
    1.16   * \sa SDL_HapticIndex
    1.17   * \sa SDL_HapticOpenFromJoystick
    1.18   * \sa SDL_HapticClose
    1.19 + * \sa SDL_HapticSetGain
    1.20 + * \sa SDL_HapticSetAutocenter
    1.21   */
    1.22  extern DECLSPEC SDL_Haptic * SDL_HapticOpen(int device_index);
    1.23  
    1.24 @@ -854,6 +857,11 @@
    1.25   *
    1.26   * Device must support the SDL_HAPTIC_GAIN feature.
    1.27   *
    1.28 + * The user may specify the maxmimum gain by setting the environment variable
    1.29 + *  SDL_HAPTIC_GAIN_MAX which should be between 0 and 100.  All calls to
    1.30 + *  SDL_HapticSetGain will scale linearly using SDL_HAPTIC_GAIN_MAX as the
    1.31 + *  maximum.
    1.32 + *
    1.33   *    \param haptic Haptic device to set the gain on.
    1.34   *    \param gain Value to set the gain to, should be between 0 and 100.
    1.35   *    \return 0 on success or -1 on error.
     2.1 --- a/src/haptic/SDL_haptic.c	Tue Jul 08 19:35:10 2008 +0000
     2.2 +++ b/src/haptic/SDL_haptic.c	Tue Jul 08 19:55:12 2008 +0000
     2.3 @@ -138,6 +138,12 @@
     2.4        return NULL;
     2.5     }
     2.6  
     2.7 +   /* Disable autocenter and set gain to max. */
     2.8 +   if (haptic->supported & SDL_HAPTIC_GAIN)
     2.9 +      SDL_HapticSetGain(haptic,100);
    2.10 +   if (haptic->supported & SDL_HAPTIC_AUTOCENTER)
    2.11 +      SDL_HapticSetAutocenter(haptic,0);
    2.12 +
    2.13     /* Add haptic to list */
    2.14     ++haptic->ref_count;
    2.15     for (i=0; SDL_haptics[i]; i++)
    2.16 @@ -494,6 +500,9 @@
    2.17  int
    2.18  SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
    2.19  {
    2.20 +   const char *env;
    2.21 +   int real_gain, max_gain;
    2.22 +
    2.23     if (!ValidHaptic(&haptic)) {
    2.24        return -1;
    2.25     }
    2.26 @@ -508,7 +517,23 @@
    2.27        return -1;
    2.28     }
    2.29  
    2.30 -   if (SDL_SYS_HapticSetGain(haptic,gain) < 0) {
    2.31 +   /* We use the envvar to get the maximum gain. */
    2.32 +   env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
    2.33 +   if (env != NULL) {
    2.34 +      max_gain = SDL_atoi(env);
    2.35 +
    2.36 +      /* Check for sanity. */
    2.37 +      if (max_gain < 0) max_gain = 0;
    2.38 +      else if (max_gain > 100) max_gain = 100;
    2.39 +
    2.40 +      /* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
    2.41 +      real_gain = (gain * max_gain) / 100;
    2.42 +   }
    2.43 +   else {
    2.44 +      real_gain = gain;
    2.45 +   }
    2.46 +
    2.47 +   if (SDL_SYS_HapticSetGain(haptic,real_gain) < 0) {
    2.48        return -1;
    2.49     }
    2.50