Skip to content

Commit 955bcca

Browse files
committed
Refactoring from poll-detached branch
1 parent 604981f commit 955bcca

File tree

4 files changed

+152
-116
lines changed

4 files changed

+152
-116
lines changed

alvr/client_core/src/c_api.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use alvr_common::{
1515
warn, DeviceMotion, Fov, OptLazy, Pose,
1616
};
1717
use alvr_graphics::{
18-
compute_target_view_resolution, GraphicsContext, LobbyRenderer, LobbyViewParams,
18+
compute_target_view_resolution, GraphicsContext, HandData, LobbyRenderer, LobbyViewParams,
1919
StreamRenderer, StreamViewParams,
2020
};
2121
use alvr_packets::{ButtonEntry, ButtonValue, FaceData, ViewParams};
@@ -839,7 +839,18 @@ pub unsafe extern "C" fn alvr_render_lobby_opengl(
839839
if let Some(renderer) = renderer {
840840
renderer.render(
841841
view_inputs,
842-
[(None, None), (None, None)],
842+
[
843+
HandData {
844+
grip_motion: None,
845+
detached_grip_motion: None,
846+
skeleton_joints: None,
847+
},
848+
HandData {
849+
grip_motion: None,
850+
detached_grip_motion: None,
851+
skeleton_joints: None,
852+
},
853+
],
843854
None,
844855
None,
845856
None,

alvr/client_openxr/src/interaction.rs

Lines changed: 91 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use alvr_common::{
1515
glam::{Quat, Vec3},
1616
*,
1717
};
18+
use alvr_graphics::HandData;
1819
use alvr_packets::{ButtonEntry, ButtonValue, StreamConfig, ViewParams};
1920
use alvr_session::{BodyTrackingBDConfig, BodyTrackingSourcesConfig, FaceTrackingSourcesConfig};
2021
use openxr as xr;
@@ -107,13 +108,14 @@ pub struct HandInteraction {
107108
pub grip_action: xr::Action<xr::Posef>,
108109
pub grip_space: xr::Space,
109110

110-
pub detached_controller_space: Option<xr::Space>,
111-
112111
#[expect(dead_code)]
113112
pub aim_action: xr::Action<xr::Posef>,
114113
#[expect(dead_code)]
115114
pub aim_space: xr::Space,
116115

116+
pub detached_grip_action: Option<xr::Action<xr::Posef>>,
117+
pub detached_grip_space: Option<xr::Space>,
118+
117119
pub vibration_action: xr::Action<xr::Haptic>,
118120
pub skeleton_tracker: Option<xr::HandTracker>,
119121
}
@@ -304,49 +306,53 @@ impl InteractionContext {
304306
MultimodalMeta::new(xr_session.clone(), &extra_extensions, xr_system)
305307
});
306308

307-
let left_detached_controller_pose_action;
308-
let right_detached_controller_pose_action;
309-
let mut left_detached_controller_space = None;
310-
let mut right_detached_controller_space = None;
309+
let mut left_detached_grip_action = None;
310+
let mut right_detached_grip_action = None;
311+
let mut left_detached_grip_space = None;
312+
let mut right_detached_grip_space = None;
311313
if multimodal_handle.is_some() {
312314
// Note: when multimodal input is enabled, both controllers and hands will always be active.
313315
// To be able to detect when controllers are actually held, we have to register detached
314316
// controllers pose; the controller pose will be diverted to the detached controllers when
315317
// they are not held. Currently the detached controllers pose is ignored
316-
left_detached_controller_pose_action = action_set
318+
left_detached_grip_action = action_set
317319
.create_action::<xr::Posef>(
318-
"left_detached_controller_pose",
319-
"Left detached controller pose",
320+
"left_detached_grip_pose",
321+
"Left detached grip pose",
320322
&[],
321323
)
322-
.unwrap();
323-
right_detached_controller_pose_action = action_set
324+
.ok();
325+
right_detached_grip_action = action_set
324326
.create_action::<xr::Posef>(
325-
"right_detached_controller_pose",
326-
"Right detached controller pose",
327+
"right_detached_grip_pose",
328+
"Right detached grip pose",
327329
&[],
328330
)
329-
.unwrap();
331+
.ok();
330332

331-
bindings.push(binding(
332-
&left_detached_controller_pose_action,
333-
"/user/detached_controller_meta/left/input/grip/pose",
334-
));
335-
bindings.push(binding(
336-
&right_detached_controller_pose_action,
337-
"/user/detached_controller_meta/right/input/grip/pose",
338-
));
339-
340-
left_detached_controller_space = Some(
341-
left_detached_controller_pose_action
333+
if let Some(action) = &left_detached_grip_action {
334+
bindings.push(binding(
335+
action,
336+
"/user/detached_controller_meta/left/input/grip/pose",
337+
));
338+
}
339+
if let Some(action) = &right_detached_grip_action {
340+
bindings.push(binding(
341+
action,
342+
"/user/detached_controller_meta/right/input/grip/pose",
343+
));
344+
}
345+
346+
left_detached_grip_space = left_detached_grip_action.as_ref().map(|action| {
347+
action
342348
.create_space(xr_session.clone(), xr::Path::NULL, xr::Posef::IDENTITY)
343-
.unwrap(),
344-
);
345-
right_detached_controller_space = Some(
346-
right_detached_controller_pose_action
349+
.unwrap()
350+
});
351+
right_detached_grip_space = right_detached_grip_action.as_ref().map(|action| {
352+
action
347353
.create_space(xr_session.clone(), xr::Path::NULL, xr::Posef::IDENTITY)
348-
.unwrap(),
349-
);
354+
.unwrap()
355+
});
350356
}
351357

352358
// Apply bindings:
@@ -452,7 +458,8 @@ impl InteractionContext {
452458
pose_offset: get_controller_offset(platform, false),
453459
grip_action: left_grip_action,
454460
grip_space: left_grip_space,
455-
detached_controller_space: left_detached_controller_space,
461+
detached_grip_action: left_detached_grip_action,
462+
detached_grip_space: left_detached_grip_space,
456463
aim_action: left_aim_action,
457464
aim_space: left_aim_space,
458465
vibration_action: left_vibration_action,
@@ -463,7 +470,8 @@ impl InteractionContext {
463470
pose_offset: get_controller_offset(platform, true),
464471
grip_action: right_grip_action,
465472
grip_space: right_grip_space,
466-
detached_controller_space: right_detached_controller_space,
473+
detached_grip_action: right_detached_grip_action,
474+
detached_grip_space: right_detached_grip_space,
467475
aim_action: right_aim_action,
468476
aim_space: right_aim_space,
469477
vibration_action: right_vibration_action,
@@ -748,49 +756,6 @@ pub fn get_head_data(
748756
Some((motion, view_params))
749757
}
750758

751-
pub fn get_detached_controller_motion_data(
752-
xr_session: &xr::Session<xr::OpenGlEs>,
753-
reference_space: &xr::Space,
754-
time: Duration,
755-
hand_source: &HandInteraction,
756-
last_controller_pose: &mut Pose,
757-
) -> Option<DeviceMotion> {
758-
let xr_time = crate::to_xr_time(time);
759-
760-
if hand_source
761-
.grip_action
762-
.is_active(xr_session, xr::Path::NULL)
763-
.unwrap_or(false)
764-
{
765-
return None;
766-
}
767-
768-
if let Some(space) = &hand_source.detached_controller_space {
769-
if let Ok((location, velocity)) = space.relate(reference_space, xr_time) {
770-
if location
771-
.location_flags
772-
.contains(xr::SpaceLocationFlags::ORIENTATION_VALID)
773-
{
774-
last_controller_pose.orientation = crate::from_xr_quat(location.pose.orientation);
775-
}
776-
if location
777-
.location_flags
778-
.contains(xr::SpaceLocationFlags::POSITION_VALID)
779-
{
780-
last_controller_pose.position = crate::from_xr_vec3(location.pose.position);
781-
}
782-
783-
return Some(DeviceMotion {
784-
pose: *last_controller_pose * hand_source.pose_offset,
785-
linear_velocity: crate::from_xr_vec3(velocity.linear_velocity),
786-
angular_velocity: crate::from_xr_vec3(velocity.angular_velocity),
787-
});
788-
}
789-
}
790-
791-
None
792-
}
793-
794759
#[expect(clippy::too_many_arguments)]
795760
pub fn get_hand_data(
796761
xr_session: &xr::Session<xr::OpenGlEs>,
@@ -801,10 +766,10 @@ pub fn get_hand_data(
801766
hand_source: &HandInteraction,
802767
last_controller_pose: &mut Pose,
803768
last_palm_pose: &mut Pose,
804-
) -> (Option<DeviceMotion>, Option<[Pose; 26]>) {
769+
) -> HandData {
805770
let xr_time = crate::to_xr_time(time);
806771

807-
let controller_motion = if hand_source
772+
let grip_motion = if hand_source
808773
.grip_action
809774
.is_active(xr_session, xr::Path::NULL)
810775
.unwrap_or(false)
@@ -873,7 +838,49 @@ pub fn get_hand_data(
873838
None
874839
};
875840

876-
let hand_joints = if let Some(tracker) = &hand_source.skeleton_tracker {
841+
let detached_grip_motion = if let Some(detached_grip_action) = &hand_source.detached_grip_action
842+
{
843+
if detached_grip_action
844+
.is_active(xr_session, xr::Path::NULL)
845+
.unwrap_or(false)
846+
{
847+
if let Ok((location, velocity)) = hand_source
848+
.detached_grip_space
849+
.as_ref()
850+
.unwrap()
851+
.relate(reference_space, xr_time)
852+
{
853+
if location
854+
.location_flags
855+
.contains(xr::SpaceLocationFlags::ORIENTATION_VALID)
856+
{
857+
last_controller_pose.orientation =
858+
crate::from_xr_quat(location.pose.orientation);
859+
}
860+
861+
if location
862+
.location_flags
863+
.contains(xr::SpaceLocationFlags::POSITION_VALID)
864+
{
865+
last_controller_pose.position = crate::from_xr_vec3(location.pose.position);
866+
}
867+
868+
Some(DeviceMotion {
869+
pose: *last_controller_pose,
870+
linear_velocity: crate::from_xr_vec3(velocity.linear_velocity),
871+
angular_velocity: crate::from_xr_vec3(velocity.angular_velocity),
872+
})
873+
} else {
874+
None
875+
}
876+
} else {
877+
None
878+
}
879+
} else {
880+
None
881+
};
882+
883+
let skeleton_joints = if let Some(tracker) = &hand_source.skeleton_tracker {
877884
let xr_now = crate::xr_runtime_now(xr_session.instance()).unwrap_or(xr_time);
878885

879886
if let Some(joint_locations) = reference_space
@@ -913,7 +920,11 @@ pub fn get_hand_data(
913920
None
914921
};
915922

916-
(controller_motion, hand_joints)
923+
HandData {
924+
grip_motion,
925+
detached_grip_motion,
926+
skeleton_joints,
927+
}
917928
}
918929

919930
pub fn update_buttons(

alvr/client_openxr/src/stream.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ fn stream_input_loop(
509509

510510
device_motions.push((*HEAD_ID, head_motion));
511511

512-
let (left_hand_motion, left_hand_skeleton) = crate::interaction::get_hand_data(
512+
let left_hand_data = crate::interaction::get_hand_data(
513513
&xr_session,
514514
platform,
515515
stage_reference_space,
@@ -519,7 +519,7 @@ fn stream_input_loop(
519519
&mut last_controller_poses[0],
520520
&mut last_palm_poses[0],
521521
);
522-
let (right_hand_motion, right_hand_skeleton) = crate::interaction::get_hand_data(
522+
let right_hand_data = crate::interaction::get_hand_data(
523523
&xr_session,
524524
platform,
525525
stage_reference_space,
@@ -532,35 +532,21 @@ fn stream_input_loop(
532532

533533
// Note: When multimodal input is enabled, we are sure that when free hands are used
534534
// (not holding controllers) the controller data is None.
535-
if int_ctx.multimodal_hands_enabled || left_hand_skeleton.is_none() {
536-
if let Some(motion) = left_hand_motion {
535+
if int_ctx.multimodal_hands_enabled || left_hand_data.skeleton_joints.is_none() {
536+
if let Some(motion) = left_hand_data.grip_motion {
537537
device_motions.push((*HAND_LEFT_ID, motion));
538538
}
539539
}
540-
if int_ctx.multimodal_hands_enabled || right_hand_skeleton.is_none() {
541-
if let Some(motion) = right_hand_motion {
540+
if int_ctx.multimodal_hands_enabled || right_hand_data.skeleton_joints.is_none() {
541+
if let Some(motion) = right_hand_data.grip_motion {
542542
device_motions.push((*HAND_RIGHT_ID, motion));
543543
}
544544
}
545545

546-
let left_detached_motion = crate::interaction::get_detached_controller_motion_data(
547-
&xr_session,
548-
stage_reference_space,
549-
now,
550-
&int_ctx.hands_interaction[0],
551-
&mut last_controller_poses[0],
552-
);
553-
if let Some(motion) = left_detached_motion {
546+
if let Some(motion) = left_hand_data.detached_grip_motion {
554547
device_motions.push((*DETACHED_CONTROLLER_LEFT_ID, motion));
555548
};
556-
let right_detached_motion = crate::interaction::get_detached_controller_motion_data(
557-
&xr_session,
558-
stage_reference_space,
559-
now,
560-
&int_ctx.hands_interaction[1],
561-
&mut last_controller_poses[1],
562-
);
563-
if let Some(motion) = right_detached_motion {
549+
if let Some(motion) = right_hand_data.detached_grip_motion {
564550
device_motions.push((*DETACHED_CONTROLLER_RIGHT_ID, motion));
565551
};
566552

@@ -602,7 +588,10 @@ fn stream_input_loop(
602588
core_ctx.send_tracking(
603589
Duration::from_nanos(now.as_nanos() as u64),
604590
device_motions,
605-
[left_hand_skeleton, right_hand_skeleton],
591+
[
592+
left_hand_data.skeleton_joints,
593+
right_hand_data.skeleton_joints,
594+
],
606595
face_data,
607596
);
608597

0 commit comments

Comments
 (0)