diff --git a/client/application.cpp b/client/application.cpp index f4775103..cefde89a 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -58,6 +58,7 @@ #include "jnipp.h" #else #include +#include "utils/xdg_base_directory.h" #endif using namespace std::chrono_literals; @@ -293,30 +294,6 @@ static XrActionType guess_action_type(const std::string & name) return XR_ACTION_TYPE_FLOAT_INPUT; } -#ifndef __ANDROID__ -static std::filesystem::path get_config_base_dir() -{ - const char * xdg_config_home = std::getenv("XDG_CONFIG_HOME"); - if (xdg_config_home) - return xdg_config_home; - const char * home = std::getenv("HOME"); - if (home) - return std::filesystem::path(home) / ".config"; - return "."; -} - -static std::filesystem::path get_cache_base_dir() -{ - const char * xdg_cache_home = std::getenv("XDG_CACHE_HOME"); - if (xdg_cache_home) - return xdg_cache_home; - const char * home = std::getenv("HOME"); - if (home) - return std::filesystem::path(home) / ".cache"; - return "."; -} -#endif - VkBool32 application::vulkan_debug_report_callback( VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, @@ -927,8 +904,8 @@ application::application(application_info info) : } #else - config_path = get_config_base_dir() / "wivrn"; - cache_path = get_cache_base_dir() / "wivrn"; + config_path = xdg_config_home() / "wivrn"; + cache_path = xdg_cache_home() / "wivrn"; #endif std::filesystem::create_directories(config_path); diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index e1c1cc69..b864c380 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -24,6 +24,7 @@ configure_file(wivrn_config.h.in wivrn_config.h) add_library(wivrn-common STATIC wivrn_sockets.cpp + utils/xdg_base_directory.cpp vk/allocation.cpp vk/error_category.cpp vk/vk_allocator.cpp diff --git a/common/utils/xdg_base_directory.cpp b/common/utils/xdg_base_directory.cpp new file mode 100644 index 00000000..3d070d86 --- /dev/null +++ b/common/utils/xdg_base_directory.cpp @@ -0,0 +1,43 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * 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 +#include + +std::filesystem::path xdg_config_home() +{ + const char * xdg_config_home = std::getenv("XDG_CONFIG_HOME"); + if (xdg_config_home) + return xdg_config_home; + const char * home = std::getenv("HOME"); + if (home) + return std::filesystem::path(home) / ".config"; + return "."; +} + +std::filesystem::path xdg_cache_home() +{ + const char * xdg_cache_home = std::getenv("XDG_CACHE_HOME"); + if (xdg_cache_home) + return xdg_cache_home; + const char * home = std::getenv("HOME"); + if (home) + return std::filesystem::path(home) / ".cache"; + return "."; +} diff --git a/common/utils/xdg_base_directory.h b/common/utils/xdg_base_directory.h new file mode 100644 index 00000000..caf0ff1f --- /dev/null +++ b/common/utils/xdg_base_directory.h @@ -0,0 +1,25 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * Copyright (C) 2024 Patrick Nicolas + * + * 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 . + */ + +#pragma once + +#include + +std::filesystem::path xdg_config_home(); +std::filesystem::path xdg_cache_home(); diff --git a/common/wivrn_config.h.in b/common/wivrn_config.h.in index b901f997..27207109 100644 --- a/common/wivrn_config.h.in +++ b/common/wivrn_config.h.in @@ -24,3 +24,5 @@ #cmakedefine WIVRN_USE_X264 #cmakedefine WIVRN_USE_SYSTEMD + +#define WIVRN_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@" diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index d8a74ad8..1b3ab745 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -57,10 +57,11 @@ endif() add_executable(wivrn-server - main.cpp - target_instance_wivrn.cpp + active_runtime.cpp avahi_publisher.cpp hostname.cpp + main.cpp + target_instance_wivrn.cpp wivrn_ipc.cpp encoder/encoder_settings.cpp diff --git a/server/active_runtime.cpp b/server/active_runtime.cpp new file mode 100644 index 00000000..b42e4964 --- /dev/null +++ b/server/active_runtime.cpp @@ -0,0 +1,62 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * + * 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 "active_runtime.h" +#include "utils/xdg_base_directory.h" +#include "wivrn_config.h" +#include +#include + +active_runtime::active_runtime() : pid(getpid()), active_runtime_json(xdg_config_home() / "openxr" / "1" / "active_runtime.json") +{ + try + { + if (std::filesystem::exists(active_runtime_json)) + return; + + std::filesystem::create_directories(active_runtime_json.parent_path()); + + auto exe = std::filesystem::read_symlink("/proc/self/exe"); + auto openxr_wivrn_json = exe.parent_path().parent_path() / "openxr_wivrn-dev.json"; + if (!std::filesystem::exists(openxr_wivrn_json)) + openxr_wivrn_json = std::filesystem::path(WIVRN_INSTALL_PREFIX) / "share" / "openxr" / "1" / "openxr_wivrn.json"; + + std::filesystem::create_symlink(openxr_wivrn_json, active_runtime_json); + + to_be_deleted = true; + } + catch(std::exception& e) + { + std::cerr << "Cannot set active OpenXR runtime: " << e.what() << std::endl; + } +} + +active_runtime::~active_runtime() +{ + try + { + if (pid == getpid() && to_be_deleted) + { + std::filesystem::remove(active_runtime_json); + } + } + catch(std::exception& e) + { + std::cerr << "Cannot unset active OpenXR runtime: " << e.what() << std::endl; + } +} diff --git a/server/active_runtime.h b/server/active_runtime.h new file mode 100644 index 00000000..79e4d928 --- /dev/null +++ b/server/active_runtime.h @@ -0,0 +1,35 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * + * 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 . + */ + +#pragma once + +#include +#include + +class active_runtime +{ + bool to_be_deleted = false; + std::filesystem::path active_runtime_json; + pid_t pid; + +public: + active_runtime(); + active_runtime(const active_runtime&) = delete; + active_runtime& operator=(const active_runtime&) = delete; + ~active_runtime(); +}; diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp index 59dff3b7..86e8c6bc 100644 --- a/server/driver/configuration.cpp +++ b/server/driver/configuration.cpp @@ -31,26 +31,16 @@ #include "util/u_logging.h" #include "configuration.h" - -static std::filesystem::path get_config_base_dir() -{ - const char * xdg_config_home = std::getenv("XDG_CONFIG_HOME"); - if (xdg_config_home) - return xdg_config_home; - const char * home = std::getenv("HOME"); - if (home) - return std::filesystem::path(home) / ".config"; - return "."; -} +#include "utils/xdg_base_directory.h" static std::filesystem::path get_config_file() { - return get_config_base_dir() / "wivrn" / "config.json"; + return xdg_config_home() / "wivrn" / "config.json"; } static std::filesystem::path get_cookie_file() { - return get_config_base_dir() / "wivrn" / "cookie"; + return xdg_config_home() / "wivrn" / "cookie"; } namespace xrt::drivers::wivrn diff --git a/server/main.cpp b/server/main.cpp index 277b299d..fa9657b9 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -29,6 +29,7 @@ #include "hostname.h" #include #include +#include "active_runtime.h" // Insert the on load constructor to init trace marker. U_TRACE_TARGET_SETUP(U_TRACE_WHICH_SERVICE) @@ -237,6 +238,8 @@ int inner_main(int argc, char * argv[]) return EXIT_FAILURE; } + active_runtime runtime_setter; + pid_t client_pid = start_application(); pid_t server_pid = fork();