From 7e6f1069fa34fdd530fbb2bf493cca6ebdf99e1e Mon Sep 17 00:00:00 2001 From: Edgar Simo Date: Tue, 1 Jul 2008 14:09:53 +0000 Subject: [PATCH] Setting effects memory to 0. Added SDL_HapticSetGain(). --- include/SDL_haptic.h | 11 +++++++++++ src/haptic/SDL_haptic.c | 22 ++++++++++++++++++++++ src/haptic/SDL_syshaptic.h | 1 + src/haptic/linux/SDL_syshaptic.c | 28 +++++++++++++++++++++++++--- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/include/SDL_haptic.h b/include/SDL_haptic.h index fbc768ea7..df694cfc6 100644 --- a/include/SDL_haptic.h +++ b/include/SDL_haptic.h @@ -232,11 +232,15 @@ extern DECLSPEC unsigned int SDL_HapticQueryEffects(SDL_Haptic * haptic); /* * Creates a new haptic effect on the device. + * + * Returns the id of the effect on success, -1 on failure. */ extern DECLSPEC int SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect); /* * Runs the haptic effect on it's assosciated haptic device. + * + * Returns 0 on success or -1 on failure. */ extern DECLSPEC int SDL_HapticRunEffect(SDL_Haptic * haptic, int effect); @@ -245,6 +249,13 @@ extern DECLSPEC int SDL_HapticRunEffect(SDL_Haptic * haptic, int effect); */ extern DECLSPEC void SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect); +/* + * Sets the global gain of the device. Gain should be between 0 and 100. + * + * Returns 0 on success or -1 on failure. + */ +extern DECLSPEC int SDL_HapticSetGain(SDL_Haptic * haptic, int gain); + /* Ends C function definitions when using C++ */ #ifdef __cplusplus diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c index 83a2451fe..53bb2177a 100644 --- a/src/haptic/SDL_haptic.c +++ b/src/haptic/SDL_haptic.c @@ -324,4 +324,26 @@ SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect) SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]); } +/* + * Sets the global gain of the device. + */ +int +SDL_HapticSetGain(SDL_Haptic * haptic, int gain ) +{ + if (!ValidHaptic(&haptic)) { + return -1; + } + + if ((gain < 0) || (gain > 100)) { + SDL_SetError("Haptic gain must be between 0 and 100."); + return -1; + } + + if (SDL_SYS_HapticSetGain(haptic,gain) < 0) { + return -1; + } + + return 0; +} + diff --git a/src/haptic/SDL_syshaptic.h b/src/haptic/SDL_syshaptic.h index 149445ce1..9bfe7ab7c 100644 --- a/src/haptic/SDL_syshaptic.h +++ b/src/haptic/SDL_syshaptic.h @@ -58,5 +58,6 @@ extern int SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect); extern void SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect); +extern int SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain); diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c index 7147fe76f..53d289eee 100644 --- a/src/haptic/linux/SDL_syshaptic.c +++ b/src/haptic/linux/SDL_syshaptic.c @@ -35,7 +35,6 @@ #include #include #include -#include #define MAX_HAPTICS 32 @@ -222,6 +221,9 @@ SDL_SYS_HapticOpen(SDL_Haptic * haptic) SDL_OutOfMemory(); goto open_err; } + /* Clear the memory */ + SDL_memset(haptic->effects, 0, + sizeof(struct haptic_effect) * haptic->neffects); return 0; @@ -435,6 +437,7 @@ SDL_SYS_ToFFEffect( struct ff_effect * dest, SDL_HapticEffect * src ) return 0; } + /* * Creates a new haptic effect. */ @@ -461,8 +464,7 @@ SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect, /* Upload the effect */ if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) { - SDL_SetError("Error uploading effect to the haptic device: %s", - strerror(errno)); + SDL_SetError("Error uploading effect to the haptic device."); return -1; } @@ -506,4 +508,24 @@ SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect) } +/* + * Sets the gain. + */ +int +SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain) +{ + struct input_event ie; + + 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) { + SDL_SetError("Error setting gain."); + } + +} + + #endif /* SDL_HAPTIC_LINUX */