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