Better handling of wireless devies that get turned off. The dongle still responds but it sends empty packets.

This commit is contained in:
IceStormNG
2023-07-26 21:01:39 +02:00
parent f9a8665290
commit 6c6c93b378
3 changed files with 48 additions and 6 deletions

View File

@@ -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)

View File

@@ -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);

View File

@@ -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;