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 "touchpad_transform_processor.h"
17
18 #include <sstream>
19
20 #include <linux/input.h>
21
22 #include "event_log_helper.h"
23 #include "input_windows_manager.h"
24 #include "mmi_log.h"
25 #include "mouse_device_state.h"
26 #include "preferences.h"
27 #include "preferences_impl.h"
28 #include "preferences_errno.h"
29 #include "preferences_helper.h"
30 #include "preferences_xml_utils.h"
31 #include "dfx_hisysevent.h"
32
33 namespace OHOS {
34 namespace MMI {
35 namespace {
36 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MMI_LOG_DOMAIN, "TouchPadTransformProcessor" };
37 constexpr int32_t MT_TOOL_NONE { -1 };
38 constexpr int32_t BTN_DOWN { 1 };
39 constexpr int32_t FINGER_COUNT_MAX { 5 };
40 constexpr int32_t TP_SYSTEM_PINCH_FINGER_CNT { 2 };
41 const std::string TOUCHPAD_FILE_NAME = "/data/service/el1/public/multimodalinput/touchpad_settings.xml";
42 } // namespace
43
TouchPadTransformProcessor(int32_t deviceId)44 TouchPadTransformProcessor::TouchPadTransformProcessor(int32_t deviceId)
45 : deviceId_(deviceId)
46 {
47 InitToolType();
48 }
49
OnEventTouchPadDown(struct libinput_event * event)50 int32_t TouchPadTransformProcessor::OnEventTouchPadDown(struct libinput_event *event)
51 {
52 CALL_DEBUG_ENTER;
53 CHKPR(event, RET_ERR);
54 auto touchpad = libinput_event_get_touchpad_event(event);
55 CHKPR(touchpad, RET_ERR);
56 auto device = libinput_event_get_device(event);
57 CHKPR(device, RET_ERR);
58
59 uint64_t time = libinput_event_touchpad_get_time_usec(touchpad);
60 auto pointIds = pointerEvent_->GetPointerIds();
61 if (pointIds.empty()) {
62 pointerEvent_->SetActionStartTime(time);
63 }
64 pointerEvent_->SetActionTime(time);
65 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
66 PointerEvent::PointerItem item;
67 int32_t longAxis = libinput_event_touchpad_get_touch_contact_long_axis(touchpad);
68 int32_t shortAxis = libinput_event_touchpad_get_touch_contact_short_axis(touchpad);
69 double pressure = libinput_event_touchpad_get_pressure(touchpad);
70 int32_t seatSlot = libinput_event_touchpad_get_seat_slot(touchpad);
71 double logicalX = libinput_event_touchpad_get_x(touchpad);
72 double logicalY = libinput_event_touchpad_get_y(touchpad);
73 double toolPhysicalX = libinput_event_touchpad_get_tool_x(touchpad);
74 double toolPhysicalY = libinput_event_touchpad_get_tool_y(touchpad);
75 double toolWidth = libinput_event_touchpad_get_tool_width(touchpad);
76 double toolHeight = libinput_event_touchpad_get_tool_height(touchpad);
77 int32_t toolType = GetTouchPadToolType(touchpad, device);
78 if (toolType == PointerEvent::TOOL_TYPE_PALM) {
79 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
80 }
81
82 item.SetLongAxis(longAxis);
83 item.SetShortAxis(shortAxis);
84 item.SetPressure(pressure);
85 item.SetToolType(toolType);
86 item.SetPointerId(seatSlot);
87 item.SetDownTime(time);
88 item.SetPressed(true);
89 item.SetDisplayX(static_cast<int32_t>(logicalX));
90 item.SetDisplayY(static_cast<int32_t>(logicalY));
91 item.SetToolDisplayX(static_cast<int32_t>(toolPhysicalX));
92 item.SetToolDisplayY(static_cast<int32_t>(toolPhysicalY));
93 item.SetToolWidth(static_cast<int32_t>(toolWidth));
94 item.SetToolHeight(static_cast<int32_t>(toolHeight));
95 item.SetDeviceId(deviceId_);
96 pointerEvent_->SetDeviceId(deviceId_);
97 pointerEvent_->AddPointerItem(item);
98 pointerEvent_->SetPointerId(seatSlot);
99
100 return RET_OK;
101 }
102
OnEventTouchPadMotion(struct libinput_event * event)103 int32_t TouchPadTransformProcessor::OnEventTouchPadMotion(struct libinput_event *event)
104 {
105 CALL_DEBUG_ENTER;
106 CHKPR(event, RET_ERR);
107 auto touchpad = libinput_event_get_touchpad_event(event);
108 CHKPR(touchpad, RET_ERR);
109 int32_t seatSlot = libinput_event_touchpad_get_seat_slot(touchpad);
110 auto device = libinput_event_get_device(event);
111 CHKPR(device, RET_ERR);
112
113 uint64_t time = libinput_event_touchpad_get_time_usec(touchpad);
114 pointerEvent_->SetActionTime(time);
115 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
116 PointerEvent::PointerItem item;
117 if (!pointerEvent_->GetPointerItem(seatSlot, item)) {
118 MMI_HILOGE("Can't find the pointer item data, seatSlot:%{public}d, errCode:%{public}d",
119 seatSlot, PARAM_INPUT_FAIL);
120 return RET_ERR;
121 }
122 int32_t longAxis = libinput_event_touchpad_get_touch_contact_long_axis(touchpad);
123 int32_t shortAxis = libinput_event_touchpad_get_touch_contact_short_axis(touchpad);
124 double pressure = libinput_event_touchpad_get_pressure(touchpad);
125 double logicalX = libinput_event_touchpad_get_x(touchpad);
126 double logicalY = libinput_event_touchpad_get_y(touchpad);
127 double toolPhysicalX = libinput_event_touchpad_get_tool_x(touchpad);
128 double toolPhysicalY = libinput_event_touchpad_get_tool_y(touchpad);
129 double toolWidth = libinput_event_touchpad_get_tool_width(touchpad);
130 double toolHeight = libinput_event_touchpad_get_tool_height(touchpad);
131 int32_t toolType = GetTouchPadToolType(touchpad, device);
132 if (toolType == PointerEvent::TOOL_TYPE_PALM) {
133 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_CANCEL);
134 }
135
136 item.SetLongAxis(longAxis);
137 item.SetShortAxis(shortAxis);
138 item.SetPressure(pressure);
139 item.SetDisplayX(static_cast<int32_t>(logicalX));
140 item.SetDisplayY(static_cast<int32_t>(logicalY));
141 item.SetToolDisplayX(static_cast<int32_t>(toolPhysicalX));
142 item.SetToolDisplayY(static_cast<int32_t>(toolPhysicalY));
143 item.SetToolWidth(static_cast<int32_t>(toolWidth));
144 item.SetToolHeight(static_cast<int32_t>(toolHeight));
145 pointerEvent_->UpdatePointerItem(seatSlot, item);
146 pointerEvent_->SetPointerId(seatSlot);
147
148 return RET_OK;
149 }
150
OnEventTouchPadUp(struct libinput_event * event)151 int32_t TouchPadTransformProcessor::OnEventTouchPadUp(struct libinput_event *event)
152 {
153 CALL_DEBUG_ENTER;
154 CHKPR(event, RET_ERR);
155 auto touchpad = libinput_event_get_touchpad_event(event);
156 CHKPR(touchpad, RET_ERR);
157 int32_t seatSlot = libinput_event_touchpad_get_seat_slot(touchpad);
158
159 uint64_t time = libinput_event_touchpad_get_time_usec(touchpad);
160 pointerEvent_->SetActionTime(time);
161 pointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
162
163 PointerEvent::PointerItem item;
164 if (!pointerEvent_->GetPointerItem(seatSlot, item)) {
165 MMI_HILOGE("Can't find the pointer item data, seatSlot:%{public}d, errCode:%{public}d",
166 seatSlot, PARAM_INPUT_FAIL);
167 return RET_ERR;
168 }
169 item.SetPressed(false);
170 pointerEvent_->UpdatePointerItem(seatSlot, item);
171 pointerEvent_->SetPointerId(seatSlot);
172
173 return RET_OK;
174 }
175
OnEvent(struct libinput_event * event)176 std::shared_ptr<PointerEvent> TouchPadTransformProcessor::OnEvent(struct libinput_event *event)
177 {
178 CALL_DEBUG_ENTER;
179 CHKPP(event);
180 if (pointerEvent_ == nullptr) {
181 pointerEvent_ = PointerEvent::Create();
182 CHKPP(pointerEvent_);
183 }
184
185 int32_t ret = RET_OK;
186 int32_t type = libinput_event_get_type(event);
187 switch (type) {
188 case LIBINPUT_EVENT_TOUCHPAD_DOWN: {
189 ret = OnEventTouchPadDown(event);
190 break;
191 }
192 case LIBINPUT_EVENT_TOUCHPAD_UP: {
193 ret = OnEventTouchPadUp(event);
194 break;
195 }
196 case LIBINPUT_EVENT_TOUCHPAD_MOTION: {
197 ret = OnEventTouchPadMotion(event);
198 break;
199 }
200 case LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN: {
201 ret = OnEventTouchPadSwipeBegin(event);
202 break;
203 }
204 case LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE: {
205 ret = OnEventTouchPadSwipeUpdate(event);
206 break;
207 }
208 case LIBINPUT_EVENT_GESTURE_SWIPE_END: {
209 ret = OnEventTouchPadSwipeEnd(event);
210 break;
211 }
212
213 case LIBINPUT_EVENT_GESTURE_PINCH_BEGIN: {
214 ret = OnEventTouchPadPinchBegin(event);
215 break;
216 }
217 case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE: {
218 ret = OnEventTouchPadPinchUpdate(event);
219 break;
220 }
221 case LIBINPUT_EVENT_GESTURE_PINCH_END: {
222 ret = OnEventTouchPadPinchEnd(event);
223 break;
224 }
225 default: {
226 return nullptr;
227 }
228 }
229
230 if (ret != RET_OK) {
231 return nullptr;
232 }
233
234 pointerEvent_->UpdateId();
235 MMI_HILOGD("Pointer event dispatcher of server:");
236 EventLogHelper::PrintEventData(pointerEvent_, pointerEvent_->GetPointerAction(),
237 pointerEvent_->GetPointerIds().size());
238 return pointerEvent_;
239 }
240
GetTouchPadToolType(struct libinput_event_touch * touchpad,struct libinput_device * device)241 int32_t TouchPadTransformProcessor::GetTouchPadToolType(
242 struct libinput_event_touch *touchpad, struct libinput_device *device)
243 {
244 int32_t toolType = libinput_event_touchpad_get_tool_type(touchpad);
245 switch (toolType) {
246 case MT_TOOL_NONE: {
247 return GetTouchPadToolType(device);
248 }
249 case MT_TOOL_FINGER: {
250 return PointerEvent::TOOL_TYPE_FINGER;
251 }
252 case MT_TOOL_PEN: {
253 return PointerEvent::TOOL_TYPE_PEN;
254 }
255 case MT_TOOL_PALM: {
256 MMI_HILOGD("ToolType is MT_TOOL_PALM");
257 return PointerEvent::TOOL_TYPE_PALM;
258 }
259 default : {
260 MMI_HILOGW("Unknown tool type, identified as finger, toolType:%{public}d", toolType);
261 return PointerEvent::TOOL_TYPE_FINGER;
262 }
263 }
264 }
265
GetTouchPadToolType(struct libinput_device * device)266 int32_t TouchPadTransformProcessor::GetTouchPadToolType(struct libinput_device *device)
267 {
268 for (const auto &item : vecToolType_) {
269 if (libinput_device_touchpad_btn_tool_type_down(device, item.first) == BTN_DOWN) {
270 return item.second;
271 }
272 }
273 MMI_HILOGW("Unknown Btn tool type, identified as finger");
274 return PointerEvent::TOOL_TYPE_FINGER;
275 }
276
SetTouchPadSwipeData(struct libinput_event * event,int32_t action)277 int32_t TouchPadTransformProcessor::SetTouchPadSwipeData(struct libinput_event *event, int32_t action)
278 {
279 CALL_DEBUG_ENTER;
280
281 bool tpSwipeSwitch = true;
282 if (GetTouchpadSwipeSwitch(tpSwipeSwitch) != RET_OK) {
283 MMI_HILOGD("Failed to get touchpad swipe switch flag, default is true.");
284 }
285
286 if (!tpSwipeSwitch) {
287 MMI_HILOGD("Touchpad swipe switch is false.");
288 return RET_ERR;
289 }
290
291 CHKPR(event, RET_ERR);
292 struct libinput_event_gesture *gesture = libinput_event_get_gesture_event(event);
293 CHKPR(gesture, RET_ERR);
294
295 int64_t time = static_cast<int64_t>(libinput_event_gesture_get_time(gesture));
296 pointerEvent_->SetActionTime(GetSysClockTime());
297 pointerEvent_->SetActionStartTime(time);
298 pointerEvent_->SetPointerAction(action);
299
300 int32_t fingerCount = libinput_event_gesture_get_finger_count(gesture);
301 if (fingerCount < 0 || fingerCount > FINGER_COUNT_MAX) {
302 MMI_HILOGE("Finger count is invalid.");
303 return RET_ERR;
304 }
305 pointerEvent_->SetFingerCount(fingerCount);
306
307 if (fingerCount == 0) {
308 MMI_HILOGD("There is no finger in swipe action %{public}d.", action);
309 return RET_ERR;
310 }
311
312 int32_t sumX = 0;
313 int32_t sumY = 0;
314 for (int32_t i = 0; i < fingerCount; i++) {
315 sumX += libinput_event_gesture_get_device_coords_x(gesture, i);
316 sumY += libinput_event_gesture_get_device_coords_y(gesture, i);
317 }
318
319 PointerEvent::PointerItem pointerItem;
320 pointerEvent_->GetPointerItem(defaultPointerId, pointerItem);
321 pointerItem.SetPressed(MouseState->IsLeftBtnPressed());
322 pointerItem.SetDownTime(time);
323 pointerItem.SetDisplayX(sumX / fingerCount);
324 pointerItem.SetDisplayY(sumY / fingerCount);
325 pointerItem.SetDeviceId(deviceId_);
326 pointerItem.SetPointerId(defaultPointerId);
327 pointerEvent_->UpdatePointerItem(defaultPointerId, pointerItem);
328 pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
329
330 if (action == PointerEvent::POINTER_ACTION_SWIPE_BEGIN) {
331 MMI_HILOGE("lisenhao-app go in to report POINTER_ACTION_SWIPE_BEGIN");
332 DfxHisysevent::StatisticTouchpadGesture(pointerEvent_);
333 }
334
335 return RET_OK;
336 }
337
OnEventTouchPadSwipeBegin(struct libinput_event * event)338 int32_t TouchPadTransformProcessor::OnEventTouchPadSwipeBegin(struct libinput_event *event)
339 {
340 CALL_DEBUG_ENTER;
341 return SetTouchPadSwipeData(event, PointerEvent::POINTER_ACTION_SWIPE_BEGIN);
342 }
343
OnEventTouchPadSwipeUpdate(struct libinput_event * event)344 int32_t TouchPadTransformProcessor::OnEventTouchPadSwipeUpdate(struct libinput_event *event)
345 {
346 CALL_DEBUG_ENTER;
347 return SetTouchPadSwipeData(event, PointerEvent::POINTER_ACTION_SWIPE_UPDATE);
348 }
349
OnEventTouchPadSwipeEnd(struct libinput_event * event)350 int32_t TouchPadTransformProcessor::OnEventTouchPadSwipeEnd(struct libinput_event *event)
351 {
352 CALL_DEBUG_ENTER;
353 return SetTouchPadSwipeData(event, PointerEvent::POINTER_ACTION_SWIPE_END);
354 }
355
SetTouchPadPinchData(struct libinput_event * event,int32_t action)356 int32_t TouchPadTransformProcessor::SetTouchPadPinchData(struct libinput_event *event, int32_t action)
357 {
358 CALL_DEBUG_ENTER;
359
360 bool tpPinchSwitch = true;
361 if (GetTouchpadPinchSwitch(tpPinchSwitch) != RET_OK) {
362 MMI_HILOGD("Failed to get touchpad pinch switch flag, default is true.");
363 }
364
365 CHKPR(event, RET_ERR);
366 auto gesture = libinput_event_get_gesture_event(event);
367 CHKPR(gesture, RET_ERR);
368 int32_t fingerCount = libinput_event_gesture_get_finger_count(gesture);
369 if (fingerCount <= 0 || fingerCount > FINGER_COUNT_MAX) {
370 MMI_HILOGE("Finger count is invalid.");
371 return RET_ERR;
372 }
373
374 if (!tpPinchSwitch && fingerCount == TP_SYSTEM_PINCH_FINGER_CNT) {
375 MMI_HILOGD("Touchpad pinch switch is false.");
376 return RET_ERR;
377 }
378
379 int64_t time = static_cast<int64_t>(libinput_event_gesture_get_time(gesture));
380 double scale = libinput_event_gesture_get_scale(gesture);
381
382 pointerEvent_->SetActionTime(GetSysClockTime());
383 pointerEvent_->SetActionStartTime(time);
384
385 SetPinchPointerItem(time);
386
387 ProcessTouchPadPinchDataEvent(fingerCount, action, scale);
388
389 return RET_OK;
390 }
391
SetPinchPointerItem(int64_t time)392 void TouchPadTransformProcessor::SetPinchPointerItem(int64_t time)
393 {
394 PointerEvent::PointerItem pointerItem;
395 pointerItem.SetDownTime(time);
396 pointerItem.SetPressed(MouseState->IsLeftBtnPressed());
397 pointerItem.SetPointerId(defaultPointerId);
398 pointerItem.SetWindowX(0);
399 pointerItem.SetWindowY(0);
400 auto mouseInfo = WinMgr->GetMouseInfo();
401 pointerItem.SetDisplayX(mouseInfo.physicalX);
402 pointerItem.SetDisplayY(mouseInfo.physicalY);
403 pointerEvent_->UpdatePointerItem(defaultPointerId, pointerItem);
404 }
405
ProcessTouchPadPinchDataEvent(int32_t fingerCount,int32_t action,double scale)406 void TouchPadTransformProcessor::ProcessTouchPadPinchDataEvent(int32_t fingerCount, int32_t action, double scale)
407 {
408 pointerEvent_->ClearButtonPressed();
409 std::vector<int32_t> pressedButtons;
410 MouseState->GetPressedButtons(pressedButtons);
411 for (const auto &item : pressedButtons) {
412 pointerEvent_->SetButtonPressed(item);
413 }
414
415 pointerEvent_->SetFingerCount(fingerCount);
416 pointerEvent_->SetDeviceId(deviceId_);
417 pointerEvent_->SetTargetDisplayId(0);
418 pointerEvent_->SetTargetWindowId(-1);
419 pointerEvent_->SetPointerId(defaultPointerId);
420 pointerEvent_->SetPointerAction(action);
421 pointerEvent_->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, scale);
422
423 if (fingerCount == TP_SYSTEM_PINCH_FINGER_CNT) {
424 pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
425 pointerEvent_->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, scale);
426 } else {
427 pointerEvent_->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHPAD);
428 pointerEvent_->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, scale);
429 }
430
431 if (pointerEvent_->GetFingerCount() == TP_SYSTEM_PINCH_FINGER_CNT) {
432 WinMgr->UpdateTargetPointer(pointerEvent_);
433 }
434
435 // only three or four finger pinch need to statistic
436 if (action == PointerEvent::POINTER_ACTION_AXIS_BEGIN && fingerCount > TP_SYSTEM_PINCH_FINGER_CNT) {
437 DfxHisysevent::StatisticTouchpadGesture(pointerEvent_);
438 }
439 }
440
OnEventTouchPadPinchBegin(struct libinput_event * event)441 int32_t TouchPadTransformProcessor::OnEventTouchPadPinchBegin(struct libinput_event *event)
442 {
443 CALL_DEBUG_ENTER;
444 return SetTouchPadPinchData(event, PointerEvent::POINTER_ACTION_AXIS_BEGIN);
445 }
446
OnEventTouchPadPinchUpdate(struct libinput_event * event)447 int32_t TouchPadTransformProcessor::OnEventTouchPadPinchUpdate(struct libinput_event *event)
448 {
449 CALL_DEBUG_ENTER;
450 return SetTouchPadPinchData(event, PointerEvent::POINTER_ACTION_AXIS_UPDATE);
451 }
452
OnEventTouchPadPinchEnd(struct libinput_event * event)453 int32_t TouchPadTransformProcessor::OnEventTouchPadPinchEnd(struct libinput_event *event)
454 {
455 CALL_DEBUG_ENTER;
456 return SetTouchPadPinchData(event, PointerEvent::POINTER_ACTION_AXIS_END);
457 }
458
InitToolType()459 void TouchPadTransformProcessor::InitToolType()
460 {
461 vecToolType_.push_back(std::make_pair(BTN_TOOL_PEN, PointerEvent::TOOL_TYPE_PEN));
462 vecToolType_.push_back(std::make_pair(BTN_TOOL_RUBBER, PointerEvent::TOOL_TYPE_RUBBER));
463 vecToolType_.push_back(std::make_pair(BTN_TOOL_BRUSH, PointerEvent::TOOL_TYPE_BRUSH));
464 vecToolType_.push_back(std::make_pair(BTN_TOOL_PENCIL, PointerEvent::TOOL_TYPE_PENCIL));
465 vecToolType_.push_back(std::make_pair(BTN_TOOL_AIRBRUSH, PointerEvent::TOOL_TYPE_AIRBRUSH));
466 vecToolType_.push_back(std::make_pair(BTN_TOOL_FINGER, PointerEvent::TOOL_TYPE_FINGER));
467 vecToolType_.push_back(std::make_pair(BTN_TOOL_MOUSE, PointerEvent::TOOL_TYPE_MOUSE));
468 vecToolType_.push_back(std::make_pair(BTN_TOOL_LENS, PointerEvent::TOOL_TYPE_LENS));
469 }
470
SetTouchpadSwipeSwitch(bool switchFlag)471 int32_t TouchPadTransformProcessor::SetTouchpadSwipeSwitch(bool switchFlag)
472 {
473 std::string name = "touchpadSwipe";
474 if (PutConfigDataToDatabase(name, switchFlag) != RET_OK) {
475 MMI_HILOGE("Failed to set touchpad swpie switch flag to mem.");
476 return RET_ERR;
477 }
478
479 DfxHisysevent::ReportTouchpadSettingState(DfxHisysevent::TOUCHPAD_SETTING_CODE::TOUCHPAD_SWIPE_SETTING,
480 switchFlag);
481 return RET_OK;
482 }
483
GetTouchpadSwipeSwitch(bool & switchFlag)484 int32_t TouchPadTransformProcessor::GetTouchpadSwipeSwitch(bool &switchFlag)
485 {
486 std::string name = "touchpadSwipe";
487 if (GetConfigDataFromDatabase(name, switchFlag) != RET_OK) {
488 MMI_HILOGE("Failed to get touchpad swpie switch flag from mem.");
489 return RET_ERR;
490 }
491
492 return RET_OK;
493 }
494
SetTouchpadPinchSwitch(bool switchFlag)495 int32_t TouchPadTransformProcessor::SetTouchpadPinchSwitch(bool switchFlag)
496 {
497 std::string name = "touchpadPinch";
498 if (PutConfigDataToDatabase(name, switchFlag) != RET_OK) {
499 MMI_HILOGE("Failed to set touchpad pinch switch flag to mem.");
500 return RET_ERR;
501 }
502
503 DfxHisysevent::ReportTouchpadSettingState(DfxHisysevent::TOUCHPAD_SETTING_CODE::TOUCHPAD_PINCH_SETTING,
504 switchFlag);
505 return RET_OK;
506 }
507
GetTouchpadPinchSwitch(bool & switchFlag)508 int32_t TouchPadTransformProcessor::GetTouchpadPinchSwitch(bool &switchFlag)
509 {
510 std::string name = "touchpadPinch";
511 if (GetConfigDataFromDatabase(name, switchFlag) != RET_OK) {
512 MMI_HILOGE("Failed to get touchpad pinch switch flag from mem.");
513 return RET_ERR;
514 }
515
516 return RET_OK;
517 }
518
PutConfigDataToDatabase(std::string & key,bool value)519 int32_t TouchPadTransformProcessor::PutConfigDataToDatabase(std::string &key, bool value)
520 {
521 int32_t errCode = RET_OK;
522 std::shared_ptr<NativePreferences::Preferences> pref =
523 NativePreferences::PreferencesHelper::GetPreferences(TOUCHPAD_FILE_NAME, errCode);
524 if (pref == nullptr) {
525 MMI_HILOGE("pref is nullptr, errCode: %{public}d", errCode);
526 return RET_ERR;
527 }
528 int32_t ret = pref->PutBool(key, value);
529 if (ret != RET_OK) {
530 MMI_HILOGE("Put value is failed, ret:%{public}d", ret);
531 return RET_ERR;
532 }
533 ret = pref->FlushSync();
534 if (ret != RET_OK) {
535 MMI_HILOGE("Flush sync is failed, ret:%{public}d", ret);
536 return RET_ERR;
537 }
538
539 NativePreferences::PreferencesHelper::RemovePreferencesFromCache(TOUCHPAD_FILE_NAME);
540 return RET_OK;
541 }
542
GetConfigDataFromDatabase(std::string & key,bool & value)543 int32_t TouchPadTransformProcessor::GetConfigDataFromDatabase(std::string &key, bool &value)
544 {
545 int32_t errCode = RET_OK;
546 std::shared_ptr<NativePreferences::Preferences> pref =
547 NativePreferences::PreferencesHelper::GetPreferences(TOUCHPAD_FILE_NAME, errCode);
548 if (pref == nullptr) {
549 MMI_HILOGE("pref is nullptr, errCode: %{public}d", errCode);
550 return RET_ERR;
551 }
552 value = pref->GetBool(key, true);
553
554 NativePreferences::PreferencesHelper::RemovePreferencesFromCache(TOUCHPAD_FILE_NAME);
555 return RET_OK;
556 }
557 } // namespace MMI
558 } // namespace OHOS
559