Skip to content

fix: collapse the triple auto-switch re-apply in the Options OK handler - #7

Merged
bnayahu merged 1 commit into
mainfrom
fix/single-autoswitch-reapply
Aug 11, 2026
Merged

fix: collapse the triple auto-switch re-apply in the Options OK handler#7
bnayahu merged 1 commit into
mainfrom
fix/single-autoswitch-reapply

Conversation

@bnayahu

@bnayahu bnayahu commented Aug 11, 2026

Copy link
Copy Markdown
Owner

Closes #2

Problem

OptionsDialogProc's IDOK handler could call CheckAndApplyAutoSwitch() three times per click — in the auto-switch settings block, in the base-mouse-count block, and in the forced re-apply after the cached device state is discarded. Each call enumerates raw input devices with up to three GetRawInputDeviceList retries.

Why it was safe, and why it is still safe

  • The direction is persisted before all three calls, so none could read a stale value.
  • ApplyMouseOrientation() passes an absolute value to SwapMouseButton() rather than toggling, so repeated application is idempotent.
  • In the common case the first two calls early-return on unchanged state, so only the last one did work.

Change

Remove the two earlier calls; keep the forced re-apply at the end of the handler, still gated on autoSwitchEnabled && autoSwitchWritten and still preceded by the g_lastExternalMouseState = EXTERNAL_MOUSE_UNKNOWN reset that makes it effective.

The surviving call is strictly the better one: by the end of the handler every setting it reads — direction, auto-switch flag, base device count — is persisted. The first call ran before SetBaseMouseCount(), so it could never see a changed base count.

Dropping the base-mouse-count call also closes a small inconsistency: it was gated on autoSwitchEnabled alone, so it could re-apply even when the auto-switch flag failed to persist and monitoring was never started. The surviving call is gated on both, matching 3a00a42.

Verification

  • ./build.sh succeeds, no compiler warnings.
  • Net effect per OK click: one device enumeration instead of up to three; no change to the resulting mouse orientation in any path.

Assisted-By: Claude (Anthropic AI) noreply@anthropic.com

Pressing OK could call CheckAndApplyAutoSwitch() three times: once in the
auto-switch settings block, once in the base-mouse-count block, and once
in the forced re-apply after the cached device state is discarded. Each
call enumerates raw input devices, retrying GetRawInputDeviceList up to
three times, so a single click cost up to three enumerations.

The repeats were harmless — the direction is persisted before all three
calls and ApplyMouseOrientation() sets an absolute value rather than
toggling — but only the last call could do useful work in the common
case, since the first two early-return on unchanged state.

Remove the two earlier calls and keep the forced re-apply at the end of
the handler. By that point every setting the check reads is persisted, so
one pass applies them all; the first call previously ran before
SetBaseMouseCount(), so it could not see a changed base count anyway.

Dropping the base-mouse-count call also stops a re-apply from running
when the auto-switch flag itself failed to persist: that call was gated
on autoSwitchEnabled alone, not on autoSwitchWritten. The surviving call
is gated on both, matching 3a00a42.

Closes #2

Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com>
Signed-off-by: Jonathan Bnayahu <bnayahu@il.ibm.com>
@bnayahu
bnayahu merged commit 645507e into main Aug 11, 2026
1 check passed
@bnayahu
bnayahu deleted the fix/single-autoswitch-reapply branch August 11, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CheckAndApplyAutoSwitch() runs up to 3 times when OK is pressed in Options

1 participant