1 /* 2 * Copyright (c) 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 MOUSE_TRANSFORM_PROCESSOR_H 17 #define MOUSE_TRANSFORM_PROCESSOR_H 18 19 #include "aggregator.h" 20 #include "device_type_definition.h" 21 #include "libinput.h" 22 #include "timer_manager.h" 23 #include "pointer_event.h" 24 #include "touchpad_control_display_gain.h" 25 #include "old_display_info.h" 26 #include <preferences_value.h> 27 28 namespace OHOS { 29 30 extern "C" { 31 struct Offset { 32 double dx; 33 double dy; 34 }; 35 36 int32_t HandleMotionAccelerateMouse(const Offset* offset, bool mode, double* abs_x, double* abs_y, 37 int32_t speed, int32_t deviceType); 38 int32_t HandleMotionAccelerateTouchpad(const Offset* offset, bool mode, double* abs_x, double* abs_y, 39 int32_t speed, int32_t deviceType); 40 int32_t HandleAxisAccelerateTouchpad(bool mode, double* abs_axis, int32_t deviceType); 41 int32_t HandleMotionDynamicAccelerateMouse(const Offset* offset, bool mode, double* abs_x, double* abs_y, 42 int32_t speed, uint64_t delta_time, double display_ppi, double factor); 43 int32_t HandleMotionDynamicAccelerateTouchpad(const Offset* offset, bool mode, double* abs_x, double* abs_y, 44 int32_t speed, double display_size, double touchpad_size, double touchpad_ppi, int32_t frequency); 45 } 46 47 namespace MMI { 48 struct AccelerateCurve { 49 std::vector<int32_t> speeds; 50 std::vector<double> slopes; 51 std::vector<double> diffNums; 52 }; 53 class MouseTransformProcessor final : public std::enable_shared_from_this<MouseTransformProcessor> { 54 struct Movement { 55 double dx; 56 double dy; 57 }; 58 59 public: 60 enum class RightClickType { 61 TP_RIGHT_BUTTON = 1, 62 TP_LEFT_BUTTON = 2, 63 TP_TWO_FINGER_TAP = 3, 64 TP_TWO_FINGER_TAP_OR_RIGHT_BUTTON = 4, 65 TP_TWO_FINGER_TAP_OR_LEFT_BUTTON = 5, 66 }; 67 68 enum class PointerDataSource { 69 MOUSE = 1, 70 TOUCHPAD = 2, 71 }; 72 73 #ifdef OHOS_BUILD_MOUSE_REPORTING_RATE 74 struct FilterInsertionPoint { 75 double filterX{ 0.0 }; 76 double filterY{ 0.0 }; 77 uint64_t filterPrePointTime{ 0 }; 78 uint64_t filterDeltaTime{ 0 }; 79 bool filterFlag{ false }; 80 static constexpr int32_t FILTER_THRESHOLD_US = 800; // <=1ms 81 }; 82 #endif // OHOS_BUILD_MOUSE_REPORTING_RATE 83 84 public: 85 DISALLOW_COPY_AND_MOVE(MouseTransformProcessor); 86 explicit MouseTransformProcessor(int32_t deviceId); 87 ~MouseTransformProcessor() = default; 88 std::shared_ptr<PointerEvent> GetPointerEvent() const; 89 int32_t Normalize(struct libinput_event *event); 90 int32_t NormalizeRotateEvent(struct libinput_event *event, int32_t type, double angle); 91 void Dump(int32_t fd, const std::vector<std::string> &args); 92 bool CheckAndPackageAxisEvent(); 93 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING 94 bool NormalizeMoveMouse(int32_t offsetX, int32_t offsetY); 95 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING 96 #ifdef OHOS_BUILD_MOUSE_REPORTING_RATE 97 void HandleFilterMouseEvent(Offset* offset); 98 bool CheckFilterMouseEvent(struct libinput_event *event); 99 #endif // OHOS_BUILD_MOUSE_REPORTING_RATE 100 int32_t SetMouseAccelerateMotionSwitch(bool enable); 101 102 private: 103 int32_t HandleMotionInner(struct libinput_event_pointer* data, struct libinput_event *event); 104 int32_t HandleButtonInner(struct libinput_event_pointer* data, struct libinput_event *event); 105 int32_t HandleAxisInner(struct libinput_event_pointer* data); 106 int32_t HandleAxisBeginEndInner(struct libinput_event *event); 107 int32_t HandleScrollFingerInner(struct libinput_event *event); 108 void HandleAxisPostInner(PointerEvent::PointerItem &pointerItem); 109 bool HandlePostInner(struct libinput_event_pointer* data, PointerEvent::PointerItem &pointerItem); 110 void HandleTouchPadAxisState(libinput_pointer_axis_source source, int32_t& direction, bool& tpScrollSwitch); 111 void HandleTouchPadButton(enum libinput_button_state state, int32_t type); 112 int32_t UpdateMouseMoveLocation(const OLD::DisplayInfo* displayInfo, Offset &offset, 113 double &abs_x, double &abs_y, int32_t deviceType); 114 int32_t UpdateTouchpadMoveLocation(const OLD::DisplayInfo* displayInfo, struct libinput_event* event, 115 Offset &offset, double &abs_x, double &abs_y, int32_t deviceType); 116 #ifndef OHOS_BUILD_ENABLE_WATCH 117 void HandleTouchpadRightButton(struct libinput_event_pointer* data, const int32_t evenType, uint32_t &button); 118 void HandleTouchpadLeftButton(struct libinput_event_pointer* data, const int32_t evenType, uint32_t &button); 119 void HandleTouchpadTwoFingerButton(struct libinput_event_pointer* data, const int32_t evenType, uint32_t &button); 120 void HandleTouchpadTwoFingerButtonOrRightButton(struct libinput_event_pointer* data, 121 const int32_t evenType, uint32_t &button); 122 void HandleTouchpadTwoFingerButtonOrLeftButton(struct libinput_event_pointer* data, 123 const int32_t evenType, uint32_t &button); 124 void TransTouchpadRightButton(struct libinput_event_pointer* data, const int32_t type, uint32_t &button); 125 double HandleAxisAccelateTouchPad(double axisValue); 126 #endif // OHOS_BUILD_ENABLE_WATCH 127 void CalculateOffset(const OLD::DisplayInfo* displayInfo, Offset &offset); 128 bool IsWindowRotation(const OLD::DisplayInfo* displayInfo); 129 Direction GetDisplayDirection(const OLD::DisplayInfo *displayInfo); 130 void HandleReportMouseResponseTime(std::string &connectType, std::map<long long, int32_t> &curMap); 131 void CalculateMouseResponseTimeProbability(struct libinput_event *event); 132 std::map<std::string, std::map<long long, int32_t>> mouseResponseMap = {}; 133 std::map<std::string, std::chrono::time_point<std::chrono::steady_clock>> mouseMap = {}; 134 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING 135 void HandleMotionMoveMouse(int32_t offsetX, int32_t offsetY); 136 void HandlePostMoveMouse(PointerEvent::PointerItem &pointerItem); 137 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING 138 int32_t HandleButtonValueInner(struct libinput_event_pointer* data, uint32_t& button, int32_t type); 139 DeviceType CheckDeviceType(int32_t width, int32_t height); 140 void DeletePressedButton(uint32_t originButton); 141 void DumpInner(); 142 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD 143 static bool IsEventFromVirtualSource(struct libinput_event* event); 144 static void GetVirtualTouchpadTapSwitch(bool &switchFlag); 145 static void GetVirtualTouchpadRightClickType(int32_t &type); 146 static int32_t GetVirtualTouchpadPrimaryButton(); 147 #endif // OHOS_BUILD_ENABLE_VKEYBOARD 148 static int32_t PutConfigDataToDatabase(std::string &key, bool value); 149 static void GetConfigDataFromDatabase(std::string &key, bool &value); 150 static int32_t PutConfigDataToDatabase(std::string &key, int32_t value); 151 static void GetConfigDataFromDatabase(std::string &key, int32_t &value); 152 153 public: 154 static void OnDisplayLost(int32_t displayId); 155 static int32_t GetDisplayId(); 156 static int32_t SetMousePrimaryButton(int32_t primaryButton); 157 static int32_t GetMousePrimaryButton(); 158 static int32_t SetMouseScrollRows(int32_t rows); 159 static int32_t GetMouseScrollRows(); 160 static int32_t SetPointerSpeed(int32_t speed); 161 static int32_t GetPointerSpeed(); 162 static int32_t SetPointerLocation(int32_t x, int32_t y, int32_t displayId); 163 static int32_t GetPointerLocation(int32_t &displayId, double &displayX, double &displayY); 164 static int32_t SetTouchpadScrollSwitch(int32_t pid, bool switchFlag); 165 static void GetTouchpadScrollSwitch(bool &switchFlag); 166 static int32_t SetTouchpadScrollDirection(bool state); 167 static void GetTouchpadScrollDirection(bool &state); 168 static int32_t SetTouchpadTapSwitch(bool switchFlag); 169 static void GetTouchpadTapSwitch(bool &switchFlag); 170 static int32_t SetTouchpadRightClickType(int32_t type); 171 static void GetTouchpadRightClickType(int32_t &type); 172 static int32_t SetTouchpadPointerSpeed(int32_t speed); 173 static void GetTouchpadPointerSpeed(int32_t &speed); 174 static void GetTouchpadCDG(TouchpadCDG &touchpadCDG); 175 static void UpdateTouchpadCDG(double touchpadPPi, double touchpadSize, int32_t frequency); 176 static int32_t GetTouchpadSpeed(); 177 178 private: 179 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD 180 static std::atomic_bool isVirtualDeviceEvent_; 181 #endif // OHOS_BUILD_ENABLE_VKEYBOARD 182 static int32_t globalPointerSpeed_; 183 static int32_t scrollSwitchPid_; 184 std::shared_ptr<PointerEvent> pointerEvent_ { nullptr }; 185 int32_t timerId_ { -1 }; 186 int32_t buttonId_ { -1 }; 187 uint32_t pressedButton_ { 0 }; 188 bool isPressed_ { false }; 189 int32_t deviceId_ { -1 }; 190 bool isAxisBegin_ { false }; 191 Movement unaccelerated_ {}; 192 std::map<int32_t, int32_t> buttonMapping_; 193 static TouchpadCDG touchpadOption_; 194 Aggregator aggregator_ { 195 [](int32_t intervalMs, int32_t repeatCount, std::function<void()> callback) -> int32_t { 196 return TimerMgr->AddTimer(intervalMs, repeatCount, std::move(callback)); 197 }, 198 [](int32_t timerId) -> int32_t 199 { 200 return TimerMgr->ResetTimer(timerId); 201 } 202 }; 203 bool enableMouseAleaccelerateBool_ { true }; 204 #ifdef OHOS_BUILD_MOUSE_REPORTING_RATE 205 struct FilterInsertionPoint filterInsertionPoint_; 206 #endif // OHOS_BUILD_MOUSE_REPORTING_RATE 207 }; 208 } // namespace MMI 209 } // namespace OHOS 210 #endif // MOUSE_TRANSFORM_PROCESSOR_H