Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 63 additions & 38 deletions runtime/hle/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ int bind_label_family(void) {

static void result(int kind, int dev, int slot, int other);
static int act_default(int dev, int id);
static void act_refresh(void);

void bind_reset(int dev) {
if (dev == BIND_DEV_PAD) memcpy(g_bind_pad, k_def_pad, sizeof g_bind_pad);
Expand All @@ -167,6 +168,7 @@ void bind_reset_slot(int slot) {
if (slot < 0 || slot >= BIND_KEY_COUNT) return;
g_bind_key[slot] = k_def_key[slot];
if (bind_pad_slot(slot)) g_bind_pad[slot] = k_def_pad[slot];
act_refresh();
}

bool bind_is_default(int dev) {
Expand Down Expand Up @@ -247,6 +249,7 @@ static void assign(int dev, int slot, int host) {
other >= 0 ? k_names[other] : "", other >= 0 ? ")" : "");
if (other >= 0) LOG("[input] %s -> %s\n", k_names[other], dev == BIND_DEV_PAD ? bind_pad_label(t[other], 0) : bind_key_label(t[other]));
result(other >= 0 ? 2 : 1, dev, slot, other);
act_refresh();
}

bool bind_capture_event(const SDL_Event *e) {
Expand Down Expand Up @@ -325,6 +328,7 @@ bool bind_settings_load(const char *key, const char *val) {
}
if (i < BIND_PAD_COUNT) act_migrate(pad ? BIND_DEV_PAD : BIND_DEV_KEY, i);
}
act_refresh();
return true;
}

Expand Down Expand Up @@ -369,6 +373,43 @@ static const char *const k_act_names[ACT_COUNT] = {
const char *act_name(int id) { return id >= 0 && id < ACT_COUNT ? k_act_names[id] : NULL; }
bool act_hidden(int id) { return id >= 0 && id < ACT_COUNT && (ACT_HIDDEN_MASK >> id & 1); }

// The game's mask bits are sead's pad indices (open-ead: PadIdx), not the Switch's nn::hid bits.
static int mask_button(uint32_t m) {
switch (m) {
case 0x1: return BIND_A;
case 0x2: return BIND_B;
case 0x4: return BIND_ZL;
case 0x8: return BIND_X;
case 0x10: return BIND_Y;
case 0x20: return BIND_ZR;
case 0x40: return BIND_RSTICK;
case 0x80: return BIND_LSTICK;
case 0x2000: return BIND_L;
case 0x4000: return BIND_R;
case 0x10000: return BIND_UP;
case 0x20000: return BIND_DOWN;
default: return -1;
}
}

// The game's table for 1.9.0 (all 41 masks): factory defaults and legacy migration always use this fixed table.
// act_switch_button also uses it before the game has supplied its live table.
static const uint32_t k_masks_190[ACT_COUNT] = {
0x1, 0x2, 0x8, 0x10, 0x8, 0x1, 0x8, 0x2, 0x8, 0x2, 0x8, 0x8, 0x2, 0x4, 0x2, 0x40, 0x1, 0x80, 0x20, 0x8, 0x2,
0x10000, 0x20000, 0x10000, 0x20000, 0x2, 0x1, 0x2000, 0x4000, 0x4, 0x2, 0x4000, 0x4000, 0x2000, 0x1, 0x20000,
0x10, 0x20000, 0x1, 0x2, 0x2000};

int act_switch_button(int id) {
if (id < 0 || id >= ACT_COUNT) return -1;
return mask_button((k_masks ? k_masks : k_masks_190)[id]);
}

static int act_default(int dev, int id) {
int b = mask_button(k_masks_190[id]);
if (b < 0) return ACT_NONE;
return dev == BIND_DEV_PAD ? k_def_pad[b] : k_def_key[b];
}

bool act_is_default(int id) {
return id < 0 || id >= ACT_COUNT ||
(g_act_key[id] == act_default(BIND_DEV_KEY, id) && g_act_pad[id] == act_default(BIND_DEV_PAD, id));
Expand Down Expand Up @@ -406,38 +447,9 @@ void act_reset(void) {
// Initialized before settings or either Controls page can read the public arrays (also in standalone tests).
__attribute__((constructor)) static void act_init(void) { act_reset(); }

void act_masks(const uint32_t *table) { k_masks = table; }

// The game's mask bits are sead's pad indices (open-ead: PadIdx), not the Switch's nn::hid bits.
static int mask_button(uint32_t m) {
switch (m) {
case 0x1: return BIND_A;
case 0x2: return BIND_B;
case 0x4: return BIND_ZL;
case 0x8: return BIND_X;
case 0x10: return BIND_Y;
case 0x20: return BIND_ZR;
case 0x40: return BIND_RSTICK;
case 0x80: return BIND_LSTICK;
case 0x2000: return BIND_L;
case 0x4000: return BIND_R;
case 0x10000: return BIND_UP;
case 0x20000: return BIND_DOWN;
default: return -1;
}
}

// The game's table for 1.9.0 (all 41 masks): factory defaults and legacy migration always use this fixed table.
// act_switch_button also uses it before the game has supplied its live table.
static const uint32_t k_masks_190[ACT_COUNT] = {
0x1, 0x2, 0x8, 0x10, 0x8, 0x1, 0x8, 0x2, 0x8, 0x2, 0x8, 0x8, 0x2, 0x4, 0x2, 0x40, 0x1, 0x80, 0x20, 0x8, 0x2,
0x10000, 0x20000, 0x10000, 0x20000, 0x2, 0x1, 0x2000, 0x4000, 0x4, 0x2, 0x4000, 0x4000, 0x2000, 0x1, 0x20000,
0x10, 0x20000, 0x1, 0x2, 0x2000};

static int act_default(int dev, int id) {
int b = mask_button(k_masks_190[id]);
if (b < 0) return ACT_NONE;
return dev == BIND_DEV_PAD ? k_def_pad[b] : k_def_key[b];
void act_masks(const uint32_t *table) {
k_masks = table;
act_refresh();
}

// Applied while reading legacy ini lines, independent of line order and independently for each device.
Expand All @@ -452,11 +464,6 @@ static void act_migrate(int dev, int slot) {
act_refresh();
}

int act_switch_button(int id) {
if (id < 0 || id >= ACT_COUNT) return -1;
return mask_button((k_masks ? k_masks : k_masks_190)[id]);
}

uint64_t act_scan_keys(uint64_t key_buttons, int (*held)(int scancode)) {
(void)key_buttons; // kept for callers; actions read host inputs, never the live Switch layout
uint64_t out = 0, custom = a_custom;
Expand All @@ -479,6 +486,24 @@ uint64_t act_scan_pad(uint64_t pad_buttons, uint32_t host_buttons) {
return out;
}

static bool act_held_host(int id, int (*held)(int scancode), uint32_t host) {
if (id < 0 || id >= ACT_COUNT) return false;
int k = g_act_key[id], p = g_act_pad[id];
return (k > 0 && held && held(k)) || (p >= 0 && p < PADB_HOST_COUNT && (host >> p & 1));
}

// During gameplay, decouple in-game action buttons (Y/X/A/B) from raw menu rebinds
// so that rebinding a menu slot (e.g. "Menu: sort / sensor") never hijacks actions in play.
// Only the raw keyboard/pad mask is rewritten; script/console/cursor/auto-continue bits stay untouched.
uint64_t act_gameplay_buttons(uint64_t raw, int (*held)(int scancode), uint32_t host) {
raw &= ~((1ull << BIND_Y) | (1ull << BIND_X) | (1ull << BIND_A) | (1ull << BIND_B));
if (act_held_host(3, held, host) || act_held_host(36, held, host)) raw |= 1ull << BIND_Y;
if (act_held_host(2, held, host)) raw |= 1ull << BIND_X;
if (act_held_host(0, held, host)) raw |= 1ull << BIND_A;
if (act_held_host(12, held, host) || act_held_host(14, held, host)) raw |= 1ull << BIND_B;
return raw;
}

// Each side publishes its own half (the keyboard and the controller change at different times)
void act_publish(uint64_t keys, uint64_t pads) {
if (keys != ~0ull) a_keys = keys;
Expand Down Expand Up @@ -656,7 +681,7 @@ void act_poll(bool live) {
}

int rt_act_query(int kind, uint64_t pad, uint64_t id, uint64_t lr, uint64_t masks) {
if (!k_masks && masks) k_masks = (const uint32_t *)(uintptr_t)masks;
if (!k_masks && masks) { k_masks = (const uint32_t *)(uintptr_t)masks; act_refresh(); }
bool custom = id < ACT_COUNT && (a_custom >> id & 1);
if (act_log_on()) act_log_query(kind, pad, (unsigned)id, lr, custom && a_live);
if (id >= ACT_COUNT || !a_live || !custom) return -1;
Expand Down
9 changes: 7 additions & 2 deletions runtime/hle/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,10 @@ void input_frame(uint64_t frame) {
// Buttons held right now (keyboard, gamepad, test script), independent of whether the game polls (movies).
uint64_t input_buttons_now(void) {
bool muted = (!win_focused && !host_test) || menu_input_captured();
uint64_t b = (muted ? 0 : key_buttons | pad_buttons | escape_buttons) | script_buttons;
uint64_t raw = muted ? 0 : key_buttons | pad_buttons;
if (!muted && !framepace_title_ui() && !framepace_menu_ui() && !pc_cursor_active())
raw = act_gameplay_buttons(raw, game_key, host_buttons);
uint64_t b = raw | (muted ? 0 : escape_buttons) | script_buttons;
control_input_apply(&b, NULL, NULL, NULL, NULL);
return b;
}
Expand Down Expand Up @@ -1057,7 +1060,9 @@ static void fill_state(Cpu *c) {
// the window unfocused or the overlay up, use the Switch layer, so no binding can lock the player out.
act_poll(!menu && !framepace_title_ui() && !framepace_menu_ui());
uint64_t bow = pc_bow_sample();
uint64_t b = menu ? script_buttons : key_buttons | pad_buttons | escape_buttons | bow | script_buttons;
bool in_menu = menu || framepace_title_ui() || framepace_menu_ui();
uint64_t raw = menu ? 0 : (in_menu ? (key_buttons | pad_buttons) : act_gameplay_buttons(key_buttons | pad_buttons, game_key, host_buttons));
uint64_t b = raw | (menu ? 0 : escape_buttons | bow) | script_buttons;
if (!menu) b |= auto_continue_buttons(b); // (not while the overlay has the controls)
b=pc_cursor_buttons(b);
{ // injected Plus: held for ~10 presented frames (the game polls several times per frame)
Expand Down
1 change: 1 addition & 0 deletions runtime/input_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ void act_reset(void); // every action back to the defaul
// Host state in, from runtime/hle/input.c. Main thread, on every key / button event:
uint64_t act_scan_keys(uint64_t key_buttons, int (*held)(int scancode)); // actions the keyboard side holds
uint64_t act_scan_pad(uint64_t pad_buttons, uint32_t host_buttons); // ... the controller side holds
uint64_t act_gameplay_buttons(uint64_t raw, int (*held)(int scancode), uint32_t host_buttons); // gameplay A/B/X/Y from action bindings
void act_publish(uint64_t keys, uint64_t pads); // (bit per action)
// The controller poll (game thread, once per GetNpadStates): freezes the held / pressed sets the game's queries see.
// live = false in the game's menus, with the window unfocused or the overlay up: the Switch layer answers then.
Expand Down
19 changes: 19 additions & 0 deletions tests/test_pc_controls.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,25 @@ int main(void) {
e.type=SDL_EVENT_MOUSE_BUTTON_DOWN;e.button.button=SDL_BUTTON_LEFT;
input_handle_event(&e);assert(input_buttons_now()&(1ull<<BTN_Y));
e.type=SDL_EVENT_MOUSE_BUTTON_UP;input_handle_event(&e);

// Rebinding "Menu: sort / sensor" to a key must not hijack Attack in play (#608)
int16_t saved_bind_y = g_bind_key[BIND_Y];
g_bind_key[BIND_Y] = SDL_SCANCODE_Y;
e.type=SDL_EVENT_KEY_DOWN;e.key.scancode=SDL_SCANCODE_Y;
input_handle_event(&e);
assert(!(input_buttons_now()&(1ull<<BTN_Y))); // Y key does NOT attack in gameplay
e.type=SDL_EVENT_MOUSE_BUTTON_DOWN;e.button.button=SDL_BUTTON_LEFT;
input_handle_event(&e);
assert(input_buttons_now()&(1ull<<BTN_Y)); // mouse left still attacks
e.type=SDL_EVENT_MOUSE_BUTTON_UP;input_handle_event(&e);
e.type=SDL_EVENT_KEY_UP;e.key.scancode=SDL_SCANCODE_Y;input_handle_event(&e);
g_bind_key[BIND_Y] = saved_bind_y;

// Scripted/console A/B/X/Y buttons must still reach the game in play
script_buttons = (1ull << BTN_A) | (1ull << BTN_B) | (1ull << BTN_X) | (1ull << BTN_Y);
assert((input_buttons_now() & script_buttons) == script_buttons);
script_buttons = 0;

puts("PASS: F bow, right-mouse shield/lock-on and left-mouse attack use ordinary bindings");

// Controller Walk scales both left-stick axes and returns to full speed on release (never cumulative).
Expand Down
1 change: 1 addition & 0 deletions tools/test_input_focus.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void bind_reset(int dev) { (void)dev; unexpected_helper(); }
// (per-action bindings: none here, the Switch layer answers)
uint64_t act_scan_keys(uint64_t b, int (*held)(int)) { (void)b; (void)held; return 0; }
uint64_t act_scan_pad(uint64_t b, uint32_t host) { (void)b; (void)host; return 0; }
uint64_t act_gameplay_buttons(uint64_t raw, int (*held)(int), uint32_t host) { (void)held; (void)host; return raw; }
void act_publish(uint64_t keys, uint64_t pads) { (void)keys; (void)pads; }
void act_poll(bool live) { (void)live; }
void motion_script(float p, float r, float ps, float ys) { (void)p; (void)r; (void)ps; (void)ys; unexpected_helper(); }
Expand Down
Loading