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