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 4973

Summary: Mac: Joystick Plug/Unplug isn't working correctly
Product: SDL Reporter: rofferom
Component: joystickAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: major    
Priority: P2 Keywords: target-2.0.12
Version: 2.0.10   
Hardware: x86_64   
OS: Mac OS X (All)   
Attachments: Dirty patch

Description rofferom 2020-02-12 08:48:24 UTC
Created attachment 4206 [details]
Dirty patch

I have an annoying issue on MacOS about gamepad detection. Note that with the same code, I have no issue on Windows.

If a gamepad (USB or Bluetooth) is plugged before application start, I get the correct device count when doing:
    SDL_GameControllerUpdate();
    SDL_NumJoysticks();

However, if I unplug my device, I don't get an correct device count. If I plug another device, the deconnection of the first one will be detected at the same time.

I also made some tests on Chrome + https://html5gamepad.com. This test case is working perfectly.

I have compared the gamepad implementation of SDL vs Chromium :
* SDL: https://github.com/spurious/SDL-mirror/blob/c9f0264cf3a254128c1d406720c5a4bcce8485d5/src/joystick/hidapi/SDL_hidapijoystick.c#L219
* Chromium: https://cs.chromium.org/chromium/src/services/device/hid/hid_service_mac.cc?q=IOServiceAddMatchingNotification&sq=package:chromium&g=0&l=93

There is a difference : Chromium calls IOServiceAddMatchingNotification(kIOTerminatedNotification), and that isn't done by SDL. If I add the same call in SDL, the runtime plug/unplug detection is working correctly.

I have attached my dirty patch to the issue. It is working, but because I'm not a macOS developper, I'm pretty sure it is invalid and/or incomplete. Furthermore, I have copied some Chromium parameters that are different in SDL code.

Not sure about the real root cause of the issue, and about my patch correctness, but I think some macOS devs can confirm/reject my issue and provide a correct fix.
Comment 1 Sam Lantinga 2020-02-12 20:06:58 UTC
Ryan, can you look at this for the 2.0.12 release?
Comment 2 Ryan C. Gordon 2020-02-21 21:05:03 UTC
I'll take a look.

--ryan.
Comment 3 Sam Lantinga 2020-03-01 21:02:31 UTC
Your change is in, thanks!
https://hg.libsdl.org/SDL/rev/e1a1a5a3e551