KWayland

dpms_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 "dpms_interface_p.h"
7 #include "display.h"
8 #include "output_interface.h"
9 
10 namespace KWayland
11 {
12 namespace Server
13 {
14 
15 const quint32 DpmsManagerInterface::Private::s_version = 1;
16 
17 #ifndef K_DOXYGEN
18 const struct org_kde_kwin_dpms_manager_interface DpmsManagerInterface::Private::s_interface = {
19  getDpmsCallback
20 };
21 #endif
22 
23 DpmsManagerInterface::Private::Private(DpmsManagerInterface *qptr, Display *d)
24  : Global::Private(d, &org_kde_kwin_dpms_manager_interface, s_version)
25  , q(qptr)
26 {
27 }
28 
29 void DpmsManagerInterface::Private::bind(wl_client *client, uint32_t version, uint32_t id)
30 {
31  auto c = display->getConnection(client);
32  wl_resource *dpms = c->createResource(&org_kde_kwin_dpms_manager_interface, qMin(version, s_version), id);
33  if (!dpms) {
34  wl_client_post_no_memory(client);
35  return;
36  }
37  wl_resource_set_implementation(dpms, &s_interface, this, nullptr);
38 }
39 
40 void DpmsManagerInterface::Private::getDpmsCallback(wl_client *client, wl_resource *resource, uint32_t id, wl_resource *output)
41 {
42  auto p = Private::cast(resource);
43  auto c = p->display->getConnection(client);
44  OutputInterface *o = OutputInterface::get(output);
45  DpmsInterface *dpms = new DpmsInterface(o, resource, p->q);
46  dpms->create(c, wl_resource_get_version(resource), id);
47  if (!dpms->resource()) {
48  wl_resource_post_no_memory(resource);
49  return;
50  }
51  dpms->sendSupported();
52  dpms->sendMode();
53  dpms->sendDone();
54 }
55 
56 DpmsManagerInterface::DpmsManagerInterface(Display *display, QObject *parent)
57  : Global(new Private(this, display), parent)
58 {
59 }
60 
61 DpmsManagerInterface::~DpmsManagerInterface() = default;
62 
63 
64 #ifndef K_DOXYGEN
65 const struct org_kde_kwin_dpms_interface DpmsInterface::Private::s_interface = {
66  setCallback,
67  resourceDestroyedCallback
68 };
69 #endif
70 
71 DpmsInterface::Private::Private(DpmsInterface *q, DpmsManagerInterface *g, wl_resource *parentResource, OutputInterface *outputInterface)
72  : Resource::Private(q, g, parentResource, &org_kde_kwin_dpms_interface, &s_interface)
73  , output(outputInterface)
74 {
75 }
76 
77 void DpmsInterface::Private::setCallback(wl_client *client, wl_resource *resource, uint32_t mode)
78 {
79  Q_UNUSED(client)
80  OutputInterface::DpmsMode dpmsMode;
81  switch (mode) {
82  case ORG_KDE_KWIN_DPMS_MODE_ON:
83  dpmsMode = OutputInterface::DpmsMode::On;
84  break;
85  case ORG_KDE_KWIN_DPMS_MODE_STANDBY:
86  dpmsMode = OutputInterface::DpmsMode::Standby;
87  break;
88  case ORG_KDE_KWIN_DPMS_MODE_SUSPEND:
89  dpmsMode = OutputInterface::DpmsMode::Suspend;
90  break;
91  case ORG_KDE_KWIN_DPMS_MODE_OFF:
92  dpmsMode = OutputInterface::DpmsMode::Off;
93  break;
94  default:
95  return;
96  }
97  emit cast<Private>(resource)->output->dpmsModeRequested(dpmsMode);
98 }
99 
100 DpmsInterface::DpmsInterface(OutputInterface *output, wl_resource *parentResource, DpmsManagerInterface *manager)
101  : Resource(new Private(this, manager, parentResource, output))
102 {
103  connect(output, &OutputInterface::dpmsSupportedChanged, this,
104  [this] {
105  sendSupported();
106  sendDone();
107  }
108  );
109  connect(output, &OutputInterface::dpmsModeChanged, this,
110  [this] {
111  sendMode();
112  sendDone();
113  }
114  );
115 }
116 
117 DpmsInterface::~DpmsInterface() = default;
118 
119 void DpmsInterface::sendSupported()
120 {
121  Q_D();
122  org_kde_kwin_dpms_send_supported(d->resource, d->output->isDpmsSupported() ? 1 : 0);
123 }
124 
125 void DpmsInterface::sendMode()
126 {
127  Q_D();
128  const auto mode = d->output->dpmsMode();
129  org_kde_kwin_dpms_mode wlMode;
130  switch (mode) {
131  case OutputInterface::DpmsMode::On:
132  wlMode = ORG_KDE_KWIN_DPMS_MODE_ON;
133  break;
134  case OutputInterface::DpmsMode::Standby:
135  wlMode = ORG_KDE_KWIN_DPMS_MODE_STANDBY;
136  break;
137  case OutputInterface::DpmsMode::Suspend:
138  wlMode = ORG_KDE_KWIN_DPMS_MODE_SUSPEND;
139  break;
140  case OutputInterface::DpmsMode::Off:
141  wlMode = ORG_KDE_KWIN_DPMS_MODE_OFF;
142  break;
143  default:
144  Q_UNREACHABLE();
145  }
146  org_kde_kwin_dpms_send_mode(d->resource, wlMode);
147 }
148 
149 void DpmsInterface::sendDone()
150 {
151  Q_D();
152  org_kde_kwin_dpms_send_done(d->resource);
153  client()->flush();
154 }
155 
156 DpmsInterface::Private *DpmsInterface::d_func() const
157 {
158  return reinterpret_cast<Private*>(d.data());
159 }
160 
161 }
162 }
Mode mode() const
The current Dpms mode.
Definition: dpms.cpp:228
T * data() const const
QPointer< Output > output() const
Definition: dpms.cpp:255
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
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.