Fix idle disconnect for Sony Dongle connections

Related to issue #13
This commit is contained in:
Travis Nickles 2017-04-08 16:13:56 -07:00
parent a6adf886dc
commit da3efd2589
2 changed files with 54 additions and 15 deletions

View File

@ -431,15 +431,16 @@ namespace DS4Windows
for (int i = 0, arlength = DS4Controllers.Length; ind == -1 && i < arlength; i++)
if (DS4Controllers[i] != null && device.MacAddress == DS4Controllers[i].MacAddress)
ind = i;
if (ind != -1)
{
bool removingStatus = false;
lock (device.removeLocker)
{
if (!DS4Controllers[ind].IsRemoving)
if (!device.IsRemoving)
{
removingStatus = true;
DS4Controllers[ind].IsRemoving = true;
device.IsRemoving = true;
}
}
@ -448,12 +449,13 @@ namespace DS4Windows
CurrentState[ind].Battery = PreviousState[ind].Battery = 0; // Reset for the next connection's initial status change.
x360Bus.Unplug(ind);
string removed = Properties.Resources.ControllerWasRemoved.Replace("*Mac address*", (ind + 1).ToString());
if (DS4Controllers[ind].Battery <= 20 &&
DS4Controllers[ind].ConnectionType == ConnectionType.BT && !DS4Controllers[ind].Charging)
if (device.Battery <= 20 &&
device.ConnectionType == ConnectionType.BT && !device.Charging)
removed += ". " + Properties.Resources.ChargeController;
LogDebug(removed);
Log.LogToTray(removed);
System.Threading.Thread.Sleep(XINPUT_UNPLUG_SETTLE_TIME);
device.IsRemoved = true;
DS4Controllers[ind] = null;
touchPad[ind] = null;
lag[ind] = false;

View File

@ -157,8 +157,36 @@ namespace DS4Windows
public HidDevice HidDevice => hDevice;
public bool IsExclusive => HidDevice.IsExclusive;
public bool IsDisconnecting { get; private set; }
public bool IsRemoving { get; set; }
private bool isDisconnecting = false;
public bool IsDisconnecting
{
get { return isDisconnecting; }
private set
{
this.isDisconnecting = value;
}
}
private bool isRemoving = false;
public bool IsRemoving
{
get { return isRemoving; }
set
{
this.isRemoving = value;
}
}
private bool isRemoved = false;
public bool IsRemoved
{
get { return isRemoved; }
set
{
this.isRemoved = value;
}
}
public object removeLocker = new object();
public string MacAddress => Mac;
@ -491,7 +519,7 @@ namespace DS4Windows
Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> disconnect due to read failure: " + Marshal.GetLastWin32Error());
sendOutputReport(true); // Kick Windows into noticing the disconnection.
StopOutputUpdate();
IsDisconnecting = true;
isDisconnecting = true;
if (Removal != null)
Removal(this, EventArgs.Empty);
return;
@ -507,7 +535,7 @@ namespace DS4Windows
{
Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> disconnect due to read failure: " + Marshal.GetLastWin32Error());
StopOutputUpdate();
IsDisconnecting = true;
isDisconnecting = true;
if (Removal != null)
Removal(this, EventArgs.Empty);
return;
@ -613,6 +641,11 @@ namespace DS4Windows
} */
bool ds4Idle = cState.FrameCounter == pState.FrameCounter;
if (!ds4Idle)
{
isRemoved = false;
}
if (conType == ConnectionType.USB)
{
lastActive = utcNow;
@ -621,7 +654,7 @@ namespace DS4Windows
{
bool shouldDisconnect = false;
int idleTime = idleTimeout;
if (idleTime > 0)
if (!isRemoved && idleTime > 0)
{
bool idleInput = isDS4Idle();
if (idleInput)
@ -649,8 +682,7 @@ namespace DS4Windows
}
else if (conType == ConnectionType.SONYWA)
{
if (DisconnectDongle())
return; // all done
DisconnectDongle();
}
}
}
@ -792,23 +824,28 @@ namespace DS4Windows
return false;
}
public bool DisconnectDongle(bool remove=false)
public bool DisconnectDongle(bool remove = false)
{
bool result = false;
byte[] disconnectReport = new byte[65];
disconnectReport[0] = 0xe2;
disconnectReport[1] = 0x02;
for (int i = 2; i < 65; i++)
disconnectReport[i] = 0;
Array.Clear(disconnectReport, 2, 63);
//for (int i = 2; i < 65; i++)
// disconnectReport[i] = 0;
result = hDevice.WriteFeatureReport(disconnectReport);
if (result && remove)
{
IsDisconnecting = true;
isDisconnecting = true;
StopOutputUpdate();
if (Removal != null)
Removal(this, EventArgs.Empty);
}
else if (result && !remove)
{
isRemoved = true;
}
return result;
}