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 #include "json.h"
32
33 using namespace std;
34 static const int START_PARAM_INVALID_CODE = 11;
35 static const int NOTIFY_INTERVAL_TIME = 1000; // Unit millisecond
36
InitDeviceOrientation()37 static void InitDeviceOrientation()
38 {
39 CommandParser& parser = CommandParser::GetInstance();
40 if (parser.GetCompressionResolutionWidth() <= parser.GetCompressionResolutionHeight()) {
41 ILOG("InitDeviceOrientation is portrait.");
42 JsAppImpl::GetInstance().SetDeviceOrentation("portrait");
43 } else {
44 ILOG("InitDeviceOrientation is landscape.");
45 JsAppImpl::GetInstance().SetDeviceOrentation("landscape");
46 }
47 }
48
InitJsApp()49 static void InitJsApp()
50 {
51 CommandParser& parser = CommandParser::GetInstance();
52 // Initialize Image Pipeline Name
53 if (parser.IsSet("s")) {
54 JsAppImpl::GetInstance().SetPipeName(parser.Value("s"));
55 }
56
57 if (parser.IsSet("lws")) {
58 JsAppImpl::GetInstance().SetPipePort(parser.Value("lws"));
59 }
60
61 JsAppImpl::GetInstance().SetJsAppPath(parser.Value("j"));
62
63 if (parser.IsSet("url")) {
64 JsAppImpl::GetInstance().SetUrlPath(parser.Value("url"));
65 }
66
67 if (parser.IsSet("cm")) {
68 JsAppImpl::GetInstance().SetArgsColorMode(parser.Value("cm"));
69 }
70
71 if (parser.IsSet("av")) {
72 JsAppImpl::GetInstance().SetArgsAceVersion(parser.Value("av"));
73 }
74
75 if (parser.IsSet("sd")) {
76 JsAppImpl::GetInstance().SetScreenDensity(parser.Value("sd"));
77 }
78
79 if (parser.IsSet("cc")) {
80 JsAppImpl::GetInstance().SetConfigChanges(parser.Value("cc"));
81 }
82
83 InitDeviceOrientation();
84
85 JsAppImpl::GetInstance().Start();
86 }
87
NotifyInspectorChanged()88 static void NotifyInspectorChanged()
89 {
90 if (!VirtualScreenImpl::GetInstance().isFrameUpdated) {
91 return;
92 }
93 VirtualScreenImpl::GetInstance().isFrameUpdated = false;
94
95 static std::string jsonTreeLast = "";
96 std::string jsonTree = JsAppImpl::GetInstance().GetJSONTree();
97 if (jsonTree == jsonTreeLast) {
98 return;
99 }
100
101 jsonTreeLast = jsonTree;
102 Json::Value commandResult;
103 commandResult["version"] = CommandLineInterface::COMMAND_VERSION;
104 commandResult["command"] = "inspector";
105 commandResult["result"] = jsonTree;
106 CommandLineInterface::GetInstance().SendJsonData(commandResult);
107 ILOG("Send inspector json tree.");
108 }
109
ProcessCommand()110 static void ProcessCommand()
111 {
112 static CppTimer inspectorNotifytimer(NotifyInspectorChanged);
113 inspectorNotifytimer.Start(NOTIFY_INTERVAL_TIME); // Notify per second
114 CppTimerManager::GetTimerManager().AddCppTimer(inspectorNotifytimer);
115
116 VirtualScreenImpl::GetInstance().InitFrameCountTimer();
117 while (!Interrupter::IsInterrupt()) {
118 CommandLineInterface::GetInstance().ProcessCommand();
119 CppTimerManager::GetTimerManager().RunTimerTick();
120 this_thread::sleep_for(chrono::milliseconds(1));
121 }
122 JsAppImpl::GetInstance().Stop();
123 }
124
InitSharedData()125 static void InitSharedData()
126 {
127 CommandParser& parser = CommandParser::GetInstance();
128 if (parser.IsSet("l")) {
129 SharedData<string>(SharedDataType::LANGUAGE, parser.Value("l"));
130 } else {
131 SharedData<string>(SharedDataType::LANGUAGE, "zh_CN");
132 }
133 string lanInfo = SharedData<string>::GetData(SharedDataType::LANGUAGE);
134 SharedData<string>(SharedDataType::LAN, lanInfo.substr(0, lanInfo.find("_")));
135 SharedData<string>(SharedDataType::REGION, lanInfo.substr(lanInfo.find("_") + 1, lanInfo.length() - 1));
136 ILOG("Start language is : %s", SharedData<string>::GetData(SharedDataType::LANGUAGE).c_str());
137 }
138
main(int argc,char * argv[])139 int main(int argc, char* argv[])
140 {
141 ILOG("RichPreviewer enter the main function.");
142 auto richCrashHandler = std::make_unique<CrashHandler>();
143 if (richCrashHandler == nullptr) {
144 ELOG("RichPreviewer crashHandler new fail.");
145 return 0;
146 }
147 // init exception handler
148 richCrashHandler->InitExceptionHandler();
149 // Parsing User Commands
150 CommandParser& parser = CommandParser::GetInstance();
151 vector<string> strs;
152 for (int i = 1; i < argc; ++i) {
153 strs.push_back(argv[i]);
154 }
155
156 if (!parser.ProcessCommand(strs)) {
157 return 0;
158 }
159
160 if (!parser.IsCommandValid()) {
161 return START_PARAM_INVALID_CODE;
162 }
163
164 InitSharedData();
165 if (parser.IsSet("s")) {
166 CommandLineInterface::GetInstance().Init(parser.Value("s"));
167 }
168
169 TraceTool::GetInstance().HandleTrace("Enter the main function");
170
171 std::thread commandThead(ProcessCommand);
172 commandThead.detach();
173 InitJsApp();
174 this_thread::sleep_for(chrono::milliseconds(500)); // 500ms wait threads finish
175 return 0;
176 }
177