KWaylandServer

idle_interface.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Martin Gräßlin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "display.h"
7 #include "idle_interface_p.h"
8 #include "seat_interface.h"
9 
10 namespace KWaylandServer
11 {
12 static const quint32 s_version = 1;
13 
14 IdleInterfacePrivate::IdleInterfacePrivate(IdleInterface *_q, Display *display)
15  : QtWaylandServer::org_kde_kwin_idle(*display, s_version)
16  , q(_q)
17 {
18 }
19 
20 void IdleInterfacePrivate::org_kde_kwin_idle_get_idle_timeout(Resource *resource, uint32_t id, wl_resource *seat, uint32_t timeout)
21 {
22  SeatInterface *s = SeatInterface::get(seat);
23  Q_ASSERT(s);
24 
25  wl_resource *idleTimoutResource = wl_resource_create(resource->client(), &org_kde_kwin_idle_timeout_interface, resource->version(), id);
26  if (!idleTimoutResource) {
27  wl_client_post_no_memory(resource->client());
28  return;
29  }
30 
31  IdleTimeoutInterface *idleTimeout = new IdleTimeoutInterface(s, q, idleTimoutResource);
32  idleTimeouts << idleTimeout;
33 
34  QObject::connect(idleTimeout, &IdleTimeoutInterface::destroyed, q, [this, idleTimeout]() {
35  idleTimeouts.removeOne(idleTimeout);
36  });
37  idleTimeout->setup(timeout);
38 }
39 
40 IdleInterface::IdleInterface(Display *display, QObject *parent)
41  : QObject(parent)
42  , d(new IdleInterfacePrivate(this, display))
43 {
44 }
45 
46 IdleInterface::~IdleInterface() = default;
47 
49 {
50  d->inhibitCount++;
51  if (d->inhibitCount == 1) {
52  Q_EMIT inhibitedChanged();
53  }
54 }
55 
57 {
58  d->inhibitCount--;
59  if (d->inhibitCount == 0) {
60  Q_EMIT inhibitedChanged();
61  }
62 }
63 
65 {
66  return d->inhibitCount > 0;
67 }
68 
70 {
71  for (auto i : qAsConst(d->idleTimeouts)) {
72  i->simulateUserActivity();
73  }
74 }
75 
76 IdleTimeoutInterface::IdleTimeoutInterface(SeatInterface *seat, IdleInterface *manager, wl_resource *resource)
77  : QObject()
78  , QtWaylandServer::org_kde_kwin_idle_timeout(resource)
79  , seat(seat)
80  , manager(manager)
81 {
82  connect(seat, &SeatInterface::timestampChanged, this, [this] {
83  simulateUserActivity();
84  });
85  connect(manager, &IdleInterface::inhibitedChanged, this, [this, manager] {
86  if (!timer) {
87  // not yet configured
88  return;
89  }
90  if (manager->isInhibited()) {
91  if (!timer->isActive()) {
92  send_resumed();
93  }
94  timer->stop();
95  } else {
96  timer->start();
97  }
98  });
99 }
100 
101 IdleTimeoutInterface::~IdleTimeoutInterface() = default;
102 
103 void IdleTimeoutInterface::org_kde_kwin_idle_timeout_release(Resource *resource)
104 {
105  wl_resource_destroy(resource->handle);
106 }
107 
108 void IdleTimeoutInterface::org_kde_kwin_idle_timeout_destroy_resource(Resource *resource)
109 {
110  Q_UNUSED(resource)
111  delete this;
112 }
113 
114 void IdleTimeoutInterface::org_kde_kwin_idle_timeout_simulate_user_activity(Resource *resource)
115 {
116  Q_UNUSED(resource)
117  simulateUserActivity();
118 }
119 void IdleTimeoutInterface::simulateUserActivity()
120 {
121  if (!timer) {
122  // not yet configured
123  return;
124  }
125  if (manager->isInhibited()) {
126  // ignored while inhibited
127  return;
128  }
129  if (!timer->isActive()) {
130  send_resumed();
131  }
132  timer->start();
133 }
134 
135 void IdleTimeoutInterface::setup(quint32 timeout)
136 {
137  if (timer) {
138  return;
139  }
140  timer = new QTimer(this);
141  timer->setSingleShot(true);
142  // less than 500 msec is not idle by definition
143  timer->setInterval(qMax(timeout, 500u));
144  QObject::connect(timer, &QTimer::timeout, this, [this] {
145  send_idle();
146  });
147  if (manager->isInhibited()) {
148  // don't start if inhibited
149  return;
150  }
151  timer->start();
152 }
153 }
Represents a Seat on the Wayland Display.
void inhibit()
Inhibits the IdleInterface.
void uninhibit()
Inhibits the IdleInterface.
void timeout()
void inhibitedChanged()
Emitted when the system gets inhibited or uninhibited.
Global representing the org_kde_kwin_idle interface.
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
Q_EMITQ_EMIT
void simulateUserActivity()
Calling this method allows the Compositor to simulate user activity.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:08:27 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.