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