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 { 5 };
30 } // namespace
31
from(std::shared_ptr<InputEvent> inputEvent)32 std::shared_ptr<PointerEvent> PointerEvent::from(std::shared_ptr<InputEvent> inputEvent)
33 {
34 return nullptr;
35 }
36
PointerItem()37 PointerEvent::PointerItem::PointerItem() {}
38
~PointerItem()39 PointerEvent::PointerItem::~PointerItem() {}
40
GetPointerId() const41 int32_t PointerEvent::PointerItem::GetPointerId() const
42 {
43 return pointerId_;
44 }
45
SetPointerId(int32_t pointerId)46 void PointerEvent::PointerItem::SetPointerId(int32_t pointerId)
47 {
48 pointerId_ = pointerId;
49 }
50
GetDownTime() const51 int64_t PointerEvent::PointerItem::GetDownTime() const
52 {
53 return downTime_;
54 }
55
SetDownTime(int64_t downTime)56 void PointerEvent::PointerItem::SetDownTime(int64_t downTime)
57 {
58 downTime_ = downTime;
59 }
60
IsPressed() const61 bool PointerEvent::PointerItem::IsPressed() const
62 {
63 return pressed_;
64 }
65
SetPressed(bool pressed)66 void PointerEvent::PointerItem::SetPressed(bool pressed)
67 {
68 pressed_ = pressed;
69 }
70
GetDisplayX() const71 int32_t PointerEvent::PointerItem::GetDisplayX() const
72 {
73 return displayX_;
74 }
75
SetDisplayX(int32_t x)76 void PointerEvent::PointerItem::SetDisplayX(int32_t x)
77 {
78 displayX_ = x;
79 }
80
GetDisplayY() const81 int32_t PointerEvent::PointerItem::GetDisplayY() const
82 {
83 return displayY_;
84 }
85
SetDisplayY(int32_t y)86 void PointerEvent::PointerItem::SetDisplayY(int32_t y)
87 {
88 displayY_ = y;
89 }
90
GetWindowX() const91 int32_t PointerEvent::PointerItem::GetWindowX() const
92 {
93 return windowX_;
94 }
95
SetWindowX(int32_t x)96 void PointerEvent::PointerItem::SetWindowX(int32_t x)
97 {
98 windowX_ = x;
99 }
100
GetWindowY() const101 int32_t PointerEvent::PointerItem::GetWindowY() const
102 {
103 return windowY_;
104 }
105
SetWindowY(int32_t y)106 void PointerEvent::PointerItem::SetWindowY(int32_t y)
107 {
108 windowY_ = y;
109 }
110
GetWidth() const111 int32_t PointerEvent::PointerItem::GetWidth() const
112 {
113 return width_;
114 }
115
SetWidth(int32_t width)116 void PointerEvent::PointerItem::SetWidth(int32_t width)
117 {
118 width_ = width;
119 }
120
GetHeight() const121 int32_t PointerEvent::PointerItem::GetHeight() const
122 {
123 return height_;
124 }
125
SetHeight(int32_t height)126 void PointerEvent::PointerItem::SetHeight(int32_t height)
127 {
128 height_ = height;
129 }
130
GetTiltX() const131 double PointerEvent::PointerItem::GetTiltX() const
132 {
133 return tiltX_;
134 }
135
SetTiltX(double tiltX)136 void PointerEvent::PointerItem::SetTiltX(double tiltX)
137 {
138 tiltX_ = tiltX;
139 }
140
GetTiltY() const141 double PointerEvent::PointerItem::GetTiltY() const
142 {
143 return tiltY_;
144 }
145
SetTiltY(double tiltY)146 void PointerEvent::PointerItem::SetTiltY(double tiltY)
147 {
148 tiltY_ = tiltY;
149 }
150
GetToolDisplayX() const151 int32_t PointerEvent::PointerItem::GetToolDisplayX() const
152 {
153 return toolDisplayX_;
154 }
155
SetToolDisplayX(int32_t x)156 void PointerEvent::PointerItem::SetToolDisplayX(int32_t x)
157 {
158 toolDisplayX_ = x;
159 }
160
GetToolDisplayY() const161 int32_t PointerEvent::PointerItem::GetToolDisplayY() const
162 {
163 return toolDisplayY_;
164 }
165
SetToolDisplayY(int32_t y)166 void PointerEvent::PointerItem::SetToolDisplayY(int32_t y)
167 {
168 toolDisplayY_ = y;
169 }
170
GetToolWindowX() const171 int32_t PointerEvent::PointerItem::GetToolWindowX() const
172 {
173 return toolWindowX_;
174 }
175
SetToolWindowX(int32_t x)176 void PointerEvent::PointerItem::SetToolWindowX(int32_t x)
177 {
178 toolWindowX_ = x;
179 }
180
GetToolWindowY() const181 int32_t PointerEvent::PointerItem::GetToolWindowY() const
182 {
183 return toolWindowY_;
184 }
185
SetToolWindowY(int32_t y)186 void PointerEvent::PointerItem::SetToolWindowY(int32_t y)
187 {
188 toolWindowY_ = y;
189 }
190
GetToolWidth() const191 int32_t PointerEvent::PointerItem::GetToolWidth() const
192 {
193 return toolWidth_;
194 }
195
SetToolWidth(int32_t width)196 void PointerEvent::PointerItem::SetToolWidth(int32_t width)
197 {
198 toolWidth_ = width;
199 }
200
GetToolHeight() const201 int32_t PointerEvent::PointerItem::GetToolHeight() const
202 {
203 return toolHeight_;
204 }
205
SetToolHeight(int32_t height)206 void PointerEvent::PointerItem::SetToolHeight(int32_t height)
207 {
208 toolHeight_ = height;
209 }
210
GetPressure() const211 double PointerEvent::PointerItem::GetPressure() const
212 {
213 return pressure_;
214 }
215
SetPressure(double pressure)216 void PointerEvent::PointerItem::SetPressure(double pressure)
217 {
218 pressure_ = pressure >= MAX_PRESSURE ? MAX_PRESSURE : pressure;
219 }
220
GetLongAxis() const221 int32_t PointerEvent::PointerItem::GetLongAxis() const
222 {
223 return longAxis_;
224 }
225
SetLongAxis(int32_t longAxis)226 void PointerEvent::PointerItem::SetLongAxis(int32_t longAxis)
227 {
228 longAxis_ = longAxis;
229 }
230
GetShortAxis() const231 int32_t PointerEvent::PointerItem::GetShortAxis() const
232 {
233 return shortAxis_;
234 }
235
SetShortAxis(int32_t shortAxis)236 void PointerEvent::PointerItem::SetShortAxis(int32_t shortAxis)
237 {
238 shortAxis_ = shortAxis;
239 }
240
GetDeviceId() const241 int32_t PointerEvent::PointerItem::GetDeviceId() const
242 {
243 return deviceId_;
244 }
245
SetDeviceId(int32_t deviceId)246 void PointerEvent::PointerItem::SetDeviceId(int32_t deviceId)
247 {
248 deviceId_ = deviceId;
249 }
250
GetToolType() const251 int32_t PointerEvent::PointerItem::GetToolType() const
252 {
253 return toolType_;
254 }
255
SetToolType(int32_t toolType)256 void PointerEvent::PointerItem::SetToolType(int32_t toolType)
257 {
258 toolType_ = toolType;
259 }
260
GetTargetWindowId() const261 int32_t PointerEvent::PointerItem::GetTargetWindowId() const
262 {
263 return targetWindowId_;
264 }
265
SetTargetWindowId(int32_t windowId)266 void PointerEvent::PointerItem::SetTargetWindowId(int32_t windowId)
267 {
268 targetWindowId_ = windowId;
269 }
270
GetRawDx() const271 int32_t PointerEvent::PointerItem::GetRawDx() const
272 {
273 return rawDx_;
274 }
275
SetRawDx(int32_t rawDx)276 void PointerEvent::PointerItem::SetRawDx(int32_t rawDx)
277 {
278 rawDx_ = rawDx;
279 }
280
GetRawDy() const281 int32_t PointerEvent::PointerItem::GetRawDy() const
282 {
283 return rawDy_;
284 }
285
SetRawDy(int32_t rawDy)286 void PointerEvent::PointerItem::SetRawDy(int32_t rawDy)
287 {
288 rawDy_ = rawDy;
289 }
290
WriteToParcel(Parcel & out) const291 bool PointerEvent::PointerItem::WriteToParcel(Parcel &out) const
292 {
293 return (
294 out.WriteInt32(pointerId_) &&
295 out.WriteInt64(downTime_) &&
296 out.WriteBool(pressed_) &&
297 out.WriteInt32(displayX_) &&
298 out.WriteInt32(displayY_) &&
299 out.WriteInt32(windowX_) &&
300 out.WriteInt32(windowY_) &&
301 out.WriteInt32(width_) &&
302 out.WriteInt32(height_) &&
303 out.WriteInt32(toolDisplayX_) &&
304 out.WriteInt32(toolDisplayY_) &&
305 out.WriteInt32(toolWindowX_) &&
306 out.WriteInt32(toolWindowY_) &&
307 out.WriteInt32(toolWidth_) &&
308 out.WriteInt32(toolHeight_) &&
309 out.WriteDouble(tiltX_) &&
310 out.WriteDouble(tiltY_) &&
311 out.WriteDouble(pressure_) &&
312 out.WriteInt32(longAxis_) &&
313 out.WriteInt32(shortAxis_) &&
314 out.WriteInt32(toolType_) &&
315 out.WriteInt32(deviceId_) &&
316 out.WriteInt32(rawDx_) &&
317 out.WriteInt32(rawDy_)
318 );
319 }
320
ReadFromParcel(Parcel & in)321 bool PointerEvent::PointerItem::ReadFromParcel(Parcel &in)
322 {
323 return (
324 in.ReadInt32(pointerId_) &&
325 in.ReadInt64(downTime_) &&
326 in.ReadBool(pressed_) &&
327 in.ReadInt32(displayX_) &&
328 in.ReadInt32(displayY_) &&
329 in.ReadInt32(windowX_) &&
330 in.ReadInt32(windowY_) &&
331 in.ReadInt32(width_) &&
332 in.ReadInt32(height_) &&
333 in.ReadInt32(toolDisplayX_) &&
334 in.ReadInt32(toolDisplayY_) &&
335 in.ReadInt32(toolWindowX_) &&
336 in.ReadInt32(toolWindowY_) &&
337 in.ReadInt32(toolWidth_) &&
338 in.ReadInt32(toolHeight_) &&
339 in.ReadDouble(tiltX_) &&
340 in.ReadDouble(tiltY_) &&
341 in.ReadDouble(pressure_) &&
342 in.ReadInt32(longAxis_) &&
343 in.ReadInt32(shortAxis_) &&
344 in.ReadInt32(toolType_) &&
345 in.ReadInt32(deviceId_) &&
346 in.ReadInt32(rawDx_) &&
347 in.ReadInt32(rawDy_)
348 );
349 }
350
PointerEvent(int32_t eventType)351 PointerEvent::PointerEvent(int32_t eventType) : InputEvent(eventType) {}
352
PointerEvent(const PointerEvent & other)353 PointerEvent::PointerEvent(const PointerEvent& other)
354 : InputEvent(other), pointerId_(other.pointerId_), pointers_(other.pointers_),
355 pressedButtons_(other.pressedButtons_), sourceType_(other.sourceType_),
356 pointerAction_(other.pointerAction_), buttonId_(other.buttonId_),
357 axes_(other.axes_), axisValues_(other.axisValues_),
358 pressedKeys_(other.pressedKeys_) {}
359
~PointerEvent()360 PointerEvent::~PointerEvent() {}
361
Create()362 std::shared_ptr<PointerEvent> PointerEvent::Create()
363 {
364 auto event = std::shared_ptr<PointerEvent>(new (std::nothrow) PointerEvent(InputEvent::EVENT_TYPE_POINTER));
365 CHKPP(event);
366 return event;
367 }
368
Reset()369 void PointerEvent::Reset()
370 {
371 InputEvent::Reset();
372 pointerId_ = -1;
373 pointers_.clear();
374 pressedButtons_.clear();
375 sourceType_ = SOURCE_TYPE_UNKNOWN;
376 pointerAction_ = POINTER_ACTION_UNKNOWN;
377 buttonId_ = -1;
378 axes_ = 0U;
379 axisValues_.fill(0.0);
380 pressedKeys_.clear();
381 }
382
GetPointerAction() const383 int32_t PointerEvent::GetPointerAction() const
384 {
385 return pointerAction_;
386 }
387
SetPointerAction(int32_t pointerAction)388 void PointerEvent::SetPointerAction(int32_t pointerAction)
389 {
390 pointerAction_ = pointerAction;
391 }
392
DumpPointerAction() const393 const char* PointerEvent::DumpPointerAction() const
394 {
395 switch (pointerAction_) {
396 case PointerEvent::POINTER_ACTION_CANCEL: {
397 return "cancel";
398 }
399 case PointerEvent::POINTER_ACTION_DOWN: {
400 return "down";
401 }
402 case PointerEvent::POINTER_ACTION_MOVE: {
403 return "move";
404 }
405 case PointerEvent::POINTER_ACTION_UP: {
406 return "up";
407 }
408 case PointerEvent::POINTER_ACTION_AXIS_BEGIN: {
409 return "axis-begin";
410 }
411 case PointerEvent::POINTER_ACTION_AXIS_UPDATE: {
412 return "axis-update";
413 }
414 case PointerEvent::POINTER_ACTION_AXIS_END: {
415 return "axis-end";
416 }
417 case PointerEvent::POINTER_ACTION_BUTTON_DOWN: {
418 return "button-down";
419 }
420 case PointerEvent::POINTER_ACTION_BUTTON_UP: {
421 return "button-up";
422 }
423 case PointerEvent::POINTER_ACTION_ENTER_WINDOW: {
424 return "enter-window";
425 }
426 case PointerEvent::POINTER_ACTION_LEAVE_WINDOW: {
427 return "leave-window";
428 }
429 default: {
430 break;
431 }
432 }
433 return "unknown";
434 }
435
GetPointerId() const436 int32_t PointerEvent::GetPointerId() const
437 {
438 return pointerId_;
439 }
440
SetPointerId(int32_t pointerId)441 void PointerEvent::SetPointerId(int32_t pointerId)
442 {
443 pointerId_ = pointerId;
444 }
445
GetPointerItem(int32_t pointerId,PointerItem & pointerItem)446 bool PointerEvent::GetPointerItem(int32_t pointerId, PointerItem &pointerItem)
447 {
448 for (const auto &item : pointers_) {
449 if (item.GetPointerId() == pointerId) {
450 pointerItem = item;
451 return true;
452 }
453 }
454 return false;
455 }
456
RemovePointerItem(int32_t pointerId)457 void PointerEvent::RemovePointerItem(int32_t pointerId)
458 {
459 for (auto it = pointers_.begin(); it != pointers_.end(); ++it) {
460 if (it->GetPointerId() == pointerId) {
461 pointers_.erase(it);
462 break;
463 }
464 }
465 }
466
AddPointerItem(PointerItem & pointerItem)467 void PointerEvent::AddPointerItem(PointerItem &pointerItem)
468 {
469 if (pointers_.size() >= MAX_N_POINTER_ITEMS) {
470 MMI_HILOGE("Exceed maximum allowed number of pointer items");
471 return;
472 }
473 pointers_.push_back(pointerItem);
474 }
475
UpdatePointerItem(int32_t pointerId,PointerItem & pointerItem)476 void PointerEvent::UpdatePointerItem(int32_t pointerId, PointerItem &pointerItem)
477 {
478 for (auto &item : pointers_) {
479 if (item.GetPointerId() == pointerId) {
480 item = pointerItem;
481 return;
482 }
483 }
484 AddPointerItem(pointerItem);
485 }
486
GetPressedButtons() const487 std::set<int32_t> PointerEvent::GetPressedButtons() const
488 {
489 return pressedButtons_;
490 }
491
IsButtonPressed(int32_t buttonId) const492 bool PointerEvent::IsButtonPressed(int32_t buttonId) const
493 {
494 return (pressedButtons_.find(buttonId) != pressedButtons_.end());
495 }
496
SetButtonPressed(int32_t buttonId)497 void PointerEvent::SetButtonPressed(int32_t buttonId)
498 {
499 if (pressedButtons_.size() >= MAX_N_PRESSED_BUTTONS) {
500 MMI_HILOGE("Exceed maximum allowed number of pressed buttons");
501 return;
502 }
503 auto iter = pressedButtons_.insert(buttonId);
504 if (!iter.second) {
505 MMI_HILOGE("Insert value failed, button:%{public}d", buttonId);
506 }
507 }
508
DeleteReleaseButton(int32_t buttonId)509 void PointerEvent::DeleteReleaseButton(int32_t buttonId)
510 {
511 if (pressedButtons_.find(buttonId) != pressedButtons_.end()) {
512 pressedButtons_.erase(buttonId);
513 }
514 }
515
ClearButtonPressed()516 void PointerEvent::ClearButtonPressed()
517 {
518 pressedButtons_.clear();
519 }
520
GetPointerIds() const521 std::vector<int32_t> PointerEvent::GetPointerIds() const
522 {
523 std::vector<int32_t> pointerIdList;
524 for (const auto &item : pointers_) {
525 pointerIdList.push_back(item.GetPointerId());
526 }
527 return pointerIdList;
528 }
529
GetSourceType() const530 int32_t PointerEvent::GetSourceType() const
531 {
532 return sourceType_;
533 }
534
SetSourceType(int32_t sourceType)535 void PointerEvent::SetSourceType(int32_t sourceType)
536 {
537 sourceType_ = sourceType;
538 }
539
DumpSourceType() const540 const char* PointerEvent::DumpSourceType() const
541 {
542 switch (sourceType_) {
543 case PointerEvent::SOURCE_TYPE_MOUSE: {
544 return "mouse";
545 }
546 case PointerEvent::SOURCE_TYPE_TOUCHSCREEN: {
547 return "touch-screen";
548 }
549 case PointerEvent::SOURCE_TYPE_TOUCHPAD: {
550 return "touch-pad";
551 }
552 case PointerEvent::SOURCE_TYPE_JOYSTICK: {
553 return "joystick";
554 }
555 default: {
556 break;
557 }
558 }
559 return "unknown";
560 }
561
GetButtonId() const562 int32_t PointerEvent::GetButtonId() const
563 {
564 return buttonId_;
565 }
566
SetButtonId(int32_t buttonId)567 void PointerEvent::SetButtonId(int32_t buttonId)
568 {
569 buttonId_ = buttonId;
570 }
571
GetAxisValue(AxisType axis) const572 double PointerEvent::GetAxisValue(AxisType axis) const
573 {
574 double axisValue {};
575 if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
576 axisValue = axisValues_[axis];
577 }
578 return axisValue;
579 }
580
SetAxisValue(AxisType axis,double axisValue)581 void PointerEvent::SetAxisValue(AxisType axis, double axisValue)
582 {
583 if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
584 axisValues_[axis] = axisValue;
585 axes_ = static_cast<uint32_t>(axes_ | static_cast<uint32_t>(1 << axis));
586 }
587 }
588
ClearAxisValue()589 void PointerEvent::ClearAxisValue()
590 {
591 axisValues_ = {};
592 axes_ = 0;
593 }
594
HasAxis(uint32_t axes,AxisType axis)595 bool PointerEvent::HasAxis(uint32_t axes, AxisType axis)
596 {
597 bool ret { false };
598 if ((axis >= AXIS_TYPE_UNKNOWN) && (axis < AXIS_TYPE_MAX)) {
599 ret = static_cast<bool>(static_cast<uint32_t>(axes) & (1 << static_cast<uint32_t>(axis)));
600 }
601 return ret;
602 }
603
SetPressedKeys(const std::vector<int32_t> pressedKeys)604 void PointerEvent::SetPressedKeys(const std::vector<int32_t> pressedKeys)
605 {
606 pressedKeys_ = pressedKeys;
607 }
608
GetPressedKeys() const609 std::vector<int32_t> PointerEvent::GetPressedKeys() const
610 {
611 return pressedKeys_;
612 }
613
WriteToParcel(Parcel & out) const614 bool PointerEvent::WriteToParcel(Parcel &out) const
615 {
616 if (!InputEvent::WriteToParcel(out)) {
617 return false;
618 }
619
620 WRITEINT32(out, pointerId_);
621
622 WRITEINT32(out, static_cast<int32_t>(pointers_.size()));
623
624 for (const auto &item : pointers_) {
625 if (!item.WriteToParcel(out)) {
626 return false;
627 }
628 }
629
630 WRITEINT32(out, static_cast<int32_t>(pressedButtons_.size()));
631
632 for (const auto &item : pressedButtons_) {
633 WRITEINT32(out, item);
634 }
635
636 WRITEINT32(out, sourceType_);
637
638 WRITEINT32(out, pointerAction_);
639
640 WRITEINT32(out, buttonId_);
641
642 const uint32_t axes { GetAxes() };
643 WRITEUINT32(out, axes);
644
645 for (int32_t i = AXIS_TYPE_UNKNOWN; i < AXIS_TYPE_MAX; ++i) {
646 const AxisType axis { static_cast<AxisType>(i) };
647 if (HasAxis(axes, axis)) {
648 WRITEDOUBLE(out, GetAxisValue(axis));
649 }
650 }
651
652 return true;
653 }
654
ReadFromParcel(Parcel & in)655 bool PointerEvent::ReadFromParcel(Parcel &in)
656 {
657 if (!InputEvent::ReadFromParcel(in)) {
658 return false;
659 }
660
661 READINT32(in, pointerId_);
662
663 int32_t nPointers;
664 READINT32(in, nPointers);
665 if (nPointers > static_cast<int32_t>(MAX_N_POINTER_ITEMS)) {
666 return false;
667 }
668
669 for (int32_t i = 0; i < nPointers; ++i) {
670 PointerItem item;
671 if (!item.ReadFromParcel(in)) {
672 return false;
673 }
674 AddPointerItem(item);
675 }
676
677 int32_t nPressedButtons;
678 READINT32(in, nPressedButtons);
679 if (nPressedButtons > static_cast<int32_t>(MAX_N_PRESSED_BUTTONS)) {
680 return false;
681 }
682
683 for (int32_t i = 0; i < nPressedButtons; ++i) {
684 int32_t buttonId;
685 READINT32(in, buttonId);
686 SetButtonPressed(buttonId);
687 }
688
689 READINT32(in, sourceType_);
690
691 READINT32(in, pointerAction_);
692
693 READINT32(in, buttonId_);
694
695 uint32_t axes;
696 READUINT32(in, axes);
697
698 for (int32_t i = AXIS_TYPE_UNKNOWN; i < AXIS_TYPE_MAX; ++i) {
699 const AxisType axis { static_cast<AxisType>(i) };
700 if (HasAxis(axes, axis)) {
701 double val;
702 READDOUBLE(in, val);
703 SetAxisValue(axis, val);
704 }
705 }
706
707 return true;
708 }
709
IsValidCheckMouseFunc() const710 bool PointerEvent::IsValidCheckMouseFunc() const
711 {
712 CALL_DEBUG_ENTER;
713 if (pointers_.size() != 1) {
714 MMI_HILOGE("Pointers_ is invalid");
715 return false;
716 }
717
718 size_t maxPressedButtons = 3;
719 if (pressedButtons_.size() > maxPressedButtons) {
720 MMI_HILOGE("PressedButtons_.size is greater than three and is invalid");
721 return false;
722 }
723
724 for (const auto &item : pressedButtons_) {
725 if (item != MOUSE_BUTTON_LEFT && item != MOUSE_BUTTON_RIGHT && item != MOUSE_BUTTON_MIDDLE) {
726 MMI_HILOGE("PressedButtons_ is invalid");
727 return false;
728 }
729 }
730
731 int32_t pointAction = GetPointerAction();
732 if (pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
733 pointAction != POINTER_ACTION_AXIS_BEGIN && pointAction != POINTER_ACTION_AXIS_UPDATE &&
734 pointAction != POINTER_ACTION_AXIS_END && pointAction != POINTER_ACTION_BUTTON_DOWN &&
735 pointAction != POINTER_ACTION_BUTTON_UP) {
736 MMI_HILOGE("PointAction is invalid");
737 return false;
738 }
739
740 int32_t buttonId = GetButtonId();
741 if (pointAction == POINTER_ACTION_BUTTON_DOWN || pointAction == POINTER_ACTION_BUTTON_UP) {
742 if (buttonId != MOUSE_BUTTON_LEFT && buttonId != MOUSE_BUTTON_RIGHT && buttonId != MOUSE_BUTTON_MIDDLE) {
743 MMI_HILOGE("ButtonId is invalid");
744 return false;
745 }
746 } else {
747 if (buttonId != BUTTON_NONE) {
748 MMI_HILOGE("ButtonId is not BUTTON_NONE and is invalid");
749 return false;
750 }
751 }
752 return true;
753 }
754
IsValidCheckMouse() const755 bool PointerEvent::IsValidCheckMouse() const
756 {
757 CALL_DEBUG_ENTER;
758 int32_t mousePointID = GetPointerId();
759 if (mousePointID < 0) {
760 MMI_HILOGE("MousePointID is invalid");
761 return false;
762 }
763
764 if (!IsValidCheckMouseFunc()) {
765 MMI_HILOGE("IsValidCheckMouseFunc is invalid");
766 return false;
767 }
768
769 for (const auto &item : pointers_) {
770 if (item.GetPointerId() < 0) {
771 MMI_HILOGE("Item.pointerid is invalid");
772 return false;
773 }
774
775 if (item.GetPointerId() != mousePointID) {
776 MMI_HILOGE("Item.pointerid is not same to mousePointID and is invalid");
777 return false;
778 }
779
780 if (item.GetDownTime() > 0) {
781 MMI_HILOGE("Item.downtime is invalid");
782 return false;
783 }
784
785 if (item.IsPressed() != false) {
786 MMI_HILOGE("Item.ispressed is not false and is invalid");
787 return false;
788 }
789 }
790 return true;
791 }
792
IsValidCheckTouchFunc() const793 bool PointerEvent::IsValidCheckTouchFunc() const
794 {
795 CALL_DEBUG_ENTER;
796 int32_t touchPointID = GetPointerId();
797 if (touchPointID < 0) {
798 MMI_HILOGE("TouchPointID is invalid");
799 return false;
800 }
801
802 if (!pressedButtons_.empty()) {
803 MMI_HILOGE("PressedButtons_.size is invalid");
804 return false;
805 }
806
807 int32_t pointAction = GetPointerAction();
808 if (pointAction != POINTER_ACTION_CANCEL && pointAction != POINTER_ACTION_MOVE &&
809 pointAction != POINTER_ACTION_DOWN && pointAction != POINTER_ACTION_UP) {
810 MMI_HILOGE("PointAction is invalid");
811 return false;
812 }
813
814 if (GetButtonId() != BUTTON_NONE) {
815 MMI_HILOGE("ButtonId is invalid");
816 return false;
817 }
818 return true;
819 }
820
IsValidCheckTouch() const821 bool PointerEvent::IsValidCheckTouch() const
822 {
823 CALL_DEBUG_ENTER;
824 if (!IsValidCheckTouchFunc()) {
825 MMI_HILOGE("IsValidCheckTouchFunc is invalid");
826 return false;
827 }
828 bool isSameItem = false;
829 int32_t touchPointID = GetPointerId();
830 for (auto item = pointers_.begin(); item != pointers_.end(); item++) {
831 if (item->GetPointerId() < 0) {
832 MMI_HILOGE("Item.pointerid is invalid");
833 return false;
834 }
835
836 if (item->GetPointerId() == touchPointID) {
837 isSameItem = true;
838 }
839
840 if (item->GetDownTime() <= 0) {
841 MMI_HILOGE("Item.downtime is invalid");
842 return false;
843 }
844
845 if (item->IsPressed() != false) {
846 MMI_HILOGE("Item.ispressed is not false and is invalid");
847 return false;
848 }
849
850 auto itemtmp = item;
851 for (++itemtmp; itemtmp != pointers_.end(); itemtmp++) {
852 if (item->GetPointerId() == itemtmp->GetPointerId()) {
853 MMI_HILOGE("Pointitems pointerid exist same items and is invalid");
854 return false;
855 }
856 }
857 }
858
859 if (!isSameItem) {
860 MMI_HILOGE("Item.pointerid is not same to touchPointID and is invalid");
861 return false;
862 }
863 return true;
864 }
865
IsValid() const866 bool PointerEvent::IsValid() const
867 {
868 CALL_DEBUG_ENTER;
869 int32_t sourceType = GetSourceType();
870 if (sourceType != SOURCE_TYPE_MOUSE && sourceType != SOURCE_TYPE_TOUCHSCREEN &&
871 sourceType != SOURCE_TYPE_TOUCHPAD) {
872 MMI_HILOGE("SourceType is invalid");
873 return false;
874 }
875 switch (sourceType) {
876 case SOURCE_TYPE_MOUSE: {
877 if (!IsValidCheckMouse()) {
878 MMI_HILOGE("IsValidCheckMouse is invalid");
879 return false;
880 }
881 break;
882 }
883 case SOURCE_TYPE_TOUCHSCREEN:
884 case SOURCE_TYPE_TOUCHPAD: {
885 if (!IsValidCheckTouch()) {
886 MMI_HILOGE("IsValidCheckTouch is invalid");
887 return false;
888 }
889 break;
890 }
891 default: {
892 MMI_HILOGE("SourceType is invalid");
893 return false;
894 }
895 }
896 return true;
897 }
898 } // namespace MMI
899 } // namespace OHOS
900