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 #ifndef TOUCHPAD_TRANSFORM_PROCESSOR_H
17 #define TOUCHPAD_TRANSFORM_PROCESSOR_H
18 
19 #include <map>
20 
21 #include "nocopyable.h"
22 #include "singleton.h"
23 
24 #include "aggregator.h"
25 #include "timer_manager.h"
26 #include "transform_processor.h"
27 
28 namespace OHOS {
29 namespace MMI {
30 enum class MulFingersTap : int32_t {
31     NO_TAP = 0,
32     TRIPLE_TAP = 3,
33     QUAD_TAP = 4,
34     QUINT_TAP = 5,
35 };
36 
37 class MultiFingersTapHandler final {
38     DECLARE_DELAYED_SINGLETON(MultiFingersTapHandler);
39 
40 public:
41     DISALLOW_COPY_AND_MOVE(MultiFingersTapHandler);
42 
43     enum class TapTrends : int32_t {
44         BEGIN = 0,
45         DOWNING = 1,
46         UPING = 2,
47         NO_MULTAP = 3,
48     };
49 
50     int32_t HandleMulFingersTap(struct libinput_event_touch *event, int32_t type);
51     MulFingersTap GetMultiFingersState();
52     void SetMultiFingersTapHdrDefault(bool isAllDefault = true);
53     bool ClearPointerItems(std::shared_ptr<PointerEvent> pointer);
54     bool CanAddToPointerMaps(struct libinput_event_touch *event);
55     bool CanUnsetPointerItem(struct libinput_event_touch *event);
56 
57 private:
58     int32_t downCnt = 0;
59     int32_t upCnt = 0;
60     TapTrends tapTrends_ = TapTrends::BEGIN;
61     MulFingersTap multiFingersState_ = MulFingersTap::NO_TAP;
62     uint64_t lastTime = 0;
63     uint64_t beginTime = 0;
64     std::map<int32_t, std::pair<float, float>> pointerMaps;
65     const uint64_t perTimeThreshold = 150 * 1e3;
66     const uint64_t totalTimeThreshold = 500 * 1e3;
67     const float distanceThreshold = 0.2F;
68 };
69 #define MULTI_FINGERTAP_HDR ::OHOS::DelayedSingleton<MultiFingersTapHandler>::GetInstance()
70 
71 class TouchPadTransformProcessor final : public TransformProcessor {
72 public:
73     explicit TouchPadTransformProcessor(int32_t deviceId);
74     DISALLOW_COPY_AND_MOVE(TouchPadTransformProcessor);
75     ~TouchPadTransformProcessor() = default;
76     std::shared_ptr<PointerEvent> OnEvent(struct libinput_event *event) override;
77     std::shared_ptr<PointerEvent> GetPointerEvent() override;
78 
79     static int32_t SetTouchpadPinchSwitch(bool switchFlag);
80     static void GetTouchpadPinchSwitch(bool &switchFlag);
81     static int32_t SetTouchpadSwipeSwitch(bool switchFlag);
82     static void GetTouchpadSwipeSwitch(bool &switchFlag);
83     static int32_t SetTouchpadRotateSwitch(bool rotateSwitch);
84     static void GetTouchpadRotateSwitch(bool &rotateSwitch);
85     static int32_t SetTouchpadDoubleTapAndDragState(bool switchFlag);
86     static void GetTouchpadDoubleTapAndDragState(bool &switchFlag);
87 
88 private:
89     static int32_t PutConfigDataToDatabase(std::string &key, bool value);
90     static void GetConfigDataFromDatabase(std::string &key, bool &value);
91 
92     int32_t OnEventTouchPadDown(struct libinput_event *event);
93     int32_t OnEventTouchPadMotion(struct libinput_event *event);
94     int32_t OnEventTouchPadUp(struct libinput_event *event);
95     int32_t SetTouchPadSwipeData(struct libinput_event *event, int32_t action);
96     int32_t OnEventTouchPadSwipeBegin(struct libinput_event *event);
97     int32_t OnEventTouchPadSwipeUpdate(struct libinput_event *event);
98     int32_t OnEventTouchPadSwipeEnd(struct libinput_event *event);
99 
100     int32_t OnEventTouchPadPinchGesture(struct libinput_event *event);
101     int32_t GetPinchGestureType(int32_t type, double angle);
102 
103     int32_t SetTouchPadPinchData(struct libinput_event *event, int32_t action);
104     void SetTouchPadMultiTapData();
105     void SetPinchPointerItem(int64_t time);
106     void ProcessTouchPadPinchDataEvent(int32_t fingerCount, int32_t action, double scale, double angle);
107 
108     int32_t GetTouchPadToolType(struct libinput_event_touch *data, struct libinput_device *device);
109     int32_t GetTouchPadToolType(struct libinput_device *device);
110     void InitToolType();
111 private:
112     const int32_t deviceId_ { -1 };
113     bool isRotateGesture_ { false };
114     double rotateAngle_ { 0.0 };
115     std::shared_ptr<PointerEvent> pointerEvent_ { nullptr };
116     std::vector<std::pair<int32_t, int32_t>> vecToolType_;
117     Aggregator aggregator_ {
118             [](int32_t intervalMs, int32_t repeatCount, std::function<void()> callback) -> int32_t {
119                 return TimerMgr->AddTimer(intervalMs, repeatCount, std::move(callback));
120             },
121             [](int32_t timerId) -> int32_t
122             {
123                 return TimerMgr->ResetTimer(timerId);
124             }
125     };
126 };
127 } // namespace MMI
128 } // namespace OHOS
129 #endif // TOUCHPAD_TRANSFORM_PROCESSOR_H