pub struct XInputHandle { /* private fields */ }
Expand description

A handle to a loaded XInput DLL.

Implementations§

source§

impl XInputHandle

source

pub fn load_default() -> Result<XInputHandle, XInputLoadingFailure>

Attempts to dynamically load an XInput DLL and get the function pointers.

Failure

This can fail in a few ways, as explained in the XInputLoadingFailure type. The most likely failure case is that the user’s system won’t have the required DLL, in which case you should probably allow them to play with just a keyboard/mouse instead.

Current DLL Names

Currently the following DLL names are searched for in this order:

  • xinput1_4.dll
  • xinput1_3.dll
  • xinput1_2.dll
  • xinput1_1.dll
  • xinput9_1_0.dll
source

pub fn load<S: AsRef<str>>(s: S) -> Result<XInputHandle, XInputLoadingFailure>

Attempt to load a specific XInput DLL and get the function pointers.

source§

impl XInputHandle

source

pub fn enable(&self, enable: bool)

Enables or disables XInput.

See the MSDN documentation for XInputEnable.

source

pub fn get_state( &self, user_index: u32 ) -> Result<XInputState, XInputUsageError>

Polls the controller port given for the current controller state.

This cannot detect the “Guide” button. Use get_state_ex for that.

Notes

It is a persistent problem with xinput (since ~2007?) that polling for the data of a controller that isn’t connected will cause a long stall. In the area of 500,000 cpu cycles. That’s like 2,000 cache misses in a row.

Once a controller is detected as not being plugged in you are strongly advised to not poll for its data again next frame. Instead, you should probably only poll for one known-missing controller per frame at most.

Alternately, you can register for your app to get plug and play events and then wait for one of them to come in before you ever poll for a missing controller a second time. That’s up to you.

Errors

A few things can cause an Err value to come back, as explained by the XInputUsageError type.

Most commonly, a controller will simply not be connected. Most people don’t have all four slots plugged in all the time.

source

pub fn get_state_ex( &self, user_index: u32 ) -> Result<XInputState, XInputUsageError>

Works like get_state, but can detect the “Guide” button as well.

Failure
  • This function is technically an undocumented API. It was introduced in XInput 1.3, but may not be present in the currently loaded XInput. If it’s not available then XInputNotLoaded is returned as an Err, even when other XInput functions may be available.
source§

impl XInputHandle

source

pub fn set_state( &self, user_index: u32, left_motor_speed: u16, right_motor_speed: u16 ) -> Result<(), XInputUsageError>

Allows you to set the rumble speeds of the left and right motors.

Valid motor speeds are across the whole u16 range, and the number is the scale of the motor intensity. In other words, 0 is 0%, and 65,535 is 100%.

On a 360 controller the left motor is low-frequency and the right motor is high-frequency. On other controllers running through xinput this might be the case, or the controller might not even have rumble ability at all. If rumble is missing from the device you’ll still get Ok return values, so treat rumble as an extra, not an essential.

Errors

A few things can cause an Err value to come back, as explained by the XInputUsageError type.

Most commonly, a controller will simply not be connected. Most people don’t have all four slots plugged in all the time.

source§

impl XInputHandle

source

pub fn get_capabilities( &self, user_index: u32 ) -> Result<XINPUT_CAPABILITIES, XInputUsageError>

Retrieve the capabilities of a controller.

See the MSDN documentation for XInputGetCapabilities.

source

pub fn get_capabilities_ex( &self, user_index: u32 ) -> Result<XINPUT_CAPABILITIES_EX, XInputUsageError>

Retrieve the Extended capabilities of a controller.

Undocumented!! This isn’t part of the official XInput API, but is often available.

Failure
  • This function is technically an undocumented API. If it’s not available then XInputNotLoaded is returned as an Err, even when other XInput functions may be available.
source

pub fn get_keystroke( &self, user_index: u32 ) -> Result<Option<XINPUT_KEYSTROKE>, XInputOptionalFnUsageError>

Retrieve a gamepad input event.

See the MSDN documentation for XInputGetKeystroke.

source§

impl XInputHandle

source

pub fn get_gamepad_battery_information( &self, user_index: u32 ) -> Result<XInputBatteryInformation, XInputOptionalFnUsageError>

Get battery type and charge level of a gamepad.

See also XInputGetBatteryInformation

source

pub fn get_headset_battery_information( &self, user_index: u32 ) -> Result<XInputBatteryInformation, XInputOptionalFnUsageError>

Get battery type and charge level of a headset.

See also XInputGetBatteryInformation

Trait Implementations§

source§

impl Clone for XInputHandle

source§

fn clone(&self) -> XInputHandle

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for XInputHandle

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Send for XInputHandle

source§

impl Sync for XInputHandle

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.