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
24 extern "C" {
25 #endif
26 
OH_NativeXComponent_GetXComponentId(OH_NativeXComponent* component, char* id, uint64_t* size)27 int32_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 
OH_NativeXComponent_GetXComponentSize( OH_NativeXComponent* component, const void* window, uint64_t* width, uint64_t* height)39 int32_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 
OH_NativeXComponent_GetXComponentOffset( OH_NativeXComponent* component, const void* window, double* x, double* y)48 int32_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 
OH_NativeXComponent_GetTouchEvent( OH_NativeXComponent* component, const void* window, OH_NativeXComponent_TouchEvent* touchEvent)57 int32_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 
OH_NativeXComponent_GetTouchPointToolType( OH_NativeXComponent* component, uint32_t pointIndex, OH_NativeXComponent_TouchPointToolType* toolType)66 int32_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 
OH_NativeXComponent_GetTouchPointTiltX(OH_NativeXComponent* component, uint32_t pointIndex, float* tiltX)75 int32_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 
OH_NativeXComponent_GetTouchPointTiltY(OH_NativeXComponent* component, uint32_t pointIndex, float* tiltY)83 int32_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 
OH_NativeXComponent_GetTouchPointWindowX(OH_NativeXComponent* component, uint32_t pointIndex, float* windowX)91 int32_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 
OH_NativeXComponent_GetTouchPointWindowY(OH_NativeXComponent* component, uint32_t pointIndex, float* windowY)99 int32_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 
OH_NativeXComponent_GetTouchPointDisplayX(OH_NativeXComponent* component, uint32_t pointIndex, float* displayX)107 int32_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 
OH_NativeXComponent_GetTouchPointDisplayY(OH_NativeXComponent* component, uint32_t pointIndex, float* displayY)115 int32_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 
OH_NativeXComponent_GetHistoricalPoints(OH_NativeXComponent* component, const void* window, int32_t* size, OH_NativeXComponent_HistoricalPoint** historicalPoints)123 int32_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 
OH_NativeXComponent_GetMouseEvent( OH_NativeXComponent* component, const void* window, OH_NativeXComponent_MouseEvent* mouseEvent)132 int32_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 
OH_NativeXComponent_RegisterCallback(OH_NativeXComponent* component, OH_NativeXComponent_Callback* callback)141 int32_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 
OH_NativeXComponent_RegisterMouseEventCallback( OH_NativeXComponent* component, OH_NativeXComponent_MouseEvent_Callback* callback)149 int32_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 
OH_NativeXComponent_RegisterFocusEventCallback( OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window))158 int32_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 
OH_NativeXComponent_RegisterKeyEventCallback( OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window))167 int32_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 
OH_NativeXComponent_RegisterBlurEventCallback( OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window))176 int32_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 
OH_NativeXComponent_GetKeyEvent(OH_NativeXComponent* component, OH_NativeXComponent_KeyEvent** keyEvent)185 int32_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 
OH_NativeXComponent_GetKeyEventAction( OH_NativeXComponent_KeyEvent* keyEvent, OH_NativeXComponent_KeyAction* action)193 int32_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 
OH_NativeXComponent_GetKeyEventCode(OH_NativeXComponent_KeyEvent* keyEvent, OH_NativeXComponent_KeyCode* code)203 int32_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 
OH_NativeXComponent_GetKeyEventSourceType( OH_NativeXComponent_KeyEvent* keyEvent, OH_NativeXComponent_EventSourceType* sourceType)212 int32_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 
OH_NativeXComponent_GetKeyEventDeviceId(OH_NativeXComponent_KeyEvent* keyEvent, int64_t* deviceId)222 int32_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 
OH_NativeXComponent_GetKeyEventTimestamp(OH_NativeXComponent_KeyEvent* keyEvent, int64_t* timestamp)231 int32_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 
OH_NativeXComponent_SetExpectedFrameRateRange( OH_NativeXComponent* component, OH_NativeXComponent_ExpectedRateRange* range)240 int32_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 
OH_NativeXComponent_RegisterOnFrameCallback(OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, uint64_t timestamp, uint64_t targetTimestamp))249 int32_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 
OH_NativeXComponent_UnregisterOnFrameCallback(OH_NativeXComponent* component)258 int32_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 
OH_NativeXComponent_AttachNativeRootNode( OH_NativeXComponent* component, ArkUI_NodeHandle root)266 int32_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 
OH_NativeXComponent_DetachNativeRootNode( OH_NativeXComponent* component, ArkUI_NodeHandle root)275 int32_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 
OH_NativeXComponent_RegisterUIInputEventCallback(OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event, ArkUI_UIInputEvent_Type type), ArkUI_UIInputEvent_Type type)284 int32_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 
OH_NativeXComponent_SetNeedSoftKeyboard(OH_NativeXComponent* component, bool needSoftKeyboard)297 int32_t OH_NativeXComponent_SetNeedSoftKeyboard(OH_NativeXComponent* component, bool needSoftKeyboard)
298 {
299     return component ? component->SetNeedSoftKeyboard(needSoftKeyboard) : OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
300 }
301 
OH_NativeXComponent_RegisterSurfaceShowCallback( OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window))302 int32_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 
OH_NativeXComponent_RegisterSurfaceHideCallback( OH_NativeXComponent* component, void (*callback)(OH_NativeXComponent* component, void* window))309 int32_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 
OH_NativeXComponent_RegisterOnTouchInterceptCallback( OH_NativeXComponent* component, HitTestMode (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event))316 int32_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 
OH_NativeXComponent_GetTouchEventSourceType( OH_NativeXComponent* component, int32_t pointId, OH_NativeXComponent_EventSourceType* sourceType)325 int32_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 
OH_NativeXComponent_GetNativeXComponent(ArkUI_NodeHandle node)332 OH_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 
OH_NativeXComponent_GetNativeAccessibilityProvider( OH_NativeXComponent* component, ArkUI_AccessibilityProvider** handle)342 int32_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