Skip to content

Commit

Permalink
game_pad_stick_wheels_interval_milliseconds -> game_pad_wheels_stick_…
Browse files Browse the repository at this point in the history
…interval_milliseconds_formula
  • Loading branch information
tekezo committed Oct 19, 2023
1 parent 15cc0d8 commit ebd300f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
magnitude_(0.0),
stroke_acceleration_(0.0),
remain_deadzone_threshold_milliseconds_(0),
stroke_acceleration_measurement_milliseconds_(0),
wheels_interval_milliseconds_(0) {
stroke_acceleration_measurement_milliseconds_(0) {
auto now = pqrs::osx::chrono::mach_absolute_time_point();
deadzone_entered_at_ = now;
deadzone_left_at_ = now;
Expand Down Expand Up @@ -102,15 +101,15 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
remain_deadzone_threshold_milliseconds_ = 100;
stroke_acceleration_measurement_milliseconds_ = 50;

wheels_interval_milliseconds_ = core_configuration.get_selected_profile().get_device_game_pad_stick_wheels_interval_milliseconds(device_identifiers);

xy_stick_interval_milliseconds_formula_string_ = core_configuration.get_selected_profile().get_device_game_pad_xy_stick_interval_milliseconds_formula(device_identifiers);
wheels_stick_interval_milliseconds_formula_string_ = core_configuration.get_selected_profile().get_device_game_pad_wheels_stick_interval_milliseconds_formula(device_identifiers);
x_formula_string_ = core_configuration.get_selected_profile().get_device_game_pad_stick_x_formula(device_identifiers);
y_formula_string_ = core_configuration.get_selected_profile().get_device_game_pad_stick_y_formula(device_identifiers);
vertical_wheel_formula_string_ = core_configuration.get_selected_profile().get_device_game_pad_stick_vertical_wheel_formula(device_identifiers);
horizontal_wheel_formula_string_ = core_configuration.get_selected_profile().get_device_game_pad_stick_horizontal_wheel_formula(device_identifiers);

xy_stick_interval_milliseconds_formula_ = make_formula_expression(xy_stick_interval_milliseconds_formula_string_);
wheels_stick_interval_milliseconds_formula_ = make_formula_expression(wheels_stick_interval_milliseconds_formula_string_);
x_formula_ = make_formula_expression(x_formula_string_);
y_formula_ = make_formula_expression(y_formula_string_);
vertical_wheel_formula_ = make_formula_expression(vertical_wheel_formula_string_);
Expand Down Expand Up @@ -169,12 +168,22 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
return std::chrono::milliseconds(static_cast<int>(interval));
}

std::chrono::milliseconds wheels_interval(void) const {
std::chrono::milliseconds wheels_stick_interval(void) const {
if (stroke_acceleration_ == 0.0) {
return std::chrono::milliseconds(0);
}

return std::chrono::milliseconds(wheels_interval_milliseconds_);
auto interval = wheels_stick_interval_milliseconds_formula_.value();
if (std::isnan(interval)) {
logger::get_logger()->error("game_pad_stick_converter wheels_stick_interval_milliseconds_formula_ returns nan: {0} (radian: {1}, magnitude: {2}, acceleration: {3})",
wheels_stick_interval_milliseconds_formula_string_,
radian_,
magnitude_,
stroke_acceleration_);
interval = 0;
}

return std::chrono::milliseconds(static_cast<int>(interval));
}

private:
Expand Down Expand Up @@ -246,12 +255,13 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
int remain_deadzone_threshold_milliseconds_;
int stroke_acceleration_measurement_milliseconds_;
std::string xy_stick_interval_milliseconds_formula_string_;
int wheels_interval_milliseconds_;
std::string wheels_stick_interval_milliseconds_formula_string_;
std::string x_formula_string_;
std::string y_formula_string_;
std::string vertical_wheel_formula_string_;
std::string horizontal_wheel_formula_string_;
exprtk_utility::expression_t xy_stick_interval_milliseconds_formula_;
exprtk_utility::expression_t wheels_stick_interval_milliseconds_formula_;
exprtk_utility::expression_t x_formula_;
exprtk_utility::expression_t y_formula_;
exprtk_utility::expression_t vertical_wheel_formula_;
Expand Down Expand Up @@ -339,13 +349,21 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche

xy_timer_.start(
[this, event_origin] {
//
// Update interval
//

auto interval = xy_.xy_stick_interval();
if (interval == std::chrono::milliseconds(0)) {
xy_timer_.stop();
return;
}
xy_timer_.set_interval(interval);

//
// Post event
//

auto [x, y] = xy_.xy_value();

pointing_motion m(x,
Expand All @@ -363,13 +381,15 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
event_origin,
event_queue::state::original);

pointing_motion_arrived_(entry);
enqueue_to_dispatcher([this, entry] {
pointing_motion_arrived_(entry);
});
},
interval);
}

void update_wheels_timer(event_origin event_origin) {
auto interval = wheels_.wheels_interval();
auto interval = wheels_.wheels_stick_interval();
if (interval == std::chrono::milliseconds(0)) {
wheels_timer_.stop();
return;
Expand All @@ -381,10 +401,20 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche

wheels_timer_.start(
[this, event_origin] {
if (wheels_.wheels_interval() == std::chrono::milliseconds(0)) {
//
// Update interval
//

auto interval = wheels_.wheels_stick_interval();
if (interval == std::chrono::milliseconds(0)) {
wheels_timer_.stop();
return;
}
wheels_timer_.set_interval(interval);

//
// Post event
//

auto [v, h] = wheels_.wheels_value();

Expand All @@ -403,7 +433,9 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
event_origin,
event_queue::state::original);

pointing_motion_arrived_(entry);
enqueue_to_dispatcher([this, entry] {
pointing_motion_arrived_(entry);
});
},
interval);
}
Expand Down
14 changes: 7 additions & 7 deletions src/share/core_configuration/details/profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ class profile final {
}

void set_device_game_pad_xy_stick_interval_milliseconds_formula(const device_identifiers& identifiers,
std::optional<std::string> value) {
const std::optional<std::string>& value) {
add_device(identifiers);

for (auto&& device : devices_) {
Expand All @@ -573,24 +573,24 @@ class profile final {
}
}

int get_device_game_pad_stick_wheels_interval_milliseconds(const device_identifiers& identifiers) const {
std::string get_device_game_pad_wheels_stick_interval_milliseconds_formula(const device_identifiers& identifiers) const {
for (const auto& d : devices_) {
if (d.get_identifiers() == identifiers) {
if (auto value = d.get_game_pad_stick_wheels_interval_milliseconds()) {
if (auto value = d.get_game_pad_wheels_stick_interval_milliseconds_formula()) {
return *value;
}
}
}
return device::game_pad_stick_wheels_interval_milliseconds_default_value;
return std::string(device::game_pad_wheels_stick_interval_milliseconds_formula_default_value);
}

void set_device_game_pad_stick_wheels_interval_milliseconds(const device_identifiers& identifiers,
std::optional<int> value) {
void set_device_game_pad_wheels_stick_interval_milliseconds_formula(const device_identifiers& identifiers,
const std::optional<std::string>& value) {
add_device(identifiers);

for (auto&& device : devices_) {
if (device.get_identifiers() == identifiers) {
device.set_game_pad_stick_wheels_interval_milliseconds(value);
device.set_game_pad_wheels_stick_interval_milliseconds_formula(value);
return;
}
}
Expand Down
54 changes: 26 additions & 28 deletions src/share/core_configuration/details/profile/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ class device final {

static constexpr std::string_view game_pad_xy_stick_interval_milliseconds_formula_default_value =
"20";
static constexpr int game_pad_stick_wheels_interval_milliseconds_default_value = 100;
static constexpr std::string_view game_pad_wheels_stick_interval_milliseconds_formula_default_value =
"switch {\n"
" case acceleration > 0.5: 50;\n"
" default: 100;\n"
"}\n";

// The logical value range of Karabiner-DriverKit-VirtualHIDPointing is -127 ... 127.
static constexpr std::string_view game_pad_stick_x_formula_default_value =
"var a := acceleration ^ 2;"
"cos(radian) * a * 127;";
"var a := acceleration ^ 2;\n"
"cos(radian) * a * 127;\n";

// The logical value range of Karabiner-DriverKit-VirtualHIDPointing is -127 ... 127.
static constexpr std::string_view game_pad_stick_y_formula_default_value =
"var a := acceleration ^ 2;"
"sin(radian) * a * 127;";
"var a := acceleration ^ 2;\n"
"sin(radian) * a * 127;\n";

// The logical value range of Karabiner-DriverKit-VirtualHIDPointing is -127 ... 127.
static constexpr std::string_view game_pad_stick_vertical_wheel_formula_default_value =
"var scale := switch {"
" case acceleration > 0.6: 5;"
" default: 1;"
"};"
"if (abs(cos(radian)) > abs(sin(radian))) { scale := 0; };"
"sgn(sin(radian)) * scale;";
"switch {\n"
" case abs(cos(radian)) > abs(sin(radian)) : 0;\n"
" default : sgn(sin(radian));\n"
"}\n";

// The logical value range of Karabiner-DriverKit-VirtualHIDPointing is -127 ... 127.
static constexpr std::string_view game_pad_stick_horizontal_wheel_formula_default_value =
"var scale := switch {"
" case acceleration > 0.6: 5;"
" default: 1;"
"};"
"if (abs(cos(radian)) < abs(sin(radian))) { scale := 0; };"
"sgn(cos(radian)) * scale;";
"switch {\n"
" case abs(cos(radian)) < abs(sin(radian)) : 0;\n"
" default : sgn(cos(radian));\n"
"}\n";

device(const nlohmann::json& json) : json_(json),
ignore_(false),
Expand Down Expand Up @@ -150,10 +150,8 @@ class device final {
} else if (key == "game_pad_xy_stick_interval_milliseconds_formula") {
game_pad_xy_stick_interval_milliseconds_formula_ = unmarshal_formula(value, key);

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

game_pad_stick_wheels_interval_milliseconds_ = value.get<double>();
} else if (key == "game_pad_wheels_stick_interval_milliseconds_formula") {
game_pad_wheels_stick_interval_milliseconds_formula_ = unmarshal_formula(value, key);

} else if (key == "game_pad_stick_x_formula") {
game_pad_stick_x_formula_ = unmarshal_formula(value, key);
Expand Down Expand Up @@ -250,8 +248,8 @@ class device final {
if (game_pad_xy_stick_interval_milliseconds_formula_ != std::nullopt) {
j["game_pad_xy_stick_interval_milliseconds_formula"] = marshal_formula(*game_pad_xy_stick_interval_milliseconds_formula_);
}
if (game_pad_stick_wheels_interval_milliseconds_ != std::nullopt) {
j["game_pad_stick_wheels_interval_milliseconds"] = *game_pad_stick_wheels_interval_milliseconds_;
if (game_pad_wheels_stick_interval_milliseconds_formula_ != std::nullopt) {
j["game_pad_wheels_stick_interval_milliseconds_formula"] = marshal_formula(*game_pad_wheels_stick_interval_milliseconds_formula_);
}
if (game_pad_stick_x_formula_ != std::nullopt) {
j["game_pad_stick_x_formula"] = marshal_formula(*game_pad_stick_x_formula_);
Expand Down Expand Up @@ -400,11 +398,11 @@ class device final {
coordinate_between_properties();
}

std::optional<double> get_game_pad_stick_wheels_interval_milliseconds(void) const {
return game_pad_stick_wheels_interval_milliseconds_;
const std::optional<std::string>& get_game_pad_wheels_stick_interval_milliseconds_formula(void) const {
return game_pad_wheels_stick_interval_milliseconds_formula_;
}
void set_game_pad_stick_wheels_interval_milliseconds(std::optional<double> value) {
game_pad_stick_wheels_interval_milliseconds_ = value;
void set_game_pad_wheels_stick_interval_milliseconds_formula(const std::optional<std::string>& value) {
game_pad_wheels_stick_interval_milliseconds_formula_ = value;

coordinate_between_properties();
}
Expand Down Expand Up @@ -530,7 +528,7 @@ class device final {
std::optional<double> game_pad_xy_stick_deadzone_;
std::optional<double> game_pad_wheels_stick_deadzone_;
std::optional<std::string> game_pad_xy_stick_interval_milliseconds_formula_;
std::optional<int> game_pad_stick_wheels_interval_milliseconds_;
std::optional<std::string> game_pad_wheels_stick_interval_milliseconds_formula_;
std::optional<std::string> game_pad_stick_x_formula_;
std::optional<std::string> game_pad_stick_y_formula_;
std::optional<std::string> game_pad_stick_vertical_wheel_formula_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@
"error": "`game_pad_xy_stick_interval_milliseconds_formula` must be array of string, or string, but is `null`"
},

// game_pad_stick_wheels_interval_milliseconds
// game_pad_wheels_stick_interval_milliseconds_formula

{
"class": "devices",
"input": {
"game_pad_stick_wheels_interval_milliseconds": null
"game_pad_wheels_stick_interval_milliseconds_formula": null
},
"error": "`game_pad_stick_wheels_interval_milliseconds` must be number, but is `null`"
"error": "`game_pad_wheels_stick_interval_milliseconds_formula` must be array of string, or string, but is `null`"
},

// game_pad_stick_x_formula
Expand Down
6 changes: 3 additions & 3 deletions tests/src/core_configuration/src/device_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void run_device_test(void) {
expect(device.get_game_pad_xy_stick_deadzone() == std::nullopt);
expect(device.get_game_pad_wheels_stick_deadzone() == std::nullopt);
expect(device.get_game_pad_xy_stick_interval_milliseconds_formula() == std::nullopt);
expect(device.get_game_pad_stick_wheels_interval_milliseconds() == std::nullopt);
expect(device.get_game_pad_wheels_stick_interval_milliseconds_formula() == 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);
Expand Down Expand Up @@ -354,7 +354,7 @@ void run_device_test(void) {
{"game_pad_xy_stick_deadzone", 0.08},
{"game_pad_wheels_stick_deadzone", 0.08},
{"game_pad_xy_stick_interval_milliseconds_formula", "20"},
{"game_pad_stick_wheels_interval_milliseconds", 20},
{"game_pad_wheels_stick_interval_milliseconds_formula", "100"},
{"game_pad_stick_x_formula", "cos(radian) * acceleration * 127"},
{"game_pad_stick_y_formula", nlohmann::json::array({
"var a := acceleration ^ 2;",
Expand Down Expand Up @@ -399,7 +399,7 @@ void run_device_test(void) {
{"game_pad_xy_stick_deadzone", 0.08},
{"game_pad_wheels_stick_deadzone", 0.08},
{"game_pad_xy_stick_interval_milliseconds_formula", "20"},
{"game_pad_stick_wheels_interval_milliseconds", 20},
{"game_pad_wheels_stick_interval_milliseconds_formula", "100"},
{"game_pad_stick_x_formula", "cos(radian) * acceleration * 127"},
{"game_pad_stick_y_formula", nlohmann::json::array({
"var a := acceleration ^ 2;",
Expand Down

0 comments on commit ebd300f

Please sign in to comment.