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_pen_device.h"
17
18 using namespace OHOS::MMI;
19
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "ProcessingPenDevice" };
22 } // namespace
23
TransformJsonDataToInputData(const Json & penEventArrays,InputEventArray & inputEventArray)24 int32_t ProcessingPenDevice::TransformJsonDataToInputData(const Json& penEventArrays,
25 InputEventArray& inputEventArray)
26 {
27 MMI_LOGD("Enter");
28 if (penEventArrays.empty()) {
29 return RET_ERR;
30 }
31 if (penEventArrays.find("events") == penEventArrays.end()) {
32 MMI_LOGE("manage pen array faild, inputData is empty.");
33 return RET_ERR;
34 }
35 Json inputData = penEventArrays.at("events");
36 if (inputData.empty()) {
37 MMI_LOGE("manage pen array faild, inputData is empty.");
38 return RET_ERR;
39 }
40 std::vector<PenEvent> penEventArray;
41 if (AnalysisPenPadEvent(inputData, penEventArray) == RET_ERR) {
42 MMI_LOGE("AnalysisPenPadEvent error.");
43 return RET_ERR;
44 }
45 TransformPenEventToInputEvent(penEventArray, inputEventArray);
46 MMI_LOGD("Leave");
47 return RET_OK;
48 }
49
TransformPenEventToInputEvent(const std::vector<PenEvent> & penEventArray,InputEventArray & inputEventArray)50 void ProcessingPenDevice::TransformPenEventToInputEvent(const std::vector<PenEvent>& penEventArray,
51 InputEventArray& inputEventArray)
52 {
53 SetPenApproachPadEvent(penEventArray[0], inputEventArray);
54 for (const auto &item : penEventArray) {
55 SetPenSlidePadEvent(item, inputEventArray);
56 }
57 uint64_t lastEventIndex = penEventArray.size() - 1;
58 SetPenLeavePadEvent(penEventArray[lastEventIndex], inputEventArray);
59 }
60
SetPenApproachPadEvent(const PenEvent & penEvent,InputEventArray & inputEventArray)61 void ProcessingPenDevice::SetPenApproachPadEvent(const PenEvent& penEvent, InputEventArray& inputEventArray)
62 {
63 SetEvAbsX(inputEventArray, 0, penEvent.xPos);
64 SetEvAbsY(inputEventArray, 0, penEvent.yPos);
65 SetAbsTiltX(inputEventArray, 0, penEvent.tiltX);
66 SetAbsTiltY(inputEventArray, 0, penEvent.tiltY);
67 SetEvAbsZ(inputEventArray, 0, EV_ABS_Z_DEFAULT_VALUE);
68 SetAbsDistance(inputEventArray, 0, penEvent.distance);
69 if (penEvent.eventType == "PEN_TOUCH") {
70 SetBtnPen(inputEventArray, 0, 1);
71 } else if (penEvent.eventType == "RUBBER_TOUCH") {
72 SetBtnRubber(inputEventArray, 0, 1);
73 } else {
74 // nothing to do.
75 }
76
77 SetMscSerial(inputEventArray, 0);
78 SetAbsMisc(inputEventArray, 0, EV_ABS_MISC_DEFAULT_VALUE);
79 SetSynReport(inputEventArray);
80 }
81
SetPenSlidePadEvent(const PenEvent & penEvent,InputEventArray & inputEventArray)82 void ProcessingPenDevice::SetPenSlidePadEvent(const PenEvent& penEvent, InputEventArray& inputEventArray)
83 {
84 if (penEvent.eventType == "PEN_KEY") {
85 SetBtnStylus(inputEventArray, 0, static_cast<uint16_t>(penEvent.keyValue), penEvent.keyStatus);
86 return;
87 }
88 if (penEvent.distance == 0) {
89 SetMscSerial(inputEventArray, 0);
90 SetSynReport(inputEventArray, 0);
91 return;
92 }
93 SetEvAbsX(inputEventArray, 0, penEvent.xPos);
94 SetEvAbsY(inputEventArray, 0, penEvent.yPos);
95 static int32_t previousPressure = 0;
96 if (penEvent.pressure > 0) {
97 if (previousPressure == 0) {
98 SetAbsPressure(inputEventArray, 0, penEvent.pressure);
99 SetBtnTouch(inputEventArray, 0, 1);
100 } else if (previousPressure > 0) {
101 SetAbsPressure(inputEventArray, 0, penEvent.pressure);
102 } else {
103 // nothing to do.
104 }
105 } else if ((penEvent.pressure == 0) && (previousPressure > 0)) {
106 SetAbsPressure(inputEventArray, 0, penEvent.pressure);
107 SetBtnTouch(inputEventArray, 0, 0);
108 } else {
109 // nothing to do.
110 }
111 previousPressure = penEvent.pressure;
112 SetAbsDistance(inputEventArray, 0, penEvent.distance);
113 SetAbsTiltX(inputEventArray, 0, penEvent.tiltX);
114 SetAbsTiltY(inputEventArray, 0, penEvent.tiltY);
115 SetMscSerial(inputEventArray, 0);
116 SetSynReport(inputEventArray);
117 }
118
SetPenLeavePadEvent(const PenEvent & penEvent,InputEventArray & inputEventArray)119 void ProcessingPenDevice::SetPenLeavePadEvent(const PenEvent& penEvent, InputEventArray& inputEventArray)
120 {
121 SetEvAbsX(inputEventArray, 0);
122 SetEvAbsY(inputEventArray, 0);
123 SetAbsTiltX(inputEventArray, 0);
124 SetAbsTiltY(inputEventArray, 0);
125 SetEvAbsZ(inputEventArray, 0);
126 SetAbsDistance(inputEventArray, 0, 0);
127 if (penEvent.eventType == "PEN_TOUCH") {
128 SetBtnPen(inputEventArray, 0, 0);
129 } else if (penEvent.eventType == "RUBBER_TOUCH") {
130 SetBtnRubber(inputEventArray, 0, 0);
131 } else {
132 // nothing to do.
133 }
134
135 SetMscSerial(inputEventArray, 0);
136 SetAbsMisc(inputEventArray, 0, 0);
137 SetSynReport(inputEventArray);
138 }
139
AnalysisPenPadEvent(const Json & inputData,std::vector<PenEvent> & penEventArray)140 int32_t ProcessingPenDevice::AnalysisPenPadEvent(const Json& inputData, std::vector<PenEvent>& penEventArray)
141 {
142 if (inputData.empty()) {
143 return RET_ERR;
144 }
145 uint64_t endEventIndex = inputData.size() - 1;
146 if (AnalysisPenApproachPadEvent(inputData[0], penEventArray) == RET_ERR) {
147 return RET_ERR;
148 }
149 for (uint64_t i = 1; i < endEventIndex; i++) {
150 if (AnalysisPenSlidePadEvent(inputData[i], penEventArray) == RET_ERR) {
151 return RET_ERR;
152 }
153 }
154 if (AnalysisPenLeavePadEvent(inputData[endEventIndex], penEventArray) == RET_ERR) {
155 return RET_ERR;
156 }
157
158 return RET_OK;
159 }
160
AnalysisPenApproachPadEvent(const Json & event,std::vector<PenEvent> & penEventArray)161 int32_t ProcessingPenDevice::AnalysisPenApproachPadEvent(const Json& event, std::vector<PenEvent>& penEventArray)
162 {
163 if (event.empty()) {
164 MMI_LOGE("AnalysisPenApproachPadEvent is empty.");
165 return RET_ERR;
166 }
167 PenEvent penEvent = {};
168 penEvent.eventType = event.at("eventType").get<std::string>();
169 if ((penEvent.eventType != "RUBBER_TOUCH") && (penEvent.eventType != "PEN_TOUCH")) {
170 MMI_LOGE("Enter the correct event type in the configuration file.");
171 return RET_ERR;
172 }
173 penEvent.xPos = event.at("xPos").get<int32_t>();
174 penEvent.yPos = event.at("yPos").get<int32_t>();
175 penEvent.tiltX = event.at("tiltX").get<int32_t>();
176 penEvent.tiltY = event.at("tiltY").get<int32_t>();
177 penEvent.pressure = event.at("pressure").get<int32_t>();
178 penEvent.distance = event.at("distance").get<int32_t>();
179 penEventArray.push_back(penEvent);
180
181 return RET_OK;
182 }
183
AnalysisPenSlidePadEvent(const Json & event,std::vector<PenEvent> & penEventArray)184 int32_t ProcessingPenDevice::AnalysisPenSlidePadEvent(const Json& event, std::vector<PenEvent>& penEventArray)
185 {
186 if (event.empty()) {
187 MMI_LOGE("AnalysisPenSlidePadEvent is empty.");
188 return RET_ERR;
189 }
190 PenEvent penEvent = {};
191 penEvent.eventType = event.at("eventType").get<std::string>();
192 if (penEvent.eventType == "PEN_KEY") {
193 penEvent.keyValue = event.at("keyValue").get<int32_t>();
194 penEvent.keyStatus = event.at("keyStatus").get<int32_t>();
195 } else if ((penEvent.eventType == "PEN_TOUCH") || (penEvent.eventType == "RUBBER_TOUCH")) {
196 penEvent.xPos = event.at("xPos").get<int32_t>();
197 penEvent.yPos = event.at("yPos").get<int32_t>();
198 penEvent.tiltX = event.at("tiltX").get<int32_t>();
199 penEvent.tiltY = event.at("tiltY").get<int32_t>();
200 penEvent.pressure = event.at("pressure").get<int32_t>();
201 penEvent.distance = event.at("distance").get<int32_t>();
202 } else {
203 // nothing to do.
204 }
205 penEventArray.push_back(penEvent);
206
207 return RET_OK;
208 }
209
AnalysisPenLeavePadEvent(const Json & event,std::vector<PenEvent> & penEventArray)210 int32_t ProcessingPenDevice::AnalysisPenLeavePadEvent(const Json& event, std::vector<PenEvent>& penEventArray)
211 {
212 if (event.empty()) {
213 MMI_LOGE("AnalysisPenLeavePadEvent is empty.");
214 return RET_ERR;
215 }
216 PenEvent penEvent = {};
217 penEvent.eventType = event.at("eventType").get<std::string>();
218 if ((penEvent.eventType != "RUBBER_TOUCH") && (penEvent.eventType != "PEN_TOUCH")) {
219 MMI_LOGE("Enter the correct event type in the configuration file.");
220 return RET_ERR;
221 }
222 penEvent.xPos = event.at("xPos").get<int32_t>();
223 penEvent.yPos = event.at("yPos").get<int32_t>();
224 penEvent.tiltX = event.at("tiltX").get<int32_t>();
225 penEvent.tiltY = event.at("tiltY").get<int32_t>();
226 penEvent.pressure = event.at("pressure").get<int32_t>();
227 penEvent.distance = event.at("distance").get<int32_t>();
228 penEventArray.push_back(penEvent);
229
230 return RET_OK;
231 }