#cs
/***************************************************************************
*                                                                          *
*   XInput.h -- This module defines Xbox 360 Common Controller APIs        *
*               and constants for the Windows platform.                    *
*                                                                          *
*   Copyright (c) Microsoft Corp. All rights reserved.                     *
*                                                                          *
*                                                                          *
*                                                AutoIt UDF by Yjuq        *
*                                                                          *
***************************************************************************/
#ce

#include-once
OnAutoItExitRegister(_XINPUT_ONEXIT)

; Current name of the DLL shipped in the same SDK as this header.
; The name reflects the current version

Global Const $XINPUT_DLL = _XINPUT_DLL(True)

;
; Device types available in XINPUT_CAPABILITIES
;

Global Const $XINPUT_DEVTYPE_GAMEPAD = 0x01

;
; Device subtypes available in XINPUT_CAPABILITIES
;

Global Const $XINPUT_DEVSUBTYPE_GAMEPAD = 0x01

If _XINPUT_OSVERSION(1) Then
	Global Const $XINPUT_DEVSUBTYPE_UNKNOWN          = 0x00
	Global Const $XINPUT_DEVSUBTYPE_WHEEL            = 0x02
	Global Const $XINPUT_DEVSUBTYPE_ARCADE_STICK     = 0x03
	Global Const $XINPUT_DEVSUBTYPE_FLIGHT_STICK     = 0x04
	Global Const $XINPUT_DEVSUBTYPE_DANCE_PAD        = 0x05
	Global Const $XINPUT_DEVSUBTYPE_GUITAR           = 0x06
	Global Const $XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE = 0x07
	Global Const $XINPUT_DEVSUBTYPE_DRUM_KIT         = 0x08
	Global Const $XINPUT_DEVSUBTYPE_GUITAR_BASS      = 0x0B
	Global Const $XINPUT_DEVSUBTYPE_ARCADE_PAD       = 0x13
EndIf

;
; Flags for XINPUT_CAPABILITIES
;

Global Const $XINPUT_CAPS_VOICE_SUPPORTED = 0x0004

If _XINPUT_OSVERSION(1) Then
	Global Const $XINPUT_CAPS_FFB_SUPPORTED = 0x0001
	Global Const $XINPUT_CAPS_WIRELESS      = 0x0002
	Global Const $XINPUT_CAPS_PMD_SUPPORTED = 0x0008
	Global Const $XINPUT_CAPS_NO_NAVIGATION = 0x0010
EndIf

;
; Constants for gamepad buttons
;

Global Const $XINPUT_GAMEPAD_DPAD_UP        = 0x0001
Global Const $XINPUT_GAMEPAD_DPAD_DOWN      = 0x0002
Global Const $XINPUT_GAMEPAD_DPAD_LEFT      = 0x0004
Global Const $XINPUT_GAMEPAD_DPAD_RIGHT     = 0x0008
Global Const $XINPUT_GAMEPAD_START          = 0x0010
Global Const $XINPUT_GAMEPAD_BACK           = 0x0020
Global Const $XINPUT_GAMEPAD_LEFT_THUMB     = 0x0040
Global Const $XINPUT_GAMEPAD_RIGHT_THUMB    = 0x0080
Global Const $XINPUT_GAMEPAD_LEFT_SHOULDER  = 0x0100
Global Const $XINPUT_GAMEPAD_RIGHT_SHOULDER = 0x0200
Global Const $XINPUT_GAMEPAD_A              = 0x1000
Global Const $XINPUT_GAMEPAD_B              = 0x2000
Global Const $XINPUT_GAMEPAD_X              = 0x4000
Global Const $XINPUT_GAMEPAD_Y              = 0x8000

;
; Gamepad thresholds
;

Global Const $XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE  = 7849
Global Const $XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE = 8689
Global Const $XINPUT_GAMEPAD_TRIGGER_THRESHOLD    = 30

;
; Flags to pass to XInputGetCapabilities
;

Global Const $XINPUT_FLAG_GAMEPAD = 0x00000001

If _XINPUT_OSVERSION(1) Then
	;
	; Devices that support batteries
	;

	Global Const $BATTERY_DEVTYPE_GAMEPAD = 0x00
	Global Const $BATTERY_DEVTYPE_HEADSET = 0x01

	;
	; Flags for battery status level
	;

	Global Const $BATTERY_TYPE_DISCONNECTED = 0x00    ; This device is not connected
	Global Const $BATTERY_TYPE_WIRED        = 0x01    ; Wired device, no battery
	Global Const $BATTERY_TYPE_ALKALINE     = 0x02    ; Alkaline battery source
	Global Const $BATTERY_TYPE_NIMH         = 0x03    ; Nickel Metal Hydride battery source
	Global Const $BATTERY_TYPE_UNKNOWN      = 0xFF    ; Cannot determine the battery type

	; These are only valid for wireless, connected devices, with known battery types
	; The amount of use time remaining depends on the type of device.
	Global Const $BATTERY_LEVEL_EMPTY  = 0x00
	Global Const $BATTERY_LEVEL_LOW    = 0x01
	Global Const $BATTERY_LEVEL_MEDIUM = 0x02
	Global Const $BATTERY_LEVEL_FULL   = 0x03
