Skip to content

Commit 9d6ef40

Browse files
sezeroKethen
authored andcommitted
cleanups after hidapi input and FFB support for Logitech G29 patch:
- add new files to Makefile.os2 and Makefile.w32 - fix negative return check from SDL_hid_read() - fix SDL_HIDAPI_HapticDestroyEffect() as it returns void - fix C89 build issues - change an sprintf() call to SDL_snprintf() - fix a -Wuninitialized warning - add casts Sint32 and float casts to avoid warnings - add missing newlines to new files
1 parent 1ffc307 commit 9d6ef40

File tree

8 files changed

+37
-29
lines changed

8 files changed

+37
-29
lines changed

Makefile.os2

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ SRCS+= SDL_systimer.c
9494
SRCS+= SDL_sysloadso.c
9595
SRCS+= SDL_sysfilesystem.c
9696
SRCS+= SDL_os2joystick.c SDL_syshaptic.c SDL_sysjoystick.c SDL_virtualjoystick.c
97-
SRCS+= SDL_hidapijoystick.c SDL_hidapi_rumble.c SDL_hidapi_combined.c SDL_hidapi_gamecube.c SDL_hidapi_luna.c SDL_hidapi_ps3.c SDL_hidapi_ps4.c SDL_hidapi_ps5.c SDL_hidapi_shield.c SDL_hidapi_stadia.c SDL_hidapi_switch.c SDL_hidapi_wii.c SDL_hidapi_xbox360.c SDL_hidapi_xbox360w.c SDL_hidapi_xboxone.c SDL_hidapi_steam.c SDL_hidapi_steamdeck.c SDL_steam_virtual_gamepad.c
97+
SRCS+= SDL_hidapihaptic.c SDL_hidapihaptic_lg4ff.c
98+
SRCS+= SDL_hidapijoystick.c SDL_hidapi_rumble.c SDL_hidapi_combined.c SDL_hidapi_gamecube.c SDL_hidapi_luna.c SDL_hidapi_ps3.c SDL_hidapi_ps4.c SDL_hidapi_ps5.c SDL_hidapi_shield.c SDL_hidapi_stadia.c SDL_hidapi_switch.c SDL_hidapi_wii.c SDL_hidapi_xbox360.c SDL_hidapi_xbox360w.c SDL_hidapi_xboxone.c SDL_hidapi_steam.c SDL_hidapi_steamdeck.c SDL_hidapi_lg4ff.c SDL_steam_virtual_gamepad.c
9899
SRCS+= SDL_dummyaudio.c SDL_diskaudio.c
99100
SRCS+= SDL_nullvideo.c SDL_nullframebuffer.c SDL_nullevents.c
100101
SRCS+= SDL_dummysensor.c
@@ -116,7 +117,7 @@ OBJS = $(SRCS:.c=.obj)
116117
.extensions:
117118
.extensions: .lib .dll .obj .c .asm
118119

119-
.c: ./src;./src/dynapi;./src/audio;./src/cpuinfo;./src/events;./src/file;./src/haptic;./src/joystick;./src/power;./src/render;./src/render/software;./src/sensor;./src/stdlib;./src/thread;./src/timer;./src/video;./src/video/yuv2rgb;./src/atomic;./src/audio/disk;
120+
.c: ./src;./src/dynapi;./src/audio;./src/cpuinfo;./src/events;./src/file;./src/haptic;./src/haptic/hidapi;./src/joystick;./src/power;./src/render;./src/render/software;./src/sensor;./src/stdlib;./src/thread;./src/timer;./src/video;./src/video/yuv2rgb;./src/atomic;./src/audio/disk;
120121
.c: ./src/haptic/dummy;./src/joystick/dummy;./src/joystick/virtual;./src/audio/dummy;./src/video/dummy;./src/sensor/dummy;
121122
.c: ./src/core/os2;./src/audio/os2;./src/loadso/os2;./src/filesystem/os2;./src/joystick/os2;./src/thread/os2;./src/timer/os2;./src/video/os2;
122123
.c: ./src/core/os2/geniconv;

