blob: 74a91951b230bd769b7148c50de4c5b6578cd536 [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
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};
Colin Crossa8666952010-04-13 19:20:44 -070098
Tom Cherry81f5d3e2017-06-22 12:53:17 -070099} // namespace init
100} // namespace android
101
Colin Crossa8666952010-04-13 19:20:44 -0700102#endif