Skip to content

Commit

Permalink
Add profile.erase_not_connected_devices
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jul 27, 2024
1 parent 72719a9 commit e09ac11
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 130 deletions.
2 changes: 1 addition & 1 deletion src/core/grabber/include/grabber/device_grabber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client {
void output_devices_json(void) const {
connected_devices::connected_devices connected_devices;
for (const auto& e : entries_) {
connected_devices::details::device d(e.second->get_device_properties());
auto d = std::make_shared<connected_devices::details::device>(e.second->get_device_properties());
connected_devices.push_back_device(d);
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/libkrbn/include/libkrbn/libkrbn.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ bool libkrbn_core_configuration_get_selected_profile_device_game_pad_swap_sticks
void libkrbn_core_configuration_set_selected_profile_device_game_pad_swap_sticks(const libkrbn_device_identifiers* device_identifiers,
bool value);

void libkrbn_core_configuration_erase_selected_profile_not_connected_devices(void);

// game_pad_xy_stick_continued_movement_absolute_magnitude_threshold

double libkrbn_core_configuration_get_selected_profile_device_game_pad_xy_stick_continued_movement_absolute_magnitude_threshold(const libkrbn_device_identifiers* device_identifiers);
Expand Down
9 changes: 9 additions & 0 deletions src/lib/libkrbn/src/libkrbn_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,15 @@ void libkrbn_core_configuration_set_selected_profile_device_game_pad_swap_sticks
d->set_game_pad_swap_sticks(value);
}

void libkrbn_core_configuration_erase_selected_profile_not_connected_devices(void) {
if (auto manager = libkrbn_cpp::get_components_manager()) {
if (auto connected_devices = manager->get_current_connected_devices()) {
auto c = get_current_core_configuration();
c->get_selected_profile().erase_not_connected_devices(*connected_devices);
}
}
}

// game_pad_xy_stick_continued_movement_absolute_magnitude_threshold

double libkrbn_core_configuration_get_selected_profile_device_game_pad_xy_stick_continued_movement_absolute_magnitude_threshold(const libkrbn_device_identifiers* device_identifiers) {
Expand Down
30 changes: 15 additions & 15 deletions src/lib/libkrbn/src/libkrbn_connected_devices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool libkrbn_connected_devices_get_descriptions_manufacturer(size_t index,
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
strlcpy(buffer, type_safe::get(devices[index].get_descriptions().get_manufacturer()).c_str(), length);
strlcpy(buffer, type_safe::get(devices[index]->get_descriptions().get_manufacturer()).c_str(), length);
return true;
}
}
Expand All @@ -47,7 +47,7 @@ bool libkrbn_connected_devices_get_descriptions_product(size_t index,
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
strlcpy(buffer, type_safe::get(devices[index].get_descriptions().get_product()).c_str(), length);
strlcpy(buffer, type_safe::get(devices[index]->get_descriptions().get_product()).c_str(), length);
return true;
}
}
Expand All @@ -65,7 +65,7 @@ bool libkrbn_connected_devices_get_descriptions_transport(size_t index,
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
strlcpy(buffer, devices[index].get_descriptions().get_transport().c_str(), length);
strlcpy(buffer, devices[index]->get_descriptions().get_transport().c_str(), length);
return true;
}
}
Expand All @@ -78,7 +78,7 @@ bool libkrbn_connected_devices_get_device_identifiers(size_t index, libkrbn_devi
if (device_identifiers) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
auto identifiers = devices[index].get_identifiers();
auto identifiers = devices[index]->get_identifiers();
device_identifiers->vendor_id = type_safe::get(identifiers.get_vendor_id());
device_identifiers->product_id = type_safe::get(identifiers.get_product_id());
device_identifiers->is_keyboard = identifiers.get_is_keyboard();
Expand All @@ -95,7 +95,7 @@ uint64_t libkrbn_connected_devices_get_vendor_id(size_t index) {
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
return type_safe::get(devices[index].get_identifiers().get_vendor_id());
return type_safe::get(devices[index]->get_identifiers().get_vendor_id());
}
}
return 0;
Expand All @@ -105,7 +105,7 @@ uint64_t libkrbn_connected_devices_get_product_id(size_t index) {
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
return type_safe::get(devices[index].get_identifiers().get_product_id());
return type_safe::get(devices[index]->get_identifiers().get_product_id());
}
}
return 0;
Expand All @@ -121,7 +121,7 @@ bool libkrbn_connected_devices_get_device_address(size_t index,
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
strlcpy(buffer, devices[index].get_identifiers().get_device_address().c_str(), length);
strlcpy(buffer, devices[index]->get_identifiers().get_device_address().c_str(), length);
return true;
}
}
Expand All @@ -133,7 +133,7 @@ bool libkrbn_connected_devices_get_is_keyboard(size_t index) {
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
return devices[index].get_identifiers().get_is_keyboard();
return devices[index]->get_identifiers().get_is_keyboard();
}
}
return false;
Expand All @@ -143,7 +143,7 @@ bool libkrbn_connected_devices_get_is_pointing_device(size_t index) {
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
return devices[index].get_identifiers().get_is_pointing_device();
return devices[index]->get_identifiers().get_is_pointing_device();
}
}
return false;
Expand All @@ -153,7 +153,7 @@ bool libkrbn_connected_devices_get_is_game_pad(size_t index) {
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
return devices[index].get_identifiers().get_is_game_pad();
return devices[index]->get_identifiers().get_is_game_pad();
}
}
return false;
Expand All @@ -163,7 +163,7 @@ bool libkrbn_connected_devices_get_is_built_in_keyboard(size_t index) {
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
return devices[index].get_is_built_in_keyboard();
return devices[index]->get_is_built_in_keyboard();
}
}
return 0;
Expand All @@ -173,7 +173,7 @@ bool libkrbn_connected_devices_get_is_built_in_trackpad(size_t index) {
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
return devices[index].get_is_built_in_trackpad();
return devices[index]->get_is_built_in_trackpad();
}
}
return 0;
Expand All @@ -183,7 +183,7 @@ bool libkrbn_connected_devices_get_is_built_in_touch_bar(size_t index) {
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
return devices[index].get_is_built_in_touch_bar();
return devices[index]->get_is_built_in_touch_bar();
}
}
return 0;
Expand All @@ -193,7 +193,7 @@ bool libkrbn_connected_devices_is_apple(size_t index) {
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
return devices[index].is_apple();
return devices[index]->is_apple();
}
}
return 0;
Expand All @@ -203,7 +203,7 @@ bool libkrbn_connected_devices_is_karabiner_virtual_hid_device(size_t index) {
if (auto c = get_current_connected_devices()) {
const auto& devices = c->get_devices();
if (index < devices.size()) {
return devices[index].is_karabiner_virtual_hid_device();
return devices[index]->is_karabiner_virtual_hid_device();
}
}
return 0;
Expand Down
94 changes: 58 additions & 36 deletions src/share/connected_devices/connected_devices.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "types.hpp"
#include <algorithm>
#include <fstream>
#include <gsl/gsl>
#include <nlohmann/json.hpp>
#include <pqrs/filesystem.hpp>

Expand Down Expand Up @@ -60,7 +61,7 @@ class connected_devices final {

if (json.is_array()) {
for (const auto& j : json) {
devices_.emplace_back(details::device::make_from_json(j));
devices_.push_back(std::make_shared<details::device>(j));
}
}

Expand All @@ -73,52 +74,73 @@ class connected_devices final {
}

nlohmann::json to_json(void) const {
return nlohmann::json(devices_);
auto json = nlohmann::json::array();

for (const auto& d : devices_) {
json.push_back(*d);
}

return json;
}

bool is_loaded(void) const { return loaded_; }

const std::vector<details::device>& get_devices(void) const {
const std::vector<gsl::not_null<std::shared_ptr<details::device>>>& get_devices(void) const {
return devices_;
}

void push_back_device(const details::device& device) {
if (std::find(std::begin(devices_), std::end(devices_), device) != std::end(devices_)) {
std::shared_ptr<details::device> find_device(const device_identifiers& identifiers) const {
auto it = std::find_if(std::begin(devices_),
std::end(devices_),
[&](const auto& d) {
return d->get_identifiers() == identifiers;
});
if (it != std::end(devices_)) {
return *it;
}

return nullptr;
}

void push_back_device(gsl::not_null<std::shared_ptr<details::device>> device) {
if (find_device(device->get_identifiers())) {
return;
}

devices_.push_back(device);

std::sort(devices_.begin(), devices_.end(), [](auto&& a, auto&& b) {
auto a_name = fmt::format("{0} {1} {2}",
type_safe::get(a.get_descriptions().get_product()),
type_safe::get(a.get_descriptions().get_manufacturer()),
a.get_descriptions().get_transport());
auto a_kb = a.get_identifiers().get_is_keyboard();
auto a_pd = a.get_identifiers().get_is_pointing_device();

auto b_name = fmt::format("{0} {1} {2}",
type_safe::get(b.get_descriptions().get_product()),
type_safe::get(b.get_descriptions().get_manufacturer()),
b.get_descriptions().get_transport());
auto b_kb = b.get_identifiers().get_is_keyboard();
auto b_pd = b.get_identifiers().get_is_pointing_device();

if (a_name == b_name) {
if (a_kb == b_kb) {
if (a_pd == b_pd) {
return false;
} else {
return a_pd;
}
} else {
return a_kb;
}
} else {
return a_name < b_name;
}
return true;
});
std::sort(devices_.begin(),
devices_.end(),
[](const auto& a, const auto& b) {
auto a_name = fmt::format("{0} {1} {2}",
type_safe::get(a->get_descriptions().get_product()),
type_safe::get(a->get_descriptions().get_manufacturer()),
a->get_descriptions().get_transport());
auto a_kb = a->get_identifiers().get_is_keyboard();
auto a_pd = a->get_identifiers().get_is_pointing_device();

auto b_name = fmt::format("{0} {1} {2}",
type_safe::get(b->get_descriptions().get_product()),
type_safe::get(b->get_descriptions().get_manufacturer()),
b->get_descriptions().get_transport());
auto b_kb = b->get_identifiers().get_is_keyboard();
auto b_pd = b->get_identifiers().get_is_pointing_device();

if (a_name == b_name) {
if (a_kb == b_kb) {
if (a_pd == b_pd) {
return false;
} else {
return a_pd;
}
} else {
return a_kb;
}
} else {
return a_name < b_name;
}
return true;
});
}

void clear(void) {
Expand All @@ -132,7 +154,7 @@ class connected_devices final {
private:
bool loaded_;

std::vector<details::device> devices_;
std::vector<gsl::not_null<std::shared_ptr<details::device>>> devices_;
};
} // namespace connected_devices
} // namespace krbn
26 changes: 11 additions & 15 deletions src/share/connected_devices/details/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace connected_devices {
namespace details {
class device final {
public:
device(const device&) = delete;

device(const descriptions& descriptions,
const device_identifiers& identifiers,
bool is_built_in_keyboard,
Expand All @@ -32,22 +34,20 @@ class device final {
is_built_in_touch_bar_ = device_properties.get_is_built_in_touch_bar().value_or(false);
}

static device make_from_json(const nlohmann::json& json) {
descriptions d;
device_identifiers i;

device(const nlohmann::json& json) {
if (auto j = pqrs::json::find_json(json, "descriptions")) {
d = descriptions::make_from_json(j->value());
descriptions_ = descriptions::make_from_json(j->value());
}

if (auto j = pqrs::json::find_json(json, "identifiers")) {
i = j->value().get<device_identifiers>();
identifiers_ = j->value().get<device_identifiers>();
}

return device(d,
i,
pqrs::json::find<bool>(json, "is_built_in_keyboard").value_or(false),
pqrs::json::find<bool>(json, "is_built_in_trackpad").value_or(false),
pqrs::json::find<bool>(json, "is_built_in_touch_bar").value_or(false));
is_built_in_keyboard_ = pqrs::json::find<bool>(json, "is_built_in_keyboard").value_or(false);

is_built_in_trackpad_ = pqrs::json::find<bool>(json, "is_built_in_trackpad").value_or(false);

is_built_in_touch_bar_ = pqrs::json::find<bool>(json, "is_built_in_touch_bar").value_or(false);
}

nlohmann::json to_json(void) const {
Expand Down Expand Up @@ -91,10 +91,6 @@ class device final {
descriptions_.get_product());
}

bool operator==(const device& other) const {
return identifiers_ == other.identifiers_;
}

private:
descriptions descriptions_;
device_identifiers identifiers_;
Expand Down
10 changes: 10 additions & 0 deletions src/share/core_configuration/details/profile.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../configuration_json_helper.hpp"
#include "connected_devices/connected_devices.hpp"
#include "profile/complex_modifications.hpp"
#include "profile/device.hpp"
#include "profile/parameters.hpp"
Expand Down Expand Up @@ -238,6 +239,15 @@ class profile final {
return devices_.back();
}

void erase_not_connected_devices(const connected_devices::connected_devices& connected_devices) {
devices_.erase(std::remove_if(std::begin(devices_),
std::end(devices_),
[&](auto& d) {
return !connected_devices.find_device(d->get_identifiers());
}),
std::end(devices_));
}

private:
nlohmann::json json_;
error_handling error_handling_;
Expand Down
Loading

0 comments on commit e09ac11

Please sign in to comment.