summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTreehugger Robot2018-05-29 18:10:25 -0500
committerGerrit Code Review2018-05-29 18:10:25 -0500
commit420fe5fe9010081cbe61774ac661c0d5e7f09114 (patch)
tree8f3b41c142e3f89ee504b1394dcbe7b092ac50cd
parent16dd3582b714303613eb3ed6f7ad3b5788aabe92 (diff)
parent06aeb41c16cb26e0f53cb964e657fe528437bb4d (diff)
downloadplatform-system-core-420fe5fe9010081cbe61774ac661c0d5e7f09114.tar.gz
platform-system-core-420fe5fe9010081cbe61774ac661c0d5e7f09114.tar.xz
platform-system-core-420fe5fe9010081cbe61774ac661c0d5e7f09114.zip
Merge "init: turn keychords into a standalone class"
-rw-r--r--init/init.cpp7
-rw-r--r--init/keychords.cpp229
-rw-r--r--init/keychords.h72
3 files changed, 183 insertions, 125 deletions
diff --git a/init/init.cpp b/init/init.cpp
index 43242b2dc..5652c5e51 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -749,12 +749,13 @@ int main(int argc, char** argv) {
749 am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng"); 749 am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");
750 am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits"); 750 am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");
751 am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict"); 751 am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict");
752 Keychords keychords;
752 am.QueueBuiltinAction( 753 am.QueueBuiltinAction(
753 [&epoll](const BuiltinArguments& args) -> Result<Success> { 754 [&epoll, &keychords](const BuiltinArguments& args) -> Result<Success> {
754 for (const auto& svc : ServiceList::GetInstance()) { 755 for (const auto& svc : ServiceList::GetInstance()) {
755 svc->set_keychord_id(GetKeychordId(svc->keycodes())); 756 svc->set_keychord_id(keychords.GetId(svc->keycodes()));
756 } 757 }
757 KeychordInit(&epoll, HandleKeychord); 758 keychords.Start(&epoll, HandleKeychord);
758 return Success(); 759 return Success();
759 }, 760 },
760 "KeychordInit"); 761 "KeychordInit");
diff --git a/init/keychords.cpp b/init/keychords.cpp
index 1a8f2aea3..9aa8b2a78 100644
--- a/init/keychords.cpp
+++ b/init/keychords.cpp
@@ -37,95 +37,85 @@
37namespace android { 37namespace android {
38namespace init { 38namespace init {
39 39
40namespace { 40Keychords::Keychords() : epoll_(nullptr), count_(0), inotify_fd_(-1) {}
41
42int keychords_count;
43Epoll* epoll;
44std::function<void(int)> handle_keychord;
45
46struct KeychordEntry {
47 const std::vector<int> keycodes;
48 bool notified;
49 int id;
50
51 KeychordEntry(const std::vector<int>& keycodes, int id)
52 : keycodes(keycodes), notified(false), id(id) {}
53};
54
55std::vector<KeychordEntry> keychord_entries;
56
57// Bit management
58class KeychordMask {
59 private:
60 typedef unsigned int mask_t;
61 std::vector<mask_t> bits;
62 static constexpr size_t bits_per_byte = 8;
63
64 public:
65 explicit KeychordMask(size_t bit = 0) : bits((bit + sizeof(mask_t) - 1) / sizeof(mask_t), 0) {}
66
67 void SetBit(size_t bit, bool value = true) {
68 auto idx = bit / (bits_per_byte * sizeof(mask_t));
69 if (idx >= bits.size()) return;
70 if (value) {
71 bits[idx] |= mask_t(1) << (bit % (bits_per_byte * sizeof(mask_t)));
72 } else {
73 bits[idx] &= ~(mask_t(1) << (bit % (bits_per_byte * sizeof(mask_t))));
74 }
75 }
76 41
77 bool GetBit(size_t bit) const { 42Keychords::~Keychords() noexcept {
78 auto idx = bit / (bits_per_byte * sizeof(mask_t)); 43 if (inotify_fd_ >= 0) {
79 return bits[idx] & (mask_t(1) << (bit % (bits_per_byte * sizeof(mask_t)))); 44 epoll_->UnregisterHandler(inotify_fd_);
45 ::close(inotify_fd_);
80 } 46 }
47 while (!registration_.empty()) GeteventCloseDevice(registration_.begin()->first);
48}
81 49
82 size_t bytesize() const { return bits.size() * sizeof(mask_t); } 50Keychords::Mask::Mask(size_t bit) : bits_((bit + sizeof(mask_t) - 1) / sizeof(mask_t), 0) {}
83 void* data() { return bits.data(); }
84 size_t size() const { return bits.size() * sizeof(mask_t) * bits_per_byte; }
85 void resize(size_t bit) {
86 auto idx = bit / (bits_per_byte * sizeof(mask_t));
87 if (idx >= bits.size()) {
88 bits.resize(idx + 1, 0);
89 }
90 }
91 51
92 operator bool() const { 52void Keychords::Mask::SetBit(size_t bit, bool value) {
93 for (size_t i = 0; i < bits.size(); ++i) { 53 auto idx = bit / (kBitsPerByte * sizeof(mask_t));
94 if (bits[i]) return true; 54 if (idx >= bits_.size()) return;
95 } 55 if (value) {
96 return false; 56 bits_[idx] |= mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t)));
57 } else {
58 bits_[idx] &= ~(mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t))));
97 } 59 }
60}
98 61
99 KeychordMask operator&(const KeychordMask& rval) const { 62bool Keychords::Mask::GetBit(size_t bit) const {
100 auto len = std::min(bits.size(), rval.bits.size()); 63 auto idx = bit / (kBitsPerByte * sizeof(mask_t));
101 KeychordMask ret; 64 return bits_[idx] & (mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t))));
102 ret.bits.resize(len); 65}
103 for (size_t i = 0; i < len; ++i) { 66
104 ret.bits[i] = bits[i] & rval.bits[i]; 67size_t Keychords::Mask::bytesize() const {
105 } 68 return bits_.size() * sizeof(mask_t);
106 return ret; 69}
70
71void* Keychords::Mask::data() {
72 return bits_.data();
73}
74
75size_t Keychords::Mask::size() const {
76 return bits_.size() * sizeof(mask_t) * kBitsPerByte;
77}
78
79void Keychords::Mask::resize(size_t bit) {
80 auto idx = bit / (kBitsPerByte * sizeof(mask_t));
81 if (idx >= bits_.size()) {
82 bits_.resize(idx + 1, 0);
107 } 83 }
84}
108 85
109 void operator|=(const KeychordMask& rval) { 86Keychords::Mask::operator bool() const {
110 size_t len = rval.bits.size(); 87 for (size_t i = 0; i < bits_.size(); ++i) {
111 bits.resize(len); 88 if (bits_[i]) return true;
112 for (size_t i = 0; i < len; ++i) {
113 bits[i] |= rval.bits[i];
114 }
115 } 89 }
116}; 90 return false;
91}
117 92
118KeychordMask keychord_current; 93Keychords::Mask Keychords::Mask::operator&(const Keychords::Mask& rval) const {
94 auto len = std::min(bits_.size(), rval.bits_.size());
95 Keychords::Mask ret;
96 ret.bits_.resize(len);
97 for (size_t i = 0; i < len; ++i) {
98 ret.bits_[i] = bits_[i] & rval.bits_[i];
99 }
100 return ret;
101}
119 102
120constexpr char kDevicePath[] = "/dev/input"; 103void Keychords::Mask::operator|=(const Keychords::Mask& rval) {
104 auto len = rval.bits_.size();
105 bits_.resize(len);
106 for (size_t i = 0; i < len; ++i) {
107 bits_[i] |= rval.bits_[i];
108 }
109}
121 110
122std::map<std::string, int> keychord_registration; 111Keychords::Entry::Entry(const std::vector<int>& keycodes, int id)
112 : keycodes(keycodes), id(id), notified(false) {}
123 113
124void KeychordLambdaCheck() { 114void Keychords::LambdaCheck() {
125 for (auto& e : keychord_entries) { 115 for (auto& e : entries_) {
126 bool found = true; 116 auto found = true;
127 for (auto& code : e.keycodes) { 117 for (auto& code : e.keycodes) {
128 if (!keychord_current.GetBit(code)) { 118 if (!current_.GetBit(code)) {
129 e.notified = false; 119 e.notified = false;
130 found = false; 120 found = false;
131 break; 121 break;
@@ -134,19 +124,19 @@ void KeychordLambdaCheck() {
134 if (!found) continue; 124 if (!found) continue;
135 if (e.notified) continue; 125 if (e.notified) continue;
136 e.notified = true; 126 e.notified = true;
137 handle_keychord(e.id); 127 handler_(e.id);
138 } 128 }
139} 129}
140 130
141void KeychordLambdaHandler(int fd) { 131void Keychords::LambdaHandler(int fd) {
142 input_event event; 132 input_event event;
143 auto res = TEMP_FAILURE_RETRY(::read(fd, &event, sizeof(event))); 133 auto res = TEMP_FAILURE_RETRY(::read(fd, &event, sizeof(event)));
144 if ((res != sizeof(event)) || (event.type != EV_KEY)) return; 134 if ((res != sizeof(event)) || (event.type != EV_KEY)) return;
145 keychord_current.SetBit(event.code, event.value); 135 current_.SetBit(event.code, event.value);
146 KeychordLambdaCheck(); 136 LambdaCheck();
147} 137}
148 138
149bool KeychordGeteventEnable(int fd) { 139bool Keychords::GeteventEnable(int fd) {
150 // Make sure it is an event channel, should pass this ioctl call 140 // Make sure it is an event channel, should pass this ioctl call
151 int version; 141 int version;
152 if (::ioctl(fd, EVIOCGVERSION, &version)) return false; 142 if (::ioctl(fd, EVIOCGVERSION, &version)) return false;
@@ -154,7 +144,7 @@ bool KeychordGeteventEnable(int fd) {
154#ifdef EVIOCSMASK 144#ifdef EVIOCSMASK
155 static auto EviocsmaskSupported = true; 145 static auto EviocsmaskSupported = true;
156 if (EviocsmaskSupported) { 146 if (EviocsmaskSupported) {
157 KeychordMask mask(EV_KEY); 147 Keychords::Mask mask(EV_KEY);
158 mask.SetBit(EV_KEY); 148 mask.SetBit(EV_KEY);
159 input_mask msg = {}; 149 input_mask msg = {};
160 msg.type = EV_SYN; 150 msg.type = EV_SYN;
@@ -167,16 +157,16 @@ bool KeychordGeteventEnable(int fd) {
167 } 157 }
168#endif 158#endif
169 159
170 KeychordMask mask; 160 Keychords::Mask mask;
171 for (auto& e : keychord_entries) { 161 for (auto& e : entries_) {
172 for (auto& code : e.keycodes) { 162 for (auto& code : e.keycodes) {
173 mask.resize(code); 163 mask.resize(code);
174 mask.SetBit(code); 164 mask.SetBit(code);
175 } 165 }
176 } 166 }
177 167
178 keychord_current.resize(mask.size()); 168 current_.resize(mask.size());
179 KeychordMask available(mask.size()); 169 Keychords::Mask available(mask.size());
180 auto res = ::ioctl(fd, EVIOCGBIT(EV_KEY, available.bytesize()), available.data()); 170 auto res = ::ioctl(fd, EVIOCGBIT(EV_KEY, available.bytesize()), available.data());
181 if (res == -1) return false; 171 if (res == -1) return false;
182 if (!(available & mask)) return false; 172 if (!(available & mask)) return false;
@@ -191,45 +181,43 @@ bool KeychordGeteventEnable(int fd) {
191 } 181 }
192#endif 182#endif
193 183
194 KeychordMask set(mask.size()); 184 Keychords::Mask set(mask.size());
195 res = ::ioctl(fd, EVIOCGKEY(res), set.data()); 185 res = ::ioctl(fd, EVIOCGKEY(res), set.data());
196 if (res > 0) { 186 if (res > 0) {
197 keychord_current |= mask & available & set; 187 current_ |= mask & available & set;
198 KeychordLambdaCheck(); 188 LambdaCheck();
199 } 189 }
200 epoll->RegisterHandler(fd, [fd]() { KeychordLambdaHandler(fd); }); 190 epoll_->RegisterHandler(fd, [this, fd]() { this->LambdaHandler(fd); });
201 return true; 191 return true;
202} 192}
203 193
204void GeteventOpenDevice(const std::string& device) { 194void Keychords::GeteventOpenDevice(const std::string& device) {
205 if (keychord_registration.count(device)) return; 195 if (registration_.count(device)) return;
206 auto fd = TEMP_FAILURE_RETRY(::open(device.c_str(), O_RDWR | O_CLOEXEC)); 196 auto fd = TEMP_FAILURE_RETRY(::open(device.c_str(), O_RDWR | O_CLOEXEC));
207 if (fd == -1) { 197 if (fd == -1) {
208 PLOG(ERROR) << "Can not open " << device; 198 PLOG(ERROR) << "Can not open " << device;
209 return; 199 return;
210 } 200 }
211 if (!KeychordGeteventEnable(fd)) { 201 if (!GeteventEnable(fd)) {
212 ::close(fd); 202 ::close(fd);
213 } else { 203 } else {
214 keychord_registration.emplace(device, fd); 204 registration_.emplace(device, fd);
215 } 205 }
216} 206}
217 207
218void GeteventCloseDevice(const std::string& device) { 208void Keychords::GeteventCloseDevice(const std::string& device) {
219 auto it = keychord_registration.find(device); 209 auto it = registration_.find(device);
220 if (it == keychord_registration.end()) return; 210 if (it == registration_.end()) return;
221 auto fd = (*it).second; 211 auto fd = (*it).second;
222 epoll->UnregisterHandler(fd); 212 epoll_->UnregisterHandler(fd);
223 keychord_registration.erase(it); 213 registration_.erase(it);
224 ::close(fd); 214 ::close(fd);
225} 215}
226 216
227int inotify_fd = -1; 217void Keychords::InotifyHandler() {
218 unsigned char buf[512]; // History shows 32-64 bytes typical
228 219
229void InotifyHandler() { 220 auto res = TEMP_FAILURE_RETRY(::read(inotify_fd_, buf, sizeof(buf)));
230 unsigned char buf[512];
231
232 auto res = TEMP_FAILURE_RETRY(::read(inotify_fd, buf, sizeof(buf)));
233 if (res < 0) { 221 if (res < 0) {
234 PLOG(WARNING) << "could not get event"; 222 PLOG(WARNING) << "could not get event";
235 return; 223 return;
@@ -255,14 +243,15 @@ void InotifyHandler() {
255 } 243 }
256} 244}
257 245
258void GeteventOpenDevice() { 246void Keychords::GeteventOpenDevice() {
259 inotify_fd = ::inotify_init1(IN_NONBLOCK | IN_CLOEXEC); 247 inotify_fd_ = ::inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
260 if (inotify_fd < 0) { 248 if (inotify_fd_ < 0) {
261 PLOG(WARNING) << "Could not instantiate inotify for " << kDevicePath; 249 PLOG(WARNING) << "Could not instantiate inotify for " << kDevicePath;
262 } else if (::inotify_add_watch(inotify_fd, kDevicePath, IN_DELETE | IN_CREATE | IN_ONLYDIR) < 0) { 250 } else if (::inotify_add_watch(inotify_fd_, kDevicePath, IN_DELETE | IN_CREATE | IN_ONLYDIR) <
251 0) {
263 PLOG(WARNING) << "Could not add watch for " << kDevicePath; 252 PLOG(WARNING) << "Could not add watch for " << kDevicePath;
264 ::close(inotify_fd); 253 ::close(inotify_fd_);
265 inotify_fd = -1; 254 inotify_fd_ = -1;
266 } 255 }
267 256
268 std::unique_ptr<DIR, decltype(&closedir)> device(opendir(kDevicePath), closedir); 257 std::unique_ptr<DIR, decltype(&closedir)> device(opendir(kDevicePath), closedir);
@@ -277,22 +266,22 @@ void GeteventOpenDevice() {
277 } 266 }
278 } 267 }
279 268
280 if (inotify_fd >= 0) epoll->RegisterHandler(inotify_fd, InotifyHandler); 269 if (inotify_fd_ >= 0) {
270 epoll_->RegisterHandler(inotify_fd_, [this]() { this->InotifyHandler(); });
271 }
281} 272}
282 273
283} // namespace 274int Keychords::GetId(const std::vector<int>& keycodes) {
284
285int GetKeychordId(const std::vector<int>& keycodes) {
286 if (keycodes.empty()) return 0; 275 if (keycodes.empty()) return 0;
287 ++keychords_count; 276 ++count_;
288 keychord_entries.emplace_back(KeychordEntry(keycodes, keychords_count)); 277 entries_.emplace_back(Entry(keycodes, count_));
289 return keychords_count; 278 return count_;
290} 279}
291 280
292void KeychordInit(Epoll* init_epoll, std::function<void(int)> handler) { 281void Keychords::Start(Epoll* epoll, std::function<void(int)> handler) {
293 epoll = init_epoll; 282 epoll_ = epoll;
294 handle_keychord = handler; 283 handler_ = handler;
295 if (keychords_count) GeteventOpenDevice(); 284 if (count_) GeteventOpenDevice();
296} 285}
297 286
298} // namespace init 287} // namespace init
diff --git a/init/keychords.h b/init/keychords.h
index f273c8c2e..74a91951b 100644
--- a/init/keychords.h
+++ b/init/keychords.h
@@ -18,6 +18,8 @@
18#define _INIT_KEYCHORDS_H_ 18#define _INIT_KEYCHORDS_H_
19 19
20#include <functional> 20#include <functional>
21#include <map>
22#include <string>
21#include <vector> 23#include <vector>
22 24
23#include "epoll.h" 25#include "epoll.h"
@@ -25,8 +27,74 @@
25namespace android { 27namespace android {
26namespace init { 28namespace init {
27 29
28void KeychordInit(Epoll* init_epoll, std::function<void(int)> handler); 30class Keychords {
29int GetKeychordId(const std::vector<int>& keycodes); 31 public:
32 Keychords();
33 Keychords(const Keychords&) = delete;
34 Keychords(Keychords&&) = delete;
35 Keychords& operator=(const Keychords&) = delete;
36 Keychords& operator=(Keychords&&) = delete;
37 ~Keychords() noexcept;
38
39 int GetId(const std::vector<int>& keycodes);
40 void Start(Epoll* epoll, std::function<void(int)> handler);
41
42 private:
43 // Bit management
44 class Mask {
45 public:
46 explicit Mask(size_t bit = 0);
47
48 void SetBit(size_t bit, bool value = true);
49 bool GetBit(size_t bit) const;
50
51 size_t bytesize() const;
52 void* data();
53 size_t size() const;
54 void resize(size_t bit);
55
56 operator bool() const;
57 Mask operator&(const Mask& rval) const;
58 void operator|=(const Mask& rval);
59
60 private:
61 typedef unsigned int mask_t;
62 static constexpr size_t kBitsPerByte = 8;
63
64 std::vector<mask_t> bits_;
65 };
66
67 struct Entry {
68 Entry(const std::vector<int>& keycodes, int id);
69
70 const std::vector<int> keycodes;
71 const int id;
72 bool notified;
73 };
74
75 static constexpr char kDevicePath[] = "/dev/input";
76
77 void LambdaCheck();
78 void LambdaHandler(int fd);
79 void InotifyHandler();
80
81 bool GeteventEnable(int fd);
82 void GeteventOpenDevice(const std::string& device);
83 void GeteventOpenDevice();
84 void GeteventCloseDevice(const std::string& device);
85
86 Epoll* epoll_;
87 std::function<void(int)> handler_;
88
89 std::map<std::string, int> registration_;
90
91 int count_;
92 std::vector<Entry> entries_;
93
94 Mask current_;
95
96 int inotify_fd_;
97};
30 98
31} // namespace init 99} // namespace init
32} // namespace android 100} // namespace android