Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions HidSharp/OpenOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public sealed class OpenOption
/// </summary>
public static OpenOption Exclusive { get; private set; }

public static OpenOption DeviceExclusive { get; private set; }

/// <summary>
/// Allow other processes to send interruption requests.
/// If another other process with higher priority attempts to open the HID device this process is using,
Expand Down Expand Up @@ -86,6 +88,11 @@ static OpenOption()
{
_options = new Dictionary<Guid, OpenOption>();

DeviceExclusive = OpenOption.New(new Guid("{79F41C23-C92C-47C7-8F8C-152127EF9822}"),
deserializeCallback: DeserializeBoolean,
serializeCallback: SerializeBoolean,
defaultValue: false,
friendlyName: "DeviceExclusive");
Exclusive = OpenOption.New(new Guid("{49DB23CD-727E-4788-BBAD-7D67ACCBC469}"),
deserializeCallback: DeserializeBoolean,
serializeCallback: SerializeBoolean,
Expand Down
2 changes: 1 addition & 1 deletion HidSharp/Platform/Linux/LinuxHidDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected override DeviceStream OpenDeviceDirectly(OpenConfiguration openConfig)
RequiresGetInfo();

var stream = new LinuxHidStream(this);
try { stream.Init(_path); return stream; }
try { stream.Init(_path, openConfig); return stream; }
catch { stream.Close(); throw; }
}

Expand Down
11 changes: 7 additions & 4 deletions HidSharp/Platform/Linux/LinuxHidStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,15 @@ internal static int DeviceHandleFromPath(string path, HidDevice hidDevice, Nativ
throw new FileNotFoundException("HID class device not found.");
}

internal void Init(string path)
internal void Init(string path, OpenConfiguration openConfig)
{
int handle;
handle = DeviceHandleFromPath(path, Device, NativeMethods.oflag.RDWR | NativeMethods.oflag.NONBLOCK);

_handle = handle;
if ((bool)openConfig.GetOption(OpenOption.DeviceExclusive))
handle = DeviceHandleFromPath(path, Device, NativeMethods.oflag.RDWR | NativeMethods.oflag.EXCL);
else
handle = DeviceHandleFromPath(path, Device, NativeMethods.oflag.RDWR | NativeMethods.oflag.NONBLOCK);

_handle = handle;
HandleInitAndOpen();

_readThread.Start();
Expand Down
2 changes: 1 addition & 1 deletion HidSharp/Platform/MacOS/MacHidDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ internal static MacHidDevice TryCreate(NativeMethods.io_string_t path)
protected override DeviceStream OpenDeviceDirectly(OpenConfiguration openConfig)
{
var stream = new MacHidStream(this);
try { stream.Init(_path); return stream; }
try { stream.Init(_path, openConfig); return stream; }
catch { stream.Close(); throw; }
}

Expand Down
9 changes: 7 additions & 2 deletions HidSharp/Platform/MacOS/MacHidStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal MacHidStream(MacHidDevice device)
_writeThread = new Thread(WriteThread) { IsBackground = true, Name = "HID Writer" };
}

internal void Init(NativeMethods.io_string_t path)
internal void Init(NativeMethods.io_string_t path, OpenConfiguration openConfig)
{
IntPtr handle; int retryCount = 0, maxRetries = 10;
while (true)
Expand All @@ -59,7 +59,12 @@ internal void Init(NativeMethods.io_string_t path)
handle = NativeMethods.IOHIDDeviceCreate(IntPtr.Zero, service);
if (handle != IntPtr.Zero)
{
var ret = NativeMethods.IOHIDDeviceOpen(handle);
NativeMethods.IOReturn ret;
if ((bool)openConfig.GetOption(OpenOption.DeviceExclusive))
ret = NativeMethods.IOHIDDeviceOpen(handle, NativeMethods.IOOptionBits.SeizeDevice);
else
ret = NativeMethods.IOHIDDeviceOpen(handle, NativeMethods.IOOptionBits.None);

if (ret == NativeMethods.IOReturn.Success) { break; }

NativeMethods.CFRelease(handle);
Expand Down
3 changes: 2 additions & 1 deletion HidSharp/Platform/MacOS/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public enum OSType : uint

public enum IOOptionBits
{
None = 0
None = 0,
SeizeDevice = 1
}

public enum IOHIDElementType
Expand Down
2 changes: 1 addition & 1 deletion HidSharp/Platform/Windows/WinHidDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected override DeviceStream OpenDeviceDirectly(OpenConfiguration openConfig)
RequiresGetInfo(GetInfoFlags.ReportInfo);

var stream = new WinHidStream(this);
try { stream.Init(_path); return stream; }
try { stream.Init(_path, openConfig); return stream; }
catch { stream.Close(); throw; }
}

Expand Down
8 changes: 6 additions & 2 deletions HidSharp/Platform/Windows/WinHidStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ internal WinHidStream(WinHidDevice device)
NativeMethods.CloseHandle(_closeEventHandle);
}

internal void Init(string path)
internal void Init(string path, OpenConfiguration openConfig)
{
IntPtr handle = NativeMethods.CreateFileFromDevice(path, NativeMethods.EFileAccess.Read | NativeMethods.EFileAccess.Write, NativeMethods.EFileShare.Read | NativeMethods.EFileShare.Write);
IntPtr handle;
if ((bool)openConfig.GetOption(OpenOption.DeviceExclusive))
handle = NativeMethods.CreateFileFromDevice(path, NativeMethods.EFileAccess.Read | NativeMethods.EFileAccess.Write, NativeMethods.EFileShare.None);
else
handle = NativeMethods.CreateFileFromDevice(path, NativeMethods.EFileAccess.Read | NativeMethods.EFileAccess.Write, NativeMethods.EFileShare.Read | NativeMethods.EFileShare.Write);
if (handle == (IntPtr)(-1))
{
throw DeviceException.CreateIOException(Device, "Unable to open HID class device (" + path + ").");
Expand Down