diff --git a/VisualPinball.Engine/VisualPinball.Engine.csproj b/VisualPinball.Engine/VisualPinball.Engine.csproj index 5ceffaa33..47a790853 100644 --- a/VisualPinball.Engine/VisualPinball.Engine.csproj +++ b/VisualPinball.Engine/VisualPinball.Engine.csproj @@ -15,7 +15,7 @@ https://visualpinball.org icon.png LICENSE - 0.0.7 + 0.0.8 diff --git a/VisualPinball.Unity/VisualPinball.Unity/Simulation/NativeInputApi.cs b/VisualPinball.Unity/VisualPinball.Unity/Simulation/NativeInputApi.cs index 102d22975..d97ba3095 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Simulation/NativeInputApi.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Simulation/NativeInputApi.cs @@ -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; + } + /// /// Native device metadata returned by enumeration. /// @@ -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); diff --git a/VisualPinball.Unity/VisualPinball.Unity/Simulation/NativeInputManager.cs b/VisualPinball.Unity/VisualPinball.Unity/Simulation/NativeInputManager.cs index 2f1efaddb..3582adca5 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Simulation/NativeInputManager.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Simulation/NativeInputManager.cs @@ -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 _deviceIdsByIndex = new(); + private volatile Dictionary _deviceIdsByIndex = new(); + private int _deviceRevision; + public int DeviceRevision => Volatile.Read(ref _deviceRevision); // Input configuration private readonly List _bindings = new(); @@ -61,6 +63,7 @@ public class NativeInputManager : IDisposable private static volatile List _configuredBindings; private static volatile NativeInputApi.InputControlBinding[] _configuredControlBindings = Array.Empty(); 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 @@ -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; @@ -220,8 +229,8 @@ public void ClearBindings() } /// - /// Custom bindings supplied by the host application. When non-null and non-empty, these are sent - /// to the native layer on the next instead of the built-in defaults. + /// Keyboard bindings supplied by the host and sent to the native layer on the next . + /// 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. /// @@ -252,6 +261,7 @@ public static void ConfigureControlBindings(IReadOnlyList _hasNativeButtons; + public bool SupportsDeviceButtonStates => _hasNativeButtonStates; public bool SupportsControlBindings => _hasNativeControlBindings; private void SendControlBindings() @@ -435,6 +445,16 @@ public IReadOnlyList ListDeviceAxes(int deviceIndex) return buttons; } + /// Copies the latest button state and returns the total number of buttons on the device. + 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; + } + /// /// Starts native input polling and sends the current action bindings to the /// native layer. @@ -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(); 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; @@ -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); @@ -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); @@ -682,6 +704,7 @@ public void Dispose() _initialized = false; _hasNativeGameplayGate = false; _hasNativeButtons = false; + _hasNativeButtonStates = false; _hasNativeControlBindings = false; }