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

Commit

Permalink
HapticSetGain checks to see if device supports it.
Browse files Browse the repository at this point in the history
Added HapticSetAutocenter().
  • Loading branch information
bobbens committed Jul 1, 2008
1 parent 7e6f106 commit ce595d2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
8 changes: 8 additions & 0 deletions include/SDL_haptic.h
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/haptic/SDL_haptic.c
Expand Up @@ -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;
Expand All @@ -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;
}


1 change: 1 addition & 0 deletions src/haptic/SDL_syshaptic.h
Expand Up @@ -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);


27 changes: 25 additions & 2 deletions src/haptic/linux/SDL_syshaptic.c
Expand Up @@ -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 */

0 comments on commit ce595d2

Please sign in to comment.