1  /*
2   * Copyright (c) 2021-2024 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 OHOS_DISTRIBUTED_INPUT_CONSTANTS_H
17  #define OHOS_DISTRIBUTED_INPUT_CONSTANTS_H
18  
19  #include <map>
20  #include <string>
21  #include <vector>
22  
23  #include <linux/uinput.h>
24  
25  namespace OHOS {
26  namespace DistributedHardware {
27  namespace DistributedInput {
28  #define INPUT_KEY_WHEN  "when"
29  #define INPUT_KEY_TYPE  "type"
30  #define INPUT_KEY_CODE  "code"
31  #define INPUT_KEY_VALUE "value"
32  #define INPUT_KEY_DESCRIPTOR  "descriptor"
33  #define INPUT_KEY_PATH "path"
34  
35  #define VIRTUAL_DEVICE_NAME "DistributedInput "
36  #define LONG_BITS (sizeof(long) * 8)
37  #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
38  
39      const char INPUT_STRING_SPLIT_POINT = '.';
40      const uint32_t KEY_DOWN_STATE = 1;
41      const uint32_t KEY_UP_STATE = 0;
42      const uint32_t KEY_REPEAT = 2;
43      const uint32_t READ_SLEEP_TIME_MS = 50;
44      const uint32_t READ_RETRY_MAX = 5;
45      const uint32_t DH_ID_LENGTH_MAX = 256;
46      const uint32_t DEV_ID_LENGTH_MAX = 256;
47      const uint32_t STRING_MAX_SIZE = 40 * 1024 * 1024;
48      const uint32_t SCREEN_MSG_MAX = 40 * 1024 * 1024;
49      const uint32_t AUTH_SESSION_SIDE_SERVER = 0;
50      const uint32_t IPC_VECTOR_MAX_SIZE = 32;
51      const uint32_t EVENT_BUFFER_MAX = 512;
52  
53      /*
54       * Device Type definitions
55       */
56      enum class DInputDeviceType : uint32_t {
57          NONE = 0x0000,
58          MOUSE = 0x0001,
59          KEYBOARD = 0x0002,
60          TOUCHSCREEN = 0x0004,
61          TOUCHPAD = MOUSE,
62          ALL = MOUSE | KEYBOARD | TOUCHSCREEN,
63      };
64  
65      const char * const DEVICE_PATH = "/dev/input";
66  
67      /*
68       * Maximum number of signalled FDs to handle at a time.
69       */
70      constexpr uint32_t EPOLL_MAX_EVENTS = 16;
71  
72      /*
73       * Maximum number of event buffer size.
74       */
75      constexpr uint32_t INPUT_EVENT_BUFFER_SIZE = 256;
76  
77      constexpr int32_t INPUT_LOAD_SA_TIMEOUT_MS = 10000;
78  
79      constexpr int32_t INPUT_LATENCY_DELAYTIME_US = 50 * 1000;
80  
81      constexpr uint32_t INPUT_LATENCY_DELAY_TIMES = 60;
82  
83      constexpr int32_t SESSION_WAIT_TIMEOUT_SECOND = 5;
84  
85      constexpr int32_t EPOLL_WAITTIME = 100;
86  
87      constexpr uint64_t WATCHDOG_INTERVAL_TIME_MS = 20 * 1000;
88  
89      /* The input device is a keyboard or has buttons. */
90      constexpr uint32_t INPUT_DEVICE_CLASS_KEYBOARD      = 0x00000001;
91  
92      /* The input device is an alpha-numeric keyboard (not just a dial pad). */
93      constexpr uint32_t INPUT_DEVICE_CLASS_ALPHAKEY      = 0x00000002;
94  
95      /* The input device is a touchscreen or a touchpad (either single-touch or multi-touch). */
96      constexpr uint32_t INPUT_DEVICE_CLASS_TOUCH         = 0x00000004;
97  
98      /* The input device is a cursor device such as a trackball or mouse. */
99      constexpr uint32_t INPUT_DEVICE_CLASS_CURSOR        = 0x00000008;
100  
101      /* The input device is a multi-touch touchscreen. */
102      constexpr uint32_t INPUT_DEVICE_CLASS_TOUCH_MT      = 0x00000010;
103  
104      /* The input device is a directional pad (implies keyboard, has DPAD keys). */
105      constexpr uint32_t INPUT_DEVICE_CLASS_DPAD          = 0x00000020;
106  
107      /* The input device is a gamepad (implies keyboard, has BUTTON keys). */
108      constexpr uint32_t INPUT_DEVICE_CLASS_GAMEPAD       = 0x00000040;
109  
110      /* The input device has switches. */
111      constexpr uint32_t INPUT_DEVICE_CLASS_SWITCH        = 0x00000080;
112  
113      /* The input device is a joystick (implies gamepad, has joystick absolute axes). */
114      constexpr uint32_t INPUT_DEVICE_CLASS_JOYSTICK      = 0x00000100;
115  
116      /* The input device has a vibrator (supports FF_RUMBLE). */
117      constexpr uint32_t INPUT_DEVICE_CLASS_VIBRATOR      = 0x00000200;
118  
119      /* The input device has a microphone. */
120      constexpr uint32_t INPUT_DEVICE_CLASS_MIC           = 0x00000400;
121  
122      /* The input device is an external stylus (has data we want to fuse with touch data). */
123      constexpr uint32_t INPUT_DEVICE_CLASS_EXTERNAL_STYLUS = 0x00000800;
124  
125      /* The input device has a rotary encoder. */
126      constexpr uint32_t INPUT_DEVICE_CLASS_ROTARY_ENCODER = 0x00001000;
127  
128      /* The input device is virtual (not a real device, not part of UI configuration). */
129      constexpr uint32_t INPUT_DEVICE_CLASS_VIRTUAL       = 0x40000000;
130  
131      /* The input device is external (not built-in). */
132      constexpr uint32_t INPUT_DEVICE_CLASS_EXTERNAL      = 0x80000000;
133  
134      constexpr uint32_t MAX_SIZE_OF_DEVICE_NAME = UINPUT_MAX_NAME_SIZE - 1;
135  
136      const std::string DH_ID_PREFIX = "Input_";
137  
138      const std::string DINPUT_SPLIT_COMMA = ", ";
139  
140      const char VIR_NODE_SPLIT_CHAR = '|';
141      const std::string VIR_NODE_SPLIT = "|";
142      const std::string VIR_NODE_PID_SPLIT = "/";
143      const uint32_t VIR_NODE_PHY_LEN = 3;
144      const uint32_t VIR_NODE_PHY_DEVID_IDX = 1;
145      const uint32_t VIR_NODE_PHY_DHID_IDX = 2;
146  
147      const std::string SOURCE_DEVICE_ID = "sourceDevId";
148  
149      const std::string SINK_DEVICE_ID = "sinkDevId";
150  
151      const std::string SOURCE_WINDOW_ID = "sourceWinId";
152  
153      const std::string SINK_SHOW_WINDOW_ID = "sinkShowWinId";
154  
155      const std::string SOURCE_WINDOW_WIDTH = "sourceWinWidth";
156  
157      const std::string SOURCE_WINDOW_HEIGHT = "sourceWinHeight";
158  
159      const std::string SINK_PROJECT_SHOW_WIDTH = "sinkProjShowWidth";
160  
161      const std::string SINK_PROJECT_SHOW_HEIGHT = "sinkProjShowHeight";
162  
163      const std::string SINK_WINDOW_SHOW_X = "sinkWinShowX";
164  
165      const std::string SINK_WINDOW_SHOW_Y = "sinkWinShowY";
166  
167      const std::string DEVICE_NAME = "name";
168  
169      const std::string PHYSICAL_PATH = "physicalPath";
170  
171      const std::string UNIQUE_ID = "uniqueId";
172  
173      const std::string BUS = "bus";
174  
175      const std::string VENDOR = "vendor";
176  
177      const std::string PRODUCT = "product";
178  
179      const std::string VERSION = "version";
180  
181      const std::string DESCRIPTOR = "descriptor";
182  
183      const std::string CLASSES = "classes";
184  
185      const std::string EVENT_TYPES = "eventTypes";
186  
187      const std::string EVENT_KEYS = "eventKeys";
188  
189      const std::string ABS_TYPES = "absTypes";
190  
191      const std::string ABS_INFOS = "absInfos";
192  
193      const std::string REL_TYPES = "relTypes";
194  
195      const std::string PROPERTIES = "properties";
196  
197      const std::string MISCELLANEOUS = "miscellaneous";
198  
199      const std::string LEDS = "leds";
200  
201      const std::string REPEATS = "repeats";
202  
203      const std::string SWITCHS = "switchs";
204  
205      const std::string DH_TOUCH_PAD = "touchpad";
206  
207      const std::string DINPUT_LOG_TITLE_TAG = "DINPUT";
208  
209      constexpr const char* LATENCY_COUNT_THREAD_NAME = "latencyCount";
210  
211      constexpr const char* EVENT_INJECT_THREAD_NAME = "eventInject";
212  
213      constexpr const char* COLLECT_EVENT_THREAD_NAME = "collectEvents";
214  
215      constexpr const char* CHECK_KEY_STATUS_THREAD_NAME = "checkKeyStatus";
216  
217      constexpr int32_t LOG_MAX_LEN = 4096;
218  
219      constexpr uint32_t SCREEN_ID_DEFAULT = 0;
220  
221      constexpr uint32_t DEFAULT_VALUE = 0;
222  
223      constexpr int32_t UN_INIT_FD_VALUE = -1;
224  
225      constexpr int32_t SINK_SCREEN_INFO_SIZE = 4;
226  
227      constexpr int32_t MAX_LOG_TIMES = 20;
228  
229      enum class EHandlerMsgType {
230          DINPUT_SINK_EVENT_HANDLER_MSG = 1,
231          DINPUT_SOURCE_EVENT_HANDLER_MSG = 2
232      };
233  
234      struct BusinessEvent {
235          std::vector<int32_t> pressedKeys;
236          int32_t keyCode;
237          int32_t keyAction;
238      };
239  
240      struct TouchScreenEvent {
241          uint32_t absX;
242          uint32_t absY;
243      };
244  
245      /*
246       * A raw event as retrieved from the input_event.
247       */
248      struct RawEvent {
249          int64_t when;
250          uint32_t type;
251          uint32_t code;
252          int32_t value;
253          std::string descriptor;
254          std::string path;
255  
256          bool operator == (const RawEvent &e)
257          {
258              return this->type == e.type && this->code == e.code &&
259                  this->descriptor == e.descriptor && this->path == e.path;
260          }
261      };
262  
263      /*
264       * Input device Info retrieved from the kernel.
265       */
266      struct InputDevice {
InputDeviceInputDevice267          inline InputDevice() : name(""), physicalPath(""), uniqueId(""), bus(0), vendor(0), product(0),
268              version(0), descriptor(""), classes(0) {}
269          std::string name;
270          std::string physicalPath;
271          std::string uniqueId;
272          uint16_t bus;
273          uint16_t vendor;
274          uint16_t product;
275          uint16_t version;
276          std::string descriptor;
277          uint32_t classes;
278          std::vector<uint32_t> eventTypes;
279          std::vector<uint32_t> eventKeys;
280          std::vector<uint32_t> absTypes;
281          std::map<uint32_t, std::vector<int32_t>> absInfos;
282          std::vector<uint32_t> relTypes;
283          std::vector<uint32_t> properties;
284  
285          std::vector<uint32_t> miscellaneous;
286          std::vector<uint32_t> leds;
287          std::vector<uint32_t> switchs;
288          std::vector<uint32_t> repeats;
289      };
290  
291      /*
292       * Distributed Hardware Handle
293       */
294      struct HardwareHandle {
295          // equipment ID
296          std::string eqId;
297  
298          // Hardware ID
299          std::string hhId;
300  
301          // Hardware detailed information
302          std::string hdInfo;
303      };
304  
305      // Synthetic raw event type codes produced when devices are added or removed.
306      enum class DeviceType {
307          // Sent when a device is added.
308          DEVICE_ADDED = 0x10000000,
309  
310          // Sent when a device is removed.
311          DEVICE_REMOVED = 0x20000000,
312  
313          // Sent when all added/removed devices from the most recent scan have been reported.
314          // This event is always sent at least once.
315          FINISHED_DEVICE_SCAN = 0x30000000,
316  
317          FIRST_SYNTHETIC_EVENT = DEVICE_ADDED,
318      };
319  
320      /*
321       * Input device connection status
322       */
323      struct InputDeviceEvent {
324          DeviceType type;
325          InputDevice deviceInfo;
326      };
327  
328      /*
329       * Device Type definitions
330       */
331      enum class DInputServerType {
332          /*
333           * null server
334           */
335          NULL_SERVER_TYPE = 0,
336  
337          /*
338           * source server
339           */
340          SOURCE_SERVER_TYPE = 1,
341  
342          /*
343           * sink server.
344           */
345          SINK_SERVER_TYPE = 2,
346      };
347  
348      // Current Input Session Status
349      enum class SessionStatus : uint32_t {
350          CLOSED = 0x00,
351          OPENING = 0x01,
352          OPENED = 0x02,
353          CLOSING = 0x03,
354      };
355  } // namespace DistributedInput
356  } // namespace DistributedHardware
357  } // namespace OHOS
358  
359  #endif // OHOS_DISTRIBUTED_INPUT_CONSTANTS_H
360