From 2442c85cb89c262672b337b2555fff77cf770478 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 29 Apr 2025 11:21:26 -0700 Subject: [PATCH] Call IOHIDDeviceClose() if needed in hid_close() on macOS Unregistering the input report callback marks the device as disconnected, so IOHIDDeviceClose() would never be called if the device wasn't already disconnected when hid_close() was called. Fixes https://github.com/libsdl-org/SDL/issues/12255 --- src/hidapi/mac/hid.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/hidapi/mac/hid.c b/src/hidapi/mac/hid.c index 0dbe4227d2..24e0b8ac2a 100644 --- a/src/hidapi/mac/hid.c +++ b/src/hidapi/mac/hid.c @@ -1135,11 +1135,14 @@ int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, void HID_API_EXPORT hid_close(hid_device *dev) { + int disconnected; + if (!dev) return; /* Disconnect the report callback before close. */ - if (!dev->disconnected) { + disconnected = dev->disconnected; + if (!disconnected) { IOHIDDeviceRegisterInputReportCallback( dev->device_handle, dev->input_report_buf, dev->max_input_report_len, NULL, dev); @@ -1163,7 +1166,7 @@ void HID_API_EXPORT hid_close(hid_device *dev) /* Close the OS handle to the device, but only if it's not been unplugged. If it's been unplugged, then calling IOHIDDeviceClose() will crash. */ - if (!dev->disconnected) { + if (!disconnected) { IOHIDDeviceClose(dev->device_handle, kIOHIDOptionsTypeNone); }