Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 17 | #include "keychords.h" |
| 18 | |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 21 | #include <linux/input.h> |
| 22 | #include <sys/cdefs.h> |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 23 | #include <sys/inotify.h> |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 24 | #include <sys/ioctl.h> |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 25 | #include <sys/types.h> |
Olivier Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 27 | |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <functional> |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 30 | #include <map> |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 31 | #include <memory> |
| 32 | #include <string> |
| 33 | #include <vector> |
| 34 | |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | namespace init { |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 39 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 40 | Keychords::Keychords() : epoll_(nullptr), count_(0), inotify_fd_(-1) {} |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 41 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 42 | Keychords::~Keychords() noexcept { |
| 43 | if (inotify_fd_ >= 0) { |
| 44 | epoll_->UnregisterHandler(inotify_fd_); |
| 45 | ::close(inotify_fd_); |
Colin Cross | f7ca604 | 2011-01-04 18:18:45 -0800 | [diff] [blame] | 46 | } |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 47 | while (!registration_.empty()) GeteventCloseDevice(registration_.begin()->first); |
| 48 | } |
Colin Cross | f7ca604 | 2011-01-04 18:18:45 -0800 | [diff] [blame] | 49 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 50 | Keychords::Mask::Mask(size_t bit) : bits_((bit + sizeof(mask_t) - 1) / sizeof(mask_t), 0) {} |
| 51 | |
| 52 | void Keychords::Mask::SetBit(size_t bit, bool value) { |
| 53 | auto idx = bit / (kBitsPerByte * sizeof(mask_t)); |
| 54 | if (idx >= bits_.size()) return; |
| 55 | if (value) { |
| 56 | bits_[idx] |= mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t))); |
| 57 | } else { |
| 58 | bits_[idx] &= ~(mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t)))); |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 59 | } |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 60 | } |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 61 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 62 | bool Keychords::Mask::GetBit(size_t bit) const { |
| 63 | auto idx = bit / (kBitsPerByte * sizeof(mask_t)); |
| 64 | return bits_[idx] & (mask_t(1) << (bit % (kBitsPerByte * sizeof(mask_t)))); |
| 65 | } |
| 66 | |
| 67 | size_t Keychords::Mask::bytesize() const { |
| 68 | return bits_.size() * sizeof(mask_t); |
| 69 | } |
| 70 | |
| 71 | void* Keychords::Mask::data() { |
| 72 | return bits_.data(); |
| 73 | } |
| 74 | |
| 75 | size_t Keychords::Mask::size() const { |
| 76 | return bits_.size() * sizeof(mask_t) * kBitsPerByte; |
| 77 | } |
| 78 | |
| 79 | void 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); |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 83 | } |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 84 | } |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 85 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 86 | Keychords::Mask::operator bool() const { |
| 87 | for (size_t i = 0; i < bits_.size(); ++i) { |
| 88 | if (bits_[i]) return true; |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 89 | } |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 90 | return false; |
| 91 | } |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 92 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 93 | Keychords::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]; |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 99 | } |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 100 | return ret; |
| 101 | } |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 102 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 103 | void 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]; |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 108 | } |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 109 | } |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 110 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 111 | Keychords::Entry::Entry(const std::vector<int>& keycodes, int id) |
| 112 | : keycodes(keycodes), id(id), notified(false) {} |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 113 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 114 | void Keychords::LambdaCheck() { |
| 115 | for (auto& e : entries_) { |
| 116 | auto found = true; |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 117 | for (auto& code : e.keycodes) { |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 118 | if (!current_.GetBit(code)) { |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 119 | e.notified = false; |
| 120 | found = false; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | if (!found) continue; |
| 125 | if (e.notified) continue; |
| 126 | e.notified = true; |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 127 | handler_(e.id); |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 131 | void Keychords::LambdaHandler(int fd) { |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 132 | input_event event; |
| 133 | auto res = TEMP_FAILURE_RETRY(::read(fd, &event, sizeof(event))); |
| 134 | if ((res != sizeof(event)) || (event.type != EV_KEY)) return; |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 135 | current_.SetBit(event.code, event.value); |
| 136 | LambdaCheck(); |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 139 | bool Keychords::GeteventEnable(int fd) { |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 140 | // Make sure it is an event channel, should pass this ioctl call |
| 141 | int version; |
| 142 | if (::ioctl(fd, EVIOCGVERSION, &version)) return false; |
| 143 | |
Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 144 | #ifdef EVIOCSMASK |
| 145 | static auto EviocsmaskSupported = true; |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 146 | if (EviocsmaskSupported) { |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 147 | Keychords::Mask mask(EV_KEY); |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 148 | mask.SetBit(EV_KEY); |
| 149 | input_mask msg = {}; |
| 150 | msg.type = EV_SYN; |
| 151 | msg.codes_size = mask.bytesize(); |
| 152 | msg.codes_ptr = reinterpret_cast<uintptr_t>(mask.data()); |
| 153 | if (::ioctl(fd, EVIOCSMASK, &msg) == -1) { |
| 154 | PLOG(WARNING) << "EVIOCSMASK not supported"; |
| 155 | EviocsmaskSupported = false; |
| 156 | } |
| 157 | } |
Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 158 | #endif |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 159 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 160 | Keychords::Mask mask; |
| 161 | for (auto& e : entries_) { |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 162 | for (auto& code : e.keycodes) { |
| 163 | mask.resize(code); |
| 164 | mask.SetBit(code); |
| 165 | } |
| 166 | } |
| 167 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 168 | current_.resize(mask.size()); |
| 169 | Keychords::Mask available(mask.size()); |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 170 | auto res = ::ioctl(fd, EVIOCGBIT(EV_KEY, available.bytesize()), available.data()); |
| 171 | if (res == -1) return false; |
| 172 | if (!(available & mask)) return false; |
| 173 | |
Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 174 | #ifdef EVIOCSMASK |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 175 | if (EviocsmaskSupported) { |
| 176 | input_mask msg = {}; |
| 177 | msg.type = EV_KEY; |
| 178 | msg.codes_size = mask.bytesize(); |
| 179 | msg.codes_ptr = reinterpret_cast<uintptr_t>(mask.data()); |
| 180 | ::ioctl(fd, EVIOCSMASK, &msg); |
| 181 | } |
Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 182 | #endif |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 183 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 184 | Keychords::Mask set(mask.size()); |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 185 | res = ::ioctl(fd, EVIOCGKEY(res), set.data()); |
| 186 | if (res > 0) { |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 187 | current_ |= mask & available & set; |
| 188 | LambdaCheck(); |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 189 | } |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 190 | epoll_->RegisterHandler(fd, [this, fd]() { this->LambdaHandler(fd); }); |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 194 | void Keychords::GeteventOpenDevice(const std::string& device) { |
| 195 | if (registration_.count(device)) return; |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 196 | auto fd = TEMP_FAILURE_RETRY(::open(device.c_str(), O_RDWR | O_CLOEXEC)); |
| 197 | if (fd == -1) { |
| 198 | PLOG(ERROR) << "Can not open " << device; |
| 199 | return; |
| 200 | } |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 201 | if (!GeteventEnable(fd)) { |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 202 | ::close(fd); |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 203 | } else { |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 204 | registration_.emplace(device, fd); |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 208 | void Keychords::GeteventCloseDevice(const std::string& device) { |
| 209 | auto it = registration_.find(device); |
| 210 | if (it == registration_.end()) return; |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 211 | auto fd = (*it).second; |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 212 | epoll_->UnregisterHandler(fd); |
| 213 | registration_.erase(it); |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 214 | ::close(fd); |
| 215 | } |
| 216 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 217 | void Keychords::InotifyHandler() { |
| 218 | unsigned char buf[512]; // History shows 32-64 bytes typical |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 219 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 220 | auto res = TEMP_FAILURE_RETRY(::read(inotify_fd_, buf, sizeof(buf))); |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 221 | if (res < 0) { |
| 222 | PLOG(WARNING) << "could not get event"; |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | auto event_buf = buf; |
| 227 | while (static_cast<size_t>(res) >= sizeof(inotify_event)) { |
| 228 | auto event = reinterpret_cast<inotify_event*>(event_buf); |
| 229 | auto event_size = sizeof(inotify_event) + event->len; |
| 230 | if (static_cast<size_t>(res) < event_size) break; |
| 231 | if (event->len) { |
| 232 | std::string devname(kDevicePath); |
| 233 | devname += '/'; |
| 234 | devname += event->name; |
| 235 | if (event->mask & IN_CREATE) { |
| 236 | GeteventOpenDevice(devname); |
| 237 | } else { |
| 238 | GeteventCloseDevice(devname); |
| 239 | } |
| 240 | } |
| 241 | res -= event_size; |
| 242 | event_buf += event_size; |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 246 | void Keychords::GeteventOpenDevice() { |
| 247 | inotify_fd_ = ::inotify_init1(IN_NONBLOCK | IN_CLOEXEC); |
| 248 | if (inotify_fd_ < 0) { |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 249 | PLOG(WARNING) << "Could not instantiate inotify for " << kDevicePath; |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 250 | } else if (::inotify_add_watch(inotify_fd_, kDevicePath, IN_DELETE | IN_CREATE | IN_ONLYDIR) < |
| 251 | 0) { |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 252 | PLOG(WARNING) << "Could not add watch for " << kDevicePath; |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 253 | ::close(inotify_fd_); |
| 254 | inotify_fd_ = -1; |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 255 | } |
Mark Salyzyn | 44692de | 2018-05-02 11:22:15 -0700 | [diff] [blame] | 256 | |
| 257 | std::unique_ptr<DIR, decltype(&closedir)> device(opendir(kDevicePath), closedir); |
| 258 | if (device) { |
| 259 | dirent* entry; |
| 260 | while ((entry = readdir(device.get()))) { |
| 261 | if (entry->d_name[0] == '.') continue; |
| 262 | std::string devname(kDevicePath); |
| 263 | devname += '/'; |
| 264 | devname += entry->d_name; |
| 265 | GeteventOpenDevice(devname); |
| 266 | } |
| 267 | } |
| 268 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 269 | if (inotify_fd_ >= 0) { |
| 270 | epoll_->RegisterHandler(inotify_fd_, [this]() { this->InotifyHandler(); }); |
| 271 | } |
Mark Salyzyn | 353bf1f | 2018-04-30 07:33:31 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 274 | int Keychords::GetId(const std::vector<int>& keycodes) { |
Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 275 | if (keycodes.empty()) return 0; |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 276 | ++count_; |
| 277 | entries_.emplace_back(Entry(keycodes, count_)); |
| 278 | return count_; |
Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame^] | 281 | void Keychords::Start(Epoll* epoll, std::function<void(int)> handler) { |
| 282 | epoll_ = epoll; |
| 283 | handler_ = handler; |
| 284 | if (count_) GeteventOpenDevice(); |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 285 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 286 | |
| 287 | } // namespace init |
| 288 | } // namespace android |