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 "processing_touch_screen_device.h"
17 
18 #undef MMI_LOG_TAG
19 #define MMI_LOG_TAG "ProcessingTouchScreenDevice"
20 
21 namespace OHOS {
22 namespace MMI {
TransformJsonDataToInputData(const DeviceItem & touchScreenEventArrays,InputEventArray & inputEventArray)23 int32_t ProcessingTouchScreenDevice::TransformJsonDataToInputData(const DeviceItem &touchScreenEventArrays,
24                                                                   InputEventArray &inputEventArray)
25 {
26     CALL_DEBUG_ENTER;
27     if (!touchScreenEventArrays.events.empty()) {
28         return TransformJsonDataSingleTouchScreen(touchScreenEventArrays, inputEventArray);
29     }
30     std::vector<DeviceEvent> inputData = touchScreenEventArrays.events;
31     if (inputData.empty()) {
32         MMI_HILOGE("Manage touchScreen array failed, inputData is empty.");
33         return RET_ERR;
34     }
35     TouchScreenInputEvents touchScreenInputEvents = {};
36     AnalysisTouchScreenDate(inputData, touchScreenInputEvents);
37     TouchScreenInputEvent pressEvents = touchScreenInputEvents.eventArray[0];
38     AnalysisTouchScreenPressData(inputEventArray, pressEvents);
39     for (uint64_t i = 1; i < static_cast<uint64_t>(touchScreenInputEvents.eventNumber); i++) {
40         AnalysisTouchScreenMoveData(inputEventArray, touchScreenInputEvents.eventArray[i]);
41     }
42     uint64_t releaseEventIndex = static_cast<uint64_t>(touchScreenInputEvents.eventNumber) - 1;
43     TouchScreenInputEvent releaseEvents = touchScreenInputEvents.eventArray[releaseEventIndex];
44     AnalysisTouchScreenReleaseData(inputEventArray, releaseEvents);
45     return RET_OK;
46 }
47 
TransformJsonDataSingleTouchScreen(const DeviceItem & touchScreenEventArrays,InputEventArray & inputEventArray)48 int32_t ProcessingTouchScreenDevice::TransformJsonDataSingleTouchScreen(const DeviceItem &touchScreenEventArrays,
49     InputEventArray &inputEventArray)
50 {
51     CALL_DEBUG_ENTER;
52     std::vector<DeviceEvent> inputData = touchScreenEventArrays.events;
53     if (inputData.empty()) {
54         MMI_HILOGE("Manage touchScreen array failed, inputData is empty.");
55         return RET_ERR;
56     }
57 
58     std::vector<TouchSingleEventData> touchSingleEventDatas;
59     AnalysisSingleTouchScreenDate(inputData, touchSingleEventDatas);
60     for (const auto &item : touchSingleEventDatas) {
61         AnalysisTouchScreenToInputData(inputEventArray, item);
62     }
63     return RET_OK;
64 }
65 
AnalysisTouchScreenDate(const std::vector<DeviceEvent> & inputData,TouchScreenInputEvents & touchScreenInputEvents)66 void ProcessingTouchScreenDevice::AnalysisTouchScreenDate(const std::vector<DeviceEvent> &inputData,
67                                                           TouchScreenInputEvents &touchScreenInputEvents)
68 {
69     TouchScreenCoordinates touchScreenCoordinates = {};
70     TouchScreenInputEvent touchScreenInputEvent = {};
71     for (const auto &item : inputData) {
72         touchScreenInputEvent.groupNumber = 0;
73         for (auto &posXYItem : item.posXY) {
74             touchScreenCoordinates.xPos = posXYItem.xPos;
75             touchScreenCoordinates.yPos = posXYItem.yPos;
76             touchScreenInputEvent.events.push_back(touchScreenCoordinates);
77             ++touchScreenInputEvent.groupNumber;
78         }
79         touchScreenInputEvents.eventNumber = static_cast<uint32_t>(inputData.size());
80         touchScreenInputEvents.eventArray.push_back(touchScreenInputEvent);
81         touchScreenInputEvent.events.clear();
82     }
83 }
84 
AnalysisSingleTouchScreenDate(const std::vector<DeviceEvent> & inputData,std::vector<TouchSingleEventData> & touchSingleEventDatas)85 void ProcessingTouchScreenDevice::AnalysisSingleTouchScreenDate(const std::vector<DeviceEvent> &inputData,
86     std::vector<TouchSingleEventData> &touchSingleEventDatas)
87 {
88     TouchSingleEventData touchSingleEventData = {};
89     for (auto &item : inputData) {
90         touchSingleEventData = {};
91         touchSingleEventData.eventType = item.eventType;
92         touchSingleEventData.trackingId = item.trackingId;
93         if (touchSingleEventData.eventType != "release") {
94             touchSingleEventData.xPos = item.xPos;
95             touchSingleEventData.yPos = item.yPos;
96         }
97         touchSingleEventData.blockTime = item.blockTime;
98         touchSingleEventData.reportType = item.reportType;
99         touchSingleEventDatas.push_back(touchSingleEventData);
100     }
101 }
102 
AnalysisTouchScreenPressData(InputEventArray & inputEventArray,const TouchScreenInputEvent & touchScreenInputEvent)103 void ProcessingTouchScreenDevice::AnalysisTouchScreenPressData(InputEventArray &inputEventArray,
104                                                                const TouchScreenInputEvent &touchScreenInputEvent)
105 {
106     int32_t yPos = 0;
107     int32_t xPos = 0;
108     for (uint32_t i = 0; i < touchScreenInputEvent.groupNumber; i++) {
109         yPos = touchScreenInputEvent.events[i].yPos;
110         xPos = touchScreenInputEvent.events[i].xPos;
111         SetTrackingId(inputEventArray, 0, static_cast<int32_t>(i + 1));
112         SetSynMtReport(inputEventArray, 0);
113         SetPositionY(inputEventArray, 0, yPos);
114         SetPositionX(inputEventArray, 0, xPos);
115         SetBtnTouch(inputEventArray, 0, 1);
116     }
117     SetSynReport(inputEventArray);
118 }
119 
AnalysisTouchScreenMoveData(InputEventArray & inputEventArray,const TouchScreenInputEvent & touchScreenInputEvent)120 void ProcessingTouchScreenDevice::AnalysisTouchScreenMoveData(InputEventArray &inputEventArray,
121                                                               const TouchScreenInputEvent &touchScreenInputEvent)
122 {
123     int32_t xPos = 0;
124     int32_t yPos = 0;
125     for (uint32_t i = 0; i < touchScreenInputEvent.groupNumber; i++) {
126         xPos = touchScreenInputEvent.events[i].xPos;
127         yPos = touchScreenInputEvent.events[i].yPos;
128         SetPositionX(inputEventArray, 0, xPos);
129         SetPositionY(inputEventArray, 0, yPos);
130         SetTrackingId(inputEventArray, 0, static_cast<int32_t>(i + 1));
131         SetSynMtReport(inputEventArray, 0);
132     }
133     SetSynReport(inputEventArray);
134 }
135 
AnalysisTouchScreenReleaseData(InputEventArray & inputEventArray,const TouchScreenInputEvent & touchScreenInputEvent)136 void ProcessingTouchScreenDevice::AnalysisTouchScreenReleaseData(InputEventArray &inputEventArray,
137                                                                  const TouchScreenInputEvent &touchScreenInputEvent)
138 {
139     for (uint32_t i = 0; i < touchScreenInputEvent.groupNumber; i++) {
140         SetTrackingId(inputEventArray, 0, static_cast<int32_t>(i + 1));
141         SetBtnTouch(inputEventArray, 0, 0);
142         SetSynMtReport(inputEventArray, 0);
143     }
144     SetSynReport(inputEventArray);
145     SetSynMtReport(inputEventArray, 0);
146     SetSynReport(inputEventArray);
147 }
148 
AnalysisTouchScreenToInputData(InputEventArray & inputEventArray,const TouchSingleEventData & touchSingleEventData)149 void ProcessingTouchScreenDevice::AnalysisTouchScreenToInputData(InputEventArray &inputEventArray,
150                                                                  const TouchSingleEventData &touchSingleEventData)
151 {
152     if (touchSingleEventData.eventType == "press") {
153         AnalysisTouchScreenPressData(inputEventArray, touchSingleEventData);
154     } else if (touchSingleEventData.eventType == "move") {
155         AnalysisTouchScreenMoveData(inputEventArray, touchSingleEventData);
156     } else if (touchSingleEventData.eventType == "release") {
157         AnalysisTouchScreenReleaseData(inputEventArray, touchSingleEventData);
158     }
159 }
160 
AnalysisTouchScreenPressData(InputEventArray & inputEventArray,const TouchSingleEventData & touchSingleEventData)161 void ProcessingTouchScreenDevice::AnalysisTouchScreenPressData(InputEventArray &inputEventArray,
162                                                                const TouchSingleEventData &touchSingleEventData)
163 {
164     SetPositionX(inputEventArray, 0, touchSingleEventData.xPos);
165     SetPositionY(inputEventArray, 0, touchSingleEventData.yPos);
166     SetTrackingId(inputEventArray, 0, touchSingleEventData.trackingId);
167     SetBtnTouch(inputEventArray, 0, 1);
168     if (touchSingleEventData.reportType == "mtReport") {
169         SetSynMtReport(inputEventArray, 0);
170     } else if (touchSingleEventData.reportType == "synReport") {
171         SetSynMtReport(inputEventArray, 0);
172         SetSynReport(inputEventArray, touchSingleEventData.blockTime);
173     }
174 }
175 
AnalysisTouchScreenMoveData(InputEventArray & inputEventArray,const TouchSingleEventData & touchSingleEventData)176 void ProcessingTouchScreenDevice::AnalysisTouchScreenMoveData(InputEventArray &inputEventArray,
177                                                               const TouchSingleEventData &touchSingleEventData)
178 {
179     SetPositionX(inputEventArray, 0, touchSingleEventData.xPos);
180     SetPositionY(inputEventArray, 0, touchSingleEventData.yPos);
181     SetTrackingId(inputEventArray, 0, touchSingleEventData.trackingId);
182     if (touchSingleEventData.reportType == "mtReport") {
183         SetSynMtReport(inputEventArray, 0);
184     } else if (touchSingleEventData.reportType == "synReport") {
185         SetSynMtReport(inputEventArray, 0);
186         SetSynReport(inputEventArray, touchSingleEventData.blockTime);
187     }
188 }
189 
AnalysisTouchScreenReleaseData(InputEventArray & inputEventArray,const TouchSingleEventData & touchSingleEventData)190 void ProcessingTouchScreenDevice::AnalysisTouchScreenReleaseData(InputEventArray &inputEventArray,
191                                                                  const TouchSingleEventData &touchSingleEventData)
192 {
193     SetTrackingId(inputEventArray, 0, touchSingleEventData.trackingId);
194     SetBtnTouch(inputEventArray, 0, 0);
195     if (touchSingleEventData.reportType == "mtReport") {
196         SetSynMtReport(inputEventArray, 0);
197     } else if (touchSingleEventData.reportType == "synReport") {
198         SetSynMtReport(inputEventArray, 0);
199         SetSynReport(inputEventArray);
200         SetSynMtReport(inputEventArray, 0);
201         SetSynReport(inputEventArray, touchSingleEventData.blockTime);
202     }
203 }
204 } // namespace MMI
205 } // namespace OHOS