mirror of
https://github.com/cemu-project/DS4Windows.git
synced 2024-11-25 18:46:58 +01:00
108 lines
3.9 KiB
C#
108 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using HidLibrary;
|
|
namespace DS4Library
|
|
{
|
|
public class DS4Devices
|
|
{
|
|
private static Dictionary<string, DS4Device> Devices = new Dictionary<string, DS4Device>();
|
|
private static HashSet<String> DevicePaths = new HashSet<String>();
|
|
public static bool isExclusiveMode = false;
|
|
|
|
//enumerates ds4 controllers in the system
|
|
public static void findControllers()
|
|
{
|
|
lock (Devices)
|
|
{
|
|
int[] pid = { 0x5C4 };
|
|
IEnumerable<HidDevice> hDevices = HidDevices.Enumerate(0x054C, pid);
|
|
// Sort Bluetooth first in case USB is also connected on the same controller.
|
|
hDevices = hDevices.OrderBy<HidDevice, ConnectionType>((HidDevice d) => { return DS4Device.HidConnectionType(d); });
|
|
|
|
foreach (HidDevice hDevice in hDevices)
|
|
{
|
|
if (DevicePaths.Contains(hDevice.DevicePath))
|
|
continue; // BT/USB endpoint already open once
|
|
if (!hDevice.IsOpen)
|
|
{
|
|
hDevice.OpenDevice(isExclusiveMode);
|
|
// TODO in exclusive mode, try to hold both open when both are connected
|
|
if (isExclusiveMode && !hDevice.IsOpen)
|
|
hDevice.OpenDevice(false);
|
|
}
|
|
if (hDevice.IsOpen)
|
|
{
|
|
if (Devices.ContainsKey(hDevice.readSerial()))
|
|
continue; // happens when the BT endpoint already is open and the USB is plugged into the same host
|
|
else
|
|
{
|
|
DS4Device ds4Device = new DS4Device(hDevice);
|
|
ds4Device.Removal += On_Removal;
|
|
Devices.Add(ds4Device.MacAddress, ds4Device);
|
|
DevicePaths.Add(hDevice.DevicePath);
|
|
ds4Device.StartUpdate();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
//allows to get DS4Device by specifying unique MAC address
|
|
//format for MAC address is XX:XX:XX:XX:XX:XX
|
|
public static DS4Device getDS4Controller(string mac)
|
|
{
|
|
lock (Devices)
|
|
{
|
|
DS4Device device = null;
|
|
try
|
|
{
|
|
Devices.TryGetValue(mac, out device);
|
|
}
|
|
catch (ArgumentNullException) { }
|
|
return device;
|
|
}
|
|
}
|
|
|
|
//returns DS4 controllers that were found and are running
|
|
public static IEnumerable<DS4Device> getDS4Controllers()
|
|
{
|
|
lock (Devices)
|
|
{
|
|
DS4Device[] controllers = new DS4Device[Devices.Count];
|
|
Devices.Values.CopyTo(controllers, 0);
|
|
return controllers;
|
|
}
|
|
}
|
|
|
|
public static void stopControllers()
|
|
{
|
|
lock (Devices)
|
|
{
|
|
IEnumerable<DS4Device> devices = getDS4Controllers();
|
|
foreach (DS4Device device in devices)
|
|
{
|
|
device.StopUpdate();
|
|
device.HidDevice.CloseDevice();
|
|
}
|
|
Devices.Clear();
|
|
DevicePaths.Clear();
|
|
}
|
|
}
|
|
|
|
//called when devices is diconnected, timed out or has input reading failure
|
|
public static void On_Removal(object sender, EventArgs e)
|
|
{
|
|
lock (Devices)
|
|
{
|
|
DS4Device device = (DS4Device)sender;
|
|
device.HidDevice.CloseDevice();
|
|
Devices.Remove(device.MacAddress);
|
|
DevicePaths.Remove(device.HidDevice.DevicePath);
|
|
}
|
|
}
|
|
}
|
|
}
|