• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 multimodal input service sends input events to the interceptor and listener. This is the default value.
74      *
75      * @since 9
76      */
77     static constexpr uint32_t EVENT_FLAG_NONE = 0x00000000;
78 
79     /**
80      * The multimodal input service does not intercept the input event.
81      *
82      * @since 9
83      */
84     static constexpr uint32_t EVENT_FLAG_NO_INTERCEPT = 0x00000001;
85 
86     /**
87      * The multimodal input service does not listen for the input event.
88      *
89      * @since 9
90      */
91     static constexpr uint32_t EVENT_FLAG_NO_MONITOR = 0x00000002;
92 
93 public:
94     /**
95      * Copy constructor function for InputEvent
96      *
97      * @since 9
98      */
99     InputEvent(const InputEvent& other);
100 
101     /**
102      * Virtual destructor of InputEvent
103      *
104      * @since 9
105      */
106     virtual ~InputEvent();
107 
108     virtual InputEvent& operator=(const InputEvent& other) = delete;
109     DISALLOW_MOVE(InputEvent);
110 
111     /**
112      * Create InputEvent object
113      *
114      * @since 9
115      */
116     static std::shared_ptr<InputEvent> Create();
117 
118     /**
119      * @brief Converts an input event type into a string.
120      * @param Indicates the input event type.
121      * @return Returns the string converted from the input event type.
122      * @since 9
123      */
124     static const char* EventTypeToString(int32_t eventType);
125 
126     /**
127      * @brief Resets an input event to the initial state.
128      * @return void
129      * @since 9
130      */
131     virtual void Reset();
132 
133     /**
134      * @brief Obtains the unique ID of an input event.
135      * @return Returns the unique ID of the input event.
136      * @since 9
137      */
138     int32_t GetId() const;
139 
140     /**
141      * @brief Sets the unique ID of an input event.
142      * @param id Indicates the unique ID.
143      * @return void
144      * @since 9
145      */
146     void SetId(int32_t id);
147 
148     /**
149      * @brief Updates the unique ID of an input event.
150      * @return void
151      * @since 9
152      */
153     void UpdateId();
154 
155     /**
156      * @brief Obtains the time when the action for this input event occurs.
157      * @return Returns the time when the action for this input event occurs.
158      * @since 9
159      */
160     int64_t GetActionTime() const;
161 
162     /**
163      * @brief Sets the time when the action for this input event occurs.
164      * @param actionTime Indicates the time when the action for this input event occurs.
165      * @return void
166      * @since 9
167      */
168     void SetActionTime(int64_t actionTime);
169 
170     /**
171      * @brief Obtains the action for this input event.
172      * @return Returns the action for this input event.
173      * @since 9
174      */
175     int32_t GetAction() const;
176 
177     /**
178      * @brief Sets the action for this input event.
179      * @param action Indicates the action for the input event.
180      * @return void
181      * @since 9
182      */
183     void SetAction(int32_t action);
184 
185     /**
186      * @brief Obtains the time when the action for the first input event in a series of related input events occurs.
187      * @return Returns the time when the action for the first input event occurs.
188      * @since 9
189      */
190     int64_t GetActionStartTime() const;
191 
192     /**
193      * @brief Sets the time when the action for the first input event in a series of related input events occurs.
194      * @param time Indicates the time when the action for the first input event occurs.
195      * @return void
196      * @since 9
197      */
198     void SetActionStartTime(int64_t time);
199 
200     /**
201      * @brief Obtains the unique ID of the device that generates this input event.
202      * @return Returns the unique ID of the device.
203      * @since 9
204      */
205     int32_t GetDeviceId() const;
206 
207     /**
208      * @brief Sets the unique ID of the device that generates this input event.
209      * @param deviceId Indicates the unique ID of the device.
210      * @return void
211      * @since 9
212      */
213     void SetDeviceId(int32_t deviceId);
214 
215     /**
216      * @brief Obtains the ID of the target display for an input event.
217      * @return Returns the ID of the target display.
218      * @since 9
219      */
220     int32_t GetTargetDisplayId() const;
221 
222     /**
223      * @brief Sets the ID of the target screen for an input event.
224      * @param displayId Indicates the ID of the target display.
225      * @return void
226      * @since 9
227      */
228     void SetTargetDisplayId(int32_t displayId);
229 
230     /**
231      * @brief Obtains the ID of the target window for an input event.
232      * @return Returns the ID of the target window.
233      * @since 9
234      */
235     int32_t GetTargetWindowId() const;
236 
237     /**
238      * @brief Sets the ID of the target window for an input event.
239      * @param windowId Indicates the ID of the target window.
240      * @return void
241      * @since 9
242      */
243     void SetTargetWindowId(int32_t windowId);
244 
245     /**
246      * @brief Obtains the ID of the agent window for an input event.
247      * @return Returns the ID of the agent window.
248      * @since 9
249      */
250     int32_t GetAgentWindowId() const;
251 
252     /**
253      * @brief Sets the ID of the agent window for an input event.
254      * @param windowId Indicates the ID of the agent window.
255      * @return void
256      * @since 9
257      */
258     void SetAgentWindowId(int32_t windowId);
259 
260     /**
261      * @brief Obtains the type of the this input event.
262      * @return Returns the type of the this input event.
263      * @since 9
264      */
265     int32_t GetEventType() const;
266 
267     /**
268      * @brief Obtains all flags of an input event.
269      * @return Returns the flags of the input event.
270      * @since 9
271      */
272     uint32_t GetFlag() const;
273 
274     /**
275      * @brief Checks whether a flag has been set for an input event.
276      * @param flag Indicates the flag of the input event.
277      * @return Returns <b>true</b> if a flag has been set; returns <b>false</b> otherwise.
278      * @since 9
279      */
280     bool HasFlag(uint32_t flag);
281 
282     /**
283      * @brief Adds a flag for an input event.
284      * @param flag Indicates the flag of the input event.
285      * @return void
286      * @since 9
287      */
288     void AddFlag(uint32_t flag);
289 
290     /**
291      * @brief Clears all flags of an input event.
292      * @return void
293      * @since 9
294      */
295     void ClearFlag();
296 
297     /**
298      * @brief Marks an input event as completed.
299      * This method can only be called once.
300      * @return void
301      * @since 9
302      */
303     void MarkProcessed();
304 
305     /**
306      * @brief Sets the callback invoked when an input event is processed.
307      * This method is not available for third-party applications.
308      * @return void
309      * @since 9
310      */
311     void SetProcessedCallback(std::function<void(int32_t)> callback);
312 
313 public:
314     /**
315      * @brief Writes data to a <b>Parcel</b> object.
316      * @param out Indicates the object into which data will be written.
317      * @return Returns <b>true</b> if the data is successfully written; returns <b>false</b> otherwise.
318      * @since 9
319      */
320     bool WriteToParcel(Parcel &out) const;
321 
322     /**
323      * @brief Reads data from a <b>Parcel</b> object.
324      * @param in Indicates the object from which data will be read.
325      * @return Returns <b>true</b> if the data is successfully read; returns <b>false</b> otherwise.
326      * @since 9
327      */
328     bool ReadFromParcel(Parcel &in);
329 
330 protected:
331     /**
332      * @brief Constructs an input event object by using the specified input event type. Generally, this method
333      * is used to construct a base class object when constructing a derived class object.
334      * @since 9
335      */
336     explicit InputEvent(int32_t eventType);
337 
338 private:
339     int32_t eventType_;
340     int32_t id_;
341     int64_t actionTime_;
342     int32_t action_;
343     int64_t actionStartTime_;
344     int32_t deviceId_;
345     int32_t targetDisplayId_;
346     int32_t targetWindowId_;
347     int32_t agentWindowId_;
348     uint32_t bitwise_;
349     std::function<void(int32_t)> processedCallback_;
350 };
351 } // namespace MMI
352 } // namespace OHOS
353 #endif // INPUT_EVENT_H