From 052b958bf2f3713721044dfb0000425e18ed7c92 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 21 Jan 2024 10:56:32 -0800 Subject: [PATCH] Made SDL_HapticEffect const in the API Also added some additional parameter validation to haptic effect functions --- include/SDL3/SDL_haptic.h | 6 +++--- src/dynapi/SDL_dynapi_procs.h | 6 +++--- src/haptic/SDL_haptic.c | 18 +++++++++++++++--- src/haptic/SDL_syshaptic.h | 4 ++-- src/haptic/android/SDL_syshaptic.c | 4 ++-- src/haptic/darwin/SDL_syshaptic.c | 18 +++++++++--------- src/haptic/dummy/SDL_syshaptic.c | 4 ++-- src/haptic/linux/SDL_syshaptic.c | 18 +++++++++--------- src/haptic/windows/SDL_dinputhaptic.c | 24 ++++++++++++------------ src/haptic/windows/SDL_dinputhaptic_c.h | 4 ++-- src/haptic/windows/SDL_windowshaptic.c | 4 ++-- 11 files changed, 61 insertions(+), 49 deletions(-) diff --git a/include/SDL3/SDL_haptic.h b/include/SDL3/SDL_haptic.h index 40582e771..926faf764 100644 --- a/include/SDL3/SDL_haptic.h +++ b/include/SDL3/SDL_haptic.h @@ -1090,7 +1090,7 @@ extern DECLSPEC int SDLCALL SDL_GetNumHapticAxes(SDL_Haptic *haptic); * \sa SDL_CreateHapticEffect * \sa SDL_GetHapticFeatures */ -extern DECLSPEC SDL_bool SDLCALL SDL_HapticEffectSupported(SDL_Haptic *haptic, SDL_HapticEffect *effect); +extern DECLSPEC SDL_bool SDLCALL SDL_HapticEffectSupported(SDL_Haptic *haptic, const SDL_HapticEffect *effect); /** * Create a new haptic effect on a specified device. @@ -1107,7 +1107,7 @@ extern DECLSPEC SDL_bool SDLCALL SDL_HapticEffectSupported(SDL_Haptic *haptic, S * \sa SDL_RunHapticEffect * \sa SDL_UpdateHapticEffect */ -extern DECLSPEC int SDLCALL SDL_CreateHapticEffect(SDL_Haptic *haptic, SDL_HapticEffect *effect); +extern DECLSPEC int SDLCALL SDL_CreateHapticEffect(SDL_Haptic *haptic, const SDL_HapticEffect *effect); /** * Update the properties of an effect. @@ -1130,7 +1130,7 @@ extern DECLSPEC int SDLCALL SDL_CreateHapticEffect(SDL_Haptic *haptic, SDL_Hapti * \sa SDL_CreateHapticEffect * \sa SDL_RunHapticEffect */ -extern DECLSPEC int SDLCALL SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, SDL_HapticEffect *data); +extern DECLSPEC int SDLCALL SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, const SDL_HapticEffect *data); /** * Run the haptic effect on its associated haptic device. diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 39aa35774..5a9e807de 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -437,9 +437,9 @@ SDL_DYNAPI_PROC(SDL_YUV_CONVERSION_MODE,SDL_GetYUVConversionMode,(void),(),retur SDL_DYNAPI_PROC(SDL_YUV_CONVERSION_MODE,SDL_GetYUVConversionModeForResolution,(int a, int b),(a,b),return) SDL_DYNAPI_PROC(void,SDL_CloseHaptic,(SDL_Haptic *a),(a),) SDL_DYNAPI_PROC(void,SDL_DestroyHapticEffect,(SDL_Haptic *a, int b),(a,b),) -SDL_DYNAPI_PROC(SDL_bool,SDL_HapticEffectSupported,(SDL_Haptic *a, SDL_HapticEffect *b),(a,b),return) +SDL_DYNAPI_PROC(SDL_bool,SDL_HapticEffectSupported,(SDL_Haptic *a, const SDL_HapticEffect *b),(a,b),return) SDL_DYNAPI_PROC(int,SDL_GetHapticEffectStatus,(SDL_Haptic *a, int b),(a,b),return) -SDL_DYNAPI_PROC(int,SDL_CreateHapticEffect,(SDL_Haptic *a, SDL_HapticEffect *b),(a,b),return) +SDL_DYNAPI_PROC(int,SDL_CreateHapticEffect,(SDL_Haptic *a, const SDL_HapticEffect *b),(a,b),return) SDL_DYNAPI_PROC(int,SDL_GetNumHapticAxes,(SDL_Haptic *a),(a),return) SDL_DYNAPI_PROC(int,SDL_GetMaxHapticEffects,(SDL_Haptic *a),(a),return) SDL_DYNAPI_PROC(int,SDL_GetMaxHapticEffectsPlaying,(SDL_Haptic *a),(a),return) @@ -458,7 +458,7 @@ SDL_DYNAPI_PROC(int,SDL_SetHapticGain,(SDL_Haptic *a, int b),(a,b),return) SDL_DYNAPI_PROC(int,SDL_StopHapticEffects,(SDL_Haptic *a),(a),return) SDL_DYNAPI_PROC(int,SDL_StopHapticEffect,(SDL_Haptic *a, int b),(a,b),return) SDL_DYNAPI_PROC(int,SDL_ResumeHaptic,(SDL_Haptic *a),(a),return) -SDL_DYNAPI_PROC(int,SDL_UpdateHapticEffect,(SDL_Haptic *a, int b, SDL_HapticEffect *c),(a,b,c),return) +SDL_DYNAPI_PROC(int,SDL_UpdateHapticEffect,(SDL_Haptic *a, int b, const SDL_HapticEffect *c),(a,b,c),return) SDL_DYNAPI_PROC(SDL_bool,SDL_HasARMSIMD,(void),(),return) SDL_DYNAPI_PROC(SDL_bool,SDL_HasAVX,(void),(),return) SDL_DYNAPI_PROC(SDL_bool,SDL_HasAVX2,(void),(),return) diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c index b563b36da..d4502b115 100644 --- a/src/haptic/SDL_haptic.c +++ b/src/haptic/SDL_haptic.c @@ -380,22 +380,30 @@ int SDL_GetNumHapticAxes(SDL_Haptic *haptic) return haptic->naxes; } -SDL_bool SDL_HapticEffectSupported(SDL_Haptic *haptic, SDL_HapticEffect *effect) +SDL_bool SDL_HapticEffectSupported(SDL_Haptic *haptic, const SDL_HapticEffect *effect) { CHECK_HAPTIC_MAGIC(haptic, SDL_FALSE); + if (!effect) { + return SDL_FALSE; + } + if ((haptic->supported & effect->type) != 0) { return SDL_TRUE; } return SDL_FALSE; } -int SDL_CreateHapticEffect(SDL_Haptic *haptic, SDL_HapticEffect *effect) +int SDL_CreateHapticEffect(SDL_Haptic *haptic, const SDL_HapticEffect *effect) { int i; CHECK_HAPTIC_MAGIC(haptic, -1); + if (!effect) { + return SDL_InvalidParamError("effect"); + } + /* Check to see if effect is supported */ if (SDL_HapticEffectSupported(haptic, effect) == SDL_FALSE) { return SDL_SetError("Haptic: Effect not supported by haptic device."); @@ -428,7 +436,7 @@ static int ValidEffect(SDL_Haptic *haptic, int effect) return 1; } -int SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, SDL_HapticEffect *data) +int SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, const SDL_HapticEffect *data) { CHECK_HAPTIC_MAGIC(haptic, -1); @@ -436,6 +444,10 @@ int SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, SDL_HapticEffect *dat return -1; } + if (!data) { + return SDL_InvalidParamError("data"); + } + /* Can't change type dynamically. */ if (data->type != haptic->effects[effect].effect.type) { return SDL_SetError("Haptic: Updating effect type is illegal."); diff --git a/src/haptic/SDL_syshaptic.h b/src/haptic/SDL_syshaptic.h index 31c2cf6b0..a0cba77cd 100644 --- a/src/haptic/SDL_syshaptic.h +++ b/src/haptic/SDL_syshaptic.h @@ -134,7 +134,7 @@ extern void SDL_SYS_HapticQuit(void); */ extern int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, - SDL_HapticEffect *base); + const SDL_HapticEffect *base); /* * Updates the haptic effect on the haptic device using data @@ -144,7 +144,7 @@ extern int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, */ extern int SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, - SDL_HapticEffect *data); + const SDL_HapticEffect *data); /* * Runs the effect on the haptic device. diff --git a/src/haptic/android/SDL_syshaptic.c b/src/haptic/android/SDL_syshaptic.c index 2019ff4f0..3f35004b4 100644 --- a/src/haptic/android/SDL_syshaptic.c +++ b/src/haptic/android/SDL_syshaptic.c @@ -201,14 +201,14 @@ void SDL_SYS_HapticQuit(void) } int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, - struct haptic_effect *effect, SDL_HapticEffect *base) + struct haptic_effect *effect, const SDL_HapticEffect *base) { return 0; } int SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, - SDL_HapticEffect *data) + const SDL_HapticEffect *data) { return 0; } diff --git a/src/haptic/darwin/SDL_syshaptic.c b/src/haptic/darwin/SDL_syshaptic.c index 54ae0502b..b6c69c051 100644 --- a/src/haptic/darwin/SDL_syshaptic.c +++ b/src/haptic/darwin/SDL_syshaptic.c @@ -709,7 +709,7 @@ static DWORD FFGetTriggerButton(Uint16 button) /* * Sets the direction. */ -static int SDL_SYS_SetDirection(FFEFFECT *effect, SDL_HapticDirection *dir, int naxes) +static int SDL_SYS_SetDirection(FFEFFECT *effect, const SDL_HapticDirection *dir, int naxes) { LONG *rglDir; @@ -770,7 +770,7 @@ static int SDL_SYS_SetDirection(FFEFFECT *effect, SDL_HapticDirection *dir, int /* * Creates the FFEFFECT from a SDL_HapticEffect. */ -static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, SDL_HapticEffect *src) +static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, const SDL_HapticEffect *src) { int i; FFCONSTANTFORCE *constant = NULL; @@ -779,11 +779,11 @@ static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, SDL_HapticEffe FFRAMPFORCE *ramp = NULL; FFCUSTOMFORCE *custom = NULL; FFENVELOPE *envelope = NULL; - SDL_HapticConstant *hap_constant = NULL; - SDL_HapticPeriodic *hap_periodic = NULL; - SDL_HapticCondition *hap_condition = NULL; - SDL_HapticRamp *hap_ramp = NULL; - SDL_HapticCustom *hap_custom = NULL; + const SDL_HapticConstant *hap_constant = NULL; + const SDL_HapticPeriodic *hap_periodic = NULL; + const SDL_HapticCondition *hap_condition = NULL; + const SDL_HapticRamp *hap_ramp = NULL; + const SDL_HapticCustom *hap_custom = NULL; DWORD *axes = NULL; /* Set global stuff. */ @@ -1115,7 +1115,7 @@ SDL_SYS_HapticEffectType(Uint16 type) * Creates a new haptic effect. */ int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, - SDL_HapticEffect *base) + const SDL_HapticEffect *base) { HRESULT ret; CFUUIDRef type; @@ -1162,7 +1162,7 @@ err_hweffect: */ int SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, - SDL_HapticEffect *data) + const SDL_HapticEffect *data) { HRESULT ret; FFEffectParameterFlag flags; diff --git a/src/haptic/dummy/SDL_syshaptic.c b/src/haptic/dummy/SDL_syshaptic.c index 8dbaa68eb..feace4e15 100644 --- a/src/haptic/dummy/SDL_syshaptic.c +++ b/src/haptic/dummy/SDL_syshaptic.c @@ -87,14 +87,14 @@ void SDL_SYS_HapticQuit(void) } int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, - struct haptic_effect *effect, SDL_HapticEffect *base) + struct haptic_effect *effect, const SDL_HapticEffect *base) { return SDL_SYS_LogicError(); } int SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, - SDL_HapticEffect *data) + const SDL_HapticEffect *data) { return SDL_SYS_LogicError(); } diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c index 53a1c1f3f..4792360e2 100644 --- a/src/haptic/linux/SDL_syshaptic.c +++ b/src/haptic/linux/SDL_syshaptic.c @@ -656,7 +656,7 @@ static Uint16 SDL_SYS_ToButton(Uint16 button) /* * Initializes the ff_effect usable direction from a SDL_HapticDirection. */ -static int SDL_SYS_ToDirection(Uint16 *dest, SDL_HapticDirection *src) +static int SDL_SYS_ToDirection(Uint16 *dest, const SDL_HapticDirection *src) { Uint32 tmp; @@ -717,13 +717,13 @@ static int SDL_SYS_ToDirection(Uint16 *dest, SDL_HapticDirection *src) * Initializes the Linux effect struct from a haptic_effect. * Values above 32767 (for unsigned) are unspecified so we must clamp. */ -static int SDL_SYS_ToFFEffect(struct ff_effect *dest, SDL_HapticEffect *src) +static int SDL_SYS_ToFFEffect(struct ff_effect *dest, const SDL_HapticEffect *src) { - SDL_HapticConstant *constant; - SDL_HapticPeriodic *periodic; - SDL_HapticCondition *condition; - SDL_HapticRamp *ramp; - SDL_HapticLeftRight *leftright; + const SDL_HapticConstant *constant; + const SDL_HapticPeriodic *periodic; + const SDL_HapticCondition *condition; + const SDL_HapticRamp *ramp; + const SDL_HapticLeftRight *leftright; /* Clear up */ SDL_memset(dest, 0, sizeof(struct ff_effect)); @@ -917,7 +917,7 @@ static int SDL_SYS_ToFFEffect(struct ff_effect *dest, SDL_HapticEffect *src) * Creates a new haptic effect. */ int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, - SDL_HapticEffect *base) + const SDL_HapticEffect *base) { struct ff_effect *linux_effect; @@ -958,7 +958,7 @@ new_effect_err: */ int SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, - SDL_HapticEffect *data) + const SDL_HapticEffect *data) { struct ff_effect linux_effect; diff --git a/src/haptic/windows/SDL_dinputhaptic.c b/src/haptic/windows/SDL_dinputhaptic.c index 0b7513497..700a8155e 100644 --- a/src/haptic/windows/SDL_dinputhaptic.c +++ b/src/haptic/windows/SDL_dinputhaptic.c @@ -524,7 +524,7 @@ static DWORD DIGetTriggerButton(Uint16 button) /* * Sets the direction. */ -static int SDL_SYS_SetDirection(DIEFFECT *effect, SDL_HapticDirection *dir, int naxes) +static int SDL_SYS_SetDirection(DIEFFECT *effect, const SDL_HapticDirection *dir, int naxes) { LONG *rglDir; @@ -586,7 +586,7 @@ static int SDL_SYS_SetDirection(DIEFFECT *effect, SDL_HapticDirection *dir, int * Creates the DIEFFECT from a SDL_HapticEffect. */ static int SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest, - SDL_HapticEffect *src) + const SDL_HapticEffect *src) { int i; DICONSTANTFORCE *constant; @@ -595,11 +595,11 @@ static int SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest, DIRAMPFORCE *ramp; DICUSTOMFORCE *custom; DIENVELOPE *envelope; - SDL_HapticConstant *hap_constant; - SDL_HapticPeriodic *hap_periodic; - SDL_HapticCondition *hap_condition; - SDL_HapticRamp *hap_ramp; - SDL_HapticCustom *hap_custom; + const SDL_HapticConstant *hap_constant; + const SDL_HapticPeriodic *hap_periodic; + const SDL_HapticCondition *hap_condition; + const SDL_HapticRamp *hap_ramp; + const SDL_HapticCustom *hap_custom; DWORD *axes; /* Set global stuff. */ @@ -878,7 +878,7 @@ static void SDL_SYS_HapticFreeDIEFFECT(DIEFFECT *effect, int type) * Gets the effect type from the generic SDL haptic effect wrapper. */ /* NOLINTNEXTLINE(readability-const-return-type): Can't fix Windows' headers */ -static REFGUID SDL_SYS_HapticEffectType(SDL_HapticEffect *effect) +static REFGUID SDL_SYS_HapticEffectType(const SDL_HapticEffect *effect) { switch (effect->type) { case SDL_HAPTIC_CONSTANT: @@ -921,7 +921,7 @@ static REFGUID SDL_SYS_HapticEffectType(SDL_HapticEffect *effect) return NULL; } } -int SDL_DINPUT_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *base) +int SDL_DINPUT_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *base) { HRESULT ret; REFGUID type = SDL_SYS_HapticEffectType(base); @@ -951,7 +951,7 @@ err_effectdone: return -1; } -int SDL_DINPUT_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *data) +int SDL_DINPUT_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *data) { HRESULT ret; DWORD flags; @@ -1186,12 +1186,12 @@ void SDL_DINPUT_HapticQuit(void) { } -int SDL_DINPUT_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *base) +int SDL_DINPUT_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *base) { return SDL_Unsupported(); } -int SDL_DINPUT_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *data) +int SDL_DINPUT_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *data) { return SDL_Unsupported(); } diff --git a/src/haptic/windows/SDL_dinputhaptic_c.h b/src/haptic/windows/SDL_dinputhaptic_c.h index 0f87afbc4..1075a542f 100644 --- a/src/haptic/windows/SDL_dinputhaptic_c.h +++ b/src/haptic/windows/SDL_dinputhaptic_c.h @@ -35,8 +35,8 @@ extern int SDL_DINPUT_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joyst extern int SDL_DINPUT_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick); extern void SDL_DINPUT_HapticClose(SDL_Haptic *haptic); extern void SDL_DINPUT_HapticQuit(void); -extern int SDL_DINPUT_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *base); -extern int SDL_DINPUT_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *data); +extern int SDL_DINPUT_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *base); +extern int SDL_DINPUT_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *data); extern int SDL_DINPUT_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, Uint32 iterations); extern int SDL_DINPUT_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect); extern void SDL_DINPUT_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect); diff --git a/src/haptic/windows/SDL_windowshaptic.c b/src/haptic/windows/SDL_windowshaptic.c index 8931af061..cb334354a 100644 --- a/src/haptic/windows/SDL_windowshaptic.c +++ b/src/haptic/windows/SDL_windowshaptic.c @@ -261,7 +261,7 @@ void SDL_SYS_HapticQuit(void) * Creates a new haptic effect. */ int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, - SDL_HapticEffect *base) + const SDL_HapticEffect *base) { int result; @@ -282,7 +282,7 @@ int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, /* * Updates an effect. */ -int SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, SDL_HapticEffect *data) +int SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *data) { return SDL_DINPUT_HapticUpdateEffect(haptic, effect, data); }