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
32 namespace {
33
34 constexpr char ACE_VERSION_2[] = "2.0";
35 constexpr char MAX_ARGS_COUNT = 2;
36 #ifdef MAC_PLATFORM
37 const std::string assetPathJs = "/Volumes/SSD2T/daily-test/preview/js/default";
38 const std::string assetPathEts = "/Volumes/SSD2T/daily-test/preview/js/default_2.0";
39 #elif WINDOWS_PLATFORM
40 const std::string assetPathJs = "D:\\Workspace\\preview\\js\\default";
41 const std::string assetPathEts = "D:\\Workspace\\preview\\js\\default_2.0";
42 #else
43 const std::string assetPathJs = "/home/ubuntu/demo/preview/js/default";
44 const std::string assetPathEts = "/home/ubuntu/demo/preview/js/default_2.0";
45 #endif
46
__anon4d6174250202(const void*, const size_t bufferSize, const int32_t width, const int32_t height) 47 auto&& renderCallback = [](const void*, const size_t bufferSize, const int32_t width, const int32_t height) -> bool {
48 LOGI("OnRender: bufferSize = %{public}zu, [width, height] = [%{public}d, %{public}d]", bufferSize, width, height);
49 return true;
50 };
51
__anon4d6174250302(const std::string currentPagePath) 52 auto&& pageCallback = [](const std::string currentPagePath) -> bool {
53 LOGI("OnRouterChange: current page: %s", currentPagePath.c_str());
54 return true;
55 };
56
57 } // namespace
58
main(int argc,const char * argv[])59 int main(int argc, const char* argv[])
60 {
61 OHOS::Ace::Platform::AceRunArgs args = {
62 .assetPath = assetPathJs,
63 .deviceConfig.density = 2.0,
64 .deviceConfig.deviceType = OHOS::Ace::DeviceType::WATCH,
65 .windowTitle = "ACE wearable",
66 .isRound = true,
67 .deviceWidth = 466,
68 .deviceHeight = 466,
69 .onRender = std::move(renderCallback),
70 .onRouterChange = std::move(pageCallback),
71 };
72
73 if (argc == MAX_ARGS_COUNT && !std::strcmp(argv[1], ACE_VERSION_2)) {
74 args.assetPath = assetPathEts;
75 args.aceVersion = OHOS::Ace::Platform::AceVersion::ACE_2_0;
76 }
77
78 // Initialize and create the glfw window.
79 auto ctx = OHOS::Rosen::GlfwRenderContext::GetGlobal();
80 if (!ctx->Init()) {
81 LOGI("Failed to initialize the glfw.");
82 return -1;
83 }
84 ctx->CreateGlfwWindow(args.deviceWidth, args.deviceHeight, true);
85 OHOS::Ace::Sample::EventAdapter::GetInstance().Initialize(ctx);
86
87 // Create the ace ability
88 auto ability = OHOS::Ace::Platform::AceAbility::CreateInstance(args);
89 CHECK_NULL_RETURN(ability, -1);
90
91 auto&& keyEventCallback = [&ability](
92 const std::shared_ptr<KeyEvent>& keyEvent) { ability->OnInputEvent(keyEvent); };
93 OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterKeyEventCallback(keyEventCallback);
94
95 auto&& pointerEventCallback = [&ability](const std::shared_ptr<PointerEvent>& pointerEvent) {
96 ability->OnInputEvent(pointerEvent);
97 };
98 OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterPointerEventCallback(pointerEventCallback);
99
100 auto&& inspectorCallback = [&ability]() {
101 constexpr char FILE_NAME[] = "InspectorTree.json";
102 std::string jsonTreeStr = ability->GetJSONTree();
103 std::ofstream fileCleaner(FILE_NAME, std::ios_base::out);
104 std::ofstream fileWriter(FILE_NAME, std::ofstream::app);
105 fileWriter << jsonTreeStr;
106 fileWriter << std::endl;
107 fileWriter.close();
108 };
109 OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterInspectorCallback(inspectorCallback);
110
111 OHOS::Rosen::WMError errCode;
112 OHOS::sptr<OHOS::Rosen::WindowOption> sp = nullptr;
113 auto window = OHOS::Rosen::Window::Create("previewer", sp, nullptr, errCode);
114 window->CreateSurfaceNode("preview_surface", args.onRender);
115 ability->SetWindow(window);
116
117 ability->InitEnv();
118 LOGI("Ace initialize done. run event loop now");
119 while (!ctx->WindowShouldClose()) {
120 OHOS::AppExecFwk::EventHandler::Run();
121 ctx->PollEvents();
122 std::this_thread::sleep_for(std::chrono::milliseconds(1));
123 }
124
125 LOGI("Successfully exit the application.");
126 return 0;
127 }
128