//Release the controller from HidGuardian public void HidGuardianReleaseController(ControllerDetails ConnectedController) { try { using (RegistryKey registryKeyLocalMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)) { using (RegistryKey openSubKey = registryKeyLocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\HidGuardian\Parameters", true)) { string[] stringArray = openSubKey.GetValue("AffectedDevices") as string[]; List <string> stringList = (stringArray != null) ? new List <string>(stringArray) : new List <string>(); if (stringList.Contains(ConnectedController.HardwareId)) { stringList.Remove(ConnectedController.HardwareId); openSubKey.SetValue("AffectedDevices", stringList.ToArray()); Debug.WriteLine("Released HidGuardian controller: " + ConnectedController.HardwareId); } } } } catch { } }
//Receive all the connected Controllers async Task ControllerReceiveAllConnected() { try { //Add Win Usb Devices IEnumerable <EnumerateInfo> SelectedWinDevice = EnumerateDevicesDi(GuidClassScpDS3Driver, true); foreach (EnumerateInfo EnumDevice in SelectedWinDevice) { try { //Get vendor and product hex id string VendorHexId = "0x" + AVFunctions.StringShowAfter(EnumDevice.DevicePath, "vid_", 4).ToLower(); string ProductHexId = "0x" + AVFunctions.StringShowAfter(EnumDevice.DevicePath, "pid_", 4).ToLower(); //Validate the connected controller if (!ControllerValidate(VendorHexId, ProductHexId, EnumDevice.DevicePath, string.Empty)) { continue; } //Create new Json controller profile if it doesnt exist IEnumerable <ControllerProfile> profileList = vDirectControllersProfile.Where(x => x.ProductID.ToLower() == ProductHexId && x.VendorID.ToLower() == VendorHexId); if (!profileList.Any()) { //Create controller profile ControllerProfile controllerProfile = new ControllerProfile() { ProductID = ProductHexId, VendorID = VendorHexId, ProductName = EnumDevice.Description, VendorName = "Unknown" }; //Add profile to list vDirectControllersProfile.Add(controllerProfile); //Save profile to Json file JsonSaveObject(controllerProfile, GenerateJsonNameControllerProfile(controllerProfile)); Debug.WriteLine("Added win profile: " + EnumDevice.Description); } ControllerProfile profileController = profileList.FirstOrDefault(); //Check if controller is wireless bool ConnectedWireless = EnumDevice.DevicePath.ToLower().Contains("00805f9b34fb"); ControllerDetails newController = new ControllerDetails() { Type = ControllerType.WinUsbDevice, Profile = profileController, DisplayName = EnumDevice.Description, Path = EnumDevice.DevicePath, Wireless = ConnectedWireless }; //Connect with the controller await ControllerConnect(newController); } catch { } } //Add Hid Usb Devices IEnumerable <EnumerateInfo> SelectedHidDevice = EnumerateDevicesDi(GuidClassHidDevice, true); foreach (EnumerateInfo EnumDevice in SelectedHidDevice) { try { //Read information from the controller HidDevice foundHidDevice = new HidDevice(EnumDevice.DevicePath, EnumDevice.ModelId, false, true); //Check if device is a gamepad or joystick bool genericGamePad = foundHidDevice.Capabilities.UsageGeneric == (short)HID_USAGE_GENERIC_DESKTOP_PAGE.HID_USAGE_GENERIC_GAMEPAD; bool genericJoystick = foundHidDevice.Capabilities.UsageGeneric == (short)HID_USAGE_GENERIC_DESKTOP_PAGE.HID_USAGE_GENERIC_JOYSTICK; if (!genericGamePad && !genericJoystick) { continue; } //Get vendor and product hex id string VendorHexId = foundHidDevice.Attributes.VendorHexId.ToLower(); string ProductHexId = foundHidDevice.Attributes.ProductHexId.ToLower(); //Validate the connected controller if (!ControllerValidate(VendorHexId, ProductHexId, EnumDevice.DevicePath, foundHidDevice.Attributes.SerialNumber)) { continue; } //Get controller product information string ProductNameString = foundHidDevice.Attributes.ProductName; string VendorNameString = foundHidDevice.Attributes.VendorName; //Create new Json controller profile if it doesnt exist IEnumerable <ControllerProfile> profileList = vDirectControllersProfile.Where(x => x.ProductID.ToLower() == ProductHexId && x.VendorID.ToLower() == VendorHexId); if (!profileList.Any()) { //Create controller profile ControllerProfile controllerProfile = new ControllerProfile() { ProductID = ProductHexId, VendorID = VendorHexId, ProductName = ProductNameString, VendorName = VendorNameString }; //Add profile to list vDirectControllersProfile.Add(controllerProfile); //Save profile to Json file JsonSaveObject(controllerProfile, GenerateJsonNameControllerProfile(controllerProfile)); Debug.WriteLine("Added hid profile: " + ProductNameString + " (" + VendorNameString + ")"); } ControllerProfile profileController = profileList.FirstOrDefault(); //Check if controller is wireless bool ConnectedWireless = foundHidDevice.DevicePath.ToLower().Contains("00805f9b34fb"); ControllerDetails newController = new ControllerDetails() { Type = ControllerType.HidDevice, Profile = profileController, DisplayName = ProductNameString, ModelId = foundHidDevice.ModelId, Path = foundHidDevice.DevicePath, Wireless = ConnectedWireless }; //Connect with the controller await ControllerConnect(newController); } catch { } } } catch (Exception ex) { Debug.WriteLine("Failed adding new controller: " + ex.Message); } }
//Reset controller status to defaults public void ResetControllerStatus() { try { Debug.WriteLine("Reset the controller status for controller: " + NumberId); //Controller Status Activated = false; //Battery Status BatteryCurrent = new ControllerBattery(); BatteryPrevious = new ControllerBattery(); //Time Variables PrevInputTicks = 0; LastInputTicks = 0; LastActiveTicks = 0; //Controller Details Details = null; BlockOutput = false; //Controller Tasks InputVirtualOverlapped = new NativeOverlapped() { EventHandle = CreateEvent(IntPtr.Zero, true, false, null) }; OutputVirtualOverlapped = new NativeOverlapped() { EventHandle = CreateEvent(IntPtr.Zero, true, false, null) }; InputControllerTask = new AVTaskDetails(); OutputControllerTask = new AVTaskDetails(); OutputVirtualTask = new AVTaskDetails(); OutputGyroTask = new AVTaskDetails(); //WinUsb Device Variables WinUsbDevice = null; //Hid Device Variables HidDevice = null; //Gyro Dsu Client Variables GyroDsuClientPacketNumber = 0; GyroDsuClientEndPoint = null; //Device In and Output InputButtonCountLoop1 = 0; InputButtonCountTotal1 = 80; InputButtonCountLoop2 = 0; InputButtonCountTotal2 = 80; InputButtonCountLoop3 = 0; InputButtonCountTotal3 = 80; InputHeaderOffsetFinished = false; InputHeaderOffsetByte = 0; InputButtonOffsetFinished = false; InputButtonOffsetByte = 0; InputReport = null; OutputReport = null; XInputData = new XUSB_INPUT_REPORT(); XOutputData = new XUSB_OUTPUT_REPORT(); XOutputCurrentRumbleHeavy = 0; XOutputCurrentRumbleLight = 0; XOutputPreviousRumbleHeavy = 0; XOutputPreviousRumbleLight = 0; //Controller Input InputCurrent = new ControllerInput(); SupportedCurrent = new ControllerSupported(); } catch { } }
//Connect with the controller async Task ControllerConnect(ControllerDetails ConnectedController) { try { //Check if the controller is already in use bool ControllerInuse = false; if (vController0.Connected() && vController0.Details.Path == ConnectedController.Path) { ControllerInuse = true; } if (vController1.Connected() && vController1.Details.Path == ConnectedController.Path) { ControllerInuse = true; } if (vController2.Connected() && vController2.Details.Path == ConnectedController.Path) { ControllerInuse = true; } if (vController3.Connected() && vController3.Details.Path == ConnectedController.Path) { ControllerInuse = true; } if (ControllerInuse) { return; } Debug.WriteLine("Found a connected " + ConnectedController.Type + " controller to use: " + ConnectedController.DisplayName); //Connect the controller to available slot if (!vController0.Connected()) { vController0.Details = ConnectedController; bool controllerStarted = await StartControllerDirectInput(vController0); if (controllerStarted) { AVActions.ActionDispatcherInvoke(delegate { image_Controller0.Source = vImagePreloadIconControllerAccent; textblock_Controller0.Text = vController0.Details.DisplayName; textblock_Controller0CodeName.Text = vController0.SupportedCurrent.CodeName; }); } } else if (!vController1.Connected()) { vController1.Details = ConnectedController; bool controllerStarted = await StartControllerDirectInput(vController1); if (controllerStarted) { AVActions.ActionDispatcherInvoke(delegate { image_Controller1.Source = vImagePreloadIconControllerAccent; textblock_Controller1.Text = vController1.Details.DisplayName; textblock_Controller1CodeName.Text = vController1.SupportedCurrent.CodeName; }); } } else if (!vController2.Connected()) { vController2.Details = ConnectedController; bool controllerStarted = await StartControllerDirectInput(vController2); if (controllerStarted) { AVActions.ActionDispatcherInvoke(delegate { image_Controller2.Source = vImagePreloadIconControllerAccent; textblock_Controller2.Text = vController2.Details.DisplayName; textblock_Controller2CodeName.Text = vController2.SupportedCurrent.CodeName; }); } } else if (!vController3.Connected()) { vController3.Details = ConnectedController; bool controllerStarted = await StartControllerDirectInput(vController3); if (controllerStarted) { AVActions.ActionDispatcherInvoke(delegate { image_Controller3.Source = vImagePreloadIconControllerAccent; textblock_Controller3.Text = vController3.Details.DisplayName; textblock_Controller3CodeName.Text = vController3.SupportedCurrent.CodeName; }); } } } catch { } }