1 /*
2 * Copyright (c) 2021-2025 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 #include "pointer_event.h"
17
18 #include "mmi_log.h"
19
20 #undef MMI_LOG_TAG
21 #define MMI_LOG_TAG "PointerEvent"
22
23 using namespace OHOS::HiviewDFX;
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr double MAX_PRESSURE { 1.0 };
28 constexpr size_t MAX_N_PRESSED_BUTTONS { 10 };
29 constexpr size_t MAX_N_POINTER_ITEMS { 10 };
30 constexpr int32_t SIMULATE_EVENT_START_ID { 10000 };
31 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
32 constexpr size_t MAX_N_ENHANCE_DATA_SIZE { 64 };
33 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
34 constexpr size_t MAX_N_BUFFER_SIZE { 512 };
35 constexpr size_t MAX_N_PRESSED_KEYS { 10 };
36 } // namespace
37
from(std::shared_ptr<InputEvent> inputEvent)38 std::shared_ptr<PointerEvent> PointerEvent::from(std::shared_ptr<InputEvent> inputEvent)
39 {
40 return nullptr;
41 }
42
PointerItem()43 PointerEvent::PointerItem::PointerItem() {}
44
~PointerItem()45 PointerEvent::PointerItem::~PointerItem() {}
46
GetPointerId() const47 int32_t PointerEvent::PointerItem::GetPointerId() const
48 {
49 return pointerId_;
50 }
51
SetPointerId(int32_t pointerId)52 void PointerEvent::PointerItem::SetPointerId(int32_t pointerId)
53 {
54 pointerId_ = pointerId;
55 }
56
GetDownTime() const57 int64_t PointerEvent::PointerItem::GetDownTime() const
58 {
59 return downTime_;
60 }
61
SetDownTime(int64_t downTime)62 void PointerEvent::PointerItem::SetDownTime(int64_t downTime)
63 {
64 downTime_ = downTime;
65 }
66
IsPressed() const67 bool PointerEvent::PointerItem::IsPressed() const
68 {
69 return pressed_;
70 }
71
SetPressed(bool pressed)72 void PointerEvent::PointerItem::SetPressed(bool pressed)
73 {
74 pressed_ = pressed;
75 }
76
GetDisplayX() const77 int32_t PointerEvent::PointerItem::GetDisplayX() const
78 {
79 return static_cast<int32_t>(GetDisplayXPos());
80 }
81
SetDisplayX(int32_t x)82 void PointerEvent::PointerItem::SetDisplayX(int32_t x)
83 {
84 SetDisplayXPos(x);
85 }
86
GetDisplayY() const87 int32_t PointerEvent::PointerItem::GetDisplayY() const
88 {
89 return static_cast<int32_t>(GetDisplayYPos());
90 }
91
SetDisplayY(int32_t y)92 void PointerEvent::PointerItem::SetDisplayY(int32_t y)
93 {
94 SetDisplayYPos(y);
95 }
96
GetWindowX() const97 int32_t PointerEvent::PointerItem::GetWindowX() const
98 {
99 return windowX_;
100 }
101
SetWindowX(int32_t x)102 void PointerEvent::PointerItem::SetWindowX(int32_t x)
103 {
104 windowX_ = x;
105 }
106
GetWindowY() const107 int32_t PointerEvent::PointerItem::GetWindowY() const
108 {
109 return windowY_;
110 }
111
SetWindowY(int32_t y)112 void PointerEvent::PointerItem::SetWindowY(int32_t y)
113 {
114 windowY_ = y;
115 }
116
GetDisplayXPos() const117 double PointerEvent::PointerItem::GetDisplayXPos() const
118 {
119 return displayXPos_;
120 }
121
SetDisplayXPos(double x)122 void PointerEvent::PointerItem::SetDisplayXPos(double x)
123 {
124 displayXPos_ = x;
125 }
126
GetDisplayYPos() const127 double PointerEvent::PointerItem::GetDisplayYPos() const
128 {
129 return displayYPos_;
130 }
131
SetDisplayYPos(double y)132 void PointerEvent::PointerItem::SetDisplayYPos(double y)
133 {
134 displayYPos_ = y;
135 }
136
GetWindowXPos() const137 double PointerEvent::PointerItem::GetWindowXPos() const
138 {
139 return windowXPos_;
140 }
141
SetWindowXPos(double x)142 void PointerEvent::PointerItem::SetWindowXPos(double x)
143 {
144 windowXPos_ = x;
145 }
146
GetWindowYPos() const147 double PointerEvent::PointerItem::GetWindowYPos() const
148 {
149 return windowYPos_;
150 }
151
SetWindowYPos(double y)152 void PointerEvent::PointerItem::SetWindowYPos(double y)
153 {
154 windowYPos_ = y;
155 }
156
GetFixedDisplayX() const157 int32_t PointerEvent::PointerItem::GetFixedDisplayX() const
158 {
159 return static_cast<int32_t>(fixedDisplayX_);
160 }
161
SetFixedDisplayX(int32_t fixedDisplayX)162 void PointerEvent::PointerItem::SetFixedDisplayX(int32_t fixedDisplayX)
163 {
164 fixedDisplayX_ = static_cast<double>(fixedDisplayX);
165 }
166
GetFixedDisplayY() const167 int32_t PointerEvent::PointerItem::GetFixedDisplayY() const
168 {
169 return static_cast<int32_t>(fixedDisplayY_);
170 }
171
SetFixedDisplayY(int32_t fixedDisplayY)172 void PointerEvent::PointerItem::SetFixedDisplayY(int32_t fixedDisplayY)
173 {
174 fixedDisplayY_ = static_cast<double>(fixedDisplayY);
175 }
176
GetFixedDisplayXPos() const177 double PointerEvent::PointerItem::GetFixedDisplayXPos() const
178 {
179 return fixedDisplayX_;
180 }
181
SetFixedDisplayXPos(double fixedDisplayX)182 void PointerEvent::PointerItem::SetFixedDisplayXPos(double fixedDisplayX)
183 {
184 fixedDisplayX_ = fixedDisplayX;
185 }
186
GetFixedDisplayYPos() const187 double PointerEvent::PointerItem::GetFixedDisplayYPos() const
188 {
189 return fixedDisplayY_;
190 }
191
SetFixedDisplayYPos(double fixedDisplayY)192 void PointerEvent::PointerItem::SetFixedDisplayYPos(double fixedDisplayY)
193 {
194 fixedDisplayY_ = fixedDisplayY;
195 }
196
GetWidth() const197 int32_t PointerEvent::PointerItem::GetWidth() const
198 {
199 return width_;
200 }
201
SetWidth(int32_t width)202 void PointerEvent::PointerItem::SetWidth(int32_t width)
203 {
204 width_ = width;
205 }
206
GetHeight() const207 int32_t PointerEvent::PointerItem::GetHeight() const
208 {
209 return height_;
210 }
211
SetHeight(int32_t height)212 void PointerEvent::PointerItem::SetHeight(int32_t height)
213 {
214 height_ = height;
215 }
216
GetTiltX() const217 double PointerEvent::PointerItem::GetTiltX() const
218 {
219 return tiltX_;
220 }
221
SetTiltX(double tiltX)222 void PointerEvent::PointerItem::SetTiltX(double tiltX)
223 {
224 tiltX_ = tiltX;
225 }
226
GetTiltY() const227 double PointerEvent::PointerItem::GetTiltY() const
228 {
229 return tiltY_;
230 }
231
SetTiltY(double tiltY)232 void PointerEvent::PointerItem::SetTiltY(double tiltY)
233 {
234 tiltY_ = tiltY;
235 }
236
GetToolDisplayX() const237 int32_t PointerEvent::PointerItem::GetToolDisplayX() const
238 {
239 return toolDisplayX_;
240 }
241
SetToolDisplayX(int32_t x)242 void PointerEvent::PointerItem::SetToolDisplayX(int32_t x)
243 {
244 toolDisplayX_ = x;
245 }
246
GetToolDisplayY() const247 int32_t PointerEvent::PointerItem::GetToolDisplayY() const
248 {
249 return toolDisplayY_;
250 }
251
SetToolDisplayY(int32_t y)252 void PointerEvent::PointerItem::SetToolDisplayY(int32_t y)
253 {
254 toolDisplayY_ = y;
255 }
256
GetToolWindowX() const257 int32_t PointerEvent::PointerItem::GetToolWindowX() const
258 {
259 return toolWindowX_;
260 }
261
SetToolWindowX(int32_t x)262 void PointerEvent::PointerItem::SetToolWindowX(int32_t x)
263 {
264 toolWindowX_ = x;
265 }
266
GetToolWindowY() const267 int32_t PointerEvent::PointerItem::GetToolWindowY() const
268 {
269 return toolWindowY_;
270 }
271
SetToolWindowY(int32_t y)272 void PointerEvent::PointerItem::SetToolWindowY(int32_t y)
273 {
274 toolWindowY_ = y;
275 }
276
SetGlobalX(double globalX)277 void PointerEvent::PointerItem::SetGlobalX(double globalX)
278 {
279 globalX_ = globalX;
280 }
281
GetGlobalX() const282 double PointerEvent::PointerItem::GetGlobalX() const
283 {
284 return globalX_;
285 }
286
SetGlobalY(double globalY)287 void PointerEvent::PointerItem::SetGlobalY(double globalY)
288 {
289 globalY_ = globalY;
290 }
291
GetGlobalY() const292 double PointerEvent::PointerItem::GetGlobalY() const
293 {
294 return globalY_;
295 }
296
IsValidGlobalXY() const297 bool PointerEvent::PointerItem::IsValidGlobalXY() const
298 {
299 return globalX_ > DBL_MIN && globalX_ < DBL_MAX &&
300 globalY_ > DBL_MIN && globalY_ < DBL_MAX;
301 }
302
GetToolWidth() const303 int32_t PointerEvent::PointerItem::GetToolWidth() const
304 {
305 return toolWidth_;
306 }
307
SetToolWidth(int32_t width)308 void PointerEvent::PointerItem::SetToolWidth(int32_t width)
309 {
310 toolWidth_ = width;
311 }
312
GetToolHeight() const313 int32_t PointerEvent::PointerItem::GetToolHeight() const
314 {
315 return toolHeight_;
316 }
317
SetToolHeight(int32_t height)318 void PointerEvent::PointerItem::SetToolHeight(int32_t height)
319 {
320 toolHeight_ = height;
321 }
322
GetPressure() const323 double PointerEvent::PointerItem::GetPressure() const
324 {
325 return pressure_;
326 }
327
SetPressure(double pressure)328 void PointerEvent::PointerItem::SetPressure(double pressure)
329 {
330 if (pressure < 0.0) {
331 pressure_ = 0.0;
332 } else if (TOOL_TYPE_PEN == GetToolType()) {
333 pressure_ = pressure >= MAX_PRESSURE ? MAX_PRESSURE : pressure;
334 } else {
335 pressure_ = pressure;
336 }
337 }
338
GetMoveFlag() const339 int32_t PointerEvent::PointerItem::GetMoveFlag() const
340 {
341 return moveFlag_;
342 }
343
SetMoveFlag(int32_t moveflag)344 void PointerEvent::PointerItem::SetMoveFlag(int32_t moveflag)
345 {
346 moveFlag_ = moveflag;
347 }
348
349
GetLongAxis() const350 int32_t PointerEvent::PointerItem::GetLongAxis() const
351 {
352 return longAxis_;
353 }
354
SetLongAxis(int32_t longAxis)355 void PointerEvent::PointerItem::SetLongAxis(int32_t longAxis)
356 {
357 longAxis_ = longAxis;
358 }
359
GetShortAxis() const360 int32_t PointerEvent::PointerItem::GetShortAxis() const
361 {
362 return shortAxis_;
363 }
364
SetShortAxis(int32_t shortAxis)365 void PointerEvent::PointerItem::SetShortAxis(int32_t shortAxis)
366 {
367 shortAxis_ = shortAxis;
368 }
369
GetDeviceId() const370 int32_t PointerEvent::PointerItem::GetDeviceId() const
371 {
372 return deviceId_;
373 }
374
SetDeviceId(int32_t deviceId)375 void PointerEvent::PointerItem::SetDeviceId(int32_t deviceId)
376 {
377 deviceId_ = deviceId;
378 }
379
GetToolType() const380 int32_t PointerEvent::PointerItem::GetToolType() const
381 {
382 return toolType_;
383 }
384
SetToolType(int32_t toolType)385 void PointerEvent::PointerItem::SetToolType(int32_t toolType)
386 {
387 toolType_ = toolType;
388 }
389
GetTargetWindowId() const390 int32_t PointerEvent::PointerItem::GetTargetWindowId() const
391 {
392 return targetWindowId_;
393 }
394
SetTargetWindowId(int32_t windowId)395 void PointerEvent::PointerItem::SetTargetWindowId(int32_t windowId)
396 {
397 targetWindowId_ = windowId;
398 }
399
GetRawDx() const400 int32_t PointerEvent::PointerItem::GetRawDx() const
401 {
402 return rawDx_;
403 }
404
SetRawDx(int32_t rawDx)405 void PointerEvent::PointerItem::SetRawDx(int32_t rawDx)
406 {
407 rawDx_ = rawDx;
408 }
409
GetOriginPointerId() const410 int32_t PointerEvent::PointerItem::GetOriginPointerId() const
411 {
412 return originPointerId_;
413 }
414
SetOriginPointerId(int32_t originPointerId)415 void PointerEvent::PointerItem::SetOriginPointerId(int32_t originPointerId)
416 {
417 originPointerId_ = originPointerId;
418 }
419
GetRawDy() const420 int32_t PointerEvent::PointerItem::GetRawDy() const
421 {
422 return rawDy_;
423 }
424
SetRawDy(int32_t rawDy)425 void PointerEvent::PointerItem::SetRawDy(int32_t rawDy)
426 {
427 rawDy_ = rawDy;
428 }
429
GetRawDisplayX() const430 int32_t PointerEvent::PointerItem::GetRawDisplayX() const
431 {
432 return rawDisplayX_;
433 }
SetRawDisplayX(int32_t rawDisplayX)434 void PointerEvent::PointerItem::SetRawDisplayX(int32_t rawDisplayX)
435 {
436 rawDisplayX_ = rawDisplayX;
437 }
GetRawDisplayY() const438 int32_t PointerEvent::PointerItem::GetRawDisplayY() const
439 {
440 return rawDisplayY_;
441 }
SetRawDisplayY(int32_t rawDisplayY)442 void PointerEvent::PointerItem::SetRawDisplayY(int32_t rawDisplayY)
443 {
444 rawDisplayY_ = rawDisplayY;
445 }
446
GetBlobId() const447 int32_t PointerEvent::PointerItem::GetBlobId() const
448 {
449 return blobId_;
450 }
451
SetBlobId(int32_t blobId)452 void PointerEvent::PointerItem::SetBlobId(int32_t blobId)
453 {
454 blobId_ = blobId;
455 }
456
GetTwist() const457 int32_t PointerEvent::PointerItem::GetTwist() const
458 {
459 return twist_;
460 }
461
SetTwist(int32_t twist)462 void PointerEvent::PointerItem::SetTwist(int32_t twist)
463 {
464 twist_ = twist;
465 }
466
IsCanceled() const467 bool PointerEvent::PointerItem::IsCanceled() const
468 {
469 return canceled_;
470 }
471
SetCanceled(bool canceled)472 void PointerEvent::PointerItem::SetCanceled(bool canceled)
473 {
474 canceled_ = canceled;
475 }
476
SetOrientation(int32_t orientation)477 void PointerEvent::PointerItem::SetOrientation(int32_t orientation)
478 {
479 orientation_ = orientation;
480 }
481
GetOrientation()482 int32_t PointerEvent::PointerItem::GetOrientation()
483 {
484 return orientation_;
485 }
486
WriteToParcel(Parcel & out) const487 bool PointerEvent::PointerItem::WriteToParcel(Parcel &out) const
488 {
489 return (
490 out.WriteInt32(pointerId_) &&
491 out.WriteInt64(downTime_) &&
492 out.WriteBool(pressed_) &&
493 out.WriteInt32(windowX_) &&
494 out.WriteInt32(windowY_) &&
495 out.WriteInt32(width_) &&
496 out.WriteInt32(height_) &&
497 out.WriteInt32(toolDisplayX_) &&
498 out.WriteInt32(toolDisplayY_) &&
499 out.WriteInt32(toolWindowX_) &&
500 out.WriteInt32(toolWindowY_) &&
501 out.WriteInt32(toolWidth_) &&
502 out.WriteInt32(toolHeight_) &&
503 out.WriteDouble(tiltX_) &&
504 out.WriteDouble(tiltY_) &&
505 out.WriteDouble(pressure_) &&
506 out.WriteInt32(longAxis_) &&
507 out.WriteInt32(shortAxis_) &&
508 out.WriteInt32(toolType_) &&
509 out.WriteInt32(deviceId_) &&
510 out.WriteInt32(rawDx_) &&
511 out.WriteInt32(rawDy_) &&
512 out.WriteInt32(rawDisplayX_) &&
513 out.WriteInt32(rawDisplayY_) &&
514 out.WriteInt32(targetWindowId_) &&
515 out.WriteInt32(originPointerId_) &&
516 out.WriteDouble(displayXPos_) &&
517 out.WriteDouble(displayYPos_) &&
518 out.WriteDouble(windowXPos_) &&
519 out.WriteDouble(windowYPos_) &&
520 out.WriteInt32(blobId_) &&
521 out.WriteInt32(twist_) &&
522 out.WriteDouble(fixedDisplayX_) &&
523 out.WriteDouble(fixedDisplayY_) &&
524 out.WriteDouble(globalX_) &&
525 out.WriteDouble(globalY_)
526 );
527 }
528
ReadFromParcel(Parcel & in)529 bool PointerEvent::PointerItem::ReadFromParcel(Parcel &in)
530 {
531 return (
532 in.ReadInt32(pointerId_) &&
533 in.ReadInt64(downTime_) &&
534 in.ReadBool(pressed_) &&
535 in.ReadInt32(windowX_) &&
536 in.ReadInt32(windowY_) &&
537 in.ReadInt32(width_) &&
538 in.ReadInt32(height_) &&
539 in.ReadInt32(toolDisplayX_) &&
540 in.ReadInt32(toolDisplayY_) &&
541 in.ReadInt32(toolWindowX_) &&
542 in.ReadInt32(toolWindowY_) &&
543 in.ReadInt32(toolWidth_) &&
544 in.ReadInt32(toolHeight_) &&
545 in.ReadDouble(tiltX_) &&
546 in.ReadDouble(tiltY_) &&
547 in.ReadDouble(pressure_) &&
548 in.ReadInt32(longAxis_) &&
549 in.ReadInt32(shortAxis_) &&
550 in.ReadInt32(toolType_) &&
551 in.ReadInt32(deviceId_) &&
552 in.ReadInt32(rawDx_) &&
553 in.ReadInt32(rawDy_) &&
554 in.ReadInt32(rawDisplayX_) &&
555 in.ReadInt32(rawDisplayY_) &&
556 in.ReadInt32(targetWindowId_) &&
557 in.ReadInt32(originPointerId_) &&
558 in.ReadDouble(displayXPos_) &&
559 in.ReadDouble(displayYPos_) &&
560 in.ReadDouble(windowXPos_) &&
561 in.ReadDouble(windowYPos_) &&
562 in.ReadInt32(blobId_) &&
563 in.ReadInt32(twist_) &&
564 in.ReadDouble(fixedDisplayX_) &&
565 in.ReadDouble(fixedDisplayY_) &&
566 in.ReadDouble(globalX_) &&
567 in.ReadDouble(globalY_)
568 );
569 }
570
PointerEvent(int32_t eventType)571 PointerEvent::PointerEvent(int32_t eventType) : InputEvent(eventType) {}
572
PointerEvent(const PointerEvent & other)573 PointerEvent::PointerEvent(const PointerEvent& other)
574 : InputEvent(other), pointerId_(other.pointerId_), pointers_(other.pointers_),
575 pressedButtons_(other.pressedButtons_),
576 pointerAction_(other.pointerAction_), originPointerAction_(other.originPointerAction_),
577 buttonId_(other.buttonId_), fingerCount_(other.fingerCount_), pullId_(other.pullId_), zOrder_(other.zOrder_),
578 axes_(other.axes_), axisValues_(other.axisValues_), velocity_(other.velocity_),
579 pressedKeys_(other.pressedKeys_), buffer_(other.buffer_), axisEventType_(other.axisEventType_),
580 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
581 fingerprintDistanceX_(other.fingerprintDistanceX_), fingerprintDistanceY_(other.fingerprintDistanceY_),
582 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
583 dispatchTimes_(other.dispatchTimes_),
584 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
585 enhanceData_(other.enhanceData_),
586 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
587 #ifdef OHOS_BUILD_ENABLE_ANCO
588 ancoDeal_(other.ancoDeal_),
589 #endif // OHOS_BUILD_ENABLE_ANCO
590 handleEventType_(other.handleEventType_),
591 throwAngle_(other.throwAngle_),
592 throwSpeed_(other.throwSpeed_),
593 settings_(other.settings_),
594 autoToVirtualScreen_(other.autoToVirtualScreen_),
595 handOption_(other.handOption_), fixedMode_(other.fixedMode_) {}
596
~PointerEvent()597 PointerEvent::~PointerEvent() {}
598
Create()599 std::shared_ptr<PointerEvent> PointerEvent::Create()
600 {
601 auto event = std::shared_ptr<PointerEvent>(new (std::nothrow) PointerEvent(InputEvent::EVENT_TYPE_POINTER));
602 CHKPP(event);
603 return event;
604 }
605
Reset()606 void PointerEvent::Reset()
607 {
608 InputEvent::Reset();
609 handOption_ = -1;
610 pointerId_ = -1;
611 pointers_.clear();
612 pressedButtons_.clear();
613 pointerAction_ = POINTER_ACTION_UNKNOWN;
614 originPointerAction_ = POINTER_ACTION_UNKNOWN;
615 buttonId_ = -1;
616 fingerCount_ = 0;
617 zOrder_ = -1.0f;
618 dispatchTimes_ = 0;
619 axes_ = 0U;
620 axisValues_.fill(0.0);
621 velocity_ = 0.0;
622 axisEventType_ = AXIS_EVENT_TYPE_UNKNOWN;
623 pressedKeys_.clear();
624 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
625 fingerprintDistanceX_ = 0.0;
626 fingerprintDistanceY_ = 0.0;
627 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
628 }
629
ToString()630 std::string PointerEvent::ToString()
631 {
632 std::string eventStr = InputEvent::ToString();
633 eventStr += ",pointerId:" + std::to_string(pointerId_);
634 eventStr += ",pointerAction:" + std::to_string(pointerAction_);
635 eventStr += ",buttonId:" + std::to_string(buttonId_);
636 eventStr += ",pointers:[";
637 size_t pointerSize = 0;
638 for (auto it = pointers_.begin(); it != pointers_.end(); it++) {
639 pointerSize++;
640 eventStr += "{displayX:" + std::to_string((*it).GetDisplayX());
641 eventStr += ",displayY:" + std::to_string((*it).GetDisplayY());
642 eventStr += ",windowX:" + std::to_string((*it).GetWindowX());
643 eventStr += ",windowY:" + std::to_string((*it).GetWindowY());
644 eventStr += ",targetWindowId:" + std::to_string((*it).GetTargetWindowId());
645 eventStr += ",longAxis:" + std::to_string((*it).GetLongAxis());
646 eventStr += ",shortAxis:" + std::to_string((*it).GetShortAxis()) + "}";
647 if (pointerSize != pointers_.size()) {
648 eventStr += ",";
649 }
650 }
651 eventStr += "],pressedButtons:[";
652 size_t buttonsSize = 0;
653 for (auto it = pressedButtons_.begin(); it != pressedButtons_.end(); it++) {
654 buttonsSize++;
655 eventStr += std::to_string(*it);
656 if (buttonsSize != pressedButtons_.size()) {
657 eventStr += ",";
658 }
659 }
660 eventStr += "]";
661 return eventStr;
662 }
663
GetPointerAction() const664 int32_t PointerEvent::GetPointerAction() const
665 {
666 return pointerAction_;
667 }
668
GetThrowAngle() const669 double PointerEvent::GetThrowAngle() const
670 {
671 return throwAngle_ ;
672 }
673
SetThrowAngle(double throwAngle)674 void PointerEvent::SetThrowAngle(double throwAngle)
675 {
676 throwAngle_ = throwAngle;
677 }
678
GetThrowSpeed() const679 double PointerEvent::GetThrowSpeed() const
680 {
681 return throwSpeed_;
682 }
683
SetThrowSpeed(double throwSpeed)684 void PointerEvent::SetThrowSpeed(double throwSpeed)
685 {
686 throwSpeed_ = throwSpeed;
687 }
688
SetPointerAction(int32_t pointerAction)689 void PointerEvent::SetPointerAction(int32_t pointerAction)
690 {
691 pointerAction_ = pointerAction;
692 originPointerAction_ = pointerAction;
693 }
694
GetOriginPointerAction() const695 int32_t PointerEvent::GetOriginPointerAction() const
696 {
697 return originPointerAction_;
698 }
699
SetOriginPointerAction(int32_t pointerAction)700 void PointerEvent::SetOriginPointerAction(int32_t pointerAction)
701 {
702 originPointerAction_ = pointerAction;
703 }
704
GetHandOption() const705 int32_t PointerEvent::GetHandOption() const
706 {
707 return handOption_;
708 }
709
SetHandOption(int32_t handOption)710 void PointerEvent::SetHandOption(int32_t handOption)
711 {
712 handOption_ = handOption;
713 }
714
715 static const std::unordered_map<int32_t, std::string> pointerActionMap = {
716 { PointerEvent::POINTER_ACTION_CANCEL, "cancel" },
717 { PointerEvent::POINTER_ACTION_DOWN, "down" },
718 { PointerEvent::POINTER_ACTION_MOVE, "move" },
719 { PointerEvent::POINTER_ACTION_UP, "up" },
720 { PointerEvent::POINTER_ACTION_AXIS_BEGIN, "axis-begin" },
721 { PointerEvent::POINTER_ACTION_AXIS_UPDATE, "axis-update" },
722 { PointerEvent::POINTER_ACTION_AXIS_END, "axis-end" },
723 { PointerEvent::POINTER_ACTION_BUTTON_DOWN, "button-down" },
724 { PointerEvent::POINTER_ACTION_BUTTON_UP, "button-up" },
725 { PointerEvent::POINTER_ACTION_ENTER_WINDOW, "enter-window" },
726 { PointerEvent::POINTER_ACTION_LEAVE_WINDOW, "leave-window" },
727 { PointerEvent::POINTER_ACTION_PULL_DOWN, "pull-down" },
728 { PointerEvent::POINTER_ACTION_PULL_MOVE, "pull-move" },
729 { PointerEvent::POINTER_ACTION_PULL_UP, "pull-up" },
730 { PointerEvent::POINTER_ACTION_PULL_IN_WINDOW, "pull-in-window" },
731 { PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW, "pull-out-window" },
732 { PointerEvent::POINTER_ACTION_SWIPE_BEGIN, "swipe-begin" },
733 { PointerEvent::POINTER_ACTION_SWIPE_UPDATE, "swipe-update" },
734 { PointerEvent::POINTER_ACTION_SWIPE_END, "swipe-end" },
735 { PointerEvent::POINTER_ACTION_ROTATE_BEGIN, "rotate-begin" },
736 { PointerEvent::POINTER_ACTION_ROTATE_UPDATE, "rotate-update" },
737 { PointerEvent::POINTER_ACTION_ROTATE_END, "rotate-end" },
738 { PointerEvent::POINTER_ACTION_TRIPTAP, "touchpad-triptap" },
739 { PointerEvent::POINTER_ACTION_QUADTAP, "quadtap" },
740 { PointerEvent::POINTER_ACTION_HOVER_MOVE, "hover-move" },
741 { PointerEvent::POINTER_ACTION_HOVER_ENTER, "hover-enter" },
742 { PointerEvent::POINTER_ACTION_HOVER_EXIT, "hover-exit" },
743 { PointerEvent::POINTER_ACTION_FINGERPRINT_DOWN, "fingerprint-down" },
744 { PointerEvent::POINTER_ACTION_FINGERPRINT_UP, "fingerprint-up" },
745 { PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE, "fingerprint-slide" },
746 { PointerEvent::POINTER_ACTION_FINGERPRINT_RETOUCH, "fingerprint-retouch" },
747 { PointerEvent::POINTER_ACTION_FINGERPRINT_CLICK, "fingerprint-click" },
748 { PointerEvent::POINTER_ACTION_FINGERPRINT_HOLD, "fingerprint-hold" },
749 { PointerEvent::POINTER_ACTION_FINGERPRINT_TOUCH, "fingerprint-touch" },
750 { PointerEvent::TOUCH_ACTION_SWIPE_DOWN, "touch-swipe-down" },
751 { PointerEvent::TOUCH_ACTION_SWIPE_UP, "touch-swipe-up" },
752 { PointerEvent::TOUCH_ACTION_SWIPE_LEFT, "touch-swipe-left" },
753 { PointerEvent::TOUCH_ACTION_SWIPE_RIGHT, "touch-swipe-right" },
754 { PointerEvent::TOUCH_ACTION_PINCH_OPENED, "touch-pinch-open" },
755 { PointerEvent::TOUCH_ACTION_PINCH_CLOSEED, "touch-pinch-close" },
756 { PointerEvent::TOUCH_ACTION_GESTURE_END, "touch-gesture-end" },
757 { PointerEvent::POINTER_ACTION_PROXIMITY_IN, "pen-proximity-in" },
758 { PointerEvent::POINTER_ACTION_PROXIMITY_OUT, "pen-proximity-out" },
759 };
760
DumpPointerAction() const761 const char* PointerEvent::DumpPointerAction() const
762 {
763 if (pointerAction_ == PointerEvent::POINTER_ACTION_AXIS_BEGIN) {
764 if (HasAxis(axes_, PointerEvent::AXIS_TYPE_SCROLL_VERTICAL) ||
765 HasAxis(axes_, PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL)) {
766 return "axis-begin";
767 } else if (HasAxis(axes_, PointerEvent::AXIS_TYPE_PINCH)) {
768 return "pinch-begin";
769 }
770 } else if (pointerAction_ == PointerEvent::POINTER_ACTION_AXIS_UPDATE) {
771 if (HasAxis(axes_, PointerEvent::AXIS_TYPE_SCROLL_VERTICAL) ||
772 HasAxis(axes_, PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL)) {
773 return "axis-update";
774 } else if (HasAxis(axes_, PointerEvent::AXIS_TYPE_PINCH)) {
775 return "pinch-update";
776 }
777 } else if (pointerAction_ == PointerEvent::POINTER_ACTION_AXIS_END) {
778 if (HasAxis(axes_, PointerEvent::AXIS_TYPE_SCROLL_VERTICAL) ||
779 HasAxis(axes_, PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL)) {
780 return "axis-end";
781 } else if (HasAxis(axes_, PointerEvent::AXIS_TYPE_PINCH)) {
782 return "pinch-end";
783 }
784 }
785 auto it = pointerActionMap.find(pointerAction_);
786 if (it != pointerActionMap.end()) {
787 return it->second.c_str();
788 }
789 return "unknown";
790 }
791
GetPointerId() const792 int32_t PointerEvent::GetPointerId() const
793 {
794 return pointerId_;
795 }
796
SetPointerId(int32_t pointerId)797 void PointerEvent::SetPointerId(int32_t pointerId)
798 {
799 pointerId_ = pointerId;
800 }
801
GetPointerItem(int32_t pointerId,PointerItem & pointerItem) const802 bool PointerEvent::GetPointerItem(int32_t pointerId, PointerItem &pointerItem) const
803 {
804 for (const auto &item : pointers_) {
805 if (item.GetPointerId() == pointerId) {
806 pointerItem = item;
807 return true;
808 }
809 }
810 return false;
811 }
812
GetOriginPointerItem(int32_t pointerId,PointerItem & pointerItem) const813 bool PointerEvent::GetOriginPointerItem(int32_t pointerId, PointerItem &pointerItem) const
814 {
815 for (const auto &item : pointers_) {
816 if (item.GetOriginPointerId() == pointerId) {
817 pointerItem = item;
818 return true;
819 }
820 }
821 return false;
822 }
823
RemovePointerItem(int32_t pointerId)824 void PointerEvent::RemovePointerItem(int32_t pointerId)
825 {
826 for (auto it = pointers_.begin(); it != pointers_.end(); ++it) {
827 if (it->GetPointerId() == pointerId) {
828 pointers_.erase(it);
829 break;
830 }
831 }
832 }
833
RemoveAllPointerItems()834 void PointerEvent::RemoveAllPointerItems()
835 {
836 pointers_.clear();
837 }
838
AddPointerItem(PointerItem & pointerItem)839 void PointerEvent::AddPointerItem(PointerItem &pointerItem)
840 {
841 if (pointers_.size() >= MAX_N_POINTER_ITEMS) {
842 MMI_HILOGE("Exceed maximum allowed number of pointer items");
843 return;
844 }
845 int32_t pointerId = pointerItem.GetPointerId();
846 for (auto &item : pointers_) {
847 if (item.GetPointerId() == pointerId) {
848 item = pointerItem;
849 return;
850 }
851 }
852 pointers_.push_back(pointerItem);
853 }
854
UpdatePointerItem(int32_t pointerId,PointerItem & pointerItem)855 void PointerEvent::UpdatePointerItem(int32_t pointerId, PointerItem &pointerItem)
856 {
857 for (auto &item : pointers_) {
858 if ((item.GetPointerId() % SIMULATE_EVENT_START_ID) == pointerId) {
859 item = pointerItem;
860 return;
861 }
862 }
863 AddPointerItem(pointerItem);
864 }
865
GetPressedButtons() const866 std::set<int32_t> PointerEvent::GetPressedButtons() const
867 {
868 return pressedButtons_;
869 }
870
IsButtonPressed(int32_t buttonId) const871 bool PointerEvent::IsButtonPressed(int32_t buttonId) const
872 {
873 return (pressedButtons_.find(buttonId) != pressedButtons_.end());
874 }
875
SetButtonPressed(int32_t buttonId)876 void PointerEvent::SetButtonPressed(int32_t buttonId)
877 {
878 if (pressedButtons_.size() >= MAX_N_PRESSED_BUTTONS) {
879 MMI_HILOGE("Exceed maximum allowed number of pressed buttons");
880 return;
881 }
882 auto iter = pressedButtons_.insert(buttonId);
883 if (!iter.second) {
884 MMI_HILOGE("Insert value failed, button:%{public}d", buttonId);
885 }
886 }
887
DeleteReleaseButton(int32_t buttonId)888 void PointerEvent::DeleteReleaseButton(int32_t buttonId)
889 {
890 if (pressedButtons_.find(buttonId) != pressedButtons_.end()) {
891 pressedButtons_.erase(buttonId);
892 }
893 }
894
ClearButtonPressed()895 void PointerEvent::ClearButtonPressed()
896 {
897 pressedButtons_.clear();
898 }
899
GetPointerCount() const900 int32_t PointerEvent::GetPointerCount() const
901 {
902 return static_cast<int32_t>(pointers_.size());
903 }
904
GetPointerIds() const905 std::vector<int32_t> PointerEvent::GetPointerIds() const
906 {
907 std::vector<int32_t> pointerIdList;
908 for (const auto &item : pointers_) {
909 pointerIdList.push_back(item.GetPointerId());
910 }
911 return pointerIdList;
912 }
913
GetAllPointerItems() const914 std::list<PointerEvent::PointerItem> PointerEvent::GetAllPointerItems() const
915 {
916 return pointers_;
917 }
918
GetButtonId() const919 int32_t PointerEvent::GetButtonId() const
920 {
921 return buttonId_;
922 }
923
SetButtonId(int32_t buttonId)924 void PointerEvent::SetButtonId(int32_t buttonId)
925 {
926 buttonId_ = buttonId;
927 }
928
GetFingerCount() const929 int32_t PointerEvent::GetFingerCount() const
930 {
931 return fingerCount_;
932 }
933
SetFingerCount(int32_t fingerCount)934 void PointerEvent::SetFingerCount(int32_t fingerCount)
935 {
936 fingerCount_ = fingerCount;
937 }
938
GetZOrder() const939 float PointerEvent::GetZOrder() const
940 {
941 return zOrder_;
942 }
943
SetZOrder(float zOrder)944 void PointerEvent::SetZOrder(float zOrder)
945 {
946 zOrder_ = zOrder;
947 }
948
GetAxisValue(AxisType axis) const949 double PointerEvent::GetAxisValue(AxisType axis) const
950 {
951 double axisValue {};
952 if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
953 axisValue = axisValues_[axis];
954 }
955 return axisValue;
956 }
957
SetAxisValue(AxisType axis,double axisValue)958 void PointerEvent::SetAxisValue(AxisType axis, double axisValue)
959 {
960 if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
961 axisValues_[axis] = axisValue;
962 axes_ = static_cast<uint32_t>(axes_ | static_cast<uint32_t>(1 << axis));
963 }
964 }
965
ClearAxisValue()966 void PointerEvent::ClearAxisValue()
967 {
968 axisValues_ = {};
969 axes_ = 0;
970 }
971
ClearAxisStatus(AxisType axis)972 void PointerEvent::ClearAxisStatus(AxisType axis)
973 {
974 axes_ &= ~static_cast<uint32_t>(1U << axis);
975 }
976
HasAxis(uint32_t axes,AxisType axis)977 bool PointerEvent::HasAxis(uint32_t axes, AxisType axis)
978 {
979 bool ret { false };
980 if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
981 ret = static_cast<bool>(static_cast<uint32_t>(axes) & (1 << static_cast<uint32_t>(axis)));
982 }
983 return ret;
984 }
985
GetVelocity() const986 double PointerEvent::GetVelocity() const
987 {
988 return velocity_;
989 }
990
SetVelocity(double velocity)991 void PointerEvent::SetVelocity(double velocity)
992 {
993 velocity_ = velocity;
994 }
995
SetPressedKeys(const std::vector<int32_t> pressedKeys)996 void PointerEvent::SetPressedKeys(const std::vector<int32_t> pressedKeys)
997 {
998 pressedKeys_ = pressedKeys;
999 }
1000
GetPressedKeys() const1001 std::vector<int32_t> PointerEvent::GetPressedKeys() const
1002 {
1003 return pressedKeys_;
1004 }
1005
GetAxisEventType() const1006 int32_t PointerEvent::GetAxisEventType() const
1007 {
1008 return axisEventType_;
1009 }
1010
SetAxisEventType(int32_t axisEventType)1011 void PointerEvent::SetAxisEventType(int32_t axisEventType)
1012 {
1013 axisEventType_ = axisEventType;
1014 }
1015
GetPullId() const1016 int32_t PointerEvent::GetPullId() const
1017 {
1018 return pullId_;
1019 }
1020
SetPullId(int32_t pullId)1021 void PointerEvent::SetPullId(int32_t pullId)
1022 {
1023 pullId_ = pullId;
1024 }
1025
1026 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
SetEnhanceData(const std::vector<uint8_t> & enhanceData)1027 void PointerEvent::SetEnhanceData(const std::vector<uint8_t>& enhanceData)
1028 {
1029 enhanceData_ = enhanceData;
1030 }
1031
GetEnhanceData() const1032 std::vector<uint8_t> PointerEvent::GetEnhanceData() const
1033 {
1034 return enhanceData_;
1035 }
1036 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1037
WriteToParcel(Parcel & out) const1038 bool PointerEvent::WriteToParcel(Parcel &out) const
1039 {
1040 if (!InputEvent::WriteToParcel(out)) {
1041 return false;
1042 }
1043
1044 WRITEINT32(out, pointerId_);
1045
1046 WRITEINT32(out, static_cast<int32_t>(pointers_.size()));
1047
1048 for (const auto &item : pointers_) {
1049 if (!item.WriteToParcel(out)) {
1050 return false;
1051 }
1052 }
1053
1054 WRITEINT32(out, static_cast<int32_t>(buffer_.size()));
1055
1056 for (const auto& buff : buffer_) {
1057 WRITEUINT8(out, buff);
1058 }
1059
1060 WRITEINT32(out, static_cast<int32_t>(pressedButtons_.size()));
1061
1062 for (const auto &item : pressedButtons_) {
1063 WRITEINT32(out, item);
1064 }
1065
1066 WRITEINT32(out, static_cast<int32_t>(pressedKeys_.size()));
1067
1068 for (const auto &item : pressedKeys_) {
1069 WRITEINT32(out, item);
1070 }
1071
1072 WRITEINT32(out, pointerAction_);
1073
1074 WRITEINT32(out, originPointerAction_);
1075
1076 WRITEINT32(out, buttonId_);
1077
1078 WRITEINT32(out, fingerCount_);
1079
1080 WRITEFLOAT(out, zOrder_);
1081
1082 const uint32_t axes { GetAxes() };
1083 WRITEUINT32(out, axes);
1084
1085 for (int32_t i = AXIS_TYPE_UNKNOWN; i < AXIS_TYPE_MAX; ++i) {
1086 const AxisType axis { static_cast<AxisType>(i) };
1087 if (HasAxis(axes, axis)) {
1088 WRITEDOUBLE(out, GetAxisValue(axis));
1089 }
1090 }
1091 WRITEDOUBLE(out, velocity_);
1092 WRITEINT32(out, axisEventType_);
1093 WRITEINT32(out, pullId_);
1094 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1095 WRITEINT32(out, static_cast<int32_t>(enhanceData_.size()));
1096 for (uint32_t i = 0; i < enhanceData_.size(); i++) {
1097 WRITEUINT32(out, enhanceData_[i]);
1098 }
1099 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1100
1101 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
1102 WRITEDOUBLE(out, fingerprintDistanceX_);
1103 WRITEDOUBLE(out, fingerprintDistanceY_);
1104 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
1105 WRITEINT32(out, handOption_);
1106 WRITEINT32(out, static_cast<int32_t>(fixedMode_));
1107 WRITEBOOL(out, autoToVirtualScreen_);
1108 return true;
1109 }
1110
Marshalling(Parcel & out) const1111 bool PointerEvent::Marshalling(Parcel &out) const
1112 {
1113 return WriteToParcel(out);
1114 }
1115
ReadFromParcel(Parcel & in)1116 bool PointerEvent::ReadFromParcel(Parcel &in)
1117 {
1118 if (!InputEvent::ReadFromParcel(in)) {
1119 return false;
1120 }
1121
1122 READINT32(in, pointerId_);
1123
1124 int32_t nPointers;
1125 READINT32(in, nPointers);
1126 if (nPointers > static_cast<int32_t>(MAX_N_POINTER_ITEMS)) {
1127 return false;
1128 }
1129
1130 for (int32_t i = 0; i < nPointers; ++i) {
1131 PointerItem item;
1132 if (!item.ReadFromParcel(in)) {
1133 return false;
1134 }
1135 AddPointerItem(item);
1136 }
1137
1138 if (!ReadBufferFromParcel(in)) {
1139 return false;
1140 }
1141
1142 int32_t nPressedButtons;
1143 READINT32(in, nPressedButtons);
1144 if (nPressedButtons > static_cast<int32_t>(MAX_N_PRESSED_BUTTONS)) {
1145 return false;
1146 }
1147
1148 for (int32_t i = 0; i < nPressedButtons; ++i) {
1149 int32_t buttonId = 0;
1150 READINT32(in, buttonId);
1151 SetButtonPressed(buttonId);
1152 }
1153
1154 int32_t nPressedKeys = 0;
1155 READINT32(in, nPressedKeys);
1156 if (nPressedKeys > static_cast<int32_t>(MAX_N_PRESSED_KEYS)) {
1157 return false;
1158 }
1159
1160 for (int32_t i = 0; i < nPressedKeys; i++) {
1161 int32_t val = 0;
1162 READINT32(in, val);
1163 pressedKeys_.emplace_back(val);
1164 }
1165 READINT32(in, pointerAction_);
1166
1167 READINT32(in, originPointerAction_);
1168
1169 READINT32(in, buttonId_);
1170
1171 READINT32(in, fingerCount_);
1172
1173 READFLOAT(in, zOrder_);
1174
1175 if (!ReadAxisFromParcel(in)) {
1176 return false;
1177 }
1178 READDOUBLE(in, velocity_);
1179
1180 READINT32(in, axisEventType_);
1181 READINT32(in, pullId_);
1182 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1183 if (!ReadEnhanceDataFromParcel(in)) {
1184 return false;
1185 }
1186 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1187
1188 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
1189 READDOUBLE(in, fingerprintDistanceX_);
1190 READDOUBLE(in, fingerprintDistanceY_);
1191 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
1192 READINT32(in, handOption_);
1193 if (!ReadFixedModeFromParcel(in)) {
1194 return false;
1195 }
1196 READBOOL(in, autoToVirtualScreen_);
1197 return true;
1198 }
1199
Unmarshalling(Parcel & parcel)1200 PointerEvent *PointerEvent::Unmarshalling(Parcel &parcel)
1201 {
1202 PointerEvent *data = new (std::nothrow) PointerEvent(InputEvent::EVENT_TYPE_POINTER);
1203 if (data && !data->ReadFromParcel(parcel)) {
1204 delete data;
1205 data = nullptr;
1206 }
1207 return data;
1208 }
1209
ReadFixedModeFromParcel(Parcel & in)1210 bool PointerEvent::ReadFixedModeFromParcel(Parcel &in)
1211 {
1212 int32_t value = 0;
1213 READINT32(in, value);
1214 if (value < static_cast<int32_t>(FixedMode::SCREEN_MODE_UNKNOWN) ||
1215 value >= static_cast<int32_t>(FixedMode::SCREEN_MODE_MAX)) {
1216 MMI_HILOGE("invalid fixed mode %{public}d", value);
1217 return false;
1218 }
1219 fixedMode_ = static_cast<FixedMode>(value);
1220 return true;
1221 }
1222
ReadAxisFromParcel(Parcel & in)1223 bool PointerEvent::ReadAxisFromParcel(Parcel &in)
1224 {
1225 uint32_t axes = 0;
1226 READUINT32(in, axes);
1227
1228 for (int32_t i = AXIS_TYPE_UNKNOWN; i < AXIS_TYPE_MAX; ++i) {
1229 const AxisType axis { static_cast<AxisType>(i) };
1230 if (HasAxis(axes, axis)) {
1231 double val;
1232 READDOUBLE(in, val);
1233 SetAxisValue(axis, val);
1234 }
1235 }
1236 return true;
1237 }
1238
1239 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
SetFingerprintDistanceX(double x)1240 void PointerEvent::SetFingerprintDistanceX(double x)
1241 {
1242 fingerprintDistanceX_ = x;
1243 }
1244
SetFingerprintDistanceY(double y)1245 void PointerEvent::SetFingerprintDistanceY(double y)
1246 {
1247 fingerprintDistanceY_ = y;
1248 }
1249
GetFingerprintDistanceX() const1250 double PointerEvent::GetFingerprintDistanceX() const
1251 {
1252 return fingerprintDistanceX_;
1253 }
1254
GetFingerprintDistanceY() const1255 double PointerEvent::GetFingerprintDistanceY() const
1256 {
1257 return fingerprintDistanceY_;
1258 }
1259 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
1260
1261 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
ReadEnhanceDataFromParcel(Parcel & in)1262 bool PointerEvent::ReadEnhanceDataFromParcel(Parcel &in)
1263 {
1264 int32_t size = 0;
1265 READINT32(in, size);
1266 if (size > static_cast<int32_t>(MAX_N_ENHANCE_DATA_SIZE) || size < 0) {
1267 MMI_HILOGE("The enhanceData_ size is invalid");
1268 return false;
1269 }
1270
1271 for (int32_t i = 0; i < size; i++) {
1272 uint32_t val = 0;
1273 READUINT32(in, val);
1274 enhanceData_.emplace_back(val);
1275 }
1276 return true;
1277 }
1278 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1279
ReadBufferFromParcel(Parcel & in)1280 bool PointerEvent::ReadBufferFromParcel(Parcel &in)
1281 {
1282 int32_t bufflen = 0;
1283 READINT32(in, bufflen);
1284 if (bufflen > static_cast<int32_t>(MAX_N_BUFFER_SIZE)) {
1285 return false;
1286 }
1287
1288 for (int32_t i = 0; i < bufflen; ++i) {
1289 uint8_t data;
1290 READUINT8(in, data);
1291 buffer_.push_back(data);
1292 }
1293 return true;
1294 }
1295
IsValidCheckMouseFunc() const1296 bool PointerEvent::IsValidCheckMouseFunc() const
1297 {
1298 CALL_DEBUG_ENTER;
1299 if (pointers_.size() != 1) {
1300 MMI_HILOGE("Pointers_ is invalid");
1301 return false;
1302 }
1303
1304 const size_t maxPressedButtons = 3;
1305 if (pressedButtons_.size() > maxPressedButtons) {
1306 MMI_HILOGE("PressedButtons_.size is greater than three and is invalid");
1307 return false;
1308 }
1309
1310 for (const auto &item : pressedButtons_) {
1311 if (item != MOUSE_BUTTON_LEFT && item != MOUSE_BUTTON_RIGHT && item != MOUSE_BUTTON_MIDDLE) {
1312 MMI_HILOGE("PressedButtons_ is invalid");
1313 return false;
1314 }
1315 }
1316
1317 int32_t pointAction = GetPointerAction();
1318 bool checkFlag = pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
1319 pointAction != POINTER_ACTION_AXIS_BEGIN && pointAction != POINTER_ACTION_AXIS_UPDATE &&
1320 pointAction != POINTER_ACTION_AXIS_END && pointAction != POINTER_ACTION_BUTTON_DOWN &&
1321 pointAction != POINTER_ACTION_BUTTON_UP;
1322 if (checkFlag) {
1323 MMI_HILOGE("PointAction is invalid");
1324 return false;
1325 }
1326
1327 int32_t buttonId = GetButtonId();
1328 if (pointAction == POINTER_ACTION_BUTTON_DOWN || pointAction == POINTER_ACTION_BUTTON_UP) {
1329 if (buttonId != MOUSE_BUTTON_LEFT && buttonId != MOUSE_BUTTON_RIGHT && buttonId != MOUSE_BUTTON_MIDDLE) {
1330 MMI_HILOGE("ButtonId is invalid");
1331 return false;
1332 }
1333 } else {
1334 if (buttonId != BUTTON_NONE) {
1335 MMI_HILOGE("ButtonId is not BUTTON_NONE and is invalid");
1336 return false;
1337 }
1338 }
1339 return true;
1340 }
1341
IsValidCheckMouse() const1342 bool PointerEvent::IsValidCheckMouse() const
1343 {
1344 CALL_DEBUG_ENTER;
1345 int32_t mousePointID = GetPointerId();
1346 if (mousePointID < 0) {
1347 MMI_HILOGE("MousePointID is invalid");
1348 return false;
1349 }
1350
1351 if (!IsValidCheckMouseFunc()) {
1352 MMI_HILOGE("IsValidCheckMouseFunc is invalid");
1353 return false;
1354 }
1355
1356 for (const auto &item : pointers_) {
1357 if (item.GetPointerId() < 0) {
1358 MMI_HILOGE("Item.pointerid is invalid");
1359 return false;
1360 }
1361
1362 if (item.GetPointerId() != mousePointID) {
1363 MMI_HILOGE("Item.pointerid is not same to mousePointID and is invalid");
1364 return false;
1365 }
1366
1367 if (item.GetDownTime() > 0) {
1368 MMI_HILOGE("Item.downtime is invalid");
1369 return false;
1370 }
1371
1372 if (item.IsPressed() != false) {
1373 MMI_HILOGE("Item.ispressed is not false and is invalid");
1374 return false;
1375 }
1376 }
1377 return true;
1378 }
1379
IsValidCheckTouchFunc() const1380 bool PointerEvent::IsValidCheckTouchFunc() const
1381 {
1382 CALL_DEBUG_ENTER;
1383 if (GetPointerId() < 0) {
1384 MMI_HILOGE("TouchPointID is invalid");
1385 return false;
1386 }
1387
1388 if (!pressedButtons_.empty()) {
1389 MMI_HILOGE("PressedButtons_.size is invalid");
1390 return false;
1391 }
1392
1393 int32_t pointAction = GetPointerAction();
1394 if (pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
1395 pointAction != POINTER_ACTION_DOWN && pointAction != POINTER_ACTION_UP) {
1396 MMI_HILOGE("PointAction is invalid");
1397 return false;
1398 }
1399
1400 if (GetButtonId() != BUTTON_NONE) {
1401 MMI_HILOGE("ButtonId is invalid");
1402 return false;
1403 }
1404 return true;
1405 }
1406
IsValidCheckTouch() const1407 bool PointerEvent::IsValidCheckTouch() const
1408 {
1409 CALL_DEBUG_ENTER;
1410 if (!IsValidCheckTouchFunc()) {
1411 MMI_HILOGE("IsValidCheckTouchFunc is invalid");
1412 return false;
1413 }
1414 bool isSameItem = false;
1415 int32_t touchPointID = GetPointerId();
1416 for (auto item = pointers_.begin(); item != pointers_.end(); item++) {
1417 if (item->GetPointerId() < 0) {
1418 MMI_HILOGE("Item.pointerid is invalid");
1419 return false;
1420 }
1421
1422 if (item->GetPointerId() == touchPointID) {
1423 isSameItem = true;
1424 }
1425
1426 if (item->GetDownTime() <= 0) {
1427 MMI_HILOGE("Item.downtime is invalid");
1428 return false;
1429 }
1430
1431 if (item->IsPressed() != false) {
1432 MMI_HILOGE("Item.ispressed is not false and is invalid");
1433 return false;
1434 }
1435
1436 auto itemtmp = item;
1437 for (++itemtmp; itemtmp != pointers_.end(); itemtmp++) {
1438 if (item->GetPointerId() == itemtmp->GetPointerId()) {
1439 MMI_HILOGE("Pointitems pointerid exist same items and is invalid");
1440 return false;
1441 }
1442 }
1443 }
1444
1445 if (!isSameItem) {
1446 MMI_HILOGE("Item.pointerid is not same to touchPointID and is invalid");
1447 return false;
1448 }
1449 return true;
1450 }
1451
IsValid() const1452 bool PointerEvent::IsValid() const
1453 {
1454 CALL_DEBUG_ENTER;
1455 switch (GetSourceType()) {
1456 case SOURCE_TYPE_MOUSE: {
1457 if (!IsValidCheckMouse()) {
1458 MMI_HILOGE("IsValidCheckMouse is invalid");
1459 return false;
1460 }
1461 break;
1462 }
1463 case SOURCE_TYPE_TOUCHSCREEN:
1464 case SOURCE_TYPE_TOUCHPAD: {
1465 if (!IsValidCheckTouch()) {
1466 MMI_HILOGE("IsValidCheckTouch is invalid");
1467 return false;
1468 }
1469 break;
1470 }
1471 case SOURCE_TYPE_JOYSTICK:
1472 break;
1473 default: {
1474 MMI_HILOGE("SourceType is invalid");
1475 return false;
1476 }
1477 }
1478 return true;
1479 }
1480
SetBuffer(std::vector<uint8_t> buffer)1481 void PointerEvent::SetBuffer(std::vector<uint8_t> buffer)
1482 {
1483 buffer_ = buffer;
1484 }
1485
ClearBuffer()1486 void PointerEvent::ClearBuffer()
1487 {
1488 buffer_.clear();
1489 }
1490
GetBuffer() const1491 std::vector<uint8_t> PointerEvent::GetBuffer() const
1492 {
1493 return buffer_;
1494 }
1495
GetDispatchTimes() const1496 int32_t PointerEvent::GetDispatchTimes() const
1497 {
1498 return dispatchTimes_;
1499 }
1500
SetDispatchTimes(int32_t dispatchTimes)1501 void PointerEvent::SetDispatchTimes(int32_t dispatchTimes)
1502 {
1503 dispatchTimes_ = dispatchTimes;
1504 }
1505
SetHandlerEventType(HandleEventType eventType)1506 void PointerEvent::SetHandlerEventType(HandleEventType eventType)
1507 {
1508 handleEventType_ = eventType;
1509 }
1510
GetHandlerEventType() const1511 HandleEventType PointerEvent::GetHandlerEventType() const
1512 {
1513 return handleEventType_;
1514 }
1515
1516 #ifdef OHOS_BUILD_ENABLE_ANCO
SetAncoDeal(bool ancoDeal)1517 void PointerEvent::SetAncoDeal(bool ancoDeal)
1518 {
1519 ancoDeal_ = ancoDeal;
1520 }
1521
GetAncoDeal() const1522 bool PointerEvent::GetAncoDeal() const
1523 {
1524 return ancoDeal_;
1525 }
1526 #endif // OHOS_BUILD_ENABLE_ANCO
1527
SetAutoToVirtualScreen(bool autoToVirtualScreen)1528 void PointerEvent::SetAutoToVirtualScreen(bool autoToVirtualScreen)
1529 {
1530 autoToVirtualScreen_ = autoToVirtualScreen;
1531 }
1532
GetAutoToVirtualScreen() const1533 bool PointerEvent::GetAutoToVirtualScreen() const
1534 {
1535 return autoToVirtualScreen_;
1536 }
1537
SetFixedMode(PointerEvent::FixedMode fixedMode)1538 void PointerEvent::SetFixedMode(PointerEvent::FixedMode fixedMode)
1539 {
1540 fixedMode_ = fixedMode;
1541 }
1542
GetFixedMode() const1543 PointerEvent::FixedMode PointerEvent::GetFixedMode() const
1544 {
1545 return fixedMode_;
1546 }
1547
GetFixedModeStr() const1548 std::string PointerEvent::GetFixedModeStr() const
1549 {
1550 switch (fixedMode_) {
1551 case PointerEvent::FixedMode::NORMAL:
1552 return "normal";
1553 case PointerEvent::FixedMode::AUTO:
1554 return "one-hand";
1555 default:
1556 return "unknown";
1557 }
1558 }
1559
ActionToShortStr(int32_t action)1560 std::string_view PointerEvent::ActionToShortStr(int32_t action)
1561 {
1562 // 该函数逻辑简单,功能单一,考虑性能影响,使用switch-case而不是表驱动实现。
1563 switch (action) {
1564 case PointerEvent::POINTER_ACTION_CANCEL:
1565 return "P:C:";
1566 case PointerEvent::POINTER_ACTION_DOWN:
1567 return "P:D:";
1568 case PointerEvent::POINTER_ACTION_MOVE:
1569 return "P:M:";
1570 case PointerEvent::POINTER_ACTION_UP:
1571 return "P:U:";
1572 case PointerEvent::POINTER_ACTION_AXIS_BEGIN:
1573 return "P:AB:";
1574 case PointerEvent::POINTER_ACTION_AXIS_UPDATE:
1575 return "P:AU:";
1576 case PointerEvent::POINTER_ACTION_AXIS_END:
1577 return "P:AE:";
1578 case PointerEvent::POINTER_ACTION_BUTTON_DOWN:
1579 return "P:BD:";
1580 case PointerEvent::POINTER_ACTION_BUTTON_UP:
1581 return "P:BU:";
1582 case PointerEvent::POINTER_ACTION_ENTER_WINDOW:
1583 return "P:EW:";
1584 case PointerEvent::POINTER_ACTION_LEAVE_WINDOW:
1585 return "P:LW:";
1586 case PointerEvent::POINTER_ACTION_PULL_DOWN:
1587 return "P:PD:";
1588 case PointerEvent::POINTER_ACTION_PULL_MOVE:
1589 return "P:PM:";
1590 case PointerEvent::POINTER_ACTION_PULL_UP:
1591 return "P:PU:";
1592 case PointerEvent::POINTER_ACTION_PULL_IN_WINDOW:
1593 return "P:PI:";
1594 case PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW:
1595 return "P:PO:";
1596 case PointerEvent::POINTER_ACTION_SWIPE_BEGIN:
1597 return "P:SB:";
1598 case PointerEvent::POINTER_ACTION_SWIPE_UPDATE:
1599 return "P:SU:";
1600 case PointerEvent::POINTER_ACTION_SWIPE_END:
1601 return "P:SE:";
1602 case PointerEvent::POINTER_ACTION_ROTATE_BEGIN:
1603 return "P:RB:";
1604 case PointerEvent::POINTER_ACTION_ROTATE_UPDATE:
1605 return "P:RU:";
1606 case PointerEvent::POINTER_ACTION_ROTATE_END:
1607 return "P:RE:";
1608 case PointerEvent::POINTER_ACTION_TRIPTAP:
1609 return "P:TT:";
1610 case PointerEvent::POINTER_ACTION_QUADTAP:
1611 return "P:Q:";
1612 case PointerEvent::POINTER_ACTION_HOVER_MOVE:
1613 return "P:HM:";
1614 case PointerEvent::POINTER_ACTION_HOVER_ENTER:
1615 return "P:HE:";
1616 case PointerEvent::POINTER_ACTION_HOVER_EXIT:
1617 return "P:HEX:";
1618 case PointerEvent::POINTER_ACTION_FINGERPRINT_DOWN:
1619 return "P:FD:";
1620 case PointerEvent::POINTER_ACTION_FINGERPRINT_UP:
1621 return "P:FU:";
1622 case PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE:
1623 return "P:FS:";
1624 case PointerEvent::POINTER_ACTION_FINGERPRINT_RETOUCH:
1625 return "P:FR:";
1626 case PointerEvent::POINTER_ACTION_FINGERPRINT_CLICK:
1627 return "P:FC:";
1628 case PointerEvent::POINTER_ACTION_FINGERPRINT_CANCEL:
1629 return "P:FCA:";
1630 case PointerEvent::POINTER_ACTION_UNKNOWN:
1631 return "P:UK:";
1632 default:
1633 return "P:?:";
1634 }
1635 }
1636
SetScrollRows(int32_t scrollRows)1637 void PointerEvent::SetScrollRows(int32_t scrollRows)
1638 {
1639 settings_.scrollRows_ = scrollRows;
1640 }
1641
GetScrollRows() const1642 int32_t PointerEvent::GetScrollRows() const
1643 {
1644 return settings_.scrollRows_;
1645 }
1646 } // namespace MMI
1647 } // namespace OHOS
1648