We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5212 - SDL_HapticOpen returns NULL and Error is set
Summary: SDL_HapticOpen returns NULL and Error is set
Status: NEW
Alias: None
Product: SDL
Classification: Unclassified
Component: haptic (show other bugs)
Version: 2.0.12
Hardware: x86 Windows 10
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-06-24 14:25 UTC by 18684092@students.lincoln.ac.uk
Modified: 2020-06-24 14:50 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description 18684092@students.lincoln.ac.uk 2020-06-24 14:25:26 UTC
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.
Comment 1 18684092@students.lincoln.ac.uk 2020-06-24 14:28:04 UTC
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.
Comment 2 18684092@students.lincoln.ac.uk 2020-06-24 14:50:58 UTC
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)