use sys::InputHandle_t;
use super::*;
pub struct Input {
pub(crate) input: *mut sys::ISteamInput,
pub(crate) _inner: Arc<Inner>,
}
pub enum InputType {
Unknown,
SteamController,
XBox360Controller,
XBoxOneController,
GenericGamepad,
PS4Controller,
AppleMFiController,
AndroidController,
SwitchJoyConPair,
SwitchJoyConSingle,
SwitchProController,
MobileTouch,
PS3Controller,
PS5Controller,
SteamDeckController,
}
impl Input {
pub fn init(&self, explicitly_call_run_frame: bool) -> bool {
unsafe { sys::SteamAPI_ISteamInput_Init(self.input, explicitly_call_run_frame) }
}
pub fn run_frame(&self) {
unsafe { sys::SteamAPI_ISteamInput_RunFrame(self.input, false) }
}
pub fn get_connected_controllers(&self) -> Vec<sys::InputHandle_t> {
let mut handles = vec![0_u64; sys::STEAM_INPUT_MAX_COUNT as usize];
let quantity = self.get_connected_controllers_slice(&mut handles);
handles.shrink_to(quantity);
handles
}
pub fn get_connected_controllers_slice(
&self,
mut controllers: impl AsMut<[InputHandle_t]>,
) -> usize {
let handles = controllers.as_mut();
assert!(handles.len() >= sys::STEAM_INPUT_MAX_COUNT as usize);
unsafe {
return sys::SteamAPI_ISteamInput_GetConnectedControllers(
self.input,
handles.as_mut_ptr(),
) as usize;
}
}
pub fn set_input_action_manifest_file_path(&self, path: &str) -> bool {
let path = CString::new(path).unwrap();
unsafe {
sys::SteamAPI_ISteamInput_SetInputActionManifestFilePath(self.input, path.as_ptr())
}
}
pub fn get_action_set_handle(&self, action_set_name: &str) -> sys::InputActionSetHandle_t {
let name = CString::new(action_set_name).unwrap();
unsafe { sys::SteamAPI_ISteamInput_GetActionSetHandle(self.input, name.as_ptr()) }
}
pub fn get_input_type_for_handle(&self, input_handle: sys::InputHandle_t) -> InputType {
let input_type: sys::ESteamInputType =
unsafe { sys::SteamAPI_ISteamInput_GetInputTypeForHandle(self.input, input_handle) };
match input_type {
sys::ESteamInputType::k_ESteamInputType_SteamController => InputType::SteamController,
sys::ESteamInputType::k_ESteamInputType_GenericGamepad => InputType::GenericGamepad,
sys::ESteamInputType::k_ESteamInputType_PS4Controller => InputType::PS4Controller,
sys::ESteamInputType::k_ESteamInputType_SwitchJoyConPair => InputType::SwitchJoyConPair,
sys::ESteamInputType::k_ESteamInputType_MobileTouch => InputType::MobileTouch,
sys::ESteamInputType::k_ESteamInputType_PS3Controller => InputType::PS3Controller,
sys::ESteamInputType::k_ESteamInputType_PS5Controller => InputType::PS5Controller,
sys::ESteamInputType::k_ESteamInputType_XBox360Controller => {
InputType::XBox360Controller
}
sys::ESteamInputType::k_ESteamInputType_XBoxOneController => {
InputType::XBoxOneController
}
sys::ESteamInputType::k_ESteamInputType_AppleMFiController => {
InputType::AppleMFiController
}
sys::ESteamInputType::k_ESteamInputType_AndroidController => {
InputType::AndroidController
}
sys::ESteamInputType::k_ESteamInputType_SwitchJoyConSingle => {
InputType::SwitchJoyConSingle
}
sys::ESteamInputType::k_ESteamInputType_SwitchProController => {
InputType::SwitchProController
}
sys::ESteamInputType::k_ESteamInputType_SteamDeckController => {
InputType::SteamDeckController
}
_ => InputType::Unknown,
}
}
pub fn get_glyph_for_action_origin(&self, action_origin: sys::EInputActionOrigin) -> String {
unsafe {
let glyph_path =
sys::SteamAPI_ISteamInput_GetGlyphForActionOrigin_Legacy(self.input, action_origin);
let glyph_path = CStr::from_ptr(glyph_path);
glyph_path.to_string_lossy().into_owned()
}
}
pub fn get_string_for_action_origin(&self, action_origin: sys::EInputActionOrigin) -> String {
unsafe {
let name_path =
sys::SteamAPI_ISteamInput_GetStringForActionOrigin(self.input, action_origin);
let name_path = CStr::from_ptr(name_path);
name_path.to_string_lossy().into_owned()
}
}
pub fn activate_action_set_handle(
&self,
input_handle: sys::InputHandle_t,
action_set_handle: sys::InputActionSetHandle_t,
) {
unsafe {
sys::SteamAPI_ISteamInput_ActivateActionSet(self.input, input_handle, action_set_handle)
}
}
pub fn get_digital_action_handle(&self, action_name: &str) -> sys::InputDigitalActionHandle_t {
let name = CString::new(action_name).unwrap();
unsafe { sys::SteamAPI_ISteamInput_GetDigitalActionHandle(self.input, name.as_ptr()) }
}
pub fn get_analog_action_handle(&self, action_name: &str) -> sys::InputAnalogActionHandle_t {
let name = CString::new(action_name).unwrap();
unsafe { sys::SteamAPI_ISteamInput_GetAnalogActionHandle(self.input, name.as_ptr()) }
}
pub fn get_digital_action_data(
&self,
input_handle: sys::InputHandle_t,
action_handle: sys::InputDigitalActionHandle_t,
) -> sys::InputDigitalActionData_t {
unsafe {
sys::SteamAPI_ISteamInput_GetDigitalActionData(self.input, input_handle, action_handle)
}
}
pub fn get_analog_action_data(
&self,
input_handle: sys::InputHandle_t,
action_handle: sys::InputAnalogActionHandle_t,
) -> sys::InputAnalogActionData_t {
unsafe {
sys::SteamAPI_ISteamInput_GetAnalogActionData(self.input, input_handle, action_handle)
}
}
pub fn get_digital_action_origins(
&self,
input_handle: sys::InputHandle_t,
action_set_handle: sys::InputActionSetHandle_t,
digital_action_handle: sys::InputDigitalActionHandle_t,
) -> Vec<sys::EInputActionOrigin> {
unsafe {
let mut origins = Vec::with_capacity(sys::STEAM_INPUT_MAX_ORIGINS as usize);
let len = sys::SteamAPI_ISteamInput_GetDigitalActionOrigins(
self.input,
input_handle,
action_set_handle,
digital_action_handle,
origins.as_mut_ptr(),
);
origins.set_len(len as usize);
origins
}
}
pub fn get_analog_action_origins(
&self,
input_handle: sys::InputHandle_t,
action_set_handle: sys::InputActionSetHandle_t,
analog_action_handle: sys::InputAnalogActionHandle_t,
) -> Vec<sys::EInputActionOrigin> {
unsafe {
let mut origins = Vec::with_capacity(sys::STEAM_INPUT_MAX_ORIGINS as usize);
let len = sys::SteamAPI_ISteamInput_GetAnalogActionOrigins(
self.input,
input_handle,
action_set_handle,
analog_action_handle,
origins.as_mut_ptr(),
);
origins.set_len(len as usize);
origins
}
}
pub fn get_motion_data(&self, input_handle: sys::InputHandle_t) -> sys::InputMotionData_t {
unsafe { sys::SteamAPI_ISteamInput_GetMotionData(self.input, input_handle) }
}
pub fn show_binding_panel(&self, input_handle: sys::InputHandle_t) -> bool {
unsafe { sys::SteamAPI_ISteamInput_ShowBindingPanel(self.input, input_handle) }
}
pub fn shutdown(&self) {
unsafe {
sys::SteamAPI_ISteamInput_Shutdown(self.input);
}
}
}