EndIf

; User index definitions
Global Const $XUSER_MAX_COUNT = 4

Global Const $XUSER_INDEX_ANY = 0x000000FF

If _XINPUT_OSVERSION(1) Then
	;
	; Codes returned for the gamepad keystroke
	;

	Global Const $VK_PAD_A         = 0x5800
	Global Const $VK_PAD_B         = 0x5801
	Global Const $VK_PAD_X         = 0x5802
	Global Const $VK_PAD_Y         = 0x5803
	Global Const $VK_PAD_RSHOULDER = 0x5804
	Global Const $VK_PAD_LSHOULDER = 0x5805
	Global Const $VK_PAD_LTRIGGER  = 0x5806
	Global Const $VK_PAD_RTRIGGER  = 0x5807

	Global Const $VK_PAD_DPAD_UP      = 0x5810
	Global Const $VK_PAD_DPAD_DOWN    = 0x5811
	Global Const $VK_PAD_DPAD_LEFT    = 0x5812
	Global Const $VK_PAD_DPAD_RIGHT   = 0x5813
	Global Const $VK_PAD_START        = 0x5814
	Global Const $VK_PAD_BACK         = 0x5815
	Global Const $VK_PAD_LTHUMB_PRESS = 0x5816
	Global Const $VK_PAD_RTHUMB_PRESS = 0x5817

	Global Const $VK_PAD_LTHUMB_UP        = 0x5820
	Global Const $VK_PAD_LTHUMB_DOWN      = 0x5821
	Global Const $VK_PAD_LTHUMB_RIGHT     = 0x5822
	Global Const $VK_PAD_LTHUMB_LEFT      = 0x5823
	Global Const $VK_PAD_LTHUMB_UPLEFT    = 0x5824
	Global Const $VK_PAD_LTHUMB_UPRIGHT   = 0x5825
	Global Const $VK_PAD_LTHUMB_DOWNRIGHT = 0x5826
	Global Const $VK_PAD_LTHUMB_DOWNLEFT  = 0x5827

	Global Const $VK_PAD_RTHUMB_UP        = 0x5830
	Global Const $VK_PAD_RTHUMB_DOWN      = 0x5831
	Global Const $VK_PAD_RTHUMB_RIGHT     = 0x5832
	Global Const $VK_PAD_RTHUMB_LEFT      = 0x5833
	Global Const $VK_PAD_RTHUMB_UPLEFT    = 0x5834
	Global Const $VK_PAD_RTHUMB_UPRIGHT   = 0x5835
	Global Const $VK_PAD_RTHUMB_DOWNRIGHT = 0x5836
	Global Const $VK_PAD_RTHUMB_DOWNLEFT  = 0x5837

	;
	; Flags used in XINPUT_KEYSTROKE
	;

	Global Const $XINPUT_KEYSTROKE_KEYDOWN = 0x0001
	Global Const $XINPUT_KEYSTROKE_KEYUP   = 0x0002
	Global Const $XINPUT_KEYSTROKE_REPEAT  = 0x0004
EndIf

;
; Structures used by XInput APIs
;

Global Const $_XINPUT_GAMEPAD = _
	"word	wButtons;"		& _
	"byte	bLeftTrigger;"	& _
	"byte	bRightTrigger;"	& _
	"short	sThumbLX;"		& _
	"short	sThumbLY;"		& _
	"short	sThumbRX;"		& _
	"short	sThumbRY;"

Global Const $_XINPUT_STATE = _
	"dword	dwPacketNumber;"	& _
	"struct;"					& _
		$_XINPUT_GAMEPAD		& _
	"endstruct;"

Global Const $_XINPUT_VIBRATION = _
	"word	wLeftMotorSpeed;"	& _
	"word	wRightMotorSpeed;"

Global Const $_XINPUT_CAPABILITIES = _
	"byte	Type;"			& _
	"byte	SubType;"		& _
	"word	Flags;"			& _
	"struct;"				& _
		$_XINPUT_GAMEPAD	& _
	"endstruct;"			& _
	"struct;"				& _
		$_XINPUT_VIBRATION	& _
	"endstruct;"

If _XINPUT_OSVERSION(1) Then
	Global Const $_XINPUT_BATTERY_INFORMATION = _
		"byte	BatteryType;"	& _
		"byte	BatteryLevel;"

	Global Const $_XINPUT_KEYSTROKE = _
		"word	VirtualKey;"	& _
		"wchar	Unicode;"		& _
		"word	Flags;"			& _
		"byte	UserIndex;"		& _
		"byte	HidCode;"
