use crate::*;
use bytemuck::Zeroable;
pub fn get_capabilities(user_index: impl TryInto<u32>, flags: Flag) -> Result<Capabilities, Error> {
fn_context!(xinput::get_capabilities => XInputGetCapabilities);
#[allow(non_snake_case)] let XInputGetCapabilities = imports::XInputGetCapabilities.load(core::sync::atomic::Ordering::Relaxed);
let user_index = user_index.try_into().map_err(|_| fn_param_error!(user_index, error::BAD_ARGUMENTS))?;
let mut caps = Capabilities::zeroed();
let code = unsafe { XInputGetCapabilities(user_index, flags.into(), caps.as_mut()) };
check_success!(code)?;
Ok(caps)
}
#[test] fn test_valid_params() {
for user_index in 0 .. 4 {
for flag in [Flag::None, Flag::Gamepad] {
if let Err(err) = get_capabilities(user_index, flag) {
assert!(matches!(err.kind(), error::DEVICE_NOT_CONNECTED | error::CO_E_NOTINITIALIZED), "unexpected error type: {err:?}");
}
}
}
}
#[test] fn test_bad_user_index() {
for user_index in xuser::invalids().chain(Some(xuser::INDEX_ANY)) {
for flag in [Flag::None, Flag::Gamepad, Flag::from_unchecked(42), Flag::from_unchecked(!0)] {
let err = get_capabilities(user_index, flag).expect_err("get_capabilities should return an error on a bad user_index");
assert!(matches!(err.kind(), error::BAD_ARGUMENTS | error::CO_E_NOTINITIALIZED), "unexpected error type: {err:?}");
}
}
}
#[test] fn test_bad_flags() {
for user_index in 0 .. 4 {
for flag in [Flag::from_unchecked(42), Flag::from_unchecked(!0)] {
let err = get_capabilities(user_index, flag).expect_err("get_capabilities should return an error on a bad flag");
assert!(matches!(err.kind(), error::BAD_ARGUMENTS | error::CO_E_NOTINITIALIZED), "unexpected error type: {err:?}");
}
}
}