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