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#ifndef OHOS_ROSEN_WINDOW_ROOT_H
16#define OHOS_ROSEN_WINDOW_ROOT_H
17
18#include <refbase.h>
19#include <iremote_object.h>
20#include <transaction/rs_interfaces.h>
21
22#include "agent_death_recipient.h"
23#include "display_manager_service_inner.h"
24#include "parameters.h"
25#include "window_node_container.h"
26#include "zidl/window_manager_agent_interface.h"
27
28namespace OHOS {
29namespace Rosen {
30enum class Event : uint32_t {
31    REMOTE_DIED,
32};
33
34class WindowRoot : public RefBase {
35using Callback = std::function<void (Event event, const sptr<IRemoteObject>& remoteObject)>;
36
37public:
38    explicit WindowRoot(Callback callback) : callback_(callback) {}
39    ~WindowRoot() = default;
40
41    sptr<WindowNodeContainer> GetOrCreateWindowNodeContainer(DisplayId displayId);
42    sptr<WindowNodeContainer> GetWindowNodeContainer(DisplayId displayId);
43    sptr<WindowNodeContainer> CreateWindowNodeContainer(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo);
44    sptr<WindowNode> GetWindowNode(uint32_t windowId) const;
45    sptr<WindowNode> GetWindowNodeByMissionId(uint32_t missionId) const;
46    void GetBackgroundNodesByScreenId(ScreenId screenGroupId, std::vector<sptr<WindowNode>>& windowNodes);
47    void GetForegroundNodes(std::vector<sptr<WindowNode>>& windowNodes);
48
49    sptr<WindowNode> FindWallpaperWindow();
50    WMError SaveWindow(const sptr<WindowNode>& node);
51    void AddDeathRecipient(sptr<WindowNode> node);
52    sptr<WindowNode> FindWindowNodeWithToken(const sptr<IRemoteObject>& token) const;
53    WMError AddWindowNode(uint32_t parentId, sptr<WindowNode>& node, bool fromStartingWin = false);
54    WMError RemoveWindowNode(uint32_t windowId, bool fromAnimation = false);
55    WMError DestroyWindow(uint32_t windowId, bool onlySelf);
56    WMError UpdateWindowNode(uint32_t windowId, WindowUpdateReason reason);
57    bool IsVerticalDisplay(sptr<WindowNode>& node) const;
58    bool IsForbidDockSliceMove(DisplayId displayId) const;
59    bool IsDockSliceInExitSplitModeArea(DisplayId displayId) const;
60    void ExitSplitMode(DisplayId displayId);
61    void NotifyWindowVisibilityChange(std::shared_ptr<RSOcclusionData> occlusionData);
62    void AddSurfaceNodeIdWindowNodePair(uint64_t surfaceNodeId, sptr<WindowNode> node);
63
64    WMError RequestFocus(uint32_t windowId);
65    WMError RequestActiveWindow(uint32_t windowId);
66    WMError MinimizeStructuredAppWindowsExceptSelf(sptr<WindowNode>& node);
67    AvoidArea GetAvoidAreaByType(uint32_t windowId, AvoidAreaType avoidAreaType);
68    WMError SetWindowMode(sptr<WindowNode>& node, WindowMode dstMode);
69    WMError GetTopWindowId(uint32_t mainWinId, uint32_t& topWinId);
70    void MinimizeAllAppWindows(DisplayId displayId);
71    WMError ToggleShownStateForAllAppWindows();
72    WMError SetWindowLayoutMode(DisplayId displayId, WindowLayoutMode mode);
73
74    void ProcessWindowStateChange(WindowState state, WindowStateChangeReason reason);
75    void ProcessDisplayChange(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
76        const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type);
77    void ProcessDisplayDestroy(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
78        const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap);
79    void ProcessDisplayCreate(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
80        const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap);
81
82    void NotifySystemBarTints();
83    WMError RaiseZOrderForAppWindow(sptr<WindowNode>& node);
84    void DispatchKeyEvent(sptr<WindowNode> node, std::shared_ptr<MMI::KeyEvent> event);
85    void FocusFaultDetection() const;
86    Rect GetDisplayGroupRect(DisplayId displayId) const;
87    WMError UpdateSizeChangeReason(uint32_t windowId, WindowSizeChangeReason reason);
88    void SetBrightness(uint32_t windowId, float brightness);
89    void HandleKeepScreenOn(uint32_t windowId, bool requireLock);
90    void UpdateFocusableProperty(uint32_t windowId);
91    void SetMaxAppWindowNumber(uint32_t windowNum);
92    WMError GetModeChangeHotZones(DisplayId displayId,
93        ModeChangeHotZones& hotZones, const ModeChangeHotZonesConfig& config);
94    std::vector<DisplayId> GetAllDisplayIds() const;
95    uint32_t GetTotalWindowNum() const;
96    uint32_t GetWindowIdByObject(const sptr<IRemoteObject>& remoteObject);
97    sptr<WindowNode> GetWindowForDumpAceHelpInfo() const;
98    void DestroyLeakStartingWindow();
99    void SetSplitRatios(const std::vector<float>& splitRatioNumbers);
100    std::vector<sptr<WindowNode>> GetSplitScreenWindowNodes(DisplayId displayId);
101    void SetExitSplitRatios(const std::vector<float>& exitSplitRatios);
102    void MinimizeTargetWindows(std::vector<uint32_t>& windowIds);
103    WMError UpdateRsTree(uint32_t windowId, bool isAdd);
104    void RemoveSingleUserWindowNodes(int accountId);
105    sptr<WindowNode> FindMainWindowWithToken(sptr<IRemoteObject> token);
106    bool CheckMultiDialogWindows(WindowType type, sptr<IRemoteObject> token);
107    bool HasPrivateWindow(DisplayId displayId);
108    Rect GetDisplayRectWithoutSystemBarAreas(const sptr<WindowNode> destNode);
109    sptr<WindowNode> GetWindowNodeByAbilityToken(const sptr<IRemoteObject>& abilityToken);
110    bool TakeWindowPairSnapshot(DisplayId displayId);
111    void ClearWindowPairSnapshot(DisplayId displayId);
112    void LayoutWhenAddWindowNode(sptr<WindowNode>& node, bool afterAnimation = false);
113    void GetAllAnimationPlayingNodes(std::vector<wptr<WindowNode>>& windowNodes);
114    void GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>>& infos) const;
115    void GetUnreliableWindowInfo(int32_t windowId, std::vector<sptr<UnreliableWindowInfo>>& infos) const;
116    WMError NotifyDesktopUnfrozen();
117    void UpdateDisplayOrientationWhenHideWindow(sptr<WindowNode>& node);
118    bool HasMainFullScreenWindowShown(DisplayId displayId);
119    WMError SetGestureNavigationEnabled(bool enable);
120
121private:
122    void OnRemoteDied(const sptr<IRemoteObject>& remoteObject);
123    WMError DestroyWindowInner(sptr<WindowNode>& node);
124    WMError DestroyWindowSelf(sptr<WindowNode>& node, const sptr<WindowNodeContainer>& container);
125    WMError DestroyWindowWithChild(sptr<WindowNode>& node, const sptr<WindowNodeContainer>& container);
126    void UpdateFocusWindowWithWindowRemoved(const sptr<WindowNode>& node,
127        const sptr<WindowNodeContainer>& container) const;
128    void UpdateActiveWindowWithWindowRemoved(const sptr<WindowNode>& node,
129        const sptr<WindowNodeContainer>& container) const;
130    void UpdateBrightnessWithWindowRemoved(uint32_t windowId, const sptr<WindowNodeContainer>& container) const;
131    std::string GenAllWindowsLogInfo() const;
132    bool CheckDisplayInfo(const sptr<DisplayInfo>& display);
133    ScreenId GetScreenGroupId(DisplayId displayId, bool& isRecordedDisplay);
134    void ProcessExpandDisplayCreate(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
135        std::map<DisplayId, Rect>& displayRectMap);
136    std::map<DisplayId, Rect> GetAllDisplayRectsByDMS(sptr<DisplayInfo> displayInfo);
137    std::map<DisplayId, Rect> GetAllDisplayRectsByDisplayInfo(
138        const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap);
139    void MoveNotShowingWindowToDefaultDisplay(DisplayId defaultDisplayId, DisplayId displayId);
140    WMError PostProcessAddWindowNode(sptr<WindowNode>& node, sptr<WindowNode>& parentNode,
141        sptr<WindowNodeContainer>& container);
142    std::vector<std::pair<uint64_t, WindowVisibilityState>> GetWindowVisibilityChangeInfo(
143        std::shared_ptr<RSOcclusionData> occlusionData);
144    bool CheckAddingModeAndSize(sptr<WindowNode>& node, const sptr<WindowNodeContainer>& container);
145    WMError BindDialogToParent(sptr<WindowNode>& node, sptr<WindowNode>& parentNode);
146    void CheckAndNotifyWaterMarkChangedResult();
147
148    std::map<uint32_t, sptr<WindowNode>> windowNodeMap_;
149    std::map<sptr<IRemoteObject>, uint32_t> windowIdMap_;
150    std::map<uint64_t, sptr<WindowNode>> surfaceIdWindowNodeMap_;
151    std::vector<std::pair<uint64_t, WindowVisibilityState> > lastVisibleData_;
152    std::map<ScreenId, sptr<WindowNodeContainer>> windowNodeContainerMap_;
153    std::map<ScreenId, std::vector<DisplayId>> displayIdMap_;
154    bool lastWaterMarkShowStates_ { false };
155    bool needCheckFocusWindow = false;
156    bool lastGestureNativeEnabled_ { true };
157
158    std::map<WindowManagerAgentType, std::vector<sptr<IWindowManagerAgent>>> windowManagerAgents_;
159
160    sptr<AgentDeathRecipient> windowDeath_ = new AgentDeathRecipient(
161        [this](const sptr<IRemoteObject>& remoteObject) { this->OnRemoteDied(remoteObject); });
162    Callback callback_;
163    uint32_t maxAppWindowNumber_ = 100;
164    SplitRatioConfig splitRatioConfig_ = {0.1, 0.9, {}};
165};
166}
167}
168#endif // OHOS_ROSEN_WINDOW_ROOT_H
169