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