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 VisualPinball.Engine/VisualPinball.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageProjectUrl>https://visualpinball.org</PackageProjectUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<VisualPinballNativeInputVersion>0.0.7</VisualPinballNativeInputVersion>
<VisualPinballNativeInputVersion>0.0.8</VisualPinballNativeInputVersion>
</PropertyGroup>
<!-- Give an initial value based on the operating system where it's currently running on -->
<PropertyGroup Condition="'$(RuntimeIdentifier)' == ''">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ public struct InputButtonInfo
public string Name => DecodeUtf8(_name);
}

// Same native layout as InputButtonInfo, without marshaling the name for each poll.
[StructLayout(LayoutKind.Sequential, Pack = 4, Size = 2 * sizeof(int) + ButtonNameSize)]
public struct InputButtonState
{
public int ButtonId;
public int IsPressed;
}

/// <summary>
/// Native device metadata returned by enumeration.
/// </summary>
Expand Down Expand Up @@ -415,6 +423,9 @@ private static string DecodeUtf8(byte[] bytes)
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int VpeInputListDeviceButtons(int deviceIndex, [Out] InputButtonInfo[] buttons, int maxButtons);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern int VpeInputReadDeviceButtons(int deviceIndex, [Out] InputButtonState[] buttons, int maxButtons);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void VpeInputSetButtonBindings([In] InputButtonBinding[] bindings, int count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public class NativeInputManager : IDisposable
// on the main thread so the input polling thread can read it lock-free. Rebuilt after
// StartPolling, because that's when the native side switches to its polling enumeration,
// which is the only index space consistent with axis events.
private volatile Dictionary<int, string> _deviceIdsByIndex = new();
private volatile Dictionary<int, string> _deviceIdsByIndex = new();
private int _deviceRevision;
public int DeviceRevision => Volatile.Read(ref _deviceRevision);

// Input configuration
private readonly List<NativeInputApi.InputBinding> _bindings = new();
Expand All @@ -61,6 +63,7 @@ public class NativeInputManager : IDisposable
private static volatile List<NativeInputApi.InputBinding> _configuredBindings;
private static volatile NativeInputApi.InputControlBinding[] _configuredControlBindings = Array.Empty<NativeInputApi.InputControlBinding>();
private bool _hasNativeButtons;
private bool _hasNativeButtonStates;
private bool _hasNativeControlBindings;

// Set from the main thread (Application.isFocused). When the app window isn't focused, native input
Expand Down Expand Up @@ -176,6 +179,12 @@ public bool Initialize()
} catch (EntryPointNotFoundException) {
_hasNativeButtons = false;
}
try {
NativeInputApi.VpeInputReadDeviceButtons(-1, null, 0);
_hasNativeButtonStates = true;
} catch (EntryPointNotFoundException) {
_hasNativeButtonStates = false;
}
try {
NativeInputApi.VpeInputReadControlCapture(null, 0);
_hasNativeControlBindings = true;
Expand Down Expand Up @@ -220,8 +229,8 @@ public void ClearBindings()
}

/// <summary>
/// Custom bindings supplied by the host application. When non-null and non-empty, these are sent
/// to the native layer on the next <see cref="StartPolling"/> instead of the built-in defaults.
/// Keyboard bindings supplied by the host and sent to the native layer on the next <see cref="StartPolling"/>.
/// Null uses the built-in defaults. An empty list disables keyboard bindings.
/// Set this from your config before launching a table (and again whenever the user edits bindings).
/// Static so it outlives the per-table singleton lifecycle.
/// </summary>
Expand Down Expand Up @@ -252,6 +261,7 @@ public static void ConfigureControlBindings(IReadOnlyList<NativeInputApi.InputCo
}

public bool SupportsControllerButtons => _hasNativeButtons;
public bool SupportsDeviceButtonStates => _hasNativeButtonStates;
public bool SupportsControlBindings => _hasNativeControlBindings;

private void SendControlBindings()
Expand Down Expand Up @@ -435,6 +445,16 @@ public IReadOnlyList<NativeInputAxisInfo> ListDeviceAxes(int deviceIndex)
return buttons;
}

/// <summary>Copies the latest button state and returns the total number of buttons on the device.</summary>
public int ReadDeviceButtons(string deviceId, NativeInputApi.InputButtonState[] states)
{
if (!_polling || !_hasNativeButtonStates) return 0;
foreach (var (index, id) in _deviceIdsByIndex) {
if (id == deviceId) return NativeInputApi.VpeInputReadDeviceButtons(index, states, states.Length);
}
return 0;
}

/// <summary>
/// Starts native input polling and sends the current action bindings to the
/// native layer.
Expand Down Expand Up @@ -463,10 +483,10 @@ public bool StartPolling(int pollIntervalUs = 500)

// Send bindings to native layer: host-supplied custom bindings if present, else the defaults
var configured = _configuredBindings;
var toSend = configured != null && configured.Count > 0 ? configured.ToArray() : _bindings.ToArray();
var toSend = configured?.ToArray() ?? _bindings.ToArray();
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
NativeInputApi.VpeInputSetBindings(toSend, toSend.Length);
SendControlBindings();
Logger.Debug("{Prefix:l} [NativeInputManager] Sent {Count} bindings ({Kind:l})", LogPrefix, toSend.Length, configured != null && configured.Count > 0 ? "custom" : "default");
Logger.Debug("{Prefix:l} [NativeInputManager] Sent {Count} bindings ({Kind:l})", LogPrefix, toSend.Length, configured == null ? "default" : "custom");

// Create callback delegate (keep reference to prevent GC)
_callbackDelegate = OnInputEvent;
Expand Down Expand Up @@ -501,6 +521,7 @@ public void StopPolling()
NativeInputApi.VpeInputStopPolling();
_simulationThread?.ResetInputNotifications(false);
_polling = false;
Interlocked.Increment(ref _deviceRevision);
_pollIntervalUs = 0;
Volatile.Write(ref _actualEventRateHz, 0f);

Expand Down Expand Up @@ -635,6 +656,7 @@ private void OnNativeDevicesChanged()
{
try {
ListDevices();
Interlocked.Increment(ref _deviceRevision);
DevicesChanged?.Invoke();
} catch (Exception e) {
Logger.Error(e, "{Prefix:l} [NativeInputManager] Failed to refresh devices after hotplug", LogPrefix);
Expand Down Expand Up @@ -682,6 +704,7 @@ public void Dispose()
_initialized = false;
_hasNativeGameplayGate = false;
_hasNativeButtons = false;
_hasNativeButtonStates = false;
_hasNativeControlBindings = false;
}

Expand Down
Loading