This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
forked from tildearrow/kwin-lowlatency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idle_inhibition.cpp
121 lines (101 loc) · 3.71 KB
/
idle_inhibition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2017 Martin Flöser <[email protected]>
Copyright (C) 2018 Vlad Zahorodnii <[email protected]>
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"
#include "xdgshellclient.h"
#include "workspace.h"
#include <KWayland/Server/idle_interface.h>
#include <KWayland/Server/surface_interface.h>
#include <algorithm>
#include <functional>
using KWayland::Server::SurfaceInterface;
namespace KWin
{
IdleInhibition::IdleInhibition(IdleInterface *idle)
: QObject(idle)
, m_idle(idle)
{
// Workspace is created after the wayland server is initialized.
connect(kwinApp(), &Application::workspaceCreated, this, &IdleInhibition::slotWorkspaceCreated);
}
IdleInhibition::~IdleInhibition() = default;
void IdleInhibition::registerXdgShellClient(XdgShellClient *client)
{
auto updateInhibit = [this, client] {
update(client);
};
m_connections[client] = connect(client->surface(), &SurfaceInterface::inhibitsIdleChanged, this, updateInhibit);
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,
[this, client] {
uninhibit(client);
auto it = m_connections.find(client);
if (it != m_connections.end()) {
disconnect(it.value());
m_connections.erase(it);
}
}
);
updateInhibit();
}
void IdleInhibition::inhibit(AbstractClient *client)
{
if (isInhibited(client)) {
// already inhibited
return;
}
m_idleInhibitors << client;
m_idle->inhibit();
// TODO: notify powerdevil?
}
void IdleInhibition::uninhibit(AbstractClient *client)
{
auto it = std::find(m_idleInhibitors.begin(), m_idleInhibitors.end(), client);
if (it == m_idleInhibitors.end()) {
// not inhibited
return;
}
m_idleInhibitors.erase(it);
m_idle->uninhibit();
}
void IdleInhibition::update(AbstractClient *client)
{
if (client->isInternal()) {
return;
}
// 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); });
}
}