• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <fstream>
17 #include <iostream>
18 #include <sstream>
19 #include <thread>
20 
21 #include "adapter/preview/entrance/ace_ability.h"
22 #include "adapter/preview/entrance/ace_run_args.h"
23 #include "adapter/preview/entrance/samples/key_input_handler.h"
24 #include "adapter/preview/entrance/samples/touch_event_handler.h"
25 
26 namespace {
27 
28 constexpr int32_t GET_INSPECTOR_TREE_TIMES = 12;
29 constexpr int32_t GET_INSPECTOR_TREE_INTERVAL = 5000;
30 constexpr char FILE_NAME[] = "InspectorTree.txt";
31 constexpr char MAX_ARGS_COUNT = 2;
32 constexpr char MODEL_STAGE[] = "stage";
33 // card default width and height:
34 // 2*1 (150*54)
35 // 2*2 (150*150)
36 // 4*2 (320*150)
37 // 4*4 (320*344)
38 constexpr int32_t CARD_DEFAULT_WIDTH = 320;
39 constexpr int32_t CARD_DEFAULT_HEIGHT = 344;
40 
41 } // namespace
42 
main(int argc,const char * argv[])43 int main(int argc, const char* argv[])
44 {
45     auto&& renderCallback = [](const void*, const size_t bufferSize, const int32_t width,
46                                 const int32_t height) -> bool { return true; };
47 
48 #ifdef MAC_PLATFORM
49     std::string assetPathJs = "/Volumes/SSD2T/daily-test/preview/js/default_card";
50     std::string assetPathEtsStage = "/Volumes/SSD2T/daily-test/preview/js/default_card_stage/js";
51     std::string appResourcesPath = "/Volumes/SSD2T/daily-test/preview/js/AppResources";
52     std::string appResourcesPathStage = "/Volumes/SSD2T/daily-test/preview/js/default_card_stage";
53     std::string systemResourcesPath = "/Volumes/SSD2T/daily-test/preview/js/SystemResources";
54     constexpr double density = 2;
55 #elif WINDOWS_PLATFORM
56     std::string assetPathJs = "D:\\Workspace\\preview\\js\\default_card";
57     std::string assetPathEtsStage = "D:\\Workspace\\preview\\js\\default_card_stage\\js";
58     std::string appResourcesPath = "D:\\Workspace\\preview\\js\\AppResources\\assets\\entry";
59     std::string appResourcesPathStage = "D:\\Workspace\\preview\\js\\default_card_stage";
60     std::string systemResourcesPath = "D:\\Workspace\\preview\\js\\SystemResources\\assets\\entry";
61     constexpr double density = 1;
62 #else
63     std::string assetPathJs = "/home/ubuntu/demo/preview/js/default_card";
64     std::string assetPathEtsStage = "/home/ubuntu/demo/preview/js/default_card_stage/js";
65     std::string appResourcesPath = "/home/ubuntu/demo/preview/js/AppResources";
66     std::string appResourcesPathStage = "/home/ubuntu/demo/preview/js/default_card_stage";
67     std::string systemResourcesPath = "/home/ubuntu/demo/preview/js/SystemResources";
68     constexpr double density = 2;
69 #endif
70     std::string pageProfile = "form_config";
71     std::string url = "widget/pages/index/index";
72 
73 
74     OHOS::Ace::Platform::AceRunArgs args = {
75         .assetPath = assetPathJs,
76         .systemResourcesPath = systemResourcesPath,
77         .appResourcesPath = appResourcesPath,
78         .deviceConfig.density = density,
79         .deviceConfig.colorMode = OHOS::Ace::ColorMode::DARK,
80         .windowTitle = "ACE card",
81         .deviceWidth = CARD_DEFAULT_WIDTH,
82         .deviceHeight = CARD_DEFAULT_HEIGHT,
83         .formsEnabled = true,
84         .onRender = std::move(renderCallback),
85     };
86     if (argc == MAX_ARGS_COUNT && !std::strcmp(argv[1], MODEL_STAGE)) {
87         args.assetPath = assetPathEtsStage;
88         args.appResourcesPath = appResourcesPathStage;
89         args.projectModel = OHOS::Ace::Platform::ProjectModel::STAGE;
90         args.pageProfile = pageProfile;
91         args.url = url;
92     }
93 
94     auto ability = OHOS::Ace::Platform::AceAbility::CreateInstance(args);
95     if (!ability) {
96         std::cerr << "Could not create AceAbility!" << std::endl;
97         return -1;
98     }
99     OHOS::Ace::Platform::KeyInputHandler::InitialTextInputCallback(ability->GetGlfwWindowController());
100     OHOS::Ace::Platform::TouchEventHandler::InitialTouchEventCallback(ability->GetGlfwWindowController());
101 
102     std::thread timer([&ability]() {
103         int32_t getJSONTreeTimes = GET_INSPECTOR_TREE_TIMES;
104         while (getJSONTreeTimes--) {
105             std::this_thread::sleep_for(std::chrono::milliseconds(GET_INSPECTOR_TREE_INTERVAL));
106             std::string jsonTreeStr = ability->GetJSONTree();
107             // clear all information
108             std::ofstream fileCleaner(FILE_NAME, std::ios_base::out);
109             std::ofstream fileWriter(FILE_NAME, std::ofstream::app);
110             fileWriter << jsonTreeStr;
111             fileWriter << std::endl;
112             fileWriter.close();
113         }
114     });
115     ability->InitEnv();
116     std::cout << "Ace initialize done. run loop now" << std::endl;
117     ability->Start();
118 
119     return 0;
120 }
121