summaryrefslogtreecommitdiff
path: root/grapher/Models/Devices/DeviceIDManager.cs
diff options
context:
space:
mode:
authorJacob Palecki <[email protected]>2021-01-07 23:11:24 -0800
committerJacob Palecki <[email protected]>2021-01-07 23:11:24 -0800
commit8273ecf0c22876f0f4e9dfa9eb29b9d491614faa (patch)
tree089cb8e024722781670433502e1452a59456cc6a /grapher/Models/Devices/DeviceIDManager.cs
parentrough GUI integration of "Device Hardware ID" (diff)
downloadrawaccel-8273ecf0c22876f0f4e9dfa9eb29b9d491614faa.tar.xz
rawaccel-8273ecf0c22876f0f4e9dfa9eb29b9d491614faa.zip
Refactor
Diffstat (limited to 'grapher/Models/Devices/DeviceIDManager.cs')
-rw-r--r--grapher/Models/Devices/DeviceIDManager.cs90
1 files changed, 90 insertions, 0 deletions
diff --git a/grapher/Models/Devices/DeviceIDManager.cs b/grapher/Models/Devices/DeviceIDManager.cs
new file mode 100644
index 0000000..00e7f9d
--- /dev/null
+++ b/grapher/Models/Devices/DeviceIDManager.cs
@@ -0,0 +1,90 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+using System.Management;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace grapher.Models.Devices
+{
+ public class DeviceIDManager
+ {
+ public DeviceIDManager(ToolStripMenuItem deviceIDs)
+ {
+ DeviceIDsMenuItem = deviceIDs;
+ }
+
+ public ToolStripMenuItem DeviceIDsMenuItem { get; }
+
+ public string HWID { get => SelectedDeviceID.HWID; }
+
+ public DeviceIDItem SelectedDeviceID { get; private set; }
+
+ public Dictionary<string, DeviceIDItem> DeviceIDs { get; private set; }
+
+ public static IEnumerable<(string, string)> GetDeviceHardwareIDs(string PNPClass = "Mouse")
+ {
+ ManagementObjectSearcher searcher = new ManagementObjectSearcher(new SelectQuery("Win32_PnPEntity"));
+
+ foreach (ManagementObject obj in searcher.Get())
+ {
+ if (obj["PNPClass"] != null && obj["PNPClass"].ToString().Equals(PNPClass) && obj["HardwareID"] != null)
+ {
+ string[] hwidArray = (string[])(obj["HardwareID"]);
+ if (hwidArray.Length > 0)
+ {
+ string hwid = hwidArray[0].ToString();
+ string name = obj["Name"].ToString();
+ yield return (name, hwid);
+ }
+ }
+ }
+ }
+
+ public void SetActive(DeviceIDItem deviceIDItem)
+ {
+ if (SelectedDeviceID != null)
+ {
+ SelectedDeviceID.SetDeactivated();
+ }
+
+ SelectedDeviceID = deviceIDItem;
+ SelectedDeviceID.SetActivated();
+ }
+
+ public void OnStartup(string hwid)
+ {
+ var nonEmptyHwid = !string.IsNullOrWhiteSpace(hwid);
+
+ DeviceIDsMenuItem.Checked = nonEmptyHwid;
+ DeviceIDsMenuItem.DropDownItems.Clear();
+ var anyDevice = new DeviceIDItem("Any", string.Empty, this);
+ if (!nonEmptyHwid)
+ {
+ SetActive(anyDevice);
+ }
+
+ bool found = false;
+
+ foreach (var device in GetDeviceHardwareIDs())
+ {
+ var deviceItem = new DeviceIDItem(device.Item1, device.Item2, this);
+ if (deviceItem.HWID.Equals(hwid))
+ {
+ found = true;
+ deviceItem.SetActivated();
+ SelectedDeviceID = deviceItem;
+ }
+ }
+
+ if (nonEmptyHwid && !found)
+ {
+ var deviceItem = new DeviceIDItem(string.Empty, hwid, this);
+ deviceItem.SetDisconnected();
+ }
+ }
+
+ }
+}