improve error handling

This commit is contained in:
galister
2024-02-18 22:24:36 +01:00
parent 615470aa92
commit 63b43c0e59
17 changed files with 517 additions and 611 deletions

View File

@@ -1,5 +1,8 @@
use glam::Affine3A;
use ovr_overlay::{pose::Matrix3x4, sys::HmdMatrix34_t};
use thiserror::Error;
use crate::backend::common::BackendError;
pub trait Affine3AConvert {
fn from_affine(affine: Affine3A) -> Self;
@@ -75,3 +78,21 @@ impl Affine3AConvert for HmdMatrix34_t {
])
}
}
#[derive(Error, Debug)]
pub(super) enum OVRError {
#[error("ovr input error: {0}")]
InputError(&'static str),
}
impl From<ovr_overlay::errors::EVRInputError> for OVRError {
fn from(e: ovr_overlay::errors::EVRInputError) -> Self {
OVRError::InputError(e.description())
}
}
impl From<OVRError> for BackendError {
fn from(e: OVRError) -> Self {
BackendError::Fatal(anyhow::Error::new(e))
}
}

View File

@@ -17,16 +17,7 @@ use crate::{
state::AppState,
};
use super::helpers::Affine3AConvert;
macro_rules! result_str {
( $e:expr ) => {
match $e {
Ok(x) => Ok(x),
Err(y) => Err(y.description()),
}
};
}
use super::helpers::{Affine3AConvert, OVRError};
const SET_DEFAULT: &str = "/actions/default";
const INPUT_SOURCES: [&str; 2] = ["/user/hand/left", "/user/hand/right"];
@@ -71,34 +62,32 @@ pub(super) struct OpenVrHandSource {
}
impl OpenVrInputSource {
pub fn new(input: &mut InputManager) -> Result<Self, &'static str> {
let set_hnd = result_str!(input.get_action_set_handle(SET_DEFAULT))?;
pub fn new(input: &mut InputManager) -> Result<Self, OVRError> {
let set_hnd = input.get_action_set_handle(SET_DEFAULT)?;
let click_hnd = result_str!(input.get_action_handle(PATH_CLICK))?;
let grab_hnd = result_str!(input.get_action_handle(PATH_GRAB))?;
let scroll_hnd = result_str!(input.get_action_handle(PATH_SCROLL))?;
let alt_click_hnd = result_str!(input.get_action_handle(PATH_ALT_CLICK))?;
let show_hide_hnd = result_str!(input.get_action_handle(PATH_SHOW_HIDE))?;
let space_drag_hnd = result_str!(input.get_action_handle(PATH_SPACE_DRAG))?;
let click_modifier_right_hnd =
result_str!(input.get_action_handle(PATH_CLICK_MODIFIER_RIGHT))?;
let click_modifier_middle_hnd =
result_str!(input.get_action_handle(PATH_CLICK_MODIFIER_MIDDLE))?;
let click_hnd = input.get_action_handle(PATH_CLICK)?;
let grab_hnd = input.get_action_handle(PATH_GRAB)?;
let scroll_hnd = input.get_action_handle(PATH_SCROLL)?;
let alt_click_hnd = input.get_action_handle(PATH_ALT_CLICK)?;
let show_hide_hnd = input.get_action_handle(PATH_SHOW_HIDE)?;
let space_drag_hnd = input.get_action_handle(PATH_SPACE_DRAG)?;
let click_modifier_right_hnd = input.get_action_handle(PATH_CLICK_MODIFIER_RIGHT)?;
let click_modifier_middle_hnd = input.get_action_handle(PATH_CLICK_MODIFIER_MIDDLE)?;
let input_hnd: Vec<InputValueHandle> = INPUT_SOURCES
.iter()
.map(|path| Ok(result_str!(input.get_input_source_handle(path))?))
.collect::<Result<_, &'static str>>()?;
.map(|path| Ok((input.get_input_source_handle(path))?))
.collect::<Result<_, OVRError>>()?;
let pose_hnd: Vec<ActionHandle> = PATH_POSES
.iter()
.map(|path| Ok(result_str!(input.get_action_handle(path))?))
.collect::<Result<_, &'static str>>()?;
.map(|path| Ok((input.get_action_handle(path))?))
.collect::<Result<_, OVRError>>()?;
let haptics_hnd: Vec<ActionHandle> = PATH_HAPTICS
.iter()
.map(|path| Ok(result_str!(input.get_action_handle(path))?))
.collect::<Result<_, &'static str>>()?;
.map(|path| Ok((input.get_action_handle(path))?))
.collect::<Result<_, OVRError>>()?;
let hands: [OpenVrHandSource; 2] = array::from_fn(|i| OpenVrHandSource {
has_pose: false,
@@ -317,38 +306,29 @@ pub fn set_action_manifest(input: &mut InputManager) -> anyhow::Result<()> {
let action_path = CONFIG_ROOT_PATH.join("actions.json");
if !action_path.is_file() {
File::create(&action_path)
.unwrap()
.write_all(include_bytes!("../../res/actions.json"))
.unwrap();
File::create(&action_path)?.write_all(include_bytes!("../../res/actions.json"))?;
}
let binding_path = CONFIG_ROOT_PATH.join("actions_binding_knuckles.json");
if !binding_path.is_file() {
File::create(&binding_path)
.unwrap()
.write_all(include_bytes!("../../res/actions_binding_knuckles.json"))
.unwrap();
File::create(&binding_path)?
.write_all(include_bytes!("../../res/actions_binding_knuckles.json"))?;
}
let binding_path = CONFIG_ROOT_PATH.join("actions_binding_vive.json");
if !binding_path.is_file() {
File::create(&binding_path)
.unwrap()
.write_all(include_bytes!("../../res/actions_binding_vive.json"))
.unwrap();
File::create(&binding_path)?
.write_all(include_bytes!("../../res/actions_binding_vive.json"))?;
}
let binding_path = CONFIG_ROOT_PATH.join("actions_binding_oculus.json");
if !binding_path.is_file() {
File::create(&binding_path)
.unwrap()
.write_all(include_bytes!("../../res/actions_binding_oculus.json"))
.unwrap();
File::create(&binding_path)?
.write_all(include_bytes!("../../res/actions_binding_oculus.json"))?;
}
if let Err(e) = input.set_action_manifest(action_path.as_path()) {
bail!("Failed to set action manifest: {}", e.description());
bail!("Failed to set action manifest: {}", e);
}
Ok(())
}

View File

@@ -7,6 +7,7 @@ use std::{
time::{Duration, Instant},
};
use anyhow::{anyhow, Result};
use ovr_overlay::{
sys::{ETrackedDeviceProperty, EVRApplicationType, EVREventType},
TrackedDeviceIndex,
@@ -83,7 +84,7 @@ pub fn openvr_run(running: Arc<AtomicBool>) -> Result<(), BackendError> {
let mut state = {
let graphics = WlxGraphics::new_openvr(instance_extensions, device_extensions_fn);
AppState::from_graphics(graphics)
AppState::from_graphics(graphics)?
};
install_manifest(&mut app_mgr);
@@ -97,22 +98,17 @@ pub fn openvr_run(running: Arc<AtomicBool>) -> Result<(), BackendError> {
state.hid_provider.set_desktop_extent(overlays.extent);
if let Err(e) = set_action_manifest(&mut input_mngr) {
log::error!("{}", e.to_string());
return Err(BackendError::Fatal);
};
set_action_manifest(&mut input_mngr)?;
let Ok(mut input_source) = OpenVrInputSource::new(&mut input_mngr) else {
log::error!("Failed to initialize input");
return Err(BackendError::Fatal);
};
let mut input_source = OpenVrInputSource::new(&mut input_mngr)?;
let Ok(refresh_rate) = system_mngr.get_tracked_device_property::<f32>(
TrackedDeviceIndex::HMD,
ETrackedDeviceProperty::Prop_DisplayFrequency_Float,
) else {
log::error!("Failed to get display refresh rate");
return Err(BackendError::Fatal);
return Err(BackendError::Fatal(anyhow!(
"Failed to get HMD refresh rate"
)));
};
log::info!("HMD running @ {} Hz", refresh_rate);

View File

@@ -92,7 +92,7 @@ impl OverlayData<OpenVrOverlayData> {
};
log::debug!("{}: show", self.state.name);
if let Err(e) = overlay.set_visibility(handle, true) {
panic!("Failed to show overlay: {}", e);
log::error!("{}: Failed to show overlay: {}", self.state.name, e);
}
self.data.visible = true;
self.backend.resume(app);
@@ -104,7 +104,7 @@ impl OverlayData<OpenVrOverlayData> {
};
log::debug!("{}: hide", self.state.name);
if let Err(e) = overlay.set_visibility(handle, false) {
panic!("Failed to hide overlay: {}", e);
log::error!("{}: Failed to hide overlay: {}", self.state.name, e);
}
self.data.visible = false;
self.backend.pause(app);
@@ -116,7 +116,7 @@ impl OverlayData<OpenVrOverlayData> {
return;
};
if let Err(e) = overlay.set_opacity(handle, self.state.alpha) {
panic!("Failed to set overlay alpha: {}", e);
log::error!("{}: Failed to set overlay alpha: {}", self.state.name, e);
}
}
@@ -134,7 +134,7 @@ impl OverlayData<OpenVrOverlayData> {
a: self.data.color.w,
},
) {
panic!("Failed to set overlay tint: {}", e);
log::error!("{}: Failed to set overlay tint: {}", self.state.name, e);
}
}
@@ -144,7 +144,7 @@ impl OverlayData<OpenVrOverlayData> {
return;
};
if let Err(e) = overlay.set_width(handle, self.data.width) {
panic!("Failed to set overlay width: {}", e);
log::error!("{}: Failed to set overlay width: {}", self.state.name, e);
}
}
@@ -154,7 +154,11 @@ impl OverlayData<OpenVrOverlayData> {
return;
};
if let Err(e) = overlay.set_curvature(handle, self.data.curvature) {
panic!("Failed to set overlay curvature: {}", e);
log::error!(
"{}: Failed to set overlay curvature: {}",
self.state.name,
e
);
}
}
@@ -164,7 +168,7 @@ impl OverlayData<OpenVrOverlayData> {
return;
};
if let Err(e) = overlay.set_sort_order(handle, self.data.sort_order) {
panic!("Failed to set overlay z order: {}", e);
log::error!("{}: Failed to set overlay z order: {}", self.state.name, e);
}
}
@@ -181,7 +185,11 @@ impl OverlayData<OpenVrOverlayData> {
ETrackingUniverseOrigin::TrackingUniverseStanding,
&transform,
) {
panic!("Failed to set overlay transform: {}", e);
log::error!(
"{}: Failed to set overlay transform: {}",
self.state.name,
e
);
}
}
@@ -230,8 +238,7 @@ impl OverlayData<OpenVrOverlayData> {
image.usage()
);
if let Err(e) = overlay.set_image_vulkan(handle, &mut texture) {
panic!("Failed to set overlay texture: {}", e);
log::error!("{}: Failed to set overlay texture: {}", self.state.name, e);
}
log::debug!("{}: Uploaded texture", self.state.name);
}
}