• 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       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     axes_ = 0U;
384     axisValues_.fill(0.0);
385     pressedKeys_.clear();
386 }
387 
GetPointerAction() const388 int32_t PointerEvent::GetPointerAction() const
389 {
390     return pointerAction_;
391 }
392 
SetPointerAction(int32_t pointerAction)393 void PointerEvent::SetPointerAction(int32_t pointerAction)
394 {
395     pointerAction_ = pointerAction;
396 }
397 
DumpPointerAction() const398 const char* PointerEvent::DumpPointerAction() const
399 {
400     switch (pointerAction_) {
401         case PointerEvent::POINTER_ACTION_CANCEL: {
402             return "cancel";
403         }
404         case PointerEvent::POINTER_ACTION_DOWN: {
405             return "down";
406         }
407         case PointerEvent::POINTER_ACTION_MOVE: {
408             return "move";
409         }
410         case PointerEvent::POINTER_ACTION_UP: {
411             return "up";
412         }
413         case PointerEvent::POINTER_ACTION_AXIS_BEGIN: {
414             return "axis-begin";
415         }
416         case PointerEvent::POINTER_ACTION_AXIS_UPDATE: {
417             return "axis-update";
418         }
419         case PointerEvent::POINTER_ACTION_AXIS_END: {
420             return "axis-end";
421         }
422         case PointerEvent::POINTER_ACTION_BUTTON_DOWN: {
423             return "button-down";
424         }
425         case PointerEvent::POINTER_ACTION_BUTTON_UP: {
426             return "button-up";
427         }
428         case PointerEvent::POINTER_ACTION_ENTER_WINDOW: {
429             return "enter-window";
430         }
431         case PointerEvent::POINTER_ACTION_LEAVE_WINDOW: {
432             return "leave-window";
433         }
434         case PointerEvent::POINTER_ACTION_PULL_DOWN: {
435             return "pull-down";
436         }
437         case PointerEvent::POINTER_ACTION_PULL_MOVE: {
438             return "pull-move";
439         }
440         case PointerEvent::POINTER_ACTION_PULL_UP: {
441             return "pull-up";
442         }
443         case PointerEvent::POINTER_ACTION_PULL_IN_WINDOW: {
444             return "pull-in-window";
445         }
446         case PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW: {
447             return "pull-out-window";
448         }
449         default: {
450             break;
451         }
452     }
453     return "unknown";
454 }
455 
GetPointerId() const456 int32_t PointerEvent::GetPointerId() const
457 {
458     return pointerId_;
459 }
460 
SetPointerId(int32_t pointerId)461 void PointerEvent::SetPointerId(int32_t pointerId)
462 {
463     pointerId_ = pointerId;
464 }
465 
GetPointerItem(int32_t pointerId,PointerItem & pointerItem)466 bool PointerEvent::GetPointerItem(int32_t pointerId, PointerItem &pointerItem)
467 {
468     for (const auto &item : pointers_) {
469         if (item.GetPointerId() == pointerId) {
470             pointerItem = item;
471             return true;
472         }
473     }
474     return false;
475 }
476 
RemovePointerItem(int32_t pointerId)477 void PointerEvent::RemovePointerItem(int32_t pointerId)
478 {
479     for (auto it = pointers_.begin(); it != pointers_.end(); ++it) {
480         if (it->GetPointerId() == pointerId) {
481             pointers_.erase(it);
482             break;
483         }
484     }
485 }
486 
AddPointerItem(PointerItem & pointerItem)487 void PointerEvent::AddPointerItem(PointerItem &pointerItem)
488 {
489     if (pointers_.size() >= MAX_N_POINTER_ITEMS) {
490         MMI_HILOGE("Exceed maximum allowed number of pointer items");
491         return;
492     }
493     pointers_.push_back(pointerItem);
494 }
495 
UpdatePointerItem(int32_t pointerId,PointerItem & pointerItem)496 void PointerEvent::UpdatePointerItem(int32_t pointerId, PointerItem &pointerItem)
497 {
498     for (auto &item : pointers_) {
499         if (item.GetPointerId() == pointerId) {
500             item = pointerItem;
501             return;
502         }
503     }
504     AddPointerItem(pointerItem);
505 }
506 
GetPressedButtons() const507 std::set<int32_t> PointerEvent::GetPressedButtons() const
508 {
509     return pressedButtons_;
510 }
511 
IsButtonPressed(int32_t buttonId) const512 bool PointerEvent::IsButtonPressed(int32_t buttonId) const
513 {
514     return (pressedButtons_.find(buttonId) != pressedButtons_.end());
515 }
516 
SetButtonPressed(int32_t buttonId)517 void PointerEvent::SetButtonPressed(int32_t buttonId)
518 {
519     if (pressedButtons_.size() >= MAX_N_PRESSED_BUTTONS) {
520         MMI_HILOGE("Exceed maximum allowed number of pressed buttons");
521         return;
522     }
523     auto iter = pressedButtons_.insert(buttonId);
524     if (!iter.second) {
525         MMI_HILOGE("Insert value failed, button:%{public}d", buttonId);
526     }
527 }
528 
DeleteReleaseButton(int32_t buttonId)529 void PointerEvent::DeleteReleaseButton(int32_t buttonId)
530 {
531     if (pressedButtons_.find(buttonId) != pressedButtons_.end()) {
532         pressedButtons_.erase(buttonId);
533     }
534 }
535 
ClearButtonPressed()536 void PointerEvent::ClearButtonPressed()
537 {
538     pressedButtons_.clear();
539 }
540 
GetPointerIds() const541 std::vector<int32_t> PointerEvent::GetPointerIds() const
542 {
543     std::vector<int32_t> pointerIdList;
544     for (const auto &item : pointers_) {
545         pointerIdList.push_back(item.GetPointerId());
546     }
547     return pointerIdList;
548 }
549 
GetSourceType() const550 int32_t PointerEvent::GetSourceType() const
551 {
552     return sourceType_;
553 }
554 
SetSourceType(int32_t sourceType)555 void PointerEvent::SetSourceType(int32_t sourceType)
556 {
557     sourceType_ = sourceType;
558 }
559 
DumpSourceType() const560 const char* PointerEvent::DumpSourceType() const
561 {
562     switch (sourceType_) {
563         case PointerEvent::SOURCE_TYPE_MOUSE: {
564             return "mouse";
565         }
566         case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
567             return "touch-screen";
568         }
569         case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
570             return "touch-pad";
571         }
572         case PointerEvent::SOURCE_TYPE_JOYSTICK: {
573             return "joystick";
574         }
575         default: {
576             break;
577         }
578     }
579     return "unknown";
580 }
581 
GetButtonId() const582 int32_t PointerEvent::GetButtonId() const
583 {
584     return buttonId_;
585 }
586 
SetButtonId(int32_t buttonId)587 void PointerEvent::SetButtonId(int32_t buttonId)
588 {
589     buttonId_ = buttonId;
590 }
591 
GetFingerCount() const592 int32_t PointerEvent::GetFingerCount() const
593 {
594     return fingerCount_;
595 }
596 
SetFingerCount(int32_t fingerCount)597 void PointerEvent::SetFingerCount(int32_t fingerCount)
598 {
599     fingerCount_ = fingerCount;
600 }
601 
GetAxisValue(AxisType axis) const602 double PointerEvent::GetAxisValue(AxisType axis) const
603 {
604     double axisValue {};
605     if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
606         axisValue = axisValues_[axis];
607     }
608     return axisValue;
609 }
610 
SetAxisValue(AxisType axis,double axisValue)611 void PointerEvent::SetAxisValue(AxisType axis, double axisValue)
612 {
613     if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
614         axisValues_[axis] = axisValue;
615         axes_ = static_cast<uint32_t>(axes_ | static_cast<uint32_t>(1 << axis));
616     }
617 }
618 
ClearAxisValue()619 void PointerEvent::ClearAxisValue()
620 {
621     axisValues_ = {};
622     axes_ = 0;
623 }
624 
HasAxis(uint32_t axes,AxisType axis)625 bool PointerEvent::HasAxis(uint32_t axes, AxisType axis)
626 {
627     bool ret { false };
628     if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
629         ret = static_cast<bool>(static_cast<uint32_t>(axes) & (1 << static_cast<uint32_t>(axis)));
630     }
631     return ret;
632 }
633 
SetPressedKeys(const std::vector<int32_t> pressedKeys)634 void PointerEvent::SetPressedKeys(const std::vector<int32_t> pressedKeys)
635 {
636     pressedKeys_ = pressedKeys;
637 }
638 
GetPressedKeys() const639 std::vector<int32_t> PointerEvent::GetPressedKeys() const
640 {
641     return pressedKeys_;
642 }
643 
644 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
SetEnhanceData(const std::vector<uint8_t> enhanceData)645 void PointerEvent::SetEnhanceData(const std::vector<uint8_t> enhanceData)
646 {
647     enhanceData_ = enhanceData;
648 }
649 
GetEnhanceData() const650 std::vector<uint8_t> PointerEvent::GetEnhanceData() const
651 {
652     return enhanceData_;
653 }
654 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
655 
WriteToParcel(Parcel & out) const656 bool PointerEvent::WriteToParcel(Parcel &out) const
657 {
658     if (!InputEvent::WriteToParcel(out)) {
659         return false;
660     }
661 
662     WRITEINT32(out, pointerId_);
663 
664     WRITEINT32(out, static_cast<int32_t>(pointers_.size()));
665 
666     for (const auto &item : pointers_) {
667         if (!item.WriteToParcel(out)) {
668             return false;
669         }
670     }
671 
672     WRITEINT32(out, static_cast<int32_t>(buffer_.size()));
673 
674     for (const auto& buff : buffer_) {
675         WRITEUINT8(out, buff);
676     }
677 
678     WRITEINT32(out, static_cast<int32_t>(pressedButtons_.size()));
679 
680     for (const auto &item : pressedButtons_) {
681         WRITEINT32(out, item);
682     }
683 
684     WRITEINT32(out, sourceType_);
685 
686     WRITEINT32(out, pointerAction_);
687 
688     WRITEINT32(out, buttonId_);
689 
690     WRITEINT32(out, fingerCount_);
691 
692     const uint32_t axes { GetAxes() };
693     WRITEUINT32(out, axes);
694 
695     for (int32_t i = AXIS_TYPE_UNKNOWN; i < AXIS_TYPE_MAX; ++i) {
696         const AxisType axis { static_cast<AxisType>(i) };
697         if (HasAxis(axes, axis)) {
698             WRITEDOUBLE(out, GetAxisValue(axis));
699         }
700     }
701 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
702     WRITEINT32(out, static_cast<int32_t>(enhanceData_.size()));
703     for (uint32_t i = 0; i < enhanceData_.size(); i++) {
704         WRITEUINT32(out, enhanceData_[i]);
705     }
706 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
707     return true;
708 }
709 
ReadFromParcel(Parcel & in)710 bool PointerEvent::ReadFromParcel(Parcel &in)
711 {
712     if (!InputEvent::ReadFromParcel(in)) {
713         return false;
714     }
715 
716     READINT32(in, pointerId_);
717 
718     int32_t nPointers;
719     READINT32(in, nPointers);
720     if (nPointers > static_cast<int32_t>(MAX_N_POINTER_ITEMS)) {
721         return false;
722     }
723 
724     for (int32_t i = 0; i < nPointers; ++i) {
725         PointerItem item;
726         if (!item.ReadFromParcel(in)) {
727             return false;
728         }
729         AddPointerItem(item);
730     }
731 
732     if (!ReadBufferFromParcel(in)) {
733         return false;
734     }
735 
736     int32_t nPressedButtons;
737     READINT32(in, nPressedButtons);
738     if (nPressedButtons > static_cast<int32_t>(MAX_N_PRESSED_BUTTONS)) {
739         return false;
740     }
741 
742     for (int32_t i = 0; i < nPressedButtons; ++i) {
743         int32_t buttonId;
744         READINT32(in, buttonId);
745         SetButtonPressed(buttonId);
746     }
747 
748     READINT32(in, sourceType_);
749     READINT32(in, pointerAction_);
750     READINT32(in, buttonId_);
751     READINT32(in, fingerCount_);
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 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
764     if (!ReadEnhanceDataFromParcel(in)) {
765         return false;
766     }
767 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
768     return true;
769 }
770 
771 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
ReadEnhanceDataFromParcel(Parcel & in)772 bool PointerEvent::ReadEnhanceDataFromParcel(Parcel &in)
773 {
774     int32_t size = 0;
775     READINT32(in, size);
776     if (size > static_cast<int32_t>(MAX_N_ENHANCE_DATA_SIZE) || size < 0) {
777         MMI_HILOGE("enhanceData_ size is invalid");
778         return false;
779     }
780 
781     for (int32_t i = 0; i < size; i++) {
782         uint32_t val;
783         READUINT32(in, val);
784         enhanceData_.emplace_back(val);
785     }
786     return true;
787 }
788 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
789 
ReadBufferFromParcel(Parcel & in)790 bool PointerEvent::ReadBufferFromParcel(Parcel &in)
791 {
792     int32_t bufflen;
793     READINT32(in, bufflen);
794     if (bufflen > static_cast<int32_t>(MAX_N_BUFFER_SIZE)) {
795         return false;
796     }
797 
798     for (int32_t i = 0; i < bufflen; ++i) {
799         uint8_t data;
800         READUINT8(in, data);
801         buffer_.push_back(data);
802     }
803     return true;
804 }
805 
IsValidCheckMouseFunc() const806 bool PointerEvent::IsValidCheckMouseFunc() const
807 {
808     CALL_DEBUG_ENTER;
809     if (pointers_.size() != 1) {
810         MMI_HILOGE("Pointers_ is invalid");
811         return false;
812     }
813 
814     size_t maxPressedButtons = 3;
815     if (pressedButtons_.size() > maxPressedButtons) {
816         MMI_HILOGE("PressedButtons_.size is greater than three and is invalid");
817         return false;
818     }
819 
820     for (const auto &item : pressedButtons_) {
821         if (item != MOUSE_BUTTON_LEFT && item != MOUSE_BUTTON_RIGHT && item != MOUSE_BUTTON_MIDDLE) {
822             MMI_HILOGE("PressedButtons_ is invalid");
823             return false;
824         }
825     }
826 
827     int32_t pointAction = GetPointerAction();
828     bool checkFlag = pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
829         pointAction != POINTER_ACTION_AXIS_BEGIN && pointAction != POINTER_ACTION_AXIS_UPDATE &&
830         pointAction != POINTER_ACTION_AXIS_END && pointAction != POINTER_ACTION_BUTTON_DOWN &&
831         pointAction != POINTER_ACTION_BUTTON_UP;
832     if (checkFlag) {
833         MMI_HILOGE("PointAction is invalid");
834         return false;
835     }
836 
837     int32_t buttonId = GetButtonId();
838     if (pointAction == POINTER_ACTION_BUTTON_DOWN || pointAction == POINTER_ACTION_BUTTON_UP) {
839         if (buttonId != MOUSE_BUTTON_LEFT && buttonId != MOUSE_BUTTON_RIGHT && buttonId != MOUSE_BUTTON_MIDDLE) {
840             MMI_HILOGE("ButtonId is invalid");
841             return false;
842         }
843     } else {
844         if (buttonId != BUTTON_NONE) {
845             MMI_HILOGE("ButtonId is not BUTTON_NONE and is invalid");
846             return false;
847         }
848     }
849     return true;
850 }
851 
IsValidCheckMouse() const852 bool PointerEvent::IsValidCheckMouse() const
853 {
854     CALL_DEBUG_ENTER;
855     int32_t mousePointID = GetPointerId();
856     if (mousePointID < 0) {
857         MMI_HILOGE("MousePointID is invalid");
858         return false;
859     }
860 
861     if (!IsValidCheckMouseFunc()) {
862         MMI_HILOGE("IsValidCheckMouseFunc is invalid");
863         return false;
864     }
865 
866     for (const auto &item : pointers_) {
867         if (item.GetPointerId() < 0) {
868             MMI_HILOGE("Item.pointerid is invalid");
869             return false;
870         }
871 
872         if (item.GetPointerId() != mousePointID) {
873             MMI_HILOGE("Item.pointerid is not same to mousePointID and is invalid");
874             return false;
875         }
876 
877         if (item.GetDownTime() > 0) {
878             MMI_HILOGE("Item.downtime is invalid");
879             return false;
880         }
881 
882         if (item.IsPressed() != false) {
883             MMI_HILOGE("Item.ispressed is not false and is invalid");
884             return false;
885         }
886     }
887     return true;
888 }
889 
IsValidCheckTouchFunc() const890 bool PointerEvent::IsValidCheckTouchFunc() const
891 {
892     CALL_DEBUG_ENTER;
893     int32_t touchPointID = GetPointerId();
894     if (touchPointID < 0) {
895         MMI_HILOGE("TouchPointID is invalid");
896         return false;
897     }
898 
899     if (!pressedButtons_.empty()) {
900         MMI_HILOGE("PressedButtons_.size is invalid");
901         return false;
902     }
903 
904     int32_t pointAction = GetPointerAction();
905     if (pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
906         pointAction != POINTER_ACTION_DOWN && pointAction != POINTER_ACTION_UP) {
907         MMI_HILOGE("PointAction is invalid");
908         return false;
909     }
910 
911     if (GetButtonId() != BUTTON_NONE) {
912         MMI_HILOGE("ButtonId is invalid");
913         return false;
914     }
915     return true;
916 }
917 
IsValidCheckTouch() const918 bool PointerEvent::IsValidCheckTouch() const
919 {
920     CALL_DEBUG_ENTER;
921     if (!IsValidCheckTouchFunc()) {
922         MMI_HILOGE("IsValidCheckTouchFunc is invalid");
923         return false;
924     }
925     bool isSameItem = false;
926     int32_t touchPointID = GetPointerId();
927     for (auto item = pointers_.begin(); item != pointers_.end(); item++) {
928         if (item->GetPointerId() < 0) {
929             MMI_HILOGE("Item.pointerid is invalid");
930             return false;
931         }
932 
933         if (item->GetPointerId() == touchPointID) {
934             isSameItem = true;
935         }
936 
937         if (item->GetDownTime() <= 0) {
938             MMI_HILOGE("Item.downtime is invalid");
939             return false;
940         }
941 
942         if (item->IsPressed() != false) {
943             MMI_HILOGE("Item.ispressed is not false and is invalid");
944             return false;
945         }
946 
947         auto itemtmp = item;
948         for (++itemtmp; itemtmp != pointers_.end(); itemtmp++) {
949             if (item->GetPointerId() == itemtmp->GetPointerId()) {
950                 MMI_HILOGE("Pointitems pointerid exist same items and is invalid");
951                 return false;
952             }
953         }
954     }
955 
956     if (!isSameItem) {
957         MMI_HILOGE("Item.pointerid is not same to touchPointID and is invalid");
958         return false;
959     }
960     return true;
961 }
962 
IsValid() const963 bool PointerEvent::IsValid() const
964 {
965     CALL_DEBUG_ENTER;
966     switch (GetSourceType()) {
967         case SOURCE_TYPE_MOUSE: {
968             if (!IsValidCheckMouse()) {
969                 MMI_HILOGE("IsValidCheckMouse is invalid");
970                 return false;
971             }
972             break;
973         }
974         case SOURCE_TYPE_TOUCHSCREEN:
975         case SOURCE_TYPE_TOUCHPAD: {
976             if (!IsValidCheckTouch()) {
977                 MMI_HILOGE("IsValidCheckTouch is invalid");
978                 return false;
979             }
980             break;
981         }
982         case SOURCE_TYPE_JOYSTICK:
983             break;
984         default: {
985             MMI_HILOGE("SourceType is invalid");
986             return false;
987         }
988     }
989     return true;
990 }
991 
SetBuffer(std::vector<uint8_t> buffer)992 void PointerEvent::SetBuffer(std::vector<uint8_t> buffer)
993 {
994     buffer_ = buffer;
995 }
996 
ClearBuffer()997 void PointerEvent::ClearBuffer()
998 {
999     buffer_.clear();
1000 }
1001 
GetBuffer() const1002 std::vector<uint8_t> PointerEvent::GetBuffer() const
1003 {
1004     return buffer_;
1005 }
1006 } // namespace MMI
1007 } // namespace OHOS
1008