Bug 1855360 - Fix the skip-if syntax. a=bustage-fix
[gecko.git] / widget / ScreenManager.cpp
blob58e20806ebea317ad0da9fa233837d20918d2d07
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"
14 #ifdef MOZ_WAYLAND
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() {
31 if (!sSingleton) {
32 sSingleton = new ScreenManager();
33 ClearOnShutdown(&sSingleton);
35 return *sSingleton;
38 already_AddRefed<ScreenManager> ScreenManager::GetAddRefedSingleton() {
39 RefPtr<ScreenManager> sm = &GetSingleton();
40 return sm.forget();
43 void ScreenManager::SetHelper(UniquePtr<Helper> aHelper) {
44 mHelper = std::move(aHelper);
47 // static
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.
52 return;
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()) {
103 return;
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.
117 NS_IMETHODIMP
118 ScreenManager::ScreenForRect(int32_t aX, int32_t aY, int32_t aWidth,
119 int32_t aHeight, nsIScreen** aOutScreen) {
120 DesktopIntRect rect(aX, aY, aWidth, aHeight);
121 nsCOMPtr<nsIScreen> screen = ScreenForRect(rect);
122 screen.forget(aOutScreen);
123 return NS_OK;
126 already_AddRefed<Screen> ScreenManager::ScreenForRect(
127 const DesktopIntRect& aRect) {
128 #if defined(MOZ_WAYLAND) && defined(MOZ_LOGGING)
129 static bool inWayland = GdkIsWaylandDisplay();
130 if (inWayland) {
131 MOZ_LOG(sScreenLog, LogLevel::Warning,
132 ("Getting screen in wayland, primary display will be returned."));
134 #endif
136 if (mScreenList.IsEmpty()) {
137 MOZ_LOG(sScreenLog, LogLevel::Warning,
138 ("No screen available. This can happen in xpcshell."));
139 auto screen = MakeRefPtr<Screen>(
140 LayoutDeviceIntRect(), LayoutDeviceIntRect(), 0, 0, 0,
141 DesktopToLayoutDeviceScale(), CSSToLayoutDeviceScale(), 96 /* dpi */,
142 Screen::IsPseudoDisplay::No, hal::ScreenOrientation::None, 0);
143 return screen.forget();
146 // Optimize for the common case. If the number of screens is only
147 // one then just return the primary screen.
148 if (mScreenList.Length() == 1) {
149 return GetPrimaryScreen();
152 // which screen should we return?
153 Screen* which = mScreenList[0].get();
155 // walk the list of screens and find the one that has the most
156 // surface area.
157 uint32_t area = 0;
158 for (auto& screen : mScreenList) {
159 int32_t x, y, width, height;
160 x = y = width = height = 0;
161 screen->GetRectDisplayPix(&x, &y, &width, &height);
162 // calculate the surface area
163 DesktopIntRect screenRect(x, y, width, height);
164 screenRect.IntersectRect(screenRect, aRect);
165 uint32_t tempArea = screenRect.Area();
166 if (tempArea > area) {
167 which = screen.get();
168 area = tempArea;
172 // If the rect intersects one or more screen,
173 // return the screen that has the largest intersection.
174 if (area > 0) {
175 RefPtr<Screen> ret = which;
176 return ret.forget();
179 // If the rect does not intersect a screen, find
180 // a screen that is nearest to the rect.
181 uint32_t distance = UINT32_MAX;
182 for (auto& screen : mScreenList) {
183 int32_t x, y, width, height;
184 x = y = width = height = 0;
185 screen->GetRectDisplayPix(&x, &y, &width, &height);
187 uint32_t distanceX = 0;
188 if (aRect.x > (x + width)) {
189 distanceX = aRect.x - (x + width);
190 } else if (aRect.XMost() < x) {
191 distanceX = x - aRect.XMost();
194 uint32_t distanceY = 0;
195 if (aRect.y > (y + height)) {
196 distanceY = aRect.y - (y + height);
197 } else if (aRect.YMost() < y) {
198 distanceY = y - aRect.YMost();
201 uint32_t tempDistance = distanceX * distanceX + distanceY * distanceY;
202 if (tempDistance < distance) {
203 which = screen.get();
204 distance = tempDistance;
205 if (distance == 0) {
206 break;
211 RefPtr<Screen> ret = which;
212 return ret.forget();
215 // The screen with the menubar/taskbar. This shouldn't be needed very
216 // often.
218 already_AddRefed<Screen> ScreenManager::GetPrimaryScreen() {
219 if (mScreenList.IsEmpty()) {
220 MOZ_LOG(sScreenLog, LogLevel::Warning,
221 ("No screen available. This can happen in xpcshell."));
222 return MakeAndAddRef<Screen>(
223 LayoutDeviceIntRect(), LayoutDeviceIntRect(), 0, 0, 0,
224 DesktopToLayoutDeviceScale(), CSSToLayoutDeviceScale(), 96 /* dpi */,
225 Screen::IsPseudoDisplay::No, hal::ScreenOrientation::None, 0);
228 return do_AddRef(mScreenList[0]);
231 NS_IMETHODIMP
232 ScreenManager::GetPrimaryScreen(nsIScreen** aPrimaryScreen) {
233 nsCOMPtr<nsIScreen> screen = GetPrimaryScreen();
234 screen.forget(aPrimaryScreen);
235 return NS_OK;
238 NS_IMETHODIMP
239 ScreenManager::GetTotalScreenPixels(int64_t* aTotalScreenPixels) {
240 MOZ_ASSERT(aTotalScreenPixels);
242 if (mScreenList.IsEmpty()) {
243 MOZ_LOG(sScreenLog, LogLevel::Warning,
244 ("No screen available. This can happen in xpcshell."));
245 *aTotalScreenPixels = 0;
246 return NS_OK;
249 int64_t pixels = 0;
250 for (auto& screen : mScreenList) {
251 int32_t x, y, width, height;
252 x = y = width = height = 0;
253 screen->GetRect(&x, &y, &width, &height);
254 pixels += width * height;
257 *aTotalScreenPixels = pixels;
258 return NS_OK;
261 } // namespace mozilla::widget