Skip to content

Commit

Permalink
Use device parameters in game_pad_stick_converter
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Oct 9, 2023
1 parent e27f027 commit b439e8d
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,26 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche

void update_horizontal_stick_sensor_value(CFIndex logical_max,
CFIndex logical_min,
CFIndex integer_value) {
CFIndex integer_value,
double deadzone) {
horizontal_stick_sensor_.update_stick_sensor_value(logical_max,
logical_min,
integer_value);
update();
update(deadzone);
}

void update_vertical_stick_sensor_value(CFIndex logical_max,
CFIndex logical_min,
CFIndex integer_value) {
CFIndex integer_value,
double deadzone) {
vertical_stick_sensor_.update_stick_sensor_value(logical_max,
logical_min,
integer_value);
update();
update(deadzone);
}

std::pair<int, int> xy_value(void) const {
std::pair<int, int> xy_value(double scale) const {
// The logical value range of Karabiner-DriverKit-VirtualHIDPointing is -127 ... 127.
double scale = 1.0;
auto x = static_cast<int>(std::cos(radian_) * holding_magnitude_ * scale * 127);
auto y = static_cast<int>(std::sin(radian_) * holding_magnitude_ * scale * 127);

Expand Down Expand Up @@ -144,7 +145,7 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
}

private:
void update(void) {
void update(double deadzone) {
radian_ = std::atan2(vertical_stick_sensor_.get_value(),
horizontal_stick_sensor_.get_value());
magnitude_ = std::min(1.0,
Expand All @@ -155,7 +156,6 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
// Update holding_acceleration_, holding_magnitude_
//

const double deadzone = 0.1;
if (std::abs(vertical_stick_sensor_.get_value()) < deadzone &&
std::abs(horizontal_stick_sensor_.get_value()) < deadzone) {
histories_.clear();
Expand Down Expand Up @@ -221,6 +221,14 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
};

struct state final {
state(void) {
}

state(const device_identifiers& di)
: device_identifiers(di) {
}

device_identifiers device_identifiers;
stick xy;
stick wheels;
};
Expand Down Expand Up @@ -251,7 +259,7 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
void register_device(const device_properties& device_properties) {
if (auto is_game_pad = device_properties.get_is_game_pad()) {
if (*is_game_pad) {
states_[device_properties.get_device_id()] = state();
states_[device_properties.get_device_id()] = state(device_properties.get_device_identifiers());
}
}
}
Expand All @@ -268,6 +276,14 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
return;
}

auto c = core_configuration_.lock();
if (!c) {
return;
}

auto left_stick_deadzone = c->get_selected_profile().get_device_game_pad_stick_left_stick_deadzone(device_properties.get_device_identifiers());
auto right_stick_deadzone = c->get_selected_profile().get_device_game_pad_stick_right_stick_deadzone(device_properties.get_device_identifiers());

for (const auto& v : hid_values) {
if (auto usage_page = v.get_usage_page()) {
if (auto usage = v.get_usage()) {
Expand All @@ -282,47 +298,55 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
if (flip_sticks) {
it->second.wheels.update_horizontal_stick_sensor_value(*logical_max,
*logical_min,
v.get_integer_value());
v.get_integer_value(),
right_stick_deadzone);
} else {
it->second.xy.update_horizontal_stick_sensor_value(*logical_max,
*logical_min,
v.get_integer_value());
v.get_integer_value(),
left_stick_deadzone);
}

} else if (v.conforms_to(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::y)) {
if (flip_sticks) {
it->second.wheels.update_vertical_stick_sensor_value(*logical_max,
*logical_min,
v.get_integer_value());
v.get_integer_value(),
right_stick_deadzone);
} else {
it->second.xy.update_vertical_stick_sensor_value(*logical_max,
*logical_min,
v.get_integer_value());
v.get_integer_value(),
left_stick_deadzone);
}

} else if (v.conforms_to(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::rz)) {
if (flip_sticks) {
it->second.xy.update_vertical_stick_sensor_value(*logical_max,
*logical_min,
v.get_integer_value());
v.get_integer_value(),
left_stick_deadzone);
} else {
it->second.wheels.update_vertical_stick_sensor_value(*logical_max,
*logical_min,
v.get_integer_value());
v.get_integer_value(),
right_stick_deadzone);
}

} else if (v.conforms_to(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::z)) {
if (flip_sticks) {
it->second.xy.update_horizontal_stick_sensor_value(*logical_max,
*logical_min,
v.get_integer_value());
v.get_integer_value(),
left_stick_deadzone);
} else {
it->second.wheels.update_horizontal_stick_sensor_value(*logical_max,
*logical_min,
v.get_integer_value());
v.get_integer_value(),
right_stick_deadzone);
}
}
}
Expand Down Expand Up @@ -383,8 +407,6 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche

