• 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 #ifndef TOUCH_GESTURE_DETECTOR_H
17 #define TOUCH_GESTURE_DETECTOR_H
18 
19 #include <map>
20 #include <unordered_set>
21 
22 #include "pointer_event.h"
23 
24 namespace OHOS {
25 namespace MMI {
26 enum class GestureMode {
27     ACTION_UNKNOWN,
28     ACTION_SWIPE_DOWN,
29     ACTION_SWIPE_UP,
30     ACTION_SWIPE_LEFT,
31     ACTION_SWIPE_RIGHT,
32     ACTION_PINCH_CLOSED,
33     ACTION_PINCH_OPENED,
34     ACTION_GESTURE_END
35 };
36 
37 struct Point {
PointPoint38     Point() {}
PointPoint39     Point(float px, float py) : x(px), y(py) {}
PointPoint40     Point(float px, float py, int64_t pt)
41         : x(px), y(py), time(pt) {}
42     float x { 0.0f };
43     float y { 0.0f };
44     int64_t time { 0 };
45 };
46 
47 class TouchGestureDetector final {
48 public:
49     class GestureListener {
50     public:
51         GestureListener() = default;
52         virtual ~GestureListener() = default;
53         virtual bool OnGestureEvent(std::shared_ptr<PointerEvent> event, GestureMode mode) = 0;
54         virtual void OnGestureTrend(std::shared_ptr<PointerEvent> event) = 0;
55     };
56 
TouchGestureDetector(TouchGestureType type,std::shared_ptr<GestureListener> listener)57     TouchGestureDetector(TouchGestureType type, std::shared_ptr<GestureListener> listener)
58         : gestureType_(type), listener_(listener) {}
59     bool OnTouchEvent(std::shared_ptr<PointerEvent> event);
60     void AddGestureFingers(int32_t fingers);
61     void RemoveGestureFingers(int32_t fingers);
62     void HandleGestureWindowEmerged(int32_t windowId, std::shared_ptr<PointerEvent> lastTouchEvent);
63 
64     static bool IsPhysicalPointer(std::shared_ptr<PointerEvent> event);
65 
66 private:
67     enum class SlideState {
68         DIRECTION_UNKNOW,
69         DIRECTION_DOWN,
70         DIRECTION_UP,
71         DIRECTION_LEFT,
72         DIRECTION_RIGHT
73     };
74 
75     void ReleaseData();
76     bool IsMatchGesture(int32_t count) const;
77     bool IsMatchGesture(GestureMode action, int32_t count) const;
78     void HandleDownEvent(std::shared_ptr<PointerEvent> event);
79     void HandleMoveEvent(std::shared_ptr<PointerEvent> event);
80     void HandleUpEvent(std::shared_ptr<PointerEvent> event);
81     bool NotifyGestureEvent(std::shared_ptr<PointerEvent> event, GestureMode mode);
82     bool WhetherDiscardTouchEvent(std::shared_ptr<PointerEvent> event);
83 
84     Point CalcClusterCenter(const std::map<int32_t, Point> &points) const;
85     Point CalcGravityCenter(std::map<int32_t, Point> &map);
86     double CalcTwoPointsDistance(const Point &p1, const Point &p2) const;
87     void CalcAndStoreDistance();
88     int32_t CalcMultiFingerMovement(std::map<int32_t, Point> &map);
89     void HandlePinchMoveEvent(std::shared_ptr<PointerEvent> event);
90     bool InOppositeDirections(const std::unordered_set<SlideState> &directions) const;
91     bool InDiverseDirections(const std::unordered_set<SlideState> &directions) const;
92     GestureMode JudgeOperationMode(std::map<int32_t, Point> &movePoint);
93     bool AntiJitter(std::shared_ptr<PointerEvent> event, GestureMode mode);
94     std::vector<std::pair<int32_t, Point>> SortPoints(std::map<int32_t, Point> &points);
95 
96     bool HandleFingerDown();
97     int64_t GetMaxDownInterval() const;
98     float GetMaxFingerSpacing() const;
99     GestureMode ChangeToGestureMode(SlideState state);
100     SlideState GetSlidingDirection(double angle);
101     void HandleSwipeMoveEvent(std::shared_ptr<PointerEvent> event);
102     bool IsFingerMove(const Point &downPt, const Point &movePt) const;
103     double GetAngle(float startX, float startY, float endX, float endY) const;
104     SlideState ClacFingerMoveDirection(std::shared_ptr<PointerEvent> event);
105     void CheckGestureTrend(std::shared_ptr<PointerEvent> event) const;
106     bool IsLastTouchUp(std::shared_ptr<PointerEvent> event) const;
107     void OnGestureSendEvent(std::shared_ptr<PointerEvent> event) const;
108     std::string DumpTouches() const;
109 
110 private:
111     std::set<int32_t> fingers_;
112     TouchGestureType gestureType_ { TOUCH_GESTURE_TYPE_NONE };
113     bool isRecognized_ { false };
114     bool gestureEnable_ { false };
115     bool isFingerReady_ { false };
116     bool haveGestureWinEmerged_ { false };
117     int32_t gestureDisplayId_ { INT32_MAX };
118     int32_t continuousCloseCount_ { 0 };
119     int32_t continuousOpenCount_ { 0 };
120     int32_t gestureTimer_ { -1 };
121     std::map<int32_t, Point> downPoint_;
122     std::map<int32_t, Point> movePoint_;
123     std::map<int32_t, double> lastDistance_;
124     std::shared_ptr<GestureListener> listener_ { nullptr };
125     std::shared_ptr<PointerEvent> lastTouchEvent_ { nullptr };
126 };
127 } // namespace MMI
128 } // namespace OHOS
129 #endif // TOUCH_GESTURE_DETECTOR_H