changeset 2507:8ef1d0f4d0c1 gsoc2008_force_feedback

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.
author Edgar Simo <bobbens@gmail.com>
date Tue, 08 Jul 2008 19:55:12 +0000
parents ba8e99fe92c1
children f23cee9e16fc
files include/SDL_haptic.h src/haptic/SDL_haptic.c
diffstat 2 files changed, 36 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/include/SDL_haptic.h	Tue Jul 08 19:35:10 2008 +0000
+++ b/include/SDL_haptic.h	Tue Jul 08 19:55:12 2008 +0000
@@ -630,8 +630,9 @@
  * \brief Opens a Haptic device for usage - the index passed as an
  * argument refers to the N'th Haptic device on this system.
  *
- * This function returns a Haptic device identifier, or Null
- * if an error occurred.
+ * When opening a haptic device, it's gain will be set to maximum and
+ *  autocenter will be disabled.  To modify these values use
+ *  SDL_HapticSetGain and SDL_HapticSetAutocenter
  *
  *    \param device_index Index of the device to open.
  *    \return Device identifier or NULL on error.
@@ -639,6 +640,8 @@
  * \sa SDL_HapticIndex
  * \sa SDL_HapticOpenFromJoystick
  * \sa SDL_HapticClose
+ * \sa SDL_HapticSetGain
+ * \sa SDL_HapticSetAutocenter
  */
 extern DECLSPEC SDL_Haptic * SDL_HapticOpen(int device_index);
 
@@ -854,6 +857,11 @@
  *
  * Device must support the SDL_HAPTIC_GAIN feature.
  *
+ * The user may specify the maxmimum gain by setting the environment variable
+ *  SDL_HAPTIC_GAIN_MAX which should be between 0 and 100.  All calls to
+ *  SDL_HapticSetGain will scale linearly using SDL_HAPTIC_GAIN_MAX as the
+ *  maximum.
+ *
  *    \param haptic Haptic device to set the gain on.
  *    \param gain Value to set the gain to, should be between 0 and 100.
  *    \return 0 on success or -1 on error.
--- a/src/haptic/SDL_haptic.c	Tue Jul 08 19:35:10 2008 +0000
+++ b/src/haptic/SDL_haptic.c	Tue Jul 08 19:55:12 2008 +0000
@@ -138,6 +138,12 @@
       return NULL;
    }
 
+   /* Disable autocenter and set gain to max. */
+   if (haptic->supported & SDL_HAPTIC_GAIN)
+      SDL_HapticSetGain(haptic,100);
+   if (haptic->supported & SDL_HAPTIC_AUTOCENTER)
+      SDL_HapticSetAutocenter(haptic,0);
+
    /* Add haptic to list */
    ++haptic->ref_count;
    for (i=0; SDL_haptics[i]; i++)
@@ -494,6 +500,9 @@
 int
 SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
 {
+   const char *env;
+   int real_gain, max_gain;
+
    if (!ValidHaptic(&haptic)) {
       return -1;
    }
@@ -508,7 +517,23 @@
       return -1;
    }
 
-   if (SDL_SYS_HapticSetGain(haptic,gain) < 0) {
+   /* We use the envvar to get the maximum gain. */
+   env = SDL_getenv("SDL_HAPTIC_GAIN_MAX");
+   if (env != NULL) {
+      max_gain = SDL_atoi(env);
+
+      /* Check for sanity. */
+      if (max_gain < 0) max_gain = 0;
+      else if (max_gain > 100) max_gain = 100;
+
+      /* We'll scale it linearly with SDL_HAPTIC_GAIN_MAX */
+      real_gain = (gain * max_gain) / 100;
+   }
+   else {
+      real_gain = gain;
+   }
+
+   if (SDL_SYS_HapticSetGain(haptic,real_gain) < 0) {
       return -1;
    }