• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "pointer_event.h"
17 
18 #include <iomanip>
19 
20 #include "mmi_log.h"
21 
22 using namespace OHOS::HiviewDFX;
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "PointerEvent" };
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 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
31 constexpr size_t MAX_N_ENHANCE_DATA_SIZE { 64 };
32 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
33 constexpr size_t MAX_N_BUFFER_SIZE { 512 };
34 } // namespace
35 
from(std::shared_ptr<InputEvent> inputEvent)36 std::shared_ptr<PointerEvent> PointerEvent::from(std::shared_ptr<InputEvent> inputEvent)
37 {
38     return nullptr;
39 }
40 
PointerItem()41 PointerEvent::PointerItem::PointerItem() {}
42 
~PointerItem()43 PointerEvent::PointerItem::~PointerItem() {}
44 
GetPointerId() const45 int32_t PointerEvent::PointerItem::GetPointerId() const
46 {
47     return pointerId_;
48 }
49 
SetPointerId(int32_t pointerId)50 void PointerEvent::PointerItem::SetPointerId(int32_t pointerId)
51 {
52     pointerId_ = pointerId;
53 }
54 
GetDownTime() const55 int64_t PointerEvent::PointerItem::GetDownTime() const
56 {
57     return downTime_;
58 }
59 
SetDownTime(int64_t downTime)60 void PointerEvent::PointerItem::SetDownTime(int64_t downTime)
61 {
62     downTime_ = downTime;
63 }
64 
IsPressed() const65 bool PointerEvent::PointerItem::IsPressed() const
66 {
67     return pressed_;
68 }
69 
SetPressed(bool pressed)70 void PointerEvent::PointerItem::SetPressed(bool pressed)
71 {
72     pressed_ = pressed;
73 }
74 
GetDisplayX() const75 int32_t PointerEvent::PointerItem::GetDisplayX() const
76 {
77     return displayX_;
78 }
79 
SetDisplayX(int32_t x)80 void PointerEvent::PointerItem::SetDisplayX(int32_t x)
81 {
82     displayX_ = x;
83 }
84 
GetDisplayY() const85 int32_t PointerEvent::PointerItem::GetDisplayY() const
86 {
87     return displayY_;
88 }
89 
SetDisplayY(int32_t y)90 void PointerEvent::PointerItem::SetDisplayY(int32_t y)
91 {
92     displayY_ = y;
93 }
94 
GetWindowX() const95 int32_t PointerEvent::PointerItem::GetWindowX() const
96 {
97     return windowX_;
98 }
99 
SetWindowX(int32_t x)100 void PointerEvent::PointerItem::SetWindowX(int32_t x)
101 {
102     windowX_ = x;
103 }
104 
GetWindowY() const105 int32_t PointerEvent::PointerItem::GetWindowY() const
106 {
107     return windowY_;
108 }
109 
SetWindowY(int32_t y)110 void PointerEvent::PointerItem::SetWindowY(int32_t y)
111 {
112     windowY_ = y;
113 }
114 
GetWidth() const115 int32_t PointerEvent::PointerItem::GetWidth() const
116 {
117     return width_;
118 }
119 
SetWidth(int32_t width)120 void PointerEvent::PointerItem::SetWidth(int32_t width)
121 {
122     width_ = width;
123 }
124 
GetHeight() const125 int32_t PointerEvent::PointerItem::GetHeight() const
126 {
127     return height_;
128 }
129 
SetHeight(int32_t height)130 void PointerEvent::PointerItem::SetHeight(int32_t height)
131 {
132     height_ = height;
133 }
134 
GetTiltX() const135 double PointerEvent::PointerItem::GetTiltX() const
136 {
137     return tiltX_;
138 }
139 
SetTiltX(double tiltX)140 void PointerEvent::PointerItem::SetTiltX(double tiltX)
141 {
142     tiltX_ = tiltX;
143 }
144 
GetTiltY() const145 double PointerEvent::PointerItem::GetTiltY() const
146 {
147     return tiltY_;
148 }
149 
SetTiltY(double tiltY)150 void PointerEvent::PointerItem::SetTiltY(double tiltY)
151 {
152     tiltY_ = tiltY;
153 }
154 
GetToolDisplayX() const155 int32_t PointerEvent::PointerItem::GetToolDisplayX() const
156 {
157     return toolDisplayX_;
158 }
159 
SetToolDisplayX(int32_t x)160 void PointerEvent::PointerItem::SetToolDisplayX(int32_t x)
161 {
162     toolDisplayX_ = x;
163 }
164 
GetToolDisplayY() const165 int32_t PointerEvent::PointerItem::GetToolDisplayY() const
166 {
167     return toolDisplayY_;
168 }
169 
SetToolDisplayY(int32_t y)170 void PointerEvent::PointerItem::SetToolDisplayY(int32_t y)
171 {
172     toolDisplayY_ = y;
173 }
174 
GetToolWindowX() const175 int32_t PointerEvent::PointerItem::GetToolWindowX() const
176 {
177     return toolWindowX_;
178 }
179 
SetToolWindowX(int32_t x)180 void PointerEvent::PointerItem::SetToolWindowX(int32_t x)
181 {
182     toolWindowX_ = x;
183 }
184 
GetToolWindowY() const185 int32_t PointerEvent::PointerItem::GetToolWindowY() const
186 {
187     return toolWindowY_;
188 }
189 
SetToolWindowY(int32_t y)190 void PointerEvent::PointerItem::SetToolWindowY(int32_t y)
191 {
192     toolWindowY_ = y;
193 }
194 
GetToolWidth() const195 int32_t PointerEvent::PointerItem::GetToolWidth() const
196 {
197     return toolWidth_;
198 }
199 
SetToolWidth(int32_t width)200 void PointerEvent::PointerItem::SetToolWidth(int32_t width)
201 {
202     toolWidth_ = width;
203 }
204 
GetToolHeight() const205 int32_t PointerEvent::PointerItem::GetToolHeight() const
206 {
207     return toolHeight_;
208 }
209 
SetToolHeight(int32_t height)210 void PointerEvent::PointerItem::SetToolHeight(int32_t height)
211 {
212     toolHeight_ = height;
213 }
214 
GetPressure() const215 double PointerEvent::PointerItem::GetPressure() const
216 {
217     return pressure_;
218 }
219 
SetPressure(double pressure)220 void PointerEvent::PointerItem::SetPressure(double pressure)
221 {
222     pressure_ = pressure >= MAX_PRESSURE ? MAX_PRESSURE : pressure;
223 }
224 
GetLongAxis() const225 int32_t PointerEvent::PointerItem::GetLongAxis() const
226 {
227     return longAxis_;
228 }
229 
SetLongAxis(int32_t longAxis)230 void PointerEvent::PointerItem::SetLongAxis(int32_t longAxis)
231 {
232     longAxis_ = longAxis;
233 }
234 
GetShortAxis() const235 int32_t PointerEvent::PointerItem::GetShortAxis() const
236 {
237     return shortAxis_;
238 }
239 
SetShortAxis(int32_t shortAxis)240 void PointerEvent::PointerItem::SetShortAxis(int32_t shortAxis)
241 {
242     shortAxis_ = shortAxis;
243 }
244 
GetDeviceId() const245 int32_t PointerEvent::PointerItem::GetDeviceId() const
246 {
247     return deviceId_;
248 }
249 
SetDeviceId(int32_t deviceId)250 void PointerEvent::PointerItem::SetDeviceId(int32_t deviceId)
251 {
252     deviceId_ = deviceId;
253 }
254 
GetToolType() const255 int32_t PointerEvent::PointerItem::GetToolType() const
256 {
257     return toolType_;
258 }
259 
SetToolType(int32_t toolType)260 void PointerEvent::PointerItem::SetToolType(int32_t toolType)
261 {
262     toolType_ = toolType;
263 }
264 
GetTargetWindowId() const265 int32_t PointerEvent::PointerItem::GetTargetWindowId() const
266 {
267     return targetWindowId_;
268 }
269 
SetTargetWindowId(int32_t windowId)270 void PointerEvent::PointerItem::SetTargetWindowId(int32_t windowId)
271 {
272     targetWindowId_ = windowId;
273 }
274 
GetRawDx() const275 int32_t PointerEvent::PointerItem::GetRawDx() const
276 {
277     return rawDx_;
278 }
279 
SetRawDx(int32_t rawDx)280 void PointerEvent::PointerItem::SetRawDx(int32_t rawDx)
281 {
282     rawDx_ = rawDx;
283 }
284 
GetRawDy() const285 int32_t PointerEvent::PointerItem::GetRawDy() const
286 {
287     return rawDy_;
288 }
289 
SetRawDy(int32_t rawDy)290 void PointerEvent::PointerItem::SetRawDy(int32_t rawDy)
291 {
292     rawDy_ = rawDy;
293 }
294 
WriteToParcel(Parcel & out) const295 bool PointerEvent::PointerItem::WriteToParcel(Parcel &out) const
296 {
297     return (
298         out.WriteInt32(pointerId_) &&
299         out.WriteInt64(downTime_) &&
300         out.WriteBool(pressed_) &&
301         out.WriteInt32(displayX_) &&
302         out.WriteInt32(displayY_) &&
303         out.WriteInt32(windowX_) &&
304         out.WriteInt32(windowY_) &&
305         out.WriteInt32(width_) &&
306         out.WriteInt32(height_) &&
307         out.WriteInt32(toolDisplayX_) &&
308         out.WriteInt32(toolDisplayY_) &&
309         out.WriteInt32(toolWindowX_) &&
310         out.WriteInt32(toolWindowY_) &&
311         out.WriteInt32(toolWidth_) &&
312         out.WriteInt32(toolHeight_) &&
313         out.WriteDouble(tiltX_) &&
314         out.WriteDouble(tiltY_) &&
315         out.WriteDouble(pressure_) &&
316         out.WriteInt32(longAxis_) &&
317         out.WriteInt32(shortAxis_) &&
318         out.WriteInt32(toolType_) &&
319         out.WriteInt32(deviceId_) &&
320         out.WriteInt32(rawDx_) &&
321         out.WriteInt32(rawDy_)
322     );
323 }
324 
ReadFromParcel(Parcel & in)325 bool PointerEvent::PointerItem::ReadFromParcel(Parcel &in)
326 {
327     return (
328         in.ReadInt32(pointerId_) &&
329         in.ReadInt64(downTime_) &&
330         in.ReadBool(pressed_) &&
331         in.ReadInt32(displayX_) &&
332         in.ReadInt32(displayY_) &&
333         in.ReadInt32(windowX_) &&
334         in.ReadInt32(windowY_) &&
335         in.ReadInt32(width_) &&
336         in.ReadInt32(height_) &&
337         in.ReadInt32(toolDisplayX_) &&
338         in.ReadInt32(toolDisplayY_) &&
339         in.ReadInt32(toolWindowX_) &&
340         in.ReadInt32(toolWindowY_) &&
341         in.ReadInt32(toolWidth_) &&
342         in.ReadInt32(toolHeight_) &&
343         in.ReadDouble(tiltX_) &&
344         in.ReadDouble(tiltY_) &&
345         in.ReadDouble(pressure_) &&
346         in.ReadInt32(longAxis_) &&
347         in.ReadInt32(shortAxis_) &&
348         in.ReadInt32(toolType_) &&
349         in.ReadInt32(deviceId_) &&
350         in.ReadInt32(rawDx_) &&
351         in.ReadInt32(rawDy_)
352     );
353 }
354 
PointerEvent(int32_t eventType)355 PointerEvent::PointerEvent(int32_t eventType) : InputEvent(eventType) {}
356 
PointerEvent(const PointerEvent & other)357 PointerEvent::PointerEvent(const PointerEvent& other)
358     : InputEvent(other), pointerId_(other.pointerId_), pointers_(other.pointers_),
359       pressedButtons_(other.pressedButtons_), sourceType_(other.sourceType_),
360       pointerAction_(other.pointerAction_), buttonId_(other.buttonId_), fingerCount_(other.fingerCount_),
361       zOrder_(other.zOrder_), axes_(other.axes_), axisValues_(other.axisValues_),
362       pressedKeys_(other.pressedKeys_), buffer_(other.buffer_) {}
363 
~PointerEvent()364 PointerEvent::~PointerEvent() {}
365 
Create()366 std::shared_ptr<PointerEvent> PointerEvent::Create()
367 {
368     auto event = std::shared_ptr<PointerEvent>(new (std::nothrow) PointerEvent(InputEvent::EVENT_TYPE_POINTER));
369     CHKPP(event);
370     return event;
371 }
372 
Reset()373 void PointerEvent::Reset()
374 {
375     InputEvent::Reset();
376     pointerId_ = -1;
377     pointers_.clear();
378     pressedButtons_.clear();
379     sourceType_ = SOURCE_TYPE_UNKNOWN;
380     pointerAction_ = POINTER_ACTION_UNKNOWN;
381     buttonId_ = -1;
382     fingerCount_ = 0;
383     zOrder_ = -1.0f;
384     axes_ = 0U;
385     axisValues_.fill(0.0);
386     pressedKeys_.clear();
387 }
388 
GetPointerAction() const389 int32_t PointerEvent::GetPointerAction() const
390 {
391     return pointerAction_;
392 }
393 
SetPointerAction(int32_t pointerAction)394 void PointerEvent::SetPointerAction(int32_t pointerAction)
395 {
396     pointerAction_ = pointerAction;
397 }
398 
399 static const std::unordered_map<int32_t, std::string> pointerActionMap = {
400     { PointerEvent::POINTER_ACTION_CANCEL, "cancel" },
401     { PointerEvent::POINTER_ACTION_DOWN, "down" },
402     { PointerEvent::POINTER_ACTION_MOVE, "move" },
403     { PointerEvent::POINTER_ACTION_UP, "up" },
404     { PointerEvent::POINTER_ACTION_AXIS_BEGIN, "axis-begin" },
405     { PointerEvent::POINTER_ACTION_AXIS_UPDATE, "axis-update" },
406     { PointerEvent::POINTER_ACTION_AXIS_END, "axis-end" },
407     { PointerEvent::POINTER_ACTION_BUTTON_DOWN, "button-down" },
408     { PointerEvent::POINTER_ACTION_BUTTON_UP, "button-up" },
409     { PointerEvent::POINTER_ACTION_ENTER_WINDOW, "enter-window" },
410     { PointerEvent::POINTER_ACTION_LEAVE_WINDOW, "leave-window" },
411     { PointerEvent::POINTER_ACTION_PULL_DOWN, "pull-down" },
412     { PointerEvent::POINTER_ACTION_PULL_MOVE, "pull-move" },
413     { PointerEvent::POINTER_ACTION_PULL_UP, "pull-up" },
414     { PointerEvent::POINTER_ACTION_PULL_IN_WINDOW, "pull-in-window" },
415     { PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW, "pull-out-window" },
416     { PointerEvent::POINTER_ACTION_SWIPE_BEGIN, "swipe-begin" },
417     { PointerEvent::POINTER_ACTION_SWIPE_UPDATE, "swipe-update" },
418     { PointerEvent::POINTER_ACTION_SWIPE_END, "swipe-end" },
419     { PointerEvent::POINTER_ACTION_ROTATE_BEGIN, "rotate-begin" },
420     { PointerEvent::POINTER_ACTION_ROTATE_UPDATE, "rotate-update" },
421     { PointerEvent::POINTER_ACTION_ROTATE_END, "rotate-end" },
422     { PointerEvent::POINTER_ACTION_TRIPTAP, "touchpad-triptap" },
423     { PointerEvent::POINTER_ACTION_QUADTAP, "quadtap" },
424     { PointerEvent::POINTER_ACTION_HOVER_MOVE, "hover-move" },
425     { PointerEvent::POINTER_ACTION_HOVER_ENTER, "hover-enter" },
426     { PointerEvent::POINTER_ACTION_HOVER_EXIT, "hover-exit" }
427 };
428 
DumpPointerAction() const429 const char* PointerEvent::DumpPointerAction() const
430 {
431     auto it = pointerActionMap.find(pointerAction_);
432     if (it != pointerActionMap.end()) {
433         return it->second.c_str();
434     }
435     return "unknown";
436 }
437 
GetPointerId() const438 int32_t PointerEvent::GetPointerId() const
439 {
440     return pointerId_;
441 }
442 
SetPointerId(int32_t pointerId)443 void PointerEvent::SetPointerId(int32_t pointerId)
444 {
445     pointerId_ = pointerId;
446 }
447 
GetPointerItem(int32_t pointerId,PointerItem & pointerItem)448 bool PointerEvent::GetPointerItem(int32_t pointerId, PointerItem &pointerItem)
449 {
450     for (const auto &item : pointers_) {
451         if (item.GetPointerId() == pointerId) {
452             pointerItem = item;
453             return true;
454         }
455     }
456     return false;
457 }
458 
RemovePointerItem(int32_t pointerId)459 void PointerEvent::RemovePointerItem(int32_t pointerId)
460 {
461     for (auto it = pointers_.begin(); it != pointers_.end(); ++it) {
462         if (it->GetPointerId() == pointerId) {
463             pointers_.erase(it);
464             break;
465         }
466     }
467 }
468 
AddPointerItem(PointerItem & pointerItem)469 void PointerEvent::AddPointerItem(PointerItem &pointerItem)
470 {
471     if (pointers_.size() >= MAX_N_POINTER_ITEMS) {
472         MMI_HILOGE("Exceed maximum allowed number of pointer items");
473         return;
474     }
475     pointers_.push_back(pointerItem);
476 }
477 
UpdatePointerItem(int32_t pointerId,PointerItem & pointerItem)478 void PointerEvent::UpdatePointerItem(int32_t pointerId, PointerItem &pointerItem)
479 {
480     for (auto &item : pointers_) {
481         if (item.GetPointerId() == pointerId) {
482             item = pointerItem;
483             return;
484         }
485     }
486     AddPointerItem(pointerItem);
487 }
488 
GetPressedButtons() const489 std::set<int32_t> PointerEvent::GetPressedButtons() const
490 {
491     return pressedButtons_;
492 }
493 
IsButtonPressed(int32_t buttonId) const494 bool PointerEvent::IsButtonPressed(int32_t buttonId) const
495 {
496     return (pressedButtons_.find(buttonId) != pressedButtons_.end());
497 }
498 
SetButtonPressed(int32_t buttonId)499 void PointerEvent::SetButtonPressed(int32_t buttonId)
500 {
501     if (pressedButtons_.size() >= MAX_N_PRESSED_BUTTONS) {
502         MMI_HILOGE("Exceed maximum allowed number of pressed buttons");
503         return;
504     }
505     auto iter = pressedButtons_.insert(buttonId);
506     if (!iter.second) {
507         MMI_HILOGE("Insert value failed, button:%{public}d", buttonId);
508     }
509 }
510 
DeleteReleaseButton(int32_t buttonId)511 void PointerEvent::DeleteReleaseButton(int32_t buttonId)
512 {
513     if (pressedButtons_.find(buttonId) != pressedButtons_.end()) {
514         pressedButtons_.erase(buttonId);
515     }
516 }
517 
ClearButtonPressed()518 void PointerEvent::ClearButtonPressed()
519 {
520     pressedButtons_.clear();
521 }
522 
GetPointerIds() const523 std::vector<int32_t> PointerEvent::GetPointerIds() const
524 {
525     std::vector<int32_t> pointerIdList;
526     for (const auto &item : pointers_) {
527         pointerIdList.push_back(item.GetPointerId());
528     }
529     return pointerIdList;
530 }
531 
GetSourceType() const532 int32_t PointerEvent::GetSourceType() const
533 {
534     return sourceType_;
535 }
536 
SetSourceType(int32_t sourceType)537 void PointerEvent::SetSourceType(int32_t sourceType)
538 {
539     sourceType_ = sourceType;
540 }
541 
DumpSourceType() const542 const char* PointerEvent::DumpSourceType() const
543 {
544     switch (sourceType_) {
545         case PointerEvent::SOURCE_TYPE_MOUSE: {
546             return "mouse";
547         }
548         case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
549             return "touch-screen";
550         }
551         case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
552             return "touch-pad";
553         }
554         case PointerEvent::SOURCE_TYPE_JOYSTICK: {
555             return "joystick";
556         }
557         default: {
558             break;
559         }
560     }
561     return "unknown";
562 }
563 
GetButtonId() const564 int32_t PointerEvent::GetButtonId() const
565 {
566     return buttonId_;
567 }
568 
SetButtonId(int32_t buttonId)569 void PointerEvent::SetButtonId(int32_t buttonId)
570 {
571     buttonId_ = buttonId;
572 }
573 
GetFingerCount() const574 int32_t PointerEvent::GetFingerCount() const
575 {
576     return fingerCount_;
577 }
578 
SetFingerCount(int32_t fingerCount)579 void PointerEvent::SetFingerCount(int32_t fingerCount)
580 {
581     fingerCount_ = fingerCount;
582 }
583 
GetZOrder() const584 float  PointerEvent::GetZOrder() const
585 {
586     return zOrder_;
587 }
588 
SetZOrder(float zOrder)589 void  PointerEvent::SetZOrder(float zOrder)
590 {
591     zOrder_ = zOrder;
592 }
593 
GetAxisValue(AxisType axis) const594 double PointerEvent::GetAxisValue(AxisType axis) const
595 {
596     double axisValue {};
597     if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
598         axisValue = axisValues_[axis];
599     }
600     return axisValue;
601 }
602 
SetAxisValue(AxisType axis,double axisValue)603 void PointerEvent::SetAxisValue(AxisType axis, double axisValue)
604 {
605     if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
606         axisValues_[axis] = axisValue;
607         axes_ = static_cast<uint32_t>(axes_ | static_cast<uint32_t>(1 << axis));
608     }
609 }
610 
ClearAxisValue()611 void PointerEvent::ClearAxisValue()
612 {
613     axisValues_ = {};
614     axes_ = 0;
615 }
616 
HasAxis(uint32_t axes,AxisType axis)617 bool PointerEvent::HasAxis(uint32_t axes, AxisType axis)
618 {
619     bool ret { false };
620     if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
621         ret = static_cast<bool>(static_cast<uint32_t>(axes) & (1 << static_cast<uint32_t>(axis)));
622     }
623     return ret;
624 }
625 
SetPressedKeys(const std::vector<int32_t> pressedKeys)626 void PointerEvent::SetPressedKeys(const std::vector<int32_t> pressedKeys)
627 {
628     pressedKeys_ = pressedKeys;
629 }
630 
GetPressedKeys() const631 std::vector<int32_t> PointerEvent::GetPressedKeys() const
632 {
633     return pressedKeys_;
634 }
635 
636 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
SetEnhanceData(const std::vector<uint8_t> enhanceData)637 void PointerEvent::SetEnhanceData(const std::vector<uint8_t> enhanceData)
638 {
639     enhanceData_ = enhanceData;
640 }
641 
GetEnhanceData() const642 std::vector<uint8_t> PointerEvent::GetEnhanceData() const
643 {
644     return enhanceData_;
645 }
646 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
647 
WriteToParcel(Parcel & out) const648 bool PointerEvent::WriteToParcel(Parcel &out) const
649 {
650     if (!InputEvent::WriteToParcel(out)) {
651         return false;
652     }
653 
654     WRITEINT32(out, pointerId_);
655 
656     WRITEINT32(out, static_cast<int32_t>(pointers_.size()));
657 
658     for (const auto &item : pointers_) {
659         if (!item.WriteToParcel(out)) {
660             return false;
661         }
662     }
663 
664     WRITEINT32(out, static_cast<int32_t>(buffer_.size()));
665 
666     for (const auto& buff : buffer_) {
667         WRITEUINT8(out, buff);
668     }
669 
670     WRITEINT32(out, static_cast<int32_t>(pressedButtons_.size()));
671 
672     for (const auto &item : pressedButtons_) {
673         WRITEINT32(out, item);
674     }
675 
676     WRITEINT32(out, sourceType_);
677 
678     WRITEINT32(out, pointerAction_);
679 
680     WRITEINT32(out, buttonId_);
681 
682     WRITEINT32(out, fingerCount_);
683 
684     WRITEFLOAT(out, zOrder_);
685 
686     const uint32_t axes { GetAxes() };
687     WRITEUINT32(out, axes);
688 
689     for (int32_t i = AXIS_TYPE_UNKNOWN; i < AXIS_TYPE_MAX; ++i) {
690         const AxisType axis { static_cast<AxisType>(i) };
691         if (HasAxis(axes, axis)) {
692             WRITEDOUBLE(out, GetAxisValue(axis));
693         }
694     }
695 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
696     WRITEINT32(out, static_cast<int32_t>(enhanceData_.size()));
697     for (uint32_t i = 0; i < enhanceData_.size(); i++) {
698         WRITEUINT32(out, enhanceData_[i]);
699     }
700 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
701     return true;
702 }
703 
ReadFromParcel(Parcel & in)704 bool PointerEvent::ReadFromParcel(Parcel &in)
705 {
706     if (!InputEvent::ReadFromParcel(in)) {
707         return false;
708     }
709 
710     READINT32(in, pointerId_);
711 
712     int32_t nPointers;
713     READINT32(in, nPointers);
714     if (nPointers > static_cast<int32_t>(MAX_N_POINTER_ITEMS)) {
715         return false;
716     }
717 
718     for (int32_t i = 0; i < nPointers; ++i) {
719         PointerItem item;
720         if (!item.ReadFromParcel(in)) {
721             return false;
722         }
723         AddPointerItem(item);
724     }
725 
726     if (!ReadBufferFromParcel(in)) {
727         return false;
728     }
729 
730     int32_t nPressedButtons;
731     READINT32(in, nPressedButtons);
732     if (nPressedButtons > static_cast<int32_t>(MAX_N_PRESSED_BUTTONS)) {
733         return false;
734     }
735 
736     for (int32_t i = 0; i < nPressedButtons; ++i) {
737         int32_t buttonId;
738         READINT32(in, buttonId);
739         SetButtonPressed(buttonId);
740     }
741 
742     READINT32(in, sourceType_);
743 
744     READINT32(in, pointerAction_);
745 
746     READINT32(in, buttonId_);
747 
748     READINT32(in, fingerCount_);
749 
750     READFLOAT(in, zOrder_);
751 
752     uint32_t axes;
753     READUINT32(in, axes);
754 
755     for (int32_t i = AXIS_TYPE_UNKNOWN; i < AXIS_TYPE_MAX; ++i) {
756         const AxisType axis { static_cast<AxisType>(i) };
757         if (HasAxis(axes, axis)) {
758             double val;
759             READDOUBLE(in, val);
760             SetAxisValue(axis, val);
761         }
762     }
763 
764 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
765     if (!ReadEnhanceDataFromParcel(in)) {
766         return false;
767     }
768 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
769     return true;
770 }
771 
772 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
ReadEnhanceDataFromParcel(Parcel & in)773 bool PointerEvent::ReadEnhanceDataFromParcel(Parcel &in)
774 {
775     int32_t size = 0;
776     READINT32(in, size);
777     if (size > static_cast<int32_t>(MAX_N_ENHANCE_DATA_SIZE) || size < 0) {
778         MMI_HILOGE("enhanceData_ size is invalid");
779         return false;
780     }
781 
782     for (int32_t i = 0; i < size; i++) {
783         uint32_t val;
784         READUINT32(in, val);
785         enhanceData_.emplace_back(val);
786     }
787     return true;
788 }
789 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
790 
ReadBufferFromParcel(Parcel & in)791 bool PointerEvent::ReadBufferFromParcel(Parcel &in)
792 {
793     int32_t bufflen;
794     READINT32(in, bufflen);
795     if (bufflen > static_cast<int32_t>(MAX_N_BUFFER_SIZE)) {
796         return false;
797     }
798 
799     for (int32_t i = 0; i < bufflen; ++i) {
800         uint8_t data;
801         READUINT8(in, data);
802         buffer_.push_back(data);
803     }
804     return true;
805 }
806 
IsValidCheckMouseFunc() const807 bool PointerEvent::IsValidCheckMouseFunc() const
808 {
809     CALL_DEBUG_ENTER;
810     if (pointers_.size() != 1) {
811         MMI_HILOGE("Pointers_ is invalid");
812         return false;
813     }
814 
815     size_t maxPressedButtons = 3;
816     if (pressedButtons_.size() > maxPressedButtons) {
817         MMI_HILOGE("PressedButtons_.size is greater than three and is invalid");
818         return false;
819     }
820 
821     for (const auto &item : pressedButtons_) {
822         if (item != MOUSE_BUTTON_LEFT && item != MOUSE_BUTTON_RIGHT && item != MOUSE_BUTTON_MIDDLE) {
823             MMI_HILOGE("PressedButtons_ is invalid");
824             return false;
825         }
826     }
827 
828     int32_t pointAction = GetPointerAction();
829     bool checkFlag = pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
830         pointAction != POINTER_ACTION_AXIS_BEGIN && pointAction != POINTER_ACTION_AXIS_UPDATE &&
831         pointAction != POINTER_ACTION_AXIS_END && pointAction != POINTER_ACTION_BUTTON_DOWN &&
832         pointAction != POINTER_ACTION_BUTTON_UP;
833     if (checkFlag) {
834         MMI_HILOGE("PointAction is invalid");
835         return false;
836     }
837 
838     int32_t buttonId = GetButtonId();
839     if (pointAction == POINTER_ACTION_BUTTON_DOWN || pointAction == POINTER_ACTION_BUTTON_UP) {
840         if (buttonId != MOUSE_BUTTON_LEFT && buttonId != MOUSE_BUTTON_RIGHT && buttonId != MOUSE_BUTTON_MIDDLE) {
841             MMI_HILOGE("ButtonId is invalid");
842             return false;
843         }
844     } else {
845         if (buttonId != BUTTON_NONE) {
846             MMI_HILOGE("ButtonId is not BUTTON_NONE and is invalid");
847             return false;
848         }
849     }
850     return true;
851 }
852 
IsValidCheckMouse() const853 bool PointerEvent::IsValidCheckMouse() const
854 {
855     CALL_DEBUG_ENTER;
856     int32_t mousePointID = GetPointerId();
857     if (mousePointID < 0) {
858         MMI_HILOGE("MousePointID is invalid");
859         return false;
860     }
861 
862     if (!IsValidCheckMouseFunc()) {
863         MMI_HILOGE("IsValidCheckMouseFunc is invalid");
864         return false;
865     }
866 
867     for (const auto &item : pointers_) {
868         if (item.GetPointerId() < 0) {
869             MMI_HILOGE("Item.pointerid is invalid");
870             return false;
871         }
872 
873         if (item.GetPointerId() != mousePointID) {
874             MMI_HILOGE("Item.pointerid is not same to mousePointID and is invalid");
875             return false;
876         }
877 
878         if (item.GetDownTime() > 0) {
879             MMI_HILOGE("Item.downtime is invalid");
880             return false;
881         }
882 
883         if (item.IsPressed() != false) {
884             MMI_HILOGE("Item.ispressed is not false and is invalid");
885             return false;
886         }
887     }
888     return true;
889 }
890 
IsValidCheckTouchFunc() const891 bool PointerEvent::IsValidCheckTouchFunc() const
892 {
893     CALL_DEBUG_ENTER;
894     int32_t touchPointID = GetPointerId();
895     if (touchPointID < 0) {
896         MMI_HILOGE("TouchPointID is invalid");
897         return false;
898     }
899 
900     if (!pressedButtons_.empty()) {
901         MMI_HILOGE("PressedButtons_.size is invalid");
902         return false;
903     }
904 
905     int32_t pointAction = GetPointerAction();
906     if (pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
907         pointAction != POINTER_ACTION_DOWN && pointAction != POINTER_ACTION_UP) {
908         MMI_HILOGE("PointAction is invalid");
909         return false;
910     }
911 
912     if (GetButtonId() != BUTTON_NONE) {
913         MMI_HILOGE("ButtonId is invalid");
914         return false;
915     }
916     return true;
917 }
918 
IsValidCheckTouch() const919 bool PointerEvent::IsValidCheckTouch() const
920 {
921     CALL_DEBUG_ENTER;
922     if (!IsValidCheckTouchFunc()) {
923         MMI_HILOGE("IsValidCheckTouchFunc is invalid");
924         return false;
925     }
926     bool isSameItem = false;
927     int32_t touchPointID = GetPointerId();
928     for (auto item = pointers_.begin(); item != pointers_.end(); item++) {
929         if (item->GetPointerId() < 0) {
930             MMI_HILOGE("Item.pointerid is invalid");
931             return false;
932         }
933 
934         if (item->GetPointerId() == touchPointID) {
935             isSameItem = true;
936         }
937 
938         if (item->GetDownTime() <= 0) {
939             MMI_HILOGE("Item.downtime is invalid");
940             return false;
941         }
942 
943         if (item->IsPressed() != false) {
944             MMI_HILOGE("Item.ispressed is not false and is invalid");
945             return false;
946         }
947 
948         auto itemtmp = item;
949         for (++itemtmp; itemtmp != pointers_.end(); itemtmp++) {
950             if (item->GetPointerId() == itemtmp->GetPointerId()) {
951                 MMI_HILOGE("Pointitems pointerid exist same items and is invalid");
952                 return false;
953             }
954         }
955     }
956 
957     if (!isSameItem) {
958         MMI_HILOGE("Item.pointerid is not same to touchPointID and is invalid");
959         return false;
960     }
961     return true;
962 }
963 
IsValid() const964 bool PointerEvent::IsValid() const
965 {
966     CALL_DEBUG_ENTER;
967     switch (GetSourceType()) {
968         case SOURCE_TYPE_MOUSE: {
969             if (!IsValidCheckMouse()) {
970                 MMI_HILOGE("IsValidCheckMouse is invalid");
971                 return false;
972             }
973             break;
974         }
975         case SOURCE_TYPE_TOUCHSCREEN:
976         case SOURCE_TYPE_TOUCHPAD: {
977             if (!IsValidCheckTouch()) {
978                 MMI_HILOGE("IsValidCheckTouch is invalid");
979                 return false;
980             }
981             break;
982         }
983         case SOURCE_TYPE_JOYSTICK:
984             break;
985         default: {
986             MMI_HILOGE("SourceType is invalid");
987             return false;
988         }
989     }
990     return true;
991 }
992 
SetBuffer(std::vector<uint8_t> buffer)993 void PointerEvent::SetBuffer(std::vector<uint8_t> buffer)
994 {
995     buffer_ = buffer;
996 }
997 
ClearBuffer()998 void PointerEvent::ClearBuffer()
999 {
1000     buffer_.clear();
1001 }
1002 
GetBuffer() const1003 std::vector<uint8_t> PointerEvent::GetBuffer() const
1004 {
1005     return buffer_;
1006 }
1007 } // namespace MMI
1008 } // namespace OHOS
1009