Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiimoteReal: Fix Wiimote disconnection causing Dolphin to crash on macOS #10014

Merged
merged 4 commits into from Sep 13, 2021

Conversation

OatmealDome
Copy link
Member

When disconnecting a Wiimote on macOS (whether by the Bluetooth menu, by pulling out the batteries, etc...), Dolphin may crash in hidapi code.

The first commit changes the device removal callback to use CFRunLoopGetCurrent() instead of getting the run loop ref from the context. For whatever reason (macOS bug?), while the callback is deregistered for closed hid_devices in hid_close(), IOKit may occasionally call the callback with hid_device pointers that have already been closed and free'd. Because the device removal callback is always ran on the device's run loop, it is safer to use CFRunLoopGetCurrent() instead of accessing context->run_loop.

The second commit ports a patch from libusb's fork of hidapi. On really old versions of macOS, calling IOHIDDeviceClose() on a handle to a device that has been physically removed from the system may cause a crash. The workaround was to not call IOHIDDeviceClose() on disconnected devices. While this bug has apparently been fixed on some version since then, the behaviour has since changed. Now, there may be a crash on macOS Catalina and higher if IOHIDDeviceClose() is not called. The patch makes hid_close() always call IOHIDDeviceClose() on modern versions of macOS.

The third commit fixes a minor bug. WiimoteReal::IORead ignores all read results, including errors, if the real Wiimote isn't linked. This causes lots of log spam if a Wiimote is disconnected while no game is running. The fix is to move the error check before the link check.

I've connected and disconnected a Wiimote 20+ times on my Mac, and Dolphin has not crashed once with this PR.


These hidapi patches have not been submitted upstream, as signall11 has apparently abandoned hidapi. I have tried updating the code in Externals to libusb's fork, but Wiimote connection never worked for me. I suspect it is related to the ongoing issue with how paths are generated on newer versions (hidapi now uses IORegistry path strings, which are the same between all Bluetooth HID devices).

Because of this, I think it is better to just patch the version in Externals as it still works fine on current macOS.

@leoetlino leoetlino merged commit ee863e6 into dolphin-emu:master Sep 13, 2021
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants