1 /*
2 * Copyright (c) 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 "key_auto_repeat.h"
17
18 #include <array>
19
20 #include "define_multimodal.h"
21 #include "error_multimodal.h"
22 #include "input_device_manager.h"
23 #include "input_event_handler.h"
24 #include "mmi_log.h"
25 #include "timer_manager.h"
26
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "KeyAutoRepeat" };
31 constexpr int32_t INVALID_DEVICE_ID = -1;
32 constexpr int32_t OPEN_AUTO_REPEAT = 1;
33 } // namespace
34
KeyAutoRepeat()35 KeyAutoRepeat::KeyAutoRepeat() {}
~KeyAutoRepeat()36 KeyAutoRepeat::~KeyAutoRepeat() {}
37
GetDeviceConfig() const38 std::map<int32_t, DeviceConfig> KeyAutoRepeat::GetDeviceConfig() const
39 {
40 return deviceConfig_;
41 }
42
AddDeviceConfig(struct libinput_device * device)43 int32_t KeyAutoRepeat::AddDeviceConfig(struct libinput_device *device)
44 {
45 CALL_DEBUG_ENTER;
46 CHKPR(device, ERROR_NULL_POINTER);
47 std::string fileName = KeyMapMgr->GetKeyEventFileName(device);
48 DeviceConfig devConf;
49 auto ret = ReadTomlFile(GetTomlFilePath(fileName), devConf);
50 if (ret == RET_ERR) {
51 MMI_HILOGI("Can not read device config file");
52 return RET_ERR;
53 }
54 int32_t deviceId = InputDevMgr->FindInputDeviceId(device);
55 if (deviceId == INVALID_DEVICE_ID) {
56 MMI_HILOGE("Find to device failed");
57 return RET_ERR;
58 }
59 deviceConfig_[deviceId] = devConf;
60 return RET_OK;
61 }
62
SelectAutoRepeat(std::shared_ptr<KeyEvent> & keyEvent)63 void KeyAutoRepeat::SelectAutoRepeat(std::shared_ptr<KeyEvent>& keyEvent)
64 {
65 CALL_DEBUG_ENTER;
66 CHKPV(keyEvent);
67 DeviceConfig devConf = GetAutoSwitch(keyEvent->GetDeviceId());
68 if (devConf.autoSwitch != OPEN_AUTO_REPEAT) {
69 return;
70 }
71 keyEvent_ = keyEvent;
72 if (keyEvent_->GetKeyAction() == KeyEvent::KEY_ACTION_DOWN) {
73 if (TimerMgr->IsExist(timerId_)) {
74 MMI_HILOGI("Keyboard down but timer exists, timerId:%{public}d, keyCode:%{public}d",
75 timerId_, keyEvent_->GetKeyCode());
76 TimerMgr->RemoveTimer(timerId_);
77 timerId_ = -1;
78 }
79 AddHandleTimer(devConf.delayTime);
80 repeatKeyCode_ = keyEvent_->GetKeyCode();
81 MMI_HILOGI("Add a timer, keyCode:%{public}d", keyEvent_->GetKeyCode());
82 }
83 if (keyEvent_->GetKeyAction() == KeyEvent::KEY_ACTION_UP && TimerMgr->IsExist(timerId_)) {
84 TimerMgr->RemoveTimer(timerId_);
85 timerId_ = -1;
86 MMI_HILOGI("Stop kayboard autorepeat, keyCode:%{public}d", keyEvent_->GetKeyCode());
87 if (repeatKeyCode_ != keyEvent_->GetKeyCode()) {
88 auto pressedKeyItem = keyEvent_->GetKeyItem(keyEvent_->GetKeyCode());
89 if (pressedKeyItem != nullptr) {
90 keyEvent_->RemoveReleasedKeyItems(*pressedKeyItem);
91 } else {
92 MMI_HILOGW("The pressedKeyItem is nullptr");
93 }
94 keyEvent_->SetKeyCode(repeatKeyCode_);
95 keyEvent_->SetAction(KeyEvent::KEY_ACTION_DOWN);
96 keyEvent_->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
97 AddHandleTimer(devConf.delayTime);
98 MMI_HILOGD("The end keyboard autorepeat, keyCode:%{public}d", keyEvent_->GetKeyCode());
99 }
100 }
101 }
102
AddHandleTimer(int32_t timeout)103 void KeyAutoRepeat::AddHandleTimer(int32_t timeout)
104 {
105 CALL_DEBUG_ENTER;
106 timerId_ = TimerMgr->AddTimer(timeout, 1, [this]() {
107 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
108 auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
109 CHKPV(inputEventNormalizeHandler);
110 inputEventNormalizeHandler->HandleKeyEvent(this->keyEvent_);
111 this->keyEvent_->UpdateId();
112 #endif // OHOS_BUILD_ENABLE_KEYBOARD
113 int32_t triggertime = KeyRepeat->GetIntervalTime(keyEvent_->GetDeviceId());
114 this->AddHandleTimer(triggertime);
115 });
116 }
117
GetTomlFilePath(const std::string & fileName) const118 std::string KeyAutoRepeat::GetTomlFilePath(const std::string &fileName) const
119 {
120 return "/vendor/etc/keymap/" + fileName + ".TOML";
121 }
122
GetIntervalTime(int32_t deviceId) const123 int32_t KeyAutoRepeat::GetIntervalTime(int32_t deviceId) const
124 {
125 auto iter = deviceConfig_.find(deviceId);
126 int32_t triggertime = 100;
127 if (iter != deviceConfig_.end()) {
128 triggertime = iter->second.intervalTime;
129 }
130 return triggertime;
131 }
132
GetAutoSwitch(int32_t deviceId)133 DeviceConfig KeyAutoRepeat::GetAutoSwitch(int32_t deviceId)
134 {
135 auto iter = deviceConfig_.find(deviceId);
136 if (iter == deviceConfig_.end()) {
137 return {};
138 }
139 MMI_HILOGD("Open autorepeat:%{public}d", iter->second.autoSwitch);
140 return iter->second;
141 }
142
RemoveDeviceConfig(struct libinput_device * device)143 void KeyAutoRepeat::RemoveDeviceConfig(struct libinput_device *device)
144 {
145 CALL_DEBUG_ENTER;
146 CHKPV(device);
147 int32_t deviceId = InputDevMgr->FindInputDeviceId(device);
148 auto iter = deviceConfig_.find(deviceId);
149 if (iter == deviceConfig_.end()) {
150 MMI_HILOGI("Can not remove device config file");
151 return;
152 }
153 deviceConfig_.erase(iter);
154 }
155
RemoveTimer()156 void KeyAutoRepeat::RemoveTimer()
157 {
158 CALL_DEBUG_ENTER;
159 TimerMgr->RemoveTimer(timerId_);
160 }
161 } // namespace MMI
162 } // namespace OHOS