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 NOTIFY_INTERVAL_TIME = 1000; // Unit millisecond
34
ApplyConfig()35 static void ApplyConfig()
36 {
37 string richConfigArgs = CommandParser::GetInstance().GetConfigPath();
38 if (richConfigArgs.empty()) {
39 ELOG("No persistent properties path found.");
40 }
41 CommandLineInterface::GetInstance().ReadAndApplyConfig(richConfigArgs);
42 }
43
NotifyInspectorChanged()44 static void NotifyInspectorChanged()
45 {
46 if (!VirtualScreenImpl::GetInstance().isFrameUpdated) {
47 return;
48 }
49 VirtualScreenImpl::GetInstance().isFrameUpdated = false;
50
51 static std::string jsonTreeLast = "";
52 std::string jsonTree = JsAppImpl::GetInstance().GetJSONTree();
53 if (jsonTree == jsonTreeLast) {
54 return;
55 }
56
57 jsonTreeLast = jsonTree;
58 Json2::Value commandResult = JsonReader::CreateObject();
59 commandResult.Add("version", CommandLineInterface::COMMAND_VERSION.c_str());
60 commandResult.Add("command", "inspector");
61 commandResult.Add("result", jsonTree.c_str());
62 CommandLineInterface::GetInstance().SendJsonData(commandResult);
63 ILOG("Send inspector json tree.");
64 }
65
ProcessCommand()66 static void ProcessCommand()
67 {
68 if (!CommandParser::GetInstance().IsSet("d")) {
69 static CppTimer inspectorNotifytimer(NotifyInspectorChanged);
70 inspectorNotifytimer.Start(NOTIFY_INTERVAL_TIME); // Notify per second
71 CppTimerManager::GetTimerManager().AddCppTimer(inspectorNotifytimer);
72 }
73
74 VirtualScreenImpl::GetInstance().InitFrameCountTimer();
75 while (!Interrupter::IsInterrupt()) {
76 CommandLineInterface::GetInstance().ProcessCommand();
77 CppTimerManager::GetTimerManager().RunTimerTick();
78 this_thread::sleep_for(chrono::milliseconds(1));
79 }
80 JsAppImpl::GetInstance().Stop();
81 }
82
InitSharedData()83 static void InitSharedData()
84 {
85 CommandParser& parser = CommandParser::GetInstance();
86 if (parser.IsSet("l")) {
87 SharedData<string>(SharedDataType::LANGUAGE, parser.Value("l"));
88 } else {
89 SharedData<string>(SharedDataType::LANGUAGE, "zh_CN");
90 }
91 string lanInfo = SharedData<string>::GetData(SharedDataType::LANGUAGE);
92 SharedData<string>(SharedDataType::LAN, lanInfo.substr(0, lanInfo.find("_")));
93 SharedData<string>(SharedDataType::REGION, lanInfo.substr(lanInfo.find("_") + 1, lanInfo.length() - 1));
94 ILOG("Start language is : %s", SharedData<string>::GetData(SharedDataType::LANGUAGE).c_str());
95 }
96
main(int argc,char * argv[])97 int main(int argc, char* argv[])
98 {
99 ILOG("RichPreviewer enter the main function.");
100 auto richCrashHandler = std::make_unique<CrashHandler>();
101 if (richCrashHandler == nullptr) {
102 ELOG("RichPreviewer crashHandler new fail.");
103 return 0;
104 }
105 // init exception handler
106 richCrashHandler->InitExceptionHandler();
107 // Parsing User Commands
108 CommandParser& parser = CommandParser::GetInstance();
109 int ret = parser.ParseArgs(argc, argv);
110 if (ret >= 0) {
111 return ret;
112 }
113 InitSharedData();
114 if (parser.IsSet("s")) {
115 CommandLineInterface::GetInstance().Init(parser.Value("s"));
116 }
117
118 TraceTool::GetInstance().HandleTrace("Enter the main function");
119
120 std::thread commandThead(ProcessCommand);
121 commandThead.detach();
122 VirtualScreenImpl::GetInstance().InitResolution();
123 ApplyConfig();
124 JsAppImpl::GetInstance().InitJsApp();
125 this_thread::sleep_for(chrono::milliseconds(500)); // sleep 500 ms
126 return 0;
127 }
128