// Host input for nn::hid (runtime/ov_input.c): an SDL3 gamepad (no SDL window, no SDL video) and the keyboard, merged // into Switch npad button bits and stick axes. Button order follows libnx HidNpadButton (ISC). 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`. #include #include #include #include #include #include #include "cfg.h" #include "host.h" #include "input.h" #include "nvn_api.h" #include "vk.h" enum { B_A = 0, B_B, B_X, B_Y, B_STICK_L, B_STICK_R, B_L, B_R, B_ZL, B_ZR, B_PLUS, B_MINUS, B_LEFT, B_UP, B_RIGHT, B_DOWN, B_SL_LEFT = 16, B_SL_UP, B_SL_RIGHT, B_SL_DOWN, B_SR_LEFT, B_SR_UP, B_SR_RIGHT, B_SR_DOWN, STICK_MAX = 32767, STICK_DEAD = 2500, STICK_DIR = 16384, TRIGGER_ON = 16384, RESCAN_MS = 1000 }; static SRWLOCK lock = SRWLOCK_INIT; static int state; // 0 not started, 1 running, -1 disabled or SDL failed static SDL_Gamepad *pad; static uint64_t next_scan; static int pad_rumble; // the open gamepad has rumble motors static SDL_JoystickID sensor_pad; // the gamepad whose sensors the motion thread set up, 0 none 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) typedef struct { int vk; int bit; } KeyBtn; static const KeyBtn key_buttons[] = { {VK_LEFT, B_LEFT}, {VK_UP, B_UP}, {VK_RIGHT, B_RIGHT}, {VK_DOWN, B_DOWN}, {'E', B_A}, {VK_LSHIFT, B_B}, {VK_SPACE, B_X}, {'F', B_Y}, {'Q', B_ZL}, {'Z', B_ZR}, {VK_TAB, B_L}, {'R', B_R}, {'C', B_STICK_L}, {'V', B_STICK_R}, {VK_RETURN, B_PLUS}, {VK_BACK, B_MINUS}, }; // 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}, }; // TOTK_INPUT_SCRIPT=: lines "