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 
20 #include <functional>
21 #include <map>
22 #include <string>
23 #include <vector>
24 
25 #include "epoll.h"
26 
27 namespace android {
28 namespace init {
29 
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 
39     void Register(const std::vector<int>& keycodes);
40     void Start(Epoll* epoll, std::function<void(const std::vector<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();
69 
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_;
85     std::function<void(const std::vector<int>&)> handler_;
86 
87     std::map<std::string, int> registration_;
88 
89     std::map<const std::vector<int>, Entry> entries_;
90 
91     Mask current_;
92 
93     int inotify_fd_;
94 };
95 
96 }  // namespace init
97 }  // namespace android
98 
99 #endif
100