Skip to content

Commit 5227897

Browse files
committed
feat: improve config reading/writing
1 parent 43639f0 commit 5227897

11 files changed

Lines changed: 86 additions & 67 deletions

File tree

src/common/config_app.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ std::string config_app::generate_config_string(const GlobalAppSettings& settings
7171
}
7272

7373
void config_app::create(const std::filesystem::path& filepath, const GlobalAppSettings& settings) {
74-
std::ofstream output(filepath);
75-
output << generate_config_string(settings, false);
74+
config_base::write_config_string(filepath, generate_config_string(settings, false));
7675
}
7776

7877
std::string config_app::export_shareable(const GlobalAppSettings& settings) {
@@ -101,12 +100,9 @@ GlobalAppSettings config_app::parse(const std::string& config_content) {
101100
}
102101

103102
GlobalAppSettings config_app::parse(const std::filesystem::path& config_filepath) {
104-
std::ifstream file_stream(config_filepath);
105-
auto config_map = config_base::read_config_map(file_stream);
103+
auto settings = parse(config_base::read_config_file(config_filepath).value_or(""));
106104

107-
auto settings = parse_from_map(config_map);
108-
109-
// recreate the config file using the parsed values (keeps nice formatting)
105+
// write formatted file
110106
create(config_filepath, settings);
111107

112108
return settings;

src/common/config_base.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,37 @@ namespace config_base {
6060
}
6161
}
6262

63+
inline std::mutex config_file_mutex;
64+
65+
inline std::optional<std::string> read_config_file(const std::filesystem::path& filepath) {
66+
std::lock_guard lock(config_file_mutex);
67+
68+
std::ifstream file(filepath);
69+
if (!file)
70+
return {};
71+
72+
return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
73+
}
74+
75+
inline bool write_config_string(const std::filesystem::path& filepath, const std::string& content) {
76+
std::lock_guard lock(config_file_mutex);
77+
78+
// don't write if the content is the same
79+
{
80+
std::ifstream existing(filepath);
81+
if (existing) {
82+
std::string current((std::istreambuf_iterator<char>(existing)), std::istreambuf_iterator<char>());
83+
if (current == content)
84+
return false;
85+
}
86+
}
87+
88+
std::ofstream output(filepath);
89+
output << content;
90+
91+
return true;
92+
}
93+
6394
template<typename ConfigType>
6495
ConfigType load_config(
6596
const std::filesystem::path& config_path,

src/common/config_blur.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ std::string config_blur::generate_config_string(const BlurSettings& settings, bo
181181
}
182182

183183
void config_blur::create(const std::filesystem::path& filepath, const BlurSettings& current_settings) {
184-
std::ofstream output(filepath);
185-
output << generate_config_string(current_settings, false);
184+
config_base::write_config_string(filepath, generate_config_string(current_settings, false));
186185
}
187186

188187
std::string config_blur::export_concise(const BlurSettings& settings) {
@@ -281,14 +280,15 @@ BlurSettings config_blur::parse(const std::string& config_content) {
281280
}
282281

283282
BlurSettings config_blur::parse(const std::filesystem::path& config_filepath) {
284-
std::ifstream file_stream(config_filepath);
285-
auto config_map = config_base::read_config_map(file_stream);
286-
return parse_from_map(config_map, config_filepath);
283+
auto settings = parse(config_base::read_config_file(config_filepath).value_or(""));
284+
285+
// write formatted file
286+
create(config_filepath, settings);
287+
288+
return settings;
287289
}
288290

289-
BlurSettings config_blur::parse_from_map(
290-
const std::map<std::string, std::string>& config_map, const std::optional<std::filesystem::path>& config_filepath
291-
) {
291+
BlurSettings config_blur::parse_from_map(const std::map<std::string, std::string>& config_map) {
292292
BlurSettings settings;
293293

294294
config_base::extract_config_value(config_map, "blur", settings.blur);
@@ -383,11 +383,6 @@ BlurSettings config_blur::parse_from_map(
383383
u::verify_gpu_encoding(settings);
384384
u::set_fastest_devices(settings);
385385

386-
if (config_filepath) {
387-
// rewrite config with proper structure and default values
388-
create(*config_filepath, settings);
389-
}
390-
391386
return settings;
392387
}
393388

src/common/config_blur.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,7 @@ namespace config_blur {
152152

153153
BlurSettings parse(const std::string& config_content);
154154
BlurSettings parse(const std::filesystem::path& config_filepath);
155-
BlurSettings parse_from_map(
156-
const std::map<std::string, std::string>& config_map,
157-
const std::optional<std::filesystem::path>& config_filepath = {}
158-
);
155+
BlurSettings parse_from_map(const std::map<std::string, std::string>& config_map);
159156

160157
BlurSettings parse_global_config();
161158

src/common/config_presets.cpp

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
#include "config_base.h"
33

44
namespace {
5-
constexpr auto CACHE_RELOAD_INTERVAL = std::chrono::seconds(5);
6-
7-
std::mutex cache_mutex;
8-
PresetSettings cached_config;
9-
std::optional<std::chrono::steady_clock::time_point> cache_time;
10-
115
std::vector<std::string> get_ffmpeg_args(std::string params_str, int quality) {
126
// replace quality placeholder
137
params_str = u::replace_all(params_str, "{quality}", std::to_string(quality));
@@ -108,20 +102,17 @@ std::string config_presets::generate_config_string(const PresetSettings& setting
108102
}
109103

110104
void config_presets::create(const std::filesystem::path& filepath, const PresetSettings& current_settings) {
111-
std::ofstream output(filepath);
112-
output << generate_config_string(current_settings);
105+
config_base::write_config_string(filepath, generate_config_string(current_settings));
113106
}
114107

115108
PresetSettings config_presets::parse(const std::filesystem::path& config_filepath) {
116-
std::ifstream file(config_filepath);
117-
if (!file)
109+
auto content = config_base::read_config_file(config_filepath);
110+
if (!content)
118111
return DEFAULT_CONFIG; // defaults if file couldn't be opened
119112

120-
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
121-
122-
auto settings = parse(content);
113+
auto settings = parse(*content);
123114

124-
// recreate the config file using the parsed values (keeps nice formatting)
115+
// write formatted file
125116
create(config_filepath, settings);
126117

127118
return settings;
@@ -210,33 +201,26 @@ std::filesystem::path config_presets::get_preset_config_path() {
210201
}
211202

212203
PresetSettings config_presets::get_preset_config() {
213-
std::lock_guard lock(cache_mutex);
214-
215-
auto now = std::chrono::steady_clock::now();
216-
if (!cache_time || now - *cache_time >= CACHE_RELOAD_INTERVAL) {
217-
cached_config = config_base::load_config<PresetSettings>(get_preset_config_path(), create, parse);
218-
cache_time = now;
219-
}
220-
221-
return cached_config;
204+
return config_base::load_config<PresetSettings>(get_preset_config_path(), create, parse);
222205
}
223206

224207
void config_presets::save(const PresetSettings& settings) {
225208
create(get_preset_config_path(), settings);
226-
227-
std::lock_guard lock(cache_mutex);
228-
cached_config = settings;
229-
cache_time = std::chrono::steady_clock::now();
230209
}
231210

232211
std::vector<config_presets::PresetDetails> config_presets::get_available_presets(
233212
bool gpu_encoding, const std::string& gpu_type
213+
) {
214+
return get_available_presets(get_preset_config(), gpu_encoding, gpu_type);
215+
}
216+
217+
std::vector<config_presets::PresetDetails> config_presets::get_available_presets(
218+
const PresetSettings& config, bool gpu_encoding, const std::string& gpu_type
234219
) {
235220
std::vector<PresetDetails> available_presets;
236221

237222
std::string type_to_check = gpu_encoding ? gpu_type : "cpu";
238223

239-
PresetSettings config = get_preset_config();
240224
const auto* preset_group = config.find_preset_group(type_to_check);
241225

242226
if (preset_group) {

src/common/config_presets.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ namespace config_presets {
126126

127127
PresetSettings get_preset_config();
128128

129-
// writes the presets to disk and refreshes the cache get_preset_config reads from, so renders started right
130-
// after saving use the new presets
131129
void save(const PresetSettings& settings);
132130

133131
struct PresetDetails {
@@ -136,6 +134,9 @@ namespace config_presets {
136134
};
137135

138136
std::vector<PresetDetails> get_available_presets(bool gpu_encoding, const std::string& gpu_type);
137+
std::vector<PresetDetails> get_available_presets(
138+
const PresetSettings& config, bool gpu_encoding, const std::string& gpu_type
139+
);
139140

140141
std::vector<std::string> get_preset_params(const std::string& gpu_type, const std::string& preset, int quality);
141142
std::vector<std::string> get_preset_params(

src/common/utils.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,16 @@ std::set<std::string> u::get_available_codecs(const std::set<std::string>& codec
609609
}
610610

611611
std::vector<std::string> u::get_supported_presets(bool gpu_encoding, const std::string& gpu_type) {
612+
return get_supported_presets(config_presets::get_preset_config(), gpu_encoding, gpu_type);
613+
}
614+
615+
std::vector<std::string> u::get_supported_presets(
616+
const PresetSettings& presets, bool gpu_encoding, const std::string& gpu_type
617+
) {
612618
if (!init_hw)
613619
get_hardware_encoding_devices();
614620

615-
auto available_presets = config_presets::get_available_presets(gpu_encoding, gpu_type);
621+
auto available_presets = config_presets::get_available_presets(presets, gpu_encoding, gpu_type);
616622

617623
std::set<std::string> all_codecs;
618624
for (const auto& preset : available_presets) {

src/common/utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ namespace u {
503503
std::set<std::string> get_available_codecs(const std::set<std::string>& codecs);
504504

505505
std::vector<std::string> get_supported_presets(bool gpu_encoding, const std::string& gpu_type);
506+
std::vector<std::string> get_supported_presets(
507+
const PresetSettings& presets, bool gpu_encoding, const std::string& gpu_type
508+
);
506509

507510
std::vector<std::string> ffmpeg_string_to_args(const std::string& str);
508511

src/gui/components/configs/options.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void configs::options(ui::Container& container) {
274274
*/
275275
section_component("rendering");
276276

277-
auto presets = u::get_supported_presets(settings.gpu_encoding, app_settings.gpu_type);
277+
auto presets = u::get_supported_presets(preset_settings, settings.gpu_encoding, app_settings.gpu_type);
278278

279279
if (!presets.empty() && !u::contains(presets, settings.encode_preset)) {
280280
settings.encode_preset = presets[0];
@@ -304,6 +304,7 @@ void configs::options(ui::Container& container) {
304304

305305
if (settings.advanced.ffmpeg_override.empty()) {
306306
std::vector<std::string> preset_args = config_presets::get_preset_params(
307+
preset_settings,
307308
settings.gpu_encoding ? app_settings.gpu_type : "cpu",
308309
u::to_lower(settings.encode_preset.empty() ? "h264" : settings.encode_preset),
309310
settings.quality

src/gui/sdl.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "render/render.h"
44
#include "os/desktop_notification.h"
55
#include "os/window.h"
6+
#include "ui/ui.h"
67
#include "renderer.h"
78
#include "gui.h"
89

@@ -35,7 +36,18 @@ namespace {
3536
SDL_SetWindowSize(window, new_w, new_h);
3637
}
3738

39+
// returns true if something that's already on screen needs redrawing
3840
bool apply_app_config(const GlobalAppSettings& config) {
41+
bool highlight_color_changed = false;
42+
43+
#ifdef BLUR_COLOR_THEMES
44+
auto parsed_highlight_color = gfx::Color::from_hex_string(config.gui_color_hex, false);
45+
auto highlight_color = parsed_highlight_color.value_or(ui::DEFAULT_HIGHLIGHT_COLOR);
46+
47+
highlight_color_changed = highlight_color != ui::highlight_color;
48+
ui::highlight_color = highlight_color;
49+
#endif
50+
3951
float previous_scale = render::dpi_scale_override;
4052
float previous_content_scale = sdl::window ? render::get_content_scale(sdl::window) : 1.f;
4153

@@ -65,7 +77,7 @@ namespace {
6577
}
6678
}
6779

68-
return scale_changed;
80+
return scale_changed || highlight_color_changed;
6981
}
7082
}
7183

@@ -274,9 +286,9 @@ bool sdl::poll_config_reload() {
274286
return false; // unchanged since we last looked
275287

276288
auto config = config_app::get_app_config();
277-
bool scale_changed = apply_app_config(config);
278-
remember_config_write_time(); // note: store modified time after get_app_config since it itself edits the file
289+
bool needs_redraw = apply_app_config(config);
290+
remember_config_write_time(); // note: store modified time after get_app_config, since parsing can rewrite the file
279291

280292
// only force a redraw if something visible actually changed
281-
return scale_changed;
293+
return needs_redraw;
282294
}

0 commit comments

Comments
 (0)