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
750 READINT32(in, pointerAction_);
751
752 READINT32(in, buttonId_);
753
754 READINT32(in, fingerCount_);
755
756 uint32_t axes;
757 READUINT32(in, axes);
758
759 for (int32_t i = AXIS_TYPE_UNKNOWN; i < AXIS_TYPE_MAX; ++i) {
760 const AxisType axis { static_cast<AxisType>(i) };
761 if (HasAxis(axes, axis)) {
762 double val;
763 READDOUBLE(in, val);
764 SetAxisValue(axis, val);
765 }
766 }
767 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
768 if (!ReadEnhanceDataFromParcel(in)) {
769 return false;
770 }
771 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
772 return true;
773 }
774
775 #ifdef OHOS_BUILD_ENABLE_SECURITY_COMPONENT
ReadEnhanceDataFromParcel(Parcel & in)776 bool PointerEvent::ReadEnhanceDataFromParcel(Parcel &in)
777 {
778 int32_t size = 0;
779 READINT32(in, size);
780 if (size > static_cast<int32_t>(MAX_N_ENHANCE_DATA_SIZE) || size < 0) {
781 MMI_HILOGE("enhanceData_ size is invalid");
782 return false;
783 }
784
785 for (int32_t i = 0; i < size; i++) {
786 uint32_t val;
787 READUINT32(in, val);
788 enhanceData_.emplace_back(val);
789 }
790 return true;
791 }
792 #endif // OHOS_BUILD_ENABLE_SECURITY_COMPONENT
793
ReadBufferFromParcel(Parcel & in)794 bool PointerEvent::ReadBufferFromParcel(Parcel &in)
795 {
796 int32_t bufflen;
797 READINT32(in, bufflen);
798 if (bufflen > static_cast<int32_t>(MAX_N_BUFFER_SIZE)) {
799 return false;
800 }
801
802 for (int32_t i = 0; i < bufflen; ++i) {
803 uint8_t data;
804 READUINT8(in, data);
805 buffer_.push_back(data);
806 }
807 return true;
808 }
809
IsValidCheckMouseFunc() const810 bool PointerEvent::IsValidCheckMouseFunc() const
811 {
812 CALL_DEBUG_ENTER;
813 if (pointers_.size() != 1) {
814 MMI_HILOGE("Pointers_ is invalid");
815 return false;
816 }
817
818 size_t maxPressedButtons = 3;
819 if (pressedButtons_.size() > maxPressedButtons) {
820 MMI_HILOGE("PressedButtons_.size is greater than three and is invalid");
821 return false;
822 }
823
824 for (const auto &item : pressedButtons_) {
825 if (item != MOUSE_BUTTON_LEFT && item != MOUSE_BUTTON_RIGHT && item != MOUSE_BUTTON_MIDDLE) {
826 MMI_HILOGE("PressedButtons_ is invalid");
827 return false;
828 }
829 }
830
831 int32_t pointAction = GetPointerAction();
832 bool checkFlag = pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
833 pointAction != POINTER_ACTION_AXIS_BEGIN && pointAction != POINTER_ACTION_AXIS_UPDATE &&
834 pointAction != POINTER_ACTION_AXIS_END && pointAction != POINTER_ACTION_BUTTON_DOWN &&
835 pointAction != POINTER_ACTION_BUTTON_UP;
836 if (checkFlag) {
837 MMI_HILOGE("PointAction is invalid");
838 return false;
839 }
840
841 int32_t buttonId = GetButtonId();
842 if (pointAction == POINTER_ACTION_BUTTON_DOWN || pointAction == POINTER_ACTION_BUTTON_UP) {
843 if (buttonId != MOUSE_BUTTON_LEFT && buttonId != MOUSE_BUTTON_RIGHT && buttonId != MOUSE_BUTTON_MIDDLE) {
844 MMI_HILOGE("ButtonId is invalid");
845 return false;
846 }
847 } else {
848 if (buttonId != BUTTON_NONE) {
849 MMI_HILOGE("ButtonId is not BUTTON_NONE and is invalid");
850 return false;
851 }
852 }
853 return true;
854 }
855
IsValidCheckMouse() const856 bool PointerEvent::IsValidCheckMouse() const
857 {
858 CALL_DEBUG_ENTER;
859 int32_t mousePointID = GetPointerId();
860 if (mousePointID < 0) {
861 MMI_HILOGE("MousePointID is invalid");
862 return false;
863 }
864
865 if (!IsValidCheckMouseFunc()) {
866 MMI_HILOGE("IsValidCheckMouseFunc is invalid");
867 return false;
868 }
869
870 for (const auto &item : pointers_) {
871 if (item.GetPointerId() < 0) {
872 MMI_HILOGE("Item.pointerid is invalid");
873 return false;
874 }
875
876 if (item.GetPointerId() != mousePointID) {
877 MMI_HILOGE("Item.pointerid is not same to mousePointID and is invalid");
878 return false;
879 }
880
881 if (item.GetDownTime() > 0) {
882 MMI_HILOGE("Item.downtime is invalid");
883 return false;
884 }
885
886 if (item.IsPressed() != false) {
887 MMI_HILOGE("Item.ispressed is not false and is invalid");
888 return false;
889 }
890 }
891 return true;
892 }
893
IsValidCheckTouchFunc() const894 bool PointerEvent::IsValidCheckTouchFunc() const
895 {
896 CALL_DEBUG_ENTER;
897 int32_t touchPointID = GetPointerId();
898 if (touchPointID < 0) {
899 MMI_HILOGE("TouchPointID is invalid");
900 return false;
901 }
902
903 if (!pressedButtons_.empty()) {
904 MMI_HILOGE("PressedButtons_.size is invalid");
905 return false;
906 }
907
908 int32_t pointAction = GetPointerAction();
909 if (pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
910 pointAction != POINTER_ACTION_DOWN && pointAction != POINTER_ACTION_UP) {
911 MMI_HILOGE("PointAction is invalid");
912 return false;
913 }
914
915 if (GetButtonId() != BUTTON_NONE) {
916 MMI_HILOGE("ButtonId is invalid");
917 return false;
918 }
919 return true;
920 }
921
IsValidCheckTouch() const922 bool PointerEvent::IsValidCheckTouch() const
923 {
924 CALL_DEBUG_ENTER;
925 if (!IsValidCheckTouchFunc()) {
926 MMI_HILOGE("IsValidCheckTouchFunc is invalid");
927 return false;
928 }
929 bool isSameItem = false;
930 int32_t touchPointID = GetPointerId();
931 for (auto item = pointers_.begin(); item != pointers_.end(); item++) {
932 if (item->GetPointerId() < 0) {
933 MMI_HILOGE("Item.pointerid is invalid");
934 return false;
935 }
936
937 if (item->GetPointerId() == touchPointID) {
938 isSameItem = true;
939 }
940
941 if (item->GetDownTime() <= 0) {
942 MMI_HILOGE("Item.downtime is invalid");
943 return false;
944 }
945
946 if (item->IsPressed() != false) {
947 MMI_HILOGE("Item.ispressed is not false and is invalid");
948 return false;
949 }
950
951 auto itemtmp = item;
952 for (++itemtmp; itemtmp != pointers_.end(); itemtmp++) {
953 if (item->GetPointerId() == itemtmp->GetPointerId()) {
954 MMI_HILOGE("Pointitems pointerid exist same items and is invalid");
955 return false;
956 }
957 }
958 }
959
960 if (!isSameItem) {
961 MMI_HILOGE("Item.pointerid is not same to touchPointID and is invalid");
962 return false;
963 }
964 return true;
965 }
966
IsValid() const967 bool PointerEvent::IsValid() const
968 {
969 CALL_DEBUG_ENTER;
970 switch (GetSourceType()) {
971 case SOURCE_TYPE_MOUSE: {
972 if (!IsValidCheckMouse()) {
973 MMI_HILOGE("IsValidCheckMouse is invalid");
974 return false;
975 }
976 break;
977 }
978 case SOURCE_TYPE_TOUCHSCREEN:
979 case SOURCE_TYPE_TOUCHPAD: {
980 if (!IsValidCheckTouch()) {
981 MMI_HILOGE("IsValidCheckTouch is invalid");
982 return false;
983 }
984 break;
985 }
986 case SOURCE_TYPE_JOYSTICK:
987 break;
988 default: {
989 MMI_HILOGE("SourceType is invalid");
990 return false;
991 }
992 }
993 return true;
994 }
995
SetBuffer(std::vector<uint8_t> buffer)996 void PointerEvent::SetBuffer(std::vector<uint8_t> buffer)
997 {
998 buffer_ = buffer;
999 }
1000
ClearBuffer()1001 void PointerEvent::ClearBuffer()
1002 {
1003 buffer_.clear();
1004 }
1005
GetBuffer() const1006 std::vector<uint8_t> PointerEvent::GetBuffer() const
1007 {
1008 return buffer_;
1009 }
1010 } // namespace MMI
1011 } // namespace OHOS
1012