Xbox 360 controller (VERY basic access!)
Posted: Tue Oct 10, 2006 10:22 pm
Hi all,
Just had a quick go at accessing the Xbox 360 (wired) controller from PB. This raw, user-unfriendly code just lists any plugged-in controllers, but it's a start, and it seemed a waste not to post it since I can't find any other related code using the forum search. I haven't tried anything more yet, but there's no reason the rest of the functions shouldn't work... this might encourage someone else to take it further anyway!
NB. Please note that a couple of the structure fields could possibly be wrong, eg. WCHAR, WORD vs SHORT, etc. This stuff always baffles me when converting from C/C++/C#, etc!
Just had a quick go at accessing the Xbox 360 (wired) controller from PB. This raw, user-unfriendly code just lists any plugged-in controllers, but it's a start, and it seemed a waste not to post it since I can't find any other related code using the forum search. I haven't tried anything more yet, but there's no reason the rest of the functions shouldn't work... this might encourage someone else to take it further anyway!
NB. Please note that a couple of the structure fields could possibly be wrong, eg. WCHAR, WORD vs SHORT, etc. This stuff always baffles me when converting from C/C++/C#, etc!
Code: Select all
; -----------------------------------------------------------------------------
; Quick Xbox 360 pad test...
; -----------------------------------------------------------------------------
; See function/structure reference on how to use this stuff...
; http://msdn.microsoft.com/library/Default.asp?url=/library/en-us/directx9_c/dx9_xinput_reference.asp
; IMPORTANT: Make sure standard M$ drivers are installed! Get them here...
; http://www.microsoft.com/hardware/gaming/Productlist.aspx?pid=all
; Some relevant constants found here:
; http://forum.thegamecreators.com/?m=forum_view&t=78938&b=6
; -----------------------------------------------------------------------------
; XInput constants...
; -----------------------------------------------------------------------------
; Versions...
#XINPUT_1_0 = "xinput9_1_0.dll" ; Old DLL!
#XINPUT_1_1 = "xinput1_1.dll"
; XINPUT_GAMEPAD .wButtons bits:
#XINPUT_GAMEPAD_DPAD_UP = $00000001
#XINPUT_GAMEPAD_DPAD_DOWN = $00000002
#XINPUT_GAMEPAD_DPAD_LEFT = $00000004
#XINPUT_GAMEPAD_DPAD_RIGHT = $00000008
#XINPUT_GAMEPAD_START = $00000010
#XINPUT_GAMEPAD_BACK = $00000020
#XINPUT_GAMEPAD_LEFT_THUMB = $00000040
#XINPUT_GAMEPAD_RIGHT_THUMB = $00000080
#XINPUT_GAMEPAD_LEFT_SHOULDER = $0100
#XINPUT_GAMEPAD_RIGHT_SHOULDER = $0200
#XINPUT_GAMEPAD_A = $1000
#XINPUT_GAMEPAD_B = $2000
#XINPUT_GAMEPAD_X = $4000
#XINPUT_GAMEPAD_Y = $8000
; Thumbstick values, from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/dx9_xinput_getting_started.asp
#XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE = 7849
#XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE = 8689
#XINPUT_GAMEPAD_TRIGGER_THRESHOLD = 30
; Pass to XInputGetCapabilities as flag...
#XINPUT_FLAG_GAMEPAD = $00000001
; Bit flags for XInputGetCapabilities...
#XINPUT_DEVTYPE_GAMEPAD = $01
#XINPUT_DEVSUBTYPE_GAMEPAD = $01
#XINPUT_DEVSUBTYPE_WHEEL = $02
#XINPUT_DEVSUBTYPE_ARCADE_STICK = $03
#XINPUT_DEVSUBTYPE_FLIGHT_SICK = $04
#XINPUT_DEVSUBTYPE_DANCE_PAD = $05
; Voice support...
#XINPUT_CAPS_VOICE_SUPPORTED = $0004
; Misc...
#XINPUT_MAX_USERS = 4
#XINPUT_ERROR_SUCCESS = 0
#XINPUT_ERROR_UNSUPPORTED = 404
#XINPUT_ERROR_DLLEXIST = 405
#XINPUT_ERROR_DLLNOTEXIST = 406
; -----------------------------------------------------------------------------
; Structures...
; -----------------------------------------------------------------------------
Structure XINPUT_GAMEPAD
wButtons.w
bLeftTrigger.b
bRightTrigger.b
sThumbLX.w
sThumbLY.w
sThumbRX.w
sThumbRY.w
EndStructure
Structure XINPUT_VIBRATION
wLeftMotorSpeed.w
wRightMotorSpeed.w
EndStructure
Structure XINPUT_STATE
dwPacketNumber.l
Gamepad.XINPUT_GAMEPAD
EndStructure
Structure XINPUT_KEYSTROKE
VirtualKey.w
Unicode.w ; WCHAR - "unsigned short is the WCHAR Windows UNICODE type" - http://blog.kowalczyk.info/archives/2006/01/
Flags.w
UserIndex.b
HidCode.b
EndStructure
Structure XINPUT_CAPABILITIES
Type.b
SubType.b
Flags.w
Gamepad.XINPUT_GAMEPAD
Vibration.XINPUT_VIBRATION
EndStructure
; -----------------------------------------------------------------------------
; D E M O . . .
; -----------------------------------------------------------------------------
Global XINPUT_VERSION.s ; Store version for later use...
xinput = OpenLibrary (#PB_Any, #XINPUT_1_1)
; Check for older version if unable to open version 1.1...
If xinput = 0
; Try version 1.0...
xinput = OpenLibrary (#PB_Any, #XINPUT_1_0)
If xinput
XINPUT_VERSION = "1.0"
EndIf
Else
XINPUT_VERSION = "1.1"
EndIf
If xinput
; Just testing -- function addresses/availability...
; Debug GetFunction (xinput, "XInputGetDSoundAudioDeviceGuids")
; Debug GetFunction (xinput, "XInputGetCapabilities")
; Debug GetFunction (xinput, "XInputGetKeystroke") ; For Xbox360 wireless controller only!
; Debug GetFunction (xinput, "XInputGetState")
; Debug GetFunction (xinput, "XInputSetState")
; If XINPUT_VERSION = "1.1"
; Debug GetFunction (xinput, "XInputEnable")
; Else
; Debug "XInputEnable () not available in version 1.0!"
; EndIf
xin_GetSoundIDs = GetFunction (xinput, "XInputGetDSoundAudioDeviceGuids")
xin_GetCaps = GetFunction (xinput, "XInputGetCapabilities")
xin_GetState = GetFunction (xinput, "XInputGetState")
xin_SetState = GetFunction (xinput, "XInputSetState")
If XINPUT_VERSION = "1.1"
xin_Enable = GetFunction (xinput, "XInputEnable") ; Not in version 1.0!
EndIf
; CallFunctionFast (xin_GetState, 0, @xin_state.XINPUT_STATE)
; Debug xin_state\dwPacketNumber
; NOTE: "port" numbers are apparently assigned on the fly as
; controllers are connected and disconnected... I think!
; List plugged in controllers...
For port = 0 To #XINPUT_MAX_USERS - 1
; Note successful return value is actually 0 for XInputGetCapabilities... good old M$!
If CallFunctionFast (xin_GetCaps, port, #XINPUT_FLAG_GAMEPAD, @caps.XINPUT_CAPABILITIES) = #XINPUT_ERROR_SUCCESS
Debug "Xbox 360 controller plugged into port " + Str (port)
; Debug caps\Type
; Debug caps\SubType
; This returns 0 for some reason. Should be $01! Don't know what's up. Probably
; best to ignore and just check sub-type.
If caps\Type = #XINPUT_DEVTYPE_GAMEPAD
Debug "Controller type: game pad"
EndIf
If caps\SubType = #XINPUT_DEVSUBTYPE_GAMEPAD
Debug "(Standard Xbox 360 game pad)"
EndIf
Else
Debug "[No Xbox 360 controller plugged into port " + Str (port) + "]"
EndIf
Debug "--"
Next
CloseLibrary (xinput)
Else
Debug "Can't open XInput DLL! Install latest DirectX 9 and Xbox 360 controller drivers!"
EndIf