diff --git a/DS4Windows/DS4Control/ControlService.cs b/DS4Windows/DS4Control/ControlService.cs index 342595c..09b696e 100644 --- a/DS4Windows/DS4Control/ControlService.cs +++ b/DS4Windows/DS4Control/ControlService.cs @@ -131,6 +131,7 @@ namespace DS4Windows WarnExclusiveModeFailure(device); DS4Controllers[i] = device; + device.setUiContext(SynchronizationContext.Current); device.Removal += this.On_DS4Removal; device.Removal += DS4Devices.On_Removal; device.SyncChange += DS4Devices.UpdateSerial; @@ -270,29 +271,10 @@ namespace DS4Windows return true; } - public bool HotPlug() + public bool HotPlug(SynchronizationContext uiContext) { if (running) { - // Do first run check for Quick Charge checks. Needed so old device will - // be removed before performing another controller scan - /*if (getQuickCharge()) - { - for (int i = 0, devlen = DS4Controllers.Length; i < devlen; i++) - { - DS4Device device = DS4Controllers[i]; - if (device != null) - { - if (device.getConnectionType() == ConnectionType.BT && device.isCharging()) - { - device.StopUpdate(); - device.DisconnectBT(true); - } - } - } - } - */ - DS4Devices.findControllers(); IEnumerable devices = DS4Devices.getDS4Controllers(); //foreach (DS4Device device in devices) @@ -325,6 +307,7 @@ namespace DS4Windows LogDebug(Properties.Resources.FoundController + device.getMacAddress() + " (" + device.getConnectionType() + ")"); WarnExclusiveModeFailure(device); DS4Controllers[Index] = device; + device.setUiContext(uiContext); device.Removal += this.On_DS4Removal; device.Removal += DS4Devices.On_Removal; device.SyncChange += DS4Devices.UpdateSerial; diff --git a/DS4Windows/DS4Control/Mapping.cs b/DS4Windows/DS4Control/Mapping.cs index c224d54..7f866ac 100644 --- a/DS4Windows/DS4Control/Mapping.cs +++ b/DS4Windows/DS4Control/Mapping.cs @@ -2537,7 +2537,9 @@ namespace DS4Windows horizontalRemainder = 0.0; } - double mouseXTemp = rawMouseX - (Math.IEEERemainder(rawMouseX * 1000.0, 1.0) / 1000.0); + //double mouseXTemp = rawMouseX - (Math.IEEERemainder(rawMouseX * 1000.0, 1.0) / 1000.0); + double mouseXTemp = rawMouseX - (remainderCutoff(rawMouseX * 1000.0, 1.0) / 1000.0); + //double mouseXTemp = rawMouseX - (rawMouseX * 1000.0 - (1.0 * (int)(rawMouseX * 1000.0 / 1.0))); mouseX = (int)mouseXTemp; horizontalRemainder = mouseXTemp - mouseX; //mouseX = (int)rawMouseX; @@ -2552,13 +2554,19 @@ namespace DS4Windows verticalRemainder = 0.0; } - double mouseYTemp = rawMouseY - (Math.IEEERemainder(rawMouseY * 1000.0, 1.0) / 1000.0); + //double mouseYTemp = rawMouseY - (Math.IEEERemainder(rawMouseY * 1000.0, 1.0) / 1000.0); + double mouseYTemp = rawMouseY - (remainderCutoff(rawMouseY * 1000.0, 1.0) / 1000.0); mouseY = (int)mouseYTemp; verticalRemainder = mouseYTemp - mouseY; //mouseY = (int)rawMouseY; //verticalRemainder = rawMouseY - mouseY; } + private static double remainderCutoff(double dividend, double divisor) + { + return dividend - (divisor * (int)(dividend / divisor)); + } + public static bool compare(byte b1, byte b2) { bool result = true; diff --git a/DS4Windows/DS4Forms/DS4Form.cs b/DS4Windows/DS4Forms/DS4Form.cs index b0217c8..1a50114 100644 --- a/DS4Windows/DS4Forms/DS4Form.cs +++ b/DS4Windows/DS4Forms/DS4Form.cs @@ -1005,7 +1005,9 @@ namespace DS4Windows lbLastMessage.Text = string.Empty; } - bool inHotPlug = false; + private bool inHotPlug = false; + private int hotplugCounter = 0; + private object hotplugCounterLock = new object(); protected override void WndProc(ref Message m) { try @@ -1015,7 +1017,15 @@ namespace DS4Windows if (runHotPlug) { Int32 Type = m.WParam.ToInt32(); - InnerHotplug2(); + lock (hotplugCounterLock) + { + hotplugCounter++; + } + + if (!inHotPlug) + { + InnerHotplug2(); + } } } } @@ -1031,17 +1041,35 @@ namespace DS4Windows protected async void InnerHotplug2() { - await System.Threading.Tasks.Task.Delay(50); + //await System.Threading.Tasks.Task.Delay(50); - if (inHotPlug) + /*if (inHotPlug) { await System.Threading.Tasks.Task.Run(() => { while (inHotPlug) { System.Threading.Thread.Sleep(50); } }); } + */ - lock (this) + //lock (this) { inHotPlug = true; - Program.rootHub.HotPlug(); + System.Threading.SynchronizationContext uiContext = System.Threading.SynchronizationContext.Current; + int tempCount = 0; + lock (hotplugCounterLock) + { + tempCount = hotplugCounter; + } + + while (tempCount > 0) + { + await System.Threading.Tasks.Task.Run(() => { Program.rootHub.HotPlug(uiContext); }); + lock (hotplugCounterLock) + { + hotplugCounter--; + tempCount = hotplugCounter; + } + } + + //Program.rootHub.HotPlug(); inHotPlug = false; } } diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index 4dbd77d..f4ab65a 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -164,6 +164,7 @@ namespace DS4Windows public event EventHandler Removal = null; public event EventHandler SyncChange = null; public event EventHandler SerialChange = null; + public event EventHandler PublishRemoval = null; public HidDevice HidDevice => hDevice; public bool IsExclusive => HidDevice.IsExclusive; @@ -388,6 +389,10 @@ namespace DS4Windows { return uiContext; } + public void setUiContext(SynchronizationContext uiContext) + { + this.uiContext = uiContext; + } private Queue eventQueue = new Queue(); private object eventQueueLock = new object(); @@ -432,7 +437,6 @@ namespace DS4Windows touchpad = new DS4Touchpad(); sixAxis = new DS4SixAxis(); - uiContext = SynchronizationContext.Current; } private void timeoutTestThread() @@ -699,6 +703,9 @@ namespace DS4Windows Removal?.Invoke(this, EventArgs.Empty); }), null); + //System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); }); + //Removal?.Invoke(this, EventArgs.Empty); + timeoutExecuted = true; return; } @@ -730,6 +737,9 @@ namespace DS4Windows Removal?.Invoke(this, EventArgs.Empty); }), null); + //System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); }); + //Removal?.Invoke(this, EventArgs.Empty); + timeoutExecuted = true; return; } @@ -1089,6 +1099,8 @@ namespace DS4Windows { Removal?.Invoke(this, EventArgs.Empty); }), null); + + //System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); }); } } @@ -1120,6 +1132,9 @@ namespace DS4Windows { Removal?.Invoke(this, EventArgs.Empty); }), null); + + //System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); }); + //Removal?.Invoke(this, EventArgs.Empty); } else if (result && !remove) {