Skip to content

Commit

Permalink
Remove profile::find_simple_modifications, profile::find_fn_function_…
Browse files Browse the repository at this point in the history
…keys
  • Loading branch information
tekezo committed Jun 24, 2024
1 parent 11c2e57 commit 28d5e1a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 195 deletions.
99 changes: 45 additions & 54 deletions src/lib/libkrbn/src/libkrbn_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@ gsl::not_null<std::shared_ptr<krbn::core_configuration::core_configuration>> get
return empty_core_configuration;
}

krbn::core_configuration::details::simple_modifications* find_simple_modifications(const libkrbn_device_identifiers* device_identifiers) {
gsl::not_null<std::shared_ptr<krbn::core_configuration::details::simple_modifications>> find_simple_modifications(const libkrbn_device_identifiers* device_identifiers) {
auto di = libkrbn_cpp::make_device_identifiers(device_identifiers);
auto c = get_current_core_configuration();
if (di.empty()) {
return &(c->get_selected_profile().get_simple_modifications());
return c->get_selected_profile().get_simple_modifications();
} else {
return c->get_selected_profile().find_simple_modifications(di);
auto d = c->get_selected_profile().get_device(di);
return d->get_simple_modifications();
}
}

krbn::core_configuration::details::simple_modifications* find_fn_function_keys(const libkrbn_device_identifiers* device_identifiers) {
gsl::not_null<std::shared_ptr<krbn::core_configuration::details::simple_modifications>> find_fn_function_keys(const libkrbn_device_identifiers* device_identifiers) {
auto di = libkrbn_cpp::make_device_identifiers(device_identifiers);
auto c = get_current_core_configuration();
if (di.empty()) {
return &(c->get_selected_profile().get_fn_function_keys());
return c->get_selected_profile().get_fn_function_keys();
} else {
return c->get_selected_profile().find_fn_function_keys(di);
auto d = c->get_selected_profile().get_device(di);
return d->get_fn_function_keys();
}
}
} // namespace
Expand Down Expand Up @@ -222,10 +224,8 @@ void libkrbn_core_configuration_set_selected_profile_parameters_delay_millisecon
}

size_t libkrbn_core_configuration_get_selected_profile_simple_modifications_size(const libkrbn_device_identifiers* device_identifiers) {
if (auto m = find_simple_modifications(device_identifiers)) {
return m->get_pairs().size();
}
return 0;
auto m = find_simple_modifications(device_identifiers);
return m->get_pairs().size();
}

bool libkrbn_core_configuration_get_selected_profile_simple_modification_from_json_string(size_t index,
Expand All @@ -236,12 +236,11 @@ bool libkrbn_core_configuration_get_selected_profile_simple_modification_from_js
buffer[0] = '\0';
}

if (auto m = find_simple_modifications(device_identifiers)) {
const auto& pairs = m->get_pairs();
if (index < pairs.size()) {
strlcpy(buffer, pairs[index].first.c_str(), length);
return true;
}
auto m = find_simple_modifications(device_identifiers);
const auto& pairs = m->get_pairs();
if (index < pairs.size()) {
strlcpy(buffer, pairs[index].first.c_str(), length);
return true;
}

return false;
Expand All @@ -255,46 +254,41 @@ bool libkrbn_core_configuration_get_selected_profile_simple_modification_to_json
buffer[0] = '\0';
}

if (auto m = find_simple_modifications(device_identifiers)) {
const auto& pairs = m->get_pairs();
if (index < pairs.size()) {
strlcpy(buffer, pairs[index].second.c_str(), length);
return true;
}
auto m = find_simple_modifications(device_identifiers);
const auto& pairs = m->get_pairs();
if (index < pairs.size()) {
strlcpy(buffer, pairs[index].second.c_str(), length);
return true;
}

return false;
}

