mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-22 05:05:56 +00:00
Remap gamepad buttons and keys with bind_<button> lines in totk.ini, one data-driven table for the pad and one for keys
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d3589c904e
commit
e8697659ac
6 files changed
+201
-28
No files matched your search
+3
-1
@@ -23,7 +23,9 @@ static int n_entries;
|
||||
// Every name the runtime reads (and the patch sets in generated/), without the prefix. A file key outside this list is
|
||||
// logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line.
|
||||
static const char *const known[] = {
|
||||
"APP_POOL", "ASSUME_FOCUS", "AUDIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "BACKGROUND_WINDOW", "BIND_STATS",
|
||||
"APP_POOL", "ASSUME_FOCUS", "AUDIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT",
|
||||
"BIND_MINUS", "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y", "BIND_ZL",
|
||||
"BIND_ZR",
|
||||
"CLOTH_REALTIME", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT",
|
||||
"FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FPS60", "FREE_CORES", "GPL",
|
||||
"GPL_DRAW_BUILD",
|
||||
|
||||
+124
-11
@@ -1,22 +1,135 @@
|
||||
// Binding table and the pure mapping functions (input_map.h). No Win32 or SDL here.
|
||||
// Binding tables (keyboard/mouse and gamepad), their totk.ini overrides and the pure mapping functions (input_map.h).
|
||||
// No Win32 or SDL here.
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "cfg.h"
|
||||
#include "host.h"
|
||||
#include "input_map.h"
|
||||
|
||||
#define STICK_MAX 32767
|
||||
#define DIAGONAL 23170 // STICK_MAX / sqrt(2)
|
||||
#define MOUSE_FULL_CPS 3000.0
|
||||
|
||||
const Binding input_bindings[] = {
|
||||
{KEY_W, B_SL_UP}, {KEY_A, B_SL_LEFT}, {KEY_S, B_SL_DOWN}, {KEY_D, B_SL_RIGHT}, // move
|
||||
{KEY_UP, B_SR_UP}, {KEY_LEFT, B_SR_LEFT}, {KEY_DOWN, B_SR_DOWN}, {KEY_RIGHT, B_SR_RIGHT}, // camera, second source
|
||||
{KEY_MOUSE_L, B_Y}, {KEY_MOUSE_R, B_ZL}, {KEY_Z, B_ZL}, {KEY_F, B_ZR}, {KEY_X, B_ZR},
|
||||
{KEY_SPACE, B_X}, {KEY_LSHIFT, B_B}, {KEY_E, B_A}, {KEY_Q, B_L}, {KEY_R, B_R},
|
||||
{KEY_LCTRL, B_STICK_L}, {KEY_C, B_STICK_R},
|
||||
{KEY_1, B_LEFT}, {KEY_2, B_UP}, {KEY_3, B_RIGHT}, {KEY_4, B_DOWN},
|
||||
{KEY_TAB, B_PLUS}, {KEY_M, B_MINUS},
|
||||
};
|
||||
const int input_binding_count = (int)(sizeof input_bindings / sizeof *input_bindings);
|
||||
#define DEFAULT_KEYS \
|
||||
{KEY_W, B_SL_UP}, {KEY_A, B_SL_LEFT}, {KEY_S, B_SL_DOWN}, {KEY_D, B_SL_RIGHT}, /* move */ \
|
||||
{KEY_UP, B_SR_UP}, {KEY_LEFT, B_SR_LEFT}, {KEY_DOWN, B_SR_DOWN}, {KEY_RIGHT, B_SR_RIGHT}, /* camera, second source */ \
|
||||
{KEY_MOUSE_L, B_Y}, {KEY_MOUSE_R, B_ZL}, {KEY_Z, B_ZL}, {KEY_F, B_ZR}, {KEY_X, B_ZR}, \
|
||||
{KEY_SPACE, B_X}, {KEY_LSHIFT, B_B}, {KEY_E, B_A}, {KEY_Q, B_L}, {KEY_R, B_R}, \
|
||||
{KEY_LCTRL, B_STICK_L}, {KEY_C, B_STICK_R}, \
|
||||
{KEY_1, B_LEFT}, {KEY_2, B_UP}, {KEY_3, B_RIGHT}, {KEY_4, B_DOWN}, \
|
||||
{KEY_TAB, B_PLUS}, {KEY_M, B_MINUS}
|
||||
|
||||
// By position: south is Switch B, east A, west Y, north X (Nintendo layout); the triggers are ZL and ZR.
|
||||
#define DEFAULT_PADS \
|
||||
{PAD_SOUTH, B_B}, {PAD_EAST, B_A}, {PAD_WEST, B_Y}, {PAD_NORTH, B_X}, {PAD_LSTICK, B_STICK_L}, {PAD_RSTICK, B_STICK_R}, \
|
||||
{PAD_L, B_L}, {PAD_R, B_R}, {PAD_START, B_PLUS}, {PAD_BACK, B_MINUS}, {PAD_DPAD_LEFT, B_LEFT}, {PAD_DPAD_UP, B_UP}, \
|
||||
{PAD_DPAD_RIGHT, B_RIGHT}, {PAD_DPAD_DOWN, B_DOWN}, {PAD_ZL, B_ZL}, {PAD_ZR, B_ZR}
|
||||
|
||||
static const Binding default_keys[] = {DEFAULT_KEYS};
|
||||
static const PadBinding default_pads[] = {DEFAULT_PADS};
|
||||
Binding input_bindings[INPUT_BINDING_MAX] = {DEFAULT_KEYS};
|
||||
int input_binding_count = (int)(sizeof default_keys / sizeof *default_keys);
|
||||
PadBinding input_pad_bindings[INPUT_PAD_BINDING_MAX] = {DEFAULT_PADS};
|
||||
int input_pad_binding_count = (int)(sizeof default_pads / sizeof *default_pads);
|
||||
|
||||
static const char *const button_names[16] = {"A", "B", "X", "Y", "LCLICK", "RCLICK", "L", "R", "ZL", "ZR", "PLUS", "MINUS",
|
||||
"LEFT", "UP", "RIGHT", "DOWN"}; // indexed by action
|
||||
static const char *const pad_names[PAD_COUNT] = {
|
||||
NULL, "south", "east", "west", "north", "lstick", "rstick", "l", "r", "start", "back",
|
||||
"dpad_left", "dpad_up", "dpad_right", "dpad_down", "zl", "zr"};
|
||||
static const char *const key_names[KEY_COUNT] = { // letters and digits are handled in key_id
|
||||
[KEY_SPACE] = "space", [KEY_TAB] = "tab", [KEY_LSHIFT] = "lshift", [KEY_LCTRL] = "lctrl", [KEY_UP] = "up",
|
||||
[KEY_DOWN] = "down", [KEY_LEFT] = "left", [KEY_RIGHT] = "right", [KEY_MOUSE_L] = "mouse_l", [KEY_MOUSE_R] = "mouse_r",
|
||||
[KEY_MOUSE_M] = "mouse_m"};
|
||||
|
||||
void input_map_reset(void) {
|
||||
memcpy(input_bindings, default_keys, sizeof default_keys);
|
||||
input_binding_count = (int)(sizeof default_keys / sizeof *default_keys);
|
||||
memcpy(input_pad_bindings, default_pads, sizeof default_pads);
|
||||
input_pad_binding_count = (int)(sizeof default_pads / sizeof *default_pads);
|
||||
}
|
||||
|
||||
static int key_id(const char *name) {
|
||||
if (name[0] && !name[1]) {
|
||||
int c = tolower((unsigned char)name[0]);
|
||||
if (c >= 'a' && c <= 'z') return KEY_A + (c - 'a');
|
||||
if (c >= '0' && c <= '9') return KEY_0 + (c - '0');
|
||||
}
|
||||
for (int k = KEY_SPACE; k < KEY_COUNT; k++)
|
||||
if (key_names[k] && !_stricmp(key_names[k], name)) return k;
|
||||
return KEY_NONE;
|
||||
}
|
||||
|
||||
static int pad_id(const char *name) {
|
||||
for (int p = PAD_SOUTH; p < PAD_COUNT; p++)
|
||||
if (!_stricmp(pad_names[p], name)) return p;
|
||||
return PAD_NONE;
|
||||
}
|
||||
|
||||
static char *trim(char *s) {
|
||||
while (*s == ' ' || *s == '\t') s++;
|
||||
char *e = s + strlen(s);
|
||||
while (e > s && (e[-1] == ' ' || e[-1] == '\t' || e[-1] == '\r')) *--e = 0;
|
||||
return s;
|
||||
}
|
||||
|
||||
int input_map_bind(const char *button, const char *list) {
|
||||
int action = -1;
|
||||
for (int a = 0; a < 16; a++)
|
||||
if (!_stricmp(button_names[a], button)) action = a;
|
||||
if (action < 0) { rt_log("input: bind: unknown button '%s'", button); return 0; }
|
||||
Binding keys[INPUT_BINDING_MAX];
|
||||
PadBinding pads[INPUT_PAD_BINDING_MAX];
|
||||
int nk = 0, np = 0, none = 0, bad = 0;
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof buf, "%s", list);
|
||||
for (char *save = NULL, *tok = strtok_s(buf, ",", &save); tok; tok = strtok_s(NULL, ",", &save)) {
|
||||
tok = trim(tok);
|
||||
if (!_stricmp(tok, "none")) { none = 1; continue; }
|
||||
int ok = 0;
|
||||
if (!_strnicmp(tok, "pad:", 4)) {
|
||||
int p = pad_id(trim(tok + 4));
|
||||
if (p && np < INPUT_PAD_BINDING_MAX) pads[np++] = (PadBinding){(uint8_t)p, (uint8_t)action}, ok = 1;
|
||||
} else if (!_strnicmp(tok, "key:", 4)) {
|
||||
int k = key_id(trim(tok + 4));
|
||||
if (k && nk < INPUT_BINDING_MAX) keys[nk++] = (Binding){(uint8_t)k, (uint8_t)action}, ok = 1;
|
||||
}
|
||||
if (!ok) { bad++; rt_log("input: bind %s: ignoring '%s' (expected pad:<name> or key:<name>)", button, tok); }
|
||||
}
|
||||
if (!nk && !np && !none) { if (bad) rt_log("input: bind %s: no valid entry, keeping the defaults", button); return 0; }
|
||||
int w = 0;
|
||||
for (int i = 0; i < input_binding_count; i++)
|
||||
if (input_bindings[i].action != action) input_bindings[w++] = input_bindings[i];
|
||||
input_binding_count = w;
|
||||
for (int i = 0; i < nk && input_binding_count < INPUT_BINDING_MAX; i++) input_bindings[input_binding_count++] = keys[i];
|
||||
w = 0;
|
||||
for (int i = 0; i < input_pad_binding_count; i++)
|
||||
if (input_pad_bindings[i].action != action) input_pad_bindings[w++] = input_pad_bindings[i];
|
||||
input_pad_binding_count = w;
|
||||
for (int i = 0; i < np && input_pad_binding_count < INPUT_PAD_BINDING_MAX; i++)
|
||||
input_pad_bindings[input_pad_binding_count++] = pads[i];
|
||||
return 1;
|
||||
}
|
||||
|
||||
void input_map_configure(void) {
|
||||
input_map_reset();
|
||||
for (int a = 0; a < 16; a++) {
|
||||
char var[32];
|
||||
snprintf(var, sizeof var, "TOTK_BIND_%s", button_names[a]);
|
||||
const char *v = cfg_get(var);
|
||||
if (v && input_map_bind(button_names[a], v)) rt_log("input: %s = %s", var + 5, v);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t input_map_pad_buttons(const uint8_t down[PAD_COUNT]) {
|
||||
uint64_t b = 0;
|
||||
for (int i = 0; i < input_pad_binding_count; i++)
|
||||
if (down[input_pad_bindings[i].pad]) b |= 1ull << input_pad_bindings[i].action;
|
||||
return b;
|
||||
}
|
||||
|
||||
uint64_t input_map_buttons(const uint8_t down[KEY_COUNT]) {
|
||||
uint64_t b = 0;
|
||||
|
||||
+27
-2
@@ -22,9 +22,34 @@ enum {
|
||||
KEY_COUNT
|
||||
};
|
||||
|
||||
// Gamepad inputs by position (Nintendo layout, as SDL names them), platform-neutral like the key ids. ZL and ZR are the
|
||||
// analog triggers; input_win.c translates SDL buttons and axes to these (one place).
|
||||
enum {
|
||||
PAD_NONE = 0,
|
||||
PAD_SOUTH, PAD_EAST, PAD_WEST, PAD_NORTH, PAD_LSTICK, PAD_RSTICK, PAD_L, PAD_R, PAD_START, PAD_BACK,
|
||||
PAD_DPAD_LEFT, PAD_DPAD_UP, PAD_DPAD_RIGHT, PAD_DPAD_DOWN, PAD_ZL, PAD_ZR,
|
||||
PAD_COUNT
|
||||
};
|
||||
|
||||
enum { INPUT_BINDING_MAX = 96, INPUT_PAD_BINDING_MAX = 48 };
|
||||
|
||||
// The tables a remapping page edits. Defaults are the layout in docs/controls.md; input_map_reset restores them.
|
||||
typedef struct { uint8_t key; uint8_t action; } Binding;
|
||||
extern const Binding input_bindings[];
|
||||
extern const int input_binding_count;
|
||||
typedef struct { uint8_t pad; uint8_t action; } PadBinding;
|
||||
extern Binding input_bindings[INPUT_BINDING_MAX];
|
||||
extern int input_binding_count;
|
||||
extern PadBinding input_pad_bindings[INPUT_PAD_BINDING_MAX];
|
||||
extern int input_pad_binding_count;
|
||||
|
||||
void input_map_reset(void);
|
||||
// Replaces every binding of the Switch button `button` (script name: A B X Y LCLICK RCLICK L R ZL ZR PLUS MINUS LEFT UP
|
||||
// RIGHT DOWN, any case) with `list`: comma separated "pad:<name>" and "key:<name>" entries, or "none" for no input.
|
||||
// Invalid entries are logged and skipped; when none is valid the button keeps its bindings. 1 when applied.
|
||||
int input_map_bind(const char *button, const char *list);
|
||||
// Applies the TOTK_BIND_<button> settings (cfg_get) over the defaults. Once, at start.
|
||||
void input_map_configure(void);
|
||||
// down[pad] != 0 for the gamepad inputs held. Returns the Switch button bits (0..15) the pad table presses.
|
||||
uint64_t input_map_pad_buttons(const uint8_t down[PAD_COUNT]);
|
||||
|
||||
// down[key] != 0 for the keys held. Returns the Switch button bits (0..15) the table presses.
|
||||
uint64_t input_map_buttons(const uint8_t down[KEY_COUNT]);
|
||||
|
||||
+16
-13
@@ -1,6 +1,6 @@
|
||||
// Host input for nn::hid (runtime/ov_input.c): an SDL3 gamepad (no SDL window, no SDL video), the keyboard and the mouse,
|
||||
// merged into Switch npad button bits and stick axes. Button order follows libnx HidNpadButton (ISC). The key bindings are
|
||||
// the table in input_map.c; this file translates Win32 virtual keys to its key ids, reads the mouse (raw input, WM_INPUT)
|
||||
// merged into Switch npad button bits and stick axes. Button order follows libnx HidNpadButton (ISC). The key and pad bindings
|
||||
// are the tables in input_map.c (totk.ini overrides them); this file translates Win32 virtual keys to its key ids, reads the mouse (raw input, WM_INPUT)
|
||||
// and owns the cursor capture. Keys and their map: docs/controls.md.
|
||||
// The gamepad also rumbles (input_rumble) and, when it has a gyro and an accelerometer, feeds the six-axis sensor from a
|
||||
// thread of its own (input_motion_start). Every SDL call on the gamepad is made under `lock`.
|
||||
@@ -29,13 +29,14 @@ static SDL_JoystickID sensor_pad; // the gamepad whose sensors the motion thr
|
||||
static int sensor_ok; // ... and it has both sensors, enabled
|
||||
static int motion_thread; // 0 not started, 1 running, -1 off (TOTK_MOTION=0, no SDL)
|
||||
|
||||
// Gamepad buttons by position: south is Switch B, east A, west Y, north X (Nintendo layout).
|
||||
static const struct { SDL_GamepadButton sdl; int bit; } pad_buttons[] = {
|
||||
{SDL_GAMEPAD_BUTTON_SOUTH, B_B}, {SDL_GAMEPAD_BUTTON_EAST, B_A}, {SDL_GAMEPAD_BUTTON_WEST, B_Y},
|
||||
{SDL_GAMEPAD_BUTTON_NORTH, B_X}, {SDL_GAMEPAD_BUTTON_LEFT_STICK, B_STICK_L}, {SDL_GAMEPAD_BUTTON_RIGHT_STICK, B_STICK_R},
|
||||
{SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, B_L}, {SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, B_R}, {SDL_GAMEPAD_BUTTON_START, B_PLUS},
|
||||
{SDL_GAMEPAD_BUTTON_BACK, B_MINUS}, {SDL_GAMEPAD_BUTTON_DPAD_LEFT, B_LEFT}, {SDL_GAMEPAD_BUTTON_DPAD_UP, B_UP},
|
||||
{SDL_GAMEPAD_BUTTON_DPAD_RIGHT, B_RIGHT}, {SDL_GAMEPAD_BUTTON_DPAD_DOWN, B_DOWN},
|
||||
// SDL buttons of the gamepad inputs (input_map.h PAD_*), by position; PAD_ZL and PAD_ZR are the triggers (axes).
|
||||
static const SDL_GamepadButton pad_sdl[PAD_COUNT] = {
|
||||
[PAD_SOUTH] = SDL_GAMEPAD_BUTTON_SOUTH, [PAD_EAST] = SDL_GAMEPAD_BUTTON_EAST, [PAD_WEST] = SDL_GAMEPAD_BUTTON_WEST,
|
||||
[PAD_NORTH] = SDL_GAMEPAD_BUTTON_NORTH, [PAD_LSTICK] = SDL_GAMEPAD_BUTTON_LEFT_STICK,
|
||||
[PAD_RSTICK] = SDL_GAMEPAD_BUTTON_RIGHT_STICK, [PAD_L] = SDL_GAMEPAD_BUTTON_LEFT_SHOULDER,
|
||||
[PAD_R] = SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, [PAD_START] = SDL_GAMEPAD_BUTTON_START, [PAD_BACK] = SDL_GAMEPAD_BUTTON_BACK,
|
||||
[PAD_DPAD_LEFT] = SDL_GAMEPAD_BUTTON_DPAD_LEFT, [PAD_DPAD_UP] = SDL_GAMEPAD_BUTTON_DPAD_UP,
|
||||
[PAD_DPAD_RIGHT] = SDL_GAMEPAD_BUTTON_DPAD_RIGHT, [PAD_DPAD_DOWN] = SDL_GAMEPAD_BUTTON_DPAD_DOWN,
|
||||
};
|
||||
|
||||
// TOTK_INPUT_SCRIPT=<file>: lines "<frame> <button> [hold]" or "<frame> LSTICK|RSTICK <x> <y> [hold]", ORed into the real input.
|
||||
@@ -337,6 +338,7 @@ static void scan_pads(void) {
|
||||
}
|
||||
|
||||
static void start(void) {
|
||||
input_map_configure();
|
||||
const char *e = cfg_get("TOTK_INPUT");
|
||||
if (e && !strcmp(e, "none")) { state = -1; rt_log("input: disabled (TOTK_INPUT=none)"); return; }
|
||||
if (!SDL_Init(SDL_INIT_GAMEPAD)) { state = -1; rt_log("input: SDL gamepad init failed (%s), keyboard only", SDL_GetError()); }
|
||||
@@ -370,10 +372,11 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx
|
||||
if ((!pad || !SDL_GamepadConnected(pad)) && GetTickCount64() >= next_scan) { next_scan = GetTickCount64() + RESCAN_MS; scan_pads(); }
|
||||
SDL_UpdateGamepads();
|
||||
if (pad && SDL_GamepadConnected(pad)) {
|
||||
for (size_t i = 0; i < sizeof pad_buttons / sizeof *pad_buttons; i++)
|
||||
if (SDL_GetGamepadButton(pad, pad_buttons[i].sdl)) b |= 1ull << pad_buttons[i].bit;
|
||||
if (SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) > TRIGGER_ON) b |= 1ull << B_ZL;
|
||||
if (SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) > TRIGGER_ON) b |= 1ull << B_ZR;
|
||||
uint8_t pd[PAD_COUNT] = {0};
|
||||
for (int p = PAD_SOUTH; p < PAD_ZL; p++) pd[p] = SDL_GetGamepadButton(pad, pad_sdl[p]);
|
||||
pd[PAD_ZL] = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) > TRIGGER_ON;
|
||||
pd[PAD_ZR] = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) > TRIGGER_ON;
|
||||
b |= input_map_pad_buttons(pd);
|
||||
sl[0] = axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX));
|
||||
sl[1] = -axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY));
|
||||
sr[0] = axis(SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHTX));
|
||||
|
||||
Reference in new issue
Block a user