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