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