Skip to content

Instantly share code, notes, and snippets.

@tmyt
Last active February 2, 2021 20:00
Show Gist options
  • Save tmyt/7318d62150e554bcf69cbc542e0288cd to your computer and use it in GitHub Desktop.
Save tmyt/7318d62150e554bcf69cbc542e0288cd to your computer and use it in GitHub Desktop.
using System.Runtime.InteropServices;
namespace Interop
{
static class XInput
{
public const int XUserMaxCount = 4;
/**
* <summary>Sets the reporting state of XInput.</summary>
* <param name="enable">
* If enable is FALSE, XInput will only send neutral data in response to XInputGetState (all buttons up, axes centered, and triggers at 0).
* XInputSetState calls will be registered but not sent to the device. Sending any value other than FALSE will restore reading and writing functionality to normal.
* </param>
*/
[DllImport("XInput1_4.dll")]
public static extern void XInputEnable(
[MarshalAs(UnmanagedType.Bool)]
bool enable
);
/**
* <summary>Retrieves the battery type and charge status of a wireless controller.</summary>
* <param name="dwUserIndex">Index of the signed-in gamer associated with the device. Can be a value in the range 0–<see cref="XUserMaxCount"/> − 1.</param>
* <param name="devType">Specifies which device associated with this user index should be queried. Must be <see cref="BatteryDevType.Gamepad"/> or <see cref="BatteryDevType.Headset"/>.</param>
* <param name="pBatteryInformation">Pointer to an <see cref="XInputBatteryInformation"/> structure that receives the battery information.</param>
* <returns>If the function succeeds, the return value is ERROR_SUCCESS.</returns>
*/
[DllImport("XInput1_4.dll")]
public static extern uint XInputGetBatteryInformation(
int dwUserIndex,
BatteryDevType devType,
out XInputBatteryInformation pBatteryInformation
);
}
/**
* <summary>Contains information on battery type and charge state.</summary>
*/
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct XInputBatteryInformation
{
/**
* <summary>The type of battery.</summary>
*/
public readonly BatteryType BatteryType;
/**
* <summary>The charge state of the battery. This value is only valid for wireless devices with a known battery type.</summary>
*/
public readonly BatteryLevel BatteryLevel;
}
enum BatteryDevType : byte
{
Gamepad = 0,
Headset = 1,
}
enum BatteryType : byte
{
/**
* <summary>The device is not connected.</summary>
*/
Disconnected = 0x00,
/**
* <summary>The device is a wired device and does not have a battery.</summary>
*/
Wired = 0x01,
/**
* <summary>The device has an alkaline battery.</summary>
*/
Alkaline = 0x02,
/**
* <summary>The device has a nickel metal hydride battery.</summary>
*/
NiMH = 0x03,
/**
* <summary>The device has an unknown battery type.</summary>
*/
Unknown = 0xFF,
}
enum BatteryLevel : byte
{
Empty = 0x00,
Low = 0x01,
Medium = 0x02,
Full = 0x03,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment