Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/ControllerInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8, 4> HatDirections = {SDL_HAT_UP, SDL_HAT_RIGHT, SDL_HAT_DOWN, SDL_HAT_LEFT};
constexpr std::array<const char*, SDL_GAMEPAD_AXIS_COUNT> GamepadAxisNames = {
"Left Stick X", "Left Stick Y", "Right Stick X", "Right Stick Y", "Left Trigger", "Right Trigger"
Expand Down Expand Up @@ -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_GamepadAxis>(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);
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}
for (int i = 0; i < rawCount; ++i) {
if (mapped[i]) continue;
Expand Down Expand Up @@ -281,7 +290,10 @@ bool ControllerInput::Reconcile(int64_t timestamp) {
if (device->axes.empty() && buttons.empty()) continue;
std::vector<int> 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<int> buttonBackedAxes = standardized
? std::vector<int>{SDL_GAMEPAD_AXIS_LEFT_TRIGGER, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER} : std::vector<int>{};
NativeButtons::GetState().Connect(device->info.deviceIndex, stableId, std::move(buttons), axisIds, buttonBackedAxes);
device->info.axisCount = static_cast<int>(device->axes.size());
m_devices.push_back(std::move(device));
changed = true;
Expand Down Expand Up @@ -316,6 +328,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_GamepadAxis>(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<size_t>(button.buttonId - HatButtonBase);
const auto hat = SDL_GetJoystickHat(device->joystick, static_cast<int>(offset / HatDirections.size()));
Expand Down
21 changes: 16 additions & 5 deletions src/NativeButtons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ namespace NativeButtons {
}

void State::Connect(int deviceIndex, const std::string& stableId, std::vector<VpeInputButtonInfo> buttons,
const std::vector<int>& axisIds) {
const std::vector<int>& axisIds, const std::vector<int>& 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<bool>(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<std::mutex> lock(m_mutex);
m_devices.push_back(std::move(device));
}
Expand Down Expand Up @@ -67,7 +72,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;
});
Expand All @@ -81,6 +86,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);
Expand Down Expand Up @@ -150,10 +156,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<std::mutex> lock(m_mutex);
for (const auto& device : m_devices) {
if (device.index != deviceIndex) continue;
if (requireSample && device.unsampledButtons != 0) return 0;
const int count = static_cast<int>(device.buttons.size());
if (buttons != nullptr && maxButtons > 0) std::copy_n(device.buttons.begin(), std::min(count, maxButtons), buttons);
return count;
Expand Down Expand Up @@ -236,6 +243,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);
}
Expand Down
28 changes: 18 additions & 10 deletions src/NativeButtons.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
#include <cmath>
#include <limits>
#include <mutex>
#include <optional>
#include <string>
#include <vector>

namespace NativeButtons {
class State {
public:
void Connect(int deviceIndex, const std::string& stableId, std::vector<VpeInputButtonInfo> buttons,
const std::vector<int>& axisIds = {});
const std::vector<int>& axisIds = {}, const std::vector<int>& buttonBackedAxes = {});
void Disconnect(int deviceIndex);
void Configure(const VpeInputButtonBinding* bindings, int count);
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 <typename Sample>
Expand All @@ -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<bool> 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)
Expand All @@ -65,6 +71,7 @@ namespace NativeButtons {

struct Axis {
int id;
bool buttonBacked;
float value = std::numeric_limits<float>::quiet_NaN();
};

Expand All @@ -73,7 +80,8 @@ namespace NativeButtons {
std::string id;
std::vector<VpeInputButtonInfo> buttons;
std::vector<Axis> axes;
bool buttonsSampled = false;
std::vector<bool> buttonSampled;
size_t unsampledButtons;
};

struct Binding {
Expand Down
2 changes: 2 additions & 0 deletions src/NativeInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 15 additions & 9 deletions src/NativeInput_Windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace {
struct HatDescriptor {
HIDP_VALUE_CAPS caps;
uint8_t directions = 0;
bool sampledThisReport = false;
};

struct HidDevice {
Expand Down Expand Up @@ -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<int64_t>(value) - hat.caps.LogicalMin;
const auto range = static_cast<int64_t>(hat.caps.LogicalMax) - hat.caps.LogicalMin + 1;
hat.directions = 0;
Expand Down Expand Up @@ -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<bool> {
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);
}
Expand Down Expand Up @@ -1118,8 +1122,10 @@ VPE_API int VpeInputStartPolling(VpeInputEventCallback callback, void* userData,
{
std::lock_guard<std::mutex> 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<VpeInputAction>(action)).latch = {};
}
}
if (g_stopEvent != NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/OpenPinballDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading
Loading