Skip to content

Commit

Permalink
Remove get_device_ignore, set_device_ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jun 22, 2024
1 parent ae2a3c5 commit 000d41b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ class entry final : public pqrs::dispatcher::extra::dispatcher_client {
}

if (auto c = weak_core_configuration_.lock()) {
return !(c->get_selected_profile().get_device_ignore(device_properties_.get_device_identifiers()));
auto d = c->get_selected_profile().get_device(device_properties_.get_device_identifiers());
return !(d->get_ignore());
}

return false;
Expand Down
9 changes: 4 additions & 5 deletions src/lib/libkrbn/src/libkrbn_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,16 +541,15 @@ void libkrbn_core_configuration_set_selected_profile_virtual_hid_keyboard_indica

bool libkrbn_core_configuration_get_selected_profile_device_ignore(const libkrbn_device_identifiers* device_identifiers) {
auto c = get_current_core_configuration();
return c->get_selected_profile().get_device_ignore(
libkrbn_cpp::make_device_identifiers(device_identifiers));
auto d = c->get_selected_profile().get_device(libkrbn_cpp::make_device_identifiers(device_identifiers));
return d->get_ignore();
}

void libkrbn_core_configuration_set_selected_profile_device_ignore(const libkrbn_device_identifiers* device_identifiers,
bool value) {
auto c = get_current_core_configuration();
c->get_selected_profile().set_device_ignore(
libkrbn_cpp::make_device_identifiers(device_identifiers),
value);
auto d = c->get_selected_profile().get_device(libkrbn_cpp::make_device_identifiers(device_identifiers));
d->set_ignore(value);
}

