Files
WiVRn/server/active_runtime.cpp
T
t20kdc 0e9b72c776 Add OpenVR runtime override as a setting, add --early-active-runtime to test this (rv2)
I ran into some trouble debugging issues with Vivecraft because my OpenVR runtime setup was being automatically overridden by WiVRn.

It would be nice to attach a UI to this option, but I'm not sure on the details there and I'd like to keep the PR small and easily reviewed.

Being able to set the compat path is more useful than simply disabling compat override because of things like pressure-vessel bypass, which are finicky to get right.
2025-03-21 22:40:48 +01:00

155 lines
4.8 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <[email protected]>
* Copyright (C) 2024 Patrick Nicolas <[email protected]>
*
* 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 "active_runtime.h"
#include "driver/configuration.h"
#include "utils/flatpak.h"
#include "utils/xdg_base_directory.h"
#include "wivrn_config.h"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
namespace wivrn
{
std::filesystem::path active_runtime::manifest_path()
{
const std::filesystem::path install_location = "share/openxr/1/openxr_wivrn.json";
// Check if in a flatpak
if (auto path = flatpak_key(flatpak::section::instance, "app-path"))
return *path / install_location.relative_path();
// Check if running from build directory
auto exe = std::filesystem::read_symlink("/proc/self/exe");
auto dev_manifest = exe.parent_path().parent_path() / "openxr_wivrn-dev.json";
if (std::filesystem::exists(dev_manifest))
return dev_manifest;
// Assume we are installed
return std::filesystem::path(WIVRN_INSTALL_PREFIX) / install_location;
}
std::filesystem::path active_runtime::openvr_compat_path()
{
// config can override compat path (i.e. to debug changes to OpenComposite)
std::optional<std::string> path_override = configuration::read_user_configuration().openvr_compat_path;
if (path_override.has_value())
return std::filesystem::path(path_override.value());
// otherwise, prefer flatpak copy if present or otherwise try to find it on the system
if (auto path = flatpak_key(flatpak::section::instance, "app-path"))
return std::filesystem::path(*path) / "OpenComposite";
for (auto path: std::ranges::split_view(std::string_view(OVR_COMPAT_SEARCH_PATH), std::string_view(":")))
{
if (auto res = std::string_view(path); std::filesystem::exists(res))
return res;
}
return {};
}
static std::filesystem::path backup_name(std::filesystem::path file)
{
file += ".wivrn-backup";
return file;
}
static void move_file(const std::filesystem::path & from, const std::filesystem::path & to)
{
auto from_status = std::filesystem::symlink_status(from);
if (from_status.type() == std::filesystem::file_type::not_found)
return;
std::filesystem::rename(from, to);
}
active_runtime::active_runtime() :
pid(getpid())
{
try
{
std::filesystem::path active_runtime_json = xdg_config_home() / "openxr/1/active_runtime.json";
std::filesystem::create_directories(active_runtime_json.parent_path());
move_file(active_runtime_json, backup_name(active_runtime_json));
std::filesystem::create_symlink(manifest_path(), active_runtime_json);
this->active_runtime_json = active_runtime_json;
}
catch (std::exception & e)
{
std::cerr << "Cannot set active OpenXR runtime: " << e.what() << std::endl;
}
try
{
auto ovr_compat = openvr_compat_path();
if (not ovr_compat.empty())
{
std::filesystem::path openvr_manifest = xdg_config_home() / "openvr/openvrpaths.vrpath";
std::filesystem::create_directories(openvr_manifest.parent_path());
move_file(openvr_manifest, backup_name(openvr_manifest));
nlohmann::json manifest;
manifest["runtime"] = {ovr_compat.string()};
manifest["version"] = 1;
std::ofstream manifest_file(openvr_manifest.c_str());
manifest_file.exceptions(std::ofstream::failbit);
manifest_file << manifest;
std::filesystem::permissions(openvr_manifest, std::filesystem::perms(0400), std::filesystem::perm_options::replace);
this->openvr_manifest = openvr_manifest;
}
}
catch (std::exception & e)
{
std::cerr << "Cannot set active OpenVR runtime: " << e.what() << std::endl;
}
}
active_runtime::~active_runtime()
{
if (pid != getpid())
return;
try
{
if (not active_runtime_json.empty())
{
std::filesystem::remove(active_runtime_json);
move_file(backup_name(active_runtime_json), active_runtime_json);
}
}
catch (std::exception & e)
{
std::cerr << "Cannot unset active OpenXR runtime: " << e.what() << std::endl;
}
try
{
if (not openvr_manifest.empty())
{
std::filesystem::remove(openvr_manifest);
move_file(backup_name(openvr_manifest), openvr_manifest);
}
}
catch (std::exception & e)
{
std::cerr << "Cannot unset active OpenVR runtime: " << e.what() << std::endl;
}
}
} // namespace wivrn