blob: 00ed2052ef5e0f0b220f0b0f4881ab10a7258b03 [file] [log] [blame]
Colin Crossa8666952010-04-13 19:20:44 -07001/*
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 Salyzyneca25072018-05-16 15:10:24 -070020#include <functional>
Mark Salyzyn06aeb412018-05-18 15:25:15 -070021#include <map>
22#include <string>
Mark Salyzyneca25072018-05-16 15:10:24 -070023#include <vector>
24
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070025#include "epoll.h"
Colin Crossa8666952010-04-13 19:20:44 -070026
Tom Cherry81f5d3e2017-06-22 12:53:17 -070027namespace android {
28namespace init {
29
Mark Salyzyn06aeb412018-05-18 15:25:15 -070030class 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 Salyzyn13857252018-05-18 15:25:15 -070039 void Register(const std::vector<int>& keycodes);
40 void Start(Epoll* epoll, std::function<void(const std::vector<int>&)> handler);
Mark Salyzyn06aeb412018-05-18 15:25:15 -070041
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 Salyzyn13857252018-05-18 15:25:15 -070068 Entry();
Mark Salyzyn06aeb412018-05-18 15:25:15 -070069
Mark Salyzyn06aeb412018-05-18 15:25:15 -070070 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 Salyzyn13857252018-05-18 15:25:15 -070085 std::function<void(const std::vector<int>&)> handler_;
Mark Salyzyn06aeb412018-05-18 15:25:15 -070086
87 std::map<std::string, int> registration_;
88
Mark Salyzyn13857252018-05-18 15:25:15 -070089 std::map<const std::vector<int>, Entry> entries_;
Mark Salyzyn06aeb412018-05-18 15:25:15 -070090
91 Mask current_;
92
93 int inotify_fd_;
94};
Colin Crossa8666952010-04-13 19:20:44 -070095
Tom Cherry81f5d3e2017-06-22 12:53:17 -070096} // namespace init
97} // namespace android
98
Colin Crossa8666952010-04-13 19:20:44 -070099#endif