1 /*
2 * Copyright (c) 2023 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 "window_scene_config.h"
17
18 #include "config_policy_utils.h"
19 #include "window_helper.h"
20 #include "window_manager_hilog.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowSceneConfig"};
26 }
27
28 WindowSceneConfig::ConfigItem WindowSceneConfig::config_;
29 const std::map<std::string, WindowSceneConfig::ValueType> WindowSceneConfig::configItemTypeMap_ = {
30 { "maxAppWindowNumber", WindowSceneConfig::ValueType::INTS },
31 { "modeChangeHotZones", WindowSceneConfig::ValueType::INTS },
32 { "duration", WindowSceneConfig::ValueType::INTS },
33 { "durationIn", WindowSceneConfig::ValueType::INTS },
34 { "durationOut", WindowSceneConfig::ValueType::INTS },
35 { "defaultWindowMode", WindowSceneConfig::ValueType::INTS },
36 { "dragFrameGravity", WindowSceneConfig::ValueType::INTS },
37 { "floatingBottomPosY", WindowSceneConfig::ValueType::INTS },
38 { "defaultFloatingWindow", WindowSceneConfig::ValueType::INTS },
39 { "maxMainFloatingWindowNumber", WindowSceneConfig::ValueType::INTS },
40 { "maxFloatingWindowSize", WindowSceneConfig::ValueType::INTS },
41 { "defaultMaximizeMode", WindowSceneConfig::ValueType::INTS },
42 { "miniWidth", WindowSceneConfig::ValueType::INTS },
43 { "miniHeight", WindowSceneConfig::ValueType::INTS },
44 { "mainWindowSizeLimits", WindowSceneConfig::ValueType::MAP },
45 { "subWindowSizeLimits", WindowSceneConfig::ValueType::MAP },
46 { "windowAnimation", WindowSceneConfig::ValueType::MAP },
47 { "keyboardAnimation", WindowSceneConfig::ValueType::MAP },
48 { "timing", WindowSceneConfig::ValueType::MAP },
49 { "windowEffect", WindowSceneConfig::ValueType::MAP },
50 { "appWindows", WindowSceneConfig::ValueType::MAP },
51 { "cornerRadius", WindowSceneConfig::ValueType::MAP },
52 { "shadow", WindowSceneConfig::ValueType::MAP },
53 { "focused", WindowSceneConfig::ValueType::MAP },
54 { "unfocused", WindowSceneConfig::ValueType::MAP },
55 { "decor", WindowSceneConfig::ValueType::MAP },
56 { "startWindowTransitionAnimation", WindowSceneConfig::ValueType::MAP },
57 { "curve", WindowSceneConfig::ValueType::POSITIVE_FLOATS },
58 { "splitRatios", WindowSceneConfig::ValueType::POSITIVE_FLOATS },
59 { "exitSplitRatios", WindowSceneConfig::ValueType::POSITIVE_FLOATS },
60 { "scale", WindowSceneConfig::ValueType::POSITIVE_FLOATS },
61 { "opacity", WindowSceneConfig::ValueType::POSITIVE_FLOATS },
62 { "opacityStart", WindowSceneConfig::ValueType::POSITIVE_FLOATS },
63 { "opacityEnd", WindowSceneConfig::ValueType::POSITIVE_FLOATS },
64 { "elevation", WindowSceneConfig::ValueType::POSITIVE_FLOATS },
65 { "alpha", WindowSceneConfig::ValueType::POSITIVE_FLOATS },
66 { "rotation", WindowSceneConfig::ValueType::FLOATS },
67 { "translate", WindowSceneConfig::ValueType::FLOATS },
68 { "offsetX", WindowSceneConfig::ValueType::FLOATS },
69 { "offsetY", WindowSceneConfig::ValueType::FLOATS },
70 { "radius", WindowSceneConfig::ValueType::FLOATS },
71 { "fullScreen", WindowSceneConfig::ValueType::STRING },
72 { "split", WindowSceneConfig::ValueType::STRING },
73 { "float", WindowSceneConfig::ValueType::STRING },
74 { "color", WindowSceneConfig::ValueType::STRING },
75 { "supportedMode", WindowSceneConfig::ValueType::STRINGS },
76 { "minimizeByOther", WindowSceneConfig::ValueType::UNDIFINED },
77 { "stretchable", WindowSceneConfig::ValueType::UNDIFINED },
78 { "remoteAnimation", WindowSceneConfig::ValueType::UNDIFINED },
79 { "configMainFloatingWindowAbove", WindowSceneConfig::ValueType::UNDIFINED },
80 };
81
SplitNodeContent(const xmlNodePtr & node,const std::string & pattern)82 std::vector<std::string> WindowSceneConfig::SplitNodeContent(const xmlNodePtr& node, const std::string& pattern)
83 {
84 xmlChar* content = xmlNodeGetContent(node);
85 if (content == nullptr) {
86 WLOGFE("read xml node error: nodeName:(%{public}s)", node->name);
87 return std::vector<std::string>();
88 }
89
90 std::string contentStr = reinterpret_cast<const char*>(content);
91 xmlFree(content);
92 if (contentStr.size() == 0) {
93 return std::vector<std::string>();
94 }
95 return WindowHelper::Split(contentStr, pattern);
96 }
97
GetConfigPath(const std::string & configFileName)98 std::string WindowSceneConfig::GetConfigPath(const std::string& configFileName)
99 {
100 char buf[PATH_MAX + 1];
101 char* configPath = GetOneCfgFile(configFileName.c_str(), buf, PATH_MAX + 1);
102 char tmpPath[PATH_MAX + 1] = { 0 };
103 if (!configPath || strlen(configPath) == 0 || strlen(configPath) > PATH_MAX || !realpath(configPath, tmpPath)) {
104 WLOGI("can not get customization config file");
105 return "/system/" + configFileName;
106 }
107 return std::string(tmpPath);
108 }
109
ReadConfig(const xmlNodePtr & rootPtr,std::map<std::string,ConfigItem> & mapValue)110 void WindowSceneConfig::ReadConfig(const xmlNodePtr& rootPtr, std::map<std::string, ConfigItem>& mapValue)
111 {
112 for (xmlNodePtr curNodePtr = rootPtr->xmlChildrenNode; curNodePtr != nullptr; curNodePtr = curNodePtr->next) {
113 if (!IsValidNode(*curNodePtr)) {
114 WLOGFE("[WmConfig]: invalid node!");
115 continue;
116 }
117 std::string nodeName = reinterpret_cast<const char*>(curNodePtr->name);
118 if (configItemTypeMap_.count(nodeName)) {
119 std::map<std::string, ConfigItem> p = ReadProperty(curNodePtr);
120 if (p.size() > 0) {
121 mapValue[reinterpret_cast<const char*>(curNodePtr->name)].SetProperty(p);
122 }
123 switch (configItemTypeMap_.at(nodeName)) {
124 case ValueType::INTS: {
125 std::vector<int> v = ReadIntNumbersConfigInfo(curNodePtr);
126 mapValue[reinterpret_cast<const char*>(curNodePtr->name)].SetValue(v);
127 break;
128 }
129 case ValueType::POSITIVE_FLOATS: {
130 std::vector<float> v = ReadFloatNumbersConfigInfo(curNodePtr, false);
131 mapValue[reinterpret_cast<const char*>(curNodePtr->name)].SetValue(v);
132 break;
133 }
134 case ValueType::FLOATS: {
135 std::vector<float> v = ReadFloatNumbersConfigInfo(curNodePtr, true);
136 mapValue[reinterpret_cast<const char*>(curNodePtr->name)].SetValue(v);
137 break;
138 }
139 case ValueType::MAP: {
140 std::map<std::string, ConfigItem> v;
141 ReadConfig(curNodePtr, v);
142 mapValue[reinterpret_cast<const char*>(curNodePtr->name)].SetValue(v);
143 break;
144 }
145 case ValueType::STRING: {
146 std::string v = ReadStringConfigInfo(curNodePtr);
147 mapValue[reinterpret_cast<const char*>(curNodePtr->name)].SetValue(v);
148 break;
149 }
150 case ValueType::STRINGS: {
151 std::vector<std::string> v = ReadStringsConfigInfo(curNodePtr);
152 mapValue[reinterpret_cast<const char*>(curNodePtr->name)].SetValue(v);
153 break;
154 }
155 default:
156 break;
157 }
158 }
159 }
160 }
161
LoadConfigXml()162 bool WindowSceneConfig::LoadConfigXml()
163 {
164 auto configFilePath = GetConfigPath("etc/window/resources/window_manager_config.xml");
165 xmlDocPtr docPtr = nullptr;
166 {
167 std::lock_guard<std::recursive_mutex> lock(mutex_);
168 docPtr = xmlReadFile(configFilePath.c_str(), nullptr, XML_PARSE_NOBLANKS);
169 }
170 WLOGI("filePath: %{public}s", configFilePath.c_str());
171 if (docPtr == nullptr) {
172 WLOGFE("load xml error!");
173 return false;
174 }
175
176 xmlNodePtr rootPtr = xmlDocGetRootElement(docPtr);
177 if (rootPtr == nullptr || rootPtr->name == nullptr ||
178 xmlStrcmp(rootPtr->name, reinterpret_cast<const xmlChar*>("Configs"))) {
179 WLOGFE("get root element failed!");
180 xmlFreeDoc(docPtr);
181 return false;
182 }
183
184 std::map<std::string, ConfigItem> configMap;
185 config_.SetValue(configMap);
186 ReadConfig(rootPtr, *config_.mapValue_);
187
188 xmlFreeDoc(docPtr);
189 return true;
190 }
191
IsValidNode(const xmlNode & currNode)192 bool WindowSceneConfig::IsValidNode(const xmlNode& currNode)
193 {
194 if (currNode.name == nullptr || currNode.type == XML_COMMENT_NODE) {
195 return false;
196 }
197 return true;
198 }
199
ReadProperty(const xmlNodePtr & currNode)200 std::map<std::string, XmlConfigBase::ConfigItem> WindowSceneConfig::ReadProperty(const xmlNodePtr& currNode)
201 {
202 std::map<std::string, ConfigItem> property;
203 xmlChar* prop = xmlGetProp(currNode, reinterpret_cast<const xmlChar*>("enable"));
204 if (prop != nullptr) {
205 if (!xmlStrcmp(prop, reinterpret_cast<const xmlChar*>("true"))) {
206 property["enable"].SetValue(true);
207 } else if (!xmlStrcmp(prop, reinterpret_cast<const xmlChar*>("false"))) {
208 property["enable"].SetValue(false);
209 }
210 xmlFree(prop);
211 }
212
213 prop = xmlGetProp(currNode, reinterpret_cast<const xmlChar*>("name"));
214 if (prop != nullptr) {
215 property["name"].SetValue(std::string(reinterpret_cast<const char*>(prop)));
216 xmlFree(prop);
217 }
218
219 return property;
220 }
221
ReadIntNumbersConfigInfo(const xmlNodePtr & currNode)222 std::vector<int> WindowSceneConfig::ReadIntNumbersConfigInfo(const xmlNodePtr& currNode)
223 {
224 std::vector<int> intsValue;
225 auto numbers = SplitNodeContent(currNode);
226 for (auto& num : numbers) {
227 if (!WindowHelper::IsNumber(num)) {
228 WLOGFE("read int number error: nodeName:(%{public}s)", currNode->name);
229 return {};
230 }
231 intsValue.push_back(std::stoi(num));
232 }
233 return intsValue;
234 }
235
ReadStringsConfigInfo(const xmlNodePtr & currNode)236 std::vector<std::string> WindowSceneConfig::ReadStringsConfigInfo(const xmlNodePtr& currNode)
237 {
238 return SplitNodeContent(currNode);
239 }
240
ReadFloatNumbersConfigInfo(const xmlNodePtr & currNode,bool allowNeg)241 std::vector<float> WindowSceneConfig::ReadFloatNumbersConfigInfo(const xmlNodePtr& currNode, bool allowNeg)
242 {
243 std::vector<float> floatsValue;
244 auto numbers = SplitNodeContent(currNode);
245 for (auto& num : numbers) {
246 if (!WindowHelper::IsFloatingNumber(num, allowNeg)) {
247 WLOGFE("read float number error: nodeName:(%{public}s)", currNode->name);
248 return {};
249 }
250 floatsValue.push_back(std::stof(num));
251 }
252 return floatsValue;
253 }
254
ReadStringConfigInfo(const xmlNodePtr & currNode)255 std::string WindowSceneConfig::ReadStringConfigInfo(const xmlNodePtr& currNode)
256 {
257 std::string stringValue;
258 xmlChar* context = xmlNodeGetContent(currNode);
259 if (context == nullptr) {
260 WLOGFE("read xml node error: nodeName:(%{public}s)", currNode->name);
261 return {};
262 }
263
264 stringValue = std::string(reinterpret_cast<const char*>(context));
265 xmlFree(context);
266 return stringValue;
267 }
268
DumpConfig(const std::map<std::string,ConfigItem> & config)269 void WindowSceneConfig::DumpConfig(const std::map<std::string, ConfigItem>& config)
270 {
271 for (auto& conf : config) {
272 WLOGI("%{public}s", conf.first.c_str());
273 std::map<std::string, ConfigItem> propMap;
274 if (conf.second.property_) {
275 propMap = *conf.second.property_;
276 }
277 for (auto prop : propMap) {
278 switch (prop.second.type_) {
279 case ValueType::BOOL:
280 WLOGI("Prop: %{public}s %{public}u", prop.first.c_str(), prop.second.boolValue_);
281 break;
282 case ValueType::STRING:
283 WLOGI("Prop: %{public}s %{public}s", prop.first.c_str(),
284 prop.second.stringValue_.c_str());
285 break;
286 default:
287 break;
288 }
289 }
290 switch (conf.second.type_) {
291 case ValueType::MAP:
292 if (conf.second.mapValue_) {
293 DumpConfig(*conf.second.mapValue_);
294 }
295 break;
296 case ValueType::BOOL:
297 WLOGI("%{public}u", conf.second.boolValue_);
298 break;
299 case ValueType::STRING:
300 WLOGI("%{public}s", conf.second.stringValue_.c_str());
301 break;
302 case ValueType::INTS:
303 for (auto& num : *conf.second.intsValue_) {
304 WLOGI("Num: %{public}d", num);
305 }
306 break;
307 case ValueType::FLOATS:
308 for (auto& num : *conf.second.floatsValue_) {
309 WLOGI("Num: %{public}f", num);
310 }
311 break;
312 default:
313 break;
314 }
315 }
316 }
317
318 } // namespace Rosen
319 } // namespace OHOS
320