enqueue_to_dispatcher(
[this, device_id, event_origin, get_interval, unset_active, make_pointing_motion, entry] {
pointing_motion_arrived(entry);

emit_event(device_id,
event_origin,
get_interval,
Expand All @@ -404,8 +426,13 @@ class game_pad_stick_converter final : public pqrs::dispatcher::extra::dispatche
s.xy.set_active(false);
};

auto make_pointing_motion = [](const state& s) {
auto [x, y] = s.xy.xy_value();
auto make_pointing_motion = [this](const state& s) {
double scale = 0.0;
if (auto c = core_configuration_.lock()) {
scale = c->get_selected_profile().get_device_game_pad_stick_xy_scale(s.device_identifiers);
}

auto [x, y] = s.xy.xy_value(scale);

return pointing_motion(x,
y,
Expand Down
63 changes: 63 additions & 0 deletions src/share/core_configuration/details/profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,69 @@ class profile final {
}
}

double get_device_game_pad_stick_left_stick_deadzone(const device_identifiers& identifiers) const {
for (const auto& d : devices_) {
if (d.get_identifiers() == identifiers) {
return d.get_game_pad_stick_left_stick_deadzone();
}
}
return device::game_pad_stick_left_stick_deadzone_default_value;
}

void set_device_game_pad_stick_left_stick_deadzone(const device_identifiers& identifiers,
double value) {
add_device(identifiers);

for (auto&& device : devices_) {
if (device.get_identifiers() == identifiers) {
device.set_game_pad_stick_left_stick_deadzone(value);
return;
}
}
}

double get_device_game_pad_stick_right_stick_deadzone(const device_identifiers& identifiers) const {
for (const auto& d : devices_) {
if (d.get_identifiers() == identifiers) {
return d.get_game_pad_stick_right_stick_deadzone();
}
}
return device::game_pad_stick_right_stick_deadzone_default_value;
}

void set_device_game_pad_stick_right_stick_deadzone(const device_identifiers& identifiers,
double value) {
add_device(identifiers);

for (auto&& device : devices_) {
if (device.get_identifiers() == identifiers) {
device.set_game_pad_stick_right_stick_deadzone(value);
return;
}
}
}

double get_device_game_pad_stick_xy_scale(const device_identifiers& identifiers) const {
for (const auto& d : devices_) {
if (d.get_identifiers() == identifiers) {
return d.get_game_pad_stick_xy_scale();
}
}
return device::game_pad_stick_xy_scale_default_value;
}

void set_device_game_pad_stick_xy_scale(const device_identifiers& identifiers,
double value) {
add_device(identifiers);

for (auto&& device : devices_) {
if (device.get_identifiers() == identifiers) {
device.set_game_pad_stick_xy_scale(value);
return;
}
}
}

private:
void add_device(const device_identifiers& identifiers) {
for (auto&& device : devices_) {
Expand Down
10 changes: 7 additions & 3 deletions src/share/core_configuration/details/profile/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ namespace core_configuration {
namespace details {
class device final {
public:
static constexpr double game_pad_stick_left_stick_deadzone_default_value = 0.1;
static constexpr double game_pad_stick_right_stick_deadzone_default_value = 0.1;
static constexpr double game_pad_stick_xy_scale_default_value = 0.5;

device(const nlohmann::json& json) : json_(json),
ignore_(false),
manipulate_caps_lock_led_(false),
Expand All @@ -18,9 +22,9 @@ class device final {
mouse_flip_horizontal_wheel_(false),
mouse_swap_xy_(false),
mouse_swap_wheel_(false),
game_pad_stick_left_stick_deadzone_(0.1),
game_pad_stick_right_stick_deadzone_(0.1),
game_pad_stick_xy_scale_(0) {
game_pad_stick_left_stick_deadzone_(game_pad_stick_left_stick_deadzone_default_value),
game_pad_stick_right_stick_deadzone_(game_pad_stick_right_stick_deadzone_default_value),
game_pad_stick_xy_scale_(game_pad_stick_xy_scale_default_value) {
auto ignore_configured = false;
auto manipulate_caps_lock_led_configured = false;

Expand Down

0 comments on commit b439e8d

Please sign in to comment.