• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
17  * @addtogroup ArkUI_NativeModule
18  * @{
19  *
20  * @brief Provides UI capabilities of ArkUI on the native side, such as UI
21  * component creation and destruction, tree node operations, attribute setting,
22  * and event listening.
23  *
24  * @since 12
25  */
26 
27 /**
28  * @file native_event.h
29  *
30  * @brief Provides the event type definitions of ArkUI on the native side.
31  *
32  * @library libace_ndk.z.so
33  * @syscap SystemCapability.ArkUI.ArkUI.Full
34  * @since 12
35  */
36 
37 #ifndef ARKUI_NATIVE_COMPATIBLE
38 #define ARKUI_NATIVE_COMPATIBLE
39 
40 #include <cstdint>
41 
42 #include "native_node.h"
43 #include "native_type.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 /**
50  * @brief Enumerates the tool types of the touch event.
51  *
52  * @since 12
53  */
54 typedef enum {
55     /** Unknown tool type. */
56     NODE_TOOL_TYPE_UNKNOWN = -1,
57 
58     /** Finger. */
59     NODE_TOOL_TYPE_FINGER = 0,
60 
61     /** Stylus. */
62     NODE_TOOL_TYPE_STYLUS = 1,
63 } ArkUI_NodeToolType;
64 
65 /**
66  * @brief Enumerates the source types of the touch event.
67  *
68  * @since 12
69  */
70 typedef enum {
71     /** Unknown source type. */
72     NODE_SOURCE_TYPE_UNKNOWN = -1,
73     /** Touchscreen. */
74     NODE_SOURCE_TYPE_TOUCH_SCREEN = 0,
75     /** Stylus. */
76     NODE_SOURCE_TYPE_STYLUS = 1,
77     /** Touchpad. */
78     NODE_SOURCE_TYPE_TOUCHPAD = 2,
79 } ArkUI_NodeSourceType;
80 
81 /**
82  * @brief Defines the data structure of the touch point information of the
83  * touch event.
84  *
85  * @since 12
86  */
87 typedef struct {
88     /** ID of the touch event. */
89     int32_t id;
90 
91     /** Timestamp when a finger is pressed, in μs. */
92     int64_t pressedTime;
93 
94     /** X coordinate of the touch point on the screen. */
95     int32_t screenX;
96 
97     /** Y coordinate of the touch point on the screen. */
98     int32_t screenY;
99 
100     /** X coordinate of the touch point in the window. */
101     int32_t windowX;
102 
103     /** Y coordinate of the touch point in the window. */
104     int32_t windowY;
105 
106     /** X coordinate of the touch point in the component that triggers the event. */
107     int32_t nodeX;
108 
109     /** Y coordinate of the touch point in the component that triggers the event. */
110     int32_t nodeY;
111 
112     /** Pressure value. The value range is [0.0, 1.0]. The value 0.0 indicates
113         that the pressure is not supported. */
114     double pressure;
115 
116     /** Width of the touch area. */
117     int32_t contactAreaWidth;
118 
119     /** Height of the touch area. */
120     int32_t contactAreaHeight;
121 
122     /** Angle relative to the YZ plane. The value range is [-90, 90]. A positive value indicates a rightward tilt. */
123     double tiltX;
124 
125     /** Angle relative to the XZ plane. The value range is [-90, 90]. A positive value indicates a downward tilt. */
126     double tiltY;
127 
128     /** X coordinate of the center point of the tool area. */
129     int32_t toolX;
130 
131     /** Y coordinate of the center point of the tool area. */
132     int32_t toolY;
133 
134     /** Width of the tool area. */
135     int32_t toolWidth;
136 
137     /** Height of the tool area. */
138     int32_t toolHeight;
139 
140     /** X coordinate of the input device. */
141     int32_t rawX;
142 
143     /** Y coordinate of the input device. */
144     int32_t rawY;
145 
146     /** Tool type. */
147     ArkUI_NodeToolType toolType;
148 } ArkUI_NodeTouchPoint;
149 
150 /**
151  * @brief Enumerates touch event types.
152  *
153  * @since 12
154  */
155 typedef enum {
156     /** Cancellation of touch. */
157     NODE_ACTION_CANCEL = 0,
158     /** Pressing of a touch point. */
159     NODE_ACTION_DOWN = 1,
160     /** Moving of a touch point. */
161     NODE_ACTION_MOVE = 2,
162     /** Lifting of a touch point. */
163     NODE_ACTION_UP = 3,
164 } ArkUI_NodeTouchEventAction;
165 
166 /**
167 
168  * @brief Defines the data structure of the historical point information.
169  *
170  * @since 12
171  */
172 typedef struct {
173     /** Touch event type. */
174     ArkUI_NodeTouchEventAction action;
175     /** Timestamp of the historical touch event, in μs. */
176     int64_t timeStamp;
177     /** Source type of the historical touch event. */
178     ArkUI_NodeTouchPoint actionTouch;
179     /** Source type of the historical touch event. */
180     ArkUI_NodeSourceType sourceType;
181 } ArkUI_NodeHistoricalTouchPoint;
182 
183 /**
184  * @brief Defines the data structure of the touch event.
185  *
186  * @since 12
187  */
188 typedef struct {
189     /** Touch event type. */
190     ArkUI_NodeTouchEventAction action;
191 
192     /** Timestamp of the touch event, in μs. */
193     int64_t timeStamp;
194 
195     /** Touch point information of the touch event. */
196     ArkUI_NodeTouchPoint actionTouch;
197 
198     /**
199      * @brief Returns information about all touch points when this event occurs.
200      * @param points Indicates the pointer to the object that receives data.
201      * @return Returns the number of elements in the touch point information array.
202      * @note
203      * When this function is called, ArkUI creates a heap memory object of the touch point information array and
204      * returns the pointer to the pointer. You need to call <b>delete[]</b> to manually free the memory when the
205      * object is not in use.
206      */
207     int32_t (*getTouches)(ArkUI_NodeTouchPoint** points);
208 
209     /**
210      * @brief Returns the historical point information of this event, which covers actions that occur between
211      * this event and the previous event.
212      * @param historicalPoints Indicates the pointer to the object that receives data.
213      * @return Returns the number of elements in the historical touch point information array.
214      * @note
215      * When this function is called, ArkUI creates a heap memory object of the historical touch point information array
216      * and returns the pointer to the pointer. You need to call <b>delete[]</b> to manually free the memory
217      * when the object is not in use.
218      */
219     int32_t (*getHistoricalPoints)(ArkUI_NodeHistoricalTouchPoint** historicalPoints);
220 
221     /** Source type of the touch event. */
222     ArkUI_NodeSourceType sourceType;
223 
224     /** Whether to prevent propagation of the event to the parent node. */
225     bool stopPropagation;
226 
227     /** Whether to prevent the default event processing behavior of the current node and allow propagation of the event. */
228     bool preventDefault;
229 } ArkUI_NodeTouchEvent;
230 
231 /**
232  * @brief Defines the common structure type of a component event.
233  *
234  * @since 12
235  */
236 typedef struct {
237     /**
238      * @brief Enumerates the event types.
239      *
240      * @see ArkUI_NodeEventType
241      */
242     int32_t kind;
243 
244     /**
245      * @brief Defines the custom ID of the event.
246      *
247      * The event ID is passed as a parameter in {@link registerNodeEvent} and can be applied to the dispatch logic
248      * of the same event entry function {@link registerNodeEventReceiver}.
249      */
250     int32_t eventId;
251 
252     /** Component object that triggers the event. */
253     ArkUI_NodeHandle node;
254     union {
255         /** Callback parameter of the touch event type. */
256         ArkUI_NodeTouchEvent touchEvent;
257         /** The universal component event uses callback parameters of the number type. */
258         ArkUI_NodeComponentEvent componentEvent;
259         /** The universal component event uses callback parameters of the string type. */
260         ArkUI_StringAsyncEvent stringEvent;
261     };
262     /**
263      * @brief Defines the custom parameter of the event.
264      *
265      * This parameter is passed in {@link registerNodeEvent} and can be applied to the service logic when the event
266      * is triggered.
267      */
268     void* extraParam;
269 } ArkUI_CompatibleNodeEvent;
270 
271 /**
272  * @brief Declares a collection of native node APIs provided by ArkUI.
273  *
274  * @version 1
275  * @since 12
276  */
277 typedef struct {
278     /** Struct version. */
279     int32_t version;
280 
281     /**
282      * @brief Creates a component based on {@link ArkUI_NodeType} and returns the pointer to the created component.
283      *
284      * @param type Indicates the type of component to create.
285      * @return Returns the pointer to the created component. If the component fails to be created, NULL is returned.
286      */
287     ArkUI_NodeHandle (*createNode)(ArkUI_NodeType type);
288 
289     /**
290      * @brief Destroys the component to which the specified pointer points.
291      *
292      * When the component is being displayed, this API must be called in the main thread.
293      *
294      * @param node Indicates the pointer.
295      */
296     void (*disposeNode)(ArkUI_NodeHandle node);
297 
298     /**
299      * @brief Adds a component to a parent node.
300      *
301      * When the parent component is being displayed, this API must be called in the main thread.
302      *
303      * @param parent Indicates the pointer to the parent node.
304      * @param child Indicates the pointer to the child node.
305      * @return Returns 0 if success.
306      * Returns 401 if a parameter exception occurs.
307      */
308     int32_t (*addChild)(ArkUI_NodeHandle parent, ArkUI_NodeHandle child);
309 
310     /**
311      * @brief Removes a component from its parent node.
312      *
313      * When the component is being displayed, this API must be called in the main thread.
314      *
315      * @param parent Indicates the pointer to the parent node.
316      * @param child Indicates the pointer to the child node.
317      * @return Returns 0 if success.
318      * Returns 401 if a parameter exception occurs.
319      */
320     int32_t (*removeChild)(ArkUI_NodeHandle parent, ArkUI_NodeHandle child);
321 
322     /**
323      * @brief Inserts a component to a parent node after the specified <b>sibling</b> node.
324      *
325      * When the parent component is being displayed, this API must be called in the main thread.
326      *
327      * @param parent Indicates the pointer to the parent node.
328      * @param child Indicates the pointer to the child node.
329      * @param sibling Indicates the pointer to the sibling node after which the target node is to be inserted.
330      * If the value is null, the node is inserted at the start of the parent node.
331      * @return Returns 0 if success.
332      * Returns 401 if a parameter exception occurs.
333      */
334     int32_t (*insertChildAfter)(ArkUI_NodeHandle parent, ArkUI_NodeHandle child, ArkUI_NodeHandle sibling);
335 
336     /**
337      * @brief Inserts a component to a parent node before the specified <b>sibling</b> node.
338      *
339      * When the parent component is being displayed, this API must be called in the main thread.
340      *
341      * @param parent Indicates the pointer to the parent node.
342      * @param child Indicates the pointer to the child node.
343      * @param sibling Indicates the pointer to the sibling node before which the target node is to be inserted.
344      * If the value is null, the node is inserted at the end of the parent node.
345      * @return Returns 0 if success.
346      * Returns 401 if a parameter exception occurs.
347      */
348     int32_t (*insertChildBefore)(ArkUI_NodeHandle parent, ArkUI_NodeHandle child, ArkUI_NodeHandle sibling);
349 
350     /**
351      * @brief Inserts a component to the specified position in a parent node.
352      *
353      * When the parent component is being displayed, this API must be called in the main thread.
354      *
355      * @param parent Indicates the pointer to the parent node.
356      * @param child Indicates the pointer to the child node.
357      * @param position Indicates the position to which the target child node is to be inserted. If the value is a
358      * negative number or invalid, the node is inserted at the end of the parent node.
359      * @return Returns 0 if success.
360      * Returns 401 if a parameter exception occurs.
361      */
362     int32_t (*insertChildAt)(ArkUI_NodeHandle parent, ArkUI_NodeHandle child, int32_t position);
363 
364     /**
365      * @brief Sets the attribute of a node.
366      *
367      * When the component is being displayed, this API must be called in the main thread.
368      *
369      * @param node Indicates the node whose attribute needs to be set.
370      * @param attribute Indicates the type of attribute to set.
371      * @param value Indicates the attribute value.
372      * @return Returns 0 if success.
373      * Returns 401 if a parameter exception occurs.
374      * Returns 106102 if the dynamic implementation library of the native API was not found.
375      */
376     int32_t (*setAttribute)(ArkUI_NodeHandle node, ArkUI_NodeAttributeType attribute, const ArkUI_AttributeItem* item);
377 
378     /**
379      * @brief Obtains an attribute.
380      *
381      * The pointer returned by this API is an internal buffer pointer of the ArkUI framework. As such, you do not need
382      * to call <b>delete</b> to release the memory. However, the pointer must be used before this API is called next
383      * time. Otherwise, the pointer may be overwritten by other values.
384      * When the component is being displayed, this API must be called in the main thread.
385      *
386      * @param node Indicates the node whose attribute needs to be obtained.
387      * @param attribute Indicates the type of attribute to obtain.
388      * @return Returns the attribute value. If the operation fails, a null pointer is returned.
389      */
390     const ArkUI_AttributeItem* (*getAttribute)(ArkUI_NodeHandle node, ArkUI_NodeAttributeType attribute);
391 
392     /**
393      * @brief Resets an attribute.
394      *
395      * When the component is being displayed, this API must be called in the main thread.
396      *
397      * @param node Indicates the node whose attribute needs to be reset.
398      * @param attribute Indicates the type of attribute to reset.
399      * @return Returns 0 if success.
400      * Returns 401 if a parameter exception occurs.
401      * Returns 106102 if the dynamic implementation library of the native API was not found.
402      */
403     int32_t (*resetAttribute)(ArkUI_NodeHandle node, ArkUI_NodeAttributeType attribute);
404 
405     /**
406      * @brief Registers an event for the specified node.
407      *
408      * When the component is being displayed, this API must be called in the main thread.
409      *
410      * @param node Indicates the target node.
411      * @param eventType Indicates the type of event to register.
412      * @param eventId Indicates the custom event ID, which is passed in the callback of <@link ArkUI_NodeEvent>
413      * when the event is triggered.
414      * @return Returns 0 if success.
415      * Returns 401 if a parameter exception occurs.
416      * Returns 106102 if the dynamic implementation library of the native API was not found.
417      */
418     int32_t (*registerNodeEvent)(
419         ArkUI_NodeHandle node, ArkUI_NodeEventType eventType, int32_t eventId);
420 
421     /**
422      * @brief Unregisters an event for the specified node.
423      *
424      * When the component is being displayed, this API must be called in the main thread.
425      *
426      * @param node Indicates the target node.
427      * @param eventType Indicates the type of event to unregister.
428      */
429     void (*unregisterNodeEvent)(ArkUI_NodeHandle node, ArkUI_NodeEventType eventType);
430 
431     /**
432      * @brief Registers an event receiver.
433      *
434      * The ArkUI framework collects component events generated during the process and calls back the events through the
435      * registered event receiver. \n
436      * A new call to this API will overwrite the previously registered event receiver.
437      *
438      * @param eventReceiver Indicates the event receiver to register.
439      */
440     void (*registerNodeEventReceiver)(void (*eventReceiver)(ArkUI_CompatibleNodeEvent* event));
441 
442     /**
443      * @brief Unregisters the event receiver.
444      *
445      */
446     void (*unregisterNodeEventReceiver)();
447 
448     /**
449      * @brief Forcibly marks the current node that needs to be measured, laid out, or rendered again.
450      *
451      * Regarding updates to system attributes, the ArkUI framework automatically marks the dirty area and performs
452      * measuring, layout, or rendering again. In this case, you do not need to call this API.
453      * When the component is being displayed, this API must be called in the main thread.
454      *
455      * @param node Indicates the node for which you want to mark as dirty area.
456      * @param dirtyFlag Indicates type of dirty area.
457      */
458     void (*markDirty)(ArkUI_NodeHandle node, ArkUI_NodeDirtyFlag dirtyFlag);
459 } ArkUI_NativeNodeAPI_Compatible;
460 
461 
462 #ifdef __cplusplus
463 };
464 #endif
465 
466 #endif // ARKUI_NATIVE_COMPATIBLE
467 /** @} */
468