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 OHOS_ROSEN_DRAG_CONTROLLER_H
17 #define OHOS_ROSEN_DRAG_CONTROLLER_H
18 
19 #include <refbase.h>
20 
21 #include "display_group_info.h"
22 #include "display_info.h"
23 #include "event_handler.h"
24 #include "event_runner.h"
25 #include "input_manager.h"
26 #include "pointer_event.h"
27 #include "vsync_station.h"
28 #include "window_root.h"
29 #include "wm_common.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 using EventRunner = OHOS::AppExecFwk::EventRunner;
34 using EventHandler = OHOS::AppExecFwk::EventHandler;
35 /*
36  * DragController is the class which is used to handle drag cross window
37  */
38 class DragController : public RefBase {
39 public:
DragController(sptr<WindowRoot> & root)40     explicit DragController(sptr<WindowRoot>& root) : windowRoot_(root) {}
41     ~DragController() = default;
42     void StartDrag(uint32_t windowId);
43     void UpdateDragInfo(uint32_t windowId);
44     void FinishDrag(uint32_t windowId);
45 private:
46     sptr<WindowNode> GetHitWindow(DisplayId id, const PointInfo point);
47     bool GetHitPoint(uint32_t windowId, PointInfo& point);
48     sptr<WindowRoot> windowRoot_;
49     uint64_t hitWindowId_ = 0;
50 };
51 
52 class DragInputEventListener : public MMI::IInputEventConsumer {
53 public:
54     DragInputEventListener() = default;
55     void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override;
56     void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override;
57     void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override;
58 };
59 
60 /*
61  * MoveDragController is the class which is used to handle move or drag floating window
62  */
63 class MoveDragController : public RefBase {
64 public:
MoveDragController()65     MoveDragController() : windowProperty_(new WindowProperty()), moveDragProperty_(new MoveDragProperty())
66     {
67         vsyncCallback_->onCallback =
68             [this](int64_t timeStamp, int64_t _) { this->OnReceiveVsync(timeStamp); };
69     }
70     ~MoveDragController() = default;
71 
72     bool Init();
73     void Stop();
74     void HandleReadyToMoveOrDrag(uint32_t windowId, sptr<WindowProperty>& windowProperty,
75         sptr<MoveDragProperty>& moveDragProperty);
76     void HandleEndUpMovingOrDragging(uint32_t windowId);
77     void HandleWindowRemovedOrDestroyed(uint32_t windowId);
78     void ConsumePointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
79     uint32_t GetActiveWindowId() const;
80     void HandleDisplayLimitRectChange(const std::map<DisplayId, Rect>& limitRectMap);
81     void SetInputEventConsumer();
82     void SetWindowRoot(const sptr<WindowRoot>& windowRoot);
83 
84 private:
85     void SetDragProperty(const sptr<MoveDragProperty>& moveDragProperty);
86     void SetWindowProperty(const sptr<WindowProperty>& windowProperty);
87     void SetActiveWindowId(uint32_t);
88     const sptr<MoveDragProperty>& GetMoveDragProperty() const;
89     const sptr<WindowProperty>& GetWindowProperty() const;
90     Rect GetHotZoneRect();
91     void ConvertPointerPosToDisplayGroupPos(DisplayId displayId, int32_t& posX, int32_t& posY);
92 
93     void HandlePointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
94     void HandleDragEvent(DisplayId displayId, int32_t posX, int32_t posY, int32_t pointId, int32_t sourceType);
95     void HandleMoveEvent(DisplayId displayId, int32_t posX, int32_t posY, int32_t pointId, int32_t sourceType);
96     void OnReceiveVsync(int64_t timeStamp);
97     void ResetMoveOrDragState();
98     bool CheckWindowRect(DisplayId displayId, float vpr, const Rect& rect);
99     void CalculateNewWindowRect(Rect& newRect, DisplayId displayId, int32_t posX, int32_t posY);
100     std::shared_ptr<VsyncStation> GetVsyncStationByWindowId(uint32_t windowId);
101 
102     sptr<WindowRoot> windowRoot_;
103     sptr<WindowProperty> windowProperty_;
104     sptr<MoveDragProperty> moveDragProperty_;
105     uint32_t activeWindowId_ = INVALID_WINDOW_ID;
106     std::shared_ptr<MMI::PointerEvent> moveEvent_ = nullptr;
107     std::shared_ptr<MMI::IInputEventConsumer> inputListener_ = nullptr;
108     std::shared_ptr<VsyncCallback> vsyncCallback_ = std::make_shared<VsyncCallback>(VsyncCallback());
109     std::map<DisplayId, Rect> limitRectMap_;
110     std::mutex mtx_;
111     std::map<NodeId, std::shared_ptr<VsyncStation>> vsyncStationMap_;
112 
113     // event handler for input event
114     std::shared_ptr<EventHandler> inputEventHandler_;
115     const std::string INNER_WM_INPUT_THREAD_NAME = "InnerInputManager";
116 };
117 }
118 }
119 #endif // OHOS_ROSEN_DRAG_CONTROLLER_H
120 
121