Improved error checking in the haptic system, preventing crashes in some 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).
This commit is contained in:
Sam Lantinga 2013-07-27 02:45:26 -07:00
parent ec67327c73
commit dae53a0790

View file

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