• 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 <chrono>
17 #include <thread>
18 #include <vector>
19 
20 #include "CommandLineInterface.h"
21 #include "CommandParser.h"
22 #include "CppTimer.h"
23 #include "CppTimerManager.h"
24 #include "CrashHandler.h"
25 #include "Interrupter.h"
26 #include "JsAppImpl.h"
27 #include "PreviewerEngineLog.h"
28 #include "SharedData.h"
29 #include "TraceTool.h"
30 #include "VirtualScreenImpl.h"
31 
32 using namespace std;
33 static const int START_PARAM_INVALID_CODE = 11;
34 static const int NOTIFY_INTERVAL_TIME = 1000; // Unit millisecond
35 
InitDeviceOrientation()36 static void InitDeviceOrientation()
37 {
38     CommandParser& parser = CommandParser::GetInstance();
39     if (parser.GetCompressionResolutionWidth() <= parser.GetCompressionResolutionHeight()) {
40         ILOG("InitDeviceOrientation is portrait.");
41         JsAppImpl::GetInstance().SetDeviceOrentation("portrait");
42     } else {
43         ILOG("InitDeviceOrientation is landscape.");
44         JsAppImpl::GetInstance().SetDeviceOrentation("landscape");
45     }
46 }
47 
InitResolution()48 static void InitResolution()
49 {
50     CommandParser& parser = CommandParser::GetInstance();
51     VirtualScreenImpl::GetInstance().SetOrignalWidth(parser.GetOrignalResolutionWidth());
52     VirtualScreenImpl::GetInstance().SetOrignalHeight(parser.GetOrignalResolutionHeight());
53     VirtualScreenImpl::GetInstance().SetCompressionWidth(parser.GetCompressionResolutionWidth());
54     VirtualScreenImpl::GetInstance().SetCompressionHeight(parser.GetCompressionResolutionHeight());
55     VirtualScreenImpl::GetInstance().SetCurrentResolution(parser.GetOrignalResolutionWidth(),
56         parser.GetOrignalResolutionHeight());
57 }
58 
InitFoldParams()59 static void InitFoldParams()
60 {
61     CommandParser& parser = CommandParser::GetInstance();
62     if (parser.IsSet("foldable")) {
63         VirtualScreenImpl::GetInstance().SetFoldable(parser.IsFoldable());
64     }
65     if (parser.IsSet("foldStatus")) {
66         VirtualScreenImpl::GetInstance().SetFoldStatus(parser.GetFoldStatus());
67     }
68     if (parser.IsSet("fr")) {
69         VirtualScreenImpl::GetInstance().SetFoldResolution(parser.GetFoldResolutionWidth(),
70             parser.GetFoldResolutionHeight());
71     }
72 }
73 
InitJsApp()74 static void InitJsApp()
75 {
76     CommandParser& parser = CommandParser::GetInstance();
77     // Initialize Image Pipeline Name
78     if (parser.IsSet("s")) {
79         JsAppImpl::GetInstance().SetPipeName(parser.Value("s"));
80     }
81 
82     if (parser.IsSet("lws")) {
83         JsAppImpl::GetInstance().SetPipePort(parser.Value("lws"));
84     }
85 
86     JsAppImpl::GetInstance().SetJsAppPath(parser.Value("j"));
87 
88     if (parser.IsSet("url")) {
89         JsAppImpl::GetInstance().SetUrlPath(parser.Value("url"));
90     }
91 
92     if (parser.IsSet("cm")) {
93         JsAppImpl::GetInstance().SetArgsColorMode(parser.Value("cm"));
94     }
95 
96     if (parser.IsSet("av")) {
97         JsAppImpl::GetInstance().SetArgsAceVersion(parser.Value("av"));
98     }
99 
100     if (parser.IsSet("sd")) {
101         JsAppImpl::GetInstance().SetScreenDensity(parser.Value("sd"));
102     }
103 
104     if (parser.IsSet("cc")) {
105         JsAppImpl::GetInstance().SetConfigChanges(parser.Value("cc"));
106     }
107 
108     InitDeviceOrientation();
109 
110     if (parser.IsSet("d")) {
111         JsAppImpl::GetInstance().SetIsDebug(true);
112         if (parser.IsSet("p")) {
113             JsAppImpl::GetInstance().SetDebugServerPort(static_cast<uint16_t>(atoi(parser.Value("p").c_str())));
114         }
115     }
116     InitResolution();
117     InitFoldParams();
118 
119     JsAppImpl::GetInstance().Start();
120 }
121 
NotifyInspectorChanged()122 static void NotifyInspectorChanged()
123 {
124     if (!VirtualScreenImpl::GetInstance().isFrameUpdated) {
125         return;
126     }
127     VirtualScreenImpl::GetInstance().isFrameUpdated = false;
128 
129     static std::string jsonTreeLast = "";
130     std::string jsonTree = JsAppImpl::GetInstance().GetJSONTree();
131     if (jsonTree == jsonTreeLast) {
132         return;
133     }
134 
135     jsonTreeLast = jsonTree;
136     Json2::Value commandResult = JsonReader::CreateObject();
137     commandResult.Add("version", CommandLineInterface::COMMAND_VERSION.c_str());
138     commandResult.Add("command", "inspector");
139     commandResult.Add("result", jsonTree.c_str());
140     CommandLineInterface::GetInstance().SendJsonData(commandResult);
141     ILOG("Send inspector json tree.");
142 }
143 
ProcessCommand()144 static void ProcessCommand()
145 {
146     if (!CommandParser::GetInstance().IsSet("d")) {
147         static CppTimer inspectorNotifytimer(NotifyInspectorChanged);
148         inspectorNotifytimer.Start(NOTIFY_INTERVAL_TIME); // Notify per second
149         CppTimerManager::GetTimerManager().AddCppTimer(inspectorNotifytimer);
150     }
151 
152     VirtualScreenImpl::GetInstance().InitFrameCountTimer();
153     while (!Interrupter::IsInterrupt()) {
154         CommandLineInterface::GetInstance().ProcessCommand();
155         CppTimerManager::GetTimerManager().RunTimerTick();
156         this_thread::sleep_for(chrono::milliseconds(1));
157     }
158     JsAppImpl::GetInstance().Stop();
159 }
160 
InitSharedData()161 static void InitSharedData()
162 {
163     CommandParser& parser = CommandParser::GetInstance();
164     if (parser.IsSet("l")) {
165         SharedData<string>(SharedDataType::LANGUAGE, parser.Value("l"));
166     } else {
167         SharedData<string>(SharedDataType::LANGUAGE, "zh_CN");
168     }
169     string lanInfo = SharedData<string>::GetData(SharedDataType::LANGUAGE);
170     SharedData<string>(SharedDataType::LAN, lanInfo.substr(0, lanInfo.find("_")));
171     SharedData<string>(SharedDataType::REGION, lanInfo.substr(lanInfo.find("_") + 1, lanInfo.length() - 1));
172     ILOG("Start language is : %s", SharedData<string>::GetData(SharedDataType::LANGUAGE).c_str());
173 }
174 
main(int argc,char * argv[])175 int main(int argc, char* argv[])
176 {
177     ILOG("RichPreviewer enter the main function.");
178     auto richCrashHandler = std::make_unique<CrashHandler>();
179     if (richCrashHandler == nullptr) {
180         ELOG("RichPreviewer crashHandler new fail.");
181         return 0;
182     }
183     // init exception handler
184     richCrashHandler->InitExceptionHandler();
185     // Parsing User Commands
186     CommandParser& parser = CommandParser::GetInstance();
187     vector<string> strs;
188     for (int i = 1; i < argc; ++i) {
189         if (parser.IsMainArgLengthInvalid(argv[i])) {
190             return START_PARAM_INVALID_CODE;
191         }
192         strs.push_back(argv[i]);
193     }
194 
195     if (!parser.ProcessCommand(strs)) {
196         return 0;
197     }
198     // check params
199     if (!parser.IsCommandValid()) {
200         return START_PARAM_INVALID_CODE;
201     }
202 
203     InitSharedData();
204     if (parser.IsSet("s")) {
205         CommandLineInterface::GetInstance().Init(parser.Value("s"));
206     }
207 
208     TraceTool::GetInstance().HandleTrace("Enter the main function");
209 
210     std::thread commandThead(ProcessCommand);
211     commandThead.detach();
212     InitJsApp();
213     this_thread::sleep_for(chrono::milliseconds(500)); // sleep 500 ms
214     return 0;
215 }
216