Track all input devices with KEY_VOLUMEUP

This should in theory should support more devices for detecting the
volume up press on boot.

Close #1346
This commit is contained in:
topjohnwu 2019-04-21 19:09:08 -04:00
parent f315c4416b
commit f2b52755d6

View File

@ -197,43 +197,50 @@ static inline void parse_cmdline(const std::function<void (std::string_view, con
static bool check_key_combo() { static bool check_key_combo() {
uint8_t bitmask[(KEY_MAX + 1) / 8]; uint8_t bitmask[(KEY_MAX + 1) / 8];
int eventfd = -1; vector<int> events;
constexpr const char *name = "/event";
for (int minor = 64; minor < 96; ++minor) { for (int minor = 64; minor < 96; ++minor) {
if (mknod("/event", S_IFCHR | 0444, makedev(13, minor))) { if (mknod(name, S_IFCHR | 0444, makedev(13, minor))) {
PLOGE("mknod"); PLOGE("mknod");
continue; continue;
} }
eventfd = xopen("/event", O_RDWR | O_CLOEXEC); int fd = open(name, O_RDONLY | O_CLOEXEC);
unlink("/event"); unlink(name);
if (eventfd < 0) if (fd < 0)
continue; continue;
memset(bitmask, 0, sizeof(bitmask)); memset(bitmask, 0, sizeof(bitmask));
ioctl(eventfd, EVIOCGBIT(EV_KEY, sizeof(bitmask)), bitmask); ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bitmask)), bitmask);
if (test_bit(KEY_POWER, bitmask) && test_bit(KEY_VOLUMEUP, bitmask)) { if (test_bit(KEY_VOLUMEUP, bitmask))
// Check KEY_POWER because KEY_VOLUMEUP could be headphone input events.push_back(fd);
break;
}
} }
if (eventfd < 0) if (events.empty())
return false; return false;
RunFinally fin([&]() -> void {
for (const int &fd : events)
close(fd);
});
// Return true if volume key up is hold for more than 3 seconds // Return true if volume key up is hold for more than 3 seconds
int count = 0; int count = 0;
for (int i = 0; i < 500; ++i) { for (int i = 0; i < 500; ++i) {
for (const int &fd : events) {
memset(bitmask, 0, sizeof(bitmask)); memset(bitmask, 0, sizeof(bitmask));
ioctl(eventfd, EVIOCGKEY(sizeof(bitmask)), bitmask); ioctl(fd, EVIOCGKEY(sizeof(bitmask)), bitmask);
count = test_bit(KEY_VOLUMEUP, bitmask) ? count + 1 : 0; if (test_bit(KEY_VOLUMEUP, bitmask)) {
count++;
break;
}
}
if (count >= 300) { if (count >= 300) {
LOGD("KEY_VOLUMEUP detected: disable system-as-root\n"); LOGD("KEY_VOLUMEUP detected: disable system-as-root\n");
close(eventfd);
return true; return true;
} }
// Check every 10ms // Check every 10ms
usleep(10000); usleep(10000);
} }
close(eventfd);
return false; return false;
} }