Skip to content

Commit

Permalink
Update for core_configuration changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jun 11, 2024
1 parent 22f05ff commit c517329
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/bin/cli/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
});
}
Expand Down
63 changes: 32 additions & 31 deletions src/core/grabber/include/grabber/device_grabber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<console_user_server_client> weak_console_user_server_client,
std::weak_ptr<grabber_state_json_writer> weak_grabber_state_json_writer) : dispatcher_client(),
system_sleeping_(false),
profile_(nlohmann::json::object()),
logger_unique_filter_(logger::get_logger()) {
std::weak_ptr<grabber_state_json_writer> weak_grabber_state_json_writer)
: dispatcher_client(),
core_configuration_(std::make_shared<core_configuration::core_configuration>()),
system_sleeping_(false),
logger_unique_filter_(logger::get_logger()) {
notification_message_manager_ = std::make_shared<notification_message_manager>(
constants::get_notification_message_file_path());

Expand Down Expand Up @@ -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();
}
});

Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -991,7 +993,6 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client {
std::unique_ptr<pqrs::osx::iokit_power_management::monitor> 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_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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");
}

Expand All @@ -95,7 +95,7 @@ class simple_modifications_manipulator_manager final {
auto parameters = krbn::core_configuration::details::complex_modifications_parameters();
auto m = std::make_shared<manipulator::manipulators::mouse_basic::mouse_basic>(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) {
Expand Down
3 changes: 3 additions & 0 deletions src/share/core_configuration/core_configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion tests/src/share/manipulator_conditions_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<krbn::core_configuration::core_configuration>("", geteuid());
core_configuration_ = std::make_shared<krbn::core_configuration::core_configuration>();
manipulator_environment_.set_core_configuration(core_configuration_);
}

Expand Down

0 comments on commit c517329

Please sign in to comment.