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 #ifndef _NATIVE_INTERFACE_XCOMPONENT_IMPL_ 17 #define _NATIVE_INTERFACE_XCOMPONENT_IMPL_ 18 19 #include <functional> 20 #include <memory> 21 #include <string> 22 #include <unistd.h> 23 #include <vector> 24 25 #include "interfaces/native/native_interface_accessibility.h" 26 #include "interfaces/native/native_interface_xcomponent.h" 27 #include "interfaces/native/ui_input_event.h" 28 29 #include "base/memory/ace_type.h" 30 #include "base/utils/utils.h" 31 #include "core/accessibility/native_interface_accessibility_provider.h" 32 #include "core/components_ng/event/gesture_event_hub.h" 33 34 using NativeXComponent_Surface_Callback = void (*)(OH_NativeXComponent*, void*); 35 36 struct XComponentTouchPoint { 37 float tiltX = 0.0f; 38 float tiltY = 0.0f; 39 float windowX = 0.0f; 40 float windowY = 0.0f; 41 float displayX = 0.0f; 42 float displayY = 0.0f; 43 OH_NativeXComponent_TouchPointToolType sourceToolType = 44 OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN; 45 }; 46 47 struct OH_NativeXComponent_KeyEvent { 48 OH_NativeXComponent_KeyAction action = OH_NativeXComponent_KeyAction::OH_NATIVEXCOMPONENT_KEY_ACTION_UNKNOWN; 49 OH_NativeXComponent_KeyCode code = OH_NativeXComponent_KeyCode::KEY_UNKNOWN; 50 OH_NativeXComponent_EventSourceType sourceType = 51 OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_UNKNOWN; 52 int64_t deviceId {}; 53 int64_t timestamp {}; 54 }; 55 56 using OnTouchIntercept_Callback = HitTestMode (*)(OH_NativeXComponent*, ArkUI_UIInputEvent*); 57 58 namespace OHOS::Ace { 59 class NativeXComponentImpl : public virtual AceType { 60 DECLARE_ACE_TYPE(NativeXComponentImpl, AceType); 61 using NativeXComponent_Callback = void (*)(OH_NativeXComponent*, void*); 62 using NativeXComponent_CallbackWithResult = bool (*)(OH_NativeXComponent*, void*); 63 using NativeXComponent_UIEventCallback = void (*)( 64 OH_NativeXComponent*, ArkUI_UIInputEvent*, ArkUI_UIInputEvent_Type); 65 using SetExpectedRateRangeEvent_Callback = std::function<void()>; 66 using SetOnFrameEvent_Callback = std::function<void()>; 67 using SetUnregisterOnFrameEvent_Callback = std::function<void()>; 68 using OnFrame_Callback = void (*)(OH_NativeXComponent*, uint64_t, uint64_t); 69 using NativeNode_Callback = void (*)(void*, void*); 70 71 public: NativeXComponentImpl()72 NativeXComponentImpl() 73 { 74 accessbilityProvider_ = std::make_shared<ArkUI_AccessibilityProvider>(); 75 } 76 ~NativeXComponentImpl()77 ~NativeXComponentImpl() {} 78 SetXComponentId(const std::string & id)79 void SetXComponentId(const std::string& id) 80 { 81 xcomponentId_ = id; 82 } 83 GetXComponentId()84 const std::string& GetXComponentId() const 85 { 86 return xcomponentId_; 87 } 88 SetXComponentWidth(const int width)89 void SetXComponentWidth(const int width) 90 { 91 width_ = width; 92 } 93 GetXComponentWidth()94 int GetXComponentWidth() const 95 { 96 return width_; 97 } 98 SetXComponentHeight(const int height)99 void SetXComponentHeight(const int height) 100 { 101 height_ = height; 102 } 103 GetXComponentHeight()104 int GetXComponentHeight() const 105 { 106 return height_; 107 } 108 SetXComponentOffsetX(const double x)109 void SetXComponentOffsetX(const double x) 110 { 111 x_ = x; 112 } 113 GetXComponentOffsetX()114 double GetXComponentOffsetX() const 115 { 116 return x_; 117 } 118 SetXComponentOffsetY(const double y)119 void SetXComponentOffsetY(const double y) 120 { 121 y_ = y; 122 } 123 GetXComponentOffsetY()124 double GetXComponentOffsetY() const 125 { 126 return y_; 127 } 128 SetSurface(void * window)129 void SetSurface(void* window) 130 { 131 window_ = window; 132 } 133 GetSurface()134 const void* GetSurface() const 135 { 136 return window_; 137 } 138 SetCallback(OH_NativeXComponent_Callback * callback)139 void SetCallback(OH_NativeXComponent_Callback* callback) 140 { 141 callback_ = callback; 142 } 143 GetCallback()144 const OH_NativeXComponent_Callback* GetCallback() const 145 { 146 return callback_; 147 } 148 SetMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback * callback)149 void SetMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback* callback) 150 { 151 mouseEventCallback_ = callback; 152 } 153 GetMouseEventCallback()154 const OH_NativeXComponent_MouseEvent_Callback* GetMouseEventCallback() 155 { 156 return mouseEventCallback_; 157 } 158 GetSurfaceShowCallback()159 NativeXComponent_Surface_Callback GetSurfaceShowCallback() const 160 { 161 return surfaceShowCallback_; 162 } 163 SetSurfaceShowCallback(NativeXComponent_Surface_Callback callback)164 void SetSurfaceShowCallback(NativeXComponent_Surface_Callback callback) 165 { 166 surfaceShowCallback_ = callback; 167 } 168 GetSurfaceHideCallback()169 NativeXComponent_Surface_Callback GetSurfaceHideCallback() const 170 { 171 return surfaceHideCallback_; 172 } 173 SetSurfaceHideCallback(NativeXComponent_Surface_Callback callback)174 void SetSurfaceHideCallback(NativeXComponent_Surface_Callback callback) 175 { 176 surfaceHideCallback_ = callback; 177 } 178 SetTouchEvent(const OH_NativeXComponent_TouchEvent touchEvent)179 void SetTouchEvent(const OH_NativeXComponent_TouchEvent touchEvent) 180 { 181 touchEvent_ = touchEvent; 182 } 183 SetTouchPoint(const std::vector<XComponentTouchPoint> & xComponentTouchPoints)184 void SetTouchPoint(const std::vector<XComponentTouchPoint>& xComponentTouchPoints) 185 { 186 touchPoints_ = xComponentTouchPoints; 187 } 188 SetHistoricalPoint(const std::vector<OH_NativeXComponent_HistoricalPoint> & historicalPoints)189 void SetHistoricalPoint(const std::vector<OH_NativeXComponent_HistoricalPoint>& historicalPoints) 190 { 191 historicalPoints_ = historicalPoints; 192 } 193 SetKeyEvent(const OH_NativeXComponent_KeyEvent keyEvent)194 void SetKeyEvent(const OH_NativeXComponent_KeyEvent keyEvent) 195 { 196 keyEvent_ = keyEvent; 197 } 198 SetMouseEvent(const OH_NativeXComponent_MouseEvent mouseEvent)199 void SetMouseEvent(const OH_NativeXComponent_MouseEvent mouseEvent) 200 { 201 mouseEvent_ = mouseEvent; 202 } 203 GetTouchEvent()204 const OH_NativeXComponent_TouchEvent GetTouchEvent() const 205 { 206 return touchEvent_; 207 } 208 GetHistoryPoints(int32_t * size,OH_NativeXComponent_HistoricalPoint ** historicalPoints)209 void GetHistoryPoints(int32_t* size, OH_NativeXComponent_HistoricalPoint** historicalPoints) 210 { 211 int32_t historicalPointsSize = (int32_t)(historicalPoints_.size()); 212 *size = historicalPointsSize; 213 *historicalPoints = &(historicalPoints_[0]); 214 } 215 GetHistoryPointsSize(int32_t * size)216 void GetHistoryPointsSize(int32_t* size) const 217 { 218 *size = (int32_t)(historicalPoints_.size()); 219 } 220 GetMouseEvent()221 const OH_NativeXComponent_MouseEvent GetMouseEvent() const 222 { 223 return mouseEvent_; 224 } 225 SetToolType(size_t pointIndex,OH_NativeXComponent_TouchPointToolType toolType)226 void SetToolType(size_t pointIndex, OH_NativeXComponent_TouchPointToolType toolType) 227 { 228 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 229 return; 230 } 231 touchPoints_[pointIndex].sourceToolType = toolType; 232 } 233 GetToolType(size_t pointIndex)234 OH_NativeXComponent_TouchPointToolType GetToolType(size_t pointIndex) const 235 { 236 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 237 return OH_NativeXComponent_TouchPointToolType::OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN; 238 } 239 return touchPoints_[pointIndex].sourceToolType; 240 } 241 GetTiltX(size_t pointIndex)242 float GetTiltX(size_t pointIndex) const 243 { 244 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 245 return 0.0f; 246 } 247 return touchPoints_[pointIndex].tiltX; 248 } 249 GetTiltY(size_t pointIndex)250 float GetTiltY(size_t pointIndex) const 251 { 252 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 253 return 0.0f; 254 } 255 return touchPoints_[pointIndex].tiltY; 256 } 257 GetWindowX(size_t pointIndex)258 float GetWindowX(size_t pointIndex) const 259 { 260 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 261 return 0.0f; 262 } 263 return touchPoints_[pointIndex].windowX; 264 } 265 GetWindowY(size_t pointIndex)266 float GetWindowY(size_t pointIndex) const 267 { 268 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 269 return 0.0f; 270 } 271 return touchPoints_[pointIndex].windowY; 272 } 273 GetDisplayX(size_t pointIndex)274 float GetDisplayX(size_t pointIndex) const 275 { 276 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 277 return 0.0f; 278 } 279 return touchPoints_[pointIndex].displayX; 280 } 281 GetDisplayY(size_t pointIndex)282 float GetDisplayY(size_t pointIndex) const 283 { 284 if (pointIndex >= OH_MAX_TOUCH_POINTS_NUMBER || pointIndex >= touchPoints_.size()) { 285 return 0.0f; 286 } 287 return touchPoints_[pointIndex].displayY; 288 } 289 GetKeyEvent()290 OH_NativeXComponent_KeyEvent* GetKeyEvent() 291 { 292 return &keyEvent_; 293 } 294 GetAccessbilityProvider()295 std::shared_ptr<ArkUI_AccessibilityProvider> GetAccessbilityProvider() 296 { 297 return accessbilityProvider_; 298 } 299 GetFocusEventCallback()300 NativeXComponent_Callback GetFocusEventCallback() const 301 { 302 return focusEventCallback_; 303 } 304 GetKeyEventCallback()305 NativeXComponent_Callback GetKeyEventCallback() const 306 { 307 return keyEventCallback_; 308 } 309 GetKeyEventCallbackWithResult()310 NativeXComponent_CallbackWithResult GetKeyEventCallbackWithResult() const 311 { 312 return keyEventCallbackWithResult_; 313 } 314 GetBlurEventCallback()315 NativeXComponent_Callback GetBlurEventCallback() const 316 { 317 return blurEventCallback_; 318 } 319 SetOnTouchInterceptCallback(OnTouchIntercept_Callback callback)320 void SetOnTouchInterceptCallback(OnTouchIntercept_Callback callback) 321 { 322 onTouchInterceptCallback_ = callback; 323 } 324 GetOnTouchInterceptCallback()325 OnTouchIntercept_Callback GetOnTouchInterceptCallback() 326 { 327 return onTouchInterceptCallback_; 328 } 329 SetFocusEventCallback(NativeXComponent_Callback callback)330 void SetFocusEventCallback(NativeXComponent_Callback callback) 331 { 332 focusEventCallback_ = callback; 333 } 334 SetKeyEventCallback(NativeXComponent_Callback callback)335 void SetKeyEventCallback(NativeXComponent_Callback callback) 336 { 337 keyEventCallback_ = callback; 338 } 339 SetKeyEventCallbackWithResult(NativeXComponent_CallbackWithResult callback)340 void SetKeyEventCallbackWithResult(NativeXComponent_CallbackWithResult callback) 341 { 342 keyEventCallbackWithResult_ = callback; 343 } 344 SetBlurEventCallback(NativeXComponent_Callback callback)345 void SetBlurEventCallback(NativeXComponent_Callback callback) 346 { 347 blurEventCallback_ = callback; 348 } 349 SetUIAxisEventCallback(NativeXComponent_UIEventCallback callback)350 void SetUIAxisEventCallback(NativeXComponent_UIEventCallback callback) 351 { 352 uiAxisEventCallback_ = callback; 353 } 354 GetUIAxisEventCallback()355 NativeXComponent_UIEventCallback GetUIAxisEventCallback() const 356 { 357 return uiAxisEventCallback_; 358 } 359 SetOnFrameCallback(OnFrame_Callback callback)360 void SetOnFrameCallback(OnFrame_Callback callback) 361 { 362 onFrameCallback_ = callback; 363 } 364 GetOnFrameCallback()365 OnFrame_Callback GetOnFrameCallback() 366 { 367 return onFrameCallback_; 368 } 369 GetRateRange()370 OH_NativeXComponent_ExpectedRateRange* GetRateRange() 371 { 372 return rateRange_; 373 } 374 SetRateRange(OH_NativeXComponent_ExpectedRateRange * rateRange)375 void SetRateRange(OH_NativeXComponent_ExpectedRateRange* rateRange) 376 { 377 rateRange_ = rateRange; 378 } 379 SetExpectedRateRangeEventCallback(SetExpectedRateRangeEvent_Callback callback)380 void SetExpectedRateRangeEventCallback(SetExpectedRateRangeEvent_Callback callback) 381 { 382 setExpectedRateRangeCallback_ = callback; 383 } 384 SetOnFrameEventCallback(SetOnFrameEvent_Callback callback)385 void SetOnFrameEventCallback(SetOnFrameEvent_Callback callback) 386 { 387 setOnFrameEventCallback_ = callback; 388 } 389 SetUnregisterOnFrameEventCallback(SetUnregisterOnFrameEvent_Callback callback)390 void SetUnregisterOnFrameEventCallback(SetUnregisterOnFrameEvent_Callback callback) 391 { 392 setUnregisterOnFrameEventCallback_ = callback; 393 } 394 395 SetExpectedRateRangeEvent_Callback setExpectedRateRangeCallback_; 396 397 SetOnFrameEvent_Callback setOnFrameEventCallback_; 398 399 SetUnregisterOnFrameEvent_Callback setUnregisterOnFrameEventCallback_; 400 registerNativeNodeCallbacks(NativeNode_Callback attach,NativeNode_Callback detach)401 void registerNativeNodeCallbacks(NativeNode_Callback attach, NativeNode_Callback detach) 402 { 403 attachNativeNodeCallback_ = attach; 404 detachNativeNodeCallback_ = detach; 405 } 406 registerContaner(void * container)407 void registerContaner(void* container) 408 { 409 container_ = container; 410 } 411 AttachContainer(void * root)412 void AttachContainer(void* root) 413 { 414 CHECK_NULL_VOID(attachNativeNodeCallback_); 415 attachNativeNodeCallback_(container_, root); 416 } 417 DetachContainer(void * root)418 void DetachContainer(void* root) 419 { 420 CHECK_NULL_VOID(detachNativeNodeCallback_); 421 detachNativeNodeCallback_(container_, root); 422 } 423 SetNeedSoftKeyboard(bool needSoftKeyboard)424 void SetNeedSoftKeyboard(bool needSoftKeyboard) 425 { 426 needSoftKeyboard_ = needSoftKeyboard; 427 } 428 IsNeedSoftKeyboard()429 bool IsNeedSoftKeyboard() const 430 { 431 return needSoftKeyboard_; 432 } 433 SetCurrentSourceType(std::pair<int32_t,OH_NativeXComponent_EventSourceType> && curSourceType)434 void SetCurrentSourceType(std::pair<int32_t, OH_NativeXComponent_EventSourceType>&& curSourceType) 435 { 436 curSourceType_ = std::move(curSourceType); 437 } 438 GetSourceType(int32_t pointId,OH_NativeXComponent_EventSourceType * sourceType)439 bool GetSourceType(int32_t pointId, OH_NativeXComponent_EventSourceType* sourceType) const 440 { 441 if (curSourceType_.first != pointId) { 442 return false; 443 } 444 (*sourceType) = curSourceType_.second; 445 return true; 446 } 447 448 private: 449 std::string xcomponentId_; 450 void* window_ = nullptr; 451 int width_ = 0; 452 int height_ = 0; 453 double x_ = 0.0; 454 double y_ = 0.0; 455 OH_NativeXComponent_TouchEvent touchEvent_ {}; 456 OH_NativeXComponent_MouseEvent mouseEvent_ { .x = 0, .y = 0 }; 457 OH_NativeXComponent_KeyEvent keyEvent_; 458 std::shared_ptr<ArkUI_AccessibilityProvider> accessbilityProvider_; 459 OH_NativeXComponent_Callback* callback_ = nullptr; 460 OH_NativeXComponent_MouseEvent_Callback* mouseEventCallback_ = nullptr; 461 NativeXComponent_Surface_Callback surfaceShowCallback_ = nullptr; 462 NativeXComponent_Surface_Callback surfaceHideCallback_ = nullptr; 463 NativeXComponent_Callback focusEventCallback_ = nullptr; 464 NativeXComponent_Callback keyEventCallback_ = nullptr; 465 NativeXComponent_CallbackWithResult keyEventCallbackWithResult_ = nullptr; 466 NativeXComponent_Callback blurEventCallback_ = nullptr; 467 NativeXComponent_UIEventCallback uiAxisEventCallback_ = nullptr; 468 std::vector<XComponentTouchPoint> touchPoints_; 469 std::vector<OH_NativeXComponent_HistoricalPoint> historicalPoints_; 470 OnFrame_Callback onFrameCallback_ = nullptr; 471 OH_NativeXComponent_ExpectedRateRange* rateRange_ = nullptr; 472 NativeNode_Callback attachNativeNodeCallback_ = nullptr; 473 NativeNode_Callback detachNativeNodeCallback_ = nullptr; 474 OnTouchIntercept_Callback onTouchInterceptCallback_ = nullptr; 475 void* container_ = nullptr; 476 bool needSoftKeyboard_ = false; 477 std::pair<int32_t, OH_NativeXComponent_EventSourceType> curSourceType_ { -1, 478 OH_NativeXComponent_EventSourceType::OH_NATIVEXCOMPONENT_SOURCE_TYPE_UNKNOWN }; 479 }; 480 } // namespace OHOS::Ace 481 482 struct OH_NativeXComponent { OH_NativeXComponentOH_NativeXComponent483 explicit OH_NativeXComponent(OHOS::Ace::NativeXComponentImpl* xComponentImpl) : xcomponentImpl_(xComponentImpl) {} ~OH_NativeXComponentOH_NativeXComponent484 ~OH_NativeXComponent() {} 485 int32_t GetXComponentId(char* id, uint64_t* size); 486 int32_t GetNativeWindow(void** window); 487 int32_t GetXComponentSize(const void* window, uint64_t* width, uint64_t* height); 488 int32_t GetXComponentOffset(const void* window, double* x, double* y); 489 int32_t GetTouchEvent(const void* window, OH_NativeXComponent_TouchEvent* touchEvent); 490 int32_t GetMouseEvent(const void* window, OH_NativeXComponent_MouseEvent* mouseEvent); 491 int32_t GetHistoryPoints(const void* window, int32_t* size, OH_NativeXComponent_HistoricalPoint** historicalPoints); 492 int32_t RegisterCallback(OH_NativeXComponent_Callback* callback); 493 int32_t RegisterMouseEventCallback(OH_NativeXComponent_MouseEvent_Callback* callback); 494 int32_t RegisterSurfaceShowCallback(NativeXComponent_Surface_Callback callback); 495 int32_t RegisterSurfaceHideCallback(NativeXComponent_Surface_Callback callback); 496 int32_t GetToolType(size_t pointIndex, OH_NativeXComponent_TouchPointToolType* toolType); 497 int32_t GetTiltX(size_t pointIndex, float* tiltX); 498 int32_t GetTiltY(size_t pointIndex, float* tiltY); 499 int32_t GetWindowX(size_t pointIndex, float* windowX); 500 int32_t GetWindowY(size_t pointIndex, float* windowY); 501 int32_t GetDisplayX(size_t pointIndex, float* displayX); 502 int32_t GetDisplayY(size_t pointIndex, float* displayY); 503 int32_t RegisterFocusEventCallback(void (*callback)(OH_NativeXComponent* component, void* window)); 504 int32_t RegisterKeyEventCallback(void (*callback)(OH_NativeXComponent* component, void* window)); 505 int32_t RegisterKeyEventCallbackWithResult(bool (*callback)(OH_NativeXComponent* component, void* window)); 506 int32_t RegisterBlurEventCallback(void (*callback)(OH_NativeXComponent* component, void* window)); 507 int32_t GetKeyEvent(OH_NativeXComponent_KeyEvent** keyEvent); 508 int32_t SetExpectedFrameRateRange(OH_NativeXComponent_ExpectedRateRange* range); 509 int32_t RegisterOnFrameCallback( 510 void (*callback)(OH_NativeXComponent* component, uint64_t timestamp, uint64_t targetTimestamp)); 511 int32_t UnregisterOnFrameCallback(); 512 int32_t AttachNativeRootNode(void* root); 513 int32_t DetachNativeRootNode(void* root); 514 int32_t RegisterUIAxisEventCallback( 515 void (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event, ArkUI_UIInputEvent_Type type)); 516 int32_t SetNeedSoftKeyboard(bool needSoftKeyboard); 517 int32_t RegisterOnTouchInterceptCallback( 518 HitTestMode (*callback)(OH_NativeXComponent* component, ArkUI_UIInputEvent* event)); 519 int32_t GetSourceType(int32_t pointId, OH_NativeXComponent_EventSourceType* sourceType); 520 int32_t GetAccessibilityProvider(ArkUI_AccessibilityProvider** handle); 521 522 private: 523 OHOS::Ace::NativeXComponentImpl* xcomponentImpl_ = nullptr; 524 }; 525 526 #endif // _NATIVE_INTERFACE_XCOMPONENT_IMPL_ 527