Skip to content

Commit

Permalink
Fixed issue with an unreleased lock blocking device removal. Fixed ch…
Browse files Browse the repository at this point in the history
…ance for duplicate log entries for device removal.

Resolves issue #7.
  • Loading branch information
Ryochan7 authored and SiliconExarch committed May 1, 2017
1 parent b3d7573 commit e9146ef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
41 changes: 27 additions & 14 deletions DS4Windows/DS4Control/ControlSerivce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,20 +423,33 @@ protected virtual void On_DS4Removal(object sender, EventArgs e)
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 };
Expand Down
10 changes: 9 additions & 1 deletion DS4Windows/DS4Library/DS4Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public class DS4Device
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;

Expand Down Expand Up @@ -651,6 +653,7 @@ private void sendOutputReport(bool synchronous)
outputReportBuffer[10] = ledFlashOff; //flash off duration
outputReportBuffer[19] = outputReportBuffer[20] = Convert.ToByte(audio.Volume);
}
bool quitOutputThread = false;
lock (outputReport)
{
if (synchronous)
Expand All @@ -662,7 +665,7 @@ private void sendOutputReport(bool synchronous)
if (!writeOutput())
{
Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> encountered synchronous write failure: " + Marshal.GetLastWin32Error());
StopOutputUpdate();
quitOutputThread = true;
}
}
catch
Expand All @@ -683,6 +686,11 @@ private void sendOutputReport(bool synchronous)
}
}
}

if (quitOutputThread)
{
StopOutputUpdate();
}
}

public bool DisconnectBT()
Expand Down

0 comments on commit e9146ef

Please sign in to comment.