@@ -9,9 +9,9 @@ namespace Silk.NET.Input.SDL3.Devices.Joysticks;
99
1010internal 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}
0 commit comments