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