This commit is contained in:
Crispy 2023-11-22 17:58:36 +01:00
commit f9735a9946
7 changed files with 5719 additions and 0 deletions

5
.clang-format Normal file
View file

@ -0,0 +1,5 @@
BasedOnStyle: Microsoft
UseTab: Always
ReflowComments: false
ColumnLimit: 0
BreakBeforeBraces: Attach

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
vr_status
steamvr-status
.vscode/
*.zip
*.tar.xz

13
Makefile Normal file
View file

@ -0,0 +1,13 @@
VERSION := 1.0.0
CXX := g++
TARGET := ./vr_status
LFLAGS := -Wl,-rpath,'$$ORIGIN' -L. -lopenvr_api
build:
$(CXX) src/main.cpp $(CPPFLAGS) $(LFLAGS) -o $(TARGET)
release: build
mkdir -p steamvr-status
cp -f libopenvr_api.so steamvr-status
cp -f $(TARGET) steamvr-status
tar -caf steamvr-status-$(VERSION).tar.xz steamvr-status

2
README.md Normal file
View file

@ -0,0 +1,2 @@
# SteamVR-status
A very simple program that prints the battery levels of connected VR devices to `/tmp/steamvr_battery_status` if SteamVR is running.

5600
lib/openvr.h Normal file

File diff suppressed because it is too large Load diff

BIN
libopenvr_api.so Normal file

Binary file not shown.

94
src/main.cpp Normal file
View file

@ -0,0 +1,94 @@
#include "../lib/openvr.h"
#include <cstdio>
#include <fstream>
#include <signal.h>
#include <string>
#include <unistd.h>
#include <vector>
const char *OUT_PATH = "/tmp/steamvr_battery_status";
bool should_exit = false;
vr::IVRSystem *vr_sys;
void interrupted(int _sig) {
should_exit = true;
}
void update();
int main() {
vr::EVRInitError init_err;
vr_sys = vr::VR_Init(&init_err, vr::VRApplication_Background);
if (init_err == vr::VRInitError_Init_NoServerForBackgroundApp) {
printf("SteamVR is not running.\n");
vr::VR_Shutdown();
return 0;
} else if (init_err != 0) {
printf("Could not initialize OpenVR session. Error code: %d\n", init_err);
vr::VR_Shutdown();
return 1;
}
signal(SIGINT, interrupted);
while (!should_exit) {
update();
sleep(1);
}
std::remove(OUT_PATH);
vr::VR_Shutdown();
return 0;
}
void update() {
auto di_left = vr_sys->GetTrackedDeviceIndexForControllerRole(vr::TrackedControllerRole_LeftHand);
auto di_right = vr_sys->GetTrackedDeviceIndexForControllerRole(vr::TrackedControllerRole_RightHand);
std::vector<vr::TrackedDeviceIndex_t> trackers;
{
vr::TrackedDeviceIndex_t tracker_arr[64];
int controller_count = vr_sys->GetSortedTrackedDeviceIndicesOfClass(vr::TrackedDeviceClass_Controller, tracker_arr, 64);
trackers = std::vector<vr::TrackedDeviceIndex_t>(tracker_arr, tracker_arr + controller_count);
}
std::string output;
for (int di = 0; di < vr::k_unMaxTrackedDeviceCount; di++) {
if (!vr_sys->IsTrackedDeviceConnected(di))
continue;
vr::ETrackedPropertyError err;
bool provides_battery = vr_sys->GetBoolTrackedDeviceProperty(di, vr::Prop_DeviceProvidesBatteryStatus_Bool, &err);
if (!provides_battery || err != vr::TrackedProp_Success)
continue;
if (di == di_left) {
output += "L";
} else if (di == di_right) {
output += "R";
} else {
output += "T";
output += std::to_string(di);
}
output += ": ";
float battery_level = vr_sys->GetFloatTrackedDeviceProperty(di, vr::Prop_DeviceBatteryPercentage_Float, &err);
output += std::to_string((int)(battery_level * 100));
output += "%";
bool is_charging = vr_sys->GetBoolTrackedDeviceProperty(di, vr::Prop_DeviceIsCharging_Bool, &err);
if (is_charging) {
output += "+";
}
output += " ";
}
if (output.empty()) {
output = "No tracked devices connected";
}
std::ofstream out_file(OUT_PATH);
out_file.write(output.c_str(), output.size());
out_file.close();
}