Skip to content

Commit d0101e7

Browse files
committed
cleanup patch written by Ozkan Sezer <sezeroz@gmail.com> for
SDL2 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.
1 parent 7d16076 commit d0101e7

File tree

3 files changed

+62
-58
lines changed

3 files changed

+62
-58
lines changed

src/haptic/SDL_haptic.c

+19-19
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "SDL_syshaptic.h"
2424
#ifdef SDL_JOYSTICK_HIDAPI
2525
#include "SDL_hidapihaptic.h"
26-
#endif //SDL_JOYSTICK_HIDAPI
26+
#endif
2727
#include "SDL_haptic_c.h"
2828
#include "../joystick/SDL_joystick_c.h" // For SDL_IsJoystickValid
2929

@@ -45,7 +45,7 @@ bool SDL_InitHaptics(void)
4545
SDL_SYS_HapticQuit();
4646
return false;
4747
}
48-
#endif //SDL_JOYSTICK_HIDAPI
48+
#endif
4949

5050
return true;
5151
}
@@ -232,9 +232,9 @@ bool SDL_IsJoystickHaptic(SDL_Joystick *joystick)
232232
!SDL_IsGamepad(SDL_GetJoystickID(joystick))) {
233233
#ifdef SDL_JOYSTICK_HIDAPI
234234
result = SDL_SYS_JoystickIsHaptic(joystick) || SDL_HIDAPI_JoystickIsHaptic(joystick);
235-
#else //SDL_JOYSTICK_HIDAPI
235+
#else
236236
result = SDL_SYS_JoystickIsHaptic(joystick);
237-
#endif //SDL_JOYSTICK_HIDAPI
237+
#endif
238238
}
239239
}
240240
SDL_UnlockJoysticks();
@@ -263,7 +263,7 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
263263
if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick) || SDL_HIDAPI_JoystickSameHaptic(hapticlist, joystick)) {
264264
#else
265265
if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) {
266-
#endif //SDL_JOYSTICK_HIDAPI
266+
#endif
267267
haptic = hapticlist;
268268
++haptic->ref_count;
269269
SDL_UnlockJoysticks();
@@ -292,7 +292,7 @@ SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick)
292292
return NULL;
293293
}
294294
} else
295-
#endif //SDL_JOYSTICK_HIDAPI
295+
#endif
296296
if (!SDL_SYS_HapticOpenFromJoystick(haptic, joystick)) {
297297
SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed.");
298298
SDL_free(haptic);
@@ -339,7 +339,7 @@ void SDL_CloseHaptic(SDL_Haptic *haptic)
339339
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
340340
SDL_HIDAPI_HapticClose(haptic);
341341
} else
342-
#endif //SDL_JOYSTICK_HIDAPI
342+
#endif
343343
{
344344
// Close it, properly removing effects if needed
345345
for (i = 0; i < haptic->neffects; i++) {
@@ -382,7 +382,7 @@ void SDL_QuitHaptics(void)
382382

383383
#ifdef SDL_JOYSTICK_HIDAPI
384384
SDL_HIDAPI_HapticQuit();
385-
#endif //SDL_JOYSTICK_HIDAPI
385+
#endif
386386
SDL_SYS_HapticQuit();
387387
}
388388

@@ -449,7 +449,7 @@ int SDL_CreateHapticEffect(SDL_Haptic *haptic, const SDL_HapticEffect *effect)
449449
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
450450
return SDL_HIDAPI_HapticNewEffect(haptic, effect);
451451
}
452-
#endif //SDL_JOYSTICK_HIDAPI
452+
#endif
453453

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

492492
if (!ValidEffect(haptic, effect)) {
493493
return false;
@@ -520,7 +520,7 @@ bool SDL_RunHapticEffect(SDL_Haptic *haptic, int effect, Uint32 iterations)
520520
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
521521
return SDL_HIDAPI_HapticRunEffect(haptic, effect, iterations);
522522
}
523-
#endif //SDL_JOYSTICK_HIDAPI
523+
#endif
524524

525525
if (!ValidEffect(haptic, effect)) {
526526
return false;
@@ -542,7 +542,7 @@ bool SDL_StopHapticEffect(SDL_Haptic *haptic, int effect)
542542
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
543543
return SDL_HIDAPI_HapticStopEffect(haptic, effect);
544544
}
545-
#endif //SDL_JOYSTICK_HIDAPI
545+
#endif
546546

547547
if (!ValidEffect(haptic, effect)) {
548548
return false;
@@ -565,7 +565,7 @@ void SDL_DestroyHapticEffect(SDL_Haptic *haptic, int effect)
565565
SDL_HIDAPI_HapticDestroyEffect(haptic, effect);
566566
return;
567567
}
568-
#endif //SDL_JOYSTICK_HIDAPI
568+
#endif
569569

570570
if (!ValidEffect(haptic, effect)) {
571571
return;
@@ -587,7 +587,7 @@ bool SDL_GetHapticEffectStatus(SDL_Haptic *haptic, int effect)
587587
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
588588
return SDL_HIDAPI_HapticGetEffectStatus(haptic, effect);
589589
}
590-
#endif //SDL_JOYSTICK_HIDAPI
590+
#endif
591591

