diff --git a/client/application.cpp b/client/application.cpp index 83f567d0..94dd6e86 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -1376,6 +1376,7 @@ void application::initialize() XR_KHR_MAINTENANCE1_EXTENSION_NAME, XR_KHR_VISIBILITY_MASK_EXTENSION_NAME, + XR_EXT_INTERACTION_PROFILE_BATTERY_STATE_DISPLAY_EXTENSION_NAME, XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME, XR_EXT_HAND_INTERACTION_EXTENSION_NAME, XR_EXT_HAND_TRACKING_EXTENSION_NAME, diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index c5c93050..01230208 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -286,6 +286,7 @@ std::shared_ptr scenes::stream::create(std::unique_ptrinstance.has_extension(XR_EXT_INTERACTION_PROFILE_BATTERY_STATE_DISPLAY_EXTENSION_NAME); info.hand_tracking = config.check_feature(feature::hand_tracking); info.eye_gaze = config.check_feature(feature::eye_gaze); diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 84aaa350..27ec9f0c 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -346,6 +346,8 @@ void scenes::stream::tracking() std::vector packets; + const bool battery = instance.has_extension(XR_EXT_INTERACTION_PROFILE_BATTERY_STATE_DISPLAY_EXTENSION_NAME); + const bool hand_tracking = config.check_feature(feature::hand_tracking); std::optional left_hand; std::optional right_hand; @@ -594,12 +596,56 @@ void scenes::stream::tracking() if (next_battery_check < now) { auto status = get_battery_status(); - network_session->send_stream(from_headset::battery{ - .charge = status.charge.value_or(-1), - .present = status.charge.has_value(), + network_session->send_control(from_headset::battery{ + .device = device_id::HEAD, + .charge = status.charge, .charging = status.charging, }); + if (battery) + { + static std::array paths{ + std::make_pair("/user/hand/left", device_id::LEFT_CONTROLLER), + std::make_pair("/user/hand/right", device_id::RIGHT_CONTROLLER), + std::make_pair("/user/gamepad", device_id::GAMEPAD), + }; + + for (auto [path, device]: paths) + { + XrResult res; + XrPath p; + if (res = xrStringToPath(instance, path, &p); !XR_SUCCEEDED(res)) + { + spdlog::warn("xrStringToPath(\"{}\") returned {} when querying battery, continuing", path, xr::to_string(res)); + continue; + } + + XrBatteryStateDisplayEXT battery_state{ + .type = XR_TYPE_BATTERY_STATE_DISPLAY_EXT, + }; + XrInteractionProfileState profile_state{ + .type = XR_TYPE_INTERACTION_PROFILE_STATE, + .next = &battery_state, + }; + + if (res = xrGetCurrentInteractionProfile(session, p, &profile_state); !XR_SUCCEEDED(res)) + { + spdlog::warn("xrGetCurrentInteractionProfile returned {} when querying battery, continuing", xr::to_string(res)); + continue; + } + + std::optional charge{}; + if (battery_state.stateFlags & XR_BATTERY_STATE_DISPLAY_STATE_VALID_BIT_EXT) + charge = battery_state.batteryLevel; + + network_session->send_control(from_headset::battery{ + .device = device, + .charge = charge, + .charging = (battery_state.stateFlags & XR_BATTERY_STATE_DISPLAY_STATE_CHARGING_BIT_EXT) != 0, + }); + } + } + next_battery_check = now + battery_check_interval; } diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index af60abcc..0ff3080e 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -154,10 +154,14 @@ enum class device_id : uint8_t RIGHT_GRASP_VALUE, // /user/hand/right/input/grasp_ext/value RIGHT_GRASP_READY, // /user/hand/right/input/grasp_ext/ready_ext EYE_GAZE, // /user/eyes_ext/input/gaze_ext/pose - LEFT_HAND, // identify hand tracking - RIGHT_HAND, // identify hand tracking - BODY, // identify body tracking - FACE, // identify face tracking + + LEFT_CONTROLLER, // identify controller + RIGHT_CONTROLLER, // identify controller + LEFT_HAND = LEFT_CONTROLLER, // identify hand tracking + RIGHT_HAND = RIGHT_CONTROLLER, // identify hand tracking + BODY, // identify body tracking + FACE, // identify face tracking + GAMEPAD, // identify gamepad // Gamepad, microsoft/xbox_controller profile (/user/gamepad) GAMEPAD_MENU_CLICK, // /user/gamepad/input/menu/click @@ -346,6 +350,7 @@ struct headset_info_packet }; std::optional speaker; std::optional microphone; + bool battery_status; // supports XR_EXT_interaction_profile_battery_state_display, for reporting controller/gamepad battery status bool hand_tracking; bool eye_gaze; bool palm_pose; @@ -668,8 +673,8 @@ struct feedback struct battery { - float charge; - bool present; + device_id device; + std::optional charge; bool charging; }; diff --git a/server/driver/wivrn_controller.cpp b/server/driver/wivrn_controller.cpp index 4bd36f9a..0d244be9 100644 --- a/server/driver/wivrn_controller.cpp +++ b/server/driver/wivrn_controller.cpp @@ -728,11 +728,13 @@ wivrn_controller::wivrn_controller(xrt_device_name name, .orientation_tracking = true, .position_tracking = true, .hand_tracking = name != XRT_DEVICE_EXT_HAND_INTERACTION and cnx->get_info().hand_tracking, + .battery_status = cnx->get_info().battery_status, }, .update_inputs = method_pointer<&wivrn_controller::update_inputs>, .get_tracked_pose = method_pointer<&wivrn_controller::get_tracked_pose>, .get_hand_tracking = method_pointer<&wivrn_controller::get_hand_tracking>, .set_output = method_pointer<&wivrn_controller::set_output>, + .get_battery_status = method_pointer<&wivrn_controller::get_battery_status>, .destroy = [](xrt_device *) {}, }, grip(hand_id == 0 ? device_id::LEFT_GRIP : device_id::RIGHT_GRIP), @@ -992,6 +994,26 @@ xrt_result_t wivrn_controller::get_hand_tracking(xrt_input_name name, int64_t de } } +xrt_result_t wivrn_controller::get_battery_status(bool * out_present, + bool * out_charging, + float * out_charge) +{ + auto bat = battery.lock(); + + *out_charging = bat->charging; + if (bat->charge) + { + *out_present = true; + *out_charge = *bat->charge; + } + else + { + *out_present = false; + } + + return XRT_SUCCESS; +} + void wivrn_controller::set_derived_pose(const from_headset::derived_pose & derived) { auto list = [this](device_id id) -> pose_list * { @@ -1008,6 +1030,14 @@ void wivrn_controller::set_derived_pose(const from_headset::derived_pose & deriv target->set_derived(source, xrt_cast(derived.relation)); } +void wivrn_controller::update_battery(const from_headset::battery & new_battery) +{ + const device_id device = device_type == XRT_DEVICE_TYPE_LEFT_HAND_CONTROLLER ? device_id::LEFT_CONTROLLER : device_id::RIGHT_CONTROLLER; + if (new_battery.device == device) + { + *battery.lock() = new_battery; + } +} void wivrn_controller::update_tracking(const from_headset::tracking & tracking, const clock_offset & offset) { aim.update_tracking(tracking, offset); diff --git a/server/driver/wivrn_controller.h b/server/driver/wivrn_controller.h index be0a3380..d16c0ace 100644 --- a/server/driver/wivrn_controller.h +++ b/server/driver/wivrn_controller.h @@ -20,6 +20,7 @@ #pragma once +#include "utils/thread_safe.h" #include "xrt/xrt_device.h" #include "hand_joints_list.h" @@ -48,6 +49,8 @@ class wivrn_controller : public xrt_device std::vector inputs_array; std::vector outputs_array; + thread_safe battery{}; + wivrn::wivrn_session * cnx; public: @@ -58,12 +61,16 @@ public: xrt_result_t get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation * out_relation); xrt_result_t get_hand_tracking(xrt_input_name name, int64_t desired_timestamp_ns, xrt_hand_joint_set * out_value, int64_t * out_timestamp_ns); + xrt_result_t get_battery_status(bool * out_present, + bool * out_charging, + float * out_charge); xrt_result_t set_output(xrt_output_name name, const xrt_output_value * value); void set_inputs(const from_headset::inputs &, const clock_offset &); - void set_derived_pose(const from_headset::derived_pose &); + + void update_battery(const from_headset::battery &); void update_tracking(const from_headset::tracking &, const clock_offset &); void update_hand_tracking(const from_headset::hand_tracking &, const clock_offset &); diff --git a/server/driver/wivrn_gamepad.cpp b/server/driver/wivrn_gamepad.cpp index 9023cf79..31b2a2ec 100644 --- a/server/driver/wivrn_gamepad.cpp +++ b/server/driver/wivrn_gamepad.cpp @@ -42,9 +42,13 @@ wivrn_gamepad::wivrn_gamepad(wivrn::wivrn_session & cnx) : .str = "WiVRn Gamepad", .serial = "WiVRn Gamepad", .tracking_origin = &origin, + .supported = { + .battery_status = cnx.get_info().battery_status, + }, .update_inputs = method_pointer<&wivrn_gamepad::update_inputs>, .get_tracked_pose = method_pointer<&wivrn_gamepad::get_tracked_pose>, .set_output = method_pointer<&wivrn_gamepad::set_output>, + .get_battery_status = method_pointer<&wivrn_gamepad::get_battery_status>, .destroy = [](xrt_device *) {}, }, origin{ @@ -110,6 +114,34 @@ xrt_result_t wivrn_gamepad::get_tracked_pose(xrt_input_name name, int64_t, xrt_s return XRT_ERROR_INPUT_UNSUPPORTED; } +xrt_result_t wivrn_gamepad::get_battery_status(bool * out_present, + bool * out_charging, + float * out_charge) +{ + auto bat = battery.lock(); + + *out_charging = bat->charging; + if (bat->charge) + { + *out_present = true; + *out_charge = *bat->charge; + } + else + { + *out_present = false; + } + + return XRT_SUCCESS; +} + +void wivrn_gamepad::set_battery(const from_headset::battery & new_battery) +{ + if (new_battery.device == device_id::GAMEPAD) + { + *battery.lock() = new_battery; + } +} + void wivrn_gamepad::set_inputs(const from_headset::inputs & inputs) { std::lock_guard lock(mutex); diff --git a/server/driver/wivrn_gamepad.h b/server/driver/wivrn_gamepad.h index db122384..a20b1be9 100644 --- a/server/driver/wivrn_gamepad.h +++ b/server/driver/wivrn_gamepad.h @@ -18,6 +18,7 @@ #pragma once +#include "utils/thread_safe.h" #include "wivrn_packets.h" #include "xrt/xrt_defines.h" @@ -43,6 +44,8 @@ class wivrn_gamepad : public xrt_device std::array inputs_staging; // written from the network thread, published by update_inputs std::array outputs_array; + thread_safe battery{}; + wivrn::wivrn_session * cnx; public: @@ -51,8 +54,12 @@ public: 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); + xrt_result_t get_battery_status(bool * out_present, + bool * out_charging, + float * out_charge); xrt_result_t set_output(xrt_output_name name, const xrt_output_value * value); + void set_battery(const from_headset::battery &); // Update from a forwarded inputs packet (gamepad device_ids). void set_inputs(const from_headset::inputs &); void set_connected(bool); diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index d2fc2707..8971aadd 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -164,8 +164,10 @@ void wivrn_hmd::update_tracking(const from_headset::tracking & tracking, const c void wivrn_hmd::update_battery(const from_headset::battery & new_battery) { - // We will only request a new sample if the current one is consumed - *battery.lock() = new_battery; + if (new_battery.device == device_id::HEAD) + { + *battery.lock() = new_battery; + } } xrt_result_t wivrn_hmd::get_view_poses(const xrt_vec3 * default_eye_relation, @@ -214,9 +216,17 @@ xrt_result_t wivrn_hmd::get_battery_status(bool * out_present, float * out_charge) { auto bat = battery.lock(); - *out_present = bat->present; + *out_charging = bat->charging; - *out_charge = bat->charge; + if (bat->charge) + { + *out_present = true; + *out_charge = *bat->charge; + } + else + { + *out_present = false; + } return XRT_SUCCESS; } diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 34c3fe67..907d4e62 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -822,6 +822,12 @@ void wivrn_session::operator()(from_headset::feedback && feedback) void wivrn_session::operator()(from_headset::battery && battery) { hmd.update_battery(battery); + left_controller.update_battery(battery); + left_hand_interaction.update_battery(battery); + right_controller.update_battery(battery); + right_hand_interaction.update_battery(battery); + if (gamepad_device) + gamepad_device->set_battery(battery); } void wivrn_session::operator()(from_headset::visibility_mask_changed && mask)