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
2 changes: 1 addition & 1 deletion .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
if: matrix.platform == 'linux'
run: |
sudo apt-get update
sudo apt-get install -y libx11-dev
sudo apt-get install -y libx11-dev libudev-dev

- name: Build native library
run: |
Expand Down
62 changes: 55 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.16)
project(VpeNativeInput VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
Expand All @@ -18,21 +18,52 @@ set(SOURCES
# Platform-specific sources
if(WIN32)
list(APPEND SOURCES src/NativeInput_Windows.cpp)
set(PLATFORM_LIBS winmm hid setupapi cfgmgr32)
set(PLATFORM_LIBS winmm hid setupapi cfgmgr32 user32)
elseif(APPLE)
list(APPEND SOURCES src/NativeInput_Mac.mm)
list(APPEND SOURCES src/NativeInput_Mac.mm src/OpenPinballInput.cpp src/OpenPinballMac.cpp)
find_library(APPKIT_FRAMEWORK AppKit REQUIRED)
find_library(APPLICATION_SERVICES_FRAMEWORK ApplicationServices REQUIRED)
set(PLATFORM_LIBS ${APPKIT_FRAMEWORK} ${APPLICATION_SERVICES_FRAMEWORK})
elseif(UNIX)
list(APPEND SOURCES src/NativeInput_Linux.cpp)
find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation REQUIRED)
find_library(IOKIT_FRAMEWORK IOKit REQUIRED)
set(PLATFORM_LIBS ${APPKIT_FRAMEWORK} ${APPLICATION_SERVICES_FRAMEWORK} ${CORE_FOUNDATION_FRAMEWORK} ${IOKIT_FRAMEWORK})
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
list(APPEND SOURCES src/NativeInput_Linux.cpp src/OpenPinballInput.cpp src/OpenPinballLinux.cpp)
find_package(Threads REQUIRED)
find_package(X11 REQUIRED)
set(PLATFORM_LIBS Threads::Threads ${X11_LIBRARIES})
else()
list(APPEND SOURCES src/NativeInput_Stub.cpp)
set(PLATFORM_LIBS)
endif()

if(WIN32 OR APPLE OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
include(FetchContent)
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
set(SDL_STATIC ON CACHE BOOL "" FORCE)
set(SDL_INSTALL OFF CACHE BOOL "" FORCE)
set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
set(SDL_HIDAPI_LIBUSB OFF CACHE BOOL "" FORCE)
set(SDL_UNIX_CONSOLE_BUILD ON CACHE BOOL "" FORCE)
foreach(subsystem AUDIO VIDEO HAPTIC POWER SENSOR DIALOG TRAY)
set(SDL_${subsystem} OFF CACHE BOOL "" FORCE)
endforeach()
if(APPLE)
# SDL's macOS HIDAPI driver runs its own report thread. The IOKit and
# GameController joystick drivers can be sampled by our polling thread.
set(SDL_HIDAPI_JOYSTICK OFF CACHE BOOL "" FORCE)
endif()
if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
endif()
FetchContent_Declare(SDL3
URL https://github.com/libsdl-org/SDL/releases/download/release-3.4.20/SDL3-3.4.20.tar.gz
URL_HASH SHA256=f3bb06f55bed9557227a700af2ea62e49497ebdf58037732a731a5fb3ef8581f)
FetchContent_MakeAvailable(SDL3)
set_target_properties(SDL3-static PROPERTIES POSITION_INDEPENDENT_CODE ON C_VISIBILITY_PRESET hidden)
target_compile_definitions(SDL3-static PRIVATE SDL_DECLSPEC=)
list(APPEND SOURCES src/ControllerInput.cpp src/NativeButtons.cpp)
list(APPEND PLATFORM_LIBS SDL3::SDL3-static)
endif()

# Create shared library
add_library(VpeNativeInput SHARED ${SOURCES})
Expand All @@ -44,7 +75,7 @@ endif()
# Link platform libraries
target_link_libraries(VpeNativeInput PRIVATE ${PLATFORM_LIBS})

if(UNIX AND NOT APPLE)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_include_directories(VpeNativeInput PRIVATE ${X11_INCLUDE_DIR})
endif()

Expand Down Expand Up @@ -92,6 +123,9 @@ message(STATUS "NativeInput output directory: ${OUTPUT_DIR}")
# Set output directory
set_target_properties(VpeNativeInput PROPERTIES
OUTPUT_NAME "VisualPinball.NativeInput"
CXX_VISIBILITY_PRESET hidden
OBJCXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIR}
RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR}
ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_DIR}
Expand All @@ -105,3 +139,17 @@ install(TARGETS VpeNativeInput
LIBRARY DESTINATION ${OUTPUT_DIR}
RUNTIME DESTINATION ${OUTPUT_DIR}
)

