Support openxr hmd volume buttons (#731)

* move pactl_wrapper to wlx-common

* add bindings for hmd volume buttons

* rustfmt
This commit is contained in:
Magnus Petz authored and GitHub committed 2026-11-14 21:10:15 +09:00
1 parent e21fe11f81
commit b61486c4c9
13 files changed
+208 -60

No files matched your search

-1
View File
@@ -1,7 +1,6 @@
pub mod cached_fetcher;
pub mod networking;
pub mod openxr_bindings;
pub mod pactl_wrapper;
pub mod popup_manager;
pub mod steam_utils;
pub mod toast_manager;
+2 -5
View File
@@ -1,5 +1,6 @@
use std::rc::Rc;
use crate::frontend::{FrontendTask, FrontendTasks};
use wgui::{
assets::{AssetPathRef, AssetPathSource},
color::{WguiColor, WguiColorName},
@@ -16,11 +17,7 @@ use wgui::{
task::Tasks,
widget::{ConstructEssentials, rectangle::WidgetRectangle},
};
use crate::{
frontend::{FrontendTask, FrontendTasks},
util::pactl_wrapper::{self},
};
use wlx_common::pactl_wrapper::{self};
#[derive(Clone)]
#[allow(clippy::large_enum_variant)]
+19
View File
@@ -77,6 +77,13 @@ pub struct InputState {
disable_lerp_until: Instant,
pub head_yaw_total: f64,
head_last_yaw: Option<f64>,
pub head: HeadInput,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct HeadInput {
pub now: PointerState,
pub before: PointerState,
}
impl InputState {
@@ -93,9 +100,18 @@ impl InputState {
disable_lerp_until: Instant::now(),
head_yaw_total: 0.0,
head_last_yaw: None,
head: HeadInput::default(),
}
}
pub fn just_pressed(&self, action: impl Fn(&PointerState) -> bool) -> bool {
self.pointers
.iter()
.map(|p| (&p.now, &p.before))
.chain(std::iter::once((&self.head.now, &self.head.before)))
.any(|(now, before)| action(now) && !action(before))
}
pub fn should_disable_lerp(&self) -> bool {
self.disable_lerp_until > Instant::now()
}
@@ -190,6 +206,7 @@ impl InputState {
pub const fn pre_update(&mut self) {
self.pointers[0].before = self.pointers[0].now;
self.pointers[1].before = self.pointers[1].now;
self.head.before = self.head.now;
}
pub fn post_update(&mut self, session: &AppSession) {
@@ -396,6 +413,8 @@ pub struct PointerState {
pub click_modifier_right: bool,
pub click_modifier_middle: bool,
pub move_mouse: bool,
pub volume_up: bool,
pub volume_down: bool,
}
#[derive(Debug, Clone, Copy, Default)]
+2
View File
@@ -14,6 +14,7 @@ use smallvec::smallvec;
use wgui::i18n::Translation;
use wlx_common::{dash_interface::InterfaceFeats, overlays::ToastTopic};
use crate::system::SystemTaskManager;
use crate::{
Args, FRAME_COUNTER, RUNNING, XrBackend,
backend::{
@@ -260,6 +261,7 @@ pub fn openvr_run(args: &Args, params: RunParams) -> Result<(), BackendError> {
TaskType::Input(task) => {
app.input_state.handle_task(task);
}
TaskType::System(task) => SystemTaskManager::handle_task(task)?,
TaskType::Overlay(task) => {
overlays.handle_task(&mut app, task)?;
}
+94 -42
View File
@@ -16,6 +16,8 @@ use wlx_common::{
openxr_bindings_schema::DEFAULT_BUTTON_THRESHOLDS,
};
use super::{XrState, helpers::posef_to_transform};
use crate::backend::input::HeadInput;
use crate::{
backend::input::{
Haptics, InputState, Pointer, PointerState, TrackedDevice, TrackedDeviceRole,
@@ -23,8 +25,6 @@ use crate::{
state::{AppSession, AppState},
};
use super::{XrState, helpers::posef_to_transform};
static CLICK_TIMES: [Duration; 3] = [
Duration::ZERO,
Duration::from_millis(500),
@@ -36,6 +36,7 @@ pub(super) struct OpenXrInputSource {
physical_inputs: Vec<PhysicalInput>,
pointers: [OpenXrPointer; 2],
handsfree_pointer: OpenXrPointer,
head: OpenXrPointer,
}
pub(super) struct OpenXrPointer {
@@ -251,6 +252,8 @@ pub(super) struct OpenXrHandSource {
move_mouse: CustomClickAction,
scroll: xr::Action<Vector2f>,
haptics: xr::Action<xr::Haptic>,
volume_up: CustomClickAction,
volume_down: CustomClickAction,
}
impl OpenXrInputSource {
@@ -263,13 +266,18 @@ impl OpenXrInputSource {
let mut left_source = OpenXrHandSource::new(&mut action_set, "left")?;
let mut right_source = OpenXrHandSource::new(&mut action_set, "right")?;
let mut fallback_source = OpenXrHandSource::new(&mut action_set, "handsfree")?;
let mut head_source = OpenXrHandSource::new(&mut action_set, "head")?;
let profiles = load_xr_input_profiles();
let (physical_inputs, physical_input_map) =
create_physical_inputs(&action_set, &xr.instance, &profiles)?;
let mut hands: [&mut OpenXrHandSource; 3] =
[&mut left_source, &mut right_source, &mut fallback_source];
let mut hands: [&mut OpenXrHandSource; 4] = [
&mut left_source,
&mut right_source,
&mut fallback_source,
&mut head_source,
];
suggest_bindings(
&xr.instance,
&mut hands,
@@ -288,6 +296,7 @@ impl OpenXrInputSource {
OpenXrPointer::new(xr, right_source)?,
],
handsfree_pointer: OpenXrPointer::new(xr, fallback_source)?,
head: OpenXrPointer::new(xr, head_source)?,
})
}
@@ -314,6 +323,9 @@ impl OpenXrInputSource {
}
let physical_inputs = &self.physical_inputs;
self.head
.update_head(&mut app.input_state.head, &xr, physical_inputs)?;
app.input_state.eye_gaze = if xr.extra_exts.ext_eye_gaze_interaction {
self.handsfree_pointer.locate_tracked_pose(xr)?
} else {
@@ -548,7 +560,7 @@ impl OpenXrPointer {
return Ok(());
}
self.pointer_load_actions(pointer, xr, physical_inputs)?;
self.pointer_load_actions(&mut pointer.now, xr, physical_inputs)?;
pointer.now.click_modifier_right = handsfree_state.click_modifier_right;
pointer.now.click_modifier_middle = handsfree_state.click_modifier_middle;
pointer.now.scroll_y = handsfree_state.scroll_y;
@@ -574,11 +586,21 @@ impl OpenXrPointer {
None
},
)?;
self.pointer_load_actions(pointer, xr, physical_inputs)?;
self.pointer_load_actions(&mut pointer.now, xr, physical_inputs)?;
Ok(())
}
pub(super) fn update_head(
&mut self,
head: &mut HeadInput,
xr: &XrState,
physical_inputs: &[PhysicalInput],
) -> anyhow::Result<()> {
self.pointer_load_actions(&mut head.now, xr, physical_inputs)?;
Ok(())
}
fn pointer_load_pose(
&mut self,
pointer: &mut Pointer,
@@ -618,13 +640,13 @@ impl OpenXrPointer {
fn pointer_load_actions(
&mut self,
pointer: &mut Pointer,
pointer_state: &mut PointerState,
xr: &XrState,
physical_inputs: &[PhysicalInput],
) -> anyhow::Result<()> {
pointer.now.click = self.source.click.state(physical_inputs);
pointer_state.click = self.source.click.state(physical_inputs);
pointer.now.grab = self.source.grab.state(physical_inputs);
pointer_state.grab = self.source.grab.state(physical_inputs);
let scroll = self
.source
@@ -632,26 +654,30 @@ impl OpenXrPointer {
.state(&xr.session, xr::Path::NULL)?
.current_state;
pointer.now.scroll_x = scroll.x;
pointer.now.scroll_y = scroll.y;
pointer_state.scroll_x = scroll.x;
pointer_state.scroll_y = scroll.y;
pointer.now.alt_click = self.source.alt_click.state(physical_inputs);
pointer_state.alt_click = self.source.alt_click.state(physical_inputs);
pointer.now.show_hide = self.source.show_hide.state(physical_inputs);
pointer_state.show_hide = self.source.show_hide.state(physical_inputs);
pointer.now.click_modifier_right = self.source.modifier_right.state(physical_inputs);
pointer_state.click_modifier_right = self.source.modifier_right.state(physical_inputs);
pointer.now.toggle_dashboard = self.source.toggle_dashboard.state(physical_inputs);
pointer_state.toggle_dashboard = self.source.toggle_dashboard.state(physical_inputs);
pointer.now.click_modifier_middle = self.source.modifier_middle.state(physical_inputs);
pointer_state.click_modifier_middle = self.source.modifier_middle.state(physical_inputs);
pointer.now.move_mouse = self.source.move_mouse.state(physical_inputs);
pointer_state.move_mouse = self.source.move_mouse.state(physical_inputs);
pointer.now.space_drag = self.source.space_drag.state(physical_inputs);
pointer_state.space_drag = self.source.space_drag.state(physical_inputs);
pointer.now.space_rotate = self.source.space_rotate.state(physical_inputs);
pointer_state.space_rotate = self.source.space_rotate.state(physical_inputs);
pointer.now.space_reset = self.source.space_reset.state(physical_inputs);
pointer_state.space_reset = self.source.space_reset.state(physical_inputs);
pointer_state.volume_up = self.source.volume_up.state(physical_inputs);
pointer_state.volume_down = self.source.volume_down.state(physical_inputs);
Ok(())
}
@@ -692,6 +718,8 @@ impl OpenXrHandSource {
modifier_middle: CustomClickAction::default(),
move_mouse: CustomClickAction::default(),
haptics: action_haptics,
volume_up: CustomClickAction::default(),
volume_down: CustomClickAction::default(),
})
}
}
@@ -723,7 +751,7 @@ fn for_each_path(spec: Option<&OneOrMany<String>>, mut f: impl FnMut(&str)) {
}
}
const fn button_actions(profile: &OpenXrInputProfile) -> [Option<&OpenXrInputAction>; 11] {
const fn button_actions(profile: &OpenXrInputProfile) -> [Option<&OpenXrInputAction>; 13] {
[
profile.click.as_ref(),
profile.alt_click.as_ref(),
@@ -736,6 +764,8 @@ const fn button_actions(profile: &OpenXrInputProfile) -> [Option<&OpenXrInputAct
profile.click_modifier_right.as_ref(),
profile.click_modifier_middle.as_ref(),
profile.move_mouse.as_ref(),
profile.volume_up.as_ref(),
profile.volume_down.as_ref(),
]
}
@@ -744,6 +774,7 @@ fn action_spec(action: &OpenXrInputAction, side: usize) -> Option<&OneOrMany<Str
0 => action.left.as_ref(),
1 => action.right.as_ref(),
2 => action.handsfree.as_ref(),
3 => action.head.as_ref(),
_ => unreachable!(),
}
}
@@ -752,7 +783,7 @@ fn action_chord(action: &OpenXrInputAction, side: usize) -> Option<&[OpenXrInput
match side {
0 => action.left_chord.as_deref(),
1 => action.right_chord.as_deref(),
2 => None,
2 | 3 => None,
_ => unreachable!(),
}
}
@@ -761,7 +792,7 @@ fn action_threshold(action: &OpenXrInputAction, side: usize) -> [f32; 2] {
match side {
0 => action.threshold_left,
1 => action.threshold_right,
2 => None,
2 | 3 => None,
_ => unreachable!(),
}
.unwrap_or(DEFAULT_BUTTON_THRESHOLDS)
@@ -809,7 +840,7 @@ fn create_physical_inputs(
};
for action in button_actions(profile).into_iter().flatten() {
for side in 0..3 {
for side in 0..4 {
for_each_path(action_spec(action, side), &mut add_path);
}
@@ -915,7 +946,7 @@ fn build_button_bindings(
fn add_button_bindings(
action: Option<&OpenXrInputAction>,
profile_index: usize,
hands: &mut [&mut OpenXrHandSource; 3],
sources: &mut [&mut OpenXrHandSource; 4],
physical_input_map: &PhysicalInputMap,
field: fn(&mut OpenXrHandSource) -> &mut CustomClickAction,
) {
@@ -923,9 +954,9 @@ fn add_button_bindings(
return;
};
for (side, hand) in hands.iter_mut().enumerate() {
for (side, source) in sources.iter_mut().enumerate() {
for binding in build_button_bindings(action, side, profile_index, physical_input_map) {
field(hand).add_binding(binding);
field(source).add_binding(binding);
}
}
}
@@ -938,6 +969,7 @@ macro_rules! add_direct_bindings {
0 => action.left.as_ref(),
1 => action.right.as_ref(),
2 => action.handsfree.as_ref(),
3 => action.head.as_ref(),
_ => unreachable!(),
};
@@ -954,7 +986,7 @@ macro_rules! add_direct_bindings {
#[allow(clippy::too_many_lines)]
fn suggest_bindings(
instance: &xr::Instance,
hands: &mut [&mut OpenXrHandSource; 3],
sources: &mut [&mut OpenXrHandSource; 4],
profiles: &[OpenXrInputProfile],
physical_inputs: &[PhysicalInput],
physical_input_map: &PhysicalInputMap,
@@ -971,86 +1003,106 @@ fn suggest_bindings(
add_button_bindings(
profile.click.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.click,
);
add_button_bindings(
profile.alt_click.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.alt_click,
);
add_button_bindings(
profile.grab.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.grab,
);
add_button_bindings(
profile.show_hide.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.show_hide,
);
add_button_bindings(
profile.toggle_dashboard.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.toggle_dashboard,
);
add_button_bindings(
profile.space_drag.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.space_drag,
);
add_button_bindings(
profile.space_rotate.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.space_rotate,
);
add_button_bindings(
profile.space_reset.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.space_reset,
);
add_button_bindings(
profile.click_modifier_right.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.modifier_right,
);
add_button_bindings(
profile.click_modifier_middle.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.modifier_middle,
);
add_button_bindings(
profile.move_mouse.as_ref(),
profile_index,
hands,
sources,
physical_input_map,
|hand| &mut hand.move_mouse,
);
add_button_bindings(
profile.volume_up.as_ref(),
profile_index,
sources,
physical_input_map,
|hand| &mut hand.volume_up,
);
add_button_bindings(
profile.volume_down.as_ref(),
profile_index,
sources,
physical_input_map,
|hand| &mut hand.volume_down,
);
let mut bindings: Vec<xr::Binding> = vec![];
add_direct_bindings!(profile.pose.as_ref(), pose, hands, bindings, instance);
add_direct_bindings!(profile.haptic.as_ref(), haptics, hands, bindings, instance);
add_direct_bindings!(profile.scroll.as_ref(), scroll, hands, bindings, instance);
add_direct_bindings!(profile.pose.as_ref(), pose, sources, bindings, instance);
add_direct_bindings!(
profile.haptic.as_ref(),
haptics,
sources,
bindings,
instance
);
add_direct_bindings!(profile.scroll.as_ref(), scroll, sources, bindings, instance);
// 1 physical source → 1 action per interaction profile
for input in physical_inputs
+15 -12
View File
@@ -15,6 +15,8 @@ use wlx_common::{
overlays::{StereoMode, ToastTopic},
};
use crate::backend::task::SystemTask;
use crate::system::SystemTaskManager;
use crate::{
Args, FRAME_COUNTER, RUNNING, XrBackend,
backend::{
@@ -321,25 +323,25 @@ pub fn openxr_run(args: &Args, params: RunParams) -> Result<(), BackendError> {
blocker.update(&mut app);
}
if app
.input_state
.pointers
.iter()
.any(|p| p.now.show_hide && !p.before.show_hide)
{
if app.input_state.just_pressed(|s| s.show_hide) {
overlays.show_hide(&mut app);
}
if app
.input_state
.pointers
.iter()
.any(|p| p.now.toggle_dashboard && !p.before.toggle_dashboard)
{
if app.input_state.just_pressed(|s| s.toggle_dashboard) {
app.tasks
.enqueue(TaskType::Overlay(OverlayTask::ToggleDashboard));
}
if app.input_state.just_pressed(|s| s.volume_up) {
app.tasks
.enqueue(TaskType::System(SystemTask::ChangeVolume(true)));
}
if app.input_state.just_pressed(|s| s.volume_down) {
app.tasks
.enqueue(TaskType::System(SystemTask::ChangeVolume(false)));
}
playspace_mover.update(&mut overlays, &mut app);
for o in overlays.values_mut() {
@@ -533,6 +535,7 @@ pub fn openxr_run(args: &Args, params: RunParams) -> Result<(), BackendError> {
TaskType::Input(task) => {
app.input_state.handle_task(task);
}
TaskType::System(task) => SystemTaskManager::handle_task(task)?,
TaskType::Overlay(task) => {
overlays.handle_task(&mut app, task)?;
}
+6
View File
@@ -150,10 +150,16 @@ pub enum OverlayTask {
pub type GlobalTask = dyn FnOnce(&mut AppState) + Send;
pub enum SystemTask {
/// Change the volume: true = increase; false = decrease
ChangeVolume(bool),
}
#[allow(dead_code)]
pub enum TaskType {
Global(Box<GlobalTask>),
Input(InputTask),
System(SystemTask),
Overlay(OverlayTask),
Playspace(PlayspaceTask),
#[cfg(feature = "openvr")]
+1
View File
@@ -29,6 +29,7 @@ mod overlays;
mod shaders;
mod state;
mod subsystem;
pub mod system;
mod test;
mod windowing;
+26
View File
@@ -0,0 +1,26 @@
use crate::backend::task::SystemTask;
/// Volume change per HMD button click
const VOLUME_STEP: f32 = 0.05;
pub struct SystemTaskManager;
impl SystemTaskManager {
pub fn handle_task(task: SystemTask) -> anyhow::Result<()> {
match task {
SystemTask::ChangeVolume(increase) => {
Self::update_volume(if increase { VOLUME_STEP } else { -VOLUME_STEP })
}
}
Ok(())
}
fn update_volume(delta: f32) {
// pactl is a subprocess; keep it off the frame loop.
std::thread::spawn(move || {
if let Err(e) = wlx_common::pactl_wrapper::add_default_sink_volume(delta) {
log::warn!("Could not change volume: {e:?}");
}
});
}
}
+19
View File
@@ -30,6 +30,10 @@
// -- move_mouse --
// when using `focus_follows_mouse_mode`, you need to hold this for the mouse to move
//
// -- volume_up --
// -- volume_down --
// raise / lower the volume of the default audio output (e.g. headset volume buttons)
//
// -- pose, haptic --
// do not mess with these, unless you know what you're doing
//
@@ -37,6 +41,10 @@
// which side of the controller to drag with, click with, apply the modifier to, etc.
//
// for side-agnostic actions like show-hide, the side doesn't matter
//
// `handsfree` feeds the head/eye-driven pointer, which is only used while no controller is tracked.
// `head` is for buttons on the headset itself. it has no pointer, so only side-agnostic actions
// (show_hide, toggle_dashboard, volume_up, volume_down) do anything there.
[
// Eye+hand interaction
@@ -57,6 +65,17 @@
},
},
// Headset volume buttons
{
profile: "/interaction_profiles/htc/vive_pro",
volume_up: {
head: "/user/head/input/volume_up/click",
},
volume_down: {
head: "/user/head/input/volume_down/click",
},
},
// Fallback controller, intended for testing
//{
// profile: "/interaction_profiles/khr/simple_controller",
+1
View File
@@ -17,6 +17,7 @@ pub mod openxr_actions;
pub mod openxr_bindings_schema;
pub mod openxr_controller_profiles;
pub mod overlays;
pub mod pactl_wrapper;
pub mod palette;
pub mod timestep;
pub mod windowing;
+13
View File
@@ -46,6 +46,8 @@ pub struct OpenXrInputAction {
#[serde(skip_serializing_if = "Option::is_none")]
pub handsfree: Option<OneOrMany<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub head: Option<OneOrMany<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub left_chord: Option<Vec<OpenXrInputChordMember>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub right_chord: Option<Vec<OpenXrInputChordMember>>,
@@ -90,6 +92,10 @@ pub struct OpenXrInputProfile {
pub scroll: Option<OpenXrInputAction>,
#[serde(skip_serializing_if = "Option::is_none")]
pub haptic: Option<OpenXrInputAction>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_up: Option<OpenXrInputAction>,
#[serde(skip_serializing_if = "Option::is_none")]
pub volume_down: Option<OpenXrInputAction>,
}
fn strip_axis_component(path: &str) -> String {
@@ -146,6 +152,12 @@ fn normalize_profile(profile: &mut OpenXrInputProfile) {
if let Some(action) = profile.haptic.as_mut() {
normalize_action(action);
}
if let Some(action) = profile.volume_up.as_mut() {
normalize_action(action);
}
if let Some(action) = profile.volume_down.as_mut() {
normalize_action(action);
}
}
fn normalize_action(action: &mut OpenXrInputAction) {
@@ -173,6 +185,7 @@ fn normalize_action(action: &mut OpenXrInputAction) {
process_one_or_many(&mut action.left);
process_one_or_many(&mut action.right);
process_one_or_many(&mut action.handsfree);
process_one_or_many(&mut action.head);
let process_chord = |members: &mut Option<Vec<OpenXrInputChordMember>>| {
if let Some(m) = members {
@@ -179,6 +179,16 @@ pub fn set_sink_volume(sink_index: u32, volume: f32) -> anyhow::Result<()> {
Ok(())
}
pub fn add_default_sink_volume(delta: f32) -> anyhow::Result<()> {
std::process::Command::new("pactl")
.arg("set-sink-volume")
.arg("@DEFAULT_SINK@")
.arg(format!("{:+}%", delta * 100.))
.output()?;
Ok(())
}
pub fn set_sink_mute(sink_index: u32, mute: bool) -> anyhow::Result<()> {
std::process::Command::new("pactl")
.arg("set-sink-mute")