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 4555

Summary: [PATCH] Add mapping for Chinese-made Xbox Controller
Product: SDL Reporter: benpicco <benpicco>
Component: joystickAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: trivial    
Priority: P2 Keywords: target-2.0.10
Version: HG 2.1   
Hardware: x86_64   
OS: Linux   
Attachments: mapping for "Chinese-made Xbox Controller"

Description benpicco 2019-03-17 22:56:09 UTC
Created attachment 3701 [details]
mapping for "Chinese-made Xbox Controller"

When shopping on eBay for an 'new'/fake Original Xbox controller one usually ends up with this device.
Linux knows it as 'Chinese-made Xbox Controller' and it comes with the device ID ffff:ffff.

It maps out just like the Xbox Controller S.
Comment 1 benpicco 2019-03-17 23:04:56 UTC
Originally I just wanted to add mappings for all Xbox controllers that xpad.c knows about (filtering out wheels, arcade sticks and dance mats). They all have the same buttons and are annoyingly mapped slightly wrong by default.

However, xpad.c only knows about vendorId and productId, the GUID also contains a version.

Most of the time it may be just 0, but not always (e.g. Microsoft X-Box pad v2 - the Xbox Controller S, but gamecontrollerdb.h already knows about that one).

Would it still be worthwhile to add those controllers still unknown to SDL (but known to Linux) assuming their version is 0 with the benefit of having a proper mapping if guessed right?
Comment 2 Ryan C. Gordon 2019-05-18 18:48:54 UTC
Tagging a bunch of bugs with "target-2.0.10" so we have a clear list of things to address before a 2.0.10 release.

Please note that "addressing" one of these bugs might mean deciding to defer on it until after 2.0.10, or resolving it as WONTFIX, etc. This is just here to tell us we should look at it carefully, and soon.

If you have new information or feedback on this issue, this is a good time to add it to the conversation, as we're likely to be paying attention to this specific report in the next few days/weeks.

Thanks!

--ryan.
Comment 3 Sam Lantinga 2019-06-08 22:01:22 UTC
This should be caught by the generic XInput controller handling in SDL_gamecontroller.c, on line 1039. Does that not work for you?
Comment 4 benpicco 2019-06-10 16:12:13 UTC
I just checked: It looks like the mapping is applied, but it's wrong.

IIRC this gives Xbox, Xbox 360 and Xbox One controllers the same mapping.
But Xbox Controllers are mapped slightly different by the kernel than Xbox 360 Controllers.

This is very unfortunate since they all (almost) have the same amount of buttons, it's just that the Xbox controllers have a White and a Black button (which xpad.c maps to BTN_C and BTN_Z) whereas the Xbox 360 pad has a left and right shoulder button instead (BTN_TR & BTN_TL). Xbox 360 Controllers also have an additional menu button.

Now ideally the Xbox Black & White buttons were mapped to BTN_TR & BTN_TL and nobody would have to care about the difference, but changing the mapping in the kernel would break all kinds of userspace mappings I'm afraid.

The problem with treating the black button as 'C' and the white one as 'Z' (which really only makes logical sense when looking at the 'Duke' controller) is that is messes up the order of the remaining buttons.

So I ran jstest-sdl and I can make two observations:

1: The mapping for the original Xbox controller and the more recent copy are indeed the same without manual configuration.
2: The mappings are wrong when compared to an Xbox 360 Controller.


Button | Xbox | Xbox 360
-------+------+---------
     0 |   A  |   A
     1 |   B  |   B
     2 | black|   X
     3 |   X  |   Y
     4 |   Y  |  TriggerL
     5 | white|  TriggerR
     6 | back |  back
     7 | start|  start
     8 |ThumbL|  menu
     9 |ThumbR|  ThumbL
    10 |   -  |  ThumbR


$ jstest-sdl --list
Found 3 joystick(s)

Joystick Name:     'Microsoft X-Box 360 pad'
Joystick GUID:     030000005e0400008e02000014010000
Joystick Number:    0
Number of Axes:     6
Number of Buttons: 11
Number of Hats:     1
Number of Balls:    0
GameController:
  Name:    'X360 Controller'
  Mapping: '030000005e0400008e02000014010000,X360 Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,'

Joystick Name:     'Chinese-made Xbox Controller'
Joystick GUID:     03000000ffff0000ffff000000010000
Joystick Number:    1
Number of Axes:     6
Number of Buttons: 10
Number of Hats:     1
Number of Balls:    0
GameController:
  Name:    'XInput Controller'
  Mapping: '(null)'

Joystick Name:     'Microsoft X-Box pad v2 (US)'
Joystick GUID:     030000005e0400008902000021010000
Joystick Number:    2
Number of Axes:     6
Number of Buttons: 10
Number of Hats:     1
Number of Balls:    0
GameController:
  Name:    'Microsoft X-Box pad v2 (US)'
  Mapping: '030000005e0400008902000021010000,Microsoft X-Box pad v2 (US),a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b5,leftstick:b8,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b2,rightstick:b9,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b3,y:b4,'
Comment 5 Sam Lantinga 2019-06-10 16:23:08 UTC
Okay, thanks for the information. Your patch is in!
https://hg.libsdl.org/SDL/rev/8e4a6cf76a8c
Comment 6 benpicco 2019-06-12 18:30:40 UTC
A simple way to check whether to apply a generic Xbox 360 mapping or a Xbox Classic mapping would be to check how much buttons the joystick has in SDL_PrivateGetControllerMappingForNameAndGUID().

OG Xbox has 10 buttons, Xbox 360 has 11.

But not all Xbox controllers do have 'Xbox' in their name.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/input/joystick/xpad.c#n113