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