Latch the Minus+Plus combo until both are released, serialise menu open and close, pack the cursor release flags into one word, and drop menu_available

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-26 09:51:56 +03:00
1 parent 0cfc489bbf
commit 4d4b0efeb8
6 files changed
+64 -25

No files matched your search

+13 -8
View File
@@ -160,16 +160,21 @@ void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, int32_t
out[1] = (int32_t)lrint(y);
}
// Minus and Plus held together open or close the menu. *held: the combo was down at the previous poll (the caller keeps it).
// The poll where both are down reports neither to the game, and neither does any later poll of the same hold; Minus alone
// reaches the game unchanged. With the menu open the game sees a neutral pad. Returns 1 in the poll where the combo goes
// down: the caller toggles the menu once.
// Minus and Plus held together open or close the menu. *held: latched from the poll where both go down until neither is
// down (the caller keeps it). Every poll of the latch reports neither to the game, so releasing one button first does not
// leak the other; Minus alone before Plus reaches the game unchanged. With the menu open the game sees a neutral pad.
// Returns 1 in the poll where both go down: the caller toggles the menu once.
int input_menu_gate(int *held, int menu_open, uint64_t *buttons, int32_t sticks[4]) {
const uint64_t combo = 1ull << B_MINUS | 1ull << B_PLUS;
int down = (*buttons & combo) == combo;
int edge = down && !*held;
*held = down;
if (down) *buttons &= ~combo;
uint64_t raw = *buttons & combo;
int edge = 0;
if (*held) {
*held = raw != 0;
} else if (raw == combo) {
*held = 1;
edge = 1;
}
if (*held) *buttons &= ~combo;
if (menu_open) {
*buttons = 0;
sticks[0] = sticks[1] = sticks[2] = sticks[3] = 0;
+19 -9
View File
@@ -191,8 +191,18 @@ static int key_vk(int key) {
static volatile LONG mouse_dx, mouse_dy; // raw counts since the last frame sample
static volatile LONG cur_captured; // cursor hidden and confined to the client area
static volatile LONG cur_active; // WM_ACTIVATEAPP: the window's application is active
static volatile LONG cur_released; // Esc, or activation by a click on the title bar or border: until a client click
static volatile LONG cur_esc; // cur_released was set by Esc (not before): closing the menu gives the capture back
// Bits: CUR_REL, Esc or activation by a click on the title bar or border: until a client click; CUR_ESC, set with CUR_REL
// by Esc (not when it was already set): closing the menu gives the capture back. One word, changed by one CAS.
enum { CUR_REL = 1, CUR_ESC = 2 };
static volatile LONG cur_rel;
static void cur_rel_update(LONG (*f)(LONG)) {
LONG o, n;
do { o = cur_rel; n = f(o); } while (n != o && InterlockedCompareExchange(&cur_rel, n, o) != o);
}
static LONG rel_esc(LONG o) { return o & CUR_REL ? o : CUR_REL | CUR_ESC; }
static LONG rel_title(LONG o) { (void)o; return CUR_REL; }
static LONG rel_clear(LONG o) { (void)o; return 0; }
static LONG rel_menu_closed(LONG o) { return o & CUR_ESC ? 0 : o; }
static volatile LONG cur_suspend; // F11 or a modal size or move loop under way
static SRWLOCK cur_lock = SRWLOCK_INIT; // serialises cursor_update (window thread, input_block callers)
static volatile LONG g_block; // text entry state, see input_block
@@ -214,7 +224,7 @@ static float mouse_sens_get(void) {
static void cursor_update(HWND h) {
const char *in = cfg_get("TOTK_INPUT");
AcquireSRWLockExclusive(&cur_lock);
int want = h && cur_active && !cur_released && !cur_suspend && g_block != 1 && g_menu != 1 && mouse_sens_get() > 0 &&
int want = h && cur_active && !(cur_rel & CUR_REL) && !cur_suspend && g_block != 1 && g_menu != 1 && mouse_sens_get() > 0 &&
!(in && !strcmp(in, "none")) && GetForegroundWindow() == h && !IsIconic(h);
RECT r;
POINT tl = {0, 0};
@@ -230,7 +240,7 @@ static void cursor_update(HWND h) {
ReleaseSRWLockExclusive(&cur_lock);
}
int input_cursor_released(void) { return cur_released != 0; }
int input_cursor_released(void) { return (cur_rel & CUR_REL) != 0; }
void input_cursor_suspend(int on) {
InterlockedExchange(&cur_suspend, on != 0);
@@ -259,16 +269,16 @@ int input_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res) {
if (cur_captured && LOWORD(l) == HTCLIENT) { SetCursor(NULL); *res = TRUE; return 1; }
break;
case WM_KEYDOWN: // Esc is unbound in the game layout: it releases the cursor and opens the menu (menu_window_msg)
if (w == VK_ESCAPE && !cur_released) { InterlockedExchange(&cur_released, 1); InterlockedExchange(&cur_esc, 1); cursor_update(h); }
if (w == VK_ESCAPE && !(cur_rel & CUR_REL)) { cur_rel_update(rel_esc); cursor_update(h); }
break;
case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: case WM_MBUTTONDOWN: // client area only; poll_merged drops the click
if (cur_released) { InterlockedExchange(&cur_released, 0); InterlockedExchange(&cur_esc, 0); cursor_update(h); }
if (cur_rel & CUR_REL) { cur_rel_update(rel_clear); cursor_update(h); }
break;
case WM_MOUSEACTIVATE: // sent before WM_ACTIVATEAPP: a title bar or border click must not capture
if (LOWORD(l) != HTCLIENT) InterlockedExchange(&cur_released, 1);
if (LOWORD(l) != HTCLIENT) cur_rel_update(rel_title);
break;
case WM_ACTIVATEAPP:
if (!w) InterlockedExchange(&cur_released, 0); // Alt+Tab back in captures again
if (!w) cur_rel_update(rel_clear); // Alt+Tab back in captures again
InterlockedExchange(&cur_active, w != 0);
cursor_update(h);
break;
@@ -442,7 +452,7 @@ void input_block(int on) {
// the release. The cursor is released while it is open, for the menu's mouse.
void input_menu(int open) {
InterlockedExchange(&g_menu, open ? 1 : 2);
if (!open && InterlockedExchange(&cur_esc, 0)) InterlockedExchange(&cur_released, 0); // the capture before Esc comes back
if (!open) cur_rel_update(rel_menu_closed); // the capture before Esc comes back
cursor_update(gfx_window_handle());
}
+12
View File
@@ -756,6 +756,18 @@ static void probe_menu_gate(void) {
input_menu_gate(&held, 0, &b, st);
b = minus | plus;
CHECK(input_menu_gate(&held, 0, &b, st) == 1, "the second hold did not toggle");
// Plus released first while Minus is still down: Minus must not leak; a new Minus press after both are up passes
held = 0;
b = minus | plus;
input_menu_gate(&held, 0, &b, st);
b = minus;
int t4 = input_menu_gate(&held, 0, &b, st);
CHECK(!t4 && b == 0 && held, "Minus left down after Plus leaked: buttons %#llx", (unsigned long long)b);
b = 0;
input_menu_gate(&held, 0, &b, st);
b = minus | a;
int t5 = input_menu_gate(&held, 0, &b, st);
CHECK(!t5 && b == (minus | a) && !held, "Minus after the release did not pass: buttons %#llx", (unsigned long long)b);
printf("menu gate: Minus alone passes, Minus+Plus is consumed and toggles once per hold, an open menu gives a neutral pad\n");
}
+19 -7
View File
@@ -38,13 +38,26 @@ static const unsigned char roboto_ttf[] = {
static std::atomic<int> g_open; // the menu is open
static std::atomic<int> g_reset; // opened or closed since the last laid-out frame: drop stale input
static void set_open(int open) {
if (g_open.exchange(open) == open) return;
g_reset.store(1);
input_menu(open);
rt_log("menu: %s", open ? "open" : "closed");
// g_open, the input block and the log line change together under open_lock, so they cannot disagree when the window thread
// (F1, Esc), the polling thread (combo) and the present path (Continue, B) race.
static PlatMutex open_lock = PLAT_MUTEX_INIT;
// want 0 or 1 sets the state; -1 flips it. Returns the new state.
static int change_open(int want) {
plat_mutex_lock(&open_lock);
int open = want < 0 ? !g_open.load() : want;
if (g_open.load() != open) {
g_open.store(open);
g_reset.store(1);
input_menu(open);
rt_log("menu: %s", open ? "open" : "closed");
}
plat_mutex_unlock(&open_lock);
return open;
}
static void set_open(int open) { change_open(open); }
// TOTK_MENU_TEST=<frame>[:<frames per page>]: the presented frame the menu opens at for the shot hook; 0 when unset
static uint64_t test_frame(void) {
static std::atomic<long long> f{-1};
@@ -59,10 +72,9 @@ static uint64_t test_frame(void) {
}
static int g_failed_flag();
extern "C" int menu_available(void) { return 1; }
extern "C" int menu_is_open(void) { return g_open.load(std::memory_order_relaxed); }
extern "C" void menu_toggle(void) {
if (!g_failed_flag()) set_open(!g_open.load());
if (!g_failed_flag()) change_open(-1);
}
// ---- events from the window adapter, queued while the menu is open; the present path hands them to ImGui
-1
View File
@@ -20,7 +20,6 @@ extern "C" {
#endif
// ---- state, any thread
int menu_available(void); // always 1: the menu opens on every platform
int menu_is_open(void);
void menu_toggle(void); // F1 / Esc on the keyboard, Minus+Plus on the pad
+1
View File
@@ -44,6 +44,7 @@ extern "C" int menu_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res)
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
// bit 30: auto-repeat
// Esc with the menu open (opened by F1 too) is not a toggle here: it goes to the menu, which closes itself
if (!(l & (1 << 30)) && (w == VK_F1 || (w == VK_ESCAPE && !menu_is_open()))) {
menu_toggle();
*res = 0;