Added Stop and Start menu item to tray icon. Fixed ambiguity of temp thread objects in ControlService.

This commit is contained in:
Travis Nickles 2019-12-27 14:25:17 -06:00
parent 5fb140c91b
commit 4c4a6f5ba6
3 changed files with 44 additions and 19 deletions

View File

@ -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<string> affectedDevs = new List<string>()
{
@"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();

View File

@ -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)
{

View File

@ -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<ControllerHolder> controllerList = new List<ControllerHolder>();
@ -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" };