• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 DEVICE_H
17 #define 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 #include "i_device.h"
28 #include "i_epoll_event_source.h"
29 
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 inline constexpr size_t BIT_PER_UINT8 { 8 };
34 
NBYTES(size_t nbits)35 inline constexpr size_t NBYTES(size_t nbits)
36 {
37     return (nbits + BIT_PER_UINT8 - 1) / BIT_PER_UINT8;
38 }
39 
BYTE(size_t bit)40 inline constexpr size_t BYTE(size_t bit)
41 {
42     return (bit / BIT_PER_UINT8);
43 }
44 
OFFSET(size_t bit)45 inline constexpr size_t OFFSET(size_t bit)
46 {
47     return (bit % BIT_PER_UINT8);
48 }
49 
TestBit(size_t bit,const uint8_t * array)50 inline bool TestBit(size_t bit, const uint8_t *array)
51 {
52     return ((array)[BYTE(bit)] & (1 << OFFSET(bit)));
53 }
54 
55 class Device final : public IDevice,
56                      public IEpollEventSource {
57 public:
58     enum Capability {
59         DEVICE_CAP_KEYBOARD = 0,
60         DEVICE_CAP_TOUCH,
61         DEVICE_CAP_POINTER,
62         DEVICE_CAP_TABLET_TOOL,
63         DEVICE_CAP_TABLET_PAD,
64         DEVICE_CAP_GESTURE,
65         DEVICE_CAP_SWITCH,
66         DEVICE_CAP_JOYSTICK,
67         DEVICE_CAP_MAX
68     };
69 
70     explicit Device(int32_t deviceId);
71     DISALLOW_COPY_AND_MOVE(Device);
72     ~Device();
73 
74     int32_t Open() override;
75     void Close() override;
76     int32_t GetFd() const override;
77     void Dispatch(const struct epoll_event &ev) override;
78     void SetDevPath(const std::string &devPath) override;
79     void SetSysPath(const std::string &sysPath) override;
80     int32_t GetId() const override;
81     std::string GetDevPath() const override;
82     std::string GetSysPath() const override;
83     std::string GetName() const override;
84     int32_t GetBus() const override;
85     int32_t GetVersion() const override;
86     int32_t GetProduct() const override;
87     int32_t GetVendor() const override;
88     std::string GetPhys() const override;
89     std::string GetUniq() const override;
90     IDevice::KeyboardType GetKeyboardType() const override;
91     bool IsPointerDevice() const override;
92     bool IsKeyboard() const override;
93 
94     bool HasAbs(size_t abs) const;
95     bool HasKey(size_t key) const;
96     bool HasRel(size_t rel) const;
97     bool HasProperty(size_t property) const;
98     bool HasCapability(Capability capability) const;
99 
100 private:
101     void QueryDeviceInfo();
102     void QuerySupportedEvents();
103     void UpdateCapability();
104     bool HasAbsCoord() const;
105     bool HasMtCoord() const;
106     bool HasRelCoord() const;
107     bool HasAxesOrButton(size_t start, size_t end, const uint8_t* whichBitMask) const;
108     bool HasJoystickAxesOrButtons() const;
109     void CheckPointers();
110     void CheckAbs();
111     void CheckJoystick();
112     void CheckMt();
113     void CheckAdditional();
114     void CheckPencilMouse();
115     void CheckKeys();
116     std::string MakeConfigFileName() const;
117     int32_t ReadConfigFile(const std::string &filePath);
118     int32_t ConfigItemSwitch(const std::string &configItem, const std::string &value);
119     int32_t ReadTomlFile(const std::string &filePath);
120     void JudgeKeyboardType();
121     void LoadDeviceConfig();
122     void PrintCapsDevice() const;
123     void GetEventMask(const std::string &eventName, uint32_t type, std::size_t arrayLength,
124         uint8_t *whichBitMask) const;
125     void GetPropMask(const std::string &eventName, std::size_t arrayLength, uint8_t *whichBitMask) const;
126 
127     int32_t fd_ { -1 };
128     int32_t deviceId_ { -1 };
129     int32_t bus_ { 0 };
130     int32_t version_ { 0 };
131     int32_t product_ { 0 };
132     int32_t vendor_ { 0 };
133     std::string devPath_;
134     std::string sysPath_;
135     std::string dhid_;
136     std::string name_;
137     std::string phys_;
138     std::string uniq_;
139     std::string networkId_;
140     std::bitset<DEVICE_CAP_MAX> caps_;
141     uint8_t evBitmask_[NBYTES(EV_MAX)] {};
142     uint8_t keyBitmask_[NBYTES(KEY_MAX)] {};
143     uint8_t absBitmask_[NBYTES(ABS_MAX)] {};
144     uint8_t relBitmask_[NBYTES(REL_MAX)] {};
145     uint8_t propBitmask_[NBYTES(INPUT_PROP_MAX)] {};
146     IDevice::KeyboardType keyboardType_ { IDevice::KEYBOARD_TYPE_NONE };
147 };
148 
GetFd()149 inline int32_t Device::GetFd() const
150 {
151     return fd_;
152 }
153 
SetDevPath(const std::string & devPath)154 inline void Device::SetDevPath(const std::string &devPath)
155 {
156     devPath_ = devPath;
157 }
158 
SetSysPath(const std::string & sysPath)159 inline void Device::SetSysPath(const std::string &sysPath)
160 {
161     sysPath_ = sysPath;
162 }
163 
GetId()164 inline int32_t Device::GetId() const
165 {
166     return deviceId_;
167 }
168 
GetDevPath()169 inline std::string Device::GetDevPath() const
170 {
171     return devPath_;
172 }
173 
GetSysPath()174 inline std::string Device::GetSysPath() const
175 {
176     return sysPath_;
177 }
178 
GetName()179 inline std::string Device::GetName() const
180 {
181     return name_;
182 }
183 
GetBus()184 inline int32_t Device::GetBus() const
185 {
186     return bus_;
187 }
188 
GetVersion()189 inline int32_t Device::GetVersion() const
190 {
191     return version_;
192 }
193 
GetProduct()194 inline int32_t Device::GetProduct() const
195 {
196     return product_;
197 }
198 
GetVendor()199 inline int32_t Device::GetVendor() const
200 {
201     return vendor_;
202 }
203 
GetPhys()204 inline std::string Device::GetPhys() const
205 {
206     return phys_;
207 }
208 
GetUniq()209 inline std::string Device::GetUniq() const
210 {
211     return uniq_;
212 }
213 
GetKeyboardType()214 inline IDevice::KeyboardType Device::GetKeyboardType() const
215 {
216     return keyboardType_;
217 }
218 
IsPointerDevice()219 inline bool Device::IsPointerDevice() const
220 {
221     return caps_.test(DEVICE_CAP_POINTER);
222 }
223 
IsKeyboard()224 inline bool Device::IsKeyboard() const
225 {
226     return caps_.test(DEVICE_CAP_KEYBOARD);
227 }
228 
HasAbs(size_t abs)229 inline bool Device::HasAbs(size_t abs) const
230 {
231     return TestBit(abs, absBitmask_);
232 }
233 
HasRel(size_t rel)234 inline bool Device::HasRel(size_t rel) const
235 {
236     return TestBit(rel, relBitmask_);
237 }
238 
HasKey(size_t key)239 inline bool Device::HasKey(size_t key) const
240 {
241     return TestBit(key, keyBitmask_);
242 }
243 
HasProperty(size_t property)244 inline bool Device::HasProperty(size_t property) const
245 {
246     return TestBit(property, propBitmask_);
247 }
248 
HasCapability(Capability capability)249 inline bool Device::HasCapability(Capability capability) const
250 {
251     return caps_.test(capability);
252 }
253 } // namespace DeviceStatus
254 } // namespace Msdp
255 } // namespace OHOS
256 #endif // DEVICE_H