1 /*
2 * Copyright (c) 2021 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 POINTER_EVENT_H
17 #define POINTER_EVENT_H
18
19 #include <array>
20 #include <list>
21 #include <vector>
22 #include <memory>
23 #include <map>
24 #include <set>
25 #include "parcel.h"
26 #include "input_event.h"
27 #include "nocopyable.h"
28
29 namespace OHOS {
30 namespace MMI {
31 class PointerEvent : public InputEvent {
32 public:
33 /**
34 * Indicates an unknown pointer action. It is usually used as initial value.
35 *
36 * @since 8
37 */
38 static constexpr int32_t POINTER_ACTION_UNKNOWN = 0;
39
40 /**
41 * Indicates a pointer action that has been canceled.
42 *
43 * @since 8
44 */
45 static constexpr int32_t POINTER_ACTION_CANCEL = 1;
46
47 /**
48 * Indicates a pointer action representing that a funger is pressed on a touchscreen or touchpad.
49 *
50 * @since 8
51 */
52 static constexpr int32_t POINTER_ACTION_DOWN = 2;
53
54 /**
55 * Indicates a pointer action representing that a funger moves on a touchscreen or touchpad or a mouse pointer moves.
56 *
57 * @since 8
58 */
59 static constexpr int32_t POINTER_ACTION_MOVE = 3;
60
61 /**
62 * Indicates a pointer action representing that a funger leaves the touchscreen or touchpad.
63 *
64 * @since 8
65 */
66 static constexpr int32_t POINTER_ACTION_UP = 4;
67
68 /**
69 * Indicates the start action of the axis event related to the pointer.
70 *
71 * @since 8
72 */
73 static constexpr int32_t POINTER_ACTION_AXIS_BEGIN = 5;
74
75 /**
76 * Indicates the update action of the axis event related to the pointer.
77 *
78 * @since 8
79 */
80 static constexpr int32_t POINTER_ACTION_AXIS_UPDATE = 6;
81
82 /**
83 * Indicates the end action of the axis event related to the pointer.
84 *
85 * @since 8
86 */
87 static constexpr int32_t POINTER_ACTION_AXIS_END = 7;
88
89 /**
90 * Indicates a pointer action representing that a button is pressed.
91 *
92 * @since 8
93 */
94 static constexpr int32_t POINTER_ACTION_BUTTON_DOWN = 8;
95
96 /**
97 * Indicates a pointer action representing that a button is released.
98 *
99 * @since 8
100 */
101 static constexpr int32_t POINTER_ACTION_BUTTON_UP = 9;
102
103 enum AxisType {
104 /**
105 * Indicates an unknown axis type. It is generally used as the initial value.
106 *
107 * @since 8
108 */
109 AXIS_TYPE_UNKNOWN,
110
111 /**
112 * Indicates the vertical scroll axis. When you scrall the mouse wheel or make certain gestures on the touchpad, the status of the vertical scroll axis changes.
113 *
114 * @since 8
115 */
116 AXIS_TYPE_SCROLL_VERTICAL,
117
118 /**
119 * Indicates the horizontal scroll axis. When you scrall the mouse wheel or make certain gestures on the touchpad, the status of the horizontal scroll axis changes.
120 *
121 * @since 8
122 */
123 AXIS_TYPE_SCROLL_HORIZONTAL,
124
125 /**
126 * Indicates the pinch axis, which is used to describe a pinch gesture on the touchscreen or touchpad.
127 *
128 * @since 8
129 */
130 AXIS_TYPE_PINCH,
131
132 /**
133 * Indicates the maximum number of defined axis types.
134 *
135 * @since 8
136 */
137 AXIS_TYPE_MAX
138 };
139
140 /**
141 * Indicates an unknown input source type. It is usually used as the initial value.
142 *
143 * @since 8
144 */
145 static constexpr int32_t SOURCE_TYPE_UNKNOWN = 0;
146
147 /**
148 * Indicates that the input source generates events similar to mouse cursor movement, button press and release, and wheel scrolling.
149 *
150 * @since 8
151 */
152 static constexpr int32_t SOURCE_TYPE_MOUSE = 1;
153
154 /**
155 * Indicates that the input source generates a touchscreen multi-touch event.
156 *
157 * @since 8
158 */
159 static constexpr int32_t SOURCE_TYPE_TOUCHSCREEN = 2;
160
161 /**
162 * Indicates that the input source generates a touchpad multi-touch event.
163 *
164 * @since 8
165 */
166 static constexpr int32_t SOURCE_TYPE_TOUCHPAD = 3;
167
168 /**
169 * Indicates an invalid button ID.
170 *
171 * @since 8
172 */
173 static constexpr int32_t BUTTON_NONE = -1;
174
175 /**
176 * Indicates the left button on a mouse.
177 *
178 * @since 8
179 */
180 static constexpr int32_t MOUSE_BUTTON_LEFT = 0;
181
182 /**
183 * Indicates the right button on a mouse.
184 *
185 * @since 8
186 */
187 static constexpr int32_t MOUSE_BUTTON_RIGHT = 1;
188
189 /**
190 * Indicates the middle button on a mouse.
191 *
192 * @since 8
193 */
194 static constexpr int32_t MOUSE_BUTTON_MIDDLE = 2;
195
196 public:
197 static std::shared_ptr<PointerEvent> from(std::shared_ptr<InputEvent> inputEvent);
198
199 public:
200 class PointerItem {
201 public:
202 PointerItem();
203 ~PointerItem();
204
205 public:
206 /**
207 * @brief Obtains the ID of the pointer in this event.
208 * @return Returns the pointer ID.
209 * @since 8
210 */
211 int32_t GetPointerId() const;
212
213 /**
214 * @brief Sets the ID of the pointer in this event.
215 * @param pointerId Indicates the pointer ID to set.
216 * @return void
217 * @since 8
218 */
219 void SetPointerId(int32_t pointerId);
220
221 /**
222 * @brief Obtains the time when the pointer is pressed.
223 * @return Returns the time.
224 * @since 8
225 */
226 int64_t GetDownTime() const;
227
228 /**
229 * @brief Sets the time when the pointer is pressed.
230 * @param downTime Indicates the time to set.
231 * @return void
232 * @since 8
233 */
234 void SetDownTime(int64_t downTime);
235
236 /**
237 * @brief Checks whether the pointer is pressed.
238 * @return Returns <b>true</b> if the pointer is pressed; returns <b>false</b> otherwise.
239 * @since 8
240 */
241 bool IsPressed() const;
242
243 /**
244 * @brief Sets whether to enable the pressed state for the pointer.
245 * @param pressed Specifies whether to set the pressed state for the pointer. The value <b>true</b> means to set the pressed state for the pointer, and the <b>false</b> means the opposite.
246 * @return void
247 * @since 8
248 */
249 void SetPressed(bool pressed);
250
251 /**
252 * @brief Obtains the x coordinate relative to the upper left corner of the screen.
253 * For a touchpad input event, the value is the absolute x coordinate on the touchpad. For other pointer input events, the value is the x coordinate on the target screen.
254 * @return Returns the x coordinate.
255 * @since 8
256 */
257 int32_t GetGlobalX() const;
258
259 /**
260 * @brief Sets the x coordinate relative to the upper left corner of the screen.
261 * @param globalX Indicates the x coordinate to set.
262 * @return void
263 * @since 8
264 */
265 void SetGlobalX(int32_t globalX);
266
267 /**
268 * @brief Obtains the y coordinate relative to the upper left corner of the screen.
269 * For a touchpad input event, the value is the absolute y coordinate on the touchpad. For other pointer input events, the value is the y coordinate on the target screen.
270 * @return Returns the y coordinate.
271 * @since 8
272 */
273 int32_t GetGlobalY() const;
274
275 /**
276 * @brief Sets the y coordinate relative to the upper left corner of the screen.
277 * @param globalY Indicates the y coordinate to set.
278 * @return void
279 * @since 8
280 */
281 void SetGlobalY(int32_t globalY);
282
283 /**
284 * @brief Obtains the x coordinate of the active window.
285 * @return Returns the x coordinate.
286 * @since 8
287 */
288 int32_t GetLocalX() const;
289
290 /**
291 * @brief Sets the x coordinate of the active window.
292 * @param x Indicates the x coordinate to set.
293 * @return void
294 * @since 8
295 */
296 void SetLocalX(int32_t x);
297
298 /**
299 * @brief Obtains the y coordinate of the active window.
300 * @return Returns the y coordinate.
301 * @since 8
302 */
303 int32_t GetLocalY() const;
304
305 /**
306 * @brief Sets the y coordinate of the active window.
307 * @param y Indicates the y coordinate to set.
308 * @return void
309 * @since 8
310 */
311 void SetLocalY(int32_t y);
312
313 /**
314 * @brief Obtains the width of the pressed area.
315 * @return Returns the width.
316 * @since 8
317 */
318 int32_t GetWidth() const;
319
320 /**
321 * @brief Sets the width of the pressed area.
322 * @param width Indicates the width to set.
323 * @return void
324 * @since 8
325 */
326 void SetWidth(int32_t width);
327
328 /**
329 * @brief Obtains the height of the pressed area.
330 * @return Returns the height.
331 * @since 8
332 */
333 int32_t GetHeight() const;
334
335 /**
336 * @brief Sets the height of the pressed area.
337 * @param height Indicates the height to set.
338 * @return void
339 * @since 8
340 */
341 void SetHeight(int32_t height);
342
343 /**
344 * @brief Obtains the pressure in this event.
345 * @return Returns the pressure.
346 * @since 8
347 */
348 int32_t GetPressure() const;
349
350 /**
351 * @brief Sets the pressure for this event.
352 * @param pressure Indicates the pressure to set.
353 * @return void
354 * @since 8
355 */
356 void SetPressure(int32_t pressure);
357
358 /**
359 * @brief Obtains the ID of the current device.
360 * @return Returns the device ID.
361 * @since 8
362 */
363 int32_t GetDeviceId() const;
364
365 /**
366 * @brief Sets the ID for the current device.
367 * @param deviceId Indicates the device ID to set.
368 * @return void
369 * @since 8
370 */
371 void SetDeviceId(int32_t deviceId);
372 public:
373 /**
374 * @brief Writes data to a <b>Parcel</b> obejct.
375 * @param out Indicates the object into which data will be written.
376 * @return Returns <b>true</b> if the data is successfully written; returns <b>false</b> otherwise.
377 * @since 8
378 */
379 bool WriteToParcel(Parcel &out) const;
380
381 /**
382 * @brief Reads data from a <b>Parcel</b> obejct.
383 * @param in Indicates the object from which data will be read.
384 * @return Returns <b>true</b> if the data is successfully read; returns <b>false</b> otherwise.
385 * @since 8
386 */
387 bool ReadFromParcel(Parcel &in);
388
389 private:
390 int32_t pointerId_ { 0 };
391 bool pressed_ { false };
392 int32_t globalX_ { 0 };
393 int32_t globalY_ { 0 };
394 int32_t localX_ { 0 };
395 int32_t localY_ { 0 };
396 int32_t width_ { 0 };
397 int32_t height_ { 0 };
398 int32_t pressure_ { 0 };
399 int32_t deviceId_ { 0 };
400 int64_t downTime_ { 0 };
401 };
402
403 public:
404 PointerEvent(const PointerEvent& other);
405 virtual ~PointerEvent();
406 PointerEvent& operator=(const PointerEvent& other) = delete;
407 DISALLOW_MOVE(PointerEvent);
408
409 static std::shared_ptr<PointerEvent> Create();
410
411 /**
412 * @brief Obtains the pointer action in this event.
413 * @return Returns the pointer action.
414 * @since 8
415 */
416 int32_t GetPointerAction() const;
417
418 /**
419 * @brief Sets a pointer action for this event.
420 * @param pointerAction Indicates the pointer action to set.
421 * @return void
422 * @since 8
423 */
424 void SetPointerAction(int32_t pointerAction);
425
426 /**
427 * @brief Dumps the action of this pointer input event as a string.
428 * @return Returns the pointer to the string.
429 * @since 8
430 */
431 const char* DumpPointerAction() const;
432
433 /**
434 * @brief Obtains the pointer ID in this event.
435 * @return Returns the pointer ID.
436 * @since 8
437 */
438 int32_t GetPointerId() const;
439
440 /**
441 * @brief Sets an ID for the pointer in this event.
442 * @param pointerId Indicates the pointer ID to set.
443 * @return void
444 * @since 8
445 */
446 void SetPointerId(int32_t pointerId);
447
448 /**
449 * @brief Obtains the pointer item of a specified pointer ID.
450 * @param pointerId Indicates the pointer ID.
451 * @param pointerItem Indicates the item used to receive the data of the pointer.
452 * @return Returns <b>true</b> if the data of the pointer with the specified ID exists; returns <b>false</b> otherwise.
453 * @since 8
454 */
455 bool GetPointerItem(int32_t pointerId, PointerItem &pointerItem);
456
457 /**
458 * @brief Adds a pointer item.
459 * @param pointerItem Indicates the pointer item to add.
460 * @return void
461 * @since 8
462 */
463 void AddPointerItem(PointerItem &pointerItem);
464
465 /**
466 * @brief Removes a pointer item based on the pointer ID.
467 * @param pointerId Indicates the ID of the pointer from which the pointer item is to be removed.
468 * @return void
469 * @since 8
470 */
471 void RemovePointerItem(int32_t pointerId);
472
473 /**
474 * @brief Updates a pointer item based on the pointer ID.
475 * @param pointerId Indicates the ID of the pointer from which the pointer item is to be updated.
476 * @param pointerItem Indicates the pointer item to update.
477 * @return void
478 * @since 8
479 */
480 void UpdatePointerItem(int32_t pointerId, PointerItem &pointerItem);
481
482 /**
483 * @brief Obtains the set of pressed buttons.
484 * @return Returns the pressed buttons.
485 * @since 8
486 */
487 std::set<int32_t> GetPressedButtons() const;
488
489 /**
490 * @brief Checks whether a specified button is being pressed.
491 * @param buttonId Indicates the button ID.
492 * @return Returns <b>true</b> if the button is being pressed; returns <b>false</b> otherwise.
493 * @since 8
494 */
495 bool IsButtonPressed(int32_t buttonId) const;
496
497 /**
498 * @brief Sets the pressed state for a button.
499 * @param buttonId Indicates the button ID of the button to be set in the pressed state.
500 * @return void
501 * @since 8
502 */
503 void SetButtonPressed(int32_t buttonId);
504
505 /**
506 * @brief Deletes a released button.
507 * @param buttonId Indicates the button ID of the button.
508 * @return void
509 * @since 8
510 */
511 void DeleteReleaseButton(int32_t buttonId);
512
513 /**
514 * @brief Clears the button in the pressed state.
515 * @return void
516 * @since 8
517 */
518 void ClearButtonPressed();
519
520 /**
521 * @brief Obtains all pointers in this event.
522 * @return Returns all the pointer IDs.
523 * @since 8
524 */
525 std::vector<int32_t> GetPointersIdList() const;
526
527 /**
528 * @brief Obtains the source type of this event.
529 * @return Returns the source type.
530 * @since 8
531 */
532 int32_t GetSourceType() const;
533
534 /**
535 * @brief Sets the source type for this event.
536 * @param sourceType Indicates the source type to set.
537 * @return void
538 * @since 8
539 */
540 void SetSourceType(int32_t sourceType);
541
542 /**
543 * @brief Dumps the source type of this pointer input event as a string.
544 * @return Returns the pointer to the string.
545 * @since 8
546 */
547 const char* DumpSourceType() const;
548
549 /**
550 * @brief Obtains the button ID in this event.
551 * @return Returns the button ID.
552 * @since 8
553 */
554 int32_t GetButtonId() const;
555
556 /**
557 * @brief Sets the button ID for this event.
558 * @param buttonId Indicates the button ID to set.
559 * @return void
560 * @since 8
561 */
562 void SetButtonId(int32_t buttonId);
563
564 /**
565 * @brief Obtains the axis value.
566 * @param axis Indicates the axis type.
567 * @return Returns the axis value.
568 * @since 8
569 */
570 double GetAxisValue(AxisType axis) const;
571
572 /**
573 * @brief Sets the axis value.
574 * @param axis Indicates the axis type.
575 * @param axisValue Indicates the axis value to set.
576 * @return void
577 * @since 8
578 */
579 void SetAxisValue(AxisType axis, double axisValue);
580
581 /**
582 * @brief Checks whether this event contains a specified axis type.
583 * @param axis Indicates the axis type.
584 * @return Returns <b>true</b> if the event contains the specified axis type; returns <b>false</b> otherwise.
585 * @since 8
586 */
587 bool HasAxis(AxisType axis) const;
588
589 /**
590 * @brief Obtains all axis of this event.
591 * @return Returns all the axis, Each bit indicates an axis.
592 * @since 8
593 */
594 int32_t GetAxes() const;
595
596 /**
597 * @brief Set the front keys in the key combination.
598 * @param pressedKeys Indicates the front keys to set.
599 * @return void.
600 * @since 8
601 */
602 void SetPressedKeys(const std::vector<int32_t> pressedKeys);
603
604 /**
605 * @brief Obtains the set of pressed keys.
606 * @return Returns the pressed keys.
607 * @since 8
608 */
609 std::vector<int32_t> GetPressedKeys() const;
610
611 /**
612 * @brief Checks whether this input event is valid.
613 * @return Returns <b>true</b> if the input event is valid; returns <b>false</b> otherwise.
614 * @since 8
615 */
616 bool IsValid() const;
617 public:
618 /**
619 * @brief Checks whether the axes set represented by <b>axes</b> contains a specified type of axis.
620 * @param axes Indicates the set of axes. Each bit indicates an axis.
621 * @param axis Indicates the type of the axis to check.
622 * @return Returns <b>true</b> if the axes set contains the specified axis type; returns <b>false</b> otherwise.
623 * @since 8
624 */
625 static bool HasAxis(uint32_t axes, AxisType axis);
626
627 public:
628 /**
629 * @brief Writes data to a <b>Parcel</b> obejct.
630 * @param out Indicates the object into which data will be written.
631 * @return Returns <b>true</b> if the data is successfully written; returns <b>false</b> otherwise.
632 * @since 8
633 */
634 bool WriteToParcel(Parcel &out) const;
635
636 /**
637 * @brief Reads data from a <b>Parcel</b> obejct.
638 * @param in Indicates the object from which data will be read.
639 * @return Returns <b>true</b> if the data is successfully read; returns <b>false</b> otherwise.
640 * @since 8
641 */
642 bool ReadFromParcel(Parcel &in);
643
644 protected:
645 explicit PointerEvent(int32_t eventType);
646
647 private:
648 bool IsValidCheckMouseFunc() const;
649 bool IsValidCheckMouse() const;
650 bool IsValidCheckTouchFunc() const;
651 bool IsValidCheckTouch() const;
652
653 private:
654 int32_t pointerId_ { 0 };
655 std::list<PointerItem> pointers_;
656 std::set<int32_t> pressedButtons_;
657 int32_t sourceType_ { 0 };
658 int32_t pointerAction_ { 0 };
659 int32_t buttonId_ { -1 };
660 uint32_t axes_ { 0 };
661 std::array<double, AXIS_TYPE_MAX> axisValues_ { };
662 std::vector<int32_t> pressedKeys_;
663 };
664
HasAxis(AxisType axis)665 inline bool PointerEvent::HasAxis(AxisType axis) const
666 {
667 return HasAxis(axes_, axis);
668 }
669
GetAxes()670 inline int32_t PointerEvent::GetAxes() const
671 {
672 return axes_;
673 }
674 } // namespace MMI
675 } // namespace OHOS
676 #endif // POINTER_EVENT_H