2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2013-04-03 10:19:27 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
|
2013-04-03 10:19:27 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2013-04-03 10:19:27 +00:00
|
|
|
#include "screens.h"
|
2015-03-05 13:07:20 +00:00
|
|
|
#include <abstract_client.h>
|
2020-12-17 10:34:04 +00:00
|
|
|
#include "abstract_output.h"
|
2019-09-24 08:48:08 +00:00
|
|
|
#include <x11client.h>
|
2013-04-03 10:19:27 +00:00
|
|
|
#include "cursor.h"
|
2015-07-31 08:17:43 +00:00
|
|
|
#include "utils.h"
|
2013-04-03 10:19:27 +00:00
|
|
|
#include "settings.h"
|
2014-09-17 08:29:03 +00:00
|
|
|
#include <workspace.h>
|
2014-09-17 07:05:11 +00:00
|
|
|
#include <config-kwin.h>
|
2016-04-07 07:24:17 +00:00
|
|
|
#include "platform.h"
|
2015-03-23 13:29:07 +00:00
|
|
|
#include "wayland_server.h"
|
2014-09-17 08:29:03 +00:00
|
|
|
#ifdef KWIN_UNIT_TEST
|
|
|
|
#include <mock_screens.h>
|
|
|
|
#endif
|
2013-04-03 10:19:27 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2014-02-27 08:55:45 +00:00
|
|
|
Screens *Screens::s_self = nullptr;
|
|
|
|
Screens *Screens::create(QObject *parent)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!s_self);
|
2014-09-17 08:29:03 +00:00
|
|
|
#ifdef KWIN_UNIT_TEST
|
|
|
|
s_self = new MockScreens(parent);
|
|
|
|
#else
|
2020-12-20 13:15:57 +00:00
|
|
|
s_self = new Screens(parent);
|
2014-09-17 08:29:03 +00:00
|
|
|
#endif
|
2015-03-19 07:29:34 +00:00
|
|
|
Q_ASSERT(s_self);
|
2014-02-27 08:55:45 +00:00
|
|
|
s_self->init();
|
|
|
|
return s_self;
|
|
|
|
}
|
2013-04-03 10:19:27 +00:00
|
|
|
|
|
|
|
Screens::Screens(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_count(0)
|
|
|
|
, m_current(0)
|
|
|
|
, m_currentFollowsMouse(false)
|
2018-06-23 22:26:54 +00:00
|
|
|
, m_maxScale(1.0)
|
2014-02-27 08:55:45 +00:00
|
|
|
{
|
2020-12-20 13:15:57 +00:00
|
|
|
// TODO: Do something about testScreens and other tests that use MockScreens.
|
|
|
|
// They only make core code more convoluted with ifdefs.
|
|
|
|
#ifndef KWIN_UNIT_TEST
|
|
|
|
connect(kwinApp()->platform(), &Platform::screensQueried, this, &Screens::updateCount);
|
|
|
|
connect(kwinApp()->platform(), &Platform::screensQueried, this, &Screens::changed);
|
|
|
|
#endif
|
2014-02-27 08:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Screens::~Screens()
|
|
|
|
{
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
s_self = nullptr;
|
2014-02-27 08:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Screens::init()
|
2013-04-03 10:19:27 +00:00
|
|
|
{
|
2020-12-17 10:34:04 +00:00
|
|
|
updateCount();
|
2014-05-26 09:06:20 +00:00
|
|
|
connect(this, &Screens::countChanged, this, &Screens::changed, Qt::QueuedConnection);
|
2014-02-27 08:55:45 +00:00
|
|
|
connect(this, &Screens::changed, this, &Screens::updateSize);
|
|
|
|
connect(this, &Screens::sizeChanged, this, &Screens::geometryChanged);
|
2013-04-03 10:19:27 +00:00
|
|
|
|
|
|
|
Settings settings;
|
|
|
|
settings.setDefaults();
|
|
|
|
m_currentFollowsMouse = settings.activeMouseScreen();
|
2020-12-17 10:34:04 +00:00
|
|
|
emit changed();
|
2013-04-03 10:19:27 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 20:57:13 +00:00
|
|
|
QString Screens::name(int screen) const
|
|
|
|
{
|
2020-12-17 10:34:04 +00:00
|
|
|
if (AbstractOutput *output = findOutput(screen)) {
|
|
|
|
return output->name();
|
|
|
|
}
|
|
|
|
return QString();
|
2015-05-08 20:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 10:34:04 +00:00
|
|
|
bool Screens::isInternal(int screen) const
|
2015-05-08 20:57:13 +00:00
|
|
|
{
|
2020-12-17 10:34:04 +00:00
|
|
|
if (AbstractOutput *output = findOutput(screen)) {
|
|
|
|
return output->isInternal();
|
|
|
|
}
|
|
|
|
return false;
|
2015-05-08 20:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 10:34:04 +00:00
|
|
|
QRect Screens::geometry(int screen) const
|
2018-06-23 22:26:54 +00:00
|
|
|
{
|
2020-12-17 10:34:04 +00:00
|
|
|
if (AbstractOutput *output = findOutput(screen)) {
|
|
|
|
return output->geometry();
|
|
|
|
}
|
|
|
|
return QRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize Screens::size(int screen) const
|
|
|
|
{
|
|
|
|
if (AbstractOutput *output = findOutput(screen)) {
|
|
|
|
return output->geometry().size();
|
|
|
|
}
|
|
|
|
return QSize();
|
2018-06-23 22:26:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 23:36:02 +00:00
|
|
|
qreal Screens::scale(int screen) const
|
|
|
|
{
|
2020-12-17 10:34:04 +00:00
|
|
|
if (AbstractOutput *output = findOutput(screen)) {
|
|
|
|
return output->scale();
|
|
|
|
}
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Screens::physicalSize(int screen) const
|
|
|
|
{
|
|
|
|
if (AbstractOutput *output = findOutput(screen)) {
|
|
|
|
return output->physicalSize();
|
|
|
|
}
|
|
|
|
return QSizeF();
|
|
|
|
}
|
|
|
|
|
|
|
|
float Screens::refreshRate(int screen) const
|
|
|
|
{
|
|
|
|
if (AbstractOutput *output = findOutput(screen)) {
|
|
|
|
return output->refreshRate() / 1000.0;
|
|
|
|
}
|
|
|
|
return 60.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal Screens::maxScale() const
|
|
|
|
{
|
|
|
|
return m_maxScale;
|
2016-10-25 23:36:02 +00:00
|
|
|
}
|
|
|
|
|
2013-04-03 10:19:27 +00:00
|
|
|
void Screens::reconfigure()
|
|
|
|
{
|
|
|
|
if (!m_config) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Settings settings(m_config);
|
2014-03-25 15:29:03 +00:00
|
|
|
settings.read();
|
2013-04-03 10:19:27 +00:00
|
|
|
setCurrentFollowsMouse(settings.activeMouseScreen());
|
|
|
|
}
|
|
|
|
|
2014-02-27 08:55:45 +00:00
|
|
|
void Screens::updateSize()
|
|
|
|
{
|
|
|
|
QRect bounding;
|
2018-06-23 22:26:54 +00:00
|
|
|
qreal maxScale = 1.0;
|
2014-02-27 08:55:45 +00:00
|
|
|
for (int i = 0; i < count(); ++i) {
|
|
|
|
bounding = bounding.united(geometry(i));
|
2018-06-23 22:26:54 +00:00
|
|
|
maxScale = qMax(maxScale, scale(i));
|
2014-02-27 08:55:45 +00:00
|
|
|
}
|
|
|
|
if (m_boundingSize != bounding.size()) {
|
|
|
|
m_boundingSize = bounding.size();
|
|
|
|
emit sizeChanged();
|
|
|
|
}
|
2018-06-23 22:26:54 +00:00
|
|
|
if (!qFuzzyCompare(m_maxScale, maxScale)) {
|
|
|
|
m_maxScale = maxScale;
|
|
|
|
emit maxScaleChanged();
|
|
|
|
}
|
2014-02-27 08:55:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 10:34:04 +00:00
|
|
|
void Screens::updateCount()
|
|
|
|
{
|
|
|
|
setCount(kwinApp()->platform()->enabledOutputs().size());
|
|
|
|
}
|
|
|
|
|
2013-04-03 10:19:27 +00:00
|
|
|
void Screens::setCount(int count)
|
|
|
|
{
|
|
|
|
if (m_count == count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const int previous = m_count;
|
|
|
|
m_count = count;
|
|
|
|
emit countChanged(previous, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Screens::setCurrent(int current)
|
|
|
|
{
|
|
|
|
if (m_current == current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_current = current;
|
2013-12-11 10:13:47 +00:00
|
|
|
emit currentChanged();
|
2013-04-03 10:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Screens::setCurrent(const QPoint &pos)
|
|
|
|
{
|
|
|
|
setCurrent(number(pos));
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:07:20 +00:00
|
|
|
void Screens::setCurrent(const AbstractClient *c)
|
2013-04-03 10:19:27 +00:00
|
|
|
{
|
|
|
|
if (!c->isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!c->isOnScreen(m_current)) {
|
|
|
|
setCurrent(c->screen());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Screens::setCurrentFollowsMouse(bool follows)
|
|
|
|
{
|
|
|
|
if (m_currentFollowsMouse == follows) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_currentFollowsMouse = follows;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Screens::current() const
|
|
|
|
{
|
|
|
|
if (m_currentFollowsMouse) {
|
2020-04-02 16:18:01 +00:00
|
|
|
return number(Cursors::self()->mouse()->pos());
|
2013-04-03 10:19:27 +00:00
|
|
|
}
|
2015-03-06 12:37:56 +00:00
|
|
|
AbstractClient *client = Workspace::self()->activeClient();
|
2013-04-03 10:19:27 +00:00
|
|
|
if (client && !client->isOnScreen(m_current)) {
|
|
|
|
return client->screen();
|
|
|
|
}
|
|
|
|
return m_current;
|
|
|
|
}
|
|
|
|
|
2013-09-23 21:04:17 +00:00
|
|
|
int Screens::intersecting(const QRect &r) const
|
|
|
|
{
|
|
|
|
int cnt = 0;
|
|
|
|
for (int i = 0; i < count(); ++i) {
|
|
|
|
if (geometry(i).intersects(r)) {
|
|
|
|
++cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2016-06-08 10:46:02 +00:00
|
|
|
QSize Screens::displaySize() const
|
|
|
|
{
|
|
|
|
return size();
|
|
|
|
}
|
|
|
|
|
2017-11-10 17:07:15 +00:00
|
|
|
Qt::ScreenOrientation Screens::orientation(int screen) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(screen)
|
|
|
|
return Qt::PrimaryOrientation;
|
|
|
|
}
|
|
|
|
|
2017-11-09 17:03:15 +00:00
|
|
|
void Screens::setConfig(KSharedConfig::Ptr config)
|
|
|
|
{
|
|
|
|
m_config = config;
|
|
|
|
}
|
|
|
|
|
2020-03-23 19:26:21 +00:00
|
|
|
int Screens::physicalDpiX(int screen) const
|
|
|
|
{
|
|
|
|
return size(screen).width() / physicalSize(screen).width() * qreal(25.4);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Screens::physicalDpiY(int screen) const
|
|
|
|
{
|
|
|
|
return size(screen).height() / physicalSize(screen).height() * qreal(25.4);
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:34:04 +00:00
|
|
|
int Screens::number(const QPoint &pos) const
|
|
|
|
{
|
|
|
|
// TODO: Do something about testScreens and other tests that use MockScreens.
|
|
|
|
// They only make core code more convoluted with ifdefs.
|
|
|
|
#ifdef KWIN_UNIT_TEST
|
2021-01-15 12:48:23 +00:00
|
|
|
Q_UNUSED(pos)
|
2020-12-17 10:34:04 +00:00
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
int bestScreen = 0;
|
|
|
|
int minDistance = INT_MAX;
|
|
|
|
const auto outputs = kwinApp()->platform()->enabledOutputs();
|
|
|
|
for (int i = 0; i < outputs.size(); ++i) {
|
|
|
|
const QRect &geo = outputs[i]->geometry();
|
|
|
|
if (geo.contains(pos)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
int distance = QPoint(geo.topLeft() - pos).manhattanLength();
|
|
|
|
distance = qMin(distance, QPoint(geo.topRight() - pos).manhattanLength());
|
|
|
|
distance = qMin(distance, QPoint(geo.bottomRight() - pos).manhattanLength());
|
|
|
|
distance = qMin(distance, QPoint(geo.bottomLeft() - pos).manhattanLength());
|
|
|
|
if (distance < minDistance) {
|
|
|
|
minDistance = distance;
|
|
|
|
bestScreen = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestScreen;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractOutput *Screens::findOutput(int screen) const
|
|
|
|
{
|
|
|
|
// TODO: Do something about testScreens and other tests that use MockScreens.
|
|
|
|
// They only make core code more convoluted with ifdefs.
|
|
|
|
#ifdef KWIN_UNIT_TEST
|
2021-01-15 12:48:23 +00:00
|
|
|
Q_UNUSED(screen)
|
2020-12-17 10:34:04 +00:00
|
|
|
return nullptr;
|
|
|
|
#else
|
|
|
|
return kwinApp()->platform()->findOutput(screen);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-03 10:19:27 +00:00
|
|
|
} // namespace
|