|
| 1 | +namespace Spice86.Native; |
| 2 | + |
| 3 | +using Silk.NET.SDL; |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.IO; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// SDL2-based mouse capture backend for non-Windows platforms. |
| 10 | +/// Uses <c>SDL_SetRelativeMouseMode</c> to hide the cursor and report relative mouse deltas. |
| 11 | +/// Precompiled SDL2 native libraries are bundled via Silk.NET.SDL (Ultz.Native.SDL), |
| 12 | +/// covering macOS (universal) and Linux (x64/arm64/arm). |
| 13 | +/// </summary> |
| 14 | +internal sealed class SdlMouseCapture : IMouseCaptureBackend { |
| 15 | + private Sdl? _sdl; |
| 16 | + private bool _initialized; |
| 17 | + private bool _isCaptured; |
| 18 | + private bool _disposed; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Gets a value indicating whether the SDL subsystem was successfully initialized. |
| 22 | + /// </summary> |
| 23 | + public bool IsInitialized => _initialized; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Gets a value indicating whether relative mouse capture is currently active. |
| 27 | + /// </summary> |
| 28 | + public bool IsCaptured => _isCaptured; |
| 29 | + |
| 30 | + /// <inheritdoc/> |
| 31 | + /// <remarks><see langword="true"/>: cursor is hidden and only deltas are reported.</remarks> |
| 32 | + public bool UsesRelativeMouseMode => true; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initializes the SDL video subsystem. |
| 36 | + /// If the SDL2 native library is unavailable, this returns <see langword="false"/> without throwing. |
| 37 | + /// </summary> |
| 38 | + /// <param name="nativeWindowHandle">The platform-native window handle (HWND on Windows, X11 Window ID on Linux, NSWindow* on macOS).</param> |
| 39 | + /// <returns><see langword="true"/> if initialization succeeded; otherwise <see langword="false"/>.</returns> |
| 40 | + public bool TryInitialize(nint nativeWindowHandle) { |
| 41 | + if (_initialized) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + if (nativeWindowHandle == nint.Zero) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + try { |
| 50 | + _sdl = Sdl.GetApi(); |
| 51 | + } catch (FileNotFoundException) { |
| 52 | + return false; |
| 53 | + } catch (DllNotFoundException) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + // Suppress SDL's default signal handlers so they don't interfere with the host app. |
| 58 | + _sdl.SetHint("SDL_NO_SIGNAL_HANDLERS", "1"); |
| 59 | + |
| 60 | + // Initialize only the video subsystem so SDL can manage mouse state. |
| 61 | + int result = _sdl.Init(Sdl.InitVideo); |
| 62 | + if (result != 0) { |
| 63 | + _sdl.Dispose(); |
| 64 | + _sdl = null; |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + _initialized = true; |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Enables SDL relative mouse mode. |
| 74 | + /// The cursor is hidden and mouse motion is reported as relative deltas only. |
| 75 | + /// </summary> |
| 76 | + /// <returns><see langword="true"/> if relative mode was enabled successfully.</returns> |
| 77 | + public bool EnableCapture() { |
| 78 | + if (!_initialized || _isCaptured || _sdl is null) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + int result = _sdl.SetRelativeMouseMode(SdlBool.True); |
| 83 | + if (result != 0) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + _isCaptured = true; |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Disables SDL relative mouse mode, restoring normal cursor behaviour. |
| 93 | + /// </summary> |
| 94 | + /// <returns><see langword="true"/> if relative mode was disabled successfully.</returns> |
| 95 | + public bool DisableCapture() { |
| 96 | + if (!_initialized || !_isCaptured || _sdl is null) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + int result = _sdl.SetRelativeMouseMode(SdlBool.False); |
| 101 | + if (result != 0) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + _isCaptured = false; |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Retrieves the accumulated relative mouse motion since the last call. |
| 111 | + /// Analogous to SDL_GetRelativeMouseState used in dosbox-staging. |
| 112 | + /// </summary> |
| 113 | + /// <param name="dx">Horizontal delta in screen pixels (positive = right).</param> |
| 114 | + /// <param name="dy">Vertical delta in screen pixels (positive = down).</param> |
| 115 | + public void GetRelativeMouseDelta(out int dx, out int dy) { |
| 116 | + if (!_initialized || _sdl is null) { |
| 117 | + dx = 0; |
| 118 | + dy = 0; |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + // SDL_PumpEvents processes pending OS events so GetRelativeMouseState is up to date. |
| 123 | + _sdl.PumpEvents(); |
| 124 | + |
| 125 | + int x = 0; |
| 126 | + int y = 0; |
| 127 | + unsafe { |
| 128 | + _sdl.GetRelativeMouseState(&x, &y); |
| 129 | + } |
| 130 | + |
| 131 | + dx = x; |
| 132 | + dy = y; |
| 133 | + } |
| 134 | + |
| 135 | + /// <inheritdoc/> |
| 136 | + public void Dispose() { |
| 137 | + if (_disposed) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + _disposed = true; |
| 142 | + |
| 143 | + if (_sdl is null) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + if (_isCaptured) { |
| 148 | + _sdl.SetRelativeMouseMode(SdlBool.False); |
| 149 | + _isCaptured = false; |
| 150 | + } |
| 151 | + |
| 152 | + if (_initialized) { |
| 153 | + _sdl.QuitSubSystem(Sdl.InitVideo); |
| 154 | + _initialized = false; |
| 155 | + } |
| 156 | + |
| 157 | + _sdl.Dispose(); |
| 158 | + _sdl = null; |
| 159 | + } |
| 160 | +} |
0 commit comments