1 /*
2 * Copyright (c) 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 "common/common.h"
17 #include "xcomponent_manager.h"
18 #include <cstdint>
19 #include <string>
20
21 #include <native_buffer/native_buffer.h>
22 #include <native_window/external_window.h>
23
24 #include "manager/AccessibilityManager.h"
25 #include "plugin_manager.h"
26
27
OnSurfaceCreatedCB(OH_NativeXComponent * component,void * window)28 void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window) {
29 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceCreatedCB");
30 if ((component == nullptr) || (window == nullptr)) {
31 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback",
32 "OnSurfaceCreatedCB: component or window is null");
33 return;
34 }
35
36 char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'};
37 uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
38 if (OH_NativeXComponent_GetXComponentId(component, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
39 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback",
40 "OnSurfaceCreatedCB: Unable to get XComponent id");
41 return;
42 }
43
44 std::string id(idStr);
45 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceCreatedCB component id is %{public}s", id.c_str());
46
47 auto plugin = NativeXComponentSample::PluginManager::GetInstance();
48 plugin->AttachXComponent(id, component, reinterpret_cast<::OHNativeWindow *>(window));
49 }
50
OnSurfaceChangedCB(OH_NativeXComponent * component,void * window)51 void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window) {}
52
OnSurfaceDestroyedCB(OH_NativeXComponent * component,void * window)53 void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window) {}
54
DispatchTouchEventCB(OH_NativeXComponent * component,void * window)55 void DispatchTouchEventCB(OH_NativeXComponent *component, void *window) {
56 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Callback", "DispatchTouchEventCB");
57 OH_NativeXComponent_TouchEvent touchEvent;
58 }
59
DispatchHoverEvent(::OH_NativeXComponent * xComponent,bool isHover)60 void DispatchHoverEvent(::OH_NativeXComponent *xComponent, bool isHover) {}
61
62 namespace NativeXComponentSample {
63
XComponentManager()64 XComponentManager::XComponentManager() : accessibilityManager(new AccessibilityManager()) {}
65
~XComponentManager()66 XComponentManager::~XComponentManager() { delete accessibilityManager; }
67
RegisterCallback(OH_NativeXComponent * nativeXComponent)68 void XComponentManager::RegisterCallback(OH_NativeXComponent *nativeXComponent) {
69 renderCallback_.OnSurfaceCreated = ::OnSurfaceCreatedCB;
70 renderCallback_.OnSurfaceChanged = ::OnSurfaceChangedCB;
71 renderCallback_.OnSurfaceDestroyed = ::OnSurfaceDestroyedCB;
72 renderCallback_.DispatchTouchEvent = ::DispatchTouchEventCB;
73 int ret = OH_NativeXComponent_RegisterCallback(nativeXComponent, &renderCallback_);
74 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "XComponentManager", "RegisterCallback %{public}d", ret);
75 }
76
SetNativeXComponent(const std::string & id,OH_NativeXComponent * nativeXComponent,OHNativeWindow * window)77 void XComponentManager::SetNativeXComponent(const std::string &id, OH_NativeXComponent *nativeXComponent,
78 OHNativeWindow *window) {
79 if (nativeXComponent == nullptr) {
80 return;
81 }
82 auto it = nativeXComponentMap_.find(id);
83 if (it != nativeXComponentMap_.end()) {
84 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "SetNativeXComponent", "%{public}s already exists",
85 id.data());
86 return;
87 } else {
88 nativeXComponentMap_.emplace(id, XSurface{nativeXComponent, window});
89 }
90
91 accessibilityManager->Initialize(id, nativeXComponent);
92 }
93
94 } // namespace NativeXComponentSample