Skip to content

Commit

Permalink
fix event_queue sort
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jul 6, 2017
1 parent c54000d commit 71e0aa9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
24 changes: 16 additions & 8 deletions src/share/event_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ class event_queue final {
time_stamp_delay_ += value;
}

static bool compare(const queued_event& v1, const queued_event& v2) {
static bool needs_swap(const queued_event& v1, const queued_event& v2) {
// Some devices are send modifier flag and key at the same HID report.
// For example, a key sends control+up-arrow by this reports.
//
Expand Down Expand Up @@ -510,10 +510,10 @@ class event_queue final {
modifier_flag2 != modifier_flag::zero) {
// v2 is modifier_flag
if (v2.get_event_type() == event_type::key_up) {
return true;
return false;
} else {
// reorder to v2,v1 if v2 is pressed.
return false;
return true;
}
}

Expand All @@ -522,21 +522,29 @@ class event_queue final {
// v1 is modifier_flag
if (v1.get_event_type() == event_type::key_up) {
// reorder to v2,v1 if v1 is released.
return false;
} else {
return true;
} else {
return false;
}
}
}
}

// keep order
return v1.get_time_stamp() < v2.get_time_stamp();
return false;
}

private:
void sort_events(void) {
std::stable_sort(std::begin(events_), std::end(events_), event_queue::compare);
for (size_t i = 0; i < events_.size() - 1;) {
if (needs_swap(events_[i], events_[i + 1])) {
std::swap(events_[i], events_[i + 1]);
if (i > 0) {
--i;
}
continue;
}
++i;
}
}

std::vector<queued_event> events_;
Expand Down
25 changes: 12 additions & 13 deletions tests/src/event_queue/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ TEST_CASE("emplace_back_event") {
}
}

TEST_CASE("compare") {
TEST_CASE("needs_swap") {
krbn::event_queue::queued_event spacebar_down(krbn::device_id(1),
100,
spacebar_event,
Expand All @@ -183,22 +183,21 @@ TEST_CASE("compare") {
krbn::event_type::key_up,
right_shift_event);

REQUIRE(krbn::event_queue::compare(spacebar_down, spacebar_down) == false);
REQUIRE(krbn::event_queue::needs_swap(spacebar_down, spacebar_down) == false);
REQUIRE(krbn::event_queue::needs_swap(spacebar_down, escape_down) == false);
REQUIRE(krbn::event_queue::needs_swap(escape_down, spacebar_down) == false);

REQUIRE(krbn::event_queue::compare(spacebar_down, escape_down) == true);
REQUIRE(krbn::event_queue::compare(escape_down, spacebar_down) == false);
REQUIRE(krbn::event_queue::needs_swap(spacebar_down, right_shift_down) == true);
REQUIRE(krbn::event_queue::needs_swap(right_shift_down, spacebar_down) == false);

REQUIRE(krbn::event_queue::compare(spacebar_down, right_shift_down) == false);
REQUIRE(krbn::event_queue::compare(right_shift_down, spacebar_down) == true);
REQUIRE(krbn::event_queue::needs_swap(spacebar_down, right_shift_up) == false);
REQUIRE(krbn::event_queue::needs_swap(right_shift_up, spacebar_down) == false);

REQUIRE(krbn::event_queue::compare(spacebar_down, right_shift_up) == true);
REQUIRE(krbn::event_queue::compare(right_shift_up, spacebar_down) == false);
REQUIRE(krbn::event_queue::needs_swap(spacebar_up, right_shift_up) == false);
REQUIRE(krbn::event_queue::needs_swap(right_shift_up, spacebar_up) == true);

REQUIRE(krbn::event_queue::compare(spacebar_up, right_shift_up) == true);
REQUIRE(krbn::event_queue::compare(right_shift_up, spacebar_up) == false);

REQUIRE(krbn::event_queue::compare(spacebar_up, right_shift_down) == false);
REQUIRE(krbn::event_queue::compare(right_shift_down, spacebar_up) == true);
REQUIRE(krbn::event_queue::needs_swap(spacebar_up, right_shift_down) == false);
REQUIRE(krbn::event_queue::needs_swap(right_shift_down, spacebar_up) == false);
}

TEST_CASE("emplace_back_event.usage_page") {
Expand Down

0 comments on commit 71e0aa9

Please sign in to comment.