33
44namespace XInputDotNetPure
55{
6-
76 class Imports
87 {
98 internal const string DLLName = "XInputInterface" ;
109
1110 [ DllImport ( DLLName ) ]
12- public static extern uint XInputGamePadGetState ( uint playerIndex , IntPtr state ) ;
11+ public static extern uint XInputGamePadGetState ( uint playerIndex , out GamePadState . RawState state ) ;
1312 [ DllImport ( DLLName ) ]
1413 public static extern void XInputGamePadSetState ( uint playerIndex , float leftMotor , float rightMotor ) ;
1514 }
@@ -22,18 +21,19 @@ public enum ButtonState
2221
2322 public struct GamePadButtons
2423 {
25- ButtonState start , back , leftStick , rightStick , leftShoulder , rightShoulder , a , b , x , y ;
24+ ButtonState start , back , leftStick , rightStick , leftShoulder , rightShoulder , guide , a , b , x , y ;
2625
2726 internal GamePadButtons ( ButtonState start , ButtonState back , ButtonState leftStick , ButtonState rightStick ,
28- ButtonState leftShoulder , ButtonState rightShoulder , ButtonState a , ButtonState b ,
29- ButtonState x , ButtonState y )
27+ ButtonState leftShoulder , ButtonState rightShoulder , ButtonState guide ,
28+ ButtonState a , ButtonState b , ButtonState x , ButtonState y )
3029 {
3130 this . start = start ;
3231 this . back = back ;
3332 this . leftStick = leftStick ;
3433 this . rightStick = rightStick ;
3534 this . leftShoulder = leftShoulder ;
3635 this . rightShoulder = rightShoulder ;
36+ this . guide = guide ;
3737 this . a = a ;
3838 this . b = b ;
3939 this . x = x ;
@@ -70,6 +70,11 @@ public ButtonState RightShoulder
7070 get { return rightShoulder ; }
7171 }
7272
73+ public ButtonState Guide
74+ {
75+ get { return guide ; }
76+ }
77+
7378 public ButtonState A
7479 {
7580 get { return a ; }
@@ -190,14 +195,16 @@ public float Right
190195
191196 public struct GamePadState
192197 {
198+ [ StructLayout ( LayoutKind . Sequential ) ]
193199 internal struct RawState
194200 {
195201 public uint dwPacketNumber ;
196202 public GamePad Gamepad ;
197203
204+ [ StructLayout ( LayoutKind . Sequential ) ]
198205 public struct GamePad
199206 {
200- public ushort dwButtons ;
207+ public ushort wButtons ;
201208 public byte bLeftTrigger ;
202209 public byte bRightTrigger ;
203210 public short sThumbLX ;
@@ -226,6 +233,7 @@ enum ButtonsConstants
226233 RightThumb = 0x00000080 ,
227234 LeftShoulder = 0x0100 ,
228235 RightShoulder = 0x0200 ,
236+ Guide = 0x0400 ,
229237 A = 0x1000 ,
230238 B = 0x2000 ,
231239 X = 0x4000 ,
@@ -239,7 +247,7 @@ internal GamePadState(bool isConnected, RawState rawState, GamePadDeadZone deadZ
239247 if ( ! isConnected )
240248 {
241249 rawState . dwPacketNumber = 0 ;
242- rawState . Gamepad . dwButtons = 0 ;
250+ rawState . Gamepad . wButtons = 0 ;
243251 rawState . Gamepad . bLeftTrigger = 0 ;
244252 rawState . Gamepad . bRightTrigger = 0 ;
245253 rawState . Gamepad . sThumbLX = 0 ;
@@ -250,22 +258,23 @@ internal GamePadState(bool isConnected, RawState rawState, GamePadDeadZone deadZ
250258
251259 packetNumber = rawState . dwPacketNumber ;
252260 buttons = new GamePadButtons (
253- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . Start ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
254- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . Back ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
255- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . LeftThumb ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
256- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . RightThumb ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
257- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . LeftShoulder ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
258- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . RightShoulder ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
259- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . A ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
260- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . B ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
261- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . X ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
262- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . Y ) != 0 ? ButtonState . Pressed : ButtonState . Released
261+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . Start ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
262+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . Back ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
263+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . LeftThumb ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
264+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . RightThumb ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
265+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . LeftShoulder ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
266+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . RightShoulder ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
267+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . Guide ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
268+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . A ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
269+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . B ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
270+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . X ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
271+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . Y ) != 0 ? ButtonState . Pressed : ButtonState . Released
263272 ) ;
264273 dPad = new GamePadDPad (
265- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . DPadUp ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
266- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . DPadDown ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
267- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . DPadLeft ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
268- ( rawState . Gamepad . dwButtons & ( uint ) ButtonsConstants . DPadRight ) != 0 ? ButtonState . Pressed : ButtonState . Released
274+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . DPadUp ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
275+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . DPadDown ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
276+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . DPadLeft ) != 0 ? ButtonState . Pressed : ButtonState . Released ,
277+ ( rawState . Gamepad . wButtons & ( uint ) ButtonsConstants . DPadRight ) != 0 ? ButtonState . Pressed : ButtonState . Released
269278 ) ;
270279
271280 thumbSticks = new GamePadThumbSticks (
@@ -333,10 +342,8 @@ public static GamePadState GetState(PlayerIndex playerIndex)
333342
334343 public static GamePadState GetState ( PlayerIndex playerIndex , GamePadDeadZone deadZone )
335344 {
336- IntPtr gamePadStatePointer = Marshal . AllocHGlobal ( Marshal . SizeOf ( typeof ( GamePadState . RawState ) ) ) ;
337- uint result = Imports . XInputGamePadGetState ( ( uint ) playerIndex , gamePadStatePointer ) ;
338- GamePadState . RawState state = ( GamePadState . RawState ) Marshal . PtrToStructure ( gamePadStatePointer , typeof ( GamePadState . RawState ) ) ;
339- Marshal . FreeHGlobal ( gamePadStatePointer ) ;
345+ GamePadState . RawState state ;
346+ uint result = Imports . XInputGamePadGetState ( ( uint ) playerIndex , out state ) ;
340347 return new GamePadState ( result == Utils . Success , state , deadZone ) ;
341348 }
342349
0 commit comments