Skip to content

Commit 02624a2

Browse files
authored
Avoid taking an exclusive lock on Litra devices on macOS (#93)
Currently, when a Litra handle device is opened with `open()`, on macOS we take an exclusive lock on the device, which stops it being managed by other applications. This isn't a problem if the device handle is quickly closed again (e.g. if the application exits), but it can be annoying if you have a long-running application that holds onto the device handle for a long time (e.g. `litra auto-toggle`). As an example, if you run `litra auto-toggle` and then try to turn the device on or off with `litra on` or `litra off` in another shell, it would fail with an error: > HID error occurred: hidapi error: hid_open_path: failed to open IOHIDDevice from mach entry: (0xE00002C5) (iokit/common) exclusive access and device already open This switches to taking a non-exclusive lock on macOS, affecting use of this code as a Rust library and our long-running `auto-toggle` command.
1 parent 6f3db81 commit 02624a2

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ impl fmt::Debug for Litra {
4949
impl Litra {
5050
/// Initialize a new Litra context.
5151
pub fn new() -> DeviceResult<Self> {
52-
Ok(HidApi::new().map(Litra)?)
52+
let hidapi = HidApi::new()?;
53+
#[cfg(target_os = "macos")]
54+
hidapi.set_open_exclusive(false);
55+
Ok(Litra(hidapi))
5356
}
5457

5558
/// Returns an [`Iterator`] of connected devices supported by this library.
@@ -177,7 +180,7 @@ impl Device<'_> {
177180
}
178181

179182
/// Opens the device and returns a [`DeviceHandle`] that can be used for getting and setting the
180-
/// device status.
183+
/// device status. On macOS, this will open the device in non-exclusive mode.
181184
pub fn open(&self, context: &Litra) -> DeviceResult<DeviceHandle> {
182185
let hid_device = self.device_info.open_device(context.hidapi())?;
183186
Ok(DeviceHandle {

0 commit comments

Comments
 (0)