Use the default values in the configuration class instead of repeating them

This commit is contained in:
Guillaume Meunier
2026-07-27 23:45:26 +02:00
parent bf77b93a17
commit 79fed879e2
9 changed files with 95 additions and 70 deletions
+2 -1
View File
@@ -1404,7 +1404,8 @@ void application::initialize()
spaces[size_t(xr::spaces::view)] = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_VIEW);
spaces[size_t(xr::spaces::world)] = xr_session.create_reference_space(XR_REFERENCE_SPACE_TYPE_STAGE);
config.emplace(xr_system_id, xr_session);
config.emplace(xr_system_id, xr_session, application::get_config_path() / "client.json");
default_config.emplace(xr_system_id, xr_session);
// HTC face tracker fails if created later
// we can destroy it right away, it actually stores static handles
+7
View File
@@ -156,6 +156,7 @@ private:
std::chrono::nanoseconds last_scene_cpu_time;
std::optional<configuration> config;
std::optional<configuration> default_config;
boost::locale::generator gen;
boost::locale::gnu_gettext::messages_info messages_info;
@@ -432,6 +433,12 @@ public:
return *instance().config;
}
static configuration & get_default_config()
{
assert(instance().default_config);
return *instance().default_config;
}
static XrSessionState get_session_state()
{
return instance().session_state;
+16 -14
View File
@@ -218,29 +218,28 @@ void configuration::set_feature(feature f, bool state)
save();
}
configuration::configuration(xr::system & system, xr::session & session) :
default_refresh_rate([&session]() {
const auto & rates = session.get_refresh_rates();
float default_rate = rates.empty() ? 0 : rates[0];
for (auto rate: rates)
{
if (rate >= max_default_rate)
break;
default_rate = rate;
}
return default_rate;
}())
configuration::configuration(xr::system & system, xr::session & session)
{
passthrough_enabled = system.passthrough_supported() == xr::passthrough_type::color;
features[feature::hand_tracking] = system.hand_tracking_supported();
preferred_refresh_rate = default_refresh_rate;
const auto & rates = session.get_refresh_rates();
preferred_refresh_rate = rates.empty() ? 0 : rates[0];
for (auto rate: rates)
{
if (rate >= max_default_rate)
break;
preferred_refresh_rate = rate;
}
}
configuration::configuration(xr::system & system, xr::session & session, const std::filesystem::path & config_path) :
configuration(system, session)
{
try
{
simdjson::dom::parser parser;
simdjson::dom::element root = parser.load((application::get_config_path() / "client.json").native());
simdjson::dom::element root = parser.load(config_path.native());
for (simdjson::dom::object i: simdjson::dom::array(root["servers"]))
{
server_data data{
@@ -259,10 +258,13 @@ configuration::configuration(xr::system & system, xr::session & session) :
servers.emplace(data.service.txt["cookie"], data);
}
float default_refresh_rate = preferred_refresh_rate;
for (const auto & f: config_fields())
f.load(root, *this);
// refresh rate must be supported by this headset, otherwise fall back to the default
const auto & rates = session.get_refresh_rates();
if (preferred_refresh_rate != 0 and not utils::contains(rates, preferred_refresh_rate))
preferred_refresh_rate = default_refresh_rate;
+2 -3
View File
@@ -22,6 +22,7 @@
#include "wivrn_discover.h"
#include "wivrn_packets.h"
#include <filesystem>
#include <map>
#include <mutex>
#include <optional>
@@ -133,7 +134,7 @@ private:
public:
configuration(xr::system &, xr::session &);
configuration() = default;
configuration(xr::system &, xr::session &, const std::filesystem::path &);
void save();
@@ -150,6 +151,4 @@ public:
{
return max_bitrate(extended_config);
}
const float default_refresh_rate = 0;
};
+63 -50
View File
@@ -201,6 +201,17 @@ void render_settings(const wivrn::gui::settings_context & ctx, const char * card
ui::end_card();
ImGui::PopStyleVar();
}
template <typename Range, typename T>
std::optional<int> index(const Range & range, const T & value)
{
auto it = std::ranges::find(range, value);
if (it == range.end())
return std::nullopt;
else
return (it - range.begin());
}
} // namespace
namespace wivrn::gui
@@ -209,13 +220,13 @@ namespace wivrn::gui
void settings_video(const settings_context & ctx)
{
auto & config = ctx.config;
auto & default_config = ctx.default_config;
const std::string disconnect_tip = ctx.in_game ? _C("tooltip for disabled settings", "Disconnect to change this setting.") : std::string{};
std::vector<setting> list;
if (const auto rates = ctx.session.get_refresh_rates(); not rates.empty())
{
auto it = std::ranges::find(rates, config.default_refresh_rate);
int default_rate_index = it == rates.end() ? 0 : (it - rates.begin() + 1);
int default_rate_index = index(rates, default_config.preferred_refresh_rate).value_or(-1) + 1;
list.push_back({
.id = "##refresh",
@@ -264,7 +275,7 @@ void settings_video(const settings_context & ctx)
.v_min = 50,
.v_max = config.extended_config ? 350 : 150,
.fmt = "%d%%",
.default_int = 100,
.default_int = int(std::lround(default_config.resolution_scale * 100)),
.enabled = [&ctx] { return not ctx.in_game; },
.disabled_tooltip = disconnect_tip,
});
@@ -279,7 +290,7 @@ void settings_video(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.fps_divider == 2; },
.set_bool = [&ctx, &config](bool v) { config.fps_divider = v ? 2 : 1; config.save(); if (ctx.on_streaming_changed) ctx.on_streaming_changed(); },
.default_bool = false,
.default_bool = default_config.fps_divider == 2,
.enabled = [&config] { return config.preferred_refresh_rate != 0; },
.disabled_tooltip = _("Set a refresh rate other than auto to enable this setting."),
});
@@ -292,6 +303,7 @@ void settings_video(const settings_context & ctx)
void settings_streaming(const settings_context & ctx)
{
auto & config = ctx.config;
auto & default_config = ctx.default_config;
const std::string disconnect_tip = ctx.in_game ? _C("tooltip for disabled settings", "Disconnect to change this setting.") : std::string{};
std::vector<setting> list;
@@ -311,7 +323,7 @@ void settings_streaming(const settings_context & ctx)
.v_min = 0,
.v_max = 80,
.fmt = "%d%%",
.default_int = int(std::lround((1 - (config.check_feature(feature::eye_gaze) ? 0.3 : 0.5)) * 100)),
.default_int = int(std::lround((1 - default_config.get_stream_scale()) * 100)),
.enabled = [&ctx] { return not ctx.in_game; },
.disabled_tooltip = disconnect_tip,
});
@@ -343,12 +355,7 @@ void settings_streaming(const settings_context & ctx)
.label = _C("setting name", "Video codec"),
.description = _("How video is compressed before it is sent to the headset."),
.ui = ui_kind::combo,
.get_int = [&config, codecs] {
if (config.codec)
for (size_t i = 0; i < codecs.size(); ++i)
if (codecs[i] == *config.codec)
return int(i) + 1;
return 0; },
.get_int = [&config, codecs] { return config.codec ? index(codecs, *config.codec).value_or(-1) + 1 : 0; },
.set_int = [&config, codecs](int v) {
config.codec = v == 0 ? std::nullopt : std::optional(codecs[v - 1]);
config.save(); },
@@ -359,7 +366,7 @@ void settings_streaming(const settings_context & ctx)
opts.push_back(codec_name(c));
return opts; },
.title = _C("setting name", "Video codec"),
.default_int = 0,
.default_int = default_config.codec ? index(codecs, *default_config.codec).value_or(-1) + 1 : 0,
.enabled = [&ctx] { return not ctx.in_game; },
.disabled_tooltip = disconnect_tip,
});
@@ -373,7 +380,7 @@ void settings_streaming(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.bit_depth == 10; },
.set_bool = [&config](bool v) { config.bit_depth = v ? 10 : 8; config.save(); },
.default_bool = true,
.default_bool = default_config.bit_depth == 10,
.enabled = [&ctx] { return not ctx.in_game; },
.disabled_tooltip = disconnect_tip,
});
@@ -386,11 +393,11 @@ void settings_streaming(const settings_context & ctx)
.description = _("Video data rate sent to the headset."),
.ui = ui_kind::slider,
.get_int = [&config] { return int(config.bitrate_bps / mb); },
.set_int = [&ctx, &config](int v) { config.bitrate_bps = uint32_t(v) * 1'000'000; config.save(); if (ctx.on_streaming_changed) ctx.on_streaming_changed(); },
.set_int = [&ctx, &config](int v) { config.bitrate_bps = uint32_t(v) * mb; config.save(); if (ctx.on_streaming_changed) ctx.on_streaming_changed(); },
.v_min = 5,
.v_max = int(config.max_bitrate() / mb),
.fmt = "%d Mbit/s",
.default_int = 50,
.default_int = int(default_config.bitrate_bps / mb),
});
// in-stream: steer where foveation focuses quality
@@ -407,7 +414,7 @@ void settings_streaming(const settings_context & ctx)
config.save();
if (ctx.on_foveation_override_changed)
ctx.on_foveation_override_changed(); },
.default_bool = false,
.default_bool = default_config.override_foveation_enable,
});
list.push_back({
.id = "##fov_adjust",
@@ -440,6 +447,7 @@ void settings_streaming(const settings_context & ctx)
void settings_post_processing(const settings_context & ctx)
{
auto & config = ctx.config;
auto & default_config = ctx.default_config;
std::vector<setting> list;
if (application::get_openxr_post_processing_supported())
@@ -465,11 +473,7 @@ void settings_post_processing(const settings_context & ctx)
.label = std::move(label),
.description = std::move(desc),
.ui = ui_kind::combo,
.get_int = [&config, flags, member] {
for (size_t i = 0; i < flags.size(); ++i)
if (config.openxr_post_processing.*member == flags[i])
return int(i);
return 0; },
.get_int = [&config, flags, member] { return index(flags, config.openxr_post_processing.*member).value_or(0); },
.set_int = [&config, flags, member](int v) {
config.openxr_post_processing.*member = flags[v];
config.save(); },
@@ -479,7 +483,7 @@ void settings_post_processing(const settings_context & ctx)
o.push_back(flag_name(f));
return o; },
.title = std::move(title),
.default_int = 0,
.default_int = index(flags, config.openxr_post_processing.*member).value_or(0),
});
};
@@ -494,6 +498,7 @@ void settings_post_processing(const settings_context & ctx)
void settings_audio(const settings_context & ctx)
{
auto & config = ctx.config;
auto & default_config = ctx.default_config;
const std::string disconnect_tip = ctx.in_game ? _C("tooltip for disabled settings", "Disconnect to change this setting.") : std::string{};
std::vector<setting> list;
@@ -504,7 +509,7 @@ void settings_audio(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.check_feature(feature::microphone); },
.set_bool = [&config](bool v) { config.set_feature(feature::microphone, v); },
.default_bool = false,
.default_bool = default_config.check_feature(feature::microphone),
.enabled = [&ctx] { return not ctx.in_game; },
.disabled_tooltip = disconnect_tip,
});
@@ -516,7 +521,7 @@ void settings_audio(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.mic_unprocessed_audio; },
.set_bool = [&config](bool v) { config.mic_unprocessed_audio = v; config.save(); },
.default_bool = false,
.default_bool = default_config.mic_unprocessed_audio,
.enabled = [&ctx, &config] { return not ctx.in_game and config.check_feature(feature::microphone); },
.disabled_tooltip = disconnect_tip,
});
@@ -528,6 +533,7 @@ void settings_audio(const settings_context & ctx)
void settings_devices(const settings_context & ctx)
{
auto & config = application::get_config();
auto & default_config = ctx.default_config;
std::vector<setting> list;
list.push_back({
@@ -537,7 +543,7 @@ void settings_devices(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.forward_keyboard; },
.set_bool = [&config](bool v) { config.forward_keyboard = v; config.save(); },
.default_bool = false,
.default_bool = default_config.forward_keyboard,
});
list.push_back({
@@ -547,7 +553,7 @@ void settings_devices(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.forward_mouse; },
.set_bool = [&config](bool v) { config.forward_mouse = v; config.save(); },
.default_bool = false,
.default_bool = default_config.forward_mouse,
});
list.push_back({
@@ -557,7 +563,7 @@ void settings_devices(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.forward_gamepad; },
.set_bool = [&config](bool v) { config.forward_gamepad = v; config.save(); },
.default_bool = false,
.default_bool = default_config.forward_gamepad,
});
ui::page_header(_cS("page header title", "Devices"), _cS("page header subtitle", "Forward input devices to the PC."));
@@ -570,10 +576,11 @@ void settings_devices(const settings_context & ctx)
bool settings_tracking(const settings_context & ctx)
{
auto & config = ctx.config;
auto & default_config = ctx.default_config;
const std::string disconnect_tip = ctx.in_game ? _C("tooltip for disabled settings", "Disconnect to change this setting.") : std::string{};
std::vector<setting> list;
auto feature_toggle = [&](const char * id, std::string label, std::string desc, feature f, bool def) {
auto feature_toggle = [&](const char * id, std::string label, std::string desc, feature f) {
list.push_back({
.id = id,
.label = std::move(label),
@@ -581,20 +588,20 @@ bool settings_tracking(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config, f] { return config.check_feature(f); },
.set_bool = [&config, f](bool v) { config.set_feature(f, v); },
.default_bool = def,
.default_bool = default_config.check_feature(f),
.enabled = [&ctx] { return not ctx.in_game; },
.disabled_tooltip = disconnect_tip,
});
};
if (ctx.system.hand_tracking_supported())
feature_toggle("##hand", _C("setting name", "Hand tracking"), _("Track your hands for input when controllers are down."), feature::hand_tracking, true);
feature_toggle("##hand", _C("setting name", "Hand tracking"), _("Track your hands for input when controllers are down."), feature::hand_tracking);
if (application::get_eye_gaze_supported())
feature_toggle("##eye", _C("setting name", "Eye tracking"), _("Used by foveated encoding to focus quality where you look."), feature::eye_gaze, false);
feature_toggle("##eye", _C("setting name", "Eye tracking"), _("Used by foveated encoding to focus quality where you look."), feature::eye_gaze);
if (ctx.system.face_tracker_supported() != xr::face_tracker_type::none)
feature_toggle("##face", _C("setting name", "Face tracking"), _("Stream facial expressions to the PC."), feature::face_tracking, false);
feature_toggle("##face", _C("setting name", "Face tracking"), _("Stream facial expressions to the PC."), feature::face_tracking);
const auto body_tracker = ctx.system.body_tracker_supported();
@@ -614,7 +621,7 @@ bool settings_tracking(const settings_context & ctx)
{
if (body_tracker == xr::body_tracker_type::fb and bit > from_headset::body_part_mask::hip)
break;
body_parts_default.push_back(1);
body_parts_default.push_back((default_config.body_part_mask & std::to_underlying(bit)) != 0);
body_parts_names.push_back(name);
body_parts_bit.push_back(bit);
}
@@ -631,7 +638,7 @@ bool settings_tracking(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.check_feature(feature::body_tracking); },
.set_bool = [&config](bool v) { config.set_feature(feature::body_tracking, v); },
.default_bool = false,
.default_bool = default_config.check_feature(feature::body_tracking),
.enabled = [&ctx] { return not ctx.in_game; },
.disabled_tooltip = disconnect_tip,
});
@@ -667,6 +674,7 @@ bool settings_tracking(const settings_context & ctx)
void settings_system(const settings_context & ctx)
{
auto & config = ctx.config;
auto & default_config = ctx.default_config;
std::vector<setting> list;
auto language_name = [](const std::locale & loc = std::locale()) {
@@ -683,17 +691,20 @@ void settings_system(const settings_context & ctx)
}
std::ranges::sort(languages, [](auto & l, auto & r) { return std::get<0>(l) < std::get<0>(r); });
auto language_index = [languages](const configuration & cfg) {
if (cfg.locale != "")
for (size_t i = 0; i < languages.size(); ++i)
if (std::get<2>(languages[i]) == cfg.locale)
return int(i) + 1;
return 0;
};
list.push_back({
.id = "##language",
.label = _("Language"),
.description = _C("setting name", "Interface language."),
.ui = ui_kind::combo,
.get_int = [&config, languages] {
if (not config.locale.empty())
for (size_t i = 0; i < languages.size(); ++i)
if (std::get<2>(languages[i]) == config.locale)
return int(i) + 1;
return 0; },
.get_int = [&config, language_index] { return language_index(config); },
.set_int = [&config, languages](int v) {
config.locale = v == 0 ? "" : std::get<2>(languages[v - 1]);
config.save();
@@ -705,7 +716,7 @@ void settings_system(const settings_context & ctx)
opts.push_back(lang);
return opts; },
.title = _("Language"),
.default_int = 0,
.default_int = language_index(default_config),
});
list.push_back({
@@ -715,7 +726,7 @@ void settings_system(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.extended_config; },
.set_bool = [&config](bool v) { config.extended_config = v; config.save(); },
.default_bool = false,
.default_bool = default_config.extended_config,
});
if (ctx.instance.has_extension(XR_EXT_PERFORMANCE_SETTINGS_EXTENSION_NAME))
@@ -727,7 +738,7 @@ void settings_system(const settings_context & ctx)
.ui = ui_kind::toggle,
.get_bool = [&config] { return config.high_power_mode; },
.set_bool = [&config](bool v) { config.high_power_mode = v; config.save(); },
.default_bool = true,
.default_bool = default_config.high_power_mode,
});
}
@@ -749,7 +760,7 @@ void settings_system(const settings_context & ctx)
config.enable_stream_gui = v;
config.save();
} },
.default_bool = true,
.default_bool = default_config.enable_stream_gui,
});
ui::page_header(_cS("page header title", "System"), _cS("page header suibtitle", "Language and advanced options."));
@@ -763,6 +774,7 @@ void settings_theme(const settings_context & ctx)
{
ui::theme & theme = ui::current();
auto & config = ctx.config;
auto & default_config = ctx.default_config;
ui::page_header(_cS("page header title", "Theme"), _cS("page header subtitle", "Accent color, palette and sizing of the interface."));
@@ -797,14 +809,15 @@ void settings_theme(const settings_context & ctx)
preset_items.push_back({p.localized_name.c_str()});
// seed the selection from the saved preset so the box matches what is applied
static int preset = [&] {
auto f = [&](const configuration & cfg) {
for (int i = 0; i < int(preset_list.size()); ++i)
if (preset_list[i].name == config.theme_preset)
if (preset_list[i].name == cfg.theme_preset)
return i;
return 1; // dark_default
}();
};
static int preset = f(config);
static const int preset_default = f(default_config);
static const int preset_default = 1;
ui::setting_label(_cS("setting name", "Preset"), _cS("setting description", "Surface and background palette"), control_w);
if (ui::combo("##preset", _("Theme preset"), preset_items, &preset, control_w, &preset_default))
{
@@ -823,7 +836,7 @@ void settings_theme(const settings_context & ctx)
ui::row_separator();
// Rounding
static const int rounding_default = 8;
static const int rounding_default = default_config.theme_rounding;
int rounding = int(theme.rounding);
ui::setting_label(_cS("setting label", "Rounding"), _cS("setting description", "Corner radius of controls"), control_w);
if (ui::slider_int("##rounding", &rounding, 0, 20, "%d px", {control_w, 0}, &rounding_default))
@@ -861,7 +874,7 @@ void settings_theme(const settings_context & ctx)
ui::row_separator();
// Panel transparency, independent of the selected preset
static const int opacity_default = 75;
static const int opacity_default = int(default_config.theme_background_alpha * 100 + 0.5);
int opacity = int(ui::background_alpha() * 100 + 0.5);
ui::setting_label(_cS("setting name", "Panel opacity"), _cS("setting description", "Opacity of the panel and card backgrounds"), control_w);
if (ui::slider_int("##opacity", &opacity, 20, 100, "%d%%", {control_w, 0}, &opacity_default))
+1
View File
@@ -39,6 +39,7 @@ namespace wivrn::gui
struct settings_context
{
configuration & config;
configuration & default_config;
xr::instance & instance;
xr::session & session;
xr::system & system;
+2 -2
View File
@@ -1122,7 +1122,7 @@ void scenes::lobby::on_focused()
catch (std::exception & e)
{
spdlog::warn("Cannot load environment from {}: {}, reverting to default", config.environment_model, e.what());
config.environment_model = configuration{}.environment_model;
config.environment_model = application::get_default_config().environment_model;
lobby_entity = add_gltf(config.environment_model, layer_lobby).first;
config.save();
}
@@ -1323,7 +1323,7 @@ void scenes::lobby::on_focused()
.gltf_url = "default",
.builtin = true,
.override_order = -1,
.local_gltf_path = configuration{}.environment_model,
.local_gltf_path = application::get_default_config().environment_model,
.screenshot = imgui_ctx->load_texture("assets://default-environment.ktx2")});
std::ranges::sort(local_environments, std::less{});
+1
View File
@@ -30,6 +30,7 @@ wivrn::gui::settings_context lobby_settings_context(xr::instance & instance, xr:
{
return {
.config = application::get_config(),
.default_config = application::get_default_config(),
.instance = instance,
.session = session,
.system = system,
+1
View File
@@ -404,6 +404,7 @@ void scenes::stream::gui_settings(float)
// same pages as the lobby, with in_game enabling the in-stream controls
wivrn::gui::settings_context ctx{
.config = application::get_config(),
.default_config = application::get_default_config(),
.instance = instance,
.session = session,
.system = system,