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 VIRTUAL_DEVICE_H
17 #define VIRTUAL_DEVICE_H
18 
19 #include <map>
20 #include <memory>
21 #include <vector>
22 #include <memory>
23 
24 #include <linux/input.h>
25 
26 #include "nocopyable.h"
27 
28 #include "v_input_device.h"
29 #include "virtual_device_defines.h"
30 
31 namespace OHOS {
32 namespace Msdp {
33 namespace DeviceStatus {
34 class VirtualDevice {
35 public:
36     explicit VirtualDevice(const std::string &node);
37     virtual ~VirtualDevice() = default;
38     DISALLOW_COPY_AND_MOVE(VirtualDevice);
39 
40     bool IsActive() const;
41     bool IsMouse() const;
42     bool IsKeyboard() const;
43     bool IsTouchscreen() const;
44     bool SupportEventType(size_t ev) const;
45     bool SupportKey(size_t key) const;
46     bool SupportAbs(size_t abs) const;
47     bool SupportRel(size_t rel) const;
48     bool SupportMsc(size_t msc) const;
49     bool SupportLed(size_t led) const;
50     bool SupportRep(size_t rep) const;
51     bool SupportProperty(size_t prop) const;
52     bool QueryAbsInfo(size_t abs, struct input_absinfo &absInfo);
53     std::string GetName() const;
54     std::string GetPhys() const;
55     struct input_id GetInputId() const;
56     void SetName(const std::string &name);
57     int32_t SendEvent(uint16_t type, uint16_t code, int32_t value);
58 
59 protected:
60     static bool FindDeviceNode(const std::string &name, std::string &node);
61     void SetMinimumInterval(int32_t interval);
62 
63 private:
64     static void Execute(std::vector<std::string> &results);
65     static void GetInputDeviceNodes(std::map<std::string, std::string> &nodes);
66 
67 private:
68     int32_t minimumInterval_ { 0 };
69     std::unique_ptr<VInputDevice> inputDev_ { nullptr };
70     std::string name_;
71 };
72 
IsActive() const73 inline bool VirtualDevice::IsActive() const
74 {
75     return ((inputDev_ != nullptr) && inputDev_->IsActive());
76 }
77 
IsMouse() const78 inline bool VirtualDevice::IsMouse() const
79 {
80     return ((inputDev_ != nullptr) && inputDev_->IsMouse());
81 }
82 
IsKeyboard() const83 inline bool VirtualDevice::IsKeyboard() const
84 {
85     return ((inputDev_ != nullptr) && inputDev_->IsKeyboard());
86 }
87 
IsTouchscreen() const88 inline bool VirtualDevice::IsTouchscreen() const
89 {
90     return ((inputDev_ != nullptr) && inputDev_->IsTouchscreen());
91 }
92 
SupportEventType(size_t ev) const93 inline bool VirtualDevice::SupportEventType(size_t ev) const
94 {
95     return ((inputDev_ != nullptr) && inputDev_->SupportEventType(ev));
96 }
97 
SupportKey(size_t key) const98 inline bool VirtualDevice::SupportKey(size_t key) const
99 {
100     return ((inputDev_ != nullptr) && inputDev_->SupportKey(key));
101 }
102 
SupportAbs(size_t abs) const103 inline bool VirtualDevice::SupportAbs(size_t abs) const
104 {
105     return ((inputDev_ != nullptr) && inputDev_->SupportAbs(abs));
106 }
107 
SupportRel(size_t rel) const108 inline bool VirtualDevice::SupportRel(size_t rel) const
109 {
110     return ((inputDev_ != nullptr) && inputDev_->SupportRel(rel));
111 }
112 
SupportProperty(size_t prop) const113 inline bool VirtualDevice::SupportProperty(size_t prop) const
114 {
115     return ((inputDev_ != nullptr) && inputDev_->SupportProperty(prop));
116 }
117 
SupportMsc(size_t msc) const118 inline bool VirtualDevice::SupportMsc(size_t msc) const
119 {
120     return ((inputDev_ != nullptr) && inputDev_->SupportMsc(msc));
121 }
122 
SupportLed(size_t led) const123 inline bool VirtualDevice::SupportLed(size_t led) const
124 {
125     return ((inputDev_ != nullptr) && inputDev_->SupportLed(led));
126 }
127 
SupportRep(size_t rep) const128 inline bool VirtualDevice::SupportRep(size_t rep) const
129 {
130     return ((inputDev_ != nullptr) && inputDev_->SupportRep(rep));
131 }
132 
QueryAbsInfo(size_t abs, struct input_absinfo &absInfo)133 inline bool VirtualDevice::QueryAbsInfo(size_t abs, struct input_absinfo &absInfo)
134 {
135     return ((inputDev_ != nullptr) && inputDev_->QueryAbsInfo(abs, absInfo));
136 }
137 
GetPhys() const138 inline std::string VirtualDevice::GetPhys() const
139 {
140     if (inputDev_ != nullptr) {
141         return inputDev_->GetPhys();
142     }
143     return {};
144 }
145 
GetInputId() const146 inline struct input_id VirtualDevice::GetInputId() const
147 {
148     if (inputDev_ != nullptr) {
149         return inputDev_->GetInputId();
150     }
151     return {};
152 }
153 
SetName(const std::string &name)154 inline void VirtualDevice::SetName(const std::string &name)
155 {
156     name_ = name;
157 }
158 } // namespace DeviceStatus
159 } // namespace Msdp
160 } // namespace OHOS
161 #endif // VIRTUAL_DEVICE_H