• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "event_log_helper.h"
23 #include "input_device_manager.h"
24 #include "input_event_handler.h"
25 #include "i_preference_manager.h"
26 #include "mmi_log.h"
27 #include "timer_manager.h"
28 
29 #undef MMI_LOG_DOMAIN
30 #define MMI_LOG_DOMAIN MMI_LOG_HANDLER
31 #undef MMI_LOG_TAG
32 #define MMI_LOG_TAG "KeyAutoRepeat"
33 
34 namespace OHOS {
35 namespace MMI {
36 namespace {
37 constexpr int32_t INVALID_DEVICE_ID { -1 };
38 constexpr int32_t OPEN_AUTO_REPEAT { 1 };
39 constexpr int32_t DEFAULT_KEY_REPEAT_DELAY { 500 };
40 constexpr int32_t MIN_KEY_REPEAT_DELAY { 300 };
41 constexpr int32_t MAX_KEY_REPEAT_DELAY { 1000 };
42 constexpr int32_t DEFAULT_KEY_REPEAT_RATE { 50 };
43 constexpr int32_t MIN_KEY_REPEAT_RATE { 36 };
44 constexpr int32_t MAX_KEY_REPEAT_RATE { 100 };
45 const std::string KEYBOARD_FILE_NAME { "keyboard_settings.xml" };
46 } // namespace
47 
KeyAutoRepeat()48 KeyAutoRepeat::KeyAutoRepeat() {}
~KeyAutoRepeat()49 KeyAutoRepeat::~KeyAutoRepeat() {}
50 
GetDeviceConfig() const51 std::map<int32_t, DeviceConfig> KeyAutoRepeat::GetDeviceConfig() const
52 {
53     return deviceConfig_;
54 }
55 
AddDeviceConfig(struct libinput_device * device)56 int32_t KeyAutoRepeat::AddDeviceConfig(struct libinput_device *device)
57 {
58     CALL_DEBUG_ENTER;
59     CHKPR(device, ERROR_NULL_POINTER);
60     std::string fileName = KeyMapMgr->GetKeyEventFileName(device);
61     DeviceConfig devConf;
62     if (ReadTomlFile(GetTomlFilePath(fileName), devConf) != RET_OK) {
63         MMI_HILOGI("Can not read device config file");
64         return RET_ERR;
65     }
66     int32_t deviceId = INPUT_DEV_MGR->FindInputDeviceId(device);
67     if (deviceId == INVALID_DEVICE_ID) {
68         MMI_HILOGE("Find to device failed");
69         return RET_ERR;
70     }
71     deviceConfig_[deviceId] = devConf;
72     return RET_OK;
73 }
74 
JudgeKeyEvent(const std::shared_ptr<KeyEvent> & keyEvent)75 bool KeyAutoRepeat::JudgeKeyEvent(const std::shared_ptr<KeyEvent>& keyEvent)
76 {
77     return (keyEvent->GetKeyAction() == KeyEvent::KEY_ACTION_UP) || (keyEvent->GetKeyAction() ==
78             KeyEvent::KEY_ACTION_CANCEL);
79 }
80 
JudgeLimitPrint(const std::shared_ptr<KeyEvent> & keyEvent)81 bool KeyAutoRepeat::JudgeLimitPrint(const std::shared_ptr<KeyEvent>& keyEvent)
82 {
83     return !EventLogHelper::IsBetaVersion() || keyEvent->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE);
84 }
85 
SelectAutoRepeat(const std::shared_ptr<KeyEvent> & keyEvent)86 void KeyAutoRepeat::SelectAutoRepeat(const std::shared_ptr<KeyEvent>& keyEvent)
87 {
88     CALL_DEBUG_ENTER;
89     CHKPV(keyEvent);
90     DeviceConfig devConf = GetAutoSwitch(keyEvent->GetDeviceId());
91     MMI_HILOGD("AutoRepeatSwitch:%{public}d, keyEvent flag:%{public}x", devConf.autoSwitch, keyEvent->GetFlag());
92     if (devConf.autoSwitch != OPEN_AUTO_REPEAT && !keyEvent->HasFlag(InputEvent::EVENT_FLAG_SIMULATE)) {
93         MMI_HILOGI("AutoRepeatSwitch not open and is not simulate event");
94         return;
95     }
96     keyEvent_ = keyEvent;
97     if (keyEvent_->GetKeyAction() == KeyEvent::KEY_ACTION_DOWN) {
98         if (TimerMgr->IsExist(timerId_)) {
99             if (!EventLogHelper::IsBetaVersion()) {
100                 MMI_HILOGI("Keyboard down but timer exists, timerId:%{public}d", timerId_);
101             } else {
102                 if (keyEvent_->HasFlag(InputEvent::EVENT_FLAG_PRIVACY_MODE)) {
103                     MMI_HILOGI("Keyboard down but timer exists, timerId:%{public}d, keyCode:%d",
104                         timerId_, keyEvent_->GetKeyCode());
105                 } else {
106                     MMI_HILOGI("Keyboard down but timer exists, timerId:%{public}d, keyCode:%d",
107                         timerId_, keyEvent_->GetKeyCode());
108                 }
109             }
110             TimerMgr->RemoveTimer(timerId_);
111             timerId_ = -1;
112         }
113         int32_t delayTime = GetDelayTime();
114         AddHandleTimer(delayTime);
115         repeatKeyCode_ = keyEvent_->GetKeyCode();
116     }
117     if (JudgeKeyEvent(keyEvent_) && TimerMgr->IsExist(timerId_)) {
118         TimerMgr->RemoveTimer(timerId_);
119         if (!JudgeLimitPrint(keyEvent_)) {
120             MMI_HILOGI("Stop autorepeat, keyCode:%{private}d, repeatKeyCode:%{private}d, keyAction: %{public}d,"
121                        "timeId:%{public}d",
122                 keyEvent_->GetKeyCode(), repeatKeyCode_, keyEvent_->GetKeyAction(), timerId_);
123         } else {
124             MMI_HILOGI("Stop autorepeat, keyCode:%d, repeatKeyCode:%d, keyAction: %d, timeId:%d",
125                 keyEvent_->GetKeyCode(), repeatKeyCode_, keyEvent_->GetKeyAction(), timerId_);
126         }
127         timerId_ = -1;
128         if (keyEvent_->GetKeyAction() == KeyEvent::KEY_ACTION_UP && repeatKeyCode_ != keyEvent_->GetKeyCode()) {
129             std::optional<KeyEvent::KeyItem> pressedKeyItem = keyEvent_->GetKeyItem(keyEvent_->GetKeyCode());
130             if (pressedKeyItem) {
131                 keyEvent_->RemoveReleasedKeyItems(*pressedKeyItem);
132             } else {
133                 MMI_HILOGW("The pressedKeyItem is nullopt");
134             }
135             pressedKeyItem = keyEvent_->GetKeyItem(repeatKeyCode_);
136             if (!pressedKeyItem) {
137                 return;
138             }
139             keyEvent_->SetKeyCode(repeatKeyCode_);
140             keyEvent_->SetAction(KeyEvent::KEY_ACTION_DOWN);
141             keyEvent_->SetKeyAction(KeyEvent::KEY_ACTION_DOWN);
142             int32_t delayTime = GetDelayTime();
143             AddHandleTimer(delayTime);
144             if (!JudgeLimitPrint(keyEvent_)) {
145                 MMI_HILOGD("The end keyboard autorepeat, keyCode:%{private}d", keyEvent_->GetKeyCode());
146             } else {
147                 MMI_HILOGD("The end keyboard autorepeat, keyCode:%d", keyEvent_->GetKeyCode());
148             }
149         }
150     }
151 }
152 
AddHandleTimer(int32_t timeout)153 void KeyAutoRepeat::AddHandleTimer(int32_t timeout)
154 {
155     CALL_DEBUG_ENTER;
156     timerId_ = TimerMgr->AddTimer(timeout, 1, [this]() {
157 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
158         auto inputEventNormalizeHandler = InputHandler->GetEventNormalizeHandler();
159         CHKPV(inputEventNormalizeHandler);
160         LogTracer lt(this->keyEvent_->GetId(), this->keyEvent_->GetEventType(), this->keyEvent_->GetKeyAction());
161         inputEventNormalizeHandler->HandleKeyEvent(this->keyEvent_);
162         this->keyEvent_->UpdateId();
163 #endif // OHOS_BUILD_ENABLE_KEYBOARD
164         int32_t triggertime = KeyRepeat->GetIntervalTime(keyEvent_->GetDeviceId());
165         this->AddHandleTimer(triggertime);
166     });
167 }
168 
GetTomlFilePath(const std::string & fileName) const169 std::string KeyAutoRepeat::GetTomlFilePath(const std::string &fileName) const
170 {
171     return "/vendor/etc/keymap/" + fileName + ".TOML";
172 }
173 
GetIntervalTime(int32_t deviceId)174 int32_t KeyAutoRepeat::GetIntervalTime(int32_t deviceId)
175 {
176     int32_t triggertime = DEFAULT_KEY_REPEAT_RATE;
177     GetKeyboardRepeatRate(triggertime);
178     return triggertime;
179 }
180 
GetDelayTime()181 int32_t KeyAutoRepeat::GetDelayTime()
182 {
183     int32_t delaytime = DEFAULT_KEY_REPEAT_DELAY;
184     GetKeyboardRepeatDelay(delaytime);
185     return delaytime;
186 }
187 
GetKeyboardRepeatTime(int32_t deviceId,bool isDelay)188 int32_t KeyAutoRepeat::GetKeyboardRepeatTime(int32_t deviceId, bool isDelay)
189 {
190     CALL_DEBUG_ENTER;
191     auto iter = deviceConfig_.find(deviceId);
192     int32_t repeatTime = isDelay ? DEFAULT_KEY_REPEAT_DELAY : DEFAULT_KEY_REPEAT_RATE;
193     if (iter != deviceConfig_.end()) {
194         repeatTime = isDelay ? iter->second.delayTime : iter->second.intervalTime;
195     }
196     return repeatTime;
197 }
198 
GetAutoSwitch(int32_t deviceId)199 DeviceConfig KeyAutoRepeat::GetAutoSwitch(int32_t deviceId)
200 {
201     auto iter = deviceConfig_.find(deviceId);
202     if (iter == deviceConfig_.end()) {
203         return {};
204     }
205     MMI_HILOGD("Open autorepeat:%{public}d", iter->second.autoSwitch);
206     return iter->second;
207 }
208 
RemoveDeviceConfig(struct libinput_device * device)209 void KeyAutoRepeat::RemoveDeviceConfig(struct libinput_device *device)
210 {
211     CALL_DEBUG_ENTER;
212     CHKPV(device);
213     int32_t deviceId = INPUT_DEV_MGR->FindInputDeviceId(device);
214     auto iter = deviceConfig_.find(deviceId);
215     if (iter == deviceConfig_.end()) {
216         MMI_HILOGI("Can not remove device config file");
217         return;
218     }
219     deviceConfig_.erase(iter);
220 }
221 
RemoveTimer()222 void KeyAutoRepeat::RemoveTimer()
223 {
224     CALL_DEBUG_ENTER;
225     TimerMgr->RemoveTimer(timerId_);
226 }
227 
SetKeyboardRepeatDelay(int32_t delay)228 int32_t KeyAutoRepeat::SetKeyboardRepeatDelay(int32_t delay)
229 {
230     CALL_DEBUG_ENTER;
231     int32_t repeatDelayTime = delay;
232     if (delay < MIN_KEY_REPEAT_DELAY) {
233         repeatDelayTime = MIN_KEY_REPEAT_DELAY;
234     }
235     if (delay > MAX_KEY_REPEAT_DELAY) {
236         repeatDelayTime = MAX_KEY_REPEAT_DELAY;
237     }
238     std::string name = "keyboardRepeatDelay";
239     if (PutConfigDataToDatabase(name, repeatDelayTime) != RET_OK) {
240         MMI_HILOGE("Failed to set keyboard repeat delay");
241         return RET_ERR;
242     }
243     MMI_HILOGD("Set keyboard repeat delay:%{public}d", repeatDelayTime);
244     return RET_OK;
245 }
246 
SetKeyboardRepeatRate(int32_t rate)247 int32_t KeyAutoRepeat::SetKeyboardRepeatRate(int32_t rate)
248 {
249     CALL_DEBUG_ENTER;
250     int32_t repeatRateTime = rate;
251     if (rate < MIN_KEY_REPEAT_RATE) {
252         repeatRateTime = MIN_KEY_REPEAT_RATE;
253     }
254     if (rate > MAX_KEY_REPEAT_RATE) {
255         repeatRateTime = MAX_KEY_REPEAT_RATE;
256     }
257     std::string name = "keyboardRepeatRate";
258     if (PutConfigDataToDatabase(name, repeatRateTime) != RET_OK) {
259         MMI_HILOGE("Failed to set keyboard repeat rate");
260         return RET_ERR;
261     }
262     MMI_HILOGD("Successfully set keyboard repeat for rate:%{public}d", repeatRateTime);
263     return RET_OK;
264 }
265 
GetKeyboardRepeatDelay(int32_t & delay)266 int32_t KeyAutoRepeat::GetKeyboardRepeatDelay(int32_t &delay)
267 {
268     CALL_DEBUG_ENTER;
269     std::string name = "keyboardRepeatDelay";
270     if (GetConfigDataFromDatabase(name, delay) != RET_OK) {
271         MMI_HILOGE("Failed to get keyboard repeat delay");
272         return RET_ERR;
273     }
274     if (delay == 0) {
275         delay = DEFAULT_KEY_REPEAT_DELAY;
276         if (keyEvent_ != nullptr) {
277             delay = GetKeyboardRepeatTime(keyEvent_->GetDeviceId(), true);
278         }
279     }
280     MMI_HILOGD("Get keyboard repeat delay:%{public}d", delay);
281     return RET_OK;
282 }
283 
GetKeyboardRepeatRate(int32_t & rate)284 int32_t KeyAutoRepeat::GetKeyboardRepeatRate(int32_t &rate)
285 {
286     CALL_DEBUG_ENTER;
287     std::string name = "keyboardRepeatRate";
288     if (GetConfigDataFromDatabase(name, rate) != RET_OK) {
289         MMI_HILOGE("Failed to get keyboard repeat rate");
290         return RET_ERR;
291     }
292     if (rate == 0) {
293         rate = DEFAULT_KEY_REPEAT_RATE;
294         if (keyEvent_ != nullptr) {
295             rate = GetKeyboardRepeatTime(keyEvent_->GetDeviceId(), false);
296         }
297     }
298     MMI_HILOGD("Get keyboard repeat rate:%{public}d", rate);
299     return RET_OK;
300 }
301 
PutConfigDataToDatabase(std::string & key,int32_t value)302 int32_t KeyAutoRepeat::PutConfigDataToDatabase(std::string &key, int32_t value)
303 {
304     return PREFERENCES_MGR->SetIntValue(key, KEYBOARD_FILE_NAME, value);
305 }
306 
GetConfigDataFromDatabase(std::string & key,int32_t & value)307 int32_t KeyAutoRepeat::GetConfigDataFromDatabase(std::string &key, int32_t &value)
308 {
309     value = PREFERENCES_MGR->GetIntValue(key, value);
310     return RET_OK;
311 }
312 } // namespace MMI
313 } // namespace OHOS