Added config file support per module

This commit is contained in:
Bob Polis 2021-10-06 14:13:49 +02:00
parent 3937dc1c37
commit d7f7cf634f

View File

@ -13,6 +13,7 @@
#include <stdexcept> #include <stdexcept>
#include <cmath> #include <cmath>
#include <thread> #include <thread>
#include <fstream>
// POSIX // POSIX
#include <getopt.h> #include <getopt.h>
@ -94,7 +95,7 @@ int main(int argc, const char * argv[]) {
std::string plugin_dir {"/usr/local/share/screensaver/plugins"}; std::string plugin_dir {"/usr/local/share/screensaver/plugins"};
#endif #endif
sc::plugin<ScreensaverPlugin>::scan_plugins(plugin_dir, "saver"); sc::plugin<ScreensaverPlugin>::scan_plugins(plugin_dir, "saver");
std::unique_ptr<ScreensaverPlugin> saver; std::string saver_name;
bool random_saver {false}; bool random_saver {false};
int opt_char, opt_val; int opt_char, opt_val;
struct option long_options[] = { struct option long_options[] = {
@ -133,16 +134,15 @@ int main(int argc, const char * argv[]) {
// here when no file args // here when no file args
if (random_saver) { if (random_saver) {
std::vector<std::string> names {sc::plugin<ScreensaverPlugin>::names()}; std::vector<std::string> names {sc::plugin<ScreensaverPlugin>::names()};
std::string name {sc::random::choice(names)}; saver_name = sc::random::choice(names);
saver = sc::plugin<ScreensaverPlugin>::get(name)();
} else { } else {
saver = sc::plugin<ScreensaverPlugin>::get("Default")(); saver_name = "Default";
} }
} }
for (int i = optind; i < argc; ++i) { for (int i = optind; i < argc; ++i) {
try { try {
// process file argv[i] // process file argv[i]
saver = sc::plugin<ScreensaverPlugin>::get(argv[i])(); saver_name = argv[i];
if (random_saver) { if (random_saver) {
std::cerr << "screensaver: warning: -r option overridden by file arg\n"; std::cerr << "screensaver: warning: -r option overridden by file arg\n";
} }
@ -168,12 +168,20 @@ int main(int argc, const char * argv[]) {
create_image(WIDTH, HEIGHT); create_image(WIDTH, HEIGHT);
// setup screen saver module // setup screen saver module
if (!saver) { if (saver_name.size() == 0) {
saver = sc::plugin<ScreensaverPlugin>::get("Default")(); saver_name = "Default";
std::cerr << "screensaver: using standard 'Default' module\n"; std::cerr << "screensaver: using standard 'Default' module\n";
} }
std::unique_ptr<ScreensaverPlugin> saver {sc::plugin<ScreensaverPlugin>::get(saver_name)()};
main_saver = saver.get(); main_saver = saver.get();
main_saver->setup(main_context.get(), {0, 0, WIDTH, HEIGHT}); main_saver->setup(main_context.get(), {0, 0, WIDTH, HEIGHT});
std::string config_path {plugin_dir + "/" + saver_name + ".json"};
if (sc::file_exists(config_path)) {
std::ifstream config_file {config_path};
nlohmann::json j;
config_file >> j;
main_saver->config(j);
}
sc::gui::app().fps(main_saver->fps()); sc::gui::app().fps(main_saver->fps());
sc::gui::app().add_run_loop_action(draw); sc::gui::app().add_run_loop_action(draw);