diff options
| author | Jacob Palecki <[email protected]> | 2021-01-07 23:11:24 -0800 |
|---|---|---|
| committer | Jacob Palecki <[email protected]> | 2021-01-07 23:11:24 -0800 |
| commit | 8273ecf0c22876f0f4e9dfa9eb29b9d491614faa (patch) | |
| tree | 089cb8e024722781670433502e1452a59456cc6a /grapher/Models/Devices | |
| parent | rough GUI integration of "Device Hardware ID" (diff) | |
| download | rawaccel-8273ecf0c22876f0f4e9dfa9eb29b9d491614faa.tar.xz rawaccel-8273ecf0c22876f0f4e9dfa9eb29b9d491614faa.zip | |
Refactor
Diffstat (limited to 'grapher/Models/Devices')
| -rw-r--r-- | grapher/Models/Devices/DeviceIDItem.cs | 73 | ||||
| -rw-r--r-- | grapher/Models/Devices/DeviceIDManager.cs | 90 | ||||
| -rw-r--r-- | grapher/Models/Devices/DeviceList.cs | 33 |
3 files changed, 163 insertions, 33 deletions
diff --git a/grapher/Models/Devices/DeviceIDItem.cs b/grapher/Models/Devices/DeviceIDItem.cs new file mode 100644 index 0000000..2bdff81 --- /dev/null +++ b/grapher/Models/Devices/DeviceIDItem.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher.Models.Devices +{ + public class DeviceIDItem + { + public DeviceIDItem(string name, string hwid, DeviceIDManager manager) + { + Name = name; + HWID = hwid; + Manager = manager; + DeviceIDMenuItem = new ToolStripMenuItem(); + DeviceIDMenuItem.Checked = false; + DeviceIDMenuItem.Text = MenuItemText(); + DeviceIDMenuItem.Click += OnClicked; + manager.DeviceIDsMenuItem.DropDownItems.Add(DeviceIDMenuItem); + } + + private ToolStripMenuItem DeviceIDMenuItem { get; } + + public string Name { get; } + + public string HWID { get; } + + private DeviceIDManager Manager { get; } + + public void SetActivated() + { + DeviceIDMenuItem.Checked = true; + } + + public void SetDeactivated() + { + DeviceIDMenuItem.Checked = false; + } + + private string MenuItemText() => string.IsNullOrWhiteSpace(HWID) ? $"{Name}" : $"{Name}: {HWID}"; + + private string DisconnectedText() => $"Disconnected: {HWID}"; + + public void SetDisconnected() + { + DeviceIDMenuItem.ForeColor = Color.DarkGray; + DeviceIDMenuItem.Text = DisconnectedText(); + } + + public void OnClicked(object sender, EventArgs e) + { + Manager.SetActive(this); + } + + public override bool Equals(object obj) + { + return obj is DeviceIDItem item && + Name == item.Name && + HWID == item.HWID; + } + + public override int GetHashCode() + { + int hashCode = -1692744877; + hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name); + hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(HWID); + return hashCode; + } + } +} 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(); + } + } + + } +} diff --git a/grapher/Models/Devices/DeviceList.cs b/grapher/Models/Devices/DeviceList.cs deleted file mode 100644 index 9f2b81d..0000000 --- a/grapher/Models/Devices/DeviceList.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Management; -using System.Collections.Generic; - -namespace grapher.Models.Devices -{ - class DeviceList - { - public static List<Tuple<string, string>> GetDeviceHardwareIDs(string PNPClass = "Mouse") - { - var results = new List<Tuple<string, string>>(); - - ManagementObjectSearcher searcher = new ManagementObjectSearcher(new SelectQuery("Win32_PnPEntity")); - - foreach (ManagementObject obj in searcher.Get()) - { - if (obj["PNPClass"] != null && obj["PNPClass"].ToString() == PNPClass && obj["HardwareID"] != null) - { - String[] hwidArray = (String[])(obj["HardwareID"]); - if (hwidArray.Length > 0) - { - String hwid = hwidArray[0].ToString(); - String name = obj["Name"].ToString(); - results.Add(Tuple.Create(name, hwid)); - } - } - } - - return results; - } - - } -} |