Skip to content

Commit

Permalink
cleanup patch written by Ozkan Sezer <sezeroz@gmail.com> for SDL2
Browse files Browse the repository at this point in the history
Original commit:

Author: Ozkan Sezer <sezeroz@gmail.com>
Date:   Sun Dec 29 17:55:04 2024 +0300

minor clean-ups:

- make _abs,_llabs, get_time_ms, effect_is_periodic, effect_is_condition
  helpers static inline.
- rename _abs and _llabs to abs32 and abs64 to avoid analyzers complain
  about reserved names.
- make the drivers[] array static.
- NULL terminate the drivers[] array to avoid an empty array in case if
  SDL_HAPTIC_HIDAPI_LG4FF isn't configured.
- minor formatting clean-ups here and there.
- remove #endif comments about SDL_JOYSTICK_HIDAPI for readability.
   those #if / #endif blocks are short enough already.
  • Loading branch information
Kethen committed Dec 29, 2024
1 parent 7d16076 commit 6a9788b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 58 deletions.
38 changes: 19 additions & 19 deletions src/haptic/SDL_haptic.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "SDL_syshaptic.h"
#ifdef SDL_JOYSTICK_HIDAPI
#include "SDL_hidapihaptic.h"
#endif //SDL_JOYSTICK_HIDAPI
#endif
#include "SDL_haptic_c.h"
#include "../joystick/SDL_joystick_c.h" // For SDL_IsJoystickValid

Expand All @@ -45,7 +45,7 @@ bool SDL_InitHaptics(void)
SDL_SYS_HapticQuit();
return false;
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

