diff --git a/src/core/grabber/include/grabber/device_grabber.hpp b/src/core/grabber/include/grabber/device_grabber.hpp index 64bb85811..93476c995 100644 --- a/src/core/grabber/include/grabber/device_grabber.hpp +++ b/src/core/grabber/include/grabber/device_grabber.hpp @@ -953,6 +953,10 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client { complex_modifications_manipulator_manager_->invalidate_manipulators(); for (const auto& rule : core_configuration_->get_selected_profile().get_complex_modifications()->get_rules()) { + if (!rule.get_enabled()) { + continue; + } + for (const auto& manipulator : rule->get_manipulators()) { try { auto m = manipulator::manipulator_factory::make_manipulator(manipulator->to_json(), diff --git a/src/share/core_configuration/details/profile/complex_modifications_rule.hpp b/src/share/core_configuration/details/profile/complex_modifications_rule.hpp index 9866fe31c..67dd09bae 100644 --- a/src/share/core_configuration/details/profile/complex_modifications_rule.hpp +++ b/src/share/core_configuration/details/profile/complex_modifications_rule.hpp @@ -84,8 +84,14 @@ class complex_modifications_rule final { gsl::not_null> parameters, error_handling error_handling) : json_(json) { + helper_values_.push_back_value("enabled", + enabled_, + true); + pqrs::json::requires_object(json, "json"); + helper_values_.update_value(json, error_handling); + for (const auto& [key, value] : json.items()) { if (key == "manipulators") { pqrs::json::requires_array(value, "`" + key + "`"); @@ -131,13 +137,25 @@ class complex_modifications_rule final { } nlohmann::json to_json(void) const { - return json_; + auto j = json_; + + helper_values_.update_json(j); + + return j; } const std::vector>>& get_manipulators(void) const { return manipulators_; } + const bool& get_enabled(void) const { + return enabled_; + } + + void set_enabled(bool value) { + enabled_ = value; + } + const std::string& get_description(void) const { return description_; } @@ -145,7 +163,9 @@ class complex_modifications_rule final { private: nlohmann::json json_; std::vector>> manipulators_; + bool enabled_; std::string description_; + configuration_json_helper::helper_values helper_values_; }; } // namespace details } // namespace core_configuration diff --git a/tests/src/core_configuration/src/complex_modifications_rule_test.hpp b/tests/src/core_configuration/src/complex_modifications_rule_test.hpp new file mode 100644 index 000000000..65dd00c75 --- /dev/null +++ b/tests/src/core_configuration/src/complex_modifications_rule_test.hpp @@ -0,0 +1,87 @@ +#include "core_configuration/core_configuration.hpp" +#include + +void run_complex_modifications_rule_test(void) { + using namespace boost::ut; + using namespace boost::ut::literals; + using namespace std::string_literals; + + "complex_modifications_rule"_test = [] { + // `parameters` in `manipulators` are not omitted in to_json. + { + auto json = R"( + +{ + "description": "example", + "manipulators": [ + { + "from": { "key_code": "f12" }, + "to": [{ "key_code": "mission_control" }], + "type": "basic", + "parameters": { + "basic.simultaneous_threshold_milliseconds": 50 + } + } + ] +} + +)"_json; + auto parameters = std::make_shared(); + krbn::core_configuration::details::complex_modifications_rule rule(json, + parameters, + krbn::core_configuration::error_handling::strict); + expect(1 == rule.get_manipulators().size()); + expect(rule.get_enabled()); + expect("example"s == rule.get_description()); + expect(json == rule.to_json()); + } + + // enabled + { + auto json = R"( + +{ + "description": "enabled", + "enabled": false, + "manipulators": [ + { + "from": { "key_code": "f12" }, + "to": [{ "key_code": "mission_control" }], + "type": "basic" + } + ] +} + +)"_json; + + auto parameters = std::make_shared(); + krbn::core_configuration::details::complex_modifications_rule rule(json, + parameters, + krbn::core_configuration::error_handling::strict); + expect(1 == rule.get_manipulators().size()); + expect(!rule.get_enabled()); + expect("enabled"s == rule.get_description()); + expect(json == rule.to_json()); + + rule.set_enabled(true); + expect(rule.get_enabled()); + + auto expected_json = R"( + +{ + "description": "enabled", + "manipulators": [ + { + "from": { "key_code": "f12" }, + "to": [{ "key_code": "mission_control" }], + "type": "basic" + } + ] +} + +)"_json; + + expect(expected_json == rule.to_json()); + } + }; +} diff --git a/tests/src/core_configuration/src/test.cpp b/tests/src/core_configuration/src/test.cpp index fe62f49c1..8b9ea7eca 100644 --- a/tests/src/core_configuration/src/test.cpp +++ b/tests/src/core_configuration/src/test.cpp @@ -1,3 +1,4 @@ +#include "complex_modifications_rule_test.hpp" #include "configuration_json_helper_test.hpp" #include "core_configuration_test.hpp" #include "device_test.hpp" @@ -6,6 +7,7 @@ #include "machine_specific_test.hpp" int main(void) { + run_complex_modifications_rule_test(); run_configuration_json_helper_test(); run_core_configuration_test(); run_device_test();