From 4c4a6f5ba668ed5ee771c5dc0390da42ebe72ec3 Mon Sep 17 00:00:00 2001 From: Travis Nickles Date: Fri, 27 Dec 2019 14:25:17 -0600 Subject: [PATCH] Added Stop and Start menu item to tray icon. Fixed ambiguity of temp thread objects in ControlService. --- DS4Windows/DS4Control/ControlService.cs | 26 +++++----------- DS4Windows/DS4Forms/MainWindow.xaml.cs | 6 ++++ .../DS4Forms/ViewModels/TrayIconViewModel.cs | 31 ++++++++++++++++++- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/DS4Windows/DS4Control/ControlService.cs b/DS4Windows/DS4Control/ControlService.cs index d38f706..934bb6c 100644 --- a/DS4Windows/DS4Control/ControlService.cs +++ b/DS4Windows/DS4Control/ControlService.cs @@ -1,17 +1,11 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; - -using System.Media; using System.Threading.Tasks; -using static DS4Windows.Global; using System.Threading; using System.Diagnostics; +using static DS4Windows.Global; using Nefarius.ViGEm.Client; -using Nefarius.ViGEm.Client.Targets; -using Nefarius.ViGEm.Client.Targets.Xbox360; -using Nefarius.ViGEm.Client.Targets.DualShock4; namespace DS4Windows { @@ -40,6 +34,7 @@ namespace DS4Windows }; */ Thread tempThread; + Thread tempBusThread; public List affectedDevs = new List() { @"HID\VID_054C&PID_05C4", @@ -154,7 +149,7 @@ namespace DS4Windows //sp.Stream = DS4WinWPF.Properties.Resources.EE; // Cause thread affinity to not be tied to main GUI thread - tempThread = new Thread(() => { + tempBusThread = new Thread(() => { //_udpServer = new UdpServer(GetPadDetailForIdx); busThrRunning = true; @@ -174,9 +169,9 @@ namespace DS4Windows Monitor.Wait(busThrLck); } }); - tempThread.Priority = ThreadPriority.Normal; - tempThread.IsBackground = true; - tempThread.Start(); + tempBusThread.Priority = ThreadPriority.Normal; + tempBusThread.IsBackground = true; + tempBusThread.Start(); //while (_udpServer == null) //{ // Thread.SpinWait(500); @@ -353,17 +348,12 @@ namespace DS4Windows { Thread.SpinWait(500); } + + tempThread = null; } private void stopViGEm() { - if (tempThread != null) - { - tempThread.Interrupt(); - tempThread.Join(); - tempThread = null; - } - if (vigemTestClient != null) { vigemTestClient.Dispose(); diff --git a/DS4Windows/DS4Forms/MainWindow.xaml.cs b/DS4Windows/DS4Forms/MainWindow.xaml.cs index 292e8cd..619c208 100644 --- a/DS4Windows/DS4Forms/MainWindow.xaml.cs +++ b/DS4Windows/DS4Forms/MainWindow.xaml.cs @@ -252,6 +252,7 @@ Properties.Resources.DS4Update, MessageBoxButton.YesNo, MessageBoxImage.Question trayIconVM.ProfileSelected += TrayIconVM_ProfileSelected; trayIconVM.RequestMinimize += TrayIconVM_RequestMinimize; trayIconVM.RequestOpen += TrayIconVM_RequestOpen; + trayIconVM.RequestServiceChange += TrayIconVM_RequestServiceChange; autoProfControl.AutoDebugChanged += AutoProfControl_AutoDebugChanged; autoprofileChecker.RequestServiceChange += AutoprofileChecker_RequestServiceChange; autoProfileHolder.AutoProfileColl.CollectionChanged += AutoProfileColl_CollectionChanged; @@ -287,6 +288,11 @@ Suspend support not enabled.", true); } } + private void TrayIconVM_RequestServiceChange(object sender, EventArgs e) + { + ChangeService(); + } + private void LogItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { diff --git a/DS4Windows/DS4Forms/ViewModels/TrayIconViewModel.cs b/DS4Windows/DS4Forms/ViewModels/TrayIconViewModel.cs index d67fae5..1046dcf 100644 --- a/DS4Windows/DS4Forms/ViewModels/TrayIconViewModel.cs +++ b/DS4Windows/DS4Forms/ViewModels/TrayIconViewModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Threading; +using System.Windows; using System.Windows.Controls; using DS4Windows; @@ -16,6 +17,7 @@ namespace DS4WinWPF.DS4Forms.ViewModels public const string ballonTitle = "DS4Windows"; public static string trayTitle = $"DS4Windows v{Global.exeversion}"; private ContextMenu contextMenu; + private MenuItem changeServiceItem; public string TooltipText { get => tooltipText; set @@ -44,6 +46,7 @@ namespace DS4WinWPF.DS4Forms.ViewModels public event EventHandler RequestShutdown; public event EventHandler RequestOpen; public event EventHandler RequestMinimize; + public event EventHandler RequestServiceChange; private ReaderWriterLockSlim _colLocker = new ReaderWriterLockSlim(); private List controllerList = new List(); @@ -61,6 +64,10 @@ namespace DS4WinWPF.DS4Forms.ViewModels this.controlService = service; contextMenu = new ContextMenu(); iconSource = Global.UseWhiteIcon ? ICON_WHITE : ICON_COLOR; + changeServiceItem = new MenuItem() { Header = "Start", + FontWeight = FontWeights.Bold }; + changeServiceItem.Click += ChangeControlServiceItem_Click; + changeServiceItem.IsEnabled = false; PopulateControllerList(); PopulateToolText(); @@ -74,6 +81,7 @@ namespace DS4WinWPF.DS4Forms.ViewModels service.PreServiceStop += ClearToolText; service.PreServiceStop += UnhookEvents; service.PreServiceStop += ClearControllerList; + service.RunningChanged += Service_RunningChanged; service.HotplugController += Service_HotplugController; /*tester.StartControllers += HookBatteryUpdate; tester.StartControllers += StartPopulateText; @@ -83,6 +91,16 @@ namespace DS4WinWPF.DS4Forms.ViewModels */ } + private void Service_RunningChanged(object sender, EventArgs e) + { + string temp = controlService.running ? "Stop" : "Start"; + App.Current.Dispatcher.BeginInvoke((Action)(() => + { + changeServiceItem.Header = temp; + changeServiceItem.IsEnabled = true; + })); + } + private void ClearControllerList(object sender, EventArgs e) { _colLocker.EnterWriteLock(); @@ -173,6 +191,8 @@ namespace DS4WinWPF.DS4Forms.ViewModels items.Add(item); items.Add(new Separator()); + item = changeServiceItem; + items.Add(item); item = new MenuItem() { Header = "Open" }; item.Click += OpenMenuItem_Click; items.Add(item); @@ -188,6 +208,12 @@ namespace DS4WinWPF.DS4Forms.ViewModels items.Add(item); } + private void ChangeControlServiceItem_Click(object sender, System.Windows.RoutedEventArgs e) + { + changeServiceItem.IsEnabled = false; + RequestServiceChange?.Invoke(this, EventArgs.Empty); + } + private void OpenProgramFolderItem_Click(object sender, System.Windows.RoutedEventArgs e) { Process.Start(Global.exedirpath); @@ -348,7 +374,10 @@ namespace DS4WinWPF.DS4Forms.ViewModels { contextMenu.Items.Clear(); ItemCollection items = contextMenu.Items; - MenuItem item = new MenuItem() { Header = "Open" }; + MenuItem item; + item = changeServiceItem; + items.Add(item); + item = new MenuItem() { Header = "Open" }; item.Click += OpenMenuItem_Click; items.Add(item); item = new MenuItem() { Header = "Minimize" };