linux client: add battery reporting

This commit is contained in:
konsti219
2026-10-01 23:19:53 +02:00
committed by xytovl
parent 1b0d13c431
commit 0d9e6e121a
5 changed files with 101 additions and 13 deletions
+4
View File
@@ -120,6 +120,10 @@ else()
"decoder/ffmpeg/*.cpp")
endif()
file(GLOB CLIENT_LINUX_SOURCE CONFIGURE_DEPENDS
"linux/*.cpp")
list(APPEND PLATFORM_SOURCE ${CLIENT_LINUX_SOURCE})
if(WIVRN_USE_PIPEWIRE)
file(GLOB CLIENT_PIPEWIRE_SOURCE CONFIGURE_DEPENDS
"audio/linux/*.cpp")
+94
View File
@@ -0,0 +1,94 @@
/*
* WiVRn VR streaming
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "battery.h"
#include <chrono>
#include <filesystem>
#include <fstream>
#include <mutex>
#include <string>
namespace
{
std::mutex last_status_lock;
battery_status last_status;
std::chrono::steady_clock::time_point last_read;
std::optional<std::string> read_line(const std::filesystem::path & path)
{
std::ifstream file(path);
std::string line;
if (file and std::getline(file, line))
return line;
return std::nullopt;
}
std::optional<float> read_percent(const std::filesystem::path & path)
{
std::ifstream file(path);
float percent;
if (file >> percent and percent >= 0 and percent <= 100)
return percent / 100;
return std::nullopt;
}
battery_status read_battery_status(const std::filesystem::path & percent_path, const std::filesystem::path & status_path)
{
auto charge = read_percent(percent_path);
if (not charge)
return {};
auto status = read_line(status_path);
return {
.charge = charge,
.charging = status == "Charging" or status == "Full",
};
}
battery_status read_battery_status()
{
// SteamOS on the Steam Frame: the charger daemon's percentage is what the system UI shows
const std::filesystem::path deckard = "/run/deckardcharger";
if (auto battery = read_battery_status(deckard / "battery_percent", deckard / "battery_status"); battery.charge)
return battery;
// Generic Linux: the first battery in sysfs
std::error_code ec;
for (const auto & supply: std::filesystem::directory_iterator("/sys/class/power_supply", ec))
{
if (read_line(supply.path() / "type") != "Battery")
continue;
if (auto battery = read_battery_status(supply.path() / "capacity", supply.path() / "status"); battery.charge)
return battery;
}
return {};
}
} // namespace
battery_status get_battery_status()
{
// The lobby asks every frame, the files only change every few seconds
std::unique_lock _{last_status_lock};
auto now = std::chrono::steady_clock::now();
if (now - last_read > std::chrono::seconds(5))
{
last_status = read_battery_status();
last_read = now;
}
return last_status;
}
+1 -6
View File
@@ -19,7 +19,7 @@
#include "gui_common.h"
#include "android/battery.h"
#include "battery.h"
#include "render/ui_theme.h"
#include "render/ui_widgets.h"
@@ -37,7 +37,6 @@ float toggle_width()
std::optional<battery_indicator> battery_status_indicator(XrTime now)
{
#ifdef __ANDROID__
const auto battery = get_battery_status();
if (not battery.charge)
return std::nullopt;
@@ -82,9 +81,5 @@ std::optional<battery_indicator> battery_status_indicator(XrTime now)
.label = fmt::format("{} {}%", icon, (int)std::round(*battery.charge * 100)),
.style = style,
};
#else
(void)now;
return std::nullopt;
#endif
}
} // namespace wivrn::gui
+2 -7
View File
@@ -18,6 +18,7 @@
*/
#include "application.h"
#include "battery.h"
#include "stream.h"
#include "utils/overloaded.h"
#include "wivrn_packets.h"
@@ -31,10 +32,6 @@
#include <spdlog/spdlog.h>
#include <thread>
#ifdef __ANDROID__
#include "android/battery.h"
#endif
namespace
{
@@ -233,10 +230,10 @@ void scenes::stream::tracking()
#ifdef __ANDROID__
// Runtime may use JNI and needs the thread to be attached
application::instance().setup_jni();
#endif
XrTime next_battery_check = 0;
const XrDuration battery_check_interval = 30'000'000'000; // 30s
#endif
magic_enum::containers::array<device_id, XrSpace> spaces{};
@@ -550,7 +547,6 @@ void scenes::stream::tracking()
throw;
}
#ifdef __ANDROID__
// FIXME: switch to event based
if (next_battery_check < now)
{
@@ -563,7 +559,6 @@ void scenes::stream::tracking()
next_battery_check = now + battery_check_interval;
}
#endif
if (auto fb = std::get_if<xr::fb_body_tracker>(&body_tracker); fb and fb->should_send_skeleton())
{