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_keyboard_device.h"
17 
18 #undef MMI_LOG_TAG
19 #define MMI_LOG_TAG "ProcessingKeyboardDevice"
20 
21 namespace OHOS {
22 namespace MMI {
23 namespace {
24 constexpr int32_t EVENT_REPORT_COUNTS { 50 };
25 constexpr int64_t EVENT_REPORT_TIMES { 20 };
26 } // namespace
27 
TransformJsonDataToInputData(const DeviceItem & fingerEventArrays,InputEventArray & inputEventArray)28 int32_t ProcessingKeyboardDevice::TransformJsonDataToInputData(const DeviceItem& fingerEventArrays,
29     InputEventArray& inputEventArray)
30 {
31     CALL_DEBUG_ENTER;
32     std::vector<DeviceEvent> inputData = fingerEventArrays.events;
33     if (inputData.empty()) {
34         MMI_HILOGE("Manage KeyBoard array failed, inputData is empty.");
35         return RET_ERR;
36     }
37     std::vector<KeyBoardEvent> keyBoardEventArray;
38     if (AnalysisKeyBoardEvent(inputData, keyBoardEventArray) == RET_ERR) {
39         return RET_ERR;
40     }
41     TransformKeyBoardEventToInputEvent(keyBoardEventArray, inputEventArray);
42     return RET_OK;
43 }
44 
TransformKeyBoardEventToInputEvent(const std::vector<KeyBoardEvent> & keyBoardEventArray,InputEventArray & inputEventArray)45 void ProcessingKeyboardDevice::TransformKeyBoardEventToInputEvent(const std::vector<KeyBoardEvent>& keyBoardEventArray,
46                                                                   InputEventArray& inputEventArray)
47 {
48     for (const auto &item : keyBoardEventArray) {
49         if (item.eventType == "KEY_EVENT_PRESS") {
50             TransformKeyPressEvent(item, inputEventArray);
51         } else if (item.eventType == "KEY_EVENT_RELEASE") {
52             TransformKeyReleaseEvent(item, inputEventArray);
53         } else if (item.eventType == "KEY_EVENT_CLICK") {
54             TransformKeyClickEvent(item, inputEventArray);
55         } else if (item.eventType == "KEY_EVENT_LONG_PRESS") {
56             TransformKeyLongPressEvent(item, inputEventArray);
57         } else {
58             MMI_HILOGW("Json file format error");
59         }
60     }
61 }
62 
AnalysisKeyBoardEvent(const std::vector<DeviceEvent> & inputData,std::vector<KeyBoardEvent> & keyBoardEventArray)63 int32_t ProcessingKeyboardDevice::AnalysisKeyBoardEvent(const std::vector<DeviceEvent>& inputData,
64                                                         std::vector<KeyBoardEvent>& keyBoardEventArray)
65 {
66     KeyBoardEvent keyBoardEvent = {};
67     for (const auto &item : inputData) {
68         keyBoardEvent = {};
69         keyBoardEvent.eventType = item.eventType;
70         keyBoardEvent.keyValue = item.keyValue;
71         keyBoardEvent.blockTime = item.blockTime;
72         keyBoardEventArray.push_back(keyBoardEvent);
73     }
74 
75     return RET_OK;
76 }
77 
TransformKeyPressEvent(const KeyBoardEvent & keyBoardEvent,InputEventArray & inputEventArray)78 void ProcessingKeyboardDevice::TransformKeyPressEvent(const KeyBoardEvent& keyBoardEvent,
79                                                       InputEventArray& inputEventArray)
80 {
81     uint16_t keyValue = static_cast<uint16_t>(keyBoardEvent.keyValue);
82     SetKeyPressEvent(inputEventArray, keyBoardEvent.blockTime, keyValue);
83     SetSynReport(inputEventArray);
84 }
85 
TransformKeyLongPressEvent(const KeyBoardEvent & keyBoardEvent,InputEventArray & inputEventArray)86 void ProcessingKeyboardDevice::TransformKeyLongPressEvent(const KeyBoardEvent& keyBoardEvent,
87                                                           InputEventArray& inputEventArray)
88 {
89     uint16_t keyValue = static_cast<uint16_t>(keyBoardEvent.keyValue);
90     SetKeyPressEvent(inputEventArray, EVENT_REPORT_TIMES, keyValue);
91     SetSynReport(inputEventArray);
92     int32_t keyEventNum = (keyBoardEvent.blockTime / EVENT_REPORT_COUNTS) + 1;
93     int32_t count = 0;
94     while (count++ < keyEventNum) {
95         SetKeyLongPressEvent(inputEventArray, EVENT_REPORT_TIMES, keyValue);
96         SetSynConfigReport(inputEventArray, EVENT_REPORT_TIMES);
97     }
98     SetKeyReleaseEvent(inputEventArray, EVENT_REPORT_TIMES, keyValue);
99     SetSynReport(inputEventArray);
100 }
101 
TransformKeyReleaseEvent(const KeyBoardEvent & keyBoardEvent,InputEventArray & inputEventArray)102 void ProcessingKeyboardDevice::TransformKeyReleaseEvent(const KeyBoardEvent& keyBoardEvent,
103                                                         InputEventArray& inputEventArray)
104 {
105     uint16_t keyValue = static_cast<uint16_t>(keyBoardEvent.keyValue);
106     SetKeyReleaseEvent(inputEventArray, keyBoardEvent.blockTime, keyValue);
107     SetSynReport(inputEventArray);
108 }
109 
TransformKeyClickEvent(const KeyBoardEvent & keyBoardEvent,InputEventArray & inputEventArray)110 void ProcessingKeyboardDevice::TransformKeyClickEvent(const KeyBoardEvent& keyBoardEvent,
111                                                       InputEventArray& inputEventArray)
112 {
113     uint16_t keyValue = static_cast<uint16_t>(keyBoardEvent.keyValue);
114     SetKeyPressEvent(inputEventArray, keyBoardEvent.blockTime, keyValue);
115     SetSynReport(inputEventArray);
116     SetKeyReleaseEvent(inputEventArray, keyBoardEvent.blockTime, keyValue);
117     SetSynReport(inputEventArray);
118 }
119 } // namespace MMI
120 } // namespace OHOS