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 #include "pointer_event.h"
17 
18 #include <iomanip>
19 
20 #include "mmi_log.h"
21 
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "PointerEvent"
24 
25 using namespace OHOS::HiviewDFX;
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr double MAX_PRESSURE { 1.0 };
30 constexpr size_t MAX_N_PRESSED_BUTTONS { 10 };
31 constexpr size_t MAX_N_POINTER_ITEMS { 10 };
32 constexpr int32_t SIMULATE_EVENT_START_ID { 10000 };
33 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
34 constexpr size_t MAX_N_ENHANCE_DATA_SIZE { 64 };
35 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
36 constexpr size_t MAX_N_BUFFER_SIZE { 512 };
37 constexpr size_t MAX_N_PRESSED_KEYS { 10 };
38 } // namespace
39 
from(std::shared_ptr<InputEvent> inputEvent)40 std::shared_ptr<PointerEvent> PointerEvent::from(std::shared_ptr<InputEvent> inputEvent)
41 {
42     return nullptr;
43 }
44 
PointerItem()45 PointerEvent::PointerItem::PointerItem() {}
46 
~PointerItem()47 PointerEvent::PointerItem::~PointerItem() {}
48 
GetPointerId() const49 int32_t PointerEvent::PointerItem::GetPointerId() const
50 {
51     return pointerId_;
52 }
53 
SetPointerId(int32_t pointerId)54 void PointerEvent::PointerItem::SetPointerId(int32_t pointerId)
55 {
56     pointerId_ = pointerId;
57 }
58 
GetDownTime() const59 int64_t PointerEvent::PointerItem::GetDownTime() const
60 {
61     return downTime_;
62 }
63 
SetDownTime(int64_t downTime)64 void PointerEvent::PointerItem::SetDownTime(int64_t downTime)
65 {
66     downTime_ = downTime;
67 }
68 
IsPressed() const69 bool PointerEvent::PointerItem::IsPressed() const
70 {
71     return pressed_;
72 }
73 
SetPressed(bool pressed)74 void PointerEvent::PointerItem::SetPressed(bool pressed)
75 {
76     pressed_ = pressed;
77 }
78 
GetDisplayX() const79 int32_t PointerEvent::PointerItem::GetDisplayX() const
80 {
81     return displayX_;
82 }
83 
SetDisplayX(int32_t x)84 void PointerEvent::PointerItem::SetDisplayX(int32_t x)
85 {
86     displayX_ = x;
87 }
88 
GetDisplayY() const89 int32_t PointerEvent::PointerItem::GetDisplayY() const
90 {
91     return displayY_;
92 }
93 
SetDisplayY(int32_t y)94 void PointerEvent::PointerItem::SetDisplayY(int32_t y)
95 {
96     displayY_ = y;
97 }
98 
GetWindowX() const99 int32_t PointerEvent::PointerItem::GetWindowX() const
100 {
101     return windowX_;
102 }
103 
SetWindowX(int32_t x)104 void PointerEvent::PointerItem::SetWindowX(int32_t x)
105 {
106     windowX_ = x;
107 }
108 
GetWindowY() const109 int32_t PointerEvent::PointerItem::GetWindowY() const
110 {
111     return windowY_;
112 }
113 
SetWindowY(int32_t y)114 void PointerEvent::PointerItem::SetWindowY(int32_t y)
115 {
116     windowY_ = y;
117 }
118 
GetDisplayXPos() const119 double PointerEvent::PointerItem::GetDisplayXPos() const
120 {
121     return displayXPos_;
122 }
123 
SetDisplayXPos(double x)124 void PointerEvent::PointerItem::SetDisplayXPos(double x)
125 {
126     displayXPos_ = x;
127 }
128 
GetDisplayYPos() const129 double PointerEvent::PointerItem::GetDisplayYPos() const
130 {
131     return displayYPos_;
132 }
133 
SetDisplayYPos(double y)134 void PointerEvent::PointerItem::SetDisplayYPos(double y)
135 {
136     displayYPos_ = y;
137 }
138 
GetWindowXPos() const139 double PointerEvent::PointerItem::GetWindowXPos() const
140 {
141     return windowXPos_;
142 }
143 
SetWindowXPos(double x)144 void PointerEvent::PointerItem::SetWindowXPos(double x)
145 {
146     windowXPos_ = x;
147 }
148 
GetWindowYPos() const149 double PointerEvent::PointerItem::GetWindowYPos() const
150 {
151     return windowYPos_;
152 }
153 
SetWindowYPos(double y)154 void PointerEvent::PointerItem::SetWindowYPos(double y)
155 {
156     windowYPos_ = y;
157 }
158 
GetWidth() const159 int32_t PointerEvent::PointerItem::GetWidth() const
160 {
161     return width_;
162 }
163 
SetWidth(int32_t width)164 void PointerEvent::PointerItem::SetWidth(int32_t width)
165 {
166     width_ = width;
167 }
168 
GetHeight() const169 int32_t PointerEvent::PointerItem::GetHeight() const
170 {
171     return height_;
172 }
173 
SetHeight(int32_t height)174 void PointerEvent::PointerItem::SetHeight(int32_t height)
175 {
176     height_ = height;
177 }
178 
GetTiltX() const179 double PointerEvent::PointerItem::GetTiltX() const
180 {
181     return tiltX_;
182 }
183 
SetTiltX(double tiltX)184 void PointerEvent::PointerItem::SetTiltX(double tiltX)
185 {
186     tiltX_ = tiltX;
187 }
188 
GetTiltY() const189 double PointerEvent::PointerItem::GetTiltY() const
190 {
191     return tiltY_;
192 }
193 
SetTiltY(double tiltY)194 void PointerEvent::PointerItem::SetTiltY(double tiltY)
195 {
196     tiltY_ = tiltY;
197 }
198 
GetToolDisplayX() const199 int32_t PointerEvent::PointerItem::GetToolDisplayX() const
200 {
201     return toolDisplayX_;
202 }
203 
SetToolDisplayX(int32_t x)204 void PointerEvent::PointerItem::SetToolDisplayX(int32_t x)
205 {
206     toolDisplayX_ = x;
207 }
208 
GetToolDisplayY() const209 int32_t PointerEvent::PointerItem::GetToolDisplayY() const
210 {
211     return toolDisplayY_;
212 }
213 
SetToolDisplayY(int32_t y)214 void PointerEvent::PointerItem::SetToolDisplayY(int32_t y)
215 {
216     toolDisplayY_ = y;
217 }
218 
GetToolWindowX() const219 int32_t PointerEvent::PointerItem::GetToolWindowX() const
220 {
221     return toolWindowX_;
222 }
223 
SetToolWindowX(int32_t x)224 void PointerEvent::PointerItem::SetToolWindowX(int32_t x)
225 {
226     toolWindowX_ = x;
227 }
228 
GetToolWindowY() const229 int32_t PointerEvent::PointerItem::GetToolWindowY() const
230 {
231     return toolWindowY_;
232 }
233 
SetToolWindowY(int32_t y)234 void PointerEvent::PointerItem::SetToolWindowY(int32_t y)
235 {
236     toolWindowY_ = y;
237 }
238 
GetToolWidth() const239 int32_t PointerEvent::PointerItem::GetToolWidth() const
240 {
241     return toolWidth_;
242 }
243 
SetToolWidth(int32_t width)244 void PointerEvent::PointerItem::SetToolWidth(int32_t width)
245 {
246     toolWidth_ = width;
247 }
248 
GetToolHeight() const249 int32_t PointerEvent::PointerItem::GetToolHeight() const
250 {
251     return toolHeight_;
252 }
253 
SetToolHeight(int32_t height)254 void PointerEvent::PointerItem::SetToolHeight(int32_t height)
255 {
256     toolHeight_ = height;
257 }
258 
GetPressure() const259 double PointerEvent::PointerItem::GetPressure() const
260 {
261     return pressure_;
262 }
263 
SetPressure(double pressure)264 void PointerEvent::PointerItem::SetPressure(double pressure)
265 {
266     if (pressure < 0.0) {
267         pressure_ = 0.0;
268     } else if (TOOL_TYPE_PEN == GetToolType()) {
269         pressure_ = pressure >= MAX_PRESSURE ? MAX_PRESSURE : pressure;
270     } else {
271         pressure_ = pressure;
272     }
273 }
274 
GetMoveFlag() const275 int32_t PointerEvent::PointerItem::GetMoveFlag() const
276 {
277     return moveFlag_;
278 }
279 
SetMoveFlag(int32_t moveFlag)280 void PointerEvent::PointerItem::SetMoveFlag(int32_t moveFlag)
281 {
282     moveFlag_ = moveFlag;
283 }
284 
GetLongAxis() const285 int32_t PointerEvent::PointerItem::GetLongAxis() const
286 {
287     return longAxis_;
288 }
289 
SetLongAxis(int32_t longAxis)290 void PointerEvent::PointerItem::SetLongAxis(int32_t longAxis)
291 {
292     longAxis_ = longAxis;
293 }
294 
GetShortAxis() const295 int32_t PointerEvent::PointerItem::GetShortAxis() const
296 {
297     return shortAxis_;
298 }
299 
SetShortAxis(int32_t shortAxis)300 void PointerEvent::PointerItem::SetShortAxis(int32_t shortAxis)
301 {
302     shortAxis_ = shortAxis;
303 }
304 
GetDeviceId() const305 int32_t PointerEvent::PointerItem::GetDeviceId() const
306 {
307     return deviceId_;
308 }
309 
SetDeviceId(int32_t deviceId)310 void PointerEvent::PointerItem::SetDeviceId(int32_t deviceId)
311 {
312     deviceId_ = deviceId;
313 }
314 
GetToolType() const315 int32_t PointerEvent::PointerItem::GetToolType() const
316 {
317     return toolType_;
318 }
319 
SetToolType(int32_t toolType)320 void PointerEvent::PointerItem::SetToolType(int32_t toolType)
321 {
322     toolType_ = toolType;
323 }
324 
GetTargetWindowId() const325 int32_t PointerEvent::PointerItem::GetTargetWindowId() const
326 {
327     return targetWindowId_;
328 }
329 
SetTargetWindowId(int32_t windowId)330 void PointerEvent::PointerItem::SetTargetWindowId(int32_t windowId)
331 {
332     targetWindowId_ = windowId;
333 }
334 
GetRawDx() const335 int32_t PointerEvent::PointerItem::GetRawDx() const
336 {
337     return rawDx_;
338 }
339 
SetRawDx(int32_t rawDx)340 void PointerEvent::PointerItem::SetRawDx(int32_t rawDx)
341 {
342     rawDx_ = rawDx;
343 }
344 
GetOriginPointerId() const345 int32_t PointerEvent::PointerItem::GetOriginPointerId() const
346 {
347     return originPointerId_;
348 }
349 
SetOriginPointerId(int32_t originPointerId)350 void PointerEvent::PointerItem::SetOriginPointerId(int32_t originPointerId)
351 {
352     originPointerId_ = originPointerId;
353 }
354 
GetRawDy() const355 int32_t PointerEvent::PointerItem::GetRawDy() const
356 {
357     return rawDy_;
358 }
359 
SetRawDy(int32_t rawDy)360 void PointerEvent::PointerItem::SetRawDy(int32_t rawDy)
361 {
362     rawDy_ = rawDy;
363 }
364 
GetRawDisplayX() const365 int32_t PointerEvent::PointerItem::GetRawDisplayX() const
366 {
367     return rawDisplayX_;
368 }
369 
SetRawDisplayX(int32_t rawDisplayX)370 void PointerEvent::PointerItem::SetRawDisplayX(int32_t rawDisplayX)
371 {
372     rawDisplayX_ = rawDisplayX;
373 }
374 
GetRawDisplayY() const375 int32_t PointerEvent::PointerItem::GetRawDisplayY() const
376 {
377     return rawDisplayY_;
378 }
379 
SetRawDisplayY(int32_t rawDisplayY)380 void PointerEvent::PointerItem::SetRawDisplayY(int32_t rawDisplayY)
381 {
382     rawDisplayY_ = rawDisplayY;
383 }
384 
WriteToParcel(Parcel &out) const385 bool PointerEvent::PointerItem::WriteToParcel(Parcel &out) const
386 {
387     return (
388         out.WriteInt32(pointerId_) &&
389         out.WriteInt64(downTime_) &&
390         out.WriteBool(pressed_) &&
391         out.WriteInt32(displayX_) &&
392         out.WriteInt32(displayY_) &&
393         out.WriteInt32(windowX_) &&
394         out.WriteInt32(windowY_) &&
395         out.WriteInt32(width_) &&
396         out.WriteInt32(height_) &&
397         out.WriteInt32(toolDisplayX_) &&
398         out.WriteInt32(toolDisplayY_) &&
399         out.WriteInt32(toolWindowX_) &&
400         out.WriteInt32(toolWindowY_) &&
401         out.WriteInt32(toolWidth_) &&
402         out.WriteInt32(toolHeight_) &&
403         out.WriteDouble(tiltX_) &&
404         out.WriteDouble(tiltY_) &&
405         out.WriteDouble(pressure_) &&
406         out.WriteInt32(longAxis_) &&
407         out.WriteInt32(shortAxis_) &&
408         out.WriteInt32(toolType_) &&
409         out.WriteInt32(deviceId_) &&
410         out.WriteInt32(rawDx_) &&
411         out.WriteInt32(rawDy_) &&
412         out.WriteInt32(rawDisplayX_) &&
413         out.WriteInt32(rawDisplayY_) &&
414         out.WriteInt32(targetWindowId_) &&
415         out.WriteDouble(displayXPos_) &&
416         out.WriteDouble(displayYPos_) &&
417         out.WriteDouble(windowXPos_) &&
418         out.WriteDouble(windowYPos_) &&
419         out.WriteInt32(originPointerId_)
420     );
421 }
422 
ReadFromParcel(Parcel &in)423 bool PointerEvent::PointerItem::ReadFromParcel(Parcel &in)
424 {
425     return (
426         in.ReadInt32(pointerId_) &&
427         in.ReadInt64(downTime_) &&
428         in.ReadBool(pressed_) &&
429         in.ReadInt32(displayX_) &&
430         in.ReadInt32(displayY_) &&
431         in.ReadInt32(windowX_) &&
432         in.ReadInt32(windowY_) &&
433         in.ReadInt32(width_) &&
434         in.ReadInt32(height_) &&
435         in.ReadInt32(toolDisplayX_) &&
436         in.ReadInt32(toolDisplayY_) &&
437         in.ReadInt32(toolWindowX_) &&
438         in.ReadInt32(toolWindowY_) &&
439         in.ReadInt32(toolWidth_) &&
440         in.ReadInt32(toolHeight_) &&
441         in.ReadDouble(tiltX_) &&
442         in.ReadDouble(tiltY_) &&
443         in.ReadDouble(pressure_) &&
444         in.ReadInt32(longAxis_) &&
445         in.ReadInt32(shortAxis_) &&
446         in.ReadInt32(toolType_) &&
447         in.ReadInt32(deviceId_) &&
448         in.ReadInt32(rawDx_) &&
449         in.ReadInt32(rawDy_) &&
450         in.ReadInt32(rawDisplayX_) &&
451         in.ReadInt32(rawDisplayY_) &&
452         in.ReadInt32(targetWindowId_) &&
453         in.ReadDouble(displayXPos_) &&
454         in.ReadDouble(displayYPos_) &&
455         in.ReadDouble(windowXPos_) &&
456         in.ReadDouble(windowYPos_) &&
457         in.ReadInt32(originPointerId_)
458     );
459 }
460 
PointerEvent(int32_t eventType)461 PointerEvent::PointerEvent(int32_t eventType) : InputEvent(eventType) {}
462 
PointerEvent(const PointerEvent& other)463 PointerEvent::PointerEvent(const PointerEvent& other)
464     : InputEvent(other), pointerId_(other.pointerId_), pointers_(other.pointers_),
465       pressedButtons_(other.pressedButtons_), sourceType_(other.sourceType_),
466       pointerAction_(other.pointerAction_), originPointerAction_(other.originPointerAction_),
467       buttonId_(other.buttonId_), fingerCount_(other.fingerCount_), pullId_(other.pullId_), zOrder_(other.zOrder_),
468       axes_(other.axes_), axisValues_(other.axisValues_), velocity_(other.velocity_),
469       pressedKeys_(other.pressedKeys_), buffer_(other.buffer_), axisEventType_(other.axisEventType_),
470 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
471       fingerprintDistanceX_(other.fingerprintDistanceX_), fingerprintDistanceY_(other.fingerprintDistanceY_),
472 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
473       dispatchTimes_(other.dispatchTimes_),
474 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
475       enhanceData_(other.enhanceData_),
476 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
477 #ifdef OHOS_BUILD_ENABLE_ANCO
478       ancoDeal_(other.ancoDeal_),
479 #endif // OHOS_BUILD_ENABLE_ANCO
480       handleEventType_(other.handleEventType_)
481       {}
482 
~PointerEvent()483 PointerEvent::~PointerEvent() {}
484 
Create()485 std::shared_ptr<PointerEvent> PointerEvent::Create()
486 {
487     auto event = std::shared_ptr<PointerEvent>(new (std::nothrow) PointerEvent(InputEvent::EVENT_TYPE_POINTER));
488     CHKPP(event);
489     return event;
490 }
491 
Reset()492 void PointerEvent::Reset()
493 {
494     InputEvent::Reset();
495     pointerId_ = -1;
496     pointers_.clear();
497     pressedButtons_.clear();
498     sourceType_ = SOURCE_TYPE_UNKNOWN;
499     pointerAction_ = POINTER_ACTION_UNKNOWN;
500     originPointerAction_ = POINTER_ACTION_UNKNOWN;
501     buttonId_ = -1;
502     fingerCount_ = 0;
503     zOrder_ = -1.0f;
504     dispatchTimes_ = 0;
505     axes_ = 0U;
506     axisValues_.fill(0.0);
507     velocity_ = 0.0;
508     axisEventType_ = AXIS_EVENT_TYPE_UNKNOWN;
509     pressedKeys_.clear();
510 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
511     fingerprintDistanceX_ = 0.0;
512     fingerprintDistanceY_ = 0.0;
513 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
514 }
515 
ToString()516 std::string PointerEvent::ToString()
517 {
518     std::string eventStr = InputEvent::ToString();
519     eventStr += ",pointerId:" + std::to_string(pointerId_);
520     eventStr += ",sourceType:" + std::to_string(sourceType_);
521     eventStr += ",pointerAction:" + std::to_string(pointerAction_);
522     eventStr += ",buttonId:" + std::to_string(buttonId_);
523     eventStr += ",pointers:[";
524     size_t pointerSize = 0;
525     for (auto it = pointers_.begin(); it != pointers_.end(); it++) {
526         pointerSize++;
527         eventStr += "{displayX:" + std::to_string((*it).GetDisplayX());
528         eventStr += ",displayY:" + std::to_string((*it).GetDisplayY());
529         eventStr += ",windowX:" + std::to_string((*it).GetWindowX());
530         eventStr += ",windowY:" + std::to_string((*it).GetWindowY());
531         eventStr += ",targetWindowId:" + std::to_string((*it).GetTargetWindowId());
532         eventStr += ",longAxis:" + std::to_string((*it).GetLongAxis());
533         eventStr += ",shortAxis:" + std::to_string((*it).GetShortAxis()) + "}";
534         if (pointerSize != pointers_.size()) {
535             eventStr += ",";
536         }
537     }
538     eventStr += "],pressedButtons:[";
539     size_t buttonsSize = 0;
540     for (auto it = pressedButtons_.begin(); it != pressedButtons_.end(); it++) {
541         buttonsSize++;
542         eventStr += std::to_string(*it);
543         if (buttonsSize != pressedButtons_.size()) {
544             eventStr += ",";
545         }
546     }
547     eventStr += "]";
548     return eventStr;
549 }
550 
GetPointerAction() const551 int32_t PointerEvent::GetPointerAction() const
552 {
553     return pointerAction_;
554 }
555 
SetPointerAction(int32_t pointerAction)556 void PointerEvent::SetPointerAction(int32_t pointerAction)
557 {
558     pointerAction_ = pointerAction;
559     originPointerAction_ = pointerAction;
560 }
561 
GetOriginPointerAction() const562 int32_t PointerEvent::GetOriginPointerAction() const
563 {
564     return originPointerAction_;
565 }
566 
SetOriginPointerAction(int32_t pointerAction)567 void PointerEvent::SetOriginPointerAction(int32_t pointerAction)
568 {
569     originPointerAction_ = pointerAction;
570 }
571 
572 static const std::unordered_map<int32_t, std::string> pointerActionMap = {
573     { PointerEvent::POINTER_ACTION_CANCEL, "cancel" },
574     { PointerEvent::POINTER_ACTION_DOWN, "down" },
575     { PointerEvent::POINTER_ACTION_MOVE, "move" },
576     { PointerEvent::POINTER_ACTION_UP, "up" },
577     { PointerEvent::POINTER_ACTION_AXIS_BEGIN, "axis-begin" },
578     { PointerEvent::POINTER_ACTION_AXIS_UPDATE, "axis-update" },
579     { PointerEvent::POINTER_ACTION_AXIS_END, "axis-end" },
580     { PointerEvent::POINTER_ACTION_BUTTON_DOWN, "button-down" },
581     { PointerEvent::POINTER_ACTION_BUTTON_UP, "button-up" },
582     { PointerEvent::POINTER_ACTION_ENTER_WINDOW, "enter-window" },
583     { PointerEvent::POINTER_ACTION_LEAVE_WINDOW, "leave-window" },
584     { PointerEvent::POINTER_ACTION_PULL_DOWN, "pull-down" },
585     { PointerEvent::POINTER_ACTION_PULL_MOVE, "pull-move" },
586     { PointerEvent::POINTER_ACTION_PULL_UP, "pull-up" },
587     { PointerEvent::POINTER_ACTION_PULL_IN_WINDOW, "pull-in-window" },
588     { PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW, "pull-out-window" },
589     { PointerEvent::POINTER_ACTION_SWIPE_BEGIN, "swipe-begin" },
590     { PointerEvent::POINTER_ACTION_SWIPE_UPDATE, "swipe-update" },
591     { PointerEvent::POINTER_ACTION_SWIPE_END, "swipe-end" },
592     { PointerEvent::POINTER_ACTION_ROTATE_BEGIN, "rotate-begin" },
593     { PointerEvent::POINTER_ACTION_ROTATE_UPDATE, "rotate-update" },
594     { PointerEvent::POINTER_ACTION_ROTATE_END, "rotate-end" },
595     { PointerEvent::POINTER_ACTION_TRIPTAP, "touchpad-triptap" },
596     { PointerEvent::POINTER_ACTION_QUADTAP, "quadtap" },
597     { PointerEvent::POINTER_ACTION_HOVER_MOVE, "hover-move" },
598     { PointerEvent::POINTER_ACTION_HOVER_ENTER, "hover-enter" },
599     { PointerEvent::POINTER_ACTION_HOVER_EXIT, "hover-exit" },
600     { PointerEvent::POINTER_ACTION_FINGERPRINT_DOWN, "fingerprint-down" },
601     { PointerEvent::POINTER_ACTION_FINGERPRINT_UP, "fingerprint-up" },
602     { PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE, "fingerprint-slide" },
603     { PointerEvent::POINTER_ACTION_FINGERPRINT_RETOUCH, "fingerprint-retouch" },
604     { PointerEvent::POINTER_ACTION_FINGERPRINT_CLICK, "fingerprint-click" },
605     { PointerEvent::TOUCH_ACTION_SWIPE_DOWN, "touch-swipe-down" },
606     { PointerEvent::TOUCH_ACTION_SWIPE_UP, "touch-swipe-up" },
607     { PointerEvent::TOUCH_ACTION_SWIPE_LEFT, "touch-swipe-left" },
608     { PointerEvent::TOUCH_ACTION_SWIPE_RIGHT, "touch-swipe-right" },
609     { PointerEvent::TOUCH_ACTION_PINCH_OPENED, "touch-pinch-open" },
610     { PointerEvent::TOUCH_ACTION_PINCH_CLOSEED, "touch-pinch-close" },
611     { PointerEvent::TOUCH_ACTION_GESTURE_END, "touch-gesture-end" },
612     { PointerEvent::POINTER_ACTION_PROXIMITY_IN, "pen-proximity-in" },
613     { PointerEvent::POINTER_ACTION_PROXIMITY_OUT, "pen-proximity-out" },
614 };
615 
DumpPointerAction() const616 const char* PointerEvent::DumpPointerAction() const
617 {
618     auto it = pointerActionMap.find(pointerAction_);
619     if (it != pointerActionMap.end()) {
620         return it->second.c_str();
621     }
622     return "unknown";
623 }
624 
GetPointerId() const625 int32_t PointerEvent::GetPointerId() const
626 {
627     return pointerId_;
628 }
629 
SetPointerId(int32_t pointerId)630 void PointerEvent::SetPointerId(int32_t pointerId)
631 {
632     pointerId_ = pointerId;
633 }
634 
GetPointerItem(int32_t pointerId, PointerItem &pointerItem)635 bool PointerEvent::GetPointerItem(int32_t pointerId, PointerItem &pointerItem)
636 {
637     for (const auto &item : pointers_) {
638         if (item.GetPointerId() == pointerId) {
639             pointerItem = item;
640             return true;
641         }
642     }
643     return false;
644 }
645 
RemovePointerItem(int32_t pointerId)646 void PointerEvent::RemovePointerItem(int32_t pointerId)
647 {
648     for (auto it = pointers_.begin(); it != pointers_.end(); ++it) {
649         if (it->GetPointerId() == pointerId) {
650             pointers_.erase(it);
651             break;
652         }
653     }
654 }
655 
RemoveAllPointerItems()656 void PointerEvent::RemoveAllPointerItems()
657 {
658     pointers_.clear();
659 }
660 
AddPointerItem(PointerItem &pointerItem)661 void PointerEvent::AddPointerItem(PointerItem &pointerItem)
662 {
663     if (pointers_.size() >= MAX_N_POINTER_ITEMS) {
664         MMI_HILOGE("Exceed maximum allowed number of pointer items");
665         return;
666     }
667     int32_t pointerId = pointerItem.GetPointerId();
668     for (auto &item : pointers_) {
669         if (item.GetPointerId() == pointerId) {
670             item = pointerItem;
671             return;
672         }
673     }
674     pointers_.push_back(pointerItem);
675 }
676 
UpdatePointerItem(int32_t pointerId, PointerItem &pointerItem)677 void PointerEvent::UpdatePointerItem(int32_t pointerId, PointerItem &pointerItem)
678 {
679     for (auto &item : pointers_) {
680         if ((item.GetPointerId() % SIMULATE_EVENT_START_ID) == pointerId) {
681             item = pointerItem;
682             return;
683         }
684     }
685     AddPointerItem(pointerItem);
686 }
687 
GetPressedButtons() const688 std::set<int32_t> PointerEvent::GetPressedButtons() const
689 {
690     return pressedButtons_;
691 }
692 
IsButtonPressed(int32_t buttonId) const693 bool PointerEvent::IsButtonPressed(int32_t buttonId) const
694 {
695     return (pressedButtons_.find(buttonId) != pressedButtons_.end());
696 }
697 
SetButtonPressed(int32_t buttonId)698 void PointerEvent::SetButtonPressed(int32_t buttonId)
699 {
700     if (pressedButtons_.size() >= MAX_N_PRESSED_BUTTONS) {
701         MMI_HILOGE("Exceed maximum allowed number of pressed buttons");
702         return;
703     }
704     auto iter = pressedButtons_.insert(buttonId);
705     if (!iter.second) {
706         MMI_HILOGE("Insert value failed, button:%{public}d", buttonId);
707     }
708 }
709 
DeleteReleaseButton(int32_t buttonId)710 void PointerEvent::DeleteReleaseButton(int32_t buttonId)
711 {
712     if (pressedButtons_.find(buttonId) != pressedButtons_.end()) {
713         pressedButtons_.erase(buttonId);
714     }
715 }
716 
ClearButtonPressed()717 void PointerEvent::ClearButtonPressed()
718 {
719     pressedButtons_.clear();
720 }
721 
GetPointerCount() const722 int32_t PointerEvent::GetPointerCount() const
723 {
724     return static_cast<int32_t>(pointers_.size());
725 }
726 
GetPointerIds() const727 std::vector<int32_t> PointerEvent::GetPointerIds() const
728 {
729     std::vector<int32_t> pointerIdList;
730     for (const auto &item : pointers_) {
731         pointerIdList.push_back(item.GetPointerId());
732     }
733     return pointerIdList;
734 }
735 
GetAllPointerItems() const736 std::list<PointerEvent::PointerItem> PointerEvent::GetAllPointerItems() const
737 {
738     return pointers_;
739 }
740 
GetSourceType() const741 int32_t PointerEvent::GetSourceType() const
742 {
743     return sourceType_;
744 }
745 
SetSourceType(int32_t sourceType)746 void PointerEvent::SetSourceType(int32_t sourceType)
747 {
748     sourceType_ = sourceType;
749 }
750 
DumpSourceType() const751 const char* PointerEvent::DumpSourceType() const
752 {
753     switch (sourceType_) {
754         case PointerEvent::SOURCE_TYPE_MOUSE: {
755             return "mouse";
756         }
757         case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
758             return "touch-screen";
759         }
760         case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
761             return "touch-pad";
762         }
763         case PointerEvent::SOURCE_TYPE_JOYSTICK: {
764             return "joystick";
765         }
766         case PointerEvent::SOURCE_TYPE_FINGERPRINT: {
767             return "fingerprint";
768         }
769         case PointerEvent::SOURCE_TYPE_CROWN: {
770             return "crown";
771         }
772         default: {
773             break;
774         }
775     }
776     return "unknown";
777 }
778 
GetButtonId() const779 int32_t PointerEvent::GetButtonId() const
780 {
781     return buttonId_;
782 }
783 
SetButtonId(int32_t buttonId)784 void PointerEvent::SetButtonId(int32_t buttonId)
785 {
786     buttonId_ = buttonId;
787 }
788 
GetFingerCount() const789 int32_t PointerEvent::GetFingerCount() const
790 {
791     return fingerCount_;
792 }
793 
SetFingerCount(int32_t fingerCount)794 void PointerEvent::SetFingerCount(int32_t fingerCount)
795 {
796     fingerCount_ = fingerCount;
797 }
798 
GetZOrder() const799 float PointerEvent::GetZOrder() const
800 {
801     return zOrder_;
802 }
803 
SetZOrder(float zOrder)804 void PointerEvent::SetZOrder(float zOrder)
805 {
806     zOrder_ = zOrder;
807 }
808 
GetAxisValue(AxisType axis) const809 double PointerEvent::GetAxisValue(AxisType axis) const
810 {
811     double axisValue {};
812     if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
813         axisValue = axisValues_[axis];
814     }
815     return axisValue;
816 }
817 
SetAxisValue(AxisType axis, double axisValue)818 void PointerEvent::SetAxisValue(AxisType axis, double axisValue)
819 {
820     if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
821         axisValues_[axis] = axisValue;
822         axes_ = static_cast<uint32_t>(axes_ | static_cast<uint32_t>(1 << axis));
823     }
824 }
825 
ClearAxisValue()826 void PointerEvent::ClearAxisValue()
827 {
828     axisValues_ = {};
829     axes_ = 0;
830 }
831 
HasAxis(uint32_t axes, AxisType axis)832 bool PointerEvent::HasAxis(uint32_t axes, AxisType axis)
833 {
834     bool ret { false };
835     if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
836         ret = static_cast<bool>(static_cast<uint32_t>(axes) & (1 << static_cast<uint32_t>(axis)));
837     }
838     return ret;
839 }
840 
GetVelocity() const841 double PointerEvent::GetVelocity() const
842 {
843     return velocity_;
844 }
845 
SetVelocity(double velocity)846 void PointerEvent::SetVelocity(double velocity)
847 {
848     velocity_ = velocity;
849 }
850 
SetPressedKeys(const std::vector<int32_t> pressedKeys)851 void PointerEvent::SetPressedKeys(const std::vector<int32_t> pressedKeys)
852 {
853     pressedKeys_ = pressedKeys;
854 }
855 
GetPressedKeys() const856 std::vector<int32_t> PointerEvent::GetPressedKeys() const
857 {
858     return pressedKeys_;
859 }
860 
GetAxisEventType() const861 int32_t PointerEvent::GetAxisEventType() const
862 {
863     return axisEventType_;
864 }
865 
SetAxisEventType(int32_t axisEventType)866 void PointerEvent::SetAxisEventType(int32_t axisEventType)
867 {
868     axisEventType_ = axisEventType;
869 }
870 
GetPullId() const871 int32_t PointerEvent::GetPullId() const
872 {
873     return pullId_;
874 }
875 
SetPullId(int32_t pullId)876 void PointerEvent::SetPullId(int32_t pullId)
877 {
878     pullId_ = pullId;
879 }
880 
881 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
SetEnhanceData(const std::vector<uint8_t>& enhanceData)882 void PointerEvent::SetEnhanceData(const std::vector<uint8_t>& enhanceData)
883 {
884     enhanceData_ = enhanceData;
885 }
886 
GetEnhanceData() const887 std::vector<uint8_t> PointerEvent::GetEnhanceData() const
888 {
889     return enhanceData_;
890 }
891 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
892 
WriteToParcel(Parcel &out) const893 bool PointerEvent::WriteToParcel(Parcel &out) const
894 {
895     if (!InputEvent::WriteToParcel(out)) {
896         return false;
897     }
898 
899     WRITEINT32(out, pointerId_);
900 
901     WRITEINT32(out, static_cast<int32_t>(pointers_.size()));
902 
903     for (const auto &item : pointers_) {
904         if (!item.WriteToParcel(out)) {
905             return false;
906         }
907     }
908 
909     WRITEINT32(out, static_cast<int32_t>(buffer_.size()));
910 
911     for (const auto& buff : buffer_) {
912         WRITEUINT8(out, buff);
913     }
914 
915     WRITEINT32(out, static_cast<int32_t>(pressedButtons_.size()));
916 
917     for (const auto &item : pressedButtons_) {
918         WRITEINT32(out, item);
919     }
920 
921     WRITEINT32(out, static_cast<int32_t>(pressedKeys_.size()));
922 
923     for (const auto &item : pressedKeys_) {
924         WRITEINT32(out, item);
925     }
926 
927     WRITEINT32(out, sourceType_);
928 
929     WRITEINT32(out, pointerAction_);
930 
931     WRITEINT32(out, originPointerAction_);
932 
933     WRITEINT32(out, buttonId_);
934 
935     WRITEINT32(out, fingerCount_);
936 
937     WRITEFLOAT(out, zOrder_);
938 
939     const uint32_t axes { GetAxes() };
940     WRITEUINT32(out, axes);
941 
942     for (int32_t i = AXIS_TYPE_UNKNOWN; i < AXIS_TYPE_MAX; ++i) {
943         const AxisType axis { static_cast<AxisType>(i) };
944         if (HasAxis(axes, axis)) {
945             WRITEDOUBLE(out, GetAxisValue(axis));
946         }
947     }
948     WRITEDOUBLE(out, velocity_);
949 
950     WRITEINT32(out, axisEventType_);
951     WRITEINT32(out, pullId_);
952 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
953     WRITEINT32(out, static_cast<int32_t>(enhanceData_.size()));
954     for (uint32_t i = 0; i < enhanceData_.size(); i++) {
955         WRITEUINT32(out, enhanceData_[i]);
956     }
957 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
958 
959 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
960     WRITEDOUBLE(out, fingerprintDistanceX_);
961     WRITEDOUBLE(out, fingerprintDistanceY_);
962 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
963     return true;
964 }
965 
ReadFromParcel(Parcel &in)966 bool PointerEvent::ReadFromParcel(Parcel &in)
967 {
968     if (!InputEvent::ReadFromParcel(in)) {
969         return false;
970     }
971 
972     READINT32(in, pointerId_);
973 
974     int32_t nPointers;
975     READINT32(in, nPointers);
976     if (nPointers > static_cast<int32_t>(MAX_N_POINTER_ITEMS)) {
977         return false;
978     }
979 
980     for (int32_t i = 0; i < nPointers; ++i) {
981         PointerItem item;
982         if (!item.ReadFromParcel(in)) {
983             return false;
984         }
985         AddPointerItem(item);
986     }
987 
988     if (!ReadBufferFromParcel(in)) {
989         return false;
990     }
991 
992     int32_t nPressedButtons;
993     READINT32(in, nPressedButtons);
994     if (nPressedButtons > static_cast<int32_t>(MAX_N_PRESSED_BUTTONS)) {
995         return false;
996     }
997 
998     for (int32_t i = 0; i < nPressedButtons; ++i) {
999         int32_t buttonId = 0;
1000         READINT32(in, buttonId);
1001         SetButtonPressed(buttonId);
1002     }
1003 
1004     int32_t nPressedKeys = 0;
1005     READINT32(in, nPressedKeys);
1006     if (nPressedKeys > static_cast<int32_t>(MAX_N_PRESSED_KEYS)) {
1007         return false;
1008     }
1009 
1010     for (int32_t i = 0; i < nPressedKeys; i++) {
1011         int32_t val = 0;
1012         READINT32(in, val);
1013         pressedKeys_.emplace_back(val);
1014     }
1015     READINT32(in, sourceType_);
1016     READINT32(in, pointerAction_);
1017     READINT32(in, originPointerAction_);
1018     READINT32(in, buttonId_);
1019     READINT32(in, fingerCount_);
1020     READFLOAT(in, zOrder_);
1021 
1022     if (!ReadAxisFromParcel(in)) {
1023         return false;
1024     }
1025 
1026     READDOUBLE(in, velocity_);
1027 
1028     READINT32(in, axisEventType_);
1029     READINT32(in, pullId_);
1030 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1031     if (!ReadEnhanceDataFromParcel(in)) {
1032         return false;
1033     }
1034 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1035 
1036 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
1037     READDOUBLE(in, fingerprintDistanceX_);
1038     READDOUBLE(in, fingerprintDistanceY_);
1039 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
1040     return true;
1041 }
1042 
ReadAxisFromParcel(Parcel &in)1043 bool PointerEvent::ReadAxisFromParcel(Parcel &in)
1044 {
1045     uint32_t axes = 0;
1046     READUINT32(in, axes);
1047 
1048     for (int32_t i = AXIS_TYPE_UNKNOWN; i < AXIS_TYPE_MAX; ++i) {
1049         const AxisType axis { static_cast<AxisType>(i) };
1050         if (HasAxis(axes, axis)) {
1051             double val;
1052             READDOUBLE(in, val);
1053             SetAxisValue(axis, val);
1054         }
1055     }
1056     return true;
1057 }
1058 
1059 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
SetFingerprintDistanceX(double x)1060 void PointerEvent::SetFingerprintDistanceX(double x)
1061 {
1062     fingerprintDistanceX_ = x;
1063 }
1064 
SetFingerprintDistanceY(double y)1065 void PointerEvent::SetFingerprintDistanceY(double y)
1066 {
1067     fingerprintDistanceY_ = y;
1068 }
1069 
GetFingerprintDistanceX() const1070 double PointerEvent::GetFingerprintDistanceX() const
1071 {
1072     return fingerprintDistanceX_;
1073 }
1074 
GetFingerprintDistanceY() const1075 double PointerEvent::GetFingerprintDistanceY() const
1076 {
1077     return fingerprintDistanceY_;
1078 }
1079 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
1080 
1081 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
ReadEnhanceDataFromParcel(Parcel &in)1082 bool PointerEvent::ReadEnhanceDataFromParcel(Parcel &in)
1083 {
1084     int32_t size = 0;
1085     READINT32(in, size);
1086     if (size > static_cast<int32_t>(MAX_N_ENHANCE_DATA_SIZE) || size < 0) {
1087         MMI_HILOGE("enhanceData_ size is invalid");
1088         return false;
1089     }
1090 
1091     for (int32_t i = 0; i < size; i++) {
1092         uint32_t val = 0;
1093         READUINT32(in, val);
1094         enhanceData_.emplace_back(val);
1095     }
1096     return true;
1097 }
1098 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
1099 
ReadBufferFromParcel(Parcel &in)1100 bool PointerEvent::ReadBufferFromParcel(Parcel &in)
1101 {
1102     int32_t bufflen = 0;
1103     READINT32(in, bufflen);
1104     if (bufflen > static_cast<int32_t>(MAX_N_BUFFER_SIZE)) {
1105         return false;
1106     }
1107 
1108     for (int32_t i = 0; i < bufflen; ++i) {
1109         uint8_t data;
1110         READUINT8(in, data);
1111         buffer_.push_back(data);
1112     }
1113     return true;
1114 }
1115 
IsValidCheckMouseFunc() const1116 bool PointerEvent::IsValidCheckMouseFunc() const
1117 {
1118     CALL_DEBUG_ENTER;
1119     if (pointers_.size() != 1) {
1120         MMI_HILOGE("Pointers_ is invalid");
1121         return false;
1122     }
1123 
1124     const size_t maxPressedButtons = 3;
1125     if (pressedButtons_.size() > maxPressedButtons) {
1126         MMI_HILOGE("PressedButtons_.size is greater than three and is invalid");
1127         return false;
1128     }
1129 
1130     for (const auto &item : pressedButtons_) {
1131         if (item != MOUSE_BUTTON_LEFT && item != MOUSE_BUTTON_RIGHT && item != MOUSE_BUTTON_MIDDLE) {
1132             MMI_HILOGE("PressedButtons_ is invalid");
1133             return false;
1134         }
1135     }
1136 
1137     int32_t pointAction = GetPointerAction();
1138     bool checkFlag = pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
1139         pointAction != POINTER_ACTION_AXIS_BEGIN && pointAction != POINTER_ACTION_AXIS_UPDATE &&
1140         pointAction != POINTER_ACTION_AXIS_END && pointAction != POINTER_ACTION_BUTTON_DOWN &&
1141         pointAction != POINTER_ACTION_BUTTON_UP;
1142     if (checkFlag) {
1143         MMI_HILOGE("PointAction is invalid");
1144         return false;
1145     }
1146 
1147     int32_t buttonId = GetButtonId();
1148     if (pointAction == POINTER_ACTION_BUTTON_DOWN || pointAction == POINTER_ACTION_BUTTON_UP) {
1149         if (buttonId != MOUSE_BUTTON_LEFT && buttonId != MOUSE_BUTTON_RIGHT && buttonId != MOUSE_BUTTON_MIDDLE) {
1150             MMI_HILOGE("ButtonId is invalid");
1151             return false;
1152         }
1153     } else {
1154         if (buttonId != BUTTON_NONE) {
1155             MMI_HILOGE("ButtonId is not BUTTON_NONE and is invalid");
1156             return false;
1157         }
1158     }
1159     return true;
1160 }
1161 
IsValidCheckMouse() const1162 bool PointerEvent::IsValidCheckMouse() const
1163 {
1164     CALL_DEBUG_ENTER;
1165     int32_t mousePointID = GetPointerId();
1166     if (mousePointID < 0) {
1167         MMI_HILOGE("MousePointID is invalid");
1168         return false;
1169     }
1170 
1171     if (!IsValidCheckMouseFunc()) {
1172         MMI_HILOGE("IsValidCheckMouseFunc is invalid");
1173         return false;
1174     }
1175 
1176     for (const auto &item : pointers_) {
1177         if (item.GetPointerId() < 0) {
1178             MMI_HILOGE("Item.pointerid is invalid");
1179             return false;
1180         }
1181 
1182         if (item.GetPointerId() != mousePointID) {
1183             MMI_HILOGE("Item.pointerid is not same to mousePointID and is invalid");
1184             return false;
1185         }
1186 
1187         if (item.GetDownTime() > 0) {
1188             MMI_HILOGE("Item.downtime is invalid");
1189             return false;
1190         }
1191 
1192         if (item.IsPressed() != false) {
1193             MMI_HILOGE("Item.ispressed is not false and is invalid");
1194             return false;
1195         }
1196     }
1197     return true;
1198 }
1199 
IsValidCheckTouchFunc() const1200 bool PointerEvent::IsValidCheckTouchFunc() const
1201 {
1202     CALL_DEBUG_ENTER;
1203     if (GetPointerId() < 0) {
1204         MMI_HILOGE("TouchPointID is invalid");
1205         return false;
1206     }
1207 
1208     if (!pressedButtons_.empty()) {
1209         MMI_HILOGE("PressedButtons_.size is invalid");
1210         return false;
1211     }
1212 
1213     int32_t pointAction = GetPointerAction();
1214     if (pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
1215         pointAction != POINTER_ACTION_DOWN && pointAction != POINTER_ACTION_UP) {
1216         MMI_HILOGE("PointAction is invalid");
1217         return false;
1218     }
1219 
1220     if (GetButtonId() != BUTTON_NONE) {
1221         MMI_HILOGE("ButtonId is invalid");
1222         return false;
1223     }
1224     return true;
1225 }
1226 
IsValidCheckTouch() const1227 bool PointerEvent::IsValidCheckTouch() const
1228 {
1229     CALL_DEBUG_ENTER;
1230     if (!IsValidCheckTouchFunc()) {
1231         MMI_HILOGE("IsValidCheckTouchFunc is invalid");
1232         return false;
1233     }
1234     bool isSameItem = false;
1235     int32_t touchPointID = GetPointerId();
1236     for (auto item = pointers_.begin(); item != pointers_.end(); item++) {
1237         if (item->GetPointerId() < 0) {
1238             MMI_HILOGE("Item.pointerid is invalid");
1239             return false;
1240         }
1241 
1242         if (item->GetPointerId() == touchPointID) {
1243             isSameItem = true;
1244         }
1245 
1246         if (item->GetDownTime() <= 0) {
1247             MMI_HILOGE("Item.downtime is invalid");
1248             return false;
1249         }
1250 
1251         if (item->IsPressed() != false) {
1252             MMI_HILOGE("Item.ispressed is not false and is invalid");
1253             return false;
1254         }
1255 
1256         auto itemtmp = item;
1257         for (++itemtmp; itemtmp != pointers_.end(); itemtmp++) {
1258             if (item->GetPointerId() == itemtmp->GetPointerId()) {
1259                 MMI_HILOGE("Pointitems pointerid exist same items and is invalid");
1260                 return false;
1261             }
1262         }
1263     }
1264 
1265     if (!isSameItem) {
1266         MMI_HILOGE("Item.pointerid is not same to touchPointID and is invalid");
1267         return false;
1268     }
1269     return true;
1270 }
1271 
IsValid() const1272 bool PointerEvent::IsValid() const
1273 {
1274     CALL_DEBUG_ENTER;
1275     switch (GetSourceType()) {
1276         case SOURCE_TYPE_MOUSE: {
1277             if (!IsValidCheckMouse()) {
1278                 MMI_HILOGE("IsValidCheckMouse is invalid");
1279                 return false;
1280             }
1281             break;
1282         }
1283         case SOURCE_TYPE_TOUCHSCREEN:
1284         case SOURCE_TYPE_TOUCHPAD: {
1285             if (!IsValidCheckTouch()) {
1286                 MMI_HILOGE("IsValidCheckTouch is invalid");
1287                 return false;
1288             }
1289             break;
1290         }
1291         case SOURCE_TYPE_JOYSTICK:
1292             break;
1293         default: {
1294             MMI_HILOGE("SourceType is invalid");
1295             return false;
1296         }
1297     }
1298     return true;
1299 }
1300 
SetBuffer(std::vector<uint8_t> buffer)1301 void PointerEvent::SetBuffer(std::vector<uint8_t> buffer)
1302 {
1303     buffer_ = buffer;
1304 }
1305 
ClearBuffer()1306 void PointerEvent::ClearBuffer()
1307 {
1308     buffer_.clear();
1309 }
1310 
GetBuffer() const1311 std::vector<uint8_t> PointerEvent::GetBuffer() const
1312 {
1313     return buffer_;
1314 }
1315 
GetDispatchTimes() const1316 int32_t PointerEvent::GetDispatchTimes() const
1317 {
1318     return dispatchTimes_;
1319 }
1320 
SetDispatchTimes(int32_t dispatchTimes)1321 void PointerEvent::SetDispatchTimes(int32_t dispatchTimes)
1322 {
1323     dispatchTimes_ = dispatchTimes;
1324 }
1325 
SetHandlerEventType(HandleEventType eventType)1326 void PointerEvent::SetHandlerEventType(HandleEventType eventType)
1327 {
1328     handleEventType_ = eventType;
1329 }
1330 
GetHandlerEventType() const1331 HandleEventType PointerEvent::GetHandlerEventType() const
1332 {
1333     return handleEventType_;
1334 }
1335 
1336 #ifdef OHOS_BUILD_ENABLE_ANCO
SetAncoDeal(bool ancoDeal)1337 void PointerEvent::SetAncoDeal(bool ancoDeal)
1338 {
1339     ancoDeal_ = ancoDeal;
1340 }
1341 
GetAncoDeal() const1342 bool PointerEvent::GetAncoDeal() const
1343 {
1344     return ancoDeal_;
1345 }
1346 #endif // OHOS_BUILD_ENABLE_ANCO
1347 
ActionToShortStr(int32_t action)1348 std::string_view PointerEvent::ActionToShortStr(int32_t action)
1349 {
1350     // 该函数逻辑简单,功能单一,考虑性能影响,使用switch-case而不是表驱动实现。
1351     switch (action) {
1352         case PointerEvent::POINTER_ACTION_CANCEL:
1353             return "P:C:";
1354         case PointerEvent::POINTER_ACTION_DOWN:
1355             return "P:D:";
1356         case PointerEvent::POINTER_ACTION_MOVE:
1357             return "P:M:";
1358         case PointerEvent::POINTER_ACTION_UP:
1359             return "P:U:";
1360         case PointerEvent::POINTER_ACTION_AXIS_BEGIN:
1361             return "P:AB:";
1362         case PointerEvent::POINTER_ACTION_AXIS_UPDATE:
1363             return "P:AU:";
1364         case PointerEvent::POINTER_ACTION_AXIS_END:
1365             return "P:AE:";
1366         case PointerEvent::POINTER_ACTION_BUTTON_DOWN:
1367             return "P:BD:";
1368         case PointerEvent::POINTER_ACTION_BUTTON_UP:
1369             return "P:BU:";
1370         case PointerEvent::POINTER_ACTION_ENTER_WINDOW:
1371             return "P:EW:";
1372         case PointerEvent::POINTER_ACTION_LEAVE_WINDOW:
1373             return "P:LW:";
1374         case PointerEvent::POINTER_ACTION_PULL_DOWN:
1375             return "P:PD:";
1376         case PointerEvent::POINTER_ACTION_PULL_MOVE:
1377             return "P:PM:";
1378         case PointerEvent::POINTER_ACTION_PULL_UP:
1379             return "P:PU:";
1380         case PointerEvent::POINTER_ACTION_PULL_IN_WINDOW:
1381             return "P:PI:";
1382         case PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW:
1383             return "P:PO:";
1384         case PointerEvent::POINTER_ACTION_SWIPE_BEGIN:
1385             return "P:SB:";
1386         case PointerEvent::POINTER_ACTION_SWIPE_UPDATE:
1387             return "P:SU:";
1388         case PointerEvent::POINTER_ACTION_SWIPE_END:
1389             return "P:SE:";
1390         case PointerEvent::POINTER_ACTION_ROTATE_BEGIN:
1391             return "P:RB:";
1392         case PointerEvent::POINTER_ACTION_ROTATE_UPDATE:
1393             return "P:RU:";
1394         case PointerEvent::POINTER_ACTION_ROTATE_END:
1395             return "P:RE:";
1396         case PointerEvent::POINTER_ACTION_TRIPTAP:
1397             return "P:TT:";
1398         case PointerEvent::POINTER_ACTION_QUADTAP:
1399             return "P:Q:";
1400         case PointerEvent::POINTER_ACTION_HOVER_MOVE:
1401             return "P:HM:";
1402         case PointerEvent::POINTER_ACTION_HOVER_ENTER:
1403             return "P:HE:";
1404         case PointerEvent::POINTER_ACTION_HOVER_EXIT:
1405             return "P:HEX:";
1406         case PointerEvent::POINTER_ACTION_FINGERPRINT_DOWN:
1407             return "P:FD:";
1408         case PointerEvent::POINTER_ACTION_FINGERPRINT_UP:
1409             return "P:FU:";
1410         case PointerEvent::POINTER_ACTION_FINGERPRINT_SLIDE:
1411             return "P:FS:";
1412         case PointerEvent::POINTER_ACTION_FINGERPRINT_RETOUCH:
1413             return "P:FR:";
1414         case PointerEvent::POINTER_ACTION_FINGERPRINT_CLICK:
1415             return "P:FC:";
1416         case PointerEvent::POINTER_ACTION_FINGERPRINT_CANCEL:
1417             return "P:FCA:";
1418         case PointerEvent::POINTER_ACTION_UNKNOWN:
1419             return "P:UK:";
1420         default:
1421             return "P:?:";
1422     }
1423 }
1424 } // namespace MMI
1425 } // namespace OHOS
1426