2022-08-26 18:01:50 +02:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using interop;
|
|
|
|
|
|
using ManagedCommon;
|
|
|
|
|
|
|
2022-12-09 21:40:00 +01:00
|
|
|
|
namespace PowerAccent.UI;
|
2022-08-26 18:01:50 +02:00
|
|
|
|
|
|
|
|
|
|
internal static class Program
|
|
|
|
|
|
{
|
2022-12-09 21:40:00 +01:00
|
|
|
|
private static readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
|
2022-08-26 18:01:50 +02:00
|
|
|
|
private static App _application;
|
|
|
|
|
|
private static int _powerToysRunnerPid;
|
|
|
|
|
|
|
|
|
|
|
|
[STAThread]
|
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
|
{
|
2023-03-21 10:27:29 +01:00
|
|
|
|
Logger.InitializeLogger("\\QuickAccent\\Logs");
|
|
|
|
|
|
|
2022-10-26 14:02:31 +01:00
|
|
|
|
if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredQuickAccentEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator.");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-09 21:40:00 +01:00
|
|
|
|
Arguments(args);
|
2022-08-26 18:01:50 +02:00
|
|
|
|
|
2022-12-09 21:40:00 +01:00
|
|
|
|
InitEvents();
|
2022-08-26 18:01:50 +02:00
|
|
|
|
|
2022-12-09 21:40:00 +01:00
|
|
|
|
_application = new App();
|
|
|
|
|
|
_application.InitializeComponent();
|
|
|
|
|
|
_application.Run();
|
2022-08-26 18:01:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void InitEvents()
|
|
|
|
|
|
{
|
|
|
|
|
|
Task.Run(
|
|
|
|
|
|
() =>
|
|
|
|
|
|
{
|
2022-12-09 21:40:00 +01:00
|
|
|
|
EventWaitHandle eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.PowerAccentExitEvent());
|
2022-08-26 18:01:50 +02:00
|
|
|
|
if (eventHandle.WaitOne())
|
|
|
|
|
|
{
|
|
|
|
|
|
Terminate();
|
|
|
|
|
|
}
|
2022-12-18 14:27:14 +01:00
|
|
|
|
},
|
|
|
|
|
|
_tokenSource.Token);
|
2022-08-26 18:01:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void Arguments(string[] args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args?.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-12-09 21:40:00 +01:00
|
|
|
|
if (int.TryParse(args[0], out _powerToysRunnerPid))
|
2022-08-26 18:01:50 +02:00
|
|
|
|
{
|
2022-12-09 21:40:00 +01:00
|
|
|
|
Logger.LogInfo($"QuickAccent started from the PowerToys Runner. Runner pid={_powerToysRunnerPid}");
|
|
|
|
|
|
|
|
|
|
|
|
RunnerHelper.WaitForPowerToysRunner(_powerToysRunnerPid, () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.LogInfo("PowerToys Runner exited. Exiting QuickAccent");
|
|
|
|
|
|
Terminate();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2022-08-26 18:01:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2022-08-31 19:05:54 +01:00
|
|
|
|
Logger.LogInfo($"QuickAccent started detached from PowerToys Runner.");
|
2022-08-26 18:01:50 +02:00
|
|
|
|
_powerToysRunnerPid = -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void Terminate()
|
|
|
|
|
|
{
|
|
|
|
|
|
Application.Current.Dispatcher.BeginInvoke(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
_tokenSource.Cancel();
|
|
|
|
|
|
Application.Current.Shutdown();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|