From a0cff7ba301791ff4427971f9b5f39861b52accd Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Wed, 29 Mar 2017 17:44:10 -0700 Subject: [PATCH] Fixed issue with an unreleased lock blocking device removal. Fixed chance for duplicate log entries for device removal. Resolves issue #7. --- DS4Windows/DS4Control/ControlSerivce.cs | 41 ++++++++++++++++--------- DS4Windows/DS4Library/DS4Device.cs | 10 +++++- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/DS4Windows/DS4Control/ControlSerivce.cs b/DS4Windows/DS4Control/ControlSerivce.cs index 0e98081..7756e46 100644 --- a/DS4Windows/DS4Control/ControlSerivce.cs +++ b/DS4Windows/DS4Control/ControlSerivce.cs @@ -423,20 +423,33 @@ namespace DS4Windows ind = i; if (ind != -1) { - 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) - removed += ". " + Properties.Resources.ChargeController; - LogDebug(removed); - Log.LogToTray(removed); - System.Threading.Thread.Sleep(XINPUT_UNPLUG_SETTLE_TIME); - DS4Controllers[ind] = null; - touchPad[ind] = null; - lag[ind] = false; - inWarnMonitor[ind] = false; - ControllerStatusChanged(this); + bool removingStatus = false; + lock (device.removeLocker) + { + if (!DS4Controllers[ind].IsRemoving) + { + removingStatus = true; + DS4Controllers[ind].IsRemoving = true; + } + } + + if (removingStatus) + { + 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) + removed += ". " + Properties.Resources.ChargeController; + LogDebug(removed); + Log.LogToTray(removed); + System.Threading.Thread.Sleep(XINPUT_UNPLUG_SETTLE_TIME); + DS4Controllers[ind] = null; + touchPad[ind] = null; + lag[ind] = false; + inWarnMonitor[ind] = false; + ControllerStatusChanged(this); + } } } public bool[] lag = { false, false, false, false }; diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index 6967218..d4d64df 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -153,6 +153,8 @@ namespace DS4Windows public HidDevice HidDevice => hDevice; public bool IsExclusive => HidDevice.IsExclusive; public bool IsDisconnecting { get; private set; } + public bool IsRemoving { get; set; } + public object removeLocker = new object(); public string MacAddress => Mac; @@ -651,6 +653,7 @@ namespace DS4Windows outputReportBuffer[10] = ledFlashOff; //flash off duration outputReportBuffer[19] = outputReportBuffer[20] = Convert.ToByte(audio.Volume); } + bool quitOutputThread = false; lock (outputReport) { if (synchronous) @@ -662,7 +665,7 @@ namespace DS4Windows if (!writeOutput()) { Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> encountered synchronous write failure: " + Marshal.GetLastWin32Error()); - StopOutputUpdate(); + quitOutputThread = true; } } catch @@ -683,6 +686,11 @@ namespace DS4Windows } } } + + if (quitOutputThread) + { + StopOutputUpdate(); + } } public bool DisconnectBT()