mirror of
https://github.com/sp00nznet/xboxrecomp.git
synced 2026-12-04 01:16:20 +00:00
feat(input): add optional portable controller mapping rules
Extract the Def Jam mapping utility without changing existing readers or runtime defaults. Define stable library key IDs and adapter contracts; remove shared name buffers and binding truncation, and use wide stick magnitude arithmetic. Add standalone C/C++ fixtures and integration docs.
This commit is contained in:
@@ -18,3 +18,6 @@ if(MSVC)
|
||||
else()
|
||||
target_compile_options(xbox_input PRIVATE -Wall -Wextra -O2)
|
||||
endif()
|
||||
|
||||
# Independent mapping rules; linked explicitly by adopting ports.
|
||||
add_subdirectory(mapping)
|
||||
|
||||
@@ -0,0 +1,391 @@
|
||||
/* Host input to Xbox controller rules. See input_map.h. */
|
||||
|
||||
#include "input_map.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* ---- control names ------------------------------------------------------- */
|
||||
|
||||
static const char *const k_control_names[INPUT_CONTROL_COUNT] = {
|
||||
"dpad_up", "dpad_down", "dpad_left", "dpad_right",
|
||||
"start", "back", "left_thumb", "right_thumb",
|
||||
"a", "b", "x", "y", "black", "white",
|
||||
"left_trigger", "right_trigger",
|
||||
"left_stick_up", "left_stick_down", "left_stick_left", "left_stick_right",
|
||||
"right_stick_up", "right_stick_down", "right_stick_left", "right_stick_right",
|
||||
};
|
||||
|
||||
const char *xbox_InputMap_ControlName(int control)
|
||||
{
|
||||
return control >= 0 && control < INPUT_CONTROL_COUNT ? k_control_names[control] : NULL;
|
||||
}
|
||||
|
||||
static int ascii_lower(int c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; }
|
||||
static int ascii_upper(int c) { return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c; }
|
||||
|
||||
static int equal_nocase(const char *a, const char *b)
|
||||
{
|
||||
for (; *a && *b; a++, b++)
|
||||
if (ascii_lower((unsigned char)*a) != ascii_lower((unsigned char)*b)) return 0;
|
||||
return *a == *b;
|
||||
}
|
||||
|
||||
/* ---- merging ------------------------------------------------------------- */
|
||||
|
||||
static int64_t stick_mag2(int x, int y) { return (int64_t)x * x + (int64_t)y * y; }
|
||||
|
||||
void xbox_InputMap_PadMerge(InputPad *dst, const InputPad *src)
|
||||
{
|
||||
int i;
|
||||
dst->buttons |= src->buttons;
|
||||
for (i = 0; i < 8; i++)
|
||||
if (src->analog[i] > dst->analog[i]) dst->analog[i] = src->analog[i];
|
||||
if (stick_mag2(src->lx, src->ly) > stick_mag2(dst->lx, dst->ly)) {
|
||||
dst->lx = src->lx; dst->ly = src->ly;
|
||||
}
|
||||
if (stick_mag2(src->rx, src->ry) > stick_mag2(dst->rx, dst->ry)) {
|
||||
dst->rx = src->rx; dst->ry = src->ry;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- keys ---------------------------------------------------------------- */
|
||||
|
||||
typedef struct KeyName { const char *name; int code; } KeyName;
|
||||
|
||||
/* The first name listed for a code is the one written back. Letters, digits,
|
||||
* function keys and the numeric pad's digits are generated rather than listed. */
|
||||
static const KeyName k_keys[] = {
|
||||
{ "mouse1", INPUT_KEY_MOUSE1 }, { "mouse2", INPUT_KEY_MOUSE2 }, { "mouse3", INPUT_KEY_MOUSE3 },
|
||||
{ "mouse4", INPUT_KEY_MOUSE4 }, { "mouse5", INPUT_KEY_MOUSE5 },
|
||||
{ "wheelup", INPUT_KEY_WHEEL_UP }, { "wheeldown", INPUT_KEY_WHEEL_DOWN },
|
||||
{ "backspace", INPUT_KEY_BACKSPACE }, { "tab", INPUT_KEY_TAB }, { "enter", INPUT_KEY_ENTER }, { "return", INPUT_KEY_ENTER },
|
||||
{ "shift", INPUT_KEY_SHIFT }, { "ctrl", INPUT_KEY_CTRL }, { "control", INPUT_KEY_CTRL }, { "alt", INPUT_KEY_ALT },
|
||||
{ "pause", INPUT_KEY_PAUSE }, { "capslock", INPUT_KEY_CAPSLOCK },
|
||||
{ "escape", INPUT_KEY_ESCAPE }, { "esc", INPUT_KEY_ESCAPE }, { "space", INPUT_KEY_SPACE },
|
||||
{ "pageup", INPUT_KEY_PAGEUP }, { "pagedown", INPUT_KEY_PAGEDOWN }, { "end", INPUT_KEY_END }, { "home", INPUT_KEY_HOME },
|
||||
{ "left", INPUT_KEY_LEFT }, { "up", INPUT_KEY_UP }, { "right", INPUT_KEY_RIGHT }, { "down", INPUT_KEY_DOWN },
|
||||
{ "insert", INPUT_KEY_INSERT }, { "delete", INPUT_KEY_DELETE },
|
||||
{ "numpadmultiply", INPUT_KEY_NUMPADMULTIPLY }, { "numpadadd", INPUT_KEY_NUMPADADD }, { "numpadsubtract", INPUT_KEY_NUMPADSUBTRACT },
|
||||
{ "numpaddecimal", INPUT_KEY_NUMPADDECIMAL }, { "numpaddivide", INPUT_KEY_NUMPADDIVIDE },
|
||||
{ "lshift", INPUT_KEY_LSHIFT }, { "rshift", INPUT_KEY_RSHIFT }, { "lctrl", INPUT_KEY_LCTRL }, { "rctrl", INPUT_KEY_RCTRL },
|
||||
{ "lalt", INPUT_KEY_LALT }, { "ralt", INPUT_KEY_RALT },
|
||||
{ "semicolon", INPUT_KEY_SEMICOLON }, { "equals", INPUT_KEY_EQUALS }, { "comma", INPUT_KEY_COMMA }, { "minus", INPUT_KEY_MINUS },
|
||||
{ "period", INPUT_KEY_PERIOD }, { "slash", INPUT_KEY_SLASH }, { "grave", INPUT_KEY_GRAVE },
|
||||
{ "leftbracket", INPUT_KEY_LEFTBRACKET }, { "backslash", INPUT_KEY_BACKSLASH }, { "rightbracket", INPUT_KEY_RIGHTBRACKET },
|
||||
{ "quote", INPUT_KEY_QUOTE },
|
||||
};
|
||||
#define KEY_TABLE_COUNT ((int)(sizeof(k_keys) / sizeof(k_keys[0])))
|
||||
|
||||
/* "f5" or "numpad3": a fixed prefix and a number with no leading zero. */
|
||||
static int numbered(const char *name, const char *prefix, int lo, int hi)
|
||||
{
|
||||
size_t n = strlen(prefix);
|
||||
char tail[8];
|
||||
int v;
|
||||
if (strlen(name) <= n || strlen(name) > n + 2) return -1;
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++)
|
||||
if (ascii_lower((unsigned char)name[i]) != prefix[i]) return -1;
|
||||
}
|
||||
v = atoi(name + n);
|
||||
snprintf(tail, sizeof(tail), "%d", v);
|
||||
if (strcmp(tail, name + n) || v < lo || v > hi) return -1;
|
||||
return v;
|
||||
}
|
||||
|
||||
int xbox_InputMap_KeyFromName(const char *name)
|
||||
{
|
||||
int i, v;
|
||||
if (!name || !*name) return -1;
|
||||
if (name[1] == 0) {
|
||||
char c = (char)ascii_upper((unsigned char)name[0]);
|
||||
if (c >= 'A' && c <= 'Z') return INPUT_KEY_A + c - 'A';
|
||||
if (c >= '0' && c <= '9') return INPUT_KEY_0 + c - '0';
|
||||
return -1;
|
||||
}
|
||||
v = numbered(name, "f", 1, 12);
|
||||
if (v >= 0) return INPUT_KEY_F1 + v - 1;
|
||||
v = numbered(name, "numpad", 0, 9);
|
||||
if (v >= 0) return INPUT_KEY_NUMPAD0 + v;
|
||||
for (i = 0; i < KEY_TABLE_COUNT; i++)
|
||||
if (equal_nocase(name, k_keys[i].name)) return k_keys[i].code;
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *xbox_InputMap_KeyName(int code)
|
||||
{
|
||||
static const char *const letters[] = {
|
||||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
|
||||
};
|
||||
static const char *const digits[] = {
|
||||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
|
||||
};
|
||||
static const char *const functions[] = {
|
||||
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"
|
||||
};
|
||||
static const char *const numpad[] = {
|
||||
"Numpad0", "Numpad1", "Numpad2", "Numpad3", "Numpad4", "Numpad5", "Numpad6", "Numpad7", "Numpad8", "Numpad9"
|
||||
};
|
||||
int i;
|
||||
if (code >= INPUT_KEY_A && code <= INPUT_KEY_Z) return letters[code - INPUT_KEY_A];
|
||||
if (code >= INPUT_KEY_0 && code <= INPUT_KEY_9) return digits[code - INPUT_KEY_0];
|
||||
if (code >= INPUT_KEY_F1 && code <= INPUT_KEY_F12) return functions[code - INPUT_KEY_F1];
|
||||
if (code >= INPUT_KEY_NUMPAD0 && code <= INPUT_KEY_NUMPAD9) return numpad[code - INPUT_KEY_NUMPAD0];
|
||||
for (i = 0; i < KEY_TABLE_COUNT; i++)
|
||||
if (k_keys[i].code == code) return k_keys[i].name;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int xbox_InputMap_BindingsParse(InputBindings *b, int control, const char *text)
|
||||
{
|
||||
int rejected = 0;
|
||||
if (!b || control < 0 || control >= INPUT_CONTROL_COUNT) return -1;
|
||||
b->count[control] = 0;
|
||||
if (!text) return 0;
|
||||
for (;;) {
|
||||
const char *end = strchr(text, ',');
|
||||
const char *first = text, *last = end ? end : text + strlen(text);
|
||||
char token[32];
|
||||
size_t len;
|
||||
int code, j, dup = 0;
|
||||
while (first < last && isspace((unsigned char)*first)) first++;
|
||||
while (last > first && isspace((unsigned char)last[-1])) last--;
|
||||
len = (size_t)(last - first);
|
||||
if (len) {
|
||||
if (len >= sizeof(token)) { rejected++; }
|
||||
else {
|
||||
memcpy(token, first, len); token[len] = 0;
|
||||
if (!equal_nocase(token, "none")) {
|
||||
code = xbox_InputMap_KeyFromName(token);
|
||||
if (code < 0) rejected++;
|
||||
else {
|
||||
for (j = 0; j < b->count[control]; j++)
|
||||
if (b->key[control][j] == code) dup = 1;
|
||||
if (!dup) {
|
||||
if (b->count[control] < INPUT_BINDINGS_PER_CONTROL)
|
||||
b->key[control][b->count[control]++] = (uint16_t)code;
|
||||
else rejected++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!end) break;
|
||||
text = end + 1;
|
||||
}
|
||||
return rejected;
|
||||
}
|
||||
|
||||
const char *xbox_InputMap_BindingsFormat(const InputBindings *b, int control, char *buf, int size)
|
||||
{
|
||||
int i, used = 0;
|
||||
if (!buf || size <= 0) return buf;
|
||||
buf[0] = 0;
|
||||
if (!b || control < 0 || control >= INPUT_CONTROL_COUNT || !b->count[control]) {
|
||||
snprintf(buf, (size_t)size, "none");
|
||||
return buf;
|
||||
}
|
||||
for (i = 0; i < b->count[control] && i < INPUT_BINDINGS_PER_CONTROL; i++) {
|
||||
const char *n = xbox_InputMap_KeyName(b->key[control][i]);
|
||||
int w = snprintf(buf + used, (size_t)(size - used), "%s%s", i ? ", " : "",
|
||||
n ? n : "?");
|
||||
if (w < 0 || w >= size - used) break;
|
||||
used += w;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int control_held(const InputBindings *b, const uint8_t *down, int control)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < b->count[control] && i < INPUT_BINDINGS_PER_CONTROL; i++) {
|
||||
int k = b->key[control][i];
|
||||
if (k > INPUT_KEY_NONE && k < INPUT_KEY_COUNT && down[k]) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A stick from four keys: a unit-circle deflection, so a diagonal is as fast as
|
||||
* a straight line (32767 * 0.7071 on each axis). */
|
||||
static void keys_stick(int right, int left_k, int up, int down_k, int16_t *ox, int16_t *oy)
|
||||
{
|
||||
int x = (right ? 1 : 0) - (left_k ? 1 : 0);
|
||||
int y = (up ? 1 : 0) - (down_k ? 1 : 0);
|
||||
if (x && y) {
|
||||
*ox = (int16_t)(x * 23169);
|
||||
*oy = (int16_t)(y * 23169);
|
||||
} else {
|
||||
*ox = (int16_t)(x * 32767);
|
||||
*oy = (int16_t)(y * 32767);
|
||||
}
|
||||
}
|
||||
|
||||
void xbox_InputMap_KeysResolve(const InputBindings *b, const uint8_t *down, InputPad *out)
|
||||
{
|
||||
static const struct { int control; uint16_t bit; } digital[8] = {
|
||||
{ INPUT_DPAD_UP, INPUT_PAD_DPAD_UP }, { INPUT_DPAD_DOWN, INPUT_PAD_DPAD_DOWN },
|
||||
{ INPUT_DPAD_LEFT, INPUT_PAD_DPAD_LEFT }, { INPUT_DPAD_RIGHT, INPUT_PAD_DPAD_RIGHT },
|
||||
{ INPUT_START, INPUT_PAD_START }, { INPUT_BACK, INPUT_PAD_BACK },
|
||||
{ INPUT_LEFT_THUMB, INPUT_PAD_LEFT_THUMB }, { INPUT_RIGHT_THUMB, INPUT_PAD_RIGHT_THUMB },
|
||||
};
|
||||
/* Analog button slot for each control from INPUT_A on. */
|
||||
static const uint8_t analog_slot[] = {
|
||||
INPUT_ANALOG_A, INPUT_ANALOG_B, INPUT_ANALOG_X, INPUT_ANALOG_Y,
|
||||
INPUT_ANALOG_BLACK, INPUT_ANALOG_WHITE, INPUT_ANALOG_LTRIGGER, INPUT_ANALOG_RTRIGGER,
|
||||
};
|
||||
int i;
|
||||
|
||||
memset(out, 0, sizeof(*out));
|
||||
for (i = 0; i < 8; i++)
|
||||
if (control_held(b, down, digital[i].control)) out->buttons |= digital[i].bit;
|
||||
/* Analog on the console: a key is 255, not a flag, or a title that reads
|
||||
* the value as a pressure never sees a press. */
|
||||
for (i = 0; i < 8; i++)
|
||||
if (control_held(b, down, INPUT_A + i)) out->analog[analog_slot[i]] = 255;
|
||||
keys_stick(control_held(b, down, INPUT_LSTICK_RIGHT), control_held(b, down, INPUT_LSTICK_LEFT),
|
||||
control_held(b, down, INPUT_LSTICK_UP), control_held(b, down, INPUT_LSTICK_DOWN),
|
||||
&out->lx, &out->ly);
|
||||
keys_stick(control_held(b, down, INPUT_RSTICK_RIGHT), control_held(b, down, INPUT_RSTICK_LEFT),
|
||||
control_held(b, down, INPUT_RSTICK_UP), control_held(b, down, INPUT_RSTICK_DOWN),
|
||||
&out->rx, &out->ry);
|
||||
}
|
||||
|
||||
/* ---- pad sources ---------------------------------------------------------- */
|
||||
|
||||
static const char *const k_source_names[INPUT_SRC_COUNT] = {
|
||||
"none",
|
||||
"south", "east", "west", "north",
|
||||
"back", "guide", "start",
|
||||
"left_stick_click", "right_stick_click",
|
||||
"left_shoulder", "right_shoulder",
|
||||
"dpad_up", "dpad_down", "dpad_left", "dpad_right",
|
||||
"misc1", "touchpad",
|
||||
"paddle1", "paddle2", "paddle3", "paddle4",
|
||||
"left_trigger", "right_trigger",
|
||||
"left_stick_up", "left_stick_down", "left_stick_left", "left_stick_right",
|
||||
"right_stick_up", "right_stick_down", "right_stick_left", "right_stick_right",
|
||||
};
|
||||
|
||||
const char *xbox_InputMap_SourceName(int source)
|
||||
{
|
||||
return source >= 0 && source < INPUT_SRC_COUNT ? k_source_names[source] : NULL;
|
||||
}
|
||||
|
||||
int xbox_InputMap_SourceFromName(const char *name)
|
||||
{
|
||||
int i;
|
||||
if (!name) return -1;
|
||||
for (i = 0; i < INPUT_SRC_COUNT; i++)
|
||||
if (equal_nocase(name, k_source_names[i])) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void xbox_InputMap_PadMapDefaults(InputPadMap *m)
|
||||
{
|
||||
static const uint8_t identity[INPUT_PAD_CONTROL_COUNT] = {
|
||||
INPUT_SRC_DPAD_UP, INPUT_SRC_DPAD_DOWN, INPUT_SRC_DPAD_LEFT, INPUT_SRC_DPAD_RIGHT,
|
||||
INPUT_SRC_START, INPUT_SRC_BACK,
|
||||
INPUT_SRC_LEFT_STICK_CLICK, INPUT_SRC_RIGHT_STICK_CLICK,
|
||||
INPUT_SRC_SOUTH, INPUT_SRC_EAST, INPUT_SRC_WEST, INPUT_SRC_NORTH,
|
||||
INPUT_SRC_LEFT_SHOULDER, INPUT_SRC_RIGHT_SHOULDER,
|
||||
INPUT_SRC_LEFT_TRIGGER, INPUT_SRC_RIGHT_TRIGGER,
|
||||
};
|
||||
memcpy(m->source, identity, sizeof(identity));
|
||||
m->left_stick = INPUT_STICK_LEFT;
|
||||
m->right_stick = INPUT_STICK_RIGHT;
|
||||
m->deadzone_left = 15;
|
||||
m->deadzone_right = 15;
|
||||
m->trigger_threshold = 5;
|
||||
m->button_threshold = 50;
|
||||
}
|
||||
|
||||
/* ---- deadzones ------------------------------------------------------------ */
|
||||
|
||||
void xbox_InputMap_ApplyStick(int x, int y, int deadzone_pct, int16_t *ox, int16_t *oy)
|
||||
{
|
||||
double dz, mag, scale;
|
||||
if (deadzone_pct < 0) deadzone_pct = 0;
|
||||
if (deadzone_pct > 95) deadzone_pct = 95;
|
||||
dz = 32767.0 * deadzone_pct / 100.0;
|
||||
mag = sqrt((double)x * x + (double)y * y);
|
||||
if (mag <= dz || mag == 0.0) {
|
||||
*ox = 0; *oy = 0;
|
||||
return;
|
||||
}
|
||||
scale = (mag - dz) / (32767.0 - dz);
|
||||
if (scale > 1.0) scale = 1.0;
|
||||
{
|
||||
double fx = x / mag * scale * 32767.0, fy = y / mag * scale * 32767.0;
|
||||
*ox = (int16_t)(fx >= 0 ? fx + 0.5 : fx - 0.5);
|
||||
*oy = (int16_t)(fy >= 0 ? fy + 0.5 : fy - 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t xbox_InputMap_ApplyTrigger(int value, int threshold_pct)
|
||||
{
|
||||
int thr;
|
||||
if (value < 0) value = 0;
|
||||
if (value > 255) value = 255;
|
||||
if (threshold_pct < 0) threshold_pct = 0;
|
||||
if (threshold_pct > 95) threshold_pct = 95;
|
||||
thr = 255 * threshold_pct / 100;
|
||||
if (value <= thr) return 0;
|
||||
return (uint8_t)((value - thr) * 255 / (255 - thr));
|
||||
}
|
||||
|
||||
/* ---- pad mapping ---------------------------------------------------------- */
|
||||
|
||||
static int raw_value(const InputRaw *r, int source)
|
||||
{
|
||||
return source > INPUT_SRC_NONE && source < INPUT_SRC_COUNT ? (r->src[source] > 0 ? r->src[source] : 0) : 0;
|
||||
}
|
||||
|
||||
void xbox_InputMap_PadMap(const InputPadMap *m, const InputRaw *raw, InputPad *out)
|
||||
{
|
||||
static const uint16_t digital_bit[8] = {
|
||||
INPUT_PAD_DPAD_UP, INPUT_PAD_DPAD_DOWN, INPUT_PAD_DPAD_LEFT, INPUT_PAD_DPAD_RIGHT,
|
||||
INPUT_PAD_START, INPUT_PAD_BACK, INPUT_PAD_LEFT_THUMB, INPUT_PAD_RIGHT_THUMB,
|
||||
};
|
||||
static const uint8_t analog_slot[8] = {
|
||||
INPUT_ANALOG_A, INPUT_ANALOG_B, INPUT_ANALOG_X, INPUT_ANALOG_Y,
|
||||
INPUT_ANALOG_BLACK, INPUT_ANALOG_WHITE, INPUT_ANALOG_LTRIGGER, INPUT_ANALOG_RTRIGGER,
|
||||
};
|
||||
int pct = m->button_threshold > 100 ? 100 : m->button_threshold;
|
||||
int press_at = 32767 * pct / 100;
|
||||
int i;
|
||||
|
||||
memset(out, 0, sizeof(*out));
|
||||
if (press_at < 1) press_at = 1;
|
||||
for (i = 0; i < 8; i++)
|
||||
if (raw_value(raw, m->source[i]) >= press_at) out->buttons |= digital_bit[i];
|
||||
/* The face buttons and shoulders are analog on the console but binary
|
||||
* here: pressed is 255. The triggers keep the pad's own travel. */
|
||||
for (i = 0; i < 6; i++)
|
||||
if (raw_value(raw, m->source[INPUT_A + i]) >= press_at)
|
||||
out->analog[analog_slot[i]] = 255;
|
||||
for (i = 6; i < 8; i++) {
|
||||
int v = raw_value(raw, m->source[INPUT_A + i]) >> 7;
|
||||
out->analog[analog_slot[i]] = xbox_InputMap_ApplyTrigger(v > 255 ? 255 : v, m->trigger_threshold);
|
||||
}
|
||||
{
|
||||
int sx = 0, sy = 0;
|
||||
switch (m->left_stick) {
|
||||
case INPUT_STICK_LEFT: sx = raw->lx; sy = raw->ly; break;
|
||||
case INPUT_STICK_RIGHT: sx = raw->rx; sy = raw->ry; break;
|
||||
default: break;
|
||||
}
|
||||
xbox_InputMap_ApplyStick(sx, sy, m->deadzone_left, &out->lx, &out->ly);
|
||||
sx = sy = 0;
|
||||
switch (m->right_stick) {
|
||||
case INPUT_STICK_LEFT: sx = raw->lx; sy = raw->ly; break;
|
||||
case INPUT_STICK_RIGHT: sx = raw->rx; sy = raw->ry; break;
|
||||
default: break;
|
||||
}
|
||||
xbox_InputMap_ApplyStick(sx, sy, m->deadzone_right, &out->rx, &out->ry);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
/*
|
||||
* input_map -- what a host key, mouse button or pad control means to an Xbox
|
||||
* controller.
|
||||
*
|
||||
* Pure arithmetic and text: no Windows, no SDL, no mutable globals. An adopting host layer
|
||||
* reads the devices and hands normalized values to this; keeping
|
||||
* the rules here is what lets a test check binding resolution and deadzones
|
||||
* without a pad on the desk.
|
||||
*
|
||||
* Three pieces:
|
||||
*
|
||||
* Keyboard and mouse bindings. Each Xbox control has up to four host inputs
|
||||
* bound to it, written as a comma separated list of names ("L, Space,
|
||||
* Mouse1"). Keys use stable InputKey identifiers defined here, never native virtual-key
|
||||
* codes, SDL keycodes or scancodes. The caller translates host events.
|
||||
*
|
||||
* Pad remapping. Each of the pad's sixteen digital and trigger controls
|
||||
* takes its value from one named physical control (the names are SDL's
|
||||
* positional ones: south is the bottom face button, an Xbox A, a PlayStation
|
||||
* cross), and each stick can come from either physical stick or from none.
|
||||
*
|
||||
* Deadzones. A radial deadzone for sticks that rescales so the stick still
|
||||
* reaches full deflection, and a threshold for triggers.
|
||||
*/
|
||||
#ifndef XBOX_INPUT_MAP_H
|
||||
#define XBOX_INPUT_MAP_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Pointer contract: structs, arrays and output pointers must be non-NULL unless
|
||||
* a function explicitly documents otherwise. Independent caller-owned states
|
||||
* may be used concurrently. Coordinate access to a shared mutable state.
|
||||
* Names and InputKey values are stable library identifiers, not host events.
|
||||
*/
|
||||
|
||||
/* The Xbox controller's inputs, in the order settings list them. The first
|
||||
* sixteen are what a pad's physical controls can be remapped onto. */
|
||||
typedef enum InputControl {
|
||||
INPUT_DPAD_UP, INPUT_DPAD_DOWN, INPUT_DPAD_LEFT, INPUT_DPAD_RIGHT,
|
||||
INPUT_START, INPUT_BACK, INPUT_LEFT_THUMB, INPUT_RIGHT_THUMB,
|
||||
INPUT_A, INPUT_B, INPUT_X, INPUT_Y, INPUT_BLACK, INPUT_WHITE,
|
||||
INPUT_LEFT_TRIGGER, INPUT_RIGHT_TRIGGER,
|
||||
/* Keyboard only: the sticks as four keys each. */
|
||||
INPUT_LSTICK_UP, INPUT_LSTICK_DOWN, INPUT_LSTICK_LEFT, INPUT_LSTICK_RIGHT,
|
||||
INPUT_RSTICK_UP, INPUT_RSTICK_DOWN, INPUT_RSTICK_LEFT, INPUT_RSTICK_RIGHT,
|
||||
INPUT_CONTROL_COUNT
|
||||
} InputControl;
|
||||
#define INPUT_PAD_CONTROL_COUNT 16
|
||||
|
||||
/* Names use ASCII case-insensitive matching. The setting key for a control ("dpad_up", "left_stick_left"), or NULL. */
|
||||
const char *xbox_InputMap_ControlName(int control);
|
||||
|
||||
/* An Xbox controller's state as the title sees it. Semantic values for constructing an Xbox report (not a packed wire struct): digital buttons in the low byte, eight analog buttons (A B X Y Black
|
||||
* White, left and right trigger), and the sticks with Y positive upwards. */
|
||||
typedef struct InputPad {
|
||||
uint16_t buttons; /* INPUT_PAD_* bits */
|
||||
uint8_t analog[8]; /* INPUT_ANALOG_* */
|
||||
int16_t lx, ly, rx, ry;
|
||||
} InputPad;
|
||||
|
||||
#define INPUT_PAD_DPAD_UP 0x0001
|
||||
#define INPUT_PAD_DPAD_DOWN 0x0002
|
||||
#define INPUT_PAD_DPAD_LEFT 0x0004
|
||||
#define INPUT_PAD_DPAD_RIGHT 0x0008
|
||||
#define INPUT_PAD_START 0x0010
|
||||
#define INPUT_PAD_BACK 0x0020
|
||||
#define INPUT_PAD_LEFT_THUMB 0x0040
|
||||
#define INPUT_PAD_RIGHT_THUMB 0x0080
|
||||
|
||||
#define INPUT_ANALOG_A 0
|
||||
#define INPUT_ANALOG_B 1
|
||||
#define INPUT_ANALOG_X 2
|
||||
#define INPUT_ANALOG_Y 3
|
||||
#define INPUT_ANALOG_BLACK 4
|
||||
#define INPUT_ANALOG_WHITE 5
|
||||
#define INPUT_ANALOG_LTRIGGER 6
|
||||
#define INPUT_ANALOG_RTRIGGER 7
|
||||
|
||||
/* Add `src` to `dst`: buttons are ORed, analog buttons take the larger, and a
|
||||
* stick takes whichever of the two is deflected further. */
|
||||
void xbox_InputMap_PadMerge(InputPad *dst, const InputPad *src);
|
||||
|
||||
/* ---- keyboard and mouse -------------------------------------------------- */
|
||||
|
||||
typedef enum InputKey {
|
||||
INPUT_KEY_NONE = 0, /* reserved, never a held key */
|
||||
INPUT_KEY_MOUSE1 = 1,
|
||||
INPUT_KEY_MOUSE2 = 2,
|
||||
INPUT_KEY_MOUSE3 = 3,
|
||||
INPUT_KEY_MOUSE4 = 4,
|
||||
INPUT_KEY_MOUSE5 = 5,
|
||||
INPUT_KEY_WHEEL_UP = 6,
|
||||
INPUT_KEY_WHEEL_DOWN = 7,
|
||||
INPUT_KEY_BACKSPACE = 8,
|
||||
INPUT_KEY_TAB = 9,
|
||||
INPUT_KEY_ENTER = 10,
|
||||
INPUT_KEY_SHIFT = 11,
|
||||
INPUT_KEY_CTRL = 12,
|
||||
INPUT_KEY_ALT = 13,
|
||||
INPUT_KEY_PAUSE = 14,
|
||||
INPUT_KEY_CAPSLOCK = 15,
|
||||
INPUT_KEY_ESCAPE = 16,
|
||||
INPUT_KEY_SPACE = 17,
|
||||
INPUT_KEY_PAGEUP = 18,
|
||||
INPUT_KEY_PAGEDOWN = 19,
|
||||
INPUT_KEY_END = 20,
|
||||
INPUT_KEY_HOME = 21,
|
||||
INPUT_KEY_LEFT = 22,
|
||||
INPUT_KEY_UP = 23,
|
||||
INPUT_KEY_RIGHT = 24,
|
||||
INPUT_KEY_DOWN = 25,
|
||||
INPUT_KEY_INSERT = 26,
|
||||
INPUT_KEY_DELETE = 27,
|
||||
INPUT_KEY_NUMPADMULTIPLY = 28,
|
||||
INPUT_KEY_NUMPADADD = 29,
|
||||
INPUT_KEY_NUMPADSUBTRACT = 30,
|
||||
INPUT_KEY_NUMPADDECIMAL = 31,
|
||||
INPUT_KEY_NUMPADDIVIDE = 32,
|
||||
INPUT_KEY_LSHIFT = 33,
|
||||
INPUT_KEY_RSHIFT = 34,
|
||||
INPUT_KEY_LCTRL = 35,
|
||||
INPUT_KEY_RCTRL = 36,
|
||||
INPUT_KEY_LALT = 37,
|
||||
INPUT_KEY_RALT = 38,
|
||||
INPUT_KEY_SEMICOLON = 39,
|
||||
INPUT_KEY_EQUALS = 40,
|
||||
INPUT_KEY_COMMA = 41,
|
||||
INPUT_KEY_MINUS = 42,
|
||||
INPUT_KEY_PERIOD = 43,
|
||||
INPUT_KEY_SLASH = 44,
|
||||
INPUT_KEY_GRAVE = 45,
|
||||
INPUT_KEY_LEFTBRACKET = 46,
|
||||
INPUT_KEY_BACKSLASH = 47,
|
||||
INPUT_KEY_RIGHTBRACKET = 48,
|
||||
INPUT_KEY_QUOTE = 49,
|
||||
INPUT_KEY_A = 50,
|
||||
INPUT_KEY_B = 51,
|
||||
INPUT_KEY_C = 52,
|
||||
INPUT_KEY_D = 53,
|
||||
INPUT_KEY_E = 54,
|
||||
INPUT_KEY_F = 55,
|
||||
INPUT_KEY_G = 56,
|
||||
INPUT_KEY_H = 57,
|
||||
INPUT_KEY_I = 58,
|
||||
INPUT_KEY_J = 59,
|
||||
INPUT_KEY_K = 60,
|
||||
INPUT_KEY_L = 61,
|
||||
INPUT_KEY_M = 62,
|
||||
INPUT_KEY_N = 63,
|
||||
INPUT_KEY_O = 64,
|
||||
INPUT_KEY_P = 65,
|
||||
INPUT_KEY_Q = 66,
|
||||
INPUT_KEY_R = 67,
|
||||
INPUT_KEY_S = 68,
|
||||
INPUT_KEY_T = 69,
|
||||
INPUT_KEY_U = 70,
|
||||
INPUT_KEY_V = 71,
|
||||
INPUT_KEY_W = 72,
|
||||
INPUT_KEY_X = 73,
|
||||
INPUT_KEY_Y = 74,
|
||||
INPUT_KEY_Z = 75,
|
||||
INPUT_KEY_0 = 76,
|
||||
INPUT_KEY_1 = 77,
|
||||
INPUT_KEY_2 = 78,
|
||||
INPUT_KEY_3 = 79,
|
||||
INPUT_KEY_4 = 80,
|
||||
INPUT_KEY_5 = 81,
|
||||
INPUT_KEY_6 = 82,
|
||||
INPUT_KEY_7 = 83,
|
||||
INPUT_KEY_8 = 84,
|
||||
INPUT_KEY_9 = 85,
|
||||
INPUT_KEY_F1 = 86,
|
||||
INPUT_KEY_F2 = 87,
|
||||
INPUT_KEY_F3 = 88,
|
||||
INPUT_KEY_F4 = 89,
|
||||
INPUT_KEY_F5 = 90,
|
||||
INPUT_KEY_F6 = 91,
|
||||
INPUT_KEY_F7 = 92,
|
||||
INPUT_KEY_F8 = 93,
|
||||
INPUT_KEY_F9 = 94,
|
||||
INPUT_KEY_F10 = 95,
|
||||
INPUT_KEY_F11 = 96,
|
||||
INPUT_KEY_F12 = 97,
|
||||
INPUT_KEY_NUMPAD0 = 98,
|
||||
INPUT_KEY_NUMPAD1 = 99,
|
||||
INPUT_KEY_NUMPAD2 = 100,
|
||||
INPUT_KEY_NUMPAD3 = 101,
|
||||
INPUT_KEY_NUMPAD4 = 102,
|
||||
INPUT_KEY_NUMPAD5 = 103,
|
||||
INPUT_KEY_NUMPAD6 = 104,
|
||||
INPUT_KEY_NUMPAD7 = 105,
|
||||
INPUT_KEY_NUMPAD8 = 106,
|
||||
INPUT_KEY_NUMPAD9 = 107,
|
||||
INPUT_KEY_COUNT = 108
|
||||
} InputKey;
|
||||
#define INPUT_BINDINGS_PER_CONTROL 4
|
||||
|
||||
typedef struct InputBindings {
|
||||
uint16_t key[INPUT_CONTROL_COUNT][INPUT_BINDINGS_PER_CONTROL];
|
||||
uint8_t count[INPUT_CONTROL_COUNT];
|
||||
} InputBindings;
|
||||
|
||||
/* Stable InputKey ID for a key name, case-insensitive ("a", "space", "numpad4", "mouse1",
|
||||
* "wheelup", "lshift"), or -1 if there is no such key. */
|
||||
int xbox_InputMap_KeyFromName(const char *name);
|
||||
|
||||
/* Immutable canonical name for an InputKey ID, or NULL. Safe across threads. */
|
||||
const char *xbox_InputMap_KeyName(int code);
|
||||
|
||||
/* Parse "L, Space" into `control`'s bindings, replacing them. "none" or an
|
||||
* empty string clears it. Names that are not keys are skipped and counted in
|
||||
* the return value; at most four keys are kept. Returns the number of names
|
||||
* rejected, or -1 for an invalid binding pointer/control. No fixed input-text limit. */
|
||||
int xbox_InputMap_BindingsParse(InputBindings *b, int control, const char *text);
|
||||
|
||||
/* The bindings of `control` as text that xbox_InputMap_BindingsParse reads back. */
|
||||
const char *xbox_InputMap_BindingsFormat(const InputBindings *b, int control,
|
||||
char *buf, int size);
|
||||
|
||||
/* The pad a set of held keys makes. Counts are bounded to four per control. `down` has INPUT_KEY_COUNT entries, non-zero
|
||||
* for held. Required pointers must be non-NULL. A stick built from keys is a full-deflection circle, so a diagonal
|
||||
* is not faster than a straight line. */
|
||||
void xbox_InputMap_KeysResolve(const InputBindings *b, const uint8_t *down, InputPad *out);
|
||||
|
||||
/* ---- pad remapping and deadzones ------------------------------------------ */
|
||||
|
||||
/* The physical controls a pad has, SDL's positional names. */
|
||||
typedef enum InputSource {
|
||||
INPUT_SRC_NONE,
|
||||
INPUT_SRC_SOUTH, INPUT_SRC_EAST, INPUT_SRC_WEST, INPUT_SRC_NORTH,
|
||||
INPUT_SRC_BACK, INPUT_SRC_GUIDE, INPUT_SRC_START,
|
||||
INPUT_SRC_LEFT_STICK_CLICK, INPUT_SRC_RIGHT_STICK_CLICK,
|
||||
INPUT_SRC_LEFT_SHOULDER, INPUT_SRC_RIGHT_SHOULDER,
|
||||
INPUT_SRC_DPAD_UP, INPUT_SRC_DPAD_DOWN, INPUT_SRC_DPAD_LEFT, INPUT_SRC_DPAD_RIGHT,
|
||||
INPUT_SRC_MISC1, INPUT_SRC_TOUCHPAD,
|
||||
INPUT_SRC_PADDLE1, INPUT_SRC_PADDLE2, INPUT_SRC_PADDLE3, INPUT_SRC_PADDLE4,
|
||||
INPUT_SRC_LEFT_TRIGGER, INPUT_SRC_RIGHT_TRIGGER,
|
||||
/* A stick pushed one way, 0 to full. */
|
||||
INPUT_SRC_LSTICK_UP, INPUT_SRC_LSTICK_DOWN, INPUT_SRC_LSTICK_LEFT, INPUT_SRC_LSTICK_RIGHT,
|
||||
INPUT_SRC_RSTICK_UP, INPUT_SRC_RSTICK_DOWN, INPUT_SRC_RSTICK_LEFT, INPUT_SRC_RSTICK_RIGHT,
|
||||
INPUT_SRC_COUNT
|
||||
} InputSource;
|
||||
|
||||
const char *xbox_InputMap_SourceName(int source);
|
||||
int xbox_InputMap_SourceFromName(const char *name); /* -1 if unknown */
|
||||
|
||||
/* The adapter must populate directional sources explicitly if they are used
|
||||
* as bindings; stick axes are not automatically copied into src[].
|
||||
* Missing sources are zero. Negative src[] values are treated as zero.
|
||||
* Digital/face-button mappings use button_threshold (clamped to 0..100%).
|
||||
*/
|
||||
|
||||
/* What the host read from a pad before any mapping. Every source is 0 to
|
||||
* 32767 (a button is 0 or 32767); the sticks are signed, Y positive upwards. */
|
||||
typedef struct InputRaw {
|
||||
int16_t src[INPUT_SRC_COUNT];
|
||||
int16_t lx, ly, rx, ry;
|
||||
} InputRaw;
|
||||
|
||||
enum { INPUT_STICK_LEFT, INPUT_STICK_RIGHT, INPUT_STICK_NONE };
|
||||
|
||||
typedef struct InputPadMap {
|
||||
uint8_t source[INPUT_PAD_CONTROL_COUNT]; /* InputSource for each control */
|
||||
uint8_t left_stick; /* INPUT_STICK_*: which physical stick */
|
||||
uint8_t right_stick;
|
||||
uint8_t deadzone_left; /* percent, 0-95 */
|
||||
uint8_t deadzone_right;
|
||||
uint8_t trigger_threshold; /* percent, 0-95 */
|
||||
uint8_t button_threshold; /* percent a trigger/stick must reach to count as a button */
|
||||
} InputPadMap;
|
||||
|
||||
/* The identity mapping: a pad as the Xbox pad it is shaped like. Deadzones 15%,
|
||||
* trigger threshold 5%, an axis counts as a button at 50%. */
|
||||
void xbox_InputMap_PadMapDefaults(InputPadMap *m);
|
||||
|
||||
/* Apply `m` to a raw pad. */
|
||||
void xbox_InputMap_PadMap(const InputPadMap *m, const InputRaw *raw, InputPad *out);
|
||||
|
||||
/* A radial deadzone (percent clamped to 0..95): a stick inside `deadzone_pct` of full deflection reads as
|
||||
* centred, and the range beyond it is stretched so full deflection is still
|
||||
* full. The direction is preserved. */
|
||||
void xbox_InputMap_ApplyStick(int x, int y, int deadzone_pct, int16_t *ox, int16_t *oy);
|
||||
|
||||
/* A trigger threshold is clamped to 0..95. A trigger value (0-255) below `threshold_pct` of the range reads 0; the rest
|
||||
* is stretched to 0-255. */
|
||||
uint8_t xbox_InputMap_ApplyTrigger(int value, int threshold_pct);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* XBOX_INPUT_MAP_H */
|
||||
@@ -0,0 +1,101 @@
|
||||
# Portable input mapping (opt-in)
|
||||
|
||||
`xbox_input_map` is a C11 utility for bindings, controller remapping, stick
|
||||
and trigger thresholds. It is independent of SDL, XInput, DirectInput and
|
||||
kernel/USB code. It originates in the
|
||||
[Def Jam project](https://github.com/vyanhursky/DefJamFFNY-recomp).
|
||||
|
||||
Existing `xbox_input` readers and toolkit defaults are unchanged. The target is
|
||||
excluded from the default build and from the `xboxrecomp` umbrella. A port opts
|
||||
in by linking `xbox_input_map`; building this utility alone does not need an
|
||||
SDL package or the toolkit's runtime prerequisites. It uses the system math
|
||||
library (`m` on non-MSVC toolchains).
|
||||
|
||||
## What it does
|
||||
|
||||
The module parses/formats up to four named inputs per Xbox control, resolves
|
||||
held keys to an Xbox pad state, remaps normalized controller controls, applies
|
||||
radial deadzones and trigger thresholds, and combines two pad states. Names
|
||||
are ASCII case-insensitive. Unknown/oversized tokens and bindings beyond four
|
||||
are reported; duplicate keys are ignored. Long text is processed completely,
|
||||
not truncated. `none`, empty tokens and NULL text add no binding; parsing
|
||||
replaces the selected control's bindings.
|
||||
|
||||
Key/source name strings are immutable. States belong to the caller: independent
|
||||
states can be processed concurrently without device initialization or global
|
||||
mapping state. Synchronize concurrent access to the same writable state.
|
||||
Required pointers must be non-NULL unless a function explicitly documents
|
||||
otherwise. `InputPad` contains semantic values, not a packed USB wire report.
|
||||
|
||||
## Stable key and controller contract
|
||||
|
||||
`InputKey` contains explicitly numbered library IDs. **These are not Windows
|
||||
virtual keys, SDL keycodes/scancodes or DirectInput scan codes.** Do not index
|
||||
the held-key array with native event codes. Translate them to named constants
|
||||
such as `INPUT_KEY_SPACE` or use `xbox_InputMap_KeyFromName("space")`. Numeric
|
||||
IDs from the original Def Jam module have deliberately not been retained.
|
||||
Persist binding names rather than host codes. `INPUT_KEY_NONE` is reserved.
|
||||
|
||||
Letters, digits and named punctuation describe logical keys (for example `A`
|
||||
and `semicolon`), not Unicode text or physical scan positions. The adapter
|
||||
owns keyboard-layout translation; this module does not implement IME input or
|
||||
a layout database. A port offering physical-key controls should translate its
|
||||
chosen physical keys explicitly and document that policy. Generic modifier
|
||||
IDs (`SHIFT`, `CTRL`, `ALT`) are separate from left/right IDs: an adapter wishing
|
||||
to support both kinds of binding updates both the side-specific and aggregate
|
||||
held state. Wheel inputs are short pulses; the adapter defines their duration.
|
||||
|
||||
`InputRaw.src[]` contains normalized 0..32767 values (digital pressed=32767),
|
||||
missing controls are zero, and negative source values are treated as zero.
|
||||
Face names are positional (`south`, `east`, `west`, `north`). Signed stick
|
||||
axes use -32768..32767 with positive Y upwards; adapters invert host Y when
|
||||
necessary. An adapter must populate directional `src[]` entries separately
|
||||
when using stick directions as button sources; axes are not implicitly copied
|
||||
into those entries. Invalid source/stick selections resolve to neutral.
|
||||
|
||||
Stick/trigger threshold percentages are clamped to 0..95, digital-button
|
||||
thresholds to 0..100 (zero still requires a nonzero source). Stick output is
|
||||
radially limited to full deflection. Xbox face/shoulder controls are emitted
|
||||
as 0/255; mapped triggers retain analog travel. Merging ORs digital buttons,
|
||||
takes the maximum analog pressure, and selects the entire stick vector with
|
||||
the greater magnitude (the destination wins ties).
|
||||
|
||||
`xbox_InputMap_PadMapDefaults` explicitly selects the conventional face/shoulder
|
||||
mapping, 15% stick deadzones, 5% trigger thresholds and a 50% digital threshold.
|
||||
It is a convenience initializer, not an automatic change to existing ports.
|
||||
An adopter should deliberately choose its mapping and thresholds.
|
||||
|
||||
## Minimal keyboard example
|
||||
|
||||
```c
|
||||
#include "input_map.h"
|
||||
|
||||
InputBindings bindings = {0};
|
||||
uint8_t held[INPUT_KEY_COUNT] = {0};
|
||||
InputPad pad;
|
||||
|
||||
xbox_InputMap_BindingsParse(&bindings, INPUT_A, "space, mouse1");
|
||||
/* The host event adapter translates its Space-down event to this library ID. */
|
||||
held[INPUT_KEY_SPACE] = 1;
|
||||
xbox_InputMap_KeysResolve(&bindings, held, &pad);
|
||||
/* pad.analog[INPUT_ANALOG_A] == 255. The port supplies its normal report path. */
|
||||
```
|
||||
|
||||
Link using `target_link_libraries(your_host PRIVATE xbox_input_map)` after the
|
||||
normal toolkit `add_subdirectory`. A standalone utility consumer may instead
|
||||
add `src/input/mapping` directly. Keyboard and controller examples are tested
|
||||
in `tests/input_map`. No device reader, rumble/hotplug/focus behavior, settings
|
||||
file migration or guest report submission is added by this module.
|
||||
|
||||
## Testing
|
||||
|
||||
```sh
|
||||
cmake -S tests/input_map -B build/input-map -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build/input-map --config Release
|
||||
ctest --test-dir build/input-map -C Release --output-on-failure
|
||||
```
|
||||
|
||||
The C checks are explicit and execute under `NDEBUG`; the C++ consumer also
|
||||
links the actual C library. Tests require no controller, game data or SDL.
|
||||
Real device adoption separately needs adapter range/polarity, layout and
|
||||
hardware tests. Arithmetic success does not prove device discovery or latency.
|
||||
@@ -0,0 +1,7 @@
|
||||
# Opt-in utility: existing xbox_input readers do not link or use it.
|
||||
add_library(xbox_input_map STATIC EXCLUDE_FROM_ALL ../input_map.c)
|
||||
target_include_directories(xbox_input_map PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..)
|
||||
target_compile_features(xbox_input_map PUBLIC c_std_11)
|
||||
if(NOT MSVC)
|
||||
target_link_libraries(xbox_input_map PUBLIC m)
|
||||
endif()
|
||||
@@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(input_map_test LANGUAGES C CXX)
|
||||
add_subdirectory(../../src/input/mapping mapping)
|
||||
add_executable(input_map_test test_main.c)
|
||||
target_link_libraries(input_map_test PRIVATE xbox_input_map)
|
||||
enable_testing()
|
||||
add_test(NAME input_map_test COMMAND input_map_test)
|
||||
add_executable(input_map_cpp cpp_smoke.cpp)
|
||||
target_link_libraries(input_map_cpp PRIVATE xbox_input_map)
|
||||
add_test(NAME input_map_cpp COMMAND input_map_cpp)
|
||||
@@ -0,0 +1,12 @@
|
||||
# Portable input mapping fixture
|
||||
|
||||
See [the module contract and integration example](../../src/input/input_map.md).
|
||||
Build this directory directly with CMake and run CTest; no root runtime/SDL
|
||||
configuration is required. Explicit C checks execute in Release even under
|
||||
NDEBUG; a C++ consumer verifies extern-C linkage and the actual mapping target.
|
||||
|
||||
Coverage: key names/aliases, binding parse/format/clear/duplicates/limits,
|
||||
keyboard resolution and diagonals, whole-vector merging, mapping/disabled or
|
||||
swapped sticks, deadzones, triggers, immutable names, long binding text,
|
||||
malformed counts/IDs and range/roundtrip properties. Fixtures have no device,
|
||||
game data, save files or guest code dependencies.
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "input_map.h"
|
||||
int main()
|
||||
{
|
||||
InputPadMap map;
|
||||
InputRaw raw = {};
|
||||
InputPad out;
|
||||
xbox_InputMap_PadMapDefaults(&map);
|
||||
raw.src[INPUT_SRC_SOUTH] = 32767;
|
||||
xbox_InputMap_PadMap(&map, &raw, &out);
|
||||
return out.analog[INPUT_ANALOG_A] == 255 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,374 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
/* input_map: key names, binding parse and format, key resolution, deadzone
|
||||
* arithmetic and pad remapping. */
|
||||
|
||||
#include "input_map.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int g_failed;
|
||||
static int g_checks;
|
||||
#define CHECK(cond) do { g_checks++; if (!(cond)) { \
|
||||
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); g_failed++; } } while (0)
|
||||
#define NEAR(a, b, tol) (abs((int)(a) - (int)(b)) <= (tol))
|
||||
|
||||
static void test_key_names(void)
|
||||
{
|
||||
int c;
|
||||
char buf[64];
|
||||
CHECK(xbox_InputMap_KeyFromName("a") == INPUT_KEY_A);
|
||||
CHECK(xbox_InputMap_KeyFromName("Z") == INPUT_KEY_Z);
|
||||
CHECK(xbox_InputMap_KeyFromName("7") == INPUT_KEY_7);
|
||||
CHECK(xbox_InputMap_KeyFromName("F5") == INPUT_KEY_F5);
|
||||
CHECK(xbox_InputMap_KeyFromName("f12") == INPUT_KEY_F12);
|
||||
CHECK(xbox_InputMap_KeyFromName("f13") == -1);
|
||||
CHECK(xbox_InputMap_KeyFromName("f05") == -1);
|
||||
CHECK(xbox_InputMap_KeyFromName("f0") == -1);
|
||||
CHECK(xbox_InputMap_KeyFromName("Numpad4") == INPUT_KEY_NUMPAD4);
|
||||
CHECK(xbox_InputMap_KeyFromName("numpad10") == -1);
|
||||
CHECK(xbox_InputMap_KeyFromName("SPACE") == INPUT_KEY_SPACE);
|
||||
CHECK(xbox_InputMap_KeyFromName("Esc") == INPUT_KEY_ESCAPE);
|
||||
CHECK(xbox_InputMap_KeyFromName("mouse1") == INPUT_KEY_MOUSE1);
|
||||
CHECK(xbox_InputMap_KeyFromName("Mouse3") == INPUT_KEY_MOUSE3);
|
||||
CHECK(xbox_InputMap_KeyFromName("WheelUp") == INPUT_KEY_WHEEL_UP);
|
||||
CHECK(xbox_InputMap_KeyFromName("lshift") == INPUT_KEY_LSHIFT);
|
||||
CHECK(xbox_InputMap_KeyFromName("") == -1);
|
||||
CHECK(xbox_InputMap_KeyFromName("banana") == -1);
|
||||
CHECK(xbox_InputMap_KeyFromName("ab") == -1);
|
||||
/* Every code with a name reads back as itself. */
|
||||
for (c = 0; c < INPUT_KEY_COUNT; c++) {
|
||||
const char *n = xbox_InputMap_KeyName(c);
|
||||
if (!n) continue;
|
||||
snprintf(buf, sizeof(buf), "%s", n);
|
||||
CHECK(xbox_InputMap_KeyFromName(buf) == c);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_bindings(void)
|
||||
{
|
||||
InputBindings b;
|
||||
char text[128];
|
||||
int k;
|
||||
memset(&b, 0, sizeof(b));
|
||||
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, INPUT_A, "L, Space, Mouse1 ") == 0);
|
||||
CHECK(b.count[INPUT_A] == 3);
|
||||
CHECK(b.key[INPUT_A][0] == INPUT_KEY_L && b.key[INPUT_A][1] == INPUT_KEY_SPACE && b.key[INPUT_A][2] == INPUT_KEY_MOUSE1);
|
||||
CHECK(!strcmp(xbox_InputMap_BindingsFormat(&b, INPUT_A, text, sizeof(text)), "L, space, mouse1"));
|
||||
|
||||
/* Reading the formatted text back gives the same bindings. */
|
||||
k = xbox_InputMap_BindingsParse(&b, INPUT_B, text);
|
||||
CHECK(k == 0 && b.count[INPUT_B] == 3 && b.key[INPUT_B][1] == INPUT_KEY_SPACE);
|
||||
|
||||
/* Parsing replaces; none and empty clear; duplicates are dropped. */
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, INPUT_A, "none") == 0 && b.count[INPUT_A] == 0);
|
||||
CHECK(!strcmp(xbox_InputMap_BindingsFormat(&b, INPUT_A, text, sizeof(text)), "none"));
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, INPUT_A, "J, j, J") == 0 && b.count[INPUT_A] == 1);
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, INPUT_A, "") == 0 && b.count[INPUT_A] == 0);
|
||||
|
||||
/* Unknown names are skipped and counted, known ones kept. */
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, INPUT_X, "K, banana, wheeldown, 99") == 2);
|
||||
CHECK(b.count[INPUT_X] == 2 && b.key[INPUT_X][1] == INPUT_KEY_WHEEL_DOWN);
|
||||
|
||||
/* Four is the most; the fifth is counted as rejected. */
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, INPUT_Y, "A,B,C,D,E") == 1);
|
||||
CHECK(b.count[INPUT_Y] == 4);
|
||||
|
||||
/* Control names are the setting keys. */
|
||||
CHECK(!strcmp(xbox_InputMap_ControlName(INPUT_LSTICK_UP), "left_stick_up"));
|
||||
}
|
||||
|
||||
static void test_resolve(void)
|
||||
{
|
||||
InputBindings b;
|
||||
uint8_t down[INPUT_KEY_COUNT];
|
||||
InputPad p;
|
||||
memset(&b, 0, sizeof(b));
|
||||
memset(down, 0, sizeof(down));
|
||||
xbox_InputMap_BindingsParse(&b, INPUT_LSTICK_UP, "W");
|
||||
xbox_InputMap_BindingsParse(&b, INPUT_LSTICK_DOWN, "S");
|
||||
xbox_InputMap_BindingsParse(&b, INPUT_LSTICK_LEFT, "A");
|
||||
xbox_InputMap_BindingsParse(&b, INPUT_LSTICK_RIGHT, "D");
|
||||
xbox_InputMap_BindingsParse(&b, INPUT_RSTICK_LEFT, "Left");
|
||||
xbox_InputMap_BindingsParse(&b, INPUT_A, "L, Space");
|
||||
xbox_InputMap_BindingsParse(&b, INPUT_LEFT_TRIGGER, "Q");
|
||||
xbox_InputMap_BindingsParse(&b, INPUT_START, "Enter");
|
||||
xbox_InputMap_BindingsParse(&b, INPUT_DPAD_UP, "wheelup");
|
||||
|
||||
xbox_InputMap_KeysResolve(&b, down, &p);
|
||||
CHECK(p.buttons == 0 && p.lx == 0 && p.ly == 0 && p.rx == 0);
|
||||
|
||||
down[INPUT_KEY_W] = 1;
|
||||
xbox_InputMap_KeysResolve(&b, down, &p);
|
||||
CHECK(p.ly == 32767 && p.lx == 0);
|
||||
|
||||
down[INPUT_KEY_D] = 1; /* diagonal: not faster */
|
||||
xbox_InputMap_KeysResolve(&b, down, &p);
|
||||
CHECK(p.lx == 23169 && p.ly == 23169);
|
||||
|
||||
down[INPUT_KEY_S] = 1; /* opposite keys cancel */
|
||||
xbox_InputMap_KeysResolve(&b, down, &p);
|
||||
CHECK(p.ly == 0 && p.lx == 32767);
|
||||
|
||||
down[INPUT_KEY_LEFT] = 1;
|
||||
xbox_InputMap_KeysResolve(&b, down, &p);
|
||||
CHECK(p.rx == -32767 && p.ry == 0);
|
||||
|
||||
down[INPUT_KEY_SPACE] = 1; /* either bound key presses A */
|
||||
down[INPUT_KEY_Q] = 1;
|
||||
down[INPUT_KEY_ENTER] = 1;
|
||||
down[INPUT_KEY_WHEEL_UP] = 1;
|
||||
xbox_InputMap_KeysResolve(&b, down, &p);
|
||||
CHECK(p.analog[INPUT_ANALOG_A] == 255);
|
||||
CHECK(p.analog[INPUT_ANALOG_LTRIGGER] == 255);
|
||||
CHECK(p.analog[INPUT_ANALOG_B] == 0);
|
||||
CHECK(p.buttons == (INPUT_PAD_START | INPUT_PAD_DPAD_UP));
|
||||
}
|
||||
|
||||
static void test_merge(void)
|
||||
{
|
||||
InputPad a, b;
|
||||
memset(&a, 0, sizeof(a));
|
||||
memset(&b, 0, sizeof(b));
|
||||
a.buttons = INPUT_PAD_START;
|
||||
a.analog[0] = 255;
|
||||
a.lx = 1000;
|
||||
b.buttons = INPUT_PAD_BACK;
|
||||
b.analog[0] = 10;
|
||||
b.analog[7] = 90;
|
||||
b.lx = -20000;
|
||||
b.ry = 5;
|
||||
xbox_InputMap_PadMerge(&a, &b);
|
||||
CHECK(a.buttons == (INPUT_PAD_START | INPUT_PAD_BACK));
|
||||
CHECK(a.analog[0] == 255 && a.analog[7] == 90);
|
||||
CHECK(a.lx == -20000); /* the larger deflection wins */
|
||||
CHECK(a.ry == 5);
|
||||
}
|
||||
|
||||
static void test_deadzone(void)
|
||||
{
|
||||
int16_t x, y;
|
||||
xbox_InputMap_ApplyStick(3000, 0, 15, &x, &y); /* 15% is 4915 */
|
||||
CHECK(x == 0 && y == 0);
|
||||
xbox_InputMap_ApplyStick(3000, 3000, 15, &x, &y); /* magnitude 4243: still inside */
|
||||
CHECK(x == 0 && y == 0);
|
||||
xbox_InputMap_ApplyStick(32767, 0, 15, &x, &y); /* full stays full */
|
||||
CHECK(x == 32767 && y == 0);
|
||||
xbox_InputMap_ApplyStick(-32768, 0, 15, &x, &y);
|
||||
CHECK(x == -32767 && y == 0);
|
||||
xbox_InputMap_ApplyStick(16383, 0, 15, &x, &y); /* (16383-4915)/(32767-4915) */
|
||||
CHECK(NEAR(x, 13491, 2) && y == 0);
|
||||
xbox_InputMap_ApplyStick(0, -16383, 15, &x, &y);
|
||||
CHECK(x == 0 && NEAR(y, -13491, 2));
|
||||
xbox_InputMap_ApplyStick(20000, 20000, 15, &x, &y); /* direction preserved */
|
||||
CHECK(x == y && x > 0 && x < 32767);
|
||||
xbox_InputMap_ApplyStick(32767, 32767, 15, &x, &y); /* beyond the circle clamps to it */
|
||||
CHECK(NEAR(x, 23170, 3) && NEAR(y, 23170, 3));
|
||||
xbox_InputMap_ApplyStick(500, 0, 0, &x, &y); /* no deadzone passes through */
|
||||
CHECK(x == 500 && y == 0);
|
||||
xbox_InputMap_ApplyStick(0, 0, 15, &x, &y);
|
||||
CHECK(x == 0 && y == 0);
|
||||
xbox_InputMap_ApplyStick(32767, 0, 120, &x, &y); /* absurd percentages are clamped */
|
||||
CHECK(x == 32767);
|
||||
|
||||
CHECK(xbox_InputMap_ApplyTrigger(0, 5) == 0);
|
||||
CHECK(xbox_InputMap_ApplyTrigger(12, 5) == 0); /* 5% of 255 is 12 */
|
||||
CHECK(xbox_InputMap_ApplyTrigger(255, 5) == 255);
|
||||
CHECK(NEAR(xbox_InputMap_ApplyTrigger(134, 5), 128, 1));
|
||||
CHECK(xbox_InputMap_ApplyTrigger(100, 0) == 100);
|
||||
CHECK(xbox_InputMap_ApplyTrigger(300, 5) == 255);
|
||||
}
|
||||
|
||||
static void test_padmap(void)
|
||||
{
|
||||
InputPadMap m;
|
||||
InputRaw r;
|
||||
InputPad p;
|
||||
int i;
|
||||
|
||||
CHECK(xbox_InputMap_SourceFromName("south") == INPUT_SRC_SOUTH);
|
||||
CHECK(xbox_InputMap_SourceFromName("Left_Trigger") == INPUT_SRC_LEFT_TRIGGER);
|
||||
CHECK(xbox_InputMap_SourceFromName("a") == -1);
|
||||
for (i = 0; i < INPUT_SRC_COUNT; i++)
|
||||
CHECK(xbox_InputMap_SourceFromName(xbox_InputMap_SourceName(i)) == i);
|
||||
|
||||
xbox_InputMap_PadMapDefaults(&m);
|
||||
memset(&r, 0, sizeof(r));
|
||||
xbox_InputMap_PadMap(&m, &r, &p);
|
||||
CHECK(p.buttons == 0 && p.analog[0] == 0 && p.lx == 0);
|
||||
|
||||
/* Identity: the bottom face button is A, the shoulders are Black and White. */
|
||||
r.src[INPUT_SRC_SOUTH] = 32767;
|
||||
r.src[INPUT_SRC_LEFT_SHOULDER] = 32767;
|
||||
r.src[INPUT_SRC_START] = 32767;
|
||||
r.src[INPUT_SRC_DPAD_LEFT] = 32767;
|
||||
r.src[INPUT_SRC_RIGHT_TRIGGER] = 32767;
|
||||
r.src[INPUT_SRC_LEFT_TRIGGER] = 128 << 7;
|
||||
xbox_InputMap_PadMap(&m, &r, &p);
|
||||
CHECK(p.analog[INPUT_ANALOG_A] == 255 && p.analog[INPUT_ANALOG_B] == 0);
|
||||
CHECK(p.analog[INPUT_ANALOG_BLACK] == 255 && p.analog[INPUT_ANALOG_WHITE] == 0);
|
||||
CHECK(p.buttons == (INPUT_PAD_START | INPUT_PAD_DPAD_LEFT));
|
||||
CHECK(p.analog[INPUT_ANALOG_RTRIGGER] == 255);
|
||||
CHECK(NEAR(p.analog[INPUT_ANALOG_LTRIGGER], 128, 8)); /* analog, with the threshold applied */
|
||||
|
||||
/* A button below the press threshold does not count; at it, does. */
|
||||
memset(&r, 0, sizeof(r));
|
||||
r.src[INPUT_SRC_EAST] = 16000;
|
||||
xbox_InputMap_PadMap(&m, &r, &p);
|
||||
CHECK(p.analog[INPUT_ANALOG_B] == 0);
|
||||
r.src[INPUT_SRC_EAST] = 16384;
|
||||
xbox_InputMap_PadMap(&m, &r, &p);
|
||||
CHECK(p.analog[INPUT_ANALOG_B] == 255);
|
||||
|
||||
/* Remap: swap A and B, put Start on the touchpad, X on the left trigger. */
|
||||
m.source[INPUT_A] = INPUT_SRC_EAST;
|
||||
m.source[INPUT_B] = INPUT_SRC_SOUTH;
|
||||
m.source[INPUT_START] = INPUT_SRC_TOUCHPAD;
|
||||
m.source[INPUT_X] = INPUT_SRC_LEFT_TRIGGER;
|
||||
memset(&r, 0, sizeof(r));
|
||||
r.src[INPUT_SRC_SOUTH] = 32767;
|
||||
r.src[INPUT_SRC_TOUCHPAD] = 32767;
|
||||
r.src[INPUT_SRC_LEFT_TRIGGER] = 32767;
|
||||
xbox_InputMap_PadMap(&m, &r, &p);
|
||||
CHECK(p.analog[INPUT_ANALOG_A] == 0 && p.analog[INPUT_ANALOG_B] == 255);
|
||||
CHECK(p.buttons == INPUT_PAD_START);
|
||||
CHECK(p.analog[INPUT_ANALOG_X] == 255);
|
||||
|
||||
/* None leaves a control unbound. */
|
||||
xbox_InputMap_PadMapDefaults(&m);
|
||||
m.source[INPUT_A] = INPUT_SRC_NONE;
|
||||
memset(&r, 0, sizeof(r));
|
||||
r.src[INPUT_SRC_SOUTH] = 32767;
|
||||
xbox_InputMap_PadMap(&m, &r, &p);
|
||||
CHECK(p.analog[INPUT_ANALOG_A] == 0);
|
||||
|
||||
/* Sticks: pass through with the deadzone, swap, or switch off. */
|
||||
xbox_InputMap_PadMapDefaults(&m);
|
||||
memset(&r, 0, sizeof(r));
|
||||
r.lx = 32767; r.ly = 0; r.rx = 0; r.ry = -32767;
|
||||
xbox_InputMap_PadMap(&m, &r, &p);
|
||||
CHECK(p.lx == 32767 && p.ry == -32767 && p.rx == 0);
|
||||
m.left_stick = INPUT_STICK_RIGHT;
|
||||
m.right_stick = INPUT_STICK_LEFT;
|
||||
xbox_InputMap_PadMap(&m, &r, &p);
|
||||
CHECK(p.ly == -32767 && p.rx == 32767 && p.lx == 0);
|
||||
m.right_stick = INPUT_STICK_NONE;
|
||||
xbox_InputMap_PadMap(&m, &r, &p);
|
||||
CHECK(p.rx == 0 && p.ry == 0);
|
||||
/* Drift inside the deadzone is ignored. */
|
||||
xbox_InputMap_PadMapDefaults(&m);
|
||||
r.lx = 2500; r.ly = -2500;
|
||||
xbox_InputMap_PadMap(&m, &r, &p);
|
||||
CHECK(p.lx == 0 && p.ly == 0);
|
||||
}
|
||||
|
||||
|
||||
static void test_api_edges(void)
|
||||
{
|
||||
InputBindings b = {0};
|
||||
InputPad a = {0}, z = {0}, out;
|
||||
InputRaw raw = {0};
|
||||
InputPadMap map;
|
||||
uint8_t down[INPUT_KEY_COUNT] = {0};
|
||||
char long_text[600], small[2] = {'!', '!'}, text[80];
|
||||
const char *saved = xbox_InputMap_KeyName(INPUT_KEY_A);
|
||||
int i;
|
||||
CHECK(xbox_InputMap_KeyFromName(NULL) == -1);
|
||||
CHECK(xbox_InputMap_KeyName(INPUT_KEY_NONE) == NULL);
|
||||
CHECK(xbox_InputMap_KeyName(-1) == NULL);
|
||||
CHECK(xbox_InputMap_KeyName(INPUT_KEY_COUNT) == NULL);
|
||||
for (i = 1; i < INPUT_KEY_COUNT; i++) {
|
||||
const char *name = xbox_InputMap_KeyName(i);
|
||||
CHECK(name != NULL);
|
||||
CHECK(xbox_InputMap_KeyFromName(name) == i);
|
||||
}
|
||||
CHECK(!strcmp(saved, "A")); /* names remain valid after other lookups */
|
||||
CHECK(xbox_InputMap_BindingsParse(NULL, INPUT_A, "A") == -1);
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, -1, "A") == -1);
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, INPUT_CONTROL_COUNT, "A") == -1);
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, INPUT_A, NULL) == 0);
|
||||
memset(long_text, 'q', sizeof(long_text));
|
||||
long_text[sizeof(long_text)-3] = ',';
|
||||
long_text[sizeof(long_text)-2] = 'B';
|
||||
long_text[sizeof(long_text)-1] = 0;
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, INPUT_A, long_text) == 1);
|
||||
CHECK(b.count[INPUT_A] == 1 && b.key[INPUT_A][0] == INPUT_KEY_B);
|
||||
xbox_InputMap_BindingsFormat(&b, INPUT_A, small, 1);
|
||||
CHECK(small[0] == 0 && small[1] == '!');
|
||||
CHECK(xbox_InputMap_BindingsFormat(&b, INPUT_A, NULL, 0) == NULL);
|
||||
/* Caller-corrupted counts/IDs never read outside bounded storage. */
|
||||
b.count[INPUT_A] = 255;
|
||||
for (i = 0; i < INPUT_BINDINGS_PER_CONTROL; i++) b.key[INPUT_A][i] = UINT16_MAX;
|
||||
xbox_InputMap_BindingsFormat(&b, INPUT_A, text, sizeof(text));
|
||||
CHECK(!strcmp(text, "?, ?, ?, ?"));
|
||||
down[INPUT_KEY_NONE] = 1;
|
||||
b.key[INPUT_A][0] = INPUT_KEY_NONE;
|
||||
xbox_InputMap_KeysResolve(&b, down, &out);
|
||||
CHECK(out.analog[INPUT_ANALOG_A] == 0);
|
||||
a.lx = INT16_MIN; a.ly = INT16_MIN;
|
||||
z.lx = INT16_MAX; z.ly = INT16_MAX;
|
||||
xbox_InputMap_PadMerge(&z, &a);
|
||||
CHECK(z.lx == INT16_MIN && z.ly == INT16_MIN);
|
||||
xbox_InputMap_PadMapDefaults(&map);
|
||||
map.source[INPUT_A] = 255;
|
||||
raw.src[INPUT_SRC_LEFT_TRIGGER] = -32768;
|
||||
xbox_InputMap_PadMap(&map, &raw, &out);
|
||||
CHECK(out.analog[INPUT_ANALOG_A] == 0 && out.analog[INPUT_ANALOG_LTRIGGER] == 0);
|
||||
map.button_threshold = 255;
|
||||
raw.src[INPUT_SRC_START] = 32767;
|
||||
xbox_InputMap_PadMap(&map, &raw, &out);
|
||||
CHECK(out.buttons == INPUT_PAD_START);
|
||||
raw.src[INPUT_SRC_START] = 32766;
|
||||
xbox_InputMap_PadMap(&map, &raw, &out);
|
||||
CHECK(out.buttons == 0);
|
||||
CHECK(xbox_InputMap_ApplyTrigger(-32768, -100) == 0);
|
||||
CHECK(xbox_InputMap_ApplyTrigger(32767, 255) == 255);
|
||||
}
|
||||
|
||||
/* Independent range/symmetry and format-roundtrip properties. */
|
||||
static void test_properties(void)
|
||||
{
|
||||
int x, y, dz, i;
|
||||
for (dz = 0; dz <= 100; dz += 5)
|
||||
for (x = -32767; x <= 32767; x += 8191)
|
||||
for (y = -32767; y <= 32767; y += 8191) {
|
||||
int16_t a, b, na, nb;
|
||||
xbox_InputMap_ApplyStick(x, y, dz, &a, &b);
|
||||
xbox_InputMap_ApplyStick(-x, -y, dz, &na, &nb);
|
||||
CHECK(a == -na && b == -nb);
|
||||
CHECK((int64_t)a*a + (int64_t)b*b <= INT64_C(32769)*32769);
|
||||
}
|
||||
for (dz = -5; dz <= 100; dz += 5) {
|
||||
int last = 0;
|
||||
for (i = -1; i <= 256; i++) {
|
||||
int v = xbox_InputMap_ApplyTrigger(i, dz);
|
||||
CHECK(v >= last); last = v;
|
||||
}
|
||||
CHECK(last == 255);
|
||||
}
|
||||
for (i = 1; i < INPUT_KEY_COUNT; i++) {
|
||||
InputBindings a = {0}, b = {0};
|
||||
char text[80];
|
||||
a.count[INPUT_A] = 1; a.key[INPUT_A][0] = (uint16_t)i;
|
||||
xbox_InputMap_BindingsFormat(&a, INPUT_A, text, sizeof(text));
|
||||
CHECK(xbox_InputMap_BindingsParse(&b, INPUT_A, text) == 0);
|
||||
CHECK(b.count[INPUT_A] == 1 && b.key[INPUT_A][0] == i);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_key_names();
|
||||
test_bindings();
|
||||
test_resolve();
|
||||
test_merge();
|
||||
test_deadzone();
|
||||
test_padmap();
|
||||
test_api_edges();
|
||||
test_properties();
|
||||
if (g_failed) { printf("%d check(s) failed\n", g_failed); return 1; }
|
||||
printf("input_map: %d checks passed\n", g_checks);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user