option(NATIVEINPUT_BUILD_TESTS "Build native input report tests" OFF)
if(NATIVEINPUT_BUILD_TESTS)
enable_testing()
add_executable(OpenPinballDeviceTests tests/OpenPinballDeviceTests.cpp src/OpenPinballInput.cpp src/NativeButtons.cpp)
target_include_directories(OpenPinballDeviceTests PRIVATE src)
add_test(NAME OpenPinballDeviceTests COMMAND OpenPinballDeviceTests)
if(TARGET SDL3::SDL3-static)
add_executable(ControllerInputTests tests/ControllerInputTests.cpp src/ControllerInput.cpp src/NativeButtons.cpp)
target_include_directories(ControllerInputTests PRIVATE src)
target_link_libraries(ControllerInputTests PRIVATE SDL3::SDL3-static)
add_test(NAME ControllerInputTests COMMAND ControllerInputTests)
endif()
endif()
45 changes: 34 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ cmake .. -DCMAKE_BUILD_TYPE=Release
make
```

Requires: `libx11-dev`
Requires `libx11-dev` and `libudev-dev`. The app user needs read access to the controller's input devices.

### macOS

Expand All @@ -77,7 +77,7 @@ cmake .. -DCMAKE_BUILD_TYPE=Release
make
```

Requires: AppKit and ApplicationServices frameworks (provided by macOS)
Requires CMake 3.24 or newer and the macOS SDK.

### CI / NuGet

