File indexing completed on 2024-04-28 03:59:32

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2023 Nicolas Fella <nicolas.fella@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include <kwindowsystem.h>
0009 
0010 #include <QApplication>
0011 #include <QDialog>
0012 #include <QHBoxLayout>
0013 #include <QLabel>
0014 #include <QPushButton>
0015 
0016 #include <kwaylandextras.h>
0017 
0018 class Window : public QWidget
0019 {
0020 public:
0021     Window();
0022 
0023 private:
0024     void updateSerial();
0025     void requestToken();
0026 
0027     void exportWindow();
0028     void unexportWindow();
0029     void setExportedHandle(const QString &handle);
0030 
0031     QLabel *m_serialLabel;
0032     QLabel *m_tokenLabel;
0033     QLabel *m_exportedLabel;
0034 };
0035 
0036 Window::Window()
0037 {
0038     QPushButton *serialButton = new QPushButton("Update serial");
0039     connect(serialButton, &QPushButton::clicked, this, &Window::updateSerial);
0040 
0041     QPushButton *tokenButton = new QPushButton("Request token");
0042     connect(tokenButton, &QPushButton::clicked, this, &Window::requestToken);
0043 
0044     m_serialLabel = new QLabel;
0045     m_serialLabel->setText("Last input serial: " + QString::number(KWaylandExtras::self()->lastInputSerial(windowHandle())));
0046 
0047     m_tokenLabel = new QLabel;
0048     m_tokenLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
0049     m_tokenLabel->setText("XDG actvation token:");
0050 
0051     QHBoxLayout *exportLayout = new QHBoxLayout;
0052 
0053     QPushButton *exportButton = new QPushButton("Export window");
0054     connect(exportButton, &QPushButton::clicked, this, &Window::exportWindow);
0055 
0056     QPushButton *unexportButton = new QPushButton("Unexport window");
0057     connect(unexportButton, &QPushButton::clicked, this, &Window::unexportWindow);
0058 
0059     m_exportedLabel = new QLabel;
0060     m_exportedLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
0061     setExportedHandle(QString());
0062 
0063     QVBoxLayout *layout = new QVBoxLayout(this);
0064     layout->addWidget(serialButton);
0065     layout->addWidget(m_serialLabel);
0066     layout->addWidget(tokenButton);
0067     layout->addWidget(m_tokenLabel);
0068 
0069     exportLayout->addWidget(exportButton);
0070     exportLayout->addWidget(unexportButton);
0071     layout->addLayout(exportLayout);
0072     layout->addWidget(m_exportedLabel);
0073 }
0074 
0075 void Window::updateSerial()
0076 {
0077     m_serialLabel->setText("Last input serial: " + QString::number(KWaylandExtras::self()->lastInputSerial(windowHandle())));
0078 }
0079 
0080 void Window::requestToken()
0081 {
0082     connect(
0083         KWaylandExtras::self(),
0084         &KWaylandExtras::xdgActivationTokenArrived,
0085         this,
0086         [this](int /*serial*/, const QString &token) {
0087             m_tokenLabel->setText("XDG actvation token: " + token);
0088         },
0089         Qt::SingleShotConnection);
0090 
0091     KWaylandExtras::requestXdgActivationToken(windowHandle(), KWaylandExtras::self()->lastInputSerial(windowHandle()), QString());
0092 }
0093 
0094 void Window::exportWindow()
0095 {
0096     connect(
0097         KWaylandExtras::self(),
0098         &KWaylandExtras::windowExported,
0099         this,
0100         [this](QWindow *window, const QString &handle) {
0101             Q_UNUSED(window);
0102             setExportedHandle(handle);
0103         },
0104         Qt::SingleShotConnection);
0105     KWaylandExtras::exportWindow(windowHandle());
0106 }
0107 
0108 void Window::unexportWindow()
0109 {
0110     KWaylandExtras::unexportWindow(windowHandle());
0111     setExportedHandle(QString());
0112 }
0113 
0114 void Window::setExportedHandle(const QString &handle)
0115 {
0116     m_exportedLabel->setText("XDG foreign handle: " + handle);
0117 }
0118 
0119 int main(int argc, char **argv)
0120 {
0121     QApplication app(argc, argv);
0122     Window window;
0123     window.show();
0124     return app.exec();
0125 }