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 ACE_VERSION_2[] = "2.0";
32 constexpr char MODEL_STAGE[] = "stage";
33 constexpr char MAX_ARGS_COUNT = 2;
34
__anon8b22d5bd0202(const void*, const size_t bufferSize, const int32_t width, const int32_t height) 35 auto&& renderCallback = [](const void*, const size_t bufferSize, const int32_t width, const int32_t height) -> bool {
36 return true;
37 };
38
__anon8b22d5bd0302(const std::string currentPagePath) 39 auto&& pageCallback = [](const std::string currentPagePath) -> bool {
40 printf("callback currentPage:%s", currentPagePath.c_str());
41 return true;
42 };
43
44 } // namespace
45
main(int argc,const char * argv[])46 int main(int argc, const char* argv[])
47 {
48 #ifdef MAC_PLATFORM
49 std::string assetPathJs = "/Volumes/SSD2T/daily-test/preview/js/default";
50 std::string assetPathEts = "/Volumes/SSD2T/daily-test/preview/js/default_2.0";
51 std::string assetPathEtsStage = "/Volumes/SSD2T/daily-test/preview/js/default_stage/ets";
52 std::string appResourcesPath = "/Volumes/SSD2T/daily-test/preview/js/AppResources";
53 std::string appResourcesPathStage = "/Volumes/SSD2T/daily-test/preview/js/default_stage";
54 std::string systemResourcesPath = "/Volumes/SSD2T/daily-test/preview/js/SystemResources";
55 constexpr double density = 2;
56 #elif WINDOWS_PLATFORM
57 std::string assetPathJs = "D:\\Workspace\\preview\\js\\default";
58 std::string assetPathEts = "D:\\Workspace\\preview\\js\\default_2.0";
59 std::string assetPathEtsStage = "D:\\Workspace\\preview\\js\\default_stage\\ets";
60 std::string appResourcesPath = "D:\\Workspace\\preview\\js\\AppResources\\assets\\entry";
61 std::string appResourcesPathStage = "D:\\Workspace\\preview\\js\\default_stage";
62 std::string systemResourcesPath = "D:\\Workspace\\preview\\js\\SystemResources\\assets\\entry";
63 constexpr double density = 1;
64 #else
65 std::string assetPathJs = "/home/ubuntu/demo/preview/js/default";
66 std::string assetPathEts = "/home/ubuntu/demo/preview/js/default_2.0";
67 std::string assetPathEtsStage = "/home/ubuntu/demo/preview/js/default_stage/ets";
68 std::string appResourcesPath = "/home/ubuntu/demo/preview/js/AppResources";
69 std::string appResourcesPathStage = "/home/ubuntu/demo/preview/js/default_stage";
70 std::string systemResourcesPath = "/home/ubuntu/demo/preview/js/SystemResources";
71 constexpr double density = 2;
72 #endif
73 std::string pageProfile = "main_page";
74 OHOS::Ace::Platform::AceRunArgs args = {
75 .assetPath = assetPathJs,
76 .systemResourcesPath = systemResourcesPath,
77 .appResourcesPath = appResourcesPath,
78 .deviceConfig.density = density,
79 .windowTitle = "ACE phone",
80 .deviceWidth = 360,
81 .deviceHeight = 750,
82 .onRender = std::move(renderCallback),
83 .onRouterChange = std::move(pageCallback),
84 };
85
86 if (argc == MAX_ARGS_COUNT) {
87 if (!std::strcmp(argv[1], ACE_VERSION_2)) {
88 args.assetPath = assetPathEts;
89 args.aceVersion = OHOS::Ace::Platform::AceVersion::ACE_2_0;
90 } else if (!std::strcmp(argv[1], MODEL_STAGE)) {
91 args.assetPath = assetPathEtsStage;
92 args.appResourcesPath = appResourcesPathStage;
93 args.aceVersion = OHOS::Ace::Platform::AceVersion::ACE_2_0;
94 args.projectModel = OHOS::Ace::Platform::ProjectModel::STAGE;
95 args.pageProfile = pageProfile;
96 }
97 }
98
99 auto ability = OHOS::Ace::Platform::AceAbility::CreateInstance(args);
100 if (!ability) {
101 std::cerr << "Could not create AceAbility!" << std::endl;
102 return -1;
103 }
104
105 OHOS::Ace::Platform::KeyInputHandler::InitialTextInputCallback(ability->GetGlfwWindowController());
106 OHOS::Ace::Platform::TouchEventHandler::InitialTouchEventCallback(ability->GetGlfwWindowController());
107
108 std::thread timer([&ability]() {
109 int32_t getJSONTreeTimes = GET_INSPECTOR_TREE_TIMES;
110 while (getJSONTreeTimes--) {
111 std::string jsonTreeStr = ability->GetJSONTree();
112 // clear all information
113 std::ofstream fileCleaner(FILE_NAME, std::ios_base::out);
114 std::ofstream fileWriter(FILE_NAME, std::ofstream::app);
115 fileWriter << jsonTreeStr;
116 fileWriter << std::endl;
117 fileWriter.close();
118 std::this_thread::sleep_for(std::chrono::milliseconds(GET_INSPECTOR_TREE_INTERVAL));
119 }
120 });
121 ability->InitEnv();
122 std::cout << "Ace initialize done. run loop now" << std::endl;
123 ability->Start();
124
125 return 0;
126 }
127