| 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 |  | 
|  | 17 | #ifndef _INIT_KEYCHORDS_H_ | 
|  | 18 | #define _INIT_KEYCHORDS_H_ | 
|  | 19 |  | 
| Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 20 | #include <functional> | 
| Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 21 | #include <map> | 
|  | 22 | #include <string> | 
| Mark Salyzyn | eca2507 | 2018-05-16 15:10:24 -0700 | [diff] [blame] | 23 | #include <vector> | 
|  | 24 |  | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 25 | #include "epoll.h" | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 26 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 27 | namespace android { | 
|  | 28 | namespace init { | 
|  | 29 |  | 
| Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 30 | class Keychords { | 
|  | 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 |  | 
| Mark Salyzyn | 1385725 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 39 | void Register(const std::vector<int>& keycodes); | 
|  | 40 | void Start(Epoll* epoll, std::function<void(const std::vector<int>&)> handler); | 
| Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 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 { | 
| Mark Salyzyn | 1385725 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 68 | Entry(); | 
| Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 69 |  | 
| Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 70 | bool notified; | 
|  | 71 | }; | 
|  | 72 |  | 
|  | 73 | static constexpr char kDevicePath[] = "/dev/input"; | 
|  | 74 |  | 
|  | 75 | void LambdaCheck(); | 
|  | 76 | void LambdaHandler(int fd); | 
|  | 77 | void InotifyHandler(); | 
|  | 78 |  | 
|  | 79 | bool GeteventEnable(int fd); | 
|  | 80 | void GeteventOpenDevice(const std::string& device); | 
|  | 81 | void GeteventOpenDevice(); | 
|  | 82 | void GeteventCloseDevice(const std::string& device); | 
|  | 83 |  | 
|  | 84 | Epoll* epoll_; | 
| Mark Salyzyn | 1385725 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 85 | std::function<void(const std::vector<int>&)> handler_; | 
| Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 86 |  | 
|  | 87 | std::map<std::string, int> registration_; | 
|  | 88 |  | 
| Mark Salyzyn | 1385725 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 89 | std::map<const std::vector<int>, Entry> entries_; | 
| Mark Salyzyn | 06aeb41 | 2018-05-18 15:25:15 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | Mask current_; | 
|  | 92 |  | 
|  | 93 | int inotify_fd_; | 
|  | 94 | }; | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 95 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 96 | }  // namespace init | 
|  | 97 | }  // namespace android | 
|  | 98 |  | 
| Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 99 | #endif |