2017-11-16 20:48:19 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2017 Martin Flöser <mgraesslin@kde.org>
|
2020-01-14 16:17:18 +00:00
|
|
|
Copyright (C) 2018 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
2017-11-16 20:48:19 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
#include "idle_inhibition.h"
|
|
|
|
#include "deleted.h"
|
2019-08-30 21:36:58 +00:00
|
|
|
#include "xdgshellclient.h"
|
2018-12-04 09:32:37 +00:00
|
|
|
#include "workspace.h"
|
2017-11-16 20:48:19 +00:00
|
|
|
|
|
|
|
#include <KWayland/Server/idle_interface.h>
|
|
|
|
#include <KWayland/Server/surface_interface.h>
|
|
|
|
|
2019-01-11 13:21:20 +00:00
|
|
|
#include <algorithm>
|
2017-11-16 20:48:19 +00:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
using KWayland::Server::SurfaceInterface;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-08-30 21:36:58 +00:00
|
|
|
void IdleInhibition::registerXdgShellClient(XdgShellClient *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);
|
2019-08-30 21:36:58 +00:00
|
|
|
connect(client, &XdgShellClient::desktopChanged, this, updateInhibit);
|
|
|
|
connect(client, &XdgShellClient::clientMinimized, this, updateInhibit);
|
|
|
|
connect(client, &XdgShellClient::clientUnminimized, this, updateInhibit);
|
|
|
|
connect(client, &XdgShellClient::windowHidden, this, updateInhibit);
|
|
|
|
connect(client, &XdgShellClient::windowShown, this, updateInhibit);
|
|
|
|
connect(client, &XdgShellClient::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();
|
|
|
|
if (visible && client->surface()->inhibitsIdle()) {
|
|
|
|
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
|
|
|
}
|