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