1 /********************************************************************
2 Copyright 2015 Martin Gräßlin <mgraesslin@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), which shall
10 act as a proxy defined in Section 6 of version 3 of the license.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
20 #include "../src/client/dpms.h"
21 #include "../src/client/connection_thread.h"
22 #include "../src/client/output.h"
23 #include "../src/client/registry.h"
24 #include <QApplication>
25 #include <QDialogButtonBox>
26 #include <QFormLayout>
27 #include <QLabel>
28 #include <QPushButton>
29 #include <QVBoxLayout>
30 #include <QWidget>
31
32 using namespace Wrapland::Client;
33
modeToString(Dpms::Mode mode)34 static QString modeToString(Dpms::Mode mode)
35 {
36 switch (mode) {
37 case Dpms::Mode::On:
38 return QStringLiteral("On");
39 case Dpms::Mode::Standby:
40 return QStringLiteral("Standby");
41 case Dpms::Mode::Suspend:
42 return QStringLiteral("Suspend");
43 case Dpms::Mode::Off:
44 return QStringLiteral("Off");
45 default:
46 Q_UNREACHABLE();
47 }
48 }
49
supportedToString(bool supported)50 QString supportedToString(bool supported)
51 {
52 return supported ? QStringLiteral("Yes") : QStringLiteral("No");
53 }
54
55 static QLayout*
setupOutput(Registry::AnnouncedInterface outputInterface,Registry * registry,DpmsManager * manager)56 setupOutput(Registry::AnnouncedInterface outputInterface, Registry* registry, DpmsManager* manager)
57 {
58 Output* output
59 = registry->createOutput(outputInterface.name, outputInterface.version, registry);
60 QLabel* label = new QLabel(output->model());
61 QObject::connect(
62 output,
63 &Output::changed,
64 label,
65 [label, output] { label->setText(output->model()); },
66 Qt::QueuedConnection);
67
68 Dpms* dpms = nullptr;
69 if (manager) {
70 dpms = manager->getDpms(output, output);
71 }
72
73 QFormLayout* dpmsForm = new QFormLayout;
74 bool supported = dpms ? dpms->isSupported() : false;
75 QLabel* supportedLabel = new QLabel(supportedToString(supported));
76 dpmsForm->addRow(QStringLiteral("Supported:"), supportedLabel);
77 QLabel* modeLabel = new QLabel(modeToString(dpms ? dpms->mode() : Dpms::Mode::On));
78 dpmsForm->addRow(QStringLiteral("Mode:"), modeLabel);
79
80 QPushButton* standbyButton = new QPushButton(QStringLiteral("Standby"));
81 QPushButton* suspendButton = new QPushButton(QStringLiteral("Suspend"));
82 QPushButton* offButton = new QPushButton(QStringLiteral("Off"));
83 standbyButton->setEnabled(supported);
84 suspendButton->setEnabled(supported);
85 offButton->setEnabled(supported);
86 QDialogButtonBox* bg = new QDialogButtonBox;
87 bg->addButton(standbyButton, QDialogButtonBox::ActionRole);
88 bg->addButton(suspendButton, QDialogButtonBox::ActionRole);
89 bg->addButton(offButton, QDialogButtonBox::ActionRole);
90
91 if (dpms) {
92 QObject::connect(
93 dpms,
94 &Dpms::supportedChanged,
95 supportedLabel,
96 [supportedLabel, dpms, standbyButton, suspendButton, offButton] {
97 const bool supported = dpms->isSupported();
98 supportedLabel->setText(supportedToString(supported));
99 standbyButton->setEnabled(supported);
100 suspendButton->setEnabled(supported);
101 offButton->setEnabled(supported);
102 },
103 Qt::QueuedConnection);
104 QObject::connect(
105 dpms,
106 &Dpms::modeChanged,
107 modeLabel,
108 [modeLabel, dpms] { modeLabel->setText(modeToString(dpms->mode())); },
109 Qt::QueuedConnection);
110 QObject::connect(standbyButton, &QPushButton::clicked, dpms, [dpms] {
111 dpms->requestMode(Dpms::Mode::Standby);
112 });
113 QObject::connect(suspendButton, &QPushButton::clicked, dpms, [dpms] {
114 dpms->requestMode(Dpms::Mode::Suspend);
115 });
116 QObject::connect(
117 offButton, &QPushButton::clicked, dpms, [dpms] { dpms->requestMode(Dpms::Mode::Off); });
118 }
119
120 QVBoxLayout* layout = new QVBoxLayout;
121 layout->addWidget(label);
122 layout->addLayout(dpmsForm);
123 layout->addWidget(bg);
124 return layout;
125 }
126
main(int argc,char ** argv)127 int main(int argc, char** argv)
128 {
129 qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("wayland"));
130 QApplication app(argc, argv);
131
132 QWidget window;
133
134 ConnectionThread* connection = ConnectionThread::fromApplication();
135 Registry registry;
136 registry.create(connection);
137 QObject::connect(
138 ®istry,
139 &Registry::interfacesAnnounced,
140 &app,
141 [®istry, &window] {
142 const bool hasDpms = registry.hasInterface(Registry::Interface::Dpms);
143 QLabel* hasDpmsLabel = new QLabel(&window);
144 if (hasDpms) {
145 hasDpmsLabel->setText(QStringLiteral("Compositor provides a DpmsManager"));
146 } else {
147 hasDpmsLabel->setText(QStringLiteral("Compositor does not provide a DpmsManager"));
148 }
149
150 QVBoxLayout* layout = new QVBoxLayout;
151 layout->addWidget(hasDpmsLabel);
152 QFrame* hline = new QFrame;
153 hline->setFrameShape(QFrame::HLine);
154 layout->addWidget(hline);
155
156 DpmsManager* dpmsManager = nullptr;
157 if (hasDpms) {
158 const auto dpmsData = registry.interface(Registry::Interface::Dpms);
159 dpmsManager = registry.createDpmsManager(dpmsData.name, dpmsData.version);
160 }
161
162 // get all Outputs
163 const auto outputs = registry.interfaces(Registry::Interface::Output);
164 for (auto o : outputs) {
165 layout->addLayout(setupOutput(o, ®istry, dpmsManager));
166 QFrame* hline = new QFrame;
167 hline->setFrameShape(QFrame::HLine);
168 layout->addWidget(hline);
169 }
170
171 window.setLayout(layout);
172 window.show();
173 },
174 Qt::QueuedConnection);
175 registry.setup();
176
177 return app.exec();
178 }
179