2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2017-11-16 20:48:19 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2017 Martin Flöser <mgraesslin@kde.org>
|
|
|
|
SPDX-FileCopyrightText: 2018 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
2017-11-16 20:48:19 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2017-11-16 20:48:19 +00:00
|
|
|
#include "idle_inhibition.h"
|
2020-03-04 07:55:26 +00:00
|
|
|
#include "abstract_client.h"
|
2017-11-16 20:48:19 +00:00
|
|
|
#include "deleted.h"
|
2018-12-04 09:32:37 +00:00
|
|
|
#include "workspace.h"
|
2017-11-16 20:48:19 +00:00
|
|
|
|
2020-04-29 15:18:41 +00:00
|
|
|
#include <KWaylandServer/idle_interface.h>
|
|
|
|
#include <KWaylandServer/surface_interface.h>
|
2017-11-16 20:48:19 +00:00
|
|
|
|
2019-01-11 13:21:20 +00:00
|
|
|
#include <algorithm>
|
2017-11-16 20:48:19 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2020-04-29 15:18:41 +00:00
|
|
|
using KWaylandServer::SurfaceInterface;
|
2017-11-16 20:48:19 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
IdleInhibition::IdleInhibition(IdleInterface *idle)
|
|
|
|
: QObject(idle)
|
|
|
|
, m_idle(idle)
|
|
|
|
{
|
2018-12-04 09:32:37 +00:00
|
|
|
// Workspace is created after the wayland server is initialized.
|
|
|
|
connect(kwinApp(), &Application::workspaceCreated, this, &IdleInhibition::slotWorkspaceCreated);
|
2017-11-16 20:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IdleInhibition::~IdleInhibition() = default;
|
|
|
|
|
2020-03-04 07:55:26 +00:00
|
|
|
void IdleInhibition::registerClient(AbstractClient *client)
|
2017-11-16 20:48:19 +00:00
|
|
|
{
|
2018-12-04 09:32:37 +00:00
|
|
|
auto updateInhibit = [this, client] {
|
|
|
|
update(client);
|
2018-11-28 22:55:44 +00:00
|
|
|
};
|
2018-12-04 09:32:37 +00:00
|
|
|
|
|
|
|
m_connections[client] = connect(client->surface(), &SurfaceInterface::inhibitsIdleChanged, this, updateInhibit);
|
2020-03-04 07:55:26 +00:00
|
|
|
connect(client, &AbstractClient::desktopChanged, this, updateInhibit);
|
|
|
|
connect(client, &AbstractClient::clientMinimized, this, updateInhibit);
|
|
|
|
connect(client, &AbstractClient::clientUnminimized, this, updateInhibit);
|
|
|
|
connect(client, &AbstractClient::windowHidden, this, updateInhibit);
|
|
|
|
connect(client, &AbstractClient::windowShown, this, updateInhibit);
|
|
|
|
connect(client, &AbstractClient::windowClosed, this,
|
2017-12-03 17:01:43 +00:00
|
|
|
[this, client] {
|
|
|
|
uninhibit(client);
|
|
|
|
auto it = m_connections.find(client);
|
|
|
|
if (it != m_connections.end()) {
|
|
|
|
disconnect(it.value());
|
|
|
|
m_connections.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2017-11-16 20:48:19 +00:00
|
|
|
);
|
2018-11-28 22:55:44 +00:00
|
|
|
|
2018-12-04 09:32:37 +00:00
|
|
|
updateInhibit();
|
2017-11-16 20:48:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 09:32:37 +00:00
|
|
|
void IdleInhibition::inhibit(AbstractClient *client)
|
2017-11-16 20:48:19 +00:00
|
|
|
{
|
|
|
|
if (isInhibited(client)) {
|
|
|
|
// already inhibited
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_idleInhibitors << client;
|
|
|
|
m_idle->inhibit();
|
|
|
|
// TODO: notify powerdevil?
|
|
|
|
}
|
|
|
|
|
2018-12-04 09:32:37 +00:00
|
|
|
void IdleInhibition::uninhibit(AbstractClient *client)
|
2017-11-16 20:48:19 +00:00
|
|
|
{
|
2019-01-11 13:21:20 +00:00
|
|
|
auto it = std::find(m_idleInhibitors.begin(), m_idleInhibitors.end(), client);
|
2017-11-16 20:48:19 +00:00
|
|
|
if (it == m_idleInhibitors.end()) {
|
|
|
|
// not inhibited
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_idleInhibitors.erase(it);
|
|
|
|
m_idle->uninhibit();
|
|
|
|
}
|
|
|
|
|
2018-12-04 09:32:37 +00:00
|
|
|
void IdleInhibition::update(AbstractClient *client)
|
|
|
|
{
|
2019-08-26 07:44:04 +00:00
|
|
|
if (client->isInternal()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-04 09:32:37 +00:00
|
|
|
// TODO: Don't honor the idle inhibitor object if the shell client is not
|
|
|
|
// on the current activity (currently, activities are not supported).
|
|
|
|
const bool visible = client->isShown(true) && client->isOnCurrentDesktop();
|
2020-04-15 23:57:50 +00:00
|
|
|
if (visible && client->surface() && client->surface()->inhibitsIdle()) {
|
2018-12-04 09:32:37 +00:00
|
|
|
inhibit(client);
|
|
|
|
} else {
|
|
|
|
uninhibit(client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdleInhibition::slotWorkspaceCreated()
|
|
|
|
{
|
|
|
|
connect(workspace(), &Workspace::currentDesktopChanged, this, &IdleInhibition::slotDesktopChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IdleInhibition::slotDesktopChanged()
|
|
|
|
{
|
|
|
|
workspace()->forEachAbstractClient([this] (AbstractClient *c) { update(c); });
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:48:19 +00:00
|
|
|
}
|