1e0dac50fSopenharmony_ci/*
2e0dac50fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3e0dac50fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0dac50fSopenharmony_ci * you may not use this file except in compliance with the License.
5e0dac50fSopenharmony_ci * You may obtain a copy of the License at
6e0dac50fSopenharmony_ci *
7e0dac50fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0dac50fSopenharmony_ci *
9e0dac50fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0dac50fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0dac50fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0dac50fSopenharmony_ci * See the License for the specific language governing permissions and
13e0dac50fSopenharmony_ci * limitations under the License.
14e0dac50fSopenharmony_ci */
15e0dac50fSopenharmony_ci
16e0dac50fSopenharmony_ci#include "display_dumper.h"
17e0dac50fSopenharmony_ci
18e0dac50fSopenharmony_ci#include <cinttypes>
19e0dac50fSopenharmony_ci#include <csignal>
20e0dac50fSopenharmony_ci#include <iomanip>
21e0dac50fSopenharmony_ci#include <map>
22e0dac50fSopenharmony_ci#include <sstream>
23e0dac50fSopenharmony_ci#include <string_ex.h>
24e0dac50fSopenharmony_ci#include <unique_fd.h>
25e0dac50fSopenharmony_ci
26e0dac50fSopenharmony_ci#include "window_manager_hilog.h"
27e0dac50fSopenharmony_ci
28e0dac50fSopenharmony_cinamespace OHOS {
29e0dac50fSopenharmony_cinamespace Rosen {
30e0dac50fSopenharmony_cinamespace {
31e0dac50fSopenharmony_ciconstexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayDumper"};
32e0dac50fSopenharmony_ci
33e0dac50fSopenharmony_ciconstexpr int SCREEN_NAME_MAX_LENGTH = 20;
34e0dac50fSopenharmony_ciconst std::string ARG_DUMP_HELP = "-h";
35e0dac50fSopenharmony_ciconst std::string ARG_DUMP_ALL = "-a";
36e0dac50fSopenharmony_ciconst std::string ARG_DUMP_SCREEN = "-s";
37e0dac50fSopenharmony_ciconst std::string ARG_DUMP_DISPLAY = "-d";
38e0dac50fSopenharmony_ci// set the output width of screen
39e0dac50fSopenharmony_ciconstexpr int W_SCREEN_NAME = 21;
40e0dac50fSopenharmony_ciconstexpr int W_SCREEN_TYPE = 9;
41e0dac50fSopenharmony_ciconstexpr int W_GROUP_TYPE = 8;
42e0dac50fSopenharmony_ciconstexpr int W_DMS_ID = 6;
43e0dac50fSopenharmony_ciconstexpr int W_RS_ID = 21;
44e0dac50fSopenharmony_ciconstexpr int W_ACTIVE_IDX = 10;
45e0dac50fSopenharmony_ciconstexpr int W_VIR_PIXEL_RATIO = 4;
46e0dac50fSopenharmony_ciconstexpr int W_SCREEN_ROTATION = 9;
47e0dac50fSopenharmony_ciconstexpr int W_ORIENTATION = 12;
48e0dac50fSopenharmony_ciconstexpr int W_REQUESTED_ORIENTATION = 19;
49e0dac50fSopenharmony_ciconstexpr int W_NODE_ID = 21;
50e0dac50fSopenharmony_ciconstexpr int W_MIRROR_TYPE = 11;
51e0dac50fSopenharmony_ciconstexpr int W_MIRROR_NODE_ID = 13;
52e0dac50fSopenharmony_ci// set the output width of display
53e0dac50fSopenharmony_ciconstexpr int W_DISPLAY_ID = 10;
54e0dac50fSopenharmony_ciconstexpr int W_ABSTR_SCREEN_ID = 9;
55e0dac50fSopenharmony_ciconstexpr int W_REFRESH_RATE = 12;
56e0dac50fSopenharmony_ciconstexpr int W_DISPLAY_ROTATION = 9;
57e0dac50fSopenharmony_ciconstexpr int W_DISPLAY_ORIENTATION = 18;
58e0dac50fSopenharmony_ciconstexpr int W_DISPLAY_FREEZE_FLAG = 11;
59e0dac50fSopenharmony_ciconstexpr int W_DISPLAY_OFFSET_X = 5;
60e0dac50fSopenharmony_ciconstexpr int W_DISPLAY_OFFSET_Y = 5;
61e0dac50fSopenharmony_ciconstexpr int W_DISPLAY_WIDTH = 5;
62e0dac50fSopenharmony_ciconstexpr int W_DISPLAY_HEITHT = 5;
63e0dac50fSopenharmony_ci}
64e0dac50fSopenharmony_ci
65e0dac50fSopenharmony_ciDisplayDumper::DisplayDumper(const sptr<AbstractDisplayController>& abstractDisplayController,
66e0dac50fSopenharmony_ci    const sptr<AbstractScreenController>& abstractScreenController, std::recursive_mutex& mutex)
67e0dac50fSopenharmony_ci    : abstractDisplayController_(abstractDisplayController), abstractScreenController_(abstractScreenController),
68e0dac50fSopenharmony_ci    mutex_(mutex)
69e0dac50fSopenharmony_ci{
70e0dac50fSopenharmony_ci}
71e0dac50fSopenharmony_ci
72e0dac50fSopenharmony_ciDMError DisplayDumper::Dump(int fd, const std::vector<std::u16string>& args) const
73e0dac50fSopenharmony_ci{
74e0dac50fSopenharmony_ci    WLOGFI("Dump begin fd: %{public}d", fd);
75e0dac50fSopenharmony_ci    if (fd < 0) {
76e0dac50fSopenharmony_ci        return DMError::DM_ERROR_INVALID_PARAM;
77e0dac50fSopenharmony_ci    }
78e0dac50fSopenharmony_ci    (void) signal(SIGPIPE, SIG_IGN); // ignore SIGPIPE crash
79e0dac50fSopenharmony_ci    UniqueFd ufd = UniqueFd(fd); // auto close
80e0dac50fSopenharmony_ci    fd = ufd.Get();
81e0dac50fSopenharmony_ci    std::vector<std::string> params;
82e0dac50fSopenharmony_ci    for (auto& arg : args) {
83e0dac50fSopenharmony_ci        params.emplace_back(Str16ToStr8(arg));
84e0dac50fSopenharmony_ci    }
85e0dac50fSopenharmony_ci
86e0dac50fSopenharmony_ci    std::string dumpInfo;
87e0dac50fSopenharmony_ci    if (params.empty()) {
88e0dac50fSopenharmony_ci        ShowHelpInfo(dumpInfo);
89e0dac50fSopenharmony_ci    } else if (params.size() == 1 && params[0] == ARG_DUMP_HELP) { // 1: params num
90e0dac50fSopenharmony_ci        ShowHelpInfo(dumpInfo);
91e0dac50fSopenharmony_ci    } else {
92e0dac50fSopenharmony_ci        DMError errCode = DumpInfo(params, dumpInfo);
93e0dac50fSopenharmony_ci        if (errCode != DMError::DM_OK) {
94e0dac50fSopenharmony_ci            ShowIllegalArgsInfo(dumpInfo, errCode);
95e0dac50fSopenharmony_ci        }
96e0dac50fSopenharmony_ci    }
97e0dac50fSopenharmony_ci    int ret = dprintf(fd, "%s\n", dumpInfo.c_str());
98e0dac50fSopenharmony_ci    if (ret < 0) {
99e0dac50fSopenharmony_ci        WLOGFE("dprintf error");
100e0dac50fSopenharmony_ci        return DMError::DM_ERROR_UNKNOWN;
101e0dac50fSopenharmony_ci    }
102e0dac50fSopenharmony_ci    WLOGFI("Dump end");
103e0dac50fSopenharmony_ci    return DMError::DM_OK;
104e0dac50fSopenharmony_ci}
105e0dac50fSopenharmony_ci
106e0dac50fSopenharmony_civoid DisplayDumper::ShowHelpInfo(std::string& dumpInfo) const
107e0dac50fSopenharmony_ci{
108e0dac50fSopenharmony_ci    dumpInfo.append("Usage:\n")
109e0dac50fSopenharmony_ci        .append(" -h                          ")
110e0dac50fSopenharmony_ci        .append("|help text for the tool\n")
111e0dac50fSopenharmony_ci        .append(" -s -a                       ")
112e0dac50fSopenharmony_ci        .append("|dump all screen information in the system\n")
113e0dac50fSopenharmony_ci        .append(" -d -a                       ")
114e0dac50fSopenharmony_ci        .append("|dump all display information in the system\n")
115e0dac50fSopenharmony_ci        .append(" -s {screen id}              ")
116e0dac50fSopenharmony_ci        .append("|dump specified screen information\n")
117e0dac50fSopenharmony_ci        .append(" -d {display id}             ")
118e0dac50fSopenharmony_ci        .append("|dump specified display information\n");
119e0dac50fSopenharmony_ci}
120e0dac50fSopenharmony_ci
121e0dac50fSopenharmony_civoid DisplayDumper::ShowIllegalArgsInfo(std::string& dumpInfo, DMError errCode) const
122e0dac50fSopenharmony_ci{
123e0dac50fSopenharmony_ci    switch (errCode) {
124e0dac50fSopenharmony_ci        case DMError::DM_ERROR_INVALID_PARAM:
125e0dac50fSopenharmony_ci            dumpInfo.append("The arguments are illegal and you can enter '-h' for help.");
126e0dac50fSopenharmony_ci            break;
127e0dac50fSopenharmony_ci        case DMError::DM_ERROR_NULLPTR:
128e0dac50fSopenharmony_ci            dumpInfo.append("The screen or display is invalid, ")
129e0dac50fSopenharmony_ci                .append("you can enter '-s -a' or '-d -a' to get valid screen or display id.");
130e0dac50fSopenharmony_ci            break;
131e0dac50fSopenharmony_ci        default:
132e0dac50fSopenharmony_ci            break;
133e0dac50fSopenharmony_ci    }
134e0dac50fSopenharmony_ci}
135e0dac50fSopenharmony_ci
136e0dac50fSopenharmony_ciDMError DisplayDumper::DumpInfo(const std::vector<std::string>& args, std::string& dumpInfo) const
137e0dac50fSopenharmony_ci{
138e0dac50fSopenharmony_ci    if (args.size() != 2) { // 2: params num
139e0dac50fSopenharmony_ci        return DMError::DM_ERROR_INVALID_PARAM;
140e0dac50fSopenharmony_ci    }
141e0dac50fSopenharmony_ci
142e0dac50fSopenharmony_ci    if (args[0] == ARG_DUMP_SCREEN && args[1] == ARG_DUMP_ALL) {
143e0dac50fSopenharmony_ci        return DumpAllScreenInfo(dumpInfo);
144e0dac50fSopenharmony_ci    } else if (args[0] == ARG_DUMP_DISPLAY && args[1] == ARG_DUMP_ALL) {
145e0dac50fSopenharmony_ci        return DumpAllDisplayInfo(dumpInfo);
146e0dac50fSopenharmony_ci    } else if (args[0] == ARG_DUMP_SCREEN && IsValidDigitString(args[1])) {
147e0dac50fSopenharmony_ci        ScreenId screenId = std::stoull(args[1]);
148e0dac50fSopenharmony_ci        return DumpSpecifiedScreenInfo(screenId, dumpInfo);
149e0dac50fSopenharmony_ci    } else if (args[0] == ARG_DUMP_DISPLAY && IsValidDigitString(args[1])) {
150e0dac50fSopenharmony_ci        DisplayId displayId = std::stoull(args[1]);
151e0dac50fSopenharmony_ci        return DumpSpecifiedDisplayInfo(displayId, dumpInfo);
152e0dac50fSopenharmony_ci    } else {
153e0dac50fSopenharmony_ci        return DMError::DM_ERROR_INVALID_PARAM;
154e0dac50fSopenharmony_ci    }
155e0dac50fSopenharmony_ci}
156e0dac50fSopenharmony_ci
157e0dac50fSopenharmony_ciDMError DisplayDumper::DumpAllScreenInfo(std::string& dumpInfo) const
158e0dac50fSopenharmony_ci{
159e0dac50fSopenharmony_ci    std::map<ScreenId, sptr<AbstractScreenGroup>> screenGroups;
160e0dac50fSopenharmony_ci    std::vector<ScreenId> screenIds = abstractScreenController_->GetAllScreenIds();
161e0dac50fSopenharmony_ci    std::ostringstream oss;
162e0dac50fSopenharmony_ci    oss << "--------------------------------------Free Screen"
163e0dac50fSopenharmony_ci        << "--------------------------------------"
164e0dac50fSopenharmony_ci        << std::endl;
165e0dac50fSopenharmony_ci    oss << "ScreenName           Type     IsGroup DmsId RsId                 ActiveIdx VPR Rotation Orientation "
166e0dac50fSopenharmony_ci        << "RequestOrientation NodeId               IsMirrored MirrorNodeId"
167e0dac50fSopenharmony_ci        << std::endl;
168e0dac50fSopenharmony_ci    std::lock_guard<std::recursive_mutex> lock(mutex_);
169e0dac50fSopenharmony_ci    for (ScreenId screenId : screenIds) {
170e0dac50fSopenharmony_ci        auto screen = abstractScreenController_->GetAbstractScreen(screenId);
171e0dac50fSopenharmony_ci        if (screen == nullptr) {
172e0dac50fSopenharmony_ci            WLOGFE("screen is null");
173e0dac50fSopenharmony_ci            return DMError::DM_ERROR_NULLPTR;
174e0dac50fSopenharmony_ci        }
175e0dac50fSopenharmony_ci        if (SCREEN_ID_INVALID == screen->groupDmsId_ || screen->isScreenGroup_) {
176e0dac50fSopenharmony_ci            GetScreenInfo(screen, oss);
177e0dac50fSopenharmony_ci        }
178e0dac50fSopenharmony_ci        if (screen->isScreenGroup_) {
179e0dac50fSopenharmony_ci            auto screenGroup = abstractScreenController_->GetAbstractScreenGroup(screenId);
180e0dac50fSopenharmony_ci            screenGroups.insert(std::make_pair(screenId, screenGroup));
181e0dac50fSopenharmony_ci        }
182e0dac50fSopenharmony_ci    }
183e0dac50fSopenharmony_ci    oss << "total screen num: " << screenIds.size() << std::endl;
184e0dac50fSopenharmony_ci    dumpInfo.append(oss.str());
185e0dac50fSopenharmony_ci    for (auto it = screenGroups.begin(); it != screenGroups.end(); it++) {
186e0dac50fSopenharmony_ci        DMError ret = DumpScreenInfo(it->second, dumpInfo);
187e0dac50fSopenharmony_ci        if (ret != DMError::DM_OK) {
188e0dac50fSopenharmony_ci            return ret;
189e0dac50fSopenharmony_ci        }
190e0dac50fSopenharmony_ci    }
191e0dac50fSopenharmony_ci    return DMError::DM_OK;
192e0dac50fSopenharmony_ci}
193e0dac50fSopenharmony_ci
194e0dac50fSopenharmony_ciDMError DisplayDumper::DumpScreenInfo(const sptr<AbstractScreenGroup>& screenGroup, std::string& dumpInfo) const
195e0dac50fSopenharmony_ci{
196e0dac50fSopenharmony_ci    if (screenGroup == nullptr) {
197e0dac50fSopenharmony_ci        WLOGFE("screenGroup is null");
198e0dac50fSopenharmony_ci        return DMError::DM_ERROR_NULLPTR;
199e0dac50fSopenharmony_ci    }
200e0dac50fSopenharmony_ci    std::ostringstream oss;
201e0dac50fSopenharmony_ci    oss << "-------------------------------------ScreenGroup " << screenGroup->dmsId_
202e0dac50fSopenharmony_ci        << "-------------------------------------"
203e0dac50fSopenharmony_ci        << std::endl;
204e0dac50fSopenharmony_ci    oss << "ScreenName           Type     IsGroup DmsId RsId                 "
205e0dac50fSopenharmony_ci        << "ActiveIdx VPR Rotation Orientation "
206e0dac50fSopenharmony_ci        << "RequestOrientation NodeId               IsMirrored MirrorNodeId"
207e0dac50fSopenharmony_ci        << std::endl;
208e0dac50fSopenharmony_ci    auto childrenScreen = screenGroup->GetChildren();
209e0dac50fSopenharmony_ci    for (auto screen : childrenScreen) {
210e0dac50fSopenharmony_ci        GetScreenInfo(screen, oss);
211e0dac50fSopenharmony_ci    }
212e0dac50fSopenharmony_ci    dumpInfo.append(oss.str());
213e0dac50fSopenharmony_ci    return DMError::DM_OK;
214e0dac50fSopenharmony_ci}
215e0dac50fSopenharmony_ci
216e0dac50fSopenharmony_ciDMError DisplayDumper::DumpSpecifiedScreenInfo(ScreenId screenId, std::string& dumpInfo) const
217e0dac50fSopenharmony_ci{
218e0dac50fSopenharmony_ci    auto screen = abstractScreenController_->GetAbstractScreen(screenId);
219e0dac50fSopenharmony_ci    if (screen == nullptr) {
220e0dac50fSopenharmony_ci        WLOGFE("screen is null");
221e0dac50fSopenharmony_ci        return DMError::DM_ERROR_NULLPTR;
222e0dac50fSopenharmony_ci    }
223e0dac50fSopenharmony_ci
224e0dac50fSopenharmony_ci    const std::string& name = screen->GetScreenName();
225e0dac50fSopenharmony_ci    const std::string& screenName = name.size() <= SCREEN_NAME_MAX_LENGTH ?
226e0dac50fSopenharmony_ci        name : name.substr(0, SCREEN_NAME_MAX_LENGTH);
227e0dac50fSopenharmony_ci    std::string isGroup = screen->isScreenGroup_ ? "true" : "false";
228e0dac50fSopenharmony_ci    std::string screenType = TransferTypeToString(screen->type_);
229e0dac50fSopenharmony_ci    std::string isMirrored = screen->rSDisplayNodeConfig_.isMirrored ? "true" : "false";
230e0dac50fSopenharmony_ci    NodeId nodeId = (screen->rsDisplayNode_ == nullptr) ? SCREEN_ID_INVALID : screen->rsDisplayNode_->GetId();
231e0dac50fSopenharmony_ci    std::ostringstream oss;
232e0dac50fSopenharmony_ci    oss << "ScreenName: " << screenName << std::endl;
233e0dac50fSopenharmony_ci    oss << "Type: " << screenType << std::endl;
234e0dac50fSopenharmony_ci    oss << "IsGroup: " << isGroup << std::endl;
235e0dac50fSopenharmony_ci    oss << "DmsId: " << screen->dmsId_ << std::endl;
236e0dac50fSopenharmony_ci    oss << "RsId: " << screen->rsId_ << std::endl;
237e0dac50fSopenharmony_ci    oss << "GroupDmsId: " << screen->groupDmsId_ << std::endl;
238e0dac50fSopenharmony_ci    oss << "ActiveIdx: " << screen->activeIdx_ << std::endl;
239e0dac50fSopenharmony_ci    oss << "VPR: " << screen->virtualPixelRatio_ << std::endl;
240e0dac50fSopenharmony_ci    oss << "Rotation: " << static_cast<uint32_t>(screen->rotation_) << std::endl;
241e0dac50fSopenharmony_ci    oss << "Orientation: " << static_cast<uint32_t>(screen->orientation_) << std::endl;
242e0dac50fSopenharmony_ci    oss << "RequestOrientation: " << static_cast<uint32_t>(screen->screenRequestedOrientation_) << std::endl;
243e0dac50fSopenharmony_ci    oss << "NodeId: " << nodeId << std::endl;
244e0dac50fSopenharmony_ci    oss << "IsMirrored: " << isMirrored << std::endl;
245e0dac50fSopenharmony_ci    oss << "MirrorNodeId: " << screen->rSDisplayNodeConfig_.mirrorNodeId << std::endl;
246e0dac50fSopenharmony_ci    dumpInfo.append(oss.str());
247e0dac50fSopenharmony_ci    return DMError::DM_OK;
248e0dac50fSopenharmony_ci}
249e0dac50fSopenharmony_ci
250e0dac50fSopenharmony_ciDMError DisplayDumper::DumpAllDisplayInfo(std::string& dumpInfo) const
251e0dac50fSopenharmony_ci{
252e0dac50fSopenharmony_ci    std::vector<DisplayId> displayIds = abstractDisplayController_->GetAllDisplayIds();
253e0dac50fSopenharmony_ci    std::ostringstream oss;
254e0dac50fSopenharmony_ci    oss << "--------------------------------------Display Info"
255e0dac50fSopenharmony_ci        << "--------------------------------------"
256e0dac50fSopenharmony_ci        << std::endl;
257e0dac50fSopenharmony_ci    oss << "DisplayId ScreenId RefreshRate VPR Rotation Orientation DisplayOrientation FreezeFlag [ x   y   w   h   ]"
258e0dac50fSopenharmony_ci        << std::endl;
259e0dac50fSopenharmony_ci    std::lock_guard<std::recursive_mutex> lock(mutex_);
260e0dac50fSopenharmony_ci    for (DisplayId displayId : displayIds) {
261e0dac50fSopenharmony_ci        auto display = abstractDisplayController_->GetAbstractDisplay(displayId);
262e0dac50fSopenharmony_ci        if (display == nullptr) {
263e0dac50fSopenharmony_ci            WLOGFE("display is null");
264e0dac50fSopenharmony_ci            return DMError::DM_ERROR_NULLPTR;
265e0dac50fSopenharmony_ci        }
266e0dac50fSopenharmony_ci        GetDisplayInfo(display, oss);
267e0dac50fSopenharmony_ci    }
268e0dac50fSopenharmony_ci    dumpInfo.append(oss.str());
269e0dac50fSopenharmony_ci    return DMError::DM_OK;
270e0dac50fSopenharmony_ci}
271e0dac50fSopenharmony_ci
272e0dac50fSopenharmony_ciDMError DisplayDumper::DumpSpecifiedDisplayInfo(DisplayId displayId, std::string& dumpInfo) const
273e0dac50fSopenharmony_ci{
274e0dac50fSopenharmony_ci    auto display = abstractDisplayController_->GetAbstractDisplay(displayId);
275e0dac50fSopenharmony_ci    if (display == nullptr) {
276e0dac50fSopenharmony_ci        WLOGFE("display is null");
277e0dac50fSopenharmony_ci        return DMError::DM_ERROR_NULLPTR;
278e0dac50fSopenharmony_ci    }
279e0dac50fSopenharmony_ci    std::ostringstream oss;
280e0dac50fSopenharmony_ci    oss << "DisplayId: " << display->GetId() << std::endl;
281e0dac50fSopenharmony_ci    oss << "ScreenId: " << display->GetAbstractScreenId() << std::endl;
282e0dac50fSopenharmony_ci    oss << "RefreshRate: " << display->GetRefreshRate() << std::endl;
283e0dac50fSopenharmony_ci    oss << "VPR: " << display->GetVirtualPixelRatio() << std::endl;
284e0dac50fSopenharmony_ci    oss << "Rotation: " << static_cast<uint32_t>(display->GetRotation()) << std::endl;
285e0dac50fSopenharmony_ci    oss << "Orientation: " << static_cast<uint32_t>(display->GetOrientation()) << std::endl;
286e0dac50fSopenharmony_ci    oss << "DisplayOrientation: " << static_cast<uint32_t>(display->GetDisplayOrientation()) << std::endl;
287e0dac50fSopenharmony_ci    oss << "FreezeFlag: " << static_cast<uint32_t>(display->GetFreezeFlag()) << std::endl;
288e0dac50fSopenharmony_ci    oss << "DisplayRect: " << "[ "
289e0dac50fSopenharmony_ci        << display->GetOffsetX() << ", " << display->GetOffsetY() << ", "
290e0dac50fSopenharmony_ci        << display->GetWidth() << ", " << display->GetHeight() << " ]" << std::endl;
291e0dac50fSopenharmony_ci    dumpInfo.append(oss.str());
292e0dac50fSopenharmony_ci    return DMError::DM_OK;
293e0dac50fSopenharmony_ci}
294e0dac50fSopenharmony_ci
295e0dac50fSopenharmony_cibool DisplayDumper::IsValidDigitString(const std::string& idStr) const
296e0dac50fSopenharmony_ci{
297e0dac50fSopenharmony_ci    if (idStr.empty()) {
298e0dac50fSopenharmony_ci        return false;
299e0dac50fSopenharmony_ci    }
300e0dac50fSopenharmony_ci    for (char ch : idStr) {
301e0dac50fSopenharmony_ci        if ((ch >= '0' && ch <= '9')) {
302e0dac50fSopenharmony_ci            continue;
303e0dac50fSopenharmony_ci        }
304e0dac50fSopenharmony_ci        WLOGFE("invalid id");
305e0dac50fSopenharmony_ci        return false;
306e0dac50fSopenharmony_ci    }
307e0dac50fSopenharmony_ci    return true;
308e0dac50fSopenharmony_ci}
309e0dac50fSopenharmony_ci
310e0dac50fSopenharmony_cistd::string DisplayDumper::TransferTypeToString(ScreenType type) const
311e0dac50fSopenharmony_ci{
312e0dac50fSopenharmony_ci    std::string screenType;
313e0dac50fSopenharmony_ci    switch (type) {
314e0dac50fSopenharmony_ci        case ScreenType::REAL:
315e0dac50fSopenharmony_ci            screenType = "REAL";
316e0dac50fSopenharmony_ci            break;
317e0dac50fSopenharmony_ci        case ScreenType::VIRTUAL:
318e0dac50fSopenharmony_ci            screenType = "VIRTUAL";
319e0dac50fSopenharmony_ci            break;
320e0dac50fSopenharmony_ci        default:
321e0dac50fSopenharmony_ci            screenType = "UNDEFINED";
322e0dac50fSopenharmony_ci            break;
323e0dac50fSopenharmony_ci    }
324e0dac50fSopenharmony_ci    return screenType;
325e0dac50fSopenharmony_ci}
326e0dac50fSopenharmony_ci
327e0dac50fSopenharmony_civoid DisplayDumper::GetScreenInfo(const sptr<AbstractScreen>& screen, std::ostringstream& oss) const
328e0dac50fSopenharmony_ci{
329e0dac50fSopenharmony_ci    if (screen == nullptr) {
330e0dac50fSopenharmony_ci        WLOGFE("screen is null");
331e0dac50fSopenharmony_ci        return;
332e0dac50fSopenharmony_ci    }
333e0dac50fSopenharmony_ci
334e0dac50fSopenharmony_ci    const std::string& name = screen->GetScreenName();
335e0dac50fSopenharmony_ci    const std::string& screenName = name.size() <= SCREEN_NAME_MAX_LENGTH ?
336e0dac50fSopenharmony_ci        name : name.substr(0, SCREEN_NAME_MAX_LENGTH);
337e0dac50fSopenharmony_ci    std::string isGroup = screen->isScreenGroup_ ? "true" : "false";
338e0dac50fSopenharmony_ci    std::string screenType = TransferTypeToString(screen->type_);
339e0dac50fSopenharmony_ci    std::string isMirrored = screen->rSDisplayNodeConfig_.isMirrored ? "true" : "false";
340e0dac50fSopenharmony_ci    NodeId nodeId = (screen->rsDisplayNode_ == nullptr) ? SCREEN_ID_INVALID : screen->rsDisplayNode_->GetId();
341e0dac50fSopenharmony_ci    // std::setw is used to set the output width and different width values are set to keep the format aligned.
342e0dac50fSopenharmony_ci    oss << std::left << std::setw(W_SCREEN_NAME) << screenName
343e0dac50fSopenharmony_ci        << std::left << std::setw(W_SCREEN_TYPE) << screenType
344e0dac50fSopenharmony_ci        << std::left << std::setw(W_GROUP_TYPE) << isGroup
345e0dac50fSopenharmony_ci        << std::left << std::setw(W_DMS_ID) << screen->dmsId_
346e0dac50fSopenharmony_ci        << std::left << std::setw(W_RS_ID) << screen->rsId_
347e0dac50fSopenharmony_ci        << std::left << std::setw(W_ACTIVE_IDX) << screen->activeIdx_
348e0dac50fSopenharmony_ci        << std::left << std::setw(W_VIR_PIXEL_RATIO) << screen->virtualPixelRatio_
349e0dac50fSopenharmony_ci        << std::left << std::setw(W_SCREEN_ROTATION) << static_cast<uint32_t>(screen->rotation_)
350e0dac50fSopenharmony_ci        << std::left << std::setw(W_ORIENTATION) << static_cast<uint32_t>(screen->orientation_)
351e0dac50fSopenharmony_ci        << std::left << std::setw(W_REQUESTED_ORIENTATION) << static_cast<uint32_t>(screen->screenRequestedOrientation_)
352e0dac50fSopenharmony_ci        << std::left << std::setw(W_NODE_ID) << nodeId
353e0dac50fSopenharmony_ci        << std::left << std::setw(W_MIRROR_TYPE) << isMirrored
354e0dac50fSopenharmony_ci        << std::left << std::setw(W_MIRROR_NODE_ID) << screen->rSDisplayNodeConfig_.mirrorNodeId
355e0dac50fSopenharmony_ci        << std::endl;
356e0dac50fSopenharmony_ci}
357e0dac50fSopenharmony_ci
358e0dac50fSopenharmony_civoid DisplayDumper::GetDisplayInfo(const sptr<AbstractDisplay>& display, std::ostringstream& oss) const
359e0dac50fSopenharmony_ci{
360e0dac50fSopenharmony_ci    if (display == nullptr) {
361e0dac50fSopenharmony_ci        WLOGFE("display is null");
362e0dac50fSopenharmony_ci        return;
363e0dac50fSopenharmony_ci    }
364e0dac50fSopenharmony_ci    // std::setw is used to set the output width and different width values are set to keep the format aligned.
365e0dac50fSopenharmony_ci    oss << std::left << std::setw(W_DISPLAY_ID) << display->GetId()
366e0dac50fSopenharmony_ci        << std::left << std::setw(W_ABSTR_SCREEN_ID) << display->GetAbstractScreenId()
367e0dac50fSopenharmony_ci        << std::left << std::setw(W_REFRESH_RATE) << display->GetRefreshRate()
368e0dac50fSopenharmony_ci        << std::left << std::setw(W_VIR_PIXEL_RATIO) << display->GetVirtualPixelRatio()
369e0dac50fSopenharmony_ci        << std::left << std::setw(W_DISPLAY_ROTATION) << static_cast<uint32_t>(display->GetRotation())
370e0dac50fSopenharmony_ci        << std::left << std::setw(W_ORIENTATION) << static_cast<uint32_t>(display->GetOrientation())
371e0dac50fSopenharmony_ci        << std::left << std::setw(W_DISPLAY_ORIENTATION) << static_cast<uint32_t>(display->GetDisplayOrientation())
372e0dac50fSopenharmony_ci        << std::left << std::setw(W_DISPLAY_FREEZE_FLAG) << static_cast<uint32_t>(display->GetFreezeFlag())
373e0dac50fSopenharmony_ci        << "[ "
374e0dac50fSopenharmony_ci        << std::left << std::setw(W_DISPLAY_OFFSET_X) << display->GetOffsetX()
375e0dac50fSopenharmony_ci        << std::left << std::setw(W_DISPLAY_OFFSET_Y) << display->GetOffsetY()
376e0dac50fSopenharmony_ci        << std::left << std::setw(W_DISPLAY_WIDTH) << display->GetWidth()
377e0dac50fSopenharmony_ci        << std::left << std::setw(W_DISPLAY_HEITHT) << display->GetHeight()
378e0dac50fSopenharmony_ci        << "]"
379e0dac50fSopenharmony_ci        << std::endl;
380e0dac50fSopenharmony_ci}
381e0dac50fSopenharmony_ci}
382e0dac50fSopenharmony_ci}