From 92bf9e357b5998162e49dfc46a5c78c8e0c2373f Mon Sep 17 00:00:00 2001 From: freezy Date: Sat, 21 Nov 2026 19:17:46 +0100 Subject: [PATCH 1/3] input: route OPD secondary switches to the upper flippers --- src/OpenPinballDevice.h | 2 +- tests/OpenPinballDeviceTests.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/OpenPinballDevice.h b/src/OpenPinballDevice.h index 1dedaae..5f7f190 100644 --- a/src/OpenPinballDevice.h +++ b/src/OpenPinballDevice.h @@ -134,7 +134,7 @@ namespace OpenPinballDevice { VPE_INPUT_COIN_INSERT_1, VPE_INPUT_COIN_INSERT_2, VPE_INPUT_COIN_INSERT_3, VPE_INPUT_COIN_INSERT_4, VPE_INPUT_PLUNGE, VPE_INPUT_LOCKBAR, VPE_INPUT_LEFT_FLIPPER, VPE_INPUT_RIGHT_FLIPPER, - VPE_INPUT_LEFT_STAGED_FLIPPER, VPE_INPUT_RIGHT_STAGED_FLIPPER, + VPE_INPUT_UPPER_LEFT_FLIPPER, VPE_INPUT_UPPER_RIGHT_FLIPPER, VPE_INPUT_LEFT_MAGNASAVE, VPE_INPUT_RIGHT_MAGNASAVE, VPE_INPUT_TILT, VPE_INPUT_SLAM_TILT, VPE_INPUT_COIN_DOOR, VPE_INPUT_SERVICE_1, VPE_INPUT_SERVICE_2, VPE_INPUT_SERVICE_3, VPE_INPUT_SERVICE_4, diff --git a/tests/OpenPinballDeviceTests.cpp b/tests/OpenPinballDeviceTests.cpp index 00d9af6..e28bca6 100644 --- a/tests/OpenPinballDeviceTests.cpp +++ b/tests/OpenPinballDeviceTests.cpp @@ -124,6 +124,22 @@ static void CheckPollingState() { input.Receive(first, report, sizeof(report), 50); Check(events.size() == 8 && events[2].eventType == VPE_INPUT_EVENT_AXIS && events[2].value == .5f, "Resume OPD axis callbacks when gameplay resumes"); + events.clear(); + // Keep the device timestamp unchanged to test button events without axis samples. + report[14] = 0x08; // Left upper flipper, bit 11. + input.Receive(first, report, sizeof(report), 60); + report[14] |= 0x10; // Right upper flipper, bit 12. + input.Receive(first, report, sizeof(report), 70); + report[14] = 0x10; + input.Receive(first, report, sizeof(report), 80); + report[14] = 0; + input.Receive(first, report, sizeof(report), 90); + Check(events.size() == 4 + && events[0].action == VPE_INPUT_UPPER_LEFT_FLIPPER && events[0].value == 1 + && events[1].action == VPE_INPUT_UPPER_RIGHT_FLIPPER && events[1].value == 1 + && events[2].action == VPE_INPUT_UPPER_LEFT_FLIPPER && events[2].value == 0 + && events[3].action == VPE_INPUT_UPPER_RIGHT_FLIPPER && events[3].value == 0, + "Route and release both upper flipper switches"); input.Stop(); Check(input.ListDevices(nullptr, 0) == 0, "Clear snapshots when polling stops"); } From de585625f416512b94c15cec8fa61e4be164649d Mon Sep 17 00:00:00 2001 From: freezy Date: Sat, 21 Nov 2026 20:42:30 +0100 Subject: [PATCH 2/3] input: support per-device controller action bindings --- src/ControllerInput.cpp | 13 +++++++++ src/NativeButtons.cpp | 10 +++++-- src/NativeButtons.h | 2 +- src/NativeInput.h | 2 ++ src/NativeInput_Windows.cpp | 6 +++-- tests/ControllerInputTests.cpp | 48 +++++++++++++++++++++++++++++++++- 6 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/ControllerInput.cpp b/src/ControllerInput.cpp index cfb7442..0d0b04e 100644 --- a/src/ControllerInput.cpp +++ b/src/ControllerInput.cpp @@ -28,6 +28,7 @@ namespace { constexpr int64_t ScanIntervalUsec = 250000; constexpr int RawGamepadButtonBase = 0x10000; constexpr int HatButtonBase = 0x20000; + constexpr int TriggerButtonBase = 0x30000; constexpr std::array HatDirections = {SDL_HAT_UP, SDL_HAT_RIGHT, SDL_HAT_DOWN, SDL_HAT_LEFT}; constexpr std::array GamepadAxisNames = { "Left Stick X", "Left Stick Y", "Right Stick X", "Right Stick Y", "Left Trigger", "Right Trigger" @@ -61,6 +62,14 @@ namespace { std::snprintf(button.name, sizeof(button.name), "%s", ValueOrEmpty(name)); buttons.push_back(button); } + for (int trigger = 0; trigger < 2; ++trigger) { + const auto axis = static_cast(SDL_GAMEPAD_AXIS_LEFT_TRIGGER + trigger); + if (!SDL_GamepadHasAxis(gamepad, axis)) continue; + VpeInputButtonInfo button{}; + button.buttonId = TriggerButtonBase + trigger; + std::snprintf(button.name, sizeof(button.name), "%s", GamepadAxisNames[axis]); + buttons.push_back(button); + } } for (int i = 0; i < rawCount; ++i) { if (mapped[i]) continue; @@ -316,6 +325,10 @@ void ControllerInput::Poll(int64_t timestamp, bool foreground, VpeInputEventCall } auto axis = device->axes.begin(); NativeButtons::GetState().Update(device->info.deviceIndex, [&](const auto& button) { + if (button.buttonId == TriggerButtonBase || button.buttonId == TriggerButtonBase + 1) { + const auto axis = static_cast(SDL_GAMEPAD_AXIS_LEFT_TRIGGER + button.buttonId - TriggerButtonBase); + return SDL_GetGamepadAxis(device->gamepad, axis) >= (button.isPressed ? 12000 : 18000); + } if (button.buttonId >= HatButtonBase) { const auto offset = static_cast(button.buttonId - HatButtonBase); const auto hat = SDL_GetJoystickHat(device->joystick, static_cast(offset / HatDirections.size())); diff --git a/src/NativeButtons.cpp b/src/NativeButtons.cpp index dafe47d..4e69401 100644 --- a/src/NativeButtons.cpp +++ b/src/NativeButtons.cpp @@ -67,7 +67,7 @@ namespace NativeButtons { bool hasAxisBindings = false; for (int i = 0; controls != nullptr && i < count; ++i) { const auto& control = controls[i]; - if (!IsNudgeOrTilt(control.action) || control.group < 0) continue; + if (control.action < 0 || control.action >= VPE_INPUT_MAX || control.group < 0) continue; auto group = std::find_if(groups.begin(), groups.end(), [&](const Binding& binding) { return binding.action == control.action && binding.group == control.group; }); @@ -81,6 +81,7 @@ namespace NativeButtons { // Reject the entire group if any member is malformed. return std::any_of(group.controls.begin(), group.controls.end(), [](const auto& member) { const auto& control = member.control; + if (control.kind != VPE_CONTROL_BUTTON && !IsNudgeOrTilt(control.action)) return true; const bool validThreshold = control.kind == VPE_CONTROL_BUTTON || (control.kind == VPE_CONTROL_AXIS_ABOVE && control.threshold > 0.0f) || (control.kind == VPE_CONTROL_AXIS_BELOW && control.threshold < 0.0f); @@ -150,10 +151,11 @@ namespace NativeButtons { m_captureCandidates.clear(); } - int State::List(int deviceIndex, VpeInputButtonInfo* buttons, int maxButtons) { + int State::List(int deviceIndex, VpeInputButtonInfo* buttons, int maxButtons, bool requireSample) { std::lock_guard lock(m_mutex); for (const auto& device : m_devices) { if (device.index != deviceIndex) continue; + if (requireSample && !device.buttonsSampled) return 0; const int count = static_cast(device.buttons.size()); if (buttons != nullptr && maxButtons > 0) std::copy_n(device.buttons.begin(), std::min(count, maxButtons), buttons); return count; @@ -236,6 +238,10 @@ extern "C" { return NativeButtons::GetState().List(deviceIndex, buttons, maxButtons); } + VPE_API int VpeInputReadDeviceButtons(int deviceIndex, VpeInputButtonInfo* buttons, int maxButtons) { + return NativeButtons::GetState().List(deviceIndex, buttons, maxButtons, true); + } + VPE_API void VpeInputSetControlBindings(const VpeInputControlBinding* controls, int count) { NativeButtons::GetState().ConfigureControls(controls, count); } diff --git a/src/NativeButtons.h b/src/NativeButtons.h index c94d2a1..69af11c 100644 --- a/src/NativeButtons.h +++ b/src/NativeButtons.h @@ -19,7 +19,7 @@ namespace NativeButtons { void ConfigureControls(const VpeInputControlBinding* controls, int count); void StartCapture(int mode); int ReadCapture(VpeInputControlBinding* controls, int maxControls); - int List(int deviceIndex, VpeInputButtonInfo* buttons, int maxButtons); + int List(int deviceIndex, VpeInputButtonInfo* buttons, int maxButtons, bool requireSample = false); uint64_t PressedActions(); template diff --git a/src/NativeInput.h b/src/NativeInput.h index f14257d..841e550 100644 --- a/src/NativeInput.h +++ b/src/NativeInput.h @@ -271,6 +271,8 @@ VPE_API int VpeInputListDeviceAxes(int deviceIndex, VpeInputAxisInfo* axes, int // predefined OPD mappings. Replace them atomically, even while polling. VPE_API void VpeInputSetButtonBindings(const VpeInputButtonBinding* bindings, int count); VPE_API int VpeInputListDeviceButtons(int deviceIndex, VpeInputButtonInfo* buttons, int maxButtons); +// Copies sampled button levels. Returns zero until the device sends its first report. +VPE_API int VpeInputReadDeviceButtons(int deviceIndex, VpeInputButtonInfo* buttons, int maxButtons); VPE_API void VpeInputSetControlBindings(const VpeInputControlBinding* controls, int count); // 0 cancels, 1 captures a single control, 2 captures controls held together. VPE_API void VpeInputStartControlCapture(int mode); diff --git a/src/NativeInput_Windows.cpp b/src/NativeInput_Windows.cpp index 76fb4b5..25b4893 100644 --- a/src/NativeInput_Windows.cpp +++ b/src/NativeInput_Windows.cpp @@ -1118,8 +1118,10 @@ VPE_API int VpeInputStartPolling(VpeInputEventCallback callback, void* userData, { std::lock_guard lock(g_bindingsMutex); AddOpdBindings(); - for (auto& binding : g_bindings) { - binding.latch = {}; + // A controller-only action needs a latch even when it has no key or OPD mapping. + g_bindings.reserve(VPE_INPUT_MAX); + for (int action = 0; action < VPE_INPUT_MAX; ++action) { + GetBinding(static_cast(action)).latch = {}; } } if (g_stopEvent != NULL) { diff --git a/tests/ControllerInputTests.cpp b/tests/ControllerInputTests.cpp index e26e2b7..9788dbe 100644 --- a/tests/ControllerInputTests.cpp +++ b/tests/ControllerInputTests.cpp @@ -60,6 +60,16 @@ static SDL_JoystickID Attach(const char* name, SDL_JoystickType type, Uint16 axe } int main() { + const int newDeviceIndex = 77; + VpeInputButtonInfo button{0, 0, "Button 1"}; + NativeButtons::GetState().Connect(newDeviceIndex, "new-board", {button}); + Check(VpeInputListDeviceButtons(newDeviceIndex, &button, 1) == 1, "List button metadata before the first report"); + Check(VpeInputReadDeviceButtons(newDeviceIndex, &button, 1) == 0, "Do not report a fabricated release before the first report"); + NativeButtons::GetState().Update(newDeviceIndex, [](const auto&) { return true; }); + Check(VpeInputReadDeviceButtons(newDeviceIndex, &button, 1) == 1 && button.isPressed, + "Read the held button after the first report"); + NativeButtons::GetState().Disconnect(newDeviceIndex); + ControllerInput input; input.Start(); Observed observed{input}; @@ -94,8 +104,26 @@ int main() { && std::strcmp(rawButtons[1].name, "Hat 1 Up") == 0 && std::strcmp(rawButtons[2].name, "Hat 1 Right") == 0, "Expose raw joystick buttons and POV directions"); const auto rawButton = rawButtons[0]; - Check(VpeInputListDeviceButtons(gamepad.deviceIndex, &padButton, 1) == 1 + Check(VpeInputListDeviceButtons(gamepad.deviceIndex, &padButton, 1) >= 1 && padButton.buttonId == SDL_GAMEPAD_BUTTON_SOUTH, "Expose standardized gamepad buttons"); + constexpr int leftTrigger = 0x30000; + const auto triggerPressed = [&]() { + std::array buttons{}; + const int count = VpeInputReadDeviceButtons(gamepad.deviceIndex, buttons.data(), static_cast(buttons.size())); + const auto end = buttons.begin() + std::min(count, static_cast(buttons.size())); + const auto match = std::find_if(buttons.begin(), end, [leftTrigger](const auto& button) { return button.buttonId == leftTrigger; }); + Check(match != end, "Advertise the gamepad left trigger as a bindable button"); + return match->isPressed != 0; + }; + Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_JOYSTICK_AXIS_MIN), "Release the trigger"); + input.Poll(1010, true, callback, &observed); + Check(!triggerPressed(), "The released trigger is up"); + Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_JOYSTICK_AXIS_MAX), "Press the trigger"); + input.Poll(1020, true, callback, &observed); + Check(triggerPressed(), "The pressed trigger is down"); + Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_JOYSTICK_AXIS_MIN), "Release the trigger again"); + input.Poll(1030, true, callback, &observed); + Check(!triggerPressed(), "The trigger returns to up"); VpeInputButtonBinding bindings[] = {{VPE_INPUT_LEFT_NUDGE, rawButton.buttonId}, {VPE_INPUT_LEFT_NUDGE, padButton.buttonId}}; std::snprintf(bindings[0].deviceId, sizeof(bindings[0].deviceId), "%s", pinscape.stableId); std::snprintf(bindings[1].deviceId, sizeof(bindings[1].deviceId), "%s", gamepad.stableId); @@ -139,6 +167,24 @@ int main() { makeBinding(VPE_INPUT_SLAM_TILT, 0, VPE_CONTROL_BUTTON, rawButtons[1].buttonId, 0, pinscape.stableId), makeBinding(VPE_INPUT_SLAM_TILT, 0, VPE_CONTROL_AXIS_ABOVE, 0, .5f, gamepad.stableId), }; + const VpeInputControlBinding gameplay[] = { + makeBinding(VPE_INPUT_UPPER_LEFT_FLIPPER, 0, VPE_CONTROL_BUTTON, rawButton.buttonId, 0, pinscape.stableId), + makeBinding(VPE_INPUT_START, 0, VPE_CONTROL_BUTTON, padButton.buttonId, 0, gamepad.stableId), + makeBinding(VPE_INPUT_PLUNGE, 0, VPE_CONTROL_AXIS_ABOVE, 0, .1f, gamepad.stableId), + }; + VpeInputSetControlBindings(gameplay, 3); + Check(SDL_SetJoystickVirtualButton(pad, 0, true), "Press Start on the virtual gamepad"); + input.Poll(4020, true, callback, &observed); + const uint64_t flipper = uint64_t{1} << VPE_INPUT_UPPER_LEFT_FLIPPER; + const uint64_t start = uint64_t{1} << VPE_INPUT_START; + Check(NativeButtons::GetState().PressedActions() == (flipper | start), + "Map the joystick flipper and gamepad Start to gameplay actions"); + Check(SDL_SetJoystickVirtualButton(pad, 0, false), "Release Start"); + Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFTX, 16384), "Move the gamepad stick"); + input.Poll(4050, true, callback, &observed); + Check(NativeButtons::GetState().PressedActions() == flipper, + "Release Start independently; ordinary gameplay actions do not accept axis thresholds"); + VpeInputSetControlBindings(controls, 4); Check(SDL_SetJoystickVirtualAxis(pad, 0, 24576) && SDL_SetJoystickVirtualHat(raw, 0, SDL_HAT_RIGHTUP), "Inject a stick and diagonal POV combination"); From dc6006e1f42b580992b560c81d22d1516502db2c Mon Sep 17 00:00:00 2001 From: freezy Date: Sat, 21 Nov 2026 21:00:17 +0100 Subject: [PATCH 3/3] input: fix trigger capture and partial button reports --- src/ControllerInput.cpp | 5 ++- src/NativeButtons.cpp | 13 +++++-- src/NativeButtons.h | 26 ++++++++----- src/NativeInput_Windows.cpp | 18 +++++---- tests/ControllerInputTests.cpp | 67 ++++++++++++++++++++++++++++++---- 5 files changed, 101 insertions(+), 28 deletions(-) diff --git a/src/ControllerInput.cpp b/src/ControllerInput.cpp index 0d0b04e..2166cad 100644 --- a/src/ControllerInput.cpp +++ b/src/ControllerInput.cpp @@ -290,7 +290,10 @@ bool ControllerInput::Reconcile(int64_t timestamp) { if (device->axes.empty() && buttons.empty()) continue; std::vector axisIds; for (const auto& axis : device->axes) axisIds.push_back(axis.axisId); - NativeButtons::GetState().Connect(device->info.deviceIndex, stableId, std::move(buttons), axisIds); + // Keep both trigger axes available to analog mappings, but capture their button representation once. + const std::vector buttonBackedAxes = standardized + ? std::vector{SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER} : std::vector{}; + NativeButtons::GetState().Connect(device->info.deviceIndex, stableId, std::move(buttons), axisIds, buttonBackedAxes); device->info.axisCount = static_cast(device->axes.size()); m_devices.push_back(std::move(device)); changed = true; diff --git a/src/NativeButtons.cpp b/src/NativeButtons.cpp index 4e69401..933c58c 100644 --- a/src/NativeButtons.cpp +++ b/src/NativeButtons.cpp @@ -28,10 +28,15 @@ namespace NativeButtons { } void State::Connect(int deviceIndex, const std::string& stableId, std::vector buttons, - const std::vector& axisIds) { + const std::vector& axisIds, const std::vector& buttonBackedAxes) { if (buttons.empty() && axisIds.empty()) return; - Device device{deviceIndex, stableId.substr(0, VPE_INPUT_DEVICE_ID_SIZE - 1), std::move(buttons), {}}; - for (int id : axisIds) device.axes.push_back({id}); + const size_t buttonCount = buttons.size(); + Device device{deviceIndex, stableId.substr(0, VPE_INPUT_DEVICE_ID_SIZE - 1), std::move(buttons), {}, + std::vector(buttonCount, false), buttonCount}; + for (int id : axisIds) { + const bool buttonBacked = std::find(buttonBackedAxes.begin(), buttonBackedAxes.end(), id) != buttonBackedAxes.end(); + device.axes.push_back({id, buttonBacked}); + } std::lock_guard lock(m_mutex); m_devices.push_back(std::move(device)); } @@ -155,7 +160,7 @@ namespace NativeButtons { std::lock_guard lock(m_mutex); for (const auto& device : m_devices) { if (device.index != deviceIndex) continue; - if (requireSample && !device.buttonsSampled) return 0; + if (requireSample && device.unsampledButtons != 0) return 0; const int count = static_cast(device.buttons.size()); if (buttons != nullptr && maxButtons > 0) std::copy_n(device.buttons.begin(), std::min(count, maxButtons), buttons); return count; diff --git a/src/NativeButtons.h b/src/NativeButtons.h index 69af11c..a0b4bc1 100644 --- a/src/NativeButtons.h +++ b/src/NativeButtons.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -13,7 +14,7 @@ namespace NativeButtons { class State { public: void Connect(int deviceIndex, const std::string& stableId, std::vector buttons, - const std::vector& axisIds = {}); + const std::vector& axisIds = {}, const std::vector& buttonBackedAxes = {}); void Disconnect(int deviceIndex); void Configure(const VpeInputButtonBinding* bindings, int count); void ConfigureControls(const VpeInputControlBinding* controls, int count); @@ -33,20 +34,25 @@ namespace NativeButtons { for (auto& device : m_devices) { if (device.index != deviceIndex) continue; bool changed = false; - for (auto& button : device.buttons) { - const bool pressed = sampleButton(button); + for (size_t i = 0; i < device.buttons.size(); ++i) { + auto& button = device.buttons[i]; + const std::optional pressed = sampleButton(button); + if (!pressed.has_value()) continue; const bool wasPressed = button.isPressed != 0; - changed |= pressed != wasPressed; - if (m_captureMode && device.buttonsSampled && pressed && !wasPressed) + changed |= *pressed != wasPressed; + if (m_captureMode && device.buttonSampled[i] && *pressed && !wasPressed) CaptureCandidate(device.id, VPE_CONTROL_BUTTON, button.buttonId, 0.0f); - button.isPressed = pressed; + button.isPressed = *pressed; + if (!device.buttonSampled[i]) { + device.buttonSampled[i] = true; + --device.unsampledButtons; + } } - device.buttonsSampled = true; for (auto& axis : device.axes) { const float value = sampleAxis(axis.id, axis.value); changed |= m_hasAxisBindings && value != axis.value && (std::isfinite(value) || std::isfinite(axis.value)); - if (m_captureMode && std::isfinite(axis.value)) { + if (m_captureMode && !axis.buttonBacked && std::isfinite(axis.value)) { if (value >= CaptureThreshold && axis.value < CaptureThreshold) CaptureCandidate(device.id, VPE_CONTROL_AXIS_ABOVE, axis.id, CaptureThreshold); if (value <= -CaptureThreshold && axis.value > -CaptureThreshold) @@ -65,6 +71,7 @@ namespace NativeButtons { struct Axis { int id; + bool buttonBacked; float value = std::numeric_limits::quiet_NaN(); }; @@ -73,7 +80,8 @@ namespace NativeButtons { std::string id; std::vector buttons; std::vector axes; - bool buttonsSampled = false; + std::vector buttonSampled; + size_t unsampledButtons; }; struct Binding { diff --git a/src/NativeInput_Windows.cpp b/src/NativeInput_Windows.cpp index 25b4893..ef9a7ed 100644 --- a/src/NativeInput_Windows.cpp +++ b/src/NativeInput_Windows.cpp @@ -65,6 +65,7 @@ namespace { struct HatDescriptor { HIDP_VALUE_CAPS caps; uint8_t directions = 0; + bool sampledThisReport = false; }; struct HidDevice { @@ -745,8 +746,10 @@ namespace { constexpr uint8_t directions[] = {1, 3, 2, 6, 4, 12, 8, 9}; // clockwise from up for (auto& hat : device.hats) { ULONG value = 0; - if (HidP_GetUsageValue(HidP_Input, UsagePageGenericDesktop, hat.caps.LinkCollection, UsageHat, - &value, device.preparsedData, report, reportLength) != HIDP_STATUS_SUCCESS) continue; + hat.sampledThisReport = hat.caps.ReportID == device.inputReport[0] + && HidP_GetUsageValue(HidP_Input, UsagePageGenericDesktop, hat.caps.LinkCollection, UsageHat, + &value, device.preparsedData, report, reportLength) == HIDP_STATUS_SUCCESS; + if (!hat.sampledThisReport) continue; const auto offset = static_cast(value) - hat.caps.LogicalMin; const auto range = static_cast(hat.caps.LogicalMax) - hat.caps.LogicalMin + 1; hat.directions = 0; @@ -831,14 +834,15 @@ namespace { else axis.samplePending = true; } auto bindingAxis = device.axes.begin(); - const bool controlsChanged = NativeButtons::GetState().Update(device.deviceIndex, [&](const auto& button) { + const bool controlsChanged = NativeButtons::GetState().Update(device.deviceIndex, [&](const auto& button) -> std::optional { if (button.buttonId >= HatButtonBase) { const int offset = button.buttonId - HatButtonBase; - return (device.hats[offset / 4].directions & (1 << (offset % 4))) != 0; + const auto& hat = device.hats[offset / 4]; + if (!hat.sampledThisReport) return std::nullopt; + return (hat.directions & (1 << (offset % 4))) != 0; } - // Reports for other IDs leave these button levels untouched. - return buttonsRead && device.buttonReportIds[button.buttonId] == device.inputReport[0] - ? device.pressedDataIndices[button.buttonId] != 0 : button.isPressed != 0; + if (!buttonsRead || device.buttonReportIds[button.buttonId] != device.inputReport[0]) return std::nullopt; + return device.pressedDataIndices[button.buttonId] != 0; }, [&](int, float) { return (bindingAxis++)->bindingValue; }); if (controlsChanged) CaptureButtons(timestampUsec, buttons); } diff --git a/tests/ControllerInputTests.cpp b/tests/ControllerInputTests.cpp index 9788dbe..ddf4582 100644 --- a/tests/ControllerInputTests.cpp +++ b/tests/ControllerInputTests.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include static void Check(bool condition, const char* message) { @@ -61,13 +62,34 @@ static SDL_JoystickID Attach(const char* name, SDL_JoystickType type, Uint16 axe int main() { const int newDeviceIndex = 77; - VpeInputButtonInfo button{0, 0, "Button 1"}; - NativeButtons::GetState().Connect(newDeviceIndex, "new-board", {button}); - Check(VpeInputListDeviceButtons(newDeviceIndex, &button, 1) == 1, "List button metadata before the first report"); - Check(VpeInputReadDeviceButtons(newDeviceIndex, &button, 1) == 0, "Do not report a fabricated release before the first report"); - NativeButtons::GetState().Update(newDeviceIndex, [](const auto&) { return true; }); - Check(VpeInputReadDeviceButtons(newDeviceIndex, &button, 1) == 1 && button.isPressed, - "Read the held button after the first report"); + std::array newButtons{{{0, 0, "Button 1"}, {1, 0, "Button 2"}}}; + NativeButtons::GetState().Connect(newDeviceIndex, "new-board", {newButtons[0], newButtons[1]}, {0}); + Check(VpeInputListDeviceButtons(newDeviceIndex, newButtons.data(), 2) == 2, "List button metadata before the first report"); + VpeInputStartControlCapture(1); + NativeButtons::GetState().Update(newDeviceIndex, [](const auto&) -> std::optional { return std::nullopt; }, + [](int, float) { return .25f; }); + Check(VpeInputReadDeviceButtons(newDeviceIndex, newButtons.data(), 2) == 0, "An axis-only report does not sample buttons"); + const auto sampleButton = [&](int id, bool pressed) { + NativeButtons::GetState().Update(newDeviceIndex, [&](const auto& button) -> std::optional { + if (button.buttonId != id) return std::nullopt; + return pressed; + }); + }; + sampleButton(0, true); + Check(VpeInputReadDeviceButtons(newDeviceIndex, newButtons.data(), 2) == 0, "Wait for all button reports"); + sampleButton(1, true); + Check(VpeInputReadDeviceButtons(newDeviceIndex, newButtons.data(), 2) == 2 + && newButtons[0].isPressed && newButtons[1].isPressed, "Combine button levels from separate reports"); + sampleButton(1, false); + Check(VpeInputReadDeviceButtons(newDeviceIndex, newButtons.data(), 2) == 2 + && newButtons[0].isPressed && !newButtons[1].isPressed, "Keep the last level of buttons outside the current report"); + VpeInputControlBinding capturedButton{}; + Check(VpeInputReadControlCapture(&capturedButton, 1) == 0, "A held button is not captured on its first sample"); + sampleButton(1, true); + sampleButton(1, false); + Check(VpeInputReadControlCapture(&capturedButton, 1) == 1 && capturedButton.controlId == 1 + && capturedButton.kind == VPE_CONTROL_BUTTON, "Capture a new press after that button has been sampled"); + VpeInputStartControlCapture(0); NativeButtons::GetState().Disconnect(newDeviceIndex); ControllerInput input; @@ -124,6 +146,37 @@ int main() { Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_JOYSTICK_AXIS_MIN), "Release the trigger again"); input.Poll(1030, true, callback, &observed); Check(!triggerPressed(), "The trigger returns to up"); + + VpeInputStartControlCapture(2); + Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_JOYSTICK_AXIS_MAX), "Pull one trigger for capture"); + input.Poll(1040, true, callback, &observed); + Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_JOYSTICK_AXIS_MIN), "Release the single trigger"); + input.Poll(1050, true, callback, &observed); + VpeInputControlBinding triggerCombo[8]{}; + Check(VpeInputReadControlCapture(triggerCombo, 8) == 0, "A trigger alone does not count as a combo"); + Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_JOYSTICK_AXIS_MAX) + && SDL_SetJoystickVirtualButton(pad, 0, true), "Pull a trigger and press a button for a combo"); + input.Poll(1060, true, callback, &observed); + Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_JOYSTICK_AXIS_MIN) + && SDL_SetJoystickVirtualButton(pad, 0, false), "Release the trigger combo"); + input.Poll(1070, true, callback, &observed); + Check(VpeInputReadControlCapture(triggerCombo, 8) == 2 + && triggerCombo[0].kind == VPE_CONTROL_BUTTON && triggerCombo[0].controlId == padButton.buttonId + && triggerCombo[1].kind == VPE_CONTROL_BUTTON && triggerCombo[1].controlId == leftTrigger, + "Capture the trigger and button without a duplicate axis"); + triggerCombo[0].action = triggerCombo[1].action = VPE_INPUT_START; + VpeInputSetControlBindings(triggerCombo, 2); + Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_JOYSTICK_AXIS_MAX), "Pull the mapped trigger"); + input.Poll(1080, true, callback, &observed); + Check(NativeButtons::GetState().PressedActions() == 0, "A mapped trigger combo requires both controls"); + Check(SDL_SetJoystickVirtualButton(pad, 0, true), "Complete the mapped trigger combo"); + input.Poll(1090, true, callback, &observed); + Check(NativeButtons::GetState().PressedActions() == (uint64_t{1} << VPE_INPUT_START), + "Use the captured trigger combo for a gameplay action"); + Check(SDL_SetJoystickVirtualAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_JOYSTICK_AXIS_MIN) + && SDL_SetJoystickVirtualButton(pad, 0, false), "Restore the virtual gamepad to rest"); + input.Poll(1095, true, callback, &observed); + VpeInputButtonBinding bindings[] = {{VPE_INPUT_LEFT_NUDGE, rawButton.buttonId}, {VPE_INPUT_LEFT_NUDGE, padButton.buttonId}}; std::snprintf(bindings[0].deviceId, sizeof(bindings[0].deviceId), "%s", pinscape.stableId); std::snprintf(bindings[1].deviceId, sizeof(bindings[1].deviceId), "%s", gamepad.stableId);