From beb27a628c66cea54c520763028b5e4a96c21008 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 11 Mar 2026 16:35:58 -0700 Subject: [PATCH] Expose four macOS specific functions There are four macOS/Darwin specific functions exposed by libhidapi on macOS. The most interesting among them is hid_darwin_set_open_exclusive() which is necessary to allow concurrent access to devices. Add code necessary to expose those APIs. --- hid/__init__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/hid/__init__.py b/hid/__init__.py index 69d82eb..abbb43d 100644 --- a/hid/__init__.py +++ b/hid/__init__.py @@ -2,6 +2,7 @@ import ctypes import atexit import enum +import sys __all__ = ['HIDException', 'DeviceInfo', 'Device', 'enumerate', 'BusType'] @@ -146,6 +147,18 @@ def as_dict(self): hidapi.hid_error.argtypes = [ctypes.c_void_p] hidapi.hid_error.restype = ctypes.c_wchar_p +if version >= (0, 12, 0) and sys.platform == 'darwin': + hidapi.hid_darwin_get_location_id.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint32)] + hidapi.hid_darwin_get_location_id.restype = ctypes.c_int + + hidapi.hid_darwin_set_open_exclusive.argtypes = [ctypes.c_int] + hidapi.hid_darwin_set_open_exclusive.restype = None + + hidapi.hid_darwin_get_open_exclusive.argtypes = None + hidapi.hid_darwin_get_open_exclusive.restype = ctypes.c_int + + hidapi.hid_darwin_is_device_open_exclusive.argtypes = [ctypes.c_void_p] + hidapi.hid_darwin_is_device_open_exclusive.restype = ctypes.c_int def enumerate(vid=0, pid=0): ret = [] @@ -249,6 +262,14 @@ def close(self): hidapi.hid_close(self.__dev) self.__dev = None + if version >= (0, 12, 0) and sys.platform == 'darwin': + @property + def location_id(self): + loc_id = ctypes.c_uint32() + self.__hidcall(hidapi.hid_darwin_get_location_id, self.__dev, ctypes.byref(loc_id)) + + return loc_id.value + @property def nonblocking(self): return getattr(self, '_nonblocking', 0)