aboutsummaryrefslogtreecommitdiff
path: root/src/Config.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Config.cpp')
-rw-r--r--src/Config.cpp208
1 files changed, 186 insertions, 22 deletions
diff --git a/src/Config.cpp b/src/Config.cpp
index 112688a..734f827 100644
--- a/src/Config.cpp
+++ b/src/Config.cpp
@@ -1,41 +1,149 @@
#include "../include/Config.hpp"
#include "../include/Utils.hpp"
#include "../include/GsrInfo.hpp"
+#include "../include/GlobalHotkeys.hpp"
#include <variant>
#include <limits.h>
#include <inttypes.h>
#include <libgen.h>
+#include <string.h>
+#include <assert.h>
+#include <mglpp/window/Keyboard.hpp>
#define FORMAT_I32 "%" PRIi32
#define FORMAT_I64 "%" PRIi64
#define FORMAT_U32 "%" PRIu32
-#define CONFIG_FILE_VERSION 1
-
namespace gsr {
- Config::Config(const GsrInfo &gsr_info) {
- const std::string default_save_directory = get_videos_dir();
+ static std::vector<mgl::Keyboard::Key> hotkey_modifiers_to_mgl_keys(uint32_t modifiers) {
+ std::vector<mgl::Keyboard::Key> result;
+ if(modifiers & HOTKEY_MOD_LCTRL)
+ result.push_back(mgl::Keyboard::LControl);
+ if(modifiers & HOTKEY_MOD_LSHIFT)
+ result.push_back(mgl::Keyboard::LShift);
+ if(modifiers & HOTKEY_MOD_LALT)
+ result.push_back(mgl::Keyboard::LAlt);
+ if(modifiers & HOTKEY_MOD_LSUPER)
+ result.push_back(mgl::Keyboard::LSystem);
+ if(modifiers & HOTKEY_MOD_RCTRL)
+ result.push_back(mgl::Keyboard::RControl);
+ if(modifiers & HOTKEY_MOD_RSHIFT)
+ result.push_back(mgl::Keyboard::RShift);
+ if(modifiers & HOTKEY_MOD_RALT)
+ result.push_back(mgl::Keyboard::RAlt);
+ if(modifiers & HOTKEY_MOD_RSUPER)
+ result.push_back(mgl::Keyboard::RSystem);
+ return result;
+ }
+
+ static void string_remove_all(std::string &str, const std::string &substr) {
+ size_t index = 0;
+ while(true) {
+ index = str.find(substr, index);
+ if(index == std::string::npos)
+ break;
+ str.erase(index, substr.size());
+ }
+ }
+
+ ReplayStartupMode replay_startup_string_to_type(const char *startup_mode_str) {
+ if(strcmp(startup_mode_str, "dont_turn_on_automatically") == 0)
+ return ReplayStartupMode::DONT_TURN_ON_AUTOMATICALLY;
+ else if(strcmp(startup_mode_str, "turn_on_at_system_startup") == 0)
+ return ReplayStartupMode::TURN_ON_AT_SYSTEM_STARTUP;
+ else if(strcmp(startup_mode_str, "turn_on_at_fullscreen") == 0)
+ return ReplayStartupMode::TURN_ON_AT_FULLSCREEN;
+ else if(strcmp(startup_mode_str, "turn_on_at_power_supply_connected") == 0)
+ return ReplayStartupMode::TURN_ON_AT_POWER_SUPPLY_CONNECTED;
+ else
+ return ReplayStartupMode::DONT_TURN_ON_AUTOMATICALLY;
+ }
+
+ bool ConfigHotkey::operator==(const ConfigHotkey &other) const {
+ return key == other.key && modifiers == other.modifiers;
+ }
+
+ bool ConfigHotkey::operator!=(const ConfigHotkey &other) const {
+ return !operator==(other);
+ }
+
+ std::string ConfigHotkey::to_string(bool spaces, bool modifier_side) const {
+ std::string result;
+
+ const std::vector<mgl::Keyboard::Key> modifier_keys = hotkey_modifiers_to_mgl_keys(modifiers);
+ std::string modifier_str;
+ for(const mgl::Keyboard::Key modifier_key : modifier_keys) {
+ if(!result.empty()) {
+ if(spaces)
+ result += " + ";
+ else
+ result += "+";
+ }
+
+ modifier_str = mgl::Keyboard::key_to_string(modifier_key);
+ if(!modifier_side) {
+ string_remove_all(modifier_str, "Left");
+ string_remove_all(modifier_str, "Right");
+ }
+ result += modifier_str;
+ }
+
+ if(key != 0) {
+ if(!result.empty()) {
+ if(spaces)
+ result += " + ";
+ else
+ result += "+";
+ }
+ result += mgl::Keyboard::key_to_string((mgl::Keyboard::Key)key);
+ }
+
+ return result;
+ }
+
+ Config::Config(const SupportedCaptureOptions &capture_options) {
+ const std::string default_videos_save_directory = get_videos_dir();
+ const std::string default_pictures_save_directory = get_pictures_dir();
+
+ set_hotkeys_to_default();
streaming_config.record_options.video_quality = "custom";
streaming_config.record_options.audio_tracks.push_back("default_output");
streaming_config.record_options.video_bitrate = 15000;
- record_config.save_directory = default_save_directory;
+ record_config.save_directory = default_videos_save_directory;
record_config.record_options.audio_tracks.push_back("default_output");
record_config.record_options.video_bitrate = 45000;
replay_config.record_options.video_quality = "custom";
- replay_config.save_directory = default_save_directory;
+ replay_config.save_directory = default_videos_save_directory;
replay_config.record_options.audio_tracks.push_back("default_output");
replay_config.record_options.video_bitrate = 45000;
- if(!gsr_info.supported_capture_options.monitors.empty()) {
- streaming_config.record_options.record_area_option = gsr_info.supported_capture_options.monitors.front().name;
- record_config.record_options.record_area_option = gsr_info.supported_capture_options.monitors.front().name;
- replay_config.record_options.record_area_option = gsr_info.supported_capture_options.monitors.front().name;
+ screenshot_config.save_directory = default_pictures_save_directory;
+
+ if(!capture_options.monitors.empty()) {
+ streaming_config.record_options.record_area_option = capture_options.monitors.front().name;
+ record_config.record_options.record_area_option = capture_options.monitors.front().name;
+ replay_config.record_options.record_area_option = capture_options.monitors.front().name;
+ screenshot_config.record_area_option = capture_options.monitors.front().name;
}
}
+ void Config::set_hotkeys_to_default() {
+ streaming_config.start_stop_hotkey = {mgl::Keyboard::F8, HOTKEY_MOD_LALT};
+
+ record_config.start_stop_hotkey = {mgl::Keyboard::F9, HOTKEY_MOD_LALT};
+ record_config.pause_unpause_hotkey = {mgl::Keyboard::F7, HOTKEY_MOD_LALT};
+
+ replay_config.start_stop_hotkey = {mgl::Keyboard::F10, HOTKEY_MOD_LALT | HOTKEY_MOD_LSHIFT};
+ replay_config.save_hotkey = {mgl::Keyboard::F10, HOTKEY_MOD_LALT};
+
+ screenshot_config.take_screenshot_hotkey = {mgl::Keyboard::F1, HOTKEY_MOD_LALT};
+
+ main_config.show_hide_hotkey = {mgl::Keyboard::Z, HOTKEY_MOD_LALT};
+ }
+
static std::optional<KeyValue> parse_key_value(std::string_view line) {
const size_t space_index = line.find(' ');
if(space_index == std::string_view::npos)
@@ -49,6 +157,10 @@ namespace gsr {
return {
{"main.config_file_version", &config.main_config.config_file_version},
{"main.software_encoding_warning_shown", &config.main_config.software_encoding_warning_shown},
+ {"main.hotkeys_enable_option", &config.main_config.hotkeys_enable_option},
+ {"main.joystick_hotkeys_enable_option", &config.main_config.joystick_hotkeys_enable_option},
+ {"main.tint_color", &config.main_config.tint_color},
+ {"main.show_hide_hotkey", &config.main_config.show_hide_hotkey},
{"streaming.record_options.record_area_option", &config.streaming_config.record_options.record_area_option},
{"streaming.record_options.record_area_width", &config.streaming_config.record_options.record_area_width},
@@ -77,7 +189,7 @@ namespace gsr {
{"streaming.twitch.key", &config.streaming_config.twitch.stream_key},
{"streaming.custom.url", &config.streaming_config.custom.url},
{"streaming.custom.container", &config.streaming_config.custom.container},
- {"streaming.start_stop_recording_hotkey", &config.streaming_config.start_stop_recording_hotkey},
+ {"streaming.start_stop_hotkey", &config.streaming_config.start_stop_hotkey},
{"record.record_options.record_area_option", &config.record_config.record_options.record_area_option},
{"record.record_options.record_area_width", &config.record_config.record_options.record_area_width},
@@ -104,8 +216,8 @@ namespace gsr {
{"record.show_video_saved_notifications", &config.record_config.show_video_saved_notifications},
{"record.save_directory", &config.record_config.save_directory},
{"record.container", &config.record_config.container},
- {"record.start_stop_recording_hotkey", &config.record_config.start_stop_recording_hotkey},
- {"record.pause_unpause_recording_hotkey", &config.record_config.pause_unpause_recording_hotkey},
+ {"record.start_stop_hotkey", &config.record_config.start_stop_hotkey},
+ {"record.pause_unpause_hotkey", &config.record_config.pause_unpause_hotkey},
{"replay.record_options.record_area_option", &config.replay_config.record_options.record_area_option},
{"replay.record_options.record_area_width", &config.replay_config.record_options.record_area_width},
@@ -129,18 +241,66 @@ namespace gsr {
{"replay.record_options.restore_portal_session", &config.replay_config.record_options.restore_portal_session},
{"replay.turn_on_replay_automatically_mode", &config.replay_config.turn_on_replay_automatically_mode},
{"replay.save_video_in_game_folder", &config.replay_config.save_video_in_game_folder},
+ {"replay.restart_replay_on_save", &config.replay_config.restart_replay_on_save},
{"replay.show_replay_started_notifications", &config.replay_config.show_replay_started_notifications},
{"replay.show_replay_stopped_notifications", &config.replay_config.show_replay_stopped_notifications},
{"replay.show_replay_saved_notifications", &config.replay_config.show_replay_saved_notifications},
{"replay.save_directory", &config.replay_config.save_directory},
{"replay.container", &config.replay_config.container},
{"replay.time", &config.replay_config.replay_time},
- {"replay.start_stop_recording_hotkey", &config.replay_config.start_stop_recording_hotkey},
- {"replay.save_recording_hotkey", &config.replay_config.save_recording_hotkey}
+ {"replay.start_stop_hotkey", &config.replay_config.start_stop_hotkey},
+ {"replay.save_hotkey", &config.replay_config.save_hotkey},
+
+ {"screenshot.record_area_option", &config.screenshot_config.record_area_option},
+ {"screenshot.image_width", &config.screenshot_config.image_width},
+ {"screenshot.image_height", &config.screenshot_config.image_height},
+ {"screenshot.change_image_resolution", &config.screenshot_config.change_image_resolution},
+ {"screenshot.image_quality", &config.screenshot_config.image_quality},
+ {"screenshot.image_format", &config.screenshot_config.image_format},
+ {"screenshot.record_cursor", &config.screenshot_config.record_cursor},
+ {"screenshot.restore_portal_session", &config.screenshot_config.restore_portal_session},
+ {"screenshot.save_screenshot_in_game_folder", &config.screenshot_config.save_screenshot_in_game_folder},
+ {"screenshot.show_screenshot_saved_notifications", &config.screenshot_config.show_screenshot_saved_notifications},
+ {"screenshot.save_directory", &config.screenshot_config.save_directory},
+ {"screenshot.take_screenshot_hotkey", &config.screenshot_config.take_screenshot_hotkey}
};
}
- std::optional<Config> read_config(const GsrInfo &gsr_info) {
+ bool Config::operator==(const Config &other) {
+ const auto config_options = get_config_options(*this);
+ const auto config_options_other = get_config_options(const_cast<Config&>(other));
+ for(auto it : config_options) {
+ auto it_other = config_options_other.find(it.first);
+ if(it_other == config_options_other.end() || it_other->second.index() != it.second.index())
+ return false;
+
+ if(std::holds_alternative<bool*>(it.second)) {
+ if(*std::get<bool*>(it.second) != *std::get<bool*>(it_other->second))
+ return false;
+ } else if(std::holds_alternative<std::string*>(it.second)) {
+ if(*std::get<std::string*>(it.second) != *std::get<std::string*>(it_other->second))
+ return false;
+ } else if(std::holds_alternative<int32_t*>(it.second)) {
+ if(*std::get<int32_t*>(it.second) != *std::get<int32_t*>(it_other->second))
+ return false;
+ } else if(std::holds_alternative<ConfigHotkey*>(it.second)) {
+ if(*std::get<ConfigHotkey*>(it.second) != *std::get<ConfigHotkey*>(it_other->second))
+ return false;
+ } else if(std::holds_alternative<std::vector<std::string>*>(it.second)) {
+ if(*std::get<std::vector<std::string>*>(it.second) != *std::get<std::vector<std::string>*>(it_other->second))
+ return false;
+ } else {
+ assert(false);
+ }
+ }
+ return true;
+ }
+
+ bool Config::operator!=(const Config &other) {
+ return !operator==(other);
+ }
+
+ std::optional<Config> read_config(const SupportedCaptureOptions &capture_options) {
std::optional<Config> config;
const std::string config_path = get_config_dir() + "/config_ui";
@@ -150,7 +310,7 @@ namespace gsr {
return config;
}
- config = Config(gsr_info);
+ config = Config(capture_options);
config->streaming_config.record_options.audio_tracks.clear();
config->record_config.record_options.audio_tracks.clear();
config->replay_config.record_options.audio_tracks.clear();
@@ -185,20 +345,22 @@ namespace gsr {
} else if(std::holds_alternative<ConfigHotkey*>(it->second)) {
std::string value_str(key_value->value);
ConfigHotkey *config_hotkey = std::get<ConfigHotkey*>(it->second);
- if(sscanf(value_str.c_str(), FORMAT_I64 " " FORMAT_U32, &config_hotkey->keysym, &config_hotkey->modifiers) != 2) {
+ if(sscanf(value_str.c_str(), FORMAT_I64 " " FORMAT_U32, &config_hotkey->key, &config_hotkey->modifiers) != 2) {
fprintf(stderr, "Warning: Invalid config option value for %.*s\n", (int)key_value->key.size(), key_value->key.data());
- config_hotkey->keysym = 0;
+ config_hotkey->key = 0;
config_hotkey->modifiers = 0;
}
} else if(std::holds_alternative<std::vector<std::string>*>(it->second)) {
std::string array_value(key_value->value);
std::get<std::vector<std::string>*>(it->second)->push_back(std::move(array_value));
+ } else {
+ assert(false);
}
return true;
});
- if(config->main_config.config_file_version != CONFIG_FILE_VERSION) {
+ if(config->main_config.config_file_version != GSR_CONFIG_FILE_VERSION) {
fprintf(stderr, "Info: the config file is outdated, resetting it\n");
config = std::nullopt;
}
@@ -207,7 +369,7 @@ namespace gsr {
}
void save_config(Config &config) {
- config.main_config.config_file_version = CONFIG_FILE_VERSION;
+ config.main_config.config_file_version = GSR_CONFIG_FILE_VERSION;
const std::string config_path = get_config_dir() + "/config_ui";
@@ -236,12 +398,14 @@ namespace gsr {
fprintf(file, "%.*s " FORMAT_I32 "\n", (int)it.first.size(), it.first.data(), *std::get<int32_t*>(it.second));
} else if(std::holds_alternative<ConfigHotkey*>(it.second)) {
const ConfigHotkey *config_hotkey = std::get<ConfigHotkey*>(it.second);
- fprintf(file, "%.*s " FORMAT_I64 " " FORMAT_U32 "\n", (int)it.first.size(), it.first.data(), config_hotkey->keysym, config_hotkey->modifiers);
+ fprintf(file, "%.*s " FORMAT_I64 " " FORMAT_U32 "\n", (int)it.first.size(), it.first.data(), config_hotkey->key, config_hotkey->modifiers);
} else if(std::holds_alternative<std::vector<std::string>*>(it.second)) {
std::vector<std::string> *array = std::get<std::vector<std::string>*>(it.second);
for(const std::string &value : *array) {
fprintf(file, "%.*s %s\n", (int)it.first.size(), it.first.data(), value.c_str());
}
+ } else {
+ assert(false);
}
}