1 /*
2 * Copyright (c) 2021-2025 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 INPUT_DEVICE_H
17 #define INPUT_DEVICE_H
18
19 #include <bitset>
20 #include <vector>
21
22 #include "nocopyable.h"
23 #include "parcel.h"
24
25 namespace OHOS {
26 namespace MMI {
27 constexpr uint32_t MAX_AXIS_INFO { 64 };
28 enum InputDeviceCapability {
29 INPUT_DEV_CAP_KEYBOARD,
30 INPUT_DEV_CAP_POINTER,
31 INPUT_DEV_CAP_TOUCH,
32 INPUT_DEV_CAP_TABLET_TOOL,
33 INPUT_DEV_CAP_TABLET_PAD,
34 INPUT_DEV_CAP_GESTURE,
35 INPUT_DEV_CAP_SWITCH,
36 INPUT_DEV_CAP_JOYSTICK,
37 INPUT_DEV_CAP_MAX
38 };
39
40 enum FunctionKey {
41 FUNCTION_KEY_CAPSLOCK = 1,
42 };
43
CapabilityToTags(InputDeviceCapability capability)44 inline constexpr uint32_t CapabilityToTags(InputDeviceCapability capability)
45 {
46 return static_cast<uint32_t>((1 << capability) - (capability / INPUT_DEV_CAP_MAX));
47 }
48
49 enum KeyboardType {
50 KEYBOARD_TYPE_NONE,
51 KEYBOARD_TYPE_UNKNOWN,
52 KEYBOARD_TYPE_ALPHABETICKEYBOARD,
53 KEYBOARD_TYPE_DIGITALKEYBOARD,
54 KEYBOARD_TYPE_HANDWRITINGPEN,
55 KEYBOARD_TYPE_REMOTECONTROL,
56 KEYBOARD_TYPE_MAX
57 };
58
59 class InputDevice : public Parcelable {
60 public:
61 InputDevice() = default;
62 ~InputDevice() = default;
63
64 void SetId(int32_t deviceId);
65 int32_t GetId() const;
66 void SetName(std::string name);
67 std::string GetName() const;
68 void SetType(int32_t deviceType);
69 int32_t GetType() const;
70 void SetBus(int32_t bus);
71 int32_t GetBus() const;
72 void SetVersion(int32_t version);
73 int32_t GetVersion() const;
74 void SetProduct(int32_t product);
75 int32_t GetProduct() const;
76 void SetVendor(int32_t vendor);
77 int32_t GetVendor() const;
78 void SetPhys(std::string phys);
79 std::string GetPhys() const;
80 void SetUniq(std::string uniq);
81 std::string GetUniq() const;
82 void AddCapability(InputDeviceCapability cap);
83 bool HasCapability(InputDeviceCapability cap) const;
84 bool HasCapability(uint32_t deviceTags) const;
85
86 unsigned long GetCapabilities() const;
87 void SetCapabilities(unsigned long caps);
88
89 class AxisInfo {
90 public:
91 AxisInfo() = default;
92 AxisInfo(int32_t type, int32_t min, int32_t max, int32_t fuzz, int32_t flat, int32_t resolution);
93 ~AxisInfo() = default;
94
WriteToParcel(Parcel & out)95 bool WriteToParcel(Parcel &out) const
96 {
97 if (!out.WriteInt32(axisType_)) {
98 return false;
99 }
100 if (!out.WriteInt32(minimum_)) {
101 return false;
102 }
103 if (!out.WriteInt32(maximum_)) {
104 return false;
105 }
106 if (!out.WriteInt32(fuzz_)) {
107 return false;
108 }
109 if (!out.WriteInt32(flat_)) {
110 return false;
111 }
112 if (!out.WriteInt32(resolution_)) {
113 return false;
114 }
115 return true;
116 }
117
ReadFromParcel(Parcel & in)118 bool ReadFromParcel(Parcel &in)
119 {
120 return (
121 in.ReadInt32(axisType_) &&
122 in.ReadInt32(minimum_) &&
123 in.ReadInt32(maximum_) &&
124 in.ReadInt32(fuzz_) &&
125 in.ReadInt32(flat_) &&
126 in.ReadInt32(resolution_)
127 );
128 }
129
130 void SetAxisType(int32_t type);
131 int32_t GetAxisType() const;
132 void SetMinimum(int32_t min);
133 int32_t GetMinimum() const;
134 void SetMaximum(int32_t max);
135 int32_t GetMaximum() const;
136 void SetFuzz(int32_t fuzz);
137 int32_t GetFuzz() const;
138 void SetFlat(int32_t flat);
139 int32_t GetFlat() const;
140 void SetResolution(int32_t resolution);
141 int32_t GetResolution() const;
142
143 private:
144 int32_t axisType_ { 0 };
145 int32_t minimum_ { 0 };
146 int32_t maximum_ { 0 };
147 int32_t fuzz_ { 0 };
148 int32_t flat_ { 0 };
149 int32_t resolution_ { 0 };
150 };
151
152 void AddAxisInfo(AxisInfo axis);
153 std::vector<AxisInfo> GetAxisInfo();
154 void SetAxisInfo(std::vector<AxisInfo> axis);
155 InputDevice(int32_t id, std::string name, int32_t deviceType, int32_t bus, int32_t version, int32_t product,
156 int32_t vendor, std::string phys, std::string uniq, const std::vector<AxisInfo>& axis);
157
UnmarshalAxisInfo(Parcel & in,std::vector<AxisInfo> & info)158 static bool UnmarshalAxisInfo(Parcel &in, std::vector<AxisInfo> &info)
159 {
160 uint32_t size = 0;
161 if (!in.ReadUint32(size)) {
162 return false;
163 }
164 if (size > MAX_AXIS_INFO) {
165 return false;
166 }
167 info.resize(size);
168 for (uint32_t i = 0; i < size; i++) {
169 AxisInfo axInfo = {};
170 if (!axInfo.ReadFromParcel(in)) {
171 return false;
172 }
173 info.push_back(axInfo);
174 }
175 return true;
176 }
ReadFromParcel(Parcel & in)177 bool ReadFromParcel(Parcel &in)
178 {
179 bool result = (
180 in.ReadInt32(id_) &&
181 in.ReadInt32(type_) &&
182 in.ReadString(name_) &&
183 in.ReadInt32(bus_) &&
184 in.ReadInt32(version_) &&
185 in.ReadInt32(product_) &&
186 in.ReadInt32(vendor_) &&
187 in.ReadString(phys_) &&
188 in.ReadString(uniq_)
189 );
190 uint64_t capabilities = 0;
191 if (!result || !in.ReadUint64(capabilities)) {
192 return false;
193 }
194 SetCapabilities(capabilities);
195 return UnmarshalAxisInfo(in, axis_);
196 }
Marshalling(Parcel & out)197 bool Marshalling(Parcel &out) const override
198 {
199 if (!out.WriteInt32(id_)) {
200 return false;
201 }
202 if (!out.WriteInt32(type_)) {
203 return false;
204 }
205 if (!out.WriteString(name_)) {
206 return false;
207 }
208 if (!out.WriteInt32(bus_)) {
209 return false;
210 }
211 if (!out.WriteInt32(version_)) {
212 return false;
213 }
214 if (!out.WriteInt32(product_)) {
215 return false;
216 }
217 if (!out.WriteInt32(vendor_)) {
218 return false;
219 }
220 if (!out.WriteString(phys_)) {
221 return false;
222 }
223 if (!out.WriteString(uniq_)) {
224 return false;
225 }
226 if (!out.WriteUint64(capabilities_.to_ulong())) {
227 return false;
228 }
229 if (!out.WriteUint32(static_cast<uint32_t>(axis_.size()))) {
230 return false;
231 }
232 for (const auto &item : axis_) {
233 if (!item.WriteToParcel(out)) {
234 return false;
235 }
236 }
237 return true;
238 }
Unmarshalling(Parcel & in)239 static InputDevice* Unmarshalling(Parcel &in)
240 {
241 auto inputDevice = new (std::nothrow) InputDevice();
242 if (inputDevice && !inputDevice->ReadFromParcel(in)) {
243 delete inputDevice;
244 inputDevice = nullptr;
245 }
246 return inputDevice;
247 }
248
249 private:
250 int32_t id_ { -1 };
251 std::string name_ { "null" };
252 int32_t type_ { 0 };
253 int32_t bus_ { -1 };
254 int32_t version_ { -1 };
255 int32_t product_ { -1 };
256 int32_t vendor_ { -1 };
257 std::string phys_ { "null" };
258 std::string uniq_ { "null" };
259 std::vector<AxisInfo> axis_;
260 std::bitset<INPUT_DEV_CAP_MAX> capabilities_;
261 };
262
GetCapabilities()263 inline unsigned long InputDevice::GetCapabilities() const
264 {
265 return capabilities_.to_ulong();
266 }
267
SetCapabilities(unsigned long caps)268 inline void InputDevice::SetCapabilities(unsigned long caps)
269 {
270 capabilities_ = std::bitset<INPUT_DEV_CAP_MAX>(caps % (1 << INPUT_DEV_CAP_MAX));
271 }
272 } // namespace MMI
273 } // namespace OHOS
274 #endif // INPUT_DEVICE_H