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 228 * the current node and allow propagation of the event. */ 229 bool preventDefault; 230 } ArkUI_NodeTouchEvent; 231 232 /** 233 * @brief Defines the common structure type of a component event. 234 * 235 * @since 12 236 */ 237 typedef struct { 238 /** 239 * @brief Enumerates the event types. 240 * 241 * @see ArkUI_NodeEventType 242 */ 243 int32_t kind; 244 245 /** 246 * @brief Defines the custom ID of the event. 247 * 248 * The event ID is passed as a parameter in {@link registerNodeEvent} and can be applied to the dispatch logic 249 * of the same event entry function {@link registerNodeEventReceiver}. 250 */ 251 int32_t eventId; 252 253 /** Component object that triggers the event. */ 254 ArkUI_NodeHandle node; 255 union { 256 /** Callback parameter of the touch event type. */ 257 ArkUI_NodeTouchEvent touchEvent; 258 /** The universal component event uses callback parameters of the number type. */ 259 ArkUI_NodeComponentEvent componentEvent; 260 /** The universal component event uses callback parameters of the string type. */ 261 ArkUI_StringAsyncEvent stringEvent; 262 }; 263 /** 264 * @brief Defines the custom parameter of the event. 265 * 266 * This parameter is passed in {@link registerNodeEvent} and can be applied to the service logic when the event 267 * is triggered. 268 */ 269 void* extraParam; 270 } ArkUI_CompatibleNodeEvent; 271 272 /** 273 * @brief Declares a collection of native node APIs provided by ArkUI. 274 * 275 * @version 1 276 * @since 12 277 */ 278 typedef struct { 279 /** Struct version. */ 280 int32_t version; 281 282 /** 283 * @brief Creates a component based on {@link ArkUI_NodeType} and returns the pointer to the created component. 284 * 285 * @param type Indicates the type of component to create. 286 * @return Returns the pointer to the created component. If the component fails to be created, NULL is returned. 287 */ 288 ArkUI_NodeHandle (*createNode)(ArkUI_NodeType type); 289 290 /** 291 * @brief Destroys the component to which the specified pointer points. 292 * 293 * When the component is being displayed, this API must be called in the main thread. 294 * 295 * @param node Indicates the pointer. 296 */ 297 void (*disposeNode)(ArkUI_NodeHandle node); 298 299 /** 300 * @brief Adds a component to a parent node. 301 * 302 * When the parent component is being displayed, this API must be called in the main thread. 303 * 304 * @param parent Indicates the pointer to the parent node. 305 * @param child Indicates the pointer to the child node. 306 * @return Returns 0 if success. 307 * Returns 401 if a parameter exception occurs. 308 */ 309 int32_t (*addChild)(ArkUI_NodeHandle parent, ArkUI_NodeHandle child); 310 311 /** 312 * @brief Removes a component from its parent node. 313 * 314 * When the component is being displayed, this API must be called in the main thread. 315 * 316 * @param parent Indicates the pointer to the parent node. 317 * @param child Indicates the pointer to the child node. 318 * @return Returns 0 if success. 319 * Returns 401 if a parameter exception occurs. 320 */ 321 int32_t (*removeChild)(ArkUI_NodeHandle parent, ArkUI_NodeHandle child); 322 323 /** 324 * @brief Inserts a component to a parent node after the specified <b>sibling</b> node. 325 * 326 * When the parent component is being displayed, this API must be called in the main thread. 327 * 328 * @param parent Indicates the pointer to the parent node. 329 * @param child Indicates the pointer to the child node. 330 * @param sibling Indicates the pointer to the sibling node after which the target node is to be inserted. 331 * If the value is null, the node is inserted at the start of the parent node. 332 * @return Returns 0 if success. 333 * Returns 401 if a parameter exception occurs. 334 */ 335 int32_t (*insertChildAfter)(ArkUI_NodeHandle parent, ArkUI_NodeHandle child, ArkUI_NodeHandle sibling); 336 337 /** 338 * @brief Inserts a component to a parent node before the specified <b>sibling</b> node. 339 * 340 * When the parent component is being displayed, this API must be called in the main thread. 341 * 342 * @param parent Indicates the pointer to the parent node. 343 * @param child Indicates the pointer to the child node. 344 * @param sibling Indicates the pointer to the sibling node before which the target node is to be inserted. 345 * If the value is null, the node is inserted at the end of the parent node. 346 * @return Returns 0 if success. 347 * Returns 401 if a parameter exception occurs. 348 */ 349 int32_t (*insertChildBefore)(ArkUI_NodeHandle parent, ArkUI_NodeHandle child, ArkUI_NodeHandle sibling); 350 351 /** 352 * @brief Inserts a component to the specified position in a parent node. 353 * 354 * When the parent component is being displayed, this API must be called in the main thread. 355 * 356 * @param parent Indicates the pointer to the parent node. 357 * @param child Indicates the pointer to the child node. 358 * @param position Indicates the position to which the target child node is to be inserted. If the value is a 359 * negative number or invalid, the node is inserted at the end of the parent node. 360 * @return Returns 0 if success. 361 * Returns 401 if a parameter exception occurs. 362 */ 363 int32_t (*insertChildAt)(ArkUI_NodeHandle parent, ArkUI_NodeHandle child, int32_t position); 364 365 /** 366 * @brief Sets the attribute of a node. 367 * 368 * When the component is being displayed, this API must be called in the main thread. 369 * 370 * @param node Indicates the node whose attribute needs to be set. 371 * @param attribute Indicates the type of attribute to set. 372 * @param value Indicates the attribute value. 373 * @return Returns 0 if success. 374 * Returns 401 if a parameter exception occurs. 375 * Returns 106102 if the dynamic implementation library of the native API was not found. 376 */ 377 int32_t (*setAttribute)(ArkUI_NodeHandle node, ArkUI_NodeAttributeType attribute, const ArkUI_AttributeItem* item); 378 379 /** 380 * @brief Obtains an attribute. 381 * 382 * The pointer returned by this API is an internal buffer pointer of the ArkUI framework. As such, you do not need 383 * to call <b>delete</b> to release the memory. However, the pointer must be used before this API is called next 384 * time. Otherwise, the pointer may be overwritten by other values. 385 * When the component is being displayed, this API must be called in the main thread. 386 * 387 * @param node Indicates the node whose attribute needs to be obtained. 388 * @param attribute Indicates the type of attribute to obtain. 389 * @return Returns the attribute value. If the operation fails, a null pointer is returned. 390 */ 391 const ArkUI_AttributeItem* (*getAttribute)(ArkUI_NodeHandle node, ArkUI_NodeAttributeType attribute); 392 393 /** 394 * @brief Resets an attribute. 395 * 396 * When the component is being displayed, this API must be called in the main thread. 397 * 398 * @param node Indicates the node whose attribute needs to be reset. 399 * @param attribute Indicates the type of attribute to reset. 400 * @return Returns 0 if success. 401 * Returns 401 if a parameter exception occurs. 402 * Returns 106102 if the dynamic implementation library of the native API was not found. 403 */ 404 int32_t (*resetAttribute)(ArkUI_NodeHandle node, ArkUI_NodeAttributeType attribute); 405 406 /** 407 * @brief Registers an event for the specified node. 408 * 409 * When the component is being displayed, this API must be called in the main thread. 410 * 411 * @param node Indicates the target node. 412 * @param eventType Indicates the type of event to register. 413 * @param eventId Indicates the custom event ID, which is passed in the callback of <@link ArkUI_NodeEvent> 414 * when the event is triggered. 415 * @return Returns 0 if success. 416 * Returns 401 if a parameter exception occurs. 417 * Returns 106102 if the dynamic implementation library of the native API was not found. 418 */ 419 int32_t (*registerNodeEvent)( 420 ArkUI_NodeHandle node, ArkUI_NodeEventType eventType, int32_t eventId); 421 422 /** 423 * @brief Unregisters an event for the specified node. 424 * 425 * When the component is being displayed, this API must be called in the main thread. 426 * 427 * @param node Indicates the target node. 428 * @param eventType Indicates the type of event to unregister. 429 */ 430 void (*unregisterNodeEvent)(ArkUI_NodeHandle node, ArkUI_NodeEventType eventType); 431 432 /** 433 * @brief Registers an event receiver. 434 * 435 * The ArkUI framework collects component events generated during the process and calls back the events through the 436 * registered event receiver. \n 437 * A new call to this API will overwrite the previously registered event receiver. 438 * 439 * @param eventReceiver Indicates the event receiver to register. 440 */ 441 void (*registerNodeEventReceiver)(void (*eventReceiver)(ArkUI_CompatibleNodeEvent* event)); 442 443 /** 444 * @brief Unregisters the event receiver. 445 * 446 */ 447 void (*unregisterNodeEventReceiver)(); 448 449 /** 450 * @brief Forcibly marks the current node that needs to be measured, laid out, or rendered again. 451 * 452 * Regarding updates to system attributes, the ArkUI framework automatically marks the dirty area and performs 453 * measuring, layout, or rendering again. In this case, you do not need to call this API. 454 * When the component is being displayed, this API must be called in the main thread. 455 * 456 * @param node Indicates the node for which you want to mark as dirty area. 457 * @param dirtyFlag Indicates type of dirty area. 458 */ 459 void (*markDirty)(ArkUI_NodeHandle node, ArkUI_NodeDirtyFlag dirtyFlag); 460 } ArkUI_NativeNodeAPI_Compatible; 461 462 463 #ifdef __cplusplus 464 }; 465 #endif 466 467 #endif // ARKUI_NATIVE_COMPATIBLE 468 /** @} */ 469