Skip to content

Commit

Permalink
Add unmarshal_formula
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Oct 12, 2023
1 parent dcf5942 commit c88c8d3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/share/core_configuration/details/profile/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,16 @@ class device final {
game_pad_stick_right_stick_deadzone_ = value.get<double>();

} else if (key == "game_pad_stick_x_formula") {
pqrs::json::requires_string(value, "`" + key + "`");

game_pad_stick_x_formula_ = value.get<std::string>();
game_pad_stick_x_formula_ = unmarshal_formula(value, key);

} else if (key == "game_pad_stick_y_formula") {
pqrs::json::requires_string(value, "`" + key + "`");

game_pad_stick_y_formula_ = value.get<std::string>();
game_pad_stick_y_formula_ = unmarshal_formula(value, key);

} else if (key == "game_pad_stick_vertical_wheel_formula") {
pqrs::json::requires_string(value, "`" + key + "`");

game_pad_stick_vertical_wheel_formula_ = value.get<std::string>();
game_pad_stick_vertical_wheel_formula_ = unmarshal_formula(value, key);

} else if (key == "game_pad_stick_horizontal_wheel_formula") {
pqrs::json::requires_string(value, "`" + key + "`");

game_pad_stick_horizontal_wheel_formula_ = value.get<std::string>();
game_pad_stick_horizontal_wheel_formula_ = unmarshal_formula(value, key);

} else if (key == "simple_modifications") {
try {
Expand Down Expand Up @@ -425,6 +417,30 @@ class device final {
}

private:
static std::string unmarshal_formula(const nlohmann::json& json, const std::string& name) {
if (json.is_string()) {
return json.get<std::string>();

} else if (json.is_array()) {
std::stringstream ss;

for (const auto& j : json) {
if (!j.is_string()) {
goto error;
}

ss << j.get<std::string>();
}

return ss.str();
}

error:
throw pqrs::json::unmarshal_error(fmt::format("{0} json must be array of string, or string, but is `{1}`",
name,
pqrs::json::dump_for_error_message(json)));
}

void coordinate_between_properties(void) {
// Set `disable_built_in_keyboard_if_exists_` false if `treat_as_built_in_keyboard_` is true.
// If both settings are true, the device will always be disabled.
Expand Down
22 changes: 22 additions & 0 deletions tests/src/core_configuration/src/device_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ void run_device_test(void) {
expect(device.get_manipulate_caps_lock_led() == false);
expect(device.get_treat_as_built_in_keyboard() == false);
expect(device.get_disable_built_in_keyboard_if_exists() == false);
expect(device.get_game_pad_stick_left_stick_deadzone() == std::nullopt);
expect(device.get_game_pad_stick_right_stick_deadzone() == std::nullopt);
expect(device.get_game_pad_stick_x_formula() == std::nullopt);
expect(device.get_game_pad_stick_y_formula() == std::nullopt);
expect(device.get_game_pad_stick_vertical_wheel_formula() == std::nullopt);
expect(device.get_game_pad_stick_horizontal_wheel_formula() == std::nullopt);
}

// load values from json
Expand All @@ -140,6 +146,16 @@ void run_device_test(void) {
{"ignore", true},
{"manipulate_caps_lock_led", true},
{"treat_as_built_in_keyboard", false},
{"game_pad_stick_left_stick_deadzone", 0.08},
// string
{"game_pad_stick_x_formula", "cos(radian) * acceleration * 127"},
// array of string
{"game_pad_stick_y_formula", nlohmann::json::array({
"if (acceleration < 0.5,",
" cos(radian) * acceleration * 127 * 0.5,",
" cos(radian) * acceleration * 127",
")",
})},
});
krbn::core_configuration::details::device device(json);
expect(device.get_identifiers().get_vendor_id() == pqrs::hid::vendor_id::value_t(1234));
Expand All @@ -151,6 +167,12 @@ void run_device_test(void) {
expect(device.get_manipulate_caps_lock_led() == true);
expect(device.get_treat_as_built_in_keyboard() == false);
expect(device.get_disable_built_in_keyboard_if_exists() == true);
expect(device.get_game_pad_stick_left_stick_deadzone() == 0.08);
expect(device.get_game_pad_stick_x_formula() == "cos(radian) * acceleration * 127");
expect(device.get_game_pad_stick_y_formula() == "if (acceleration < 0.5,"
" cos(radian) * acceleration * 127 * 0.5,"
" cos(radian) * acceleration * 127"
")");
}

// Special default value for specific devices
Expand Down

0 comments on commit c88c8d3

Please sign in to comment.