1 /*
2  * Copyright (c) 2021-2024 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 "native_interface_xcomponent.h"
17 
18 #include "node/node_model.h"
19 #include "ui_input_event.h"
20 #include "native_node.h"
21 #include "core/interfaces/arkoala/arkoala_api.h"
22 
23 #include "base/error/error_code.h"
24 #include "frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
OH_NativeXComponent_GetXComponentId(OH_NativeXComponent * component,char * id,uint64_t * size)30 int32_t OH_NativeXComponent_GetXComponentId(OH_NativeXComponent* component, char* id, uint64_t* size)
31 {
32     if ((component == nullptr) || (id == nullptr) || (size == nullptr)) {
33         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
34     }
35     if (((*size) == 0) || ((*size) > (OH_XCOMPONENT_ID_LEN_MAX + 1))) {
36         LOGE("The referenced value of 'size' should be in the range (0, OH_XCOMPONENT_ID_LEN_MAX + 1]");
37         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
38     }
39     return component->GetXComponentId(id, size);
40 }
41 
OH_NativeXComponent_GetXComponentSize(OH_NativeXComponent * component,const void * window,uint64_t * width,uint64_t * height)42 int32_t OH_NativeXComponent_GetXComponentSize(
43     OH_NativeXComponent* component, const void* window, uint64_t* width, uint64_t* height)
44 {
45     if ((component == nullptr) || (window == nullptr) || (width == nullptr) || (height == nullptr)) {
46         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
47     }
48     return component->GetXComponentSize(window, width, height);
49 }
50 
OH_NativeXComponent_GetXComponentOffset(OH_NativeXComponent * component,const void * window,double * x,double * y)51 int32_t OH_NativeXComponent_GetXComponentOffset(
52     OH_NativeXComponent* component, const void* window, double* x, double* y)
53 {
54     if ((component == nullptr) || (window == nullptr) || (x == nullptr) || (y == nullptr)) {
55         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
56     }
57     return component->GetXComponentOffset(window, x, y);
58 }
59 
OH_NativeXComponent_GetTouchEvent(OH_NativeXComponent * component,const void * window,OH_NativeXComponent_TouchEvent * touchEvent)60 int32_t OH_NativeXComponent_GetTouchEvent(
61     OH_NativeXComponent* component, const void* window, OH_NativeXComponent_TouchEvent* touchEvent)
62 {
63     if ((component == nullptr) || (window == nullptr) || (touchEvent == nullptr)) {
64         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
65     }
66     return component->GetTouchEvent(window, touchEvent);
67 }
68 
OH_NativeXComponent_GetTouchPointToolType(OH_NativeXComponent * component,uint32_t pointIndex,OH_NativeXComponent_TouchPointToolType * toolType)69 int32_t OH_NativeXComponent_GetTouchPointToolType(
70     OH_NativeXComponent* component, uint32_t pointIndex, OH_NativeXComponent_TouchPointToolType* toolType)
71 {
72     if ((component == nullptr) || (toolType == nullptr)) {
73         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
74     }
75     return component->GetToolType(pointIndex, toolType);
76 }
77 
OH_NativeXComponent_GetTouchPointTiltX(OH_NativeXComponent * component,uint32_t pointIndex,float * tiltX)78 int32_t OH_NativeXComponent_GetTouchPointTiltX(OH_NativeXComponent* component, uint32_t pointIndex, float* tiltX)
79 {
80     if ((component == nullptr) || (tiltX == nullptr)) {
81         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
82     }
83     return component->GetTiltX(pointIndex, tiltX);
84 }
85 
OH_NativeXComponent_GetTouchPointTiltY(OH_NativeXComponent * component,uint32_t pointIndex,float * tiltY)86 int32_t OH_NativeXComponent_GetTouchPointTiltY(OH_NativeXComponent* component, uint32_t pointIndex, float* tiltY)
87 {
88     if ((component == nullptr) || (tiltY == nullptr)) {
89         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
90     }
91     return component->GetTiltY(pointIndex, tiltY);
92 }
93 
OH_NativeXComponent_GetTouchPointWindowX(OH_NativeXComponent * component,uint32_t pointIndex,float * windowX)94 int32_t OH_NativeXComponent_GetTouchPointWindowX(OH_NativeXComponent* component, uint32_t pointIndex, float* windowX)
95 {
96     if ((component == nullptr) || (windowX == nullptr)) {
97         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
98     }
99     return component->GetWindowX(pointIndex, windowX);
100 }
101 
OH_NativeXComponent_GetTouchPointWindowY(OH_NativeXComponent * component,uint32_t pointIndex,float * windowY)102 int32_t OH_NativeXComponent_GetTouchPointWindowY(OH_NativeXComponent* component, uint32_t pointIndex, float* windowY)
103 {
104     if ((component == nullptr) || (windowY == nullptr)) {
105         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
106     }
107     return component->GetWindowY(pointIndex, windowY);
108 }
109 
OH_NativeXComponent_GetTouchPointDisplayX(OH_NativeXComponent * component,uint32_t pointIndex,float * displayX)110 int32_t OH_NativeXComponent_GetTouchPointDisplayX(OH_NativeXComponent* component, uint32_t pointIndex, float* displayX)
111 {
112     if ((component == nullptr) || (displayX == nullptr)) {
113         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
114     }
115     return component->GetDisplayX(pointIndex, displayX);
116 }
117 
OH_NativeXComponent_GetTouchPointDisplayY(OH_NativeXComponent * component,uint32_t pointIndex,float * displayY)118 int32_t OH_NativeXComponent_GetTouchPointDisplayY(OH_NativeXComponent* component, uint32_t pointIndex, float* displayY)
119 {
120     if ((component == nullptr) || (displayY == nullptr)) {
121         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
122     }
123     return component->GetDisplayY(pointIndex, displayY);
124 }
125 
OH_NativeXComponent_GetHistoricalPoints(OH_NativeXComponent * component,const void * window,int32_t * size,OH_NativeXComponent_HistoricalPoint ** historicalPoints)126 int32_t OH_NativeXComponent_GetHistoricalPoints(OH_NativeXComponent* component, const void* window,
127     int32_t* size, OH_NativeXComponent_HistoricalPoint** historicalPoints)
128 {
129     if ((component == nullptr) || (window == nullptr)) {
130         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
131     }
132     return component->GetHistoryPoints(window, size, historicalPoints);
133 }
134 
OH_NativeXComponent_GetMouseEvent(OH_NativeXComponent * component,const void * window,OH_NativeXComponent_MouseEvent * mouseEvent)135 int32_t OH_NativeXComponent_GetMouseEvent(
136     OH_NativeXComponent* component, const void* window, OH_NativeXComponent_MouseEvent* mouseEvent)
137 {
138     if ((component == nullptr) || (window == nullptr) || (mouseEvent == nullptr)) {
139         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
140     }
141     return component->GetMouseEvent(window, mouseEvent);
142 }
143 
OH_NativeXComponent_RegisterCallback(OH_NativeXComponent * component,OH_NativeXComponent_Callback * callback)144 int32_t OH_NativeXComponent_RegisterCallback(OH_NativeXComponent* component, OH_NativeXComponent_Callback* callback)
145 {
146     if ((component == nullptr) || (callback == nullptr)) {
147         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
148     }
149     return component->RegisterCallback(callback);
150 }
151 
OH_NativeXComponent_RegisterMouseEventCallback(OH_NativeXComponent * component,OH_NativeXComponent_MouseEvent_Callback * callback)152 int32_t OH_NativeXComponent_RegisterMouseEventCallback(
153     OH_NativeXComponent* component, OH_NativeXComponent_MouseEvent_Callback* callback)
154 {
155     if ((component == nullptr) || (callback == nullptr)) {
156         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
157     }
158     return component->RegisterMouseEventCallback(callback);
159 }
160 
OH_NativeXComponent_RegisterFocusEventCallback(OH_NativeXComponent * component,void (* callback)(OH_NativeXComponent * component,void * window))161 int32_t OH_NativeXComponent_RegisterFocusEventCallback(
162     OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window))
163 {
164     if ((component == nullptr) || (callback == nullptr)) {
165         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
166     }
167     return component->RegisterFocusEventCallback(callback);
168 }
169 
OH_NativeXComponent_RegisterKeyEventCallback(OH_NativeXComponent * component,void (* callback)(OH_NativeXComponent * component,void * window))170 int32_t OH_NativeXComponent_RegisterKeyEventCallback(
171     OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window))
172 {
173     if ((component == nullptr) || (callback == nullptr)) {
174         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
175     }
176     return component->RegisterKeyEventCallback(callback);
177 }
178 
OH_NativeXComponent_RegisterBlurEventCallback(OH_NativeXComponent * component,void (* callback)(OH_NativeXComponent * component,void * window))179 int32_t OH_NativeXComponent_RegisterBlurEventCallback(
180     OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window))
181 {
182     if ((component == nullptr) || (callback == nullptr)) {
183         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
184     }
185     return component->RegisterBlurEventCallback(callback);
186 }
187 
OH_NativeXComponent_GetKeyEvent(OH_NativeXComponent * component,OH_NativeXComponent_KeyEvent ** keyEvent)188 int32_t OH_NativeXComponent_GetKeyEvent(OH_NativeXComponent* component, OH_NativeXComponent_KeyEvent** keyEvent)
189 {
190     if ((component == nullptr) || (keyEvent == nullptr)) {
191         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
192     }
193     return component->GetKeyEvent(keyEvent);
194 }
195 
OH_NativeXComponent_GetKeyEventAction(OH_NativeXComponent_KeyEvent * keyEvent,OH_NativeXComponent_KeyAction * action)196 int32_t OH_NativeXComponent_GetKeyEventAction(
197     OH_NativeXComponent_KeyEvent* keyEvent, OH_NativeXComponent_KeyAction* action)
198 {
199     if ((keyEvent == nullptr) || (action == nullptr)) {
200         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
201     }
202     (*action) = keyEvent->action;
203     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
204 }
205 
OH_NativeXComponent_GetKeyEventCode(OH_NativeXComponent_KeyEvent * keyEvent,OH_NativeXComponent_KeyCode * code)206 int32_t OH_NativeXComponent_GetKeyEventCode(OH_NativeXComponent_KeyEvent* keyEvent, OH_NativeXComponent_KeyCode* code)
207 {
208     if ((keyEvent == nullptr) || (code == nullptr)) {
209         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
210     }
211     (*code) = keyEvent->code;
212     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
213 }
214 
OH_NativeXComponent_GetKeyEventSourceType(OH_NativeXComponent_KeyEvent * keyEvent,OH_NativeXComponent_EventSourceType * sourceType)215 int32_t OH_NativeXComponent_GetKeyEventSourceType(
216     OH_NativeXComponent_KeyEvent* keyEvent, OH_NativeXComponent_EventSourceType* sourceType)
217 {
218     if ((keyEvent == nullptr) || (sourceType == nullptr)) {
219         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
220     }
221     (*sourceType) = keyEvent->sourceType;
222     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
223 }
224 
OH_NativeXComponent_GetKeyEventDeviceId(OH_NativeXComponent_KeyEvent * keyEvent,int64_t * deviceId)225 int32_t OH_NativeXComponent_GetKeyEventDeviceId(OH_NativeXComponent_KeyEvent* keyEvent, int64_t* deviceId)
226 {
227     if ((keyEvent == nullptr) || (deviceId == nullptr)) {
228         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
229     }
230     (*deviceId) = keyEvent->deviceId;
231     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
232 }
233 
OH_NativeXComponent_GetKeyEventTimestamp(OH_NativeXComponent_KeyEvent * keyEvent,int64_t * timestamp)234 int32_t OH_NativeXComponent_GetKeyEventTimestamp(OH_NativeXComponent_KeyEvent* keyEvent, int64_t* timestamp)
235 {
236     if ((keyEvent == nullptr) || (timestamp == nullptr)) {
237         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
238     }
239     (*timestamp) = keyEvent->timestamp;
240     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
241 }
242 
OH_NativeXComponent_SetExpectedFrameRateRange(OH_NativeXComponent * component,OH_NativeXComponent_ExpectedRateRange * range)243 int32_t OH_NativeXComponent_SetExpectedFrameRateRange(
244     OH_NativeXComponent* component, OH_NativeXComponent_ExpectedRateRange* range)
245 {
246     if (component == nullptr || range == nullptr) {
247         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
248     }
249     return component->SetExpectedFrameRateRange(range);
250 }
251 
OH_NativeXComponent_RegisterOnFrameCallback(OH_NativeXComponent * component,void (* callback)(OH_NativeXComponent * component,uint64_t timestamp,uint64_t targetTimestamp))252 int32_t OH_NativeXComponent_RegisterOnFrameCallback(OH_NativeXComponent* component,
253     void (*callback)(OH_NativeXComponent* component, uint64_t timestamp, uint64_t targetTimestamp))
254 {
255     if (component == nullptr) {
256         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
257     }
258     return component->RegisterOnFrameCallback(callback);
259 }
260 
OH_NativeXComponent_UnregisterOnFrameCallback(OH_NativeXComponent * component)261 int32_t OH_NativeXComponent_UnregisterOnFrameCallback(OH_NativeXComponent* component)
262 {
263     if (component == nullptr) {
264         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
265     }
266     return component->UnregisterOnFrameCallback();
267 }
268 
OH_NativeXComponent_AttachNativeRootNode(OH_NativeXComponent * component,ArkUI_NodeHandle root)269 int32_t OH_NativeXComponent_AttachNativeRootNode(
270     OH_NativeXComponent* component, ArkUI_NodeHandle root)
271 {
272     if ((component == nullptr) || (root == nullptr) || !OHOS::Ace::NodeModel::CheckIsCNode(root)) {
273         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
274     }
275     return component->AttachNativeRootNode(root->uiNodeHandle);
276 }
277 
OH_NativeXComponent_DetachNativeRootNode(OH_NativeXComponent * component,ArkUI_NodeHandle root)278 int32_t OH_NativeXComponent_DetachNativeRootNode(
279     OH_NativeXComponent* component, ArkUI_NodeHandle root)
280 {
281     if ((component == nullptr) || (root == nullptr) || !OHOS::Ace::NodeModel::CheckIsCNode(root)) {
282         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
283     }
284     return component->DetachNativeRootNode(root->uiNodeHandle);
285 }
286 
OH_NativeXComponent_RegisterSurfaceShowCallback(OH_NativeXComponent * component,void (* callback)(OH_NativeXComponent * component,void * window))287 int32_t OH_NativeXComponent_RegisterSurfaceShowCallback(
288     OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window))
289 {
290     return (component && callback) ? component->RegisterSurfaceShowCallback(callback)
291         : OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
292 }
293 
OH_NativeXComponent_RegisterSurfaceHideCallback(OH_NativeXComponent * component,void (* callback)(OH_NativeXComponent * component,void * window))294 int32_t OH_NativeXComponent_RegisterSurfaceHideCallback(
295     OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window))
296 {
297     return (component && callback) ? component->RegisterSurfaceHideCallback(callback)
298         : OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
299 }
300 
OH_NativeXComponent_RegisterUIInputEventCallback(OH_NativeXComponent * component,void (* callback)(OH_NativeXComponent * component,ArkUI_UIInputEvent * event,ArkUI_UIInputEvent_Type type),ArkUI_UIInputEvent_Type type)301 int32_t OH_NativeXComponent_RegisterUIInputEventCallback(OH_NativeXComponent* component,
302     void (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event, ArkUI_UIInputEvent_Type type),
303     ArkUI_UIInputEvent_Type type)
304 {
305     if ((component == nullptr) || (callback == nullptr)) {
306         return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
307     }
308     if (type == ArkUI_UIInputEvent_Type::ARKUI_UIINPUTEVENT_TYPE_AXIS) {
309         return component->RegisterUIAxisEventCallback(callback);
310     }
311     return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
312 }
313 
OH_NativeXComponent_RegisterOnTouchInterceptCallback(OH_NativeXComponent * component,HitTestMode (* callback)(OH_NativeXComponent * component,ArkUI_UIInputEvent * event))314 int32_t OH_NativeXComponent_RegisterOnTouchInterceptCallback(
315     OH_NativeXComponent* component, HitTestMode (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event))
316 {
317     if ((component == nullptr) || (callback == nullptr)) {
318         return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
319     }
320     return component->RegisterOnTouchInterceptCallback(callback);
321 }
322 
OH_NativeXComponent_SetNeedSoftKeyboard(OH_NativeXComponent * component,bool needSoftKeyboard)323 int32_t OH_NativeXComponent_SetNeedSoftKeyboard(OH_NativeXComponent* component, bool needSoftKeyboard)
324 {
325     return component ? component->SetNeedSoftKeyboard(needSoftKeyboard) : OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
326 }
327 
OH_NativeXComponent_GetTouchEventSourceType(OH_NativeXComponent * component,int32_t pointId,OH_NativeXComponent_EventSourceType * sourceType)328 int32_t OH_NativeXComponent_GetTouchEventSourceType(
329     OH_NativeXComponent* component, int32_t pointId, OH_NativeXComponent_EventSourceType* sourceType)
330 {
331     return (component && sourceType) ? component->GetSourceType(pointId, sourceType)
332                                      : OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
333 }
334 
OH_NativeXComponent_GetNativeXComponent(ArkUI_NodeHandle node)335 OH_NativeXComponent* OH_NativeXComponent_GetNativeXComponent(ArkUI_NodeHandle node)
336 {
337     if (node == nullptr || node->type != ARKUI_NODE_XCOMPONENT) {
338         return nullptr;
339     }
340     auto nodeModifiers = OHOS::Ace::NodeModel::GetFullImpl()->getNodeModifiers();
341     return reinterpret_cast<OH_NativeXComponent*>(
342         nodeModifiers->getXComponentModifier()->getNativeXComponent(node->uiNodeHandle));
343 }
344 
OH_NativeXComponent_GetNativeAccessibilityProvider(OH_NativeXComponent * component,ArkUI_AccessibilityProvider ** handle)345 int32_t OH_NativeXComponent_GetNativeAccessibilityProvider(
346     OH_NativeXComponent* component, ArkUI_AccessibilityProvider** handle)
347 {
348     if ((component == nullptr) || (handle == nullptr)) {
349         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
350     }
351 
352     return component->GetAccessibilityProvider(handle);
353 }
354 
OH_NativeXComponent_RegisterKeyEventCallbackWithResult(OH_NativeXComponent * component,bool (* callback)(OH_NativeXComponent * component,void * window))355 int32_t OH_NativeXComponent_RegisterKeyEventCallbackWithResult(
356     OH_NativeXComponent* component, bool (*callback)(OH_NativeXComponent* component, void* window))
357 {
358     if ((component == nullptr) || (callback == nullptr)) {
359         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
360     }
361     return component->RegisterKeyEventCallbackWithResult(callback);
362 }
363 
364 #ifdef __cplusplus
365 };
366 #endif
367