-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
319 lines (300 loc) · 10.1 KB
/
Copy pathmain.cpp
File metadata and controls
319 lines (300 loc) · 10.1 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <filesystem>
#include <cstdlib>
#include <optional>
#include <thread>
#include <iostream>
#include <cxxopts.hpp>
#include <spdlog/spdlog.h>
#include "app/config.h"
#include "audio/engine.h"
#include "hook/keyboard_hook.h"
#include "platform/tray.hpp"
#include "platform/window.hpp"
#include "util/log.h"
#ifdef _WIN32
#include <windows.h>
#include <shellapi.h>
#endif
#ifndef LIZARD_TEST
#include "glad/glad.h"
#include "overlay/gl_raii.cpp"
#include "overlay/overlay.cpp"
#endif
int main(int argc, char **argv) {
cxxopts::Options opts("lizard-hook", "Keyboard reactive overlay");
opts.add_options()("config", "Config path", cxxopts::value<std::string>())(
"log-level", "Logging level",
cxxopts::value<std::string>())("log-queue", "Logging queue size", cxxopts::value<int>())(
"log-workers", "Logging worker count", cxxopts::value<int>())("help", "Show help");
auto result = opts.parse(argc, argv);
if (result.count("help")) {
std::cout << opts.help() << "\n";
return 0;
}
std::optional<std::filesystem::path> config_path;
if (result.count("config")) {
config_path = result["config"].as<std::string>();
}
auto exe_dir = std::filesystem::canonical(argv[0]).parent_path();
lizard::app::Config cfg(exe_dir, config_path);
auto level =
result.count("log-level") ? result["log-level"].as<std::string>() : cfg.logging_level();
auto queue = result.count("log-queue") ? static_cast<std::size_t>(result["log-queue"].as<int>())
: static_cast<std::size_t>(cfg.logging_queue_size());
auto workers = result.count("log-workers")
? static_cast<std::size_t>(result["log-workers"].as<int>())
: static_cast<std::size_t>(cfg.logging_worker_count());
lizard::util::init_logging(level, queue, workers, cfg.logging_path());
lizard::audio::Engine engine(static_cast<std::uint32_t>(cfg.max_concurrent_playbacks()));
engine.init(cfg.sound_path(), cfg.volume_percent(), cfg.audio_backend(),
static_cast<std::uint32_t>(cfg.max_concurrent_playbacks()));
lizard::overlay::Overlay overlay;
overlay.init(cfg, cfg.emoji_atlas());
std::jthread overlay_thread([&](std::stop_token st) { overlay.run(st); });
std::atomic<bool> fullscreen{false};
std::atomic<bool> enabled{cfg.enabled()};
std::atomic<bool> muted{cfg.mute()};
std::atomic<bool> fullscreen_pause{cfg.fullscreen_pause()};
lizard::platform::TrayState tray_state{enabled.load(), muted.load(), fullscreen_pause.load(),
lizard::platform::FpsMode::Auto, 60};
#ifdef _WIN32
constexpr int KEY_CTRL_L = 0xA2;
constexpr int KEY_CTRL_R = 0xA3;
constexpr int KEY_SHIFT_L = 0xA0;
constexpr int KEY_SHIFT_R = 0xA1;
constexpr int KEY_F9 = 0x78;
constexpr int KEY_F10 = 0x79;
constexpr int KEY_F11 = 0x7A;
#elif defined(__APPLE__)
constexpr int KEY_CTRL_L = 59;
constexpr int KEY_CTRL_R = 62;
constexpr int KEY_SHIFT_L = 56;
constexpr int KEY_SHIFT_R = 60;
constexpr int KEY_F9 = 101;
constexpr int KEY_F10 = 109;
constexpr int KEY_F11 = 103;
#else
constexpr int KEY_CTRL_L = 37;
constexpr int KEY_CTRL_R = 105;
constexpr int KEY_SHIFT_L = 50;
constexpr int KEY_SHIFT_R = 62;
constexpr int KEY_F9 = 75;
constexpr int KEY_F10 = 76;
constexpr int KEY_F11 = 95;
#endif
auto update_state = [&] {
bool fs = fullscreen.load();
bool paused = (!enabled.load()) || (fullscreen_pause.load() && fs);
overlay.set_paused(paused);
if (paused || muted.load()) {
engine.set_volume(0.0f);
} else {
engine.set_volume(static_cast<float>(cfg.volume_percent()) / 100.0f);
}
};
update_state();
std::jthread fullscreen_thread([&](std::stop_token st) {
using namespace std::chrono_literals;
while (!st.stop_requested()) {
fullscreen = lizard::platform::fullscreen_window_present();
update_state();
std::this_thread::sleep_for(500ms);
}
});
std::atomic<bool> running{true};
lizard::platform::TrayCallbacks tray_callbacks{
[&](bool v) {
enabled = v;
tray_state.enabled = v;
update_state();
lizard::platform::update_tray(tray_state);
},
[&](bool v) {
muted = v;
tray_state.muted = v;
update_state();
lizard::platform::update_tray(tray_state);
},
[&](bool v) {
fullscreen_pause = v;
tray_state.fullscreen_pause = v;
update_state();
lizard::platform::update_tray(tray_state);
},
[&](lizard::platform::FpsMode m) {
tray_state.fps_mode = m;
overlay.set_fps_mode(m);
lizard::platform::update_tray(tray_state);
},
[&](int v) {
tray_state.fps_mode = lizard::platform::FpsMode::Fixed;
tray_state.fps_fixed = v;
overlay.set_fps_mode(lizard::platform::FpsMode::Fixed);
overlay.set_fps_fixed(v);
lizard::platform::update_tray(tray_state);
},
[&]() {
auto path = cfg.user_config_path();
if (!std::filesystem::exists(path)) {
path = cfg.logging_path().parent_path() / "lizard.json";
}
#ifdef _WIN32
std::wstring w = path.wstring();
ShellExecuteW(nullptr, L"open", w.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
#else
#ifdef __APPLE__
std::string cmd = "open \"" + path.string() + "\"";
#else
std::string cmd = "xdg-open \"" + path.string() + "\"";
#endif
int rc = std::system(cmd.c_str());
(void)rc;
#endif
},
[&]() {
auto path = cfg.logging_path();
#ifdef _WIN32
std::wstring w = path.wstring();
ShellExecuteW(nullptr, L"open", w.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
#else
#ifdef __APPLE__
std::string cmd = "open \"" + path.string() + "\"";
#else
std::string cmd = "xdg-open \"" + path.string() + "\"";
#endif
int rc2 = std::system(cmd.c_str());
(void)rc2;
#endif
},
[&]() { running = false; }};
lizard::platform::init_tray(tray_state, tray_callbacks);
bool ctrl_down = false;
bool shift_down = false;
bool f9_down = false;
bool f10_down = false;
bool f11_down = false;
auto hook = hook::KeyboardHook::create(
[&](int key, bool pressed) {
if (key == KEY_CTRL_L || key == KEY_CTRL_R) {
ctrl_down = pressed;
} else if (key == KEY_SHIFT_L || key == KEY_SHIFT_R) {
shift_down = pressed;
} else if (key == KEY_F9) {
if (!pressed) {
f9_down = false;
} else if (ctrl_down && shift_down) {
if (!f9_down) {
f9_down = true;
enabled = !enabled.load();
tray_state.enabled = enabled.load();
update_state();
lizard::platform::update_tray(tray_state);
}
f9_down = true;
return;
} else {
f9_down = true;
}
} else if (key == KEY_F10) {
if (!pressed) {
f10_down = false;
} else if (ctrl_down && shift_down) {
if (!f10_down) {
f10_down = true;
muted = !muted.load();
tray_state.muted = muted.load();
update_state();
lizard::platform::update_tray(tray_state);
}
f10_down = true;
return;
} else {
f10_down = true;
}
} else if (key == KEY_F11) {
if (!pressed) {
f11_down = false;
} else if (ctrl_down && shift_down) {
if (!f11_down) {
f11_down = true;
cfg.reload();
cfg.reload_cv().notify_all();
}
f11_down = true;
return;
} else {
f11_down = true;
}
}
if (pressed && enabled.load()) {
bool paused = fullscreen_pause.load() && fullscreen.load();
if (!paused) {
if (!muted.load()) {
engine.play();
}
float bx = 0.0f;
float by = 0.0f;
if (cfg.badge_spawn_strategy() == "cursor_follow") {
auto [cx, cy] = lizard::platform::cursor_pos();
bx = cx;
by = cy;
}
overlay.enqueue_spawn(bx, by);
}
}
},
cfg);
hook->start();
std::jthread reload_thread([&](std::stop_token st) {
std::mutex m;
std::unique_lock lk(m);
while (!st.stop_requested()) {
cfg.reload_cv().wait(lk);
if (st.stop_requested()) {
break;
}
engine.shutdown();
engine.init(cfg.sound_path(), cfg.volume_percent(), cfg.audio_backend());
overlay.refresh_from_config(cfg);
bool prev_enabled = tray_state.enabled;
bool prev_muted = tray_state.muted;
bool prev_fullscreen_pause = tray_state.fullscreen_pause;
auto prev_mode = tray_state.fps_mode;
int prev_fixed = tray_state.fps_fixed;
tray_state.enabled = cfg.enabled();
tray_state.muted = cfg.mute();
tray_state.fullscreen_pause = cfg.fullscreen_pause();
auto new_mode = cfg.fps_mode() == "fixed" ? lizard::platform::FpsMode::Fixed
: lizard::platform::FpsMode::Auto;
int new_fixed = cfg.fps_fixed();
tray_state.fps_mode = new_mode;
tray_state.fps_fixed = new_fixed;
enabled = tray_state.enabled;
muted = tray_state.muted;
fullscreen_pause = tray_state.fullscreen_pause;
bool tray_changed = tray_state.enabled != prev_enabled || tray_state.muted != prev_muted ||
tray_state.fullscreen_pause != prev_fullscreen_pause ||
prev_mode != new_mode || prev_fixed != new_fixed;
if (tray_changed) {
lizard::platform::update_tray(tray_state);
}
update_state();
}
});
using namespace std::chrono_literals;
while (running) {
std::this_thread::sleep_for(100ms);
}
reload_thread.request_stop();
fullscreen_thread.request_stop();
overlay_thread.request_stop();
hook->stop();
overlay_thread.join();
overlay.shutdown();
engine.shutdown();
lizard::platform::shutdown_tray();
return 0;
}