bool libkrbn_core_configuration_get_selected_profile_device_manipulate_caps_lock_led(const libkrbn_device_identifiers* device_identifiers) {
Expand Down
79 changes: 33 additions & 46 deletions src/share/core_configuration/details/profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class profile final {
error_handling error_handling)
: json_(json),
error_handling_(error_handling),
selected_(false),
empty_device_(std::make_shared<details::device>(nlohmann::json::object(),
error_handling_)) {
selected_(false) {
helper_values_.push_back_array<details::device>("devices",
devices_);

Expand Down Expand Up @@ -264,44 +262,33 @@ class profile final {
return devices_;
}

bool get_device_ignore(const device_identifiers& identifiers) const {
for (const auto& d : devices_) {
if (d->get_identifiers() == identifiers) {
return d->get_ignore();
}
}
gsl::not_null<std::shared_ptr<details::device>> get_device(const device_identifiers& identifiers) const {
//
// Find device
//

details::device d(nlohmann::json({
{"identifiers", identifiers},
}),
error_handling_);
return d.get_ignore();
}
auto it = std::find_if(std::begin(devices_),
std::end(devices_),
[&](auto&& d) {
return d->get_identifiers() == identifiers;
});
if (it != std::end(devices_)) {
return *it;
}

void set_device_ignore(const device_identifiers& identifiers,
bool ignore) {
add_device(identifiers);
//
// Add device
//

for (auto&& d : devices_) {
if (d->get_identifiers() == identifiers) {
d->set_ignore(ignore);
return;
}
}
devices_.push_back(std::make_shared<details::device>(nlohmann::json({
{"identifiers", identifiers},
}),
error_handling_));
return devices_.back();
}

bool get_device_manipulate_caps_lock_led(const device_identifiers& identifiers) const {
for (const auto& d : devices_) {
if (d->get_identifiers() == identifiers) {
return d->get_manipulate_caps_lock_led();
}
}

details::device d(nlohmann::json({
{"identifiers", identifiers},
}),
error_handling_);
return d.get_manipulate_caps_lock_led();
return get_device_or_new(identifiers)->get_manipulate_caps_lock_led();
}

void set_device_manipulate_caps_lock_led(const device_identifiers& identifiers,
Expand Down Expand Up @@ -599,7 +586,7 @@ class profile final {
//

double get_device_game_pad_xy_stick_continued_movement_absolute_magnitude_threshold(const device_identifiers& identifiers) const {
auto d = get_device_or_emtpy(identifiers);
auto d = get_device_or_new(identifiers);
return d->get_game_pad_xy_stick_continued_movement_absolute_magnitude_threshold();
}

Expand All @@ -614,7 +601,7 @@ class profile final {
//

int get_device_game_pad_xy_stick_continued_movement_interval_milliseconds(const device_identifiers& identifiers) const {
auto d = get_device_or_emtpy(identifiers);
auto d = get_device_or_new(identifiers);
return d->get_game_pad_xy_stick_continued_movement_interval_milliseconds();
}

Expand All @@ -629,7 +616,7 @@ class profile final {
//

int get_device_game_pad_xy_stick_flicking_input_window_milliseconds(const device_identifiers& identifiers) const {
auto d = get_device_or_emtpy(identifiers);
auto d = get_device_or_new(identifiers);
return d->get_game_pad_xy_stick_flicking_input_window_milliseconds();
}

Expand All @@ -644,7 +631,7 @@ class profile final {
//

double get_device_game_pad_wheels_stick_continued_movement_absolute_magnitude_threshold(const device_identifiers& identifiers) const {
auto d = get_device_or_emtpy(identifiers);
auto d = get_device_or_new(identifiers);
return d->get_game_pad_wheels_stick_continued_movement_absolute_magnitude_threshold();
}

Expand All @@ -659,7 +646,7 @@ class profile final {
//

int get_device_game_pad_wheels_stick_continued_movement_interval_milliseconds(const device_identifiers& identifiers) const {
auto d = get_device_or_emtpy(identifiers);
auto d = get_device_or_new(identifiers);
return d->get_game_pad_wheels_stick_continued_movement_interval_milliseconds();
}

Expand All @@ -674,7 +661,7 @@ class profile final {
//

int get_device_game_pad_wheels_stick_flicking_input_window_milliseconds(const device_identifiers& identifiers) const {
auto d = get_device_or_emtpy(identifiers);
auto d = get_device_or_new(identifiers);
return d->get_game_pad_wheels_stick_flicking_input_window_milliseconds();
}

Expand Down Expand Up @@ -849,14 +836,15 @@ class profile final {
return nullptr;
}

gsl::not_null<std::shared_ptr<details::device>> get_device_or_emtpy(const device_identifiers& identifiers) const {
gsl::not_null<std::shared_ptr<details::device>> get_device_or_new(const device_identifiers& identifiers) const {
if (auto d = find_device(identifiers)) {
return d;
}
return empty_device_;

return add_device(identifiers);
}

gsl::not_null<std::shared_ptr<details::device>> add_device(const device_identifiers& identifiers) {
gsl::not_null<std::shared_ptr<details::device>> add_device(const device_identifiers& identifiers) const {
if (auto d = find_device(identifiers)) {
return d;
}
Expand All @@ -877,8 +865,7 @@ class profile final {
details::simple_modifications fn_function_keys_;
details::complex_modifications complex_modifications_;
details::virtual_hid_keyboard virtual_hid_keyboard_;
std::vector<gsl::not_null<std::shared_ptr<details::device>>> devices_;
gsl::not_null<std::shared_ptr<details::device>> empty_device_;
mutable std::vector<gsl::not_null<std::shared_ptr<details::device>>> devices_;
configuration_json_helper::helper_values helper_values_;
};

Expand Down
34 changes: 18 additions & 16 deletions tests/src/core_configuration/src/core_configuration_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,20 +293,22 @@ void run_core_configuration_test(void) {
expect(profile.get_fn_function_keys().get_pairs().size() == 12);
expect(profile.get_devices().size() == 0);

expect(profile.get_device_ignore(krbn::device_identifiers(pqrs::hid::vendor_id::value_t(4176),
pqrs::hid::product_id::value_t(1031),
true, // is_keyboard
false, // is_pointing_device
false, // is_game_pad
"" // device_address
)) == true);
expect(profile.get_device_ignore(krbn::device_identifiers(pqrs::hid::vendor_id::value_t(0x05ac),
pqrs::hid::product_id::value_t(0x262),
true, // is_keyboard
false, // is_pointing_device
false, // is_game_pad
"" // device_address
)) == false);
expect(profile.get_device(krbn::device_identifiers(pqrs::hid::vendor_id::value_t(4176),
pqrs::hid::product_id::value_t(1031),
true, // is_keyboard
false, // is_pointing_device
false, // is_game_pad
"" // device_address
))
->get_ignore() == true);
expect(profile.get_device(krbn::device_identifiers(pqrs::hid::vendor_id::value_t(0x05ac),
pqrs::hid::product_id::value_t(0x262),
true, // is_keyboard
false, // is_pointing_device
false, // is_game_pad
"" // device_address
))
->get_ignore() == false);
}

// load values from json
Expand Down Expand Up @@ -524,7 +526,7 @@ void run_core_configuration_test(void) {
},
})
.get<krbn::device_identifiers>();
profile.set_device_ignore(identifiers, false);
profile.get_device(identifiers)->set_ignore(false);
expect(profile.get_devices().size() == 3);
// devices[0] is changed.
expect((profile.get_devices())[0]->get_identifiers().get_vendor_id() == pqrs::hid::vendor_id::value_t(1234));
Expand Down Expand Up @@ -581,7 +583,7 @@ void run_core_configuration_test(void) {
},
})
.get<krbn::device_identifiers>();
profile.set_device_ignore(identifiers, true);
profile.get_device(identifiers)->set_ignore(true);
expect(profile.get_devices().size() == 4);
expect((profile.get_devices())[3]->get_identifiers().get_vendor_id() == pqrs::hid::vendor_id::value_t(1111));
expect((profile.get_devices())[3]->get_identifiers().get_product_id() == pqrs::hid::product_id::value_t(2222));
Expand Down

0 comments on commit 000d41b

Please sign in to comment.