Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
Fix idle disconnect for Sony Dongle connections
Browse files Browse the repository at this point in the history
Related to issue #13
  • Loading branch information
Ryochan7 committed Apr 8, 2017
1 parent a6adf88 commit da3efd2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
10 changes: 6 additions & 4 deletions DS4Windows/DS4Control/ControlSerivce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,15 +431,16 @@ protected virtual void On_DS4Removal(object sender, EventArgs e)
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;
}
}

Expand All @@ -448,12 +449,13 @@ protected virtual void On_DS4Removal(object sender, EventArgs e)
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;
Expand Down
59 changes: 48 additions & 11 deletions DS4Windows/DS4Library/DS4Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,36 @@ public int getWarnInterval()

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;
Expand Down Expand Up @@ -491,7 +519,7 @@ private void performDs4Input()
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;
Expand All @@ -507,7 +535,7 @@ private void performDs4Input()
{
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;
Expand Down Expand Up @@ -613,6 +641,11 @@ private void performDs4Input()
} */

bool ds4Idle = cState.FrameCounter == pState.FrameCounter;
if (!ds4Idle)
{
isRemoved = false;
}

if (conType == ConnectionType.USB)
{
lastActive = utcNow;
Expand All @@ -621,7 +654,7 @@ private void performDs4Input()
{
bool shouldDisconnect = false;
int idleTime = idleTimeout;
if (idleTime > 0)
if (!isRemoved && idleTime > 0)
{
bool idleInput = isDS4Idle();
if (idleInput)
Expand Down Expand Up @@ -649,8 +682,7 @@ private void performDs4Input()
}
else if (conType == ConnectionType.SONYWA)
{
if (DisconnectDongle())
return; // all done
DisconnectDongle();
}
}
}
Expand Down Expand Up @@ -792,23 +824,28 @@ public bool DisconnectBT()
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;
}
Expand Down

0 comments on commit da3efd2

Please sign in to comment.