Skip to content

Commit 564d0b1

Browse files
committed
continued
1 parent 6c24e1a commit 564d0b1

File tree

9 files changed

+127
-85
lines changed

9 files changed

+127
-85
lines changed

sources/Input/Input/Implementations/SDL3/Devices/Joysticks/SdlGamepad.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Silk.NET.Input.SDL3.Devices.Joysticks;
1111
/// </summary>
1212
internal sealed unsafe class SdlGamepad : SdlDevice, IGamepad, ISdlDevice<SdlGamepad>, ISdlJoystick, IJoystick
1313
{
14-
private readonly GamepadHandle _gamepadHandle;
14+
private GamepadHandle _gamepadHandle;
1515

1616
public SdlJoystick Joystick { get; }
1717

@@ -21,13 +21,6 @@ private SdlGamepad(SdlJoystick joystick, nint uniqueId) : base(joystick.Backend,
2121
{
2222
Joystick = joystick;
2323

24-
var joystickHandle = joystick.JoystickHandle;
25-
var gamepadHandle = *(GamepadHandle*)&joystickHandle; //NativeBackend.OpenGamepad(sdlDeviceId);
26-
_gamepadHandle = gamepadHandle;
27-
Remap(gamepadHandle);
28-
29-
GamepadState = new GamepadState(joystick.RawButtonState, joystick.RawAxisState);
30-
Joystick.AddDeviceMapping(this);
3124
}
3225

3326
private void Remap(GamepadHandle gamepadHandle)
@@ -114,18 +107,30 @@ private void Remap(GamepadHandle gamepadHandle)
114107

115108
public override string Name => Joystick.Name;
116109

110+
protected internal override void Initialize()
111+
{
112+
var joystickHandle = Joystick.JoystickHandle;
113+
var gamepadHandle = *(GamepadHandle*)&joystickHandle; //NativeBackend.OpenGamepad(sdlDeviceId);
114+
_gamepadHandle = gamepadHandle;
115+
Remap(gamepadHandle);
116+
117+
_state = new GamepadState(Joystick.RawButtonState, Joystick.RawAxisState);
118+
Joystick.AddDeviceMapping(this);
119+
}
120+
117121
protected override void Release()
118122
{
119123
Joystick.RemoveDeviceMapping(this);
120124

121125
// todo: does this close the joystick as well?
122126
NativeBackend.CloseGamepad(_gamepadHandle);
123127
}
128+
private GamepadState _state;
124129

125130
#region IGamepad
126131

127132
GamepadState IGamepad.State => GamepadState;
128-
private GamepadState GamepadState { get; }
133+
private GamepadState GamepadState => _state;
129134

130135
public IReadOnlyList<IMotor> VibrationMotors =>
131136
_rumbler ??= SdlRumble.Create<GamepadHandle>(_gamepadHandle.Handle, NativeBackend, 2);

sources/Input/Input/Implementations/SDL3/Devices/Joysticks/SdlJoystick.cs

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace Silk.NET.Input.SDL3.Devices.Joysticks;
99

1010
internal sealed unsafe partial class SdlJoystick : SdlDevice, IJoystick, ISdlDevice<SdlJoystick>, IOrderedDevice
1111
{
12-
public JoystickState State { get; }
13-
private readonly JoystickType _joystickType;
14-
internal JoystickHandle JoystickHandle { get; }
12+
public JoystickState State { get; private set; }
13+
private JoystickType _joystickType;
14+
internal JoystickHandle JoystickHandle { get; private set; }
1515

1616
public static SdlJoystick CreateDevice(ulong sdlDeviceId, SdlInputBackend backend, SilkEventContext silkEvents)
1717
{
@@ -44,81 +44,31 @@ public static SdlJoystick CreateDevice(ulong sdlDeviceId, SdlInputBackend backen
4444
uniqueId = SdlInputBackend.FallbackUniqueId<SdlJoystick>(sdlDeviceId, uniqueId);
4545
return CreatePls(backend, uniqueId, sdlDeviceId, silkEvents);
4646

47-
static SdlJoystick CreatePls(SdlInputBackend sdlInputBackend, nint uniqueId, ulong sdlDeviceId, SilkEventContext context)
47+
static SdlJoystick CreatePls(SdlInputBackend sdlInputBackend, nint uniqueId, ulong sdlDeviceId,
48+
SilkEventContext context)
4849
{
49-
return new SdlJoystick(sdlDeviceId, uniqueId, sdlInputBackend) {
50-
ButtonEvents = context.ButtonChangedSdlEvents,
51-
AxisEvents = context.JoystickAxisMoveSdlEvents,
52-
HatEvents = context.JoystickHatMoveSdlEvents
53-
};
50+
return new SdlJoystick(sdlDeviceId, uniqueId, sdlInputBackend) {
51+
ButtonEvents = context.ButtonChangedSdlEvents,
52+
AxisEvents = context.JoystickAxisMoveSdlEvents,
53+
HatEvents = context.JoystickHatMoveSdlEvents
54+
};
5455
}
5556
}
5657

5758

5859
public override string Name => NativeBackend.GetJoystickNameForID((uint)SdlDeviceId).ReadToString();
60+
5961
public override ulong SdlDeviceId
6062
{
6163
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6264
get => SdlDeviceId;
6365
}
6466

6567

66-
private SdlJoystick(ulong sdlDeviceId, nint uniqueId, SdlInputBackend backend) : base(backend, uniqueId, sdlDeviceId)
68+
private SdlJoystick(ulong sdlDeviceId, nint uniqueId, SdlInputBackend backend) : base(backend, uniqueId,
69+
sdlDeviceId)
6770
{
68-
var joystickHandle = NativeBackend.OpenJoystick((uint)sdlDeviceId);
6971
_sdlDeviceId = sdlDeviceId;
70-
71-
if (joystickHandle.Handle == null)
72-
{
73-
var error = NativeBackend.GetError();
74-
string? errorStr = null;
75-
if (error.Native != null)
76-
{
77-
errorStr = error.ReadToString();
78-
NativeBackend.Free(error.Native);
79-
}
80-
81-
throw new Exception($"Failed to open joystick: {errorStr ?? "Unknown error."}");
82-
}
83-
84-
JoystickHandle = joystickHandle;
85-
_joystickType = NativeBackend.GetJoystickType(joystickHandle);
86-
87-
88-
// init current joystick state
89-
var buttonCount = NativeBackend.GetNumJoystickButtons(joystickHandle);
90-
var nowTimestamp = System.Diagnostics.Stopwatch.GetTimestamp();
91-
var nowSdlTimestamp = NativeBackend.GetTicks();
92-
for (byte i = 0; i < buttonCount; i++)
93-
{
94-
var joystickInput = NativeBackend.GetJoystickButtonRaw(JoystickHandle, i);
95-
AddButtonEvent(i, joystickInput, nowSdlTimestamp, nowTimestamp);
96-
}
97-
98-
var axisCount = NativeBackend.GetNumJoystickAxes(joystickHandle);
99-
for (var i = 0; i < axisCount; i++)
100-
{
101-
var joystickInput = NativeBackend.GetJoystickAxis(JoystickHandle, i);
102-
if (joystickInput == 0)
103-
{
104-
// this indicates an sdl error, so just set our internal axis to 0
105-
joystickInput = short.MinValue;
106-
}
107-
108-
AddAxisEvent(i, joystickInput, nowSdlTimestamp, nowTimestamp);
109-
}
110-
111-
var hatCount = NativeBackend.GetNumJoystickHats(joystickHandle);
112-
for (var i = 0; i < hatCount; ++i)
113-
{
114-
var hatInput = NativeBackend.GetJoystickHat(joystickHandle, i);
115-
AddHatEvent(i, hatInput, nowSdlTimestamp, nowTimestamp);
116-
}
117-
118-
_rawAxisState = new float[EnumInfo<JoystickAxis>.UniqueValues.Count + axisCount];
119-
_rawButtonState = new Button<JoystickButton>[EnumInfo<JoystickButton>.UniqueValues.Count + buttonCount];
120-
121-
State = new JoystickState(_rawAxisState, _rawButtonState, _rawHatState);
12272
}
12373

12474

@@ -153,7 +103,7 @@ public void AddHatEvent(int hatIdx, byte hatInput, ulong sdlTimestamp, long time
153103
var previous = hatStateRef;
154104
hatStateRef = new Vector2(x, y);
155105

156-
foreach(var device in _devices)
106+
foreach (var device in _devices)
157107
{
158108
device.UpdateFromJoyHat(hatIdx, hatState, sdlTimestamp, timestamp);
159109
}
@@ -205,16 +155,69 @@ internal static Vector2 SplitValue(float value)
205155
return value > 0 ? new Vector2(0, value) : new Vector2(value, 0);
206156
}
207157

158+
protected internal override void Initialize()
159+
{
160+
var joystickHandle = NativeBackend.OpenJoystick((uint)SdlDeviceId);
161+
if (joystickHandle.Handle == null)
162+
{
163+
var error = NativeBackend.GetError();
164+
string? errorStr = null;
165+
if (error.Native != null)
166+
{
167+
errorStr = error.ReadToString();
168+
NativeBackend.Free(error.Native);
169+
}
170+
171+
throw new Exception($"Failed to open joystick: {errorStr ?? "Unknown error."}");
172+
}
173+
174+
// init current joystick state
175+
var buttonCount = NativeBackend.GetNumJoystickButtons(joystickHandle);
176+
var nowTimestamp = System.Diagnostics.Stopwatch.GetTimestamp();
177+
var nowSdlTimestamp = NativeBackend.GetTicks();
178+
for (byte i = 0; i < buttonCount; i++)
179+
{
180+
var joystickInput = NativeBackend.GetJoystickButtonRaw(JoystickHandle, i);
181+
AddButtonEvent(i, joystickInput, nowSdlTimestamp, nowTimestamp);
182+
}
183+
184+
var axisCount = NativeBackend.GetNumJoystickAxes(joystickHandle);
185+
for (var i = 0; i < axisCount; i++)
186+
{
187+
var joystickInput = NativeBackend.GetJoystickAxis(JoystickHandle, i);
188+
if (joystickInput == 0)
189+
{
190+
// this indicates an sdl error, so just set our internal axis to 0
191+
joystickInput = short.MinValue;
192+
}
193+
194+
AddAxisEvent(i, joystickInput, nowSdlTimestamp, nowTimestamp);
195+
}
196+
197+
var hatCount = NativeBackend.GetNumJoystickHats(joystickHandle);
198+
for (var i = 0; i < hatCount; ++i)
199+
{
200+
var hatInput = NativeBackend.GetJoystickHat(joystickHandle, i);
201+
AddHatEvent(i, hatInput, nowSdlTimestamp, nowTimestamp);
202+
}
203+
204+
JoystickHandle = joystickHandle;
205+
_joystickType = NativeBackend.GetJoystickType(joystickHandle);
206+
_rawAxisState = new float[EnumInfo<JoystickAxis>.UniqueValues.Count + axisCount];
207+
_rawButtonState = new Button<JoystickButton>[EnumInfo<JoystickButton>.UniqueValues.Count + buttonCount];
208+
State = new JoystickState(_rawAxisState, _rawButtonState, _rawHatState);
209+
}
210+
208211
protected override void Release() => NativeBackend.CloseJoystick(JoystickHandle);
209212

210213

211214
public void RefreshSdlId() => _sdlDeviceId = NativeBackend.GetJoystickID(JoystickHandle);
212215
private ulong _sdlDeviceId;
213216

214217
// State
215-
private readonly Button<JoystickButton>[] _rawButtonState;
216-
private readonly float[] _rawAxisState;
217-
private readonly Vector2[] _rawHatState = [];
218+
private Button<JoystickButton>[] _rawButtonState;
219+
private float[] _rawAxisState;
220+
private Vector2[] _rawHatState = [];
218221

219222
// Constants
220223
internal const short DigitalThreshold = short.MaxValue / 8;
@@ -225,5 +228,4 @@ internal static Vector2 SplitValue(float value)
225228
internal required ISdlEventQueue<JoystickHatMoveEvent> HatEvents { get; init; }
226229

227230
ButtonReadOnlyList<JoystickButton> IButtonDevice<JoystickButton>.State => State.Buttons;
228-
229231
}

sources/Input/Input/Implementations/SDL3/Devices/Pointers/SdlMouse.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ private SdlMouse(ulong sdlDeviceId, nint uniqueId, SdlInputBackend backend, IPoi
2424
{
2525
_state = new MouseState(Buttons, Points, Vector2.Zero);
2626
Cursor = cursor;
27+
}
28+
29+
protected internal override void Initialize()
30+
{
2731
float x = 0, y = 0;
2832
var nowSdl = NativeBackend.GetTicks();
2933
var now = Stopwatch.GetTimestamp();
@@ -76,15 +80,17 @@ public static SdlMouse CreateDevice(ulong sdlDeviceId, SdlInputBackend backend,
7680
uniqueId = SdlInputBackend.FallbackUniqueId<SdlMouse>(sdlDeviceId, uniqueId);
7781
}
7882

79-
return new
80-
SdlMouse(sdlDeviceId, uniqueId, backend, backend.UnboundedPointerTarget, backend.CursorConfiguration) {
83+
var mouse =
84+
new SdlMouse(sdlDeviceId, uniqueId, backend, backend.UnboundedPointerTarget, backend.CursorConfiguration) {
8185
ScrollEvents = silkEvents.MouseScrollSdlEvents,
8286
PointEvents = silkEvents.PointChangedSdlEvents,
8387
ClickEvents = silkEvents.PointerClickSdlEvents,
8488
ButtonEvents = silkEvents.PointerButtonSdlEvents,
8589
GripEvents = silkEvents.PointerGripChangedSdlEvents,
8690
TargetEvents = silkEvents.PointerTargetChangedSdlEvents
8791
};
92+
93+
return mouse;
8894
}
8995

9096
public override string Name => NativeBackend.GetMouseNameForID((uint)SdlDeviceId).ReadToString();
@@ -158,7 +164,7 @@ public bool TrySetPosition(Vector2 position)
158164
if (currentHintVal == 0)
159165
{
160166
sbyte hintVal = 1;
161-
if(NativeBackend.SetHint(Sdl.HintMouseRelativeWarpMotion, new Ref<sbyte>(ref hintVal)))
167+
if (NativeBackend.SetHint(Sdl.HintMouseRelativeWarpMotion, new Ref<sbyte>(ref hintVal)))
162168
{
163169
_hintsAsEvents = true;
164170
}

sources/Input/Input/Implementations/SDL3/Devices/Pointers/SdlPen.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public override string Name
7070
get;
7171
}
7272

73+
protected internal override void Initialize()
74+
{
75+
// todo ?
76+
}
77+
7378
protected override void Release()
7479
{
7580
}

sources/Input/Input/Implementations/SDL3/Devices/Pointers/SdlTouchSurface.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public override string Name
7777
}
7878

7979

80+
protected internal override void Initialize()
81+
{
82+
83+
}
84+
8085
protected override void Release() => InputLog.Debug("Releasing touch device, but touch devices have no special release logic.");
8186

8287
// todo - consider whether we want to use the related mouse device's buttons here

sources/Input/Input/Implementations/SDL3/Devices/SdlDevice.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ protected SdlDevice(SdlInputBackend backend, nint uniqueId, ulong sdlDeviceId)
3636
SdlDeviceId = sdlDeviceId;
3737
}
3838

39+
protected internal abstract void Initialize();
40+
3941
protected abstract void Release();
4042

4143
public void Dispose()

sources/Input/Input/Implementations/SDL3/Devices/SdlKeyboard.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ private SdlKeyboard(ulong sdlDeviceId, nint uniqueId, SdlInputBackend backend) :
5757
numLockActive: () => (_modState & Sdl.KmodNum) == Sdl.KmodNum);
5858
}
5959

60+
protected internal override void Initialize()
61+
{
62+
63+
}
64+
6065
protected override void Release()
6166
{
6267
}

sources/Input/Input/Implementations/SDL3/SdlInputBackend.Devices.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ internal bool TryGetOrCreateDevice<T>(ulong id, [NotNullWhen(true)] out T? devic
3939
return false;
4040
}
4141

42+
try
43+
{
44+
device.Initialize();
45+
}
46+
catch (Exception e)
47+
{
48+
InputLog.Error($"Failed to initialize device {nameof(T)} with id '{id}': {e}");
49+
device.Dispose();
50+
device = null;
51+
return false;
52+
}
53+
4254
sdlDevices.Add(device);
4355
InputLog.Debug($"{typeof(T)} added: (sdl ID: {id})");
4456
return true;

sources/Input/Input/Implementations/SDL3/SdlInputBackend.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,12 @@ internal bool TryGetPointerTargetForWindow(uint id,
543543
return target != null;
544544
}
545545

546-
private readonly struct TimedSdlEvent
546+
private readonly struct TimedRawSdlEvent
547547
{
548548
public readonly long Timestamp;
549549
public readonly Event Event;
550550

551-
public TimedSdlEvent(Event @event, long timestamp)
551+
public TimedRawSdlEvent(Event @event, long timestamp)
552552
{
553553
Event = @event;
554554
Timestamp = timestamp;
@@ -557,7 +557,7 @@ public TimedSdlEvent(Event @event, long timestamp)
557557

558558
private class SdlEventQueue
559559
{
560-
private TimedSdlEvent[] _events = new TimedSdlEvent[256];
560+
private TimedRawSdlEvent[] _events = new TimedRawSdlEvent[256];
561561
private int _nextEventIndex;
562562
public void Add(ref Event p0)
563563
{
@@ -572,7 +572,7 @@ public void Add(ref Event p0)
572572
}
573573

574574
[MethodImpl(MethodImplOptions.AggressiveInlining)]
575-
public bool TryDequeue(out TimedSdlEvent p0)
575+
public bool TryDequeue(out TimedRawSdlEvent p0)
576576
{
577577
if (_nextEventIndex == 0)
578578
{
@@ -603,7 +603,7 @@ public void Clear()
603603
private bool _isSorted;
604604

605605
// order in descending order, such that "de-queueing" the last event will return the first chronological event in the queue (last event in the array)
606-
private static readonly Comparer<Event> _comparer = Comparer<Event>.Create((e1, e2) => e2.Common.Timestamp.CompareTo(e1.Common.Timestamp));
606+
private static readonly Comparer<TimedRawSdlEvent> _comparer = Comparer<TimedRawSdlEvent>.Create((e1, e2) => e2.Timestamp.CompareTo(e1.Timestamp));
607607
}
608608

609609
private struct ProcessEventArgs

0 commit comments

Comments
 (0)