KWayland

dpms.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 "dpms.h"
7 #include "event_queue.h"
8 #include "output.h"
9 #include "wayland_pointer_p.h"
10 
11 #include <wayland-client-protocol.h>
12 #include <wayland-dpms-client-protocol.h>
13 
14 namespace KWayland
15 {
16 namespace Client
17 {
18 
19 class Q_DECL_HIDDEN DpmsManager::Private
20 {
21 public:
22  WaylandPointer<org_kde_kwin_dpms_manager, org_kde_kwin_dpms_manager_destroy> manager;
23  EventQueue *queue = nullptr;
24 };
25 
27  : QObject(parent)
28  , d(new Private)
29 {
30 }
31 
32 DpmsManager::~DpmsManager()
33 {
34  release();
35 }
36 
38 {
39  d->manager.release();
40 }
41 
43 {
44  d->manager.destroy();
45 }
46 
48 {
49  return d->manager.isValid();
50 }
51 
52 void DpmsManager::setup(org_kde_kwin_dpms_manager *manager)
53 {
54  Q_ASSERT(manager);
55  Q_ASSERT(!d->manager.isValid());
56  d->manager.setup(manager);
57 }
58 
60 {
61  return d->queue;
62 }
63 
65 {
66  d->queue = queue;
67 }
68 
69 Dpms *DpmsManager::getDpms(Output *output, QObject *parent)
70 {
71  Q_ASSERT(isValid());
72  Q_ASSERT(output);
73  Dpms *dpms = new Dpms(output, parent);
74  auto w = org_kde_kwin_dpms_manager_get(d->manager, *output);
75  if (d->queue) {
76  d->queue->addProxy(w);
77  }
78  dpms->setup(w);
79  return dpms;
80 }
81 
82 DpmsManager::operator org_kde_kwin_dpms_manager*() const
83 {
84  return d->manager;
85 }
86 
87 DpmsManager::operator org_kde_kwin_dpms_manager*()
88 {
89  return d->manager;
90 }
91 
92 
93 class Q_DECL_HIDDEN Dpms::Private
94 {
95 public:
96  explicit Private(const QPointer<Output> &output, Dpms *q);
97  void setup(org_kde_kwin_dpms *d);
98 
99  WaylandPointer<org_kde_kwin_dpms, org_kde_kwin_dpms_release> dpms;
100  struct Data {
101  bool supported = false;
102  Mode mode = Mode::On;
103  bool supportedChanged = false;
104  bool modeChanged = false;
105  };
106  Data current;
107  Data pending;
108  QPointer<Output> output;
109 
110 private:
111  static void supportedCallback(void *data, org_kde_kwin_dpms *org_kde_kwin_dpms, uint32_t supported);
112  static void modeCallback(void *data, org_kde_kwin_dpms *org_kde_kwin_dpms, uint32_t mode);
113  static void doneCallback(void *data, org_kde_kwin_dpms *org_kde_kwin_dpms);
114  static const struct org_kde_kwin_dpms_listener s_listener;
115 
116  Dpms *q;
117 };
118 
119 
120 #ifndef K_DOXYGEN
121 const org_kde_kwin_dpms_listener Dpms::Private::s_listener = {
122  supportedCallback,
123  modeCallback,
124  doneCallback
125 };
126 #endif
127 
128 void Dpms::Private::supportedCallback(void *data, org_kde_kwin_dpms *org_kde_kwin_dpms, uint32_t supported)
129 {
130  Q_UNUSED(org_kde_kwin_dpms)
131  Private *p = reinterpret_cast<Private*>(data);
132  p->pending.supported = supported == 0 ? false : true;
133  p->pending.supportedChanged = true;
134 }
135 
136 void Dpms::Private::modeCallback(void *data, org_kde_kwin_dpms *org_kde_kwin_dpms, uint32_t mode)
137 {
138  Q_UNUSED(org_kde_kwin_dpms)
139  Mode m;
140  switch (mode) {
141  case ORG_KDE_KWIN_DPMS_MODE_ON:
142  m = Mode::On;
143  break;
144  case ORG_KDE_KWIN_DPMS_MODE_STANDBY:
145  m = Mode::Standby;
146  break;
147  case ORG_KDE_KWIN_DPMS_MODE_SUSPEND:
148  m = Mode::Suspend;
149  break;
150  case ORG_KDE_KWIN_DPMS_MODE_OFF:
151  m = Mode::Off;
152  break;
153  default:
154  return;
155  }
156  Private *p = reinterpret_cast<Private*>(data);
157  p->pending.mode = m;
158  p->pending.modeChanged = true;
159 }
160 
161 void Dpms::Private::doneCallback(void *data, org_kde_kwin_dpms *org_kde_kwin_dpms)
162 {
163  Q_UNUSED(org_kde_kwin_dpms)
164  Private *p = reinterpret_cast<Private*>(data);
165  const bool supportedChanged = p->pending.supportedChanged && p->pending.supported != p->current.supported;
166  const bool modeChanged = p->pending.modeChanged && p->pending.mode != p->current.mode;
167  if (supportedChanged) {
168  p->current.supported = p->pending.supported;
169  emit p->q->supportedChanged();
170  }
171  if (modeChanged) {
172  p->current.mode = p->pending.mode;
173  emit p->q->modeChanged();
174  }
175  p->pending = Data();
176 }
177 
178 Dpms::Private::Private(const QPointer<Output> &output, Dpms *q)
179  : output(output)
180  , q(q)
181 {
182 }
183 
184 void Dpms::Private::setup(org_kde_kwin_dpms *d)
185 {
186  Q_ASSERT(d);
187  Q_ASSERT(!dpms.isValid());
188  dpms.setup(d);
189  org_kde_kwin_dpms_add_listener(dpms, &s_listener, this);
190 }
191 
192 Dpms::Dpms(const QPointer<Output> &o, QObject *parent)
193  : QObject(parent)
194  , d(new Private(o, this))
195 {
196 }
197 
198 Dpms::~Dpms()
199 {
200  release();
201 }
202 
204 {
205  d->dpms.destroy();
206 }
207 
209 {
210  d->dpms.release();
211 }
212 
213 bool Dpms::isValid() const
214 {
215  return d->dpms.isValid();
216 }
217 
218 void Dpms::setup(org_kde_kwin_dpms *dpms)
219 {
220  d->setup(dpms);
221 }
222 
223 bool Dpms::isSupported() const
224 {
225  return d->current.supported;
226 }
227 
228 Dpms::Mode Dpms::mode() const
229 {
230  return d->current.mode;
231 }
232 
233 void Dpms::requestMode(Dpms::Mode mode)
234 {
235  uint32_t wlMode;
236  switch (mode) {
237  case Mode::On:
238  wlMode = ORG_KDE_KWIN_DPMS_MODE_ON;
239  break;
240  case Mode::Standby:
241  wlMode = ORG_KDE_KWIN_DPMS_MODE_STANDBY;
242  break;
243  case Mode::Suspend:
244  wlMode = ORG_KDE_KWIN_DPMS_MODE_SUSPEND;
245  break;
246  case Mode::Off:
247  wlMode = ORG_KDE_KWIN_DPMS_MODE_OFF;
248  break;
249  default:
250  Q_UNREACHABLE();
251  }
252  org_kde_kwin_dpms_set(d->dpms, wlMode);
253 }
254 
256 {
257  return d->output;
258 }
259 
260 Dpms::operator org_kde_kwin_dpms*()
261 {
262  return d->dpms;
263 }
264 
265 Dpms::operator org_kde_kwin_dpms*() const
266 {
267  return d->dpms;
268 }
269 
270 }
271 }
void setEventQueue(EventQueue *queue)
Sets the queue to use for creating a Dpms.
Definition: dpms.cpp:64
Wrapper class for wl_event_queue interface.
Definition: event_queue.h:55
Mode mode() const
The current Dpms mode.
Definition: dpms.cpp:228
Wrapper for the wl_output interface.
Definition: output.h:55
void release()
Releases the org_kde_kwin_dpms_manager interface.
Definition: dpms.cpp:37
bool isValid() const
Definition: dpms.cpp:47
void destroy()
Destroys the data held by this Dpms.
Definition: dpms.cpp:203
void requestMode(Mode mode)
Request to change the Output into Dpms mode.
Definition: dpms.cpp:233
void setup(org_kde_kwin_dpms_manager *manager)
Setup this DpmsManager to manage the manager.
Definition: dpms.cpp:52
EventQueue * eventQueue()
Definition: dpms.cpp:59
void release()
Releases the org_kde_kwin_dpms interface.
Definition: dpms.cpp:208
QPointer< Output > output() const
Definition: dpms.cpp:255
QObject(QObject *parent)
bool isSupported() const
Whether Dpms is supported for the Output.
Definition: dpms.cpp:223
void setup(org_kde_kwin_dpms *dpms)
Setup this Dpms to manage the dpms.
Definition: dpms.cpp:218
QObject * parent() const const
bool isValid() const
Definition: dpms.cpp:213
Power management for monitors.
Definition: dpms.h:131
DpmsManager(QObject *parent=nullptr)
Creates a new DpmsManager.
Definition: dpms.cpp:26
void supportedChanged()
Emitted if the supported state on the Output changes.
void destroy()
Destroys the data held by this DpmsManager.
Definition: dpms.cpp:42
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:48:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.