592592
if (!ValidEffect(haptic, effect)) {
593593
return false;
@@ -639,7 +639,7 @@ bool SDL_SetHapticGain(SDL_Haptic *haptic, int gain)
639639
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
640640
return SDL_HIDAPI_HapticSetGain(haptic, real_gain);
641641
}
642-
#endif //SDL_JOYSTICK_HIDAPI
642+
#endif
643643

644644
return SDL_SYS_HapticSetGain(haptic, real_gain);
645645
}
@@ -660,7 +660,7 @@ bool SDL_SetHapticAutocenter(SDL_Haptic *haptic, int autocenter)
660660
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
661661
return SDL_HIDAPI_HapticSetAutocenter(haptic, autocenter);
662662
}
663-
#endif //SDL_JOYSTICK_HIDAPI
663+
#endif
664664

665665
return SDL_SYS_HapticSetAutocenter(haptic, autocenter);
666666
}
@@ -677,7 +677,7 @@ bool SDL_PauseHaptic(SDL_Haptic *haptic)
677677
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
678678
return SDL_HIDAPI_HapticPause(haptic);
679679
}
680-
#endif //SDL_JOYSTICK_HIDAPI
680+
#endif
681681

682682
return SDL_SYS_HapticPause(haptic);
683683
}
@@ -694,7 +694,7 @@ bool SDL_ResumeHaptic(SDL_Haptic *haptic)
694694
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
695695
return SDL_HIDAPI_HapticResume(haptic);
696696
}
697-
#endif //SDL_JOYSTICK_HIDAPI
697+
#endif
698698

699699
return SDL_SYS_HapticResume(haptic);
700700
}
@@ -707,7 +707,7 @@ bool SDL_StopHapticEffects(SDL_Haptic *haptic)
707707
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
708708
return SDL_HIDAPI_HapticStopAll(haptic);
709709
}
710-
#endif //SDL_JOYSTICK_HIDAPI
710+
#endif
711711

712712
return SDL_SYS_HapticStopAll(haptic);
713713
}

src/haptic/hidapi/SDL_hidapihaptic.c

+14-10
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ typedef struct haptic_list_node
3737
static haptic_list_node *haptic_list_head = NULL;
3838
static SDL_Mutex *haptic_list_mutex = NULL;
3939

40-
SDL_HIDAPI_HapticDriver *drivers[] = {
40+
static SDL_HIDAPI_HapticDriver *drivers[] = {
4141
#ifdef SDL_HAPTIC_HIDAPI_LG4FF
4242
&SDL_HIDAPI_HapticDriverLg4ff,
43-
#endif //SDL_HAPTIC_HIDAPI_LG4FF
43+
#endif
44+
NULL
4445
};
4546

4647
bool SDL_HIDAPI_HapticInit()
@@ -76,16 +77,17 @@ bool SDL_HIDAPI_HapticIsHidapi(SDL_Haptic *haptic)
7677

7778
bool SDL_HIDAPI_JoystickIsHaptic(SDL_Joystick *joystick)
7879
{
80+
const int numdrivers = SDL_arraysize(drivers) - 1;
7981
int i;
8082

8183
SDL_AssertJoysticksLocked();
8284

83-
if (joystick->driver != &SDL_HIDAPI_JoystickDriver){
85+
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
8486
return false;
8587
}
8688

87-
for (i = 0; i < SDL_arraysize(drivers); ++i) {
88-
if(drivers[i]->JoystickSupported(joystick)) {
89+
for (i = 0; i < numdrivers; ++i) {
90+
if (drivers[i]->JoystickSupported(joystick)) {
8991
return true;
9092
}
9193
}
@@ -94,15 +96,17 @@ bool SDL_HIDAPI_JoystickIsHaptic(SDL_Joystick *joystick)
9496

9597
bool SDL_HIDAPI_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
9698
{
99+
const int numdrivers = SDL_arraysize(drivers) - 1;
97100
int i;
101+
98102
SDL_AssertJoysticksLocked();
99103

100-
if (joystick->driver != &SDL_HIDAPI_JoystickDriver){
104+
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
101105
return SDL_SetError("Cannot open hidapi haptic from non hidapi joystick");
102106
}
103107

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

129133
list_node = SDL_malloc(sizeof(haptic_list_node));
130-
if(list_node == NULL) {
134+
if (list_node == NULL) {
131135
device->driver->Close(device);
132136
SDL_free(device);
133137
return SDL_OutOfMemory();
@@ -175,7 +179,7 @@ bool SDL_HIDAPI_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
175179
SDL_HIDAPI_HapticDevice *device;
176180

177181
SDL_AssertJoysticksLocked();
178-
if (joystick->driver != &SDL_HIDAPI_JoystickDriver){
182+
if (joystick->driver != &SDL_HIDAPI_JoystickDriver) {
179183
return false;
180184
}
181185

0 commit comments

Comments
 (0)