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 187 - SDL incorrectly reports Joystick Device names.
Summary: SDL incorrectly reports Joystick Device names.
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: joystick (show other bugs)
Version: 1.2.9
Hardware: x86 Windows (XP)
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-03-29 14:22 UTC by Daniel Broschart
Modified: 2006-04-29 16:22 UTC (History)
2 users (show)

See Also:


Attachments
Patch for src/joystick/win32/SDL_mmjoystick.c (1.63 KB, patch)
2006-04-17 03:08 UTC, Max Horn
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Broschart 2006-03-29 14:22:54 UTC
SDL incorrectly reports Joystick Device names.

Right now on my dev machine which has a CH Fighterstick USB (19 buttons) and a Nostromo N52 Speedpad (24 buttons) connected, and when my application runs it normally reported the following:

Nostromo n52 Speedpad2
Opened Joystick 0
Name: Nostromo n52 Speedpad2
Number of Axes: 6
Number of Buttons: 24
Number of Balls: 0

CH FIGHTERSTICK USB 
Opened Joystick 1
Name: CH FIGHTERSTICK USB 
Number of Axes: 3
Number of Buttons: 19
Number of Balls: 0

but now it reports the following:
 

Microsoft PC-joystick driver
Opened Joystick 0
Name: Microsoft PC-joystick driver
Number of Axes: 6
Number of Buttons: 24
Number of Balls: 0

Nostromo n52 Speedpad2
Opened Joystick 1
Name: Nostromo n52 Speedpad2
Number of Axes: 3
Number of Buttons: 19
Number of Balls: 0

Please note that not only has the Nostromo name been associated with the wrong joystick, but the CH Fighterstick Name doesn't show up anymore.  I cannot recall exactly what occurred between the two executions, but numerous things, such as hot swapping of joysticks, adding other joysticks (such as the IP Desktop which I will refer to later), rebooting have been performed. The above problem is fully reproducible as long as my computer has both joysticks plugged in; even after reboots, or even after a reboot with no joysticks initially plugged in.

If I run my application with the CH Fighterstick unplugged I get the following results:

Microsoft PC-joystick driver
Opened Joystick 0
Name: Microsoft PC-joystick driver
Number of Axes: 6
Number of Buttons: 24
Number of Balls: 0

So it is correctly identifying the number of buttons for the Nostromo that is plugged in, but the name is wrong.

Now when I plug in an IP Desktop joystick (12 buttons) along with the other 2, I get the following results:

CH PRODUCTS IP DESKTOP CONTROLLER
Opened Joystick 0
Name: CH PRODUCTS IP DESKTOP CONTROLLER
Number of Axes: 3
Number of Buttons: 12
Number of Balls: 0

Nostromo n52 Speedpad2
Opened Joystick 1
Name: Nostromo n52 Speedpad2
Number of Axes: 6
Number of Buttons: 24
Number of Balls: 0

CH FIGHTERSTICK USB 
Opened Joystick 2
Name: CH FIGHTERSTICK USB 
Number of Axes: 3
Number of Buttons: 19
Number of Balls: 0

Amazing, the numbers and names are correct! It seems like the enumeration of joysticks and the joystick name enumerations are off, and once they are off they stay that way.

The joysticks in the Windows Control Panel always show up correctly named.
Comment 1 Max Horn 2006-04-17 03:08:38 UTC
Created attachment 106 [details]
Patch for src/joystick/win32/SDL_mmjoystick.c

I am not even a Windows user, so take the following with a grain of salt:

SDL_mmjoystick.c  has a function GetJoystickName which obtains the joystick name by looking at the registry. The way it does that seems very fishy to me. Namely, it uses the parameter "index" to construct a registry value name (BTW, those variables used in the code are really badly named). The value of "index" in turn equals the current value of "numdevs", as called from SDL_SYS_JoystickInit. 

I read through the MSDN docs at <http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarinput/html/msdn_extdirect.asp>, and I believe the simple fix is to replace line 183 of said file
  SYS_JoystickName[numdevs] = GetJoystickName(numdevs, joycaps.szRegKey);
by the following:
  SYS_JoystickName[numdevs] = GetJoystickName(SYS_JoystickID[i], joycaps.szRegKey);

However, that is only *hiding* the real issue. Problem is, the list of joysticks as returned by windows may contains "gaps", and the code deals incorrectly with that. Namely those gaps occur if joysticks are removed/(re)added, as the reporter observed.

The attached patch fixes this and another (off-by-one) issue in the code. But since I have no Windows machine, I can't even test-compile it, so use with caution.
Comment 2 Daniel Broschart 2006-04-28 16:16:37 UTC
Yes, that patch does indeed work on my Windows XP system.
Comment 3 Max Horn 2006-04-28 18:37:37 UTC
It does? Wow, cool. I didn't expect it to even compile :-).

Ryan, Sam, any chance this could get checked in then? Possibly after some more testing / review by somebody who actual knows something about coding for Windows :-).
Comment 4 Sam Lantinga 2006-04-29 16:22:39 UTC
Looks good, thanks Max!