From 9cddb56f6098fe8902cf2d4d30ff43312040d31e Mon Sep 17 00:00:00 2001 From: Alexis Maiquez Date: Tue, 31 Jan 2023 22:07:59 +0100 Subject: [PATCH] Added support for setting values to properties from command-line to modify how backgrounds look Signed-off-by: Alexis Maiquez --- main.cpp | 51 ++++++++++++++----- src/WallpaperEngine/Core/Projects/CProperty.h | 1 + .../Core/Projects/CPropertyBoolean.cpp | 5 ++ .../Core/Projects/CPropertyBoolean.h | 1 + .../Core/Projects/CPropertyColor.cpp | 36 ++++++++----- .../Core/Projects/CPropertyColor.h | 1 + .../Core/Projects/CPropertyCombo.cpp | 20 ++++++++ .../Core/Projects/CPropertyCombo.h | 1 + .../Core/Projects/CPropertySlider.cpp | 10 ++++ .../Core/Projects/CPropertySlider.h | 1 + .../Core/Projects/CPropertyText.cpp | 5 ++ .../Core/Projects/CPropertyText.h | 1 + 12 files changed, 106 insertions(+), 27 deletions(-) diff --git a/main.cpp b/main.cpp index 95d11d6..bd41654 100644 --- a/main.cpp +++ b/main.cpp @@ -50,13 +50,14 @@ void print_help (const char* route) << "\ta full path to the background's folder" << std::endl << std::endl << "options:" << std::endl - << " --silent\t\tMutes all the sound the wallpaper might produce" << std::endl - << " --volume \tSets the volume for all the sounds in the background" << std::endl - << " --screen-root \tDisplay as screen's background" << std::endl - << " --fps \tLimits the FPS to the given number, useful to keep battery consumption low" << std::endl - << " --assets-dir \tFolder where the assets are stored" << std::endl - << " --screenshot\t\tTakes a screenshot of the background" << std::endl - << " --list-properties\tList all the available properties and their possible values" << std::endl; + << "\t--silent\t\t\t\t\tMutes all the sound the wallpaper might produce" << std::endl + << "\t--volume \t\t\tSets the volume for all the sounds in the background" << std::endl + << "\t--screen-root \tDisplay as screen's background" << std::endl + << "\t--fps \t\t\tLimits the FPS to the given number, useful to keep battery consumption low" << std::endl + << "\t--assets-dir \t\t\tFolder where the assets are stored" << std::endl + << "\t--screenshot\t\t\t\tTakes a screenshot of the background" << std::endl + << "\t--list-properties\t\t\tList all the available properties and their possible values" << std::endl + << "\t--set-property \tOverrides the default value of the given property" << std::endl; } std::string stringPathFixes(const std::string& s) @@ -315,6 +316,7 @@ void takeScreenshot (WallpaperEngine::Render::CWallpaper* wp, const std::string& int main (int argc, char* argv[]) { std::vector screens; + std::map propertyOverrides; int maximumFPS = 30; bool shouldEnableAudio = true; @@ -336,6 +338,7 @@ int main (int argc, char* argv[]) {"assets-dir", required_argument, 0, 'a'}, {"screenshot", required_argument, 0, 'c'}, {"list-properties", no_argument, 0, 'l'}, + {"set-property", required_argument, 0, 'o'}, {nullptr, 0, 0, 0} }; @@ -348,6 +351,22 @@ int main (int argc, char* argv[]) switch (c) { + case 'o': + { + std::string value = optarg; + std::string::size_type equals = value.find ('='); + + // properties without value are treated as booleans for now + if (equals == std::string::npos) + propertyOverrides.insert_or_assign (value, "1"); + else + propertyOverrides.insert_or_assign ( + value.substr (0, equals), + value.substr (equals + 1) + ); + } + break; + case 'l': shouldListPropertiesAndStop = true; break; @@ -522,16 +541,22 @@ int main (int argc, char* argv[]) chdir (path.c_str ()); // show properties if required - if (shouldListPropertiesAndStop) + for (auto cur : project->getProperties ()) { - for (auto cur : project->getProperties ()) - { - std::cout << cur->dump () << std::endl; - } + // update the value of the property + auto override = propertyOverrides.find (cur->getName ()); - return 0; + if (override != propertyOverrides.end ()) + cur->update (override->second); + + if (shouldListPropertiesAndStop) + std::cout << cur->dump () << std::endl; } + // halt if the list-properties option was specified + if (shouldListPropertiesAndStop) + return 0; + // attach signals so if a stop is requested the X11 resources are freed and the program shutsdown gracefully std::signal(SIGINT, signalhandler); std::signal(SIGTERM, signalhandler); diff --git a/src/WallpaperEngine/Core/Projects/CProperty.h b/src/WallpaperEngine/Core/Projects/CProperty.h index 11758af..9abdb71 100644 --- a/src/WallpaperEngine/Core/Projects/CProperty.h +++ b/src/WallpaperEngine/Core/Projects/CProperty.h @@ -19,6 +19,7 @@ namespace WallpaperEngine::Core::Projects template bool is () { return this->m_type == T::Type; } virtual std::string dump () const = 0; + virtual void update (const std::string& value) = 0; const std::string& getName () const; const std::string& getType () const; diff --git a/src/WallpaperEngine/Core/Projects/CPropertyBoolean.cpp b/src/WallpaperEngine/Core/Projects/CPropertyBoolean.cpp index a2c6748..dc4a0ca 100644 --- a/src/WallpaperEngine/Core/Projects/CPropertyBoolean.cpp +++ b/src/WallpaperEngine/Core/Projects/CPropertyBoolean.cpp @@ -22,6 +22,11 @@ bool CPropertyBoolean::getValue () return this->m_value; } +void CPropertyBoolean::update (const std::string& value) +{ + this->m_value = value == "1" || value == "true"; +} + std::string CPropertyBoolean::dump () const { std::stringstream ss; diff --git a/src/WallpaperEngine/Core/Projects/CPropertyBoolean.h b/src/WallpaperEngine/Core/Projects/CPropertyBoolean.h index b22bad4..188b03d 100644 --- a/src/WallpaperEngine/Core/Projects/CPropertyBoolean.h +++ b/src/WallpaperEngine/Core/Projects/CPropertyBoolean.h @@ -13,6 +13,7 @@ namespace WallpaperEngine::Core::Projects bool getValue (); std::string dump () const override; + void update (const std::string& value) override; static const std::string Type; diff --git a/src/WallpaperEngine/Core/Projects/CPropertyColor.cpp b/src/WallpaperEngine/Core/Projects/CPropertyColor.cpp index d931b29..8ba2ee1 100644 --- a/src/WallpaperEngine/Core/Projects/CPropertyColor.cpp +++ b/src/WallpaperEngine/Core/Projects/CPropertyColor.cpp @@ -4,28 +4,30 @@ using namespace WallpaperEngine::Core::Projects; -CPropertyColor* CPropertyColor::fromJSON (json data, const std::string& name) +FloatColor ParseColor (const std::string& value) { - std::string value = *jsonFindRequired (data, "value", "Color property must have a value"); - std::string text = jsonFindDefault (data, "text", ""); - FloatColor color (0, 0, 0, 0); - if (value.find ('.') == std::string::npos && value != "0 0 0" && value != "1 1 1") { IntegerColor intcolor = WallpaperEngine::Core::aToColori (value); - color.r = intcolor.r / 255.0; - color.g = intcolor.g / 255.0; - color.b = intcolor.b / 255.0; - color.a = intcolor.a / 255.0; - } - else - { - color = WallpaperEngine::Core::aToColorf (value); + return { + intcolor.r / 255.0, + intcolor.g / 255.0, + intcolor.b / 255.0, + intcolor.a / 255.0 + }; } + return WallpaperEngine::Core::aToColorf (value); +} + +CPropertyColor* CPropertyColor::fromJSON (json data, const std::string& name) +{ + std::string value = *jsonFindRequired (data, "value", "Color property must have a value"); + std::string text = jsonFindDefault (data, "text", ""); + return new CPropertyColor ( - color, + ParseColor (value), name, text ); @@ -36,6 +38,12 @@ const FloatColor& CPropertyColor::getValue () const return this->m_color; } +void CPropertyColor::update (const std::string& value) +{ + this->m_color = ParseColor (value); +} + + std::string CPropertyColor::dump () const { std::stringstream ss; diff --git a/src/WallpaperEngine/Core/Projects/CPropertyColor.h b/src/WallpaperEngine/Core/Projects/CPropertyColor.h index bf7aca4..e1b0222 100644 --- a/src/WallpaperEngine/Core/Projects/CPropertyColor.h +++ b/src/WallpaperEngine/Core/Projects/CPropertyColor.h @@ -16,6 +16,7 @@ namespace WallpaperEngine::Core::Projects const FloatColor& getValue () const; std::string dump () const override; + void update (const std::string& value) override; static const std::string Type; diff --git a/src/WallpaperEngine/Core/Projects/CPropertyCombo.cpp b/src/WallpaperEngine/Core/Projects/CPropertyCombo.cpp index 15438e7..6915c00 100644 --- a/src/WallpaperEngine/Core/Projects/CPropertyCombo.cpp +++ b/src/WallpaperEngine/Core/Projects/CPropertyCombo.cpp @@ -72,6 +72,26 @@ std::string CPropertyCombo::dump () const return ss.str(); } +void CPropertyCombo::update (const std::string& value) +{ + bool found = false; + + // ensure the value is present somewhere in the value list + for (auto cur : this->m_values) + { + if (cur->value != value) + continue; + + found = true; + } + + if (found == false) + throw std::runtime_error ("Assigning invalid value to property"); + + this->m_defaultValue = value; +} + + void CPropertyCombo::addValue (std::string label, std::string value) { CPropertyComboValue* prop = new CPropertyComboValue; diff --git a/src/WallpaperEngine/Core/Projects/CPropertyCombo.h b/src/WallpaperEngine/Core/Projects/CPropertyCombo.h index fb12257..1e8ada1 100644 --- a/src/WallpaperEngine/Core/Projects/CPropertyCombo.h +++ b/src/WallpaperEngine/Core/Projects/CPropertyCombo.h @@ -20,6 +20,7 @@ namespace WallpaperEngine::Core::Projects const std::string& getValue () const; std::string dump () const override; + void update (const std::string& value) override; static const std::string Type; diff --git a/src/WallpaperEngine/Core/Projects/CPropertySlider.cpp b/src/WallpaperEngine/Core/Projects/CPropertySlider.cpp index a0bd3e1..a7a01ff 100644 --- a/src/WallpaperEngine/Core/Projects/CPropertySlider.cpp +++ b/src/WallpaperEngine/Core/Projects/CPropertySlider.cpp @@ -56,6 +56,16 @@ std::string CPropertySlider::dump () const return ss.str(); } +void CPropertySlider::update (const std::string& value) +{ + double newValue = atof (value.c_str ()); + + if (newValue < this->m_min || newValue > this->m_max) + throw std::runtime_error ("Slider value is out of range"); + + this->m_value = newValue; +} + CPropertySlider::CPropertySlider (double value, const std::string& name, const std::string& text, double min, double max, double step) : CProperty (name, Type, text), m_value (value), m_min (min), m_max (max), m_step (step) diff --git a/src/WallpaperEngine/Core/Projects/CPropertySlider.h b/src/WallpaperEngine/Core/Projects/CPropertySlider.h index 99cff75..8566a84 100644 --- a/src/WallpaperEngine/Core/Projects/CPropertySlider.h +++ b/src/WallpaperEngine/Core/Projects/CPropertySlider.h @@ -19,6 +19,7 @@ namespace WallpaperEngine::Core::Projects const double& getMaxValue () const; const double& getStep () const; std::string dump () const override; + void update (const std::string& value) override; static const std::string Type; diff --git a/src/WallpaperEngine/Core/Projects/CPropertyText.cpp b/src/WallpaperEngine/Core/Projects/CPropertyText.cpp index f8e0f1c..646c310 100644 --- a/src/WallpaperEngine/Core/Projects/CPropertyText.cpp +++ b/src/WallpaperEngine/Core/Projects/CPropertyText.cpp @@ -25,6 +25,11 @@ std::string CPropertyText::dump () const return ss.str(); } +void CPropertyText::update (const std::string& value) +{ + this->m_text = value; +} + CPropertyText::CPropertyText (const std::string& name, const std::string& text) : CProperty (name, Type, text) { diff --git a/src/WallpaperEngine/Core/Projects/CPropertyText.h b/src/WallpaperEngine/Core/Projects/CPropertyText.h index 216f607..855f7cc 100644 --- a/src/WallpaperEngine/Core/Projects/CPropertyText.h +++ b/src/WallpaperEngine/Core/Projects/CPropertyText.h @@ -11,6 +11,7 @@ namespace WallpaperEngine::Core::Projects public: static CPropertyText* fromJSON (json data, const std::string& name); std::string dump () const override; + void update (const std::string& value) override; static const std::string Type;