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