1 /*
2  * Copyright (c) 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 #ifndef OHOS_ACE_FRAMEWORK_CJ_COMMON_FFI_H
17 #define OHOS_ACE_FRAMEWORK_CJ_COMMON_FFI_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_macro.h"
23 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
24 #include "core/event/touch_event.h"
25 #include "core/gestures/gesture_info.h"
26 
27 extern "C" {
28 struct NativeFontInfo {
29     const char* path = "";
30     const char* postScriptName = "";
31     const char* fullName = "";
32     const char* family = "";
33     const char* subfamily = "";
34     uint32_t weight = 0;
35     uint32_t width = 0;
36     bool italic = false;
37     bool monoSpace = false;
38     bool symbolic = false;
39 };
40 
41 struct NativeOptionFontInfo {
42     bool hasValue;
43     NativeFontInfo* info;
44 };
45 
46 struct NativeLength {
47     double value;
48     int32_t unitType;
49 };
50 
51 struct NativeOffset {
52     NativeLength dx;
53     NativeLength dy;
54 };
55 
56 struct NativeOptionLength {
57     bool hasValue;
58     NativeLength value;
59 };
60 
61 struct NativeOptionInt32 {
62     bool hasValue;
63     int32_t value;
64 };
65 
66 struct NativeOptionInt64 {
67     bool hasValue;
68     int64_t value;
69 };
70 
71 struct NativeOptionUInt32 {
72     bool hasValue;
73     uint32_t value;
74 };
75 
76 struct NativeOptionFloat32 {
77     bool hasValue;
78     float value;
79 };
80 
81 struct NativeOptionFloat64 {
82     bool hasValue;
83     double value;
84 };
85 
86 struct NativeOptionCallBack {
87     bool hasValue;
88     void (*value)();
89 };
90 
91 struct NativeOptionBool {
92     bool hasValue;
93     bool value;
94 };
95 
96 struct CArrInt32 {
97     int32_t* head;
98     int64_t size;
99 };
100 
101 struct NativeOptionCArrInt32 {
102     bool hasValue;
103     CArrInt32 value;
104 };
105 
106 struct NativeOptionCString {
107     bool hasValue;
108     const char* value;
109 };
110 
111 struct CJOffset {
112     double xOffset;
113     double yOffset;
114 };
115 
116 struct CJTouchInfo {
117     uint8_t type;
118     int32_t fingerId;
119     double globalX;
120     double globalY;
121     double localX;
122     double localY;
123 };
124 
125 struct CJTextPickerResult {
126     const char* value;
127     uint32_t index;
128 };
129 
130 struct CJImageComplete {
131     double width;
132     double height;
133     double componentWidth;
134     double componentHeight;
135     int32_t loadingStatus;
136 };
137 
138 struct CJImageError {
139     double componentWidth;
140     double componentHeight;
141 };
142 
143 struct CJPosition {
144     double x;
145     double y;
146 };
147 
148 struct CJArea {
149     double width;
150     double height;
151     CJPosition* position;
152     CJPosition* globalPosition;
153 };
154 
155 struct CJEventTarget {
156     CJArea* area;
157 };
158 
159 struct CJTouchEvent {
160     uint8_t eventType;
161     CJTouchInfo* touches;
162     int32_t touchesSize;
163     CJTouchInfo* changedTouches;
164     int32_t changedTouchesSize;
165     int64_t timestamp;
166     CJEventTarget* target;
167     int32_t sourceType;
168     std::string ToString() const;
169 };
170 
171 struct CJMouseEvent {
172     int64_t timestamp;
173     double screenX;
174     double screenY;
175     double x;
176     double y;
177     int32_t button;
178     int32_t action;
179 };
180 
181 struct CJClickInfo {
182     double x;
183     double y;
184     int64_t timestamp;
185     int32_t source;
186     CJEventTarget* target;
187     double windowX;
188     double windowY;
189     double displayX;
190     double displayY;
191     std::string ToString() const;
192 };
193 
194 struct CJKeyEvent {
195     const char* keyText;
196     int32_t type;
197     int32_t keyCode;
198     int32_t keySource;
199     int32_t metaKey;
200     int64_t deviceId;
201     int64_t timestamp;
202 };
203 
204 struct CJFingerInfo {
205     int32_t id;
206     double globalX;
207     double globalY;
208     double localX;
209     double localY;
210 };
211 
212 struct CJGestureEvent {
213     int64_t timestamp;
214     CJEventTarget* target;
215     bool repeat;
216     CJFingerInfo* fingerList;
217     int32_t fingerListSize;
218     int32_t source;
219     double offsetX;
220     double offsetY;
221     double scale;
222     double pinchCenterX;
223     double pinchCenterY;
224     double angle;
225     double speed;
226 };
227 
228 struct CJDragInfo {
229     const char* extraParams;
230     CJPosition* position;
231 };
232 
233 struct CJDragItemInfo {
234     int64_t pixelMapId;
235     void (*builder)();
236     const char* extraInfo;
237 };
238 
239 struct AtCPackage;
240 
241 struct AtCOHOSAceFrameworkCJInstanceLoadEntryParams {
242     const char* name;
243     bool* result;
244 
AtCOHOSAceFrameworkCJInstanceLoadEntryParamsAtCOHOSAceFrameworkCJInstanceLoadEntryParams245     AtCOHOSAceFrameworkCJInstanceLoadEntryParams(const char* name, bool* result) : name(name), result(result) {}
246 };
247 
248 struct AtCOHOSAceFrameworkCJInstanceInitializeParams {
249     int16_t runtimeId;
250     int64_t version;
251     AtCPackage* package;
252     bool* result;
253 
AtCOHOSAceFrameworkCJInstanceInitializeParamsAtCOHOSAceFrameworkCJInstanceInitializeParams254     AtCOHOSAceFrameworkCJInstanceInitializeParams(int16_t runtimeId, int64_t version, AtCPackage* package, bool* result)
255         : runtimeId(runtimeId), version(version), package(package), result(result)
256     {}
257 };
258 
259 struct AtCPackage {
260     bool (*atCOHOSAceFrameworkCJInstanceInitialize)(AtCOHOSAceFrameworkCJInstanceInitializeParams* params) = nullptr;
261     bool (*atCOHOSAceFrameworkCJInstanceLoadEntry)(AtCOHOSAceFrameworkCJInstanceLoadEntryParams*) = nullptr;
262     void (*atCOHOSAceFrameworkRemoteViewRender)(int64_t self) = nullptr;
263     void (*atCOHOSAceFrameworkRemoteViewRerender)(int64_t self) = nullptr;
264     void (*atCOHOSAceFrameworkRemoteViewRelease)(int64_t self) = nullptr;
265     void (*atCOHOSAceFrameworkRemoteViewOnAppear)(int64_t self) = nullptr;
266     void (*atCOHOSAceFrameworkRemoteViewOnShow)(int64_t self) = nullptr;
267     void (*atCOHOSAceFrameworkRemoteViewOnHide)(int64_t self) = nullptr;
268     bool (*atCOHOSAceFrameworkRemoteViewOnBackPress)(int64_t self) = nullptr;
269     void (*atCOHOSAceFrameworkRemoteViewUpdateWithJson)(int64_t self, const char* json) = nullptr;
270     void (*atCOHOSAceFrameworkRemoteViewOnTransition)(int64_t self) = nullptr;
271     void (*atCOHOSAceFrameworkRemoteViewOnAboutToRender)(int64_t self) = nullptr;
272     void (*atCOHOSAceFrameworkRemoteViewOnAfterRender)(int64_t self) = nullptr;
273     void (*atCOHOSAceFrameworkRemoteViewOnDisappear)(int64_t self) = nullptr;
274     void (*atCOHOSAceFrameworkRemoteViewOnAboutToBeDeleted)(int64_t self) = nullptr;
275     void (*atCOHOSAceFrameworkRemoteViewForceCompleteRerender)(int64_t self, bool deep) = nullptr;
276     ExternalString (*atCOHOSAceFrameworkLazyForEachFuncsGenerateKey)(int64_t self, int64_t idx) = nullptr;
277     void (*atCOHOSAceFrameworkLazyForEachFuncsGenerateItem)(int64_t self, int64_t idx) = nullptr;
278     int64_t (*atCOHOSAceFrameworkLazyForEachFuncsGetTotalCount)(int64_t self) = nullptr;
279     void (*atCOHOSAceFrameworkLazyForEachFuncsMarkLazy)(int64_t self, const char* key) = nullptr;
280     void (*atCOHOSAceFrameworkLazyForEachFuncsResetLazy)(int64_t self) = nullptr;
281     void (*atCOHOSAceFrameworkLazyForEachFuncsRemoveChildGroup)(int64_t self, const char* composedId) = nullptr;
282     void (*atCOHOSAceFrameworkLazyForEachFuncsDataChangeListenerRegister)(int64_t self, int64_t idx) = nullptr;
283     void (*atCOHOSAceFrameworkLazyForEachFuncsDataChangeListenerUnregister)(int64_t self, int64_t idx) = nullptr;
284 };
285 
286 CJ_EXPORT void FfiOHOSAceFrameworkRegisterCJFuncs(AtCPackage cjFuncs);
287 
288 CJ_EXPORT int64_t FfiGeneralSizeOfPointer();
289 }
290 
291 namespace OHOS::Ace {
292 void TransformNativeTouchLocationInfo(
293     CJTouchInfo* sources, const std::list<OHOS::Ace::TouchLocationInfo>& touchLocationInfoList);
294 
295 void TransformNativeCJFingerInfo(CJFingerInfo* sources, const std::list<OHOS::Ace::FingerInfo>& fingerInfoList);
296 
297 void AssambleCJEventTarget(
298     const OHOS::Ace::EventTarget& eventTarget, CJArea& area, CJPosition& position, CJPosition& globalPosition);
299 
300 void AssambleCJClickInfo(const OHOS::Ace::GestureEvent& event, CJClickInfo& clickInfo, CJEventTarget& eventTarget,
301     CJArea& area, CJPosition& position, CJPosition& globalPosition);
302 } // namespace OHOS::Ace
303 #endif // OHOS_ACE_FRAMEWORK_CJ_COMMON_FFI_H
304