File indexing completed on 2024-11-10 04:58:08
0001 /* 0002 SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 #include "KWayland/Client/dpms.h" 0007 #include "KWayland/Client/connection_thread.h" 0008 #include "KWayland/Client/output.h" 0009 #include "KWayland/Client/registry.h" 0010 #include <QApplication> 0011 #include <QDialogButtonBox> 0012 #include <QFormLayout> 0013 #include <QLabel> 0014 #include <QPushButton> 0015 #include <QVBoxLayout> 0016 #include <QWidget> 0017 0018 using namespace KWayland::Client; 0019 0020 static QString modeToString(Dpms::Mode mode) 0021 { 0022 switch (mode) { 0023 case Dpms::Mode::On: 0024 return QStringLiteral("On"); 0025 case Dpms::Mode::Standby: 0026 return QStringLiteral("Standby"); 0027 case Dpms::Mode::Suspend: 0028 return QStringLiteral("Suspend"); 0029 case Dpms::Mode::Off: 0030 return QStringLiteral("Off"); 0031 default: 0032 Q_UNREACHABLE(); 0033 } 0034 } 0035 0036 QString supportedToString(bool supported) 0037 { 0038 return supported ? QStringLiteral("Yes") : QStringLiteral("No"); 0039 } 0040 0041 static QLayout *setupOutput(Registry::AnnouncedInterface outputInterface, Registry *registry, DpmsManager *manager) 0042 { 0043 Output *output = registry->createOutput(outputInterface.name, outputInterface.version, registry); 0044 QLabel *label = new QLabel(output->model()); 0045 QObject::connect( 0046 output, 0047 &Output::changed, 0048 label, 0049 [label, output] { 0050 label->setText(output->model()); 0051 }, 0052 Qt::QueuedConnection); 0053 0054 Dpms *dpms = nullptr; 0055 if (manager) { 0056 dpms = manager->getDpms(output, output); 0057 } 0058 0059 QFormLayout *dpmsForm = new QFormLayout; 0060 bool supported = dpms ? dpms->isSupported() : false; 0061 QLabel *supportedLabel = new QLabel(supportedToString(supported)); 0062 dpmsForm->addRow(QStringLiteral("Supported:"), supportedLabel); 0063 QLabel *modeLabel = new QLabel(modeToString(dpms ? dpms->mode() : Dpms::Mode::On)); 0064 dpmsForm->addRow(QStringLiteral("Mode:"), modeLabel); 0065 0066 QPushButton *standbyButton = new QPushButton(QStringLiteral("Standby")); 0067 QPushButton *suspendButton = new QPushButton(QStringLiteral("Suspend")); 0068 QPushButton *offButton = new QPushButton(QStringLiteral("Off")); 0069 standbyButton->setEnabled(supported); 0070 suspendButton->setEnabled(supported); 0071 offButton->setEnabled(supported); 0072 QDialogButtonBox *bg = new QDialogButtonBox; 0073 bg->addButton(standbyButton, QDialogButtonBox::ActionRole); 0074 bg->addButton(suspendButton, QDialogButtonBox::ActionRole); 0075 bg->addButton(offButton, QDialogButtonBox::ActionRole); 0076 0077 if (dpms) { 0078 QObject::connect( 0079 dpms, 0080 &Dpms::supportedChanged, 0081 supportedLabel, 0082 [supportedLabel, dpms, standbyButton, suspendButton, offButton] { 0083 const bool supported = dpms->isSupported(); 0084 supportedLabel->setText(supportedToString(supported)); 0085 standbyButton->setEnabled(supported); 0086 suspendButton->setEnabled(supported); 0087 offButton->setEnabled(supported); 0088 }, 0089 Qt::QueuedConnection); 0090 QObject::connect( 0091 dpms, 0092 &Dpms::modeChanged, 0093 modeLabel, 0094 [modeLabel, dpms] { 0095 modeLabel->setText(modeToString(dpms->mode())); 0096 }, 0097 Qt::QueuedConnection); 0098 QObject::connect(standbyButton, &QPushButton::clicked, dpms, [dpms] { 0099 dpms->requestMode(Dpms::Mode::Standby); 0100 }); 0101 QObject::connect(suspendButton, &QPushButton::clicked, dpms, [dpms] { 0102 dpms->requestMode(Dpms::Mode::Suspend); 0103 }); 0104 QObject::connect(offButton, &QPushButton::clicked, dpms, [dpms] { 0105 dpms->requestMode(Dpms::Mode::Off); 0106 }); 0107 } 0108 0109 QVBoxLayout *layout = new QVBoxLayout; 0110 layout->addWidget(label); 0111 layout->addLayout(dpmsForm); 0112 layout->addWidget(bg); 0113 return layout; 0114 } 0115 0116 int main(int argc, char **argv) 0117 { 0118 qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("wayland")); 0119 QApplication app(argc, argv); 0120 0121 QWidget window; 0122 0123 ConnectionThread *connection = ConnectionThread::fromApplication(); 0124 Registry registry; 0125 registry.create(connection); 0126 QObject::connect( 0127 ®istry, 0128 &Registry::interfacesAnnounced, 0129 &app, 0130 [®istry, &window] { 0131 const bool hasDpms = registry.hasInterface(Registry::Interface::Dpms); 0132 QLabel *hasDpmsLabel = new QLabel(&window); 0133 if (hasDpms) { 0134 hasDpmsLabel->setText(QStringLiteral("Compositor provides a DpmsManager")); 0135 } else { 0136 hasDpmsLabel->setText(QStringLiteral("Compositor does not provide a DpmsManager")); 0137 } 0138 0139 QVBoxLayout *layout = new QVBoxLayout; 0140 layout->addWidget(hasDpmsLabel); 0141 QFrame *hline = new QFrame; 0142 hline->setFrameShape(QFrame::HLine); 0143 layout->addWidget(hline); 0144 0145 DpmsManager *dpmsManager = nullptr; 0146 if (hasDpms) { 0147 const auto dpmsData = registry.interface(Registry::Interface::Dpms); 0148 dpmsManager = registry.createDpmsManager(dpmsData.name, dpmsData.version); 0149 } 0150 0151 // get all Outputs 0152 const auto outputs = registry.interfaces(Registry::Interface::Output); 0153 for (auto o : outputs) { 0154 layout->addLayout(setupOutput(o, ®istry, dpmsManager)); 0155 QFrame *hline = new QFrame; 0156 hline->setFrameShape(QFrame::HLine); 0157 layout->addWidget(hline); 0158 } 0159 0160 window.setLayout(layout); 0161 window.show(); 0162 }, 0163 Qt::QueuedConnection); 0164 registry.setup(); 0165 0166 return app.exec(); 0167 }