1/*
2 * Copyright (c) 2021-2024 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 "processing_finger_device.h"
17
18#undef MMI_LOG_TAG
19#define MMI_LOG_TAG "ProcessingFingerDevice"
20
21namespace OHOS {
22namespace MMI {
23namespace {
24constexpr int64_t FINGER_BLOCK_TIME { 6 };
25} // namespace
26
27int32_t ProcessingFingerDevice::TransformJsonDataToInputData(const DeviceItem &fingerEventArrays,
28    InputEventArray &inputEventArray)
29{
30    CALL_DEBUG_ENTER;
31    std::vector<DeviceEvent> inputData = fingerEventArrays.events;
32    if (inputData.empty()) {
33        MMI_HILOGE("Manage finger array failed, inputData is empty.");
34        return RET_ERR;
35    }
36    TouchPadInputEvents touchPadInputEvents = {};
37    AnalysisTouchPadFingerDate(inputData, touchPadInputEvents);
38    TouchPadInputEvent pressEvents = touchPadInputEvents.eventArray[0];
39    AnalysisTouchPadFingerPressData(inputEventArray, pressEvents);
40    for (uint64_t i = 1; i < touchPadInputEvents.eventNumber; i++) {
41        AnalysisTouchPadFingerMoveData(inputEventArray, touchPadInputEvents.eventArray[i]);
42    }
43    uint64_t releaseEventIndex = touchPadInputEvents.eventNumber - 1;
44    TouchPadInputEvent releaseEvents = touchPadInputEvents.eventArray[releaseEventIndex];
45    AnalysisTouchPadFingerReleaseData(inputEventArray, releaseEvents);
46    return RET_OK;
47}
48
49void ProcessingFingerDevice::AnalysisTouchPadFingerDate(const std::vector<DeviceEvent> &inputData,
50    TouchPadInputEvents &touchPadInputEvents)
51{
52    TouchPadCoordinates touchPadCoordinates = {};
53    TouchPadInputEvent touchPadInputEvent = {};
54    for (const auto &item : inputData) {
55        touchPadInputEvent.groupNumber = 0;
56        for (const auto &posXYItem : item.posXY) {
57            touchPadCoordinates.xPos = posXYItem.xPos;
58            touchPadCoordinates.yPos = posXYItem.yPos;
59            touchPadInputEvent.events.push_back(touchPadCoordinates);
60            ++touchPadInputEvent.groupNumber;
61        }
62        touchPadInputEvents.eventNumber = inputData.size();
63        touchPadInputEvents.eventArray.push_back(touchPadInputEvent);
64        touchPadInputEvent.events.clear();
65    }
66}
67
68void ProcessingFingerDevice::AnalysisTouchPadFingerPressData(InputEventArray &inputEventArray,
69                                                             const TouchPadInputEvent &touchPadInputEvent)
70{
71    int32_t yPos = 0;
72    int32_t xPos = 0;
73    for (uint64_t i = 0; i < static_cast<uint64_t>(touchPadInputEvent.groupNumber); i++) {
74        yPos = touchPadInputEvent.events[i].yPos;
75        xPos = touchPadInputEvent.events[i].xPos;
76        if (touchPadInputEvent.groupNumber > 1) {
77            SetMtSlot(inputEventArray, FINGER_BLOCK_TIME, static_cast<int32_t>(i));
78        }
79        SetTrackingId(inputEventArray, FINGER_BLOCK_TIME);
80        SetPositionX(inputEventArray, FINGER_BLOCK_TIME, xPos);
81        SetPositionY(inputEventArray, FINGER_BLOCK_TIME, yPos);
82        SetMtTouchMajor(inputEventArray, FINGER_BLOCK_TIME, 1);
83        SetMtTouchMinor(inputEventArray, FINGER_BLOCK_TIME, 1);
84        SetBtnTouch(inputEventArray, FINGER_BLOCK_TIME, 1);
85        SetMtTouchFingerType(inputEventArray, FINGER_BLOCK_TIME,
86                             static_cast<int32_t>(touchPadInputEvent.groupNumber), 1);
87        SetEvAbsX(inputEventArray, FINGER_BLOCK_TIME, xPos);
88        SetEvAbsY(inputEventArray, FINGER_BLOCK_TIME, yPos);
89    }
90    SetSynReport(inputEventArray);
91}
92
93void ProcessingFingerDevice::AnalysisTouchPadFingerMoveData(InputEventArray &inputEventArray,
94                                                            const TouchPadInputEvent &touchPadInputEvent)
95{
96    int32_t xPos = 0;
97    int32_t yPos = 0;
98    for (uint64_t i = 0; i < static_cast<uint64_t>(touchPadInputEvent.groupNumber); i++) {
99        xPos = touchPadInputEvent.events[i].xPos;
100        yPos = touchPadInputEvent.events[i].yPos;
101        if (touchPadInputEvent.groupNumber > 1) {
102            SetMtSlot(inputEventArray, FINGER_BLOCK_TIME, static_cast<int32_t>(i));
103        }
104        SetPositionX(inputEventArray, FINGER_BLOCK_TIME, xPos);
105        SetPositionY(inputEventArray, FINGER_BLOCK_TIME, yPos);
106        SetMtTouchMajor(inputEventArray, FINGER_BLOCK_TIME, 1);
107        SetMtTouchMinor(inputEventArray, FINGER_BLOCK_TIME, 1);
108        SetEvAbsX(inputEventArray, FINGER_BLOCK_TIME, xPos);
109        SetEvAbsY(inputEventArray, FINGER_BLOCK_TIME, yPos);
110    }
111    SetSynReport(inputEventArray);
112}
113
114void ProcessingFingerDevice::AnalysisTouchPadFingerReleaseData(InputEventArray &inputEventArray,
115                                                               const TouchPadInputEvent &touchPadInputEvent)
116{
117    for (uint64_t i = 0; i < static_cast<uint64_t>(touchPadInputEvent.groupNumber); i++) {
118        if (touchPadInputEvent.groupNumber > 1) {
119            SetMtSlot(inputEventArray, FINGER_BLOCK_TIME, static_cast<int32_t>(i));
120        }
121        SetTrackingId(inputEventArray, FINGER_BLOCK_TIME, -1);
122        SetBtnTouch(inputEventArray, FINGER_BLOCK_TIME, 0);
123        SetMtTouchFingerType(inputEventArray, FINGER_BLOCK_TIME,
124                             static_cast<int32_t>(touchPadInputEvent.groupNumber), 0);
125    }
126    SetSynReport(inputEventArray);
127}
128} // namespace MMI
129} // namespace OHOS
130