diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index 4bbd8fd0..0afca460 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -24,7 +24,7 @@ jobs: - name: Install dependencies run: > - sudo apt-get update && sudo apt-get install libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev libpulse-dev xz-utils libpipewire-0.3-dev + sudo apt-get update && sudo apt-get install libvulkan-dev libcjson-dev libx264-dev libavcodec-dev libavutil-dev libswscale-dev libavfilter-dev libbsd-dev libavahi-client-dev libeigen3-dev glslang-tools libudev-dev libwayland-dev libx11-xcb-dev libxrandr-dev libxcb-randr0-dev libgl-dev libglx-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libsystemd-dev libva-dev nlohmann-json3-dev libpulse-dev xz-utils libpipewire-0.3-dev libcli11-dev - name: Prepare Vulkan SDK uses: humbletim/setup-vulkan-sdk@286ed76c72de45dbf50c515779cf4ca2f6aa3944 with: diff --git a/CMakeLists.txt b/CMakeLists.txt index fea9113f..5c3b61c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -139,6 +139,7 @@ if (WIVRN_BUILD_SERVER) pkg_check_modules(AVAHI REQUIRED IMPORTED_TARGET avahi-client) find_package(Eigen3 REQUIRED) find_package(nlohmann_json REQUIRED) + find_package(CLI11 REQUIRED) endif() if (WIVRN_BUILD_CLIENT AND NOT ANDROID) diff --git a/README.md b/README.md index cfb3660a..577b753a 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ WiVRn uses the following software: - [Avahi](https://www.avahi.org/) - [Boost.Locale](https://github.com/boostorg/locale) - [Boost.PFR](https://github.com/boostorg/pfr) +- [CLI11](https://github.com/CLIUtils/CLI11) - [Dear ImGui](https://github.com/ocornut/imgui) - [fastgltf](https://github.com/spnda/fastgltf) - [ffmpeg](https://ffmpeg.org/) optional, for hardware encoding on AMD/Intel diff --git a/docs/building.md b/docs/building.md index fc8633c9..cdc6b2a4 100644 --- a/docs/building.md +++ b/docs/building.md @@ -34,7 +34,7 @@ Additionally, if your environment requires absolute paths inside the OpenXR runt # Client (headset) #### Build dependencies -As Arch package names: git pkgconf glslang cmake jre17-openjdk librsvg +As Arch package names: git pkgconf glslang cmake jre17-openjdk librsvg cli11 #### Android environment Download [sdkmanager](https://developer.android.com/tools/sdkmanager) commandline tool and extract it to any directory. diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 4dbe5a9e..02c68808 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -95,6 +95,8 @@ target_compile_definitions(wivrn-server PRIVATE VULKAN_HPP_NO_CONSTRUCTORS) target_include_directories(wivrn-server SYSTEM PRIVATE ${monado_SOURCE_DIR}/src/xrt/compositor/) target_include_directories(wivrn-server PRIVATE .) +target_link_libraries(wivrn-server PRIVATE CLI11::CLI11) + if(WIVRN_USE_NVENC) target_sources(wivrn-server PRIVATE encoder/video_encoder_nvenc.cpp) endif() diff --git a/server/dist/wivrn.service.in b/server/dist/wivrn.service.in index 3fff3a38..67b1b6e6 100644 --- a/server/dist/wivrn.service.in +++ b/server/dist/wivrn.service.in @@ -3,7 +3,7 @@ Description=OpenXR streaming server [Service] Type=simple -ExecStart=@CMAKE_INSTALL_PREFIX@/bin/wivrn-server +ExecStart=@CMAKE_INSTALL_PREFIX@/bin/wivrn-server --systemd CapabilityBoundingSet=CAP_SYS_NICE AmbientCapabilities=CAP_SYS_NICE LockPersonality=true diff --git a/server/main.cpp b/server/main.cpp index fa1379a8..b4d81683 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -17,6 +17,7 @@ #include "wivrn_ipc.h" #include "wivrn_sockets.h" +#include #include #include #include @@ -122,11 +123,13 @@ void waitpid_verbose(pid_t pid, const std::string & name) } } -int inner_main(int argc, char * argv[]) +int inner_main(int argc, char * argv[], bool use_systemd) { std::cerr << "WiVRn " << xrt::drivers::wivrn::git_version << " starting" << std::endl; #ifdef WIVRN_USE_SYSTEMD create_listen_socket(); +#else + assert(not systemd); #endif u_trace_marker_init(); @@ -176,7 +179,11 @@ int inner_main(int argc, char * argv[]) active_runtime runtime_setter; +#ifdef WIVRN_USE_SYSTEMD + pid_t client_pid = use_systemd ? start_unit_file() : fork_application(); +#else pid_t client_pid = fork_application(); +#endif pid_t server_pid = fork(); @@ -208,12 +215,6 @@ int inner_main(int argc, char * argv[]) // FIXME: synchronization fails on gfx pipeline setenv("XRT_COMPOSITOR_COMPUTE", "1", true); - // FIXME: use a proper library for cli options - if (argc == 2 and strcmp(argv[1], "-f") == 0) - { - configuration::set_config_file(argv[2]); - } - try { return ipc_server_main(argc, argv); @@ -338,32 +339,29 @@ int inner_main(int argc, char * argv[]) int main(int argc, char * argv[]) { - if (argc > 1) - { - if ((strcmp(argv[1], "--application") == 0)) - { - pid_t pid = exec_application(configuration::read_user_configuration()); - if (pid < 0) - { - return EXIT_FAILURE; - } - return EXIT_SUCCESS; - } - else if ((strcmp(argv[1], "-f") != 0 or argc < 3)) - { - std::cerr << "Usage: " << argv[0] << " [-f file]" << std::endl - << "\t -f file: configuration file to use" << std::endl; - if (strcmp(argv[1], "-h") != 0 and strcmp(argv[1], "--help") != 0) - { - return EXIT_FAILURE; - } - return EXIT_SUCCESS; - } - } + CLI::App app; + std::string config_file; + bool use_systemd = false; + app.add_option("-f", config_file, "configuration file")->option_text("FILE")->check(CLI::ExistingFile); +#ifdef WIVRN_USE_SYSTEMD + // --application should only be used from wivrn-application unit file + auto app_flag = app.add_flag("--application")->group(""); + app.add_flag("--systemd", use_systemd, "use systemd to launch user-configured application"); +#endif + + CLI11_PARSE(app, argc, argv); + + if (not config_file.empty()) + configuration::set_config_file(config_file); + +#ifdef WIVRN_USE_SYSTEMD + if (*app_flag) + return exec_application(configuration::read_user_configuration()); +#endif try { - return inner_main(argc, argv); + return inner_main(argc, argv, use_systemd); } catch (std::exception & e) { diff --git a/server/start_application.cpp b/server/start_application.cpp index 7af515c4..d1446e1d 100644 --- a/server/start_application.cpp +++ b/server/start_application.cpp @@ -9,7 +9,6 @@ #include "start_application.h" #include "driver/configuration.h" -#include "wivrn_config.h" #include #include @@ -21,26 +20,7 @@ #include #include #include -#endif -int exec_application(configuration config) -{ - if (config.application.empty()) - return 0; - - std::vector argv; - argv.reserve(config.application.size() + 1); - for (auto & arg: config.application) - argv.push_back(arg.data()); - argv.push_back(nullptr); - - execvp(config.application.front().c_str(), argv.data()); - - perror("Cannot start application"); - exit(EXIT_FAILURE); -} - -#ifdef WIVRN_USE_SYSTEMD int get_service_pid(const std::string & service_name, pid_t & pid) { sd_bus * bus = nullptr; @@ -185,7 +165,7 @@ finish: return is_active; } -pid_t fork_application() +pid_t start_unit_file() { std::string service_name = "wivrn-application.service"; pid_t pid; @@ -218,30 +198,46 @@ pid_t fork_application() return pid; } -#else +#endif pid_t fork_application() { auto config = configuration::read_user_configuration(); - if (!config.application.empty()) + if (config.application.empty()) + return 0; + + pid_t application_pid = fork(); + if (application_pid < 0) { - pid_t application_pid = fork(); - if (application_pid < 0) - { - throw std::system_error(errno, std::system_category(), "fork"); - } - - if (application_pid == 0) - { - // Start a new process group so that all processes started by the - // application can be signaled - setpgrp(); - - return exec_application(config); - } + throw std::system_error(errno, std::system_category(), "fork"); } - return 0; + if (application_pid == 0) + { + // Start a new process group so that all processes started by the + // application can be signaled + setpgrp(); + + return exec_application(config); + } + + return application_pid; +} + +int exec_application(configuration config) +{ + if (config.application.empty()) + return 0; + + std::vector argv; + argv.reserve(config.application.size() + 1); + for (auto & arg: config.application) + argv.push_back(arg.data()); + argv.push_back(nullptr); + + execvp(config.application.front().c_str(), argv.data()); + + perror("Cannot start application"); + exit(EXIT_FAILURE); } -#endif diff --git a/server/start_application.h b/server/start_application.h index 21859e8a..9372a495 100644 --- a/server/start_application.h +++ b/server/start_application.h @@ -1,8 +1,13 @@ #pragma once +#include "wivrn_config.h" + #include struct configuration; pid_t fork_application(); +#ifdef WIVRN_USE_SYSTEMD +pid_t start_unit_file(); +#endif int exec_application(configuration);