using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DS4Windows { public class DS4Devices { private static Dictionary Devices = new Dictionary(); private static HashSet DevicePaths = new HashSet(); public static bool isExclusiveMode = false; //enumerates ds4 controllers in the system public static void findControllers() { lock (Devices) { int[] pid = { 0x5C4 }; IEnumerable hDevices = HidDevices.Enumerate(0x054C, pid); // Sort Bluetooth first in case USB is also connected on the same controller. hDevices = hDevices.OrderBy((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 getDS4Controllers() { lock (Devices) { DS4Device[] controllers = new DS4Device[Devices.Count]; Devices.Values.CopyTo(controllers, 0); return controllers; } } public static void stopControllers() { lock (Devices) { IEnumerable 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); } } } }