diff --git a/docs/configuration.md b/docs/configuration.md index 5e52937f..ffc1ec76 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1,6 +1,12 @@ # Configurable items -Configuration is done on server side, in `$XDG_CONFIG_HOME/wivrn/config.json` or if `$XDG_CONFIG_HOME` is not set, `$HOME/.config/wivrn/config.json`. +Configuration is done on server side. +Files are read from +- `/usr/share/wivrn/config.json` (where `/usr` is selected at configure time with `CMAKE_INSTALL_PREFIX`) +- `/etc/wivrn/config.json` +- `$XDG_CONFIG_HOME/wivrn/config.json` or if `$XDG_CONFIG_HOME` is not set, `$HOME/.config/wivrn/config.json`. + +Files later in the list replace top-level values from previous ones. All elements are optional and have default values. diff --git a/server/active_runtime.cpp b/server/active_runtime.cpp index 84022a88..532f0531 100644 --- a/server/active_runtime.cpp +++ b/server/active_runtime.cpp @@ -76,7 +76,7 @@ std::filesystem::path active_runtime::openvr_compat_path() return std::filesystem::path(); }, }, - configuration::read_user_configuration().openvr_compat_path); + configuration().openvr_compat_path); } static std::filesystem::path backup_name(std::filesystem::path file) diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp index 2f7fc4b6..7bcd7a0f 100644 --- a/server/driver/configuration.cpp +++ b/server/driver/configuration.cpp @@ -19,6 +19,8 @@ #define JSON_DISABLE_ENUM_SERIALIZATION 1 +#include "configuration.h" + #include #include #include @@ -31,10 +33,10 @@ #include "util/u_logging.h" -#include "configuration.h" #include "utils/xdg_base_directory.h" +#include "wivrn_config.h" -static std::filesystem::path config_file = xdg_config_home() / "wivrn" / "config.json"; +static std::filesystem::path config_file; static std::filesystem::path known_keys_file = xdg_config_home() / "wivrn" / "known_keys.json"; static std::filesystem::path cookie_file = xdg_config_home() / "wivrn" / "cookie"; @@ -65,12 +67,56 @@ void configuration::set_config_file(const std::filesystem::path & path) { config_file = path; } - -const std::filesystem::path & configuration::get_config_file() +std::filesystem::path configuration::get_config_file() { + if (config_file.empty()) + return xdg_config_home() / "wivrn" / "config.json"; return config_file; } +nlohmann::json configuration::read_configuration() +{ + if (config_file.empty()) + { + nlohmann::json merged; + for (std::filesystem::path prefix: { + std::filesystem::path(WIVRN_INSTALL_PREFIX "/share"), + std::filesystem::path("/etc"), + xdg_config_home(), + }) + { + auto path = prefix / "wivrn" / "config.json"; + if (std::filesystem::exists(path)) + { + try + { + for (const auto & [key, value]: nlohmann::json::parse(std::ifstream(path)).get()) + { + merged[key] = value; + } + } + catch (std::exception & e) + { + U_LOG_E("Invalid configuration file %s: %s", path.c_str(), e.what()); + } + } + } + return merged; + } + else + { + try + { + return nlohmann::json::parse(std::ifstream(config_file)); + } + catch (std::exception & e) + { + U_LOG_E("Invalid configuration file %s: %s", config_file.c_str(), e.what()); + return nlohmann::json(); + } + } +} + configuration::encoder parse_encoder(const nlohmann::json & item) { configuration::encoder e; @@ -94,85 +140,78 @@ configuration::encoder parse_encoder(const nlohmann::json & item) return e; } -configuration configuration::read_user_configuration() +configuration::configuration() { - configuration result; - if (not std::filesystem::exists(config_file)) - return result; - std::ifstream file(config_file); try { - auto json = nlohmann::json::parse(file); + auto json = read_configuration(); if (auto it = json.find("scale"); it != json.end()) { if (it->is_number()) - result.scale = std::array{*it, *it}; + scale = std::array{*it, *it}; else - result.scale = *it; + scale = *it; } if (auto it = json.find("grip-surface"); it != json.end()) { - result.grip_surface = *it; + grip_surface = *it; } if (auto it = json.find("bitrate"); it != json.end()) - result.bitrate = *it; + bitrate = *it; if (auto it = json.find("encoders"); it != json.end()) { for (const auto & encoder: *it) - result.encoders.push_back(parse_encoder(encoder)); + encoders.push_back(parse_encoder(encoder)); } if (auto it = json.find("encoder-passthrough"); it != json.end()) - result.encoder_passthrough = parse_encoder(*it); + encoder_passthrough = parse_encoder(*it); if (auto it = json.find("application"); it != json.end()) { if (it->is_string()) - result.application.push_back(*it); + application.push_back(*it); else { for (const auto & i: *it) - result.application.push_back(i); + application.push_back(i); } } if (auto it = json.find("debug-gui"); it != json.end()) - result.debug_gui = *it; + debug_gui = *it; if (auto it = json.find("use-steamvr-lh"); it != json.end()) - result.use_steamvr_lh = *it; + use_steamvr_lh = *it; if (auto it = json.find("tcp-only"); it != json.end()) - result.tcp_only = *it; + tcp_only = *it; else if (auto it = json.find("tcp_only"); it != json.end()) - result.tcp_only = *it; + tcp_only = *it; if (auto it = json.find("publish-service"); it != json.end()) { - result.publication = *it; - if (result.publication == service_publication(-1)) + publication = *it; + if (publication == service_publication(-1)) throw std::runtime_error("invalid service publication " + it->get()); } if (auto it = json.find("openvr-compat-path"); it != json.end()) { if (it->is_null()) - result.openvr_compat_path = nullptr; + openvr_compat_path = nullptr; else - result.openvr_compat_path = *it; + openvr_compat_path = *it; } } catch (const std::exception & e) { - U_LOG_E("Invalid configuration file: %s", e.what()); - return {}; + U_LOG_E("Configuration file error: %s", e.what()); } - - return result; } std::string server_cookie() diff --git a/server/driver/configuration.h b/server/driver/configuration.h index 86eea9de..53c0ba80 100644 --- a/server/driver/configuration.h +++ b/server/driver/configuration.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -68,8 +69,10 @@ struct configuration std::variant openvr_compat_path; static void set_config_file(const std::filesystem::path &); - static const std::filesystem::path & get_config_file(); - static configuration read_user_configuration(); + static std::filesystem::path get_config_file(); + + static nlohmann::json read_configuration(); + configuration(); }; std::string server_cookie(); diff --git a/server/driver/wivrn_connection.cpp b/server/driver/wivrn_connection.cpp index 76c4756a..327d19b8 100644 --- a/server/driver/wivrn_connection.cpp +++ b/server/driver/wivrn_connection.cpp @@ -88,7 +88,7 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function angles = grip_surface.value(); float deg_2_rad = std::numbers::pi / 180.0; diff --git a/server/driver/wivrn_hmd.cpp b/server/driver/wivrn_hmd.cpp index 18cbac36..51f11eaa 100644 --- a/server/driver/wivrn_hmd.cpp +++ b/server/driver/wivrn_hmd.cpp @@ -89,7 +89,7 @@ wivrn_hmd::wivrn_hmd(wivrn::wivrn_session * cnx, }, cnx(cnx) { - const auto config = configuration::read_user_configuration(); + const auto config = configuration(); auto eye_width = info.recommended_eye_width; eye_width = ((eye_width + 3) / 4) * 4; diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index 7a50da03..884f5775 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -179,7 +179,7 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr connection static_roles.hand_tracking.right = xdevs[xdev_count++] = &right_hand; #if WIVRN_FEATURE_STEAMVR_LIGHTHOUSE - auto use_steamvr_lh = configuration::read_user_configuration().use_steamvr_lh || std::getenv("WIVRN_USE_STEAMVR_LH"); + auto use_steamvr_lh = configuration().use_steamvr_lh || std::getenv("WIVRN_USE_STEAMVR_LH"); xrt_system_devices * lhdevs = NULL; if (use_steamvr_lh && steamvr_lh_create_devices(&lhdevs) == XRT_SUCCESS) diff --git a/server/encoder/encoder_settings.cpp b/server/encoder/encoder_settings.cpp index 1b44539e..0e974abc 100644 --- a/server/encoder/encoder_settings.cpp +++ b/server/encoder/encoder_settings.cpp @@ -330,14 +330,6 @@ static uint16_t align(uint16_t value, uint16_t alignment) std::vector get_encoder_settings(wivrn_vk_bundle & bundle, uint32_t & width, uint32_t & height, const from_headset::headset_info_packet & info) { configuration config; - try - { - config = configuration::read_user_configuration(); - } - catch (const std::exception & e) - { - U_LOG_E("Failed to read encoder configuration: %s", e.what()); - } if (config.encoders.empty()) config.encoders = get_encoder_default_settings(bundle, info.supported_codecs); if (not config.encoder_passthrough) diff --git a/server/main.cpp b/server/main.cpp index 98108a81..91329ea5 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -479,7 +479,7 @@ gboolean headset_connected_success(void *) expose_known_keys_on_dbus(); - start_server(configuration::read_user_configuration()); + start_server(configuration()); try { start_app(); @@ -855,9 +855,15 @@ void on_name_acquired(GDBusConnection * connection, const gchar * name, gpointer on_headset_info_packet({}); - std::ifstream file(configuration::get_config_file()); - std::string config{std::istreambuf_iterator(file), std::istreambuf_iterator()}; - + std::string config; + try + { + config = configuration::read_configuration().dump(); + } + catch (std::exception & e) + { + std::cerr << "Invalid configuration: " << e.what() << std::endl; + } wivrn_server_set_json_configuration(dbus_server, config.c_str()); expose_known_keys_on_dbus(); @@ -1035,11 +1041,11 @@ int main(int argc, char * argv[]) if (*no_publish) publication = wivrn::service_publication::none; else - publication = configuration::read_user_configuration().publication; + publication = configuration().publication; #if WIVRN_USE_SYSTEMD if (*app_flag) - return exec_application(configuration::read_user_configuration()); + return exec_application(configuration()); #endif try { diff --git a/server/start_application.cpp b/server/start_application.cpp index a2e4aebf..b3f09941 100644 --- a/server/start_application.cpp +++ b/server/start_application.cpp @@ -219,7 +219,7 @@ pid_t wivrn::start_unit_file() pid_t wivrn::fork_application() { - auto config = configuration::read_user_configuration(); + configuration config; if (config.application.empty()) return 0;