Files
BT411/engine/MUNGA_L4/L4PADBINDINGS.cpp
T
arcattackandClaude Opus 4.8 c2ee7299b2 Generic joystick / HOTAS / pedals support: DirectInput 8 layer + capture wizard
Tester ask: configure non-Xbox controllers.  New L4JOY layer (DI8):
enumerates every non-XInput game device (up to 4), DIJOYSTATE2 polling
with range normalization, reacquire-on-loss, 3s hot-plug re-enum.
XInput devices are excluded via the documented RawInput IG_ VID/PID
check (live-verified skipping a real XBOX360 pad -- no double-feed).

bindings.txt grows joydev/joyaxis/joybutton/joyhat rows (device slots
by name substring or ordinal; axes X..RZ/SL0/SL1 with invert/slew/
deadzone; hats as 4-direction buttons with diagonal chords).  PadRIO
runs them through the existing channel/event machinery -- per-binding
edge arrays release automatically on detach (the #24 rule), and a
direct throttle axis maps full lever travel onto the 0..1 channel
while the gait detent still snaps a lever parked in the walk/run dead
band.

BT_JOYCONFIG=1 (joyconfig.bat) runs a console capture wizard: move
each control when prompted (stick/twist/throttle/fire), invert derived
from the move direction, hat look cluster auto-added, output written
between markers in bindings.txt with the rest of the file preserved.

Legacy L4DINPUT DIJoystick (L4CONTROLS=DIJOYSTICK) untouched.
Verified: grammar accept/reject per line, XInput-exclusion live,
30s in-mission no-device polling clean.  Awaiting a tester with real
stick hardware for axis verification.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-23 16:36:32 -05:00

771 lines
23 KiB
C++

#include "mungal4.h"
#pragma hdrstop
//###########################################################################
// L4PADBINDINGS -- the PadRIO input-binding profile (BT_GLASS only; this TU
// is only in the build when the gate is on -- see CMakeLists.txt).
// Format + semantics: L4PADBINDINGS.h.
//###########################################################################
#include "l4padbindings.h"
#include "l4ctrl.h"
#include <windows.h>
#include <xinput.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static const char *bindingsFileName = "bindings.txt";
//###########################################################################
// Name tables
//###########################################################################
struct NamedValue
{
const char *name;
int value;
};
//
// Win32 virtual-key names accepted in bindings.txt. Single letters and
// digits are accepted directly ("W", "5"); everything else by name.
//
static const NamedValue keyNames[] =
{
{ "SPACE", VK_SPACE }, { "ENTER", VK_RETURN },
{ "TAB", VK_TAB }, { "BACKSPACE", VK_BACK },
{ "LSHIFT", VK_LSHIFT }, { "RSHIFT", VK_RSHIFT },
{ "LCTRL", VK_LCONTROL }, { "RCTRL", VK_RCONTROL },
{ "LALT", VK_LMENU }, { "RALT", VK_RMENU },
{ "UP", VK_UP }, { "DOWN", VK_DOWN },
{ "LEFT", VK_LEFT }, { "RIGHT", VK_RIGHT },
{ "INSERT", VK_INSERT }, { "DELETE", VK_DELETE },
{ "HOME", VK_HOME }, { "END", VK_END },
{ "PAGEUP", VK_PRIOR }, { "PAGEDOWN", VK_NEXT },
{ "NUMPAD0", VK_NUMPAD0 }, { "NUMPAD1", VK_NUMPAD1 },
{ "NUMPAD2", VK_NUMPAD2 }, { "NUMPAD3", VK_NUMPAD3 },
{ "NUMPAD4", VK_NUMPAD4 }, { "NUMPAD5", VK_NUMPAD5 },
{ "NUMPAD6", VK_NUMPAD6 }, { "NUMPAD7", VK_NUMPAD7 },
{ "NUMPAD8", VK_NUMPAD8 }, { "NUMPAD9", VK_NUMPAD9 },
{ "NUMPADPLUS", VK_ADD }, { "NUMPADMINUS", VK_SUBTRACT },
{ "NUMPADSTAR", VK_MULTIPLY }, { "NUMPADSLASH", VK_DIVIDE },
{ "NUMPADDOT", VK_DECIMAL },
{ "F1", VK_F1 }, { "F2", VK_F2 }, { "F3", VK_F3 }, { "F4", VK_F4 },
{ "F5", VK_F5 }, { "F6", VK_F6 }, { "F7", VK_F7 }, { "F8", VK_F8 },
{ "F9", VK_F9 }, { "F10", VK_F10 }, { "F11", VK_F11 }, { "F12", VK_F12 },
{ "MINUS", VK_OEM_MINUS }, { "EQUALS", VK_OEM_PLUS },
{ "COMMA", VK_OEM_COMMA }, { "PERIOD", VK_OEM_PERIOD },
{ "SEMICOLON", VK_OEM_1 }, { "SLASH", VK_OEM_2 },
{ "BACKTICK", VK_OEM_3 }, { "LBRACKET", VK_OEM_4 },
{ "BACKSLASH", VK_OEM_5 }, { "RBRACKET", VK_OEM_6 },
{ "QUOTE", VK_OEM_7 },
};
static const NamedValue padButtonNames[] =
{
{ "A", XINPUT_GAMEPAD_A },
{ "B", XINPUT_GAMEPAD_B },
{ "X", XINPUT_GAMEPAD_X },
{ "Y", XINPUT_GAMEPAD_Y },
{ "LB", XINPUT_GAMEPAD_LEFT_SHOULDER },
{ "RB", XINPUT_GAMEPAD_RIGHT_SHOULDER },
{ "BACK", XINPUT_GAMEPAD_BACK },
{ "START", XINPUT_GAMEPAD_START },
{ "LS", XINPUT_GAMEPAD_LEFT_THUMB },
{ "RS", XINPUT_GAMEPAD_RIGHT_THUMB },
{ "DPAD_UP", XINPUT_GAMEPAD_DPAD_UP },
{ "DPAD_DOWN", XINPUT_GAMEPAD_DPAD_DOWN },
{ "DPAD_LEFT", XINPUT_GAMEPAD_DPAD_LEFT },
{ "DPAD_RIGHT", XINPUT_GAMEPAD_DPAD_RIGHT },
};
static const NamedValue padAxisNames[] =
{
{ "LX", PadBindingProfile::PadAxisLX },
{ "LY", PadBindingProfile::PadAxisLY },
{ "RX", PadBindingProfile::PadAxisRX },
{ "RY", PadBindingProfile::PadAxisRY },
{ "LT", PadBindingProfile::PadAxisLT },
{ "RT", PadBindingProfile::PadAxisRT },
};
static const NamedValue channelNames[] =
{
{ "Throttle", PadBindingProfile::ChannelThrottle },
{ "JoystickX", PadBindingProfile::ChannelJoystickX },
{ "JoystickY", PadBindingProfile::ChannelJoystickY },
{ "LeftPedal", PadBindingProfile::ChannelLeftPedal },
{ "RightPedal", PadBindingProfile::ChannelRightPedal },
{ "Turn", PadBindingProfile::ChannelTurn }, // issue #25 composite
};
//
// Generic-joystick axis names (the DIJOYSTATE2 layout -- L4JOY.h order).
//
static const NamedValue joyAxisNames[] =
{
{ "X", PadBindingProfile::JoyAxisX },
{ "Y", PadBindingProfile::JoyAxisY },
{ "Z", PadBindingProfile::JoyAxisZ },
{ "RX", PadBindingProfile::JoyAxisRX },
{ "RY", PadBindingProfile::JoyAxisRY },
{ "RZ", PadBindingProfile::JoyAxisRZ },
{ "SL0", PadBindingProfile::JoyAxisSL0 },
{ "SL1", PadBindingProfile::JoyAxisSL1 },
};
static const NamedValue hatDirectionNames[] =
{
{ "up", 0 }, { "right", 1 }, { "down", 2 }, { "left", 3 },
};
static int
LookupName(const NamedValue *table, int count, const char *name)
{
for (int i = 0; i < count; ++i)
{
if (_stricmp(table[i].name, name) == 0)
{
return table[i].value;
}
}
return -1;
}
static int
LookupKeyName(const char *name)
{
//
// Single letter / digit binds itself ('A'..'Z','0'..'9' are the VK
// codes for the un-shifted keys).
//
if (strlen(name) == 1)
{
char c = (char)toupper((unsigned char)name[0]);
if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
{
return (int)c;
}
}
return LookupName(keyNames, sizeof(keyNames)/sizeof(keyNames[0]), name);
}
//###########################################################################
// The default profile.
//
// RIO addresses (L4CTRL.h button enum): 0x40 joystick trigger, 0x3F the
// throttle-handle reverse button, 0x41-0x44 the hat (look behind/forward/
// right/left), 0x45-0x47 pinky/thumb-low/thumb-high, 0x10-0x15 + 0x18-0x1D
// the 12 secondary-panel buttons, 0x00-0x0F the lower aux banks, 0x20-0x37
// the upper aux banks. Keypad unit 0 = the pilot's MFD keypad.
//
// KEYBOARD RECONCILIATION (2026-07-20, input-coverage audit): the keyboard
// carries the ~20 core gameplay actions on the SAME keys the desktop
// CONTROLS.MAP uses (W/S/A/D drive, Q/E twist, R/F elevation, X all-stop,
// 1-4/Space/Ctrl fire, M/N mode/display cycle, H flush, C valve, B look
// behind, G configure, V view); FULL pod-address coverage lives on the
// on-screen panel (every one of the 72 buttons is one click away,
// right-click = hold latch). Keys DISPLACED from the previous default
// (each still reachable by editing bindings.txt or on the panel):
// - number row 1-0/-/= -> secondary-panel 0x10-0x1D (1-4 now fire)
// - QWERTY row Q..P -> pilot MFD keypad unit 0 (dropped)
// - arrow keys -> hat looks 0x41-0x44 (arrows now drive)
// - V/C/B -> fire 0x47/0x46/0x45 (now view/valve/behind)
// - LCTRL -> throttle slew down (now fire 0x47)
// The XInput pad section is unchanged.
//###########################################################################
static const char *defaultProfileText =
"# bindings.txt -- PadRIO input bindings (glass cockpit). Auto-written with\n"
"# defaults on first run; edit freely, delete to regenerate. Grammar:\n"
"# key <KEYNAME> button <addr> RIO button (hex 0x.. ok)\n"
"# key <KEYNAME> keypad <unit> <key> MFD keypad unit 0-2, key 0-15\n"
"# key <KEYNAME> axis <channel> deflect <+|-> <rate> spring stick\n"
"# key <KEYNAME> axis <channel> slew <+|-> <rate> throttle lever\n"
"# key <KEYNAME> axis <channel> set <value> jump/detent\n"
"# pad <PADBTN> button <addr>\n"
"# padaxis <PADAXIS> axis <channel> [invert] [slew <rate>]\n"
"# Channels: Throttle JoystickX JoystickY LeftPedal RightPedal Turn\n"
"# (Turn = signed composite: + drives the right pedal, - the left --\n"
"# made for mapping a gamepad stick to turning)\n"
"# Pad buttons: A B X Y LB RB BACK START LS RS DPAD_* Axes: LX LY RX RY LT RT\n"
"#\n"
"# LAYOUT: the keyboard hosts the core gameplay actions on the desktop\n"
"# CONTROLS.MAP keys; the on-screen cockpit panel covers EVERY pod button\n"
"# by click (right-click = press-and-hold latch). A key bound here is\n"
"# REMOVED from the authentic 1995 typed-hotkey channel (no double\n"
"# dispatch); unbound keys keep their 1995 meaning -- 5 = MFD1 Quad page,\n"
"# z = MFD3 Eng1 page, t/y/u/i/o = pilot select 3-7, + / - = target zoom.\n"
"# Hardcoded (not rebindable here): ` or V = view toggle, J/K/L = cycle\n"
"# the Mfd1/Mfd2/Mfd3 preset page.\n"
"\n"
"# --- drive: W/S sweep the throttle lever (sticks; X = all-stop detent),\n"
"# --- A/D spring the turn pedals, Q/E twist the torso, R/F aim it up/down\n"
"key W axis Throttle slew + 0.7\n"
"key S axis Throttle slew - 0.7\n"
"key UP axis Throttle slew + 0.7\n"
"key DOWN axis Throttle slew - 0.7\n"
"key A axis LeftPedal deflect + 2.5\n"
"key D axis RightPedal deflect + 2.5\n"
"key LEFT axis LeftPedal deflect + 2.5\n"
"key RIGHT axis RightPedal deflect + 2.5\n"
"key Q axis JoystickX deflect - 2.5\n"
"key E axis JoystickX deflect + 2.5\n"
"key R axis JoystickY deflect + 2.5\n"
"key F axis JoystickY deflect - 2.5\n"
"key X axis Throttle set 0\n"
"\n"
"# --- numpad flight cluster (alternative stick/pedals/lever) ---\n"
"key NUMPAD8 axis JoystickY deflect + 2.5\n"
"key NUMPAD2 axis JoystickY deflect - 2.5\n"
"key NUMPAD4 axis JoystickX deflect - 2.5\n"
"key NUMPAD6 axis JoystickX deflect + 2.5\n"
"key NUMPAD7 axis LeftPedal deflect + 2.5\n"
"key NUMPAD9 axis RightPedal deflect + 2.5\n"
"key LSHIFT axis Throttle slew + 0.7\n"
"key NUMPAD5 axis Throttle set 0\n"
"\n"
"# --- fire groups (the joystick's four mappable buttons) + reverse + look\n"
"key 1 button 0x40\n"
"key SPACE button 0x40\n"
"key 2 button 0x46\n"
"key 3 button 0x47\n"
"key LCTRL button 0x47\n"
"key RCTRL button 0x47\n"
"key 4 button 0x45\n"
"key LALT button 0x3F\n"
"key B button 0x41\n"
"\n"
"# --- systems (pod console buttons) ---\n"
"key M button 0x18 # cycle control mode (BAS/MID/ADV)\n"
"key N button 0x15 # cycle secondary schematic (Damage/Critical/Heat)\n"
"key H button 0x2C # HOLD = coolant flush (Reservoir InjectCoolant)\n"
"key C button 0x2F # cycle Condenser1 coolant valve (MoveValve)\n"
"key G button 0x0E # HOLD + fire key = regroup weapon 1 (Mfd1 Quad page)\n"
"\n"
"# --- Mfd2 bank on F5-F8 + F9 (page-gated: Quad page = select the Eng\n"
"# --- pages, Eng page = generator A-D select / F9 gen-mode toggle) ---\n"
"key F5 button 0x27\n"
"key F6 button 0x26\n"
"key F7 button 0x25\n"
"key F8 button 0x24\n"
"key F9 button 0x22\n"
"\n"
"# --- XInput pad ---\n"
"# POD-FAITHFUL (default): stick X = torso twist, triggers = pedals.\n"
"# TWIN-STICK alternative: replace the LX line with the two below --\n"
"# padaxis LX axis Turn\n"
"# padaxis RX axis JoystickX\n"
"padaxis LX axis JoystickX\n"
"padaxis LY axis JoystickY invert\n"
"padaxis LT axis LeftPedal\n"
"padaxis RT axis RightPedal\n"
"padaxis RY axis Throttle slew 0.7\n"
"pad A button 0x40\n"
"pad B button 0x3F\n"
"pad X button 0x47\n"
"pad Y button 0x42\n"
"pad LB button 0x44\n"
"pad RB button 0x43\n"
"pad DPAD_UP button 0x42\n"
"pad DPAD_DOWN button 0x41\n"
"pad DPAD_LEFT button 0x44\n"
"pad DPAD_RIGHT button 0x43\n"
"\n"
"# --- generic joysticks (flight sticks / HOTAS / pedals; DirectInput) ---\n"
"# EASIEST: run the game once with BT_JOYCONFIG=1 (joyconfig.bat) -- an\n"
"# interactive wizard detects your controls and writes this section for you.\n"
"# Manual grammar (rows attach to the last joydev slot; slot 0 if none):\n"
"# joydev <slot 0-3> [product-name substring]\n"
"# joyaxis <X|Y|Z|RX|RY|RZ|SL0|SL1> axis <channel> [invert] [slew <r>] [deadzone <f>]\n"
"# joybutton <0-31> button <addr>\n"
"# joyhat <0-3> <up|down|left|right> button <addr>\n"
"# Typical flight stick (twist = RZ, throttle wheel = SL0):\n"
"# joydev 0\n"
"# joyaxis X axis JoystickX\n"
"# joyaxis Y axis JoystickY invert\n"
"# joyaxis RZ axis Turn\n"
"# joyaxis SL0 axis Throttle invert deadzone 0\n"
"# joybutton 0 button 0x40\n"
"# joybutton 1 button 0x46\n"
"# joyhat 0 up button 0x42\n"
"# joyhat 0 down button 0x41\n"
"# joyhat 0 left button 0x44\n"
"# joyhat 0 right button 0x43\n";
//###########################################################################
// PadBindingProfile
//###########################################################################
PadBindingProfile::PadBindingProfile():
keyBindingCount(0),
padButtonBindingCount(0),
padAxisBindingCount(0),
joyAxisBindingCount(0),
joyButtonBindingCount(0),
joyHatBindingCount(0),
parseJoyDevice(0)
{
memset(joyDeviceMatch, 0, sizeof(joyDeviceMatch));
}
PadBindingProfile::~PadBindingProfile()
{
}
void
PadBindingProfile::WriteDefaultFile(const char *path)
{
FILE *f = fopen(path, "wt");
if (f == NULL)
{
DEBUG_STREAM << "[padrio] cannot write " << path
<< " -- using built-in defaults\n" << std::flush;
return;
}
fputs(defaultProfileText, f);
fclose(f);
DEBUG_STREAM << "[padrio] wrote default " << path << "\n" << std::flush;
}
//
// Parse one binding line into the tables. Returns 0 on success, -1 on a
// malformed line (caller logs). The line is tokenized in place.
//
int
PadBindingProfile::ParseLine(char *line, int line_number)
{
char *tokens[8];
int tokenCount = 0;
char *cursor = line;
while (tokenCount < 8)
{
while (*cursor == ' ' || *cursor == '\t') ++cursor;
if (*cursor == '\0' || *cursor == '#' ||
*cursor == '\r' || *cursor == '\n')
{
break;
}
tokens[tokenCount++] = cursor;
while (*cursor && *cursor != ' ' && *cursor != '\t' &&
*cursor != '\r' && *cursor != '\n')
{
++cursor;
}
if (*cursor) *cursor++ = '\0';
}
if (tokenCount == 0)
{
return 0; // blank / comment
}
if (tokenCount < 3)
{
return -1;
}
//
// Build the action from tokens[2]... (shared by key/pad rows).
//
Action action;
memset(&action, 0, sizeof(action));
int actionAt = 2;
if (_stricmp(tokens[0], "padaxis") == 0)
{
if (tokenCount < 4 || _stricmp(tokens[2], "axis") != 0)
{
return -1;
}
int axis = LookupName(padAxisNames,
sizeof(padAxisNames)/sizeof(padAxisNames[0]), tokens[1]);
int channel = LookupName(channelNames,
sizeof(channelNames)/sizeof(channelNames[0]), tokens[3]);
if (axis < 0 || channel < 0 ||
padAxisBindingCount >= (int)(sizeof(padAxisBindings)/sizeof(padAxisBindings[0])))
{
return -1;
}
PadAxisBinding &b = padAxisBindings[padAxisBindingCount];
b.axis = axis;
b.channel = channel;
b.invert = 0;
b.slew = 0;
b.slewRate = 0.0f;
for (int t = 4; t < tokenCount; ++t)
{
if (_stricmp(tokens[t], "invert") == 0)
{
b.invert = 1;
}
else if (_stricmp(tokens[t], "slew") == 0 && t+1 < tokenCount)
{
b.slew = 1;
b.slewRate = (float)atof(tokens[++t]);
}
else
{
return -1;
}
}
++padAxisBindingCount;
return 0;
}
//
// joydev <slot> [name substring...] -- select the device slot for the
// joy rows that follow; optional product-name match (joined verbatim).
//
if (_stricmp(tokens[0], "joydev") == 0)
{
int slot = (int)strtol(tokens[1], NULL, 0);
if (slot < 0 || slot >= JoyDeviceSlots)
{
return -1;
}
parseJoyDevice = slot;
joyDeviceMatch[slot][0] = '\0';
for (int t = 2; t < tokenCount; ++t)
{
if (t > 2)
{
strncat(joyDeviceMatch[slot], " ",
sizeof(joyDeviceMatch[slot]) - strlen(joyDeviceMatch[slot]) - 1);
}
strncat(joyDeviceMatch[slot], tokens[t],
sizeof(joyDeviceMatch[slot]) - strlen(joyDeviceMatch[slot]) - 1);
}
return 0;
}
//
// joyaxis <AXIS> axis <channel> [invert] [slew <rate>] [deadzone <f>]
//
if (_stricmp(tokens[0], "joyaxis") == 0)
{
if (tokenCount < 4 || _stricmp(tokens[2], "axis") != 0)
{
return -1;
}
int axis = LookupName(joyAxisNames,
sizeof(joyAxisNames)/sizeof(joyAxisNames[0]), tokens[1]);
int channel = LookupName(channelNames,
sizeof(channelNames)/sizeof(channelNames[0]), tokens[3]);
if (axis < 0 || channel < 0 ||
joyAxisBindingCount >= (int)(sizeof(joyAxisBindings)/sizeof(joyAxisBindings[0])))
{
return -1;
}
JoyAxisBinding &b = joyAxisBindings[joyAxisBindingCount];
b.device = parseJoyDevice;
b.axis = axis;
b.channel = channel;
b.invert = 0;
b.slew = 0;
b.slewRate = 0.0f;
b.deadzone = 0.08f; // stick default; levers set deadzone 0
for (int t = 4; t < tokenCount; ++t)
{
if (_stricmp(tokens[t], "invert") == 0)
{
b.invert = 1;
}
else if (_stricmp(tokens[t], "slew") == 0 && t+1 < tokenCount)
{
b.slew = 1;
b.slewRate = (float)atof(tokens[++t]);
}
else if (_stricmp(tokens[t], "deadzone") == 0 && t+1 < tokenCount)
{
b.deadzone = (float)atof(tokens[++t]);
if (b.deadzone < 0.0f) b.deadzone = 0.0f;
if (b.deadzone > 0.9f) b.deadzone = 0.9f;
}
else
{
return -1;
}
}
++joyAxisBindingCount;
return 0;
}
//
// joyhat's action clause starts one token later (joyhat <n> <dir> ...).
//
if (_stricmp(tokens[0], "joyhat") == 0)
{
actionAt = 3;
if (tokenCount < 5)
{
return -1;
}
}
//
// key/pad/joybutton/joyhat rows: parse the action clause.
//
if (_stricmp(tokens[actionAt], "button") == 0)
{
if (tokenCount < actionAt+2)
{
return -1;
}
action.kind = ActionButton;
action.address = (int)strtol(tokens[actionAt+1], NULL, 0);
if (action.address < 0 ||
action.address >= LBE4ControlsManager::ButtonCount)
{
return -1; // out of the buttonGroup range -- would overrun
}
}
else if (_stricmp(tokens[actionAt], "keypad") == 0)
{
if (tokenCount < actionAt+3)
{
return -1;
}
action.kind = ActionKeypad;
action.address = (int)strtol(tokens[actionAt+1], NULL, 0);
action.key = (int)strtol(tokens[actionAt+2], NULL, 0);
if (action.address < 0 ||
action.address >= LBE4ControlsManager::KeyboardCount ||
action.key < 0 || action.key > 15)
{
return -1;
}
}
else if (_stricmp(tokens[actionAt], "axis") == 0)
{
if (tokenCount < actionAt+3)
{
return -1;
}
action.channel = LookupName(channelNames,
sizeof(channelNames)/sizeof(channelNames[0]), tokens[actionAt+1]);
if (action.channel < 0)
{
return -1;
}
const char *mode = tokens[actionAt+2];
if (_stricmp(mode, "set") == 0)
{
if (tokenCount < actionAt+4)
{
return -1;
}
action.kind = ActionAxisSet;
action.rate = (float)atof(tokens[actionAt+3]);
}
else
{
if (tokenCount < actionAt+5)
{
return -1;
}
if (_stricmp(mode, "deflect") == 0)
{
action.kind = ActionAxisDeflect;
}
else if (_stricmp(mode, "slew") == 0)
{
action.kind = ActionAxisSlew;
}
else
{
return -1;
}
float sign;
if (strcmp(tokens[actionAt+3], "+") == 0) sign = 1.0f;
else if (strcmp(tokens[actionAt+3], "-") == 0) sign = -1.0f;
else
{
return -1;
}
action.rate = sign * (float)atof(tokens[actionAt+4]);
}
}
else
{
return -1;
}
if (_stricmp(tokens[0], "key") == 0)
{
int vk = LookupKeyName(tokens[1]);
if (vk < 0 ||
keyBindingCount >= (int)(sizeof(keyBindings)/sizeof(keyBindings[0])))
{
return -1;
}
keyBindings[keyBindingCount].virtualKey = vk;
keyBindings[keyBindingCount].action = action;
++keyBindingCount;
return 0;
}
else if (_stricmp(tokens[0], "pad") == 0)
{
if (action.kind != ActionButton && action.kind != ActionKeypad)
{
return -1; // pad buttons cannot drive axes; use padaxis
}
int mask = LookupName(padButtonNames,
sizeof(padButtonNames)/sizeof(padButtonNames[0]), tokens[1]);
if (mask < 0 ||
padButtonBindingCount >= (int)(sizeof(padButtonBindings)/sizeof(padButtonBindings[0])))
{
return -1;
}
padButtonBindings[padButtonBindingCount].buttonMask = (unsigned)mask;
padButtonBindings[padButtonBindingCount].action = action;
++padButtonBindingCount;
return 0;
}
else if (_stricmp(tokens[0], "joybutton") == 0)
{
if (action.kind != ActionButton && action.kind != ActionKeypad)
{
return -1; // joystick buttons cannot drive axes; use joyaxis
}
int button = (int)strtol(tokens[1], NULL, 0);
if (button < 0 || button >= 32 ||
joyButtonBindingCount >= (int)(sizeof(joyButtonBindings)/sizeof(joyButtonBindings[0])))
{
return -1;
}
joyButtonBindings[joyButtonBindingCount].device = parseJoyDevice;
joyButtonBindings[joyButtonBindingCount].button = button;
joyButtonBindings[joyButtonBindingCount].action = action;
++joyButtonBindingCount;
return 0;
}
else if (_stricmp(tokens[0], "joyhat") == 0)
{
if (action.kind != ActionButton)
{
return -1; // hat directions map to RIO buttons only
}
int hat = (int)strtol(tokens[1], NULL, 0);
int direction = LookupName(hatDirectionNames,
sizeof(hatDirectionNames)/sizeof(hatDirectionNames[0]), tokens[2]);
if (hat < 0 || hat >= 4 || direction < 0 ||
joyHatBindingCount >= (int)(sizeof(joyHatBindings)/sizeof(joyHatBindings[0])))
{
return -1;
}
joyHatBindings[joyHatBindingCount].device = parseJoyDevice;
joyHatBindings[joyHatBindingCount].hat = hat;
joyHatBindings[joyHatBindingCount].direction = direction;
joyHatBindings[joyHatBindingCount].action = action;
++joyHatBindingCount;
return 0;
}
return -1;
}
void
PadBindingProfile::LoadDefaults()
{
keyBindingCount = 0;
padButtonBindingCount = 0;
padAxisBindingCount = 0;
joyAxisBindingCount = 0;
joyButtonBindingCount = 0;
joyHatBindingCount = 0;
parseJoyDevice = 0;
memset(joyDeviceMatch, 0, sizeof(joyDeviceMatch));
//
// Parse the built-in default text line by line (one code path for
// defaults and files -- the defaults ARE a valid file).
//
const char *cursor = defaultProfileText;
char line[256];
int line_number = 0;
while (*cursor)
{
int n = 0;
while (cursor[n] && cursor[n] != '\n' && n < (int)sizeof(line)-1)
{
++n;
}
memcpy(line, cursor, n);
line[n] = '\0';
cursor += n + (cursor[n] == '\n' ? 1 : 0);
++line_number;
if (ParseLine(line, line_number) != 0)
{
DEBUG_STREAM << "[padrio] BUG: default profile line "
<< line_number << " malformed\n" << std::flush;
}
}
}
void
PadBindingProfile::Load()
{
FILE *f = fopen(bindingsFileName, "rt");
if (f == NULL)
{
WriteDefaultFile(bindingsFileName);
f = fopen(bindingsFileName, "rt");
}
if (f == NULL)
{
LoadDefaults();
return;
}
keyBindingCount = 0;
padButtonBindingCount = 0;
padAxisBindingCount = 0;
joyAxisBindingCount = 0;
joyButtonBindingCount = 0;
joyHatBindingCount = 0;
parseJoyDevice = 0;
memset(joyDeviceMatch, 0, sizeof(joyDeviceMatch));
char line[256];
int line_number = 0;
int accepted = 0, rejected = 0;
while (fgets(line, sizeof(line), f))
{
++line_number;
char parse_copy[256];
strcpy(parse_copy, line);
if (ParseLine(parse_copy, line_number) != 0)
{
DEBUG_STREAM << "[padrio] " << bindingsFileName << " line "
<< line_number << " rejected: " << line << std::flush;
++rejected;
}
else
{
++accepted;
}
}
fclose(f);
if (keyBindingCount == 0 && padAxisBindingCount == 0)
{
//
// A file that binds NOTHING is treated as damaged -- fall back so
// the developer is never left with a dead cockpit.
//
DEBUG_STREAM << "[padrio] " << bindingsFileName
<< " contains no usable bindings -- using built-in defaults\n"
<< std::flush;
LoadDefaults();
return;
}
DEBUG_STREAM << "[padrio] bindings loaded: " << keyBindingCount
<< " keys, " << padButtonBindingCount << " pad buttons, "
<< padAxisBindingCount << " pad axes, "
<< joyAxisBindingCount << " joy axes, "
<< joyButtonBindingCount << " joy buttons, "
<< joyHatBindingCount << " joy hats"
<< (rejected ? " (rejected lines logged above)" : "")
<< "\n" << std::flush;
}