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 <cstdint>
17 #include <fstream>
18 #include <iostream>
19 #include <sstream>
20 #include <thread>
21
22 #include "jsapp/rich/external/EventHandler.h"
23 #include "previewer/include/window.h"
24
25 #include "adapter/preview/entrance/ace_ability.h"
26 #include "adapter/preview/entrance/ace_run_args.h"
27 #include "adapter/preview/entrance/samples/event_adapter.h"
28 #include "base/log/log.h"
29 #include "base/utils/device_config.h"
30 #include "base/utils/utils.h"
31 #include "adapter/preview/entrance/ace_preview_helper.h"
32
33 namespace {
34 constexpr char ACE_VERSION_2[] = "2.0";
35 constexpr char MODEL_STAGE[] = "stage";
36 constexpr int MAX_ARGS_COUNT = 2;
37 const std::string pageProfile = "main_page";
38 #ifdef MAC_PLATFORM
39 const std::string assetPathJs = "/Volumes/SSD2T/daily-test/preview/js/default";
40 const std::string assetPathEts = "/Volumes/SSD2T/daily-test/preview/js/default_2.0";
41 const std::string assetPathEtsStage = "/Volumes/SSD2T/daily-test/preview/js/default_stage/ets";
42 const std::string appResourcesPath = "/Volumes/SSD2T/daily-test/preview/js/AppResources";
43 const std::string appResourcesPathStage = "/Volumes/SSD2T/daily-test/preview/js/default_stage";
44 const std::string systemResourcesPath = "/Volumes/SSD2T/daily-test/preview/js/SystemResources";
45 constexpr double density = 2;
46 #elif WINDOWS_PLATFORM
47 const std::string assetPathJs = "D:\\Workspace\\preview\\js\\default";
48 const std::string assetPathEts = "D:\\Workspace\\preview\\js\\default_2.0";
49 const std::string assetPathEtsStage = "D:\\Workspace\\preview\\js\\default_stage\\ets";
50 const std::string appResourcesPath = "D:\\Workspace\\preview\\js\\AppResources\\assets\\entry";
51 const std::string appResourcesPathStage = "D:\\Workspace\\preview\\js\\default_stage";
52 const std::string systemResourcesPath = "D:\\Workspace\\preview\\js\\SystemResources\\assets\\entry";
53 constexpr double density = 1;
54 #else
55 const std::string assetPathJs = "/home/ubuntu/demo/preview/js/default";
56 const std::string assetPathEts = "/home/ubuntu/demo/preview/js/default_2.0";
57 const std::string assetPathEtsStage = "/home/ubuntu/demo/preview/js/default_stage/ets";
58 const std::string appResourcesPath = "/home/ubuntu/demo/preview/js/AppResources";
59 const std::string appResourcesPathStage = "/home/ubuntu/demo/preview/js/default_stage";
60 const std::string systemResourcesPath = "/home/ubuntu/demo/preview/js/SystemResources";
61 constexpr double density = 2;
62 #endif
63
64 auto&& renderCallback = [](
__anon755c07750202( const void*, const size_t bufferSize, const int32_t width, const int32_t height, const uint64_t timestamp) 65 const void*, const size_t bufferSize, const int32_t width, const int32_t height, const uint64_t timestamp) -> bool {
66 LOGI("OnRender: bufferSize = %{public}zu, [width, height] = [%{public}d, %{public}d],
67 timestamp = %{public}llu", bufferSize, width, height, timestamp);
68 return true;
69 };
70
__anon755c07750302(const std::string currentPagePath) 71 auto&& pageCallback = [](const std::string currentPagePath) -> bool {
72 LOGI("OnRouterChange: current page: %s", currentPagePath.c_str());
73 return true;
74 };
75
76 } // namespace
77
main(int argc,const char * argv[])78 int main(int argc, const char* argv[])
79 {
80 // Initialize the arguments of the ability
81 OHOS::Ace::Platform::AceRunArgs args = {
82 .assetPath = assetPathJs,
83 .systemResourcesPath = systemResourcesPath,
84 .appResourcesPath = appResourcesPath,
85 .deviceConfig.density = density,
86 .windowTitle = "ACE phone",
87 .deviceWidth = 360,
88 .deviceHeight = 750,
89 .onRender = std::move(renderCallback),
90 .onRouterChange = std::move(pageCallback),
91 };
92
93 if (argc == MAX_ARGS_COUNT) {
94 if (!std::strcmp(argv[1], ACE_VERSION_2)) {
95 args.assetPath = assetPathEts;
96 args.aceVersion = OHOS::Ace::Platform::AceVersion::ACE_2_0;
97 } else if (!std::strcmp(argv[1], MODEL_STAGE)) {
98 args.assetPath = assetPathEtsStage;
99 args.appResourcesPath = appResourcesPathStage;
100 args.aceVersion = OHOS::Ace::Platform::AceVersion::ACE_2_0;
101 args.projectModel = OHOS::Ace::Platform::ProjectModel::STAGE;
102 args.pageProfile = pageProfile;
103 }
104 }
105
106 // Initialize and create the glfw window.
107 auto ctx = OHOS::Rosen::GlfwRenderContext::GetGlobal();
108 if (!ctx->Init()) {
109 LOGI("Failed to initialize the glfw.");
110 return -1;
111 }
112 ctx->CreateGlfwWindow(args.deviceWidth, args.deviceHeight, true);
113 OHOS::Ace::Sample::EventAdapter::GetInstance().Initialize(ctx);
114 OHOS::Ace::Platform::AcePreviewHelper::GetInstance()->
115 SetCallbackOfPostTask(OHOS::AppExecFwk::EventHandler::PostTask);
116 OHOS::Ace::Platform::AcePreviewHelper::GetInstance()->
117 SetCallbackOfIsCurrentRunnerThread(OHOS::AppExecFwk::EventHandler::IsCurrentRunnerThread);
118
119 // Create the ace ability
120 auto ability = OHOS::Ace::Platform::AceAbility::CreateInstance(args);
121 CHECK_NULL_RETURN(ability, -1);
122
123 auto&& keyEventCallback = [&ability](
124 const std::shared_ptr<KeyEvent>& keyEvent) { ability->OnInputEvent(keyEvent); };
125 OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterKeyEventCallback(keyEventCallback);
126
127 auto&& pointerEventCallback = [&ability](const std::shared_ptr<PointerEvent>& pointerEvent) {
128 ability->OnInputEvent(pointerEvent);
129 };
130 OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterPointerEventCallback(pointerEventCallback);
131
132 auto&& inspectorCallback = [&ability]() {
133 constexpr char FILE_NAME[] = "InspectorTree.json";
134 std::string jsonTreeStr = ability->GetJSONTree();
135 std::ofstream fileCleaner(FILE_NAME, std::ios_base::out);
136 std::ofstream fileWriter(FILE_NAME, std::ofstream::app);
137 fileWriter << jsonTreeStr;
138 fileWriter << std::endl;
139 fileWriter.close();
140 };
141 OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterInspectorCallback(inspectorCallback);
142
143 OHOS::Rosen::WMError errCode;
144 OHOS::sptr<OHOS::Rosen::WindowOption> sp = nullptr;
145 auto window = OHOS::Rosen::Window::Create("previewer", sp, nullptr, errCode);
146 window->CreateSurfaceNode("preview_surface", args.onRender);
147 ability->SetWindow(window);
148
149 ability->InitEnv();
150 LOGI("Ace initialize done. run event loop now");
151 while (!ctx->WindowShouldClose()) {
152 OHOS::AppExecFwk::EventHandler::Run();
153 ctx->PollEvents();
154 std::this_thread::sleep_for(std::chrono::milliseconds(1));
155 }
156
157 LOGI("Successfully exit the application.");
158 return 0;
159 }
160