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