mirror of
https://github.com/cemu-project/DS4Windows.git
synced 2024-11-30 04:54:20 +01:00
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:
parent
8b42562aec
commit
6074445c03
@ -1,11 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
using System.IO;
|
|
||||||
using System.Media;
|
using System.Media;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using static DS4Windows.Global;
|
using static DS4Windows.Global;
|
||||||
|
|
||||||
namespace DS4Windows
|
namespace DS4Windows
|
||||||
@ -93,7 +90,8 @@ namespace DS4Windows
|
|||||||
if (DS4Devices.isExclusiveMode && !device.isExclusive())
|
if (DS4Devices.isExclusiveMode && !device.isExclusive())
|
||||||
{
|
{
|
||||||
await System.Threading.Tasks.Task.Delay(5);
|
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);
|
LogDebug(message, true);
|
||||||
Log.LogToTray(message, true);
|
Log.LogToTray(message, true);
|
||||||
}
|
}
|
||||||
@ -131,7 +129,7 @@ namespace DS4Windows
|
|||||||
|
|
||||||
WarnExclusiveModeFailure(device);
|
WarnExclusiveModeFailure(device);
|
||||||
DS4Controllers[i] = device;
|
DS4Controllers[i] = device;
|
||||||
device.Removal -= DS4Devices.On_Removal;
|
//device.Removal -= DS4Devices.On_Removal;
|
||||||
device.Removal += this.On_DS4Removal;
|
device.Removal += this.On_DS4Removal;
|
||||||
device.Removal += DS4Devices.On_Removal;
|
device.Removal += DS4Devices.On_Removal;
|
||||||
touchPad[i] = new Mouse(i, device);
|
touchPad[i] = new Mouse(i, device);
|
||||||
@ -183,7 +181,7 @@ namespace DS4Windows
|
|||||||
Log.LogToTray(logMessage);
|
Log.LogToTray(logMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
ControllerStatusChanged(this);
|
//ControllerStatusChanged(this);
|
||||||
runHotPlug = true;
|
runHotPlug = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -202,21 +200,20 @@ namespace DS4Windows
|
|||||||
bool anyUnplugged = false;
|
bool anyUnplugged = false;
|
||||||
for (int i = 0, arlength = DS4Controllers.Length; i < arlength; i++)
|
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];
|
tempDevice.StopUpdate();
|
||||||
device.StopUpdate();
|
tempDevice.DisconnectBT(true);
|
||||||
device.DisconnectBT(true);
|
|
||||||
}
|
}
|
||||||
else if (DS4Controllers[i].getConnectionType() == ConnectionType.SONYWA)
|
else if (tempDevice.getConnectionType() == ConnectionType.SONYWA)
|
||||||
{
|
{
|
||||||
DS4Device device = DS4Controllers[i];
|
tempDevice.StopUpdate();
|
||||||
device.StopUpdate();
|
tempDevice.DisconnectDongle(true);
|
||||||
DS4Controllers[i].DisconnectDongle(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -225,7 +222,7 @@ namespace DS4Windows
|
|||||||
DS4LightBar.forcedFlash[i] = 0;
|
DS4LightBar.forcedFlash[i] = 0;
|
||||||
DS4LightBar.defaultLight = true;
|
DS4LightBar.defaultLight = true;
|
||||||
DS4LightBar.updateLightBar(DS4Controllers[i], i, CurrentState[i], ExposedState[i], touchPad[i]);
|
DS4LightBar.updateLightBar(DS4Controllers[i], i, CurrentState[i], ExposedState[i], touchPad[i]);
|
||||||
DS4Controllers[i].IsRemoved = true;
|
tempDevice.IsRemoved = true;
|
||||||
System.Threading.Thread.Sleep(50);
|
System.Threading.Thread.Sleep(50);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,8 +248,6 @@ namespace DS4Windows
|
|||||||
DS4Devices.stopControllers();
|
DS4Devices.stopControllers();
|
||||||
if (showlog)
|
if (showlog)
|
||||||
LogDebug(Properties.Resources.StoppedDS4Windows);
|
LogDebug(Properties.Resources.StoppedDS4Windows);
|
||||||
|
|
||||||
ControllerStatusChanged(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
runHotPlug = false;
|
runHotPlug = false;
|
||||||
@ -318,7 +313,7 @@ namespace DS4Windows
|
|||||||
LogDebug(Properties.Resources.FoundController + device.getMacAddress() + " (" + device.getConnectionType() + ")");
|
LogDebug(Properties.Resources.FoundController + device.getMacAddress() + " (" + device.getConnectionType() + ")");
|
||||||
WarnExclusiveModeFailure(device);
|
WarnExclusiveModeFailure(device);
|
||||||
DS4Controllers[Index] = device;
|
DS4Controllers[Index] = device;
|
||||||
device.Removal -= DS4Devices.On_Removal;
|
//device.Removal -= DS4Devices.On_Removal;
|
||||||
device.Removal += this.On_DS4Removal;
|
device.Removal += this.On_DS4Removal;
|
||||||
device.Removal += DS4Devices.On_Removal;
|
device.Removal += DS4Devices.On_Removal;
|
||||||
touchPad[Index] = new Mouse(Index, device);
|
touchPad[Index] = new Mouse(Index, device);
|
||||||
@ -570,6 +565,7 @@ namespace DS4Windows
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool[] lag = { false, false, false, false };
|
public bool[] lag = { false, false, false, false };
|
||||||
public bool[] inWarnMonitor = { false, false, false, false };
|
public bool[] inWarnMonitor = { false, false, false, false };
|
||||||
//Called every time the new input report has arrived
|
//Called every time the new input report has arrived
|
||||||
@ -1111,7 +1107,7 @@ namespace DS4Windows
|
|||||||
|
|
||||||
public virtual void LogDebug(String Data, bool warning = false)
|
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)
|
if (Debug != null)
|
||||||
{
|
{
|
||||||
DebugEventArgs args = new DebugEventArgs(Data, warning);
|
DebugEventArgs args = new DebugEventArgs(Data, warning);
|
||||||
|
@ -962,14 +962,15 @@ namespace DS4Windows
|
|||||||
hotkeysTimer.Start();
|
hotkeysTimer.Start();
|
||||||
btnStartStop.Text = Properties.Resources.StopText;
|
btnStartStop.Text = Properties.Resources.StopText;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (btnStartStop.Text == Properties.Resources.StopText)
|
else if (btnStartStop.Text == Properties.Resources.StopText)
|
||||||
{
|
{
|
||||||
blankControllerTab();
|
blankControllerTab();
|
||||||
Program.rootHub.Stop(log);
|
Program.rootHub.Stop(log);
|
||||||
hotkeysTimer.Stop();
|
hotkeysTimer.Stop();
|
||||||
btnStartStop.Text = Properties.Resources.StartText;
|
btnStartStop.Text = Properties.Resources.StartText;
|
||||||
|
blankControllerTab();
|
||||||
}
|
}
|
||||||
|
|
||||||
startToolStripMenuItem.Text = btnStartStop.Text;
|
startToolStripMenuItem.Text = btnStartStop.Text;
|
||||||
}
|
}
|
||||||
protected void btnClear_Click(object sender, EventArgs e)
|
protected void btnClear_Click(object sender, EventArgs e)
|
||||||
|
@ -355,6 +355,8 @@ namespace DS4Windows
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SynchronizationContext context = null;
|
||||||
|
|
||||||
public DS4Device(HidDevice hidDevice)
|
public DS4Device(HidDevice hidDevice)
|
||||||
{
|
{
|
||||||
hDevice = hidDevice;
|
hDevice = hidDevice;
|
||||||
@ -386,6 +388,7 @@ namespace DS4Windows
|
|||||||
|
|
||||||
touchpad = new DS4Touchpad();
|
touchpad = new DS4Touchpad();
|
||||||
sixAxis = new DS4SixAxis();
|
sixAxis = new DS4SixAxis();
|
||||||
|
context = SynchronizationContext.Current;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartUpdate()
|
public void StartUpdate()
|
||||||
@ -397,7 +400,7 @@ namespace DS4Windows
|
|||||||
hDevice.OpenFileStream(inputReport.Length);
|
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
|
sendOutputReport(true); // initialize the output report
|
||||||
ds4Output = new Thread(performDs4Output);
|
ds4Output = new Thread(performDs4Output);
|
||||||
ds4Output.Priority = ThreadPriority.AboveNormal;
|
ds4Output.Priority = ThreadPriority.AboveNormal;
|
||||||
@ -575,8 +578,15 @@ namespace DS4Windows
|
|||||||
sendOutputReport(true); // Kick Windows into noticing the disconnection.
|
sendOutputReport(true); // Kick Windows into noticing the disconnection.
|
||||||
StopOutputUpdate();
|
StopOutputUpdate();
|
||||||
isDisconnecting = true;
|
isDisconnecting = true;
|
||||||
|
context.Send(new SendOrPostCallback(delegate (object state4)
|
||||||
|
{
|
||||||
|
Removal?.Invoke(this, EventArgs.Empty);
|
||||||
|
}), null);
|
||||||
|
|
||||||
|
/*
|
||||||
if (Removal != null)
|
if (Removal != null)
|
||||||
Removal(this, EventArgs.Empty);
|
Removal(this, EventArgs.Empty);
|
||||||
|
*/
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -591,8 +601,13 @@ namespace DS4Windows
|
|||||||
Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> disconnect due to read failure: " + Marshal.GetLastWin32Error());
|
Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> disconnect due to read failure: " + Marshal.GetLastWin32Error());
|
||||||
StopOutputUpdate();
|
StopOutputUpdate();
|
||||||
isDisconnecting = true;
|
isDisconnecting = true;
|
||||||
if (Removal != null)
|
context.Send(new SendOrPostCallback(delegate (object state4)
|
||||||
Removal(this, EventArgs.Empty);
|
{
|
||||||
|
Removal?.Invoke(this, EventArgs.Empty);
|
||||||
|
}), null);
|
||||||
|
|
||||||
|
//if (Removal != null)
|
||||||
|
// Removal(this, EventArgs.Empty);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -748,7 +763,6 @@ namespace DS4Windows
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX fix initialization ordering so the null checks all go away
|
|
||||||
if (Report != null)
|
if (Report != null)
|
||||||
Report(this, EventArgs.Empty);
|
Report(this, EventArgs.Empty);
|
||||||
|
|
||||||
@ -803,9 +817,14 @@ namespace DS4Windows
|
|||||||
outputReportBuffer[8] = LightBarColor.blue; //blue
|
outputReportBuffer[8] = LightBarColor.blue; //blue
|
||||||
outputReportBuffer[9] = ledFlashOn; //flash on duration
|
outputReportBuffer[9] = ledFlashOn; //flash on duration
|
||||||
outputReportBuffer[10] = ledFlashOff; //flash off duration
|
outputReportBuffer[10] = ledFlashOff; //flash off duration
|
||||||
|
if (conType == ConnectionType.SONYWA)
|
||||||
|
{
|
||||||
|
// Headphone volume levels
|
||||||
outputReportBuffer[19] = outputReportBuffer[20] = Convert.ToByte(audio.getVolume());
|
outputReportBuffer[19] = outputReportBuffer[20] = Convert.ToByte(audio.getVolume());
|
||||||
|
// Microphone volume level
|
||||||
outputReportBuffer[21] = Convert.ToByte(micAudio.getVolume());
|
outputReportBuffer[21] = Convert.ToByte(micAudio.getVolume());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool quitOutputThread = false;
|
bool quitOutputThread = false;
|
||||||
|
|
||||||
@ -898,8 +917,13 @@ namespace DS4Windows
|
|||||||
StopOutputUpdate();
|
StopOutputUpdate();
|
||||||
|
|
||||||
if (callRemoval)
|
if (callRemoval)
|
||||||
|
{
|
||||||
|
context.Send(new SendOrPostCallback(delegate (object state)
|
||||||
{
|
{
|
||||||
Removal?.Invoke(this, EventArgs.Empty);
|
Removal?.Invoke(this, EventArgs.Empty);
|
||||||
|
}), null);
|
||||||
|
|
||||||
|
//Removal?.Invoke(this, EventArgs.Empty);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -927,8 +951,15 @@ namespace DS4Windows
|
|||||||
isDisconnecting = true;
|
isDisconnecting = true;
|
||||||
StopOutputUpdate();
|
StopOutputUpdate();
|
||||||
|
|
||||||
|
context.Send(new SendOrPostCallback(delegate (object state4)
|
||||||
|
{
|
||||||
|
Removal?.Invoke(this, EventArgs.Empty);
|
||||||
|
}), null);
|
||||||
|
|
||||||
|
/*
|
||||||
if (Removal != null)
|
if (Removal != null)
|
||||||
Removal(this, EventArgs.Empty);
|
Removal(this, EventArgs.Empty);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
else if (result && !remove)
|
else if (result && !remove)
|
||||||
{
|
{
|
||||||
|
@ -101,7 +101,7 @@ namespace DS4Windows
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
DS4Device ds4Device = new DS4Device(hDevice);
|
DS4Device ds4Device = new DS4Device(hDevice);
|
||||||
ds4Device.Removal += On_Removal;
|
//ds4Device.Removal += On_Removal;
|
||||||
Devices.Add(ds4Device.MacAddress, ds4Device);
|
Devices.Add(ds4Device.MacAddress, ds4Device);
|
||||||
DevicePaths.Add(hDevice.DevicePath);
|
DevicePaths.Add(hDevice.DevicePath);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user