Change device enumeration routine

This commit is contained in:
Travis Nickles 2017-10-28 20:34:04 -05:00
parent c75cb3ba7b
commit bf11a6d297
2 changed files with 45 additions and 3 deletions

View File

@ -7,6 +7,17 @@ using System.Security.Principal;
namespace DS4Windows
{
public class VidPidInfo
{
public readonly int vid;
public readonly int pid;
internal VidPidInfo(int vid, int pid)
{
this.vid = vid;
this.pid = pid;
}
}
public class DS4Devices
{
// (HID device path, DS4Device)
@ -17,8 +28,14 @@ namespace DS4Windows
private static List<HidDevice> DisabledDevices = new List<HidDevice>();
private static Stopwatch sw = new Stopwatch();
public static bool isExclusiveMode = false;
private static int[] vids = { 0x054C, 0x1532 };
private static int[] pid = { 0xBA0, 0x5C4, 0x09CC, 0x1000 };
internal const int SONY_VID = 0x054C;
internal const int RAZER_VID = 0x1532;
private static VidPidInfo[] knownDevices =
{
new VidPidInfo(SONY_VID, 0xBA0), new VidPidInfo(SONY_VID, 0x5C4),
new VidPidInfo(SONY_VID, 0x09CC), new VidPidInfo(RAZER_VID, 0x1000)
};
private static string devicePathToInstanceId(string devicePath)
{
@ -39,7 +56,7 @@ namespace DS4Windows
{
lock (Devices)
{
IEnumerable<HidDevice> hDevices = HidDevices.Enumerate(vids, pid);
IEnumerable<HidDevice> hDevices = HidDevices.EnumerateDS4(knownDevices);
// Sort Bluetooth first in case USB is also connected on the same controller.
hDevices = hDevices.OrderBy<HidDevice, ConnectionType>((HidDevice d) => { return DS4Device.HidConnectionType(d); });

View File

@ -42,6 +42,31 @@ namespace DS4Windows
productIds.Contains(x.Attributes.ProductId));
}
public static IEnumerable<HidDevice> EnumerateDS4(VidPidInfo[] devInfo)
{
List<HidDevice> foundDevs = new List<HidDevice>();
int devInfoLen = devInfo.Length;
IEnumerable<DeviceInfo> temp = EnumerateDevices();
for (int i = 0, len = temp.Count(); i < len; i++)
{
DeviceInfo x = temp.ElementAt(i);
HidDevice tempDev = new HidDevice(x.Path, x.Description);
bool found = false;
for (int j = 0; !found && j < devInfoLen; j++)
{
VidPidInfo tempInfo = devInfo[j];
if (tempDev.Attributes.VendorId == tempInfo.vid &&
tempDev.Attributes.ProductId == tempInfo.pid)
{
found = true;
foundDevs.Add(tempDev);
}
}
}
return foundDevs;
}
public static IEnumerable<HidDevice> Enumerate(int vendorId)
{
return EnumerateDevices().Select(x => new HidDevice(x.Path, x.Description)).Where(x => x.Attributes.VendorId == vendorId);