Skip to content

Commit

Permalink
Add configuration_json_helper into details::virtual_hid_keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jun 26, 2024
1 parent 5ee49cb commit b48e418
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 129 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 @@ -890,7 +890,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(
core_configuration_->get_selected_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
12 changes: 6 additions & 6 deletions src/lib/libkrbn/src/libkrbn_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,42 +492,42 @@ uint8_t libkrbn_core_configuration_get_selected_profile_virtual_hid_keyboard_cou
auto c = get_current_core_configuration();
return static_cast<uint8_t>(type_safe::get(c->get_selected_profile()
.get_virtual_hid_keyboard()
.get_country_code()));
->get_country_code()));
}

void libkrbn_core_configuration_set_selected_profile_virtual_hid_keyboard_country_code(uint8_t value) {
auto c = get_current_core_configuration();
c->get_selected_profile()
.get_virtual_hid_keyboard()
.set_country_code(pqrs::hid::country_code::value_t(value));
->set_country_code(pqrs::hid::country_code::value_t(value));
}

int libkrbn_core_configuration_get_selected_profile_virtual_hid_keyboard_mouse_key_xy_scale(void) {
auto c = get_current_core_configuration();
return c->get_selected_profile()
.get_virtual_hid_keyboard()
.get_mouse_key_xy_scale();
->get_mouse_key_xy_scale();
}

void libkrbn_core_configuration_set_selected_profile_virtual_hid_keyboard_mouse_key_xy_scale(int value) {
auto c = get_current_core_configuration();
c->get_selected_profile()
.get_virtual_hid_keyboard()
.set_mouse_key_xy_scale(value);
->set_mouse_key_xy_scale(value);
}

bool libkrbn_core_configuration_get_selected_profile_virtual_hid_keyboard_indicate_sticky_modifier_keys_state(void) {
auto c = get_current_core_configuration();
return c->get_selected_profile()
.get_virtual_hid_keyboard()
.get_indicate_sticky_modifier_keys_state();
->get_indicate_sticky_modifier_keys_state();
}

void libkrbn_core_configuration_set_selected_profile_virtual_hid_keyboard_indicate_sticky_modifier_keys_state(bool value) {
auto c = get_current_core_configuration();
c->get_selected_profile()
.get_virtual_hid_keyboard()
.set_indicate_sticky_modifier_keys_state(value);
->set_indicate_sticky_modifier_keys_state(value);
}

