Move battery handling into dedicated monitor class

This commit is contained in:
Sapphire
2026-11-11 16:23:54 -05:00
parent 2233817b59
commit 64ecbdc24b
15 changed files with 253 additions and 111 deletions
+2 -1
View File
@@ -19,6 +19,7 @@
#include "gui_common.h"
#include "application.h"
#include "battery.h"
#include "render/ui_theme.h"
#include "render/ui_widgets.h"
@@ -37,7 +38,7 @@ float toggle_width()
std::optional<battery_indicator> battery_status_indicator(XrTime now)
{
const auto battery = get_battery_status();
const auto battery = application::get_battery_monitor().get_battery_status(battery::device::head);
if (not battery.charge)
return std::nullopt;
+5
View File
@@ -1411,6 +1411,11 @@ void scenes::lobby::on_xr_event(const xr::event & event)
if (next_scene)
next_scene->on_xr_event(event);
}
void scenes::lobby::on_battery_update(battery::device device, const battery::battery_status & battery)
{
if (next_scene)
next_scene->on_battery_update(device, battery);
}
scene::meta & scenes::lobby::get_meta_scene()
{
+1
View File
@@ -270,6 +270,7 @@ public:
void on_unfocused() override;
void on_focused() override;
void on_xr_event(const xr::event &) override;
void on_battery_update(battery::device device, const battery::battery_status &) override;
static meta & get_meta_scene();
};
+31
View File
@@ -1305,6 +1305,12 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
spdlog::info("setup, refresh rate {}", description.refresh_rate);
session.set_refresh_rate(description.refresh_rate);
battery & battery_monitor = application::get_battery_monitor();
for (auto device: magic_enum::enum_values<battery::device>())
{
on_battery_update(device, battery_monitor.get_battery_status(device));
}
std::unique_lock lock(decoder_mutex);
if (video_stream_description == description)
return;
@@ -1536,6 +1542,31 @@ void scenes::stream::on_xr_event(const xr::event & event)
}
}
void scenes::stream::on_battery_update(battery::device device, const battery::battery_status & battery)
{
device_id id = [&] {
switch (device)
{
case battery::device::head:
return device_id::HEAD;
case battery::device::left_controller:
return device_id::LEFT_CONTROLLER;
case battery::device::right_controller:
return device_id::RIGHT_CONTROLLER;
case battery::device::gamepad:
return device_id::GAMEPAD;
default:
assert(false);
__builtin_unreachable();
}
}();
network_session->send_control(from_headset::battery{
.device = id,
.charge = battery.charge,
.charging = battery.charging,
});
}
bool scenes::stream::forward_hid_input(from_headset::hid::input_t packet, bool device_enabled)
{
// hid_forwarding is whether the server permits it; device_enabled is the headset toggle.
+1
View File
@@ -248,6 +248,7 @@ public:
void on_focused() override;
void on_unfocused() override;
void on_xr_event(const xr::event &) override;
void on_battery_update(battery::device device, const battery::battery_status &) override;
bool on_input_key_down(uint8_t key_code) override;
bool on_input_key_up(uint8_t key_code) override;
-63
View File
@@ -18,7 +18,6 @@
*/
#include "application.h"
#include "battery.h"
#include "stream.h"
#include "utils/overloaded.h"
#include "wivrn_packets.h"
@@ -279,9 +278,6 @@ void scenes::stream::tracking()
application::instance().setup_jni();
#endif
XrTime next_battery_check = 0;
const XrDuration battery_check_interval = 30'000'000'000; // 30s
magic_enum::containers::array<device_id, XrSpace> spaces{};
const auto & config = application::get_config();
@@ -346,8 +342,6 @@ void scenes::stream::tracking()
std::vector<serialization_packet> 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<xr::hand_tracker> left_hand;
std::optional<xr::hand_tracker> right_hand;
@@ -592,63 +586,6 @@ void scenes::stream::tracking()
throw;
}
// FIXME: switch to event based
if (next_battery_check < now)
{
auto status = get_battery_status();
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<float> 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;
}
if (auto fb = std::get_if<xr::fb_body_tracker>(&body_tracker); fb and fb->should_send_skeleton())
{
try