diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e58270..c45f496 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ correction is still experimental. ## [Unreleased] +### Performance + +- A/B-only opt-in high-resolution 16 ms waitable timer replaces the low-priority + WM_TIMER smoothing cadence. A worker coalesces notifications; SendInput remains + exclusively on the original message-loop thread. Falls back to WM_TIMER when + unavailable; a runtime timer/post failure stops correction safely. +- After A/B test measurements (~16.2 ms vs ~26.7 ms per tick), normal Playing + now enables the high-resolution timer by default, while remaining switchable + back to WM_TIMER through `high_res_smoothing_timer=false`. Playing runtime + smoke test is required before merging. + ### Fixed - Require repeated, observed game recenter events before intercepting hidden-cursor diff --git a/README.md b/README.md index 2a81737..3a2218b 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,7 @@ Keys beginning with `_` in the supplied configuration files are explanatory comm | `watchdog_max_output_failures` | `5` | consecutive failed output calls before the watchdog trips | | `log_mouse_moves` | `true` | `false` records buttons, wheel and state only | | `trace_correction_timing` | `false` | opt-in 250 ms timer/anchor/pending-delta summaries; enabled in A/B profile | +| `high_res_smoothing_timer` | `false` | high-resolution waitable clock for 16 ms smoothing; enabled in both A/B and Playing profiles after successful A/B field tests; optional to disable | | `log_directory` | `logs` | relative paths resolve beside the EXE | | `log_file_prefix` | `remotemousefix` | log filename prefix | | `max_log_bytes` | `8388608` | rotate after this size | diff --git a/config/config-ab-test.json b/config/config-ab-test.json index 7c0483b..3224805 100644 --- a/config/config-ab-test.json +++ b/config/config-ab-test.json @@ -46,6 +46,9 @@ "_trace_correction_timing": "Log 250 ms timing/anchor-gate summaries while absolute correction is active and the target holds the cursor.", "trace_correction_timing": true, + "_high_res_smoothing_timer": "Use the high-resolution 16ms waitable timer; fall back to WM_TIMER if unavailable. Also enabled in Playing after successful A/B measurements.", + "high_res_smoothing_timer": true, + "_console_status_interval_ms": "Live counter refresh in the console. 0 disables it.", "console_status_interval_ms": 2000, diff --git a/config/config-play.json b/config/config-play.json index 1506597..3374cd8 100644 --- a/config/config-play.json +++ b/config/config-play.json @@ -22,6 +22,10 @@ "anchor_tolerance_px": 3, "state_poll_interval_ms": 10, "log_state_changes": true, + + "_high_res_smoothing_timer": "Use 16ms high-resolution waitable timer, with WM_TIMER fallback if unsupported. Validated in A/B profile, enabled for Playing trial.", + "high_res_smoothing_timer": true, + "console_status_interval_ms": 2000, "heartbeat_interval_ms": 10000 } diff --git a/docs/diagnostics.md b/docs/diagnostics.md index 0780f2b..bc66d6b 100644 --- a/docs/diagnostics.md +++ b/docs/diagnostics.md @@ -362,3 +362,31 @@ through again (`## STATE warp-evidence revoked`). Runtime verification must incl window before the next hold, all Probe A/B/C modes, loss of target focus, the OFF/relative modes and emergency-off. +## High-resolution smoothing A/B experiment (follow-up PR) + +The original `SetTimer(..., 16, ...)` often delivered a 27 ms average tick in +the WoW capture. With `high_res_smoothing_timer=true`, the A/B profile instead +uses a high-resolution Windows waitable timer and a dedicated waiter thread +which posts at most one outstanding `WM_APP` timer notification to the main +message loop. The mouse hook and all `SendInput` calls remain on the original +thread. No process-wide timer-resolution changes, DLL injection or game hooks. + +A startup line identifies `## SMOOTH TIMER source=high-res-waitable` or +`source=wm-timer`. If Windows cannot create or arm the high-resolution timer, +the tool logs a warning and uses `WM_TIMER`. If an active high-resolution +timer fails or cannot post to the original thread, the application shuts down +without silently leaving suppressed movements uncorrected. + +In the same WoW/TeamViewer scenario, compare `## TIMING` tick counts, +`max_timer_gap_ms`, pending age and perceived camera smoothness with +`high_res_smoothing_timer=true` versus `false`. Especially verify +`Ctrl+Alt+C`, focus loss, multiple modes, `own==applied`, no `queue drops` +or watchdog trips. Hardware, scheduling and game frame rate can still impose +limits: a 16 ms requested timer does not guarantee 16 ms output or cure +the input-side 500 ms pauses. The A/B tests of 27 October 2026 showed a +measured interval of about 16.2 ms versus 26.7 ms with WM_TIMER, with subjective +improvement and no failed or missing SendInput echoes. Therefore the Playing +profile also enables this timer by default; disable +`high_res_smoothing_timer` in `config-play.json` to revert to WM_TIMER. +The new Playing default still requires an ordinary-playing smoke test on +Windows/TeamViewer before merging this PR. diff --git a/include/rmf/Config.h b/include/rmf/Config.h index d276ae7..79968f4 100644 --- a/include/rmf/Config.h +++ b/include/rmf/Config.h @@ -40,6 +40,7 @@ struct Config { unsigned statePollIntervalMs = 10; // cursor visibility / clip / anchor sampling period bool logStateChanges = true; bool traceCorrectionTiming = false; // opt-in timing trace for A/B tests + bool highResSmoothingTimer = false; // opt-in; default Windows WM_TIMER clock unsigned consoleStatusIntervalMs = 2000; // 0 disables the live console status line unsigned heartbeatIntervalMs = 10000; // periodic liveness line, 0 disables it diff --git a/src/Config.cpp b/src/Config.cpp index 5d5d12e..670ecd7 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -304,6 +304,9 @@ bool LoadConfig(const std::wstring& path, Config& cfg) { } else if (key == L"trace_correction_timing") { if (ParseBool(value, b)) cfg.traceCorrectionTiming = b; else cfg.warnings.push_back(L"trace_correction_timing is not a boolean: " + value); + } else if (key == L"high_res_smoothing_timer") { + if (ParseBool(value, b)) cfg.highResSmoothingTimer = b; + else cfg.warnings.push_back(L"high_res_smoothing_timer is not a boolean: " + value); } else if (key == L"console_status_interval_ms") { if (ParseU64(value, u) && u <= 60000) cfg.consoleStatusIntervalMs = static_cast(u); else cfg.warnings.push_back(L"console_status_interval_ms must be 0..60000: " + value); diff --git a/src/main.cpp b/src/main.cpp index 5a86d7d..1dca56b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -75,6 +76,7 @@ constexpr HotkeySpec kHotkeys[] = { constexpr UINT kMsgQuitRequested = WM_APP + 1; constexpr UINT kMsgApplyDelta = WM_APP + 2; // posted by the hook: wParam = dx, lParam = dy +constexpr UINT kMsgSmoothTick = WM_APP + 3; // coalesced high-res timer tick, main thread only constexpr UINT_PTR kSmoothTimer = 1; constexpr UINT kSmoothIntervalMs = 16; constexpr long kSmoothMaxStepPx = 20; @@ -1075,8 +1077,71 @@ int main() { } }; - const UINT_PTR smoothTimerId = SetTimer(nullptr, kSmoothTimer, kSmoothIntervalMs, nullptr); - if (smoothTimerId == 0) { + // Windows WM_TIMER is low priority and was measured around 27 ms/tick despite a + // 16 ms requested period. An opt-in high-resolution waitable timer produces at + // most ONE outstanding thread message; the actual cursor output remains here on + // the message-loop/hook thread. Do not change system-wide timer resolution. + HANDLE highResTimer = nullptr; + std::thread highResWorker; + std::atomic stopHighResWorker {false}; + std::atomic highResTickQueued {false}; + std::atomic highResTimerFailed {false}; + if (cfg.highResSmoothingTimer) { + highResTimer = CreateWaitableTimerExW( + nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, + TIMER_MODIFY_STATE | SYNCHRONIZE); + if (highResTimer) { + LARGE_INTEGER firstDue {}; + firstDue.QuadPart = -static_cast(kSmoothIntervalMs) * 10000; + if (!SetWaitableTimer(highResTimer, &firstDue, + static_cast(kSmoothIntervalMs), + nullptr, nullptr, FALSE)) { + CloseHandle(highResTimer); + highResTimer = nullptr; + } + } + if (highResTimer) { + // Ensure PostThreadMessage always has an existing message queue. + MSG initialQueueMessage {}; + PeekMessageW(&initialQueueMessage, nullptr, WM_USER, WM_USER, PM_NOREMOVE); + try { + highResWorker = std::thread([&] { + while (!stopHighResWorker.load(std::memory_order_acquire) && + g_running.load(std::memory_order_relaxed)) { + const DWORD waited = WaitForSingleObject(highResTimer, 100); + if (stopHighResWorker.load(std::memory_order_acquire)) break; + if (waited == WAIT_TIMEOUT) continue; + if (waited != WAIT_OBJECT_0) { + highResTimerFailed.store(true, std::memory_order_release); + RequestStop(); // fail closed if the timer breaks during a hold + break; + } + if (!highResTickQueued.exchange(true, std::memory_order_acq_rel) && + !PostThreadMessageW(g_mainThreadId, kMsgSmoothTick, 0, 0)) { + highResTickQueued.store(false, std::memory_order_release); + highResTimerFailed.store(true, std::memory_order_release); + RequestStop(); + break; + } + } + }); + } catch (const std::system_error&) { + CancelWaitableTimer(highResTimer); + CloseHandle(highResTimer); + highResTimer = nullptr; + } + } + if (!highResTimer) { + LogLine(L"# CONFIG WARNING : high-resolution smoothing timer unavailable; " + L"falling back to WM_TIMER"); + } + } + const UINT_PTR smoothTimerId = highResTimer ? 0 : + SetTimer(nullptr, kSmoothTimer, kSmoothIntervalMs, nullptr); + const bool smoothTimerAvailable = highResTimer || smoothTimerId != 0; + LogLine(highResTimer ? L"## SMOOTH TIMER source=high-res-waitable period_ms=16" : + L"## SMOOTH TIMER source=wm-timer period_ms=16"); + if (!smoothTimerAvailable) { LogLine(L"# CONFIG WARNING : smoothing timer unavailable; absolute mode uses immediate output"); } MSG msg {}; @@ -1116,7 +1181,7 @@ int main() { ++timing.incoming; timingLastInputMs = qpcNowMs(); } - if (mode == rmf::CorrectionMode::Absolute && smoothTimerId != 0) { + if (mode == rmf::CorrectionMode::Absolute && smoothTimerAvailable) { if (cfg.traceCorrectionTiming && smoothPendingX == 0 && smoothPendingY == 0) { timingPendingSinceMs = qpcNowMs(); } @@ -1145,7 +1210,12 @@ int main() { continue; } - if (msg.message == WM_TIMER && msg.wParam == smoothTimerId) { + if ((smoothTimerId != 0 && msg.message == WM_TIMER && + msg.wParam == smoothTimerId) || + (highResTimer && msg.message == kMsgSmoothTick)) { + if (msg.message == kMsgSmoothTick) { + highResTickQueued.store(false, std::memory_order_release); + } if (cfg.traceCorrectionTiming) { const bool active = correction.Mode() == rmf::CorrectionMode::Absolute && cursorHeldByTarget.load(std::memory_order_relaxed); @@ -1294,8 +1364,13 @@ int main() { } // ---- shutdown ------------------------------------------------------------------------ - if (smoothTimerId != 0) { - KillTimer(nullptr, smoothTimerId); + if (smoothTimerId != 0) KillTimer(nullptr, smoothTimerId); + stopHighResWorker.store(true, std::memory_order_release); + if (highResTimer) CancelWaitableTimer(highResTimer); + if (highResWorker.joinable()) highResWorker.join(); + if (highResTimer) CloseHandle(highResTimer); + if (highResTimerFailed.load(std::memory_order_acquire)) { + LogLine(L"## SMOOTH TIMER FAILED: stopped correction rather than continuing without ticks"); } g_running.store(false); correction.ForceOff();