init: switch out keychord id with std::vector match of chords
Drop all references to keychord_id and id and instead use keycodes_
as the id. The keycodes are a std::vector<int> with an unique
sorted-order emplacement method added in the parser. Solves the
academic issue with duplicate keychords and trigger all services
that match rather than first match only.
Test: init_tests
Bug: 64114943
Change-Id: I5582779d81458fda393004c551c0d3c03d9471e0
diff --git a/init/keychords.cpp b/init/keychords.cpp
index 9aa8b2a..1af06dd 100644
--- a/init/keychords.cpp
+++ b/init/keychords.cpp
@@ -37,7 +37,7 @@
namespace android {
namespace init {
-Keychords::Keychords() : epoll_(nullptr), count_(0), inotify_fd_(-1) {}
+Keychords::Keychords() : epoll_(nullptr), inotify_fd_(-1) {}
Keychords::~Keychords() noexcept {
if (inotify_fd_ >= 0) {
@@ -108,23 +108,22 @@
}
}
-Keychords::Entry::Entry(const std::vector<int>& keycodes, int id)
- : keycodes(keycodes), id(id), notified(false) {}
+Keychords::Entry::Entry() : notified(false) {}
void Keychords::LambdaCheck() {
- for (auto& e : entries_) {
+ for (auto& [keycodes, entry] : entries_) {
auto found = true;
- for (auto& code : e.keycodes) {
+ for (auto& code : keycodes) {
if (!current_.GetBit(code)) {
- e.notified = false;
+ entry.notified = false;
found = false;
break;
}
}
if (!found) continue;
- if (e.notified) continue;
- e.notified = true;
- handler_(e.id);
+ if (entry.notified) continue;
+ entry.notified = true;
+ handler_(keycodes);
}
}
@@ -158,8 +157,8 @@
#endif
Keychords::Mask mask;
- for (auto& e : entries_) {
- for (auto& code : e.keycodes) {
+ for (auto& [keycodes, entry] : entries_) {
+ for (auto& code : keycodes) {
mask.resize(code);
mask.SetBit(code);
}
@@ -271,17 +270,15 @@
}
}
-int Keychords::GetId(const std::vector<int>& keycodes) {
- if (keycodes.empty()) return 0;
- ++count_;
- entries_.emplace_back(Entry(keycodes, count_));
- return count_;
+void Keychords::Register(const std::vector<int>& keycodes) {
+ if (keycodes.empty()) return;
+ entries_.try_emplace(keycodes, Entry());
}
-void Keychords::Start(Epoll* epoll, std::function<void(int)> handler) {
+void Keychords::Start(Epoll* epoll, std::function<void(const std::vector<int>&)> handler) {
epoll_ = epoll;
handler_ = handler;
- if (count_) GeteventOpenDevice();
+ if (entries_.size()) GeteventOpenDevice();
}
} // namespace init