Makefile.w32

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ SRCS+= SDL_systimer.c
7373
SRCS+= SDL_sysloadso.c
7474
SRCS+= SDL_sysfilesystem.c
7575
SRCS+= SDL_syshaptic.c SDL_sysjoystick.c SDL_virtualjoystick.c
76-
SRCS+= SDL_hidapijoystick.c SDL_hidapi_rumble.c SDL_hidapi_combined.c SDL_hidapi_gamecube.c SDL_hidapi_luna.c SDL_hidapi_ps3.c SDL_hidapi_ps4.c SDL_hidapi_ps5.c SDL_hidapi_shield.c SDL_hidapi_stadia.c SDL_hidapi_switch.c SDL_hidapi_wii.c SDL_hidapi_xbox360.c SDL_hidapi_xbox360w.c SDL_hidapi_xboxone.c SDL_hidapi_steam.c SDL_hidapi_steamdeck.c
76+
SRCS+= SDL_hidapihaptic.c SDL_hidapihaptic_lg4ff.c
77+
SRCS+= SDL_hidapijoystick.c SDL_hidapi_rumble.c SDL_hidapi_combined.c SDL_hidapi_gamecube.c SDL_hidapi_luna.c SDL_hidapi_ps3.c SDL_hidapi_ps4.c SDL_hidapi_ps5.c SDL_hidapi_shield.c SDL_hidapi_stadia.c SDL_hidapi_switch.c SDL_hidapi_wii.c SDL_hidapi_xbox360.c SDL_hidapi_xbox360w.c SDL_hidapi_xboxone.c SDL_hidapi_steam.c SDL_hidapi_steamdeck.c SDL_hidapi_lg4ff.c
7778
SRCS+= SDL_dummyaudio.c SDL_diskaudio.c
7879
SRCS+= SDL_nullvideo.c SDL_nullframebuffer.c SDL_nullevents.c
7980
SRCS+= SDL_dummysensor.c
@@ -105,7 +106,7 @@ RCOBJS= $(RCSRCS:.rc=.res)
105106
.extensions:
106107
.extensions: .lib .dll .obj .res .c .rc .asm
107108

108-
.c: ./src;./src/dynapi;./src/audio;./src/cpuinfo;./src/events;./src/file;./src/haptic;./src/joystick;./src/power;./src/render;./src/render/software;./src/sensor;./src/stdlib;./src/thread;./src/timer;./src/video;./src/video/yuv2rgb;./src/atomic;./src/audio/disk;
109+
.c: ./src;./src/dynapi;./src/audio;./src/cpuinfo;./src/events;./src/file;./src/haptic;./src/haptic/hidapi;./src/joystick;./src/power;./src/render;./src/render/software;./src/sensor;./src/stdlib;./src/thread;./src/timer;./src/video;./src/video/yuv2rgb;./src/atomic;./src/audio/disk;
109110
.c: ./src/haptic/dummy;./src/joystick/dummy;./src/joystick/virtual;./src/audio/dummy;./src/video/dummy;./src/sensor/dummy;
110111
.c: ./src/core/windows;./src/audio/winmm;./src/audio/directsound;./src/audio/wasapi;./src/loadso/windows;./src/filesystem/windows;./src/haptic/windows;./src/joystick/windows;./src/sensor/windows;./src/thread/windows;./src/timer/windows;./src/video/windows;
111112
.c: ./src/locale/;./src/locale/windows;./src/misc;./src/misc/windows;./src/power/windows;./src/joystick/hidapi;./src/hidapi;./src/render/direct3d;./src/render/direct3d11;./src/render/direct3d12;./src/render/opengl;./src/render/opengles2

src/haptic/SDL_haptic.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ void SDL_HapticDestroyEffect(SDL_Haptic *haptic, int effect)
644644

645645
#ifdef SDL_JOYSTICK_HIDAPI
646646
if (SDL_HIDAPI_HapticIsHidapi(haptic)) {
647-
return SDL_HIDAPI_HapticDestroyEffect(haptic, effect);
647+
SDL_HIDAPI_HapticDestroyEffect(haptic, effect);
648+
return;
648649
}
649650
#endif //SDL_JOYSTICK_HIDAPI
650651

src/haptic/SDL_hidapihaptic.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ int SDL_HIDAPI_HapticPause(SDL_Haptic *haptic);
4545
int SDL_HIDAPI_HapticUnpause(SDL_Haptic *haptic);
4646
int SDL_HIDAPI_HapticStopAll(SDL_Haptic *haptic);
4747

