• 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 "device_config_file_parser.h"
17 
18 #include <fstream>
19 #include <regex>
20 
21 #include "error_multimodal.h"
22 #include "input_device.h"
23 #include "libinput.h"
24 #include "util.h"
25 
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "DeviceConfigManagement" };
30 constexpr int32_t COMMENT_SUBSCRIPT = 0;
31 } // namespace
32 
33 enum evdev_device_udev_tags {
34     EVDEV_UDEV_TAG_INPUT = 1 << 0,
35     EVDEV_UDEV_TAG_KEYBOARD = 1 << 1,
36     EVDEV_UDEV_TAG_MOUSE = 1 << 2,
37     EVDEV_UDEV_TAG_TOUCHPAD = 1 << 3,
38     EVDEV_UDEV_TAG_TOUCHSCREEN = 1 << 4,
39     EVDEV_UDEV_TAG_TABLET = 1 << 5,
40     EVDEV_UDEV_TAG_JOYSTICK = 1 << 6,
41     EVDEV_UDEV_TAG_ACCELEROMETER = 1 << 7,
42     EVDEV_UDEV_TAG_TABLET_PAD = 1 << 8,
43     EVDEV_UDEV_TAG_POINTINGSTICK = 1 << 9,
44     EVDEV_UDEV_TAG_TRACKBALL = 1 << 10,
45     EVDEV_UDEV_TAG_SWITCH = 1 << 11,
46 };
47 
CombDeviceFileName(struct libinput_device * device) const48 std::string DeviceConfigManagement::CombDeviceFileName(struct libinput_device *device) const
49 {
50     CALL_DEBUG_ENTER;
51     CHKPS(device);
52     uint32_t vendor = libinput_device_get_id_vendor(device);
53     uint32_t product = libinput_device_get_id_product(device);
54     uint32_t version = libinput_device_get_id_version(device);
55     const char *name = libinput_device_get_name(device);
56     CHKPS(name);
57     std::string fileName =
58         std::to_string(vendor) + "_" + std::to_string(product) + "_" + std::to_string(version) + "_" + name;
59     RemoveSpace(fileName);
60     return fileName;
61 }
62 
ConfigItemName2Id(const std::string & name) const63 ConfigFileItem DeviceConfigManagement::ConfigItemName2Id(const std::string &name) const
64 {
65     static const std::map<const std::string, ConfigFileItem> configList = {
66         { "speed", ConfigFileItem::POINTER_SPEED },
67     };
68 
69     auto iter = configList.find(name);
70     if (iter == configList.end()) {
71         MMI_HILOGE("Device name failed");
72         return ConfigFileItem::INVALID;
73     }
74     return configList.at(name);
75 }
76 
ReadConfigFile(const std::string & filePath) const77 std::map<ConfigFileItem, int32_t> DeviceConfigManagement::ReadConfigFile(const std::string &filePath) const
78 {
79     std::map<ConfigFileItem, int32_t> configList;
80     std::ifstream cfgFile(filePath);
81     if (!cfgFile.is_open()) {
82         MMI_HILOGE("Failed to open config file");
83         return configList;
84     }
85     std::string tmp;
86 
87     while (std::getline(cfgFile, tmp)) {
88         RemoveSpace(tmp);
89         size_t pos = tmp.find('#');
90         if (pos != tmp.npos && pos != COMMENT_SUBSCRIPT) {
91             continue;
92         }
93         if (tmp.empty() || tmp.front() == '#') {
94             continue;
95         }
96         pos = tmp.find('=');
97         if (pos == (tmp.size() - 1) || pos == tmp.npos) {
98             continue;
99         }
100         std::string key = tmp.substr(0, pos);
101 
102         std::smatch match;
103         bool isNumber = std::regex_search(tmp, match, std::regex("\\d+"));
104         if (!isNumber) {
105             continue;
106         }
107         configList[ConfigItemName2Id(key)] = std::stoi(match[0]);
108     }
109     cfgFile.close();
110     return configList;
111 }
112 
GetVendorConfig(struct libinput_device * device) const113 VendorConfig DeviceConfigManagement::GetVendorConfig(struct libinput_device *device) const
114 {
115     CALL_DEBUG_ENTER;
116     CHKPO(device);
117     std::string filePath = "/vendor/etc/pointer/" + CombDeviceFileName(device) + ".TOML";
118     VendorConfig vendorConfigTmp = {};
119     auto path = FileVerification(filePath, "TOML");
120     if (path.empty()) {
121         MMI_HILOGE("File validation failed");
122         return vendorConfigTmp;
123     }
124     auto configList = ReadConfigFile(path);
125     if (configList.empty()) {
126         MMI_HILOGE("configList is empty");
127         return vendorConfigTmp;
128     }
129     if (configList.find(ConfigFileItem::POINTER_SPEED) == configList.end()) {
130         MMI_HILOGE("configList not find POINTER_SPEED");
131         return vendorConfigTmp;
132     }
133     vendorConfigTmp.pointerSpeed = configList[ConfigFileItem::POINTER_SPEED];
134     return vendorConfigTmp;
135 }
136 } // namespace MMI
137 } // namespace OHOS