KWin
Loading...
Searching...
No Matches
idle_inhibition.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2017 Martin Flöser <mgraesslin@kde.org>
6 SPDX-FileCopyrightText: 2018 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10#include "idle_inhibition.h"
11#include "input.h"
12#include "virtualdesktops.h"
13#include "wayland/surface.h"
14#include "window.h"
15#include "workspace.h"
16
17#include <algorithm>
18#include <functional>
19
20namespace KWin
21{
22
24 : QObject(parent)
25{
26 // Workspace is created after the wayland server is initialized.
27 connect(kwinApp(), &Application::workspaceCreated, this, &IdleInhibition::slotWorkspaceCreated);
28}
29
31
33{
34 if (!client->surface()) {
35 return;
36 }
37
38 auto updateInhibit = [this, client] {
39 update(client);
40 };
41
42 m_connections[client] = connect(client->surface(), &SurfaceInterface::inhibitsIdleChanged, this, updateInhibit);
43 connect(client, &Window::desktopsChanged, this, updateInhibit);
44 connect(client, &Window::minimizedChanged, this, updateInhibit);
45 connect(client, &Window::windowHidden, this, updateInhibit);
46 connect(client, &Window::windowShown, this, updateInhibit);
47 connect(client, &Window::closed, this, [this, client]() {
48 uninhibit(client);
49 auto it = m_connections.find(client);
50 if (it != m_connections.end()) {
51 disconnect(it.value());
52 m_connections.erase(it);
53 }
54 });
55
56 updateInhibit();
57}
58
59void IdleInhibition::inhibit(Window *client)
60{
61 input()->addIdleInhibitor(client);
62 // TODO: notify powerdevil?
63}
64
65void IdleInhibition::uninhibit(Window *client)
66{
67 input()->removeIdleInhibitor(client);
68}
69
70void IdleInhibition::update(Window *client)
71{
72 if (client->isInternal() || client->isUnmanaged()) {
73 return;
74 }
75
76 // TODO: Don't honor the idle inhibitor object if the shell client is not
77 // on the current activity (currently, activities are not supported).
78 const bool visible = client->isShown() && client->isOnCurrentDesktop();
79 if (visible && client->surface() && client->surface()->inhibitsIdle()) {
80 inhibit(client);
81 } else {
82 uninhibit(client);
83 }
84}
85
86void IdleInhibition::slotWorkspaceCreated()
87{
89 connect(workspace(), &Workspace::currentDesktopChanged, this, &IdleInhibition::slotDesktopChanged);
90}
91
92void IdleInhibition::slotDesktopChanged()
93{
94 workspace()->forEachWindow([this](Window *c) {
95 update(c);
96 });
97}
98
99}
100
101#include "moc_idle_inhibition.cpp"
void registerClient(Window *client)
IdleInhibition(QObject *parent=nullptr)
~IdleInhibition() override
void addIdleInhibitor(Window *inhibitor)
Definition input.cpp:3243
void removeIdleInhibitor(Window *inhibitor)
Definition input.cpp:3253
SurfaceInterface * surface() const
Definition window.cpp:342
void windowShown(KWin::Window *window)
void desktopsChanged()
void minimizedChanged()
void windowHidden(KWin::Window *window)
void forEachWindow(std::function< void(Window *)> func)
void currentDesktopChanged(KWin::VirtualDesktop *previousDesktop, KWin::Window *)
void windowAdded(KWin::Window *)
Workspace * workspace()
Definition workspace.h:830
InputRedirection * input()
Definition input.h:549