From 25d751aeabae6424394ab06bd35bae57ae307a5c Mon Sep 17 00:00:00 2001 From: Lesueur Benjamin Date: Mon, 6 Jun 2022 13:57:51 +0200 Subject: [PATCH] 0.9.10.4 (#170) * implemented service manager exist() Allows the service manager to check if the targeted service exists. * remove profile manager from service Services can't access user level variables like MyDocuments. * Fix an issue displaying the overlay title and close icon * implement sensors StartListening * fix a series of issues casused by device resume * build 0.9.10.4 --- ControllerCommon/Managers/ServiceManager.cs | 18 +++++ ControllerService.iss | 4 +- ControllerService/AssemblyInfo1.cs | 4 +- ControllerService/ControllerService.cs | 77 +++++++++++-------- ControllerService/ControllerService.csproj | 2 +- .../Sensors/XInputAccelerometer.cs | 31 ++++++++ ControllerService/Sensors/XInputGirometer.cs | 32 +++++++- .../Sensors/XInputInclinometer.cs | 31 ++++++++ ControllerService/Targets/DualShock4Target.cs | 15 +++- ControllerService/Targets/ViGEmTarget.cs | 14 ++-- ControllerService/Targets/Xbox360Target.cs | 15 +++- ControllerService/XInputController.cs | 18 ++++- HandheldCompanion/HandheldCompanion.csproj | 2 +- .../Views/Windows/MainWindow.xaml.cs | 5 ++ HandheldCompanion/Views/Windows/Overlay.xaml | 1 - 15 files changed, 217 insertions(+), 52 deletions(-) diff --git a/ControllerCommon/Managers/ServiceManager.cs b/ControllerCommon/Managers/ServiceManager.cs index 1d325701f..170a53443 100644 --- a/ControllerCommon/Managers/ServiceManager.cs +++ b/ControllerCommon/Managers/ServiceManager.cs @@ -82,6 +82,24 @@ public void Stop() MonitorTimer = null; } + public bool Exists() + { + try + { + process.StartInfo.Arguments = $"interrogate {name}"; + process.Start(); + process.WaitForExit(); + string output = process.StandardOutput.ReadToEnd(); + return !output.Contains("FAILED 1062"); + } + catch (Exception ex) + { + LogManager.LogError("Service manager returned error: {0}", ex.Message); + } + + return false; + } + private void MonitorHelper(object sender, ElapsedEventArgs e) { lock (updateLock) diff --git a/ControllerService.iss b/ControllerService.iss index 5a185fefe..4f1cd580d 100644 --- a/ControllerService.iss +++ b/ControllerService.iss @@ -648,13 +648,13 @@ end; #define MyAppSetupName 'Handheld Companion' #define MyBuildId 'HandheldCompanion' -#define MyAppVersion '0.9.10.3' +#define MyAppVersion '0.9.10.4' #define MyAppPublisher 'BenjaminLSR' #define MyAppCopyright 'Copyright © BenjaminLSR' #define MyAppURL 'https://github.com/Valkirie/ControllerService' #define MyAppExeName "HandheldCompanion.exe" #define MySerExeName "ControllerService.exe" -#define MyConfiguration "Release" +#define MyConfiguration "Debug" AppName={#MyAppSetupName} AppVersion={#MyAppVersion} diff --git a/ControllerService/AssemblyInfo1.cs b/ControllerService/AssemblyInfo1.cs index 18e183783..45e89ca5e 100644 --- a/ControllerService/AssemblyInfo1.cs +++ b/ControllerService/AssemblyInfo1.cs @@ -19,5 +19,5 @@ // Numéro de build // Révision // -[assembly: AssemblyVersion("0.9.10.3")] -[assembly: AssemblyFileVersion("0.9.10.3")] +[assembly: AssemblyVersion("0.9.10.4")] +[assembly: AssemblyFileVersion("0.9.10.4")] diff --git a/ControllerService/ControllerService.cs b/ControllerService/ControllerService.cs index 3cc8cd3ed..a3218ad02 100644 --- a/ControllerService/ControllerService.cs +++ b/ControllerService/ControllerService.cs @@ -32,7 +32,6 @@ public class ControllerService : IHostedService public XInputController XInputController; private PipeServer pipeServer; - private ProfileManager profileManager; private SystemManager systemManager; private DSUServer DSUServer; public HidHide Hidder; @@ -161,7 +160,6 @@ public ControllerService(IHostApplicationLifetime lifetime) XInputController.SetVibrationStrength(HIDstrength); XInputController.SetPollRate(HIDrate); XInputController.Updated += OnTargetSubmited; - XInputController.StartListening(); // prepare physical controller SetControllerIdx(HIDidx, deviceInstancePath, baseContainerDeviceInstancePath); @@ -170,10 +168,6 @@ public ControllerService(IHostApplicationLifetime lifetime) DSUServer = new DSUServer(DSUip, DSUport); DSUServer.Started += OnDSUStarted; DSUServer.Stopped += OnDSUStopped; - - // initialize Profile Manager - profileManager = new ProfileManager(); - profileManager.Updated += ProfileUpdated; } private SerialUSBIMU sensor; @@ -267,7 +261,8 @@ private void SetControllerIdx(UserIndex idx, string deviceInstancePath, string b private void SetControllerMode(HIDmode mode) { - HIDmode = mode; + if (HIDmode == mode && VirtualTarget != null) + return; // disconnect current virtual controller // todo: do not disconnect if similar to incoming mode @@ -277,7 +272,9 @@ private void SetControllerMode(HIDmode mode) { default: case HIDmode.NoController: + VirtualTarget.Dispose(); VirtualTarget = null; + XInputController.DetachTarget(); break; case HIDmode.DualShock4Controller: VirtualTarget = new DualShock4Target(XInputController, VirtualClient); @@ -287,28 +284,38 @@ private void SetControllerMode(HIDmode mode) break; } - if (VirtualTarget == null) - return; - - VirtualTarget.Connected += OnTargetConnected; - VirtualTarget.Disconnected += OnTargetDisconnected; + if (VirtualTarget != null) + { + VirtualTarget.Connected += OnTargetConnected; + VirtualTarget.Disconnected += OnTargetDisconnected; + XInputController.AttachTarget(VirtualTarget); + } - XInputController.SetViGEmTarget(VirtualTarget); + // update status SetControllerStatus(HIDstatus); + + // update value + HIDmode = mode; } private void SetControllerStatus(HIDstatus status) { - HIDstatus = status; + if (VirtualTarget == null) + return; + switch (status) { + default: case HIDstatus.Connected: - VirtualTarget?.Connect(); + VirtualTarget.Connect(); break; case HIDstatus.Disconnected: - VirtualTarget?.Disconnect(); + VirtualTarget.Disconnect(); break; } + + // update value + HIDstatus = status; } private void OnTargetDisconnected(ViGEmTarget target) @@ -577,6 +584,9 @@ private void ApplySetting(string name, string property) public Task StartAsync(CancellationToken cancellationToken) { + // start listening to controller + XInputController.StartListening(); + // turn on cloaking Hidder.SetCloaking(HIDcloaked, XInputController.ProductName); @@ -585,18 +595,16 @@ public Task StartAsync(CancellationToken cancellationToken) // update virtual controller SetControllerMode(HIDmode); - SetControllerStatus(HIDstatus); // start Pipe Server pipeServer.Start(); - // start and stop Profile Manager - profileManager.Start("Default.json"); - profileManager.Stop(); - // listen to system events SystemEvents.PowerModeChanged += OnPowerChange; + // OnPowerChange(null, new PowerModeChangedEventArgs(PowerModes.Suspend)); + // OnPowerChange(null, new PowerModeChangedEventArgs(PowerModes.Resume)); + return Task.CompletedTask; } @@ -609,7 +617,7 @@ public Task StopAsync(CancellationToken cancellationToken) Hidder?.SetCloaking(!HIDuncloakonclose, XInputController.ProductName); // update virtual controller - SetControllerStatus(HIDstatus.Disconnected); + SetControllerMode(HIDmode.NoController); // stop listening to system events SystemEvents.PowerModeChanged -= OnPowerChange; @@ -636,18 +644,27 @@ private void OnPowerChange(object s, PowerModeChangedEventArgs e) case PowerModes.StatusChange: break; case PowerModes.Resume: - // (re)initialize sensors - XInputController?.UpdateSensors(); + { + // resume delay (arbitrary) + Thread.Sleep(5000); + + // (re)initialize sensors + XInputController?.UpdateSensors(); - // (re)initialize ViGEm - VirtualClient = new ViGEmClient(); + // (re)initialize ViGEm + VirtualClient = new ViGEmClient(); - // update virtual controller - SetControllerMode(HIDmode); - SetControllerStatus(HIDstatus); + SetControllerMode(HIDmode); + } break; case PowerModes.Suspend: - VirtualClient.Dispose(); + { + VirtualTarget.Dispose(); + VirtualTarget = null; + + VirtualClient.Dispose(); + VirtualClient = null; + } break; } } diff --git a/ControllerService/ControllerService.csproj b/ControllerService/ControllerService.csproj index 36c168261..db5b2075c 100644 --- a/ControllerService/ControllerService.csproj +++ b/ControllerService/ControllerService.csproj @@ -14,7 +14,7 @@ true $(SolutionDir)bin\$(Configuration) portable - 0.9.10.3 + 0.9.10.4 full diff --git a/ControllerService/Sensors/XInputAccelerometer.cs b/ControllerService/Sensors/XInputAccelerometer.cs index 87636d6ad..caf89286a 100644 --- a/ControllerService/Sensors/XInputAccelerometer.cs +++ b/ControllerService/Sensors/XInputAccelerometer.cs @@ -57,6 +57,37 @@ public void UpdateSensor(SensorFamily sensorFamily) LogManager.LogInformation("{0} initialised as a {1}. Baud rate set to {2}", this.ToString(), sensorFamily.ToString(), ((SerialUSBIMU)sensor).GetInterval()); break; } + + StartListening(sensorFamily); + } + + public void StartListening(SensorFamily sensorFamily) + { + switch (sensorFamily) + { + case SensorFamily.WindowsDevicesSensors: + ((Accelerometer)sensor).ReadingChanged += ReadingChanged; + break; + case SensorFamily.SerialUSBIMU: + ((SerialUSBIMU)sensor).ReadingChanged += ReadingChanged; + break; + } + } + + public void StopListening(SensorFamily sensorFamily) + { + if (sensor is null) + return; + + switch (sensorFamily) + { + case SensorFamily.WindowsDevicesSensors: + ((Accelerometer)sensor).ReadingChanged -= ReadingChanged; + break; + case SensorFamily.SerialUSBIMU: + ((SerialUSBIMU)sensor).ReadingChanged -= ReadingChanged; + break; + } } private void ReadingChanged(Vector3 AccelerationG, Vector3 AngularVelocityDeg) diff --git a/ControllerService/Sensors/XInputGirometer.cs b/ControllerService/Sensors/XInputGirometer.cs index 9bac799ed..62fdc8b63 100644 --- a/ControllerService/Sensors/XInputGirometer.cs +++ b/ControllerService/Sensors/XInputGirometer.cs @@ -47,14 +47,42 @@ public void UpdateSensor(SensorFamily sensorFamily) { case SensorFamily.WindowsDevicesSensors: ((Gyrometer)sensor).ReportInterval = (uint)updateInterval; - ((Gyrometer)sensor).ReadingChanged += ReadingChanged; LogManager.LogInformation("{0} initialised as a {1}. Report interval set to {2}ms", this.ToString(), sensorFamily.ToString(), updateInterval); break; + case SensorFamily.SerialUSBIMU: + LogManager.LogInformation("{0} initialised as a {1}. Baud rate set to {2}", this.ToString(), sensorFamily.ToString(), ((SerialUSBIMU)sensor).GetInterval()); + break; + } + + StartListening(sensorFamily); + } + + public void StartListening(SensorFamily sensorFamily) + { + switch (sensorFamily) + { + case SensorFamily.WindowsDevicesSensors: + ((Gyrometer)sensor).ReadingChanged += ReadingChanged; + break; case SensorFamily.SerialUSBIMU: ((SerialUSBIMU)sensor).ReadingChanged += ReadingChanged; + break; + } + } - LogManager.LogInformation("{0} initialised as a {1}. Baud rate set to {2}", this.ToString(), sensorFamily.ToString(), ((SerialUSBIMU)sensor).GetInterval()); + public void StopListening(SensorFamily sensorFamily) + { + if (sensor is null) + return; + + switch (sensorFamily) + { + case SensorFamily.WindowsDevicesSensors: + ((Gyrometer)sensor).ReadingChanged -= ReadingChanged; + break; + case SensorFamily.SerialUSBIMU: + ((SerialUSBIMU)sensor).ReadingChanged -= ReadingChanged; break; } } diff --git a/ControllerService/Sensors/XInputInclinometer.cs b/ControllerService/Sensors/XInputInclinometer.cs index 68e582e41..42b361117 100644 --- a/ControllerService/Sensors/XInputInclinometer.cs +++ b/ControllerService/Sensors/XInputInclinometer.cs @@ -49,6 +49,37 @@ public void UpdateSensor(SensorFamily sensorFamily) LogManager.LogInformation("{0} initialised as a {1}. Baud rate set to {2}", this.ToString(), sensorFamily.ToString(), ((SerialUSBIMU)sensor).GetInterval()); break; } + + StartListening(sensorFamily); + } + + public void StartListening(SensorFamily sensorFamily) + { + switch (sensorFamily) + { + case SensorFamily.WindowsDevicesSensors: + ((Accelerometer)sensor).ReadingChanged += ReadingChanged; + break; + case SensorFamily.SerialUSBIMU: + ((SerialUSBIMU)sensor).ReadingChanged += ReadingChanged; + break; + } + } + + public void StopListening(SensorFamily sensorFamily) + { + if (sensor is null) + return; + + switch (sensorFamily) + { + case SensorFamily.WindowsDevicesSensors: + ((Accelerometer)sensor).ReadingChanged -= ReadingChanged; + break; + case SensorFamily.SerialUSBIMU: + ((SerialUSBIMU)sensor).ReadingChanged -= ReadingChanged; + break; + } } private void ReadingChanged(Vector3 AccelerationG, Vector3 AngularVelocityDeg) diff --git a/ControllerService/Targets/DualShock4Target.cs b/ControllerService/Targets/DualShock4Target.cs index 2ad1bcb6e..5373ee96b 100644 --- a/ControllerService/Targets/DualShock4Target.cs +++ b/ControllerService/Targets/DualShock4Target.cs @@ -1,4 +1,5 @@ -using ControllerCommon.Utils; +using ControllerCommon.Managers; +using ControllerCommon.Utils; using ControllerService.Sensors; using Nefarius.ViGEm.Client; using Nefarius.ViGEm.Client.Exceptions; @@ -56,6 +57,8 @@ public DualShock4Target(XInputController xinput, ViGEmClient client) : base(xinp virtualController = client.CreateDualShock4Controller(); virtualController.AutoSubmitReport = false; virtualController.FeedbackReceived += FeedbackReceived; + + LogManager.LogInformation("{0} initialized, {1}", ToString(), virtualController); } public override void Connect() @@ -218,12 +221,18 @@ public override unsafe void UpdateReport(Gamepad Gamepad) { virtualController.SubmitRawReport(rawOutReportEx); } - catch (VigemBusNotFoundException) + catch (VigemBusNotFoundException ex) { - // todo: prevent this from happening ! + LogManager.LogCritical(ex.Message); } base.SubmitReport(); } + + public override void Dispose() + { + Disconnect(); + base.Dispose(); + } } } diff --git a/ControllerService/Targets/ViGEmTarget.cs b/ControllerService/Targets/ViGEmTarget.cs index 8c993e765..b4ad91739 100644 --- a/ControllerService/Targets/ViGEmTarget.cs +++ b/ControllerService/Targets/ViGEmTarget.cs @@ -11,7 +11,7 @@ namespace ControllerService.Targets { - public abstract class ViGEmTarget + public abstract class ViGEmTarget : IDisposable { #region imports [StructLayout(LayoutKind.Sequential)] @@ -55,7 +55,7 @@ protected struct XInputStateSecret public event DisconnectedEventHandler Disconnected; public delegate void DisconnectedEventHandler(ViGEmTarget target); - protected bool IsConnected; + public bool IsConnected = false; protected ViGEmTarget(XInputController xinput, ViGEmClient client) { @@ -78,12 +78,12 @@ protected void FeedbackReceived(object sender, EventArgs e) public void SetVibrationStrength(double strength) { vibrationStrength = strength / 100.0f; - LogManager.LogInformation("{0} vibration strength set to {1}%", this, strength); + LogManager.LogInformation("{0} vibration strength set to {1}%", ToString(), strength); } public override string ToString() { - return EnumUtils.GetDescriptionFromEnumValue(this.HID); + return EnumUtils.GetDescriptionFromEnumValue(HID); } public virtual void Connect() @@ -100,7 +100,6 @@ public virtual void Disconnect() LogManager.LogInformation("{0} disconnected", ToString()); } - public virtual unsafe void UpdateReport(Gamepad Gamepad) { // get current gamepad state @@ -228,7 +227,10 @@ public virtual unsafe void UpdateReport(Gamepad Gamepad) internal void SubmitReport() { - // do something + } + + public virtual void Dispose() + { } } } \ No newline at end of file diff --git a/ControllerService/Targets/Xbox360Target.cs b/ControllerService/Targets/Xbox360Target.cs index 7816142dc..b419dbc32 100644 --- a/ControllerService/Targets/Xbox360Target.cs +++ b/ControllerService/Targets/Xbox360Target.cs @@ -1,4 +1,5 @@ -using ControllerCommon.Utils; +using ControllerCommon.Managers; +using ControllerCommon.Utils; using Nefarius.ViGEm.Client; using Nefarius.ViGEm.Client.Exceptions; using Nefarius.ViGEm.Client.Targets; @@ -54,6 +55,8 @@ public Xbox360Target(XInputController xinput, ViGEmClient client) : base(xinput, virtualController = client.CreateXbox360Controller(); virtualController.AutoSubmitReport = false; virtualController.FeedbackReceived += FeedbackReceived; + + LogManager.LogInformation("{0} initialized, {1}", ToString(), virtualController); } public override void Connect() @@ -115,12 +118,18 @@ public override unsafe void UpdateReport(Gamepad Gamepad) { virtualController.SubmitReport(); } - catch (VigemBusNotFoundException) + catch (VigemBusNotFoundException ex) { - // todo: prevent this from happening ! + LogManager.LogCritical(ex.Message); } base.SubmitReport(); } + + public override void Dispose() + { + Disconnect(); + base.Dispose(); + } } } diff --git a/ControllerService/XInputController.cs b/ControllerService/XInputController.cs index ed404ead3..f7c5e092a 100644 --- a/ControllerService/XInputController.cs +++ b/ControllerService/XInputController.cs @@ -94,6 +94,10 @@ public void StartListening() public void StopListening() { + Gyrometer.StopListening(sensorFamily); + Accelerometer.StopListening(sensorFamily); + Inclinometer.StopListening(sensorFamily); + stopwatch.Stop(); UpdateTimer.Tick -= UpdateTimer_Ticked; @@ -243,8 +247,11 @@ public void SetVibrationStrength(double strength) this.virtualTarget?.SetVibrationStrength(vibrationStrength); } - public void SetViGEmTarget(ViGEmTarget target) + public void AttachTarget(ViGEmTarget target) { + if (target is null) + return; + this.virtualTarget = target; SetPollRate(updateInterval); @@ -252,5 +259,14 @@ public void SetViGEmTarget(ViGEmTarget target) LogManager.LogInformation("{0} attached to {1} on slot {2}", target, ProductName, controllerEx.Controller.UserIndex); } + + public void DetachTarget() + { + if (virtualTarget is null) + return; + + LogManager.LogInformation("{0} detached from {1} on slot {2}", virtualTarget, ProductName, controllerEx.Controller.UserIndex); + this.virtualTarget = null; + } } } \ No newline at end of file diff --git a/HandheldCompanion/HandheldCompanion.csproj b/HandheldCompanion/HandheldCompanion.csproj index c502f6abe..630749631 100644 --- a/HandheldCompanion/HandheldCompanion.csproj +++ b/HandheldCompanion/HandheldCompanion.csproj @@ -10,7 +10,7 @@ HandheldCompanion.App $(SolutionDir)bin\$(Configuration) Resources\icon.ico - 0.9.10.3 + 0.9.10.4 app.manifest diff --git a/HandheldCompanion/Views/Windows/MainWindow.xaml.cs b/HandheldCompanion/Views/Windows/MainWindow.xaml.cs index f3a43b770..e9146c140 100644 --- a/HandheldCompanion/Views/Windows/MainWindow.xaml.cs +++ b/HandheldCompanion/Views/Windows/MainWindow.xaml.cs @@ -394,7 +394,12 @@ private void Window_Loaded(object sender, RoutedEventArgs e) { // start service with companion if (settingsPage.StartServiceWithCompanion) + { + if (!serviceManager.Exists()) + serviceManager.CreateService(CurrentPathService); + serviceManager.StartServiceAsync(); + } } } diff --git a/HandheldCompanion/Views/Windows/Overlay.xaml b/HandheldCompanion/Views/Windows/Overlay.xaml index 0b8dffea4..30c72988a 100644 --- a/HandheldCompanion/Views/Windows/Overlay.xaml +++ b/HandheldCompanion/Views/Windows/Overlay.xaml @@ -7,7 +7,6 @@ xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf" xmlns:ui="http://schemas.modernwpf.com/2019" mc:Ignorable="d" - ui:WindowHelper.UseModernWindowStyle="True" Title="Overlay" Visibility="Collapsed" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ShowInTaskbar="False" Topmost="True" Focusable="False" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowState="Maximized" Closing="Window_Closing">