Skip to content

Commit

Permalink
Change generic_desktop::hat_switch, generic_desktop::z, generic_deskt…
Browse files Browse the repository at this point in the history
…op::rz only if game_pad
  • Loading branch information
tekezo committed Sep 14, 2023
1 parent 3956097 commit cb5f811
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 34 deletions.
72 changes: 38 additions & 34 deletions src/share/event_queue/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,40 +94,6 @@ static inline std::shared_ptr<queue> make_queue(const device_properties& device_
pointing_motion_time_stamp = v.get_time_stamp();
pointing_motion_horizontal_wheel = static_cast<int>(v.get_integer_value());

} else if (v.conforms_to(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::hat_switch)) {
// Convert hat switch to dpad.
auto pairs = hat_switch_converter::get_global_hat_switch_converter()->to_dpad_events(device_properties.get_device_id(),
v.get_integer_value());
for (const auto& pair : pairs) {
event_queue::event event(pair.first);
result->emplace_back_entry(device_properties.get_device_id(),
event_time_stamp(v.get_time_stamp()),
event,
pair.second,
event,
event_origin,
state::original);
}

} else if (v.conforms_to(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::rz)) {
// Convert vertical wheel
if (pointing_motion_vertical_wheel) {
emplace_back_pointing_motion_event();
}
pointing_motion_time_stamp = v.get_time_stamp();
pointing_motion_vertical_wheel = static_cast<int>(v.get_integer_value());

} else if (v.conforms_to(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::z)) {
// Convert horizontal wheel
if (pointing_motion_horizontal_wheel) {
emplace_back_pointing_motion_event();
}
pointing_motion_time_stamp = v.get_time_stamp();
pointing_motion_horizontal_wheel = -static_cast<int>(v.get_integer_value());

} else if (v.conforms_to(pqrs::hid::usage_page::leds,
pqrs::hid::usage::led::caps_lock)) {
auto event = event_queue::event::make_caps_lock_state_changed_event(v.get_integer_value());
Expand All @@ -138,6 +104,44 @@ static inline std::shared_ptr<queue> make_queue(const device_properties& device_
event,
event_origin,
state::virtual_event);

} else if (auto is_game_pad = device_properties.get_is_game_pad()) {
if (*is_game_pad) {
if (v.conforms_to(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::hat_switch)) {
// Convert hat switch to dpad.
auto pairs = hat_switch_converter::get_global_hat_switch_converter()->to_dpad_events(device_properties.get_device_id(),
v.get_integer_value());
for (const auto& pair : pairs) {
event_queue::event event(pair.first);
result->emplace_back_entry(device_properties.get_device_id(),
event_time_stamp(v.get_time_stamp()),
event,
pair.second,
event,
event_origin,
state::original);
}

} else if (v.conforms_to(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::rz)) {
// Convert vertical wheel
if (pointing_motion_vertical_wheel) {
emplace_back_pointing_motion_event();
}
pointing_motion_time_stamp = v.get_time_stamp();
pointing_motion_vertical_wheel = static_cast<int>(v.get_integer_value());

} else if (v.conforms_to(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::z)) {
// Convert horizontal wheel
if (pointing_motion_horizontal_wheel) {
emplace_back_pointing_motion_event();
}
pointing_motion_time_stamp = v.get_time_stamp();
pointing_motion_horizontal_wheel = -static_cast<int>(v.get_integer_value());
}
}
}
}
}
Expand Down
106 changes: 106 additions & 0 deletions tests/src/event_queue/src/event_queue_utility_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,112 @@ void run_event_queue_utility_test(void) {
}
};

