Repository navigation
Expand file tree
/
Copy pathMovementStateManager.cpp
More file actions
361 lines (310 loc) · 10.3 KB
/
Copy pathMovementStateManager.cpp
File metadata and controls
361 lines (310 loc) · 10.3 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// MovementStateManager.cpp
#include "MovementStateManager.h"
#include "Config.h"
#include "Globals.h"
#include "Application.h"
#include "KeybindManager.h"
#include "SpamLogic.h"
#include "Utils.h"
#include <mutex>
#include <windows.h>
namespace {
inline bool IsWasdVk(int vk) {
return (vk == 'W' || vk == 'A' || vk == 'S' || vk == 'D');
}
inline bool IsAxisXKey(int vk) { return (vk == 'A' || vk == 'D'); }
inline bool IsAxisYKey(int vk) { return (vk == 'W' || vk == 'S'); }
// Per-axis SOCD / SnapTap state (last-pressed wins, revert LIFO).
// X axis: A <-> D
// Y axis: W <-> S
struct AxisState {
int activeKey = 0;
int heldCount = 0;
int physicallyHeld[2] = {0, 0}; // press order (LIFO last)
};
// Mutex protecting AxisState and virtual axis state from concurrent access
// between the hook thread (HandleMovementKeyState) and the GUI thread
// (OnSnapTapToggled, RefreshMovementState).
std::mutex g_axisMutex;
AxisState g_axisX;
AxisState g_axisY;
// Virtual keys currently held down by SnapTap (only when SnapTap enabled and
// spam inactive).
int g_virtualAxisX = 0;
int g_virtualAxisY = 0;
AxisState *AxisForVk(int vk) {
if (IsAxisXKey(vk))
return &g_axisX;
if (IsAxisYKey(vk))
return &g_axisY;
return nullptr;
}
inline bool AxisContains(const AxisState &axis, int vk) {
return (axis.heldCount > 0 && axis.physicallyHeld[0] == vk) ||
(axis.heldCount > 1 && axis.physicallyHeld[1] == vk);
}
// Returns true only for a real up->down transition (ignores autorepeat).
bool AxisOnKeyDown(AxisState &axis, int vk) {
if (!AxisContains(axis, vk)) {
if (axis.heldCount < 2) {
axis.physicallyHeld[axis.heldCount++] = vk;
axis.activeKey = vk;
return true;
}
}
return false;
}
bool AxisOnKeyUp(AxisState &axis, int vk) {
if (axis.heldCount == 0) {
return false;
}
if (axis.physicallyHeld[0] == vk) {
if (axis.heldCount == 2) {
axis.physicallyHeld[0] = axis.physicallyHeld[1];
axis.heldCount = 1;
axis.activeKey = axis.physicallyHeld[0];
} else {
axis.heldCount = 0;
axis.activeKey = 0;
}
return true;
} else if (axis.heldCount == 2 && axis.physicallyHeld[1] == vk) {
axis.heldCount = 1;
axis.activeKey = axis.physicallyHeld[0];
return true;
}
return false;
}
void SendKeyDownImmediate(int vkCode) {
InjectKey(vkCode, true);
}
void SendKeyUpImmediate(int vkCode) {
InjectKey(vkCode, false);
}
// NOTE: Must be called with g_axisMutex held.
void ApplyVirtualAxisState_Locked(int ¤tVk, int desiredVk) {
if (currentVk == desiredVk)
return;
if (currentVk != 0) {
SendKeyUpImmediate(currentVk);
}
if (desiredVk != 0) {
SendKeyDownImmediate(desiredVk);
}
currentVk = desiredVk;
}
// NOTE: Must be called with g_axisMutex held.
void ReleaseSnapTapVirtualKeys_Locked() {
ApplyVirtualAxisState_Locked(g_virtualAxisX, 0);
ApplyVirtualAxisState_Locked(g_virtualAxisY, 0);
}
// NOTE: Must be called with g_axisMutex held.
void ApplySnapTapOutput_Locked(bool spamActive) {
const int desiredX = spamActive ? 0 : g_axisX.activeKey;
const int desiredY = spamActive ? 0 : g_axisY.activeKey;
ApplyVirtualAxisState_Locked(g_virtualAxisX, desiredX);
ApplyVirtualAxisState_Locked(g_virtualAxisY, desiredY);
}
// NOTE: Must be called with g_axisMutex held.
//
// Builds the WASD bitmask the spam thread should currently spam. With
// SnapTap on, that's the per-axis "winner" (LIFO last-pressed). With
// SnapTap off, every physically-held WASD key is spammed.
[[nodiscard]] uint32_t BuildDesiredSpamMask_Locked(bool snapTapEnabled) {
uint32_t mask = 0u;
if (snapTapEnabled) {
mask |= Globals::LurchBitForVk(g_axisX.activeKey);
mask |= Globals::LurchBitForVk(g_axisY.activeKey);
} else {
for (int key : {'W', 'A', 'S', 'D'}) {
if (Globals::g_KeyInfo[key].physicalKeyDown.load(
std::memory_order_acquire)) {
mask |= Globals::LurchBitForVk(key);
}
}
}
return mask;
}
// NOTE: Must be called with g_axisMutex held.
//
// Atomically publishes the new desired mask into `g_lurchState`, bumping
// the epoch only when the key bits actually change. On a change, also
// updates the per-key `spamming` flag and signals the spam CV.
//
// All publish work is mutex-free: spammers see a single 32-bit atomic.
void PublishSpamKeysFromState_Locked(bool snapTapEnabled) {
const uint32_t desiredKeys = BuildDesiredSpamMask_Locked(snapTapEnabled);
uint32_t prev = Globals::g_lurchState.load(std::memory_order_acquire);
uint32_t next = 0u;
bool changed = false;
do {
const uint32_t prevKeys = prev & Globals::kLurchKeyMask;
if (prevKeys == desiredKeys) {
changed = false;
next = prev;
break;
}
const uint32_t epoch =
(prev & ~Globals::kLurchKeyMask) + Globals::kLurchEpochInc;
next = (epoch & ~Globals::kLurchKeyMask) | desiredKeys;
changed = true;
} while (!Globals::g_lurchState.compare_exchange_weak(
prev, next, std::memory_order_acq_rel, std::memory_order_acquire));
// Mirror the bitmask onto the per-key `spamming` flag. This isn't on
// the spam thread's hot path — it's only used by GUI / cleanup logic.
for (int key : {'W', 'A', 'S', 'D'}) {
const bool spamming =
(desiredKeys & Globals::LurchBitForVk(key)) != 0u;
Globals::g_KeyInfo[key].spamming.store(spamming,
std::memory_order_relaxed);
}
if (changed) {
TriggerSpamEvent();
}
}
void SendKeyUpForPhysicallyHeldWasd() {
for (int key : {'W', 'A', 'S', 'D'}) {
if (Globals::g_KeyInfo[key].physicalKeyDown.load(
std::memory_order_acquire)) {
InjectKey(key, false);
}
}
}
void SendKeyDownForPhysicallyHeldWasd() {
for (int key : {'W', 'A', 'S', 'D'}) {
if (Globals::g_KeyInfo[key].physicalKeyDown.load(
std::memory_order_acquire)) {
InjectKey(key, true);
}
}
}
} // namespace
void OnSpamActivated(bool snapTapEnabled) {
std::lock_guard<std::mutex> lock(g_axisMutex);
// Ensure any held movement keys are virtually UP while spamming.
ApplySnapTapOutput_Locked(true);
SendKeyUpForPhysicallyHeldWasd();
PublishSpamKeysFromState_Locked(snapTapEnabled);
}
void OnSpamDeactivated(bool snapTapEnabled) {
std::lock_guard<std::mutex> lock(g_axisMutex);
if (snapTapEnabled) {
CleanupSpamState(false);
ApplySnapTapOutput_Locked(false);
} else {
CleanupSpamState(true);
}
}
bool HandleMovementKeyState(int vkCode, bool isKeyDown,
bool spamActive, bool snapTapEnabled) {
if (!IsWasdVk(vkCode)) {
return false;
}
std::lock_guard<std::mutex> lock(g_axisMutex);
AxisState *axis = AxisForVk(vkCode);
if (axis) {
if (isKeyDown) {
AxisOnKeyDown(*axis, vkCode);
} else {
AxisOnKeyUp(*axis, vkCode);
}
}
if (spamActive) {
// Spam layer keeps movement keys virtually UP and spams the active key(s).
// NOTE: We intentionally do NOT inject a key-up here. The physical event
// is already suppressed (return true), and OnSpamActivated() already sent
// key-up for any keys that were held when spam started. Injecting an
// extra key-up on every event (including autorepeat) races with the spam
// thread's own down/up cycle and can leave keys stuck in the target app.
ApplySnapTapOutput_Locked(true);
PublishSpamKeysFromState_Locked(snapTapEnabled);
return true;
}
if (snapTapEnabled) {
// SnapTap is always-on (even without trigger). It owns WASD output when
// enabled.
ApplySnapTapOutput_Locked(false);
return true;
}
return false;
}
void OnSnapTapToggled(bool enabled) {
const bool spamFeatureEnabled =
Config::EnableSpam.load(std::memory_order_relaxed);
const bool spamActive =
Globals::g_isSpamActive.load(std::memory_order_acquire) &&
spamFeatureEnabled;
std::lock_guard<std::mutex> lock(g_axisMutex);
if (enabled) {
// Clear any physical holds currently down in the target app before SnapTap
// takes over.
if (!spamActive) {
SendKeyUpForPhysicallyHeldWasd();
}
ApplySnapTapOutput_Locked(spamActive);
} else {
// Release any SnapTap-owned keys so we don't leave the target app in a
// stuck state.
ReleaseSnapTapVirtualKeys_Locked();
// Restore physical holds immediately (otherwise the user must re-press).
if (!spamActive) {
SendKeyDownForPhysicallyHeldWasd();
}
}
if (spamActive) {
PublishSpamKeysFromState_Locked(enabled);
}
}
void RefreshMovementState() {
const bool spamFeatureEnabled =
Config::EnableSpam.load(std::memory_order_relaxed);
const bool snapTapEnabled =
Config::EnableSnapTap.load(std::memory_order_relaxed);
const bool spamActive =
Globals::g_isSpamActive.load(std::memory_order_acquire) &&
spamFeatureEnabled;
std::lock_guard<std::mutex> lock(g_axisMutex);
if (snapTapEnabled) {
ApplySnapTapOutput_Locked(spamActive);
} else {
ReleaseSnapTapVirtualKeys_Locked();
}
if (spamActive) {
PublishSpamKeysFromState_Locked(snapTapEnabled);
}
}
void PrepareMovementStateForBackendSwitch() {
std::lock_guard<std::mutex> lock(g_axisMutex);
ReleaseSnapTapVirtualKeys_Locked();
Globals::g_isSpamActive.store(false, std::memory_order_release);
Globals::g_lurchState.store(0, std::memory_order_release);
for (int key : {'W', 'A', 'S', 'D'}) {
Globals::g_KeyInfo[key].spamming.store(false, std::memory_order_relaxed);
}
}
void ReconcileMovementStateAfterBackendSwitch() {
std::lock_guard<std::mutex> lock(g_axisMutex);
g_axisX = {};
g_axisY = {};
g_virtualAxisX = 0;
g_virtualAxisY = 0;
Globals::g_isSpamActive.store(false, std::memory_order_release);
Globals::g_lurchState.store(0, std::memory_order_release);
for (int vk = 0; vk < 256; ++vk) {
const bool isDown = (GetAsyncKeyState(vk) & 0x8000) != 0;
Globals::g_KeyInfo[vk].physicalKeyDown.store(isDown,
std::memory_order_release);
Globals::g_KeyInfo[vk].spamming.store(false, std::memory_order_relaxed);
}
for (int key : {'W', 'A', 'S', 'D'}) {
if (Globals::g_KeyInfo[key].physicalKeyDown.load(std::memory_order_acquire)) {
if (AxisState *axis = AxisForVk(key)) {
AxisOnKeyDown(*axis, key);
}
}
}
if (Config::EnableSnapTap.load(std::memory_order_relaxed)) {
SendKeyUpForPhysicallyHeldWasd();
ApplySnapTapOutput_Locked(false);
}
}