1 /*
2 * Copyright (c) 2021-2023 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 "key_event_normalize.h"
17
18 #include "input_device_manager.h"
19 #include "key_map_manager.h"
20 #include "key_unicode_transformation.h"
21
22 #undef MMI_LOG_DOMAIN
23 #define MMI_LOG_DOMAIN MMI_LOG_DISPATCH
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "KeyEventNormalize"
26
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 constexpr uint32_t KEYSTATUS { 0 };
31 } // namespace
32
KeyEventNormalize()33 KeyEventNormalize::KeyEventNormalize() {}
34
~KeyEventNormalize()35 KeyEventNormalize::~KeyEventNormalize() {}
36
GetKeyEvent()37 std::shared_ptr<KeyEvent> KeyEventNormalize::GetKeyEvent()
38 {
39 if (keyEvent_ == nullptr) {
40 keyEvent_ = KeyEvent::Create();
41 }
42 return keyEvent_;
43 }
44
Normalize(struct libinput_event * event,std::shared_ptr<KeyEvent> keyEvent)45 int32_t KeyEventNormalize::Normalize(struct libinput_event *event, std::shared_ptr<KeyEvent> keyEvent)
46 {
47 CALL_DEBUG_ENTER;
48 CHKPR(event, PARAM_INPUT_INVALID);
49 CHKPR(keyEvent, ERROR_NULL_POINTER);
50 keyEvent->UpdateId();
51 StartLogTraceId(keyEvent->GetId(), keyEvent->GetEventType(), keyEvent->GetKeyAction());
52 auto data = libinput_event_get_keyboard_event(event);
53 CHKPR(data, ERROR_NULL_POINTER);
54
55 auto device = libinput_event_get_device(event);
56 CHKPR(device, ERROR_NULL_POINTER);
57 int32_t deviceId = INPUT_DEV_MGR->FindInputDeviceId(device);
58 int32_t keyCode = static_cast<int32_t>(libinput_event_keyboard_get_key(data));
59 MMI_HILOGD("The linux input keyCode:%{private}d", keyCode);
60 keyCode = KeyMapMgr->TransferDeviceKeyValue(device, keyCode);
61 int32_t keyAction = (libinput_event_keyboard_get_key_state(data) == 0) ?
62 (KeyEvent::KEY_ACTION_UP) : (KeyEvent::KEY_ACTION_DOWN);
63 auto preAction = keyEvent->GetAction();
64 if (preAction == KeyEvent::KEY_ACTION_UP) {
65 std::optional<KeyEvent::KeyItem> preUpKeyItem = keyEvent->GetKeyItem();
66 if (preUpKeyItem) {
67 keyEvent->RemoveReleasedKeyItems(*preUpKeyItem);
68 } else {
69 MMI_HILOGE("The preUpKeyItem is nullopt");
70 }
71 }
72 uint64_t time = libinput_event_keyboard_get_time_usec(data);
73 keyEvent->SetActionTime(time);
74 keyEvent->SetAction(keyAction);
75 keyEvent->SetDeviceId(deviceId);
76 keyEvent->SetKeyCode(keyCode);
77 keyEvent->SetKeyAction(keyAction);
78 StartLogTraceId(keyEvent->GetId(), keyEvent->GetEventType(), keyEvent->GetKeyAction());
79 if (keyEvent->GetPressedKeys().empty()) {
80 keyEvent->SetActionStartTime(time);
81 }
82
83 KeyEvent::KeyItem item;
84 bool isKeyPressed = (libinput_event_keyboard_get_key_state(data) != KEYSTATUS);
85 item.SetDownTime(time);
86 item.SetKeyCode(keyCode);
87 item.SetDeviceId(deviceId);
88 item.SetPressed(isKeyPressed);
89 item.SetUnicode(KeyCodeToUnicode(keyCode, keyEvent));
90
91 HandleKeyAction(device, item, keyEvent);
92
93 int32_t keyIntention = KeyItemsTransKeyIntention(keyEvent->GetKeyItems());
94 keyEvent->SetKeyIntention(keyIntention);
95 return RET_OK;
96 }
97
HandleKeyAction(struct libinput_device * device,KeyEvent::KeyItem & item,std::shared_ptr<KeyEvent> keyEvent)98 void KeyEventNormalize::HandleKeyAction(struct libinput_device* device, KeyEvent::KeyItem &item,
99 std::shared_ptr<KeyEvent> keyEvent)
100 {
101 CHKPV(device);
102 CHKPV(keyEvent);
103 int32_t keyAction = keyEvent->GetAction();
104 int32_t keyCode = keyEvent->GetKeyCode();
105 if (keyAction == KeyEvent::KEY_ACTION_DOWN) {
106 keyEvent->AddPressedKeyItems(item);
107 }
108 if (keyAction == KeyEvent::KEY_ACTION_UP) {
109 int32_t funcKey = keyEvent->TransitionFunctionKey(keyCode);
110 if (funcKey != KeyEvent::UNKNOWN_FUNCTION_KEY) {
111 int32_t ret = keyEvent->SetFunctionKey(funcKey, libinput_get_funckey_state(device, funcKey));
112 if (ret == funcKey) {
113 MMI_HILOGD("Set function key:%{public}d to state:%{public}d succeed",
114 funcKey, keyEvent->GetFunctionKey(funcKey));
115 }
116 }
117 std::optional<KeyEvent::KeyItem> pressedKeyItem = keyEvent->GetKeyItem(keyCode);
118 if (pressedKeyItem) {
119 item.SetDownTime(pressedKeyItem->GetDownTime());
120 } else {
121 MMI_HILOGE("Find pressed key failed, keyCode:%{private}d", keyCode);
122 }
123 keyEvent->RemoveReleasedKeyItems(item);
124 keyEvent->AddPressedKeyItems(item);
125 }
126 }
127
ResetKeyEvent(struct libinput_device * device)128 void KeyEventNormalize::ResetKeyEvent(struct libinput_device* device)
129 {
130 if (INPUT_DEV_MGR->IsKeyboardDevice(device) || INPUT_DEV_MGR->IsPointerDevice(device)) {
131 if (keyEvent_ == nullptr) {
132 keyEvent_ = KeyEvent::Create();
133 }
134 if (libinput_has_event_led_type(device)) {
135 CHKPV(keyEvent_);
136 const std::vector<int32_t> funcKeys = {
137 KeyEvent::NUM_LOCK_FUNCTION_KEY,
138 KeyEvent::CAPS_LOCK_FUNCTION_KEY,
139 KeyEvent::SCROLL_LOCK_FUNCTION_KEY
140 };
141 for (const auto &funcKey : funcKeys) {
142 keyEvent_->SetFunctionKey(funcKey, libinput_get_funckey_state(device, funcKey));
143 }
144 }
145 }
146 }
147
SetShieldStatus(int32_t shieldMode,bool isShield)148 int32_t KeyEventNormalize::SetShieldStatus(int32_t shieldMode, bool isShield)
149 {
150 MMI_HILOGD("Last shield mode:%{public}d, set shield mode:%{public}d, status:%{public}d",
151 lastShieldMode_, shieldMode, isShield);
152 auto iter = shieldStatus_.find(lastShieldMode_);
153 if (isShield) {
154 if (lastShieldMode_ == shieldMode) {
155 MMI_HILOGD("Last shield mode equal with shield mode");
156 return RET_OK;
157 } else if (iter != shieldStatus_.end()) {
158 iter->second = false;
159 } else {
160 MMI_HILOGD("Last shield mode unset");
161 }
162 lastShieldMode_ = shieldMode;
163 } else if (lastShieldMode_ != shieldMode) {
164 MMI_HILOGD("Shield mode:%{public}d is already false", shieldMode);
165 return RET_OK;
166 } else {
167 MMI_HILOGD("lastShieldMode_ unset");
168 lastShieldMode_ = SHIELD_MODE::UNSET_MODE;
169 }
170 iter = shieldStatus_.find(shieldMode);
171 if (iter == shieldStatus_.end()) {
172 MMI_HILOGE("Find shieldMode:%{public}d failed", shieldMode);
173 return RET_ERR;
174 }
175 iter->second = isShield;
176 MMI_HILOGD("Last shield mode:%{public}d, set shield mode:%{public}d, status:%{public}d",
177 lastShieldMode_, shieldMode, isShield);
178 return RET_OK;
179 }
180
GetShieldStatus(int32_t shieldMode,bool & isShield)181 int32_t KeyEventNormalize::GetShieldStatus(int32_t shieldMode, bool &isShield)
182 {
183 CALL_DEBUG_ENTER;
184 auto iter = shieldStatus_.find(shieldMode);
185 if (iter == shieldStatus_.end()) {
186 MMI_HILOGE("Find shieldMode:%{public}d failed", shieldMode);
187 return RET_ERR;
188 }
189 isShield = iter->second;
190 MMI_HILOGD("Last shield mode:%{public}d, get shield mode:%{public}d, status:%{public}d",
191 lastShieldMode_, shieldMode, isShield);
192 return RET_OK;
193 }
194
GetCurrentShieldMode()195 int32_t KeyEventNormalize::GetCurrentShieldMode()
196 {
197 return lastShieldMode_;
198 }
SetCurrentShieldMode(int32_t shieldMode)199 void KeyEventNormalize::SetCurrentShieldMode(int32_t shieldMode)
200 {
201 lastShieldMode_ = shieldMode;
202 }
203 } // namespace MMI
204 } // namespace OHOS
205