-
-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
259 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Foundation | ||
|
||
class HIDDevice: Equatable, CustomStringConvertible { | ||
let hidDevice: IOHIDDevice | ||
|
||
var usagePage: UInt16 { | ||
HIDDevice.uint16Property(kIOHIDDeviceUsagePageKey, for: hidDevice)! | ||
} | ||
|
||
var usage: UInt16 { | ||
HIDDevice.uint16Property(kIOHIDDeviceUsageKey, for: hidDevice)! | ||
} | ||
|
||
var manufacturer: String? { | ||
HIDDevice.stringProperty(kIOHIDManufacturerKey, for: hidDevice) | ||
} | ||
|
||
var product: String? { | ||
HIDDevice.stringProperty(kIOHIDProductKey, for: hidDevice) | ||
} | ||
|
||
var vendorID: UInt16 { | ||
HIDDevice.uint16Property(kIOHIDVendorIDKey, for: hidDevice)! | ||
} | ||
|
||
var productID: UInt16 { | ||
HIDDevice.uint16Property(kIOHIDProductIDKey, for: hidDevice)! | ||
} | ||
|
||
var revisionBCD: UInt16 { | ||
HIDDevice.uint16Property(kIOHIDVersionNumberKey, for: hidDevice)! | ||
} | ||
|
||
init(_ device: IOHIDDevice) { | ||
hidDevice = device | ||
} | ||
|
||
var description: String { | ||
String(format: "%@ %@ (%04X:%04X:%04X)", manufacturer ?? "", product ?? "", vendorID, productID, revisionBCD) | ||
} | ||
|
||
static func == (lhs: HIDDevice, rhs: HIDDevice) -> Bool { | ||
return lhs.hidDevice === rhs.hidDevice | ||
} | ||
|
||
static func stringProperty(_ propertyName: String, for device: IOHIDDevice) -> String? { | ||
return IOHIDDeviceGetProperty(device, propertyName as CFString) as! String? | ||
} | ||
|
||
static func uint16Property(_ propertyName: String, for device: IOHIDDevice) -> UInt16? { | ||
return (IOHIDDeviceGetProperty(device, propertyName as CFString) as! NSNumber?)!.uint16Value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Foundation | ||
|
||
class RawDevice: HIDDevice {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using HidLibrary; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace QMK_Toolbox.Hid | ||
{ | ||
public abstract class BaseHidDevice | ||
{ | ||
public IHidDevice HidDevice { get; } | ||
|
||
public string ManufacturerString { get; } | ||
|
||
public string ProductString { get; } | ||
|
||
public ushort VendorId { get; } | ||
|
||
public ushort ProductId { get; } | ||
|
||
public ushort RevisionBcd { get; } | ||
|
||
public ushort UsagePage { get; } | ||
|
||
public ushort Usage { get; } | ||
|
||
public BaseHidDevice(IHidDevice device) | ||
{ | ||
HidDevice = device; | ||
HidDevice.OpenDevice(); | ||
|
||
ManufacturerString = GetManufacturerString(HidDevice); | ||
ProductString = GetProductString(HidDevice); | ||
|
||
VendorId = (ushort)HidDevice.Attributes.VendorId; | ||
ProductId = (ushort)HidDevice.Attributes.ProductId; | ||
RevisionBcd = (ushort)HidDevice.Attributes.Version; | ||
UsagePage = (ushort)HidDevice.Capabilities.UsagePage; | ||
Usage = (ushort)HidDevice.Capabilities.Usage; | ||
|
||
HidDevice.CloseDevice(); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"{ManufacturerString} {ProductString} ({VendorId:X4}:{ProductId:X4}:{RevisionBcd:X4})"; | ||
} | ||
|
||
private static string GetManufacturerString(IHidDevice d) | ||
{ | ||
if (d == null) return ""; | ||
|
||
d.ReadManufacturer(out var bs); | ||
return Encoding.Default.GetString(bs.Where(b => b > 0).ToArray()); | ||
} | ||
|
||
private static string GetProductString(IHidDevice d) | ||
{ | ||
if (d == null) return ""; | ||
|
||
d.ReadProduct(out var bs); | ||
return Encoding.Default.GetString(bs.Where(b => b > 0).ToArray()); | ||
} | ||
} | ||
} |
Oops, something went wrong.