From ea74214b017cc91dc21020275cc31eca79eb55ab Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sun, 12 May 2024 10:50:51 +1000 Subject: [PATCH 1/2] [Windows] Improve USB device hardware ID triplet collection --- windows/QMK Toolbox/Usb/UsbDevice.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/windows/QMK Toolbox/Usb/UsbDevice.cs b/windows/QMK Toolbox/Usb/UsbDevice.cs index ac911c60c5..64a26e6c2a 100644 --- a/windows/QMK Toolbox/Usb/UsbDevice.cs +++ b/windows/QMK Toolbox/Usb/UsbDevice.cs @@ -29,7 +29,7 @@ public UsbDevice(ManagementBaseObject d) ManufacturerString = (string)WmiDevice.GetPropertyValue("Manufacturer"); ProductString = (string)WmiDevice.GetPropertyValue("Name"); - var hardwareIdTriplet = HardwareIdTripletRegex.Match(GetHardwareId(WmiDevice)); + var hardwareIdTriplet = GetHardwareId(WmiDevice); VendorId = Convert.ToUInt16(hardwareIdTriplet.Groups[1].ToString(), 16); ProductId = Convert.ToUInt16(hardwareIdTriplet.Groups[2].ToString(), 16); RevisionBcd = Convert.ToUInt16(hardwareIdTriplet.Groups[3].ToString(), 16); @@ -42,12 +42,19 @@ public override string ToString() return $"{ManufacturerString} {ProductString} ({VendorId:X4}:{ProductId:X4}:{RevisionBcd:X4})"; } - private static string GetHardwareId(ManagementBaseObject d) + private static Match GetHardwareId(ManagementBaseObject d) { var hardwareIds = (string[])d.GetPropertyValue("HardwareID"); - if (hardwareIds != null && hardwareIds.Length > 0) + if (hardwareIds != null) { - return hardwareIds[0]; + foreach (string hardwareId in hardwareIds) + { + Match match = HardwareIdTripletRegex.Match(hardwareId); + if (match.Success) + { + return match; + } + } } return null; From 8626bdeb46c4522274c792f7afb9d837944f0473 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Wed, 15 May 2024 12:47:38 +1000 Subject: [PATCH 2/2] Oh yeah C# has tuples --- windows/QMK Toolbox/Usb/UsbDevice.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/windows/QMK Toolbox/Usb/UsbDevice.cs b/windows/QMK Toolbox/Usb/UsbDevice.cs index 64a26e6c2a..8eb9dc821b 100644 --- a/windows/QMK Toolbox/Usb/UsbDevice.cs +++ b/windows/QMK Toolbox/Usb/UsbDevice.cs @@ -29,10 +29,10 @@ public UsbDevice(ManagementBaseObject d) ManufacturerString = (string)WmiDevice.GetPropertyValue("Manufacturer"); ProductString = (string)WmiDevice.GetPropertyValue("Name"); - var hardwareIdTriplet = GetHardwareId(WmiDevice); - VendorId = Convert.ToUInt16(hardwareIdTriplet.Groups[1].ToString(), 16); - ProductId = Convert.ToUInt16(hardwareIdTriplet.Groups[2].ToString(), 16); - RevisionBcd = Convert.ToUInt16(hardwareIdTriplet.Groups[3].ToString(), 16); + var hardwareIdTriplet = GetHardwareId(WmiDevice).Value; + VendorId = hardwareIdTriplet.Item1; + ProductId = hardwareIdTriplet.Item2; + RevisionBcd = hardwareIdTriplet.Item3; Driver = GetDriverName(WmiDevice); } @@ -42,7 +42,7 @@ public override string ToString() return $"{ManufacturerString} {ProductString} ({VendorId:X4}:{ProductId:X4}:{RevisionBcd:X4})"; } - private static Match GetHardwareId(ManagementBaseObject d) + private static (ushort, ushort, ushort)? GetHardwareId(ManagementBaseObject d) { var hardwareIds = (string[])d.GetPropertyValue("HardwareID"); if (hardwareIds != null) @@ -52,7 +52,11 @@ private static Match GetHardwareId(ManagementBaseObject d) Match match = HardwareIdTripletRegex.Match(hardwareId); if (match.Success) { - return match; + return ( + Convert.ToUInt16(match.Groups[1].ToString(), 16), + Convert.ToUInt16(match.Groups[2].ToString(), 16), + Convert.ToUInt16(match.Groups[3].ToString(), 16) + ); } } }