From b2cf4d3cabfc16c3031f9bf52438b32514755c36 Mon Sep 17 00:00:00 2001 From: Lesueur Benjamin Date: Sat, 24 Jun 2023 14:59:49 +0200 Subject: [PATCH] Improve gyro support on Rog Ally, AYANEO Air Plus (#649) * try closing HidStreams * improve LegacyDevcon * what if we set Exclusive and Transient to false ? * oups * give the IMU some breathing space once restarted * Reduce IMU restart timer from 2000 to 500 msec. * Press to define trigger --------- Co-authored-by: CasperH2O --- ControllerCommon/Devices/ASUS/ROGAlly.cs | 29 +++++++++---- ControllerCommon/Devices/IDevice.cs | 5 ++- ControllerCommon/LegacyDevcon.cs | 42 +++++++++++-------- ControllerService/ControllerService.cs | 7 ++++ .../Properties/Resources.Designer.cs | 2 +- HandheldCompanion/Properties/Resources.resx | 2 +- 6 files changed, 60 insertions(+), 27 deletions(-) diff --git a/ControllerCommon/Devices/ASUS/ROGAlly.cs b/ControllerCommon/Devices/ASUS/ROGAlly.cs index 19a5814f8..efec9fb81 100644 --- a/ControllerCommon/Devices/ASUS/ROGAlly.cs +++ b/ControllerCommon/Devices/ASUS/ROGAlly.cs @@ -24,6 +24,7 @@ public class ROGAlly : IDevice }; private bool previousWasEmpty; + private List _hidStreams = new(); public ROGAlly() { @@ -81,8 +82,8 @@ public override bool Open() // prepare configuration var deviceConfiguration = new OpenConfiguration(); - deviceConfiguration.SetOption(OpenOption.Exclusive, true); - deviceConfiguration.SetOption(OpenOption.Transient, true); + deviceConfiguration.SetOption(OpenOption.Exclusive, false); + deviceConfiguration.SetOption(OpenOption.Transient, false); foreach (var _hidDevice in DeviceList.Local.GetHidDevices() .Where(d => d.ProductID == _pid && d.VendorID == _vid)) @@ -91,13 +92,20 @@ public override bool Open() var deviceDescriptor = _hidDevice.GetReportDescriptor(); if (!_hidDevice.TryOpen(deviceConfiguration, out var inputStream)) continue; + + // add stream to array + _hidStreams.Add(inputStream); + foreach (var inputReport in deviceDescriptor.InputReports) { - var hiddeviceInputParser = inputReport.DeviceItem.CreateDeviceItemInputParser(); - var hidDeviceInputReceiver = deviceDescriptor.CreateHidDeviceInputReceiver(); - + DeviceItemInputParser hiddeviceInputParser = inputReport.DeviceItem.CreateDeviceItemInputParser(); + HidDeviceInputReceiver hidDeviceInputReceiver = deviceDescriptor.CreateHidDeviceInputReceiver(); + + // listen for event(s) hidDeviceInputReceiver.Received += (sender, e) => - InputReportReciever_Received(_hidDevice, hiddeviceInputParser, hidDeviceInputReceiver); + InputReportReceiver_Received(_hidDevice, hiddeviceInputParser, hidDeviceInputReceiver); + + // start receiver hidDeviceInputReceiver.Start(inputStream); } } @@ -107,10 +115,17 @@ public override bool Open() public override void Close() { + // close stream(s) + foreach (HidStream stream in _hidStreams) + stream.Close(); + + // clear array + _hidStreams.Clear(); + base.Close(); } - private void InputReportReciever_Received(HidDevice hidDevice, DeviceItemInputParser hiddeviceInputParser, + private void InputReportReceiver_Received(HidDevice hidDevice, DeviceItemInputParser hiddeviceInputParser, HidDeviceInputReceiver hidDeviceInputReceiver) { var inputReportBuffer = new byte[hidDevice.GetMaxInputReportLength()]; diff --git a/ControllerCommon/Devices/IDevice.cs b/ControllerCommon/Devices/IDevice.cs index cb9c4a3ff..2315ecfca 100644 --- a/ControllerCommon/Devices/IDevice.cs +++ b/ControllerCommon/Devices/IDevice.cs @@ -354,7 +354,10 @@ public void PullSensors() public bool RestartSensor() { - var deviceId = sensor.DeviceId.Replace("\\1", string.Empty); + if (sensor is null) + return false; + + string deviceId = sensor.DeviceId.Replace("\\1", string.Empty); return LegacyDevcon.Restart(deviceId); } diff --git a/ControllerCommon/LegacyDevcon.cs b/ControllerCommon/LegacyDevcon.cs index a2325f76a..2d930c6ac 100644 --- a/ControllerCommon/LegacyDevcon.cs +++ b/ControllerCommon/LegacyDevcon.cs @@ -13,7 +13,7 @@ namespace ControllerCommon public static class LegacyDevcon { private static readonly string path; - private static readonly ProcessStartInfo startInfo; + private static bool _IsInstalled; static LegacyDevcon() { @@ -25,31 +25,39 @@ static LegacyDevcon() return; } - startInfo = new ProcessStartInfo(path) - { - UseShellExecute = false, - RedirectStandardOutput = true, - RedirectStandardError = true, - CreateNoWindow = true - }; + _IsInstalled = true; } public static bool Restart(string InstanceId) { - // register command - startInfo.Arguments = $"restart \"{InstanceId}\""; - using (var ProcessOutput = Process.Start(startInfo)) + if (!_IsInstalled) + return false; + + string output = string.Empty; + using (Process process = new Process()) { - string output = ProcessOutput.StandardOutput.ReadToEnd(); + process.StartInfo.FileName = path; + process.StartInfo.UseShellExecute = false; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.RedirectStandardError = true; + process.StartInfo.CreateNoWindow = true; + + // register command + process.StartInfo.Arguments = $"restart \"{InstanceId}\""; - ProcessOutput.WaitForExit(); + process.Start(); - if (output.Contains("No matching devices found.")) - return false; - else if (output.Contains("Restarted")) - return true; + StreamReader reader = process.StandardOutput; + output = reader.ReadToEnd(); + + process.WaitForExit(); } + if (output.Contains("No matching devices found.")) + return false; + else if (output.Contains("Restarted")) + return true; + return false; } } diff --git a/ControllerService/ControllerService.cs b/ControllerService/ControllerService.cs index 0474fe750..33d5eb6e2 100644 --- a/ControllerService/ControllerService.cs +++ b/ControllerService/ControllerService.cs @@ -114,8 +114,15 @@ public ControllerService(IHostApplicationLifetime lifetime) case "AYANEOAIRPlus": case "ROGAlly": { + LogManager.LogInformation("Restarting: {0}", CurrentDevice.InternalSensorName); + if (CurrentDevice.RestartSensor()) + { + // give the device some breathing space once restarted + Thread.Sleep(500); + LogManager.LogInformation("Successfully restarted: {0}", CurrentDevice.InternalSensorName); + } else LogManager.LogError("Failed to restart: {0}", CurrentDevice.InternalSensorName); } diff --git a/HandheldCompanion/Properties/Resources.Designer.cs b/HandheldCompanion/Properties/Resources.Designer.cs index fc086716b..bd5f5582f 100644 --- a/HandheldCompanion/Properties/Resources.Designer.cs +++ b/HandheldCompanion/Properties/Resources.Designer.cs @@ -487,7 +487,7 @@ public static string InputsHotkey_DesktopLayoutEnabledDesc { } /// - /// Looks up a localized string similar to Press to define hotkey input. + /// Looks up a localized string similar to Press to define trigger. /// public static string InputsHotkey_fallbackInput { get { diff --git a/HandheldCompanion/Properties/Resources.resx b/HandheldCompanion/Properties/Resources.resx index 0bbe425ef..c06afa76e 100644 --- a/HandheldCompanion/Properties/Resources.resx +++ b/HandheldCompanion/Properties/Resources.resx @@ -224,7 +224,7 @@ Decrease system or currently applied profile TDP by one watt - Press to define hotkey input + Press to define trigger Press to define keyboard output