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 #include "mouse_event_normalize.h"
17 
18 #include <cinttypes>
19 
20 #include <linux/input-event-codes.h>
21 
22 #include "define_multimodal.h"
23 #include "event_log_helper.h"
24 #include "i_input_windows_manager.h"
25 #include "i_pointer_drawing_manager.h"
26 #include "input_device_manager.h"
27 #include "input_event_handler.h"
28 #include "mouse_device_state.h"
29 #include "timer_manager.h"
30 #include "util_ex.h"
31 #include "util.h"
32 
33 #undef MMI_LOG_DOMAIN
34 #define MMI_LOG_DOMAIN MMI_LOG_DISPATCH
35 #undef MMI_LOG_TAG
36 #define MMI_LOG_TAG "MouseEventNormalize"
37 
38 namespace OHOS {
39 namespace MMI {
MouseEventNormalize()40 MouseEventNormalize::MouseEventNormalize() {}
~MouseEventNormalize()41 MouseEventNormalize::~MouseEventNormalize() {}
42 
GetProcessor(int32_t deviceId) const43 std::shared_ptr<MouseTransformProcessor> MouseEventNormalize::GetProcessor(int32_t deviceId) const
44 {
45     auto iter = processors_.find(deviceId);
46     if (iter == processors_.end()) {
47         MMI_HILOGE("Can't find mouse processor by deviceId:%{public}d", deviceId);
48         return nullptr;
49     }
50     return iter->second;
51 }
52 
GetCurrentProcessor() const53 std::shared_ptr<MouseTransformProcessor> MouseEventNormalize::GetCurrentProcessor() const
54 {
55     int32_t deviceId = GetCurrentDeviceId();
56     auto iter = processors_.find(deviceId);
57     if (iter == processors_.end()) {
58         MMI_HILOGE("Can't find mouse processor by deviceId:%{public}d", deviceId);
59         return nullptr;
60     }
61     return iter->second;
62 }
63 
SetCurrentDeviceId(int32_t deviceId)64 void MouseEventNormalize::SetCurrentDeviceId(int32_t deviceId)
65 {
66     currentDeviceId_ = deviceId;
67 }
68 
GetCurrentDeviceId() const69 int32_t MouseEventNormalize::GetCurrentDeviceId() const
70 {
71     return currentDeviceId_;
72 }
73 
GetPointerEvent()74 std::shared_ptr<PointerEvent> MouseEventNormalize::GetPointerEvent()
75 {
76     auto processor = GetCurrentProcessor();
77     CHKPP(processor);
78     return processor->GetPointerEvent();
79 }
80 
GetPointerEvent(int32_t deviceId)81 std::shared_ptr<PointerEvent> MouseEventNormalize::GetPointerEvent(int32_t deviceId)
82 {
83     auto iter = processors_.find(deviceId);
84     if (iter == processors_.end()) {
85         MMI_HILOGE("Can't find mouse processor by deviceId:%{public}d", deviceId);
86         return nullptr;
87     }
88     CHKPP(iter->second);
89     return iter->second->GetPointerEvent();
90 }
91 
OnEvent(struct libinput_event * event)92 int32_t MouseEventNormalize::OnEvent(struct libinput_event *event)
93 {
94     CHKPR(event, RET_ERR);
95     auto device = libinput_event_get_device(event);
96     CHKPR(device, RET_ERR);
97     int32_t deviceId = INPUT_DEV_MGR->FindInputDeviceId(device);
98     if (deviceId < 0) {
99         MMI_HILOGE("The deviceId:%{public}d is invalid", deviceId);
100         return RET_ERR;
101     }
102     SetCurrentDeviceId(deviceId);
103     std::shared_ptr<MouseTransformProcessor> processor { nullptr };
104     if (auto it = processors_.find(deviceId); it != processors_.end()) {
105         processor = it->second;
106     } else {
107         processor = std::make_shared<MouseTransformProcessor>(deviceId);
108         auto [tIter, isOk] = processors_.emplace(deviceId, processor);
109     }
110     return processor->Normalize(event);
111 }
112 
113 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
OnDisplayLost(int32_t displayId)114 void MouseEventNormalize::OnDisplayLost(int32_t displayId)
115 {
116     MouseTransformProcessor::OnDisplayLost(displayId);
117 }
118 
GetDisplayId() const119 int32_t MouseEventNormalize::GetDisplayId() const
120 {
121     return MouseTransformProcessor::GetDisplayId();
122 }
123 
NormalizeMoveMouse(int32_t offsetX,int32_t offsetY)124 bool MouseEventNormalize::NormalizeMoveMouse(int32_t offsetX, int32_t offsetY)
125 {
126     CALL_DEBUG_ENTER;
127     auto processor = GetCurrentProcessor();
128     CHKPF(processor);
129     return processor->NormalizeMoveMouse(offsetX, offsetY);
130 }
131 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
132 
Dump(int32_t fd,const std::vector<std::string> & args)133 void MouseEventNormalize::Dump(int32_t fd, const std::vector<std::string> &args)
134 {
135     auto processor = GetCurrentProcessor();
136     CHKPV(processor);
137     processor->Dump(fd, args);
138 }
139 
NormalizeRotateEvent(struct libinput_event * event,int32_t type,double angle)140 int32_t MouseEventNormalize::NormalizeRotateEvent(struct libinput_event *event, int32_t type, double angle)
141 {
142     CHKPR(event, RET_ERR);
143     auto device = libinput_event_get_device(event);
144     CHKPR(device, RET_ERR);
145     int32_t deviceId = INPUT_DEV_MGR->FindInputDeviceId(device);
146     if (deviceId < 0) {
147         MMI_HILOGE("The deviceId is invalid, deviceId: %{public}d", deviceId);
148         return RET_ERR;
149     }
150     SetCurrentDeviceId(deviceId);
151     std::shared_ptr<MouseTransformProcessor> processor { nullptr };
152     if (auto it = processors_.find(deviceId); it != processors_.end()) {
153         processor = it->second;
154     } else {
155         processor = std::make_shared<MouseTransformProcessor>(deviceId);
156         auto [tIter, isOk] = processors_.emplace(deviceId, processor);
157     }
158     return processor->NormalizeRotateEvent(event, type, angle);
159 }
160 
CheckAndPackageAxisEvent(libinput_event * event)161 bool MouseEventNormalize::CheckAndPackageAxisEvent(libinput_event* event)
162 {
163     CHKPF(event);
164     auto device = libinput_event_get_device(event);
165     CHKPR(device, RET_ERR);
166     int32_t deviceId = INPUT_DEV_MGR->FindInputDeviceId(device);
167     if (deviceId < 0) {
168         MMI_HILOGE("The deviceId is invalid, deviceId:%{public}d", deviceId);
169         return RET_ERR;
170     }
171     SetCurrentDeviceId(deviceId);
172     std::shared_ptr<MouseTransformProcessor> processor { nullptr };
173     if (auto it = processors_.find(deviceId); it != processors_.end()) {
174         processor = it->second;
175     }
176     CHKPF(processor);
177     return processor->CheckAndPackageAxisEvent();
178 }
179 
SetMouseScrollRows(int32_t rows)180 int32_t MouseEventNormalize::SetMouseScrollRows(int32_t rows)
181 {
182     return MouseTransformProcessor::SetMouseScrollRows(rows);
183 }
184 
GetMouseScrollRows() const185 int32_t MouseEventNormalize::GetMouseScrollRows() const
186 {
187     return MouseTransformProcessor::GetMouseScrollRows();
188 }
189 
SetMousePrimaryButton(int32_t primaryButton)190 int32_t MouseEventNormalize::SetMousePrimaryButton(int32_t primaryButton)
191 {
192     return MouseTransformProcessor::SetMousePrimaryButton(primaryButton);
193 }
194 
GetMousePrimaryButton() const195 int32_t MouseEventNormalize::GetMousePrimaryButton() const
196 {
197     return MouseTransformProcessor::GetMousePrimaryButton();
198 }
199 
SetPointerSpeed(int32_t speed)200 int32_t MouseEventNormalize::SetPointerSpeed(int32_t speed)
201 {
202     return MouseTransformProcessor::SetPointerSpeed(speed);
203 }
204 
GetPointerSpeed() const205 int32_t MouseEventNormalize::GetPointerSpeed() const
206 {
207     return MouseTransformProcessor::GetPointerSpeed();
208 }
209 
SetPointerLocation(int32_t x,int32_t y)210 int32_t MouseEventNormalize::SetPointerLocation(int32_t x, int32_t y)
211 {
212     return MouseTransformProcessor::SetPointerLocation(x, y);
213 }
214 
SetTouchpadScrollSwitch(bool switchFlag) const215 int32_t MouseEventNormalize::SetTouchpadScrollSwitch(bool switchFlag) const
216 {
217     return MouseTransformProcessor::SetTouchpadScrollSwitch(switchFlag);
218 }
219 
GetTouchpadScrollSwitch(bool & switchFlag) const220 void MouseEventNormalize::GetTouchpadScrollSwitch(bool &switchFlag) const
221 {
222     MouseTransformProcessor::GetTouchpadScrollSwitch(switchFlag);
223 }
224 
SetTouchpadScrollDirection(bool state) const225 int32_t MouseEventNormalize::SetTouchpadScrollDirection(bool state) const
226 {
227     return MouseTransformProcessor::SetTouchpadScrollDirection(state);
228 }
229 
GetTouchpadScrollDirection(bool & switchFlag) const230 void MouseEventNormalize::GetTouchpadScrollDirection(bool &switchFlag) const
231 {
232     MouseTransformProcessor::GetTouchpadScrollDirection(switchFlag);
233 }
234 
SetTouchpadTapSwitch(bool switchFlag) const235 int32_t MouseEventNormalize::SetTouchpadTapSwitch(bool switchFlag) const
236 {
237     return MouseTransformProcessor::SetTouchpadTapSwitch(switchFlag);
238 }
239 
GetTouchpadTapSwitch(bool & switchFlag) const240 void MouseEventNormalize::GetTouchpadTapSwitch(bool &switchFlag) const
241 {
242     MouseTransformProcessor::GetTouchpadTapSwitch(switchFlag);
243 }
244 
SetTouchpadPointerSpeed(int32_t speed) const245 int32_t MouseEventNormalize::SetTouchpadPointerSpeed(int32_t speed) const
246 {
247     return MouseTransformProcessor::SetTouchpadPointerSpeed(speed);
248 }
249 
GetTouchpadPointerSpeed(int32_t & speed) const250 void MouseEventNormalize::GetTouchpadPointerSpeed(int32_t &speed) const
251 {
252     MouseTransformProcessor::GetTouchpadPointerSpeed(speed);
253 }
254 
SetTouchpadRightClickType(int32_t type) const255 int32_t MouseEventNormalize::SetTouchpadRightClickType(int32_t type) const
256 {
257     return MouseTransformProcessor::SetTouchpadRightClickType(type);
258 }
259 
GetTouchpadRightClickType(int32_t & type) const260 void MouseEventNormalize::GetTouchpadRightClickType(int32_t &type) const
261 {
262     MouseTransformProcessor::GetTouchpadRightClickType(type);
263 }
264 } // namespace MMI
265 } // namespace OHOS
266