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