• 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 #include "mouse_transform_processor.h"
17 
18 #include <cinttypes>
19 #include <chrono>
20 #include <functional>
21 #include <linux/input-event-codes.h>
22 
23 #include "define_multimodal.h"
24 #include "dfx_hisysevent.h"
25 #include "event_log_helper.h"
26 #include "i_input_windows_manager.h"
27 #include "i_pointer_drawing_manager.h"
28 #include "i_preference_manager.h"
29 #include "input_device_manager.h"
30 #include "input_event_handler.h"
31 #include "mouse_device_state.h"
32 #include "parameters.h"
33 #include "preferences.h"
34 #include "preferences_errno.h"
35 #include "preferences_helper.h"
36 #include "scene_board_judgement.h"
37 #include "timer_manager.h"
38 #include "util.h"
39 #include "util_ex.h"
40 
41 #undef MMI_LOG_DOMAIN
42 #define MMI_LOG_DOMAIN MMI_LOG_DISPATCH
43 #undef MMI_LOG_TAG
44 #define MMI_LOG_TAG "MouseTransformProcessor"
45 
46 namespace OHOS {
47 namespace MMI {
48 namespace {
49 constexpr int32_t MIN_SPEED { 1 };
50 constexpr int32_t MAX_SPEED { 11 };
51 constexpr int32_t DEFAULT_SPEED { 7 };
52 constexpr int32_t DEFAULT_TOUCHPAD_SPEED { 6 };
53 constexpr int32_t DEFAULT_ROWS { 3 };
54 constexpr int32_t MIN_ROWS { 1 };
55 constexpr int32_t MAX_ROWS { 100 };
56 constexpr int32_t BTN_RIGHT_MENUE_CODE { 0x118 };
57 constexpr int32_t RIGHT_CLICK_TYPE_MIN { 1 };
58 constexpr int32_t RIGHT_CLICK_TYPE_MAX { 3 };
59 constexpr int32_t TP_CLICK_FINGER_ONE { 1 };
60 constexpr int32_t TP_RIGHT_CLICK_FINGER_CNT { 2 };
61 constexpr int32_t HARD_PC_PRO_DEVICE_WIDTH { 2880 };
62 constexpr int32_t HARD_PC_PRO_DEVICE_HEIGHT { 1920 };
63 constexpr int32_t SOFT_PC_PRO_DEVICE_WIDTH { 3120 };
64 constexpr int32_t SOFT_PC_PRO_DEVICE_HEIGHT { 2080 };
65 constexpr int32_t TABLET_DEVICE_WIDTH { 2880 };
66 constexpr int32_t TABLET_DEVICE_HEIGHT { 1920 };
67 const std::string DEVICE_TYPE_TABLET { "WEB" };
68 const std::string DEVICE_TYPE_PCE { "PCEL" };
69 const std::string DEVICE_TYPE_PC_PRO { "HAD" };
70 const std::string PRODUCT_TYPE = OHOS::system::GetParameter("const.build.product", "HYM");
71 const std::string MOUSE_FILE_NAME = "mouse_settings.xml";
72 const int32_t ROTATE_POLICY = system::GetIntParameter("const.window.device.rotate_policy", 0);
73 const std::string FOLDABLE_DEVICE_POLICY = system::GetParameter("const.window.foldabledevice.rotate_policy", "");
74 constexpr int32_t WINDOW_ROTATE { 0 };
75 constexpr char ROTATE_WINDOW_ROTATE { '0' };
76 constexpr int32_t FOLDABLE_DEVICE { 2 };
77 constexpr int32_t WAIT_TIME_FOR_BUTTON_UP { 35 };
78 } // namespace
79 
80 int32_t MouseTransformProcessor::globalPointerSpeed_ = DEFAULT_SPEED;
81 
MouseTransformProcessor(int32_t deviceId)82 MouseTransformProcessor::MouseTransformProcessor(int32_t deviceId)
83     : pointerEvent_(PointerEvent::Create()), deviceId_(deviceId)
84 {
85     globalPointerSpeed_ = GetPointerSpeed();
86 }
87 
GetPointerEvent() const88 std::shared_ptr<PointerEvent> MouseTransformProcessor::GetPointerEvent() const
89 {
90     return pointerEvent_;
91 }
92 
HandleMotionInner(struct libinput_event_pointer * data,struct libinput_event * event)93 int32_t MouseTransformProcessor::HandleMotionInner(struct libinput_event_pointer* data, struct libinput_event* event)
94 {
95     CALL_DEBUG_ENTER;
96     CHKPR(data, ERROR_NULL_POINTER);
97     CHKPR(pointerEvent_, ERROR_NULL_POINTER);
98     pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
99     pointerEvent_->SetButtonId(buttonId_);
100 
101     CursorPosition cursorPos = WIN_MGR->GetCursorPos();
102     if (cursorPos.displayId < 0) {
103         MMI_HILOGE("No display");
104         return RET_ERR;
105     }
106     unaccelerated_.dx = libinput_event_pointer_get_dx_unaccelerated(data);
107     unaccelerated_.dy = libinput_event_pointer_get_dy_unaccelerated(data);
108 
109     Offset offset { unaccelerated_.dx, unaccelerated_.dy };
110     auto displayInfo = WIN_MGR->GetPhysicalDisplay(cursorPos.displayId);
111     CHKPR(displayInfo, ERROR_NULL_POINTER);
112 #ifndef OHOS_BUILD_EMULATOR
113     if (IsWindowRotation(displayInfo) && Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
114         CalculateOffset(displayInfo->direction, offset);
115     }
116 #endif // OHOS_BUILD_EMULATOR
117     const int32_t type = libinput_event_get_type(event);
118     int32_t ret = RET_ERR;
119     DeviceType deviceType = CheckDeviceType(displayInfo->width, displayInfo->height);
120     if (type == LIBINPUT_EVENT_POINTER_MOTION_TOUCHPAD) {
121         pointerEvent_->AddFlag(InputEvent::EVENT_FLAG_TOUCHPAD_POINTER);
122         ret = HandleMotionAccelerateTouchpad(&offset, WIN_MGR->GetMouseIsCaptureMode(),
123             &cursorPos.cursorPos.x, &cursorPos.cursorPos.y, GetTouchpadSpeed(), static_cast<int32_t>(deviceType));
124     } else {
125         pointerEvent_->ClearFlag(InputEvent::EVENT_FLAG_TOUCHPAD_POINTER);
126         ret = HandleMotionAccelerateMouse(&offset, WIN_MGR->GetMouseIsCaptureMode(),
127             &cursorPos.cursorPos.x, &cursorPos.cursorPos.y, globalPointerSpeed_, static_cast<int32_t>(deviceType));
128     }
129     if (ret != RET_OK) {
130         MMI_HILOGE("Failed to handle motion correction");
131         return ret;
132     }
133 #ifdef OHOS_BUILD_EMULATOR
134     cursorPos.cursorPos.x = offset.dx;
135     cursorPos.cursorPos.y = offset.dy;
136 #endif // OHOS_BUILD_EMULATOR
137     WIN_MGR->UpdateAndAdjustMouseLocation(cursorPos.displayId, cursorPos.cursorPos.x, cursorPos.cursorPos.y);
138     pointerEvent_->SetTargetDisplayId(cursorPos.displayId);
139     MMI_HILOGD("Change coordinate: x:%.2f, y:%.2f, currentDisplayId:%d",
140         cursorPos.cursorPos.x, cursorPos.cursorPos.y, cursorPos.displayId);
141     return RET_OK;
142 }
143 
IsWindowRotation(const DisplayInfo * displayInfo)144 bool MouseTransformProcessor::IsWindowRotation(const DisplayInfo* displayInfo)
145 {
146     MMI_HILOGD("ROTATE_POLICY: %{public}d, FOLDABLE_DEVICE_POLICY:%{public}s",
147         ROTATE_POLICY, FOLDABLE_DEVICE_POLICY.c_str());
148     return (ROTATE_POLICY == WINDOW_ROTATE ||
149         (ROTATE_POLICY == FOLDABLE_DEVICE &&
150         ((displayInfo->displayMode == DisplayMode::MAIN &&
151         FOLDABLE_DEVICE_POLICY[0] == ROTATE_WINDOW_ROTATE) ||
152         (displayInfo->displayMode == DisplayMode::FULL &&
153         FOLDABLE_DEVICE_POLICY[FOLDABLE_DEVICE] == ROTATE_WINDOW_ROTATE))));
154 }
155 
CalculateOffset(Direction direction,Offset & offset)156 void MouseTransformProcessor::CalculateOffset(Direction direction, Offset &offset)
157 {
158     std::negate<double> neg;
159     if (direction == DIRECTION90) {
160         double tmp = offset.dx;
161         offset.dx = offset.dy;
162         offset.dy = neg(tmp);
163     } else if (direction == DIRECTION180) {
164         offset.dx = neg(offset.dx);
165         offset.dy = neg(offset.dy);
166     } else if (direction == DIRECTION270) {
167         double tmp = offset.dx;
168         offset.dx = neg(offset.dy);
169         offset.dy = tmp;
170     }
171 }
172 
HandleButtonInner(struct libinput_event_pointer * data,struct libinput_event * event)173 int32_t MouseTransformProcessor::HandleButtonInner(struct libinput_event_pointer* data, struct libinput_event* event)
174 {
175     CALL_DEBUG_ENTER;
176     CHKPR(data, ERROR_NULL_POINTER);
177     CHKPR(event, ERROR_NULL_POINTER);
178     CHKPR(pointerEvent_, ERROR_NULL_POINTER);
179     MMI_HILOGD("Current action:%{public}d", pointerEvent_->GetPointerAction());
180 
181     uint32_t button = libinput_event_pointer_get_button(data);
182     uint32_t originButton = button;
183     const int32_t type = libinput_event_get_type(event);
184     bool tpTapSwitch = true;
185     GetTouchpadTapSwitch(tpTapSwitch);
186 
187     // touch pad tap switch is disable
188     if (type == LIBINPUT_EVENT_POINTER_TAP && !tpTapSwitch) {
189         MMI_HILOGD("Touch pad is disable");
190         return RET_ERR;
191     }
192 
193     TransTouchpadRightButton(data, type, button);
194 
195     if (button == MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_MIDDLE_BUTTON_CODE &&
196         type == LIBINPUT_EVENT_POINTER_BUTTON_TOUCHPAD) {
197         button = MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_LEFT_BUTTON_CODE;
198     }
199 
200     auto ret = HandleButtonValueInner(data, button, type);
201     if (ret != RET_OK) {
202         MMI_HILOGE("The button value does not exist");
203         return RET_ERR;
204     }
205 
206     auto state = libinput_event_pointer_get_button_state(data);
207     if (state == LIBINPUT_BUTTON_STATE_RELEASED) {
208         int32_t switchTypeData = RIGHT_CLICK_TYPE_MIN;
209         GetTouchpadRightClickType(switchTypeData);
210         RightClickType switchType = RightClickType(switchTypeData);
211         if (type == LIBINPUT_EVENT_POINTER_TAP && switchType == RightClickType::TP_TWO_FINGER_TAP &&
212             button == MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_RIGHT_BUTTON_CODE) {
213             MMI_HILOGI("Right click up, do sleep");
214             std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_FOR_BUTTON_UP));
215         }
216         MouseState->MouseBtnStateCounts(button, BUTTON_STATE_RELEASED);
217         pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_UP);
218         int32_t buttonId = MouseState->LibinputChangeToPointer(button);
219         pointerEvent_->DeleteReleaseButton(buttonId);
220         DeletePressedButton(originButton);
221         isPressed_ = false;
222         buttonId_ = PointerEvent::BUTTON_NONE;
223     } else if (state == LIBINPUT_BUTTON_STATE_PRESSED) {
224         MouseState->MouseBtnStateCounts(button, BUTTON_STATE_PRESSED);
225         pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_BUTTON_DOWN);
226         int32_t buttonId = MouseState->LibinputChangeToPointer(button);
227         pointerEvent_->SetButtonPressed(buttonId);
228         buttonMapping_[originButton] = buttonId;
229         isPressed_ = true;
230         buttonId_ = pointerEvent_->GetButtonId();
231         CursorPosition cursorPos = WIN_MGR->GetCursorPos();
232         if (cursorPos.displayId < 0) {
233             MMI_HILOGE("No display");
234             return RET_ERR;
235         }
236         auto displayInfo = WIN_MGR->GetPhysicalDisplay(cursorPos.displayId);
237         CHKPR(displayInfo, ERROR_NULL_POINTER);
238         if (cursorPos.direction != displayInfo->direction) {
239             WIN_MGR->UpdateAndAdjustMouseLocation(cursorPos.displayId, cursorPos.cursorPos.x, cursorPos.cursorPos.y);
240         }
241     } else {
242         MMI_HILOGE("Unknown state, state:%{public}u", state);
243         return RET_ERR;
244     }
245     return RET_OK;
246 }
247 
DeletePressedButton(uint32_t originButton)248 void MouseTransformProcessor::DeletePressedButton(uint32_t originButton)
249 {
250     auto iter = buttonMapping_.find(originButton);
251     if (iter != buttonMapping_.end()) {
252         pointerEvent_->DeleteReleaseButton(iter->second);
253         buttonMapping_.erase(iter);
254     }
255 }
256 
HandleButtonValueInner(struct libinput_event_pointer * data,uint32_t button,int32_t type)257 int32_t MouseTransformProcessor::HandleButtonValueInner(struct libinput_event_pointer *data, uint32_t button,
258     int32_t type)
259 {
260     CALL_DEBUG_ENTER;
261     CHKPR(data, ERROR_NULL_POINTER);
262     CHKPR(pointerEvent_, ERROR_NULL_POINTER);
263     int32_t buttonId = MouseState->LibinputChangeToPointer(button);
264     if (buttonId == PointerEvent::BUTTON_NONE) {
265         MMI_HILOGE("Unknown btn, btn:%{public}u", button);
266         return RET_ERR;
267     }
268 
269     std::string name = "primaryButton";
270     int32_t primaryButton = PREFERENCES_MGR->GetIntValue(name, 0);
271     MMI_HILOGD("Set mouse primary button:%{public}d", primaryButton);
272     if (type == LIBINPUT_EVENT_POINTER_BUTTON && primaryButton == RIGHT_BUTTON) {
273         if (buttonId == PointerEvent::MOUSE_BUTTON_LEFT) {
274             buttonId = PointerEvent::MOUSE_BUTTON_RIGHT;
275         } else if (buttonId == PointerEvent::MOUSE_BUTTON_RIGHT) {
276             buttonId = PointerEvent::MOUSE_BUTTON_LEFT;
277         } else {
278             MMI_HILOGD("buttonId does not switch");
279         }
280     }
281 
282     pointerEvent_->SetButtonId(buttonId);
283     return RET_OK;
284 }
285 
SetMouseScrollRows(int32_t rows)286 int32_t MouseTransformProcessor::SetMouseScrollRows(int32_t rows)
287 {
288     CALL_DEBUG_ENTER;
289     if (rows < MIN_ROWS) {
290         rows = MIN_ROWS;
291     } else if (rows > MAX_ROWS) {
292         rows = MAX_ROWS;
293     }
294     std::string name = "rows";
295     int32_t ret = PREFERENCES_MGR->SetIntValue(name, MOUSE_FILE_NAME, rows);
296     if (ret != RET_OK) {
297         MMI_HILOGE("Set mouse scroll rows failed, code:%{public}d", ret);
298         return ret;
299     }
300     MMI_HILOGD("Set mouse scroll rows successfully, rows:%{public}d", rows);
301     return RET_OK;
302 }
303 
GetMouseScrollRows()304 int32_t MouseTransformProcessor::GetMouseScrollRows()
305 {
306     CALL_DEBUG_ENTER;
307     std::string name = "rows";
308     int32_t rows = PREFERENCES_MGR->GetIntValue(name, DEFAULT_ROWS);
309     MMI_HILOGD("Get mouse scroll rows successfully, rows:%{public}d", rows);
310     return rows;
311 }
312 
HandleTouchPadAxisState(libinput_pointer_axis_source source,int32_t & direction,bool & tpScrollSwitch)313 void MouseTransformProcessor::HandleTouchPadAxisState(libinput_pointer_axis_source source,
314     int32_t& direction, bool& tpScrollSwitch)
315 {
316     bool scrollDirectionState = true;
317     GetTouchpadScrollSwitch(tpScrollSwitch);
318     GetTouchpadScrollDirection(scrollDirectionState);
319     if (scrollDirectionState == true && source == LIBINPUT_POINTER_AXIS_SOURCE_FINGER) {
320         direction = -1;
321     }
322 }
323 
HandleAxisInner(struct libinput_event_pointer * data)324 int32_t MouseTransformProcessor::HandleAxisInner(struct libinput_event_pointer* data)
325 {
326     CALL_DEBUG_ENTER;
327     CHKPR(data, ERROR_NULL_POINTER);
328     CHKPR(pointerEvent_, ERROR_NULL_POINTER);
329 
330     bool tpScrollSwitch = true;
331     int32_t tpScrollDirection = 1;
332 
333     libinput_pointer_axis_source source = libinput_event_pointer_get_axis_source(data);
334     HandleTouchPadAxisState(source, tpScrollDirection, tpScrollSwitch);
335     if (!tpScrollSwitch && source == LIBINPUT_POINTER_AXIS_SOURCE_FINGER) {
336         MMI_HILOGD("TouchPad axis event is disable");
337         return RET_ERR;
338     }
339 
340     if (buttonId_ == PointerEvent::BUTTON_NONE && pointerEvent_->GetButtonId() != PointerEvent::BUTTON_NONE) {
341         pointerEvent_->SetButtonId(PointerEvent::BUTTON_NONE);
342     }
343     if (libinput_event_pointer_get_axis_source(data) == LIBINPUT_POINTER_AXIS_SOURCE_FINGER) {
344         MMI_HILOGD("Libinput event axis source type is finger");
345         if (!isAxisBegin_) {
346             return RET_ERR;
347         }
348         pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
349         pointerEvent_->SetAxisEventType(PointerEvent::AXIS_EVENT_TYPE_SCROLL);
350     } else {
351         if (TimerMgr->IsExist(timerId_)) {
352             pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
353             pointerEvent_->SetAxisEventType(PointerEvent::AXIS_EVENT_TYPE_SCROLL);
354             TimerMgr->ResetTimer(timerId_);
355             MMI_HILOGD("Axis update");
356         } else {
357             static constexpr int32_t timeout = 100;
358             std::weak_ptr<MouseTransformProcessor> weakPtr = shared_from_this();
359             timerId_ = TimerMgr->AddTimer(timeout, 1, [weakPtr]() {
360                 CALL_DEBUG_ENTER;
361                 auto sharedPtr = weakPtr.lock();
362                 CHKPV(sharedPtr);
363                 MMI_HILOGI("Timer:%{public}d", sharedPtr->timerId_);
364                 sharedPtr->timerId_ = -1;
365                 auto pointerEvent = sharedPtr->GetPointerEvent();
366                 CHKPV(pointerEvent);
367                 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_END);
368                 pointerEvent->SetAxisEventType(PointerEvent::AXIS_EVENT_TYPE_SCROLL);
369                 pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, 0);
370                 pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, 0);
371                 pointerEvent->UpdateId();
372                 LogTracer lt(pointerEvent->GetId(), pointerEvent->GetEventType(), pointerEvent->GetPointerAction());
373                 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
374                 CHKPV(inputEventNormalizeHandler);
375                 inputEventNormalizeHandler->HandlePointerEvent(pointerEvent);
376             });
377 
378             pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_BEGIN);
379             pointerEvent_->SetAxisEventType(PointerEvent::AXIS_EVENT_TYPE_SCROLL);
380             MMI_HILOGI("Axis begin");
381         }
382     }
383 
384     const int32_t initRows = 3;
385     if (libinput_event_pointer_has_axis(data, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
386         double axisValue = libinput_event_pointer_get_axis_value(data, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
387         if (source == LIBINPUT_POINTER_AXIS_SOURCE_FINGER) {
388             axisValue = HandleAxisAccelateTouchPad(axisValue) * tpScrollDirection;
389         } else {
390             axisValue = GetMouseScrollRows() * axisValue * tpScrollDirection;
391         }
392         pointerEvent_->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, axisValue);
393     }
394     if (libinput_event_pointer_has_axis(data, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
395         double axisValue = libinput_event_pointer_get_axis_value(data, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
396         if (source == LIBINPUT_POINTER_AXIS_SOURCE_FINGER) {
397             axisValue = HandleAxisAccelateTouchPad(axisValue) * tpScrollDirection;
398         } else {
399             axisValue = GetMouseScrollRows() * axisValue * tpScrollDirection;
400         }
401         pointerEvent_->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, axisValue);
402     }
403     return RET_OK;
404 }
405 
HandleAxisAccelateTouchPad(double axisValue)406 double MouseTransformProcessor::HandleAxisAccelateTouchPad(double axisValue)
407 {
408     const int32_t initRows = 3;
409     DeviceType deviceType = DeviceType::DEVICE_PC;
410     if (PRODUCT_TYPE == DEVICE_TYPE_PC_PRO) {
411         deviceType = DeviceType::DEVICE_SOFT_PC_PRO;
412     }
413     int32_t ret =
414         HandleAxisAccelerateTouchpad(WIN_MGR->GetMouseIsCaptureMode(), &axisValue, static_cast<int32_t>(deviceType));
415     if (ret != RET_OK) {
416         MMI_HILOGW("Fail accelerate axis");
417         axisValue = GetMouseScrollRows() * (axisValue / initRows);
418     }
419     return axisValue;
420 }
421 
HandleAxisBeginEndInner(struct libinput_event * event)422 int32_t MouseTransformProcessor::HandleAxisBeginEndInner(struct libinput_event *event)
423 {
424     CALL_DEBUG_ENTER;
425     CHKPR(event, ERROR_NULL_POINTER);
426     CHKPR(pointerEvent_, ERROR_NULL_POINTER);
427     if (buttonId_ == PointerEvent::BUTTON_NONE && pointerEvent_->GetButtonId() != PointerEvent::BUTTON_NONE) {
428         pointerEvent_->SetButtonId(PointerEvent::BUTTON_NONE);
429     }
430     if (!isAxisBegin_ && isPressed_) {
431         MMI_HILOGE("Axis is invalid");
432         return RET_ERR;
433     }
434     if (isAxisBegin_ && isPressed_) {
435         pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_END);
436         pointerEvent_->SetAxisEventType(PointerEvent::AXIS_EVENT_TYPE_SCROLL);
437         isAxisBegin_ = false;
438         MMI_HILOGD("Axis end due to a pressed event");
439         return RET_OK;
440     }
441     if (libinput_event_get_type(event) == LIBINPUT_EVENT_TOUCHPAD_DOWN && !isPressed_) {
442         pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_BEGIN);
443         pointerEvent_->SetAxisEventType(PointerEvent::AXIS_EVENT_TYPE_SCROLL);
444         isAxisBegin_ = true;
445         MMI_HILOGD("Axis begin");
446         return RET_OK;
447     }
448     if (libinput_event_get_type(event) == LIBINPUT_EVENT_TOUCHPAD_UP && !isPressed_) {
449         pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_END);
450         pointerEvent_->SetAxisEventType(PointerEvent::AXIS_EVENT_TYPE_SCROLL);
451         isAxisBegin_ = false;
452         MMI_HILOGD("Axis end");
453         return RET_OK;
454     }
455     MMI_HILOGE("Axis is invalid");
456     return RET_ERR;
457 }
458 
HandleAxisPostInner(PointerEvent::PointerItem & pointerItem)459 void MouseTransformProcessor::HandleAxisPostInner(PointerEvent::PointerItem &pointerItem)
460 {
461     CALL_DEBUG_ENTER;
462     CHKPV(pointerEvent_);
463     auto mouseInfo = WIN_MGR->GetMouseInfo();
464     MouseState->SetMouseCoords(mouseInfo.physicalX, mouseInfo.physicalY);
465     pointerItem.SetDisplayX(mouseInfo.physicalX);
466     pointerItem.SetDisplayY(mouseInfo.physicalY);
467     pointerItem.SetWindowX(0);
468     pointerItem.SetWindowY(0);
469     pointerItem.SetPointerId(0);
470     pointerItem.SetPressed(isPressed_);
471     int64_t time = GetSysClockTime();
472     pointerItem.SetDownTime(time);
473     pointerItem.SetWidth(0);
474     pointerItem.SetHeight(0);
475     pointerItem.SetPressure(0);
476     pointerItem.SetToolType(PointerEvent::TOOL_TYPE_TOUCHPAD);
477     pointerItem.SetDeviceId(deviceId_);
478     pointerItem.SetRawDx(0);
479     pointerItem.SetRawDy(0);
480     pointerEvent_->UpdateId();
481     StartLogTraceId(pointerEvent_->GetId(), pointerEvent_->GetEventType(), pointerEvent_->GetPointerAction());
482     pointerEvent_->UpdatePointerItem(pointerEvent_->GetPointerId(), pointerItem);
483     pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
484     pointerEvent_->SetActionTime(time);
485     pointerEvent_->SetActionStartTime(time);
486     pointerEvent_->SetPointerId(0);
487     pointerEvent_->SetDeviceId(deviceId_);
488     pointerEvent_->SetTargetDisplayId(mouseInfo.displayId);
489     pointerEvent_->SetTargetWindowId(-1);
490     pointerEvent_->SetAgentWindowId(-1);
491 }
492 
HandlePostInner(struct libinput_event_pointer * data,PointerEvent::PointerItem & pointerItem)493 bool MouseTransformProcessor::HandlePostInner(struct libinput_event_pointer* data,
494     PointerEvent::PointerItem &pointerItem)
495 {
496     CALL_DEBUG_ENTER;
497     CHKPF(pointerEvent_);
498     auto mouseInfo = WIN_MGR->GetMouseInfo();
499     MouseState->SetMouseCoords(mouseInfo.physicalX, mouseInfo.physicalY);
500     pointerItem.SetDisplayX(mouseInfo.physicalX);
501     pointerItem.SetDisplayY(mouseInfo.physicalY);
502     pointerItem.SetWindowX(0);
503     pointerItem.SetWindowY(0);
504     pointerItem.SetPointerId(0);
505     pointerItem.SetPressed(isPressed_);
506 
507     int64_t time = GetSysClockTime();
508     pointerItem.SetDownTime(time);
509     pointerItem.SetWidth(0);
510     pointerItem.SetHeight(0);
511     pointerItem.SetPressure(0);
512     pointerItem.SetDeviceId(deviceId_);
513     pointerItem.SetRawDx(static_cast<int32_t>(unaccelerated_.dx));
514     pointerItem.SetRawDy(static_cast<int32_t>(unaccelerated_.dy));
515 
516     pointerEvent_->UpdateId();
517     StartLogTraceId(pointerEvent_->GetId(), pointerEvent_->GetEventType(), pointerEvent_->GetPointerAction());
518     pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
519     pointerEvent_->SetActionTime(time);
520     pointerEvent_->SetActionStartTime(time);
521     pointerEvent_->SetDeviceId(deviceId_);
522     pointerEvent_->SetPointerId(0);
523     pointerEvent_->SetTargetDisplayId(mouseInfo.displayId);
524     pointerEvent_->SetTargetWindowId(-1);
525     pointerEvent_->SetAgentWindowId(-1);
526     if (data == nullptr) {
527         pointerEvent_->UpdatePointerItem(pointerEvent_->GetPointerId(), pointerItem);
528         return false;
529     }
530     if (libinput_event_pointer_get_axis_source(data) == LIBINPUT_POINTER_AXIS_SOURCE_FINGER) {
531         pointerItem.SetToolType(PointerEvent::TOOL_TYPE_TOUCHPAD);
532         MMI_HILOGD("ToolType is touchpad");
533     } else {
534         pointerItem.SetToolType(PointerEvent::TOOL_TYPE_MOUSE);
535     }
536     pointerEvent_->UpdatePointerItem(pointerEvent_->GetPointerId(), pointerItem);
537     return true;
538 }
539 
CheckAndPackageAxisEvent()540 bool MouseTransformProcessor::CheckAndPackageAxisEvent()
541 {
542     CALL_INFO_TRACE;
543     if (!isAxisBegin_) {
544         return false;
545     }
546     pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_END);
547     isAxisBegin_ = false;
548     PointerEvent::PointerItem item;
549     HandleAxisPostInner(item);
550     WIN_MGR->UpdateTargetPointer(pointerEvent_);
551     return true;
552 }
553 
Normalize(struct libinput_event * event)554 int32_t MouseTransformProcessor::Normalize(struct libinput_event *event)
555 {
556     CALL_DEBUG_ENTER;
557     CHKPR(event, ERROR_NULL_POINTER);
558     CHKPR(pointerEvent_, ERROR_NULL_POINTER);
559     const int32_t type = libinput_event_get_type(event);
560     auto data = libinput_event_get_pointer_event(event);
561     if (type != LIBINPUT_EVENT_TOUCHPAD_DOWN && type != LIBINPUT_EVENT_TOUCHPAD_UP) {
562         CHKPR(data, ERROR_NULL_POINTER);
563     }
564     pointerEvent_->ClearAxisValue();
565     int32_t result;
566     switch (type) {
567         case LIBINPUT_EVENT_POINTER_MOTION:
568         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
569         case LIBINPUT_EVENT_POINTER_MOTION_TOUCHPAD:
570             result = HandleMotionInner(data, event);
571             break;
572         case LIBINPUT_EVENT_POINTER_TAP:
573         case LIBINPUT_EVENT_POINTER_BUTTON:
574         case LIBINPUT_EVENT_POINTER_BUTTON_TOUCHPAD:
575             result = HandleButtonInner(data, event);
576             break;
577         case LIBINPUT_EVENT_POINTER_AXIS:
578             result = HandleAxisInner(data);
579             break;
580         case LIBINPUT_EVENT_TOUCHPAD_DOWN:
581         case LIBINPUT_EVENT_TOUCHPAD_UP:
582             result = HandleAxisBeginEndInner(event);
583             break;
584         default:
585             MMI_HILOGE("Unknown type:%{public}d", type);
586             return RET_ERR;
587     }
588     if (result == RET_ERR) {
589         return result;
590     }
591     PointerEvent::PointerItem pointerItem;
592     if (type == LIBINPUT_EVENT_TOUCHPAD_DOWN || type == LIBINPUT_EVENT_TOUCHPAD_UP) {
593         HandleAxisPostInner(pointerItem);
594     } else if (!HandlePostInner(data, pointerItem)) {
595         CHKPL(pointerEvent_);
596         return RET_ERR;
597     }
598     WIN_MGR->UpdateTargetPointer(pointerEvent_);
599     DumpInner();
600     return result;
601 }
602 
NormalizeRotateEvent(struct libinput_event * event,int32_t type,double angle)603 int32_t MouseTransformProcessor::NormalizeRotateEvent(struct libinput_event *event, int32_t type, double angle)
604 {
605     CALL_DEBUG_ENTER;
606     CHKPR(event, ERROR_NULL_POINTER);
607     CHKPR(pointerEvent_, ERROR_NULL_POINTER);
608     auto data = libinput_event_get_pointer_event(event);
609     pointerEvent_->SetPointerAction(type);
610     pointerEvent_->ClearAxisValue();
611     pointerEvent_->SetAxisValue(PointerEvent::AXIS_TYPE_ROTATE, angle);
612     PointerEvent::PointerItem pointerItem;
613     pointerItem.SetToolType(PointerEvent::TOOL_TYPE_TOUCHPAD);
614     if (!HandlePostInner(data, pointerItem)) {
615         WIN_MGR->UpdateTargetPointer(pointerEvent_);
616         DumpInner();
617         return ERROR_NULL_POINTER;
618     }
619     WIN_MGR->UpdateTargetPointer(pointerEvent_);
620     DumpInner();
621     return RET_OK;
622 }
623 
624 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
HandleMotionMoveMouse(int32_t offsetX,int32_t offsetY)625 void MouseTransformProcessor::HandleMotionMoveMouse(int32_t offsetX, int32_t offsetY)
626 {
627     CALL_DEBUG_ENTER;
628     CHKPV(pointerEvent_);
629     pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
630     CursorPosition cursorPos = WIN_MGR->GetCursorPos();
631     cursorPos.cursorPos.x += offsetX;
632     cursorPos.cursorPos.y += offsetY;
633     WIN_MGR->UpdateAndAdjustMouseLocation(cursorPos.displayId, cursorPos.cursorPos.x, cursorPos.cursorPos.y);
634 }
635 
OnDisplayLost(int32_t displayId)636 void MouseTransformProcessor::OnDisplayLost(int32_t displayId)
637 {
638     CursorPosition cursorPos = WIN_MGR->GetCursorPos();
639     if (cursorPos.displayId != displayId) {
640         cursorPos = WIN_MGR->ResetCursorPos();
641         WIN_MGR->UpdateAndAdjustMouseLocation(cursorPos.displayId, cursorPos.cursorPos.x, cursorPos.cursorPos.y);
642     }
643 }
644 
GetDisplayId()645 int32_t MouseTransformProcessor::GetDisplayId()
646 {
647     return WIN_MGR->GetCursorPos().displayId;
648 }
649 
HandlePostMoveMouse(PointerEvent::PointerItem & pointerItem)650 void MouseTransformProcessor::HandlePostMoveMouse(PointerEvent::PointerItem& pointerItem)
651 {
652     CALL_DEBUG_ENTER;
653     auto mouseInfo = WIN_MGR->GetMouseInfo();
654     CHKPV(pointerEvent_);
655     MouseState->SetMouseCoords(mouseInfo.physicalX, mouseInfo.physicalY);
656     pointerItem.SetDisplayX(mouseInfo.physicalX);
657     pointerItem.SetDisplayY(mouseInfo.physicalY);
658     pointerItem.SetWindowX(0);
659     pointerItem.SetWindowY(0);
660     pointerItem.SetPointerId(0);
661     pointerItem.SetPressed(isPressed_);
662 
663     int64_t time = GetSysClockTime();
664     pointerItem.SetDownTime(time);
665     pointerItem.SetWidth(0);
666     pointerItem.SetHeight(0);
667     pointerItem.SetPressure(0);
668 
669     pointerEvent_->UpdateId();
670     StartLogTraceId(pointerEvent_->GetId(), pointerEvent_->GetEventType(), pointerEvent_->GetPointerAction());
671     pointerEvent_->UpdatePointerItem(pointerEvent_->GetPointerId(), pointerItem);
672     pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
673     pointerEvent_->SetActionTime(time);
674     pointerEvent_->SetActionStartTime(time);
675 
676     pointerEvent_->SetPointerId(0);
677     pointerEvent_->SetTargetDisplayId(-1);
678     pointerEvent_->SetTargetWindowId(-1);
679     pointerEvent_->SetAgentWindowId(-1);
680 }
681 
NormalizeMoveMouse(int32_t offsetX,int32_t offsetY)682 bool MouseTransformProcessor::NormalizeMoveMouse(int32_t offsetX, int32_t offsetY)
683 {
684     CALL_DEBUG_ENTER;
685     CHKPF(pointerEvent_);
686     bool bHasPointerDevice = INPUT_DEV_MGR->HasPointerDevice();
687     if (!bHasPointerDevice) {
688         MMI_HILOGE("There hasn't any pointer device");
689         return false;
690     }
691 
692     PointerEvent::PointerItem pointerItem;
693     HandleMotionMoveMouse(offsetX, offsetY);
694     HandlePostMoveMouse(pointerItem);
695     DumpInner();
696     return bHasPointerDevice;
697 }
698 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
699 
DumpInner()700 void MouseTransformProcessor::DumpInner()
701 {
702     EventLogHelper::PrintEventData(pointerEvent_, MMI_LOG_FREEZE);
703     auto device = INPUT_DEV_MGR->GetInputDevice(pointerEvent_->GetDeviceId());
704     CHKPV(device);
705     aggregator_.Record(MMI_LOG_FREEZE, device->GetName() + ", TW: " +
706         std::to_string(pointerEvent_->GetTargetWindowId()), std::to_string(pointerEvent_->GetId()));
707 }
708 
CheckDeviceType(int32_t width,int32_t height)709 DeviceType MouseTransformProcessor::CheckDeviceType(int32_t width, int32_t height)
710 {
711     CALL_DEBUG_ENTER;
712     DeviceType ret = DeviceType::DEVICE_PC;
713     if (PRODUCT_TYPE == DEVICE_TYPE_PC_PRO) {
714         if (width == HARD_PC_PRO_DEVICE_WIDTH && height == HARD_PC_PRO_DEVICE_HEIGHT) {
715             ret = DeviceType::DEVICE_HARD_PC_PRO;
716         } else if (width == SOFT_PC_PRO_DEVICE_WIDTH && height == SOFT_PC_PRO_DEVICE_HEIGHT) {
717             ret = DeviceType::DEVICE_SOFT_PC_PRO;
718         } else if (EventLogHelper::IsBetaVersion()) {
719             MMI_HILOGE("Undefined width:%{private}d, height:%{private}d", width, height);
720         }
721         MMI_HILOGD("Device width:%{private}d, height:%{private}d", width, height);
722     }
723     if (PRODUCT_TYPE == DEVICE_TYPE_TABLET || PRODUCT_TYPE == DEVICE_TYPE_PCE) {
724         if (width == TABLET_DEVICE_WIDTH && height == TABLET_DEVICE_HEIGHT) {
725             ret = DeviceType::DEVICE_TABLET;
726         }
727     }
728     return ret;
729 }
730 
Dump(int32_t fd,const std::vector<std::string> & args)731 void MouseTransformProcessor::Dump(int32_t fd, const std::vector<std::string> &args)
732 {
733     CALL_DEBUG_ENTER;
734     PointerEvent::PointerItem item;
735     CHKPV(pointerEvent_);
736     pointerEvent_->GetPointerItem(pointerEvent_->GetPointerId(), item);
737     mprintf(fd, "Mouse device state information:\t");
738     mprintf(fd,
739             "PointerId:%d | SourceType:%s | PointerAction:%s | WindowX:%d | WindowY:%d | ButtonId:%d "
740             "| AgentWindowId:%d | TargetWindowId:%d | DownTime:%" PRId64 " | IsPressed:%s \t",
741             pointerEvent_->GetPointerId(), pointerEvent_->DumpSourceType(), pointerEvent_->DumpPointerAction(),
742             item.GetWindowX(), item.GetWindowY(), pointerEvent_->GetButtonId(), pointerEvent_->GetAgentWindowId(),
743             pointerEvent_->GetTargetWindowId(), item.GetDownTime(), item.IsPressed() ? "true" : "false");
744 }
745 
SetMousePrimaryButton(int32_t primaryButton)746 int32_t MouseTransformProcessor::SetMousePrimaryButton(int32_t primaryButton)
747 {
748     CALL_DEBUG_ENTER;
749     MMI_HILOGD("Set mouse primary button:%{public}d", primaryButton);
750     std::string name = "primaryButton";
751     int32_t ret = PREFERENCES_MGR->SetIntValue(name, MOUSE_FILE_NAME, primaryButton);
752     if (ret != RET_OK) {
753         MMI_HILOGE("Set mouse primary button failed, code:%{public}d", ret);
754         return ret;
755     }
756     return RET_OK;
757 }
758 
GetMousePrimaryButton()759 int32_t MouseTransformProcessor::GetMousePrimaryButton()
760 {
761     CALL_DEBUG_ENTER;
762     std::string name = "primaryButton";
763     int32_t primaryButton = PREFERENCES_MGR->GetIntValue(name, 0);
764     MMI_HILOGD("Set mouse primary button:%{public}d", primaryButton);
765     return primaryButton;
766 }
767 
SetPointerSpeed(int32_t speed)768 int32_t MouseTransformProcessor::SetPointerSpeed(int32_t speed)
769 {
770     CALL_DEBUG_ENTER;
771     if (speed < MIN_SPEED) {
772         speed = MIN_SPEED;
773     } else if (speed > MAX_SPEED) {
774         speed = MAX_SPEED;
775     }
776     globalPointerSpeed_ = speed;
777     std::string name = "speed";
778     int32_t ret = PREFERENCES_MGR->SetIntValue(name, MOUSE_FILE_NAME, speed);
779     if (ret != RET_OK) {
780         MMI_HILOGE("Set pointer speed failed, code:%{public}d", ret);
781         return ret;
782     }
783     MMI_HILOGD("Set pointer speed successfully, speed:%{public}d", speed);
784     return RET_OK;
785 }
786 
GetPointerSpeed()787 int32_t MouseTransformProcessor::GetPointerSpeed()
788 {
789     std::string name = "speed";
790     int32_t speed = PREFERENCES_MGR->GetIntValue(name, DEFAULT_SPEED);
791     MMI_HILOGD("Pointer speed:%{public}d", speed);
792     return speed;
793 }
794 
GetTouchpadSpeed()795 int32_t MouseTransformProcessor::GetTouchpadSpeed()
796 {
797     int32_t speed = DEFAULT_TOUCHPAD_SPEED;
798     GetTouchpadPointerSpeed(speed);
799     MMI_HILOGD("TouchPad pointer speed:%{public}d", speed);
800     return speed;
801 }
802 
SetPointerLocation(int32_t x,int32_t y)803 int32_t MouseTransformProcessor::SetPointerLocation(int32_t x, int32_t y)
804 {
805     MMI_HILOGI("SetPointerLocation x:%d, y:%d", x, y);
806     CursorPosition cursorPos = WIN_MGR->GetCursorPos();
807     if (cursorPos.displayId < 0) {
808         MMI_HILOGE("No display");
809         return RET_ERR;
810     }
811     cursorPos.cursorPos.x = x;
812     cursorPos.cursorPos.y = y;
813 
814     WIN_MGR->UpdateAndAdjustMouseLocation(cursorPos.displayId, cursorPos.cursorPos.x, cursorPos.cursorPos.y, false);
815     auto mouseLoc = WIN_MGR->GetMouseInfo();
816     IPointerDrawingManager::GetInstance()->SetPointerLocation(mouseLoc.physicalX, mouseLoc.physicalY);
817     return RET_OK;
818 }
819 
HandleTouchpadRightButton(struct libinput_event_pointer * data,const int32_t evenType,uint32_t & button)820 void MouseTransformProcessor::HandleTouchpadRightButton(struct libinput_event_pointer *data, const int32_t evenType,
821     uint32_t &button)
822 {
823     // touchpad left click 280 -> 272
824     if (button == BTN_RIGHT_MENUE_CODE) {
825         button = MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_LEFT_BUTTON_CODE;
826         return;
827     }
828 
829     // touchpad two finger tap 273 -> 0
830     if (button == MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_RIGHT_BUTTON_CODE &&
831         evenType == LIBINPUT_EVENT_POINTER_TAP) {
832         button = 0;
833         return;
834     }
835 
836     // touchpad two finger button 272 -> 0
837     if (button == MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_LEFT_BUTTON_CODE &&
838         evenType == LIBINPUT_EVENT_POINTER_BUTTON_TOUCHPAD) {
839         return;
840     }
841 }
842 
HandleTouchpadLeftButton(struct libinput_event_pointer * data,const int32_t evenType,uint32_t & button)843 void MouseTransformProcessor::HandleTouchpadLeftButton(struct libinput_event_pointer *data, const int32_t evenType,
844     uint32_t &button)
845 {
846     // touchpad left click 280 -> 273
847     if (button == BTN_RIGHT_MENUE_CODE) {
848         button = MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_RIGHT_BUTTON_CODE;
849         return;
850     }
851 
852     // touchpad right click 273 -> 272
853     if (button == MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_RIGHT_BUTTON_CODE &&
854         evenType != LIBINPUT_EVENT_POINTER_TAP) {
855         button = MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_LEFT_BUTTON_CODE;
856         return;
857     }
858 
859     // touchpad two finger tap 273 -> 0
860     if (button == MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_RIGHT_BUTTON_CODE &&
861         evenType == LIBINPUT_EVENT_POINTER_TAP) {
862         button = 0;
863         return;
864     }
865 
866     // touchpad two finger button 272 -> 0
867     if (button == MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_LEFT_BUTTON_CODE &&
868         evenType == LIBINPUT_EVENT_POINTER_BUTTON_TOUCHPAD) {
869         return;
870     }
871 }
872 
HandleTouchpadTwoFingerButton(struct libinput_event_pointer * data,const int32_t evenType,uint32_t & button)873 void MouseTransformProcessor::HandleTouchpadTwoFingerButton(struct libinput_event_pointer *data, const int32_t evenType,
874     uint32_t &button)
875 {
876     // touchpad two finger button -> 273
877     uint32_t fingerCount = libinput_event_pointer_get_finger_count(data);
878     if (fingerCount == TP_RIGHT_CLICK_FINGER_CNT) {
879         button = MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_RIGHT_BUTTON_CODE;
880         return;
881     }
882 
883     // touchpad right click 273 -> 272
884     if (button == MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_RIGHT_BUTTON_CODE &&
885         evenType == LIBINPUT_EVENT_POINTER_BUTTON_TOUCHPAD) {
886         button = MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_LEFT_BUTTON_CODE;
887         return;
888     }
889 
890     // touchpad left click 280 -> 272
891     if (button == BTN_RIGHT_MENUE_CODE) {
892         button = MouseDeviceState::LIBINPUT_BUTTON_CODE::LIBINPUT_LEFT_BUTTON_CODE;
893         return;
894     }
895 }
896 
TransTouchpadRightButton(struct libinput_event_pointer * data,const int32_t evenType,uint32_t & button)897 void MouseTransformProcessor::TransTouchpadRightButton(struct libinput_event_pointer *data, const int32_t evenType,
898     uint32_t &button)
899 {
900     int32_t switchTypeData = RIGHT_CLICK_TYPE_MIN;
901     GetTouchpadRightClickType(switchTypeData);
902 
903     RightClickType switchType = RightClickType(switchTypeData);
904     if (evenType != LIBINPUT_EVENT_POINTER_TAP && evenType != LIBINPUT_EVENT_POINTER_BUTTON_TOUCHPAD) {
905         MMI_HILOGE("Event not from touchpad");
906         return;
907     }
908 
909     switch (switchType) {
910         case RightClickType::TP_RIGHT_BUTTON:
911             HandleTouchpadRightButton(data, evenType, button);
912             break;
913 
914         case RightClickType::TP_LEFT_BUTTON:
915             HandleTouchpadLeftButton(data, evenType, button);
916             break;
917 
918         case RightClickType::TP_TWO_FINGER_TAP:
919             HandleTouchpadTwoFingerButton(data, evenType, button);
920             break;
921         default:
922             MMI_HILOGD("Invalid type, switchType:%{public}d", switchType);
923             break;
924     }
925 }
926 
SetTouchpadScrollSwitch(bool switchFlag)927 int32_t MouseTransformProcessor::SetTouchpadScrollSwitch(bool switchFlag)
928 {
929     std::string name = "scrollSwitch";
930     if (PutConfigDataToDatabase(name, switchFlag) != RET_OK) {
931         MMI_HILOGE("Failed to set scroll switch flag to mem, name:%s, switchFlag:%{public}d", name.c_str(), switchFlag);
932         return RET_ERR;
933     }
934     DfxHisysevent::ReportTouchpadSettingState(DfxHisysevent::TOUCHPAD_SETTING_CODE::TOUCHPAD_SCROLL_SETTING,
935         switchFlag);
936 
937     return RET_OK;
938 }
939 
GetTouchpadScrollSwitch(bool & switchFlag)940 void MouseTransformProcessor::GetTouchpadScrollSwitch(bool &switchFlag)
941 {
942     std::string name = "scrollSwitch";
943     GetConfigDataFromDatabase(name, switchFlag);
944 }
945 
SetTouchpadScrollDirection(bool state)946 int32_t MouseTransformProcessor::SetTouchpadScrollDirection(bool state)
947 {
948     std::string name = "scrollDirection";
949     if (PutConfigDataToDatabase(name, state) != RET_OK) {
950         MMI_HILOGE("Failed to set scroll direct switch flag to mem");
951         return RET_ERR;
952     }
953 
954     DfxHisysevent::ReportTouchpadSettingState(DfxHisysevent::TOUCHPAD_SETTING_CODE::TOUCHPAD_SCROLL_DIR_SETTING,
955         state);
956 
957     return RET_OK;
958 }
959 
GetTouchpadScrollDirection(bool & state)960 void MouseTransformProcessor::GetTouchpadScrollDirection(bool &state)
961 {
962     std::string name = "scrollDirection";
963     GetConfigDataFromDatabase(name, state);
964 }
965 
SetTouchpadTapSwitch(bool switchFlag)966 int32_t MouseTransformProcessor::SetTouchpadTapSwitch(bool switchFlag)
967 {
968     std::string name = "touchpadTap";
969     if (PutConfigDataToDatabase(name, switchFlag) != RET_OK) {
970         MMI_HILOGE("Failed to set scroll direct switch flag to mem");
971         return RET_ERR;
972     }
973 
974     DfxHisysevent::ReportTouchpadSettingState(DfxHisysevent::TOUCHPAD_SETTING_CODE::TOUCHPAD_TAP_SETTING,
975         switchFlag);
976 
977     return RET_OK;
978 }
979 
GetTouchpadTapSwitch(bool & switchFlag)980 void MouseTransformProcessor::GetTouchpadTapSwitch(bool &switchFlag)
981 {
982     std::string name = "touchpadTap";
983     GetConfigDataFromDatabase(name, switchFlag);
984 }
985 
SetTouchpadPointerSpeed(int32_t speed)986 int32_t MouseTransformProcessor::SetTouchpadPointerSpeed(int32_t speed)
987 {
988     std::string name = "touchPadPointerSpeed";
989     if (PutConfigDataToDatabase(name, speed) != RET_OK) {
990         MMI_HILOGE("Failed to set touch pad pointer speed to mem");
991         return RET_ERR;
992     }
993 
994     DfxHisysevent::ReportTouchpadSettingState(DfxHisysevent::TOUCHPAD_SETTING_CODE::TOUCHPAD_POINTER_SPEED_SETTING,
995         speed);
996 
997     return RET_OK;
998 }
999 
GetTouchpadPointerSpeed(int32_t & speed)1000 void MouseTransformProcessor::GetTouchpadPointerSpeed(int32_t &speed)
1001 {
1002     std::string name = "touchPadPointerSpeed";
1003     GetConfigDataFromDatabase(name, speed);
1004     speed = speed == 0 ? DEFAULT_TOUCHPAD_SPEED : speed;
1005     speed = speed < MIN_SPEED ? MIN_SPEED : speed;
1006     speed = speed > MAX_SPEED ? MAX_SPEED : speed;
1007 }
1008 
SetTouchpadRightClickType(int32_t type)1009 int32_t MouseTransformProcessor::SetTouchpadRightClickType(int32_t type)
1010 {
1011     std::string name = "rightMenuSwitch";
1012     if (PutConfigDataToDatabase(name, type) != RET_OK) {
1013         MMI_HILOGE("Failed to set right click type to mem");
1014         return RET_ERR;
1015     }
1016     DfxHisysevent::ReportTouchpadSettingState(DfxHisysevent::TOUCHPAD_SETTING_CODE::TOUCHPAD_RIGHT_CLICK_SETTING,
1017         type);
1018     return RET_OK;
1019 }
1020 
GetTouchpadRightClickType(int32_t & type)1021 void MouseTransformProcessor::GetTouchpadRightClickType(int32_t &type)
1022 {
1023     std::string name = "rightMenuSwitch";
1024     GetConfigDataFromDatabase(name, type);
1025 
1026     if (type < RIGHT_CLICK_TYPE_MIN || type > RIGHT_CLICK_TYPE_MAX) {
1027         type = RIGHT_CLICK_TYPE_MIN;
1028     }
1029 }
1030 
PutConfigDataToDatabase(std::string & key,bool value)1031 int32_t MouseTransformProcessor::PutConfigDataToDatabase(std::string &key, bool value)
1032 {
1033     return PREFERENCES_MGR->SetBoolValue(key, MOUSE_FILE_NAME, value);
1034 }
1035 
GetConfigDataFromDatabase(std::string & key,bool & value)1036 void MouseTransformProcessor::GetConfigDataFromDatabase(std::string &key, bool &value)
1037 {
1038     value = PREFERENCES_MGR->GetBoolValue(key, true);
1039 }
1040 
PutConfigDataToDatabase(std::string & key,int32_t value)1041 int32_t MouseTransformProcessor::PutConfigDataToDatabase(std::string &key, int32_t value)
1042 {
1043     return PREFERENCES_MGR->SetIntValue(key, MOUSE_FILE_NAME, value);
1044 }
1045 
GetConfigDataFromDatabase(std::string & key,int32_t & value)1046 void MouseTransformProcessor::GetConfigDataFromDatabase(std::string &key, int32_t &value)
1047 {
1048     int32_t defaultValue = value;
1049     value = PREFERENCES_MGR->GetIntValue(key, defaultValue);
1050 }
1051 } // namespace MMI
1052 } // namespace OHOS
1053