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 32static const int NOTIFY_INTERVAL_TIME = 1000; // Unit millisecond 33 34static 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 43static 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 65static void ProcessCommand() 66{ 67 if (!CommandParser::GetInstance().IsSet("d")) { 68 static CppTimer inspectorNotifytimer(NotifyInspectorChanged); 69 inspectorNotifytimer.Start(NOTIFY_INTERVAL_TIME); // Notify per second 70 CppTimerManager::GetTimerManager().AddCppTimer(inspectorNotifytimer); 71 } 72 73 VirtualScreenImpl::GetInstance().InitFrameCountTimer(); 74 while (!Interrupter::IsInterrupt()) { 75 CommandLineInterface::GetInstance().ProcessCommand(); 76 CppTimerManager::GetTimerManager().RunTimerTick(); 77 std::this_thread::sleep_for(std::chrono::milliseconds(1)); 78 } 79 JsAppImpl::GetInstance().Stop(); 80} 81 82static void InitSharedData() 83{ 84 CommandParser& parser = CommandParser::GetInstance(); 85 if (parser.IsSet("l")) { 86 SharedData<std::string>(SharedDataType::LANGUAGE, parser.Value("l")); 87 } else { 88 SharedData<std::string>(SharedDataType::LANGUAGE, "zh_CN"); 89 } 90 std::string lanInfo = SharedData<std::string>::GetData(SharedDataType::LANGUAGE); 91 SharedData<std::string>(SharedDataType::LAN, lanInfo.substr(0, lanInfo.find("_"))); 92 SharedData<std::string>(SharedDataType::REGION, lanInfo.substr(lanInfo.find("_") + 1, lanInfo.length() - 1)); 93 ILOG("Start language is : %s", SharedData<std::string>::GetData(SharedDataType::LANGUAGE).c_str()); 94} 95 96static void NewHandler() 97{ 98 ELOG("Custom new handler: memory allocation failed."); 99} 100 101int main(int argc, char* argv[]) 102{ 103 ILOG("RichPreviewer enter the main function."); 104 std::set_new_handler(NewHandler); // 设置全局new处理函数 105 auto richCrashHandler = std::make_unique<CrashHandler>(); 106 // init exception handler 107 richCrashHandler->InitExceptionHandler(); 108 // Parsing User Commands 109 CommandParser& parser = CommandParser::GetInstance(); 110 int ret = parser.ParseArgs(argc, argv); 111 if (ret >= 0) { 112 return ret; 113 } 114 InitSharedData(); 115 if (parser.IsSet("s")) { 116 CommandLineInterface::GetInstance().Init(parser.Value("s")); 117 } 118 119 TraceTool::GetInstance().HandleTrace("Enter the main function"); 120 121 std::thread commandThead(ProcessCommand); 122 commandThead.detach(); 123 VirtualScreenImpl::GetInstance().InitResolution(); 124 ApplyConfig(); 125 JsAppImpl::GetInstance().InitJsApp(); 126 std::this_thread::sleep_for(std::chrono::milliseconds(500)); // sleep 500 ms 127 return 0; 128} 129