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 "frameworks/core/components/xcomponent/native_interface_xcomponent_impl.h"
17 
18 #include "securec.h"
19 
20 #include "base/error/error_code.h"
21 
GetXComponentId(char * id,uint64_t * size)22 int32_t OH_NativeXComponent::GetXComponentId(char* id, uint64_t* size)
23 {
24     if (xcomponentImpl_ == nullptr) {
25         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
26     }
27     auto xcomponentId = xcomponentImpl_->GetXComponentId();
28     uint64_t idSize = static_cast<uint64_t>(xcomponentId.size());
29     if (idSize > (uint64_t)(OH_XCOMPONENT_ID_LEN_MAX)) {
30         LOGE("Length of XComponent id should be no more than OH_XCOMPONENT_ID_LEN_MAX");
31         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
32     }
33     if (strncpy_s(id, (*size), xcomponentId.c_str(), idSize) == 0) {
34         (*size) = idSize;
35         return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
36     } else {
37         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
38     }
39 }
40 
GetNativeWindow(void ** window)41 int32_t OH_NativeXComponent::GetNativeWindow(void** window)
42 {
43     if (xcomponentImpl_ == nullptr) {
44         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
45     }
46     auto surface = const_cast<void*>(xcomponentImpl_->GetSurface());
47     if (surface) {
48         (*window) = surface;
49         return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
50     } else {
51         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
52     }
53 }
54 
GetXComponentSize(const void * window,uint64_t * width,uint64_t * height)55 int32_t OH_NativeXComponent::GetXComponentSize(const void* window, uint64_t* width, uint64_t* height)
56 {
57     if (xcomponentImpl_ == nullptr) {
58         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
59     }
60     auto surfaceWindow = const_cast<void*>(xcomponentImpl_->GetSurface());
61     if (window != surfaceWindow) {
62         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
63     }
64     (*width) = static_cast<uint64_t>(xcomponentImpl_->GetXComponentWidth());
65     (*height) = static_cast<uint64_t>(xcomponentImpl_->GetXComponentHeight());
66 
67     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
68 }
69 
GetXComponentOffset(const void * window,double * x,double * y)70 int32_t OH_NativeXComponent::GetXComponentOffset(const void* window, double* x, double* y)
71 {
72     if (xcomponentImpl_ == nullptr) {
73         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
74     }
75     auto surfaceWindow = const_cast<void*>(xcomponentImpl_->GetSurface());
76     if (window != surfaceWindow) {
77         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
78     }
79     (*x) = static_cast<double>(xcomponentImpl_->GetXComponentOffsetX());
80     (*y) = static_cast<double>(xcomponentImpl_->GetXComponentOffsetY());
81 
82     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
83 }
84 
GetHistoryPoints(const void * window,int32_t * size,OH_NativeXComponent_HistoricalPoint ** historicalPoints)85 int32_t OH_NativeXComponent::GetHistoryPoints(const void* window,
86     int32_t* size, OH_NativeXComponent_HistoricalPoint** historicalPoints)
87 {
88     if (xcomponentImpl_ == nullptr) {
89         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
90     }
91     auto surfaceWindow = const_cast<void*>(xcomponentImpl_->GetSurface());
92     if (window != surfaceWindow) {
93         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
94     }
95     xcomponentImpl_->GetHistoryPoints(size, historicalPoints);
96     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
97 }
98 
GetTouchEvent(const void * window,OH_NativeXComponent_TouchEvent * touchEvent)99 int32_t OH_NativeXComponent::GetTouchEvent(const void* window, OH_NativeXComponent_TouchEvent* touchEvent)
100 {
101     if (xcomponentImpl_ == nullptr) {
102         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
103     }
104     auto surfaceWindow = const_cast<void*>(xcomponentImpl_->GetSurface());
105     if (window != surfaceWindow) {
106         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
107     }
108     (*touchEvent) = xcomponentImpl_->GetTouchEvent();
109     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
110 }
111 
GetToolType(size_t pointIndex,OH_NativeXComponent_TouchPointToolType * toolType)112 int32_t OH_NativeXComponent::GetToolType(size_t pointIndex, OH_NativeXComponent_TouchPointToolType* toolType)
113 {
114     if (xcomponentImpl_ == nullptr) {
115         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
116     }
117     (*toolType) = xcomponentImpl_->GetToolType(pointIndex);
118     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
119 }
120 
GetTiltX(size_t pointIndex,float * tiltX)121 int32_t OH_NativeXComponent::GetTiltX(size_t pointIndex, float* tiltX)
122 {
123     if (xcomponentImpl_ == nullptr) {
124         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
125     }
126     (*tiltX) = xcomponentImpl_->GetTiltX(pointIndex);
127     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
128 }
129 
GetTiltY(size_t pointIndex,float * tiltY)130 int32_t OH_NativeXComponent::GetTiltY(size_t pointIndex, float* tiltY)
131 {
132     if (xcomponentImpl_ == nullptr) {
133         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
134     }
135     (*tiltY) = xcomponentImpl_->GetTiltY(pointIndex);
136     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
137 }
138 
GetWindowX(size_t pointIndex,float * windowX)139 int32_t OH_NativeXComponent::GetWindowX(size_t pointIndex, float* windowX)
140 {
141     if (xcomponentImpl_ == nullptr) {
142         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
143     }
144     (*windowX) = xcomponentImpl_->GetWindowX(pointIndex);
145     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
146 }
147 
GetWindowY(size_t pointIndex,float * windowY)148 int32_t OH_NativeXComponent::GetWindowY(size_t pointIndex, float* windowY)
149 {
150     if (xcomponentImpl_ == nullptr) {
151         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
152     }
153     (*windowY) = xcomponentImpl_->GetWindowY(pointIndex);
154     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
155 }
156 
GetDisplayX(size_t pointIndex,float * displayX)157 int32_t OH_NativeXComponent::GetDisplayX(size_t pointIndex, float* displayX)
158 {
159     if (xcomponentImpl_ == nullptr) {
160         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
161     }
162     (*displayX) = xcomponentImpl_->GetDisplayX(pointIndex);
163     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
164 }
165 
GetDisplayY(size_t pointIndex,float * displayY)166 int32_t OH_NativeXComponent::GetDisplayY(size_t pointIndex, float* displayY)
167 {
168     if (xcomponentImpl_ == nullptr) {
169         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
170     }
171     (*displayY) = xcomponentImpl_->GetDisplayY(pointIndex);
172     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
173 }
174 
GetMouseEvent(const void * window,OH_NativeXComponent_MouseEvent * mouseEvent)175 int32_t OH_NativeXComponent::GetMouseEvent(const void* window, OH_NativeXComponent_MouseEvent* mouseEvent)
176 {
177     if (xcomponentImpl_ == nullptr) {
178         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
179     }
180     auto surfaceWindow = const_cast<void*>(xcomponentImpl_->GetSurface());
181     if (window != surfaceWindow) {
182         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
183     }
184     (*mouseEvent) = xcomponentImpl_->GetMouseEvent();
185     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
186 }
187 
RegisterCallback(OH_NativeXComponent_Callback * callback)188 int32_t OH_NativeXComponent::RegisterCallback(OH_NativeXComponent_Callback* callback)
189 {
190     if (xcomponentImpl_ == nullptr) {
191         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
192     }
193     xcomponentImpl_->SetCallback(callback);
194     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
195 }
196 
RegisterMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback * callback)197 int32_t OH_NativeXComponent::RegisterMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback* callback)
198 {
199     if (xcomponentImpl_ == nullptr) {
200         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
201     }
202     xcomponentImpl_->SetMouseEventCallback(callback);
203     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
204 }
205 
RegisterFocusEventCallback(void (* callback)(OH_NativeXComponent * component,void * window))206 int32_t OH_NativeXComponent::RegisterFocusEventCallback(void (*callback)(OH_NativeXComponent* component, void* window))
207 {
208     if (xcomponentImpl_ == nullptr) {
209         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
210     }
211     xcomponentImpl_->SetFocusEventCallback(callback);
212     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
213 }
214 
RegisterKeyEventCallback(void (* callback)(OH_NativeXComponent * component,void * window))215 int32_t OH_NativeXComponent::RegisterKeyEventCallback(void (*callback)(OH_NativeXComponent* component, void* window))
216 {
217     if (xcomponentImpl_ == nullptr) {
218         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
219     }
220     xcomponentImpl_->SetKeyEventCallback(callback);
221     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
222 }
223 
RegisterKeyEventCallbackWithResult(bool (* callback)(OH_NativeXComponent * component,void * window))224 int32_t OH_NativeXComponent::RegisterKeyEventCallbackWithResult(
225     bool (*callback)(OH_NativeXComponent* component, void* window))
226 {
227     if (xcomponentImpl_ == nullptr) {
228         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
229     }
230     xcomponentImpl_->SetKeyEventCallbackWithResult(callback);
231     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
232 }
233 
RegisterBlurEventCallback(void (* callback)(OH_NativeXComponent * component,void * window))234 int32_t OH_NativeXComponent::RegisterBlurEventCallback(void (*callback)(OH_NativeXComponent* component, void* window))
235 {
236     if (xcomponentImpl_ == nullptr) {
237         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
238     }
239     xcomponentImpl_->SetBlurEventCallback(callback);
240     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
241 }
242 
GetKeyEvent(OH_NativeXComponent_KeyEvent ** keyEvent)243 int32_t OH_NativeXComponent::GetKeyEvent(OH_NativeXComponent_KeyEvent** keyEvent)
244 {
245     if (xcomponentImpl_ == nullptr) {
246         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
247     }
248     (*keyEvent) = xcomponentImpl_->GetKeyEvent();
249     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
250 }
251 
SetExpectedFrameRateRange(OH_NativeXComponent_ExpectedRateRange * range)252 int32_t OH_NativeXComponent::SetExpectedFrameRateRange(OH_NativeXComponent_ExpectedRateRange* range)
253 {
254     if (xcomponentImpl_ == nullptr || range == nullptr || !xcomponentImpl_->setExpectedRateRangeCallback_) {
255         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
256     }
257     int32_t min = range->min;
258     int32_t max = range->max;
259     int32_t expected = range->expected;
260 
261     if (!(min <= max && expected >= min && expected <= max)) {
262         LOGE("Xcomponent Expeted FrameRateRange Error.");
263         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
264     }
265     xcomponentImpl_->SetRateRange(range);
266     xcomponentImpl_->setExpectedRateRangeCallback_();
267     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
268 }
269 
RegisterOnFrameCallback(void (* callback)(OH_NativeXComponent * component,uint64_t timestamp,uint64_t targetTimestamp))270 int32_t OH_NativeXComponent::RegisterOnFrameCallback(
271     void (*callback)(OH_NativeXComponent* component, uint64_t timestamp, uint64_t targetTimestamp))
272 {
273     if (xcomponentImpl_ == nullptr || !xcomponentImpl_->setOnFrameEventCallback_) {
274         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
275     }
276     xcomponentImpl_->SetOnFrameCallback(callback);
277     xcomponentImpl_->setOnFrameEventCallback_();
278     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
279 }
280 
UnregisterOnFrameCallback()281 int32_t OH_NativeXComponent::UnregisterOnFrameCallback()
282 {
283     if (xcomponentImpl_ == nullptr || !xcomponentImpl_->setUnregisterOnFrameEventCallback_) {
284         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
285     }
286     xcomponentImpl_->SetOnFrameCallback(nullptr);
287     xcomponentImpl_->setUnregisterOnFrameEventCallback_();
288     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
289 }
290 
AttachNativeRootNode(void * root)291 int32_t OH_NativeXComponent::AttachNativeRootNode(void* root)
292 {
293     if (root == nullptr) {
294         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
295     }
296     xcomponentImpl_->AttachContainer(root);
297     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
298 }
299 
DetachNativeRootNode(void * root)300 int32_t OH_NativeXComponent::DetachNativeRootNode(void* root)
301 {
302     if (root == nullptr) {
303         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
304     }
305     xcomponentImpl_->DetachContainer(root);
306     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
307 }
308 
RegisterSurfaceShowCallback(NativeXComponent_Surface_Callback callback)309 int32_t OH_NativeXComponent::RegisterSurfaceShowCallback(NativeXComponent_Surface_Callback callback)
310 {
311     if (xcomponentImpl_ == nullptr) {
312         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
313     }
314     xcomponentImpl_->SetSurfaceShowCallback(callback);
315     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
316 }
317 
RegisterSurfaceHideCallback(NativeXComponent_Surface_Callback callback)318 int32_t OH_NativeXComponent::RegisterSurfaceHideCallback(NativeXComponent_Surface_Callback callback)
319 {
320     if (xcomponentImpl_ == nullptr) {
321         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
322     }
323     xcomponentImpl_->SetSurfaceHideCallback(callback);
324     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
325 }
326 
RegisterUIAxisEventCallback(void (* callback)(OH_NativeXComponent * component,ArkUI_UIInputEvent * event,ArkUI_UIInputEvent_Type type))327 int32_t OH_NativeXComponent::RegisterUIAxisEventCallback(
328     void (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event, ArkUI_UIInputEvent_Type type))
329 {
330     if (xcomponentImpl_ == nullptr) {
331         return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
332     }
333     xcomponentImpl_->SetUIAxisEventCallback(callback);
334     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
335 }
336 
RegisterOnTouchInterceptCallback(HitTestMode (* callback)(OH_NativeXComponent * component,ArkUI_UIInputEvent * event))337 int32_t OH_NativeXComponent::RegisterOnTouchInterceptCallback(
338     HitTestMode (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event))
339 {
340     if (xcomponentImpl_ == nullptr) {
341         return OHOS::Ace::ERROR_CODE_PARAM_INVALID;
342     }
343     xcomponentImpl_->SetOnTouchInterceptCallback(callback);
344     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
345 }
346 
SetNeedSoftKeyboard(bool needSoftKeyboard)347 int32_t OH_NativeXComponent::SetNeedSoftKeyboard(bool needSoftKeyboard)
348 {
349     if (xcomponentImpl_ == nullptr) {
350         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
351     }
352     xcomponentImpl_->SetNeedSoftKeyboard(needSoftKeyboard);
353     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
354 }
355 
GetSourceType(int32_t pointId,OH_NativeXComponent_EventSourceType * sourceType)356 int32_t OH_NativeXComponent::GetSourceType(int32_t pointId, OH_NativeXComponent_EventSourceType* sourceType)
357 {
358     return (xcomponentImpl_ && xcomponentImpl_->GetSourceType(pointId, sourceType))
359                ? OH_NATIVEXCOMPONENT_RESULT_SUCCESS
360                : OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
361 }
362 
GetAccessibilityProvider(ArkUI_AccessibilityProvider ** handle)363 int32_t OH_NativeXComponent::GetAccessibilityProvider(ArkUI_AccessibilityProvider** handle)
364 {
365     if (xcomponentImpl_ == nullptr || handle == nullptr) {
366         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
367     }
368 
369     (*handle) = xcomponentImpl_->GetAccessbilityProvider().get();
370     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
371 }
372