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