From c5173295d7ae73399119d0551e52273c9da2b170 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Tue, 11 Jun 2024 14:52:21 +0900 Subject: [PATCH] Update for core_configuration changes --- src/bin/cli/src/main.cpp | 4 +- .../include/grabber/device_grabber.hpp | 63 ++++++++++--------- .../fn_function_keys_manipulator_manager.hpp | 4 +- ...mple_modifications_manipulator_manager.hpp | 26 ++++---- .../core_configuration/core_configuration.hpp | 3 + .../share/manipulator_conditions_helper.hpp | 2 +- 6 files changed, 53 insertions(+), 49 deletions(-) diff --git a/src/bin/cli/src/main.cpp b/src/bin/cli/src/main.cpp index cb5d506fd..961aedc52 100644 --- a/src/bin/cli/src/main.cpp +++ b/src/bin/cli/src/main.cpp @@ -40,7 +40,7 @@ void select_profile(const std::string& name) { apply_core_configuration_function([name](auto core_configuration) { auto& profiles = core_configuration->get_profiles(); for (size_t i = 0; i < profiles.size(); ++i) { - if (profiles[i].get_name() == name) { + if (profiles[i]->get_name() == name) { core_configuration->select_profile(i); core_configuration->sync_save_to_file(); return; @@ -59,7 +59,7 @@ void show_current_profile_name(void) { void list_profile_names(void) { apply_core_configuration_function([](auto core_configuration) { for (const auto& profile : core_configuration->get_profiles()) { - std::cout << profile.get_name() << std::endl; + std::cout << profile->get_name() << std::endl; } }); } diff --git a/src/core/grabber/include/grabber/device_grabber.hpp b/src/core/grabber/include/grabber/device_grabber.hpp index 96d9cfae8..dc4e34bd0 100644 --- a/src/core/grabber/include/grabber/device_grabber.hpp +++ b/src/core/grabber/include/grabber/device_grabber.hpp @@ -41,10 +41,11 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client { device_grabber(const device_grabber&) = delete; device_grabber(std::weak_ptr weak_console_user_server_client, - std::weak_ptr weak_grabber_state_json_writer) : dispatcher_client(), - system_sleeping_(false), - profile_(nlohmann::json::object()), - logger_unique_filter_(logger::get_logger()) { + std::weak_ptr weak_grabber_state_json_writer) + : dispatcher_client(), + core_configuration_(std::make_shared()), + system_sleeping_(false), + logger_unique_filter_(logger::get_logger()) { notification_message_manager_ = std::make_shared( constants::get_notification_message_file_path()); @@ -457,7 +458,30 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client { manipulator_managers_connector_.set_manipulator_environment_core_configuration(core_configuration); logger_unique_filter_.reset(); - set_profile(core_configuration->get_selected_profile()); + + // + // Reflect profile changes + // + + auto& profile = core_configuration_->get_selected_profile(); + + if (hid_manager_) { + hid_manager_->async_set_device_matched_delay( + profile.get_parameters().get_delay_milliseconds_before_open_device()); + hid_manager_->async_start(); + } + + simple_modifications_manipulator_manager_->update(profile); + fn_function_keys_manipulator_manager_->update(profile, + system_preferences_properties_); + update_complex_modifications_manipulators(); + + update_virtual_hid_keyboard(); + update_virtual_hid_pointing(); + + update_devices_disabled(); + async_grab_devices(); + async_post_system_preferences_properties_changed_event(); } }); @@ -495,7 +519,7 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client { enqueue_to_dispatcher([this, value] { system_preferences_properties_ = value; - fn_function_keys_manipulator_manager_->update(profile_, + fn_function_keys_manipulator_manager_->update(core_configuration_->get_selected_profile(), system_preferences_properties_); async_post_system_preferences_properties_changed_event(); }); @@ -863,7 +887,7 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client { void update_virtual_hid_keyboard(void) { virtual_hid_device_service_client_->async_virtual_hid_keyboard_initialize( - profile_.get_virtual_hid_keyboard().get_country_code()); + core_configuration_->get_selected_profile().get_virtual_hid_keyboard().get_country_code()); } void update_virtual_hid_pointing(void) { @@ -922,32 +946,10 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client { json_writer::async_save_to_file(nlohmann::json(device_details), file_path, 0755, 0644); } - void set_profile(const core_configuration::details::profile& profile) { - profile_ = profile; - - if (hid_manager_) { - hid_manager_->async_set_device_matched_delay( - profile_.get_parameters().get_delay_milliseconds_before_open_device()); - hid_manager_->async_start(); - } - - simple_modifications_manipulator_manager_->update(profile_); - update_complex_modifications_manipulators(); - fn_function_keys_manipulator_manager_->update(profile_, - system_preferences_properties_); - - update_virtual_hid_keyboard(); - update_virtual_hid_pointing(); - - update_devices_disabled(); - async_grab_devices(); - async_post_system_preferences_properties_changed_event(); - } - void update_complex_modifications_manipulators(void) { complex_modifications_manipulator_manager_->invalidate_manipulators(); - for (const auto& rule : profile_.get_complex_modifications().get_rules()) { + 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(), @@ -991,7 +993,6 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client { std::unique_ptr power_management_monitor_; bool system_sleeping_; - core_configuration::details::profile profile_; pqrs::osx::system_preferences::properties system_preferences_properties_; manipulator::manipulator_managers_connector manipulator_managers_connector_; diff --git a/src/core/grabber/include/grabber/device_grabber_details/fn_function_keys_manipulator_manager.hpp b/src/core/grabber/include/grabber/device_grabber_details/fn_function_keys_manipulator_manager.hpp index 748adb3be..e88ce98fc 100644 --- a/src/core/grabber/include/grabber/device_grabber_details/fn_function_keys_manipulator_manager.hpp +++ b/src/core/grabber/include/grabber/device_grabber_details/fn_function_keys_manipulator_manager.hpp @@ -80,7 +80,7 @@ class fn_function_keys_manipulator_manager final { // from_modifiers+f1 -> display_brightness_decrement ... for (const auto& device : profile.get_devices()) { - for (const auto& pair : device.get_fn_function_keys().get_pairs()) { + for (const auto& pair : device->get_fn_function_keys().get_pairs()) { try { if (auto m = make_manipulator(pair, from_mandatory_modifiers, @@ -89,7 +89,7 @@ class fn_function_keys_manipulator_manager final { m->push_back_condition(manipulator::manipulator_factory::make_event_changed_if_condition(false)); m->push_back_condition(manipulator::manipulator_factory::make_device_unless_touch_bar_condition()); - auto c = manipulator::manipulator_factory::make_device_if_condition(device); + auto c = manipulator::manipulator_factory::make_device_if_condition(*device); m->push_back_condition(c); manipulator_manager_->push_back_manipulator(m); 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 7224de459..f0446f5ee 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 @@ -27,10 +27,10 @@ class simple_modifications_manipulator_manager final { // Add profile.device.simple_modifications // - for (const auto& pair : device.get_simple_modifications().get_pairs()) { + for (const auto& pair : device->get_simple_modifications().get_pairs()) { try { if (auto m = make_manipulator(pair)) { - auto c = manipulator::manipulator_factory::make_device_if_condition(device); + auto c = manipulator::manipulator_factory::make_device_if_condition(*device); m->push_back_condition(c); manipulator_manager_->push_back_manipulator(m); } @@ -49,38 +49,38 @@ class simple_modifications_manipulator_manager final { { auto flip = nlohmann::json::array(); - if (device.get_mouse_flip_x()) { + if (device->get_mouse_flip_x()) { flip.push_back("x"); } - if (device.get_mouse_flip_y()) { + if (device->get_mouse_flip_y()) { flip.push_back("y"); } - if (device.get_mouse_flip_vertical_wheel()) { + if (device->get_mouse_flip_vertical_wheel()) { flip.push_back("vertical_wheel"); } - if (device.get_mouse_flip_horizontal_wheel()) { + if (device->get_mouse_flip_horizontal_wheel()) { flip.push_back("horizontal_wheel"); } auto swap = nlohmann::json::array(); - if (device.get_mouse_swap_xy()) { + if (device->get_mouse_swap_xy()) { swap.push_back("xy"); } - if (device.get_mouse_swap_wheels()) { + if (device->get_mouse_swap_wheels()) { swap.push_back("wheels"); } auto discard = nlohmann::json::array(); - if (device.get_mouse_discard_x()) { + if (device->get_mouse_discard_x()) { discard.push_back("x"); } - if (device.get_mouse_discard_y()) { + if (device->get_mouse_discard_y()) { discard.push_back("y"); } - if (device.get_mouse_discard_vertical_wheel()) { + if (device->get_mouse_discard_vertical_wheel()) { discard.push_back("vertical_wheel"); } - if (device.get_mouse_discard_horizontal_wheel()) { + if (device->get_mouse_discard_horizontal_wheel()) { discard.push_back("horizontal_wheel"); } @@ -95,7 +95,7 @@ class simple_modifications_manipulator_manager final { auto parameters = krbn::core_configuration::details::complex_modifications_parameters(); auto m = std::make_shared(json, parameters); - auto c = manipulator::manipulator_factory::make_device_if_condition(device); + auto c = manipulator::manipulator_factory::make_device_if_condition(*device); m->push_back_condition(c); manipulator_manager_->push_back_manipulator(m); } catch (const std::exception& e) { diff --git a/src/share/core_configuration/core_configuration.hpp b/src/share/core_configuration/core_configuration.hpp index fbd900f30..0df813ebb 100644 --- a/src/share/core_configuration/core_configuration.hpp +++ b/src/share/core_configuration/core_configuration.hpp @@ -32,6 +32,9 @@ class core_configuration final { public: core_configuration(const core_configuration&) = delete; + core_configuration(void) : core_configuration("", 0) { + } + core_configuration(const std::string& file_path, uid_t expected_file_owner) : json_(nlohmann::json::object()), diff --git a/tests/src/share/manipulator_conditions_helper.hpp b/tests/src/share/manipulator_conditions_helper.hpp index 7e179548d..9974b7086 100644 --- a/tests/src/share/manipulator_conditions_helper.hpp +++ b/tests/src/share/manipulator_conditions_helper.hpp @@ -12,7 +12,7 @@ namespace unit_testing { class manipulator_conditions_helper final { public: manipulator_conditions_helper(void) : last_device_id_(0) { - core_configuration_ = std::make_shared("", geteuid()); + core_configuration_ = std::make_shared(); manipulator_environment_.set_core_configuration(core_configuration_); }