• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
GetXComponentId(char * id,uint64_t * size)20 int32_t OH_NativeXComponent::GetXComponentId(char* id, uint64_t* size)
21 {
22     if (xcomponentImpl_ == nullptr) {
23         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
24     }
25     auto xcomponentId = xcomponentImpl_->GetXComponentId();
26     uint64_t idSize = static_cast<uint64_t>(xcomponentId.size());
27     if (idSize > (uint64_t)(OH_XCOMPONENT_ID_LEN_MAX)) {
28         LOGE("Length of XComponent id should be no more than OH_XCOMPONENT_ID_LEN_MAX");
29         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
30     }
31     if (strncpy_s(id, (*size), xcomponentId.c_str(), idSize) == 0) {
32         (*size) = idSize;
33         return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
34     } else {
35         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
36     }
37 }
38 
GetNativeWindow(void ** window)39 int32_t OH_NativeXComponent::GetNativeWindow(void** window)
40 {
41     if (xcomponentImpl_ == nullptr) {
42         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
43     }
44     auto surface = const_cast<void*>(xcomponentImpl_->GetSurface());
45     if (surface) {
46         (*window) = surface;
47         return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
48     } else {
49         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
50     }
51 }
52 
GetXComponentSize(const void * window,uint64_t * width,uint64_t * height)53 int32_t OH_NativeXComponent::GetXComponentSize(const void* window, uint64_t* width, uint64_t* height)
54 {
55     if (xcomponentImpl_ == nullptr) {
56         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
57     }
58     auto surfaceWindow = const_cast<void*>(xcomponentImpl_->GetSurface());
59     if (window != surfaceWindow) {
60         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
61     }
62     (*width) = static_cast<uint64_t>(xcomponentImpl_->GetXComponentWidth());
63     (*height) = static_cast<uint64_t>(xcomponentImpl_->GetXComponentHeight());
64 
65     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
66 }
67 
GetXComponentOffset(const void * window,double * x,double * y)68 int32_t OH_NativeXComponent::GetXComponentOffset(const void* window, double* x, double* y)
69 {
70     if (xcomponentImpl_ == nullptr) {
71         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
72     }
73     auto surfaceWindow = const_cast<void*>(xcomponentImpl_->GetSurface());
74     if (window != surfaceWindow) {
75         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
76     }
77     (*x) = static_cast<double>(xcomponentImpl_->GetXComponentOffsetX());
78     (*y) = static_cast<double>(xcomponentImpl_->GetXComponentOffsetY());
79 
80     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
81 }
82 
GetHistoryPoints(const void * window,int32_t * size,OH_NativeXComponent_HistoricalPoint ** historicalPoints)83 int32_t OH_NativeXComponent::GetHistoryPoints(const void* window,
84     int32_t* size, OH_NativeXComponent_HistoricalPoint** historicalPoints)
85 {
86     if (xcomponentImpl_ == nullptr) {
87         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
88     }
89     auto surfaceWindow = const_cast<void*>(xcomponentImpl_->GetSurface());
90     if (window != surfaceWindow) {
91         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
92     }
93     xcomponentImpl_->GetHistoryPoints(size, historicalPoints);
94     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
95 }
96 
GetTouchEvent(const void * window,OH_NativeXComponent_TouchEvent * touchEvent)97 int32_t OH_NativeXComponent::GetTouchEvent(const void* window, OH_NativeXComponent_TouchEvent* touchEvent)
98 {
99     if (xcomponentImpl_ == nullptr) {
100         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
101     }
102     auto surfaceWindow = const_cast<void*>(xcomponentImpl_->GetSurface());
103     if (window != surfaceWindow) {
104         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
105     }
106     (*touchEvent) = xcomponentImpl_->GetTouchEvent();
107     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
108 }
109 
GetToolType(size_t pointIndex,OH_NativeXComponent_TouchPointToolType * toolType)110 int32_t OH_NativeXComponent::GetToolType(size_t pointIndex, OH_NativeXComponent_TouchPointToolType* toolType)
111 {
112     if (xcomponentImpl_ == nullptr) {
113         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
114     }
115     (*toolType) = xcomponentImpl_->GetToolType(pointIndex);
116     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
117 }
118 
GetTiltX(size_t pointIndex,float * tiltX)119 int32_t OH_NativeXComponent::GetTiltX(size_t pointIndex, float* tiltX)
120 {
121     if (xcomponentImpl_ == nullptr) {
122         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
123     }
124     (*tiltX) = xcomponentImpl_->GetTiltX(pointIndex);
125     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
126 }
127 
GetTiltY(size_t pointIndex,float * tiltY)128 int32_t OH_NativeXComponent::GetTiltY(size_t pointIndex, float* tiltY)
129 {
130     if (xcomponentImpl_ == nullptr) {
131         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
132     }
133     (*tiltY) = xcomponentImpl_->GetTiltX(pointIndex);
134     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
135 }
136 
GetMouseEvent(const void * window,OH_NativeXComponent_MouseEvent * mouseEvent)137 int32_t OH_NativeXComponent::GetMouseEvent(const void* window, OH_NativeXComponent_MouseEvent* mouseEvent)
138 {
139     if (xcomponentImpl_ == nullptr) {
140         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
141     }
142     auto surfaceWindow = const_cast<void*>(xcomponentImpl_->GetSurface());
143     if (window != surfaceWindow) {
144         return OH_NATIVEXCOMPONENT_RESULT_FAILED;
145     }
146     (*mouseEvent) = xcomponentImpl_->GetMouseEvent();
147     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
148 }
149 
RegisterCallback(OH_NativeXComponent_Callback * callback)150 int32_t OH_NativeXComponent::RegisterCallback(OH_NativeXComponent_Callback* callback)
151 {
152     if (xcomponentImpl_ == nullptr) {
153         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
154     }
155     xcomponentImpl_->SetCallback(callback);
156     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
157 }
158 
RegisterMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback * callback)159 int32_t OH_NativeXComponent::RegisterMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback* callback)
160 {
161     if (xcomponentImpl_ == nullptr) {
162         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
163     }
164     xcomponentImpl_->SetMouseEventCallback(callback);
165     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
166 }
167 
RegisterFocusEventCallback(void (* callback)(OH_NativeXComponent * component,void * window))168 int32_t OH_NativeXComponent::RegisterFocusEventCallback(void (*callback)(OH_NativeXComponent* component, void* window))
169 {
170     if (xcomponentImpl_ == nullptr) {
171         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
172     }
173     xcomponentImpl_->SetFocusEventCallback(callback);
174     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
175 }
176 
RegisterKeyEventCallback(void (* callback)(OH_NativeXComponent * component,void * window))177 int32_t OH_NativeXComponent::RegisterKeyEventCallback(void (*callback)(OH_NativeXComponent* component, void* window))
178 {
179     if (xcomponentImpl_ == nullptr) {
180         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
181     }
182     xcomponentImpl_->SetKeyEventCallback(callback);
183     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
184 }
185 
RegisterBlurEventCallback(void (* callback)(OH_NativeXComponent * component,void * window))186 int32_t OH_NativeXComponent::RegisterBlurEventCallback(void (*callback)(OH_NativeXComponent* component, void* window))
187 {
188     if (xcomponentImpl_ == nullptr) {
189         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
190     }
191     xcomponentImpl_->SetBlurEventCallback(callback);
192     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
193 }
194 
GetKeyEvent(OH_NativeXComponent_KeyEvent ** keyEvent)195 int32_t OH_NativeXComponent::GetKeyEvent(OH_NativeXComponent_KeyEvent** keyEvent)
196 {
197     if (xcomponentImpl_ == nullptr) {
198         return OH_NATIVEXCOMPONENT_RESULT_BAD_PARAMETER;
199     }
200     (*keyEvent) = xcomponentImpl_->GetKeyEvent();
201     return OH_NATIVEXCOMPONENT_RESULT_SUCCESS;
202 }
203