41 lines
1.4 KiB
C++
41 lines
1.4 KiB
C++
#ifndef RUNNER_DESKTOP_INSTANCE_H_
|
|
#define RUNNER_DESKTOP_INSTANCE_H_
|
|
#include <windows.h>
|
|
|
|
// Shared with the Avalonia entry point, across installed/portable copies in
|
|
// the same interactive Windows session.
|
|
class DesktopInstance {
|
|
public:
|
|
DesktopInstance() {
|
|
event_ = CreateEventW(nullptr, FALSE, FALSE, L"Local\\TTSTD.ADS.Desktop.Activate");
|
|
mutex_ = CreateMutexW(nullptr, TRUE, L"Local\\TTSTD.ADS.Desktop.Instance");
|
|
primary_ = mutex_ && GetLastError() != ERROR_ALREADY_EXISTS;
|
|
}
|
|
~DesktopInstance() {
|
|
if (wait_) UnregisterWaitEx(wait_, INVALID_HANDLE_VALUE);
|
|
if (primary_) ReleaseMutex(mutex_);
|
|
if (mutex_) CloseHandle(mutex_);
|
|
if (event_) CloseHandle(event_);
|
|
}
|
|
bool valid() const { return event_ && mutex_; }
|
|
bool primary() const { return primary_; }
|
|
void Activate() {
|
|
AllowSetForegroundWindow(ASFW_ANY);
|
|
SetEvent(event_);
|
|
}
|
|
bool Listen(HWND window) {
|
|
return RegisterWaitForSingleObject(&wait_, event_, Wake,
|
|
reinterpret_cast<void*>(window), INFINITE, WT_EXECUTEDEFAULT) != FALSE;
|
|
}
|
|
static UINT ActivationMessage() {
|
|
return RegisterWindowMessageW(L"TTSTD.ADS.Desktop.Activate");
|
|
}
|
|
private:
|
|
static void CALLBACK Wake(void* context, BOOLEAN) {
|
|
PostMessageW(reinterpret_cast<HWND>(context), ActivationMessage(), 0, 0);
|
|
}
|
|
HANDLE event_ = nullptr, mutex_ = nullptr, wait_ = nullptr;
|
|
bool primary_ = false;
|
|
};
|
|
#endif
|