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 <map>
20 #include <memory>
21 
22 #include "libinput.h"
23 #include "singleton.h"
24 
25 #include "aggregator.h"
26 #include "define_multimodal.h"
27 #include "timer_manager.h"
28 #include "pointer_event.h"
29 #include "window_info.h"
30 
31 namespace OHOS {
32 
33 extern "C" {
34     struct Offset {
35         double dx;
36         double dy;
37     };
38     enum class DeviceType {
39         DEVICE_UNKOWN = 0,
40         DEVICE_PC = 1,
41         DEVICE_SOFT_PC_PRO = 2,
42         DEVICE_HARD_PC_PRO = 3,
43         DEVICE_TABLET = 4,
44     };
45     int32_t HandleMotionAccelerateMouse(const Offset* offset, bool mode, double* abs_x, double* abs_y,
46         int32_t speed, int32_t deviceType);
47     int32_t HandleMotionAccelerateTouchpad(const Offset* offset, bool mode, double* abs_x, double* abs_y,
48         int32_t speed, int32_t deviceType);
49     int32_t HandleAxisAccelerateTouchpad(bool mode, double* abs_axis, int32_t deviceType);
50 }
51 
52 namespace MMI {
53 struct AccelerateCurve {
54     std::vector<int32_t> speeds;
55     std::vector<double> slopes;
56     std::vector<double> diffNums;
57 };
58 class MouseTransformProcessor final : public std::enable_shared_from_this<MouseTransformProcessor> {
59     struct Movement {
60         double dx;
61         double dy;
62     };
63 
64 public:
65     enum class RightClickType {
66         TP_RIGHT_BUTTON = 1,
67         TP_LEFT_BUTTON = 2,
68         TP_TWO_FINGER_TAP = 3,
69     };
70 
71     enum class PointerDataSource {
72         MOUSE = 1,
73         TOUCHPAD = 2,
74     };
75 
76 public:
77     DISALLOW_COPY_AND_MOVE(MouseTransformProcessor);
78     explicit MouseTransformProcessor(int32_t deviceId);
79     ~MouseTransformProcessor() = default;
80     std::shared_ptr<PointerEvent> GetPointerEvent() const;
81     int32_t Normalize(struct libinput_event *event);
82     int32_t NormalizeRotateEvent(struct libinput_event *event, int32_t type, double angle);
83     void Dump(int32_t fd, const std::vector<std::string> &args);
84     bool CheckAndPackageAxisEvent();
85 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
86     bool NormalizeMoveMouse(int32_t offsetX, int32_t offsetY);
87 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
88 private:
89     int32_t HandleMotionInner(struct libinput_event_pointer* data, struct libinput_event *event);
90     int32_t HandleButtonInner(struct libinput_event_pointer* data, struct libinput_event *event);
91     int32_t HandleAxisInner(struct libinput_event_pointer* data);
92     int32_t HandleAxisBeginEndInner(struct libinput_event *event);
93     int32_t HandleScrollFingerInner(struct libinput_event *event);
94     void HandleAxisPostInner(PointerEvent::PointerItem &pointerItem);
95     bool HandlePostInner(struct libinput_event_pointer* data, PointerEvent::PointerItem &pointerItem);
96     void HandleTouchPadAxisState(libinput_pointer_axis_source source, int32_t& direction, bool& tpScrollSwitch);
97     void HandleTouchpadRightButton(struct libinput_event_pointer* data, const int32_t evenType, uint32_t &button);
98     void HandleTouchpadLeftButton(struct libinput_event_pointer* data, const int32_t evenType, uint32_t &button);
99     void HandleTouchpadTwoFingerButton(struct libinput_event_pointer* data, const int32_t evenType, uint32_t &button);
100     void TransTouchpadRightButton(struct libinput_event_pointer* data, const int32_t type, uint32_t &button);
101     void CalculateOffset(Direction direction, Offset &offset);
102     double HandleAxisAccelateTouchPad(double axisValue);
103     bool IsWindowRotation(const DisplayInfo* displayInfo);
104 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
105     void HandleMotionMoveMouse(int32_t offsetX, int32_t offsetY);
106     void HandlePostMoveMouse(PointerEvent::PointerItem &pointerItem);
107 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
108     int32_t HandleButtonValueInner(struct libinput_event_pointer* data, uint32_t button, int32_t type);
109     DeviceType CheckDeviceType(int32_t width, int32_t height);
110     void DeletePressedButton(uint32_t originButton);
111     void DumpInner();
112 
113     static int32_t PutConfigDataToDatabase(std::string &key, bool value);
114     static void GetConfigDataFromDatabase(std::string &key, bool &value);
115     static int32_t PutConfigDataToDatabase(std::string &key, int32_t value);
116     static void GetConfigDataFromDatabase(std::string &key, int32_t &value);
117 
118 public:
119     static void OnDisplayLost(int32_t displayId);
120     static int32_t GetDisplayId();
121     static int32_t SetMousePrimaryButton(int32_t primaryButton);
122     static int32_t GetMousePrimaryButton();
123     static int32_t SetMouseScrollRows(int32_t rows);
124     static int32_t GetMouseScrollRows();
125     static int32_t SetPointerSpeed(int32_t speed);
126     static int32_t GetPointerSpeed();
127     static int32_t SetPointerLocation(int32_t x, int32_t y);
128     static int32_t SetTouchpadScrollSwitch(bool switchFlag);
129     static void GetTouchpadScrollSwitch(bool &switchFlag);
130     static int32_t SetTouchpadScrollDirection(bool state);
131     static void GetTouchpadScrollDirection(bool &state);
132     static int32_t SetTouchpadTapSwitch(bool switchFlag);
133     static void GetTouchpadTapSwitch(bool &switchFlag);
134     static int32_t SetTouchpadRightClickType(int32_t type);
135     static void GetTouchpadRightClickType(int32_t &type);
136     static int32_t SetTouchpadPointerSpeed(int32_t speed);
137     static void GetTouchpadPointerSpeed(int32_t &speed);
138     static int32_t GetTouchpadSpeed();
139 
140 private:
141     static int32_t globalPointerSpeed_;
142 
143     std::shared_ptr<PointerEvent> pointerEvent_ { nullptr };
144     int32_t timerId_ { -1 };
145     int32_t buttonId_ { -1 };
146     bool isPressed_ { false };
147     int32_t deviceId_ { -1 };
148     bool isAxisBegin_ { false };
149     Movement unaccelerated_ {};
150     std::map<int32_t, int32_t> buttonMapping_;
151     Aggregator aggregator_ {
152             [](int32_t intervalMs, int32_t repeatCount, std::function<void()> callback) -> int32_t {
153                 return TimerMgr->AddTimer(intervalMs, repeatCount, std::move(callback));
154             },
155             [](int32_t timerId) -> int32_t
156             {
157                 return TimerMgr->ResetTimer(timerId);
158             }
159     };
160 };
161 } // namespace MMI
162 } // namespace OHOS
163 #endif // MOUSE_TRANSFORM_PROCESSOR_H