2021-12-29 20:33:20 +03:00
|
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
2024-10-24 22:04:32 +02:00
|
|
|
|
#include <common/utils/logger_helper.h>
|
2021-12-29 20:33:20 +03:00
|
|
|
|
#include <common/utils/ProcessWaiter.h>
|
|
|
|
|
|
#include <common/utils/window.h>
|
2022-04-08 05:19:42 -04:00
|
|
|
|
#include <common/utils/UnhandledExceptionHandler.h>
|
2022-10-26 14:02:31 +01:00
|
|
|
|
#include <common/utils/gpo.h>
|
2021-12-29 20:33:20 +03:00
|
|
|
|
|
2024-10-24 22:04:32 +02:00
|
|
|
|
#include <common/Telemetry/EtwTrace/EtwTrace.h>
|
2021-12-29 20:33:20 +03:00
|
|
|
|
|
|
|
|
|
|
#include <AlwaysOnTop.h>
|
|
|
|
|
|
#include <trace.h>
|
|
|
|
|
|
|
|
|
|
|
|
// Non-localizable
|
|
|
|
|
|
const std::wstring moduleName = L"AlwaysOnTop";
|
|
|
|
|
|
const std::wstring internalPath = L"";
|
|
|
|
|
|
const std::wstring instanceMutexName = L"Local\\PowerToys_AlwaysOnTop_InstanceMutex";
|
|
|
|
|
|
|
|
|
|
|
|
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ PWSTR lpCmdLine, _In_ int nCmdShow)
|
|
|
|
|
|
{
|
2024-10-24 22:04:32 +02:00
|
|
|
|
Shared::Trace::ETWTrace trace;
|
|
|
|
|
|
trace.UpdateState(true);
|
|
|
|
|
|
|
2021-12-29 20:33:20 +03:00
|
|
|
|
winrt::init_apartment();
|
|
|
|
|
|
LoggerHelpers::init_logger(moduleName, internalPath, LogSettings::alwaysOnTopLoggerName);
|
2022-10-26 14:02:31 +01:00
|
|
|
|
|
|
|
|
|
|
if (powertoys_gpo::getConfiguredAlwaysOnTopEnabledValue() == powertoys_gpo::gpo_rule_configured_disabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger::warn(L"Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator.");
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InitUnhandledExceptionHandler();
|
2021-12-29 20:33:20 +03:00
|
|
|
|
|
|
|
|
|
|
auto mutex = CreateMutex(nullptr, true, instanceMutexName.c_str());
|
|
|
|
|
|
if (mutex == nullptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger::error(L"Failed to create mutex. {}", get_last_error_or_default(GetLastError()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (GetLastError() == ERROR_ALREADY_EXISTS)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-24 22:04:32 +02:00
|
|
|
|
auto mainThreadId = GetCurrentThreadId();
|
|
|
|
|
|
|
2021-12-29 20:33:20 +03:00
|
|
|
|
std::wstring pid = std::wstring(lpCmdLine);
|
|
|
|
|
|
if (!pid.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
ProcessWaiter::OnProcessTerminate(pid, [mainThreadId](int err) {
|
|
|
|
|
|
if (err != ERROR_SUCCESS)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger::error(L"Failed to wait for parent process exit. {}", get_last_error_or_default(err));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger::trace(L"PowerToys runner exited.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Logger::trace(L"Exiting AlwaysOnTop");
|
|
|
|
|
|
PostThreadMessage(mainThreadId, WM_QUIT, 0, 0);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-24 22:04:32 +02:00
|
|
|
|
Trace::AlwaysOnTop::RegisterProvider();
|
2021-12-29 20:33:20 +03:00
|
|
|
|
|
2024-10-24 22:04:32 +02:00
|
|
|
|
AlwaysOnTop app(!pid.empty(), mainThreadId);
|
2021-12-29 20:33:20 +03:00
|
|
|
|
|
|
|
|
|
|
run_message_loop();
|
|
|
|
|
|
|
2024-10-24 22:04:32 +02:00
|
|
|
|
Trace::AlwaysOnTop::UnregisterProvider();
|
|
|
|
|
|
|
|
|
|
|
|
trace.Flush();
|
|
|
|
|
|
|
2021-12-29 20:33:20 +03:00
|
|
|
|
return 0;
|
|
|
|
|
|
}
|