/* * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include "jsapp/rich/external/EventHandler.h" #include "previewer/include/window.h" #include "adapter/preview/entrance/ace_ability.h" #include "adapter/preview/entrance/ace_run_args.h" #include "adapter/preview/entrance/samples/event_adapter.h" #include "base/log/log.h" #include "base/utils/device_config.h" #include "base/utils/utils.h" #include "adapter/preview/entrance/ace_preview_helper.h" namespace { constexpr char ACE_VERSION_2[] = "2.0"; constexpr char MAX_ARGS_COUNT = 2; #ifdef MAC_PLATFORM const std::string assetPathJs = "/Volumes/SSD2T/daily-test/preview/js/default"; const std::string assetPathEts = "/Volumes/SSD2T/daily-test/preview/js/default_2.0"; #elif WINDOWS_PLATFORM const std::string assetPathJs = "D:\\Workspace\\preview\\js\\default"; const std::string assetPathEts = "D:\\Workspace\\preview\\js\\default_2.0"; #else const std::string assetPathJs = "/home/ubuntu/demo/preview/js/default"; const std::string assetPathEts = "/home/ubuntu/demo/preview/js/default_2.0"; #endif auto&& renderCallback = []( const void*, const size_t bufferSize, const int32_t width, const int32_t height, const uint64_t timestamp) -> bool { LOGI("OnRender: bufferSize = %{public}zu, [width, height] = [%{public}d, %{public}d], timestamp = %{public}llu", bufferSize, width, height, timestamp); return true; }; auto&& pageCallback = [](const std::string currentPagePath) -> bool { LOGI("OnRouterChange: current page: %s", currentPagePath.c_str()); return true; }; } // namespace int main(int argc, const char* argv[]) { OHOS::Ace::Platform::AceRunArgs args = { .assetPath = assetPathJs, .deviceConfig.density = 2.0, .deviceConfig.deviceType = OHOS::Ace::DeviceType::WATCH, .windowTitle = "ACE wearable", .isRound = true, .deviceWidth = 466, .deviceHeight = 466, .onRender = std::move(renderCallback), .onRouterChange = std::move(pageCallback), }; if (argc == MAX_ARGS_COUNT && !std::strcmp(argv[1], ACE_VERSION_2)) { args.assetPath = assetPathEts; args.aceVersion = OHOS::Ace::Platform::AceVersion::ACE_2_0; } // Initialize and create the glfw window. auto ctx = OHOS::Rosen::GlfwRenderContext::GetGlobal(); if (!ctx->Init()) { LOGI("Failed to initialize the glfw."); return -1; } ctx->CreateGlfwWindow(args.deviceWidth, args.deviceHeight, true); OHOS::Ace::Sample::EventAdapter::GetInstance().Initialize(ctx); OHOS::Ace::Platform::AcePreviewHelper::GetInstance()-> SetCallbackOfPostTask(OHOS::AppExecFwk::EventHandler::PostTask); OHOS::Ace::Platform::AcePreviewHelper::GetInstance()-> SetCallbackOfIsCurrentRunnerThread(OHOS::AppExecFwk::EventHandler::IsCurrentRunnerThread); // Create the ace ability auto ability = OHOS::Ace::Platform::AceAbility::CreateInstance(args); CHECK_NULL_RETURN(ability, -1); auto&& keyEventCallback = [&ability]( const std::shared_ptr& keyEvent) { ability->OnInputEvent(keyEvent); }; OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterKeyEventCallback(keyEventCallback); auto&& pointerEventCallback = [&ability](const std::shared_ptr& pointerEvent) { ability->OnInputEvent(pointerEvent); }; OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterPointerEventCallback(pointerEventCallback); auto&& inspectorCallback = [&ability]() { constexpr char FILE_NAME[] = "InspectorTree.json"; std::string jsonTreeStr = ability->GetJSONTree(); std::ofstream fileCleaner(FILE_NAME, std::ios_base::out); std::ofstream fileWriter(FILE_NAME, std::ofstream::app); fileWriter << jsonTreeStr; fileWriter << std::endl; fileWriter.close(); }; OHOS::Ace::Sample::EventAdapter::GetInstance().RegisterInspectorCallback(inspectorCallback); OHOS::Rosen::WMError errCode; OHOS::sptr sp = nullptr; auto window = OHOS::Rosen::Window::Create("previewer", sp, nullptr, errCode); window->CreateSurfaceNode("preview_surface", args.onRender); ability->SetWindow(window); ability->InitEnv(); LOGI("Ace initialize done. run event loop now"); while (!ctx->WindowShouldClose()) { OHOS::AppExecFwk::EventHandler::Run(); ctx->PollEvents(); std::this_thread::sleep_for(std::chrono::milliseconds(1)); } LOGI("Successfully exit the application."); return 0; }