libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
8.78k stars 1.66k forks source link

SDL_HapticOpen returns NULL and Error is set #3754

Closed SDLBugzilla closed 8 months ago

SDLBugzilla commented 3 years ago

This bug report was migrated from our old Bugzilla tracker.

Reported in version: 2.0.12 Reported for operating system, platform: Windows 10, x86

Comments on the original bug report:

On 2020-06-24 14:25:26 +0000, 18684092@students.lincoln.ac.uk wrote:

With 2 devices (Haptic Wheel and cheap game controller) plugged in and non haptic one has device index of 0 and haptic device has device index 1 a call to SDL_HapticOpen(1) returns NULL and an error is set.

The cause appears to be in SDL_haptic.c line 112 on:

112 / 113 Opens a Haptic device. 114 / 115 SDL_Haptic 116 SDL_HapticOpen(int device_index) 117 { 118 int i; 119 SDL_Haptic *haptic; 120 121 if ((device_index < 0) || (device_index >= SDL_numhaptics)) { 122 SDL_SetError("Haptic: There are %d haptic devices available", 123 SDL_numhaptics); 124 return NULL; 125 } 126 ....

int SDL_NumHaptics(void) returns 1 since there is one haptic device

The if statement therefore becomes true when (device_index >= SDL_numhaptics) condition is tested since the device_index (of 1) is the same as SDL_numhaptics and sets the error and returns null.

Using haptic = SDL_HapticOpenFromJoystick(joy); works OK Using haptic = SDL_HapticOpen(1); fails and GetError reports "Haptic: There are 1 haptic devices available"

Since you can open using "from joystick" "SDL_Joystick* joy" means there is a work around.

I presume the fix requires device_index >= SDL_numhaptics to be replaced with device_index > SDL_numhaptics?

I havent looked further into the solution.

On 2020-06-24 14:28:04 +0000, 18684092@students.lincoln.ac.uk wrote:

Thinking a little bit...

I presume the fix requires device_index >= SDL_numhaptics to be replaced with device_index > SDL_numhaptics?

is not the solution.

On 2020-06-24 14:50:58 +0000, 18684092@students.lincoln.ac.uk wrote:

Since the device index is the number and order of devices found (whether haptic or not) as per SDL wiki code:

/ Print the names of all attached joysticks / int num_joy, i; num_joy = SDL_NumJoysticks(); printf("%d joysticks found\n", num_joy); for(i = 0; i < num_joy; i++) { SDL_Joystick *joystick = SDL_JoystickOpen(i); printf("%s\n", SDL_JoystickName(joystick)); }

you can have a device list of:

ID haptic Name 0 no Cheapo controller 1 no Cheapo controller 2 yes G27 FFB Haptic wheel 3 no Some other joystic or wheel

The device_index should not be tested for being greater or equal to the number of haptic devices since obviously they do not need to correspond.

Perhaps the SDL_haptics[i] array should be the same size as the number of devices, not the number of haptic devices. The array could store whether the device was haptic or not. That way the device_index and the haptic_device_index would match (??? - thinking out allowed)

slouken commented 8 months ago

SDL 2.0 is now in maintenance mode, and all inactive issues are being closed. If this issue is impacting you, please feel free to reopen it with additional information.