Attempt at better clean up when removing devices

Use SynchronizationContext to run some tasks in main thread
as opposed to input thread
This commit is contained in:
Travis Nickles 2017-05-09 03:11:50 -07:00
parent 8b42562aec
commit 6074445c03
4 changed files with 58 additions and 30 deletions

View File

@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Media;
using System.Threading.Tasks;
using static DS4Windows.Global;
namespace DS4Windows
@ -93,7 +90,8 @@ namespace DS4Windows
if (DS4Devices.isExclusiveMode && !device.isExclusive())
{
await System.Threading.Tasks.Task.Delay(5);
String message = Properties.Resources.CouldNotOpenDS4.Replace("*Mac address*", device.getMacAddress()) + " " + Properties.Resources.QuitOtherPrograms;
string message = Properties.Resources.CouldNotOpenDS4.Replace("*Mac address*", device.getMacAddress()) + " " +
Properties.Resources.QuitOtherPrograms;
LogDebug(message, true);
Log.LogToTray(message, true);
}
@ -131,7 +129,7 @@ namespace DS4Windows
WarnExclusiveModeFailure(device);
DS4Controllers[i] = device;
device.Removal -= DS4Devices.On_Removal;
//device.Removal -= DS4Devices.On_Removal;
device.Removal += this.On_DS4Removal;
device.Removal += DS4Devices.On_Removal;
touchPad[i] = new Mouse(i, device);
@ -183,7 +181,7 @@ namespace DS4Windows
Log.LogToTray(logMessage);
}
ControllerStatusChanged(this);
//ControllerStatusChanged(this);
runHotPlug = true;
return true;
@ -202,21 +200,20 @@ namespace DS4Windows
bool anyUnplugged = false;
for (int i = 0, arlength = DS4Controllers.Length; i < arlength; i++)
{
if (DS4Controllers[i] != null)
DS4Device tempDevice = DS4Controllers[i];
if (tempDevice != null)
{
if (DCBTatStop && !DS4Controllers[i].isCharging())
if (DCBTatStop && !tempDevice.isCharging())
{
if (DS4Controllers[i].getConnectionType() == ConnectionType.BT)
if (tempDevice.getConnectionType() == ConnectionType.BT)
{
DS4Device device = DS4Controllers[i];
device.StopUpdate();
device.DisconnectBT(true);
tempDevice.StopUpdate();
tempDevice.DisconnectBT(true);
}
else if (DS4Controllers[i].getConnectionType() == ConnectionType.SONYWA)
else if (tempDevice.getConnectionType() == ConnectionType.SONYWA)
{
DS4Device device = DS4Controllers[i];
device.StopUpdate();
DS4Controllers[i].DisconnectDongle(true);
tempDevice.StopUpdate();
tempDevice.DisconnectDongle(true);
}
}
else
@ -225,7 +222,7 @@ namespace DS4Windows
DS4LightBar.forcedFlash[i] = 0;
DS4LightBar.defaultLight = true;
DS4LightBar.updateLightBar(DS4Controllers[i], i, CurrentState[i], ExposedState[i], touchPad[i]);
DS4Controllers[i].IsRemoved = true;
tempDevice.IsRemoved = true;
System.Threading.Thread.Sleep(50);
}
@ -251,8 +248,6 @@ namespace DS4Windows
DS4Devices.stopControllers();
if (showlog)
LogDebug(Properties.Resources.StoppedDS4Windows);
ControllerStatusChanged(this);
}
runHotPlug = false;
@ -318,7 +313,7 @@ namespace DS4Windows
LogDebug(Properties.Resources.FoundController + device.getMacAddress() + " (" + device.getConnectionType() + ")");
WarnExclusiveModeFailure(device);
DS4Controllers[Index] = device;
device.Removal -= DS4Devices.On_Removal;
//device.Removal -= DS4Devices.On_Removal;
device.Removal += this.On_DS4Removal;
device.Removal += DS4Devices.On_Removal;
touchPad[Index] = new Mouse(Index, device);
@ -570,6 +565,7 @@ namespace DS4Windows
}
}
}
public bool[] lag = { false, false, false, false };
public bool[] inWarnMonitor = { false, false, false, false };
//Called every time the new input report has arrived
@ -1111,7 +1107,7 @@ namespace DS4Windows
public virtual void LogDebug(String Data, bool warning = false)
{
Console.WriteLine(System.DateTime.Now.ToString("G") + "> " + Data);
//Console.WriteLine(System.DateTime.Now.ToString("G") + "> " + Data);
if (Debug != null)
{
DebugEventArgs args = new DebugEventArgs(Data, warning);

View File

@ -962,14 +962,15 @@ namespace DS4Windows
hotkeysTimer.Start();
btnStartStop.Text = Properties.Resources.StopText;
}
else if (btnStartStop.Text == Properties.Resources.StopText)
{
blankControllerTab();
Program.rootHub.Stop(log);
hotkeysTimer.Stop();
btnStartStop.Text = Properties.Resources.StartText;
blankControllerTab();
}
startToolStripMenuItem.Text = btnStartStop.Text;
}
protected void btnClear_Click(object sender, EventArgs e)

