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