1e0dac50fSopenharmony_ci/*
2e0dac50fSopenharmony_ci * Copyright (c) 2021-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 <cstdio>
17e0dac50fSopenharmony_ci#include <image_type.h>
18e0dac50fSopenharmony_ci#include <iosfwd>
19e0dac50fSopenharmony_ci#include <iostream>
20e0dac50fSopenharmony_ci#include <memory>
21e0dac50fSopenharmony_ci#include <ostream>
22e0dac50fSopenharmony_ci#include <refbase.h>
23e0dac50fSopenharmony_ci
24e0dac50fSopenharmony_ci#include "display_manager.h"
25e0dac50fSopenharmony_ci#include "parameters.h"
26e0dac50fSopenharmony_ci#include "snapshot_utils.h"
27e0dac50fSopenharmony_ci
28e0dac50fSopenharmony_ciusing namespace OHOS;
29e0dac50fSopenharmony_ciusing namespace OHOS::Media;
30e0dac50fSopenharmony_ciusing namespace OHOS::Rosen;
31e0dac50fSopenharmony_ciusing OHOS::system::GetParameter;
32e0dac50fSopenharmony_ci
33e0dac50fSopenharmony_ci// developer mode
34e0dac50fSopenharmony_cistatic const std::string DEVELOPER_MODE_STATE_ON_DEFAULT = "false";
35e0dac50fSopenharmony_cistatic const std::string DEVELOPER_MODE_PARAMETER = "const.security.developermode.state";
36e0dac50fSopenharmony_cistatic const std::string IS_DEVELOPER_MODE = GetParameter(DEVELOPER_MODE_PARAMETER, DEVELOPER_MODE_STATE_ON_DEFAULT);
37e0dac50fSopenharmony_ci
38e0dac50fSopenharmony_cistatic bool GetScreenshotByCmdArguments(CmdArguments& cmdArguments, sptr<Display> display,
39e0dac50fSopenharmony_ci    std::shared_ptr<OHOS::Media::PixelMap>& pixelMap);
40e0dac50fSopenharmony_ci
41e0dac50fSopenharmony_ciint main(int argc, char *argv[])
42e0dac50fSopenharmony_ci{
43e0dac50fSopenharmony_ci    CmdArguments cmdArguments;
44e0dac50fSopenharmony_ci    cmdArguments.fileName = "";
45e0dac50fSopenharmony_ci
46e0dac50fSopenharmony_ci    if (!SnapShotUtils::ProcessArgs(argc, argv, cmdArguments)) {
47e0dac50fSopenharmony_ci        _exit(-1);
48e0dac50fSopenharmony_ci    }
49e0dac50fSopenharmony_ci
50e0dac50fSopenharmony_ci    if (DEVELOPER_MODE_STATE_ON_DEFAULT == IS_DEVELOPER_MODE) {
51e0dac50fSopenharmony_ci        std::cout << "current mode is not developer mode, just return." << std::endl;
52e0dac50fSopenharmony_ci        _exit(-1);
53e0dac50fSopenharmony_ci    }
54e0dac50fSopenharmony_ci
55e0dac50fSopenharmony_ci    auto display = DisplayManager::GetInstance().GetDisplayById(cmdArguments.displayId);
56e0dac50fSopenharmony_ci    if (display == nullptr) {
57e0dac50fSopenharmony_ci        std::cout << "error: GetDisplayById " << cmdArguments.displayId << " error!" << std::endl;
58e0dac50fSopenharmony_ci        _exit(-1);
59e0dac50fSopenharmony_ci    }
60e0dac50fSopenharmony_ci    if (cmdArguments.fileType != "png") {
61e0dac50fSopenharmony_ci        cmdArguments.fileType = "jpeg";
62e0dac50fSopenharmony_ci    }
63e0dac50fSopenharmony_ci
64e0dac50fSopenharmony_ci    std::cout << "process: display " << cmdArguments.displayId << ", file type: " << cmdArguments.fileType <<
65e0dac50fSopenharmony_ci        ", width: " << display->GetWidth() << ", height: " << display->GetHeight() << std::endl;
66e0dac50fSopenharmony_ci
67e0dac50fSopenharmony_ci    // get PixelMap from DisplayManager API
68e0dac50fSopenharmony_ci    std::shared_ptr<OHOS::Media::PixelMap> pixelMap = nullptr;
69e0dac50fSopenharmony_ci    if (!GetScreenshotByCmdArguments(cmdArguments, display, pixelMap)) {
70e0dac50fSopenharmony_ci        _exit(-1);
71e0dac50fSopenharmony_ci    }
72e0dac50fSopenharmony_ci
73e0dac50fSopenharmony_ci    bool ret = false;
74e0dac50fSopenharmony_ci    if (pixelMap != nullptr) {
75e0dac50fSopenharmony_ci        if (cmdArguments.fileType == "png") {
76e0dac50fSopenharmony_ci            ret = SnapShotUtils::SaveSnapShot(cmdArguments.fileName, *pixelMap, cmdArguments.fileType);
77e0dac50fSopenharmony_ci        } else {
78e0dac50fSopenharmony_ci            ret = SnapShotUtils::WriteToJpegWithPixelMap(cmdArguments.fileName, *pixelMap);
79e0dac50fSopenharmony_ci        }
80e0dac50fSopenharmony_ci    }
81e0dac50fSopenharmony_ci    if (!ret) {
82e0dac50fSopenharmony_ci        std::cout << "\nerror: snapshot display " << cmdArguments.displayId <<
83e0dac50fSopenharmony_ci            ", write to " << cmdArguments.fileName << " as jpeg failed!" << std::endl;
84e0dac50fSopenharmony_ci        _exit(-1);
85e0dac50fSopenharmony_ci    }
86e0dac50fSopenharmony_ci
87e0dac50fSopenharmony_ci    std::cout << "\nsuccess: snapshot display " << cmdArguments.displayId << " , write to " <<
88e0dac50fSopenharmony_ci        cmdArguments.fileName << " as " << cmdArguments.fileType << ", width: " << pixelMap->GetWidth() <<
89e0dac50fSopenharmony_ci        ", height: " << pixelMap->GetHeight() << std::endl;
90e0dac50fSopenharmony_ci    _exit(0);
91e0dac50fSopenharmony_ci}
92e0dac50fSopenharmony_ci
93e0dac50fSopenharmony_cistatic bool GetScreenshotByCmdArguments(CmdArguments& cmdArguments, sptr<Display> display,
94e0dac50fSopenharmony_ci    std::shared_ptr<OHOS::Media::PixelMap>& pixelMap)
95e0dac50fSopenharmony_ci{
96e0dac50fSopenharmony_ci    if (!cmdArguments.isWidthSet && !cmdArguments.isHeightSet) {
97e0dac50fSopenharmony_ci        pixelMap = DisplayManager::GetInstance().GetScreenshot(cmdArguments.displayId);  // default width & height
98e0dac50fSopenharmony_ci    } else {
99e0dac50fSopenharmony_ci        if (!cmdArguments.isWidthSet) {
100e0dac50fSopenharmony_ci            cmdArguments.width = display->GetWidth();
101e0dac50fSopenharmony_ci            std::cout << "process: reset to display's width " << cmdArguments.width << std::endl;
102e0dac50fSopenharmony_ci        }
103e0dac50fSopenharmony_ci        if (!cmdArguments.isHeightSet) {
104e0dac50fSopenharmony_ci            cmdArguments.height = display->GetHeight();
105e0dac50fSopenharmony_ci            std::cout << "process: reset to display's height " << cmdArguments.height << std::endl;
106e0dac50fSopenharmony_ci        }
107e0dac50fSopenharmony_ci        if (!SnapShotUtils::CheckWidthAndHeightValid(cmdArguments.width, cmdArguments.height)) {
108e0dac50fSopenharmony_ci            std::cout << "error: width " << cmdArguments.width << " height " <<
109e0dac50fSopenharmony_ci            cmdArguments.height << " invalid!" << std::endl;
110e0dac50fSopenharmony_ci            return false;
111e0dac50fSopenharmony_ci        }
112e0dac50fSopenharmony_ci        const Media::Rect rect = {0, 0, display->GetWidth(), display->GetHeight()};
113e0dac50fSopenharmony_ci        const Media::Size size = {cmdArguments.width, cmdArguments.height};
114e0dac50fSopenharmony_ci        constexpr int rotation = 0;
115e0dac50fSopenharmony_ci        pixelMap = DisplayManager::GetInstance().GetScreenshot(cmdArguments.displayId, rect, size, rotation);
116e0dac50fSopenharmony_ci    }
117e0dac50fSopenharmony_ci    return true;
118e0dac50fSopenharmony_ci}