From e908fe58daf177da467235cf6b13bc9a614cfa61 Mon Sep 17 00:00:00 2001 From: DaPutzy <9727551+DaPutzy@users.noreply.github.com> Date: Thu, 24 Nov 2022 23:25:28 +0100 Subject: [PATCH] added option to open devices on darwin in non-exclusive mode --- .../org/hid4java/jna/DarwinHidApiLibrary.java | 24 +++++++++++++++++++ src/main/java/org/hid4java/jna/HidApi.java | 14 +++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/main/java/org/hid4java/jna/DarwinHidApiLibrary.java diff --git a/src/main/java/org/hid4java/jna/DarwinHidApiLibrary.java b/src/main/java/org/hid4java/jna/DarwinHidApiLibrary.java new file mode 100644 index 0000000..178e898 --- /dev/null +++ b/src/main/java/org/hid4java/jna/DarwinHidApiLibrary.java @@ -0,0 +1,24 @@ +package org.hid4java.jna; + +import com.sun.jna.Native; +import com.sun.jna.WString; + +public interface DarwinHidApiLibrary extends HidrawHidApiLibrary { + + DarwinHidApiLibrary INSTANCE = Native.load("hidapi", DarwinHidApiLibrary.class); + + /** + * Changes the behavior of all further calls to {@link #hid_open(short, short, WString)} or {@link #hid_open_path(String)}. + *
+ * All devices opened by HIDAPI with {@link #hid_open(short, short, WString)} or {@link #hid_open_path(String)} + * are opened in exclusive mode per default. + *
+ * Calling this function before {@link #hid_init()} or after {@link #hid_exit()} has no effect. + * + * @since hidapi 0.12.0 + * @param openExclusive When set to 0 - all further devices will be opened in non-exclusive mode. + * Otherwise - all further devices will be opened in exclusive mode. + */ + void hid_darwin_set_open_exclusive(int openExclusive); + +} diff --git a/src/main/java/org/hid4java/jna/HidApi.java b/src/main/java/org/hid4java/jna/HidApi.java index 773b9ca..1146ea7 100644 --- a/src/main/java/org/hid4java/jna/HidApi.java +++ b/src/main/java/org/hid4java/jna/HidApi.java @@ -61,6 +61,14 @@ public class HidApi { */ public static boolean useLibUsbVariant = false; + /** + * When false - all devices will be opened in exclusive mode. (Default) + * When true - all devices will be opened in non-exclusive mode. + *
+ * See {@link DarwinHidApiLibrary#hid_darwin_set_open_exclusive(int)} for more information. + */ + public static boolean darwinOpenDevicesNonExclusive = false; + /** * Enables HID traffic logging to stdout to assist debugging. This will show all bytes (including the extra report ID) * that were sent or received via HIDAPI buffers. It does not log direct string calls (e.g. getEnumeratedString()). @@ -110,11 +118,17 @@ public static void init() { if (useLibUsbVariant && Platform.isLinux()) { hidApiLibrary = LibusbHidApiLibrary.INSTANCE; + } else if (Platform.isMac()) { + hidApiLibrary = DarwinHidApiLibrary.INSTANCE; } else { hidApiLibrary = HidrawHidApiLibrary.INSTANCE; } hidApiLibrary.hid_init(); + + if (hidApiLibrary instanceof DarwinHidApiLibrary) { + ((DarwinHidApiLibrary) hidApiLibrary).hid_darwin_set_open_exclusive(darwinOpenDevicesNonExclusive ? 0 : 1); + } } /**