return true;
}
Expand Down Expand Up @@ -232,9 +232,9 @@ bool SDL_IsJoystickHaptic(SDL_Joystick *joystick)
!SDL_IsGamepad(SDL_GetJoystickID(joystick))) {
#ifdef SDL_JOYSTICK_HIDAPI
result = SDL_SYS_JoystickIsHaptic(joystick) || SDL_HIDAPI_JoystickIsHaptic(joystick);
#else //SDL_JOYSTICK_HIDAPI
#else
result = SDL_SYS_JoystickIsHaptic(joystick);
#endif //SDL_JOYSTICK_HIDAPI
#endif
}
}
SDL_UnlockJoysticks();
Expand Down Expand Up @@ -263,7 +263,7 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick) || SDL_HIDAPI_JoystickSameHaptic(hapticlist, joystick)) {
#else
if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
#endif //SDL_JOYSTICK_HIDAPI
#endif
haptic = hapticlist;
++haptic->ref_count;
SDL_UnlockJoysticks();
Expand Down Expand Up @@ -292,7 +292,7 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
return NULL;
}
} else
#endif //SDL_JOYSTICK_HIDAPI
#endif
if (!SDL_SYS_HapticOpenFromJoystick(haptic, joystick)) {
SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed.");
SDL_free(haptic);
Expand Down Expand Up @@ -339,7 +339,7 @@ void SDL_CloseHaptic(SDL_Haptic *haptic)
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
SDL_HIDAPI_HapticClose(haptic);
} else
#endif //SDL_JOYSTICK_HIDAPI
#endif
{
// Close it, properly removing effects if needed
for (i = 0; i < haptic->neffects; i++) {
Expand Down Expand Up @@ -382,7 +382,7 @@ void SDL_QuitHaptics(void)

#ifdef SDL_JOYSTICK_HIDAPI
SDL_HIDAPI_HapticQuit();
#endif //SDL_JOYSTICK_HIDAPI
#endif
SDL_SYS_HapticQuit();
}

Expand Down Expand Up @@ -449,7 +449,7 @@ int SDL_CreateHapticEffect(SDL_Haptic *haptic, const SDL_HapticEffect *effect)
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticNewEffect(haptic, effect);
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

// See if there's a free slot
for (i = 0; i < haptic->neffects; i++) {
Expand Down Expand Up @@ -487,7 +487,7 @@ bool SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, const SDL_HapticEffe
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticUpdateEffect(haptic, effect, data);
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

if (!ValidEffect(haptic, effect)) {
return false;
Expand Down Expand Up @@ -520,7 +520,7 @@ bool SDL_RunHapticEffect(SDL_Haptic *haptic, int effect, Uint32 iterations)
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticRunEffect(haptic, effect, iterations);
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

if (!ValidEffect(haptic, effect)) {
return false;
Expand All @@ -542,7 +542,7 @@ bool SDL_StopHapticEffect(SDL_Haptic *haptic, int effect)
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticStopEffect(haptic, effect);
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

if (!ValidEffect(haptic, effect)) {
return false;
Expand All @@ -565,7 +565,7 @@ void SDL_DestroyHapticEffect(SDL_Haptic *haptic, int effect)
SDL_HIDAPI_HapticDestroyEffect(haptic, effect);
return;
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

if (!ValidEffect(haptic, effect)) {
return;
Expand All @@ -587,7 +587,7 @@ bool SDL_GetHapticEffectStatus(SDL_Haptic *haptic, int effect)
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticGetEffectStatus(haptic, effect);
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

if (!ValidEffect(haptic, effect)) {
return false;
Expand Down Expand Up @@ -639,7 +639,7 @@ bool SDL_SetHapticGain(SDL_Haptic *haptic, int gain)
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticSetGain(haptic, real_gain);
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

return SDL_SYS_HapticSetGain(haptic, real_gain);
}
Expand All @@ -660,7 +660,7 @@ bool SDL_SetHapticAutocenter(SDL_Haptic *haptic, int autocenter)
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticSetAutocenter(haptic, autocenter);
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

return SDL_SYS_HapticSetAutocenter(haptic, autocenter);
}
Expand All @@ -677,7 +677,7 @@ bool SDL_PauseHaptic(SDL_Haptic *haptic)
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticPause(haptic);
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

return SDL_SYS_HapticPause(haptic);
}
Expand All @@ -694,7 +694,7 @@ bool SDL_ResumeHaptic(SDL_Haptic *haptic)
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticResume(haptic);
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

return SDL_SYS_HapticResume(haptic);
}
Expand All @@ -707,7 +707,7 @@ bool SDL_StopHapticEffects(SDL_Haptic *haptic)
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
return SDL_HIDAPI_HapticStopAll(haptic);
}
#endif //SDL_JOYSTICK_HIDAPI
#endif

return SDL_SYS_HapticStopAll(haptic);
}
Expand Down
24 changes: 14 additions & 10 deletions src/haptic/hidapi/SDL_hidapihaptic.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ typedef struct haptic_list_node
static haptic_list_node *haptic_list_head = NULL;
static SDL_Mutex *haptic_list_mutex = NULL;

SDL_HIDAPI_HapticDriver *drivers[] = {
static SDL_HIDAPI_HapticDriver *drivers[] = {
#ifdef SDL_HAPTIC_HIDAPI_LG4FF
&SDL_HIDAPI_HapticDriverLg4ff,
#endif //SDL_HAPTIC_HIDAPI_LG4FF
#endif
NULL
};

bool SDL_HIDAPI_HapticInit()
Expand Down Expand Up @@ -76,16 +77,17 @@ bool SDL_HIDAPI_HapticIsHidapi(SDL_Haptic *haptic)

bool SDL_HIDAPI_JoystickIsHaptic(SDL_Joystick *joystick)
{
const int numdrivers = SDL_arraysize(drivers) - 1;
int i;

SDL_AssertJoysticksLocked();

if (joystick->driver != &SDL_HIDAPI_JoystickDriver){
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
return false;
}

for (i = 0; i < SDL_arraysize(drivers); ++i) {
if(drivers[i]->JoystickSupported(joystick)) {
for (i = 0; i < numdrivers; ++i) {
if (drivers[i]->JoystickSupported(joystick)) {
return true;
}
}
Expand All @@ -94,15 +96,17 @@ bool SDL_HIDAPI_JoystickIsHaptic(SDL_Joystick *joystick)

bool SDL_HIDAPI_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
{
const int numdrivers = SDL_arraysize(drivers) - 1;
int i;

SDL_AssertJoysticksLocked();

if (joystick->driver != &SDL_HIDAPI_JoystickDriver){
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
return SDL_SetError("Cannot open hidapi haptic from non hidapi joystick");
}

for (i = 0;i < SDL_arraysize(drivers);++i) {
if(drivers[i]->JoystickSupported(joystick)) {
for (i = 0; i < numdrivers; ++i) {
if (drivers[i]->JoystickSupported(joystick)) {
SDL_HIDAPI_HapticDevice *device;
haptic_list_node *list_node;
// the driver is responsible for calling SDL_SetError
Expand All @@ -127,7 +131,7 @@ bool SDL_HIDAPI_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystic
device->ctx = ctx;

list_node = SDL_malloc(sizeof(haptic_list_node));
if(list_node == NULL) {
if (list_node == NULL) {
device->driver->Close(device);
SDL_free(device);
return SDL_OutOfMemory();
Expand Down Expand Up @@ -175,7 +179,7 @@ bool SDL_HIDAPI_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
SDL_HIDAPI_HapticDevice *device;

SDL_AssertJoysticksLocked();
if (joystick->driver != &SDL_HIDAPI_JoystickDriver){
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
return false;
}

Expand Down
Loading

0 comments on commit 6a9788b

Please sign in to comment.