Expand Down Expand Up @@ -131,14 +131,37 @@ NativeInputApi.VpeInputStopPolling();
NativeInputApi.VpeInputShutdown();
```

## Open Pinball Device

NativeInput recognizes Open Pinball Device 1.x HID collections by their report signature on Windows, Linux, and macOS. The native input thread reads the reports and exposes named X/Y acceleration and velocity axes and the reported plunger fields. Preassigned OPD buttons that match existing VPE actions work alongside keyboard bindings, including physical tilt and digital nudges. An action stays pressed while any connected source holds it.

Pinscape Pico can expose both OPD and a joystick. VPE's cabinet setup prefers OPD and lets a player explicitly choose either collection. On Linux and macOS, OPD mapping identities include the physical USB port so identical boards can be mapped independently. Pico acceleration and velocity ranges depend on its firmware configuration.

On Linux, run VPE with an X11 display. The local user needs read access to the Pico's hidraw node for input and read/write access to its USB device node for the signature request. On a systemd-based desktop, these udev rules grant the active local user access to a Pico using the default IDs. Save them as `/etc/udev/rules.d/70-pinscape-pico.rules`, reload the rules, and reconnect the board. Use the configured VID and PID if the firmware overrides them.

```udev
SUBSYSTEM=="hidraw", ATTRS{idVendor}=="1209", ATTRS{idProduct}=="eaeb", TAG+="uaccess"
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="1209", ATTR{idProduct}=="eaeb", TAG+="uaccess"
```

Build the report replay tests with `-DNATIVEINPUT_BUILD_TESTS=ON`, then run `ctest --test-dir build -C Release --output-on-failure` from this repository.

## Platform Status

| Platform | Status | API Used |
|----------|--------|----------|
| Windows | ✅ Implemented | GetAsyncKeyState, QueryPerformanceCounter |
| Linux | ✅ Implemented | X11 keyboard state polling |
| macOS | ✅ Implemented | CoreGraphics keyboard state polling |
| Windows | Keyboard, joystick and gamepad axes, Open Pinball Device | GetAsyncKeyState, Windows HID, SDL |
| Linux | Keyboard, joystick and gamepad axes, Open Pinball Device | X11, SDL, hidraw, usbfs |
| macOS | Keyboard, joystick and gamepad axes, Open Pinball Device | CoreGraphics, SDL, IOKit |

SDL is linked into the plugin and only its joystick and gamepad subsystem is started. The native polling thread samples all controller axes. Recognized gamepads expose named stick and trigger axes, and generic joysticks expose raw axes for manual nudge mapping. Windows keeps the existing HID axes and their saved IDs. If a Windows gamepad also exposes HID joystick axes, its standardized axes appear under the separate `(Gamepad)` entry.

Some Nintendo controllers change their report format when SDL opens them. On Windows, use the standardized entry if a saved raw HID mapping for one of those controllers stops receiving samples.

Controller mappings use serial numbers and physical paths where available. When the platform exposes identical devices without a physical identity, the mappings use slots for that model. Recheck those slots after changing the connection order of identical controllers.

The Player's Nudge settings can bind joystick, gamepad, and generic OPD controls to left, right, and forward nudge, Tilt, and Slam Tilt. POV directions are exposed as buttons. Analog axes can activate an action above or below a threshold. Each action accepts alternatives, including combos whose controls must be held together. The native polling thread evaluates bindings and records presses for capture. Bindings follow stable device IDs across polling sessions and supplement keyboard and predefined OPD buttons. Tilt uses the table's physical Tilt Bob routing.

## Performance

- **Latency**: <0.1ms from physical input to event callback
Expand All @@ -148,13 +171,13 @@ NativeInputApi.VpeInputShutdown();

## Future Enhancements

1. **Gamepad support**: XInput (Windows), SDL/libinput (Linux), GCController (macOS)
2. **Analog inputs**: Plunger, steering, analog triggers
3. **Force feedback**: Haptics for nudge, tilt, button feedback
4. **Hot-plugging**: Detect device connect/disconnect
5. **Configuration**: JSON-based key mapping files
6. **Profiles**: Per-table input profiles
1. **Controller buttons**: Configurable bindings for other gameplay actions
2. **Force feedback**: Haptics for nudge, tilt, button feedback
3. **Configuration**: JSON-based key mapping files
4. **Profiles**: Per-table input profiles

## License

GPLv3+ (same as VPE)

SDL is distributed under the [zlib license](native/nuget/SDL-LICENSE.txt).
17 changes: 17 additions & 0 deletions native/nuget/SDL-LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
7 changes: 4 additions & 3 deletions native/nuget/VisualPinball.NativeInput.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
<license type="expression">GPL-3.0-or-later</license>
<icon>icon.png</icon>
<readme>README.md</readme>
<description>Cross-platform native keyboard polling library for Visual Pinball Engine.</description>
<summary>Native keyboard polling for Visual Pinball Engine.</summary>
<tags>visualpinball unity native input keyboard</tags>
<description>Cross-platform native keyboard, joystick, gamepad and cabinet input polling for Visual Pinball Engine.</description>
<summary>Native input polling for Visual Pinball Engine.</summary>
<tags>visualpinball unity native input keyboard joystick gamepad</tags>
</metadata>
<files>
<file src="_._" target="lib\netstandard2.1" />
<file src="icon.png" target="icon.png" />
<file src="SDL-LICENSE.txt" target="native\nuget\SDL-LICENSE.txt" />
<file src="..\..\README.md" target="README.md" />
<file src="..\..\artifacts\win-x64\VisualPinball.NativeInput.dll" target="runtimes\win-x64\native" />
<file src="..\..\artifacts\win-x86\VisualPinball.NativeInput.dll" target="runtimes\win-x86\native" />
Expand Down
Loading
Loading