void libkrbn_core_configuration_replace_selected_profile_simple_modification(size_t index,
const char* from_json_string,
const char* to_json_string,
const libkrbn_device_identifiers* device_identifiers) {
if (auto m = find_simple_modifications(device_identifiers)) {
if (from_json_string &&
to_json_string) {
m->replace_pair(index, from_json_string, to_json_string);
}
auto m = find_simple_modifications(device_identifiers);
if (from_json_string &&
to_json_string) {
m->replace_pair(index, from_json_string, to_json_string);
}
}

void libkrbn_core_configuration_push_back_selected_profile_simple_modification(const libkrbn_device_identifiers* device_identifiers) {
if (auto m = find_simple_modifications(device_identifiers)) {
m->push_back_pair();
}
auto m = find_simple_modifications(device_identifiers);
m->push_back_pair();
}

void libkrbn_core_configuration_erase_selected_profile_simple_modification(size_t index,
const libkrbn_device_identifiers* device_identifiers) {
if (auto m = find_simple_modifications(device_identifiers)) {
m->erase_pair(index);
}
auto m = find_simple_modifications(device_identifiers);
m->erase_pair(index);
}

size_t libkrbn_core_configuration_get_selected_profile_fn_function_keys_size(const libkrbn_device_identifiers* device_identifiers) {
if (auto k = find_fn_function_keys(device_identifiers)) {
return k->get_pairs().size();
}
return 0;
auto k = find_fn_function_keys(device_identifiers);
return k->get_pairs().size();
}

bool libkrbn_core_configuration_get_selected_profile_fn_function_key_from_json_string(size_t index,
Expand All @@ -305,12 +299,11 @@ bool libkrbn_core_configuration_get_selected_profile_fn_function_key_from_json_s
buffer[0] = '\0';
}

if (auto k = find_fn_function_keys(device_identifiers)) {
const auto& pairs = k->get_pairs();
if (index < pairs.size()) {
strlcpy(buffer, pairs[index].first.c_str(), length);
return true;
}
auto k = find_fn_function_keys(device_identifiers);
const auto& pairs = k->get_pairs();
if (index < pairs.size()) {
strlcpy(buffer, pairs[index].first.c_str(), length);
return true;
}

return false;
Expand All @@ -324,12 +317,11 @@ bool libkrbn_core_configuration_get_selected_profile_fn_function_key_to_json_str
buffer[0] = '\0';
}

if (auto k = find_fn_function_keys(device_identifiers)) {
const auto& pairs = k->get_pairs();
if (index < pairs.size()) {
strlcpy(buffer, pairs[index].second.c_str(), length);
return true;
}
auto k = find_fn_function_keys(device_identifiers);
const auto& pairs = k->get_pairs();
if (index < pairs.size()) {
strlcpy(buffer, pairs[index].second.c_str(), length);
return true;
}

return false;
Expand All @@ -338,11 +330,10 @@ bool libkrbn_core_configuration_get_selected_profile_fn_function_key_to_json_str
void libkrbn_core_configuration_replace_selected_profile_fn_function_key(const char* from_json_string,
const char* to_json_string,
const libkrbn_device_identifiers* device_identifiers) {
if (auto k = find_fn_function_keys(device_identifiers)) {
if (from_json_string &&
to_json_string) {
k->replace_second(from_json_string, to_json_string);
}
auto k = find_fn_function_keys(device_identifiers);
if (from_json_string &&
to_json_string) {
k->replace_second(from_json_string, to_json_string);
}
}

Expand Down
84 changes: 12 additions & 72 deletions src/share/core_configuration/details/profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ class profile final {
error_handling error_handling)
: json_(json),
error_handling_(error_handling),
selected_(false) {
selected_(false),
simple_modifications_(std::make_shared<simple_modifications>()),
fn_function_keys_(std::make_shared<simple_modifications>()) {
helper_values_.push_back_array<details::device>("devices",
devices_);

//
// Set default value
//

fn_function_keys_.update(make_default_fn_function_keys_json());
fn_function_keys_->update(make_default_fn_function_keys_json());

//
// Load from json
Expand Down Expand Up @@ -57,14 +59,14 @@ class profile final {

} else if (key == "simple_modifications") {
try {
simple_modifications_.update(value);
simple_modifications_->update(value);
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}

} else if (key == "fn_function_keys") {
try {
fn_function_keys_.update(value);
fn_function_keys_->update(value);
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}
Expand Down Expand Up @@ -148,8 +150,8 @@ class profile final {
j["name"] = name_;
j["selected"] = selected_;
j["parameters"] = parameters_;
j["simple_modifications"] = simple_modifications_.to_json(nlohmann::json::array());
j["fn_function_keys"] = fn_function_keys_.to_json(make_default_fn_function_keys_json());
j["simple_modifications"] = simple_modifications_->to_json(nlohmann::json::array());
j["fn_function_keys"] = fn_function_keys_->to_json(make_default_fn_function_keys_json());
j["complex_modifications"] = complex_modifications_.to_json();
j["virtual_hid_keyboard"] = virtual_hid_keyboard_;

Expand Down Expand Up @@ -180,52 +182,14 @@ class profile final {
return const_cast<details::parameters&>(static_cast<const profile&>(*this).get_parameters());
}

const details::simple_modifications& get_simple_modifications(void) const {
gsl::not_null<std::shared_ptr<simple_modifications>> get_simple_modifications(void) const {
return simple_modifications_;
}

details::simple_modifications& get_simple_modifications(void) {
return const_cast<details::simple_modifications&>(static_cast<const profile&>(*this).get_simple_modifications());
}

const details::simple_modifications* find_simple_modifications(const device_identifiers& identifiers) const {
for (const auto& d : devices_) {
if (d->get_identifiers() == identifiers) {
return &(d->get_simple_modifications());
}
}
return nullptr;
}

details::simple_modifications* find_simple_modifications(const device_identifiers& identifiers) {
add_device(identifiers);

return const_cast<details::simple_modifications*>(static_cast<const profile&>(*this).find_simple_modifications(identifiers));
}

const details::simple_modifications& get_fn_function_keys(void) const {
gsl::not_null<std::shared_ptr<simple_modifications>> get_fn_function_keys(void) const {
return fn_function_keys_;
}

details::simple_modifications& get_fn_function_keys(void) {
return const_cast<details::simple_modifications&>(static_cast<const profile&>(*this).get_fn_function_keys());
}

const details::simple_modifications* find_fn_function_keys(const device_identifiers& identifiers) const {
for (const auto& d : devices_) {
if (d->get_identifiers() == identifiers) {
return &(d->get_fn_function_keys());
}
}
return nullptr;
}

details::simple_modifications* find_fn_function_keys(const device_identifiers& identifiers) {
add_device(identifiers);

return const_cast<details::simple_modifications*>(static_cast<const profile&>(*this).find_fn_function_keys(identifiers));
}

const details::complex_modifications& get_complex_modifications(void) const {
return complex_modifications_;
}
Expand Down Expand Up @@ -288,37 +252,13 @@ class profile final {
}

private:
std::shared_ptr<details::device> find_device(const device_identifiers& identifiers) const {
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;
}
return nullptr;
}

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

devices_.push_back(std::make_shared<details::device>(nlohmann::json({
{"identifiers", identifiers},
}),
error_handling_));
return devices_.back();
}

nlohmann::json json_;
error_handling error_handling_;
std::string name_;
bool selected_;
details::parameters parameters_;
details::simple_modifications simple_modifications_;
details::simple_modifications fn_function_keys_;
gsl::not_null<std::shared_ptr<simple_modifications>> simple_modifications_;
gsl::not_null<std::shared_ptr<simple_modifications>> fn_function_keys_;
details::complex_modifications complex_modifications_;
details::virtual_hid_keyboard virtual_hid_keyboard_;
mutable std::vector<gsl::not_null<std::shared_ptr<details::device>>> devices_;
Expand Down
Loading

0 comments on commit 28d5e1a

Please sign in to comment.