From b50f26e08618e22bd10f7ce4ab006f1e9be1329e Mon Sep 17 00:00:00 2001 From: Edgar Simo Date: Tue, 1 Jul 2008 18:35:05 +0000 Subject: [PATCH] Added SDL_HapticUpdateEffect(). --- include/SDL_haptic.h | 10 ++++++++ src/haptic/SDL_haptic.c | 19 ++++++++++++++++ src/haptic/SDL_syshaptic.h | 2 ++ src/haptic/linux/SDL_syshaptic.c | 39 ++++++++++++++++++++++++++++++-- 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/include/SDL_haptic.h b/include/SDL_haptic.h index 8eb642ce7..4aa86fed6 100644 --- a/include/SDL_haptic.h +++ b/include/SDL_haptic.h @@ -240,6 +240,16 @@ extern DECLSPEC int SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEff */ extern DECLSPEC int SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect); +/* + * Uploads an effect. Can be used dynamically, although behaviour when + * dynamically changing direction may be strange. Specifically the effect + * may reupload itself and start playing from the start. You cannot change + * the type either when running UpdateEffect. + * + * Returns the id of the effect on success, -1 on failure. + */ +extern DECLSPEC int SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data); + /* * Runs the haptic effect on it's assosciated haptic device. * diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c index cbe6ad644..84fb5bada 100644 --- a/src/haptic/SDL_haptic.c +++ b/src/haptic/SDL_haptic.c @@ -291,6 +291,25 @@ ValidEffect(SDL_Haptic * haptic, int effect) return 1; } +/* + * Updates an effect. + */ +int +SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data) +{ + if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) { + return -1; + } + + /* Updates the effect */ + if (SDL_SYS_HapticUpdateEffect(haptic,&haptic->effects[effect],data) < 0) { + return -1; + } + + return 0; +} + + /* * Runs the haptic effect on the device. */ diff --git a/src/haptic/SDL_syshaptic.h b/src/haptic/SDL_syshaptic.h index 71aa66ac3..66dbcd6bc 100644 --- a/src/haptic/SDL_syshaptic.h +++ b/src/haptic/SDL_syshaptic.h @@ -54,6 +54,8 @@ extern void SDL_SYS_HapticClose(SDL_Haptic * haptic); extern void SDL_SYS_HapticQuit(void); extern int SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect, SDL_HapticEffect * base); +extern int SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic, + struct haptic_effect * effect, SDL_HapticEffect * data); extern int SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect * effect); extern int SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c index 55e5b025b..606eeb94e 100644 --- a/src/haptic/linux/SDL_syshaptic.c +++ b/src/haptic/linux/SDL_syshaptic.c @@ -456,17 +456,52 @@ SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect, /* Prepare the ff_effect */ linux_effect = &effect->hweffect->effect; if (SDL_SYS_ToFFEffect( linux_effect, base ) != 0) { - return -1; + goto new_effect_err; } linux_effect->id = -1; /* Have the kernel give it an id */ /* Upload the effect */ if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) { SDL_SetError("Error uploading effect to the haptic device."); - return -1; + goto new_effect_err; } return 0; + +new_effect_err: + free(effect->hweffect); + effect->hweffect = NULL; + return -1; +} + + +/* + * Updates an effect. + * + * Note: Dynamically updating the direction can in some cases force + * the effect to restart and run once. + */ +int SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic, + struct haptic_effect * effect, SDL_HapticEffect * data) +{ + struct ff_effect linux_effect; + + /* Create the new effect */ + if (SDL_SYS_ToFFEffect( &linux_effect, data ) != 0) { + return -1; + } + linux_effect.id = effect->hweffect->effect.id; + + /* See if it can be uploaded. */ + if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) { + SDL_SetError("Error updating the haptic effect."); + return -1; + } + + /* Copy the new effect into memory. */ + SDL_memcpy( &effect->hweffect->effect, &linux_effect, sizeof(struct ff_effect) ); + + return effect->hweffect->effect.id; }