Skip to content

Commit

Permalink
feat(openxr): Monado playspace mover
Browse files Browse the repository at this point in the history
Co-authored-by: RinLovesYou <[email protected]>
  • Loading branch information
galister and RinLovesYou committed Jun 3, 2024
1 parent 0096267 commit 4a45683
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 2 deletions.
77 changes: 76 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ input-linux = "0.6.0"
json = { version = "0.12.4", optional = true }
json5 = "0.4.1"
libc = "0.2.153"
libloading = "0.8.3"
log = "0.4.21"
once_cell = "1.19.0"
openxr = { version = "0.17.1", features = ["linked"], optional = true }
Expand All @@ -48,6 +49,7 @@ serde_json = "1.0.113"
serde_yaml = "0.9.34"
smallvec = "1.11.0"
strum = { version = "0.26.2", features = ["derive"] }
sysinfo = { version = "0.30.0", optional = true }
thiserror = "1.0.56"
vulkano = { git = "https://github.com/vulkano-rs/vulkano", rev = "94f50f1" }
vulkano-shaders = { git = "https://github.com/vulkano-rs/vulkano", rev = "94f50f1" }
Expand All @@ -59,7 +61,7 @@ log-panics = { version = "2.1.0", features = ["with-backtrace"] }
[features]
default = ["openvr", "openxr", "osc", "x11", "wayland"]
openvr = ["dep:ovr_overlay", "dep:json"]
openxr = ["dep:openxr"]
openxr = ["dep:openxr", "dep:sysinfo"]
osc = ["dep:rosc"]
x11 = ["wlx-capture/xshm"]
wayland = ["wlx-capture/pipewire", "wlx-capture/wlr"]
Expand Down
44 changes: 44 additions & 0 deletions src/backend/openxr/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::path::PathBuf;

use anyhow::{bail, ensure};
use glam::{Affine3A, Quat, Vec3, Vec3A};
use openxr as xr;
use sysinfo::Process;
use xr::OverlaySessionCreateFlagsEXTX;

pub(super) fn init_xr() -> Result<(xr::Instance, xr::SystemId), anyhow::Error> {
Expand Down Expand Up @@ -169,3 +172,44 @@ pub(super) fn transform_to_posef(transform: &Affine3A) -> xr::Posef {
let rotation = transform_to_norm_quat(transform);
translation_rotation_to_posef(translation, rotation)
}

pub(super) fn find_libmonado() -> anyhow::Result<libloading::Library> {
//check env var first
if let Ok(path) = std::env::var("LIBMONADO_PATH") {
let path = PathBuf::from(path);
if path.exists() {
return Ok(unsafe { libloading::Library::new(path)? });
} else {
bail!("LIBMONADO_PATH points to a non-existing file.");
}
}

const PROC_NAMES: [&str; 1] = ["monado-service"];
let mut system = sysinfo::System::new();
system.refresh_processes();
for p in system.processes().values() {
for proc_name in PROC_NAMES.iter() {
if p.name().contains(proc_name) {
if let Some(lib) = proc_load_libmonado(p) {
return Ok(lib);
}
}
}
}
bail!("Could not find libmonado.");
}

fn proc_load_libmonado(proc: &Process) -> Option<libloading::Library> {
let path = proc
.exe()?
.parent()?
.parent()?
.join("lib")
.join("libmonado.so");

if path.exists() {
Some(unsafe { libloading::Library::new(path).ok()? })
} else {
None
}
}
21 changes: 21 additions & 0 deletions src/backend/openxr/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub(super) struct OpenXrHandSource {
action_scroll: xr::Action<f32>,
action_alt_click: xr::Action<f32>,
action_show_hide: xr::Action<bool>,
action_space_drag: xr::Action<bool>,
action_click_modifier_right: xr::Action<bool>,
action_click_modifier_middle: xr::Action<bool>,
action_move_mouse: xr::Action<bool>,
Expand Down Expand Up @@ -201,6 +202,12 @@ impl OpenXrHand {
.state(&xr.session, xr::Path::NULL)?
.current_state;

pointer.now.space_drag = self
.source
.action_space_drag
.state(&xr.session, xr::Path::NULL)?
.current_state;

Ok(())
}
}
Expand Down Expand Up @@ -259,6 +266,11 @@ impl OpenXrHandSource {
&format!("{} hand haptics", side),
&[],
)?;
let action_space_drag = action_set.create_action::<bool>(
&format!("{}_space_drag", side),
&format!("{} hand space drag", side),
&[],
)?;

Ok(Self {
action_pose,
Expand All @@ -271,6 +283,7 @@ impl OpenXrHandSource {
action_click_modifier_middle,
action_move_mouse,
action_haptics,
action_space_drag,
})
}
}
Expand Down Expand Up @@ -380,6 +393,10 @@ fn suggest_bindings(
&hands[1].action_move_mouse,
instance.string_to_path("/user/hand/right/input/trigger/touch")?,
),
xr::Binding::new(
&hands[0].action_space_drag,
instance.string_to_path("/user/hand/left/input/menu/click")?,
),
xr::Binding::new(
&hands[0].action_haptics,
instance.string_to_path("/user/hand/left/output/haptic")?,
Expand Down Expand Up @@ -439,6 +456,10 @@ fn suggest_bindings(
&hands[0].action_show_hide,
instance.string_to_path("/user/hand/left/input/b/click")?,
),
xr::Binding::new(
&hands[1].action_space_drag,
instance.string_to_path("/user/hand/right/input/b/click")?,
),
xr::Binding::new(
&hands[0].action_click_modifier_right,
instance.string_to_path("/user/hand/left/input/b/touch")?,
Expand Down
7 changes: 7 additions & 0 deletions src/backend/openxr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod helpers;
mod input;
mod lines;
mod overlay;
mod playspace;
mod swapchain;

const VIEW_TYPE: xr::ViewConfigurationType = xr::ViewConfigurationType::PRIMARY_STEREO;
Expand Down Expand Up @@ -72,6 +73,9 @@ pub fn openxr_run(running: Arc<AtomicBool>) -> Result<(), BackendError> {
notifications.run_udp();

let mut delete_queue = vec![];
let mut space_mover = playspace::PlayspaceMover::try_new()
.map_err(|e| log::warn!("Failed to initialize Monado playspace mover: {}", e))
.ok();

#[cfg(feature = "osc")]
let mut osc_sender =
Expand Down Expand Up @@ -204,6 +208,9 @@ pub fn openxr_run(running: Arc<AtomicBool>) -> Result<(), BackendError> {
}

watch_fade(&mut app_state, overlays.mut_by_id(watch_id).unwrap()); // want panic
if let Some(ref mut space_mover) = space_mover {
space_mover.update(&mut overlays, &app_state);
}

for o in overlays.iter_mut() {
o.after_input(&mut app_state)?;
Expand Down
Loading

0 comments on commit 4a45683

Please sign in to comment.