init: refactor keychord for testing
Move things around so that keychords.cpp is independent of service
and init and can be individually tested with few dependencies.
Permits also rolling out the keychords as a class in a future commit.
Improve parser checking.
Test: init_tests
Bug: 64114943
Change-Id: I82570bc6269ed478db784ec38a8bc916da2be2be
diff --git a/init/service.cpp b/init/service.cpp
index 0e08d9b..5778a93 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -18,6 +18,7 @@
#include <fcntl.h>
#include <inttypes.h>
+#include <linux/input.h>
#include <linux/securebits.h>
#include <sched.h>
#include <sys/mount.h>
@@ -544,10 +545,13 @@
Result<Success> Service::ParseKeycodes(const std::vector<std::string>& args) {
for (std::size_t i = 1; i < args.size(); i++) {
int code;
- if (ParseInt(args[i], &code)) {
+ if (ParseInt(args[i], &code, 0, KEY_MAX)) {
+ for (auto& key : keycodes_) {
+ if (key == code) return Error() << "duplicate keycode: " << args[i];
+ }
keycodes_.emplace_back(code);
} else {
- LOG(WARNING) << "ignoring invalid keycode: " << args[i];
+ return Error() << "invalid keycode: " << args[i];
}
}
return Success();