1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef V_INPUT_DEVICE_H
17 #define V_INPUT_DEVICE_H
18
19 #include <bitset>
20 #include <string>
21 #include <vector>
22
23 #include <linux/input.h>
24
25 #include "nocopyable.h"
26
27 namespace OHOS {
28 namespace Msdp {
29 namespace DeviceStatus {
30 inline constexpr size_t BITS_PER_UINT8 { 8 };
31
OFFSET(size_t bit)32 inline constexpr size_t OFFSET(size_t bit)
33 {
34 return (bit % BITS_PER_UINT8);
35 }
36
BYTE(size_t bit)37 inline constexpr size_t BYTE(size_t bit)
38 {
39 return (bit / BITS_PER_UINT8);
40 }
41
TestBit(size_t bit,const uint8_t * array)42 inline bool TestBit(size_t bit, const uint8_t *array)
43 {
44 return ((array)[BYTE(bit)] & (1 << OFFSET(bit)));
45 }
46
NBYTES(size_t nbits)47 inline constexpr size_t NBYTES(size_t nbits)
48 {
49 return (nbits + BITS_PER_UINT8 - 1) / BITS_PER_UINT8;
50 }
51
52 class VInputDevice final {
53 public:
54 enum Capability {
55 DEVICE_CAP_KEYBOARD = 0,
56 DEVICE_CAP_TOUCH,
57 DEVICE_CAP_POINTER,
58 DEVICE_CAP_TABLET_TOOL,
59 DEVICE_CAP_TABLET_PAD,
60 DEVICE_CAP_GESTURE,
61 DEVICE_CAP_SWITCH,
62 DEVICE_CAP_JOYSTICK,
63 DEVICE_CAP_MAX
64 };
65
66 public:
67 explicit VInputDevice(const std::string &node);
68 ~VInputDevice();
69 DISALLOW_COPY_AND_MOVE(VInputDevice);
70
71 int32_t Open();
72 void Close();
73 bool IsActive() const;
74 bool SupportEventType(size_t ev) const;
75 bool SupportKey(size_t key) const;
76 bool SupportAbs(size_t abs) const;
77 bool SupportRel(size_t rel) const;
78 bool SupportMsc(size_t msc) const;
79 bool SupportLed(size_t led) const;
80 bool SupportRep(size_t rep) const;
81 bool SupportProperty(size_t prop) const;
82 bool QueryAbsInfo(size_t abs, struct input_absinfo &absInfo);
83 int32_t SendEvent(uint16_t type, uint16_t code, int32_t value);
84
85 int32_t GetFd() const;
86 std::string GetDevPath() const;
87 std::string GetSysPath() const;
88 std::string GetName() const;
89 struct input_id GetInputId() const;
90 std::string GetPhys() const;
91 std::string GetUniq() const;
92 bool IsMouse() const;
93 bool IsKeyboard() const;
94 bool IsTouchscreen() const;
95
96 private:
97 void QueryDeviceInfo();
98 void QuerySupportedEvents();
99 void UpdateCapability();
100 bool HasMouseButton() const;
101 bool HasJoystickAxesOrButtons() const;
102 void CheckPointers();
103 void CheckKeys();
104 void GetEventMask(const std::string &eventName, uint32_t type, std::size_t arrayLength,
105 uint8_t *whichBitMask) const;
106 void GetPropMask(const std::string &eventName, std::size_t arrayLength, uint8_t *whichBitMask) const;
107 void PrintCapsDevice() const;
108
109 private:
110 int32_t fd_ { -1 };
111 struct input_id inputId_ {};
112 std::string devPath_;
113 std::string sysPath_;
114 std::string name_;
115 std::string phys_;
116 std::string uniq_;
117 std::string dhid_;
118 std::string networkId_;
119 std::bitset<DEVICE_CAP_MAX> caps_;
120 uint8_t evBitmask_[NBYTES(EV_MAX)] {};
121 uint8_t keyBitmask_[NBYTES(KEY_MAX)] {};
122 uint8_t absBitmask_[NBYTES(ABS_MAX)] {};
123 uint8_t relBitmask_[NBYTES(REL_MAX)] {};
124 uint8_t mscBitmask_[NBYTES(MSC_MAX)] {};
125 uint8_t ledBitmask_[NBYTES(LED_MAX)] {};
126 uint8_t repBitmask_[NBYTES(REP_MAX)] {};
127 uint8_t propBitmask_[NBYTES(INPUT_PROP_MAX)] {};
128 };
129
IsActive()130 inline bool VInputDevice::IsActive() const
131 {
132 return (fd_ >= 0);
133 }
134
SupportEventType(size_t ev)135 inline bool VInputDevice::SupportEventType(size_t ev) const
136 {
137 return TestBit(ev, evBitmask_);
138 }
139
SupportKey(size_t key)140 inline bool VInputDevice::SupportKey(size_t key) const
141 {
142 return (TestBit(EV_KEY, evBitmask_) && TestBit(key, keyBitmask_));
143 }
144
SupportAbs(size_t abs)145 inline bool VInputDevice::SupportAbs(size_t abs) const
146 {
147 return (TestBit(EV_ABS, evBitmask_) && TestBit(abs, absBitmask_));
148 }
149
SupportRel(size_t rel)150 inline bool VInputDevice::SupportRel(size_t rel) const
151 {
152 return (TestBit(EV_REL, evBitmask_) && TestBit(rel, relBitmask_));
153 }
154
SupportMsc(size_t msc)155 inline bool VInputDevice::SupportMsc(size_t msc) const
156 {
157 return (TestBit(EV_MSC, evBitmask_) && TestBit(msc, mscBitmask_));
158 }
159
SupportLed(size_t led)160 inline bool VInputDevice::SupportLed(size_t led) const
161 {
162 return (TestBit(EV_LED, evBitmask_) && TestBit(led, ledBitmask_));
163 }
164
SupportRep(size_t rep)165 inline bool VInputDevice::SupportRep(size_t rep) const
166 {
167 return (TestBit(EV_REP, evBitmask_) && TestBit(rep, repBitmask_));
168 }
169
SupportProperty(size_t prop)170 inline bool VInputDevice::SupportProperty(size_t prop) const
171 {
172 return TestBit(prop, propBitmask_);
173 }
174
GetFd()175 inline int32_t VInputDevice::GetFd() const
176 {
177 return fd_;
178 }
179
GetDevPath()180 inline std::string VInputDevice::GetDevPath() const
181 {
182 return devPath_;
183 }
184
GetSysPath()185 inline std::string VInputDevice::GetSysPath() const
186 {
187 return sysPath_;
188 }
189
GetName()190 inline std::string VInputDevice::GetName() const
191 {
192 return name_;
193 }
194
GetInputId()195 inline struct input_id VInputDevice::GetInputId() const
196 {
197 return inputId_;
198 }
199
GetPhys()200 inline std::string VInputDevice::GetPhys() const
201 {
202 return phys_;
203 }
204
GetUniq()205 inline std::string VInputDevice::GetUniq() const
206 {
207 return uniq_;
208 }
209
IsMouse()210 inline bool VInputDevice::IsMouse() const
211 {
212 return caps_.test(DEVICE_CAP_POINTER);
213 }
214
IsKeyboard()215 inline bool VInputDevice::IsKeyboard() const
216 {
217 return caps_.test(DEVICE_CAP_KEYBOARD);
218 }
219
IsTouchscreen()220 inline bool VInputDevice::IsTouchscreen() const
221 {
222 return caps_.test(DEVICE_CAP_TOUCH);
223 }
224 } // namespace DeviceStatus
225 } // namespace Msdp
226 } // namespace OHOS
227 #endif // V_INPUT_DEVICE_H