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 "input-event-codes.h"
21
22 #include "define_multimodal.h"
23 #include "event_log_helper.h"
24 #include "i_pointer_drawing_manager.h"
25 #include "input_device_manager.h"
26 #include "input_event_handler.h"
27 #include "input_windows_manager.h"
28 #include "mouse_device_state.h"
29 #include "timer_manager.h"
30 #include "util_ex.h"
31 #include "util.h"
32
33 namespace OHOS {
34 namespace MMI {
35 namespace {
36 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "MouseEventNormalize" };
37 } // namespace
MouseEventNormalize()38 MouseEventNormalize::MouseEventNormalize() {}
~MouseEventNormalize()39 MouseEventNormalize::~MouseEventNormalize() {}
40
GetProcessor(int32_t deviceId) const41 std::shared_ptr<MouseTransformProcessor> MouseEventNormalize::GetProcessor(int32_t deviceId) const
42 {
43 auto iter = processors_.find(deviceId);
44 if (iter == processors_.end()) {
45 MMI_HILOGE("Can't find mouse processor by deviceId:%{public}d", deviceId);
46 return nullptr;
47 }
48 return iter->second;
49 }
50
GetCurrentProcessor() const51 std::shared_ptr<MouseTransformProcessor> MouseEventNormalize::GetCurrentProcessor() const
52 {
53 int32_t deviceId = GetCurrentDeviceId();
54 auto iter = processors_.find(deviceId);
55 if (iter == processors_.end()) {
56 MMI_HILOGE("Can't find mouse processor by deviceId:%{public}d", deviceId);
57 return nullptr;
58 }
59 return iter->second;
60 }
61
SetCurrentDeviceId(int32_t deviceId)62 void MouseEventNormalize::SetCurrentDeviceId(int32_t deviceId)
63 {
64 currentDeviceId_ = deviceId;
65 }
66
GetCurrentDeviceId() const67 int32_t MouseEventNormalize::GetCurrentDeviceId() const
68 {
69 return currentDeviceId_;
70 }
71
GetPointerEvent()72 std::shared_ptr<PointerEvent>MouseEventNormalize::GetPointerEvent()
73 {
74 auto processor = GetCurrentProcessor();
75 CHKPP(processor);
76 return processor->GetPointerEvent();
77 }
78
OnEvent(struct libinput_event * event)79 int32_t MouseEventNormalize::OnEvent(struct libinput_event *event)
80 {
81 CHKPR(event, RET_ERR);
82 auto device = libinput_event_get_device(event);
83 CHKPR(device, RET_ERR);
84 int32_t deviceId = InputDevMgr->FindInputDeviceId(device);
85 if (deviceId < 0) {
86 MMI_HILOGE("The deviceId:%{public}d is invalid", deviceId);
87 return RET_ERR;
88 }
89 SetCurrentDeviceId(deviceId);
90 std::shared_ptr<MouseTransformProcessor>processor { nullptr };
91 if (auto it = processors_.find(deviceId); it != processors_.end()) {
92 processor = it->second;
93 } else {
94 processor = std::make_shared<MouseTransformProcessor>(deviceId);
95 auto vendorConfig = InputDevMgr->GetVendorConfig(deviceId);
96 if (vendorConfig.pointerSpeed != -1) {
97 processor->SetConfigPointerSpeed(vendorConfig.pointerSpeed);
98 }
99 auto [tIter, isOk] = processors_.emplace(deviceId, processor);
100 if (!isOk) {
101 MMI_HILOGE("Duplicate device record:%{public}d", deviceId);
102 }
103 }
104 return processor->Normalize(event);
105 }
106
107 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
OnDisplayLost(int32_t displayId)108 void MouseEventNormalize::OnDisplayLost(int32_t displayId)
109 {
110 MouseTransformProcessor::OnDisplayLost(displayId);
111 }
112
GetDisplayId() const113 int32_t MouseEventNormalize::GetDisplayId() const
114 {
115 return MouseTransformProcessor::GetDisplayId();
116 }
117
NormalizeMoveMouse(int32_t offsetX,int32_t offsetY)118 bool MouseEventNormalize::NormalizeMoveMouse(int32_t offsetX, int32_t offsetY)
119 {
120 CALL_DEBUG_ENTER;
121 auto processor = GetCurrentProcessor();
122 CHKPF(processor);
123 return processor->NormalizeMoveMouse(offsetX, offsetY);
124 }
125 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
126
Dump(int32_t fd,const std::vector<std::string> & args)127 void MouseEventNormalize::Dump(int32_t fd, const std::vector<std::string> &args)
128 {
129 auto processor = GetCurrentProcessor();
130 CHKPV(processor);
131 processor->Dump(fd, args);
132 }
133
SetMouseScrollRows(int32_t rows)134 int32_t MouseEventNormalize::SetMouseScrollRows(int32_t rows)
135 {
136 return MouseTransformProcessor::SetMouseScrollRows(rows);
137 }
138
GetMouseScrollRows() const139 int32_t MouseEventNormalize::GetMouseScrollRows() const
140 {
141 return MouseTransformProcessor::GetMouseScrollRows();
142 }
143
SetMousePrimaryButton(int32_t primaryButton)144 int32_t MouseEventNormalize::SetMousePrimaryButton(int32_t primaryButton)
145 {
146 return MouseTransformProcessor::SetMousePrimaryButton(primaryButton);
147 }
148
GetMousePrimaryButton() const149 int32_t MouseEventNormalize::GetMousePrimaryButton() const
150 {
151 return MouseTransformProcessor::GetMousePrimaryButton();
152 }
153
SetPointerSpeed(int32_t speed)154 int32_t MouseEventNormalize::SetPointerSpeed(int32_t speed)
155 {
156 return MouseTransformProcessor::SetPointerSpeed(speed);
157 }
158
GetPointerSpeed() const159 int32_t MouseEventNormalize::GetPointerSpeed() const
160 {
161 return MouseTransformProcessor::GetPointerSpeed();
162 }
163
SetPointerLocation(int32_t x,int32_t y)164 int32_t MouseEventNormalize::SetPointerLocation(int32_t x, int32_t y)
165 {
166 return MouseTransformProcessor::SetPointerLocation(x, y);
167 }
168
SetTouchpadScrollSwitch(bool switchFlag) const169 int32_t MouseEventNormalize::SetTouchpadScrollSwitch(bool switchFlag) const
170 {
171 return MouseTransformProcessor::SetTouchpadScrollSwitch(switchFlag);
172 }
173
GetTouchpadScrollSwitch(bool & switchFlag) const174 int32_t MouseEventNormalize::GetTouchpadScrollSwitch(bool &switchFlag) const
175 {
176 return MouseTransformProcessor::GetTouchpadScrollSwitch(switchFlag);
177 }
178
SetTouchpadScrollDirection(bool state) const179 int32_t MouseEventNormalize::SetTouchpadScrollDirection(bool state) const
180 {
181 return MouseTransformProcessor::SetTouchpadScrollDirection(state);
182 }
183
GetTouchpadScrollDirection(bool & switchFlag) const184 int32_t MouseEventNormalize::GetTouchpadScrollDirection(bool &switchFlag) const
185 {
186 return MouseTransformProcessor::GetTouchpadScrollDirection(switchFlag);
187 }
188
SetTouchpadTapSwitch(bool switchFlag) const189 int32_t MouseEventNormalize::SetTouchpadTapSwitch(bool switchFlag) const
190 {
191 return MouseTransformProcessor::SetTouchpadTapSwitch(switchFlag);
192 }
193
GetTouchpadTapSwitch(bool & switchFlag) const194 int32_t MouseEventNormalize::GetTouchpadTapSwitch(bool &switchFlag) const
195 {
196 return MouseTransformProcessor::GetTouchpadTapSwitch(switchFlag);
197 }
198
SetTouchpadPointerSpeed(int32_t speed) const199 int32_t MouseEventNormalize::SetTouchpadPointerSpeed(int32_t speed) const
200 {
201 return MouseTransformProcessor::SetTouchpadPointerSpeed(speed);
202 }
203
GetTouchpadPointerSpeed(int32_t & speed) const204 int32_t MouseEventNormalize::GetTouchpadPointerSpeed(int32_t &speed) const
205 {
206 return MouseTransformProcessor::GetTouchpadPointerSpeed(speed);
207 }
208
SetTouchpadRightClickType(int32_t type) const209 int32_t MouseEventNormalize::SetTouchpadRightClickType(int32_t type) const
210 {
211 return MouseTransformProcessor::SetTouchpadRightClickType(type);
212 }
213
GetTouchpadRightClickType(int32_t & type) const214 int32_t MouseEventNormalize::GetTouchpadRightClickType(int32_t &type) const
215 {
216 return MouseTransformProcessor::GetTouchpadRightClickType(type);
217 }
218 } // namespace MMI
219 } // namespace OHOS
220