View File

@ -355,6 +355,8 @@ namespace DS4Windows
return result;
}
private SynchronizationContext context = null;
public DS4Device(HidDevice hidDevice)
{
hDevice = hidDevice;
@ -386,6 +388,7 @@ namespace DS4Windows
touchpad = new DS4Touchpad();
sixAxis = new DS4SixAxis();
context = SynchronizationContext.Current;
}
public void StartUpdate()
@ -397,7 +400,7 @@ namespace DS4Windows
hDevice.OpenFileStream(inputReport.Length);
}
Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> start");
//Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> start");
sendOutputReport(true); // initialize the output report
ds4Output = new Thread(performDs4Output);
ds4Output.Priority = ThreadPriority.AboveNormal;
@ -575,8 +578,15 @@ namespace DS4Windows
sendOutputReport(true); // Kick Windows into noticing the disconnection.
StopOutputUpdate();
isDisconnecting = true;
context.Send(new SendOrPostCallback(delegate (object state4)
{
Removal?.Invoke(this, EventArgs.Empty);
}), null);
/*
if (Removal != null)
Removal(this, EventArgs.Empty);
*/
return;
}
@ -591,8 +601,13 @@ namespace DS4Windows
Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> disconnect due to read failure: " + Marshal.GetLastWin32Error());
StopOutputUpdate();
isDisconnecting = true;
if (Removal != null)
Removal(this, EventArgs.Empty);
context.Send(new SendOrPostCallback(delegate (object state4)
{
Removal?.Invoke(this, EventArgs.Empty);
}), null);
//if (Removal != null)
// Removal(this, EventArgs.Empty);
return;
}
else
@ -748,7 +763,6 @@ namespace DS4Windows
}
}
// XXX fix initialization ordering so the null checks all go away
if (Report != null)
Report(this, EventArgs.Empty);
@ -803,8 +817,13 @@ namespace DS4Windows
outputReportBuffer[8] = LightBarColor.blue; //blue
outputReportBuffer[9] = ledFlashOn; //flash on duration
outputReportBuffer[10] = ledFlashOff; //flash off duration
outputReportBuffer[19] = outputReportBuffer[20] = Convert.ToByte(audio.getVolume());
outputReportBuffer[21] = Convert.ToByte(micAudio.getVolume());
if (conType == ConnectionType.SONYWA)
{
// Headphone volume levels
outputReportBuffer[19] = outputReportBuffer[20] = Convert.ToByte(audio.getVolume());
// Microphone volume level
outputReportBuffer[21] = Convert.ToByte(micAudio.getVolume());
}
}
bool quitOutputThread = false;
@ -899,7 +918,12 @@ namespace DS4Windows
if (callRemoval)
{
Removal?.Invoke(this, EventArgs.Empty);
context.Send(new SendOrPostCallback(delegate (object state)
{
Removal?.Invoke(this, EventArgs.Empty);
}), null);
//Removal?.Invoke(this, EventArgs.Empty);
}
}
@ -927,8 +951,15 @@ namespace DS4Windows
isDisconnecting = true;
StopOutputUpdate();
context.Send(new SendOrPostCallback(delegate (object state4)
{
Removal?.Invoke(this, EventArgs.Empty);
}), null);
/*
if (Removal != null)
Removal(this, EventArgs.Empty);
*/
}
else if (result && !remove)
{

View File

@ -101,7 +101,7 @@ namespace DS4Windows
else
{
DS4Device ds4Device = new DS4Device(hDevice);
ds4Device.Removal += On_Removal;
//ds4Device.Removal += On_Removal;
Devices.Add(ds4Device.MacAddress, ds4Device);
DevicePaths.Add(hDevice.DevicePath);
}