1 /*
2  * Copyright (c) 2021-2022 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_WINDOW_MANAGER_SERVICE_H
17 #define OHOS_WINDOW_MANAGER_SERVICE_H
18 
19 #include <input_window_monitor.h>
20 #include <map>
21 #include <nocopyable.h>
22 #include <system_ability.h>
23 #include <transaction/rs_interfaces.h>
24 #include <vector>
25 #include <window_manager_service_handler_stub.h>
26 
27 #include "atomic_map.h"
28 #include "display_change_listener.h"
29 #include "drag_controller.h"
30 #include "event_handler.h"
31 #include "freeze_controller.h"
32 #include "perform_reporter.h"
33 #include "singleton_delegator.h"
34 #include "snapshot_controller.h"
35 #include "struct_multimodal.h"
36 #include "window_common_event.h"
37 #include "window_controller.h"
38 #include "window_dumper.h"
39 #include "window_group_mgr.h"
40 #include "window_manager_config.h"
41 #include "window_root.h"
42 #include "window_system_effect.h"
43 #include "wm_common_inner.h"
44 #include "wm_single_instance.h"
45 #include "zidl/window_manager_stub.h"
46 
47 namespace OHOS {
48 namespace Rosen {
49 class DisplayChangeListener : public IDisplayChangeListener {
50 public:
51     virtual void OnDisplayStateChange(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
52         const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type) override;
53     virtual void OnScreenshot(DisplayId displayId) override;
54 };
55 
56 class WindowInfoQueriedListener : public IWindowInfoQueriedListener {
57 public:
58     virtual void HasPrivateWindow(DisplayId id, bool& hasPrivateWindow) override;
59 };
60 
61 class WindowManagerServiceHandler : public AAFwk::WindowManagerServiceHandlerStub {
62 public:
63     virtual void NotifyWindowTransition(
64         sptr<AAFwk::AbilityTransitionInfo> from, sptr<AAFwk::AbilityTransitionInfo> to, bool& animaEnabled) override;
65     int32_t GetFocusWindow(sptr<IRemoteObject>& abilityToken) override;
66     virtual void StartingWindow(
67         sptr<AAFwk::AbilityTransitionInfo> info, std::shared_ptr<Media::PixelMap> pixelMap, uint32_t bgColor) override;
68     virtual void StartingWindow(
69         sptr<AAFwk::AbilityTransitionInfo> info, std::shared_ptr<Media::PixelMap> pixelMap) override;
70     virtual void CancelStartingWindow(sptr<IRemoteObject> abilityToken) override;
71     virtual void NotifyAnimationAbilityDied(sptr<AAFwk::AbilityTransitionInfo> info) override;
72     virtual int32_t MoveMissionsToForeground(const std::vector<int32_t>& missionIds, int32_t topMissionId) override;
73     virtual int32_t MoveMissionsToBackground(
74         const std::vector<int32_t>& missionIds, std::vector<int32_t>& result) override;
75 };
76 
77 class RSUIDirector;
78 class WindowManagerService : public SystemAbility, public WindowManagerStub {
79 friend class DisplayChangeListener;
80 friend class WindowManagerServiceHandler;
81 DECLARE_SYSTEM_ABILITY(WindowManagerService);
82 WM_DECLARE_SINGLE_INSTANCE_BASE(WindowManagerService);
83 
84 public:
85     using Task = std::function<void()>;
86     void OnStart() override;
87     void OnStop() override;
88     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
89     void SetWindowInputEventConsumer();
90     int Dump(int fd, const std::vector<std::u16string>& args) override;
91 
92     WMError CreateWindow(sptr<IWindow>& window, sptr<WindowProperty>& property,
93         const std::shared_ptr<RSSurfaceNode>& surfaceNode,
94         uint32_t& windowId, sptr<IRemoteObject> token) override;
95     WMError AddWindow(sptr<WindowProperty>& property) override;
96     WMError RemoveWindow(uint32_t windowId, bool isFromInnerkits) override;
97     WMError NotifyWindowTransition(sptr<WindowTransitionInfo>& from, sptr<WindowTransitionInfo>& to,
98         bool isFromClient = false) override;
99     void NotifyAnimationAbilityDied(sptr<WindowTransitionInfo> info);
100     WMError DestroyWindow(uint32_t windowId, bool onlySelf = false) override;
101     WMError RequestFocus(uint32_t windowId) override;
102     AvoidArea GetAvoidAreaByType(uint32_t windowId, AvoidAreaType avoidAreaType) override;
103     void NotifyServerReadyToMoveOrDrag(uint32_t windowId, sptr<WindowProperty>& windowProperty,
104         sptr<MoveDragProperty>& moveDragProperty) override;
105     void ProcessPointDown(uint32_t windowId, bool isPointDown) override;
106     void ProcessPointUp(uint32_t windowId) override;
107     WMError GetTopWindowId(uint32_t mainWinId, uint32_t& topWinId) override;
108     WMError MinimizeAllAppWindows(DisplayId displayId) override;
109     WMError ToggleShownStateForAllAppWindows() override;
110     WMError SetWindowLayoutMode(WindowLayoutMode mode) override;
111     WMError UpdateProperty(sptr<WindowProperty>& windowProperty, PropertyChangeAction action,
112         bool isAsyncTask = false) override;
113     WMError SetWindowGravity(uint32_t windowId, WindowGravity gravity, uint32_t percent) override;
114     WMError GetAccessibilityWindowInfo(std::vector<sptr<AccessibilityWindowInfo>>& infos) override;
115     WMError GetUnreliableWindowInfo(int32_t windowId, std::vector<sptr<UnreliableWindowInfo>>& infos) override;
116     WMError GetVisibilityWindowInfo(std::vector<sptr<WindowVisibilityInfo>>& infos) override;
117     WMError RaiseToAppTop(uint32_t windowId) override;
118     std::shared_ptr<Media::PixelMap> GetSnapshot(int32_t windowId) override;
119     WMError SetGestureNavigaionEnabled(bool enable) override;
120     void DispatchKeyEvent(uint32_t windowId, std::shared_ptr<MMI::KeyEvent> event) override;
121     void NotifyDumpInfoResult(const std::vector<std::string>& info) override;
122     WMError GetWindowAnimationTargets(std::vector<uint32_t> missionIds,
123         std::vector<sptr<RSWindowAnimationTarget>>& targets) override;
124     WMError RegisterWindowManagerAgent(WindowManagerAgentType type,
125         const sptr<IWindowManagerAgent>& windowManagerAgent) override;
126     WMError UnregisterWindowManagerAgent(WindowManagerAgentType type,
127         const sptr<IWindowManagerAgent>& windowManagerAgent) override;
128 
129     WMError SetWindowAnimationController(const sptr<RSIWindowAnimationController>& controller) override;
130     WMError GetSystemConfig(SystemConfig& systemConfig) override;
131     WMError GetModeChangeHotZones(DisplayId displayId, ModeChangeHotZones& hotZones) override;
132     WMError UpdateAvoidAreaListener(uint32_t windowId, bool haveAvoidAreaListener) override;
133     void StartingWindow(sptr<WindowTransitionInfo> info, std::shared_ptr<Media::PixelMap> pixelMap,
134         bool isColdStart, uint32_t bkgColor = 0xffffffff);
135     void CancelStartingWindow(sptr<IRemoteObject> abilityToken);
136     WMError MoveMissionsToForeground(const std::vector<int32_t>& missionIds, int32_t topMissionId);
137     WMError MoveMissionsToBackground(const std::vector<int32_t>& missionIds,
138         std::vector<int32_t>& result);
139     void MinimizeWindowsByLauncher(std::vector<uint32_t> windowIds, bool isAnimated,
140         sptr<RSIWindowAnimationFinishedCallback>& finishCallback) override;
141     WMError UpdateRsTree(uint32_t windowId, bool isAdd) override;
142     void OnScreenshot(DisplayId displayId);
143     void OnAccountSwitched(int accountId);
144     WMError BindDialogTarget(uint32_t& windowId, sptr<IRemoteObject> targetToken) override;
145     void HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow);
146     void NotifyWindowClientPointUp(uint32_t windowId, const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
147 
148     void SetAnchorAndScale(int32_t x, int32_t y, float scale) override;
149     void SetAnchorOffset(int32_t deltaX, int32_t deltaY) override;
150     void OffWindowZoom() override;
151     void PostAsyncTask(Task task, const std::string& taskName = "WMSTask", uint32_t delay = 0);
152     void SetMaximizeMode(MaximizeMode maximizeMode) override;
153     MaximizeMode GetMaximizeMode() override;
154     void GetFocusWindowInfo(FocusChangeInfo& focusInfo) override;
155 
156 protected:
157     WindowManagerService();
158     virtual ~WindowManagerService() = default;
159 
160 private:
161     bool Init();
162     void InitWithAbilityManagerServiceAdded();
163     void InitWithRanderServiceAdded();
164     void WindowVisibilityChangeCallback(std::shared_ptr<RSOcclusionData> occlusionData);
165     void OnWindowEvent(Event event, const sptr<IRemoteObject>& remoteObject);
166     void NotifyDisplayStateChange(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
167         const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type);
168     WMError GetFocusWindowInfo(sptr<IRemoteObject>& abilityToken);
169     bool CheckSystemWindowPermission(const sptr<WindowProperty>& property) const;
170     bool CheckAnimationPermission(const sptr<WindowProperty>& property) const;
171     void ConfigureWindowManagerService();
172     void PostVoidSyncTask(Task task, const std::string& taskName = "WMSTask");
173     template<typename SyncTask, typename Return = std::invoke_result_t<SyncTask>>
174     Return PostSyncTask(SyncTask&& task, const std::string& taskName = "WMSTask")
175     {
176         Return ret;
177         std::function<void()> syncTask([&ret, &task]() {ret = task();});
178         if (handler_) {
179             handler_->PostSyncTask(syncTask, "wms:" + taskName, AppExecFwk::EventQueue::Priority::IMMEDIATE);
180         }
181         return ret;
182     }
183     void ConfigHotZones(const std::vector<int>& hotZones);
184     void ConfigDecor(const WindowManagerConfig::ConfigItem& decorConfig);
185     void ConfigWindowAnimation(const WindowManagerConfig::ConfigItem& animeConfig);
186     void ConfigKeyboardAnimation(const WindowManagerConfig::ConfigItem& animeConfig);
187     void ConfigStartingWindowAnimation(const WindowManagerConfig::ConfigItem& animeConfig);
188     RSAnimationTimingCurve CreateCurve(const WindowManagerConfig::ConfigItem& curveConfig);
189     void CreateKeyboardCurve(const WindowManagerConfig::ConfigItem& config,
190         AnimationConfig::KeyboardAnimation& animateConfig, KeyboardAnimationCurve& sysCurveConfig);
191     void RecordShowTimeEvent(int64_t costTime);
192     void ConfigWindowEffect(const WindowManagerConfig::ConfigItem& effectConfig);
193     bool ConfigAppWindowCornerRadius(const WindowManagerConfig::ConfigItem& item, float& out);
194     bool ConfigAppWindowShadow(const WindowManagerConfig::ConfigItem& shadowConfig, WindowShadowParameters& outShadow);
195 
196     static inline SingletonDelegator<WindowManagerService> delegator;
197     std::string name_ = "WindowManagerService";
198     AtomicMap<uint32_t, uint32_t> accessTokenIdMaps_;
199     sptr<WindowRoot> windowRoot_;
200     sptr<WindowController> windowController_;
201     sptr<InputWindowMonitor> inputWindowMonitor_;
202     sptr<SnapshotController> snapshotController_;
203     sptr<WindowManagerServiceHandler> wmsHandler_;
204     sptr<DragController> dragController_;
205     sptr<FreezeController> freezeDisplayController_;
206     sptr<WindowDumper> windowDumper_;
207     sptr<WindowGroupMgr> windowGroupMgr_;
208     SystemConfig systemConfig_;
209     ModeChangeHotZonesConfig hotZonesConfig_ { false, 0, 0, 0 };
210     std::shared_ptr<WindowCommonEvent> windowCommonEvent_;
211     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
212     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
213     RSInterfaces& rsInterface_;
214     bool startingOpen_ = true;
215     std::shared_ptr<RSUIDirector> rsUiDirector_;
216     std::shared_ptr<PerformReporter> windowShowPerformReport_;
217     MaximizeMode maximizeMode_ = MaximizeMode::MODE_RECOVER;
218 };
219 } // namespace Rosen
220 } // namespace OHOS
221 #endif // OHOS_WINDOW_MANAGER_SERVICE_H
222