From c682f8cd9f206d973226a5b3924563c4af1ea8c1 Mon Sep 17 00:00:00 2001 From: Sapphire Date: Mon, 7 Jul 2025 02:43:42 -0500 Subject: [PATCH] Make hand interaction a different xdev * Don't override hands from other drivers on interaction profile change * Rename *_hand to *_controller * Fix derived pose and tracking control desync with hand interaction * Reset pose history on switch from hand interaction, code cleanup --- client/scenes/stream_tracking.cpp | 6 +- server/CMakeLists.txt | 1 + server/driver/history.h | 6 + server/driver/wivrn_controller.cpp | 101 +---- server/driver/wivrn_controller.h | 5 +- server/driver/wivrn_hand_interaction.cpp | 483 +++++++++++++++++++++++ server/driver/wivrn_hand_interaction.h | 65 +++ server/driver/wivrn_session.cpp | 92 ++++- server/driver/wivrn_session.h | 11 +- 9 files changed, 658 insertions(+), 112 deletions(-) create mode 100644 server/driver/wivrn_hand_interaction.cpp create mode 100644 server/driver/wivrn_hand_interaction.h diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 76f70ec1..6a0e5150 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -315,7 +315,7 @@ void scenes::stream::tracking() } } - auto enabled = [this](const to_headset::tracking_control & control, device_id id) -> bool { + auto enabled = [](const to_headset::tracking_control & control, device_id id) -> bool { switch (id) { case device_id::HEAD: @@ -330,7 +330,7 @@ void scenes::stream::tracking() case device_id::LEFT_PINCH_POSE: case device_id::LEFT_POKE: { const auto tracking_id = id == device_id::LEFT_PINCH_POSE ? tid::left_pinch : tid::left_poke; - return control.enabled[size_t(tracking_id)] and interaction_profiles[0].load() == interaction_profile::ext_hand_interaction_ext; + return control.enabled[size_t(tracking_id)]; } case device_id::RIGHT_AIM: return control.enabled[size_t(tid::right_aim)]; @@ -341,7 +341,7 @@ void scenes::stream::tracking() case device_id::RIGHT_PINCH_POSE: case device_id::RIGHT_POKE: { const auto tracking_id = id == device_id::RIGHT_PINCH_POSE ? tid::right_pinch : tid::right_poke; - return control.enabled[size_t(tracking_id)] and interaction_profiles[1].load() == interaction_profile::ext_hand_interaction_ext; + return control.enabled[size_t(tracking_id)]; } default: break; diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index e3e4b029..a6ada1a8 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -93,6 +93,7 @@ if (WIVRN_BUILD_SERVER) driver/wivrn_pacer.cpp driver/wivrn_comp_target.cpp driver/wivrn_controller.cpp + driver/wivrn_hand_interaction.cpp driver/wivrn_eye_tracker.cpp driver/wivrn_fb_face2_tracker.cpp driver/wivrn_htc_face_tracker.cpp diff --git a/server/driver/history.h b/server/driver/history.h index d1428f93..8540645a 100644 --- a/server/driver/history.h +++ b/server/driver/history.h @@ -131,5 +131,11 @@ public: return {}; } + void reset() + { + std::lock_guard lock(mutex); + data.fill({}); + last_request = os_monotonic_get_ns(); + } }; } // namespace wivrn diff --git a/server/driver/wivrn_controller.cpp b/server/driver/wivrn_controller.cpp index 7c8f1bd9..b5e69019 100644 --- a/server/driver/wivrn_controller.cpp +++ b/server/driver/wivrn_controller.cpp @@ -39,6 +39,7 @@ namespace wivrn enum wivrn_controller_input_index { + WIVRN_CONTROLLER_INPUT_INVALID = -1, WIVRN_CONTROLLER_AIM_POSE, WIVRN_CONTROLLER_GRIP_POSE, WIVRN_CONTROLLER_PALM_POSE, @@ -77,14 +78,6 @@ enum wivrn_controller_input_index WIVRN_CONTROLLER_TRACKPAD_FORCE, // /user/hand/XXXX/input/trackpad/force WIVRN_CONTROLLER_TRACKPAD_TOUCH, // /user/hand/XXXX/input/trackpad/touch WIVRN_CONTROLLER_STYLUS_FORCE, // /user/hand/XXXX/input/stylus_fb/force - WIVRN_CONTROLLER_PINCH_POSE, // /user/hand/XXXX/input/pinch_ext/pose - WIVRN_CONTROLLER_PINCH_VALUE, // /user/hand/XXXX/input/pinch_ext/value - WIVRN_CONTROLLER_PINCH_READY, // /user/hand/XXXX/input/pinch_ext/ready_ext - WIVRN_CONTROLLER_POKE_POSE, // /user/hand/XXXX/input/poke_ext/pose - WIVRN_CONTROLLER_AIM_ACTIVATE_VALUE, // /user/hand/XXXX/input/aim_activate_ext/value - WIVRN_CONTROLLER_AIM_ACTIVATE_READY, // /user/hand/XXXX/input/aim_activate_ext/ready_ext - WIVRN_CONTROLLER_GRASP_VALUE, // /user/hand/XXXX/input/grasp_ext/value - WIVRN_CONTROLLER_GRASP_READY, // /user/hand/XXXX/input/ready_ext/value WIVRN_CONTROLLER_INPUT_COUNT }; @@ -242,43 +235,16 @@ static input_data map_input(device_id id) return {WIVRN_CONTROLLER_THUMBREST_FORCE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; case device_id::RIGHT_STYLUS_FORCE: return {WIVRN_CONTROLLER_STYLUS_FORCE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; - // XR_EXT_hand_interaction - case device_id::LEFT_PINCH_POSE: - return {WIVRN_CONTROLLER_PINCH_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; - case device_id::LEFT_PINCH_VALUE: - return {WIVRN_CONTROLLER_PINCH_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; - case device_id::LEFT_PINCH_READY: - return {WIVRN_CONTROLLER_PINCH_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; - case device_id::RIGHT_PINCH_POSE: - return {WIVRN_CONTROLLER_PINCH_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; - case device_id::RIGHT_PINCH_VALUE: - return {WIVRN_CONTROLLER_PINCH_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; - case device_id::RIGHT_PINCH_READY: - return {WIVRN_CONTROLLER_PINCH_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; - case device_id::LEFT_POKE: - return {WIVRN_CONTROLLER_POKE_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; - case device_id::RIGHT_POKE: - return {WIVRN_CONTROLLER_POKE_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; - case device_id::LEFT_AIM_ACTIVATE_VALUE: - return {WIVRN_CONTROLLER_AIM_ACTIVATE_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; - case device_id::LEFT_AIM_ACTIVATE_READY: - return {WIVRN_CONTROLLER_AIM_ACTIVATE_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; - case device_id::LEFT_GRASP_VALUE: - return {WIVRN_CONTROLLER_GRASP_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; - case device_id::LEFT_GRASP_READY: - return {WIVRN_CONTROLLER_GRASP_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; - case device_id::RIGHT_AIM_ACTIVATE_VALUE: - return {WIVRN_CONTROLLER_AIM_ACTIVATE_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; - case device_id::RIGHT_AIM_ACTIVATE_READY: - return {WIVRN_CONTROLLER_AIM_ACTIVATE_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; - case device_id::RIGHT_GRASP_VALUE: - return {WIVRN_CONTROLLER_GRASP_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; - case device_id::RIGHT_GRASP_READY: - return {WIVRN_CONTROLLER_GRASP_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; default: break; } - throw std::range_error("bad input id " + std::to_string((int)id)); + // If the headset supports hand_interaction_ext, upon switch + // to/from hand tracking we may get the inputs packet before + // the interaction profile change, so if we get a bad input + // just return an invalid index so we can ignore it + U_LOG_D("wivrn_controller: bad input id %s", std::string(magic_enum::enum_name(id)).c_str()); + // the type here doesn't really matter + return {WIVRN_CONTROLLER_INPUT_INVALID, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_UNKNOWN}; } static xrt_binding_input_pair simple_input_binding[] = { @@ -292,19 +258,6 @@ static xrt_binding_output_pair simple_output_binding[] = { {XRT_OUTPUT_NAME_SIMPLE_VIBRATION, XRT_OUTPUT_NAME_TOUCH_HAPTIC}, }; -static xrt_binding_input_pair hand_interaction_input_binding[] = { - {XRT_INPUT_HAND_GRIP_POSE, XRT_INPUT_TOUCH_GRIP_POSE}, - {XRT_INPUT_HAND_AIM_POSE, XRT_INPUT_TOUCH_AIM_POSE}, - {XRT_INPUT_HAND_PINCH_POSE, XRT_INPUT_HAND_PINCH_POSE}, - {XRT_INPUT_HAND_PINCH_VALUE, XRT_INPUT_HAND_PINCH_VALUE}, - {XRT_INPUT_HAND_PINCH_READY, XRT_INPUT_HAND_PINCH_READY}, - {XRT_INPUT_HAND_POKE_POSE, XRT_INPUT_HAND_POKE_POSE}, - {XRT_INPUT_HAND_AIM_ACTIVATE_READY, XRT_INPUT_HAND_AIM_ACTIVATE_READY}, - {XRT_INPUT_HAND_AIM_ACTIVATE_VALUE, XRT_INPUT_HAND_AIM_ACTIVATE_VALUE}, - {XRT_INPUT_HAND_GRASP_VALUE, XRT_INPUT_HAND_GRASP_VALUE}, - {XRT_INPUT_HAND_GRASP_READY, XRT_INPUT_HAND_GRASP_READY}, -}; - static xrt_binding_input_pair index_input_binding[] = { {XRT_INPUT_INDEX_SYSTEM_CLICK, XRT_INPUT_TOUCH_SYSTEM_CLICK}, {XRT_INPUT_INDEX_SYSTEM_TOUCH, XRT_INPUT_INDEX_SYSTEM_TOUCH}, @@ -483,13 +436,6 @@ static xrt_binding_profile wivrn_binding_profiles[] = { .outputs = simple_output_binding, .output_count = std::size(simple_output_binding), }, - { - .name = XRT_DEVICE_EXT_HAND_INTERACTION, - .inputs = hand_interaction_input_binding, - .input_count = std::size(hand_interaction_input_binding), - .outputs = nullptr, - .output_count = 0, - }, { .name = XRT_DEVICE_INDEX_CONTROLLER, .inputs = index_input_binding, @@ -616,8 +562,6 @@ wivrn_controller::wivrn_controller(int hand_id, aim(hand_id == 0 ? device_id::LEFT_AIM : device_id::RIGHT_AIM), palm(hand_id == 0 ? device_id::LEFT_PALM : device_id::RIGHT_PALM), joints(hand_id), - pinch_ext(hand_id == 0 ? device_id::LEFT_PINCH_POSE : device_id::RIGHT_PINCH_POSE), - poke_ext(hand_id == 0 ? device_id::LEFT_POKE : device_id::RIGHT_POKE), inputs_array(input_count, xrt_input{}), cnx(cnx) { @@ -670,14 +614,6 @@ wivrn_controller::wivrn_controller(int hand_id, SET_INPUT(TOUCH, A_TOUCH); SET_INPUT(TOUCH, B_TOUCH); } - SET_INPUT(HAND, PINCH_POSE); - SET_INPUT(HAND, PINCH_VALUE); - SET_INPUT(HAND, PINCH_READY); - SET_INPUT(HAND, POKE_POSE); - SET_INPUT(HAND, AIM_ACTIVATE_VALUE); - SET_INPUT(HAND, AIM_ACTIVATE_READY); - SET_INPUT(HAND, GRASP_VALUE); - SET_INPUT(HAND, GRASP_READY); SET_INPUT(VIVE_FOCUS3, SQUEEZE_CLICK); SET_INPUT(INDEX, SQUEEZE_FORCE); SET_INPUT(TOUCH, SQUEEZE_VALUE); @@ -811,14 +747,6 @@ xrt_result_t wivrn_controller::get_tracked_pose(xrt_input_name name, int64_t at_ std::tie(extrapolation_time, *res, device) = palm.get_pose_at(at_timestamp_ns); cnx->set_enabled(device, true); break; - case XRT_INPUT_HAND_PINCH_POSE: - std::tie(extrapolation_time, *res, device) = pinch_ext.get_pose_at(at_timestamp_ns); - cnx->set_enabled(device, true); - break; - case XRT_INPUT_HAND_POKE_POSE: - std::tie(extrapolation_time, *res, device) = poke_ext.get_pose_at(at_timestamp_ns); - cnx->set_enabled(device, true); - break; default: U_LOG_XDEV_UNSUPPORTED_INPUT(this, u_log_get_global_level(), name); return XRT_ERROR_INPUT_UNSUPPORTED; @@ -833,8 +761,6 @@ xrt_result_t wivrn_controller::get_tracked_pose(xrt_input_name name, int64_t at_ case XRT_INPUT_TOUCH_AIM_POSE: return aim.device; case XRT_INPUT_TOUCH_GRIP_POSE: return grip.device; case XRT_INPUT_GENERIC_PALM_POSE: return palm.device; - case XRT_INPUT_HAND_PINCH_POSE: return pinch_ext.device; - case XRT_INPUT_HAND_POKE_POSE: return poke_ext.device; default: assert(false); __builtin_unreachable(); @@ -898,10 +824,6 @@ void wivrn_controller::update_tracking(const from_headset::tracking & tracking, cnx->set_enabled(grip.device, false); if (not palm.update_tracking(tracking, offset)) cnx->set_enabled(palm.device, false); - if (not pinch_ext.update_tracking(tracking, offset)) - cnx->set_enabled(pinch_ext.device, false); - if (not poke_ext.update_tracking(tracking, offset)) - cnx->set_enabled(poke_ext.device, false); if (auto & out = tracking_dump(); out and offset) { std::lock_guard lock{tracking_dump_mutex}; @@ -958,4 +880,11 @@ xrt_result_t wivrn_controller::set_output(xrt_output_name name, const xrt_output } return XRT_SUCCESS; } + +void wivrn_controller::reset_history() +{ + grip.reset(); + aim.reset(); + palm.reset(); +} } // namespace wivrn diff --git a/server/driver/wivrn_controller.h b/server/driver/wivrn_controller.h index f66214af..4a737e0b 100644 --- a/server/driver/wivrn_controller.h +++ b/server/driver/wivrn_controller.h @@ -39,9 +39,6 @@ class wivrn_controller : public xrt_device pose_list aim; pose_list palm; hand_joints_list joints; - // hand_interaction - pose_list pinch_ext; - pose_list poke_ext; std::vector inputs_staging; std::vector inputs_array; @@ -65,5 +62,7 @@ public: void set_derived_pose(const from_headset::derived_pose &); void update_tracking(const from_headset::tracking &, const clock_offset &); void update_hand_tracking(const from_headset::hand_tracking &, const clock_offset &); + + void reset_history(); }; } // namespace wivrn diff --git a/server/driver/wivrn_hand_interaction.cpp b/server/driver/wivrn_hand_interaction.cpp new file mode 100644 index 00000000..def312f9 --- /dev/null +++ b/server/driver/wivrn_hand_interaction.cpp @@ -0,0 +1,483 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022 Guillaume Meunier + * Copyright (C) 2022 Patrick Nicolas + * Copyright (C) 2025 Sapphire + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "wivrn_hand_interaction.h" +#include "configuration.h" +#include "driver/xrt_cast.h" +#include "math/m_api.h" +#include "utils/method.h" +#include "wivrn_session.h" + +#include "util/u_logging.h" +#include +#include +#include +#include + +#include + +#include "xrt/xrt_defines.h" + +namespace wivrn +{ + +enum wivrn_hand_interaction_input_index +{ + WIVRN_HAND_INTERACTION_INPUT_INVALID = -1, + WIVRN_HAND_INTERACTION_AIM_POSE, + WIVRN_HAND_INTERACTION_GRIP_POSE, + WIVRN_HAND_INTERACTION_PALM_POSE, + + WIVRN_HAND_INTERACTION_PINCH_POSE, // /user/hand/XXXX/input/pinch_ext/pose + WIVRN_HAND_INTERACTION_PINCH_VALUE, // /user/hand/XXXX/input/pinch_ext/value + WIVRN_HAND_INTERACTION_PINCH_READY, // /user/hand/XXXX/input/pinch_ext/ready_ext + WIVRN_HAND_INTERACTION_POKE_POSE, // /user/hand/XXXX/input/poke_ext/pose + WIVRN_HAND_INTERACTION_AIM_ACTIVATE_VALUE, // /user/hand/XXXX/input/aim_activate_ext/value + WIVRN_HAND_INTERACTION_AIM_ACTIVATE_READY, // /user/hand/XXXX/input/aim_activate_ext/ready_ext + WIVRN_HAND_INTERACTION_GRASP_VALUE, // /user/hand/XXXX/input/grasp_ext/value + WIVRN_HAND_INTERACTION_GRASP_READY, // /user/hand/XXXX/input/ready_ext/value + + WIVRN_HAND_INTERACTION_INPUT_COUNT +}; + +enum class wivrn_input_type +{ + BOOL, + FLOAT, + VEC2_X, + VEC2_Y, + POSE, +}; + +struct input_data +{ + wivrn_hand_interaction_input_index index; + wivrn_input_type type; + xrt_device_type device; +}; + +static input_data map_input(device_id id) +{ + switch (id) + { + case device_id::LEFT_GRIP: + return {WIVRN_HAND_INTERACTION_GRIP_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::RIGHT_GRIP: + return {WIVRN_HAND_INTERACTION_GRIP_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + case device_id::LEFT_AIM: + return {WIVRN_HAND_INTERACTION_AIM_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::RIGHT_AIM: + return {WIVRN_HAND_INTERACTION_AIM_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + case device_id::LEFT_PALM: + return {WIVRN_HAND_INTERACTION_PALM_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::RIGHT_PALM: + return {WIVRN_HAND_INTERACTION_PALM_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + // XR_EXT_hand_interaction + case device_id::LEFT_PINCH_POSE: + return {WIVRN_HAND_INTERACTION_PINCH_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::LEFT_PINCH_VALUE: + return {WIVRN_HAND_INTERACTION_PINCH_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::LEFT_PINCH_READY: + return {WIVRN_HAND_INTERACTION_PINCH_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::RIGHT_PINCH_POSE: + return {WIVRN_HAND_INTERACTION_PINCH_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + case device_id::RIGHT_PINCH_VALUE: + return {WIVRN_HAND_INTERACTION_PINCH_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + case device_id::RIGHT_PINCH_READY: + return {WIVRN_HAND_INTERACTION_PINCH_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + case device_id::LEFT_POKE: + return {WIVRN_HAND_INTERACTION_POKE_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::RIGHT_POKE: + return {WIVRN_HAND_INTERACTION_POKE_POSE, wivrn_input_type::POSE, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + case device_id::LEFT_AIM_ACTIVATE_VALUE: + return {WIVRN_HAND_INTERACTION_AIM_ACTIVATE_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::LEFT_AIM_ACTIVATE_READY: + return {WIVRN_HAND_INTERACTION_AIM_ACTIVATE_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::LEFT_GRASP_VALUE: + return {WIVRN_HAND_INTERACTION_GRASP_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::LEFT_GRASP_READY: + return {WIVRN_HAND_INTERACTION_GRASP_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER}; + case device_id::RIGHT_AIM_ACTIVATE_VALUE: + return {WIVRN_HAND_INTERACTION_AIM_ACTIVATE_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + case device_id::RIGHT_AIM_ACTIVATE_READY: + return {WIVRN_HAND_INTERACTION_AIM_ACTIVATE_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + case device_id::RIGHT_GRASP_VALUE: + return {WIVRN_HAND_INTERACTION_GRASP_VALUE, wivrn_input_type::FLOAT, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + case device_id::RIGHT_GRASP_READY: + return {WIVRN_HAND_INTERACTION_GRASP_READY, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER}; + default: + break; + } + // If the headset supports hand_interaction_ext, upon switch + // to/from hand tracking we may get the inputs packet before + // the interaction profile change, so if we get a bad input + // just return an invalid index so we can ignore it + U_LOG_D("wivrn_hand_interaction: bad input id %s", std::string(magic_enum::enum_name(id)).c_str()); + // the type here doesn't really matter + return {WIVRN_HAND_INTERACTION_INPUT_INVALID, wivrn_input_type::BOOL, XRT_DEVICE_TYPE_UNKNOWN}; +} + +static xrt_binding_input_pair hand_interaction_input_binding[] = { + {XRT_INPUT_HAND_GRIP_POSE, XRT_INPUT_HAND_GRIP_POSE}, + {XRT_INPUT_HAND_AIM_POSE, XRT_INPUT_HAND_AIM_POSE}, + {XRT_INPUT_HAND_PINCH_POSE, XRT_INPUT_HAND_PINCH_POSE}, + {XRT_INPUT_HAND_PINCH_VALUE, XRT_INPUT_HAND_PINCH_VALUE}, + {XRT_INPUT_HAND_PINCH_READY, XRT_INPUT_HAND_PINCH_READY}, + {XRT_INPUT_HAND_POKE_POSE, XRT_INPUT_HAND_POKE_POSE}, + {XRT_INPUT_HAND_AIM_ACTIVATE_READY, XRT_INPUT_HAND_AIM_ACTIVATE_READY}, + {XRT_INPUT_HAND_AIM_ACTIVATE_VALUE, XRT_INPUT_HAND_AIM_ACTIVATE_VALUE}, + {XRT_INPUT_HAND_GRASP_VALUE, XRT_INPUT_HAND_GRASP_VALUE}, + {XRT_INPUT_HAND_GRASP_READY, XRT_INPUT_HAND_GRASP_READY}, +}; + +static xrt_binding_profile wivrn_binding_profiles[] = { + { + .name = XRT_DEVICE_EXT_HAND_INTERACTION, + .inputs = hand_interaction_input_binding, + .input_count = std::size(hand_interaction_input_binding), + .outputs = nullptr, + .output_count = 0, + }, +}; + +namespace +{ +struct xrt_space_relation_csv_header +{}; +} // namespace + +static std::ostream & operator<<(std::ostream & out, const xrt_space_relation_csv_header &) +{ + for (auto [value, name]: magic_enum::enum_entries()) + { + if (value and value != XRT_SPACE_RELATION_BITMASK_ALL) + { + name = name.substr(strlen("XRT_SPACE_RELATION_")); + name = name.substr(0, name.size() - strlen("_BIT")); + out << name << ","; + } + } + out << "x,y,z,"; + out << "qw,qx,qy,qz"; + return out; +} + +static std::ostream & operator<<(std::ostream & out, const xrt_space_relation & rel) +{ + const auto & pos = rel.pose.position; + const auto & o = rel.pose.orientation; + for (const auto & [value, name]: magic_enum::enum_entries()) + { + if (value and value != XRT_SPACE_RELATION_BITMASK_ALL) + out << bool(rel.relation_flags & value) << ','; + } + out << pos.x << ',' << pos.y << ',' << pos.z << ','; + out << o.w << ',' << o.x << ',' << o.y << ',' << o.z; + return out; +} + +static std::mutex tracking_dump_mutex; +static std::ofstream & tracking_dump() +{ + static auto res = [] { + std::ofstream res; + if (auto wivrn_dump = std::getenv("WIVRN_DUMP_TRACKING")) + { + res.open(wivrn_dump); + res << "device_id," + "now_ns," + "timestamp_ns," + "extrapolation_ns," + "receive/get," + << xrt_space_relation_csv_header{} << std::endl; + } + return res; + }(); + return res; +} + +wivrn_hand_interaction::wivrn_hand_interaction(int hand_id, + xrt_device * hmd, + wivrn::wivrn_session * cnx) : + xrt_device{ + .name = XRT_DEVICE_EXT_HAND_INTERACTION, + .device_type = hand_id == 0 ? XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER : XRT_DEVICE_TYPE_RIGHT_HAND_CONTROLLER, + .hmd = nullptr, + .tracking_origin = hmd->tracking_origin, + .binding_profile_count = std::size(wivrn_binding_profiles), + .binding_profiles = wivrn_binding_profiles, + .input_count = WIVRN_HAND_INTERACTION_INPUT_COUNT, + .supported = { + .orientation_tracking = true, + .position_tracking = true, + }, + .update_inputs = method_pointer<&wivrn_hand_interaction::update_inputs>, + .get_tracked_pose = method_pointer<&wivrn_hand_interaction::get_tracked_pose>, + .destroy = [](xrt_device *) {}, + }, + grip(hand_id == 0 ? device_id::LEFT_GRIP : device_id::RIGHT_GRIP), + aim(hand_id == 0 ? device_id::LEFT_AIM : device_id::RIGHT_AIM), + palm(hand_id == 0 ? device_id::LEFT_PALM : device_id::RIGHT_PALM), + pinch_ext(hand_id == 0 ? device_id::LEFT_PINCH_POSE : device_id::RIGHT_PINCH_POSE), + poke_ext(hand_id == 0 ? device_id::LEFT_POKE : device_id::RIGHT_POKE), + inputs_array(input_count, xrt_input{}), + cnx(cnx) +{ + inputs = inputs_array.data(); + + // Setup input. +#define SET_INPUT(VENDOR, NAME) \ + do \ + { \ + inputs[WIVRN_HAND_INTERACTION_##NAME].name = XRT_INPUT_##VENDOR##_##NAME; \ + inputs[WIVRN_HAND_INTERACTION_##NAME].active = true; \ + } while (0) + + if (auto grip_surface = configuration().grip_surface) + { + std::array angles = grip_surface.value(); + float deg_2_rad = std::numbers::pi / 180.0; + xrt_vec3 rotation_angles{angles[0] * deg_2_rad, angles[1] * deg_2_rad, angles[2] * deg_2_rad}; + xrt_pose offset = XRT_POSE_IDENTITY; + math_quat_from_euler_angles(&rotation_angles, &offset.orientation); + + palm.set_derived(&grip, offset, true); + cnx->set_enabled(palm.device, false); + } + else if (not cnx->get_info().palm_pose) + { + palm.set_derived(&grip, XRT_POSE_IDENTITY, false); + cnx->set_enabled(palm.device, false); + } + + SET_INPUT(HAND, AIM_POSE); + SET_INPUT(HAND, GRIP_POSE); + SET_INPUT(GENERIC, PALM_POSE); + SET_INPUT(HAND, PINCH_POSE); + SET_INPUT(HAND, PINCH_VALUE); + SET_INPUT(HAND, PINCH_READY); + SET_INPUT(HAND, POKE_POSE); + SET_INPUT(HAND, AIM_ACTIVATE_VALUE); + SET_INPUT(HAND, AIM_ACTIVATE_READY); + SET_INPUT(HAND, GRASP_VALUE); + SET_INPUT(HAND, GRASP_READY); + +#undef SET_INPUT + inputs_staging = inputs_array; + + // Make sure everything is mapped + for (const auto & item: inputs_array) + { + assert(item.name); + } + + outputs = nullptr; + output_count = 0; + + switch (hand_id) + { + case 0: + // Print name. + strcpy(str, "WiVRn left hand interaction"); + strcpy(serial, "WiVRn left hand interaction"); + break; + case 1: + // Print name. + strcpy(str, "WiVRn right hand interaction"); + strcpy(serial, "WiVRn right hand interaction"); + break; + default: + throw std::runtime_error("Invalid hand ID"); + } +} + +xrt_result_t wivrn_hand_interaction::update_inputs() +{ + std::lock_guard _{mutex}; + inputs_array = inputs_staging; + return XRT_SUCCESS; +} + +void wivrn_hand_interaction::set_inputs(const from_headset::inputs & inputs, const clock_offset & clock_offset) +{ + std::lock_guard lock{mutex}; + for (auto & input: inputs_staging) + { + switch (XRT_GET_INPUT_TYPE(input.name)) + { + case XRT_INPUT_TYPE_VEC1_ZERO_TO_ONE: + case XRT_INPUT_TYPE_VEC1_MINUS_ONE_TO_ONE: + case XRT_INPUT_TYPE_VEC2_MINUS_ONE_TO_ONE: + case XRT_INPUT_TYPE_VEC3_MINUS_ONE_TO_ONE: + case XRT_INPUT_TYPE_BOOLEAN: + input.active = false; + case XRT_INPUT_TYPE_POSE: + case XRT_INPUT_TYPE_HAND_TRACKING: + case XRT_INPUT_TYPE_FACE_TRACKING: + case XRT_INPUT_TYPE_BODY_TRACKING: + break; + } + } + + for (const auto & input: inputs.values) + { + int64_t last_change_time = input.last_change_time ? clock_offset.from_headset(input.last_change_time) : 0; + auto [index, type, device] = map_input(input.id); + if (device != device_type) + continue; + assert(index > 0 and index < input_count); + inputs_staging[index].timestamp = last_change_time; + inputs_staging[index].active = true; + switch (type) + { + case wivrn_input_type::BOOL: + inputs_staging[index].value.boolean = (input.value != 0); + break; + case wivrn_input_type::FLOAT: + inputs_staging[index].value.vec1.x = input.value; + break; + case wivrn_input_type::VEC2_X: + inputs_staging[index].value.vec2.x = input.value; + break; + case wivrn_input_type::VEC2_Y: + inputs_staging[index].value.vec2.y = input.value; + break; + case wivrn_input_type::POSE: + // Pose should not be in the inputs array + U_LOG_W("Unexpected input id %d", int(input.id)); + } + } +} + +xrt_result_t wivrn_hand_interaction::get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation * res) +{ + std::chrono::nanoseconds extrapolation_time; + device_id device; + switch (name) + { + case XRT_INPUT_HAND_AIM_POSE: + std::tie(extrapolation_time, *res, device) = aim.get_pose_at(at_timestamp_ns); + cnx->set_enabled(device, true); + break; + case XRT_INPUT_HAND_GRIP_POSE: + std::tie(extrapolation_time, *res, device) = grip.get_pose_at(at_timestamp_ns); + cnx->set_enabled(device, true); + break; + case XRT_INPUT_GENERIC_PALM_POSE: + std::tie(extrapolation_time, *res, device) = palm.get_pose_at(at_timestamp_ns); + cnx->set_enabled(device, true); + break; + case XRT_INPUT_HAND_PINCH_POSE: + std::tie(extrapolation_time, *res, device) = pinch_ext.get_pose_at(at_timestamp_ns); + cnx->set_enabled(device, true); + break; + case XRT_INPUT_HAND_POKE_POSE: + std::tie(extrapolation_time, *res, device) = poke_ext.get_pose_at(at_timestamp_ns); + cnx->set_enabled(device, true); + break; + default: + U_LOG_XDEV_UNSUPPORTED_INPUT(this, u_log_get_global_level(), name); + return XRT_ERROR_INPUT_UNSUPPORTED; + } + cnx->add_predict_offset(extrapolation_time); + if (auto & out = tracking_dump()) + { + std::lock_guard lock{tracking_dump_mutex}; + auto device = [&] { + switch (name) + { + case XRT_INPUT_HAND_AIM_POSE: return aim.device; + case XRT_INPUT_HAND_GRIP_POSE: return grip.device; + case XRT_INPUT_GENERIC_PALM_POSE: return palm.device; + case XRT_INPUT_HAND_PINCH_POSE: return pinch_ext.device; + case XRT_INPUT_HAND_POKE_POSE: return poke_ext.device; + default: + assert(false); + __builtin_unreachable(); + } }(); + out << magic_enum::enum_name(device) << ',' + << os_monotonic_get_ns() << ',' + << at_timestamp_ns << ',' + << extrapolation_time.count() << ',' + << "g," + << *res << std::endl; + ; + } + return XRT_SUCCESS; +} + +void wivrn_hand_interaction::set_derived_pose(const from_headset::derived_pose & derived) +{ + auto list = [this](device_id id) -> pose_list * { + for (auto item: {&grip, &aim, &palm}) + { + if (item->device == id) + return item; + } + return nullptr; + }; + auto source = list(derived.source); + auto target = list(derived.target); + if (source and target) + { + target->set_derived(source, xrt_cast(derived.relation)); + if (source != target) + cnx->set_enabled(derived.target, false); + } +} + +void wivrn_hand_interaction::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset) +{ + if (not aim.update_tracking(tracking, offset)) + cnx->set_enabled(aim.device, false); + if (not grip.update_tracking(tracking, offset)) + cnx->set_enabled(grip.device, false); + if (not palm.update_tracking(tracking, offset)) + cnx->set_enabled(palm.device, false); + if (not pinch_ext.update_tracking(tracking, offset)) + cnx->set_enabled(pinch_ext.device, false); + if (not poke_ext.update_tracking(tracking, offset)) + cnx->set_enabled(poke_ext.device, false); + if (auto & out = tracking_dump(); out and offset) + { + std::lock_guard lock{tracking_dump_mutex}; + auto now = os_monotonic_get_ns(); + for (const auto & pose: tracking.device_poses) + { + if (pose.device == aim.device or pose.device == grip.device or pose.device == palm.device) + out << magic_enum::enum_name(pose.device) << ',' + << now << ',' + << offset.from_headset(tracking.timestamp) << ',' + << tracking.timestamp - tracking.production_timestamp << ',' + << "r," + << pose_list::convert_pose(pose) << std::endl; + } + } +} + +void wivrn_hand_interaction::reset_history() +{ + aim.reset(); + grip.reset(); + palm.reset(); + pinch_ext.reset(); + poke_ext.reset(); +} +} // namespace wivrn diff --git a/server/driver/wivrn_hand_interaction.h b/server/driver/wivrn_hand_interaction.h new file mode 100644 index 00000000..165723bc --- /dev/null +++ b/server/driver/wivrn_hand_interaction.h @@ -0,0 +1,65 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022 Guillaume Meunier + * Copyright (C) 2022 Patrick Nicolas + * Copyright (C) 2025 Sapphire + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "xrt/xrt_device.h" + +#include "hand_joints_list.h" +#include "pose_list.h" + +#include +#include + +namespace wivrn +{ +class wivrn_session; + +class wivrn_hand_interaction : public xrt_device +{ + std::mutex mutex; + + pose_list grip; + pose_list aim; + pose_list palm; + pose_list pinch_ext; + pose_list poke_ext; + + std::vector inputs_staging; + std::vector inputs_array; + + wivrn::wivrn_session * cnx; + +public: + using base = xrt_device; + wivrn_hand_interaction(int hand_id, xrt_device * hmd, wivrn::wivrn_session * cnx); + + xrt_result_t update_inputs(); + + xrt_result_t get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation * out_relation); + + void set_inputs(const from_headset::inputs &, const clock_offset &); + + void set_derived_pose(const from_headset::derived_pose &); + void update_tracking(const from_headset::tracking &, const clock_offset &); + + void reset_history(); +}; +} // namespace wivrn diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 3429b555..3c767761 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -146,8 +146,10 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr connection connection(std::move(connection)), xrt_system(system), hmd(this, get_info()), - left_hand(0, &hmd, this), - right_hand(1, &hmd, this) + left_controller(0, &hmd, this), + left_hand_interaction(0, &hmd, this), + right_controller(1, &hmd, this), + right_hand_interaction(1, &hmd, this) { try { @@ -172,11 +174,13 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr connection if (hmd.supported.face_tracking) static_roles.face = &hmd; - roles.left = xdev_count; - static_roles.hand_tracking.left = xdevs[xdev_count++] = &left_hand; + roles.left = left_controller_index = xdev_count++; + static_roles.hand_tracking.left = xdevs[left_controller_index] = &left_controller; + xdevs[left_hand_interaction_index = xdev_count++] = &left_hand_interaction; - roles.right = xdev_count; - static_roles.hand_tracking.right = xdevs[xdev_count++] = &right_hand; + roles.right = right_controller_index = xdev_count++; + static_roles.hand_tracking.right = xdevs[right_controller_index] = &right_controller; + xdevs[right_hand_interaction_index = xdev_count++] = &right_hand_interaction; #if WIVRN_FEATURE_STEAMVR_LIGHTHOUSE auto use_steamvr_lh = configuration().use_steamvr_lh || std::getenv("WIVRN_USE_STEAMVR_LH"); @@ -312,8 +316,8 @@ xrt_result_t wivrn::wivrn_session::create_session(std::unique_ptrxrt_system.broadcast, &self->hmd, - &self->left_hand, - &self->right_hand, + &self->left_controller, + &self->right_controller, nullptr, self->xdevs, self->xdev_count, @@ -408,8 +412,8 @@ static xrt_device_name get_name(interaction_profile profile) } void wivrn_session::operator()(from_headset::trackings && tracking) { - auto left = xdevs[roles.left] == &left_hand ? get_name(tracking.interaction_profiles[0]) : XRT_DEVICE_INVALID; - auto right = xdevs[roles.right] == &right_hand ? get_name(tracking.interaction_profiles[1]) : XRT_DEVICE_INVALID; + auto left = (roles.left == left_controller_index || roles.left == left_hand_interaction_index) ? get_name(tracking.interaction_profiles[0]) : XRT_DEVICE_INVALID; + auto right = (roles.right == right_controller_index || roles.right == right_hand_interaction_index) ? get_name(tracking.interaction_profiles[1]) : XRT_DEVICE_INVALID; if (left != roles.left_profile or right != roles.right_profile) { U_LOG_I("Updating interaction profiles: from \n" @@ -420,8 +424,42 @@ void wivrn_session::operator()(from_headset::trackings && tracking) std::string(magic_enum::enum_name(roles.right_profile)).c_str(), std::string(magic_enum::enum_name(right)).c_str()); std::lock_guard lock(roles_mutex); + + // don't change role when hand from other driver is used + if (roles.left == left_hand_interaction_index || roles.left == left_controller_index) + { + if (left == XRT_DEVICE_EXT_HAND_INTERACTION) + { + left_hand_interaction.reset_history(); + roles.left = left_hand_interaction_index; + } + else + { + left_controller.reset_history(); + roles.left = left_controller_index; + set_enabled(device_id::LEFT_PINCH_POSE, false); + set_enabled(device_id::LEFT_POKE, false); + } + } roles.left_profile = left; + + if (roles.right == right_hand_interaction_index || roles.right == right_controller_index) + { + if (right == XRT_DEVICE_EXT_HAND_INTERACTION) + { + right_hand_interaction.reset_history(); + roles.right = right_hand_interaction_index; + } + else + { + right_controller.reset_history(); + roles.right = right_controller_index; + set_enabled(device_id::RIGHT_PINCH_POSE, false); + set_enabled(device_id::RIGHT_POKE, false); + } + } roles.right_profile = right; + ++roles.generation_id; } @@ -440,8 +478,16 @@ void wivrn_session::operator()(const from_headset::tracking & tracking) auto offset = offset_est.get_offset(); hmd.update_tracking(tracking, offset); - left_hand.update_tracking(tracking, offset); - right_hand.update_tracking(tracking, offset); + if (roles.left == left_hand_interaction_index) + left_hand_interaction.update_tracking(tracking, offset); + else + left_controller.update_tracking(tracking, offset); + + if (roles.right == right_hand_interaction_index) + right_hand_interaction.update_tracking(tracking, offset); + else + right_controller.update_tracking(tracking, offset); + if (eye_tracker) eye_tracker->update_tracking(tracking, offset); { @@ -458,16 +504,19 @@ void wivrn_session::operator()(const from_headset::tracking & tracking) void wivrn_session::operator()(from_headset::derived_pose && derived) { - left_hand.set_derived_pose(derived); - right_hand.set_derived_pose(derived); + left_controller.set_derived_pose(derived); + left_hand_interaction.set_derived_pose(derived); + + right_controller.set_derived_pose(derived); + right_hand_interaction.set_derived_pose(derived); } void wivrn_session::operator()(from_headset::hand_tracking && hand_tracking) { auto offset = offset_est.get_offset(); - left_hand.update_hand_tracking(hand_tracking, offset); - right_hand.update_hand_tracking(hand_tracking, offset); + left_controller.update_hand_tracking(hand_tracking, offset); + right_controller.update_hand_tracking(hand_tracking, offset); } void wivrn_session::operator()(from_headset::body_tracking && body_tracking) { @@ -486,8 +535,15 @@ void wivrn_session::operator()(from_headset::inputs && inputs) { auto offset = get_offset(); - left_hand.set_inputs(inputs, offset); - right_hand.set_inputs(inputs, offset); + if (roles.left == left_hand_interaction_index) + left_hand_interaction.set_inputs(inputs, offset); + else if (roles.left == left_controller_index) + left_controller.set_inputs(inputs, offset); + + if (roles.right == right_hand_interaction_index) + right_hand_interaction.set_inputs(inputs, offset); + else if (roles.right == right_controller_index) + right_controller.set_inputs(inputs, offset); } void wivrn_session::operator()(from_headset::timesync_response && timesync) diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 43930f07..882ed29f 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -23,6 +23,7 @@ #include "driver/app_pacer.h" #include "wivrn_connection.h" #include "wivrn_controller.h" +#include "wivrn_hand_interaction.h" #include "wivrn_hmd.h" #include "wivrn_ipc.h" #include "wivrn_packets.h" @@ -105,8 +106,14 @@ class wivrn_session : public xrt_system_devices }; wivrn_hmd hmd; - wivrn_controller left_hand; - wivrn_controller right_hand; + wivrn_controller left_controller; + int32_t left_controller_index; + wivrn_hand_interaction left_hand_interaction; + int32_t left_hand_interaction_index; + wivrn_controller right_controller; + int32_t right_controller_index; + wivrn_hand_interaction right_hand_interaction; + int32_t right_hand_interaction_index; std::unique_ptr eye_tracker; std::unique_ptr fb_face2_tracker; std::unique_ptr htc_face_tracker;