GamepadBatteryInfo constructor

GamepadBatteryInfo(
  1. int controller,
  2. GamepadDeviceType deviceType
)

Creates an instance of GamepadBatteryInfo for the specified controller and device type.

The controller parameter represents the index of the gamepad controller, and the deviceType parameter specifies whether it's a gamepad controller or an attached headset.

Throws a DeviceNotConnectedError if the specified controller is not connected.

Implementation

GamepadBatteryInfo(int controller, GamepadDeviceType deviceType) {
  final pBatteryInformation = calloc<XINPUT_BATTERY_INFORMATION>();
  try {
    final dwResult = XInputGetBatteryInformation(
      controller,
      deviceType == GamepadDeviceType.controller
          ? BATTERY_DEVTYPE.BATTERY_DEVTYPE_GAMEPAD
          : BATTERY_DEVTYPE.BATTERY_DEVTYPE_HEADSET,
      pBatteryInformation,
    );
    if (dwResult == WIN32_ERROR.ERROR_DEVICE_NOT_CONNECTED) {
      throw DeviceNotConnectedError();
    } else {
      final XINPUT_BATTERY_INFORMATION(:BatteryLevel, :BatteryType) =
          pBatteryInformation.ref;
      _batteryLevel = BatteryLevel;
      _batteryType = BatteryType;
    }
  } finally {
    free(pBatteryInformation);
  }
}