Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Improved error checking in the haptic system, preventing crashes in s…

…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 6d09ee97c2c2597f7d6bbc8c176b4952f98b1d41
Showing with 36 additions and 8 deletions.
  1. +36 −8 src/haptic/SDL_haptic.c
@@ -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;
}

@@ -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) {
@@ -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.");
@@ -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;
}
@@ -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)) {
@@ -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);
}

0 comments on commit 6d09ee9

Please sign in to comment.