diff --git a/runtime/input.c b/runtime/input.c index 3f539acf..4e776858 100644 --- a/runtime/input.c +++ b/runtime/input.c @@ -767,15 +767,54 @@ static int pad_physical(uint8_t pd[PAD_COUNT]) { // Under lock, state 1: the open gamepad's buttons (Switch bits after TOTK_PAD_LAYOUT and the TOTK_BIND_* lines) and // sticks; 0 without a connected gamepad. +// Under lock: the last pad_read's inputs by position, [PAD_GUIDE] the Home / Guide button (no Switch button), and its +// left and right trigger axes, 0..32767: what the gamepad combos and the free camera read +#define PAD_GUIDE PAD_COUNT +static uint8_t pad_seen[PAD_COUNT + 1]; +static int16_t trigger_seen[2]; + static int pad_read(uint64_t *b, int32_t sl[2], int32_t sr[2]) { uint8_t pd[PAD_COUNT] = {0}; - if (!pad_physical(pd)) return 0; + if (!pad_physical(pd)) { + memset(pad_seen, 0, sizeof pad_seen); + memset(trigger_seen, 0, sizeof trigger_seen); + return 0; + } + memcpy(pad_seen, pd, sizeof pd); + pad_seen[PAD_GUIDE] = SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_GUIDE); + trigger_seen[0] = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER); + trigger_seen[1] = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER); *b |= input_map_pad_buttons(pd, pad_layout_get()); stick(SDL_GAMEPAD_AXIS_LEFTX, SDL_GAMEPAD_AXIS_LEFTY, stick_dead_get(0), sl); stick(SDL_GAMEPAD_AXIS_RIGHTX, SDL_GAMEPAD_AXIS_RIGHTY, stick_dead_get(1), sr); return 1; } +uint32_t input_pad_combo(const char *env, const char *s) { + uint32_t mask = 0; + char name[32]; + size_t n = 0; + for (const char *c = s;; c++) { + if (*c && *c != '+' && *c != ' ') { + if (n < sizeof name - 1) name[n++] = *c; + continue; + } + if (n) { + name[n] = 0; + int p = !strcasecmp(name, "guide") || !strcasecmp(name, "home") ? PAD_GUIDE : input_pad_id(name); + if (p == PAD_NONE) { + rt_log("input: %s=%s: %s is not a gamepad button (south east west north lstick rstick " + "l r zl zr start back guide dpad_*, paddles)", env, s, name); + return 0; + } + mask |= 1u << p; + n = 0; + } + if (!*c) break; + } + return mask; +} + int input_pad_stick_percent(double pct[2]) { int ok = 0; pct[0] = pct[1] = 0; diff --git a/runtime/input.h b/runtime/input.h index e2b61a6a..2316d5df 100644 --- a/runtime/input.h +++ b/runtime/input.h @@ -91,6 +91,10 @@ void input_menu(int open); // of the combo, sticks are lx, ly, rx, ry. Returns 1 in the poll where the combo goes down (toggle the menu once). int input_menu_gate(int *held, int menu_open, uint64_t *buttons, int32_t sticks[4]); int input_overlay_gate(int *held, uint64_t *buttons); // L3+R3: 1 in the poll both go down +// A gamepad combo setting: buttons by position joined with '+' (guide+south: Home and the bottom face button, the +// pad: names of the bind_ lines plus guide or home). Bit p is PAD_* p, bit PAD_COUNT the Home button; 0 for an empty +// value or one with a name that is no button, which logs a line naming env. +uint32_t input_pad_combo(const char *env, const char *s); int input_pad_read(uint64_t *buttons, int32_t *lx, int32_t *ly); // The gamepad layout in effect (PAD_LAYOUT_*, input_map.h; TOTK_PAD_LAYOUT, live); -1 without a gamepad. Any thread. int input_pad_layout(void); diff --git a/runtime/input_map.c b/runtime/input_map.c index 5d5ae885..44009ba7 100644 --- a/runtime/input_map.c +++ b/runtime/input_map.c @@ -66,13 +66,13 @@ const char *input_pad_name(int pad) { return pad > PAD_NONE && pad < PAD_COUNT ? void input_map_reset(void) { input_map = defaults; } -static int key_id(const char *name) { +int input_key_id(const char *name) { for (int k = KEY_A; k < KEY_COUNT; k++) if (key_names[k] && !strcasecmp(key_names[k], name)) return k; return KEY_NONE; } -static int pad_id(const char *name) { +int input_pad_id(const char *name) { for (int p = PAD_SOUTH; p < PAD_COUNT; p++) if (!strcasecmp(pad_names[p], name)) return p; return PAD_NONE; @@ -133,12 +133,12 @@ int input_map_bind(const char *button, const char *list) { int ok = 0; if (!strncasecmp(tok, "pad:", 4) && action < B_SL_LEFT) { const char *name = trim(tok + 4); - int p = pad_id(name); + int p = input_pad_id(name); if (!strcasecmp(name, "default")) pad_def = ok = 1; else if (p && np < INPUT_PAD_BINDING_MAX) pads[np++] = (uint8_t)p, ok = 1; } else if (!strncasecmp(tok, "key:", 4)) { const char *name = trim(tok + 4); - int k = key_id(name); + int k = input_key_id(name); if (!strcasecmp(name, "default")) key_def = ok = 1; else if (k && nk < INPUT_BINDING_MAX) keys[nk++] = (uint8_t)k, ok = 1; } diff --git a/runtime/input_map.h b/runtime/input_map.h index bb660ea6..376be4c9 100644 --- a/runtime/input_map.h +++ b/runtime/input_map.h @@ -73,6 +73,8 @@ void input_map_reset(void); // stick directions the keys drive), then SHAKE. Keys: a-z 0-9 space tab lshift ... (key_names in input_map.c). Pads: south east ... const char *input_action_name(int action); const char *input_key_name(int key); +int input_key_id(const char *name); // KEY_NONE for a name without a key +int input_pad_id(const char *name); // PAD_NONE for a name without a gamepad button const char *input_pad_name(int pad); // Replaces every binding of the action `button` (an action name) with `list`: comma separated "pad:" and // "key:" entries, "key:default" / "pad:default" for that part's defaults, or "none" for no input. A part the list diff --git a/runtime/ui/binds_page.cpp b/runtime/ui/binds_page.cpp index 700b0f2a..0e57a0fc 100644 --- a/runtime/ui/binds_page.cpp +++ b/runtime/ui/binds_page.cpp @@ -122,6 +122,8 @@ static const char *pad_label(int p) { return menu_text((UiTextId)(UI_PAD_SOUTH + (p - PAD_SOUTH))); } +const char *binds_pad_label(int pad) { return pad_label(pad); } + // The layout the gamepad cells show: the one in effect, or without a gamepad the one the setting names (Xbox for auto). static int shown_layout(void) { int l = input_pad_layout(); diff --git a/runtime/ui/binds_page.h b/runtime/ui/binds_page.h index 219cb0f9..5cca0665 100644 --- a/runtime/ui/binds_page.h +++ b/runtime/ui/binds_page.h @@ -15,3 +15,5 @@ bool binds_modal(void); // A key setting (TOTK_ name) as a row of one key cell, set by pressing the key as a binding's cell is; Delete sets it // to none. note (NULL none) follows the description. void binds_key_row(const char *setting, UiTextId label, UiTextId desc, const char *note); +// A gamepad input by position (PAD_*): the label the open pad has on a face button, else its place on the pad. +const char *binds_pad_label(int pad);