• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_EVENT_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_EVENT_MANAGER_H
18 
19 #include <unordered_map>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/memory/referenced.h"
23 #include "core/common/event_dump.h"
24 #include "core/common/key_event_manager.h"
25 #include "core/components/common/layout/constants.h"
26 #include "core/components_ng/event/response_ctrl.h"
27 #include "core/components_ng/gestures/gesture_referee.h"
28 #include "core/components_ng/gestures/recognizers/gesture_recognizer.h"
29 #include "core/event/axis_event.h"
30 #include "core/event/key_event.h"
31 #include "core/event/mouse_event.h"
32 #include "core/event/pointer_event.h"
33 #include "core/event/non_pointer_event.h"
34 #include "core/event/rotation_event.h"
35 #include "core/event/touch_event.h"
36 #include "core/focus/focus_node.h"
37 #include "core/gestures/gesture_referee.h"
38 #include "core/event/resample_algo.h"
39 
40 namespace OHOS::Ace {
41 namespace NG {
42 class FrameNode;
43 class SelectOverlayManager;
44 class ResponseCtrl;
45 } // namespace NG
46 class RenderNode;
47 class Element;
48 class TextOverlayManager;
49 using MouseHoverTestList = std::list<WeakPtr<RenderNode>>;
50 using OutOfRectGetRectCallback = std::function<void(std::vector<Rect>&)>;
51 using OutOfRectTouchCallback = std::function<void(void)>;
52 using OutOfRectMouseCallback = std::function<void(void)>;
53 
54 struct RectCallback final {
RectCallbackfinal55     RectCallback(OutOfRectGetRectCallback rectGetCallback, OutOfRectTouchCallback touchCallback,
56         OutOfRectMouseCallback mouseCallback)
57         : rectGetCallback(std::move(rectGetCallback)), touchCallback(std::move(touchCallback)),
58           mouseCallback(std::move(mouseCallback))
59     {}
60     ~RectCallback() = default;
61     OutOfRectGetRectCallback rectGetCallback;
62     OutOfRectTouchCallback touchCallback;
63     OutOfRectMouseCallback mouseCallback;
64 };
65 
66 struct MarkProcessedEventInfo {
67     int32_t eventId = -1;
68     int64_t lastLogTimeStamp = 0;
69 };
70 
71 class EventManager : virtual public NG::KeyEventManager {
72     DECLARE_ACE_TYPE(EventManager, KeyEventManager);
73 
74 public:
75     EventManager();
76     ~EventManager() override = default;
77     // After the touch down event is triggered, the touch test is performed to collect the corresponding
78     // touch event target list.
79     void TouchTest(const TouchEvent& touchPoint, const RefPtr<RenderNode>& renderNode,
80         TouchRestrict& touchRestrict, const Offset& offset = Offset(),
81         float viewScale = 1.0f, bool needAppend = false);
82 
83     void TouchTest(const TouchEvent& touchPoint, const RefPtr<NG::FrameNode>& frameNode,
84         TouchRestrict& touchRestrict, const Offset& offset = Offset(),
85         float viewScale = 1.0f, bool needAppend = false);
86 
87     bool PostEventTouchTest(const TouchEvent& touchPoint, const RefPtr<NG::UINode>& uiNode,
88         TouchRestrict& touchRestrict);
89 
90     void TouchTest(const AxisEvent& event, const RefPtr<RenderNode>& renderNode, TouchRestrict& touchRestrict);
91 
92     void TouchTest(const AxisEvent& event, const RefPtr<NG::FrameNode>& frameNode, TouchRestrict& touchRestrict);
93 
94     bool HasDifferentDirectionGesture();
95 
96     bool OnNonPointerEvent(const NonPointerEvent& event);
97     ACE_NON_VIRTUAL bool DispatchTouchEvent(const TouchEvent& point, bool sendOnTouch = true);
98     bool DispatchTouchEvent(const AxisEvent& event, bool sendOnTouch = true);
99     void DispatchTouchCancelToRecognizer(
100         TouchEventTarget* touchEventTarget, const std::vector<std::pair<int32_t, TouchTestResult::iterator>>& items);
101     bool PostEventDispatchTouchEvent(const TouchEvent& point);
102     void FlushTouchEventsBegin(const std::list<TouchEvent>& touchEvents);
103     void FlushTouchEventsEnd(const std::list<TouchEvent>& touchEvents);
104     void PostEventFlushTouchEventEnd(const TouchEvent& touchEvent);
105 
106     // Distribute the rotation event to the corresponding render tree or requested render node. If the render is not
107     // processed, return false and the platform will handle it.
108     static bool DispatchRotationEvent(
109         const RotationEvent& event, const RefPtr<RenderNode>& renderNode, const RefPtr<RenderNode>& requestFocusNode);
110 
111     // mouse event target list.
112     void MouseTest(const MouseEvent& touchPoint, const RefPtr<RenderNode>& renderNode);
113     bool DispatchMouseEvent(const MouseEvent& event);
114     void DispatchMouseHoverAnimation(const MouseEvent& event);
115     bool DispatchMouseHoverEvent(const MouseEvent& event);
116 
117     void LogPrintMouseTest();
118     void MouseTest(const MouseEvent& event, const RefPtr<NG::FrameNode>& frameNode, TouchRestrict& touchRestrict);
119     void AccessibilityHoverTest(
120         const TouchEvent& event, const RefPtr<NG::FrameNode>& frameNode, TouchRestrict& touchRestrict);
121     void UpdateAccessibilityHoverNode(const TouchEvent& event, const TouchTestResult& testResult);
122     void PenHoverTest(const TouchEvent& event, const RefPtr<NG::FrameNode>& frameNode, TouchRestrict& touchRestrict);
123     void UpdatePenHoverNode(const TouchEvent& event, const TouchTestResult& testResult);
124     void UpdatePenHoverMoveNode(const TouchEvent& event, const TouchTestResult& testResult);
125     void UpdateHoverNode(const MouseEvent& event, const TouchTestResult& testResult);
126     bool DispatchMouseEventNG(const MouseEvent& event);
127     void DispatchMouseHoverAnimationNG(const MouseEvent& event);
128     bool DispatchMouseHoverEventNG(const MouseEvent& event);
129     void DispatchHoverEffectEvent(const MouseEvent& event);
130     void DispatchAccessibilityHoverEventNG(const TouchEvent& event);
131     void DispatchPenHoverEventNG(const TouchEvent& event);
132     void DispatchPenHoverMoveEventNG(const TouchEvent& event);
133 
134     void AxisTest(const AxisEvent& event, const RefPtr<RenderNode>& renderNode);
135     bool DispatchAxisEvent(const AxisEvent& event);
136 
137     void AxisTest(const AxisEvent& event, const RefPtr<NG::FrameNode>& frameNode);
138     bool DispatchAxisEventNG(const AxisEvent& event);
139 
140     void ClearResults();
SetInstanceId(int32_t instanceId)141     void SetInstanceId(int32_t instanceId)
142     {
143         instanceId_ = instanceId;
144     }
GetInstanceId()145     int32_t GetInstanceId() override
146     {
147         return instanceId_;
148     }
149     void HandleGlobalEvent(const TouchEvent& touchPoint, const RefPtr<TextOverlayManager>& textOverlayManager);
150     void HandleGlobalEventNG(const TouchEvent& touchPoint, const RefPtr<NG::SelectOverlayManager>& selectOverlayManager,
151         const NG::OffsetF& rootOffset);
152 
153     void CollectTabIndexNodes(const RefPtr<FocusNode>& rootNode);
154 
155     void AdjustTabIndexNodes();
156 
157     bool HandleFocusByTabIndex(
158         const KeyEvent& event, const RefPtr<FocusNode>& focusNode, const RefPtr<FocusGroup>& curPage);
159 
160     void HandleOutOfRectCallback(const Point& point, std::vector<RectCallback>& rectCallbackList);
161 
GetGestureReferee()162     RefPtr<GestureReferee> GetGestureReferee()
163     {
164         return referee_;
165     }
166 
GetGestureRefereeNG(const RefPtr<NG::NGGestureRecognizer> & recognizer)167     RefPtr<NG::GestureReferee> GetGestureRefereeNG(const RefPtr<NG::NGGestureRecognizer>& recognizer)
168     {
169         if (recognizer && recognizer->IsPostEventResult()) {
170             return postEventRefereeNG_;
171         }
172         return refereeNG_;
173     }
174 
GetMouseStyleManager()175     RefPtr<MouseStyleManager> GetMouseStyleManager() const
176     {
177         return mouseStyleManager_;
178     }
179 
FlushCursorStyleRequests()180     void FlushCursorStyleRequests()
181     {
182         CHECK_NULL_VOID(mouseStyleManager_);
183         mouseStyleManager_->VsyncMouseFormat();
184     }
185 
186     bool TryResampleTouchEvent(std::vector<TouchEvent>& history,
187         const std::vector<TouchEvent>& current, uint64_t nanoTimeStamp, TouchEvent& resample);
188 
189     bool GetResampleTouchEvent(const std::vector<TouchEvent>& history,
190         const std::vector<TouchEvent>& current, uint64_t nanoTimeStamp, TouchEvent& newTouchEvent);
191 
192     TouchEvent GetLatestPoint(const std::vector<TouchEvent>& current, uint64_t nanoTimeStamp);
193 
194     DragPointerEvent GetResamplePointerEvent(const std::vector<DragPointerEvent>& history,
195         const std::vector<DragPointerEvent>& current, uint64_t nanoTimeStamp);
196 
197     DragPointerEvent GetPointerLatestPoint(const std::vector<DragPointerEvent>& current, uint64_t nanoTimeStamp);
198 
199     MouseEvent GetResampleMouseEvent(
200         const std::vector<MouseEvent>& history, const std::vector<MouseEvent>& current, uint64_t nanoTimeStamp);
201 
202     MouseEvent GetMouseLatestPoint(const std::vector<MouseEvent>& current, uint64_t nanoTimeStamp);
203 
204     void DoMouseActionRelease();
205 
SetIsDragging(bool isDragging)206     void SetIsDragging(bool isDragging)
207     {
208         isDragging_ = isDragging;
209     }
210 
IsDragging()211     bool IsDragging() const
212     {
213         return isDragging_;
214     }
215 
SetLastMoveBeforeUp(bool isLastMoveBeforeUp)216     void SetLastMoveBeforeUp(bool isLastMoveBeforeUp)
217     {
218         isLastMoveBeforeUp_ = isLastMoveBeforeUp;
219     }
220 
IsLastMoveBeforeUp()221     bool IsLastMoveBeforeUp() const
222     {
223         return isLastMoveBeforeUp_;
224     }
225 
GetEventTreeRecord(NG::EventTreeType treeType)226     NG::EventTreeRecord& GetEventTreeRecord(NG::EventTreeType treeType)
227     {
228         switch (treeType) {
229             case NG::EventTreeType::TOUCH :
230                 return eventTree_;
231             case NG::EventTreeType::POST_EVENT :
232                 return postEventTree_;
233         }
234     }
235 
236     void DumpEvent(NG::EventTreeType type, bool hasJson = false);
237 
238     void AddGestureSnapshot(
239         int32_t finger, int32_t depth, const RefPtr<TouchEventTarget>& target, NG::EventTreeType type);
240 
GetResponseCtrl()241     RefPtr<NG::ResponseCtrl> GetResponseCtrl()
242     {
243         return responseCtrl_;
244     }
245 
246     void CheckDownEvent(const TouchEvent& touchEvent);
247     void CheckUpEvent(const TouchEvent& touchEvent);
248     std::unordered_map<size_t, TouchTestResult> touchTestResults_;
249     std::unordered_map<size_t, TouchTestResult> postEventTouchTestResults_;
250 
GetAxisTouchTestResults()251     const std::unordered_map<size_t, TouchTestResult>& GetAxisTouchTestResults() const
252     {
253         return axisTouchTestResults_;
254     }
255 
SetAxisTouchTestResults(std::unordered_map<size_t,TouchTestResult> & axisTouchTestResults)256     void SetAxisTouchTestResults(std::unordered_map<size_t, TouchTestResult>& axisTouchTestResults)
257     {
258         axisTouchTestResults_ = axisTouchTestResults;
259     }
260 
SetInnerFlag(bool value)261     void SetInnerFlag(bool value)
262     {
263         innerEventWin_ = value;
264     }
265 
GetInnerFlag()266     bool GetInnerFlag() const
267     {
268         return innerEventWin_;
269     }
270 
GetLastTouchEventEndTimestamp()271     int64_t GetLastTouchEventEndTimestamp()
272     {
273         return lastTouchEventEndTimestamp_;
274     }
275 
276     void RecordHitEmptyMessage(
277         const TouchEvent& touchPoint, const std::string& resultInfo, const RefPtr<NG::FrameNode>& frameNode);
278 
279     void CheckAndLogLastReceivedTouchEventInfo(int32_t eventId, TouchType type);
280 
281     void CheckAndLogLastConsumedTouchEventInfo(int32_t eventId, TouchType type);
282 
283     void CheckAndLogLastReceivedMouseEventInfo(int32_t eventId, MouseAction action);
284 
285     void CheckAndLogLastConsumedMouseEventInfo(int32_t eventId, MouseAction action);
286 
287     void CheckAndLogLastReceivedAxisEventInfo(int32_t eventId, AxisAction action);
288 
289     void CheckAndLogLastConsumedAxisEventInfo(int32_t eventId, AxisAction action);
290 
291     void CheckAndLogLastReceivedEventInfo(int32_t eventId, bool logImmediately = false);
292 
293     void CheckAndLogLastConsumedEventInfo(int32_t eventId, bool logImmediately = false);
294 
295     void ClearTouchTestTargetForPenStylus(TouchEvent& touchEvent);
296 
GetDownFingerIds()297     inline const std::unordered_map<int32_t, int32_t>& GetDownFingerIds() const
298     {
299         return downFingerIds_;
300     }
301 
GetIdToTouchPoint()302     inline const std::unordered_map<int32_t, TouchEvent>& GetIdToTouchPoint() const
303     {
304         return idToTouchPoints_;
305     }
306 
EraseFingerId(int32_t id)307     inline void EraseFingerId(int32_t id)
308     {
309         idToTouchPoints_.erase(id);
310     }
311 
SetIdToTouchPoint(std::unordered_map<int32_t,TouchEvent> && idToTouchPoint)312     inline void SetIdToTouchPoint(std::unordered_map<int32_t, TouchEvent>&& idToTouchPoint)
313     {
314         idToTouchPoints_ = std::move(idToTouchPoint);
315     }
316 
GetLastDispatchTime()317     inline std::unordered_map<int32_t, uint64_t>& GetLastDispatchTime()
318     {
319         return lastDispatchTime_;
320     }
321 
SetLastDispatchTime(std::unordered_map<int32_t,uint64_t> && lastDispatchTime)322     inline void SetLastDispatchTime(std::unordered_map<int32_t, uint64_t>&& lastDispatchTime)
323     {
324         lastDispatchTime_ = std::move(lastDispatchTime);
325     }
326 
327     TouchEvent ConvertAxisEventToTouchEvent(const AxisEvent& axisEvent);
328 
329     void CleanRecognizersForDragBegin(TouchEvent& touchEvent);
330 
331     void CleanHoverStatusForDragBegin();
332 
333     void RegisterDragTouchEventListener(int32_t uniqueIdentify, std::function<void(const TouchEvent&)> callback);
334 
335     void UnRegisterDragTouchEventListener(int32_t uniqueIdentify);
336 
337     void NotifyDragTouchEventListener(const TouchEvent& dragPointerEvent);
338 
339     void AddToMousePendingRecognizers(const WeakPtr<NG::NGGestureRecognizer>& recognizer);
340 
341     template<typename T>
342     bool CheckDifferentTargetDisplay(const std::vector<T>& historyEvents, const std::vector<T>& events);
343 
344 #if defined(SUPPORT_TOUCH_TARGET_TEST)
345     bool TouchTargetHitTest(const TouchEvent& touchPoint, const RefPtr<NG::FrameNode>& frameNode,
346         TouchRestrict& touchRestrict, const Offset& offset = Offset(), float viewScale = 1.0f,
347         bool needAppend = false, const std::string& target = "");
348 #endif
349 private:
350     void SetHittedFrameNode(const std::list<RefPtr<NG::NGGestureRecognizer>>& touchTestResults);
351     void CleanGestureEventHub();
352     void GetTouchTestIds(const TouchEvent& touchPoint, std::vector<std::string>& touchTestIds,
353         bool& isMousePressAtSelectedNode, int32_t selectedNodeId);
354     void CheckMouseTestResults(bool& isMousePressAtSelectedNode, int32_t selectedNodeId);
355     void CleanRefereeBeforeTouchTest(TouchEvent touchPoint, bool needAppend);
356     void LogTouchTestResultInfo(const TouchEvent& touchPoint, const RefPtr<NG::FrameNode>& frameNode,
357         TouchRestrict& touchRestrict, const Offset& offset = Offset(),
358         float viewScale = 1.0f, bool needAppend = false);
359     void LogTouchTestResultRecognizers(const TouchTestResult& result, int32_t touchEventId);
360     void LogTouchTestRecognizerStates(int32_t touchEventId);
361     void CheckRefereeStateAndReTouchTest(const TouchEvent& touchPoint, const RefPtr<NG::FrameNode>& frameNode,
362         TouchRestrict& touchRestrict, const Offset& offset = Offset(),
363         float viewScale = 1.0f, bool needAppend = false);
364     bool DispatchMultiContainerEvent(const TouchEvent& point);
365     void DispatchTouchEventAndCheck(const TouchEvent& event, bool sendOnTouch = true);
366     void DispatchTouchEventInOldPipeline(const TouchEvent& point, bool dispatchSuccess);
367     void DispatchTouchEventToTouchTestResult(TouchEvent touchEvent, TouchTestResult touchTestResult,
368         bool sendOnTouch);
369     void SetResponseLinkRecognizers(const TouchTestResult& result, const ResponseLinkResult& responseLinkRecognizers);
370     void FalsifyCancelEventAndDispatch(const TouchEvent& touchPoint, bool sendOnTouch = true);
371     void FalsifyCancelEventAndDispatch(const AxisEvent& axisEvent, bool sendOnTouch = true);
372     void FalsifyHoverCancelEventAndDispatch(const TouchEvent& touchPoint);
373     void UpdateDragInfo(TouchEvent& point);
374     void UpdateInfoWhenFinishDispatch(const TouchEvent& point, bool sendOnTouch);
375     void DoSingleMouseActionRelease(MouseButton button);
376     bool DispatchMouseEventInGreatOrEqualAPI13(const MouseEvent& event);
377     bool DispatchMouseEventInLessAPI13(const MouseEvent& event);
378     void DispatchMouseEventToPressResults(const MouseEvent& event, const MouseTestResult& targetResults,
379         MouseTestResult& handledResults, bool& isStopPropagation);
380     bool DispatchMouseEventToCurResults(
381         const MouseEvent& event, const MouseTestResult& handledResults, bool isStopPropagation);
382     bool DispatchMouseEventToCurResultsInLessAPI13(
383         const MouseEvent& event, const MouseTestResult& handledResults, bool isStopPropagation);
384     void CheckMousePendingRecognizersState(const TouchEvent& event);
385     bool innerEventWin_ = false;
386     std::unordered_map<size_t, TouchTestResult> mouseTestResults_;
387     MouseTestResult currMouseTestResults_;
388     // used less than API13
389     MouseTestResult pressMouseTestResults_;
390     // used great or equal API13
391     std::unordered_map<MouseButton, MouseTestResult> pressMouseTestResultsMap_;
392     HoverTestResult currHoverTestResults_;
393     HoverTestResult lastHoverTestResults_;
394     HoverTestResult curAccessibilityHoverResults_;
395     HoverTestResult lastAccessibilityHoverResults_;
396     HoverTestResult curPenHoverResults_;
397     HoverTestResult curPenHoverMoveResults_;
398     HoverTestResult lastPenHoverResults_;
399     AxisTestResult axisTestResults_;
400     WeakPtr<NG::FrameNode> lastHoverNode_;
401     WeakPtr<NG::FrameNode> currHoverNode_;
402     std::unordered_map<size_t, TouchTestResult> axisTouchTestResults_;
403     /**
404      * One mechanism to let someone can receive all touch events beyond the controllers and recognizers' dispatching
405      * process. This only can be used for some models which have global status, such as drag and drop manager, it's
406      * handling does not belong to any controller in general
407      */
408     std::unordered_map<int32_t, std::function<void(const TouchEvent&)>> dragTouchEventListener_;
409     MouseHoverTestList mouseHoverTestResults_;
410     MouseHoverTestList mouseHoverTestResultsPre_;
411     WeakPtr<RenderNode> mouseHoverNodePre_;
412     WeakPtr<RenderNode> mouseHoverNode_;
413     WeakPtr<RenderNode> axisNode_;
414     int32_t instanceId_ = 0;
415     uint32_t lastHoverDispatchLength_ = 0;
416     uint32_t lastAccessibilityHoverDispatchLength_ = 0;
417     uint32_t lastPenHoverDispatchLength_ = 0;
418     bool inSelectedRect_ = false;
419     bool isDragging_ = false;
420     bool isLastMoveBeforeUp_ = false;
421     RefPtr<GestureReferee> referee_;
422     RefPtr<NG::GestureReferee> refereeNG_;
423     RefPtr<NG::GestureReferee> postEventRefereeNG_;
424     RefPtr<MouseStyleManager> mouseStyleManager_;
425     NG::EventTreeRecord eventTree_;
426     NG::EventTreeRecord postEventTree_;
427     RefPtr<NG::ResponseCtrl> responseCtrl_;
428     TimeStamp lastEventTime_;
429     int64_t lastTouchEventEndTimestamp_ = 0;
430     std::unordered_map<int32_t, int32_t> downFingerIds_;
431     std::set<WeakPtr<NG::FrameNode>> hittedFrameNode_;
432     MarkProcessedEventInfo lastReceivedEvent_;
433     MarkProcessedEventInfo lastConsumedEvent_;
434     int32_t lastDownFingerNumber_ = 0;
435     SourceTool lastSourceTool_ = SourceTool::UNKNOWN;
436     // used to pseudo cancel event.
437     TouchEvent lastTouchEvent_;
438     // used to pseudo hover out event.
439     MouseEvent lastMouseEvent_;
440     std::unordered_map<int32_t, TouchEvent> idToTouchPoints_;
441     std::unordered_map<int32_t, uint64_t> lastDispatchTime_;
442     std::vector<WeakPtr<NG::NGGestureRecognizer>> mousePendingRecognizers_;
443 };
444 
445 } // namespace OHOS::Ace
446 
447 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_EVENT_MANAGER_H
448