Skip to content

Instantly share code, notes, and snippets.

@Ryochan7
Created March 24, 2019 06:38
Show Gist options
  • Save Ryochan7/2d091f48f4dbd00790d500aa777ce44b to your computer and use it in GitHub Desktop.
Save Ryochan7/2d091f48f4dbd00790d500aa777ce44b to your computer and use it in GitHub Desktop.
Patch for DS4Windows to skip grabbing virtual DS4 controllers. Will be needed if DS4 emulation gets integrated
diff --git a/DS4Windows/DS4Control/ScpUtil.cs b/DS4Windows/DS4Control/ScpUtil.cs
index f5ccddc..c4111ef 100644
--- a/DS4Windows/DS4Control/ScpUtil.cs
+++ b/DS4Windows/DS4Control/ScpUtil.cs
@@ -334,6 +334,34 @@ namespace DS4Windows
return result;
}
+ internal static string GetDeviceProperty(string deviceInstanceId,
+ NativeMethods.DEVPROPKEY prop)
+ {
+ string result = string.Empty;
+ NativeMethods.SP_DEVINFO_DATA deviceInfoData = new NativeMethods.SP_DEVINFO_DATA();
+ deviceInfoData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(deviceInfoData);
+ var dataBuffer = new byte[4096];
+ ulong propertyType = 0;
+ var requiredSize = 0;
+
+ Guid hidGuid = new Guid();
+ NativeMethods.HidD_GetHidGuid(ref hidGuid);
+ IntPtr deviceInfoSet = NativeMethods.SetupDiGetClassDevs(ref hidGuid, deviceInstanceId, 0, NativeMethods.DIGCF_PRESENT | NativeMethods.DIGCF_DEVICEINTERFACE);
+ NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, 0, ref deviceInfoData);
+ if (NativeMethods.SetupDiGetDeviceProperty(deviceInfoSet, ref deviceInfoData, ref prop, ref propertyType,
+ dataBuffer, dataBuffer.Length, ref requiredSize, 0))
+ {
+ result = dataBuffer.ToUTF16String();
+ }
+
+ if (deviceInfoSet.ToInt64() != NativeMethods.INVALID_HANDLE_VALUE)
+ {
+ NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);
+ }
+
+ return result;
+ }
+
public static bool IsHidGuardianInstalled()
{
return CheckForSysDevice(@"Root\HidGuardian");
diff --git a/DS4Windows/DS4Library/DS4Devices.cs b/DS4Windows/DS4Library/DS4Devices.cs
index c97ea87..8ce525b 100644
--- a/DS4Windows/DS4Library/DS4Devices.cs
+++ b/DS4Windows/DS4Library/DS4Devices.cs
@@ -60,12 +60,22 @@ namespace DS4Windows
return deviceInstanceId;
}
+ private static bool IsRealDS4(HidDevice hDevice)
+ {
+ string deviceInstanceId = devicePathToInstanceId(hDevice.DevicePath);
+ string temp = Global.GetDeviceProperty(deviceInstanceId,
+ NativeMethods.DEVPKEY_Device_UINumber);
+ return string.IsNullOrEmpty(temp);
+ }
+
// Enumerates ds4 controllers in the system
public static void findControllers()
{
lock (Devices)
{
IEnumerable<HidDevice> hDevices = HidDevices.EnumerateDS4(knownDevices);
+ hDevices = hDevices.Where(dev => IsRealDS4(dev)).Select(dev => dev);
+ //hDevices = from dev in hDevices where IsRealDS4(dev) select dev;
// Sort Bluetooth first in case USB is also connected on the same controller.
hDevices = hDevices.OrderBy<HidDevice, ConnectionType>((HidDevice d) => { return DS4Device.HidConnectionType(d); });
diff --git a/DS4Windows/HidLibrary/NativeMethods.cs b/DS4Windows/HidLibrary/NativeMethods.cs
index 8ee0adf..1fca5dd 100644
--- a/DS4Windows/HidLibrary/NativeMethods.cs
+++ b/DS4Windows/HidLibrary/NativeMethods.cs
@@ -234,6 +234,9 @@ namespace DS4Windows
internal static DEVPROPKEY DEVPKEY_Device_HardwareIds =
new DEVPROPKEY { fmtid = new Guid(0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0), pid = 3 };
+ internal static DEVPROPKEY DEVPKEY_Device_UINumber =
+ new DEVPROPKEY { fmtid = new Guid(0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0), pid = 18 };
+
[DllImport("setupapi.dll", EntryPoint = "SetupDiGetDeviceRegistryProperty")]
public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, int propertyVal, ref int propertyRegDataType, byte[] propertyBuffer, int propertyBufferSize, ref int requiredSize);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment