Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit 8645e80

Browse files
committed
fix: hardware Ids
1 parent 558cc88 commit 8645e80

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

XOutput/ApplicationConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public static ArgumentParser GetArgumentParser()
1616
return new ArgumentParser();
1717
}
1818
[ResolverMethod]
19-
public static HidGuardianManager GetHidGuardianManager(RegistryModifier registryModifier)
19+
public static HidGuardianManager GetHidGuardianManager()
2020
{
21-
return new HidGuardianManager(registryModifier);
21+
return new HidGuardianManager();
2222
}
2323
[ResolverMethod]
2424
public static RegistryModifier GetRegistryModifier()

XOutput/Devices/Input/DirectInput/DirectDevice.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SharpDX;
1+
using Microsoft.Win32;
2+
using SharpDX;
23
using SharpDX.DirectInput;
34
using System;
45
using System.Collections.Generic;
@@ -8,6 +9,7 @@
89
using System.Windows;
910
using System.Windows.Interop;
1011
using XOutput.Logging;
12+
using XOutput.Tools;
1113

1214
namespace XOutput.Devices.Input.DirectInput
1315
{
@@ -98,6 +100,23 @@ public string HardwareID
98100
var match = hidRegex.Match(path);
99101
if (match.Success)
100102
{
103+
string key = $"SYSTEM\\CurrentControlSet\\Enum\\USB\\{match.Groups[2].Value}";
104+
if (RegistryModifier.KeyExists(Registry.LocalMachine, key))
105+
{
106+
foreach (string subkey in RegistryModifier.GetSubKeyNames(Registry.LocalMachine, key))
107+
{
108+
string parentIdPrefix = (string) RegistryModifier.GetValue(Registry.LocalMachine, $"{key}\\{subkey}", "ParentIdPrefix");
109+
if (parentIdPrefix == null || !match.Groups[3].Value.StartsWith(parentIdPrefix))
110+
{
111+
continue;
112+
}
113+
object registryHardwareIds = RegistryModifier.GetValue(Registry.LocalMachine, $"{key}\\{subkey}", "HardwareID");
114+
if (registryHardwareIds is string[])
115+
{
116+
return (registryHardwareIds as string[]).Select(id => id.Replace("USB\\", "HID\\")).FirstOrDefault();
117+
}
118+
}
119+
}
101120
return string.Join("\\", new string[] { match.Groups[1].Value, match.Groups[2].Value, match.Groups[3].Value }).ToUpper();
102121
}
103122
if (path.Contains("hid#"))

XOutput/Tools/HidGuardianManager.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,18 @@ public class HidGuardianManager
99
static readonly string WHITE_LIST = PARAMETERS + "\\Whitelist";
1010
static readonly string AFFECTED_DEVICES = "AffectedDevices";
1111

12-
private RegistryModifier registryModifier;
13-
14-
public HidGuardianManager(RegistryModifier registryModifier)
15-
{
16-
this.registryModifier = registryModifier;
17-
}
18-
1912
public void ResetPid(int pid)
2013
{
21-
if (registryModifier.KeyExists(Registry.LocalMachine, WHITE_LIST))
14+
if (RegistryModifier.KeyExists(Registry.LocalMachine, WHITE_LIST))
2215
{
23-
registryModifier.DeleteTree(Registry.LocalMachine, WHITE_LIST);
16+
RegistryModifier.DeleteTree(Registry.LocalMachine, WHITE_LIST);
2417
}
25-
registryModifier.CreateKey(Registry.LocalMachine, WHITE_LIST + "\\" + pid);
18+
RegistryModifier.CreateKey(Registry.LocalMachine, WHITE_LIST + "\\" + pid);
2619
}
2720

2821
public List<string> GetDevices()
2922
{
30-
object value = registryModifier.GetValue(Registry.LocalMachine, PARAMETERS, AFFECTED_DEVICES);
23+
object value = RegistryModifier.GetValue(Registry.LocalMachine, PARAMETERS, AFFECTED_DEVICES);
3124
if (value is string[])
3225
{
3326
return new List<string>(value as string[]);
@@ -43,7 +36,7 @@ public void AddAffectedDevice(string device)
4336
}
4437
var devices = GetDevices();
4538
devices.Add(device);
46-
registryModifier.SetValue(Registry.LocalMachine, PARAMETERS, AFFECTED_DEVICES, devices.ToArray());
39+
RegistryModifier.SetValue(Registry.LocalMachine, PARAMETERS, AFFECTED_DEVICES, devices.ToArray());
4740
}
4841

4942
public bool RemoveAffectedDevice(string device)
@@ -56,7 +49,7 @@ public bool RemoveAffectedDevice(string device)
5649
bool removed = devices.Remove(device);
5750
if (removed)
5851
{
59-
registryModifier.SetValue(Registry.LocalMachine, PARAMETERS, AFFECTED_DEVICES, devices.ToArray());
52+
RegistryModifier.SetValue(Registry.LocalMachine, PARAMETERS, AFFECTED_DEVICES, devices.ToArray());
6053
}
6154
return removed;
6255
}

XOutput/Tools/RegistryModifier.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,42 +74,47 @@ public void ClearAutostart()
7474
}
7575
}
7676

77-
private RegistryKey GetRegistryKey(bool writeable = true)
77+
private static RegistryKey GetRegistryKey(bool writeable = true)
7878
{
7979
return Registry.CurrentUser.OpenSubKey(AutostartRegistry, writeable);
8080
}
8181

82-
public bool KeyExists(RegistryKey registryKey, string subkey)
82+
public static bool KeyExists(RegistryKey registryKey, string subkey)
8383
{
8484
using (var registry = registryKey.OpenSubKey(subkey))
8585
{
8686
return registry != null;
8787
}
8888
}
8989

90-
public void DeleteTree(RegistryKey registryKey, string subkey)
90+
public static void DeleteTree(RegistryKey registryKey, string subkey)
9191
{
9292
registryKey.DeleteSubKeyTree(subkey, false);
9393
registryKey.Close();
9494
}
9595

96-
public void CreateKey(RegistryKey registryKey, string subkey)
96+
public static void CreateKey(RegistryKey registryKey, string subkey)
9797
{
9898
var registry = registryKey.CreateSubKey(subkey);
9999
registry.Close();
100100
}
101101

102-
public object GetValue(RegistryKey registryKey, string subkey, string key)
102+
public static object GetValue(RegistryKey registryKey, string subkey, string key)
103103
{
104104
return registryKey.OpenSubKey(subkey).GetValue(key);
105105
}
106106

107-
public void SetValue(RegistryKey registryKey, string subkey, string key, object value)
107+
public static void SetValue(RegistryKey registryKey, string subkey, string key, object value)
108108
{
109109
using (var registry = registryKey.OpenSubKey(subkey, true))
110110
{
111111
registry.SetValue(key, value);
112112
}
113113
}
114+
115+
public static string[] GetSubKeyNames(RegistryKey registryKey, string subkey)
116+
{
117+
return registryKey.OpenSubKey(subkey).GetSubKeyNames();
118+
}
114119
}
115120
}

0 commit comments

Comments
 (0)