From 6c6c93b378e191e15ebf7ccfc9f08bdf88a0b7d6 Mon Sep 17 00:00:00 2001 From: IceStormNG Date: Wed, 26 Jul 2023 21:01:39 +0200 Subject: [PATCH] Better handling of wireless devies that get turned off. The dongle still responds but it sends empty packets. --- app/AsusMouseSettings.cs | 21 +++++++++++++++++++++ app/Peripherals/Mouse/AsusMouse.cs | 29 ++++++++++++++++++++++++++--- app/Settings.cs | 4 +--- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/app/AsusMouseSettings.cs b/app/AsusMouseSettings.cs index 56d101ae..d3c3b069 100644 --- a/app/AsusMouseSettings.cs +++ b/app/AsusMouseSettings.cs @@ -89,6 +89,22 @@ namespace GHelper { mouse.BatteryUpdated -= Mouse_BatteryUpdated; mouse.Disconnect -= Mouse_Disconnect; + mouse.MouseReadyChanged -= Mouse_MouseReadyChanged; + } + + private void Mouse_MouseReadyChanged(object? sender, EventArgs e) + { + if (!mouse.IsDeviceReady) + { + this.Invoke(delegate + { + if (Disposing || IsDisposed) + { + return; + } + Close(); + }); + } } private void Mouse_BatteryUpdated(object? sender, EventArgs e) @@ -606,6 +622,10 @@ namespace GHelper for (int i = 0; i < mouse.DPIProfileCount() && i < 4; ++i) { AsusMouseDPI dpi = mouse.DpiSettings[i]; + if (dpi is null) + { + continue; + } if (mouse.HasDPIColors()) { dpiButtons[i].Image = ControlHelper.TintImage(Properties.Resources.lighting_dot_24, dpi.Color); @@ -641,6 +661,7 @@ namespace GHelper mouse.Disconnect += Mouse_Disconnect; mouse.BatteryUpdated += Mouse_BatteryUpdated; + mouse.MouseReadyChanged += Mouse_MouseReadyChanged; } private void ButtonSync_Click(object sender, EventArgs e) diff --git a/app/Peripherals/Mouse/AsusMouse.cs b/app/Peripherals/Mouse/AsusMouse.cs index 28635ef9..35cf9134 100644 --- a/app/Peripherals/Mouse/AsusMouse.cs +++ b/app/Peripherals/Mouse/AsusMouse.cs @@ -107,10 +107,27 @@ namespace GHelper.Peripherals.Mouse public event EventHandler? Disconnect; public event EventHandler? BatteryUpdated; + public event EventHandler? MouseReadyChanged; private readonly string path; public bool IsDeviceReady { get; protected set; } + + private void SetDeviceReady(bool ready) + { + bool notify = false; + if (IsDeviceReady != ready) + { + notify = true; + } + IsDeviceReady = ready; + + + if (MouseReadyChanged is not null && notify) + { + MouseReadyChanged(this, EventArgs.Empty); + } + } public bool Wireless { get; protected set; } public int Battery { get; protected set; } public bool Charging { get; protected set; } @@ -271,10 +288,10 @@ namespace GHelper.Peripherals.Mouse { //Likely only the dongle connected and the mouse is either sleeping or turned off. //The mouse will not respond with proper data, but empty responses at this point - IsDeviceReady = false; + SetDeviceReady(false); return; } - IsDeviceReady = true; + SetDeviceReady(true); ReadProfile(); ReadDPI(); @@ -380,7 +397,13 @@ namespace GHelper.Peripherals.Mouse Charging = ParseChargingState(response); //If the device goes to standby it will not report battery state anymore. - IsDeviceReady = Battery > 0; + SetDeviceReady(Battery > 0); + + if (!IsDeviceReady) + { + Logger.WriteLine(GetDisplayName() + ": Device gone"); + return; + } Logger.WriteLine(GetDisplayName() + ": Got Battery Percentage " + Battery + "% - Charging:" + Charging); diff --git a/app/Settings.cs b/app/Settings.cs index b1394cad..3bd4b8a2 100644 --- a/app/Settings.cs +++ b/app/Settings.cs @@ -1096,13 +1096,11 @@ namespace GHelper { b.Text = m.GetDisplayName() + "\n" + m.Battery + "%" + (m.Charging ? "(" + Properties.Strings.Charging + ")" : ""); - b.Enabled = true; } else { //Mouse is either not connected or in standby b.Text = m.GetDisplayName() + "\n(" + Properties.Strings.NotConnected + ")"; - b.Enabled = false; } switch (m.DeviceType()) @@ -1144,7 +1142,7 @@ namespace GHelper if (iph.DeviceType() == PeripheralType.Mouse) { AsusMouse? am = iph as AsusMouse; - if (am is null) + if (am is null || !am.IsDeviceReady) { //Should not happen if all device classes are implemented correctly. But better safe than sorry. return;