From da3efd258970d1b38a5a7973b8810e5bcf5711e5 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Sat, 8 Apr 2017 16:13:56 -0700 Subject: [PATCH] Fix idle disconnect for Sony Dongle connections Related to issue #13 --- DS4Windows/DS4Control/ControlSerivce.cs | 10 +++-- DS4Windows/DS4Library/DS4Device.cs | 59 ++++++++++++++++++++----- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/DS4Windows/DS4Control/ControlSerivce.cs b/DS4Windows/DS4Control/ControlSerivce.cs index 97961dad4a..2455279b75 100644 --- a/DS4Windows/DS4Control/ControlSerivce.cs +++ b/DS4Windows/DS4Control/ControlSerivce.cs @@ -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; } } @@ -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; diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index 330b0b201b..c7734428eb 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -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; @@ -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; @@ -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; @@ -613,6 +641,11 @@ private void performDs4Input() } */ bool ds4Idle = cState.FrameCounter == pState.FrameCounter; + if (!ds4Idle) + { + isRemoved = false; + } + if (conType == ConnectionType.USB) { lastActive = utcNow; @@ -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) @@ -649,8 +682,7 @@ private void performDs4Input() } else if (conType == ConnectionType.SONYWA) { - if (DisconnectDongle()) - return; // all done + DisconnectDongle(); } } } @@ -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; }