diff --git a/DS4Windows/DS4Control/ControlService.cs b/DS4Windows/DS4Control/ControlService.cs index caafcfb..f97bbd1 100644 --- a/DS4Windows/DS4Control/ControlService.cs +++ b/DS4Windows/DS4Control/ControlService.cs @@ -421,6 +421,7 @@ namespace DS4Windows } } + private SynchronizationContext uiContext = null; public bool Start(object tempui, bool showlog = true) { startViGEm(); @@ -433,6 +434,7 @@ namespace DS4Windows LogDebug("Connection to ViGEm established"); DS4Devices.isExclusiveMode = getUseExclusiveMode(); + uiContext = tempui as SynchronizationContext; if (showlog) { LogDebug(Properties.Resources.SearchingController); @@ -466,7 +468,6 @@ namespace DS4Windows task.Start(); DS4Controllers[i] = device; - device.setUiContext(tempui as SynchronizationContext); device.Removal += this.On_DS4Removal; device.Removal += DS4Devices.On_Removal; device.SyncChange += this.On_SyncChange; @@ -653,7 +654,7 @@ namespace DS4Windows return true; } - public bool HotPlug(SynchronizationContext uiContext) + public bool HotPlug() { if (running) { @@ -690,7 +691,6 @@ namespace DS4Windows Task task = new Task(() => { Thread.Sleep(5); WarnExclusiveModeFailure(device); }); task.Start(); DS4Controllers[Index] = device; - device.setUiContext(uiContext); device.Removal += this.On_DS4Removal; device.Removal += DS4Devices.On_Removal; device.SyncChange += this.On_SyncChange; @@ -1142,7 +1142,7 @@ namespace DS4Windows string devError = tempStrings[ind] = device.error; if (!string.IsNullOrEmpty(devError)) { - device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) + uiContext?.Post(new SendOrPostCallback(delegate (object state) { LogDebug(devError); }), null); @@ -1154,7 +1154,7 @@ namespace DS4Windows if (!lag[ind] && device.Latency >= flashWhenLateAt) { lag[ind] = true; - device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) + uiContext?.Post(new SendOrPostCallback(delegate (object state) { LagFlashWarning(ind, true); }), null); @@ -1162,7 +1162,7 @@ namespace DS4Windows else if (lag[ind] && device.Latency < flashWhenLateAt) { lag[ind] = false; - device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) + uiContext?.Post(new SendOrPostCallback(delegate (object state) { LagFlashWarning(ind, false); }), null); @@ -1185,7 +1185,7 @@ namespace DS4Windows if (device.firstReport && device.IsAlive()) { device.firstReport = false; - device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) + uiContext?.Post(new SendOrPostCallback(delegate (object state) { OnDeviceStatusChanged(this, ind); }), null); @@ -1194,7 +1194,7 @@ namespace DS4Windows { byte tempBattery = currentBattery[ind] = cState.Battery; bool tempCharging = charging[ind] = device.isCharging(); - device.getUiContext()?.Post(new SendOrPostCallback(delegate (object state) + uiContext?.Post(new SendOrPostCallback(delegate (object state) { OnBatteryStatusChange(this, ind, tempBattery, tempCharging); }), null); diff --git a/DS4Windows/DS4Forms/DS4Form.cs b/DS4Windows/DS4Forms/DS4Form.cs index fd980fe..07b0dca 100644 --- a/DS4Windows/DS4Forms/DS4Form.cs +++ b/DS4Windows/DS4Forms/DS4Form.cs @@ -246,7 +246,7 @@ namespace DS4Windows */ //tabProfiles.Controls.Add(opt); - autoProfilesTimer.Elapsed += CheckAutoProfiles; + //autoProfilesTimer.Elapsed += CheckAutoProfiles; autoProfilesTimer.Interval = 1000; autoProfilesTimer.AutoReset = false; @@ -268,11 +268,12 @@ namespace DS4Windows Enable_Controls(3, false); btnStartStop.Text = Properties.Resources.StartText; - hotkeysTimer.Elapsed += Hotkeys; + //hotkeysTimer.Elapsed += Hotkeys; hotkeysTimer.AutoReset = false; if (SwipeProfiles) { - hotkeysTimer.Start(); + ChangeHotkeysStatus(true); + //hotkeysTimer.Start(); } startToolStripMenuItem.Text = btnStartStop.Text; @@ -434,6 +435,34 @@ namespace DS4Windows lbLastMessage.ForeColor = SystemColors.GrayText; } + private void ChangeAutoProfilesStatus(bool state) + { + if (state) + { + autoProfilesTimer.Elapsed += CheckAutoProfiles; + autoProfilesTimer.Start(); + } + else + { + autoProfilesTimer.Stop(); + autoProfilesTimer.Elapsed -= CheckAutoProfiles; + } + } + + private void ChangeHotkeysStatus(bool state) + { + if (state) + { + hotkeysTimer.Elapsed += Hotkeys; + hotkeysTimer.Start(); + } + else + { + hotkeysTimer.Stop(); + hotkeysTimer.Elapsed -= Hotkeys; + } + } + private void blankControllerTab() { for (int Index = 0, PadsLen = Pads.Length; @@ -535,30 +564,33 @@ namespace DS4Windows string slide = Program.rootHub.TouchpadSlide(i); if (slide == "left") { + int ind = i; this.BeginInvoke((System.Action)(() => { - if (cbs[i].SelectedIndex <= 0) - cbs[i].SelectedIndex = cbs[i].Items.Count - 2; + if (cbs[ind].SelectedIndex <= 0) + cbs[ind].SelectedIndex = cbs[ind].Items.Count - 2; else - cbs[i].SelectedIndex--; + cbs[ind].SelectedIndex--; })); } else if (slide == "right") { + int ind = i; this.BeginInvoke((System.Action)(() => { - if (cbs[i].SelectedIndex == cbs[i].Items.Count - 2) - cbs[i].SelectedIndex = 0; + if (cbs[ind].SelectedIndex == cbs[ind].Items.Count - 2) + cbs[ind].SelectedIndex = 0; else - cbs[i].SelectedIndex++; + cbs[ind].SelectedIndex++; })); } if (slide.Contains("t")) { + int ind = i; this.BeginInvoke((System.Action)(() => { - ShowNotification(this, Properties.Resources.UsingProfile.Replace("*number*", (i + 1).ToString()).Replace("*Profile name*", cbs[i].Text)); + ShowNotification(this, Properties.Resources.UsingProfile.Replace("*number*", (ind + 1).ToString()).Replace("*Profile name*", cbs[ind].Text)); })); } } @@ -599,8 +631,10 @@ namespace DS4Windows turnOffTemp = true; if (btnStartStop.Text == Properties.Resources.StopText) { - autoProfilesTimer.Stop(); - hotkeysTimer.Stop(); + //autoProfilesTimer.Stop(); + //hotkeysTimer.Stop(); + ChangeAutoProfilesStatus(false); + ChangeHotkeysStatus(false); this.Invoke((System.Action)(() => { this.changingService = true; @@ -614,8 +648,10 @@ namespace DS4Windows this.Invoke((System.Action)(() => { - hotkeysTimer.Start(); - autoProfilesTimer.Start(); + //hotkeysTimer.Start(); + ChangeHotkeysStatus(true); + ChangeAutoProfilesStatus(true); + //autoProfilesTimer.Start(); })); } } @@ -688,11 +724,13 @@ namespace DS4Windows bool timerEnabled = autoProfilesTimer.Enabled; if (pathCount > 0 && !timerEnabled) { - autoProfilesTimer.Start(); + ChangeAutoProfilesStatus(true); + //autoProfilesTimer.Start(); } else if (pathCount == 0 && timerEnabled) { - autoProfilesTimer.Stop(); + //autoProfilesTimer.Stop(); + ChangeAutoProfilesStatus(false); } } @@ -939,12 +977,14 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question { if (SwipeProfiles && !hotkeysTimer.Enabled) { - hotkeysTimer.Start(); + ChangeHotkeysStatus(true); + //hotkeysTimer.Start(); } if (programpaths.Count > 0 && !autoProfilesTimer.Enabled) { - autoProfilesTimer.Start(); + ChangeAutoProfilesStatus(true); + //autoProfilesTimer.Start(); } startToolStripMenuItem.Text = btnStartStop.Text = Properties.Resources.StopText; @@ -963,8 +1003,10 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question private void ServiceShutdownFinish() { - hotkeysTimer.Stop(); - autoProfilesTimer.Stop(); + ChangeAutoProfilesStatus(false); + ChangeHotkeysStatus(false); + //hotkeysTimer.Stop(); + //autoProfilesTimer.Stop(); startToolStripMenuItem.Text = btnStartStop.Text = Properties.Resources.StartText; blankControllerTab(); populateFullNotifyText(); @@ -1007,11 +1049,10 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question hotplugCounter++; } - var uiContext = SynchronizationContext.Current; if (!inHotPlug) { inHotPlug = true; - TaskRunner.Run(() => { Thread.Sleep(1500); InnerHotplug2(uiContext); }); + TaskRunner.Run(() => { Thread.Sleep(1500); InnerHotplug2(); }); } } } @@ -1025,7 +1066,7 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question base.WndProc(ref m); } - private void InnerHotplug2(SynchronizationContext uiContext) + private void InnerHotplug2() { inHotPlug = true; @@ -1037,7 +1078,7 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question while (loopHotplug == true) { - Program.rootHub.HotPlug(uiContext); + Program.rootHub.HotPlug(); //TaskRunner.Run(() => { Program.rootHub.HotPlug(uiContext); }); lock (hotplugCounterLock) { @@ -2152,11 +2193,13 @@ Properties.Resources.DS4Update, MessageBoxButtons.YesNo, MessageBoxIcon.Question bool timerEnabled = hotkeysTimer.Enabled; if (swipe && !timerEnabled) { - hotkeysTimer.Start(); + ChangeHotkeysStatus(true); + //hotkeysTimer.Start(); } else if (!swipe && timerEnabled) { - hotkeysTimer.Stop(); + ChangeHotkeysStatus(false); + //hotkeysTimer.Stop(); } } diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index db239f7..4a58fc8 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -392,16 +392,6 @@ namespace DS4Windows return result; } - private SynchronizationContext uiContext = null; - public SynchronizationContext getUiContext() - { - return uiContext; - } - public void setUiContext(SynchronizationContext uiContext) - { - this.uiContext = uiContext; - } - private Queue eventQueue = new Queue(); private object eventQueueLock = new object(); @@ -446,7 +436,7 @@ namespace DS4Windows warnInterval = WARN_INTERVAL_BT; audio = new DS4Audio(); micAudio = new DS4Audio(DS4Library.CoreAudio.DataFlow.Capture); - synced = isValidSerial(); + runCalib = synced = isValidSerial(); } } else @@ -808,10 +798,7 @@ namespace DS4Windows sendOutputReport(true, true); // Kick Windows into noticing the disconnection. StopOutputUpdate(); isDisconnecting = true; - uiContext.Send(new SendOrPostCallback(delegate (object state4) - { - Removal?.Invoke(this, EventArgs.Empty); - }), null); + Removal?.Invoke(this, EventArgs.Empty); timeoutExecuted = true; return; @@ -838,10 +825,7 @@ namespace DS4Windows StopOutputUpdate(); isDisconnecting = true; - uiContext.Send(new SendOrPostCallback(delegate (object state4) - { - Removal?.Invoke(this, EventArgs.Empty); - }), null); + Removal?.Invoke(this, EventArgs.Empty); timeoutExecuted = true; return; @@ -1016,7 +1000,7 @@ namespace DS4Windows bool controllerSynced = inputReport[31] == 0; if (controllerSynced != synced) { - synced = controllerSynced; + runCalib = synced = controllerSynced; SyncChange?.Invoke(this, EventArgs.Empty); sendOutputReport(true, true); } @@ -1292,10 +1276,7 @@ namespace DS4Windows if (callRemoval) { - uiContext.Send(new SendOrPostCallback(delegate (object state) - { - Removal?.Invoke(this, EventArgs.Empty); - }), null); + Removal?.Invoke(this, EventArgs.Empty); //System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); }); } @@ -1327,10 +1308,7 @@ namespace DS4Windows { isDisconnecting = true; - uiContext.Send(new SendOrPostCallback(delegate (object state4) - { - Removal?.Invoke(this, EventArgs.Empty); - }), null); + Removal?.Invoke(this, EventArgs.Empty); //System.Threading.Tasks.Task.Factory.StartNew(() => { Removal?.Invoke(this, EventArgs.Empty); }); //Removal?.Invoke(this, EventArgs.Empty); diff --git a/DS4Windows/Properties/AssemblyInfo.cs b/DS4Windows/Properties/AssemblyInfo.cs index c360ca1..afdc6ab 100644 --- a/DS4Windows/Properties/AssemblyInfo.cs +++ b/DS4Windows/Properties/AssemblyInfo.cs @@ -33,7 +33,7 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.5.6")] -[assembly: AssemblyFileVersion("1.5.6")] +[assembly: AssemblyVersion("1.5.7")] +[assembly: AssemblyFileVersion("1.5.7")] [assembly: NeutralResourcesLanguage("en")] diff --git a/README.md b/README.md index d69283d..e0ca3cc 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ website at [ds4windows.com](http://ds4windows.com). - **[Main builds of DS4Windows](https://github.com/Ryochan7/DS4Windows/releases)** - Alternative builds: - - [UdpServer ViGEm build](http://ryochan7.xyz/ds4windows/test/DS4Windows_1.5.5_ViGEm_UdpServer_x64.zip) + - [UdpServer ViGEm build](http://ryochan7.xyz/ds4windows/test/DS4Windows_1.5.6_ViGEm_UdpServer_x64.zip) ## Requirements diff --git a/TODO.md b/TODO.md index eb8eec2..3876e29 100644 --- a/TODO.md +++ b/TODO.md @@ -1,8 +1,9 @@ # TODO * ~~Perform some final cleanup and release version 1.5~~ -* Attempt to remove reliance on the main thread when disconnecting a device. -Currently used to delay hotplug routine +* ~~Attempt to remove reliance on the main thread when disconnecting a device. +Currently used to delay hotplug routine~~ +* Look into newer version of HidGuardian * Look into distributing profile properties around various objects rather than using a lot of getters to obtain properties each poll. It will complicate the architecture a little bit but hopefully