Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Added SDL_HapticUpdateEffect().
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbens committed Jul 1, 2008
1 parent 4fdd153 commit b50f26e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
10 changes: 10 additions & 0 deletions include/SDL_haptic.h
Expand Up @@ -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.
*
Expand Down
19 changes: 19 additions & 0 deletions src/haptic/SDL_haptic.c
Expand Up @@ -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.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/haptic/SDL_syshaptic.h
Expand Up @@ -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,
Expand Down
39 changes: 37 additions & 2 deletions src/haptic/linux/SDL_syshaptic.c
Expand Up @@ -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;
}


Expand Down

0 comments on commit b50f26e

Please sign in to comment.