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