From ce595d24b4200bbea51a74b751103a7b6a415bf8 Mon Sep 17 00:00:00 2001 From: Edgar Simo Date: Tue, 1 Jul 2008 14:21:09 +0000 Subject: [PATCH] HapticSetGain checks to see if device supports it. Added HapticSetAutocenter(). --- include/SDL_haptic.h | 8 ++++++++ src/haptic/SDL_haptic.c | 32 ++++++++++++++++++++++++++++++++ src/haptic/SDL_syshaptic.h | 1 + src/haptic/linux/SDL_syshaptic.c | 27 +++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 2 deletions(-) diff --git a/include/SDL_haptic.h b/include/SDL_haptic.h index df694cfc6..51a158ca3 100644 --- a/include/SDL_haptic.h +++ b/include/SDL_haptic.h @@ -256,6 +256,14 @@ extern DECLSPEC void SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect); */ extern DECLSPEC int SDL_HapticSetGain(SDL_Haptic * haptic, int gain); +/* + * Sets the global autocenter of the device. Autocenter should be between + * 0 and 100. Setting it to 0 will disable autocentering. + * + * Returns 0 on success or -1 on failure. + */ +extern DECLSPEC int SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter); + /* Ends C function definitions when using C++ */ #ifdef __cplusplus diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c index 53bb2177a..bc246b02d 100644 --- a/src/haptic/SDL_haptic.c +++ b/src/haptic/SDL_haptic.c @@ -334,6 +334,11 @@ SDL_HapticSetGain(SDL_Haptic * haptic, int gain ) return -1; } + if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) { + SDL_SetError("Haptic device does not support setting gain."); + return -1; + } + if ((gain < 0) || (gain > 100)) { SDL_SetError("Haptic gain must be between 0 and 100."); return -1; @@ -346,4 +351,31 @@ SDL_HapticSetGain(SDL_Haptic * haptic, int gain ) return 0; } +/* + * Makes the device autocenter, 0 disables. + */ +int +SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter ) +{ + if (!ValidHaptic(&haptic)) { + return -1; + } + + if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) { + SDL_SetError("Haptic device does not support setting autocenter."); + return -1; + } + + if ((autocenter < 0) || (autocenter > 100)) { + SDL_SetError("Haptic autocenter must be between 0 and 100."); + return -1; + } + + if (SDL_SYS_HapticSetAutocenter(haptic,autocenter) < 0) { + return -1; + } + + return 0; +} + diff --git a/src/haptic/SDL_syshaptic.h b/src/haptic/SDL_syshaptic.h index 9bfe7ab7c..6a2487bfc 100644 --- a/src/haptic/SDL_syshaptic.h +++ b/src/haptic/SDL_syshaptic.h @@ -59,5 +59,6 @@ extern int SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, extern void SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect); extern int SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain); +extern int SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter); diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c index 53d289eee..d23ee8cc9 100644 --- a/src/haptic/linux/SDL_syshaptic.c +++ b/src/haptic/linux/SDL_syshaptic.c @@ -519,13 +519,36 @@ SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain) ie.type = EV_FF; ie.code = FF_GAIN; ie.value = (0xFFFFUL * gain) / 100; - printf("%d\n",ie.value); - if (write(haptic->hwdata->fd, &ie, sizeof(ie)) == -1) { + if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) { SDL_SetError("Error setting gain."); + return -1; + } + + return 0; +} + + +/* + * Sets the autocentering. + */ +int +SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter) +{ + struct input_event ie; + + ie.type = EV_FF; + ie.code = FF_AUTOCENTER; + ie.value = (0xFFFFUL * autocenter) / 100; + + if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) { + SDL_SetError("Error setting autocenter."); + return -1; } + return 0; } + #endif /* SDL_HAPTIC_LINUX */