Skip to content

Commit

Permalink
Avoid taking an exclusive lock on Litra devices on macOS (#93)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timrogers authored Oct 3, 2024
1 parent 6f3db81 commit 02624a2
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ impl fmt::Debug for Litra {
impl Litra {
/// Initialize a new Litra context.
pub fn new() -> DeviceResult<Self> {
Ok(HidApi::new().map(Litra)?)
let hidapi = HidApi::new()?;
#[cfg(target_os = "macos")]
hidapi.set_open_exclusive(false);
Ok(Litra(hidapi))
}

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

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

0 comments on commit 02624a2

Please sign in to comment.