b9e5c0ac3ce55b715de83511f6f05757cdde33c3
[platform/core/uifw/aurum.git] / org.tizen.aurum-bootstrap / src / Commands / TakeScreenshotCommand.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17
18 #include "bootstrap.h"
19 #include "TakeScreenshotCommand.h"
20 #include <fstream>
21 #include "UiObject.h"
22 #include "UiDevice.h"
23
24 TakeScreenshotCommand::TakeScreenshotCommand(
25     const ::aurum::ReqTakeScreenshot *request,
26     ::grpc::ServerWriter< ::aurum::RspTakeScreenshot> *writer)
27     : mRequest{request}, mWriter{writer}
28 {
29 }
30
31 ::grpc::Status TakeScreenshotCommand::execute()
32 {
33     LOGI("TakeScreenshot --------------- ");
34
35     struct tm timeinfo;
36     time_t now = time(0);
37     if (!localtime_r(&now, &timeinfo)) {
38         LOGE("fail to get localtime. Screenshot cancelled");
39         return grpc::Status::CANCELLED;
40     }
41
42     char name[128];
43     std::snprintf(name, 128, "/tmp/screenshot-%d-%d-%d-%d:%d:%d.png",
44                               (timeinfo.tm_year + 1900), (timeinfo.tm_mon + 1), timeinfo.tm_mday,
45                               timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
46     std::string path(name);
47     std::shared_ptr<UiDevice> mDevice = UiDevice::getInstance();
48     mDevice->takeScreenshot(path, 1.0, 1);
49
50     std::ifstream ifs(path, std::ifstream::binary);
51     ::aurum::RspTakeScreenshot rsp;
52     int size = mDevice->getScreenSize().width * mDevice->getScreenSize().height;
53     char buf[size];
54
55     while (!ifs.eof()) {
56         ifs.read(buf, size);
57         rsp.set_image(buf, ifs.gcount());
58         mWriter->Write(rsp);
59     }
60     ifs.close();
61
62     return grpc::Status::OK;
63 }