Merge "init: turn keychords into a standalone class"
This commit is contained in:
commit
420fe5fe90
3 changed files with 182 additions and 124 deletions
|
|
@ -749,12 +749,13 @@ int main(int argc, char** argv) {
|
||||||
am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");
|
am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");
|
||||||
am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");
|
am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");
|
||||||
am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict");
|
am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict");
|
||||||
|
Keychords keychords;
|
||||||
am.QueueBuiltinAction(
|
am.QueueBuiltinAction(
|
||||||
[&epoll](const BuiltinArguments& args) -> Result<Success> {
|
[&epoll, &keychords](const BuiltinArguments& args) -> Result<Success> {
|
||||||
for (const auto& svc : ServiceList::GetInstance()) {
|
for (const auto& svc : ServiceList::GetInstance()) {
|
||||||
svc->set_keychord_id(GetKeychordId(svc->keycodes()));
|
svc->set_keychord_id(keychords.GetId(svc->keycodes()));
|
||||||
}
|
}
|
||||||
KeychordInit(&epoll, HandleKeychord);
|
keychords.Start(&epoll, HandleKeychord);
|
||||||
return Success();
|
return Success();
|
||||||
},
|
},
|
||||||
"KeychordInit");
|
"KeychordInit");
|
||||||
|
|
|
||||||
|
|
@ -37,95 +37,85 @@
|
||||||
namespace android {
|
namespace android {
|
||||||
namespace init {
|
namespace init {
|
||||||
|
|
||||||
namespace {
|
Keychords::Keychords() : epoll_(nullptr), count_(0), inotify_fd_(-1) {}
|
||||||
|
|
||||||
int keychords_count;
|
Keychords::~Keychords() noexcept {
|
||||||
Epoll* epoll;
|
if (inotify_fd_ >= 0) {
|
||||||
std::function<void(int)> handle_keychord;
|
epoll_->UnregisterHandler(inotify_fd_);
|
||||||
|
::close(inotify_fd_);
|
||||||
struct KeychordEntry {
|
|
||||||
const std::vector<int> keycodes;
|
|
||||||
bool notified;
|
|
||||||
int id;
|
|
||||||
|
|
||||||
KeychordEntry(const std::vector<int>& keycodes, int id)
|
|
||||||
: keycodes(keycodes), notified(false), id(id) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<KeychordEntry> keychord_entries;
|
|
||||||
|
|
||||||
// Bit management
|
|
||||||
class KeychordMask {
|
|
||||||
private:
|
|
||||||
typedef unsigned int mask_t;
|
|
||||||
std::vector<mask_t> bits;
|
|
||||||
static constexpr size_t bits_per_byte = 8;
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit KeychordMask(size_t bit = 0) : bits((bit + sizeof(mask_t) - 1) / sizeof(mask_t), 0) {}
|
|
||||||
|
|
||||||
void SetBit(size_t bit, bool value = true) {
|
|
||||||
auto idx = bit / (bits_per_byte * sizeof(mask_t));
|
|
||||||
if (idx >= bits.size()) return;
|
|
||||||
if (value) {
|
|
||||||
bits[idx] |= mask_t(1) << (bit % (bits_per_byte * sizeof(mask_t)));
|
|
||||||
} else {
|
|
||||||
bits[idx] &= ~(mask_t(1) << (bit % (bits_per_byte * sizeof(mask_t))));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
while (!registration_.empty()) GeteventCloseDevice(registration_.begin()->first);
|
||||||
|
}
|
||||||
|
|
||||||
bool GetBit(size_t bit) const {
|
Keychords::Mask::Mask(size_t bit) : bits_((bit + sizeof(mask_t) - 1) / sizeof(mask_t), 0) {}
|
||||||
auto idx = bit / (bits_per_byte * sizeof(mask_t));
|
|
||||||
return bits[idx] & (mask_t(1) << (bit % (bits_per_byte * sizeof(mask_t))));
|
void Keychords::Mask::SetBit(size_t bit, bool value) {
|
||||||
|
auto idx = bit / (kBitsPerByte * sizeof(mask_t));
|
||||||
|
if (idx >= bits_.size()) return;
|
||||||
|
if (value) {
|
||||||
|
bits_[idx] |= mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t)));
|
||||||
|
} else {
|
||||||
|
bits_[idx] &= ~(mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t))));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
size_t bytesize() const { return bits.size() * sizeof(mask_t); }
|
bool Keychords::Mask::GetBit(size_t bit) const {
|
||||||
void* data() { return bits.data(); }
|
auto idx = bit / (kBitsPerByte * sizeof(mask_t));
|
||||||
size_t size() const { return bits.size() * sizeof(mask_t) * bits_per_byte; }
|
return bits_[idx] & (mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t))));
|
||||||
void resize(size_t bit) {
|
}
|
||||||
auto idx = bit / (bits_per_byte * sizeof(mask_t));
|
|
||||||
if (idx >= bits.size()) {
|
size_t Keychords::Mask::bytesize() const {
|
||||||
bits.resize(idx + 1, 0);
|
return bits_.size() * sizeof(mask_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* Keychords::Mask::data() {
|
||||||
|
return bits_.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Keychords::Mask::size() const {
|
||||||
|
return bits_.size() * sizeof(mask_t) * kBitsPerByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Keychords::Mask::resize(size_t bit) {
|
||||||
|
auto idx = bit / (kBitsPerByte * sizeof(mask_t));
|
||||||
|
if (idx >= bits_.size()) {
|
||||||
|
bits_.resize(idx + 1, 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
operator bool() const {
|
Keychords::Mask::operator bool() const {
|
||||||
for (size_t i = 0; i < bits.size(); ++i) {
|
for (size_t i = 0; i < bits_.size(); ++i) {
|
||||||
if (bits[i]) return true;
|
if (bits_[i]) return true;
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
KeychordMask operator&(const KeychordMask& rval) const {
|
Keychords::Mask Keychords::Mask::operator&(const Keychords::Mask& rval) const {
|
||||||
auto len = std::min(bits.size(), rval.bits.size());
|
auto len = std::min(bits_.size(), rval.bits_.size());
|
||||||
KeychordMask ret;
|
Keychords::Mask ret;
|
||||||
ret.bits.resize(len);
|
ret.bits_.resize(len);
|
||||||
for (size_t i = 0; i < len; ++i) {
|
for (size_t i = 0; i < len; ++i) {
|
||||||
ret.bits[i] = bits[i] & rval.bits[i];
|
ret.bits_[i] = bits_[i] & rval.bits_[i];
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
void operator|=(const KeychordMask& rval) {
|
void Keychords::Mask::operator|=(const Keychords::Mask& rval) {
|
||||||
size_t len = rval.bits.size();
|
auto len = rval.bits_.size();
|
||||||
bits.resize(len);
|
bits_.resize(len);
|
||||||
for (size_t i = 0; i < len; ++i) {
|
for (size_t i = 0; i < len; ++i) {
|
||||||
bits[i] |= rval.bits[i];
|
bits_[i] |= rval.bits_[i];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
KeychordMask keychord_current;
|
Keychords::Entry::Entry(const std::vector<int>& keycodes, int id)
|
||||||
|
: keycodes(keycodes), id(id), notified(false) {}
|
||||||
|
|
||||||
constexpr char kDevicePath[] = "/dev/input";
|
void Keychords::LambdaCheck() {
|
||||||
|
for (auto& e : entries_) {
|
||||||
std::map<std::string, int> keychord_registration;
|
auto found = true;
|
||||||
|
|
||||||
void KeychordLambdaCheck() {
|
|
||||||
for (auto& e : keychord_entries) {
|
|
||||||
bool found = true;
|
|
||||||
for (auto& code : e.keycodes) {
|
for (auto& code : e.keycodes) {
|
||||||
if (!keychord_current.GetBit(code)) {
|
if (!current_.GetBit(code)) {
|
||||||
e.notified = false;
|
e.notified = false;
|
||||||
found = false;
|
found = false;
|
||||||
break;
|
break;
|
||||||
|
|
@ -134,19 +124,19 @@ void KeychordLambdaCheck() {
|
||||||
if (!found) continue;
|
if (!found) continue;
|
||||||
if (e.notified) continue;
|
if (e.notified) continue;
|
||||||
e.notified = true;
|
e.notified = true;
|
||||||
handle_keychord(e.id);
|
handler_(e.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeychordLambdaHandler(int fd) {
|
void Keychords::LambdaHandler(int fd) {
|
||||||
input_event event;
|
input_event event;
|
||||||
auto res = TEMP_FAILURE_RETRY(::read(fd, &event, sizeof(event)));
|
auto res = TEMP_FAILURE_RETRY(::read(fd, &event, sizeof(event)));
|
||||||
if ((res != sizeof(event)) || (event.type != EV_KEY)) return;
|
if ((res != sizeof(event)) || (event.type != EV_KEY)) return;
|
||||||
keychord_current.SetBit(event.code, event.value);
|
current_.SetBit(event.code, event.value);
|
||||||
KeychordLambdaCheck();
|
LambdaCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KeychordGeteventEnable(int fd) {
|
bool Keychords::GeteventEnable(int fd) {
|
||||||
// Make sure it is an event channel, should pass this ioctl call
|
// Make sure it is an event channel, should pass this ioctl call
|
||||||
int version;
|
int version;
|
||||||
if (::ioctl(fd, EVIOCGVERSION, &version)) return false;
|
if (::ioctl(fd, EVIOCGVERSION, &version)) return false;
|
||||||
|
|
@ -154,7 +144,7 @@ bool KeychordGeteventEnable(int fd) {
|
||||||
#ifdef EVIOCSMASK
|
#ifdef EVIOCSMASK
|
||||||
static auto EviocsmaskSupported = true;
|
static auto EviocsmaskSupported = true;
|
||||||
if (EviocsmaskSupported) {
|
if (EviocsmaskSupported) {
|
||||||
KeychordMask mask(EV_KEY);
|
Keychords::Mask mask(EV_KEY);
|
||||||
mask.SetBit(EV_KEY);
|
mask.SetBit(EV_KEY);
|
||||||
input_mask msg = {};
|
input_mask msg = {};
|
||||||
msg.type = EV_SYN;
|
msg.type = EV_SYN;
|
||||||
|
|
@ -167,16 +157,16 @@ bool KeychordGeteventEnable(int fd) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
KeychordMask mask;
|
Keychords::Mask mask;
|
||||||
for (auto& e : keychord_entries) {
|
for (auto& e : entries_) {
|
||||||
for (auto& code : e.keycodes) {
|
for (auto& code : e.keycodes) {
|
||||||
mask.resize(code);
|
mask.resize(code);
|
||||||
mask.SetBit(code);
|
mask.SetBit(code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
keychord_current.resize(mask.size());
|
current_.resize(mask.size());
|
||||||
KeychordMask available(mask.size());
|
Keychords::Mask available(mask.size());
|
||||||
auto res = ::ioctl(fd, EVIOCGBIT(EV_KEY, available.bytesize()), available.data());
|
auto res = ::ioctl(fd, EVIOCGBIT(EV_KEY, available.bytesize()), available.data());
|
||||||
if (res == -1) return false;
|
if (res == -1) return false;
|
||||||
if (!(available & mask)) return false;
|
if (!(available & mask)) return false;
|
||||||
|
|
@ -191,45 +181,43 @@ bool KeychordGeteventEnable(int fd) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
KeychordMask set(mask.size());
|
Keychords::Mask set(mask.size());
|
||||||
res = ::ioctl(fd, EVIOCGKEY(res), set.data());
|
res = ::ioctl(fd, EVIOCGKEY(res), set.data());
|
||||||
if (res > 0) {
|
if (res > 0) {
|
||||||
keychord_current |= mask & available & set;
|
current_ |= mask & available & set;
|
||||||
KeychordLambdaCheck();
|
LambdaCheck();
|
||||||
}
|
}
|
||||||
epoll->RegisterHandler(fd, [fd]() { KeychordLambdaHandler(fd); });
|
epoll_->RegisterHandler(fd, [this, fd]() { this->LambdaHandler(fd); });
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GeteventOpenDevice(const std::string& device) {
|
void Keychords::GeteventOpenDevice(const std::string& device) {
|
||||||
if (keychord_registration.count(device)) return;
|
if (registration_.count(device)) return;
|
||||||
auto fd = TEMP_FAILURE_RETRY(::open(device.c_str(), O_RDWR | O_CLOEXEC));
|
auto fd = TEMP_FAILURE_RETRY(::open(device.c_str(), O_RDWR | O_CLOEXEC));
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
PLOG(ERROR) << "Can not open " << device;
|
PLOG(ERROR) << "Can not open " << device;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!KeychordGeteventEnable(fd)) {
|
if (!GeteventEnable(fd)) {
|
||||||
::close(fd);
|
::close(fd);
|
||||||
} else {
|
} else {
|
||||||
keychord_registration.emplace(device, fd);
|
registration_.emplace(device, fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GeteventCloseDevice(const std::string& device) {
|
void Keychords::GeteventCloseDevice(const std::string& device) {
|
||||||
auto it = keychord_registration.find(device);
|
auto it = registration_.find(device);
|
||||||
if (it == keychord_registration.end()) return;
|
if (it == registration_.end()) return;
|
||||||
auto fd = (*it).second;
|
auto fd = (*it).second;
|
||||||
epoll->UnregisterHandler(fd);
|
epoll_->UnregisterHandler(fd);
|
||||||
keychord_registration.erase(it);
|
registration_.erase(it);
|
||||||
::close(fd);
|
::close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int inotify_fd = -1;
|
void Keychords::InotifyHandler() {
|
||||||
|
unsigned char buf[512]; // History shows 32-64 bytes typical
|
||||||
|
|
||||||
void InotifyHandler() {
|
auto res = TEMP_FAILURE_RETRY(::read(inotify_fd_, buf, sizeof(buf)));
|
||||||
unsigned char buf[512];
|
|
||||||
|
|
||||||
auto res = TEMP_FAILURE_RETRY(::read(inotify_fd, buf, sizeof(buf)));
|
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
PLOG(WARNING) << "could not get event";
|
PLOG(WARNING) << "could not get event";
|
||||||
return;
|
return;
|
||||||
|
|
@ -255,14 +243,15 @@ void InotifyHandler() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GeteventOpenDevice() {
|
void Keychords::GeteventOpenDevice() {
|
||||||
inotify_fd = ::inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
inotify_fd_ = ::inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
||||||
if (inotify_fd < 0) {
|
if (inotify_fd_ < 0) {
|
||||||
PLOG(WARNING) << "Could not instantiate inotify for " << kDevicePath;
|
PLOG(WARNING) << "Could not instantiate inotify for " << kDevicePath;
|
||||||
} else if (::inotify_add_watch(inotify_fd, kDevicePath, IN_DELETE | IN_CREATE | IN_ONLYDIR) < 0) {
|
} else if (::inotify_add_watch(inotify_fd_, kDevicePath, IN_DELETE | IN_CREATE | IN_ONLYDIR) <
|
||||||
|
0) {
|
||||||
PLOG(WARNING) << "Could not add watch for " << kDevicePath;
|
PLOG(WARNING) << "Could not add watch for " << kDevicePath;
|
||||||
::close(inotify_fd);
|
::close(inotify_fd_);
|
||||||
inotify_fd = -1;
|
inotify_fd_ = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<DIR, decltype(&closedir)> device(opendir(kDevicePath), closedir);
|
std::unique_ptr<DIR, decltype(&closedir)> device(opendir(kDevicePath), closedir);
|
||||||
|
|
@ -277,22 +266,22 @@ void GeteventOpenDevice() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inotify_fd >= 0) epoll->RegisterHandler(inotify_fd, InotifyHandler);
|
if (inotify_fd_ >= 0) {
|
||||||
|
epoll_->RegisterHandler(inotify_fd_, [this]() { this->InotifyHandler(); });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
int Keychords::GetId(const std::vector<int>& keycodes) {
|
||||||
|
|
||||||
int GetKeychordId(const std::vector<int>& keycodes) {
|
|
||||||
if (keycodes.empty()) return 0;
|
if (keycodes.empty()) return 0;
|
||||||
++keychords_count;
|
++count_;
|
||||||
keychord_entries.emplace_back(KeychordEntry(keycodes, keychords_count));
|
entries_.emplace_back(Entry(keycodes, count_));
|
||||||
return keychords_count;
|
return count_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeychordInit(Epoll* init_epoll, std::function<void(int)> handler) {
|
void Keychords::Start(Epoll* epoll, std::function<void(int)> handler) {
|
||||||
epoll = init_epoll;
|
epoll_ = epoll;
|
||||||
handle_keychord = handler;
|
handler_ = handler;
|
||||||
if (keychords_count) GeteventOpenDevice();
|
if (count_) GeteventOpenDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace init
|
} // namespace init
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@
|
||||||
#define _INIT_KEYCHORDS_H_
|
#define _INIT_KEYCHORDS_H_
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "epoll.h"
|
#include "epoll.h"
|
||||||
|
|
@ -25,8 +27,74 @@
|
||||||
namespace android {
|
namespace android {
|
||||||
namespace init {
|
namespace init {
|
||||||
|
|
||||||
void KeychordInit(Epoll* init_epoll, std::function<void(int)> handler);
|
class Keychords {
|
||||||
int GetKeychordId(const std::vector<int>& keycodes);
|
public:
|
||||||
|
Keychords();
|
||||||
|
Keychords(const Keychords&) = delete;
|
||||||
|
Keychords(Keychords&&) = delete;
|
||||||
|
Keychords& operator=(const Keychords&) = delete;
|
||||||
|
Keychords& operator=(Keychords&&) = delete;
|
||||||
|
~Keychords() noexcept;
|
||||||
|
|
||||||
|
int GetId(const std::vector<int>& keycodes);
|
||||||
|
void Start(Epoll* epoll, std::function<void(int)> handler);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Bit management
|
||||||
|
class Mask {
|
||||||
|
public:
|
||||||
|
explicit Mask(size_t bit = 0);
|
||||||
|
|
||||||
|
void SetBit(size_t bit, bool value = true);
|
||||||
|
bool GetBit(size_t bit) const;
|
||||||
|
|
||||||
|
size_t bytesize() const;
|
||||||
|
void* data();
|
||||||
|
size_t size() const;
|
||||||
|
void resize(size_t bit);
|
||||||
|
|
||||||
|
operator bool() const;
|
||||||
|
Mask operator&(const Mask& rval) const;
|
||||||
|
void operator|=(const Mask& rval);
|
||||||
|
|
||||||
|
private:
|
||||||
|
typedef unsigned int mask_t;
|
||||||
|
static constexpr size_t kBitsPerByte = 8;
|
||||||
|
|
||||||
|
std::vector<mask_t> bits_;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Entry {
|
||||||
|
Entry(const std::vector<int>& keycodes, int id);
|
||||||
|
|
||||||
|
const std::vector<int> keycodes;
|
||||||
|
const int id;
|
||||||
|
bool notified;
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr char kDevicePath[] = "/dev/input";
|
||||||
|
|
||||||
|
void LambdaCheck();
|
||||||
|
void LambdaHandler(int fd);
|
||||||
|
void InotifyHandler();
|
||||||
|
|
||||||
|
bool GeteventEnable(int fd);
|
||||||
|
void GeteventOpenDevice(const std::string& device);
|
||||||
|
void GeteventOpenDevice();
|
||||||
|
void GeteventCloseDevice(const std::string& device);
|
||||||
|
|
||||||
|
Epoll* epoll_;
|
||||||
|
std::function<void(int)> handler_;
|
||||||
|
|
||||||
|
std::map<std::string, int> registration_;
|
||||||
|
|
||||||
|
int count_;
|
||||||
|
std::vector<Entry> entries_;
|
||||||
|
|
||||||
|
Mask current_;
|
||||||
|
|
||||||
|
int inotify_fd_;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace init
|
} // namespace init
|
||||||
} // namespace android
|
} // namespace android
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue