xref: /ide/tools/previewer/ThinPreviewer.cpp (revision 7c804472)
17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ci#include <VirtualScreen.h>
177c804472Sopenharmony_ci#include <cstdint>
187c804472Sopenharmony_ci#include <cstdlib>
197c804472Sopenharmony_ci#include <fstream>
207c804472Sopenharmony_ci#include <vector>
217c804472Sopenharmony_ci#include <new>
227c804472Sopenharmony_ci#include "CommandLineFactory.h"
237c804472Sopenharmony_ci#include "CommandLineInterface.h"
247c804472Sopenharmony_ci#include "CommandParser.h"
257c804472Sopenharmony_ci#include "CppTimerManager.h"
267c804472Sopenharmony_ci#include "CrashHandler.h"
277c804472Sopenharmony_ci#include "Interrupter.h"
287c804472Sopenharmony_ci#include "JsAppImpl.h"
297c804472Sopenharmony_ci#include "ModelManager.h"
307c804472Sopenharmony_ci#include "PreviewerEngineLog.h"
317c804472Sopenharmony_ci#include "SharedData.h"
327c804472Sopenharmony_ci#include "TimerTaskHandler.h"
337c804472Sopenharmony_ci#include "TraceTool.h"
347c804472Sopenharmony_ci#include "VirtualScreenImpl.h"
357c804472Sopenharmony_ci#include "jsi.h"
367c804472Sopenharmony_ci
377c804472Sopenharmony_cistatic const int NOTIFY_INTERVAL_TIME = 1000; // Unit millisecond
387c804472Sopenharmony_ci
397c804472Sopenharmony_cistatic void InitSettings()
407c804472Sopenharmony_ci{
417c804472Sopenharmony_ci    // Setting the Simulator Model
427c804472Sopenharmony_ci    ModelManager::SetCurrentDevice(CommandParser::GetInstance().GetDeviceType());
437c804472Sopenharmony_ci    VirtualScreenImpl::GetInstance().InitResolution();
447c804472Sopenharmony_ci    VirtualScreenImpl::GetInstance().InitFrameCountTimer();
457c804472Sopenharmony_ci}
467c804472Sopenharmony_ci
477c804472Sopenharmony_cistatic void ApplyConfig()
487c804472Sopenharmony_ci{
497c804472Sopenharmony_ci    std::string liteConfigArgs = CommandParser::GetInstance().GetConfigPath();
507c804472Sopenharmony_ci    if (liteConfigArgs.empty()) {
517c804472Sopenharmony_ci        ELOG("No persistent properties path found.");
527c804472Sopenharmony_ci    }
537c804472Sopenharmony_ci    CommandLineInterface::GetInstance().ReadAndApplyConfig(liteConfigArgs);
547c804472Sopenharmony_ci}
557c804472Sopenharmony_ci
567c804472Sopenharmony_cistatic void DataChangeCheck()
577c804472Sopenharmony_ci{
587c804472Sopenharmony_ci    SharedDataManager::CheckTick();
597c804472Sopenharmony_ci}
607c804472Sopenharmony_ci
617c804472Sopenharmony_cistatic void InitSharedData()
627c804472Sopenharmony_ci{
637c804472Sopenharmony_ci    // The brightness ranges from 1 to 255. The default value is 255.
647c804472Sopenharmony_ci    SharedData<uint8_t>(SharedDataType::BRIGHTNESS_VALUE, 255, 1, 255);
657c804472Sopenharmony_ci    SharedData<uint8_t>(SharedDataType::BRIGHTNESS_MODE, (uint8_t)BrightnessMode::MANUAL,
667c804472Sopenharmony_ci                        (uint8_t)BrightnessMode::MANUAL, (uint8_t)BrightnessMode::AUTO);
677c804472Sopenharmony_ci    SharedData<bool>(SharedDataType::KEEP_SCREEN_ON, true);
687c804472Sopenharmony_ci    SharedData<uint8_t>(SharedDataType::BATTERY_STATUS, (uint8_t)ChargeState::NOCHARGE,
697c804472Sopenharmony_ci                        (uint8_t)ChargeState::NOCHARGE, (uint8_t)ChargeState::CHARGING);
707c804472Sopenharmony_ci    // Battery level range: 0.0–1.0; default: 1.0
717c804472Sopenharmony_ci    SharedData<double>(SharedDataType::BATTERY_LEVEL, 1.0, 0.0, 1.0);
727c804472Sopenharmony_ci    // Heart rate range: 0 to 255. The default value is 80.
737c804472Sopenharmony_ci    SharedData<uint8_t>(SharedDataType::HEARTBEAT_VALUE, 80, 0, 255);
747c804472Sopenharmony_ci    // The value ranges from 0 to 999999. The default value is 0.
757c804472Sopenharmony_ci    SharedData<uint32_t>(SharedDataType::SUMSTEP_VALUE, 0, 0, 999999);
767c804472Sopenharmony_ci    // The volume ranges from 0.0 to 1.0. The default value is 1.0.
777c804472Sopenharmony_ci    SharedData<double>(SharedDataType::VOLUME_VALUE, 1.0, 0.0, 1.0);
787c804472Sopenharmony_ci    // The atmospheric pressure ranges from 0 to 999900. The default value is 101325.
797c804472Sopenharmony_ci    SharedData<uint32_t>(SharedDataType::PRESSURE_VALUE, 101325, 0, 999900);
807c804472Sopenharmony_ci    SharedData<bool>(SharedDataType::WEARING_STATE, true);
817c804472Sopenharmony_ci    SharedData<std::string>(SharedDataType::LANGUAGE, "zh-CN");
827c804472Sopenharmony_ci    // The value ranges from 180 to 180. The default value is 0.
837c804472Sopenharmony_ci    SharedData<double>(SharedDataType::LONGITUDE, 0, -180, 180);
847c804472Sopenharmony_ci    // The value ranges from -90 to 90. The default value is 0.
857c804472Sopenharmony_ci    SharedData<double>(SharedDataType::LATITUDE, 0, -90, 90);
867c804472Sopenharmony_ci}
877c804472Sopenharmony_ci
887c804472Sopenharmony_cistatic void NewHandler()
897c804472Sopenharmony_ci{
907c804472Sopenharmony_ci    ELOG("Custom new handler: memory allocation failed.");
917c804472Sopenharmony_ci}
927c804472Sopenharmony_ci
937c804472Sopenharmony_cistatic void SendJsHeapData()
947c804472Sopenharmony_ci{
957c804472Sopenharmony_ci    OHOS::ACELite::JSHeapStatus status;
967c804472Sopenharmony_ci    OHOS::ACELite::JSI::GetJSHeapStatus(status);
977c804472Sopenharmony_ci    CommandLineInterface::GetInstance().SendJSHeapMemory(status.totalBytes, status.allocBytes, status.peakAllocBytes);
987c804472Sopenharmony_ci}
997c804472Sopenharmony_ci
1007c804472Sopenharmony_ciint main(int argc, char* argv[])
1017c804472Sopenharmony_ci{
1027c804472Sopenharmony_ci    ILOG("ThinPreviewer enter the main function.");
1037c804472Sopenharmony_ci    std::set_new_handler(NewHandler);
1047c804472Sopenharmony_ci    // thin device global exception handler
1057c804472Sopenharmony_ci    auto thinCrashHandler = std::make_unique<CrashHandler>();
1067c804472Sopenharmony_ci    thinCrashHandler->InitExceptionHandler();
1077c804472Sopenharmony_ci    // Creating a Main Thread Timer Manager
1087c804472Sopenharmony_ci    CppTimerManager& manager = CppTimerManager::GetTimerManager();
1097c804472Sopenharmony_ci    // Parsing User Commands
1107c804472Sopenharmony_ci    CommandParser& parser = CommandParser::GetInstance();
1117c804472Sopenharmony_ci    int ret = parser.ParseArgs(argc, argv);
1127c804472Sopenharmony_ci    if (ret >= 0) {
1137c804472Sopenharmony_ci        return ret;
1147c804472Sopenharmony_ci    }
1157c804472Sopenharmony_ci    InitSharedData();
1167c804472Sopenharmony_ci    InitSettings();
1177c804472Sopenharmony_ci    if (parser.IsSet("s")) {
1187c804472Sopenharmony_ci        CommandLineInterface::GetInstance().Init(parser.Value("s"));
1197c804472Sopenharmony_ci    }
1207c804472Sopenharmony_ci    ApplyConfig();
1217c804472Sopenharmony_ci    JsAppImpl::GetInstance().InitJsApp();
1227c804472Sopenharmony_ci    TraceTool::GetInstance().HandleTrace("Enter the main function");
1237c804472Sopenharmony_ci    CppTimer dataCheckTimer(DataChangeCheck);
1247c804472Sopenharmony_ci    manager.AddCppTimer(dataCheckTimer);
1257c804472Sopenharmony_ci    dataCheckTimer.Start(100); // 100ms Timer polling period
1267c804472Sopenharmony_ci    CppTimer jsHeapSendTimer(SendJsHeapData);
1277c804472Sopenharmony_ci    if (parser.IsSendJSHeap()) {
1287c804472Sopenharmony_ci        manager.AddCppTimer(jsHeapSendTimer);
1297c804472Sopenharmony_ci        jsHeapSendTimer.Start(NOTIFY_INTERVAL_TIME); // 1000ms Timer polling period
1307c804472Sopenharmony_ci    }
1317c804472Sopenharmony_ci    // Registering and monitoring the changes of the brightness and volume
1327c804472Sopenharmony_ci    std::thread::id curThreadId = std::this_thread::get_id();
1337c804472Sopenharmony_ci    SharedData<uint8_t>::AppendNotify(SharedDataType::BRIGHTNESS_VALUE,
1347c804472Sopenharmony_ci                                      TimerTaskHandler::CheckBrightnessValueChanged, curThreadId);
1357c804472Sopenharmony_ci    while (!Interrupter::IsInterrupt()) {
1367c804472Sopenharmony_ci        CommandLineInterface::GetInstance().ProcessCommand();
1377c804472Sopenharmony_ci        manager.RunTimerTick();
1387c804472Sopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(1));
1397c804472Sopenharmony_ci    }
1407c804472Sopenharmony_ci    JsAppImpl::GetInstance().Stop();
1417c804472Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(500)); // sleep 500 ms
1427c804472Sopenharmony_ci    return 0;
1437c804472Sopenharmony_ci}
144