From 0d9e6e121a180f7dc9b806e9f04284f89bea6bb8 Mon Sep 17 00:00:00 2001
From: konsti219 <37149441+konsti219@users.noreply.github.com>
Date: Thu, 1 Oct 2026 22:11:58 +0200
Subject: [PATCH] linux client: add battery reporting
---
client/CMakeLists.txt | 4 ++
client/{android => }/battery.h | 0
client/linux/battery.cpp | 94 +++++++++++++++++++++++++++++++
client/scenes/gui_common.cpp | 7 +--
client/scenes/stream_tracking.cpp | 9 +--
5 files changed, 101 insertions(+), 13 deletions(-)
rename client/{android => }/battery.h (100%)
create mode 100644 client/linux/battery.cpp
diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt
index 3ab787f3..fd5cf163 100644
--- a/client/CMakeLists.txt
+++ b/client/CMakeLists.txt
@@ -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")
diff --git a/client/android/battery.h b/client/battery.h
similarity index 100%
rename from client/android/battery.h
rename to client/battery.h
diff --git a/client/linux/battery.cpp b/client/linux/battery.cpp
new file mode 100644
index 00000000..cc92df3c
--- /dev/null
+++ b/client/linux/battery.cpp
@@ -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 .
+ */
+
+#include "battery.h"
+
+#include
+#include
+#include
+#include
+#include
+
+namespace
+{
+std::mutex last_status_lock;
+battery_status last_status;
+std::chrono::steady_clock::time_point last_read;
+
+std::optional 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 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;
+}
diff --git a/client/scenes/gui_common.cpp b/client/scenes/gui_common.cpp
index f0d863b2..4c9ef6ee 100644
--- a/client/scenes/gui_common.cpp
+++ b/client/scenes/gui_common.cpp
@@ -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_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_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
diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp
index 15f07984..2cc4c2e1 100644
--- a/client/scenes/stream_tracking.cpp
+++ b/client/scenes/stream_tracking.cpp
@@ -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
#include
-#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 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(&body_tracker); fb and fb->should_send_skeleton())
{