1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ScreenManager.h"
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/dom/ContentParent.h"
11 #include "mozilla/dom/DOMTypes.h"
12 #include "mozilla/Logging.h"
13 #include "mozilla/StaticPtr.h"
15 # include "mozilla/WidgetUtilsGtk.h"
16 #endif /* MOZ_WAYLAND */
18 static mozilla::LazyLogModule
sScreenLog("WidgetScreen");
20 namespace mozilla::widget
{
22 NS_IMPL_ISUPPORTS(ScreenManager
, nsIScreenManager
)
24 ScreenManager::ScreenManager() = default;
26 ScreenManager::~ScreenManager() = default;
28 static StaticRefPtr
<ScreenManager
> sSingleton
;
30 ScreenManager
& ScreenManager::GetSingleton() {
32 sSingleton
= new ScreenManager();
33 ClearOnShutdown(&sSingleton
);
38 already_AddRefed
<ScreenManager
> ScreenManager::GetAddRefedSingleton() {
39 RefPtr
<ScreenManager
> sm
= &GetSingleton();
43 void ScreenManager::SetHelper(UniquePtr
<Helper
> aHelper
) {
44 mHelper
= std::move(aHelper
);
48 void ScreenManager::Refresh(nsTArray
<RefPtr
<Screen
>>&& aScreens
) {
49 if (PastShutdownPhase(ShutdownPhase::XPCOMShutdown
)) {
50 // We don't refresh screen data if starting XPCOM shutdown path.
51 // GetSingleton returns invalid data since it is freed.
54 MOZ_LOG(sScreenLog
, LogLevel::Debug
, ("Refresh screens"));
55 GetSingleton().RefreshInternal(std::move(aScreens
));
58 void ScreenManager::Refresh(nsTArray
<mozilla::dom::ScreenDetails
>&& aScreens
) {
59 MOZ_LOG(sScreenLog
, LogLevel::Debug
, ("Refresh screens from IPC"));
61 AutoTArray
<RefPtr
<Screen
>, 4> screens
;
62 for (auto& screen
: aScreens
) {
63 screens
.AppendElement(new Screen(screen
));
65 RefreshInternal(std::move(screens
));
68 void ScreenManager::RefreshInternal(nsTArray
<RefPtr
<Screen
>>&& aScreens
) {
69 mScreenList
= std::move(aScreens
);
71 CopyScreensToAllRemotesIfIsParent();
72 if (nsCOMPtr
<nsIObserverService
> s
= services::GetObserverService()) {
73 s
->NotifyObservers(nullptr, "screen-information-changed", nullptr);
77 template <class Range
>
78 void ScreenManager::CopyScreensToRemoteRange(Range aRemoteRange
) {
79 AutoTArray
<dom::ScreenDetails
, 4> screens
;
80 for (auto& screen
: mScreenList
) {
81 screens
.AppendElement(screen
->ToScreenDetails());
83 for (auto cp
: aRemoteRange
) {
84 MOZ_LOG(sScreenLog
, LogLevel::Debug
,
85 ("Send screens to [Pid %d]", cp
->Pid()));
86 if (!cp
->SendRefreshScreens(screens
)) {
87 MOZ_LOG(sScreenLog
, LogLevel::Error
,
88 ("SendRefreshScreens to [Pid %d] failed", cp
->Pid()));
93 void ScreenManager::CopyScreensToRemote(dom::ContentParent
* aContentParent
) {
94 MOZ_ASSERT(aContentParent
);
95 MOZ_ASSERT(XRE_IsParentProcess());
97 auto range
= {aContentParent
};
98 CopyScreensToRemoteRange(range
);
101 void ScreenManager::CopyScreensToAllRemotesIfIsParent() {
102 if (XRE_IsContentProcess()) {
106 MOZ_LOG(sScreenLog
, LogLevel::Debug
, ("Refreshing all ContentParents"));
108 CopyScreensToRemoteRange(
109 dom::ContentParent::AllProcesses(dom::ContentParent::eLive
));
112 // Returns the screen that contains the rectangle. If the rect overlaps
113 // multiple screens, it picks the screen with the greatest area of intersection.
115 // The coordinates are in desktop pixels.
118 ScreenManager::ScreenForRect(int32_t aX
, int32_t aY
, int32_t aWidth
,
119 int32_t aHeight
, nsIScreen
** aOutScreen
) {
120 #if defined(MOZ_WAYLAND) && defined(MOZ_LOGGING)
121 static bool inWayland
= GdkIsWaylandDisplay();
123 MOZ_LOG(sScreenLog
, LogLevel::Warning
,
124 ("Getting screen in wayland, primary display will be returned."));
128 if (mScreenList
.IsEmpty()) {
129 MOZ_LOG(sScreenLog
, LogLevel::Warning
,
130 ("No screen available. This can happen in xpcshell."));
131 auto screen
= MakeRefPtr
<Screen
>(
132 LayoutDeviceIntRect(), LayoutDeviceIntRect(), 0, 0, 0,
133 DesktopToLayoutDeviceScale(), CSSToLayoutDeviceScale(), 96 /* dpi */,
134 Screen::IsPseudoDisplay::No
, hal::ScreenOrientation::None
, 0);
135 screen
.forget(aOutScreen
);
139 // Optimize for the common case. If the number of screens is only
140 // one then just return the primary screen.
141 if (mScreenList
.Length() == 1) {
142 return GetPrimaryScreen(aOutScreen
);
145 // which screen should we return?
146 Screen
* which
= mScreenList
[0].get();
148 // walk the list of screens and find the one that has the most
151 DesktopIntRect
windowRect(aX
, aY
, aWidth
, aHeight
);
152 for (auto& screen
: mScreenList
) {
153 int32_t x
, y
, width
, height
;
154 x
= y
= width
= height
= 0;
155 screen
->GetRectDisplayPix(&x
, &y
, &width
, &height
);
156 // calculate the surface area
157 DesktopIntRect
screenRect(x
, y
, width
, height
);
158 screenRect
.IntersectRect(screenRect
, windowRect
);
159 uint32_t tempArea
= screenRect
.Area();
160 if (tempArea
> area
) {
161 which
= screen
.get();
166 // If the rect intersects one or more screen,
167 // return the screen that has the largest intersection.
169 RefPtr
<Screen
> ret
= which
;
170 ret
.forget(aOutScreen
);
174 // If the rect does not intersect a screen, find
175 // a screen that is nearest to the rect.
176 uint32_t distance
= UINT32_MAX
;
177 for (auto& screen
: mScreenList
) {
178 int32_t x
, y
, width
, height
;
179 x
= y
= width
= height
= 0;
180 screen
->GetRectDisplayPix(&x
, &y
, &width
, &height
);
182 uint32_t distanceX
= 0;
183 if (aX
> (x
+ width
)) {
184 distanceX
= aX
- (x
+ width
);
185 } else if ((aX
+ aWidth
) < x
) {
186 distanceX
= x
- (aX
+ aWidth
);
189 uint32_t distanceY
= 0;
190 if (aY
> (y
+ height
)) {
191 distanceY
= aY
- (y
+ height
);
192 } else if ((aY
+ aHeight
) < y
) {
193 distanceY
= y
- (aY
+ aHeight
);
196 uint32_t tempDistance
= distanceX
* distanceX
+ distanceY
* distanceY
;
197 if (tempDistance
< distance
) {
198 which
= screen
.get();
199 distance
= tempDistance
;
206 RefPtr
<Screen
> ret
= which
;
207 ret
.forget(aOutScreen
);
211 // The screen with the menubar/taskbar. This shouldn't be needed very
214 already_AddRefed
<Screen
> ScreenManager::GetPrimaryScreen() {
215 if (mScreenList
.IsEmpty()) {
216 MOZ_LOG(sScreenLog
, LogLevel::Warning
,
217 ("No screen available. This can happen in xpcshell."));
218 return MakeAndAddRef
<Screen
>(
219 LayoutDeviceIntRect(), LayoutDeviceIntRect(), 0, 0, 0,
220 DesktopToLayoutDeviceScale(), CSSToLayoutDeviceScale(), 96 /* dpi */,
221 Screen::IsPseudoDisplay::No
, hal::ScreenOrientation::None
, 0);
224 return do_AddRef(mScreenList
[0]);
228 ScreenManager::GetPrimaryScreen(nsIScreen
** aPrimaryScreen
) {
229 nsCOMPtr
<nsIScreen
> screen
= GetPrimaryScreen();
230 screen
.forget(aPrimaryScreen
);
235 ScreenManager::GetTotalScreenPixels(int64_t* aTotalScreenPixels
) {
236 MOZ_ASSERT(aTotalScreenPixels
);
238 if (mScreenList
.IsEmpty()) {
239 MOZ_LOG(sScreenLog
, LogLevel::Warning
,
240 ("No screen available. This can happen in xpcshell."));
241 *aTotalScreenPixels
= 0;
246 for (auto& screen
: mScreenList
) {
247 int32_t x
, y
, width
, height
;
248 x
= y
= width
= height
= 0;
249 screen
->GetRect(&x
, &y
, &width
, &height
);
250 pixels
+= width
* height
;
253 *aTotalScreenPixels
= pixels
;
257 } // namespace mozilla::widget