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

View File

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