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 INPUT_EVENT_H
17 #define INPUT_EVENT_H
18 
19 #include <functional>
20 #include <memory>
21 #include <mutex>
22 
23 #include "nocopyable.h"
24 #include "parcel.h"
25 
26 namespace OHOS {
27 namespace MMI {
28 class InputEvent {
29 public:
30     /**
31      * Unknown action. It is usually used as the initial value.
32      *
33      * @since 9
34      */
35     static constexpr int32_t ACTION_UNKNOWN = 0;
36 
37     /**
38      * Cancel the action. It indicates that a consecutive input event is cancelled.
39      *
40      * @since 9
41      */
42     static constexpr int32_t ACTION_CANCEL = 1;
43 
44     /**
45      * The actual type of the current input event is the basic type (InputEvent type).
46      *
47      * @since 9
48      */
49     static constexpr int32_t EVENT_TYPE_BASE = 0X00000000;
50 
51     /**
52      * The actual type of the current input event is KeyEvent or its derived class.
53      *
54      * @since 9
55      */
56     static constexpr int32_t EVENT_TYPE_KEY = 0X00010000;
57 
58     /**
59      * The actual type of the current input event is PointerEvent or its derived class.
60      *
61      * @since 9
62      */
63     static constexpr int32_t EVENT_TYPE_POINTER = 0X00020000;
64 
65     /**
66      * The actual type of the current input event is AxisEvent or its derived class.
67      *
68      * @since 9
69      */
70     static constexpr int32_t EVENT_TYPE_AXIS = 0X00030000;
71 
72     /**
73      * The actual type of the current input event is FingerprintEvent or its derived class.
74      *
75      * @since 12
76      */
77     static constexpr int32_t EVENT_TYPE_FINGERPRINT = 0X00040000;
78 
79     /**
80      * The multimodal input service sends input events to the interceptor and listener. This is the default value.
81      *
82      * @since 9
83      */
84     static constexpr uint32_t EVENT_FLAG_NONE = 0x00000000;
85 
86     /**
87      * The multimodal input service does not intercept the input event.
88      *
89      * @since 9
90      */
91     static constexpr uint32_t EVENT_FLAG_NO_INTERCEPT = 0x00000001;
92 
93     /**
94      * The multimodal input service does not listen for the input event.
95      *
96      * @since 9
97      */
98     static constexpr uint32_t EVENT_FLAG_NO_MONITOR = 0x00000002;
99 
100     /**
101      * The multimodal input event from simulation.
102      *
103      * @since 10
104      */
105     static constexpr uint32_t EVENT_FLAG_SIMULATE = 0x00000004;
106 
107     /**
108      * The multimodal input service hide pointer.
109      *
110      * @since 12
111      */
112     static constexpr uint32_t EVENT_FLAG_HIDE_POINTER = 0x00000008;
113 
114     static constexpr uint32_t EVENT_FLAG_RAW_POINTER_MOVEMENT = 0x00000010;
115     static constexpr uint32_t EVENT_FLAG_TOUCHPAD_POINTER = 0x00000020;
116     static constexpr uint32_t EVENT_FLAG_PRIVACY_MODE = 0x00000040;
117     static constexpr uint32_t EVENT_FLAG_SHELL = 0x00000080;
118     static constexpr uint32_t EVENT_FLAG_ACCESSIBILITY = 0x00000100;
119 
120     /**
121      * The multimodal input event from navigation window.
122      *
123      * @since 12
124      */
125     static constexpr uint32_t EVENT_FLAG_SIMULATE_NAVIGATION = 0x00000200;
126 
127     static constexpr uint32_t EVENT_FLAG_GENERATE_FROM_REAL = 0x00000400;
128 
129 public:
130     /**
131      * Copy constructor function for InputEvent
132      *
133      * @since 9
134      */
135     InputEvent(const InputEvent& other);
136 
137     /**
138      * Virtual destructor of InputEvent
139      *
140      * @since 9
141      */
142     virtual ~InputEvent();
143 
144     virtual InputEvent& operator=(const InputEvent& other) = delete;
145     DISALLOW_MOVE(InputEvent);
146 
147     /**
148      * Create InputEvent object
149      *
150      * @since 9
151      */
152     static std::shared_ptr<InputEvent> Create();
153 
154     /**
155      * @brief Converts an input event type into a string.
156      * @param Indicates the input event type.
157      * @return Returns the string converted from the input event type.
158      * @since 9
159      */
160     static const char* EventTypeToString(int32_t eventType);
161 
162     /**
163      * @brief Resets an input event to the initial state.
164      * @return void
165      * @since 9
166      */
167     virtual void Reset();
168 
169     /**
170      * @brief Obtains the unique ID of an input event.
171      * @return Returns the unique ID of the input event.
172      * @since 9
173      */
174     int32_t GetId() const;
175 
176     /**
177      * @brief Sets the unique ID of an input event.
178      * @param id Indicates the unique ID.
179      * @return void
180      * @since 9
181      */
182     void SetId(int32_t id);
183 
184     /**
185      * @brief Updates the unique ID of an input event.
186      * @return void
187      * @since 9
188      */
189     void UpdateId();
190 
191     /**
192      * @brief Obtains the time when the action for this input event occurs.
193      * @return Returns the time when the action for this input event occurs.
194      * @since 9
195      */
196     int64_t GetActionTime() const;
197 
198     /**
199      * @brief Sets the time when the action for this input event occurs.
200      * @param actionTime Indicates the time when the action for this input event occurs.
201      * @return void
202      * @since 9
203      */
204     void SetActionTime(int64_t actionTime);
205 
206     /**
207      * @brief Get the time when sensor perceive the event.
208      * @param sensorTime Indicates the time when sensor event occurs.
209      * @return void
210      * @since 9
211      */
212     void SetSensorInputTime(uint64_t sensorTime);
213 
214     /**
215      * @brief Set the time for sensor when the action for this input event occurs.
216      * @return Returns the time when sensor perceive the event.
217      * @since 9
218      */
219     uint64_t GetSensorInputTime();
220 
221     /**
222      * @brief Obtains the action for this input event.
223      * @return Returns the action for this input event.
224      * @since 9
225      */
226     int32_t GetAction() const;
227 
228     /**
229      * @brief Sets the action for this input event.
230      * @param action Indicates the action for the input event.
231      * @return void
232      * @since 9
233      */
234     void SetAction(int32_t action);
235 
236     /**
237      * @brief Obtains the time when the action for the first input event in a series of related input events occurs.
238      * @return Returns the time when the action for the first input event occurs.
239      * @since 9
240      */
241     int64_t GetActionStartTime() const;
242 
243     /**
244      * @brief Sets the time when the action for the first input event in a series of related input events occurs.
245      * @param time Indicates the time when the action for the first input event occurs.
246      * @return void
247      * @since 9
248      */
249     void SetActionStartTime(int64_t time);
250 
251     /**
252      * @brief Obtains the unique ID of the device that generates this input event.
253      * @return Returns the unique ID of the device.
254      * @since 9
255      */
256     int32_t GetDeviceId() const;
257 
258     /**
259      * @brief Sets the unique ID of the device that generates this input event.
260      * @param deviceId Indicates the unique ID of the device.
261      * @return void
262      * @since 9
263      */
264     void SetDeviceId(int32_t deviceId);
265 
266     /**
267      * @brief Obtains the ID of the target display for an input event.
268      * @return Returns the ID of the target display.
269      * @since 9
270      */
271     int32_t GetTargetDisplayId() const;
272 
273     /**
274      * @brief Sets the ID of the target screen for an input event.
275      * @param displayId Indicates the ID of the target display.
276      * @return void
277      * @since 9
278      */
279     void SetTargetDisplayId(int32_t displayId);
280 
281     /**
282      * @brief Obtains the ID of the target window for an input event.
283      * @return Returns the ID of the target window.
284      * @since 9
285      */
286     int32_t GetTargetWindowId() const;
287 
288     /**
289      * @brief Sets the ID of the target window for an input event.
290      * @param windowId Indicates the ID of the target window.
291      * @return void
292      * @since 9
293      */
294     void SetTargetWindowId(int32_t windowId);
295 
296     /**
297      * @brief Obtains the ID of the agent window for an input event.
298      * @return Returns the ID of the agent window.
299      * @since 9
300      */
301     int32_t GetAgentWindowId() const;
302 
303     /**
304      * @brief Sets the ID of the agent window for an input event.
305      * @param windowId Indicates the ID of the agent window.
306      * @return void
307      * @since 9
308      */
309     void SetAgentWindowId(int32_t windowId);
310 
311     /**
312      * @brief Obtains the type of the this input event.
313      * @return Returns the type of the this input event.
314      * @since 9
315      */
316     int32_t GetEventType() const;
317 
318     /**
319      * @brief Obtains all flags of an input event.
320      * @return Returns the flags of the input event.
321      * @since 9
322      */
323     uint32_t GetFlag() const;
324 
325     /**
326      * @brief Checks whether a flag has been set for an input event.
327      * @param flag Indicates the flag of the input event.
328      * @return Returns <b>true</b> if a flag has been set; returns <b>false</b> otherwise.
329      * @since 9
330      */
331     bool HasFlag(uint32_t flag);
332 
333     /**
334      * @brief Adds a flag for an input event.
335      * @param flag Indicates the flag of the input event.
336      * @return void
337      * @since 9
338      */
339     void AddFlag(uint32_t flag);
340 
341     /**
342      * @brief Clears all flags of an input event.
343      * @return void
344      * @since 9
345      */
346     void ClearFlag();
347     void ClearFlag(uint32_t flag);
348 
349     /**
350      * @brief Marks an input event as completed.
351      * This method can only be called once.
352      * @return void
353      * @since 9
354      */
355     void MarkProcessed();
356 
357     /**
358      * @brief Sets the callback invoked when an input event is processed.
359      * This method is not available for third-party applications.
360      * @return void
361      * @since 9
362      */
363     void SetProcessedCallback(std::function<void(int32_t, int64_t)> callback);
364 
365     /**
366      * @brief Sets the extra data of an input event.
367      * @param data the extra data.
368      * @param length data length in bytes.
369      * @return void
370      * @since 10
371      */
372     void SetExtraData(const std::shared_ptr<const uint8_t[]> data, uint32_t length);
373 
374     /**
375      * @brief Obtains the extra data of an input event.
376      * @param data the extra data.
377      * @param length data length in bytes.
378      * @return void
379      * @since 10
380      */
381     void GetExtraData(std::shared_ptr<const uint8_t[]> &data, uint32_t &length) const;
382 
383     /**
384      * @brief Checks whether the "Processed feedback" of this event is enabled or not.
385      * @return Returns <b>true</b> if we need a "Processed feedback" for this event;
386      * returns <b>false</b> otherwise.
387      * @since 12
388      */
389     bool IsMarkEnabled() const;
390 
391     /**
392      * @brief Sets the markEnabled_ field of an input event.
393      * @param markEnabled Indicates whether we need a "Processed feedback" or not for this event.
394      * @return void
395      * @since 12
396      */
397     void SetMarkEnabled(bool markEnabled);
398 
399     /**
400      * @brief Converts a input event action into a short string.
401      * @param Indicates the input event action.
402      * @return Returns the string converted from the input action.
403      * @since 12
404     */
405     static std::string_view ActionToShortStr(int32_t action);
406 public:
407     /**
408      * @brief Writes data to a <b>Parcel</b> object.
409      * @param out Indicates the object into which data will be written.
410      * @return Returns <b>true</b> if the data is successfully written; returns <b>false</b> otherwise.
411      * @since 9
412      */
413     bool WriteToParcel(Parcel &out) const;
414 
415     /**
416      * @brief Reads data from a <b>Parcel</b> object.
417      * @param in Indicates the object from which data will be read.
418      * @return Returns <b>true</b> if the data is successfully read; returns <b>false</b> otherwise.
419      * @since 9
420      */
421     bool ReadFromParcel(Parcel &in);
422 
423 protected:
424     /**
425      * @brief Constructs an input event object by using the specified input event type. Generally, this method
426      * is used to construct a base class object when constructing a derived class object.
427      * @since 9
428      */
429     explicit InputEvent(int32_t eventType);
430 
431 private:
432     int32_t eventType_ { -1 };
433     int32_t id_ { -1 };
434     int64_t actionTime_ { -1 };
435     uint64_t sensorInputTime_ { 0 };
436     int32_t action_ { -1 };
437     int64_t actionStartTime_ { -1 };
438     int32_t deviceId_ { -1 };
439     int32_t targetDisplayId_ { -1 };
440     int32_t targetWindowId_ { -1 };
441     int32_t agentWindowId_ { -1 };
442     uint32_t bitwise_ { 0 };
443     bool markEnabled_ { true };
444     std::function<void(int32_t, int64_t)> processedCallback_;
445     std::shared_ptr<const uint8_t[]> extraData_;
446     uint32_t extraDataLength_ { 0 };
447 };
448 } // namespace MMI
449 } // namespace OHOS
450 #endif // INPUT_EVENT_H