1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gesture_monitor_handler.h"
17 #include "pointer_event.h"
18
19 namespace OHOS {
20 namespace MMI {
GestureMonitorHandler(const GestureMonitorHandler &other)21 GestureMonitorHandler::GestureMonitorHandler(const GestureMonitorHandler &other)
22 : fingers_(other.fingers_), gestureType_(other.gestureType_), touchGestureInfo_(other.touchGestureInfo_)
23 {}
24
operator =(const GestureMonitorHandler &other)25 GestureMonitorHandler& GestureMonitorHandler::operator=(const GestureMonitorHandler &other)
26 {
27 gestureType_ = other.gestureType_;
28 fingers_ = other.fingers_;
29 touchGestureInfo_ = other.touchGestureInfo_;
30 return *this;
31 }
32
CheckMonitorValid(TouchGestureType type, int32_t fingers)33 bool GestureMonitorHandler::CheckMonitorValid(TouchGestureType type, int32_t fingers)
34 {
35 TouchGestureType ret = TOUCH_GESTURE_TYPE_NONE;
36 if (fingers == ALL_FINGER_COUNT) {
37 return true;
38 }
39 if (((type & TOUCH_GESTURE_TYPE_SWIPE) == TOUCH_GESTURE_TYPE_SWIPE) &&
40 (THREE_FINGER_COUNT <= fingers && fingers <= MAX_FINGERS_COUNT)) {
41 ret = TOUCH_GESTURE_TYPE_SWIPE;
42 } else if (((type & TOUCH_GESTURE_TYPE_PINCH) == TOUCH_GESTURE_TYPE_PINCH) &&
43 (FOUR_FINGER_COUNT <= fingers && fingers <= MAX_FINGERS_COUNT)) {
44 ret = TOUCH_GESTURE_TYPE_PINCH;
45 }
46 if (ret != TOUCH_GESTURE_TYPE_NONE) {
47 if ((type = type ^ ret) != TOUCH_GESTURE_TYPE_NONE) {
48 return CheckMonitorValid(type, fingers);
49 }
50 } else {
51 return false;
52 }
53 return true;
54 }
55
IsTouchGestureEvent(int32_t pointerAction)56 bool GestureMonitorHandler::IsTouchGestureEvent(int32_t pointerAction)
57 {
58 switch (pointerAction) {
59 case PointerEvent::TOUCH_ACTION_SWIPE_DOWN:
60 case PointerEvent::TOUCH_ACTION_SWIPE_UP:
61 case PointerEvent::TOUCH_ACTION_SWIPE_RIGHT:
62 case PointerEvent::TOUCH_ACTION_SWIPE_LEFT:
63 case PointerEvent::TOUCH_ACTION_PINCH_OPENED:
64 case PointerEvent::TOUCH_ACTION_PINCH_CLOSEED:
65 case PointerEvent::TOUCH_ACTION_GESTURE_END: {
66 return true;
67 }
68 default: {
69 return false;
70 }
71 }
72 }
73
IsMatchGesture(int32_t action, int32_t count) const74 bool GestureMonitorHandler::IsMatchGesture(int32_t action, int32_t count) const
75 {
76 TouchGestureType type = TOUCH_GESTURE_TYPE_NONE;
77 switch (action) {
78 case PointerEvent::TOUCH_ACTION_SWIPE_DOWN:
79 case PointerEvent::TOUCH_ACTION_SWIPE_UP:
80 case PointerEvent::TOUCH_ACTION_SWIPE_RIGHT:
81 case PointerEvent::TOUCH_ACTION_SWIPE_LEFT:
82 type = TOUCH_GESTURE_TYPE_SWIPE;
83 break;
84 case PointerEvent::TOUCH_ACTION_PINCH_OPENED:
85 case PointerEvent::TOUCH_ACTION_PINCH_CLOSEED:
86 type = TOUCH_GESTURE_TYPE_PINCH;
87 break;
88 case PointerEvent::TOUCH_ACTION_GESTURE_END:
89 return true;
90 default:
91 return false;
92 }
93 auto iter = touchGestureInfo_.find(type);
94 if (iter == touchGestureInfo_.end()) {
95 iter = touchGestureInfo_.find(TOUCH_GESTURE_TYPE_ALL);
96 if (iter == touchGestureInfo_.end()) {
97 return false;
98 }
99 }
100 const std::set<int32_t> &info = iter->second;
101 return ((gestureType_ & type) == type) &&
102 (info.find(count) != info.end() || info.find(ALL_FINGER_COUNT) != info.end());
103 }
104
AddGestureMonitor(TouchGestureType type, int32_t fingers)105 void GestureMonitorHandler::AddGestureMonitor(TouchGestureType type, int32_t fingers)
106 {
107 if (type == TOUCH_GESTURE_TYPE_NONE) {
108 return;
109 }
110 fingers_ = fingers;
111 gestureType_ = gestureType_ | type;
112 auto iter = touchGestureInfo_.find(type);
113 if (iter == touchGestureInfo_.end()) {
114 touchGestureInfo_.insert({type, { fingers }});
115 return;
116 }
117 iter->second.insert(fingers);
118 }
119
RemoveGestureMonitor(TouchGestureType type, int32_t fingers)120 bool GestureMonitorHandler::RemoveGestureMonitor(TouchGestureType type, int32_t fingers)
121 {
122 auto iter = touchGestureInfo_.find(type);
123 if (iter == touchGestureInfo_.end()) {
124 return false;
125 }
126 std::set<int32_t> &info = iter->second;
127 auto it = info.find(fingers);
128 if (it == info.end()) {
129 return false;
130 }
131 info.erase(it);
132 if (info.empty()) {
133 if (touchGestureInfo_.find(TOUCH_GESTURE_TYPE_ALL) == touchGestureInfo_.end()) {
134 gestureType_ = gestureType_ ^ type;
135 }
136 touchGestureInfo_.erase(iter);
137 }
138 return touchGestureInfo_.empty();
139 }
140 } // namespace MMI
141 } // namespace OHOS