From 6f2c8ab39d3c0aa59a00c245077a192b82b35e51 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Thu, 27 Jun 2024 22:19:38 +0900 Subject: [PATCH 1/4] add name_ and selected_ to helper_values_ in profile --- .../core_configuration/details/profile.hpp | 26 +++++++------------ .../json/errors/profile_errors.jsonc | 2 +- .../json/to_json_example.json | 7 ++--- .../src/core_configuration_test.hpp | 2 -- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/share/core_configuration/details/profile.hpp b/src/share/core_configuration/details/profile.hpp index 48d063489..4296506ef 100644 --- a/src/share/core_configuration/details/profile.hpp +++ b/src/share/core_configuration/details/profile.hpp @@ -19,11 +19,18 @@ class profile final { error_handling error_handling) : json_(json), error_handling_(error_handling), - selected_(false), parameters_(std::make_shared()), simple_modifications_(std::make_shared()), fn_function_keys_(std::make_shared()), virtual_hid_keyboard_(std::make_shared()) { + helper_values_.push_back_value("name", + name_, + std::string("")); + + helper_values_.push_back_value("selected", + selected_, + false); + helper_values_.push_back_object("parameters", parameters_); @@ -48,17 +55,7 @@ class profile final { helper_values_.update_value(json, error_handling); for (const auto& [key, value] : json.items()) { - if (key == "name") { - pqrs::json::requires_string(value, "`" + key + "`"); - - name_ = value.get(); - - } else if (key == "selected") { - pqrs::json::requires_boolean(value, "`" + key + "`"); - - selected_ = value.get(); - - } else if (key == "simple_modifications") { + if (key == "simple_modifications") { try { simple_modifications_->update(value); } catch (const pqrs::json::unmarshal_error& e) { @@ -141,9 +138,6 @@ class profile final { helper_values_.update_json(j); - j["name"] = name_; - j["selected"] = selected_; - { auto jj = simple_modifications_->to_json(nlohmann::json::array()); if (!jj.empty()) { @@ -175,7 +169,7 @@ class profile final { name_ = value; } - bool get_selected(void) const { + const bool& get_selected(void) const { return selected_; } diff --git a/tests/src/core_configuration/json/errors/profile_errors.jsonc b/tests/src/core_configuration/json/errors/profile_errors.jsonc index c5b65f028..adc9ffb6f 100644 --- a/tests/src/core_configuration/json/errors/profile_errors.jsonc +++ b/tests/src/core_configuration/json/errors/profile_errors.jsonc @@ -12,7 +12,7 @@ "input": { "name": null }, - "error": "`name` must be string, but is `null`" + "error": "`name` must be array of string, or string, but is `null`" }, // selected diff --git a/tests/src/core_configuration/json/to_json_example.json b/tests/src/core_configuration/json/to_json_example.json index a9be9fad0..f461e28a9 100644 --- a/tests/src/core_configuration/json/to_json_example.json +++ b/tests/src/core_configuration/json/to_json_example.json @@ -260,8 +260,7 @@ }, "rules": [] }, - "name": "Empty", - "selected": false + "name": "Empty" }, { "complex_modifications": { @@ -286,8 +285,7 @@ ] } ], - "name": "fn_function_keys v1", - "selected": false + "name": "fn_function_keys v1" }, { "complex_modifications": { @@ -301,7 +299,6 @@ "rules": [] }, "name": "simple_modifications v2", - "selected": false, "simple_modifications": [ { "from": { diff --git a/tests/src/core_configuration/src/core_configuration_test.hpp b/tests/src/core_configuration/src/core_configuration_test.hpp index 3133de428..b37137dcf 100644 --- a/tests/src/core_configuration/src/core_configuration_test.hpp +++ b/tests/src/core_configuration/src/core_configuration_test.hpp @@ -673,8 +673,6 @@ void run_core_configuration_test(void) { {"mouse_motion_to_scroll.speed", 100}, })}, })}, - {"name", ""}, - {"selected", false}, }); expect(empty_profile.to_json() == expected) << UT_SHOW_LINE; } From 915c2ab42cda40bd71d68caaef1be31d58f7a699 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Sat, 29 Jun 2024 23:33:25 +0900 Subject: [PATCH 2/4] Add helper_values_ to complex_modifications_parameters --- src/apps/share/swift/LibKrbn/Settings.swift | 40 +-- src/bin/cli/src/main.cpp | 3 +- .../include/grabber/device_grabber.hpp | 4 +- ...mple_modifications_manipulator_manager.hpp | 2 +- ...n_complex_modifications_assets_manager.hpp | 15 +- src/lib/libkrbn/include/libkrbn/libkrbn.h | 19 +- src/lib/libkrbn/src/libkrbn_configuration.cpp | 131 +++++--- .../complex_modifications_assets_file.hpp | 24 +- .../complex_modifications_assets_manager.hpp | 19 +- src/share/complex_modifications_utility.hpp | 1 + .../core_configuration/details/profile.hpp | 48 ++- .../details/profile/complex_modifications.hpp | 81 ++--- .../complex_modifications_parameters.hpp | 173 ++++++----- .../profile/complex_modifications_rule.hpp | 25 +- .../details/profile/virtual_hid_keyboard.hpp | 1 + src/share/manipulator/manipulator_factory.hpp | 2 +- src/share/manipulator/manipulator_manager.hpp | 6 +- .../manipulator/manipulators/basic/basic.hpp | 28 +- .../manipulators/mouse_basic/mouse_basic.hpp | 2 +- .../mouse_motion_to_scroll/counter.hpp | 29 +- .../mouse_motion_to_scroll.hpp | 5 +- ...complex_modifications_assets_file_test.hpp | 4 +- ...plex_modifications_assets_manager_test.hpp | 28 +- .../json/minmax_parameter_value_test1.json | 41 --- .../json/minmax_parameter_value_test2.json | 41 --- .../json/minmax_parameter_value_test3.json | 41 --- .../json/to_json_default.json | 10 - .../json/to_json_example.json | 34 --- .../src/core_configuration_test.hpp | 287 ++++++++---------- .../core_configuration/src/errors_test.hpp | 3 +- .../src/manipulator_factory_test.hpp | 4 +- .../src/manipulator_manager_test.hpp | 2 +- .../src/manipulator_basic/src/errors_test.hpp | 2 +- .../src/manipulator_basic_test.hpp | 6 +- .../src/to_after_key_up_test.hpp | 6 +- .../src/to_delayed_action_test.hpp | 6 +- .../src/to_if_alone_test.hpp | 6 +- .../src/to_if_held_down_test.hpp | 6 +- .../src/errors_test.hpp | 2 +- .../src/counter_test.hpp | 5 +- .../src/errors_test.hpp | 2 +- tests/src/share/manipulator_helper.hpp | 2 +- 42 files changed, 550 insertions(+), 646 deletions(-) delete mode 100644 tests/src/core_configuration/json/minmax_parameter_value_test1.json delete mode 100644 tests/src/core_configuration/json/minmax_parameter_value_test2.json delete mode 100644 tests/src/core_configuration/json/minmax_parameter_value_test3.json diff --git a/src/apps/share/swift/LibKrbn/Settings.swift b/src/apps/share/swift/LibKrbn/Settings.swift index c730db9fa..ef50782ba 100644 --- a/src/apps/share/swift/LibKrbn/Settings.swift +++ b/src/apps/share/swift/LibKrbn/Settings.swift @@ -79,25 +79,20 @@ extension LibKrbn { updateComplexModificationsRules() complexModificationsParameterToIfAloneTimeoutMilliseconds = Int( - libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter( - "basic.to_if_alone_timeout_milliseconds" - )) + libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_to_if_alone_timeout_milliseconds() + ) complexModificationsParameterToIfHeldDownThresholdMilliseconds = Int( - libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter( - "basic.to_if_held_down_threshold_milliseconds" - )) + libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_to_if_held_down_threshold_milliseconds() + ) complexModificationsParameterToDelayedActionDelayMilliseconds = Int( - libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter( - "basic.to_delayed_action_delay_milliseconds" - )) + libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_to_delayed_action_delay_milliseconds() + ) complexModificationsParameterSimultaneousThresholdMilliseconds = Int( - libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter( - "basic.simultaneous_threshold_milliseconds" - )) + libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_simultaneous_threshold_milliseconds() + ) complexModificationsParameterMouseMotionToScrollSpeed = Int( - libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter( - "mouse_motion_to_scroll.speed" - )) + libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_mouse_motion_to_scroll_speed() + ) updateConnectedDeviceSettings() @@ -467,8 +462,7 @@ extension LibKrbn { @Published var complexModificationsParameterToIfAloneTimeoutMilliseconds: Int = 0 { didSet { if didSetEnabled { - libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter( - "basic.to_if_alone_timeout_milliseconds", + libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_to_if_alone_timeout_milliseconds( Int32(complexModificationsParameterToIfAloneTimeoutMilliseconds) ) save() @@ -479,8 +473,7 @@ extension LibKrbn { @Published var complexModificationsParameterToIfHeldDownThresholdMilliseconds: Int = 0 { didSet { if didSetEnabled { - libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter( - "basic.to_if_held_down_threshold_milliseconds", + libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_to_if_held_down_threshold_milliseconds( Int32(complexModificationsParameterToIfHeldDownThresholdMilliseconds) ) save() @@ -491,8 +484,7 @@ extension LibKrbn { @Published var complexModificationsParameterToDelayedActionDelayMilliseconds: Int = 0 { didSet { if didSetEnabled { - libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter( - "basic.to_delayed_action_delay_milliseconds", + libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_to_delayed_action_delay_milliseconds( Int32(complexModificationsParameterToDelayedActionDelayMilliseconds) ) save() @@ -503,8 +495,7 @@ extension LibKrbn { @Published var complexModificationsParameterSimultaneousThresholdMilliseconds: Int = 0 { didSet { if didSetEnabled { - libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter( - "basic.simultaneous_threshold_milliseconds", + libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_simultaneous_threshold_milliseconds( Int32(complexModificationsParameterSimultaneousThresholdMilliseconds) ) save() @@ -515,8 +506,7 @@ extension LibKrbn { @Published var complexModificationsParameterMouseMotionToScrollSpeed: Int = 0 { didSet { if didSetEnabled { - libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter( - "mouse_motion_to_scroll.speed", + libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_mouse_motion_to_scroll_speed( Int32(complexModificationsParameterMouseMotionToScrollSpeed) ) save() diff --git a/src/bin/cli/src/main.cpp b/src/bin/cli/src/main.cpp index 1841586b7..6e246cf25 100644 --- a/src/bin/cli/src/main.cpp +++ b/src/bin/cli/src/main.cpp @@ -249,7 +249,8 @@ int main(int argc, char** argv) { } try { - auto assets_file = krbn::complex_modifications_assets_file(file_path.string()); + auto assets_file = krbn::complex_modifications_assets_file(file_path.string(), + krbn::core_configuration::error_handling::strict); auto error_messages = assets_file.lint(); if (error_messages.empty()) { if (!silent) { diff --git a/src/core/grabber/include/grabber/device_grabber.hpp b/src/core/grabber/include/grabber/device_grabber.hpp index 0cd588a5b..1962a2eb8 100644 --- a/src/core/grabber/include/grabber/device_grabber.hpp +++ b/src/core/grabber/include/grabber/device_grabber.hpp @@ -952,8 +952,8 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client { void update_complex_modifications_manipulators(void) { complex_modifications_manipulator_manager_->invalidate_manipulators(); - for (const auto& rule : core_configuration_->get_selected_profile().get_complex_modifications().get_rules()) { - for (const auto& manipulator : rule.get_manipulators()) { + for (const auto& rule : core_configuration_->get_selected_profile().get_complex_modifications()->get_rules()) { + for (const auto& manipulator : rule->get_manipulators()) { try { auto m = manipulator::manipulator_factory::make_manipulator(manipulator.get_json(), manipulator.get_parameters()); diff --git a/src/core/grabber/include/grabber/device_grabber_details/simple_modifications_manipulator_manager.hpp b/src/core/grabber/include/grabber/device_grabber_details/simple_modifications_manipulator_manager.hpp index e0cfa21d1..c06cd2cad 100644 --- a/src/core/grabber/include/grabber/device_grabber_details/simple_modifications_manipulator_manager.hpp +++ b/src/core/grabber/include/grabber/device_grabber_details/simple_modifications_manipulator_manager.hpp @@ -92,7 +92,7 @@ class simple_modifications_manipulator_manager final { {"swap", swap}, {"discard", discard}, }); - auto parameters = krbn::core_configuration::details::complex_modifications_parameters(); + auto parameters = std::make_shared(); auto m = std::make_shared(json, parameters); auto c = manipulator::manipulator_factory::make_device_if_condition(*device); diff --git a/src/lib/libkrbn/include/libkrbn/impl/libkrbn_complex_modifications_assets_manager.hpp b/src/lib/libkrbn/include/libkrbn/impl/libkrbn_complex_modifications_assets_manager.hpp index 092f4c94b..2507bfb32 100644 --- a/src/lib/libkrbn/include/libkrbn/impl/libkrbn_complex_modifications_assets_manager.hpp +++ b/src/lib/libkrbn/include/libkrbn/impl/libkrbn_complex_modifications_assets_manager.hpp @@ -18,7 +18,8 @@ class libkrbn_complex_modifications_assets_manager final { } void reload(void) const { - manager_->reload(krbn::constants::get_user_complex_modifications_assets_directory()); + manager_->reload(krbn::constants::get_user_complex_modifications_assets_directory(), + krbn::core_configuration::error_handling::loose); } size_t get_files_size(void) const { @@ -73,25 +74,25 @@ class libkrbn_complex_modifications_assets_manager final { size_t index, krbn::core_configuration::core_configuration& core_configuration) const { if (auto r = find_rule(file_index, index)) { - core_configuration.get_selected_profile().push_front_complex_modifications_rule(*r); + core_configuration.get_selected_profile().get_complex_modifications()->push_front_rule(r); } } private: - const krbn::complex_modifications_assets_file* find_file(size_t index) const { + std::shared_ptr find_file(size_t index) const { auto& files = manager_->get_files(); if (index < files.size()) { - return &(files[index]); + return files[index]; } return nullptr; } - const krbn::core_configuration::details::complex_modifications_rule* find_rule(size_t file_index, - size_t index) const { + std::shared_ptr find_rule(size_t file_index, + size_t index) const { if (auto f = find_file(file_index)) { auto& rules = f->get_rules(); if (index < rules.size()) { - return &(rules[index]); + return rules[index]; } } return nullptr; diff --git a/src/lib/libkrbn/include/libkrbn/libkrbn.h b/src/lib/libkrbn/include/libkrbn/libkrbn.h index 6caa6a083..4d6b832f5 100644 --- a/src/lib/libkrbn/include/libkrbn/libkrbn.h +++ b/src/lib/libkrbn/include/libkrbn/libkrbn.h @@ -170,9 +170,22 @@ void libkrbn_core_configuration_push_front_selected_profile_complex_modification size_t error_message_buffer_length); void libkrbn_core_configuration_erase_selected_profile_complex_modifications_rule(size_t index); void libkrbn_core_configuration_move_selected_profile_complex_modifications_rule(size_t source_index, size_t destination_index); -int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter(const char* name); -void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter(const char* name, - int value); + +int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_simultaneous_threshold_milliseconds(void); +void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_simultaneous_threshold_milliseconds(int value); + +int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_to_if_alone_timeout_milliseconds(void); +void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_to_if_alone_timeout_milliseconds(int value); + +int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_to_if_held_down_threshold_milliseconds(void); +void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_to_if_held_down_threshold_milliseconds(int value); + +int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_to_delayed_action_delay_milliseconds(void); +void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_to_delayed_action_delay_milliseconds(int value); + +int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_mouse_motion_to_scroll_speed(void); +void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_mouse_motion_to_scroll_speed(int value); + void libkrbn_core_configuration_get_new_complex_modifications_rule_json_string(char* buffer, size_t length); diff --git a/src/lib/libkrbn/src/libkrbn_configuration.cpp b/src/lib/libkrbn/src/libkrbn_configuration.cpp index 7cde1706e..e05ad63da 100644 --- a/src/lib/libkrbn/src/libkrbn_configuration.cpp +++ b/src/lib/libkrbn/src/libkrbn_configuration.cpp @@ -339,22 +339,17 @@ void libkrbn_core_configuration_replace_selected_profile_fn_function_key(const c size_t libkrbn_core_configuration_get_selected_profile_complex_modifications_rules_size(void) { auto c = get_current_core_configuration(); - return c->get_selected_profile().get_complex_modifications().get_rules().size(); -} - -void libkrbn_core_configuration_push_front_complex_modifications_rule_to_selected_profile(const krbn::core_configuration::details::complex_modifications_rule& rule) { - auto c = get_current_core_configuration(); - c->get_selected_profile().push_front_complex_modifications_rule(rule); + return c->get_selected_profile().get_complex_modifications()->get_rules().size(); } void libkrbn_core_configuration_erase_selected_profile_complex_modifications_rule(size_t index) { auto c = get_current_core_configuration(); - c->get_selected_profile().erase_complex_modifications_rule(index); + c->get_selected_profile().get_complex_modifications()->erase_rule(index); } void libkrbn_core_configuration_move_selected_profile_complex_modifications_rule(size_t source_index, size_t destination_index) { auto c = get_current_core_configuration(); - c->get_selected_profile().move_complex_modifications_rule(source_index, destination_index); + c->get_selected_profile().get_complex_modifications()->move_rule(source_index, destination_index); } bool libkrbn_core_configuration_get_selected_profile_complex_modifications_rule_description(size_t index, @@ -365,9 +360,9 @@ bool libkrbn_core_configuration_get_selected_profile_complex_modifications_rule_ } auto c = get_current_core_configuration(); - const auto& rules = c->get_selected_profile().get_complex_modifications().get_rules(); + const auto& rules = c->get_selected_profile().get_complex_modifications()->get_rules(); if (index < rules.size()) { - strlcpy(buffer, rules[index].get_description().c_str(), length); + strlcpy(buffer, rules[index]->get_description().c_str(), length); return true; } @@ -382,9 +377,9 @@ bool libkrbn_core_configuration_get_selected_profile_complex_modifications_rule_ } auto c = get_current_core_configuration(); - const auto& rules = c->get_selected_profile().get_complex_modifications().get_rules(); + const auto& rules = c->get_selected_profile().get_complex_modifications()->get_rules(); if (index < rules.size()) { - auto json_string = krbn::json_utility::dump(rules[index].get_json()); + auto json_string = krbn::json_utility::dump(rules[index]->get_json()); // Return false if no enough space. if (json_string.length() < length) { strlcpy(buffer, json_string.c_str(), length); @@ -405,12 +400,13 @@ void libkrbn_core_configuration_replace_selected_profile_complex_modifications_r try { auto c = get_current_core_configuration(); - auto&& complex_modifications = c->get_selected_profile().get_complex_modifications(); - krbn::core_configuration::details::complex_modifications_rule rule( + auto m = c->get_selected_profile().get_complex_modifications(); + auto r = std::make_shared( krbn::json_utility::parse_jsonc(std::string(json_string)), - complex_modifications.get_parameters()); + m->get_parameters(), + krbn::core_configuration::error_handling::strict); - auto error_messages = krbn::complex_modifications_utility::lint_rule(rule); + auto error_messages = krbn::complex_modifications_utility::lint_rule(*r); if (error_messages.size() > 0) { std::ostringstream os; std::copy(std::begin(error_messages), @@ -422,7 +418,7 @@ void libkrbn_core_configuration_replace_selected_profile_complex_modifications_r return; } - complex_modifications.replace_rule(index, rule); + m->replace_rule(index, r); } catch (const std::exception& e) { auto message = fmt::format("error: {0}", e.what()); @@ -439,12 +435,13 @@ void libkrbn_core_configuration_push_front_selected_profile_complex_modification try { auto c = get_current_core_configuration(); - auto&& complex_modifications = c->get_selected_profile().get_complex_modifications(); - krbn::core_configuration::details::complex_modifications_rule rule( + auto m = c->get_selected_profile().get_complex_modifications(); + auto r = std::make_shared( krbn::json_utility::parse_jsonc(std::string(json_string)), - complex_modifications.get_parameters()); + m->get_parameters(), + krbn::core_configuration::error_handling::strict); - auto error_messages = krbn::complex_modifications_utility::lint_rule(rule); + auto error_messages = krbn::complex_modifications_utility::lint_rule(*r); if (error_messages.size() > 0) { std::ostringstream os; std::copy(std::begin(error_messages), @@ -456,7 +453,7 @@ void libkrbn_core_configuration_push_front_selected_profile_complex_modification return; } - complex_modifications.push_front_rule(rule); + m->push_front_rule(r); } catch (const std::exception& e) { auto message = fmt::format("error: {0}", e.what()); @@ -464,22 +461,84 @@ void libkrbn_core_configuration_push_front_selected_profile_complex_modification } } -int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter(const char* name) { - if (name) { - auto c = get_current_core_configuration(); - if (auto value = c->get_selected_profile().get_complex_modifications().get_parameters().get_value(name)) { - return *value; - } - } - return 0; +// +// basic_simultaneous_threshold_milliseconds +// + +int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_simultaneous_threshold_milliseconds(void) { + auto c = get_current_core_configuration(); + auto p = c->get_selected_profile().get_complex_modifications()->get_parameters(); + return p->get_basic_simultaneous_threshold_milliseconds(); } -void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter(const char* name, - int value) { - if (name) { - auto c = get_current_core_configuration(); - c->get_selected_profile().set_complex_modifications_parameter(name, value); - } +void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_simultaneous_threshold_milliseconds(int value) { + auto c = get_current_core_configuration(); + auto p = c->get_selected_profile().get_complex_modifications()->get_parameters(); + p->set_basic_simultaneous_threshold_milliseconds(value); +} + +// +// basic_to_if_alone_timeout_milliseconds +// + +int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_to_if_alone_timeout_milliseconds(void) { + auto c = get_current_core_configuration(); + auto p = c->get_selected_profile().get_complex_modifications()->get_parameters(); + return p->get_basic_to_if_alone_timeout_milliseconds(); +} + +void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_to_if_alone_timeout_milliseconds(int value) { + auto c = get_current_core_configuration(); + auto p = c->get_selected_profile().get_complex_modifications()->get_parameters(); + p->set_basic_to_if_alone_timeout_milliseconds(value); +} + +// +// basic_to_if_held_down_threshold_milliseconds +// + +int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_to_if_held_down_threshold_milliseconds(void) { + auto c = get_current_core_configuration(); + auto p = c->get_selected_profile().get_complex_modifications()->get_parameters(); + return p->get_basic_to_if_held_down_threshold_milliseconds(); +} + +void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_to_if_held_down_threshold_milliseconds(int value) { + auto c = get_current_core_configuration(); + auto p = c->get_selected_profile().get_complex_modifications()->get_parameters(); + p->set_basic_to_if_held_down_threshold_milliseconds(value); +} + +// +// basic_to_delayed_action_delay_milliseconds +// + +int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_basic_to_delayed_action_delay_milliseconds(void) { + auto c = get_current_core_configuration(); + auto p = c->get_selected_profile().get_complex_modifications()->get_parameters(); + return p->get_basic_to_delayed_action_delay_milliseconds(); +} + +void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_basic_to_delayed_action_delay_milliseconds(int value) { + auto c = get_current_core_configuration(); + auto p = c->get_selected_profile().get_complex_modifications()->get_parameters(); + p->set_basic_to_delayed_action_delay_milliseconds(value); +} + +// +// mouse_motion_to_scroll_speed +// + +int libkrbn_core_configuration_get_selected_profile_complex_modifications_parameter_mouse_motion_to_scroll_speed(void) { + auto c = get_current_core_configuration(); + auto p = c->get_selected_profile().get_complex_modifications()->get_parameters(); + return p->get_mouse_motion_to_scroll_speed(); +} + +void libkrbn_core_configuration_set_selected_profile_complex_modifications_parameter_mouse_motion_to_scroll_speed(int value) { + auto c = get_current_core_configuration(); + auto p = c->get_selected_profile().get_complex_modifications()->get_parameters(); + p->set_mouse_motion_to_scroll_speed(value); } void libkrbn_core_configuration_get_new_complex_modifications_rule_json_string(char* buffer, diff --git a/src/share/complex_modifications_assets_file.hpp b/src/share/complex_modifications_assets_file.hpp index 72b31f09d..473f230a1 100644 --- a/src/share/complex_modifications_assets_file.hpp +++ b/src/share/complex_modifications_assets_file.hpp @@ -10,7 +10,11 @@ namespace krbn { class complex_modifications_assets_file final { public: - complex_modifications_assets_file(const std::filesystem::path& file_path) : file_path_(file_path) { + complex_modifications_assets_file(const complex_modifications_assets_file&) = delete; + + complex_modifications_assets_file(const std::filesystem::path& file_path, + core_configuration::error_handling error_handling) + : file_path_(file_path) { std::ifstream stream(file_path); if (!stream) { throw std::runtime_error(fmt::format("failed to open {0}", file_path.string())); @@ -32,10 +36,13 @@ class complex_modifications_assets_file final { } else if (key == "rules") { pqrs::json::requires_array(value, "`" + key + "`"); - core_configuration::details::complex_modifications_parameters parameters; for (const auto& j : value) { try { - rules_.emplace_back(j, parameters); + auto parameters = std::make_shared(); + auto r = std::make_shared(j, + parameters, + error_handling); + rules_.push_back(r); } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what())); } @@ -56,14 +63,15 @@ class complex_modifications_assets_file final { return title_; } - const std::vector& get_rules(void) const { + const std::vector>>& get_rules(void) const { return rules_; } void push_front_rule_to_core_configuration_profile(core_configuration::details::profile& profile, size_t index) { if (index < rules_.size()) { - profile.push_front_complex_modifications_rule(rules_[index]); + auto c = profile.get_complex_modifications(); + c->push_front_rule(rules_[index]); } } @@ -93,8 +101,8 @@ class complex_modifications_assets_file final { std::vector lint(void) const { std::vector error_messages; - for (const auto& rule : rules_) { - for (const auto& message : complex_modifications_utility::lint_rule(rule)) { + for (const auto& r : rules_) { + for (const auto& message : complex_modifications_utility::lint_rule(*r)) { error_messages.push_back(message); } } @@ -105,6 +113,6 @@ class complex_modifications_assets_file final { private: std::filesystem::path file_path_; std::string title_; - std::vector rules_; + std::vector>> rules_; }; } // namespace krbn diff --git a/src/share/complex_modifications_assets_manager.hpp b/src/share/complex_modifications_assets_manager.hpp index 85c4ebad3..31cf875a6 100644 --- a/src/share/complex_modifications_assets_manager.hpp +++ b/src/share/complex_modifications_assets_manager.hpp @@ -7,14 +7,19 @@ namespace krbn { class complex_modifications_assets_manager final { public: - void reload(const std::string& directory, bool load_system_example_file = true) { + void reload(const std::string& directory, + core_configuration::error_handling error_handling, + bool load_system_example_file = true) { files_.clear(); // Load system example file. if (load_system_example_file) { const std::string file_path = "/Library/Application Support/org.pqrs/Karabiner-Elements/complex_modifications_rules_example.json"; try { - files_.emplace_back(file_path); + auto f = std::make_shared(file_path, + error_handling); + files_.push_back(f); + } catch (std::exception& e) { logger::get_logger()->error("Error in {0}: {1}", file_path, e.what()); } @@ -29,7 +34,9 @@ class complex_modifications_assets_manager final { auto file_path = directory + "/" + entry->d_name; try { - files_.emplace_back(file_path); + auto f = std::make_shared(file_path, + error_handling); + files_.push_back(f); } catch (std::exception& e) { logger::get_logger()->error("Error in {0}: {1}", file_path, e.what()); @@ -43,15 +50,15 @@ class complex_modifications_assets_manager final { std::sort(std::begin(files_), std::end(files_), [](auto& a, auto& b) { - return a.get_title() < b.get_title(); + return a->get_title() < b->get_title(); }); } - const std::vector& get_files(void) const { + const std::vector>>& get_files(void) const { return files_; } private: - std::vector files_; + std::vector>> files_; }; } // namespace krbn diff --git a/src/share/complex_modifications_utility.hpp b/src/share/complex_modifications_utility.hpp index 385c08398..367065a10 100644 --- a/src/share/complex_modifications_utility.hpp +++ b/src/share/complex_modifications_utility.hpp @@ -3,6 +3,7 @@ #include "core_configuration/core_configuration.hpp" #include "json_utility.hpp" #include "manipulator/manipulator_factory.hpp" +#include "memory_utility.hpp" namespace krbn { namespace complex_modifications_utility { diff --git a/src/share/core_configuration/details/profile.hpp b/src/share/core_configuration/details/profile.hpp index 4296506ef..59cf5b0fb 100644 --- a/src/share/core_configuration/details/profile.hpp +++ b/src/share/core_configuration/details/profile.hpp @@ -22,6 +22,7 @@ class profile final { parameters_(std::make_shared()), simple_modifications_(std::make_shared()), fn_function_keys_(std::make_shared()), + complex_modifications_(std::make_shared()), virtual_hid_keyboard_(std::make_shared()) { helper_values_.push_back_value("name", name_, @@ -71,7 +72,8 @@ class profile final { } else if (key == "complex_modifications") { try { - complex_modifications_ = complex_modifications(value); + complex_modifications_ = std::make_shared(value, + error_handling); } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what())); } @@ -139,24 +141,34 @@ class profile final { helper_values_.update_json(j); { + auto key = "simple_modifications"; auto jj = simple_modifications_->to_json(nlohmann::json::array()); if (!jj.empty()) { - j["simple_modifications"] = jj; + j[key] = jj; } else { - j.erase("simple_modifications"); + j.erase(key); } } { + auto key = "fn_function_keys"; auto jj = fn_function_keys_->to_json(make_default_fn_function_keys_json()); if (!jj.empty()) { - j["fn_function_keys"] = jj; + j[key] = jj; } else { - j.erase("fn_function_keys"); + j.erase(key); } } - j["complex_modifications"] = complex_modifications_.to_json(); + { + auto key = "complex_modifications"; + auto jj = complex_modifications_->to_json(); + if (!jj.empty()) { + j[key] = jj; + } else { + j.erase(key); + } + } return j; } @@ -189,30 +201,10 @@ class profile final { return fn_function_keys_; } - const details::complex_modifications& get_complex_modifications(void) const { + gsl::not_null> get_complex_modifications(void) const { return complex_modifications_; } - details::complex_modifications& get_complex_modifications(void) { - return const_cast(static_cast(*this).get_complex_modifications()); - } - - void push_front_complex_modifications_rule(const details::complex_modifications_rule& rule) { - complex_modifications_.push_front_rule(rule); - } - - void erase_complex_modifications_rule(size_t index) { - complex_modifications_.erase_rule(index); - } - - void move_complex_modifications_rule(size_t source_index, size_t destination_index) { - complex_modifications_.move_rule(source_index, destination_index); - } - - void set_complex_modifications_parameter(const std::string& name, int value) { - complex_modifications_.set_parameter_value(name, value); - } - gsl::not_null> get_virtual_hid_keyboard(void) const { return virtual_hid_keyboard_; } @@ -254,7 +246,7 @@ class profile final { gsl::not_null> parameters_; gsl::not_null> simple_modifications_; gsl::not_null> fn_function_keys_; - details::complex_modifications complex_modifications_; + gsl::not_null> complex_modifications_; gsl::not_null> virtual_hid_keyboard_; mutable std::vector>> devices_; configuration_json_helper::helper_values helper_values_; diff --git a/src/share/core_configuration/details/profile/complex_modifications.hpp b/src/share/core_configuration/details/profile/complex_modifications.hpp index 54e89db8e..1e67827e1 100644 --- a/src/share/core_configuration/details/profile/complex_modifications.hpp +++ b/src/share/core_configuration/details/profile/complex_modifications.hpp @@ -9,55 +9,57 @@ namespace core_configuration { namespace details { class complex_modifications final { public: - complex_modifications(void) : complex_modifications(nlohmann::json::object()) { + complex_modifications(void) + : complex_modifications(nlohmann::json::object(), + krbn::core_configuration::error_handling::loose) { } - complex_modifications(const nlohmann::json& json) : json_(json) { - pqrs::json::requires_object(json, "json"); + complex_modifications(const nlohmann::json& json, + error_handling error_handling) + : json_(json), + parameters_(std::make_shared(nlohmann::json::object(), + error_handling)) { + helper_values_.push_back_object("parameters", + parameters_); - // Load parameters_ + pqrs::json::requires_object(json, "json"); - if (auto v = pqrs::json::find_json(json, "parameters")) { - parameters_ = complex_modifications_parameters(v->value()); - } + helper_values_.update_value(json, error_handling); // Load rules_ if (auto v = pqrs::json::find_array(json, "rules")) { for (const auto& j : v->value()) { - rules_.emplace_back(j, parameters_); + rules_.push_back(std::make_shared(j, + parameters_, + error_handling)); } } } nlohmann::json to_json(void) const { auto j = json_; - j["rules"] = rules_; - j["parameters"] = parameters_.to_json(); + + helper_values_.update_json(j); + + // Note: Use the given value for `rules`. + return j; } - const complex_modifications_parameters& get_parameters(void) const { + gsl::not_null> get_parameters(void) const { return parameters_; } - void set_parameter_value(const std::string& name, int value) { - parameters_.set_value(name, value); - } - - const std::vector& get_rules(void) const { + const std::vector>>& get_rules(void) const { return rules_; } - void push_back_rule(const complex_modifications_rule& rule) { - rules_.push_back(rule); - } - - void push_front_rule(const complex_modifications_rule& rule) { + void push_front_rule(gsl::not_null> rule) { rules_.insert(std::begin(rules_), rule); } - void replace_rule(size_t index, const complex_modifications_rule& rule) { + void replace_rule(size_t index, gsl::not_null> rule) { if (index < rules_.size()) { rules_[index] = rule; } @@ -73,40 +75,11 @@ class complex_modifications final { vector_utility::move_element(rules_, source_index, destination_index); } - std::optional> minmax_parameter_value(const std::string& name) const { - std::optional> result; - - if (auto value = parameters_.get_value(name)) { - if (!result) { - result = std::make_pair(*value, *value); - } else if (*value < result->first) { - result->first = *value; - } else if (*value > result->second) { - result->second = *value; - } - } - - for (const auto& r : rules_) { - for (const auto& m : r.get_manipulators()) { - if (auto value = m.get_parameters().get_value(name)) { - if (!result) { - result = std::make_pair(*value, *value); - } else if (*value < result->first) { - result->first = *value; - } else if (*value > result->second) { - result->second = *value; - } - } - } - } - - return result; - } - private: nlohmann::json json_; - complex_modifications_parameters parameters_; - std::vector rules_; + gsl::not_null> parameters_; + std::vector>> rules_; + configuration_json_helper::helper_values helper_values_; }; inline void to_json(nlohmann::json& json, const complex_modifications& complex_modifications) { diff --git a/src/share/core_configuration/details/profile/complex_modifications_parameters.hpp b/src/share/core_configuration/details/profile/complex_modifications_parameters.hpp index 0a83447c1..4e63e3f32 100644 --- a/src/share/core_configuration/details/profile/complex_modifications_parameters.hpp +++ b/src/share/core_configuration/details/profile/complex_modifications_parameters.hpp @@ -1,5 +1,6 @@ #pragma once +#include "../../configuration_json_helper.hpp" #include namespace krbn { @@ -7,125 +8,149 @@ namespace core_configuration { namespace details { class complex_modifications_parameters final { public: - complex_modifications_parameters(void) : complex_modifications_parameters(nlohmann::json::object()) { + complex_modifications_parameters(const complex_modifications_parameters&) = delete; + + complex_modifications_parameters(void) + : complex_modifications_parameters(nlohmann::json::object(), + krbn::core_configuration::error_handling::loose) { } - complex_modifications_parameters(const nlohmann::json& json) : json_(json), - basic_simultaneous_threshold_milliseconds_(50), - basic_to_if_alone_timeout_milliseconds_(1000), - basic_to_if_held_down_threshold_milliseconds_(500), - basic_to_delayed_action_delay_milliseconds_(500), - mouse_motion_to_scroll_speed_(100) { - update(json); + complex_modifications_parameters(const nlohmann::json& json, + error_handling error_handling) + : json_(json) { + helper_values_.push_back_value("basic.simultaneous_threshold_milliseconds", + basic_simultaneous_threshold_milliseconds_, + 50); + + helper_values_.push_back_value("basic.to_if_alone_timeout_milliseconds", + basic_to_if_alone_timeout_milliseconds_, + 1000); + + helper_values_.push_back_value("basic.to_if_held_down_threshold_milliseconds", + basic_to_if_held_down_threshold_milliseconds_, + 500); + + helper_values_.push_back_value("basic.to_delayed_action_delay_milliseconds", + basic_to_delayed_action_delay_milliseconds_, + 500); + + helper_values_.push_back_value("mouse_motion_to_scroll.speed", + mouse_motion_to_scroll_speed_, + 100); + + pqrs::json::requires_object(json, "json"); + + helper_values_.update_value(json, error_handling); + + adjust_values(); } nlohmann::json to_json(void) const { auto j = json_; - for (const auto& pair : make_map()) { - j[pair.first] = pair.second; - } + + helper_values_.update_json(j); + return j; } - int get_basic_simultaneous_threshold_milliseconds(void) const { + void update(const nlohmann::json& json, + error_handling error_handling) { + pqrs::json::requires_object(json, "json"); + + helper_values_.update_value(json, error_handling); + + adjust_values(); + } + + const int& get_basic_simultaneous_threshold_milliseconds(void) const { return basic_simultaneous_threshold_milliseconds_; } - int get_basic_to_if_alone_timeout_milliseconds(void) const { + void set_basic_simultaneous_threshold_milliseconds(int value) { + basic_simultaneous_threshold_milliseconds_ = value; + + adjust_values(); + } + + const int& get_basic_to_if_alone_timeout_milliseconds(void) const { return basic_to_if_alone_timeout_milliseconds_; } - int get_basic_to_if_held_down_threshold_milliseconds(void) const { - return basic_to_if_held_down_threshold_milliseconds_; + void set_basic_to_if_alone_timeout_milliseconds(int value) { + basic_to_if_alone_timeout_milliseconds_ = value; + + adjust_values(); } - int get_basic_to_delayed_action_delay_milliseconds(void) const { - return basic_to_delayed_action_delay_milliseconds_; + const int& get_basic_to_if_held_down_threshold_milliseconds(void) const { + return basic_to_if_held_down_threshold_milliseconds_; } - int get_mouse_motion_to_scroll_speed(void) const { - return mouse_motion_to_scroll_speed_; + void set_basic_to_if_held_down_threshold_milliseconds(int value) { + basic_to_if_held_down_threshold_milliseconds_ = value; + + adjust_values(); } - double make_mouse_motion_to_scroll_speed_rate(void) const { - return static_cast(mouse_motion_to_scroll_speed_) / 100; + const int& get_basic_to_delayed_action_delay_milliseconds(void) const { + return basic_to_delayed_action_delay_milliseconds_; } - void update(const nlohmann::json& json) { - for (const auto& pair : make_map()) { - if (auto v = pqrs::json::find(json, pair.first)) { - const_cast(pair.second) = *v; - } - } + void set_basic_to_delayed_action_delay_milliseconds(int value) { + basic_to_delayed_action_delay_milliseconds_ = value; - normalize(); + adjust_values(); } - std::optional get_value(const std::string& name) const { - auto map = make_map(); - auto it = map.find(name); - if (it != std::end(map)) { - return it->second; - } - return std::nullopt; + const int& get_mouse_motion_to_scroll_speed(void) const { + return mouse_motion_to_scroll_speed_; } - void set_value(const std::string& name, int value) { - auto map = make_map(); - auto it = map.find(name); - if (it != std::end(map)) { - const_cast(it->second) = value; - } + void set_mouse_motion_to_scroll_speed(int value) { + mouse_motion_to_scroll_speed_ = value; - normalize(); + adjust_values(); + } + + double make_mouse_motion_to_scroll_speed_rate(void) const { + return static_cast(mouse_motion_to_scroll_speed_) / 100; } private: - void normalize(void) { - normalize(basic_simultaneous_threshold_milliseconds_, 0, 1000, "basic.simultaneous_threshold_milliseconds"); - normalize(basic_to_if_alone_timeout_milliseconds_, 0, std::nullopt, "basic.to_if_alone_timeout_milliseconds"); - normalize(basic_to_if_held_down_threshold_milliseconds_, 0, std::nullopt, "basic.to_if_held_down_threshold_milliseconds"); - normalize(basic_to_delayed_action_delay_milliseconds_, 0, std::nullopt, "basic.to_delayed_action_delay_milliseconds"); - normalize(mouse_motion_to_scroll_speed_, 1, 10000, "mouse_motion_to_scroll.speed"); - } - - void normalize(int& value, std::optional min, std::optional max, const std::string& name) { - if (min) { - if (value < *min) { - logger::get_logger()->warn("{0} should be >= {1}.", name, *min); + void adjust_values(void) { + adjust_value(basic_simultaneous_threshold_milliseconds_, 0, 1000); + adjust_value(basic_to_if_alone_timeout_milliseconds_, 0, std::nullopt); + adjust_value(basic_to_if_held_down_threshold_milliseconds_, 0, std::nullopt); + adjust_value(basic_to_delayed_action_delay_milliseconds_, 0, std::nullopt); + adjust_value(mouse_motion_to_scroll_speed_, 1, 10000); + } + + void adjust_value(int& value, std::optional min, std::optional max) { + if (auto v = helper_values_.find_value(value)) { + if (min) { + if (value < *min) { + logger::get_logger()->warn("{0} should be >= {1}.", v->get_key(), *min); + } + value = std::max(value, *min); } - value = std::max(value, *min); - } - if (max) { - if (value > *max) { - logger::get_logger()->warn("{0} should be <= {1}.", name, *max); + if (max) { + if (value > *max) { + logger::get_logger()->warn("{0} should be <= {1}.", v->get_key(), *max); + } + value = std::min(value, *max); } - value = std::min(value, *max); } } - std::unordered_map make_map(void) const { - return { - {"basic.simultaneous_threshold_milliseconds", basic_simultaneous_threshold_milliseconds_}, - {"basic.to_if_alone_timeout_milliseconds", basic_to_if_alone_timeout_milliseconds_}, - {"basic.to_if_held_down_threshold_milliseconds", basic_to_if_held_down_threshold_milliseconds_}, - {"basic.to_delayed_action_delay_milliseconds", basic_to_delayed_action_delay_milliseconds_}, - {"mouse_motion_to_scroll.speed", mouse_motion_to_scroll_speed_}, - }; - } - nlohmann::json json_; int basic_simultaneous_threshold_milliseconds_; int basic_to_if_alone_timeout_milliseconds_; int basic_to_if_held_down_threshold_milliseconds_; int basic_to_delayed_action_delay_milliseconds_; int mouse_motion_to_scroll_speed_; + configuration_json_helper::helper_values helper_values_; }; - -inline void to_json(nlohmann::json& json, const complex_modifications_parameters& parameters) { - json = parameters.to_json(); -} } // namespace details } // namespace core_configuration } // namespace krbn 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 b349dee96..7ae083a17 100644 --- a/src/share/core_configuration/details/profile/complex_modifications_rule.hpp +++ b/src/share/core_configuration/details/profile/complex_modifications_rule.hpp @@ -24,8 +24,10 @@ class complex_modifications_rule final { }; manipulator(const nlohmann::json& json, - const complex_modifications_parameters& parameters) : json_(json), - parameters_(parameters) { + gsl::not_null> parameters, + error_handling error_handling) + : json_(json), + parameters_(std::make_shared(parameters->to_json(), error_handling)) { pqrs::json::requires_object(json, "json"); for (const auto& [key, value] : json.items()) { @@ -37,9 +39,8 @@ class complex_modifications_rule final { } } else if (key == "parameters") { - pqrs::json::requires_object(value, "`" + key + "`"); - - parameters_.update(value); + parameters_->update(value, + error_handling); } else if (key == "description") { pqrs::json::requires_string(value, "`" + key + "`"); @@ -60,7 +61,7 @@ class complex_modifications_rule final { return conditions_; } - const complex_modifications_parameters& get_parameters(void) const { + gsl::not_null> get_parameters(void) const { return parameters_; } @@ -71,12 +72,16 @@ class complex_modifications_rule final { private: nlohmann::json json_; std::vector conditions_; - complex_modifications_parameters parameters_; + gsl::not_null> parameters_; std::string description_; }; + complex_modifications_rule(const complex_modifications_rule&) = delete; + complex_modifications_rule(const nlohmann::json& json, - const complex_modifications_parameters& parameters) : json_(json) { + gsl::not_null> parameters, + error_handling error_handling) + : json_(json) { pqrs::json::requires_object(json, "json"); for (const auto& [key, value] : json.items()) { @@ -85,7 +90,9 @@ class complex_modifications_rule final { for (const auto& j : value) { try { - manipulators_.emplace_back(j, parameters); + manipulators_.emplace_back(j, + parameters, + error_handling); } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what())); } diff --git a/src/share/core_configuration/details/profile/virtual_hid_keyboard.hpp b/src/share/core_configuration/details/profile/virtual_hid_keyboard.hpp index edd83b5fd..765a10773 100644 --- a/src/share/core_configuration/details/profile/virtual_hid_keyboard.hpp +++ b/src/share/core_configuration/details/profile/virtual_hid_keyboard.hpp @@ -1,5 +1,6 @@ #pragma once +#include "../../configuration_json_helper.hpp" #include "types.hpp" #include diff --git a/src/share/manipulator/manipulator_factory.hpp b/src/share/manipulator/manipulator_factory.hpp index 27e30f5d9..650ff2f1e 100644 --- a/src/share/manipulator/manipulator_factory.hpp +++ b/src/share/manipulator/manipulator_factory.hpp @@ -20,7 +20,7 @@ namespace krbn { namespace manipulator { namespace manipulator_factory { inline std::shared_ptr make_manipulator(const nlohmann::json& json, - const core_configuration::details::complex_modifications_parameters& parameters) { + gsl::not_null> parameters) { auto it = json.find("type"); if (it == std::end(json)) { throw pqrs::json::unmarshal_error(fmt::format("`type` must be specified: {0}", pqrs::json::dump_for_error_message(json))); diff --git a/src/share/manipulator/manipulator_manager.hpp b/src/share/manipulator/manipulator_manager.hpp index f1c13bf4a..e35f4cacb 100644 --- a/src/share/manipulator/manipulator_manager.hpp +++ b/src/share/manipulator/manipulator_manager.hpp @@ -15,7 +15,7 @@ class manipulator_manager final { } void push_back_manipulator(const nlohmann::json& json, - const core_configuration::details::complex_modifications_parameters& parameters) { + gsl::not_null> parameters) { try { auto m = manipulator_factory::make_manipulator(json, parameters); @@ -34,7 +34,7 @@ class manipulator_manager final { } } - void push_back_manipulator(std::shared_ptr ptr) { + void push_back_manipulator(gsl::not_null> ptr) { std::lock_guard lock(manipulators_mutex_); manipulators_.push_back(ptr); @@ -208,7 +208,7 @@ class manipulator_manager final { std::end(manipulators_)); } - std::vector> manipulators_; + std::vector>> manipulators_; mutable std::mutex manipulators_mutex_; }; } // namespace manipulator diff --git a/src/share/manipulator/manipulators/basic/basic.hpp b/src/share/manipulator/manipulators/basic/basic.hpp index 4d5445463..b7e4f690a 100644 --- a/src/share/manipulator/manipulators/basic/basic.hpp +++ b/src/share/manipulator/manipulators/basic/basic.hpp @@ -21,9 +21,10 @@ namespace basic { class basic final : public base, public pqrs::dispatcher::extra::dispatcher_client { public: basic(const nlohmann::json& json, - const core_configuration::details::complex_modifications_parameters& parameters) : base(), - dispatcher_client(), - parameters_(parameters) { + gsl::not_null> parameters) + : base(), + dispatcher_client(), + parameters_(parameters) { try { pqrs::json::requires_object(json, "json"); @@ -129,11 +130,14 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie } } + // For simple_modifications basic(const from_event_definition& from, - const std::vector& to) : base(), - dispatcher_client(), - from_(from), - to_(to) { + const std::vector& to) + : base(), + dispatcher_client(), + parameters_(std::make_shared()), + from_(from), + to_(to) { } virtual ~basic(void) { @@ -234,7 +238,7 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie { std::vector ordered_key_down_events; std::vector ordered_key_up_events; - std::chrono::milliseconds simultaneous_threshold_milliseconds(parameters_.get_basic_simultaneous_threshold_milliseconds()); + std::chrono::milliseconds simultaneous_threshold_milliseconds(parameters_->get_basic_simultaneous_threshold_milliseconds()); auto end_time_stamp = front_input_event.get_event_time_stamp().get_time_stamp() + pqrs::osx::chrono::make_absolute_time_duration(simultaneous_threshold_milliseconds); @@ -503,7 +507,7 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie if (!to_if_alone_.empty()) { auto duration = pqrs::osx::chrono::make_milliseconds(front_input_event.get_event_time_stamp().get_time_stamp() - current_manipulated_original_event->get_key_down_time_stamp()); if (current_manipulated_original_event->get_alone() && - duration < std::chrono::milliseconds(parameters_.get_basic_to_if_alone_timeout_milliseconds())) { + duration < std::chrono::milliseconds(parameters_->get_basic_to_if_alone_timeout_milliseconds())) { // // Before sending to_if_alone events, we should restore the modifier flags to the state they were in when the from-key was pressed. // @@ -613,7 +617,7 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie to_if_held_down_->setup(front_input_event, current_manipulated_original_event, output_event_queue, - std::chrono::milliseconds(parameters_.get_basic_to_if_held_down_threshold_milliseconds())); + std::chrono::milliseconds(parameters_->get_basic_to_if_held_down_threshold_milliseconds())); } // to_delayed_action_ @@ -622,7 +626,7 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie to_delayed_action_->setup(front_input_event, current_manipulated_original_event, output_event_queue, - std::chrono::milliseconds(parameters_.get_basic_to_delayed_action_delay_milliseconds())); + std::chrono::milliseconds(parameters_->get_basic_to_delayed_action_delay_milliseconds())); } // increase_time_stamp_delay @@ -758,7 +762,7 @@ class basic final : public base, public pqrs::dispatcher::extra::dispatcher_clie } } - core_configuration::details::complex_modifications_parameters parameters_; + gsl::not_null> parameters_; from_event_definition from_; std::vector to_; diff --git a/src/share/manipulator/manipulators/mouse_basic/mouse_basic.hpp b/src/share/manipulator/manipulators/mouse_basic/mouse_basic.hpp index 7e28eead4..016850490 100644 --- a/src/share/manipulator/manipulators/mouse_basic/mouse_basic.hpp +++ b/src/share/manipulator/manipulators/mouse_basic/mouse_basic.hpp @@ -10,7 +10,7 @@ namespace mouse_basic { class mouse_basic final : public base { public: mouse_basic(const nlohmann::json& json, - const core_configuration::details::complex_modifications_parameters& parameters) + gsl::not_null> parameters) : base(), flip_x_(false), flip_y_(false), diff --git a/src/share/manipulator/manipulators/mouse_motion_to_scroll/counter.hpp b/src/share/manipulator/manipulators/mouse_motion_to_scroll/counter.hpp index e4d545a2d..655d52f5a 100644 --- a/src/share/manipulator/manipulators/mouse_motion_to_scroll/counter.hpp +++ b/src/share/manipulator/manipulators/mouse_motion_to_scroll/counter.hpp @@ -33,18 +33,19 @@ class counter final : pqrs::dispatcher::extra::dispatcher_client { // Methods counter(std::weak_ptr weak_dispatcher, - const core_configuration::details::complex_modifications_parameters& parameters, - const options& options) : dispatcher_client(weak_dispatcher), - parameters_(parameters), - options_(options), - counter_direction_(counter_direction::none), - total_x_(0), - total_y_(0), - momentum_x_(0), - momentum_y_(0), - momentum_count_(0), - momentum_wait_(0), - timer_(*this) { + gsl::not_null> parameters, + const options& options) + : dispatcher_client(weak_dispatcher), + parameters_(parameters), + options_(options), + counter_direction_(counter_direction::none), + total_x_(0), + total_y_(0), + momentum_x_(0), + momentum_y_(0), + momentum_count_(0), + momentum_wait_(0), + timer_(*this) { } ~counter(void) { @@ -207,7 +208,7 @@ class counter final : pqrs::dispatcher::extra::dispatcher_client { // Multiply x,y - double multiplier = parameters_.make_mouse_motion_to_scroll_speed_rate() * + double multiplier = parameters_->make_mouse_motion_to_scroll_speed_rate() * options_.get_speed_multiplier(); x *= multiplier; y *= multiplier; @@ -409,7 +410,7 @@ class counter final : pqrs::dispatcher::extra::dispatcher_client { } } - const core_configuration::details::complex_modifications_parameters parameters_; + gsl::not_null> parameters_; const options options_; std::deque entries_; diff --git a/src/share/manipulator/manipulators/mouse_motion_to_scroll/mouse_motion_to_scroll.hpp b/src/share/manipulator/manipulators/mouse_motion_to_scroll/mouse_motion_to_scroll.hpp index f2ca1c1e2..cf5dd8ddc 100644 --- a/src/share/manipulator/manipulators/mouse_motion_to_scroll/mouse_motion_to_scroll.hpp +++ b/src/share/manipulator/manipulators/mouse_motion_to_scroll/mouse_motion_to_scroll.hpp @@ -14,8 +14,9 @@ namespace mouse_motion_to_scroll { class mouse_motion_to_scroll final : public base, public pqrs::dispatcher::extra::dispatcher_client { public: mouse_motion_to_scroll(const nlohmann::json& json, - const core_configuration::details::complex_modifications_parameters& parameters) : base(), - dispatcher_client() { + gsl::not_null> parameters) + : base(), + dispatcher_client() { try { pqrs::json::requires_object(json, "json"); diff --git a/tests/src/complex_modifications_assets/src/complex_modifications_assets_file_test.hpp b/tests/src/complex_modifications_assets/src/complex_modifications_assets_file_test.hpp index 6682da692..81ab5d137 100644 --- a/tests/src/complex_modifications_assets/src/complex_modifications_assets_file_test.hpp +++ b/tests/src/complex_modifications_assets/src/complex_modifications_assets_file_test.hpp @@ -13,7 +13,9 @@ void run_complex_modifications_assets_file_test(void) { std::vector error_messages; try { auto file_path = "json/lint/" + assets_json_entry.at("input").get(); - error_messages = krbn::complex_modifications_assets_file(file_path).lint(); + error_messages = krbn::complex_modifications_assets_file(file_path, + krbn::core_configuration::error_handling::loose) + .lint(); } catch (std::exception& e) { error_messages.push_back(e.what()); } diff --git a/tests/src/complex_modifications_assets/src/complex_modifications_assets_manager_test.hpp b/tests/src/complex_modifications_assets/src/complex_modifications_assets_manager_test.hpp index 29e960a48..4b42b08b5 100644 --- a/tests/src/complex_modifications_assets/src/complex_modifications_assets_manager_test.hpp +++ b/tests/src/complex_modifications_assets/src/complex_modifications_assets_manager_test.hpp @@ -9,33 +9,37 @@ void run_complex_modifications_assets_manager_test(void) { "reload"_test = [] { { krbn::complex_modifications_assets_manager complex_modifications_assets_manager; - complex_modifications_assets_manager.reload("json/complex_modifications", false); + complex_modifications_assets_manager.reload("json/complex_modifications", + krbn::core_configuration::error_handling::strict, + false); auto& files = complex_modifications_assets_manager.get_files(); expect(files.size() == 5); - expect(files[0].get_title() == "CCC"); + expect(files[0]->get_title() == "CCC"); { - auto& rules = files[0].get_rules(); + auto& rules = files[0]->get_rules(); expect(rules.size() == 1); - expect(rules[0].get_description() == "CCC1"); + expect(rules[0]->get_description() == "CCC1"); } - expect(files[1].get_title() == "EEE"); + expect(files[1]->get_title() == "EEE"); { - auto& rules = files[1].get_rules(); + auto& rules = files[1]->get_rules(); expect(rules.size() == 2); - expect(rules[0].get_description() == "EEE1"); - expect(rules[1].get_description() == "EEE2"); + expect(rules[0]->get_description() == "EEE1"); + expect(rules[1]->get_description() == "EEE2"); } - expect(files[2].get_title() == "FFF"); - expect(files[3].get_title() == "SSS"); - expect(files[4].get_title() == "symlink"); + expect(files[2]->get_title() == "FFF"); + expect(files[3]->get_title() == "SSS"); + expect(files[4]->get_title() == "symlink"); } { krbn::complex_modifications_assets_manager complex_modifications_assets_manager; - complex_modifications_assets_manager.reload("not_found", false); + complex_modifications_assets_manager.reload("not_found", + krbn::core_configuration::error_handling::strict, + false); auto& files = complex_modifications_assets_manager.get_files(); expect(files.size() == 0); diff --git a/tests/src/core_configuration/json/minmax_parameter_value_test1.json b/tests/src/core_configuration/json/minmax_parameter_value_test1.json deleted file mode 100644 index 781adf308..000000000 --- a/tests/src/core_configuration/json/minmax_parameter_value_test1.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "profiles": [ - { - "name": "Default profile", - "selected": true, - "complex_modifications": { - "rules": [ - { - "manipulators": [ - { - "type": "basic", - "parameters": { - "basic.simultaneous_threshold_milliseconds": 101 - } - }, - { - "type": "basic", - "parameters": { - "basic.simultaneous_threshold_milliseconds": 201 - } - } - ] - }, - { - "manipulators": [ - { - "type": "basic", - "parameters": { - "basic.simultaneous_threshold_milliseconds": 301 - } - } - ] - } - ], - "parameters": { - "basic.simultaneous_threshold_milliseconds": 401 - } - } - } - ] -} diff --git a/tests/src/core_configuration/json/minmax_parameter_value_test2.json b/tests/src/core_configuration/json/minmax_parameter_value_test2.json deleted file mode 100644 index b0d89d709..000000000 --- a/tests/src/core_configuration/json/minmax_parameter_value_test2.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "profiles": [ - { - "name": "Default profile", - "selected": true, - "complex_modifications": { - "rules": [ - { - "manipulators": [ - { - "type": "basic", - "parameters": { - "basic.simultaneous_threshold_milliseconds": 402 - } - }, - { - "type": "basic", - "parameters": { - "basic.simultaneous_threshold_milliseconds": 202 - } - } - ] - }, - { - "manipulators": [ - { - "type": "basic", - "parameters": { - "basic.simultaneous_threshold_milliseconds": 302 - } - } - ] - } - ], - "parameters": { - "basic.simultaneous_threshold_milliseconds": 102 - } - } - } - ] -} diff --git a/tests/src/core_configuration/json/minmax_parameter_value_test3.json b/tests/src/core_configuration/json/minmax_parameter_value_test3.json deleted file mode 100644 index 1953fbd90..000000000 --- a/tests/src/core_configuration/json/minmax_parameter_value_test3.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "profiles": [ - { - "name": "Default profile", - "selected": true, - "complex_modifications": { - "rules": [ - { - "manipulators": [ - { - "type": "basic", - "parameters": { - "basic.simultaneous_threshold_milliseconds": 203 - } - }, - { - "type": "basic", - "parameters": { - "basic.simultaneous_threshold_milliseconds": 303 - } - } - ] - }, - { - "manipulators": [ - { - "type": "basic", - "parameters": { - "basic.simultaneous_threshold_milliseconds": 403 - } - } - ] - } - ], - "parameters": { - "basic.simultaneous_threshold_milliseconds": 103 - } - } - } - ] -} diff --git a/tests/src/core_configuration/json/to_json_default.json b/tests/src/core_configuration/json/to_json_default.json index e964e0d98..e9fb50ab7 100644 --- a/tests/src/core_configuration/json/to_json_default.json +++ b/tests/src/core_configuration/json/to_json_default.json @@ -1,16 +1,6 @@ { "profiles": [ { - "complex_modifications": { - "rules": [], - "parameters": { - "basic.simultaneous_threshold_milliseconds": 50, - "basic.to_delayed_action_delay_milliseconds": 500, - "basic.to_if_alone_timeout_milliseconds": 1000, - "basic.to_if_held_down_threshold_milliseconds": 500, - "mouse_motion_to_scroll.speed": 100 - } - }, "name": "Default profile", "selected": true } diff --git a/tests/src/core_configuration/json/to_json_example.json b/tests/src/core_configuration/json/to_json_example.json index f461e28a9..11671717f 100644 --- a/tests/src/core_configuration/json/to_json_example.json +++ b/tests/src/core_configuration/json/to_json_example.json @@ -15,11 +15,7 @@ "keep_me": true }, "parameters": { - "basic.simultaneous_threshold_milliseconds": 50, - "basic.to_delayed_action_delay_milliseconds": 500, "basic.to_if_alone_timeout_milliseconds": 800, - "basic.to_if_held_down_threshold_milliseconds": 500, - "mouse_motion_to_scroll.speed": 100, "dummy": { "keep_me": true } @@ -250,29 +246,9 @@ } }, { - "complex_modifications": { - "parameters": { - "basic.simultaneous_threshold_milliseconds": 50, - "basic.to_delayed_action_delay_milliseconds": 500, - "basic.to_if_alone_timeout_milliseconds": 1000, - "basic.to_if_held_down_threshold_milliseconds": 500, - "mouse_motion_to_scroll.speed": 100 - }, - "rules": [] - }, "name": "Empty" }, { - "complex_modifications": { - "parameters": { - "basic.simultaneous_threshold_milliseconds": 50, - "basic.to_delayed_action_delay_milliseconds": 500, - "basic.to_if_alone_timeout_milliseconds": 1000, - "basic.to_if_held_down_threshold_milliseconds": 500, - "mouse_motion_to_scroll.speed": 100 - }, - "rules": [] - }, "fn_function_keys": [ { "from": { @@ -288,16 +264,6 @@ "name": "fn_function_keys v1" }, { - "complex_modifications": { - "parameters": { - "basic.simultaneous_threshold_milliseconds": 50, - "basic.to_delayed_action_delay_milliseconds": 500, - "basic.to_if_alone_timeout_milliseconds": 1000, - "basic.to_if_held_down_threshold_milliseconds": 500, - "mouse_motion_to_scroll.speed": 100 - }, - "rules": [] - }, "name": "simple_modifications v2", "simple_modifications": [ { diff --git a/tests/src/core_configuration/src/core_configuration_test.hpp b/tests/src/core_configuration/src/core_configuration_test.hpp index b37137dcf..f729e0bcc 100644 --- a/tests/src/core_configuration/src/core_configuration_test.hpp +++ b/tests/src/core_configuration/src/core_configuration_test.hpp @@ -28,7 +28,7 @@ void run_core_configuration_test(void) { expect(expected == configuration.get_selected_profile().get_simple_modifications()->get_pairs()) << UT_SHOW_LINE; } { - auto manipulator = configuration.get_selected_profile().get_complex_modifications().get_rules()[0].get_manipulators()[0].get_json(); + auto manipulator = configuration.get_selected_profile().get_complex_modifications()->get_rules()[0]->get_manipulators()[0].get_json(); expect("basic" == manipulator["type"]); expect("open_bracket" == manipulator["from"]["key_code"]); } @@ -74,14 +74,14 @@ void run_core_configuration_test(void) { expect(expected == configuration.get_selected_profile().get_fn_function_keys()->get_pairs()) << UT_SHOW_LINE; } { - auto& complex_modifications = configuration.get_selected_profile().get_complex_modifications(); - auto& rules = complex_modifications.get_rules(); - expect(complex_modifications.get_parameters().get_basic_to_if_alone_timeout_milliseconds() == 800); - expect(rules[0].get_manipulators()[0].get_parameters().get_basic_to_if_alone_timeout_milliseconds() == 800); - expect(rules[0].get_manipulators()[2].get_parameters().get_basic_to_if_alone_timeout_milliseconds() == 400); - expect(rules[0].get_description() == "Emacs bindings, etc."); - expect(rules[1].get_description() == "description test"); - expect(rules[2].get_description() == ""); + auto complex_modifications = configuration.get_selected_profile().get_complex_modifications(); + auto& rules = complex_modifications->get_rules(); + expect(complex_modifications->get_parameters()->get_basic_to_if_alone_timeout_milliseconds() == 800); + expect(rules[0]->get_manipulators()[0].get_parameters()->get_basic_to_if_alone_timeout_milliseconds() == 800); + expect(rules[0]->get_manipulators()[2].get_parameters()->get_basic_to_if_alone_timeout_milliseconds() == 400); + expect(rules[0]->get_description() == "Emacs bindings, etc."); + expect(rules[1]->get_description() == "description test"); + expect(rules[2]->get_description() == ""); } { expect(configuration @@ -662,19 +662,7 @@ void run_core_configuration_test(void) { auto json = nlohmann::json::object(); krbn::core_configuration::details::profile empty_profile(json, krbn::core_configuration::error_handling::strict); - nlohmann::json expected({ - {"complex_modifications", nlohmann::json::object({ - {"rules", nlohmann::json::array()}, - {"parameters", nlohmann::json::object({ - {"basic.simultaneous_threshold_milliseconds", 50}, - {"basic.to_if_alone_timeout_milliseconds", 1000}, - {"basic.to_if_held_down_threshold_milliseconds", 500}, - {"basic.to_delayed_action_delay_milliseconds", 500}, - {"mouse_motion_to_scroll.speed", 100}, - })}, - })}, - }); - expect(empty_profile.to_json() == expected) << UT_SHOW_LINE; + expect(json == empty_profile.to_json()) << UT_SHOW_LINE; } { nlohmann::json json({ @@ -834,16 +822,6 @@ void run_core_configuration_test(void) { profile.get_virtual_hid_keyboard()->set_mouse_key_xy_scale(250); nlohmann::json expected({ - {"complex_modifications", nlohmann::json::object({ - {"rules", nlohmann::json::array()}, - {"parameters", nlohmann::json::object({ - {"basic.simultaneous_threshold_milliseconds", 50}, - {"basic.to_if_alone_timeout_milliseconds", 1000}, - {"basic.to_if_held_down_threshold_milliseconds", 500}, - {"basic.to_delayed_action_delay_milliseconds", 500}, - {"mouse_motion_to_scroll.speed", 100}, - })}, - })}, {"devices", { { {"identifiers", { @@ -1151,9 +1129,10 @@ void run_core_configuration_test(void) { // empty json { auto json = nlohmann::json::object(); - krbn::core_configuration::details::complex_modifications complex_modifications(json); + krbn::core_configuration::details::complex_modifications complex_modifications(json, + krbn::core_configuration::error_handling::strict); expect(complex_modifications.get_rules().empty()); - expect(complex_modifications.get_parameters().get_basic_to_if_alone_timeout_milliseconds() == 1000); + expect(complex_modifications.get_parameters()->get_basic_to_if_alone_timeout_milliseconds() == 1000); } // load values from json @@ -1184,12 +1163,13 @@ void run_core_configuration_test(void) { }, }, }); - krbn::core_configuration::details::complex_modifications complex_modifications(json); + krbn::core_configuration::details::complex_modifications complex_modifications(json, + krbn::core_configuration::error_handling::strict); expect(complex_modifications.get_rules().size() == 3); } }; - "complex_modifications.push_back_rule"_test = [] { + "complex_modifications.push_front_rule"_test = [] { { auto manipulators = nlohmann::json::array({ nlohmann::json::object({ @@ -1217,25 +1197,28 @@ void run_core_configuration_test(void) { }, }, }); - krbn::core_configuration::details::complex_modifications complex_modifications(json); + krbn::core_configuration::details::complex_modifications complex_modifications(json, + krbn::core_configuration::error_handling::strict); auto& rules = complex_modifications.get_rules(); expect(rules.size() == 3); - expect(rules[0].get_description() == "rule 1"); - expect(rules[1].get_description() == "rule 2"); - expect(rules[2].get_description() == "rule 3"); + expect(rules[0]->get_description() == "rule 1"); + expect(rules[1]->get_description() == "rule 2"); + expect(rules[2]->get_description() == "rule 3"); - krbn::core_configuration::details::complex_modifications_parameters parameters; nlohmann::json rule_json; rule_json["description"] = "rule 4"; rule_json["manipulators"] = manipulators; - krbn::core_configuration::details::complex_modifications_rule rule(rule_json, parameters); + auto rule = std::make_shared( + rule_json, + complex_modifications.get_parameters(), + krbn::core_configuration::error_handling::strict); - complex_modifications.push_back_rule(rule); + complex_modifications.push_front_rule(rule); expect(rules.size() == 4); - expect(rules[0].get_description() == "rule 1"); - expect(rules[1].get_description() == "rule 2"); - expect(rules[2].get_description() == "rule 3"); - expect(rules[3].get_description() == "rule 4"); + expect(rules[0]->get_description() == "rule 4"); + expect(rules[1]->get_description() == "rule 1"); + expect(rules[2]->get_description() == "rule 2"); + expect(rules[3]->get_description() == "rule 3"); } }; @@ -1267,36 +1250,43 @@ void run_core_configuration_test(void) { }, }, }); - krbn::core_configuration::details::complex_modifications complex_modifications(json); + krbn::core_configuration::details::complex_modifications complex_modifications(json, + krbn::core_configuration::error_handling::strict); auto& rules = complex_modifications.get_rules(); expect(3 == rules.size()); - expect("rule 1"sv == rules[0].get_description()); - expect("rule 2"sv == rules[1].get_description()); - expect("rule 3"sv == rules[2].get_description()); - - complex_modifications.replace_rule(1, - krbn::core_configuration::details::complex_modifications_rule( - nlohmann::json({ - {"description", "replaced 2"}, - {"manipulators", manipulators}, - }), - krbn::core_configuration::details::complex_modifications_parameters())); - expect(3 == rules.size()); - expect("rule 1"sv == rules[0].get_description()); - expect("replaced 2"sv == rules[1].get_description()); - expect("rule 3"sv == rules[2].get_description()); - - complex_modifications.replace_rule(3, - krbn::core_configuration::details::complex_modifications_rule( - nlohmann::json({ - {"description", "ignored"}, - {"manipulators", manipulators}, - }), - krbn::core_configuration::details::complex_modifications_parameters())); - expect(3 == rules.size()); - expect("rule 1"sv == rules[0].get_description()); - expect("replaced 2"sv == rules[1].get_description()); - expect("rule 3"sv == rules[2].get_description()); + expect("rule 1"sv == rules[0]->get_description()); + expect("rule 2"sv == rules[1]->get_description()); + expect("rule 3"sv == rules[2]->get_description()); + + { + auto rule = std::make_shared( + nlohmann::json({ + {"description", "replaced 2"}, + {"manipulators", manipulators}, + }), + complex_modifications.get_parameters(), + krbn::core_configuration::error_handling::strict); + complex_modifications.replace_rule(1, rule); + expect(3 == rules.size()); + expect("rule 1"sv == rules[0]->get_description()); + expect("replaced 2"sv == rules[1]->get_description()); + expect("rule 3"sv == rules[2]->get_description()); + } + + { + auto rule = std::make_shared( + nlohmann::json({ + {"description", "ignored"}, + {"manipulators", manipulators}, + }), + complex_modifications.get_parameters(), + krbn::core_configuration::error_handling::strict); + complex_modifications.replace_rule(3, rule); + expect(3 == rules.size()); + expect("rule 1"sv == rules[0]->get_description()); + expect("replaced 2"sv == rules[1]->get_description()); + expect("rule 3"sv == rules[2]->get_description()); + } } }; @@ -1328,21 +1318,22 @@ void run_core_configuration_test(void) { }, }, }); - krbn::core_configuration::details::complex_modifications complex_modifications(json); + krbn::core_configuration::details::complex_modifications complex_modifications(json, + krbn::core_configuration::error_handling::strict); auto& rules = complex_modifications.get_rules(); expect(rules.size() == 3); - expect(rules[0].get_description() == "rule 1"); - expect(rules[1].get_description() == "rule 2"); - expect(rules[2].get_description() == "rule 3"); + expect(rules[0]->get_description() == "rule 1"); + expect(rules[1]->get_description() == "rule 2"); + expect(rules[2]->get_description() == "rule 3"); complex_modifications.erase_rule(0); expect(rules.size() == 2); - expect(rules[0].get_description() == "rule 2"); - expect(rules[1].get_description() == "rule 3"); + expect(rules[0]->get_description() == "rule 2"); + expect(rules[1]->get_description() == "rule 3"); complex_modifications.erase_rule(1); expect(rules.size() == 1); - expect(rules[0].get_description() == "rule 2"); + expect(rules[0]->get_description() == "rule 2"); complex_modifications.erase_rule(1); expect(rules.size() == 1); @@ -1387,40 +1378,43 @@ void run_core_configuration_test(void) { }); { - krbn::core_configuration::details::complex_modifications complex_modifications(json); + krbn::core_configuration::details::complex_modifications complex_modifications(json, + krbn::core_configuration::error_handling::strict); auto& rules = complex_modifications.get_rules(); expect(5_ul == rules.size()); - expect("rule 1" == rules[0].get_description()); - expect("rule 2" == rules[1].get_description()); - expect("rule 3" == rules[2].get_description()); - expect("rule 4" == rules[3].get_description()); - expect("rule 5" == rules[4].get_description()); + expect("rule 1" == rules[0]->get_description()); + expect("rule 2" == rules[1]->get_description()); + expect("rule 3" == rules[2]->get_description()); + expect("rule 4" == rules[3]->get_description()); + expect("rule 5" == rules[4]->get_description()); } // 0 -> 2 { - krbn::core_configuration::details::complex_modifications complex_modifications(json); + krbn::core_configuration::details::complex_modifications complex_modifications(json, + krbn::core_configuration::error_handling::strict); auto& rules = complex_modifications.get_rules(); complex_modifications.move_rule(0, 2); expect(5_ul == rules.size()); - expect("rule 2"sv == rules[0].get_description()); - expect("rule 1"sv == rules[1].get_description()); - expect("rule 3"sv == rules[2].get_description()); - expect("rule 4"sv == rules[3].get_description()); - expect("rule 5"sv == rules[4].get_description()); + expect("rule 2"sv == rules[0]->get_description()); + expect("rule 1"sv == rules[1]->get_description()); + expect("rule 3"sv == rules[2]->get_description()); + expect("rule 4"sv == rules[3]->get_description()); + expect("rule 5"sv == rules[4]->get_description()); } // 2 -> 0 { - krbn::core_configuration::details::complex_modifications complex_modifications(json); + krbn::core_configuration::details::complex_modifications complex_modifications(json, + krbn::core_configuration::error_handling::strict); auto& rules = complex_modifications.get_rules(); complex_modifications.move_rule(2, 0); expect(5_ul == rules.size()); - expect("rule 3" == rules[0].get_description()); - expect("rule 1" == rules[1].get_description()); - expect("rule 2" == rules[2].get_description()); - expect("rule 4" == rules[3].get_description()); - expect("rule 5" == rules[4].get_description()); + expect("rule 3" == rules[0]->get_description()); + expect("rule 1" == rules[1]->get_description()); + expect("rule 2" == rules[2]->get_description()); + expect("rule 4" == rules[3]->get_description()); + expect("rule 5" == rules[4]->get_description()); } } }; @@ -1428,8 +1422,9 @@ void run_core_configuration_test(void) { "complex_modifications.parameters"_test = [] { // empty json { - nlohmann::json json; - krbn::core_configuration::details::complex_modifications_parameters parameters(json); + auto json = "{}"_json; + krbn::core_configuration::details::complex_modifications_parameters parameters(json, + krbn::core_configuration::error_handling::strict); expect(parameters.get_basic_to_if_alone_timeout_milliseconds() == 1000); } @@ -1437,7 +1432,8 @@ void run_core_configuration_test(void) { { nlohmann::json json; json["basic.to_if_alone_timeout_milliseconds"] = 1234; - krbn::core_configuration::details::complex_modifications_parameters parameters(json); + krbn::core_configuration::details::complex_modifications_parameters parameters(json, + krbn::core_configuration::error_handling::strict); expect(parameters.get_basic_to_if_alone_timeout_milliseconds() == 1234); } @@ -1445,18 +1441,21 @@ void run_core_configuration_test(void) { { nlohmann::json json; json["basic.to_if_alone_timeout_milliseconds"] = "1234"; - krbn::core_configuration::details::complex_modifications_parameters parameters(json); + krbn::core_configuration::details::complex_modifications_parameters parameters(json, + krbn::core_configuration::error_handling::loose); expect(parameters.get_basic_to_if_alone_timeout_milliseconds() == 1000); } // normalize { - krbn::core_configuration::details::complex_modifications_parameters parameters(nlohmann::json::object({ - {"basic.simultaneous_threshold_milliseconds", -1000}, - {"basic.to_if_alone_timeout_milliseconds", -1000}, - {"basic.to_if_held_down_threshold_milliseconds", -1000}, - {"basic.to_delayed_action_delay_milliseconds", -1000}, - })); + krbn::core_configuration::details::complex_modifications_parameters parameters( + nlohmann::json::object({ + {"basic.simultaneous_threshold_milliseconds", -1000}, + {"basic.to_if_alone_timeout_milliseconds", -1000}, + {"basic.to_if_held_down_threshold_milliseconds", -1000}, + {"basic.to_delayed_action_delay_milliseconds", -1000}, + }), + krbn::core_configuration::error_handling::strict); expect(parameters.get_basic_simultaneous_threshold_milliseconds() == 0); expect(parameters.get_basic_to_if_alone_timeout_milliseconds() == 0); @@ -1464,12 +1463,14 @@ void run_core_configuration_test(void) { expect(parameters.get_basic_to_delayed_action_delay_milliseconds() == 0); } { - krbn::core_configuration::details::complex_modifications_parameters parameters(nlohmann::json::object({ - {"basic.simultaneous_threshold_milliseconds", 100000}, - {"basic.to_if_alone_timeout_milliseconds", 100000}, - {"basic.to_if_held_down_threshold_milliseconds", 100000}, - {"basic.to_delayed_action_delay_milliseconds", 100000}, - })); + krbn::core_configuration::details::complex_modifications_parameters parameters( + nlohmann::json::object({ + {"basic.simultaneous_threshold_milliseconds", 100000}, + {"basic.to_if_alone_timeout_milliseconds", 100000}, + {"basic.to_if_held_down_threshold_milliseconds", 100000}, + {"basic.to_delayed_action_delay_milliseconds", 100000}, + }), + krbn::core_configuration::error_handling::strict); expect(parameters.get_basic_simultaneous_threshold_milliseconds() == 1000); expect(parameters.get_basic_to_if_alone_timeout_milliseconds() == 100000); @@ -1477,22 +1478,24 @@ void run_core_configuration_test(void) { expect(parameters.get_basic_to_delayed_action_delay_milliseconds() == 100000); } { - krbn::core_configuration::details::complex_modifications_parameters parameters(nlohmann::json::object({})); + krbn::core_configuration::details::complex_modifications_parameters parameters( + nlohmann::json::object({}), + krbn::core_configuration::error_handling::strict); - parameters.set_value("basic.simultaneous_threshold_milliseconds", -1000); - parameters.set_value("basic.to_if_alone_timeout_milliseconds", -1000); - parameters.set_value("basic.to_if_held_down_threshold_milliseconds", -1000); - parameters.set_value("basic.to_delayed_action_delay_milliseconds", -1000); + parameters.set_basic_simultaneous_threshold_milliseconds(-1000); + parameters.set_basic_to_if_alone_timeout_milliseconds(-1000); + parameters.set_basic_to_if_held_down_threshold_milliseconds(-1000); + parameters.set_basic_to_delayed_action_delay_milliseconds(-1000); expect(parameters.get_basic_simultaneous_threshold_milliseconds() == 0); expect(parameters.get_basic_to_if_alone_timeout_milliseconds() == 0); expect(parameters.get_basic_to_if_held_down_threshold_milliseconds() == 0); expect(parameters.get_basic_to_delayed_action_delay_milliseconds() == 0); - parameters.set_value("basic.simultaneous_threshold_milliseconds", 100000); - parameters.set_value("basic.to_if_alone_timeout_milliseconds", 100000); - parameters.set_value("basic.to_if_held_down_threshold_milliseconds", 100000); - parameters.set_value("basic.to_delayed_action_delay_milliseconds", 100000); + parameters.set_basic_simultaneous_threshold_milliseconds(100000); + parameters.set_basic_to_if_alone_timeout_milliseconds(100000); + parameters.set_basic_to_if_held_down_threshold_milliseconds(100000); + parameters.set_basic_to_delayed_action_delay_milliseconds(100000); expect(parameters.get_basic_simultaneous_threshold_milliseconds() == 1000); expect(parameters.get_basic_to_if_alone_timeout_milliseconds() == 100000); @@ -1501,40 +1504,6 @@ void run_core_configuration_test(void) { } }; - "complex_modifications.minmax_parameter_value"_test = [] { - { - krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test1.json", - geteuid(), - krbn::core_configuration::error_handling::strict); - auto actual = configuration.get_selected_profile().get_complex_modifications().minmax_parameter_value("basic.simultaneous_threshold_milliseconds"); - expect(actual->first == 101); - expect(actual->second == 401); - } - { - krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test2.json", - geteuid(), - krbn::core_configuration::error_handling::strict); - auto actual = configuration.get_selected_profile().get_complex_modifications().minmax_parameter_value("basic.simultaneous_threshold_milliseconds"); - expect(actual->first == 102); - expect(actual->second == 402); - } - { - krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test3.json", - geteuid(), - krbn::core_configuration::error_handling::strict); - auto actual = configuration.get_selected_profile().get_complex_modifications().minmax_parameter_value("basic.simultaneous_threshold_milliseconds"); - expect(actual->first == 103); - expect(actual->second == 403); - } - - { - krbn::core_configuration::core_configuration configuration("json/minmax_parameter_value_test1.json", - geteuid(), - krbn::core_configuration::error_handling::strict); - expect(!configuration.get_selected_profile().get_complex_modifications().minmax_parameter_value("unknown")); - } - }; - "virtual_hid_keyboard"_test = [] { // empty json { diff --git a/tests/src/core_configuration/src/errors_test.hpp b/tests/src/core_configuration/src/errors_test.hpp index 7eb0a50a3..53ecd3bc8 100644 --- a/tests/src/core_configuration/src/errors_test.hpp +++ b/tests/src/core_configuration/src/errors_test.hpp @@ -9,7 +9,8 @@ using namespace boost::ut::literals; void handle_json(const nlohmann::json& json) { auto c = json.at("class").get(); if (c == "complex_modifications") { - krbn::core_configuration::details::complex_modifications(json.at("input")); + krbn::core_configuration::details::complex_modifications(json.at("input"), + krbn::core_configuration::error_handling::strict); } else if (c == "devices") { krbn::core_configuration::details::device(json.at("input"), krbn::core_configuration::error_handling::strict); diff --git a/tests/src/manipulator/src/manipulator_factory_test.hpp b/tests/src/manipulator/src/manipulator_factory_test.hpp index 823d10519..472d59f92 100644 --- a/tests/src/manipulator/src/manipulator_factory_test.hpp +++ b/tests/src/manipulator/src/manipulator_factory_test.hpp @@ -47,7 +47,7 @@ void run_manipulator_factory_test(void) { }, }, }); - krbn::core_configuration::details::complex_modifications_parameters parameters; + auto parameters = std::make_shared(); auto manipulator = krbn::manipulator::manipulator_factory::make_manipulator(json, parameters); expect(dynamic_cast(manipulator.get()) != nullptr); @@ -179,7 +179,7 @@ void run_manipulator_factory_test(void) { { try { nlohmann::json json; - krbn::core_configuration::details::complex_modifications_parameters parameters; + auto parameters = std::make_shared(); krbn::manipulator::manipulator_factory::make_manipulator(json, parameters); expect(false); } catch (pqrs::json::unmarshal_error& ex) { diff --git a/tests/src/manipulator/src/manipulator_manager_test.hpp b/tests/src/manipulator/src/manipulator_manager_test.hpp index bf51575e8..063a9c9f7 100644 --- a/tests/src/manipulator/src/manipulator_manager_test.hpp +++ b/tests/src/manipulator/src/manipulator_manager_test.hpp @@ -99,7 +99,7 @@ void run_manipulator_manager_test(void) { auto json = krbn::unit_testing::json_helper::load_jsonc(file_name); auto manager = std::make_shared(); for (const auto& j : json) { - krbn::core_configuration::details::complex_modifications_parameters parameters; + auto parameters = std::make_shared(); auto m = krbn::manipulator::manipulator_factory::make_manipulator(j, parameters); manager->push_back_manipulator(m); diff --git a/tests/src/manipulator_basic/src/errors_test.hpp b/tests/src/manipulator_basic/src/errors_test.hpp index 0c3594094..fd551cdb4 100644 --- a/tests/src/manipulator_basic/src/errors_test.hpp +++ b/tests/src/manipulator_basic/src/errors_test.hpp @@ -10,7 +10,7 @@ using namespace boost::ut::literals; void handle_json(const nlohmann::json& json) { auto c = json.at("class").get(); if (c == "basic") { - krbn::core_configuration::details::complex_modifications_parameters parameters; + auto parameters = std::make_shared(); krbn::manipulator::manipulators::basic::basic(json.at("input"), parameters); } else if (c == "from_event_definition") { json.at("input").get(); diff --git a/tests/src/manipulator_basic/src/manipulator_basic_test.hpp b/tests/src/manipulator_basic/src/manipulator_basic_test.hpp index cbe71b19f..934db4fa7 100644 --- a/tests/src/manipulator_basic/src/manipulator_basic_test.hpp +++ b/tests/src/manipulator_basic/src/manipulator_basic_test.hpp @@ -256,8 +256,9 @@ void run_manipulator_basic_test(void) { {"key_code", "tab"}, })}, }); + auto parameters = std::make_shared(); basic::basic b(json, - krbn::core_configuration::details::complex_modifications_parameters()); + parameters); expect(b.get_to().size() == 1); { auto& d = b.get_to()[0].get_event_definition(); @@ -276,8 +277,9 @@ void run_manipulator_basic_test(void) { nlohmann::json::object({{"key_code", "spacebar"}}), })}, }); + auto parameters = std::make_shared(); basic::basic b(json, - krbn::core_configuration::details::complex_modifications_parameters()); + parameters); expect(b.get_to().size() == 2); { auto& d = b.get_to()[0].get_event_definition(); diff --git a/tests/src/manipulator_basic/src/to_after_key_up_test.hpp b/tests/src/manipulator_basic/src/to_after_key_up_test.hpp index f00102e5b..91f27b084 100644 --- a/tests/src/manipulator_basic/src/to_after_key_up_test.hpp +++ b/tests/src/manipulator_basic/src/to_after_key_up_test.hpp @@ -19,8 +19,9 @@ void run_to_after_key_up_test(void) { })}, }); + auto parameters = std::make_shared(); basic::basic b(json, - krbn::core_configuration::details::complex_modifications_parameters()); + parameters); expect(b.get_to_after_key_up().size() == 1); { auto& d = b.get_to_after_key_up()[0].get_event_definition(); @@ -40,8 +41,9 @@ void run_to_after_key_up_test(void) { })}, }); + auto parameters = std::make_shared(); basic::basic b(json, - krbn::core_configuration::details::complex_modifications_parameters()); + parameters); expect(b.get_to_after_key_up().size() == 2); { auto& d = b.get_to_after_key_up()[0].get_event_definition(); diff --git a/tests/src/manipulator_basic/src/to_delayed_action_test.hpp b/tests/src/manipulator_basic/src/to_delayed_action_test.hpp index 42b951661..22fccdb0a 100644 --- a/tests/src/manipulator_basic/src/to_delayed_action_test.hpp +++ b/tests/src/manipulator_basic/src/to_delayed_action_test.hpp @@ -24,8 +24,9 @@ void run_to_delayed_action_test(void) { })}, }); + auto parameters = std::make_shared(); basic::basic b(json, - krbn::core_configuration::details::complex_modifications_parameters()); + parameters); expect(b.get_to_delayed_action().get() != nullptr); // to_if_invoked @@ -65,8 +66,9 @@ void run_to_delayed_action_test(void) { })}, }); + auto parameters = std::make_shared(); basic::basic b(json, - krbn::core_configuration::details::complex_modifications_parameters()); + parameters); expect(b.get_to_delayed_action().get() != nullptr); // to_if_invoked diff --git a/tests/src/manipulator_basic/src/to_if_alone_test.hpp b/tests/src/manipulator_basic/src/to_if_alone_test.hpp index cef1e337f..48970ca13 100644 --- a/tests/src/manipulator_basic/src/to_if_alone_test.hpp +++ b/tests/src/manipulator_basic/src/to_if_alone_test.hpp @@ -19,8 +19,9 @@ void run_to_if_alone_test(void) { })}, }); + auto parameters = std::make_shared(); basic::basic b(json, - krbn::core_configuration::details::complex_modifications_parameters()); + parameters); expect(b.get_to_if_alone().size() == 1); { auto& d = b.get_to_if_alone()[0].get_event_definition(); @@ -40,8 +41,9 @@ void run_to_if_alone_test(void) { })}, }); + auto parameters = std::make_shared(); basic::basic b(json, - krbn::core_configuration::details::complex_modifications_parameters()); + parameters); expect(b.get_to_if_alone().size() == 2); { auto& d = b.get_to_if_alone()[0].get_event_definition(); diff --git a/tests/src/manipulator_basic/src/to_if_held_down_test.hpp b/tests/src/manipulator_basic/src/to_if_held_down_test.hpp index bd2271ab7..4ff8fa493 100644 --- a/tests/src/manipulator_basic/src/to_if_held_down_test.hpp +++ b/tests/src/manipulator_basic/src/to_if_held_down_test.hpp @@ -19,8 +19,9 @@ void run_to_if_held_down_test(void) { })}, }); + auto parameters = std::make_shared(); basic::basic b(json, - krbn::core_configuration::details::complex_modifications_parameters()); + parameters); expect(b.get_to_if_held_down().get() != nullptr); expect(b.get_to_if_held_down()->get_to().size() == 1); { @@ -41,8 +42,9 @@ void run_to_if_held_down_test(void) { })}, }); + auto parameters = std::make_shared(); basic::basic b(json, - krbn::core_configuration::details::complex_modifications_parameters()); + parameters); expect(b.get_to_if_held_down().get() != nullptr); expect(b.get_to_if_held_down()->get_to().size() == 2); { diff --git a/tests/src/manipulator_mouse_basic/src/errors_test.hpp b/tests/src/manipulator_mouse_basic/src/errors_test.hpp index 0b502641e..29e275672 100644 --- a/tests/src/manipulator_mouse_basic/src/errors_test.hpp +++ b/tests/src/manipulator_mouse_basic/src/errors_test.hpp @@ -10,7 +10,7 @@ using namespace boost::ut::literals; void handle_json(const nlohmann::json& json) { auto c = json.at("class").get(); if (c == "mouse_basic") { - krbn::core_configuration::details::complex_modifications_parameters parameters; + auto parameters = std::make_shared(); krbn::manipulator::manipulators::mouse_basic::mouse_basic(json.at("input"), parameters); } else { expect(false); diff --git a/tests/src/manipulator_mouse_motion_to_scroll/src/counter_test.hpp b/tests/src/manipulator_mouse_motion_to_scroll/src/counter_test.hpp index b5f2543f8..b2e969df1 100644 --- a/tests/src/manipulator_mouse_motion_to_scroll/src/counter_test.hpp +++ b/tests/src/manipulator_mouse_motion_to_scroll/src/counter_test.hpp @@ -9,7 +9,7 @@ class counter_test final : pqrs::dispatcher::extra::dispatcher_client { public: counter_test(std::shared_ptr time_source, std::weak_ptr weak_dispatcher, - const krbn::core_configuration::details::complex_modifications_parameters& parameters, + gsl::not_null> parameters, const mouse_motion_to_scroll::options& options) : dispatcher_client(weak_dispatcher), time_source_(time_source), counter_(weak_dispatcher, @@ -97,7 +97,8 @@ void run_counter_test(void) { auto dispatcher = std::make_shared(time_source); { - krbn::core_configuration::details::complex_modifications_parameters parameters(input_json.at("parameters")); + auto parameters = std::make_shared(input_json.at("parameters"), + krbn::core_configuration::error_handling::strict); mouse_motion_to_scroll::options options; options.update(input_json.at("options")); counter_test counter_test(time_source, diff --git a/tests/src/manipulator_mouse_motion_to_scroll/src/errors_test.hpp b/tests/src/manipulator_mouse_motion_to_scroll/src/errors_test.hpp index c21115652..d1d7d2263 100644 --- a/tests/src/manipulator_mouse_motion_to_scroll/src/errors_test.hpp +++ b/tests/src/manipulator_mouse_motion_to_scroll/src/errors_test.hpp @@ -11,7 +11,7 @@ using namespace boost::ut::literals; void handle_json(const nlohmann::json& json) { auto c = json.at("class").get(); if (c == "mouse_motion_to_scroll") { - krbn::core_configuration::details::complex_modifications_parameters parameters; + auto parameters = std::make_shared(); krbn::manipulator::manipulators::mouse_motion_to_scroll::mouse_motion_to_scroll(json.at("input"), parameters); } else { expect(false); diff --git a/tests/src/share/manipulator_helper.hpp b/tests/src/share/manipulator_helper.hpp index 29be91ac6..f95add9a0 100644 --- a/tests/src/share/manipulator_helper.hpp +++ b/tests/src/share/manipulator_helper.hpp @@ -60,7 +60,7 @@ class manipulator_helper final : pqrs::dispatcher::extra::dispatcher_client { std::ifstream ifs(rule.get()); expect(static_cast(ifs)); for (const auto& j : json_utility::parse_jsonc(ifs)) { - core_configuration::details::complex_modifications_parameters parameters; + auto parameters = std::make_shared(); auto m = manipulator::manipulator_factory::make_manipulator(j, parameters); From 2c6bae02f2ae596d1e80f3c549daa5d80d9a7326 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Sun, 30 Jun 2024 09:21:33 +0900 Subject: [PATCH 3/4] Clean up #include --- src/share/complex_modifications_utility.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/share/complex_modifications_utility.hpp b/src/share/complex_modifications_utility.hpp index 367065a10..385c08398 100644 --- a/src/share/complex_modifications_utility.hpp +++ b/src/share/complex_modifications_utility.hpp @@ -3,7 +3,6 @@ #include "core_configuration/core_configuration.hpp" #include "json_utility.hpp" #include "manipulator/manipulator_factory.hpp" -#include "memory_utility.hpp" namespace krbn { namespace complex_modifications_utility { From 28e58f448b8aa8e19457a75eb780d524787440c4 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Sun, 30 Jun 2024 09:56:34 +0900 Subject: [PATCH 4/4] Delete copy constructor of complex_modifications, manipulator --- .../grabber/include/grabber/device_grabber.hpp | 6 +++--- src/share/complex_modifications_utility.hpp | 8 ++++---- .../details/profile/complex_modifications.hpp | 2 ++ .../profile/complex_modifications_rule.hpp | 17 ++++++++++------- .../src/core_configuration_test.hpp | 6 +++--- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/core/grabber/include/grabber/device_grabber.hpp b/src/core/grabber/include/grabber/device_grabber.hpp index 1962a2eb8..eb038f29b 100644 --- a/src/core/grabber/include/grabber/device_grabber.hpp +++ b/src/core/grabber/include/grabber/device_grabber.hpp @@ -955,9 +955,9 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client { for (const auto& rule : core_configuration_->get_selected_profile().get_complex_modifications()->get_rules()) { for (const auto& manipulator : rule->get_manipulators()) { try { - auto m = manipulator::manipulator_factory::make_manipulator(manipulator.get_json(), - manipulator.get_parameters()); - for (const auto& c : manipulator.get_conditions()) { + auto m = manipulator::manipulator_factory::make_manipulator(manipulator->get_json(), + manipulator->get_parameters()); + for (const auto& c : manipulator->get_conditions()) { m->push_back_condition(manipulator::manipulator_factory::make_condition(c.get_json())); } complex_modifications_manipulator_manager_->push_back_manipulator(m); diff --git a/src/share/complex_modifications_utility.hpp b/src/share/complex_modifications_utility.hpp index 385c08398..4e80f6e6a 100644 --- a/src/share/complex_modifications_utility.hpp +++ b/src/share/complex_modifications_utility.hpp @@ -9,11 +9,11 @@ namespace complex_modifications_utility { inline std::vector lint_rule(const core_configuration::details::complex_modifications_rule& rule) { std::vector error_messages; - for (const auto& manipulator : rule.get_manipulators()) { + for (const auto& m : rule.get_manipulators()) { try { - manipulator::manipulator_factory::make_manipulator(manipulator.get_json(), - manipulator.get_parameters()); - for (const auto& c : manipulator.get_conditions()) { + manipulator::manipulator_factory::make_manipulator(m->get_json(), + m->get_parameters()); + for (const auto& c : m->get_conditions()) { manipulator::manipulator_factory::make_condition(c.get_json()); } diff --git a/src/share/core_configuration/details/profile/complex_modifications.hpp b/src/share/core_configuration/details/profile/complex_modifications.hpp index 1e67827e1..42f6a47c1 100644 --- a/src/share/core_configuration/details/profile/complex_modifications.hpp +++ b/src/share/core_configuration/details/profile/complex_modifications.hpp @@ -9,6 +9,8 @@ namespace core_configuration { namespace details { class complex_modifications final { public: + complex_modifications(const complex_modifications&) = delete; + complex_modifications(void) : complex_modifications(nlohmann::json::object(), krbn::core_configuration::error_handling::loose) { 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 7ae083a17..a9138be34 100644 --- a/src/share/core_configuration/details/profile/complex_modifications_rule.hpp +++ b/src/share/core_configuration/details/profile/complex_modifications_rule.hpp @@ -23,6 +23,8 @@ class complex_modifications_rule final { nlohmann::json json_; }; + manipulator(const manipulator&) = delete; + manipulator(const nlohmann::json& json, gsl::not_null> parameters, error_handling error_handling) @@ -90,9 +92,10 @@ class complex_modifications_rule final { for (const auto& j : value) { try { - manipulators_.emplace_back(j, - parameters, - error_handling); + auto m = std::make_shared(j, + parameters, + error_handling); + manipulators_.push_back(m); } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key, e.what())); } @@ -119,8 +122,8 @@ class complex_modifications_rule final { // Use manipulators_'s description if needed. if (description_.empty()) { for (const auto& m : manipulators_) { - if (!m.get_description().empty()) { - description_ = m.get_description(); + if (!m->get_description().empty()) { + description_ = m->get_description(); break; } } @@ -131,7 +134,7 @@ class complex_modifications_rule final { return json_; } - const std::vector& get_manipulators(void) const { + const std::vector>>& get_manipulators(void) const { return manipulators_; } @@ -141,7 +144,7 @@ class complex_modifications_rule final { private: nlohmann::json json_; - std::vector manipulators_; + std::vector>> manipulators_; std::string description_; }; diff --git a/tests/src/core_configuration/src/core_configuration_test.hpp b/tests/src/core_configuration/src/core_configuration_test.hpp index f729e0bcc..c7b572dc8 100644 --- a/tests/src/core_configuration/src/core_configuration_test.hpp +++ b/tests/src/core_configuration/src/core_configuration_test.hpp @@ -28,7 +28,7 @@ void run_core_configuration_test(void) { expect(expected == configuration.get_selected_profile().get_simple_modifications()->get_pairs()) << UT_SHOW_LINE; } { - auto manipulator = configuration.get_selected_profile().get_complex_modifications()->get_rules()[0]->get_manipulators()[0].get_json(); + auto manipulator = configuration.get_selected_profile().get_complex_modifications()->get_rules()[0]->get_manipulators()[0]->get_json(); expect("basic" == manipulator["type"]); expect("open_bracket" == manipulator["from"]["key_code"]); } @@ -77,8 +77,8 @@ void run_core_configuration_test(void) { auto complex_modifications = configuration.get_selected_profile().get_complex_modifications(); auto& rules = complex_modifications->get_rules(); expect(complex_modifications->get_parameters()->get_basic_to_if_alone_timeout_milliseconds() == 800); - expect(rules[0]->get_manipulators()[0].get_parameters()->get_basic_to_if_alone_timeout_milliseconds() == 800); - expect(rules[0]->get_manipulators()[2].get_parameters()->get_basic_to_if_alone_timeout_milliseconds() == 400); + expect(rules[0]->get_manipulators()[0]->get_parameters()->get_basic_to_if_alone_timeout_milliseconds() == 800); + expect(rules[0]->get_manipulators()[2]->get_parameters()->get_basic_to_if_alone_timeout_milliseconds() == 400); expect(rules[0]->get_description() == "Emacs bindings, etc."); expect(rules[1]->get_description() == "description test"); expect(rules[2]->get_description() == "");