Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Joystick button numbering doesn't always start at 0?  (Read 5614 times)

0 Members and 1 Guest are viewing this topic.

TheMaskedFox

  • Newbie
  • *
  • Posts: 7
    • View Profile
Joystick button numbering doesn't always start at 0?
« on: September 26, 2012, 04:35:49 am »
I'm coming over to SFML from an SDL background.  I'm porting a tool that was written in SDL and I've come across unexpected behavior when dealing with joysticks (on Mac).  I've got a wired 360 controller from which SDL will deliver button events numbered 0 through 14.  The A, B, X, and Y buttons are the 0 through 3.  However, when using SFML, those buttons are mapped to 16 through 19.

SFML appears to number the 360 controller's buttons as 5 through 19 and has no buttons that correspond to 0 through 3.  All 15 of the buttons still work, but it's really weird to have a joystick whose first button is 5 instead of 0.  Is it possible I've done something wrong in code or is this common and expected behavior in SFML?

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Joystick button numbering doesn't always start at 0?
« Reply #1 on: September 28, 2012, 06:15:45 pm »
I have Win 7 x64, SFML 2.0, wired 360 controller

button A = number 0
button B = number 1
button X = number 2
button Y = number 3
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Joystick button numbering doesn't always start at 0?
« Reply #2 on: September 28, 2012, 10:36:05 pm »
This must be a Mac issue, on Linux and Windows the button numbers are simply those assigned by the joystick driver.
Laurent Gomila - SFML developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Joystick button numbering doesn't always start at 0?
« Reply #3 on: September 29, 2012, 11:28:15 pm »
Ok so I'll have to improve https://github.com/SFML/SFML/blob/master/src/SFML/Window/OSX/JoystickImpl.cpp#L210 I suppose. Thanks for the feedback.
« Last Edit: September 29, 2012, 11:32:32 pm by Hiura »
SFML / OS X developer

codecranker

  • Guest
Re: Joystick button numbering doesn't always start at 0?
« Reply #4 on: September 30, 2012, 07:06:33 am »
hey. saw this post. I am also looking into using x360 controller input for my game. Haven't tried anything yet, may be its quite straightforward. I am curious to know how did you interface x360 input's with sfml? Does the sfml's joystick class directly reads from the USB port or wireless adapter or there is something extra that needs to be done?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Joystick button numbering doesn't always start at 0?
« Reply #5 on: September 30, 2012, 11:11:25 am »
You can directly use SFML 2.0 without extra work but keep in mind what TheMaskedFox said above for the Mac version.

Quote
SFML appears to number the 360 controller's buttons as 5 through 19 and has no buttons that correspond to 0 through 3.  All 15 of the buttons still work, but it's really weird to have a joystick whose first button is 5 instead of 0.
SFML / OS X developer

TheMaskedFox

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Joystick button numbering doesn't always start at 0?
« Reply #6 on: October 02, 2012, 08:18:45 am »
hey. saw this post. I am also looking into using x360 controller input for my game. Haven't tried anything yet, may be its quite straightforward. I am curious to know how did you interface x360 input's with sfml? Does the sfml's joystick class directly reads from the USB port or wireless adapter or there is something extra that needs to be done?

In Mac, I had to install a third-party driver from http://tattiebogle.net/index.php/ProjectRoot/Xbox360Controller/OsxDriver in order to use the 360 controller.  However, once the driver was installed, the 360 controller was recognized in SDL and SFML without any extra work.  It is treated like any other joystick.  In my case, I'm just watching for joystick events (JoystickButtonPressed, JoystickMoved) from pollEvent().  The only issue I've encountered is the button numbering one that this topic is about.

TheMaskedFox

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Joystick button numbering doesn't always start at 0?
« Reply #7 on: October 02, 2012, 08:30:55 am »
Ok so I'll have to improve https://github.com/SFML/SFML/blob/master/src/SFML/Window/OSX/JoystickImpl.cpp#L210 I suppose. Thanks for the feedback.

I've been digging into SDL, SFML, and the source code for the driver I'm using (which is http://tattiebogle.net/index.php/ProjectRoot/Xbox360Controller/OsxDriver).  Not being too familiar with any of the codebases, I'm not making much progress in figuring out why SDL sees the buttons as 0-15 and SFML sees them as 5 through 19.  It's a really weird "bug".  I find it highly unlikely that SDL specifically recognizes the 360 Mac driver and remaps 16, 17, 18, and 19 down to 0, 1, 2, and 3.  So that leaves me trying to determine why, when both SDL and SFML are looking at the same output from the same driver, they get two different button mappings.

I probably need to go through the task of hooking the SFML source into my Xcode project so that I can directly debug SFML's recognition of my controller.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Joystick button numbering doesn't always start at 0?
« Reply #8 on: October 02, 2012, 08:49:10 am »
I'll have a look when I've some free time (unfortunately not in the near future) at SDL code. But, tell me, which version of SDL did you try ?
SFML / OS X developer

TheMaskedFox

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Joystick button numbering doesn't always start at 0?
« Reply #9 on: October 02, 2012, 09:40:54 am »
I'll have a look when I've some free time (unfortunately not in the near future) at SDL code. But, tell me, which version of SDL did you try ?

It was SDL 2.0 (which I think is just 1.3 without the 1.2 compatibility...).

Nevermind that though.  I think I've found the problem and solved it.  I'm not a contributor to SFML, so I've got no idea what the proper process is for submitting a patch.  I would assume it starts with a fork on github.

In lieu of that, let me quickly explain what I did.  First off, I defined a sort predicate for the joystick buttons as:

Quote
bool JoystickButtonSortPredicate(IOHIDElementRef d1, IOHIDElementRef d2)
{
    return IOHIDElementGetUsage(d1) < IOHIDElementGetUsage(d2);
}

Then, after the for loop that goes through the elements, I added (truncated for-loop included):

Quote
    // Go through all connected elements.
    for (int i = 0; i < elementsCount; ++i) {
       ...
    }
   
    // Sort buttons according to HID Usage
    std::sort(m_buttons.begin(), m_buttons.end(), JoystickButtonSortPredicate);

In doing so, my buttons are all exactly where I expect them to be and mesh properly with SDL's interpretation of them.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Joystick button numbering doesn't always start at 0?
« Reply #10 on: October 02, 2012, 08:35:54 pm »
Thanks a lot!  :D

I put your solution on the tracker until I have enough time to test it out with my own devices.
SFML / OS X developer