src/input.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file input.cpp | ||
| 3 | * @brief Implementation of the input polling and hotkey management system. | ||
| 4 | * | ||
| 5 | * Provides InputPoller (RAII polling engine) and InputManager (singleton wrapper) for monitoring keyboard, mouse, and | ||
| 6 | * gamepad input states on a background thread. Supports press (edge-triggered) and hold (level-triggered) input modes | ||
| 7 | * with modifier combinations, focus-aware polling, and XInput gamepad support. | ||
| 8 | */ | ||
| 9 | |||
| 10 | #include "DetourModKit/input.hpp" | ||
| 11 | #include "DetourModKit/diagnostics.hpp" | ||
| 12 | #include "DetourModKit/config.hpp" | ||
| 13 | #include "DetourModKit/logger.hpp" | ||
| 14 | |||
| 15 | #include "platform.hpp" | ||
| 16 | #include "input_intercept.hpp" | ||
| 17 | #include "input_key_cache.hpp" | ||
| 18 | |||
| 19 | #include <windows.h> | ||
| 20 | #include <Xinput.h> | ||
| 21 | #include <algorithm> | ||
| 22 | #include <array> | ||
| 23 | #include <atomic> | ||
| 24 | #include <cstdint> | ||
| 25 | #include <exception> | ||
| 26 | #include <new> | ||
| 27 | #include <shared_mutex> | ||
| 28 | #include <type_traits> | ||
| 29 | #include <unordered_set> | ||
| 30 | |||
| 31 | using DetourModKit::detail::is_loader_lock_held; | ||
| 32 | using DetourModKit::detail::pin_current_module; | ||
| 33 | |||
| 34 | namespace DetourModKit | ||
| 35 | { | ||
| 36 | namespace | ||
| 37 | { | ||
| 38 | /** | ||
| 39 | * @brief Checks whether a single InputCode is currently pressed. | ||
| 40 | * @param code The input code to check. | ||
| 41 | * @param key_cache Per-cycle keyboard/mouse down-state memoization, probed at most once per distinct VK. | ||
| 42 | * @param gamepad_state Cached XInput state for the current poll cycle. | ||
| 43 | * @param gamepad_connected Whether the gamepad is connected. | ||
| 44 | * @param trigger_threshold Analog trigger deadzone threshold. | ||
| 45 | * @param stick_threshold Thumbstick deadzone threshold. | ||
| 46 | * @param wheel_pulse Per-cycle wheel pulse mask (bit 0 = WheelUp .. bit 3 = | ||
| 47 | * WheelRight), latched once per cycle by the poll loop so repeated reads within a cycle stay consistent. | ||
| 48 | * @return true if the input is currently pressed. | ||
| 49 | */ | ||
| 50 | 1089 | bool is_code_pressed(const InputCode &code, detail::KeyStateCache &key_cache, const XINPUT_STATE &gamepad_state, | |
| 51 | bool gamepad_connected, int trigger_threshold, int stick_threshold, | ||
| 52 | uint8_t wheel_pulse) noexcept | ||
| 53 | { | ||
| 54 |
2/4✓ Branch 2 → 3 taken 1083 times.
✓ Branch 2 → 9 taken 6 times.
✗ Branch 2 → 13 not taken.
✗ Branch 2 → 29 not taken.
|
1089 | switch (code.source) |
| 55 | { | ||
| 56 | 1083 | case InputSource::Keyboard: | |
| 57 | case InputSource::Mouse: | ||
| 58 | // Route every keyboard/mouse read through the per-cycle cache so a VK referenced by many bindings (and | ||
| 59 | // by the strict known-modifier rescan) costs one GetAsyncKeyState call per cycle, not one per | ||
| 60 | // reference. The probe reads only the high (down) bit and gives the whole cycle one coherent sample. | ||
| 61 |
2/4✓ Branch 3 → 4 taken 1083 times.
✗ Branch 3 → 7 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1083 times.
|
1083 | return code.code != 0 && key_cache.pressed(code.code, [](int vk) noexcept |
| 62 | 1203 | { return (GetAsyncKeyState(vk) & 0x8000) != 0; }); | |
| 63 | 6 | case InputSource::MouseWheel: | |
| 64 | { | ||
| 65 | // The wheel has no held state; the poll loop latches each notch into wheel_pulse. WheelCode values are | ||
| 66 | // 1-based and dense, so the direction index is code - WheelCode::Up. | ||
| 67 | 6 | const int dir = code.code - WheelCode::Up; | |
| 68 |
2/4✓ Branch 9 → 10 taken 6 times.
✗ Branch 9 → 11 not taken.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 6 times.
|
6 | if (dir < 0 || dir > 3) |
| 69 | { | ||
| 70 | ✗ | return false; | |
| 71 | } | ||
| 72 | 6 | return (wheel_pulse & (1u << dir)) != 0; | |
| 73 | } | ||
| 74 | ✗ | case InputSource::Gamepad: | |
| 75 | { | ||
| 76 | ✗ | if (!gamepad_connected) | |
| 77 | { | ||
| 78 | ✗ | return false; | |
| 79 | } | ||
| 80 | // Fast path: digital button bitmask (all codes below synthetic range) | ||
| 81 | ✗ | if (code.code < GamepadCode::LeftTrigger) | |
| 82 | { | ||
| 83 | ✗ | return (gamepad_state.Gamepad.wButtons & static_cast<WORD>(code.code)) != 0; | |
| 84 | } | ||
| 85 | // Synthetic analog codes | ||
| 86 | ✗ | switch (code.code) | |
| 87 | { | ||
| 88 | ✗ | case GamepadCode::LeftTrigger: | |
| 89 | ✗ | return gamepad_state.Gamepad.bLeftTrigger > trigger_threshold; | |
| 90 | ✗ | case GamepadCode::RightTrigger: | |
| 91 | ✗ | return gamepad_state.Gamepad.bRightTrigger > trigger_threshold; | |
| 92 | ✗ | case GamepadCode::LeftStickUp: | |
| 93 | ✗ | return gamepad_state.Gamepad.sThumbLY > stick_threshold; | |
| 94 | ✗ | case GamepadCode::LeftStickDown: | |
| 95 | ✗ | return gamepad_state.Gamepad.sThumbLY < -stick_threshold; | |
| 96 | ✗ | case GamepadCode::LeftStickLeft: | |
| 97 | ✗ | return gamepad_state.Gamepad.sThumbLX < -stick_threshold; | |
| 98 | ✗ | case GamepadCode::LeftStickRight: | |
| 99 | ✗ | return gamepad_state.Gamepad.sThumbLX > stick_threshold; | |
| 100 | ✗ | case GamepadCode::RightStickUp: | |
| 101 | ✗ | return gamepad_state.Gamepad.sThumbRY > stick_threshold; | |
| 102 | ✗ | case GamepadCode::RightStickDown: | |
| 103 | ✗ | return gamepad_state.Gamepad.sThumbRY < -stick_threshold; | |
| 104 | ✗ | case GamepadCode::RightStickLeft: | |
| 105 | ✗ | return gamepad_state.Gamepad.sThumbRX < -stick_threshold; | |
| 106 | ✗ | case GamepadCode::RightStickRight: | |
| 107 | ✗ | return gamepad_state.Gamepad.sThumbRX > stick_threshold; | |
| 108 | ✗ | default: | |
| 109 | ✗ | return false; | |
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | ✗ | return false; | |
| 114 | } | ||
| 115 | |||
| 116 | /** | ||
| 117 | * @brief Checks if a held input satisfies a required modifier. | ||
| 118 | * @details Returns true when the codes match exactly, or when both are keyboard modifiers in the same family | ||
| 119 | * (e.g., LShift satisfies generic Shift, and generic Shift satisfies LShift). | ||
| 120 | */ | ||
| 121 | ✗ | bool modifier_satisfies(const InputCode &required, const InputCode &held) noexcept | |
| 122 | { | ||
| 123 | ✗ | if (required == held) | |
| 124 | { | ||
| 125 | ✗ | return true; | |
| 126 | } | ||
| 127 | ✗ | if (required.source != InputSource::Keyboard || held.source != InputSource::Keyboard) | |
| 128 | { | ||
| 129 | ✗ | return false; | |
| 130 | } | ||
| 131 | // Modifier family groups: {generic, left, right} | ||
| 132 | ✗ | constexpr int families[][3] = { | |
| 133 | {0x11, 0xA2, 0xA3}, // Ctrl, LCtrl, RCtrl | ||
| 134 | {0x10, 0xA0, 0xA1}, // Shift, LShift, RShift | ||
| 135 | {0x12, 0xA4, 0xA5}, // Alt, LAlt, RAlt | ||
| 136 | }; | ||
| 137 | ✗ | for (const auto &family : families) | |
| 138 | { | ||
| 139 | ✗ | bool req_in = false; | |
| 140 | ✗ | bool held_in = false; | |
| 141 | ✗ | for (int vk : family) | |
| 142 | { | ||
| 143 | ✗ | if (required.code == vk) | |
| 144 | { | ||
| 145 | ✗ | req_in = true; | |
| 146 | } | ||
| 147 | ✗ | if (held.code == vk) | |
| 148 | { | ||
| 149 | ✗ | held_in = true; | |
| 150 | } | ||
| 151 | } | ||
| 152 | ✗ | if (req_in && held_in) | |
| 153 | { | ||
| 154 | ✗ | return true; | |
| 155 | } | ||
| 156 | } | ||
| 157 | ✗ | return false; | |
| 158 | } | ||
| 159 | |||
| 160 | /** | ||
| 161 | * @brief Scans bindings to determine if any use gamepad input codes. | ||
| 162 | * @param bindings The vector of bindings to scan. | ||
| 163 | * @return true if at least one binding contains a gamepad InputCode. | ||
| 164 | */ | ||
| 165 | 2416 | bool scan_for_gamepad_bindings(const std::vector<InputBinding> &bindings) noexcept | |
| 166 | { | ||
| 167 |
2/2✓ Branch 47 → 4 taken 47924 times.
✓ Branch 47 → 48 taken 2396 times.
|
52736 | for (const auto &binding : bindings) |
| 168 | { | ||
| 169 |
2/2✓ Branch 21 → 8 taken 47594 times.
✓ Branch 21 → 22 taken 47904 times.
|
143422 | for (const auto &key : binding.keys) |
| 170 | { | ||
| 171 |
2/2✓ Branch 10 → 11 taken 20 times.
✓ Branch 10 → 12 taken 47574 times.
|
47594 | if (key.source == InputSource::Gamepad) |
| 172 | { | ||
| 173 | 20 | return true; | |
| 174 | } | ||
| 175 | } | ||
| 176 |
2/2✓ Branch 37 → 24 taken 13 times.
✓ Branch 37 → 38 taken 47904 times.
|
95821 | for (const auto &mod : binding.modifiers) |
| 177 | { | ||
| 178 |
1/2✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 13 times.
|
13 | if (mod.source == InputSource::Gamepad) |
| 179 | { | ||
| 180 | ✗ | return true; | |
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | 2396 | return false; | |
| 185 | } | ||
| 186 | |||
| 187 | /** | ||
| 188 | * @brief Reports whether any binding uses a mouse-wheel trigger. | ||
| 189 | * @details Wheel codes only appear as trigger keys (never modifiers), so modifiers are not scanned. Drives lazy | ||
| 190 | * installation of the window-procedure hook that captures wheel events. | ||
| 191 | */ | ||
| 192 | 2416 | bool scan_for_wheel_bindings(const std::vector<InputBinding> &bindings) noexcept | |
| 193 | { | ||
| 194 |
2/2✓ Branch 31 → 4 taken 47929 times.
✓ Branch 31 → 32 taken 2414 times.
|
52759 | for (const auto &binding : bindings) |
| 195 | { | ||
| 196 |
2/2✓ Branch 21 → 8 taken 47599 times.
✓ Branch 21 → 22 taken 47927 times.
|
143455 | for (const auto &key : binding.keys) |
| 197 | { | ||
| 198 |
2/2✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 12 taken 47597 times.
|
47599 | if (key.source == InputSource::MouseWheel) |
| 199 | { | ||
| 200 | 2 | return true; | |
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | 2414 | return false; | |
| 205 | } | ||
| 206 | |||
| 207 | /** | ||
| 208 | * @brief Reports whether any consume binding carries a suppressible gamepad button (gates the XInput hook). | ||
| 209 | * @details Only digital buttons gate it: the detour masks XINPUT_GAMEPAD.wButtons, so analog triggers and stick | ||
| 210 | * directions (the synthetic codes >= | ||
| 211 | * GamepadCode::LeftTrigger) can never be cleared and must not install a hook that would mask nothing. | ||
| 212 | */ | ||
| 213 | 2416 | bool scan_for_consume_gamepad_bindings(const std::vector<InputBinding> &bindings) noexcept | |
| 214 | { | ||
| 215 |
2/2✓ Branch 36 → 4 taken 47928 times.
✓ Branch 36 → 37 taken 2411 times.
|
52755 | for (const auto &binding : bindings) |
| 216 | { | ||
| 217 |
2/2✓ Branch 6 → 7 taken 47918 times.
✓ Branch 6 → 8 taken 10 times.
|
47928 | if (!binding.consume) |
| 218 | { | ||
| 219 | 47918 | continue; | |
| 220 | } | ||
| 221 |
2/2✓ Branch 25 → 10 taken 10 times.
✓ Branch 25 → 26 taken 5 times.
|
25 | for (const auto &key : binding.keys) |
| 222 | { | ||
| 223 |
5/6✓ Branch 12 → 13 taken 7 times.
✓ Branch 12 → 16 taken 3 times.
✓ Branch 13 → 14 taken 7 times.
✗ Branch 13 → 16 not taken.
✓ Branch 14 → 15 taken 5 times.
✓ Branch 14 → 16 taken 2 times.
|
10 | if (key.source == InputSource::Gamepad && key.code > 0 && key.code < GamepadCode::LeftTrigger) |
| 224 | { | ||
| 225 | 5 | return true; | |
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | 2411 | return false; | |
| 230 | } | ||
| 231 | |||
| 232 | /// Reports whether any consume binding carries a wheel trigger (gates wheel swallowing). | ||
| 233 | 2416 | bool scan_for_wheel_consume_bindings(const std::vector<InputBinding> &bindings) noexcept | |
| 234 | { | ||
| 235 |
2/2✓ Branch 34 → 4 taken 47929 times.
✓ Branch 34 → 35 taken 2415 times.
|
52760 | for (const auto &binding : bindings) |
| 236 | { | ||
| 237 |
2/2✓ Branch 6 → 7 taken 47918 times.
✓ Branch 6 → 8 taken 11 times.
|
47929 | if (!binding.consume) |
| 238 | { | ||
| 239 | 47918 | continue; | |
| 240 | } | ||
| 241 |
2/2✓ Branch 23 → 10 taken 11 times.
✓ Branch 23 → 24 taken 10 times.
|
32 | for (const auto &key : binding.keys) |
| 242 | { | ||
| 243 |
2/2✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 14 taken 10 times.
|
11 | if (key.source == InputSource::MouseWheel) |
| 244 | { | ||
| 245 | 1 | return true; | |
| 246 | } | ||
| 247 | } | ||
| 248 | } | ||
| 249 | 2415 | return false; | |
| 250 | } | ||
| 251 | |||
| 252 | /** | ||
| 253 | * @brief Builds the detour-evaluable consume rule list from the current bindings. | ||
| 254 | * @details A rule is emitted for every consume binding whose masked triggers include a digital gamepad button, | ||
| 255 | * but only when every known modifier (across all bindings) is itself a digital gamepad button. The | ||
| 256 | * XInput detour sees only | ||
| 257 | * XINPUT_GAMEPAD.wButtons, so it cannot observe a keyboard/mouse modifier or an analog trigger/stick | ||
| 258 | * used as a modifier; if any such modifier exists, the poll loop's strict-match decision is not | ||
| 259 | * reproducible in the detour, so the whole list is dropped and the reactive (poll-published) mask | ||
| 260 | * alone covers the held-modifier case. For an eligible binding the rule carries: | ||
| 261 | * modifier_mask -- all of the chord's modifier bits, | ||
| 262 | * trigger_mask -- the chord's digital gamepad trigger bits to clear, | ||
| 263 | * forbidden_mask -- every other known modifier bit, so holding a modifier | ||
| 264 | * that belongs to a different chord rejects this one, exactly as the poll loop's | ||
| 265 | * strict-match check does. | ||
| 266 | */ | ||
| 267 | std::vector<detail::GamepadConsumeRule> | ||
| 268 | 2416 | build_gamepad_consume_rules(const std::vector<InputBinding> &bindings, | |
| 269 | const std::vector<InputCode> &known_modifiers) | ||
| 270 | { | ||
| 271 | 28 | const auto is_digital_gamepad = [](const InputCode &code) noexcept | |
| 272 |
5/6✓ Branch 2 → 3 taken 15 times.
✓ Branch 2 → 6 taken 13 times.
✓ Branch 3 → 4 taken 15 times.
✗ Branch 3 → 6 not taken.
✓ Branch 4 → 5 taken 13 times.
✓ Branch 4 → 6 taken 2 times.
|
28 | { return code.source == InputSource::Gamepad && code.code > 0 && code.code < GamepadCode::LeftTrigger; }; |
| 273 | |||
| 274 | // The detour can only reproduce strict matching when every known modifier is a digital gamepad button it | ||
| 275 | // can read in wButtons. If any is not, emit no rules (the reactive path still handles the held-modifier | ||
| 276 | // case). | ||
| 277 | 2416 | uint16_t known_mod_mask = 0; | |
| 278 |
2/2✓ Branch 19 → 4 taken 18 times.
✓ Branch 19 → 20 taken 2406 times.
|
4840 | for (const auto &mod : known_modifiers) |
| 279 | { | ||
| 280 |
2/2✓ Branch 7 → 8 taken 10 times.
✓ Branch 7 → 10 taken 8 times.
|
18 | if (!is_digital_gamepad(mod)) |
| 281 | { | ||
| 282 | 10 | return {}; | |
| 283 | } | ||
| 284 | 8 | known_mod_mask = static_cast<uint16_t>(known_mod_mask | static_cast<uint16_t>(mod.code)); | |
| 285 | } | ||
| 286 | |||
| 287 | 2406 | std::vector<detail::GamepadConsumeRule> rules; | |
| 288 |
2/2✓ Branch 70 → 22 taken 47908 times.
✓ Branch 70 → 71 taken 2406 times.
|
52720 | for (const auto &binding : bindings) |
| 289 | { | ||
| 290 |
2/2✓ Branch 24 → 25 taken 47898 times.
✓ Branch 24 → 26 taken 10 times.
|
47908 | if (!binding.consume) |
| 291 | { | ||
| 292 | 47898 | continue; | |
| 293 | } | ||
| 294 | 10 | uint16_t trigger_mask = 0; | |
| 295 |
2/2✓ Branch 42 → 28 taken 10 times.
✓ Branch 42 → 43 taken 10 times.
|
30 | for (const auto &key : binding.keys) |
| 296 | { | ||
| 297 |
2/2✓ Branch 31 → 32 taken 5 times.
✓ Branch 31 → 33 taken 5 times.
|
10 | if (is_digital_gamepad(key)) |
| 298 | { | ||
| 299 | 5 | trigger_mask = static_cast<uint16_t>(trigger_mask | static_cast<uint16_t>(key.code)); | |
| 300 | } | ||
| 301 | } | ||
| 302 |
2/2✓ Branch 43 → 44 taken 5 times.
✓ Branch 43 → 45 taken 5 times.
|
10 | if (trigger_mask == 0) |
| 303 | { | ||
| 304 | // No digital gamepad trigger to clear (e.g. a wheel or analog consume binding); nothing here for | ||
| 305 | // the detour to mask. | ||
| 306 | 5 | continue; | |
| 307 | } | ||
| 308 | // Every modifier is a digital gamepad button here: the gate above returned an empty list if any known | ||
| 309 | // modifier was not, and a chord's modifiers are a subset of the known modifiers. | ||
| 310 | 5 | uint16_t modifier_mask = 0; | |
| 311 |
2/2✓ Branch 58 → 47 taken 3 times.
✓ Branch 58 → 59 taken 5 times.
|
13 | for (const auto &mod : binding.modifiers) |
| 312 | { | ||
| 313 | 3 | modifier_mask = static_cast<uint16_t>(modifier_mask | static_cast<uint16_t>(mod.code)); | |
| 314 | } | ||
| 315 | 5 | const uint16_t forbidden_mask = | |
| 316 | 5 | static_cast<uint16_t>(known_mod_mask & static_cast<uint16_t>(~modifier_mask)); | |
| 317 |
1/2✓ Branch 59 → 60 taken 5 times.
✗ Branch 59 → 75 not taken.
|
5 | rules.push_back(detail::GamepadConsumeRule{modifier_mask, forbidden_mask, trigger_mask}); |
| 318 | } | ||
| 319 | 2406 | return rules; | |
| 320 | 2406 | } | |
| 321 | |||
| 322 | // Release grace for gamepad consume-until-release. Long enough to absorb the modifier-released-before-trigger | ||
| 323 | // window (the player relaxing the bumper a frame or two before the thumb leaves the D-pad) without noticeably | ||
| 324 | // delaying a deliberate tap that follows. | ||
| 325 | constexpr uint64_t GAMEPAD_SUPPRESS_GRACE_MS = 80; | ||
| 326 | |||
| 327 | // Process-wide monotonic source for BindingToken generations. Each InputPoller reshape draws a fresh value, so | ||
| 328 | // a generation is unique across the whole process and across poller lifetimes: a token minted by one poller can | ||
| 329 | // never alias a different poller's state (for example after a shutdown / start cycle swaps the poller). Starts | ||
| 330 | // at 1 so the value 0 stays reserved for an invalid token. | ||
| 331 | std::atomic<std::uint64_t> s_next_binding_generation{1}; | ||
| 332 | |||
| 333 | /// Draws the next unique binding generation. Lock-free; relaxed suffices (uniqueness, not ordering, is needed). | ||
| 334 | 2419 | std::uint64_t next_binding_generation() noexcept | |
| 335 | { | ||
| 336 | 2419 | return s_next_binding_generation.fetch_add(1, std::memory_order_relaxed); | |
| 337 | } | ||
| 338 | } // anonymous namespace | ||
| 339 | |||
| 340 | static_assert(std::is_nothrow_move_assignable_v<InputBinding>, | ||
| 341 | "Input reshape commits rely on noexcept InputBinding move assignment"); | ||
| 342 | |||
| 343 | // --- InputPoller --- | ||
| 344 | |||
| 345 | 96 | InputPoller::InputPoller(std::vector<InputBinding> bindings, std::chrono::milliseconds poll_interval, | |
| 346 | 96 | bool require_focus, int gamepad_index, int trigger_threshold, int stick_threshold) | |
| 347 | 192 | : m_bindings(std::move(bindings)), | |
| 348 | 192 | m_poll_interval(std::clamp(poll_interval, MIN_POLL_INTERVAL, MAX_POLL_INTERVAL)), | |
| 349 |
1/2✓ Branch 15 → 16 taken 96 times.
✗ Branch 15 → 35 not taken.
|
192 | m_require_focus(require_focus), m_active_states(std::make_unique<std::atomic<uint8_t>[]>(m_bindings.size())), |
| 350 |
2/4✓ Branch 16 → 17 taken 96 times.
✗ Branch 16 → 27 not taken.
✓ Branch 17 → 18 taken 96 times.
✗ Branch 17 → 29 not taken.
|
96 | m_gamepad_index(std::clamp(gamepad_index, 0, 3)), m_trigger_threshold(std::clamp(trigger_threshold, 0, 255)), |
| 351 |
3/6✓ Branch 8 → 9 taken 96 times.
✗ Branch 8 → 41 not taken.
✓ Branch 13 → 14 taken 96 times.
✗ Branch 13 → 37 not taken.
✓ Branch 18 → 19 taken 96 times.
✗ Branch 18 → 31 not taken.
|
384 | m_stick_threshold(std::clamp(stick_threshold, 0, 32767)) |
| 352 | { | ||
| 353 |
1/2✓ Branch 24 → 25 taken 96 times.
✗ Branch 24 → 33 not taken.
|
96 | m_name_index.reserve(m_bindings.size()); |
| 354 | 96 | recompute_modifier_caches_locked(); | |
| 355 | 96 | } | |
| 356 | |||
| 357 | 2416 | void InputPoller::recompute_modifier_caches_locked() noexcept | |
| 358 | { | ||
| 359 | // Any call here rebuilds m_name_index, so a cached BindingToken's indices may no longer address the same | ||
| 360 | // bindings. Advance the generation up front -- even if the rebuild below fails into the catch and clears the | ||
| 361 | // caches -- so every outstanding token is conservatively invalidated and fails closed until re-acquired. | ||
| 362 | 2416 | m_binding_generation = next_binding_generation(); | |
| 363 | |||
| 364 | // Rebuild the lookup caches into local containers and commit them with non-throwing moves only after every | ||
| 365 | // allocation has succeeded. This helper is noexcept and reachable from loader-lock teardown, so an allocation | ||
| 366 | // failure must keep the poller internally consistent rather than letting std::bad_alloc escape and terminate | ||
| 367 | // the host. | ||
| 368 | try | ||
| 369 | { | ||
| 370 | 2416 | decltype(m_name_index) name_index; | |
| 371 | 2416 | std::unordered_set<InputCode, InputCodeHash> modifier_set; | |
| 372 |
2/2✓ Branch 30 → 6 taken 47929 times.
✓ Branch 30 → 31 taken 2416 times.
|
50345 | for (size_t i = 0; i < m_bindings.size(); ++i) |
| 373 | { | ||
| 374 |
1/2✓ Branch 8 → 9 taken 47929 times.
✗ Branch 8 → 12 not taken.
|
47929 | if (!m_bindings[i].name.empty()) |
| 375 | { | ||
| 376 |
2/4✓ Branch 10 → 11 taken 47929 times.
✗ Branch 10 → 63 not taken.
✓ Branch 11 → 12 taken 47929 times.
✗ Branch 11 → 63 not taken.
|
47929 | name_index[m_bindings[i].name].push_back(i); |
| 377 | } | ||
| 378 |
2/2✓ Branch 27 → 15 taken 22 times.
✓ Branch 27 → 28 taken 47929 times.
|
95880 | for (const auto &mod : m_bindings[i].modifiers) |
| 379 | { | ||
| 380 |
1/2✓ Branch 17 → 18 taken 22 times.
✗ Branch 17 → 61 not taken.
|
22 | modifier_set.insert(mod); |
| 381 | } | ||
| 382 | } | ||
| 383 |
1/2✓ Branch 35 → 36 taken 2416 times.
✗ Branch 35 → 64 not taken.
|
2416 | std::vector<InputCode> known_modifiers(modifier_set.begin(), modifier_set.end()); |
| 384 | |||
| 385 | // Built from the same bindings and modifier set as the reactive path so the poll-published mask and the | ||
| 386 | // detour-side consume rules never disagree (published in the commit step below). | ||
| 387 | const std::vector<detail::GamepadConsumeRule> consume_rules = | ||
| 388 |
1/2✓ Branch 37 → 38 taken 2416 times.
✗ Branch 37 → 67 not taken.
|
2416 | build_gamepad_consume_rules(m_bindings, known_modifiers); |
| 389 | |||
| 390 | // Commit. Container move-assignment with the default allocator does not allocate, so from here the function | ||
| 391 | // cannot fail. | ||
| 392 | 2416 | m_name_index = std::move(name_index); | |
| 393 | 2416 | m_known_modifiers = std::move(known_modifiers); | |
| 394 | 2416 | m_has_gamepad_bindings.store(scan_for_gamepad_bindings(m_bindings), std::memory_order_relaxed); | |
| 395 | 2416 | m_has_wheel_bindings.store(scan_for_wheel_bindings(m_bindings), std::memory_order_relaxed); | |
| 396 | 2416 | m_has_consume_gamepad_bindings.store(scan_for_consume_gamepad_bindings(m_bindings), | |
| 397 | std::memory_order_relaxed); | ||
| 398 | 2416 | m_has_wheel_consume_bindings.store(scan_for_wheel_consume_bindings(m_bindings), std::memory_order_relaxed); | |
| 399 | |||
| 400 | // Publish the detour-side consume rule list. The XInput detour evaluates these against the exact snapshot | ||
| 401 | // the game reads, closing the leading-edge window the poll-published mask leaves for a modifier and trigger | ||
| 402 | // pressed inside one poll interval. | ||
| 403 | 2416 | detail::publish_gamepad_consume_rules(consume_rules.data(), consume_rules.size()); | |
| 404 | 2416 | } | |
| 405 | ✗ | catch (...) | |
| 406 | { | ||
| 407 | // m_bindings and m_active_states may already reflect a reshape. Keep every derived cache conservative and | ||
| 408 | // index-safe rather than leaving a stale name map whose old indices could address past the new binding | ||
| 409 | // array. | ||
| 410 | ✗ | m_name_index.clear(); | |
| 411 | ✗ | m_known_modifiers.clear(); | |
| 412 | ✗ | m_has_gamepad_bindings.store(false, std::memory_order_relaxed); | |
| 413 | ✗ | m_has_wheel_bindings.store(false, std::memory_order_relaxed); | |
| 414 | ✗ | m_has_consume_gamepad_bindings.store(false, std::memory_order_relaxed); | |
| 415 | ✗ | m_has_wheel_consume_bindings.store(false, std::memory_order_relaxed); | |
| 416 | ✗ | detail::publish_gamepad_consume_rules(nullptr, 0); | |
| 417 | ✗ | (void)Logger::get_instance().try_log( | |
| 418 | LogLevel::Error, "InputPoller: out of memory rebuilding modifier caches; " | ||
| 419 | "name lookup and input interception disabled until the next successful rebuild"); | ||
| 420 | ✗ | } | |
| 421 | 2416 | } | |
| 422 | |||
| 423 | 96 | InputPoller::~InputPoller() noexcept | |
| 424 | { | ||
| 425 | 96 | shutdown(); | |
| 426 | 96 | } | |
| 427 | |||
| 428 | 75 | void InputPoller::start() | |
| 429 | { | ||
| 430 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 7 taken 74 times.
|
75 | if (m_poll_thread.joinable()) |
| 431 | { | ||
| 432 |
1/2✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 13 not taken.
|
1 | Logger::get_instance().debug("InputPoller: start() called while already running; no-op."); |
| 433 | 1 | return; | |
| 434 | } | ||
| 435 | |||
| 436 | 74 | m_running.store(true, std::memory_order_release); | |
| 437 | try | ||
| 438 | { | ||
| 439 |
2/4✓ Branch 5 → 6 taken 74 times.
✗ Branch 5 → 8 not taken.
✓ Branch 8 → 9 taken 74 times.
✗ Branch 8 → 14 not taken.
|
222 | m_poll_thread = std::jthread([this](std::stop_token token) { poll_loop(std::move(token)); }); |
| 440 | } | ||
| 441 | ✗ | catch (...) | |
| 442 | { | ||
| 443 | ✗ | m_running.store(false, std::memory_order_release); | |
| 444 | ✗ | throw; | |
| 445 | ✗ | } | |
| 446 | } | ||
| 447 | |||
| 448 | 45 | bool InputPoller::is_running() const noexcept | |
| 449 | { | ||
| 450 | 45 | return m_running.load(std::memory_order_acquire); | |
| 451 | } | ||
| 452 | |||
| 453 | 67658 | size_t InputPoller::binding_count() const noexcept | |
| 454 | { | ||
| 455 | 67658 | std::shared_lock lock(m_bindings_rw_mutex); | |
| 456 | 67665 | return m_bindings.size(); | |
| 457 | 67585 | } | |
| 458 | |||
| 459 | 4 | std::chrono::milliseconds InputPoller::poll_interval() const noexcept | |
| 460 | { | ||
| 461 | 4 | return m_poll_interval; | |
| 462 | } | ||
| 463 | |||
| 464 | 2 | int InputPoller::gamepad_index() const noexcept | |
| 465 | { | ||
| 466 | 2 | return m_gamepad_index; | |
| 467 | } | ||
| 468 | |||
| 469 | 5 | bool InputPoller::is_binding_active(size_t index) const noexcept | |
| 470 | { | ||
| 471 | // Acquire the shared lock so the index/array pair stays consistent across a reshape (add_binding, | ||
| 472 | // remove_bindings_by_name, update_combos all swap m_active_states under the writer lock and resize m_bindings | ||
| 473 | // alongside it). The relaxed atomic load on the element itself is still cheap; it is the unique_ptr<atomic[]> | ||
| 474 | // ownership swap that needs synchronisation. | ||
| 475 | 5 | std::shared_lock lock(m_bindings_rw_mutex); | |
| 476 |
2/2✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 3 times.
|
5 | if (index >= m_bindings.size()) |
| 477 | { | ||
| 478 | 2 | return false; | |
| 479 | } | ||
| 480 | 6 | return m_active_states[index].load(std::memory_order_relaxed) != 0; | |
| 481 | 5 | } | |
| 482 | |||
| 483 | 67631 | bool InputPoller::is_binding_active(std::string_view name) const noexcept | |
| 484 | { | ||
| 485 | 67631 | std::shared_lock lock(m_bindings_rw_mutex); | |
| 486 | 67546 | const auto it = m_name_index.find(name); | |
| 487 |
2/2✓ Branch 6 → 7 taken 65587 times.
✓ Branch 6 → 38 taken 2977 times.
|
65509 | if (it != m_name_index.end()) |
| 488 | { | ||
| 489 |
2/2✓ Branch 36 → 10 taken 85194 times.
✓ Branch 36 → 37 taken 62196 times.
|
212977 | for (const size_t idx : it->second) |
| 490 | { | ||
| 491 | // The shared lock holds m_name_index and m_active_states consistent, so idx is in bounds here. The | ||
| 492 | // explicit bound check is defence in depth against a future reshape that repopulates m_name_index | ||
| 493 | // without resizing m_active_states (the same guard the BindingToken overload carries); it costs one | ||
| 494 | // comparison per matching binding (typically 1-3). | ||
| 495 |
4/6✓ Branch 13 → 14 taken 85328 times.
✓ Branch 13 → 24 taken 140 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 85018 times.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 85158 times.
|
170212 | if (idx < m_bindings.size() && m_active_states[idx].load(std::memory_order_relaxed) != 0) |
| 496 | { | ||
| 497 | ✗ | return true; | |
| 498 | } | ||
| 499 | } | ||
| 500 | } | ||
| 501 | 65173 | return false; | |
| 502 | 65173 | } | |
| 503 | |||
| 504 | 16 | BindingToken InputPoller::acquire_binding_token(std::string_view name) const noexcept | |
| 505 | { | ||
| 506 | 16 | BindingToken token; | |
| 507 | try | ||
| 508 | { | ||
| 509 | 16 | std::shared_lock lock(m_bindings_rw_mutex); | |
| 510 |
1/2✓ Branch 3 → 4 taken 16 times.
✗ Branch 3 → 20 not taken.
|
16 | const auto it = m_name_index.find(name); |
| 511 |
2/2✓ Branch 6 → 7 taken 2 times.
✓ Branch 6 → 9 taken 14 times.
|
16 | if (it == m_name_index.end()) |
| 512 | { | ||
| 513 | // Unknown name: leave the token invalid (generation 0). | ||
| 514 | 2 | return token; | |
| 515 | } | ||
| 516 | // Copy the resolved indices first (the only throwing step), then stamp the generation only once the copy | ||
| 517 | // succeeds, so an allocation failure leaves the token invalid rather than valid-but-empty. | ||
| 518 |
1/2✓ Branch 10 → 11 taken 14 times.
✗ Branch 10 → 20 not taken.
|
14 | token.m_indices = it->second; |
| 519 | 14 | token.m_generation = m_binding_generation; | |
| 520 |
2/2✓ Branch 13 → 14 taken 14 times.
✓ Branch 13 → 16 taken 2 times.
|
16 | } |
| 521 | ✗ | catch (...) | |
| 522 | { | ||
| 523 | // Out of memory copying the index set. acquire_binding_token is noexcept; return an invalid token so the | ||
| 524 | // consumer falls back to the name-based query rather than terminating the host. | ||
| 525 | ✗ | return BindingToken{}; | |
| 526 | ✗ | } | |
| 527 | 14 | return token; | |
| 528 | 16 | } | |
| 529 | |||
| 530 | 13 | bool InputPoller::is_binding_active(const BindingToken &token) const noexcept | |
| 531 | { | ||
| 532 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 11 times.
|
13 | if (!token.valid()) |
| 533 | { | ||
| 534 | 2 | return false; | |
| 535 | } | ||
| 536 | 11 | std::shared_lock lock(m_bindings_rw_mutex); | |
| 537 | // A reshape since acquisition advanced m_binding_generation, so a mismatch means the cached indices may no | ||
| 538 | // longer address the same bindings (or may be out of bounds): fail closed without touching them. | ||
| 539 |
2/2✓ Branch 6 → 7 taken 7 times.
✓ Branch 6 → 8 taken 4 times.
|
11 | if (token.m_generation != m_binding_generation) |
| 540 | { | ||
| 541 | 7 | return false; | |
| 542 | } | ||
| 543 |
2/2✓ Branch 36 → 10 taken 5 times.
✓ Branch 36 → 37 taken 4 times.
|
13 | for (const size_t idx : token.m_indices) |
| 544 | { | ||
| 545 | // The generation match proves no reshape resized the binding array since acquisition, so idx is in bounds. | ||
| 546 | // The explicit bound check is defence in depth against a future reshape path that forgets to advance the | ||
| 547 | // generation; it costs one comparison per cached entry (typically 1-3). | ||
| 548 |
3/6✓ Branch 13 → 14 taken 5 times.
✗ Branch 13 → 24 not taken.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 5 times.
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 5 times.
|
10 | if (idx < m_bindings.size() && m_active_states[idx].load(std::memory_order_relaxed) != 0) |
| 549 | { | ||
| 550 | ✗ | return true; | |
| 551 | } | ||
| 552 | } | ||
| 553 | 4 | return false; | |
| 554 | 11 | } | |
| 555 | |||
| 556 | 23 | bool InputPoller::binding_token_current(const BindingToken &token) const noexcept | |
| 557 | { | ||
| 558 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 5 taken 21 times.
|
23 | if (!token.valid()) |
| 559 | { | ||
| 560 | 2 | return false; | |
| 561 | } | ||
| 562 | 21 | std::shared_lock lock(m_bindings_rw_mutex); | |
| 563 | 21 | return token.m_generation == m_binding_generation; | |
| 564 | 21 | } | |
| 565 | |||
| 566 | 8 | void InputPoller::set_require_focus(bool require_focus) noexcept | |
| 567 | { | ||
| 568 | 8 | m_require_focus.store(require_focus, std::memory_order_relaxed); | |
| 569 | 8 | } | |
| 570 | |||
| 571 | 4 | void InputPoller::set_consume(std::string_view name, bool consume) noexcept | |
| 572 | { | ||
| 573 | 4 | std::unique_lock lock(m_bindings_rw_mutex); | |
| 574 | 4 | const auto it = m_name_index.find(name); | |
| 575 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 4 times.
|
4 | if (it == m_name_index.end()) |
| 576 | { | ||
| 577 | ✗ | return; | |
| 578 | } | ||
| 579 |
2/2✓ Branch 23 → 11 taken 4 times.
✓ Branch 23 → 24 taken 4 times.
|
12 | for (const size_t idx : it->second) |
| 580 | { | ||
| 581 | 4 | m_bindings[idx].consume = consume; | |
| 582 | } | ||
| 583 | // Refresh the interception gates so the poll loop installs or skips the | ||
| 584 | // XInput / window-procedure hooks on its next cycle. | ||
| 585 | 4 | recompute_modifier_caches_locked(); | |
| 586 |
1/2✓ Branch 27 → 28 taken 4 times.
✗ Branch 27 → 30 not taken.
|
4 | } |
| 587 | |||
| 588 | 171 | void InputPoller::shutdown() noexcept | |
| 589 | { | ||
| 590 |
2/2✓ Branch 3 → 4 taken 97 times.
✓ Branch 3 → 5 taken 74 times.
|
171 | if (!m_poll_thread.joinable()) |
| 591 | { | ||
| 592 | 97 | return; | |
| 593 | } | ||
| 594 | |||
| 595 | 74 | m_poll_thread.request_stop(); | |
| 596 | 74 | m_cv.notify_all(); | |
| 597 | |||
| 598 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 14 taken 74 times.
|
74 | if (is_loader_lock_held()) |
| 599 | { | ||
| 600 | // Under loader lock (FreeLibrary / process unload) the poll thread cannot be joined without deadlocking the | ||
| 601 | // loader, so it is detached after pinning the module. It is still running and will exit only once it | ||
| 602 | // observes the stop request, so we must NOT touch shared binding state or fire hold-release callbacks here: | ||
| 603 | // that would race the detached thread and run user callbacks under the loader lock (a callback that enters | ||
| 604 | // the loader -- LoadLibrary family or a peer | ||
| 605 | // DllMain mutex -- would deadlock). Mirrors clear_bindings(invoke_callbacks=false). | ||
| 606 | ✗ | pin_current_module(); | |
| 607 | ✗ | m_poll_thread.detach(); | |
| 608 | ✗ | DetourModKit::Diagnostics::record_intentional_leak(DetourModKit::Diagnostics::LeakSubsystem::Input); | |
| 609 | ✗ | m_running.store(false, std::memory_order_release); | |
| 610 | ✗ | return; | |
| 611 | } | ||
| 612 | |||
| 613 | 74 | m_poll_thread.join(); | |
| 614 | |||
| 615 | // The poll thread is provably stopped here, so releasing active holds and firing their on_state_change(false) | ||
| 616 | // callbacks is race-free. | ||
| 617 | 74 | m_running.store(false, std::memory_order_release); | |
| 618 | |||
| 619 | // The poll thread is the sole publisher of the suppression mask and the sole reader of the XInput trampoline, | ||
| 620 | // so tearing the interception hooks down now is race-free. This is skipped on the loader-lock path above: | ||
| 621 | // safetyhook's hook removal VirtualProtects the patched code pages and registers a vectored exception handler | ||
| 622 | // to fix up any in-flight thread, which must not run under the loader lock, so the detours are intentionally | ||
| 623 | // left installed against the pinned module instead. | ||
| 624 | 74 | detail::uninstall(); | |
| 625 | |||
| 626 | 74 | release_active_holds(); | |
| 627 | } | ||
| 628 | |||
| 629 | 74 | void InputPoller::poll_loop(std::stop_token stop_token) | |
| 630 | { | ||
| 631 | 74 | const int trigger_thresh = m_trigger_threshold; | |
| 632 | 74 | const int stick_thresh = m_stick_threshold; | |
| 633 | |||
| 634 | 74 | constexpr auto gamepad_reconnect_interval = std::chrono::seconds{2}; | |
| 635 | 74 | bool gamepad_was_connected = false; | |
| 636 | 74 | auto last_gamepad_poll = std::chrono::steady_clock::time_point{}; | |
| 637 | |||
| 638 | // Interception state, carried across cycles. Both are poll-thread-private: | ||
| 639 | // the published mask and wheel latch they feed live in input_intercept. | ||
| 640 | 74 | detail::WheelPulseState wheel_pulse{}; | |
| 641 | 74 | detail::GamepadSuppressState gp_suppress{}; | |
| 642 | |||
| 643 | // Reused across cycles so each tick does not re-zero a 16-byte XINPUT_STATE. Poll-thread-private: only this | ||
| 644 | // loop reads or writes it, and a stale value is never observed because is_code_pressed reads it only when | ||
| 645 | // gamepad_connected (recomputed every cycle) is true, which holds only after a successful poll overwrites it. | ||
| 646 | 74 | XINPUT_STATE gamepad_state{}; | |
| 647 | |||
| 648 | // Per-cycle keyboard/mouse down-state cache, reset at the top of every cycle so each distinct VK costs one | ||
| 649 | // GetAsyncKeyState call per cycle instead of one per binding reference (see input_key_cache.hpp). Declared | ||
| 650 | // once so its 256-byte table is allocated for the poll thread's lifetime, not rebuilt each cycle. | ||
| 651 | 74 | detail::KeyStateCache key_cache; | |
| 652 | |||
| 653 | struct PendingCallback | ||
| 654 | { | ||
| 655 | std::string name; | ||
| 656 | std::function<void()> on_press; | ||
| 657 | std::function<void(bool)> on_state_change; | ||
| 658 | bool hold_value; | ||
| 659 | }; | ||
| 660 | 74 | std::vector<PendingCallback> pending; | |
| 661 | |||
| 662 |
2/2✓ Branch 270 → 3 taken 101 times.
✓ Branch 270 → 271 taken 74 times.
|
175 | while (!stop_token.stop_requested()) |
| 663 | { | ||
| 664 | 101 | pending.clear(); | |
| 665 | 101 | key_cache.reset(); | |
| 666 |
3/4✓ Branch 6 → 7 taken 61 times.
✓ Branch 6 → 9 taken 40 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 61 times.
|
101 | const bool process_focused = !m_require_focus.load(std::memory_order_relaxed) || is_process_foreground(); |
| 667 | |||
| 668 | // Lazily install the active-input hooks the current bindings need. Each call is idempotent and fails | ||
| 669 | // cheaply until its target (a loaded xinput module / the game window) becomes available, so this also | ||
| 670 | // handles a target that appears after the poller starts. | ||
| 671 |
2/6✗ Branch 12 → 13 not taken.
✓ Branch 12 → 16 taken 101 times.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 101 times.
|
101 | if (m_has_consume_gamepad_bindings.load(std::memory_order_relaxed) && !detail::xinput_installed()) |
| 672 | { | ||
| 673 | ✗ | (void)detail::install_xinput(m_gamepad_index); | |
| 674 | } | ||
| 675 |
6/6✓ Branch 20 → 21 taken 6 times.
✓ Branch 20 → 24 taken 95 times.
✓ Branch 22 → 23 taken 2 times.
✓ Branch 22 → 24 taken 4 times.
✓ Branch 25 → 26 taken 2 times.
✓ Branch 25 → 27 taken 99 times.
|
101 | if (m_has_wheel_bindings.load(std::memory_order_relaxed) && !detail::wndproc_installed()) |
| 676 | { | ||
| 677 | 2 | (void)detail::install_wndproc(); | |
| 678 | } | ||
| 679 | |||
| 680 | // Snapshot the wheel notches the window-procedure hook accumulated into a per-cycle pulse mask, so every | ||
| 681 | // binding this cycle reads a consistent value and each notch maps to exactly one Press edge. The counters | ||
| 682 | // are drained even when unfocused so a background notch is discarded rather than queued to fire on the next | ||
| 683 | // focus. | ||
| 684 | 101 | uint8_t wheel_pulse_mask = 0; | |
| 685 |
2/2✓ Branch 28 → 29 taken 6 times.
✓ Branch 28 → 33 taken 95 times.
|
101 | if (m_has_wheel_bindings.load(std::memory_order_relaxed)) |
| 686 | { | ||
| 687 | 6 | const auto taken = detail::take_wheel_counts(); | |
| 688 | 6 | detail::add_wheel_notches(wheel_pulse, taken); | |
| 689 | 6 | wheel_pulse_mask = detail::step_wheel_pulse(wheel_pulse); | |
| 690 | } | ||
| 691 | |||
| 692 | // Drive the wheel-swallow flag every cycle, outside the wheel-binding guard above, so it disarms on the | ||
| 693 | // first cycle after the last consume wheel binding is removed at runtime: the window-procedure subclass | ||
| 694 | // stays installed until shutdown, so a stale true would keep eating the game's wheel forever (the gamepad | ||
| 695 | // mask self-heals via its TTL, but the wheel flag has none). Gate it on focus to mirror the gamepad mask | ||
| 696 | // clear | ||
| 697 | // below: a backgrounded mod must not swallow the focused app's wheel. | ||
| 698 |
4/4✓ Branch 33 → 34 taken 40 times.
✓ Branch 33 → 37 taken 61 times.
✓ Branch 35 → 36 taken 1 time.
✓ Branch 35 → 37 taken 39 times.
|
101 | detail::set_wheel_consume(process_focused && m_has_wheel_consume_bindings.load(std::memory_order_relaxed)); |
| 699 | |||
| 700 | // Digital gamepad button bits claimed by active consume chords this cycle, accumulated in the binding loop | ||
| 701 | // and published afterwards. | ||
| 702 | 101 | uint16_t gamepad_owned = 0; | |
| 703 | |||
| 704 | // Poll gamepad state once per cycle when connected, into the hoisted gamepad_state buffer. When | ||
| 705 | // disconnected, throttle reconnection attempts to avoid the per-cycle overhead of XInputGetState on empty | ||
| 706 | // slots. Read through the saved trampoline when the suppression hook is installed so the poll observes the | ||
| 707 | // true, unmasked controller state rather than its own published mask. A successful poll overwrites the | ||
| 708 | // whole struct, and gamepad_state is read only when gamepad_connected is true, so a stale buffer is never | ||
| 709 | // observed. | ||
| 710 | 101 | bool gamepad_connected = false; | |
| 711 |
4/6✓ Branch 40 → 41 taken 23 times.
✓ Branch 40 → 43 taken 78 times.
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 43 taken 23 times.
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 62 taken 101 times.
|
101 | if (m_has_gamepad_bindings.load(std::memory_order_relaxed) && process_focused) |
| 712 | { | ||
| 713 | ✗ | const auto now = std::chrono::steady_clock::now(); | |
| 714 | ✗ | if (gamepad_was_connected || (now - last_gamepad_poll) >= gamepad_reconnect_interval) | |
| 715 | { | ||
| 716 | ✗ | last_gamepad_poll = now; | |
| 717 | ✗ | const detail::XInputGetStateFn xinput_original = detail::xinput_trampoline(); | |
| 718 | const DWORD xinput_result = | ||
| 719 | (xinput_original != nullptr) | ||
| 720 | ✗ | ? xinput_original(static_cast<DWORD>(m_gamepad_index), &gamepad_state) | |
| 721 | ✗ | : XInputGetState(static_cast<DWORD>(m_gamepad_index), &gamepad_state); | |
| 722 | ✗ | gamepad_was_connected = xinput_result == ERROR_SUCCESS; | |
| 723 | } | ||
| 724 | ✗ | gamepad_connected = gamepad_was_connected; | |
| 725 | } | ||
| 726 | |||
| 727 | // Stage this cycle's edge callbacks, then dispatch them after releasing the binding lock so user code can | ||
| 728 | // call back into update_binding_combos() without deadlocking. Growing the staging vector can allocate, and | ||
| 729 | // copying each entry's name/std::function can allocate or run a throwing target copy constructor. The whole | ||
| 730 | // staging pass runs under one catch so a failed callback batch is dropped instead of escaping the jthread | ||
| 731 | // body and calling std::terminate. m_active_states may then reflect a partial pass, but the next cycle | ||
| 732 | // re-evaluates from the live physical input, so at most one cycle of edge callbacks is missed under | ||
| 733 | // sustained failure. | ||
| 734 | try | ||
| 735 | { | ||
| 736 | // Re-reserve to the current binding count before taking the evaluation lock. add_binding can grow | ||
| 737 | // m_bindings past the startup capacity while the poller runs, so without this the per-cycle push_back | ||
| 738 | // could reallocate the staging vector while the shared lock is held. Reading the count under a short | ||
| 739 | // reader lock and reserving after releasing it keeps that growth allocation out of the evaluation | ||
| 740 | // critical section; the catch above still covers the residual race where a concurrent add_binding | ||
| 741 | // grows the set again before the evaluation lock is taken. | ||
| 742 | 101 | size_t reserve_hint = 0; | |
| 743 | { | ||
| 744 | 101 | std::shared_lock count_lock(m_bindings_rw_mutex); | |
| 745 | 101 | reserve_hint = m_bindings.size(); | |
| 746 | 101 | } | |
| 747 |
1/2✓ Branch 65 → 66 taken 101 times.
✗ Branch 65 → 302 not taken.
|
101 | pending.reserve(reserve_hint); |
| 748 | |||
| 749 | 101 | std::shared_lock lock(m_bindings_rw_mutex); | |
| 750 | 101 | const size_t count = m_bindings.size(); | |
| 751 | 101 | const auto &known_mods = m_known_modifiers; | |
| 752 | |||
| 753 |
2/2✓ Branch 228 → 69 taken 1171 times.
✓ Branch 228 → 229 taken 100 times.
|
1271 | for (size_t i = 0; i < count; ++i) |
| 754 | { | ||
| 755 | 1171 | const auto &binding = m_bindings[i]; | |
| 756 |
2/2✓ Branch 71 → 72 taken 4 times.
✓ Branch 71 → 73 taken 1167 times.
|
1171 | if (binding.keys.empty()) |
| 757 | { | ||
| 758 | 4 | continue; | |
| 759 | } | ||
| 760 | |||
| 761 | 1167 | bool any_pressed = false; | |
| 762 | |||
| 763 |
2/2✓ Branch 73 → 74 taken 1089 times.
✓ Branch 73 → 155 taken 78 times.
|
1167 | if (process_focused) |
| 764 | { | ||
| 765 | 1089 | bool modifiers_held = true; | |
| 766 |
1/2✗ Branch 90 → 76 not taken.
✓ Branch 90 → 91 taken 1089 times.
|
2178 | for (const auto &mod : binding.modifiers) |
| 767 | { | ||
| 768 | ✗ | if (!is_code_pressed(mod, key_cache, gamepad_state, gamepad_connected, trigger_thresh, | |
| 769 | stick_thresh, wheel_pulse_mask)) | ||
| 770 | { | ||
| 771 | ✗ | modifiers_held = false; | |
| 772 | ✗ | break; | |
| 773 | } | ||
| 774 | } | ||
| 775 | |||
| 776 |
1/2✓ Branch 91 → 92 taken 1089 times.
✗ Branch 91 → 129 not taken.
|
1089 | if (modifiers_held) |
| 777 | { | ||
| 778 | // Strict matching: reject if any known modifier that is | ||
| 779 | // NOT in this binding's required set is currently held. | ||
| 780 |
1/2✗ Branch 127 → 94 not taken.
✓ Branch 127 → 128 taken 1089 times.
|
2178 | for (const auto &km : known_mods) |
| 781 | { | ||
| 782 | ✗ | if (!is_code_pressed(km, key_cache, gamepad_state, gamepad_connected, trigger_thresh, | |
| 783 | stick_thresh, wheel_pulse_mask)) | ||
| 784 | { | ||
| 785 | ✗ | continue; | |
| 786 | } | ||
| 787 | ✗ | bool is_required = false; | |
| 788 | ✗ | for (const auto &mod : binding.modifiers) | |
| 789 | { | ||
| 790 | ✗ | if (modifier_satisfies(mod, km)) | |
| 791 | { | ||
| 792 | ✗ | is_required = true; | |
| 793 | ✗ | break; | |
| 794 | } | ||
| 795 | } | ||
| 796 | ✗ | if (!is_required) | |
| 797 | { | ||
| 798 | ✗ | modifiers_held = false; | |
| 799 | ✗ | break; | |
| 800 | } | ||
| 801 | } | ||
| 802 | } | ||
| 803 | |||
| 804 |
1/2✓ Branch 129 → 130 taken 1089 times.
✗ Branch 129 → 155 not taken.
|
1089 | if (modifiers_held) |
| 805 | { | ||
| 806 |
2/2✓ Branch 153 → 132 taken 1089 times.
✓ Branch 153 → 154 taken 1087 times.
|
3265 | for (const auto &key : binding.keys) |
| 807 | { | ||
| 808 | const bool key_pressed = | ||
| 809 | 1089 | is_code_pressed(key, key_cache, gamepad_state, gamepad_connected, trigger_thresh, | |
| 810 | stick_thresh, wheel_pulse_mask); | ||
| 811 | |||
| 812 | // Pre-arm the consume bit while the binding's modifiers are held, before the trigger | ||
| 813 | // button itself is pressed. The suppression mask is published one poll cycle behind the | ||
| 814 | // physical state, so claiming the bit only once the trigger reads as pressed lets the | ||
| 815 | // game's own XInput poll (an independent clock, usually faster than this loop) catch | ||
| 816 | // the trigger's leading edge before the mask catches up -- a one-frame leak of an | ||
| 817 | // otherwise-suppressed press. Holding the claim to the modifier keeps the mask up | ||
| 818 | // before the trigger arrives. Masking a bit whose physical button is still up is a | ||
| 819 | // no-op: apply_suppress ANDs ~mask into wButtons, and clearing an already-zero bit | ||
| 820 | // changes nothing. The consume-until-release latch still trails the trigger, so the | ||
| 821 | // trailing edge is unchanged. Keep scanning the remaining keys so every owned bit is | ||
| 822 | // collected. | ||
| 823 |
3/6✓ Branch 135 → 136 taken 1 time.
✓ Branch 135 → 140 taken 1088 times.
✗ Branch 136 → 137 not taken.
✓ Branch 136 → 140 taken 1 time.
✗ Branch 137 → 138 not taken.
✗ Branch 137 → 140 not taken.
|
1089 | if (binding.consume && key.source == InputSource::Gamepad && key.code > 0 && |
| 824 | ✗ | key.code < GamepadCode::LeftTrigger) | |
| 825 | { | ||
| 826 | ✗ | gamepad_owned = | |
| 827 | ✗ | static_cast<uint16_t>(gamepad_owned | static_cast<uint16_t>(key.code)); | |
| 828 | } | ||
| 829 | |||
| 830 | // Activation still keys off the real press: a non-consume binding fires on the first | ||
| 831 | // pressed key and stops, while a consume binding keeps scanning so the pre-arm above | ||
| 832 | // sees every owned bit. | ||
| 833 |
2/2✓ Branch 140 → 141 taken 1087 times.
✓ Branch 140 → 142 taken 2 times.
|
1089 | if (!key_pressed) |
| 834 | { | ||
| 835 | 1087 | continue; | |
| 836 | } | ||
| 837 | 2 | any_pressed = true; | |
| 838 |
1/2✓ Branch 142 → 143 taken 2 times.
✗ Branch 142 → 144 not taken.
|
2 | if (!binding.consume) |
| 839 | { | ||
| 840 | 2 | break; | |
| 841 | } | ||
| 842 | } | ||
| 843 | } | ||
| 844 | } | ||
| 845 | |||
| 846 | 1167 | const bool was_active = m_active_states[i].load(std::memory_order_relaxed) != 0; | |
| 847 | |||
| 848 |
2/3✓ Branch 163 → 164 taken 1148 times.
✓ Branch 163 → 196 taken 19 times.
✗ Branch 163 → 227 not taken.
|
1167 | switch (binding.mode) |
| 849 | { | ||
| 850 | 1148 | case InputMode::Press: | |
| 851 | { | ||
| 852 |
6/8✓ Branch 164 → 165 taken 2 times.
✓ Branch 164 → 169 taken 1146 times.
✓ Branch 165 → 166 taken 2 times.
✗ Branch 165 → 169 not taken.
✓ Branch 167 → 168 taken 2 times.
✗ Branch 167 → 169 not taken.
✓ Branch 170 → 171 taken 2 times.
✓ Branch 170 → 183 taken 1146 times.
|
1148 | if (any_pressed && !was_active && binding.on_press) |
| 853 | { | ||
| 854 | 3 | pending.push_back({binding.name, binding.on_press, {}, false}); | |
| 855 | } | ||
| 856 |
2/2✓ Branch 184 → 185 taken 1 time.
✓ Branch 184 → 186 taken 1146 times.
|
1147 | m_active_states[i].store(any_pressed ? 1 : 0, std::memory_order_relaxed); |
| 857 | 1147 | break; | |
| 858 | } | ||
| 859 | 19 | case InputMode::Hold: | |
| 860 | { | ||
| 861 |
2/6✗ Branch 196 → 197 not taken.
✓ Branch 196 → 200 taken 19 times.
✗ Branch 198 → 199 not taken.
✗ Branch 198 → 200 not taken.
✗ Branch 201 → 202 not taken.
✓ Branch 201 → 214 taken 19 times.
|
19 | if (any_pressed != was_active && binding.on_state_change) |
| 862 | { | ||
| 863 | ✗ | pending.push_back({binding.name, {}, binding.on_state_change, any_pressed}); | |
| 864 | } | ||
| 865 |
1/2✗ Branch 215 → 216 not taken.
✓ Branch 215 → 217 taken 19 times.
|
19 | m_active_states[i].store(any_pressed ? 1 : 0, std::memory_order_relaxed); |
| 866 | 19 | break; | |
| 867 | } | ||
| 868 | } | ||
| 869 | } | ||
| 870 | 101 | } | |
| 871 | 1 | catch (...) | |
| 872 | { | ||
| 873 | // Staging this cycle's edge callbacks failed. Drop the partial callback list (the shared lock has | ||
| 874 | // already been released by stack unwinding, so there is no deadlock) rather than terminating the poll | ||
| 875 | // thread. The gamepad suppression publish below still runs from whatever was accumulated, and | ||
| 876 | // self-heals next cycle. | ||
| 877 | 1 | pending.clear(); | |
| 878 |
1/2✓ Branch 306 → 307 taken 1 time.
✗ Branch 306 → 309 not taken.
|
1 | (void)Logger::get_instance().try_log( |
| 879 | LogLevel::Error, "InputPoller: failed staging poll-cycle callbacks; callbacks skipped"); | ||
| 880 |
1/2✓ Branch 308 → 231 taken 1 time.
✗ Branch 308 → 331 not taken.
|
1 | } |
| 881 | |||
| 882 | // Publish the gamepad suppression mask for the XInput detour. The consume-until-release latch keeps a | ||
| 883 | // trigger masked until the physical button is released plus a grace window, so releasing the modifier | ||
| 884 | // before the trigger cannot leak a bare trigger to the game. When unfocused or disconnected, clear the | ||
| 885 | // latch and mask so the game keeps its input while the mod is in the background. | ||
| 886 |
1/2✗ Branch 232 → 233 not taken.
✓ Branch 232 → 243 taken 101 times.
|
101 | if (m_has_consume_gamepad_bindings.load(std::memory_order_relaxed)) |
| 887 | { | ||
| 888 | ✗ | if (process_focused && gamepad_connected) | |
| 889 | { | ||
| 890 | const uint16_t suppress = | ||
| 891 | ✗ | detail::step_gamepad_suppress(gp_suppress, gamepad_owned, gamepad_state.Gamepad.wButtons, | |
| 892 | GetTickCount64(), GAMEPAD_SUPPRESS_GRACE_MS); | ||
| 893 | ✗ | detail::publish_gamepad_suppress(suppress); | |
| 894 | // Enable the detour's rule masking only while focused and connected. The published rule list and | ||
| 895 | // its time-to-live survive focus changes, so the detour needs this explicit gate to stop masking | ||
| 896 | // the foreground game's input once the mod is backgrounded, exactly as the reactive mask is cleared | ||
| 897 | // below and as the wheel-consume flag is gated. | ||
| 898 | ✗ | detail::set_gamepad_rule_suppress_enabled(true); | |
| 899 | ✗ | } | |
| 900 | else | ||
| 901 | { | ||
| 902 | ✗ | gp_suppress = detail::GamepadSuppressState{}; | |
| 903 | ✗ | detail::publish_gamepad_suppress(0); | |
| 904 | ✗ | detail::set_gamepad_rule_suppress_enabled(false); | |
| 905 | } | ||
| 906 | } | ||
| 907 | |||
| 908 |
2/2✓ Branch 262 → 245 taken 1 time.
✓ Branch 262 → 263 taken 101 times.
|
203 | for (auto &callback : pending) |
| 909 | { | ||
| 910 | try | ||
| 911 | { | ||
| 912 |
1/2✓ Branch 248 → 249 taken 1 time.
✗ Branch 248 → 250 not taken.
|
1 | if (callback.on_press) |
| 913 | { | ||
| 914 |
1/2✓ Branch 249 → 253 taken 1 time.
✗ Branch 249 → 311 not taken.
|
1 | callback.on_press(); |
| 915 | } | ||
| 916 | ✗ | else if (callback.on_state_change) | |
| 917 | { | ||
| 918 | ✗ | callback.on_state_change(callback.hold_value); | |
| 919 | } | ||
| 920 | } | ||
| 921 | ✗ | catch (const std::exception &e) | |
| 922 | { | ||
| 923 | ✗ | (void)Logger::get_instance().try_log( | |
| 924 | ✗ | LogLevel::Error, "InputPoller: Exception in callback \"{}\": {}", callback.name, e.what()); | |
| 925 | ✗ | } | |
| 926 | ✗ | catch (...) | |
| 927 | { | ||
| 928 | ✗ | (void)Logger::get_instance().try_log( | |
| 929 | ✗ | LogLevel::Error, "InputPoller: Unknown exception in callback \"{}\"", callback.name); | |
| 930 | ✗ | } | |
| 931 | } | ||
| 932 | |||
| 933 |
1/2✓ Branch 263 → 264 taken 101 times.
✗ Branch 263 → 331 not taken.
|
101 | std::unique_lock lock(m_cv_mutex); |
| 934 |
1/2✓ Branch 265 → 266 taken 101 times.
✗ Branch 265 → 326 not taken.
|
298 | m_cv.wait_for(lock, stop_token, m_poll_interval, [&stop_token]() { return stop_token.stop_requested(); }); |
| 935 | 101 | } | |
| 936 |
8/36✓ Branch 171 → 172 taken 2 times.
✗ Branch 171 → 287 not taken.
✓ Branch 172 → 173 taken 1 time.
✓ Branch 172 → 284 taken 1 time.
✓ Branch 174 → 175 taken 1 time.
✗ Branch 174 → 276 not taken.
✗ Branch 176 → 177 not taken.
✓ Branch 176 → 178 taken 1 time.
✗ Branch 178 → 179 not taken.
✓ Branch 178 → 180 taken 1 time.
✗ Branch 180 → 181 not taken.
✓ Branch 180 → 182 taken 1 time.
✗ Branch 202 → 203 not taken.
✗ Branch 202 → 299 not taken.
✗ Branch 204 → 205 not taken.
✗ Branch 204 → 293 not taken.
✗ Branch 205 → 206 not taken.
✗ Branch 205 → 288 not taken.
✗ Branch 207 → 208 not taken.
✗ Branch 207 → 209 not taken.
✗ Branch 209 → 210 not taken.
✗ Branch 209 → 211 not taken.
✗ Branch 211 → 212 not taken.
✗ Branch 211 → 213 not taken.
✗ Branch 278 → 279 not taken.
✗ Branch 278 → 280 not taken.
✗ Branch 281 → 282 not taken.
✗ Branch 281 → 283 not taken.
✓ Branch 284 → 285 taken 1 time.
✗ Branch 284 → 286 not taken.
✗ Branch 290 → 291 not taken.
✗ Branch 290 → 292 not taken.
✗ Branch 293 → 294 not taken.
✗ Branch 293 → 295 not taken.
✗ Branch 296 → 297 not taken.
✗ Branch 296 → 298 not taken.
|
77 | } |
| 937 | |||
| 938 | 2003 | bool InputPoller::update_combos(std::string_view name, const Config::KeyComboList &combos) noexcept | |
| 939 | { | ||
| 940 | 2003 | std::vector<std::function<void(bool)>> hold_release_callbacks; | |
| 941 | 2003 | std::vector<std::string> hold_release_names; | |
| 942 | |||
| 943 | try | ||
| 944 | { | ||
| 945 |
1/2✓ Branch 2 → 3 taken 2003 times.
✗ Branch 2 → 276 not taken.
|
2003 | std::unique_lock lock(m_bindings_rw_mutex); |
| 946 |
1/2✓ Branch 3 → 4 taken 2003 times.
✗ Branch 3 → 274 not taken.
|
2003 | const auto it = m_name_index.find(name); |
| 947 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 11 taken 2003 times.
|
2003 | if (it == m_name_index.end()) |
| 948 | { | ||
| 949 | // Release the writer lock before logging so the emit does not run inside the critical section | ||
| 950 | // (deferred-logging convention). | ||
| 951 | ✗ | lock.unlock(); | |
| 952 | ✗ | (void)Logger::get_instance().try_log( | |
| 953 | LogLevel::Debug, "InputPoller: update_combos(\"{}\") ignored: name not found", name); | ||
| 954 | ✗ | return false; | |
| 955 | } | ||
| 956 | |||
| 957 |
1/2✓ Branch 12 → 13 taken 2003 times.
✗ Branch 12 → 274 not taken.
|
2003 | std::vector<size_t> indices = it->second; |
| 958 |
1/2✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 2003 times.
|
2003 | if (indices.empty()) |
| 959 | { | ||
| 960 | ✗ | return false; | |
| 961 | } | ||
| 962 | |||
| 963 | // Cardinality-preserving fast path: in-place rewrite of keys and modifiers leaves m_bindings and | ||
| 964 | // m_active_states in lockstep. The poll thread's binding-evaluation pass and every other reader | ||
| 965 | // (is_binding_active, binding_count) hold the shared lock, so the unique_lock here serializes against | ||
| 966 | // them; concurrent is_binding_active(size_t) reads stay valid because the binding count and array | ||
| 967 | // sizes do not change. | ||
| 968 |
2/2✓ Branch 18 → 19 taken 1335 times.
✓ Branch 18 → 49 taken 668 times.
|
2003 | if (indices.size() == combos.size()) |
| 969 | { | ||
| 970 | 1335 | std::vector<InputBinding> replacements; | |
| 971 |
1/2✓ Branch 20 → 21 taken 1335 times.
✗ Branch 20 → 246 not taken.
|
1335 | replacements.reserve(indices.size()); |
| 972 |
2/2✓ Branch 35 → 22 taken 1335 times.
✓ Branch 35 → 36 taken 1335 times.
|
2670 | for (size_t i = 0; i < indices.size(); ++i) |
| 973 | { | ||
| 974 | 1335 | const size_t idx = indices[i]; | |
| 975 |
1/2✓ Branch 24 → 25 taken 1335 times.
✗ Branch 24 → 245 not taken.
|
1335 | InputBinding binding = m_bindings[idx]; |
| 976 |
1/2✓ Branch 26 → 27 taken 1335 times.
✗ Branch 26 → 243 not taken.
|
1335 | binding.keys = combos[i].keys; |
| 977 |
1/2✓ Branch 28 → 29 taken 1335 times.
✗ Branch 28 → 243 not taken.
|
1335 | binding.modifiers = combos[i].modifiers; |
| 978 |
1/2✓ Branch 31 → 32 taken 1335 times.
✗ Branch 31 → 243 not taken.
|
1335 | replacements.push_back(std::move(binding)); |
| 979 | 1335 | } | |
| 980 |
2/2✓ Branch 45 → 37 taken 1335 times.
✓ Branch 45 → 46 taken 1335 times.
|
2670 | for (size_t i = 0; i < indices.size(); ++i) |
| 981 | { | ||
| 982 | 2670 | m_bindings[indices[i]] = std::move(replacements[i]); | |
| 983 | } | ||
| 984 | 1335 | recompute_modifier_caches_locked(); | |
| 985 | 1335 | return true; | |
| 986 | 1335 | } | |
| 987 | |||
| 988 | // Cardinality change requires rebuilding the bindings vector and the parallel m_active_states array. | ||
| 989 | // Capture the prototype from the first existing entry so callback identity, mode, and name stay stable | ||
| 990 | // across the rebuild. | ||
| 991 |
1/2✓ Branch 51 → 52 taken 668 times.
✗ Branch 51 → 272 not taken.
|
668 | InputBinding prototype = m_bindings[indices.front()]; |
| 992 |
1/2✓ Branch 54 → 55 taken 668 times.
✗ Branch 54 → 270 not taken.
|
668 | std::sort(indices.begin(), indices.end()); |
| 993 | |||
| 994 |
2/2✓ Branch 56 → 57 taken 333 times.
✓ Branch 56 → 58 taken 335 times.
|
668 | const size_t append_count = combos.empty() ? 1 : combos.size(); |
| 995 | 668 | const size_t new_size = m_bindings.size() - indices.size() + append_count; | |
| 996 | |||
| 997 | // Phase 1 -- allocate everything that can throw without yet touching m_bindings. If any allocation fails | ||
| 998 | // the poller is left exactly as it was. The appended entries are prototype copies (the copy is the throwing | ||
| 999 | // step); an empty replacement yields a single inert sentinel so the name stays addressable for a later | ||
| 1000 | // non-empty update (without it the bound -> unbound -> bound INI hot-reload cycle would break with "name | ||
| 1001 | // not found"). | ||
| 1002 | 668 | std::vector<InputBinding> appended; | |
| 1003 |
1/2✓ Branch 61 → 62 taken 668 times.
✗ Branch 61 → 268 not taken.
|
668 | appended.reserve(append_count); |
| 1004 |
2/2✓ Branch 63 → 64 taken 333 times.
✓ Branch 63 → 72 taken 335 times.
|
668 | if (combos.empty()) |
| 1005 | { | ||
| 1006 |
1/2✓ Branch 64 → 65 taken 333 times.
✗ Branch 64 → 251 not taken.
|
333 | InputBinding sentinel = prototype; |
| 1007 | 333 | sentinel.keys.clear(); | |
| 1008 | 333 | sentinel.modifiers.clear(); | |
| 1009 |
1/2✓ Branch 69 → 70 taken 333 times.
✗ Branch 69 → 249 not taken.
|
333 | appended.push_back(std::move(sentinel)); |
| 1010 | 333 | } | |
| 1011 | else | ||
| 1012 | { | ||
| 1013 |
2/2✓ Branch 92 → 74 taken 669 times.
✓ Branch 92 → 93 taken 335 times.
|
1339 | for (const auto &combo : combos) |
| 1014 | { | ||
| 1015 |
1/2✓ Branch 76 → 77 taken 669 times.
✗ Branch 76 → 254 not taken.
|
669 | InputBinding binding = prototype; |
| 1016 |
1/2✓ Branch 77 → 78 taken 669 times.
✗ Branch 77 → 252 not taken.
|
669 | binding.keys = combo.keys; |
| 1017 |
1/2✓ Branch 78 → 79 taken 669 times.
✗ Branch 78 → 252 not taken.
|
669 | binding.modifiers = combo.modifiers; |
| 1018 |
1/2✓ Branch 81 → 82 taken 669 times.
✗ Branch 81 → 252 not taken.
|
669 | appended.push_back(std::move(binding)); |
| 1019 | 669 | } | |
| 1020 | } | ||
| 1021 | |||
| 1022 | 668 | std::vector<InputBinding> rebuilt; | |
| 1023 |
1/2✓ Branch 94 → 95 taken 668 times.
✗ Branch 94 → 266 not taken.
|
668 | rebuilt.reserve(new_size); |
| 1024 | 668 | std::vector<uint8_t> rebuilt_states; | |
| 1025 |
1/2✓ Branch 95 → 96 taken 668 times.
✗ Branch 95 → 264 not taken.
|
668 | rebuilt_states.reserve(new_size); |
| 1026 |
1/2✓ Branch 96 → 97 taken 668 times.
✗ Branch 96 → 264 not taken.
|
668 | auto new_states = std::make_unique<std::atomic<uint8_t>[]>(new_size); |
| 1027 | |||
| 1028 | // Capture release callbacks for any held entries this update drops. Without this, a register_hold consumer | ||
| 1029 | // whose combo cardinality changes via INI hot-reload would latch in the held state forever because the | ||
| 1030 | // underlying entry vanishes before the next poll tick. | ||
| 1031 |
2/2✓ Branch 131 → 99 taken 1002 times.
✓ Branch 131 → 132 taken 668 times.
|
2338 | for (size_t idx : indices) |
| 1032 | { | ||
| 1033 | 1002 | if (m_active_states[idx].load(std::memory_order_relaxed) != 0 && | |
| 1034 |
2/8✗ Branch 109 → 110 not taken.
✓ Branch 109 → 116 taken 1002 times.
✗ Branch 111 → 112 not taken.
✗ Branch 111 → 116 not taken.
✗ Branch 114 → 115 not taken.
✗ Branch 114 → 116 not taken.
✗ Branch 117 → 118 not taken.
✓ Branch 117 → 122 taken 1002 times.
|
1002 | m_bindings[idx].mode == InputMode::Hold && m_bindings[idx].on_state_change) |
| 1035 | { | ||
| 1036 | ✗ | hold_release_callbacks.push_back(m_bindings[idx].on_state_change); | |
| 1037 | ✗ | hold_release_names.push_back(m_bindings[idx].name); | |
| 1038 | } | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | // Phase 2 -- commit. Every operation below is non-throwing: the reserved vectors never reallocate, | ||
| 1042 | // InputBinding moves are noexcept, and the atomic stores and container move-assignments do not allocate. | ||
| 1043 | // Surviving entries carry their prior atomic state across | ||
| 1044 | // the swap so a held binding does not momentarily report inactive; | ||
| 1045 | // appended entries start at zero (no prior state to inherit). | ||
| 1046 | 668 | size_t cursor = 0; | |
| 1047 |
2/2✓ Branch 161 → 134 taken 1002 times.
✓ Branch 161 → 162 taken 668 times.
|
2338 | for (size_t skip : indices) |
| 1048 | { | ||
| 1049 |
1/2✗ Branch 151 → 137 not taken.
✓ Branch 151 → 152 taken 1002 times.
|
1002 | for (size_t i = cursor; i < skip; ++i) |
| 1050 | { | ||
| 1051 | ✗ | rebuilt_states.push_back(m_active_states[i].load(std::memory_order_relaxed)); | |
| 1052 | ✗ | rebuilt.push_back(std::move(m_bindings[i])); | |
| 1053 | } | ||
| 1054 | 1002 | cursor = skip + 1; | |
| 1055 | } | ||
| 1056 |
1/2✗ Branch 178 → 163 not taken.
✓ Branch 178 → 179 taken 668 times.
|
668 | for (size_t i = cursor; i < m_bindings.size(); ++i) |
| 1057 | { | ||
| 1058 | ✗ | rebuilt_states.push_back(m_active_states[i].load(std::memory_order_relaxed)); | |
| 1059 | ✗ | rebuilt.push_back(std::move(m_bindings[i])); | |
| 1060 | } | ||
| 1061 |
2/2✓ Branch 196 → 181 taken 1002 times.
✓ Branch 196 → 197 taken 668 times.
|
3340 | for (auto &binding : appended) |
| 1062 | { | ||
| 1063 |
1/2✓ Branch 185 → 186 taken 1002 times.
✗ Branch 185 → 261 not taken.
|
1002 | rebuilt.push_back(std::move(binding)); |
| 1064 |
1/2✓ Branch 186 → 187 taken 1002 times.
✗ Branch 186 → 260 not taken.
|
1002 | rebuilt_states.push_back(0); |
| 1065 | } | ||
| 1066 | |||
| 1067 |
2/2✓ Branch 210 → 198 taken 1002 times.
✓ Branch 210 → 211 taken 668 times.
|
1670 | for (size_t i = 0; i < rebuilt_states.size(); ++i) |
| 1068 | { | ||
| 1069 | 1002 | new_states[i].store(rebuilt_states[i], std::memory_order_relaxed); | |
| 1070 | } | ||
| 1071 | |||
| 1072 | 668 | m_bindings = std::move(rebuilt); | |
| 1073 | 668 | m_active_states = std::move(new_states); | |
| 1074 | 668 | recompute_modifier_caches_locked(); | |
| 1075 |
4/4✓ Branch 225 → 226 taken 668 times.
✓ Branch 225 → 227 taken 1335 times.
✓ Branch 229 → 230 taken 668 times.
✓ Branch 229 → 232 taken 1335 times.
|
3338 | } |
| 1076 | ✗ | catch (...) | |
| 1077 | { | ||
| 1078 | // Out of memory during the rebuild. update_combos is noexcept; the poller is left unchanged (Phase 1 | ||
| 1079 | // allocates before any mutation) and no release callbacks are fired. | ||
| 1080 | ✗ | (void)Logger::get_instance().try_log(LogLevel::Error, | |
| 1081 | "InputPoller: out of memory in update_combos; combos unchanged"); | ||
| 1082 | ✗ | return false; | |
| 1083 | ✗ | } | |
| 1084 | |||
| 1085 | // Fire the captured release callbacks outside the writer lock so user code may safely call back into the | ||
| 1086 | // InputManager (matching the remove_bindings_by_name pattern). This path runs in response to a user-driven INI | ||
| 1087 | // reshape, never from a DllMain detach, so synchronous callback dispatch is safe here. | ||
| 1088 |
1/2✗ Branch 237 → 233 not taken.
✓ Branch 237 → 238 taken 668 times.
|
668 | for (size_t i = 0; i < hold_release_callbacks.size(); ++i) |
| 1089 | { | ||
| 1090 | try | ||
| 1091 | { | ||
| 1092 | ✗ | hold_release_callbacks[i](false); | |
| 1093 | } | ||
| 1094 | ✗ | catch (const std::exception &e) | |
| 1095 | { | ||
| 1096 | ✗ | (void)Logger::get_instance().try_log(LogLevel::Error, | |
| 1097 | "InputPoller: Exception in hold release callback \"{}\": {}", | ||
| 1098 | ✗ | hold_release_names[i], e.what()); | |
| 1099 | ✗ | } | |
| 1100 | ✗ | catch (...) | |
| 1101 | { | ||
| 1102 | ✗ | (void)Logger::get_instance().try_log(LogLevel::Error, | |
| 1103 | "InputPoller: Unknown exception in hold release callback \"{}\"", | ||
| 1104 | ✗ | hold_release_names[i]); | |
| 1105 | ✗ | } | |
| 1106 | } | ||
| 1107 | |||
| 1108 | 668 | return true; | |
| 1109 | 2003 | } | |
| 1110 | |||
| 1111 | 310 | void InputPoller::add_binding(InputBinding binding) noexcept | |
| 1112 | { | ||
| 1113 | 310 | std::unique_lock lock(m_bindings_rw_mutex); | |
| 1114 | |||
| 1115 | 310 | const size_t old_count = m_bindings.size(); | |
| 1116 | 310 | const size_t new_count = old_count + 1; | |
| 1117 | |||
| 1118 | try | ||
| 1119 | { | ||
| 1120 | // Build the replacement state array before mutating m_bindings so an allocation failure leaves the binding | ||
| 1121 | // vector and the state array at their prior, matching sizes. The poll thread indexes m_active_states by | ||
| 1122 | // binding position, so a size mismatch would be an out-of-bounds read. Seed each surviving slot from the | ||
| 1123 | // existing atomic value (relaxed is sufficient under the writer lock) so a held binding does not flicker | ||
| 1124 | // through a one-tick "inactive" blip. | ||
| 1125 |
1/2✓ Branch 4 → 5 taken 310 times.
✗ Branch 4 → 47 not taken.
|
310 | auto new_states = std::make_unique<std::atomic<uint8_t>[]>(new_count); |
| 1126 |
2/2✓ Branch 24 → 6 taken 45162 times.
✓ Branch 24 → 25 taken 310 times.
|
45472 | for (size_t i = 0; i < old_count; ++i) |
| 1127 | { | ||
| 1128 | 90324 | new_states[i].store(m_active_states[i].load(std::memory_order_relaxed), std::memory_order_relaxed); | |
| 1129 | } | ||
| 1130 | 310 | new_states[old_count].store(0, std::memory_order_relaxed); | |
| 1131 | |||
| 1132 | // push_back has the strong guarantee (InputBinding moves are noexcept), so if a reallocation fails here | ||
| 1133 | // m_bindings is unchanged and the new_states array is simply discarded. Only after it succeeds do the | ||
| 1134 | // non-throwing commits below run. | ||
| 1135 |
1/2✓ Branch 36 → 37 taken 310 times.
✗ Branch 36 → 45 not taken.
|
620 | m_bindings.push_back(std::move(binding)); |
| 1136 | 310 | m_active_states = std::move(new_states); | |
| 1137 | 310 | recompute_modifier_caches_locked(); | |
| 1138 | 310 | } | |
| 1139 | ✗ | catch (...) | |
| 1140 | { | ||
| 1141 | // Out of memory growing the poller. add_binding is noexcept and reachable from teardown, so the binding is | ||
| 1142 | // dropped (the poller is left exactly as it was) rather than terminating the host. | ||
| 1143 | ✗ | (void)Logger::get_instance().try_log(LogLevel::Error, | |
| 1144 | "InputPoller: out of memory in add_binding; binding not added"); | ||
| 1145 | ✗ | } | |
| 1146 | 310 | } | |
| 1147 | |||
| 1148 | 3 | size_t InputPoller::remove_bindings_by_name(std::string_view name, bool invoke_callbacks) noexcept | |
| 1149 | { | ||
| 1150 | 3 | std::vector<std::function<void(bool)>> hold_release_callbacks; | |
| 1151 | 3 | std::vector<std::string> hold_release_names; | |
| 1152 | 3 | size_t removed = 0; | |
| 1153 | |||
| 1154 | try | ||
| 1155 | { | ||
| 1156 |
1/2✓ Branch 2 → 3 taken 3 times.
✗ Branch 2 → 166 not taken.
|
3 | std::unique_lock lock(m_bindings_rw_mutex); |
| 1157 |
1/2✓ Branch 3 → 4 taken 3 times.
✗ Branch 3 → 164 not taken.
|
3 | const auto it = m_name_index.find(name); |
| 1158 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 3 times.
|
3 | if (it == m_name_index.end()) |
| 1159 | { | ||
| 1160 | ✗ | return 0; | |
| 1161 | } | ||
| 1162 | |||
| 1163 |
1/2✓ Branch 9 → 10 taken 3 times.
✗ Branch 9 → 164 not taken.
|
3 | std::vector<size_t> indices = it->second; |
| 1164 |
1/2✓ Branch 12 → 13 taken 3 times.
✗ Branch 12 → 162 not taken.
|
3 | std::sort(indices.begin(), indices.end()); |
| 1165 | |||
| 1166 | // Capture release callbacks for active hold bindings before erasure; fire them after the lock is released | ||
| 1167 | // so user code is free to call back into the InputManager. The Bootstrap unload path passes | ||
| 1168 | // invoke_callbacks=false to skip this step because the user callbacks live in a Logic DLL whose code pages | ||
| 1169 | // may be about to be unmapped. | ||
| 1170 |
1/2✓ Branch 13 → 14 taken 3 times.
✗ Branch 13 → 50 not taken.
|
3 | if (invoke_callbacks) |
| 1171 | { | ||
| 1172 |
2/2✓ Branch 48 → 16 taken 3 times.
✓ Branch 48 → 49 taken 3 times.
|
9 | for (size_t idx : indices) |
| 1173 | { | ||
| 1174 | 3 | if (m_active_states[idx].load(std::memory_order_relaxed) != 0 && | |
| 1175 |
2/8✗ Branch 26 → 27 not taken.
✓ Branch 26 → 33 taken 3 times.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 33 not taken.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 39 taken 3 times.
|
3 | m_bindings[idx].mode == InputMode::Hold && m_bindings[idx].on_state_change) |
| 1176 | { | ||
| 1177 | ✗ | hold_release_callbacks.push_back(m_bindings[idx].on_state_change); | |
| 1178 | ✗ | hold_release_names.push_back(m_bindings[idx].name); | |
| 1179 | } | ||
| 1180 | } | ||
| 1181 | } | ||
| 1182 | |||
| 1183 | // Build a flat skip-mask so the new m_active_states slot for every surviving binding inherits its prior | ||
| 1184 | // atomic value. Without this a held binding would briefly report inactive after the reshape, breaking | ||
| 1185 | // register_hold consumers that observe the state through is_binding_active(size_t). | ||
| 1186 |
1/2✓ Branch 53 → 54 taken 3 times.
✗ Branch 53 → 147 not taken.
|
3 | std::vector<bool> drop(m_bindings.size(), false); |
| 1187 |
2/2✓ Branch 70 → 57 taken 3 times.
✓ Branch 70 → 71 taken 3 times.
|
9 | for (size_t idx : indices) |
| 1188 | { | ||
| 1189 | 3 | drop[idx] = true; | |
| 1190 | } | ||
| 1191 | 3 | const size_t survivor_count = m_bindings.size() - indices.size(); | |
| 1192 | 3 | std::vector<uint8_t> carried; | |
| 1193 |
1/2✓ Branch 73 → 74 taken 3 times.
✗ Branch 73 → 158 not taken.
|
3 | carried.reserve(survivor_count); |
| 1194 |
2/2✓ Branch 90 → 75 taken 7 times.
✓ Branch 90 → 91 taken 3 times.
|
10 | for (size_t i = 0; i < m_bindings.size(); ++i) |
| 1195 | { | ||
| 1196 |
2/2✓ Branch 77 → 78 taken 4 times.
✓ Branch 77 → 88 taken 3 times.
|
7 | if (!drop[i]) |
| 1197 | { | ||
| 1198 |
1/2✓ Branch 86 → 87 taken 4 times.
✗ Branch 86 → 151 not taken.
|
8 | carried.push_back(m_active_states[i].load(std::memory_order_relaxed)); |
| 1199 | } | ||
| 1200 | } | ||
| 1201 | |||
| 1202 | // Allocate the replacement state array before erasing any binding so an allocation failure leaves | ||
| 1203 | // m_bindings and m_active_states at their prior, matching sizes (the poll thread indexes m_active_states by | ||
| 1204 | // position; a mismatch would be an out-of-bounds read). | ||
| 1205 |
1/2✓ Branch 91 → 92 taken 3 times.
✗ Branch 91 → 158 not taken.
|
3 | auto new_states = std::make_unique<std::atomic<uint8_t>[]>(survivor_count); |
| 1206 |
2/2✓ Branch 105 → 93 taken 4 times.
✓ Branch 105 → 106 taken 3 times.
|
7 | for (size_t i = 0; i < carried.size(); ++i) |
| 1207 | { | ||
| 1208 | 4 | new_states[i].store(carried[i], std::memory_order_relaxed); | |
| 1209 | } | ||
| 1210 | |||
| 1211 | // Commit. erase moves survivors down via InputBinding's noexcept move-assignment and the array swap does | ||
| 1212 | // not allocate, so the reshape past this point cannot fail. | ||
| 1213 |
2/2✓ Branch 120 → 107 taken 3 times.
✓ Branch 120 → 121 taken 3 times.
|
6 | for (auto idx_it = indices.rbegin(); idx_it != indices.rend(); ++idx_it) |
| 1214 | { | ||
| 1215 |
1/2✓ Branch 116 → 117 taken 3 times.
✗ Branch 116 → 152 not taken.
|
9 | m_bindings.erase(m_bindings.begin() + static_cast<std::ptrdiff_t>(*idx_it)); |
| 1216 | } | ||
| 1217 | 3 | m_active_states = std::move(new_states); | |
| 1218 | 3 | removed = indices.size(); | |
| 1219 | |||
| 1220 | 3 | recompute_modifier_caches_locked(); | |
| 1221 |
1/2✓ Branch 132 → 133 taken 3 times.
✗ Branch 132 → 135 not taken.
|
3 | } |
| 1222 | ✗ | catch (...) | |
| 1223 | { | ||
| 1224 | // Out of memory preparing the reshape. remove_bindings_by_name is noexcept and reachable from teardown; the | ||
| 1225 | // poller is left unchanged (allocation precedes erasure) and no callbacks are fired. | ||
| 1226 | ✗ | (void)Logger::get_instance().try_log( | |
| 1227 | LogLevel::Error, "InputPoller: out of memory in remove_bindings_by_name; bindings unchanged"); | ||
| 1228 | ✗ | return 0; | |
| 1229 | ✗ | } | |
| 1230 | |||
| 1231 |
1/2✗ Branch 140 → 136 not taken.
✓ Branch 140 → 141 taken 3 times.
|
3 | for (size_t i = 0; i < hold_release_callbacks.size(); ++i) |
| 1232 | { | ||
| 1233 | try | ||
| 1234 | { | ||
| 1235 | ✗ | hold_release_callbacks[i](false); | |
| 1236 | } | ||
| 1237 | ✗ | catch (const std::exception &e) | |
| 1238 | { | ||
| 1239 | ✗ | (void)Logger::get_instance().try_log(LogLevel::Error, | |
| 1240 | "InputPoller: Exception in hold release callback \"{}\": {}", | ||
| 1241 | ✗ | hold_release_names[i], e.what()); | |
| 1242 | ✗ | } | |
| 1243 | ✗ | catch (...) | |
| 1244 | { | ||
| 1245 | ✗ | (void)Logger::get_instance().try_log(LogLevel::Error, | |
| 1246 | "InputPoller: Unknown exception in hold release callback \"{}\"", | ||
| 1247 | ✗ | hold_release_names[i]); | |
| 1248 | ✗ | } | |
| 1249 | } | ||
| 1250 | |||
| 1251 | 3 | return removed; | |
| 1252 | 3 | } | |
| 1253 | |||
| 1254 | 3 | void InputPoller::clear_bindings(bool invoke_callbacks) noexcept | |
| 1255 | { | ||
| 1256 | 3 | std::vector<std::pair<std::function<void(bool)>, std::string>> hold_releases; | |
| 1257 | |||
| 1258 | try | ||
| 1259 | { | ||
| 1260 |
1/2✓ Branch 2 → 3 taken 3 times.
✗ Branch 2 → 69 not taken.
|
3 | std::unique_lock lock(m_bindings_rw_mutex); |
| 1261 | // Skip the release-callback capture entirely on the loader-lock path (Bootstrap::on_logic_dll_unload_all). | ||
| 1262 | // Running user callbacks under loader lock is unsafe because the Logic DLL hosting those callbacks may be | ||
| 1263 | // in the middle of being unmapped, and any callback that touches Win32 LoadLibrary family or a peer | ||
| 1264 | // DllMain's mutex would deadlock. | ||
| 1265 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 28 taken 1 time.
|
3 | if (invoke_callbacks) |
| 1266 | { | ||
| 1267 |
2/2✓ Branch 27 → 5 taken 3 times.
✓ Branch 27 → 28 taken 2 times.
|
5 | for (size_t i = 0; i < m_bindings.size(); ++i) |
| 1268 | { | ||
| 1269 | 3 | if (m_active_states[i].load(std::memory_order_relaxed) != 0 && | |
| 1270 |
2/8✗ Branch 13 → 14 not taken.
✓ Branch 13 → 20 taken 3 times.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 20 not taken.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 25 taken 3 times.
|
3 | m_bindings[i].mode == InputMode::Hold && m_bindings[i].on_state_change) |
| 1271 | { | ||
| 1272 | ✗ | hold_releases.emplace_back(m_bindings[i].on_state_change, m_bindings[i].name); | |
| 1273 | } | ||
| 1274 | } | ||
| 1275 | } | ||
| 1276 | |||
| 1277 | // Allocate the empty replacement state array before clearing so an allocation failure leaves the poller | ||
| 1278 | // untouched. The clears, atomic stores, rule publish, and array swap below do not allocate. | ||
| 1279 |
1/2✓ Branch 28 → 29 taken 3 times.
✗ Branch 28 → 67 not taken.
|
3 | auto new_states = std::make_unique<std::atomic<uint8_t>[]>(0); |
| 1280 | |||
| 1281 | 3 | m_bindings.clear(); | |
| 1282 | 3 | m_name_index.clear(); | |
| 1283 | 3 | m_known_modifiers.clear(); | |
| 1284 | // clear_bindings does not route through recompute_modifier_caches_locked, so advance the generation here so | ||
| 1285 | // outstanding BindingTokens fail closed once the binding set is emptied. | ||
| 1286 | 3 | m_binding_generation = next_binding_generation(); | |
| 1287 | 3 | m_has_gamepad_bindings.store(false, std::memory_order_relaxed); | |
| 1288 | 3 | m_has_wheel_bindings.store(false, std::memory_order_relaxed); | |
| 1289 | 3 | m_has_consume_gamepad_bindings.store(false, std::memory_order_relaxed); | |
| 1290 | 3 | m_has_wheel_consume_bindings.store(false, std::memory_order_relaxed); | |
| 1291 | 3 | detail::publish_gamepad_consume_rules(nullptr, 0); | |
| 1292 | 3 | m_active_states = std::move(new_states); | |
| 1293 | 3 | } | |
| 1294 | ✗ | catch (...) | |
| 1295 | { | ||
| 1296 | ✗ | (void)Logger::get_instance().try_log(LogLevel::Error, | |
| 1297 | "InputPoller: out of memory in clear_bindings; bindings unchanged"); | ||
| 1298 | ✗ | return; | |
| 1299 | ✗ | } | |
| 1300 | |||
| 1301 |
1/2✗ Branch 59 → 45 not taken.
✓ Branch 59 → 60 taken 3 times.
|
6 | for (auto &[callback, name] : hold_releases) |
| 1302 | { | ||
| 1303 | try | ||
| 1304 | { | ||
| 1305 | ✗ | callback(false); | |
| 1306 | } | ||
| 1307 | ✗ | catch (const std::exception &e) | |
| 1308 | { | ||
| 1309 | ✗ | (void)Logger::get_instance().try_log( | |
| 1310 | ✗ | LogLevel::Error, "InputPoller: Exception in hold release callback \"{}\": {}", name, e.what()); | |
| 1311 | ✗ | } | |
| 1312 | ✗ | catch (...) | |
| 1313 | { | ||
| 1314 | ✗ | (void)Logger::get_instance().try_log( | |
| 1315 | LogLevel::Error, "InputPoller: Unknown exception in hold release callback \"{}\"", name); | ||
| 1316 | ✗ | } | |
| 1317 | } | ||
| 1318 |
1/2✓ Branch 62 → 63 taken 3 times.
✗ Branch 62 → 65 not taken.
|
3 | } |
| 1319 | |||
| 1320 | 74 | void InputPoller::release_active_holds() noexcept | |
| 1321 | { | ||
| 1322 |
2/2✓ Branch 31 → 3 taken 398 times.
✓ Branch 31 → 32 taken 74 times.
|
472 | for (size_t i = 0; i < m_bindings.size(); ++i) |
| 1323 | { | ||
| 1324 |
2/2✓ Branch 11 → 12 taken 1 time.
✓ Branch 11 → 29 taken 397 times.
|
796 | if (m_active_states[i].load(std::memory_order_relaxed) != 0) |
| 1325 | { | ||
| 1326 | 1 | m_active_states[i].store(0, std::memory_order_relaxed); | |
| 1327 | |||
| 1328 | 1 | const auto &binding = m_bindings[i]; | |
| 1329 |
2/6✗ Branch 22 → 23 not taken.
✓ Branch 22 → 26 taken 1 time.
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 26 not taken.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 1 time.
|
1 | if (binding.mode == InputMode::Hold && binding.on_state_change) |
| 1330 | { | ||
| 1331 | try | ||
| 1332 | { | ||
| 1333 | ✗ | binding.on_state_change(false); | |
| 1334 | } | ||
| 1335 | ✗ | catch (const std::exception &e) | |
| 1336 | { | ||
| 1337 | ✗ | (void)Logger::get_instance().try_log( | |
| 1338 | ✗ | LogLevel::Error, "InputPoller: Exception in hold release callback \"{}\": {}", binding.name, | |
| 1339 | ✗ | e.what()); | |
| 1340 | ✗ | } | |
| 1341 | ✗ | catch (...) | |
| 1342 | { | ||
| 1343 | ✗ | (void)Logger::get_instance().try_log( | |
| 1344 | LogLevel::Error, "InputPoller: Unknown exception in hold release callback \"{}\"", | ||
| 1345 | ✗ | binding.name); | |
| 1346 | ✗ | } | |
| 1347 | } | ||
| 1348 | } | ||
| 1349 | } | ||
| 1350 | 74 | } | |
| 1351 | |||
| 1352 | 61 | bool InputPoller::is_process_foreground() const noexcept | |
| 1353 | { | ||
| 1354 | 61 | HWND foreground = GetForegroundWindow(); | |
| 1355 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 61 times.
|
61 | if (!foreground) |
| 1356 | { | ||
| 1357 | ✗ | return false; | |
| 1358 | } | ||
| 1359 | 61 | DWORD foreground_pid = 0; | |
| 1360 | 61 | GetWindowThreadProcessId(foreground, &foreground_pid); | |
| 1361 | 61 | return foreground_pid == GetCurrentProcessId(); | |
| 1362 | } | ||
| 1363 | |||
| 1364 | // --- InputManager --- | ||
| 1365 | |||
| 1366 | 561 | void InputManager::register_press(std::string_view name, const std::vector<InputCode> &keys, | |
| 1367 | std::function<void()> callback) | ||
| 1368 | { | ||
| 1369 |
1/2✓ Branch 6 → 7 taken 561 times.
✗ Branch 6 → 10 not taken.
|
561 | register_press(name, keys, {}, std::move(callback)); |
| 1370 | 561 | } | |
| 1371 | |||
| 1372 | 595 | void InputManager::register_press(std::string_view name, const std::vector<InputCode> &keys, | |
| 1373 | const std::vector<InputCode> &modifiers, std::function<void()> callback) | ||
| 1374 | { | ||
| 1375 | 595 | std::shared_ptr<InputPoller> live_poller; | |
| 1376 | 595 | InputBinding binding; | |
| 1377 |
1/2✓ Branch 5 → 6 taken 594 times.
✗ Branch 5 → 44 not taken.
|
594 | binding.name = std::string{name}; |
| 1378 |
1/2✓ Branch 9 → 10 taken 594 times.
✗ Branch 9 → 51 not taken.
|
594 | binding.keys = keys; |
| 1379 |
1/2✓ Branch 10 → 11 taken 593 times.
✗ Branch 10 → 51 not taken.
|
594 | binding.modifiers = modifiers; |
| 1380 | 593 | binding.mode = InputMode::Press; | |
| 1381 | 593 | binding.on_press = std::move(callback); | |
| 1382 | |||
| 1383 | { | ||
| 1384 |
1/2✓ Branch 14 → 15 taken 595 times.
✗ Branch 14 → 50 not taken.
|
592 | std::lock_guard lock(m_mutex); |
| 1385 |
2/2✓ Branch 16 → 17 taken 307 times.
✓ Branch 16 → 18 taken 288 times.
|
595 | if (m_poller) |
| 1386 | { | ||
| 1387 | 307 | live_poller = m_poller; | |
| 1388 | } | ||
| 1389 | else | ||
| 1390 | { | ||
| 1391 |
1/2✓ Branch 20 → 21 taken 288 times.
✗ Branch 20 → 48 not taken.
|
576 | m_pending_bindings.push_back(std::move(binding)); |
| 1392 | 288 | return; | |
| 1393 | } | ||
| 1394 |
2/2✓ Branch 24 → 25 taken 307 times.
✓ Branch 24 → 33 taken 288 times.
|
595 | } |
| 1395 | |||
| 1396 | // Forward outside the InputManager mutex so the poller's exclusive m_bindings_rw_mutex acquisition cannot AB/BA | ||
| 1397 | // against any caller already holding m_mutex. | ||
| 1398 | 614 | live_poller->add_binding(std::move(binding)); | |
| 1399 |
4/4✓ Branch 35 → 36 taken 307 times.
✓ Branch 35 → 37 taken 288 times.
✓ Branch 39 → 40 taken 307 times.
✓ Branch 39 → 42 taken 288 times.
|
883 | } |
| 1400 | |||
| 1401 | 10 | void InputManager::register_hold(std::string_view name, const std::vector<InputCode> &keys, | |
| 1402 | std::function<void(bool)> callback) | ||
| 1403 | { | ||
| 1404 |
1/2✓ Branch 6 → 7 taken 10 times.
✗ Branch 6 → 10 not taken.
|
10 | register_hold(name, keys, {}, std::move(callback)); |
| 1405 | 10 | } | |
| 1406 | |||
| 1407 | 26 | void InputManager::register_hold(std::string_view name, const std::vector<InputCode> &keys, | |
| 1408 | const std::vector<InputCode> &modifiers, std::function<void(bool)> callback) | ||
| 1409 | { | ||
| 1410 | 26 | std::shared_ptr<InputPoller> live_poller; | |
| 1411 | 26 | InputBinding binding; | |
| 1412 |
1/2✓ Branch 5 → 6 taken 26 times.
✗ Branch 5 → 44 not taken.
|
26 | binding.name = std::string{name}; |
| 1413 |
1/2✓ Branch 9 → 10 taken 26 times.
✗ Branch 9 → 51 not taken.
|
26 | binding.keys = keys; |
| 1414 |
1/2✓ Branch 10 → 11 taken 26 times.
✗ Branch 10 → 51 not taken.
|
26 | binding.modifiers = modifiers; |
| 1415 | 26 | binding.mode = InputMode::Hold; | |
| 1416 | 26 | binding.on_state_change = std::move(callback); | |
| 1417 | |||
| 1418 | { | ||
| 1419 |
1/2✓ Branch 14 → 15 taken 26 times.
✗ Branch 14 → 50 not taken.
|
26 | std::lock_guard lock(m_mutex); |
| 1420 |
2/2✓ Branch 16 → 17 taken 2 times.
✓ Branch 16 → 18 taken 24 times.
|
26 | if (m_poller) |
| 1421 | { | ||
| 1422 | 2 | live_poller = m_poller; | |
| 1423 | } | ||
| 1424 | else | ||
| 1425 | { | ||
| 1426 |
1/2✓ Branch 20 → 21 taken 24 times.
✗ Branch 20 → 48 not taken.
|
48 | m_pending_bindings.push_back(std::move(binding)); |
| 1427 | 24 | return; | |
| 1428 | } | ||
| 1429 |
2/2✓ Branch 24 → 25 taken 2 times.
✓ Branch 24 → 33 taken 24 times.
|
26 | } |
| 1430 | |||
| 1431 | 4 | live_poller->add_binding(std::move(binding)); | |
| 1432 |
4/4✓ Branch 35 → 36 taken 2 times.
✓ Branch 35 → 37 taken 24 times.
✓ Branch 39 → 40 taken 2 times.
✓ Branch 39 → 42 taken 24 times.
|
50 | } |
| 1433 | |||
| 1434 | 23 | void InputManager::register_press(std::string_view name, const Config::KeyComboList &combos, | |
| 1435 | std::function<void()> callback) | ||
| 1436 | { | ||
| 1437 | // An empty combo list still has to register the binding name so a later update_binding_combos() can attach a | ||
| 1438 | // real combo. Without this the for-each loop produces zero bindings, the name never lands in | ||
| 1439 | // m_pending_bindings, and the INI-driven update silently fails with "name not found". | ||
| 1440 |
2/2✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 14 taken 18 times.
|
23 | if (combos.empty()) |
| 1441 | { | ||
| 1442 |
1/2✓ Branch 9 → 10 taken 5 times.
✗ Branch 9 → 33 not taken.
|
5 | register_press(name, std::vector<InputCode>{}, std::vector<InputCode>{}, std::move(callback)); |
| 1443 | 5 | return; | |
| 1444 | } | ||
| 1445 |
2/2✓ Branch 30 → 16 taken 22 times.
✓ Branch 30 → 31 taken 18 times.
|
58 | for (const auto &combo : combos) |
| 1446 | { | ||
| 1447 |
2/4✓ Branch 18 → 19 taken 22 times.
✗ Branch 18 → 44 not taken.
✓ Branch 19 → 20 taken 22 times.
✗ Branch 19 → 42 not taken.
|
22 | register_press(name, combo.keys, combo.modifiers, callback); |
| 1448 | } | ||
| 1449 | } | ||
| 1450 | |||
| 1451 | 10 | void InputManager::register_hold(std::string_view name, const Config::KeyComboList &combos, | |
| 1452 | std::function<void(bool)> callback) | ||
| 1453 | { | ||
| 1454 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 14 taken 9 times.
|
10 | if (combos.empty()) |
| 1455 | { | ||
| 1456 |
1/2✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 33 not taken.
|
1 | register_hold(name, std::vector<InputCode>{}, std::vector<InputCode>{}, std::move(callback)); |
| 1457 | 1 | return; | |
| 1458 | } | ||
| 1459 |
2/2✓ Branch 30 → 16 taken 12 times.
✓ Branch 30 → 31 taken 9 times.
|
30 | for (const auto &combo : combos) |
| 1460 | { | ||
| 1461 |
2/4✓ Branch 18 → 19 taken 12 times.
✗ Branch 18 → 44 not taken.
✓ Branch 19 → 20 taken 12 times.
✗ Branch 19 → 42 not taken.
|
12 | register_hold(name, combo.keys, combo.modifiers, callback); |
| 1462 | } | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | 79 | void InputManager::set_require_focus(bool require_focus) | |
| 1466 | { | ||
| 1467 |
1/2✓ Branch 2 → 3 taken 79 times.
✗ Branch 2 → 9 not taken.
|
79 | std::lock_guard lock(m_mutex); |
| 1468 | 79 | m_require_focus = require_focus; | |
| 1469 |
2/2✓ Branch 4 → 5 taken 6 times.
✓ Branch 4 → 7 taken 73 times.
|
79 | if (m_poller) |
| 1470 | { | ||
| 1471 | 6 | m_poller->set_require_focus(require_focus); | |
| 1472 | } | ||
| 1473 | 79 | } | |
| 1474 | |||
| 1475 | 12 | void InputManager::set_consume(std::string_view name, bool consume) noexcept | |
| 1476 | { | ||
| 1477 | 12 | std::shared_ptr<InputPoller> live_poller; | |
| 1478 | |||
| 1479 | { | ||
| 1480 | 12 | std::lock_guard lock(m_mutex); | |
| 1481 |
2/2✓ Branch 4 → 5 taken 4 times.
✓ Branch 4 → 6 taken 8 times.
|
12 | if (m_poller) |
| 1482 | { | ||
| 1483 | 4 | live_poller = m_poller; | |
| 1484 | } | ||
| 1485 | else | ||
| 1486 | { | ||
| 1487 |
2/2✓ Branch 23 → 8 taken 8 times.
✓ Branch 23 → 24 taken 8 times.
|
24 | for (auto &binding : m_pending_bindings) |
| 1488 | { | ||
| 1489 |
2/2✓ Branch 12 → 13 taken 7 times.
✓ Branch 12 → 14 taken 1 time.
|
8 | if (binding.name == name) |
| 1490 | { | ||
| 1491 | 7 | binding.consume = consume; | |
| 1492 | } | ||
| 1493 | } | ||
| 1494 | 8 | return; | |
| 1495 | } | ||
| 1496 |
2/2✓ Branch 27 → 28 taken 4 times.
✓ Branch 27 → 32 taken 8 times.
|
12 | } |
| 1497 | |||
| 1498 | // Forward outside the InputManager mutex so the poller's exclusive m_bindings_rw_mutex acquisition cannot | ||
| 1499 | // deadlock against a caller already holding m_mutex (matches register_press / register_hold). | ||
| 1500 | 4 | live_poller->set_consume(name, consume); | |
| 1501 |
2/2✓ Branch 34 → 35 taken 4 times.
✓ Branch 34 → 37 taken 8 times.
|
12 | } |
| 1502 | |||
| 1503 | 1 | void InputManager::set_gamepad_index(int index) | |
| 1504 | { | ||
| 1505 |
1/2✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 10 not taken.
|
1 | std::lock_guard lock(m_mutex); |
| 1506 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 6 not taken.
|
1 | m_gamepad_index = std::clamp(index, 0, 3); |
| 1507 | 1 | } | |
| 1508 | |||
| 1509 | 1 | void InputManager::set_trigger_threshold(int threshold) | |
| 1510 | { | ||
| 1511 |
1/2✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 10 not taken.
|
1 | std::lock_guard lock(m_mutex); |
| 1512 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 6 not taken.
|
1 | m_trigger_threshold = std::clamp(threshold, 0, 255); |
| 1513 | 1 | } | |
| 1514 | |||
| 1515 | 1 | void InputManager::set_stick_threshold(int threshold) | |
| 1516 | { | ||
| 1517 |
1/2✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 10 not taken.
|
1 | std::lock_guard lock(m_mutex); |
| 1518 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 6 not taken.
|
1 | m_stick_threshold = std::clamp(threshold, 0, 32767); |
| 1519 | 1 | } | |
| 1520 | |||
| 1521 | 42 | void InputManager::start(std::chrono::milliseconds poll_interval) | |
| 1522 | { | ||
| 1523 |
1/2✓ Branch 2 → 3 taken 42 times.
✗ Branch 2 → 62 not taken.
|
42 | std::lock_guard lock(m_mutex); |
| 1524 | |||
| 1525 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 8 taken 41 times.
|
42 | if (m_poller) |
| 1526 | { | ||
| 1527 |
2/4✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 60 not taken.
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 51 not taken.
|
1 | Logger::get_instance().debug("InputManager: start() called while already running; no-op."); |
| 1528 | 1 | return; | |
| 1529 | } | ||
| 1530 | |||
| 1531 |
2/2✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 11 taken 40 times.
|
41 | if (m_pending_bindings.empty()) |
| 1532 | { | ||
| 1533 | 1 | return; | |
| 1534 | } | ||
| 1535 | |||
| 1536 |
1/2✓ Branch 11 → 12 taken 40 times.
✗ Branch 11 → 60 not taken.
|
40 | Logger &logger = Logger::get_instance(); |
| 1537 |
1/2✓ Branch 14 → 15 taken 40 times.
✗ Branch 14 → 52 not taken.
|
40 | logger.info("InputManager: Starting with {} binding(s), poll interval {}ms", m_pending_bindings.size(), |
| 1538 | 40 | poll_interval.count()); | |
| 1539 | |||
| 1540 |
2/2✓ Branch 31 → 17 taken 52 times.
✓ Branch 31 → 32 taken 40 times.
|
132 | for (const auto &binding : m_pending_bindings) |
| 1541 | { | ||
| 1542 |
1/2✓ Branch 21 → 22 taken 52 times.
✗ Branch 21 → 55 not taken.
|
52 | logger.trace("InputManager: Registered {} binding \"{}\" with {} key(s)", |
| 1543 | 104 | input_mode_to_string(binding.mode), binding.name, binding.keys.size()); | |
| 1544 | } | ||
| 1545 | |||
| 1546 |
1/2✓ Branch 34 → 35 taken 40 times.
✗ Branch 34 → 59 not taken.
|
80 | m_poller = std::make_shared<InputPoller>(std::move(m_pending_bindings), poll_interval, m_require_focus, |
| 1547 | 80 | m_gamepad_index, m_trigger_threshold, m_stick_threshold); | |
| 1548 | 40 | m_pending_bindings.clear(); | |
| 1549 |
1/2✓ Branch 39 → 40 taken 40 times.
✗ Branch 39 → 60 not taken.
|
40 | m_poller->start(); |
| 1550 | 40 | m_active_poller.store(m_poller, std::memory_order_release); | |
| 1551 | 40 | m_running.store(true, std::memory_order_release); | |
| 1552 |
2/2✓ Branch 46 → 47 taken 40 times.
✓ Branch 46 → 49 taken 2 times.
|
42 | } |
| 1553 | |||
| 1554 | 36 | bool InputManager::is_running() const noexcept | |
| 1555 | { | ||
| 1556 | 36 | return m_running.load(std::memory_order_acquire); | |
| 1557 | } | ||
| 1558 | |||
| 1559 | 65000 | size_t InputManager::binding_count() const noexcept | |
| 1560 | { | ||
| 1561 | 65000 | std::shared_ptr<InputPoller> live_poller; | |
| 1562 | { | ||
| 1563 | 65000 | std::lock_guard lock(m_mutex); | |
| 1564 |
2/2✓ Branch 4 → 5 taken 65 times.
✓ Branch 4 → 7 taken 67938 times.
|
68003 | if (!m_poller) |
| 1565 | { | ||
| 1566 | 65 | return m_pending_bindings.size(); | |
| 1567 | } | ||
| 1568 | 67938 | live_poller = m_poller; | |
| 1569 |
2/2✓ Branch 10 → 11 taken 67875 times.
✓ Branch 10 → 14 taken 63 times.
|
68003 | } |
| 1570 | 67875 | return live_poller->binding_count(); | |
| 1571 | 67576 | } | |
| 1572 | |||
| 1573 | 66722 | bool InputManager::is_binding_active(std::string_view name) const noexcept | |
| 1574 | { | ||
| 1575 | 66722 | auto active_poller = m_active_poller.load(std::memory_order_acquire); | |
| 1576 |
2/2✓ Branch 4 → 5 taken 67879 times.
✓ Branch 4 → 7 taken 6 times.
|
67912 | if (active_poller) |
| 1577 | { | ||
| 1578 | 67879 | return active_poller->is_binding_active(name); | |
| 1579 | } | ||
| 1580 | 6 | return false; | |
| 1581 | 66493 | } | |
| 1582 | |||
| 1583 | 7 | BindingToken InputManager::acquire_binding_token(std::string_view name) const noexcept | |
| 1584 | { | ||
| 1585 | 7 | auto active_poller = m_active_poller.load(std::memory_order_acquire); | |
| 1586 |
2/2✓ Branch 4 → 5 taken 6 times.
✓ Branch 4 → 7 taken 1 time.
|
7 | if (active_poller) |
| 1587 | { | ||
| 1588 | 6 | return active_poller->acquire_binding_token(name); | |
| 1589 | } | ||
| 1590 | 1 | return BindingToken{}; | |
| 1591 | 7 | } | |
| 1592 | |||
| 1593 | 6 | bool InputManager::is_binding_active(const BindingToken &token) const noexcept | |
| 1594 | { | ||
| 1595 | 6 | auto active_poller = m_active_poller.load(std::memory_order_acquire); | |
| 1596 |
2/2✓ Branch 4 → 5 taken 5 times.
✓ Branch 4 → 7 taken 1 time.
|
6 | if (active_poller) |
| 1597 | { | ||
| 1598 | 5 | return active_poller->is_binding_active(token); | |
| 1599 | } | ||
| 1600 | 1 | return false; | |
| 1601 | 6 | } | |
| 1602 | |||
| 1603 | 11 | bool InputManager::binding_token_current(const BindingToken &token) const noexcept | |
| 1604 | { | ||
| 1605 | 11 | auto active_poller = m_active_poller.load(std::memory_order_acquire); | |
| 1606 |
2/2✓ Branch 4 → 5 taken 10 times.
✓ Branch 4 → 7 taken 1 time.
|
11 | if (active_poller) |
| 1607 | { | ||
| 1608 | 10 | return active_poller->binding_token_current(token); | |
| 1609 | } | ||
| 1610 | 1 | return false; | |
| 1611 | 11 | } | |
| 1612 | |||
| 1613 | 2036 | void InputManager::update_binding_combos(std::string_view name, const Config::KeyComboList &combos) noexcept | |
| 1614 | { | ||
| 1615 | 2036 | std::shared_ptr<InputPoller> local_poller; | |
| 1616 | 2036 | bool updated_pending = false; | |
| 1617 | |||
| 1618 | try | ||
| 1619 | { | ||
| 1620 |
1/2✓ Branch 2 → 3 taken 2036 times.
✗ Branch 2 → 208 not taken.
|
2036 | std::unique_lock lock(m_mutex); |
| 1621 |
2/2✓ Branch 4 → 5 taken 2002 times.
✓ Branch 4 → 6 taken 34 times.
|
2036 | if (m_poller) |
| 1622 | { | ||
| 1623 | 2002 | local_poller = m_poller; | |
| 1624 | } | ||
| 1625 | else | ||
| 1626 | { | ||
| 1627 | 34 | std::vector<size_t> indices; | |
| 1628 |
1/2✓ Branch 7 → 8 taken 34 times.
✗ Branch 7 → 203 not taken.
|
34 | indices.reserve(m_pending_bindings.size()); |
| 1629 |
2/2✓ Branch 16 → 9 taken 22 times.
✓ Branch 16 → 17 taken 34 times.
|
56 | for (size_t i = 0; i < m_pending_bindings.size(); ++i) |
| 1630 | { | ||
| 1631 |
1/2✓ Branch 12 → 13 taken 22 times.
✗ Branch 12 → 14 not taken.
|
22 | if (m_pending_bindings[i].name == name) |
| 1632 | { | ||
| 1633 |
1/2✓ Branch 13 → 14 taken 22 times.
✗ Branch 13 → 180 not taken.
|
22 | indices.push_back(i); |
| 1634 | } | ||
| 1635 | } | ||
| 1636 |
2/2✓ Branch 18 → 19 taken 15 times.
✓ Branch 18 → 23 taken 19 times.
|
34 | if (indices.empty()) |
| 1637 | { | ||
| 1638 | // Release the lock before logging so the emit does not run inside the critical section | ||
| 1639 | // (deferred-logging convention). | ||
| 1640 |
1/2✓ Branch 19 → 20 taken 15 times.
✗ Branch 19 → 203 not taken.
|
15 | lock.unlock(); |
| 1641 |
1/2✓ Branch 20 → 21 taken 15 times.
✗ Branch 20 → 203 not taken.
|
15 | (void)Logger::get_instance().try_log( |
| 1642 | LogLevel::Debug, "InputManager: update_binding_combos(\"{}\") ignored: name not found", name); | ||
| 1643 | 15 | return; | |
| 1644 | } | ||
| 1645 | |||
| 1646 |
2/2✓ Branch 25 → 26 taken 11 times.
✓ Branch 25 → 55 taken 8 times.
|
19 | if (indices.size() == combos.size()) |
| 1647 | { | ||
| 1648 | 11 | std::vector<InputBinding> replacements; | |
| 1649 |
1/2✓ Branch 27 → 28 taken 11 times.
✗ Branch 27 → 184 not taken.
|
11 | replacements.reserve(indices.size()); |
| 1650 |
2/2✓ Branch 42 → 29 taken 11 times.
✓ Branch 42 → 43 taken 11 times.
|
22 | for (size_t i = 0; i < indices.size(); ++i) |
| 1651 | { | ||
| 1652 |
1/2✓ Branch 31 → 32 taken 11 times.
✗ Branch 31 → 183 not taken.
|
11 | InputBinding binding = m_pending_bindings[indices[i]]; |
| 1653 |
1/2✓ Branch 33 → 34 taken 11 times.
✗ Branch 33 → 181 not taken.
|
11 | binding.keys = combos[i].keys; |
| 1654 |
1/2✓ Branch 35 → 36 taken 11 times.
✗ Branch 35 → 181 not taken.
|
11 | binding.modifiers = combos[i].modifiers; |
| 1655 |
1/2✓ Branch 38 → 39 taken 11 times.
✗ Branch 38 → 181 not taken.
|
11 | replacements.push_back(std::move(binding)); |
| 1656 | 11 | } | |
| 1657 |
2/2✓ Branch 52 → 44 taken 11 times.
✓ Branch 52 → 53 taken 11 times.
|
22 | for (size_t i = 0; i < indices.size(); ++i) |
| 1658 | { | ||
| 1659 | 22 | m_pending_bindings[indices[i]] = std::move(replacements[i]); | |
| 1660 | } | ||
| 1661 | 11 | updated_pending = true; | |
| 1662 | 11 | } | |
| 1663 | else | ||
| 1664 | { | ||
| 1665 |
1/2✓ Branch 57 → 58 taken 8 times.
✗ Branch 57 → 202 not taken.
|
8 | InputBinding prototype = m_pending_bindings[indices.front()]; |
| 1666 |
1/2✓ Branch 60 → 61 taken 8 times.
✗ Branch 60 → 200 not taken.
|
8 | std::sort(indices.begin(), indices.end()); |
| 1667 | |||
| 1668 | // Build the replacement entries (prototype copies are the throwing step) and reserve the rebuilt | ||
| 1669 | // vector before moving any survivor out of m_pending_bindings, so an allocation failure leaves the | ||
| 1670 | // pending list untouched. An empty replacement keeps one inert sentinel so the name stays | ||
| 1671 | // addressable for a later non-empty update. | ||
| 1672 |
2/2✓ Branch 62 → 63 taken 4 times.
✓ Branch 62 → 64 taken 4 times.
|
8 | const size_t append_count = combos.empty() ? 1 : combos.size(); |
| 1673 | 8 | std::vector<InputBinding> appended; | |
| 1674 |
1/2✓ Branch 65 → 66 taken 8 times.
✗ Branch 65 → 198 not taken.
|
8 | appended.reserve(append_count); |
| 1675 |
2/2✓ Branch 67 → 68 taken 4 times.
✓ Branch 67 → 76 taken 4 times.
|
8 | if (combos.empty()) |
| 1676 | { | ||
| 1677 |
1/2✓ Branch 68 → 69 taken 4 times.
✗ Branch 68 → 189 not taken.
|
4 | InputBinding sentinel = prototype; |
| 1678 | 4 | sentinel.keys.clear(); | |
| 1679 | 4 | sentinel.modifiers.clear(); | |
| 1680 |
1/2✓ Branch 73 → 74 taken 4 times.
✗ Branch 73 → 187 not taken.
|
4 | appended.push_back(std::move(sentinel)); |
| 1681 | 4 | } | |
| 1682 | else | ||
| 1683 | { | ||
| 1684 |
2/2✓ Branch 96 → 78 taken 6 times.
✓ Branch 96 → 97 taken 4 times.
|
14 | for (const auto &combo : combos) |
| 1685 | { | ||
| 1686 |
1/2✓ Branch 80 → 81 taken 6 times.
✗ Branch 80 → 192 not taken.
|
6 | InputBinding binding = prototype; |
| 1687 |
1/2✓ Branch 81 → 82 taken 6 times.
✗ Branch 81 → 190 not taken.
|
6 | binding.keys = combo.keys; |
| 1688 |
1/2✓ Branch 82 → 83 taken 6 times.
✗ Branch 82 → 190 not taken.
|
6 | binding.modifiers = combo.modifiers; |
| 1689 |
1/2✓ Branch 85 → 86 taken 6 times.
✗ Branch 85 → 190 not taken.
|
6 | appended.push_back(std::move(binding)); |
| 1690 | 6 | } | |
| 1691 | } | ||
| 1692 | |||
| 1693 | 8 | std::vector<InputBinding> rebuilt; | |
| 1694 |
1/2✓ Branch 100 → 101 taken 8 times.
✗ Branch 100 → 196 not taken.
|
8 | rebuilt.reserve(m_pending_bindings.size() - indices.size() + append_count); |
| 1695 | 8 | size_t cursor = 0; | |
| 1696 |
2/2✓ Branch 121 → 103 taken 11 times.
✓ Branch 121 → 122 taken 8 times.
|
27 | for (size_t skip : indices) |
| 1697 | { | ||
| 1698 |
1/2✗ Branch 111 → 106 not taken.
✓ Branch 111 → 112 taken 11 times.
|
11 | for (size_t i = cursor; i < skip; ++i) |
| 1699 | { | ||
| 1700 | ✗ | rebuilt.push_back(std::move(m_pending_bindings[i])); | |
| 1701 | } | ||
| 1702 | 11 | cursor = skip + 1; | |
| 1703 | } | ||
| 1704 |
1/2✗ Branch 129 → 123 not taken.
✓ Branch 129 → 130 taken 8 times.
|
8 | for (size_t i = cursor; i < m_pending_bindings.size(); ++i) |
| 1705 | { | ||
| 1706 | ✗ | rebuilt.push_back(std::move(m_pending_bindings[i])); | |
| 1707 | } | ||
| 1708 |
2/2✓ Branch 146 → 132 taken 10 times.
✓ Branch 146 → 147 taken 8 times.
|
36 | for (auto &binding : appended) |
| 1709 | { | ||
| 1710 |
1/2✓ Branch 136 → 137 taken 10 times.
✗ Branch 136 → 195 not taken.
|
10 | rebuilt.push_back(std::move(binding)); |
| 1711 | } | ||
| 1712 | 8 | m_pending_bindings = std::move(rebuilt); | |
| 1713 | 8 | updated_pending = true; | |
| 1714 | 8 | } | |
| 1715 |
2/2✓ Branch 156 → 157 taken 19 times.
✓ Branch 156 → 159 taken 15 times.
|
34 | } |
| 1716 |
2/2✓ Branch 162 → 163 taken 2021 times.
✓ Branch 162 → 166 taken 15 times.
|
2036 | } |
| 1717 | ✗ | catch (...) | |
| 1718 | { | ||
| 1719 | // update_binding_combos is noexcept; on out-of-memory the pending bindings are left unchanged (allocation | ||
| 1720 | // precedes the move-commit) rather than terminating the process. | ||
| 1721 | ✗ | (void)Logger::get_instance().try_log( | |
| 1722 | LogLevel::Error, "InputManager: out of memory in update_binding_combos; pending bindings unchanged"); | ||
| 1723 | ✗ | return; | |
| 1724 | ✗ | } | |
| 1725 | |||
| 1726 |
2/2✓ Branch 165 → 167 taken 2002 times.
✓ Branch 165 → 169 taken 19 times.
|
2021 | if (local_poller) |
| 1727 | { | ||
| 1728 | 2002 | (void)local_poller->update_combos(name, combos); | |
| 1729 | } | ||
| 1730 |
1/2✓ Branch 169 → 170 taken 19 times.
✗ Branch 169 → 173 not taken.
|
19 | else if (updated_pending) |
| 1731 | { | ||
| 1732 | 19 | (void)Logger::get_instance().try_log( | |
| 1733 | LogLevel::Trace, "InputManager: update_binding_combos(\"{}\") applied to pending bindings", name); | ||
| 1734 | } | ||
| 1735 |
2/2✓ Branch 175 → 176 taken 2021 times.
✓ Branch 175 → 178 taken 15 times.
|
2036 | } |
| 1736 | |||
| 1737 | 14 | size_t InputManager::remove_binding_by_name(std::string_view name, bool invoke_callbacks) noexcept | |
| 1738 | { | ||
| 1739 | 14 | std::shared_ptr<InputPoller> live_poller; | |
| 1740 | 14 | size_t removed_pending = 0; | |
| 1741 | |||
| 1742 | { | ||
| 1743 | 14 | std::lock_guard lock(m_mutex); | |
| 1744 |
2/2✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 12 times.
|
14 | if (m_poller) |
| 1745 | { | ||
| 1746 | 2 | live_poller = m_poller; | |
| 1747 | } | ||
| 1748 | else | ||
| 1749 | { | ||
| 1750 | 12 | auto new_end = std::remove_if(m_pending_bindings.begin(), m_pending_bindings.end(), | |
| 1751 | 11 | [name](const InputBinding &b) { return b.name == name; }); | |
| 1752 | 12 | removed_pending = static_cast<size_t>(std::distance(new_end, m_pending_bindings.end())); | |
| 1753 | 24 | m_pending_bindings.erase(new_end, m_pending_bindings.end()); | |
| 1754 | } | ||
| 1755 | 14 | } | |
| 1756 | |||
| 1757 |
2/2✓ Branch 33 → 34 taken 2 times.
✓ Branch 33 → 36 taken 12 times.
|
14 | if (live_poller) |
| 1758 | { | ||
| 1759 | 2 | return live_poller->remove_bindings_by_name(name, invoke_callbacks); | |
| 1760 | } | ||
| 1761 | 12 | return removed_pending; | |
| 1762 | 14 | } | |
| 1763 | |||
| 1764 | 11 | void InputManager::clear_bindings(bool invoke_callbacks) noexcept | |
| 1765 | { | ||
| 1766 | 11 | std::shared_ptr<InputPoller> live_poller; | |
| 1767 | |||
| 1768 | { | ||
| 1769 | 11 | std::lock_guard lock(m_mutex); | |
| 1770 | 11 | m_pending_bindings.clear(); | |
| 1771 |
2/2✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 7 taken 10 times.
|
11 | if (m_poller) |
| 1772 | { | ||
| 1773 | 1 | live_poller = m_poller; | |
| 1774 | } | ||
| 1775 | 11 | } | |
| 1776 | |||
| 1777 |
2/2✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 12 taken 10 times.
|
11 | if (live_poller) |
| 1778 | { | ||
| 1779 | 1 | live_poller->clear_bindings(invoke_callbacks); | |
| 1780 | } | ||
| 1781 | 11 | } | |
| 1782 | |||
| 1783 | 224 | void InputManager::shutdown() noexcept | |
| 1784 | { | ||
| 1785 | 224 | std::shared_ptr<InputPoller> local_poller; | |
| 1786 | |||
| 1787 | { | ||
| 1788 | 224 | std::lock_guard lock(m_mutex); | |
| 1789 | // Clear atomic shared_ptr before releasing the poller to ensure concurrent is_binding_active() callers hold | ||
| 1790 | // a valid shared_ptr. | ||
| 1791 | 224 | m_active_poller.store(nullptr, std::memory_order_release); | |
| 1792 | 224 | m_running.store(false, std::memory_order_release); | |
| 1793 | 448 | local_poller = std::move(m_poller); | |
| 1794 | 224 | m_pending_bindings.clear(); | |
| 1795 | 224 | } | |
| 1796 | |||
| 1797 |
2/2✓ Branch 13 → 14 taken 40 times.
✓ Branch 13 → 29 taken 184 times.
|
224 | if (local_poller) |
| 1798 | { | ||
| 1799 | // Read loader-lock ownership once; it is stable across this call because InputPoller::shutdown() re-checks | ||
| 1800 | // it on the same thread with no intervening lock release, so both observe the same result. | ||
| 1801 | 40 | const bool under_loader_lock = is_loader_lock_held(); | |
| 1802 | 40 | local_poller->shutdown(); | |
| 1803 | |||
| 1804 |
1/2✗ Branch 17 → 18 not taken.
✓ Branch 17 → 29 taken 40 times.
|
40 | if (under_loader_lock) |
| 1805 | { | ||
| 1806 | // Under the loader lock InputPoller::shutdown() detaches its poll thread instead of joining it (a join | ||
| 1807 | // would deadlock the loader). The detached thread keeps reading InputPoller members (m_cv, m_cv_mutex, | ||
| 1808 | // m_poll_interval, m_bindings) until it observes the stop request, so destroying the poller now would | ||
| 1809 | // free those members | ||
| 1810 | // mid-access: a use-after-free. Move the last reference into a | ||
| 1811 | // nothrow-allocated heap cell that is never freed, so the object outlives the detached thread. The | ||
| 1812 | // module is already pinned by | ||
| 1813 | // InputPoller::shutdown(). This mirrors the leak-on-loader-lock | ||
| 1814 | // discipline used for the Logger and ConfigWatcher teardown paths; | ||
| 1815 | // nothrow keeps this noexcept path honest under OOM (the poller is then destroyed -- the pre-existing | ||
| 1816 | // hazard -- rather than throwing). | ||
| 1817 | ✗ | auto *leaked = new (std::nothrow) std::shared_ptr<InputPoller>(std::move(local_poller)); | |
| 1818 | (void)leaked; | ||
| 1819 | } | ||
| 1820 | } | ||
| 1821 | 224 | } | |
| 1822 | } // namespace DetourModKit | ||
| 1823 |