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