48-
#endif //SDL_hidapihaptic_h_
48+
#endif /* SDL_hidapihaptic_h_ */
49+

src/haptic/hidapi/SDL_hidapihaptic.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ void SDL_HIDAPI_HapticClose(SDL_Haptic *haptic)
192192
SDL_LockMutex(haptic_list_mutex);
193193

194194
cur = haptic_list_head;
195+
last = NULL;
195196
while (cur != NULL) {
196197
if (cur->haptic == haptic) {
197198
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
@@ -256,7 +257,7 @@ int SDL_HIDAPI_HapticStopEffect(SDL_Haptic *haptic, int id)
256257
void SDL_HIDAPI_HapticDestroyEffect(SDL_Haptic *haptic, int id)
257258
{
258259
SDL_HIDAPI_HapticDevice *device = (SDL_HIDAPI_HapticDevice *)haptic->hwdata;
259-
return device->driver->DestroyEffect(device, id);
260+
device->driver->DestroyEffect(device, id);
260261
}
261262

262263
int SDL_HIDAPI_HapticGetEffectStatus(SDL_Haptic *haptic, int id)
@@ -295,4 +296,4 @@ int SDL_HIDAPI_HapticStopAll(SDL_Haptic *haptic)
295296
return device->driver->StopAll(device);
296297
}
297298

298-
#endif //SDL_JOYSTICK_HIDAPI
299+
#endif /* SDL_JOYSTICK_HIDAPI */

src/haptic/hidapi/SDL_hidapihaptic_c.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ struct SDL_HIDAPI_HapticDriver
7070

7171
extern SDL_HIDAPI_HapticDriver SDL_HIDAPI_HapticDriverLg4ff;
7272

73-
#endif //SDL_joystick_c_h_
73+
#endif /* SDL_joystick_c_h_ */

src/haptic/hidapi/SDL_hidapihaptic_lg4ff.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static Uint16 to_linux_direction(SDL_HapticDirection *src)
191191
} else if (!src->dir[0]) {
192192
return (Uint16) (src->dir[1] >= 0 ? 0x8000 : 0);
193193
} else {
194-
float f = SDL_atan2(src->dir[1], src->dir[0]); /* Ideally we'd use fixed point math instead of floats... */
194+
float f = (float) SDL_atan2(src->dir[1], src->dir[0]); /* Ideally we'd use fixed point math instead of floats... */
195195
/*
196196
SDL_atan2 takes the parameters: Y-axis-value and X-axis-value (in that order)
197197
- Y-axis-value is the second coordinate (from center to SOUTH)
@@ -405,9 +405,9 @@ static Sint32 lg4ff_calculate_constant(struct lg4ff_effect_state *state)
405405
if (state->time_playing < constant->attack_length) {
406406
level_sign = level < 0 ? -1 : 1;
407407
d = level - level_sign * constant->attack_level;
408-
level = level_sign * constant->attack_level + d * state->time_playing / constant->attack_length;
408+
level = (Sint32) (level_sign * constant->attack_level + d * state->time_playing / constant->attack_length);
409409
} else if (constant->length && constant->fade_length) {
410-
t = state->time_playing - constant->length + constant->fade_length;
410+
t = (Sint32) (state->time_playing - constant->length + constant->fade_length);
411411
if (t > 0) {
412412
level_sign = level < 0 ? -1 : 1;
413413
d = level - level_sign * constant->fade_level;
@@ -428,17 +428,17 @@ static Sint32 lg4ff_calculate_ramp(struct lg4ff_effect_state *state)
428428
if (state->time_playing < ramp->attack_length) {
429429
level = ramp->start;
430430
level_sign = level < 0 ? -1 : 1;
431-
t = ramp->attack_length - state->time_playing;
431+
t = (Sint32) (ramp->attack_length - state->time_playing);
432432
d = level - level_sign * ramp->attack_level;
433433
level = level_sign * ramp->attack_level + d * t / ramp->attack_length;
434434
} else if (ramp->length && state->time_playing >= ramp->length - ramp->fade_length && ramp->fade_length) {
435435
level = ramp->end;
436436
level_sign = level < 0 ? -1 : 1;
437-
t = state->time_playing - ramp->length + ramp->fade_length;
437+
t = (Sint32) (state->time_playing - ramp->length + ramp->fade_length);
438438
d = level_sign * ramp->fade_level - level;
439439
level = level - d * t / ramp->fade_length;
440440
} else {
441-
t = state->time_playing - ramp->attack_length;
441+
t = (Sint32) (state->time_playing - ramp->attack_length);
442442
level = ramp->start + ((t * state->slope) >> 16);
443443
}
444444

@@ -455,9 +455,9 @@ static Sint32 lg4ff_calculate_periodic(struct lg4ff_effect_state *state)
455455

456456
if (state->time_playing < periodic->attack_length) {
457457
d = magnitude - magnitude_sign * periodic->attack_level;
458-
magnitude = magnitude_sign * periodic->attack_level + d * state->time_playing / periodic->attack_length;
458+
magnitude = (Sint32) (magnitude_sign * periodic->attack_level + d * state->time_playing / periodic->attack_length);
459459
} else if (periodic->length && periodic->fade_length) {
460-
t = state->time_playing - get_effect_replay_length(&state->effect) + periodic->fade_length;
460+
t = (Sint32) (state->time_playing - get_effect_replay_length(&state->effect) + periodic->fade_length);
461461
if (t > 0) {
462462
d = magnitude - magnitude_sign * periodic->fade_level;
463463
magnitude = magnitude - d * t / periodic->fade_length;
@@ -474,7 +474,7 @@ static Sint32 lg4ff_calculate_periodic(struct lg4ff_effect_state *state)
474474
break;
475475
*/
476476
case SDL_HAPTIC_TRIANGLE:
477-
level += llabs((Sint64)state->phase * magnitude * 2 / 360 - magnitude) * 2 - magnitude;
477+
level += (Sint32) (llabs((Sint64)state->phase * magnitude * 2 / 360 - magnitude) * 2 - magnitude);
478478
break;
479479
case SDL_HAPTIC_SAWTOOTHUP:
480480
level += state->phase * magnitude * 2 / 360 - magnitude;
@@ -762,10 +762,11 @@ static SDL_bool SDL_HIDAPI_HapticDriverLg4ff_JoystickSupported(SDL_Joystick *joy
762762
{
763763
Uint16 vendor_id = SDL_JoystickGetVendor(joystick);
764764
Uint16 product_id = SDL_JoystickGetProduct(joystick);
765+
int i;
765766
if (vendor_id != USB_VENDOR_ID_LOGITECH) {
766767
return SDL_FALSE;
767768
}
768-
for (int i = 0;i < sizeof(supported_device_ids) / sizeof(Uint32);i++) {
769+
for (i = 0; i < sizeof(supported_device_ids) / sizeof(Uint32); i++) {
769770
if (supported_device_ids[i] == product_id) {
770771
return SDL_TRUE;
771772
}
@@ -840,7 +841,7 @@ static void *SDL_HIDAPI_HapticDriverLg4ff_Open(SDL_Joystick *joystick)
840841

841842
ctx->product_id = SDL_JoystickGetProduct(joystick);
842843

843-
sprintf(ctx->thread_name_buf, "SDL_hidapihaptic_lg4ff 0x%16llx %04x:%04x", (Uint64)joystick, USB_VENDOR_ID_LOGITECH, ctx->product_id);
844+
SDL_snprintf(ctx->thread_name_buf, sizeof(ctx->thread_name_buf), "SDL_hidapihaptic_lg4ff %d %04x:%04x", SDL_JoystickInstanceID(joystick), USB_VENDOR_ID_LOGITECH, ctx->product_id);
844845
ctx->stop_thread = SDL_FALSE;
845846
ctx->thread = SDL_CreateThread(SDL_HIDAPI_HapticDriverLg4ff_ThreadFunction, ctx->thread_name_buf, ctx);
846847

@@ -1179,5 +1180,5 @@ SDL_HIDAPI_HapticDriver SDL_HIDAPI_HapticDriverLg4ff = {
11791180
SDL_HIDAPI_HapticDriverLg4ff_StopAll,
11801181
};
11811182

1182-
#endif //SDL_HAPTIC_HIDAPI_LG4FF
1183-
#endif //SDL_JOYSTICK_HIDAPI
1183+
#endif /* SDL_HAPTIC_HIDAPI_LG4FF */
1184+
#endif /* SDL_JOYSTICK_HIDAPI */

src/joystick/hidapi/SDL_hidapi_lg4ff.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ static const char *supported_device_names[] = {
6262

6363
static const char *HIDAPI_DriverLg4ff_GetDeviceName(Uint32 device_id)
6464
{
65-
for (int i = 0;i < (sizeof supported_device_ids) / sizeof(Uint32);i++) {
65+
int i;
66+
for (i = 0; i < (sizeof supported_device_ids) / sizeof(Uint32); i++) {
6667
if (supported_device_ids[i] == device_id) {
6768
return supported_device_names[i];
6869
}
@@ -139,10 +140,11 @@ static SDL_bool HIDAPI_DriverLg4ff_IsSupportedDevice(
139140
should trigger mode switch, then return false, so the next probe cycle
140141
would take the device on a supported mode
141142
*/
143+
int i;
142144
if (vendor_id != USB_VENDOR_ID_LOGITECH) {
143145
return SDL_FALSE;
144146
}
145-
for (int i = 0;i < sizeof(supported_device_ids) / sizeof(Uint32);i++) {
147+
for (i = 0; i < sizeof(supported_device_ids) / sizeof(Uint32); i++) {
146148
if (supported_device_ids[i] == product_id) {
147149
return SDL_TRUE;
148150
}
@@ -382,6 +384,7 @@ static SDL_bool HIDAPI_DriverLg4ff_HandleState(SDL_HIDAPI_Device *device,
382384
Uint8 last_hat = 0;
383385
int num_buttons = HIDAPI_DriverLg4ff_GetNumberOfButtons(device->product_id);
384386
int bit_offset = 0;
387+
int i;
385388

386389
SDL_bool state_changed;
387390

@@ -458,7 +461,7 @@ static SDL_bool HIDAPI_DriverLg4ff_HandleState(SDL_HIDAPI_Device *device,
458461
SDL_assert(0);
459462
}
460463

461-
for (int i = 0;i < num_buttons;i++) {
464+
for (i = 0;i < num_buttons;i++) {
462465
int bit_num = bit_offset + i;
463466
SDL_bool button_on = HIDAPI_DriverLg4ff_GetBit(report_buf, bit_num, report_size);
464467
SDL_bool button_was_on = HIDAPI_DriverLg4ff_GetBit(ctx->last_report_buf, bit_num, report_size);
@@ -599,8 +602,8 @@ static int SDL_HIDAPI_DriverLg4ff_GetEnvInt(const char *env_name, int min, int m
599602
static SDL_bool HIDAPI_DriverLg4ff_UpdateDevice(SDL_HIDAPI_Device *device)
600603
{
601604
SDL_Joystick *joystick = NULL;
602-
size_t r;
603605
Uint8 report_buf[32] = {0};
606+
int r;
604607
size_t report_size = 0;
605608
SDL_DriverLg4ff_Context *ctx = (SDL_DriverLg4ff_Context *)device->context;
606609

@@ -639,7 +642,7 @@ static SDL_bool HIDAPI_DriverLg4ff_UpdateDevice(SDL_HIDAPI_Device *device)
639642
/* Failed to read from controller */
640643
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
641644
return SDL_FALSE;
642-
} else if (r == report_size) {
645+
} else if ((size_t)r == report_size) {
643646
SDL_bool state_changed = HIDAPI_DriverLg4ff_HandleState(device, joystick, report_buf, report_size);
644647
if(state_changed && !ctx->initialized) {
645648
ctx->initialized = SDL_TRUE;
@@ -799,7 +802,6 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverLg4ff = {
799802
HIDAPI_DriverLg4ff_FreeDevice,
800803
};
801804

802-
803805
#endif /* SDL_JOYSTICK_HIDAPI_LG4FF */
804806

805-
#endif /* SDL_JOYSTICK_HIDAPI */
807+
#endif /* SDL_JOYSTICK_HIDAPI */

0 commit comments

Comments
 (0)