Games for Windows and the DirectX SDK blog

Technical tips, tricks, and news about game development for Microsoft platforms including desktop, Xbox, and UWP


Project maintained by walbourn Hosted on GitHub Pages — Theme by mattgraham
Home | Posts by Tag | Posts by Month

XINPUT and Windows 8

audio, input, win8

Originally posted to Chuck Walbourn's Blog on MSDN,

Windows 8.x and Windows 10 include version 1.4 of the XInput API for use with Xbox Common Controller compatible game devices, and this API is fully supported for both Windows Store apps (including x86, x64, and Windows on ARM) and desktop Win32 applications. The full details of the differences compared to XInput 1.3 which shipped in the DirectX SDK (June 2010) release are addressed on Microsoft Docs. The headers and libraries for Xinput 1.4 are included in the Windows 8.x/10 SDK. Windows 8 or later also includes an updated driver for these devices, XUSB22.SYS.

REDIST: For XInput 1.4 on Windows 8.x, Windows 10, and Windows RT; no redistribution is required since XInput 1.4 is included with the OS. For XInput 9.1.0 on Windows Vista, Windows 7, Windows 8.x, or Windows 10, no redistribution is required since XInput 9.1.0 is included with the OS. For XInput 1.3 on any version of Windows, use the legacy DirectX SDK REDIST.

Since Xinput version 1.4 is not available on Windows 7, Win32 desktop games that support older versions of Windows can use either XInput 1.3 or the still supported older XInput 9.1.0 which is included in Windows Vista, Windows 7, and Windows 8.

When building an application that is ‘down-level’ using headers in the Windows 8.x/10 SDK, be sure to explicitly select the correct ‘minimum’ _WIN32_WINNT value. For Windows 8, that is 0x0602 (which is the default when building code with Visual Studio 2012 and for all Windows Store apps). For Windows 7 use 0x0601, and for Windows Vista use 0x0600. Typically this is done as part of the project configuration via Preprocessor Definitions.

If you set _WIN32_WINNT correctly and try building with the Windows 8.0 SDK version of the xinput.h header, you will be using XInput 1.4 if set to 0x0602, or XInput 9.1.0 otherwise. If using XInput 1.4, you should link with XINPUT.LIB. If using XInput 9.1.0, link with XINPUT9_1_0.LIB instead.

If your usage of XInput is limited to basic gamepad functionality via XInputGetState and XInputSetState, then XInput 9.1.0 may be all the functionality you require and is supported on a broad range of Windows OSes without the need to use the DirectSetup redistribution package. A few things to note about XInput 9.1.0 are that XInputGetCapabilities returns a fixed set of values regardless of the attached device (notably including the subtype), and this version of XInput does not support headset audio functionality.

The following code will compile using all three versions of XInput:

#include <xinput.h>

XINPUT_STATE state = {};

if (XInputGetState(0, &state) == ERROR_SUCCESS)
{
    // Controller is connected
    if ( state.Gamepad.wButtons & XINPUT_GAMEPAD_A )
    {
        // Button A is pressed
    }

    XINPUT_VIBRATION motor = {};
    if (state.Gamepad.bLeftTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
    {
        motor.wLeftMotorSpeed = state.Gamepad.bLeftTrigger << 8;
    }

    if (state.Gamepad.bRightTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
    {
        motor.wRightMotorSpeed = state.Gamepad.bRightTrigger << 8;
    }

    XInputSetState(0, &motor);
}
else
{
    // Controller is not connected, shouldn't recheck it for a few seconds
}

If using the XInput 1.3 solution, You should follow the instructions on Microsoft Docs to use the Windows 8.x/10 SDK headers and libraries where possible and explicitly link to the DXSDK_DIR for the headers where you need older versions to support older versions of Windows. (see “Where is the DirectX SDK?”).

NOTICE: The legacy XInput 1.1, 1.2, and 1.3 DLLs are SHA-1 signed, and there are known security issues. Therefore, it’s best to avoid using XInput 1.1, 1.2, or 1.3 entirely.

In a future post, I address the details of using XInput 1.4’s audio features, and how to implement similar behavior down-level using XInput 1.3. XInput 9.1.0 doesn’t support audio features.

Note: The Windows 7.1 SDK includes the xinput.h header and xinput.lib import library for the the Xinput 9.1.0 version as well. XInput 9.1.0 can be deployed on Windows XP using the legacy DirectX SDK’s REDIST (aka DirectSetup).

Windows Server: Note that XInput is not included in Windows Server 2012. XInput 9.1.0 is also not present on Windows Server 2008 or 2012.

Xbox One Controller: The Xbox One controller is not supported by the XUSB21.SYS (Windows 7) or XUSB22.SYS (Windows 8.x) driver. There is a X1USB1.SYS driver now available which does support the Xbox One Controller for XINPUT when using a micro-USB cable–the Xbox One controller is not compatible with the Xbox 360 Wireless Receiver for Windows.

Windows 10: To continue to use XInput 1.4 with Universal Windows Platform (UWP) apps, be sure to link with xinputuap.lib rather than xinput.lib. Alternatively, you can make direct use of the new Windows.Gaming.Input API or the GamePad class in DirectX Tool Kit.

Also note that for Windows 10, input focus is automatic like it is with mouse and keyboard. For this reason, using XInputEnable when building with _WIN32_WINNT set to 0x0A00 will generate a deprecation compiler warning.

Related: XInput Win32 Samples, DirectX Tool Kit - Now with GamePads