-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
130 lines (107 loc) · 3.91 KB
/
main.cpp
File metadata and controls
130 lines (107 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "pch.hpp"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <wchar.h>
#include "utils/utils.hpp"
#include "tray/tray.hpp"
#include "keyboardManager.hpp"
#include "mouseManager.hpp"
#include "settings/config.hpp"
#include "resource.h"
#include "tinylog.hpp"
#include <atomic>
#include <chrono>
#include <thread>
#include <mutex>
#include <ShlObj.h> // Icon Definitions
#pragma comment(lib, "shell32.lib")
#define VERSION "0.1.3"
constexpr int NOT_ADMIN = 1;
constexpr int ALREADY_RUNNING = 2;
constexpr int CONFIG_ERROR = 3;
constexpr int MUTEX_ERROR = 4;
struct AppState {
Config cfg;
std::atomic_bool running{true};
DWORD mainTid = 0;
void PostQuitToMain() const noexcept {
if (mainTid)
PostThreadMessageW(mainTid, WM_QUIT, 0, 0);
}
};
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) {
if (!utils::EnsureRunAsAdminAndExitIfNot())
return NOT_ADMIN;
HANDLE mutex = CreateMutexW(nullptr, FALSE, L"hyprwin.throwtop.dev");
if (!mutex)
return MUTEX_ERROR;
if (GetLastError() == ERROR_ALREADY_EXISTS) {
MessageBoxW(nullptr, L"Another instance is already running.", L"Error", MB_OK | MB_ICONERROR);
CloseHandle(mutex);
return ALREADY_RUNNING;
}
SetCurrentProcessExplicitAppUserModelID(L"hyprwin.throwtop.dev");
tinylog::init({.console = true,
.file_path = "hyprwin.log",
.console_level = tinylog::Level::Debug,
.file_level = tinylog::Level::Trace,
.date_format = L"MM'-'dd",
.time_format = L"HH':'mm':'ss"});
SET_THREAD_NAME("Main+Tray");
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
AppState state{};
state.mainTid = GetCurrentThreadId();
if (!state.cfg.LoadConfig()) {
MessageBoxW(nullptr, L"Config Fuarked SUPER is required", L"Error", MB_OK | MB_ICONERROR);
CloseHandle(mutex);
return CONFIG_ERROR;
}
utils::DisableProcessThrottling();
mm::MouseManager mm(hInstance, &state.cfg);
km::KeyboardManager km(&state.cfg);
km.SetSuperPressedCallback([&]() { mm.InstallHook(); });
km.SetSuperReleasedCallback([&]() { mm.UninstallHook(); });
// Tray on main thread
try {
Tray::Icon HW_ICON(IDI_HWICON);
Tray::Tray sys_tray(L"HyprWin " VERSION, HW_ICON);
sys_tray.setTooltip(L"HyprWin");
sys_tray.DarkMode(Tray::dark::AppModeForceDark);
sys_tray.onLeftClick([&] { return true; });
sys_tray.onDoubleClick([&] { return false; });
auto reloadBtn = sys_tray.addEntry(Tray::Button(L"Reload Config", [&] {
Config newCfg;
if (!newCfg.LoadConfig()) {
MessageBoxW(nullptr, L"Config Fuarked", L"Error", MB_OK | MB_ICONERROR);
return;
}
state.cfg = std::move(newCfg);
}));
reloadBtn->setGlyphIcon(Tray::Icon(IDI_HWICON));
reloadBtn->setDefault(true);
sys_tray
.addEntry(Tray::Button(L"Open Config Folder",
[&] {
wchar_t path[MAX_PATH]{};
GetModuleFileNameW(nullptr, path, MAX_PATH);
if (wchar_t* last = wcsrchr(path, L'\\'))
*last = L'\0';
ShellExecuteW(nullptr, L"open", path, nullptr, nullptr, SW_SHOWNORMAL);
}))
->setGlyphIcon(Tray::Icon::FromStock(SIID_FOLDEROPEN));
sys_tray.addEntry(Tray::Separator());
sys_tray
.addEntry(Tray::Button(L"Exit",
[&] {
state.running.store(false, std::memory_order_relaxed);
sys_tray.exit();
}))
->setGlyphIcon(Tray::Icon(LoadIconW(nullptr, IDI_HAND)));
sys_tray.run();
} catch (std::runtime_error& e) {
MessageBoxA(nullptr, e.what(), "Error", MB_OK | MB_ICONERROR);
}
if (mutex)
CloseHandle(mutex);
return 0;
}