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_map_manager.h"
17 
18 #include <array>
19 
20 #include "define_multimodal.h"
21 #include "input_device_manager.h"
22 #include "mmi_log.h"
23 #include "util.h"
24 
25 #undef MMI_LOG_DOMAIN
26 #define MMI_LOG_DOMAIN MMI_LOG_DISPATCH
27 #undef MMI_LOG_TAG
28 #define MMI_LOG_TAG "KeyMapManager"
29 
30 namespace OHOS {
31 namespace MMI {
KeyMapManager()32 KeyMapManager::KeyMapManager() {}
~KeyMapManager()33 KeyMapManager::~KeyMapManager() {}
34 
GetConfigKeyValue(const std::string & fileName,int32_t deviceId)35 void KeyMapManager::GetConfigKeyValue(const std::string &fileName, int32_t deviceId)
36 {
37     CALL_DEBUG_ENTER;
38     if (fileName.empty()) {
39         MMI_HILOGE("fileName is empty");
40         return;
41     }
42     std::string filePath = GetProFilePath(fileName);
43     ReadProFile(filePath, deviceId, configKeyValue_);
44     MMI_HILOGD("Number of loaded config files:%{public}zu", configKeyValue_.size());
45 }
46 
ParseDeviceConfigFile(struct libinput_device * device)47 void KeyMapManager::ParseDeviceConfigFile(struct libinput_device *device)
48 {
49     CHKPV(device);
50     std::string fileName = GetKeyEventFileName(device);
51     if (fileName.empty()) {
52         MMI_HILOGE("Get fileName is empty");
53         return;
54     }
55     int32_t deviceId = INPUT_DEV_MGR->FindInputDeviceId(device);
56     GetConfigKeyValue(fileName, deviceId);
57 }
58 
RemoveKeyValue(struct libinput_device * device)59 void KeyMapManager::RemoveKeyValue(struct libinput_device *device)
60 {
61     CHKPV(device);
62     int32_t deviceId = INPUT_DEV_MGR->FindInputDeviceId(device);
63     auto iter = configKeyValue_.find(deviceId);
64     if (iter == configKeyValue_.end()) {
65         MMI_HILOGD("Device config file does not exist");
66         return;
67     }
68     configKeyValue_.erase(iter);
69     MMI_HILOGD("Number of files that remain after deletion:%{public}zu", configKeyValue_.size());
70 }
71 
GetDefaultKeyId()72 int32_t KeyMapManager::GetDefaultKeyId()
73 {
74     return defaultKeyId_;
75 }
76 
GetProFilePath(const std::string & fileName) const77 std::string KeyMapManager::GetProFilePath(const std::string &fileName) const
78 {
79     return "/vendor/etc/keymap/" + fileName + ".pro";
80 }
81 
GetKeyEventFileName(struct libinput_device * device)82 std::string KeyMapManager::GetKeyEventFileName(struct libinput_device *device)
83 {
84     CHKPS(device);
85     uint32_t vendor = libinput_device_get_id_vendor(device);
86     uint32_t product = libinput_device_get_id_product(device);
87     uint32_t version = libinput_device_get_id_version(device);
88     const char *name = libinput_device_get_name(device);
89     CHKPS(name);
90     std::string fileName = std::to_string(vendor) + "_" + std::to_string(product) + "_" +
91         std::to_string(version) + "_" + name;
92     RemoveSpace(fileName);
93     return fileName;
94 }
95 
TransferDefaultKeyValue(int32_t inputKey)96 int32_t KeyMapManager::TransferDefaultKeyValue(int32_t inputKey)
97 {
98     CALL_DEBUG_ENTER;
99     if (auto itr = configKeyValue_.find(defaultKeyId_); itr != configKeyValue_.end()) {
100         if (auto defaultKey = itr->second.find(inputKey); defaultKey != itr->second.end()) {
101             return defaultKey->second;
102         }
103     }
104     MMI_HILOGD("Return key values in the TransferKeyValue");
105     return TransferKeyValue(inputKey).sysKeyValue;
106 }
107 
TransferDeviceKeyValue(struct libinput_device * device,int32_t inputKey)108 int32_t KeyMapManager::TransferDeviceKeyValue(struct libinput_device *device,
109     int32_t inputKey)
110 {
111     CALL_DEBUG_ENTER;
112     if (device == nullptr) {
113         return TransferDefaultKeyValue(inputKey);
114     }
115     int32_t deviceId = INPUT_DEV_MGR->FindInputDeviceId(device);
116     if (auto itr = configKeyValue_.find(deviceId); itr != configKeyValue_.end()) {
117         if (auto devKey = itr->second.find(inputKey); devKey != itr->second.end()) {
118             return devKey->second;
119         }
120     }
121     return TransferDefaultKeyValue(inputKey);
122 }
123 
InputTransferKeyValue(int32_t deviceId,int32_t keyCode)124 std::vector<int32_t> KeyMapManager::InputTransferKeyValue(int32_t deviceId, int32_t keyCode)
125 {
126     std::vector<int32_t> sysKey;
127     if (auto iter = configKeyValue_.find(deviceId); iter != configKeyValue_.end()) {
128         for (const auto &it : iter->second) {
129             if (it.second == keyCode) {
130                 sysKey.push_back(it.first);
131             }
132         }
133         return sysKey;
134     } else if (auto itr = configKeyValue_.find(defaultKeyId_); itr != configKeyValue_.end()) {
135         for (const auto &it : itr->second) {
136             if (it.second == keyCode) {
137                 sysKey.push_back(it.first);
138             }
139         }
140         return sysKey;
141     } else {
142         sysKey.push_back(InputTransformationKeyValue(keyCode));
143         return sysKey;
144     }
145     return sysKey;
146 }
147 } // namespace MMI
148 } // namespace OHOS
149