EndIf

Func XInputGetState($dwUserIndex, $pState)
	Return DllCall($XINPUT_DLL, "dword", "XInputGetState", _
		"dword", $dwUserIndex, _		; Index of the gamer associated with the device
		"struct*", $pState)[0]			; Receives the current state
EndFunc

Func XInputSetState($dwUserIndex, $pVibration)
	Return DllCall($XINPUT_DLL, "dword", "XInputSetState", _
		"dword", $dwUserIndex, _		; Index of the gamer associated with the device
		"struct*", $pVibration)[0]		; The vibration information to send to the controller
EndFunc

Func XInputGetCapabilities($dwUserIndex, $dwFlags, $pCapabilities)
	Return DllCall($XINPUT_DLL, "dword", "XInputGetCapabilities", _
		"dword", $dwUserIndex, _		; Index of the gamer associated with the device
		"dword", $dwFlags, _			; Input flags that identify the device type
		"struct*", $pCapabilities)[0]	; Receives the capabilities
EndFunc

Func XInputEnable($enable)
	If _XINPUT_OSVERSION(2) Then
		ConsoleWrite("! XInputEnable is deprecated on " & @OSVersion & @CRLF)
		Return
	EndIf

	Return DllCall($XINPUT_DLL, "none", "XInputEnable", _
		"boolean", $enable)[0]	; [in] Indicates whether xinput is enabled or disabled.
EndFunc

Func XInputGetAudioDeviceIds($dwUserIndex, $pRenderDeviceId, $pRenderCount, $pCaptureDeviceId, $pCaptureCount)
	Return DllCall($XINPUT_DLL, "dword", "XInputGetAudioDeviceIds", _
		"dword", $dwUserIndex, _		; Index of the gamer associated with the device
		"wstr*", $pRenderDeviceId, _	; Windows Core Audio device ID string for render (speakers)
		"uint*", $pRenderCount, _		; Size of render device ID string buffer (in wide-chars)
		"wstr*", $pCaptureDeviceId, _	; Windows Core Audio device ID string for capture (microphone)
		"uint*", $pCaptureCount)[0]		; Size of capture device ID string buffer (in wide-chars)
EndFunc

Func XInputGetBatteryInformation($dwUserIndex, $devType, $pBatteryInformation)
	Return DllCall($XINPUT_DLL, "dword", "XInputGetBatteryInformation", _
		"dword", $dwUserIndex, _			; Index of the gamer associated with the device
		"byte",	$devType, _					; Which device on this user index
		"struct*", $pBatteryInformation)[0]	; Contains the level and types of batteries
EndFunc

Func XInputGetKeystroke($dwUserIndex, $dwReserved, $pKeystroke)
	Return DllCall($XINPUT_DLL, "dword","XInputGetKeystroke", _
		"dword", $dwUserIndex, _	; Index of the gamer associated with the device
		"dword", $dwReserved, _		; Reserved for future use
		"struct*", $pKeystroke)[0]	; Pointer to an XINPUT_KEYSTROKE structure that receives an input event.
EndFunc

Func XInputGetDSoundAudioDeviceGuids($dwUserIndex, $pDSoundRenderGuid, $pDSoundCaptureGuid)
	If _XINPUT_OSVERSION(2) Then
		ConsoleWrite("! XInputGetDSoundAudioDeviceGuids is deprecated on " & @OSVersion & @CRLF)
		Return
	EndIf

	Return DllCall($XINPUT_DLL, "dword", "XInputGetDSoundAudioDeviceGuids", _
		"dword", $dwUserIndex, _			; Index of the gamer associated with the device
		"struct*", $pDSoundRenderGuid, _	; DSound device ID for render (speakers)
		"struct*", $pDSoundCaptureGuid)[0]	; DSound device ID for capture (microphone)
EndFunc

; ++++++++++ +++++++++ ++++++++ +++++++ ++++++ +++++ ++++ +++ ++ +

Func _XINPUT_ONEXIT()
	_XINPUT_DLL(False)
EndFunc

Func _XINPUT_DLL($open)
	Local Static $dll = Null

	If $open Then
		If $dll <> Null Then DllClose($dll)

		If _XINPUT_OSVERSION(1) Then
			$dll = DllOpen("xinput1_4.dll")
		Else
			$dll = DllOpen("xinput9_1_0.dll")
		EndIf

		Return $dll
	EndIf

	DllClose($dll)
	$dll = Null
EndFunc

Func _XINPUT_OSVERSION($type)
	Switch $type
		Case 1
			Return @OSVersion = "WIN_10" Or _
				@OSVersion = "WIN_81" Or _
				@OSVersion = "WIN_8"
		Case 2
			Return @OSVersion = "WIN_10"
	EndSwitch
EndFunc