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 "display_manager_config.h"
17
18 #include <climits>
19 #include <cstdint>
20 #include <cstdlib>
21 #include <libxml/globals.h>
22 #include <libxml/xmlstring.h>
23 #include <map>
24 #include <string>
25 #include <utility>
26 #include <vector>
27
28 #include "config_policy_utils.h"
29 #include "window_manager_hilog.h"
30
31
32 namespace OHOS::Rosen {
33 namespace {
34 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManagerConfig"};
35 }
36
37 std::map<std::string, bool> DisplayManagerConfig::enableConfig_;
38 std::map<std::string, std::vector<int>> DisplayManagerConfig::intNumbersConfig_;
39 std::map<std::string, std::string> DisplayManagerConfig::stringConfig_;
40
Split(std::string str,std::string pattern)41 std::vector<std::string> DisplayManagerConfig::Split(std::string str, std::string pattern)
42 {
43 std::vector<std::string> result;
44 str += pattern;
45 size_t length = str.size();
46 for (size_t i = 0; i < length; i++) {
47 size_t position = str.find(pattern, i);
48 if (position < length) {
49 std::string tmp = str.substr(i, position - i);
50 result.push_back(tmp);
51 i = position + pattern.size() - 1;
52 }
53 }
54 return result;
55 }
56
IsNumber(std::string str)57 bool inline DisplayManagerConfig::IsNumber(std::string str)
58 {
59 for (int32_t i = 0; i < static_cast<int32_t>(str.size()); i++) {
60 if (str.at(i) < '0' || str.at(i) > '9') {
61 return false;
62 }
63 }
64 return true;
65 }
66
GetConfigPath(const std::string & configFileName)67 std::string DisplayManagerConfig::GetConfigPath(const std::string& configFileName)
68 {
69 char buf[PATH_MAX + 1];
70 char* configPath = GetOneCfgFile(configFileName.c_str(), buf, PATH_MAX + 1);
71 char tmpPath[PATH_MAX + 1] = { 0 };
72 if (!configPath || strlen(configPath) == 0 || strlen(configPath) > PATH_MAX || !realpath(configPath, tmpPath)) {
73 WLOGFI("[DmConfig] can not get customization config file");
74 return "/system/" + configFileName;
75 }
76 return std::string(tmpPath);
77 }
78
LoadConfigXml()79 bool DisplayManagerConfig::LoadConfigXml()
80 {
81 auto configFilePath = GetConfigPath("etc/window/resources/display_manager_config.xml");
82 xmlDocPtr docPtr = nullptr;
83 {
84 std::lock_guard<std::recursive_mutex> lock(mutex_);
85 docPtr = xmlReadFile(configFilePath.c_str(), nullptr, XML_PARSE_NOBLANKS);
86 }
87 WLOGFI("[DmConfig] filePath: %{public}s", configFilePath.c_str());
88 if (docPtr == nullptr) {
89 WLOGFE("[DmConfig] load xml error!");
90 return false;
91 }
92
93 xmlNodePtr rootPtr = xmlDocGetRootElement(docPtr);
94 if (rootPtr == nullptr || rootPtr->name == nullptr ||
95 xmlStrcmp(rootPtr->name, reinterpret_cast<const xmlChar*>("Configs"))) {
96 WLOGFE("[DmConfig] get root element failed!");
97 xmlFreeDoc(docPtr);
98 return false;
99 }
100
101 for (xmlNodePtr curNodePtr = rootPtr->xmlChildrenNode; curNodePtr != nullptr; curNodePtr = curNodePtr->next) {
102 if (!IsValidNode(*curNodePtr)) {
103 WLOGFE("DmConfig]: invalid node!");
104 continue;
105 }
106
107 auto nodeName = curNodePtr->name;
108 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("isWaterfallDisplay")) ||
109 !xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("isWaterfallAreaCompressionEnableWhenHorizontal"))) {
110 ReadEnableConfigInfo(curNodePtr);
111 continue;
112 }
113 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("dpi")) ||
114 !xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("defaultDeviceRotationOffset")) ||
115 !xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("cutoutArea")) ||
116 !xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("curvedScreenBoundary")) ||
117 !xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("waterfallAreaCompressionSizeWhenHorzontal")) ||
118 !xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("buildInDefaultOrientation"))) {
119 ReadIntNumbersConfigInfo(curNodePtr);
120 continue;
121 }
122 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("defaultDisplayCutoutPath"))) {
123 ReadStringConfigInfo(curNodePtr);
124 continue;
125 }
126 }
127 xmlFreeDoc(docPtr);
128 return true;
129 }
130
IsValidNode(const xmlNode & currNode)131 bool DisplayManagerConfig::IsValidNode(const xmlNode& currNode)
132 {
133 if (currNode.name == nullptr || currNode.type == XML_COMMENT_NODE) {
134 return false;
135 }
136 return true;
137 }
138
ReadIntNumbersConfigInfo(const xmlNodePtr & currNode)139 void DisplayManagerConfig::ReadIntNumbersConfigInfo(const xmlNodePtr& currNode)
140 {
141 xmlChar* context = xmlNodeGetContent(currNode);
142 if (context == nullptr) {
143 WLOGFE("[DmConfig] read xml node error: nodeName:(%{public}s)", currNode->name);
144 return;
145 }
146
147 std::vector<int> numbersVec;
148 std::string numbersStr = reinterpret_cast<const char*>(context);
149 if (numbersStr.empty()) {
150 xmlFree(context);
151 return;
152 }
153 auto numbers = Split(numbersStr, " ");
154 for (auto& num : numbers) {
155 if (!IsNumber(num)) {
156 WLOGFE("[DmConfig] read number error: nodeName:(%{public}s)", currNode->name);
157 xmlFree(context);
158 return;
159 }
160 numbersVec.emplace_back(std::stoi(num));
161 }
162
163 std::string nodeName = reinterpret_cast<const char *>(currNode->name);
164 intNumbersConfig_[nodeName] = numbersVec;
165 xmlFree(context);
166 }
167
ReadEnableConfigInfo(const xmlNodePtr & currNode)168 void DisplayManagerConfig::ReadEnableConfigInfo(const xmlNodePtr& currNode)
169 {
170 xmlChar* enable = xmlGetProp(currNode, reinterpret_cast<const xmlChar*>("enable"));
171 if (enable == nullptr) {
172 WLOGFE("[DmConfig] read xml node error: nodeName:(%{public}s)", currNode->name);
173 return;
174 }
175
176 std::string nodeName = reinterpret_cast<const char *>(currNode->name);
177 if (!xmlStrcmp(enable, reinterpret_cast<const xmlChar*>("true"))) {
178 enableConfig_[nodeName] = true;
179 } else {
180 enableConfig_[nodeName] = false;
181 }
182 xmlFree(enable);
183 }
184
ReadStringConfigInfo(const xmlNodePtr & currNode)185 void DisplayManagerConfig::ReadStringConfigInfo(const xmlNodePtr& currNode)
186 {
187 xmlChar* context = xmlNodeGetContent(currNode);
188 if (context == nullptr) {
189 WLOGFE("[DmConfig] read xml node error: nodeName:(%{public}s)", currNode->name);
190 return;
191 }
192
193 std::string inputString = reinterpret_cast<const char*>(context);
194 std::string nodeName = reinterpret_cast<const char*>(currNode->name);
195 stringConfig_[nodeName] = inputString;
196 xmlFree(context);
197 }
198
GetEnableConfig()199 const std::map<std::string, bool>& DisplayManagerConfig::GetEnableConfig()
200 {
201 return enableConfig_;
202 }
203
GetIntNumbersConfig()204 const std::map<std::string, std::vector<int>>& DisplayManagerConfig::GetIntNumbersConfig()
205 {
206 return intNumbersConfig_;
207 }
208
GetStringConfig()209 const std::map<std::string, std::string>& DisplayManagerConfig::GetStringConfig()
210 {
211 return stringConfig_;
212 }
213
DumpConfig()214 void DisplayManagerConfig::DumpConfig()
215 {
216 for (auto& enable : enableConfig_) {
217 WLOGFI("[DmConfig] Enable: %{public}s %{public}u", enable.first.c_str(), enable.second);
218 }
219 for (auto& numbers : intNumbersConfig_) {
220 WLOGFI("[DmConfig] Numbers: %{public}s %{public}zu", numbers.first.c_str(), numbers.second.size());
221 for (auto& num : numbers.second) {
222 WLOGFI("[DmConfig] Num: %{public}d", num);
223 }
224 }
225 for (auto& string : stringConfig_) {
226 WLOGFI("[DmConfig] String: %{public}s", string.first.c_str());
227 }
228 }
229 } // namespace OHOS::Rosen
230