2017-03-14 10:44:54 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ScreenManager.h"
|
|
|
|
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2017-03-09 11:30:26 +00:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2017-10-12 16:03:35 +00:00
|
|
|
#include "mozilla/dom/DOMTypes.h"
|
2017-03-14 10:44:54 +00:00
|
|
|
#include "mozilla/Logging.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2019-10-17 11:27:08 +00:00
|
|
|
#ifdef MOZ_WAYLAND
|
2021-03-11 14:32:53 +00:00
|
|
|
# include "mozilla/WidgetUtilsGtk.h"
|
2019-10-17 11:27:08 +00:00
|
|
|
#endif /* MOZ_WAYLAND */
|
2017-03-14 10:44:54 +00:00
|
|
|
|
2017-10-12 16:03:35 +00:00
|
|
|
static mozilla::LazyLogModule sScreenLog("WidgetScreen");
|
2017-03-14 10:44:54 +00:00
|
|
|
|
2020-01-18 13:48:34 +00:00
|
|
|
namespace mozilla::widget {
|
2017-03-14 10:44:54 +00:00
|
|
|
|
2017-10-12 16:03:35 +00:00
|
|
|
NS_IMPL_ISUPPORTS(ScreenManager, nsIScreenManager)
|
|
|
|
|
2020-03-16 10:56:57 +00:00
|
|
|
ScreenManager::ScreenManager() = default;
|
2017-03-14 10:44:54 +00:00
|
|
|
|
2020-03-16 10:56:57 +00:00
|
|
|
ScreenManager::~ScreenManager() = default;
|
2017-03-14 10:44:54 +00:00
|
|
|
|
|
|
|
static StaticRefPtr<ScreenManager> sSingleton;
|
|
|
|
|
|
|
|
ScreenManager& ScreenManager::GetSingleton() {
|
|
|
|
if (!sSingleton) {
|
|
|
|
sSingleton = new ScreenManager();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
return *sSingleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<ScreenManager> ScreenManager::GetAddRefedSingleton() {
|
|
|
|
RefPtr<ScreenManager> sm = &GetSingleton();
|
|
|
|
return sm.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenManager::SetHelper(UniquePtr<Helper> aHelper) {
|
2018-05-30 19:15:35 +00:00
|
|
|
mHelper = std::move(aHelper);
|
2017-03-14 10:44:54 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 12:13:28 +00:00
|
|
|
// static
|
2017-03-14 10:44:54 +00:00
|
|
|
void ScreenManager::Refresh(nsTArray<RefPtr<Screen>>&& aScreens) {
|
2021-12-09 12:13:28 +00:00
|
|
|
if (PastShutdownPhase(ShutdownPhase::XPCOMShutdown)) {
|
|
|
|
// We don't refresh screen data if starting XPCOM shutdown path.
|
|
|
|
// GetSingleton returns invalid data since it is freed.
|
|
|
|
return;
|
|
|
|
}
|
2017-03-09 11:30:26 +00:00
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Debug, ("Refresh screens"));
|
2022-02-15 20:22:54 +00:00
|
|
|
GetSingleton().RefreshInternal(std::move(aScreens));
|
2017-03-14 10:44:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 11:30:26 +00:00
|
|
|
void ScreenManager::Refresh(nsTArray<mozilla::dom::ScreenDetails>&& aScreens) {
|
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Debug, ("Refresh screens from IPC"));
|
2017-03-14 10:44:54 +00:00
|
|
|
|
2022-02-15 20:22:54 +00:00
|
|
|
AutoTArray<RefPtr<Screen>, 4> screens;
|
2017-03-09 11:30:26 +00:00
|
|
|
for (auto& screen : aScreens) {
|
2022-02-15 20:22:54 +00:00
|
|
|
screens.AppendElement(new Screen(screen));
|
2017-03-09 11:30:26 +00:00
|
|
|
}
|
2022-02-15 20:22:54 +00:00
|
|
|
RefreshInternal(std::move(screens));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenManager::RefreshInternal(nsTArray<RefPtr<Screen>>&& aScreens) {
|
|
|
|
mScreenList = std::move(aScreens);
|
2017-03-09 11:30:26 +00:00
|
|
|
|
|
|
|
CopyScreensToAllRemotesIfIsParent();
|
2022-02-15 20:22:54 +00:00
|
|
|
if (nsCOMPtr<nsIObserverService> s = services::GetObserverService()) {
|
|
|
|
s->NotifyObservers(nullptr, "screen-information-changed", nullptr);
|
|
|
|
}
|
2017-03-09 11:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Range>
|
|
|
|
void ScreenManager::CopyScreensToRemoteRange(Range aRemoteRange) {
|
2017-10-12 16:03:35 +00:00
|
|
|
AutoTArray<dom::ScreenDetails, 4> screens;
|
2017-03-14 10:44:54 +00:00
|
|
|
for (auto& screen : mScreenList) {
|
2017-03-09 11:30:26 +00:00
|
|
|
screens.AppendElement(screen->ToScreenDetails());
|
|
|
|
}
|
|
|
|
for (auto cp : aRemoteRange) {
|
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Debug,
|
|
|
|
("Send screens to [Pid %d]", cp->Pid()));
|
|
|
|
if (!cp->SendRefreshScreens(screens)) {
|
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Error,
|
|
|
|
("SendRefreshScreens to [Pid %d] failed", cp->Pid()));
|
2017-03-14 10:44:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-09 11:30:26 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 16:03:35 +00:00
|
|
|
void ScreenManager::CopyScreensToRemote(dom::ContentParent* aContentParent) {
|
2017-03-09 11:30:26 +00:00
|
|
|
MOZ_ASSERT(aContentParent);
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
|
|
|
|
auto range = {aContentParent};
|
|
|
|
CopyScreensToRemoteRange(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenManager::CopyScreensToAllRemotesIfIsParent() {
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Debug, ("Refreshing all ContentParents"));
|
2017-03-14 10:44:54 +00:00
|
|
|
|
2017-10-12 16:03:35 +00:00
|
|
|
CopyScreensToRemoteRange(
|
|
|
|
dom::ContentParent::AllProcesses(dom::ContentParent::eLive));
|
2017-03-14 10:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the screen that contains the rectangle. If the rect overlaps
|
|
|
|
// multiple screens, it picks the screen with the greatest area of intersection.
|
|
|
|
//
|
|
|
|
// The coordinates are in desktop pixels.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ScreenManager::ScreenForRect(int32_t aX, int32_t aY, int32_t aWidth,
|
|
|
|
int32_t aHeight, nsIScreen** aOutScreen) {
|
2023-02-06 04:51:26 +00:00
|
|
|
DesktopIntRect rect(aX, aY, aWidth, aHeight);
|
|
|
|
nsCOMPtr<nsIScreen> screen = ScreenForRect(rect);
|
|
|
|
screen.forget(aOutScreen);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Screen> ScreenManager::ScreenForRect(
|
|
|
|
const DesktopIntRect& aRect) {
|
2020-08-05 07:43:54 +00:00
|
|
|
#if defined(MOZ_WAYLAND) && defined(MOZ_LOGGING)
|
2022-04-06 10:08:58 +00:00
|
|
|
static bool inWayland = GdkIsWaylandDisplay();
|
2019-10-17 11:27:08 +00:00
|
|
|
if (inWayland) {
|
2020-08-05 07:43:54 +00:00
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Warning,
|
|
|
|
("Getting screen in wayland, primary display will be returned."));
|
2019-10-17 11:27:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-14 10:44:54 +00:00
|
|
|
if (mScreenList.IsEmpty()) {
|
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Warning,
|
|
|
|
("No screen available. This can happen in xpcshell."));
|
2022-02-15 20:22:54 +00:00
|
|
|
auto screen = MakeRefPtr<Screen>(
|
2022-05-06 23:37:25 +00:00
|
|
|
LayoutDeviceIntRect(), LayoutDeviceIntRect(), 0, 0, 0,
|
2022-02-15 20:22:54 +00:00
|
|
|
DesktopToLayoutDeviceScale(), CSSToLayoutDeviceScale(), 96 /* dpi */,
|
2024-03-22 00:55:46 +00:00
|
|
|
Screen::IsPseudoDisplay::No, Screen::IsHDR::No,
|
|
|
|
hal::ScreenOrientation::None, 0);
|
2023-02-06 04:51:26 +00:00
|
|
|
return screen.forget();
|
2017-03-14 10:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Optimize for the common case. If the number of screens is only
|
|
|
|
// one then just return the primary screen.
|
|
|
|
if (mScreenList.Length() == 1) {
|
2023-02-06 04:51:26 +00:00
|
|
|
return GetPrimaryScreen();
|
2017-03-14 10:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// which screen should we return?
|
|
|
|
Screen* which = mScreenList[0].get();
|
|
|
|
|
|
|
|
// walk the list of screens and find the one that has the most
|
|
|
|
// surface area.
|
|
|
|
uint32_t area = 0;
|
|
|
|
for (auto& screen : mScreenList) {
|
|
|
|
int32_t x, y, width, height;
|
|
|
|
x = y = width = height = 0;
|
|
|
|
screen->GetRectDisplayPix(&x, &y, &width, &height);
|
|
|
|
// calculate the surface area
|
|
|
|
DesktopIntRect screenRect(x, y, width, height);
|
2023-02-06 04:51:26 +00:00
|
|
|
screenRect.IntersectRect(screenRect, aRect);
|
2018-01-10 16:14:16 +00:00
|
|
|
uint32_t tempArea = screenRect.Area();
|
2017-05-17 14:30:05 +00:00
|
|
|
if (tempArea > area) {
|
2017-03-14 10:44:54 +00:00
|
|
|
which = screen.get();
|
|
|
|
area = tempArea;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 14:30:05 +00:00
|
|
|
// If the rect intersects one or more screen,
|
|
|
|
// return the screen that has the largest intersection.
|
|
|
|
if (area > 0) {
|
|
|
|
RefPtr<Screen> ret = which;
|
2023-02-06 04:51:26 +00:00
|
|
|
return ret.forget();
|
2017-05-17 14:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the rect does not intersect a screen, find
|
|
|
|
// a screen that is nearest to the rect.
|
|
|
|
uint32_t distance = UINT32_MAX;
|
|
|
|
for (auto& screen : mScreenList) {
|
|
|
|
int32_t x, y, width, height;
|
|
|
|
x = y = width = height = 0;
|
|
|
|
screen->GetRectDisplayPix(&x, &y, &width, &height);
|
|
|
|
|
|
|
|
uint32_t distanceX = 0;
|
2023-02-06 04:51:26 +00:00
|
|
|
if (aRect.x > (x + width)) {
|
|
|
|
distanceX = aRect.x - (x + width);
|
|
|
|
} else if (aRect.XMost() < x) {
|
|
|
|
distanceX = x - aRect.XMost();
|
2017-05-17 14:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t distanceY = 0;
|
2023-02-06 04:51:26 +00:00
|
|
|
if (aRect.y > (y + height)) {
|
|
|
|
distanceY = aRect.y - (y + height);
|
|
|
|
} else if (aRect.YMost() < y) {
|
|
|
|
distanceY = y - aRect.YMost();
|
2017-05-17 14:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t tempDistance = distanceX * distanceX + distanceY * distanceY;
|
|
|
|
if (tempDistance < distance) {
|
|
|
|
which = screen.get();
|
|
|
|
distance = tempDistance;
|
|
|
|
if (distance == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 10:44:54 +00:00
|
|
|
RefPtr<Screen> ret = which;
|
2023-02-06 04:51:26 +00:00
|
|
|
return ret.forget();
|
2017-03-14 10:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The screen with the menubar/taskbar. This shouldn't be needed very
|
|
|
|
// often.
|
|
|
|
//
|
2021-12-30 05:09:40 +00:00
|
|
|
already_AddRefed<Screen> ScreenManager::GetPrimaryScreen() {
|
2017-03-14 10:44:54 +00:00
|
|
|
if (mScreenList.IsEmpty()) {
|
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Warning,
|
|
|
|
("No screen available. This can happen in xpcshell."));
|
2024-03-22 00:55:46 +00:00
|
|
|
return MakeAndAddRef<Screen>(LayoutDeviceIntRect(), LayoutDeviceIntRect(),
|
|
|
|
0, 0, 0, DesktopToLayoutDeviceScale(),
|
|
|
|
CSSToLayoutDeviceScale(), 96 /* dpi */,
|
|
|
|
Screen::IsPseudoDisplay::No, Screen::IsHDR::No,
|
|
|
|
hal::ScreenOrientation::None, 0);
|
2017-03-14 10:44:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 20:22:54 +00:00
|
|
|
return do_AddRef(mScreenList[0]);
|
2021-12-30 05:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ScreenManager::GetPrimaryScreen(nsIScreen** aPrimaryScreen) {
|
|
|
|
nsCOMPtr<nsIScreen> screen = GetPrimaryScreen();
|
|
|
|
screen.forget(aPrimaryScreen);
|
2017-03-14 10:44:54 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-10-02 18:48:11 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ScreenManager::GetTotalScreenPixels(int64_t* aTotalScreenPixels) {
|
|
|
|
MOZ_ASSERT(aTotalScreenPixels);
|
|
|
|
|
|
|
|
if (mScreenList.IsEmpty()) {
|
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Warning,
|
|
|
|
("No screen available. This can happen in xpcshell."));
|
|
|
|
*aTotalScreenPixels = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t pixels = 0;
|
|
|
|
for (auto& screen : mScreenList) {
|
|
|
|
int32_t x, y, width, height;
|
|
|
|
x = y = width = height = 0;
|
|
|
|
screen->GetRect(&x, &y, &width, &height);
|
|
|
|
pixels += width * height;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aTotalScreenPixels = pixels;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-01-18 13:48:34 +00:00
|
|
|
} // namespace mozilla::widget
|