File: dpmstest.cpp

package info (click to toggle)
kwin 4%3A6.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,376 kB
  • sloc: cpp: 240,899; javascript: 2,225; xml: 2,096; ansic: 775; sh: 37; python: 15; makefile: 8
file content (167 lines) | stat: -rw-r--r-- 5,745 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
    SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "KWayland/Client/dpms.h"
#include "KWayland/Client/connection_thread.h"
#include "KWayland/Client/output.h"
#include "KWayland/Client/registry.h"
#include <QApplication>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>

using namespace KWayland::Client;

static QString modeToString(Dpms::Mode mode)
{
    switch (mode) {
    case Dpms::Mode::On:
        return QStringLiteral("On");
    case Dpms::Mode::Standby:
        return QStringLiteral("Standby");
    case Dpms::Mode::Suspend:
        return QStringLiteral("Suspend");
    case Dpms::Mode::Off:
        return QStringLiteral("Off");
    default:
        Q_UNREACHABLE();
    }
}

QString supportedToString(bool supported)
{
    return supported ? QStringLiteral("Yes") : QStringLiteral("No");
}

static QLayout *setupOutput(Registry::AnnouncedInterface outputInterface, Registry *registry, DpmsManager *manager)
{
    Output *output = registry->createOutput(outputInterface.name, outputInterface.version, registry);
    QLabel *label = new QLabel(output->model());
    QObject::connect(
        output,
        &Output::changed,
        label,
        [label, output] {
            label->setText(output->model());
        },
        Qt::QueuedConnection);

    Dpms *dpms = nullptr;
    if (manager) {
        dpms = manager->getDpms(output, output);
    }

    QFormLayout *dpmsForm = new QFormLayout;
    bool supported = dpms ? dpms->isSupported() : false;
    QLabel *supportedLabel = new QLabel(supportedToString(supported));
    dpmsForm->addRow(QStringLiteral("Supported:"), supportedLabel);
    QLabel *modeLabel = new QLabel(modeToString(dpms ? dpms->mode() : Dpms::Mode::On));
    dpmsForm->addRow(QStringLiteral("Mode:"), modeLabel);

    QPushButton *standbyButton = new QPushButton(QStringLiteral("Standby"));
    QPushButton *suspendButton = new QPushButton(QStringLiteral("Suspend"));
    QPushButton *offButton = new QPushButton(QStringLiteral("Off"));
    standbyButton->setEnabled(supported);
    suspendButton->setEnabled(supported);
    offButton->setEnabled(supported);
    QDialogButtonBox *bg = new QDialogButtonBox;
    bg->addButton(standbyButton, QDialogButtonBox::ActionRole);
    bg->addButton(suspendButton, QDialogButtonBox::ActionRole);
    bg->addButton(offButton, QDialogButtonBox::ActionRole);

    if (dpms) {
        QObject::connect(
            dpms,
            &Dpms::supportedChanged,
            supportedLabel,
            [supportedLabel, dpms, standbyButton, suspendButton, offButton] {
                const bool supported = dpms->isSupported();
                supportedLabel->setText(supportedToString(supported));
                standbyButton->setEnabled(supported);
                suspendButton->setEnabled(supported);
                offButton->setEnabled(supported);
            },
            Qt::QueuedConnection);
        QObject::connect(
            dpms,
            &Dpms::modeChanged,
            modeLabel,
            [modeLabel, dpms] {
                modeLabel->setText(modeToString(dpms->mode()));
            },
            Qt::QueuedConnection);
        QObject::connect(standbyButton, &QPushButton::clicked, dpms, [dpms] {
            dpms->requestMode(Dpms::Mode::Standby);
        });
        QObject::connect(suspendButton, &QPushButton::clicked, dpms, [dpms] {
            dpms->requestMode(Dpms::Mode::Suspend);
        });
        QObject::connect(offButton, &QPushButton::clicked, dpms, [dpms] {
            dpms->requestMode(Dpms::Mode::Off);
        });
    }

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(label);
    layout->addLayout(dpmsForm);
    layout->addWidget(bg);
    return layout;
}

int main(int argc, char **argv)
{
    qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("wayland"));
    QApplication app(argc, argv);

    QWidget window;

    ConnectionThread *connection = ConnectionThread::fromApplication();
    Registry registry;
    registry.create(connection);
    QObject::connect(
        &registry,
        &Registry::interfacesAnnounced,
        &app,
        [&registry, &window] {
            const bool hasDpms = registry.hasInterface(Registry::Interface::Dpms);
            QLabel *hasDpmsLabel = new QLabel(&window);
            if (hasDpms) {
                hasDpmsLabel->setText(QStringLiteral("Compositor provides a DpmsManager"));
            } else {
                hasDpmsLabel->setText(QStringLiteral("Compositor does not provide a DpmsManager"));
            }

            QVBoxLayout *layout = new QVBoxLayout;
            layout->addWidget(hasDpmsLabel);
            QFrame *hline = new QFrame;
            hline->setFrameShape(QFrame::HLine);
            layout->addWidget(hline);

            DpmsManager *dpmsManager = nullptr;
            if (hasDpms) {
                const auto dpmsData = registry.interface(Registry::Interface::Dpms);
                dpmsManager = registry.createDpmsManager(dpmsData.name, dpmsData.version);
            }

            // get all Outputs
            const auto outputs = registry.interfaces(Registry::Interface::Output);
            for (auto o : outputs) {
                layout->addLayout(setupOutput(o, &registry, dpmsManager));
                QFrame *hline = new QFrame;
                hline->setFrameShape(QFrame::HLine);
                layout->addWidget(hline);
            }

            window.setLayout(layout);
            window.show();
        },
        Qt::QueuedConnection);
    registry.setup();

    return app.exec();
}