bool libkrbn_core_configuration_get_selected_profile_device_ignore(const libkrbn_device_identifiers* device_identifiers) {
Expand Down
22 changes: 7 additions & 15 deletions src/share/core_configuration/details/profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ class profile final {
selected_(false),
parameters_(std::make_shared<details::parameters>()),
simple_modifications_(std::make_shared<simple_modifications>()),
fn_function_keys_(std::make_shared<simple_modifications>()) {
fn_function_keys_(std::make_shared<simple_modifications>()),
virtual_hid_keyboard_(std::make_shared<details::virtual_hid_keyboard>()) {
helper_values_.push_back_object<details::parameters>("parameters",
parameters_);

helper_values_.push_back_object<details::virtual_hid_keyboard>("virtual_hid_keyboard",
virtual_hid_keyboard_);

helper_values_.push_back_array<details::device>("devices",
devices_);

Expand Down Expand Up @@ -74,13 +78,6 @@ class profile final {
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}

} else if (key == "virtual_hid_keyboard") {
try {
virtual_hid_keyboard_ = value.get<virtual_hid_keyboard>();
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` error: {1}", key, e.what()));
}
}
}
}
Expand Down Expand Up @@ -166,7 +163,6 @@ class profile final {
}

j["complex_modifications"] = complex_modifications_.to_json();
j["virtual_hid_keyboard"] = virtual_hid_keyboard_;

return j;
}
Expand Down Expand Up @@ -223,11 +219,7 @@ class profile final {
complex_modifications_.set_parameter_value(name, value);
}

const details::virtual_hid_keyboard& get_virtual_hid_keyboard(void) const {
return virtual_hid_keyboard_;
}

details::virtual_hid_keyboard& get_virtual_hid_keyboard(void) {
gsl::not_null<std::shared_ptr<details::virtual_hid_keyboard>> get_virtual_hid_keyboard(void) const {
return virtual_hid_keyboard_;
}

Expand Down Expand Up @@ -269,7 +261,7 @@ class profile final {
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_;
gsl::not_null<std::shared_ptr<details::virtual_hid_keyboard>> virtual_hid_keyboard_;
mutable std::vector<gsl::not_null<std::shared_ptr<details::device>>> devices_;
configuration_json_helper::helper_values helper_values_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,50 @@ namespace core_configuration {
namespace details {
class virtual_hid_keyboard final {
public:
virtual_hid_keyboard(void) : json_(nlohmann::json::object()),
country_code_(0),
mouse_key_xy_scale_(100),
indicate_sticky_modifier_keys_state_(true) {
virtual_hid_keyboard(const virtual_hid_keyboard&) = delete;

virtual_hid_keyboard(void)
: virtual_hid_keyboard(nlohmann::json::object(),
krbn::core_configuration::error_handling::loose) {
}

const nlohmann::json& get_json(void) const {
return json_;
virtual_hid_keyboard(const nlohmann::json& json,
error_handling error_handling)
: json_(json) {
helper_values_.push_back_value<pqrs::hid::country_code::value_t>("country_code",
country_code_,
pqrs::hid::country_code::value_t(0));

helper_values_.push_back_value<int>("mouse_key_xy_scale",
mouse_key_xy_scale_,
100);

helper_values_.push_back_value<bool>("indicate_sticky_modifier_keys_state",
indicate_sticky_modifier_keys_state_,
true);

pqrs::json::requires_object(json, "json");

helper_values_.update_value(json, error_handling);
}

void set_json(const nlohmann::json& value) {
json_ = value;
nlohmann::json to_json(void) const {
auto j = json_;

helper_values_.update_json(j);

return j;
}

pqrs::hid::country_code::value_t get_country_code(void) const {
const pqrs::hid::country_code::value_t& get_country_code(void) const {
return country_code_;
}

void set_country_code(pqrs::hid::country_code::value_t value) {
country_code_ = value;
}

int get_mouse_key_xy_scale(void) const {
const int& get_mouse_key_xy_scale(void) const {
return mouse_key_xy_scale_;
}

Expand All @@ -41,7 +62,7 @@ class virtual_hid_keyboard final {
mouse_key_xy_scale_ = value;
}

bool get_indicate_sticky_modifier_keys_state(void) const {
const bool& get_indicate_sticky_modifier_keys_state(void) const {
return indicate_sticky_modifier_keys_state_;
}

Expand All @@ -61,40 +82,8 @@ class virtual_hid_keyboard final {
pqrs::hid::country_code::value_t country_code_;
int mouse_key_xy_scale_;
bool indicate_sticky_modifier_keys_state_;
configuration_json_helper::helper_values helper_values_;
};

inline void to_json(nlohmann::json& json, const virtual_hid_keyboard& value) {
json = value.get_json();
json["country_code"] = value.get_country_code();
json["mouse_key_xy_scale"] = value.get_mouse_key_xy_scale();
json["indicate_sticky_modifier_keys_state"] = value.get_indicate_sticky_modifier_keys_state();
}

inline void from_json(const nlohmann::json& json, virtual_hid_keyboard& value) {
pqrs::json::requires_object(json, "json");

for (const auto& [k, v] : json.items()) {
if (k == "country_code") {
value.set_country_code(v.get<pqrs::hid::country_code::value_t>());

} else if (k == "mouse_key_xy_scale") {
pqrs::json::requires_number(v, "`" + k + "`");

value.set_mouse_key_xy_scale(v.get<int>());

} else if (k == "indicate_sticky_modifier_keys_state") {
pqrs::json::requires_boolean(v, "`" + k + "`");

value.set_indicate_sticky_modifier_keys_state(v.get<int>());

} else {
// Allow unknown keys in order to be able to load
// newer version of karabiner.json with older Karabiner-Elements.
}
}

value.set_json(json);
}
} // namespace details
} // namespace core_configuration
} // namespace krbn
Expand Down
2 changes: 1 addition & 1 deletion src/share/manipulator/manipulator_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class manipulator_environment final {
void update_virtual_hid_keyboard_keyboard_type(void) {
pqrs::hid::country_code::value_t country_code(0);
if (auto c = core_configuration_.lock()) {
country_code = c->get_selected_profile().get_virtual_hid_keyboard().get_country_code();
country_code = c->get_selected_profile().get_virtual_hid_keyboard()->get_country_code();
}

pqrs::osx::system_preferences::keyboard_type_key key(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class mouse_key_handler final : public pqrs::dispatcher::extra::dispatcher_clien

double xy_scale = 1.0;
if (auto c = oeq->get_manipulator_environment().get_core_configuration().lock()) {
xy_scale = static_cast<double>(c->get_selected_profile().get_virtual_hid_keyboard().get_mouse_key_xy_scale()) / 100.0;
xy_scale = static_cast<double>(c->get_selected_profile().get_virtual_hid_keyboard()->get_mouse_key_xy_scale()) / 100.0;
}

pqrs::karabiner::driverkit::virtual_hid_device_driver::hid_report::pointing_input report;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ class post_event_to_virtual_devices final : public base, public pqrs::dispatcher
void update_sticky_modifiers_notification_message(const event_queue::queue& output_event_queue) {
if (auto notification_message_manager = weak_notification_message_manager_.lock()) {
if (auto c = output_event_queue.get_manipulator_environment().get_core_configuration().lock()) {
if (c->get_selected_profile().get_virtual_hid_keyboard().get_indicate_sticky_modifier_keys_state()) {
if (c->get_selected_profile().get_virtual_hid_keyboard()->get_indicate_sticky_modifier_keys_state()) {
notification_message_manager->async_update_sticky_modifiers_message(output_event_queue.get_modifier_flag_manager());
} else {
notification_message_manager->async_clear_sticky_modifiers_message();
Expand Down
7 changes: 1 addition & 6 deletions tests/src/core_configuration/json/to_json_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
}
},
"name": "Default profile",
"selected": true,
"virtual_hid_keyboard": {
"country_code": 0,
"indicate_sticky_modifier_keys_state": true,
"mouse_key_xy_scale": 100
}
"selected": true
}
]
}
21 changes: 3 additions & 18 deletions tests/src/core_configuration/json/to_json_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,7 @@
"rules": []
},
"name": "Empty",
"selected": false,
"virtual_hid_keyboard": {
"country_code": 0,
"indicate_sticky_modifier_keys_state": true,
"mouse_key_xy_scale": 100
}
"selected": false
},
{
"complex_modifications": {
Expand All @@ -292,12 +287,7 @@
}
],
"name": "fn_function_keys v1",
"selected": false,
"virtual_hid_keyboard": {
"country_code": 0,
"indicate_sticky_modifier_keys_state": true,
"mouse_key_xy_scale": 100
}
"selected": false
},
{
"complex_modifications": {
Expand All @@ -323,12 +313,7 @@
}
]
}
],
"virtual_hid_keyboard": {
"country_code": 0,
"indicate_sticky_modifier_keys_state": true,
"mouse_key_xy_scale": 100
}
]
}
]
}
Loading

0 comments on commit b48e418

Please sign in to comment.