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

Commit

Permalink
Improved error checking in the haptic system, preventing crashes in s…
Browse files Browse the repository at this point in the history
…ome cases.

Edgar Simo 2012-05-06 02:33:39 EDT

I recall that driver being buggy back in the day, but looking over the code there's a number of things being done wrong which I've fixed and it should now properly error out instead of crashing. Also make sure you initialize the haptic subsystem before using haptic commands (which I now more explicitly try to enforce).
  • Loading branch information
slouken committed Jul 27, 2013
1 parent c6d6854 commit 6d09ee9
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions src/haptic/SDL_haptic.c
Expand Up @@ -149,18 +149,23 @@ SDL_HapticOpen(int device_index)
return NULL;
}

/* Add haptic to list */
for (i = 0; SDL_haptics[i]; i++)
/* Skip to next haptic */ ;
if (i >= SDL_numhaptics) {
SDL_free(haptic);
SDL_SetError("Haptic: Trying to add device past the number originally detected");
return NULL;
}
SDL_haptics[i] = haptic;
++haptic->ref_count;

/* Disable autocenter and set gain to max. */
if (haptic->supported & SDL_HAPTIC_GAIN)
SDL_HapticSetGain(haptic, 100);
if (haptic->supported & SDL_HAPTIC_AUTOCENTER)
SDL_HapticSetAutocenter(haptic, 0);

/* Add haptic to list */
++haptic->ref_count;
for (i = 0; SDL_haptics[i]; i++)
/* Skip to next haptic */ ;
SDL_haptics[i] = haptic;

return haptic;
}

Expand All @@ -173,6 +178,13 @@ SDL_HapticOpened(int device_index)
{
int i, opened;

/* Make sure it's valid. */
if ((device_index < 0) || (device_index >= SDL_numhaptics)) {
SDL_SetError("Haptic: There are %d haptic devices available",
SDL_numhaptics);
return -1;
}

opened = 0;
for (i = 0; SDL_haptics[i]; i++) {
if (SDL_haptics[i]->index == (Uint8) device_index) {
Expand Down Expand Up @@ -262,6 +274,13 @@ SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
int i;
SDL_Haptic *haptic;

/* Make sure there is room. */
if (SDL_numhaptics <= 0) {
SDL_SetError("Haptic: There are %d haptic devices available",
SDL_numhaptics);
return NULL;
}

/* Must be a valid joystick */
if (!SDL_PrivateJoystickValid(joystick)) {
SDL_SetError("Haptic: Joystick isn't valid.");
Expand Down Expand Up @@ -299,10 +318,15 @@ SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
}

/* Add haptic to list */
++haptic->ref_count;
for (i = 0; SDL_haptics[i]; i++)
/* Skip to next haptic */ ;
if (i >= SDL_numhaptics) {
SDL_free(haptic);
SDL_SetError("Haptic: Trying to add device past the number originally detected");
return NULL;
}
SDL_haptics[i] = haptic;
++haptic->ref_count;

return haptic;
}
Expand Down Expand Up @@ -757,6 +781,7 @@ SDL_HapticRumbleInit(SDL_Haptic * haptic)
int
SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length)
{
int ret;
SDL_HapticPeriodic *efx;

if (!ValidHaptic(haptic)) {
Expand All @@ -779,7 +804,10 @@ SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length)
efx = &haptic->rumble_effect.periodic;
efx->magnitude = (Sint16)(32767.0f*strength);
efx->length = length;
SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect);
ret = SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect);
if (ret) {
return ret;
}

return SDL_HapticRunEffect(haptic, haptic->rumble_id, 1);
}
Expand Down

0 comments on commit 6d09ee9

Please sign in to comment.