"utility::make_queue not game_pad"_test = [] {
std::vector<pqrs::osx::iokit_hid_value> hid_values;

hid_values.emplace_back(pqrs::osx::iokit_hid_value(krbn::absolute_time_point(1000),
1,
pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::hat_switch));
hid_values.emplace_back(pqrs::osx::iokit_hid_value(krbn::absolute_time_point(1001),
-1,
pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::hat_switch));

hid_values.emplace_back(pqrs::osx::iokit_hid_value(krbn::absolute_time_point(2000),
10,
pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::z));

hid_values.emplace_back(pqrs::osx::iokit_hid_value(krbn::absolute_time_point(3000),
10,
pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::rz));

auto device_properties = krbn::device_properties(krbn::device_id(1),
nullptr);
auto queue = krbn::event_queue::utility::make_queue(device_properties,
hid_values,
krbn::event_origin::grabbed_device);
expect(0_ul == queue->get_entries().size());
};

"utility::make_queue game_pad"_test = [] {
std::vector<pqrs::osx::iokit_hid_value> hid_values;

hid_values.emplace_back(pqrs::osx::iokit_hid_value(krbn::absolute_time_point(1000),
1,
pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::hat_switch));
hid_values.emplace_back(pqrs::osx::iokit_hid_value(krbn::absolute_time_point(1001),
-1,
pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::hat_switch));

hid_values.emplace_back(pqrs::osx::iokit_hid_value(krbn::absolute_time_point(2000),
10,
pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::z));

hid_values.emplace_back(pqrs::osx::iokit_hid_value(krbn::absolute_time_point(3000),
20,
pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::rz));

auto device_properties = krbn::device_properties(krbn::device_id(1),
nullptr);
device_properties.set_is_game_pad(true);

auto queue = krbn::event_queue::utility::make_queue(device_properties,
hid_values,
krbn::event_origin::grabbed_device);
expect(5_ul == queue->get_entries().size());

{
auto& e = queue->get_entries()[0];
expect(e.get_device_id() == krbn::device_id(1));
expect(e.get_event_time_stamp().get_time_stamp() == krbn::absolute_time_point(1000));
expect(e.get_event().get_if<krbn::momentary_switch_event>()->get_usage_pair() ==
pqrs::hid::usage_pair(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::dpad_up));
expect(e.get_event_type() == krbn::event_type::key_down);
}
{
auto& e = queue->get_entries()[1];
expect(e.get_device_id() == krbn::device_id(1));
expect(e.get_event_time_stamp().get_time_stamp() == krbn::absolute_time_point(1000));
expect(e.get_event().get_if<krbn::momentary_switch_event>()->get_usage_pair() ==
pqrs::hid::usage_pair(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::dpad_right));
expect(e.get_event_type() == krbn::event_type::key_down);
}
{
auto& e = queue->get_entries()[2];
expect(e.get_device_id() == krbn::device_id(1));
expect(e.get_event_time_stamp().get_time_stamp() == krbn::absolute_time_point(1001));
expect(e.get_event().get_if<krbn::momentary_switch_event>()->get_usage_pair() ==
pqrs::hid::usage_pair(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::dpad_up));
expect(e.get_event_type() == krbn::event_type::key_up);
}
{
auto& e = queue->get_entries()[3];
expect(e.get_device_id() == krbn::device_id(1));
expect(e.get_event_time_stamp().get_time_stamp() == krbn::absolute_time_point(1001));
expect(e.get_event().get_if<krbn::momentary_switch_event>()->get_usage_pair() ==
pqrs::hid::usage_pair(pqrs::hid::usage_page::generic_desktop,
pqrs::hid::usage::generic_desktop::dpad_right));
expect(e.get_event_type() == krbn::event_type::key_up);
}
{
auto& e = queue->get_entries()[4];
expect(e.get_device_id() == krbn::device_id(1));
expect(e.get_event_time_stamp().get_time_stamp() == krbn::absolute_time_point(3000));
expect(e.get_event().get_pointing_motion() == krbn::pointing_motion(0, 0, 20, -10));
expect(e.get_event_type() == krbn::event_type::single);
}
};

"utility::insert_device_keys_and_pointing_buttons_are_released_event"_test = [] {
krbn::event_queue::event a_event(
krbn::momentary_switch_event(pqrs::hid::usage_page::keyboard_or_keypad,
Expand Down

0 comments on commit cb5f811

Please sign in to comment.