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