use crate::*;
use bytemuck::Zeroable;
pub fn get_keystroke(user_index: impl TryInto<u32>, _reserved: ()) -> Result<Option<Keystroke>, Error> {
fn_context!(xinput::get_keystroke => XInputGetKeystroke);
#[allow(non_snake_case)] let XInputGetKeystroke = imports::XInputGetKeystroke.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 keystroke = Keystroke::zeroed();
let code = unsafe { XInputGetKeystroke(user_index, 0, keystroke.as_mut()) };
if code == winresult::ERROR::EMPTY.to_u32() { return Ok(None) }
check_success!(code)?;
Ok(Some(keystroke))
}
#[test] fn test_valid_args() {
for user_index in (0..4).chain(Some(xuser::INDEX_ANY)) {
if let Err(err) = get_keystroke(user_index, ()) {
assert!(matches!(err.kind(), error::DEVICE_NOT_CONNECTED | error::INVALID_FUNCTION | error::CO_E_NOTINITIALIZED), "unexpected error type: {err:?}");
}
}
}
#[test] fn test_bad_user_index() {
for user_index in xuser::invalids() {
let err = get_keystroke(user_index, ()).expect_err("get_keystroke should return an error for invalid users");
assert!(matches!(err.kind(), error::BAD_ARGUMENTS | error::INVALID_FUNCTION | error::CO_E_NOTINITIALIZED), "unexpected error type: {err:?}");
}
}