From 6de73d0fee692343bfae1bc222ed84fe0f5950fd Mon Sep 17 00:00:00 2001 From: Edgar Simo Date: Tue, 8 Jul 2008 19:55:12 +0000 Subject: [PATCH] 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 | 12 ++++++++++-- src/haptic/SDL_haptic.c | 27 ++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/include/SDL_haptic.h b/include/SDL_haptic.h index db3581c9c..d68e1ae0c 100644 --- a/include/SDL_haptic.h +++ b/include/SDL_haptic.h @@ -630,8 +630,9 @@ extern DECLSPEC const char *SDLCALL SDL_HapticName(int device_index); * \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 @@ extern DECLSPEC const char *SDLCALL SDL_HapticName(int device_index); * \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 @@ extern DECLSPEC int SDL_HapticGetEffectStatus(SDL_Haptic *haptic, int effect); * * 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. diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c index 3e61e6b26..c04144319 100644 --- a/src/haptic/SDL_haptic.c +++ b/src/haptic/SDL_haptic.c @@ -138,6 +138,12 @@ SDL_HapticOpen(int device_index) 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 @@ SDL_HapticGetEffectStatus(SDL_Haptic *haptic, int effect) 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 @@ SDL_HapticSetGain(SDL_Haptic * haptic, int gain ) 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; }