From c88c8d347c43f97b4f473c05fec1b9e45213a4ed Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Fri, 13 Oct 2023 08:06:57 +0900 Subject: [PATCH] Add unmarshal_formula --- .../details/profile/device.hpp | 40 +++++++++++++------ .../core_configuration/src/device_test.hpp | 22 ++++++++++ 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/share/core_configuration/details/profile/device.hpp b/src/share/core_configuration/details/profile/device.hpp index bea61a5d6..06e34cc60 100644 --- a/src/share/core_configuration/details/profile/device.hpp +++ b/src/share/core_configuration/details/profile/device.hpp @@ -147,24 +147,16 @@ class device final { game_pad_stick_right_stick_deadzone_ = value.get(); } else if (key == "game_pad_stick_x_formula") { - pqrs::json::requires_string(value, "`" + key + "`"); - - game_pad_stick_x_formula_ = value.get(); + 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(); + 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(); + 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(); + game_pad_stick_horizontal_wheel_formula_ = unmarshal_formula(value, key); } else if (key == "simple_modifications") { try { @@ -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(); + + } else if (json.is_array()) { + std::stringstream ss; + + for (const auto& j : json) { + if (!j.is_string()) { + goto error; + } + + ss << j.get(); + } + + 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. diff --git a/tests/src/core_configuration/src/device_test.hpp b/tests/src/core_configuration/src/device_test.hpp index dc22ce256..b3503d647 100644 --- a/tests/src/core_configuration/src/device_test.hpp +++ b/tests/src/core_configuration/src/device_test.hpp @@ -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 @@ -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)); @@ -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