• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include "pointer_event.h"
19 
20 namespace OHOS {
21 namespace MMI {
GestureMonitorHandler(const GestureMonitorHandler & other)22 GestureMonitorHandler::GestureMonitorHandler(const GestureMonitorHandler &other)
23     : fingers_(other.fingers_), gestureType_(other.gestureType_), touchGestureInfo_(other.touchGestureInfo_)
24 {}
25 
operator =(const GestureMonitorHandler & other)26 GestureMonitorHandler& GestureMonitorHandler::operator=(const GestureMonitorHandler &other)
27 {
28     gestureType_ = other.gestureType_;
29     fingers_ = other.fingers_;
30     touchGestureInfo_ = other.touchGestureInfo_;
31     return *this;
32 }
33 
CheckMonitorValid(TouchGestureType type,int32_t fingers)34 bool GestureMonitorHandler::CheckMonitorValid(TouchGestureType type, int32_t fingers)
35 {
36     if (type == TOUCH_GESTURE_TYPE_NONE ||
37         (TOUCH_GESTURE_TYPE_ALL & type) != type) {
38         return false;
39     }
40     TouchGestureType ret = TOUCH_GESTURE_TYPE_NONE;
41     if (fingers == ALL_FINGER_COUNT) {
42         return true;
43     }
44     if (((type & TOUCH_GESTURE_TYPE_SWIPE) == TOUCH_GESTURE_TYPE_SWIPE) &&
45         (THREE_FINGER_COUNT <= fingers && fingers <= MAX_FINGERS_COUNT)) {
46         ret = TOUCH_GESTURE_TYPE_SWIPE;
47     } else if (((type & TOUCH_GESTURE_TYPE_PINCH) == TOUCH_GESTURE_TYPE_PINCH) &&
48         (FOUR_FINGER_COUNT <= fingers && fingers <= MAX_FINGERS_COUNT)) {
49         ret = TOUCH_GESTURE_TYPE_PINCH;
50     }
51     if (ret != TOUCH_GESTURE_TYPE_NONE) {
52         if ((type = type ^ ret) != TOUCH_GESTURE_TYPE_NONE) {
53             return CheckMonitorValid(type, fingers);
54         }
55     } else {
56         return false;
57     }
58     return true;
59 }
60 
IsTouchGestureEvent(int32_t pointerAction)61 bool GestureMonitorHandler::IsTouchGestureEvent(int32_t pointerAction)
62 {
63     switch (pointerAction) {
64         case PointerEvent::TOUCH_ACTION_SWIPE_DOWN:
65         case PointerEvent::TOUCH_ACTION_SWIPE_UP:
66         case PointerEvent::TOUCH_ACTION_SWIPE_RIGHT:
67         case PointerEvent::TOUCH_ACTION_SWIPE_LEFT:
68         case PointerEvent::TOUCH_ACTION_PINCH_OPENED:
69         case PointerEvent::TOUCH_ACTION_PINCH_CLOSEED:
70         case PointerEvent::TOUCH_ACTION_GESTURE_END: {
71             return true;
72         }
73         default: {
74             return false;
75         }
76     }
77 }
78 
IsMatchGesture(int32_t action,int32_t count) const79 bool GestureMonitorHandler::IsMatchGesture(int32_t action, int32_t count) const
80 {
81     TouchGestureType type = TOUCH_GESTURE_TYPE_NONE;
82     switch (action) {
83         case PointerEvent::TOUCH_ACTION_SWIPE_DOWN:
84         case PointerEvent::TOUCH_ACTION_SWIPE_UP:
85         case PointerEvent::TOUCH_ACTION_SWIPE_RIGHT:
86         case PointerEvent::TOUCH_ACTION_SWIPE_LEFT:
87             type = TOUCH_GESTURE_TYPE_SWIPE;
88             break;
89         case PointerEvent::TOUCH_ACTION_PINCH_OPENED:
90         case PointerEvent::TOUCH_ACTION_PINCH_CLOSEED:
91             type = TOUCH_GESTURE_TYPE_PINCH;
92             break;
93         case PointerEvent::TOUCH_ACTION_GESTURE_END:
94             return true;
95         default:
96             return false;
97     }
98     auto iter = touchGestureInfo_.find(type);
99     if (iter == touchGestureInfo_.end()) {
100         iter = touchGestureInfo_.find(TOUCH_GESTURE_TYPE_ALL);
101         if (iter == touchGestureInfo_.end()) {
102             return false;
103         }
104     }
105     const std::set<int32_t> &info = iter->second;
106     return ((gestureType_ & type) == type) &&
107         (info.find(count) != info.end() || info.find(ALL_FINGER_COUNT) != info.end());
108 }
109 
AddGestureMonitor(TouchGestureType type,int32_t fingers)110 void GestureMonitorHandler::AddGestureMonitor(TouchGestureType type, int32_t fingers)
111 {
112     if (type == TOUCH_GESTURE_TYPE_NONE) {
113         return;
114     }
115     fingers_ = fingers;
116     gestureType_ = gestureType_ | type;
117     auto iter = touchGestureInfo_.find(type);
118     if (iter == touchGestureInfo_.end()) {
119         touchGestureInfo_.insert({type, { fingers }});
120         return;
121     }
122     iter->second.insert(fingers);
123 }
124 
RemoveGestureMonitor(TouchGestureType type,int32_t fingers)125 bool GestureMonitorHandler::RemoveGestureMonitor(TouchGestureType type, int32_t fingers)
126 {
127     auto iter = touchGestureInfo_.find(type);
128     if (iter == touchGestureInfo_.end()) {
129         return false;
130     }
131     std::set<int32_t> &info = iter->second;
132     auto it = info.find(fingers);
133     if (it == info.end()) {
134         return false;
135     }
136     info.erase(it);
137     if (info.empty()) {
138         if (touchGestureInfo_.find(TOUCH_GESTURE_TYPE_ALL) == touchGestureInfo_.end()) {
139             gestureType_ = gestureType_ ^ type;
140         }
141         touchGestureInfo_.erase(iter);
142     }
143     return touchGestureInfo_.empty();
144 }
145 } // namespace MMI
146 } // namespace OHOS