File indexing completed on 2024-12-01 05:02:03

0001 /*
0002     SPDX-FileCopyrightText: 2016 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/compositor.h"
0007 #include "KWayland/Client/connection_thread.h"
0008 #include "KWayland/Client/event_queue.h"
0009 #include "KWayland/Client/plasmashell.h"
0010 #include "KWayland/Client/registry.h"
0011 #include "KWayland/Client/shell.h"
0012 #include "KWayland/Client/shm_pool.h"
0013 #include "KWayland/Client/surface.h"
0014 // Qt
0015 #include <QCommandLineParser>
0016 #include <QGuiApplication>
0017 #include <QImage>
0018 #include <QThread>
0019 
0020 using namespace KWayland::Client;
0021 
0022 class PlasmaSurfaceTest : public QObject
0023 {
0024     Q_OBJECT
0025 public:
0026     explicit PlasmaSurfaceTest(QObject *parent = nullptr);
0027     virtual ~PlasmaSurfaceTest();
0028 
0029     void init();
0030 
0031     void setRole(PlasmaShellSurface::Role role)
0032     {
0033         m_role = role;
0034     }
0035     void setSkipTaskbar(bool set)
0036     {
0037         m_skipTaskbar = set;
0038     }
0039 
0040     void setSkipSwitcher(bool set)
0041     {
0042         m_skipSwitcher = set;
0043     }
0044 
0045 private:
0046     void setupRegistry(Registry *registry);
0047     void render();
0048     QThread *m_connectionThread;
0049     ConnectionThread *m_connectionThreadObject;
0050     EventQueue *m_eventQueue = nullptr;
0051     Compositor *m_compositor = nullptr;
0052     Shell *m_shell = nullptr;
0053     ShellSurface *m_shellSurface = nullptr;
0054     ShmPool *m_shm = nullptr;
0055     Surface *m_surface = nullptr;
0056     PlasmaShell *m_plasmaShell = nullptr;
0057     PlasmaShellSurface *m_plasmaShellSurface = nullptr;
0058     PlasmaShellSurface::Role m_role = PlasmaShellSurface::Role::Normal;
0059     bool m_skipTaskbar = false;
0060     bool m_skipSwitcher = false;
0061 };
0062 
0063 PlasmaSurfaceTest::PlasmaSurfaceTest(QObject *parent)
0064     : QObject(parent)
0065     , m_connectionThread(new QThread(this))
0066     , m_connectionThreadObject(new ConnectionThread())
0067 {
0068 }
0069 
0070 PlasmaSurfaceTest::~PlasmaSurfaceTest()
0071 {
0072     m_connectionThread->quit();
0073     m_connectionThread->wait();
0074     m_connectionThreadObject->deleteLater();
0075 }
0076 
0077 void PlasmaSurfaceTest::init()
0078 {
0079     connect(
0080         m_connectionThreadObject,
0081         &ConnectionThread::connected,
0082         this,
0083         [this] {
0084             m_eventQueue = new EventQueue(this);
0085             m_eventQueue->setup(m_connectionThreadObject);
0086 
0087             Registry *registry = new Registry(this);
0088             setupRegistry(registry);
0089         },
0090         Qt::QueuedConnection);
0091     m_connectionThreadObject->moveToThread(m_connectionThread);
0092     m_connectionThread->start();
0093 
0094     m_connectionThreadObject->initConnection();
0095 }
0096 
0097 void PlasmaSurfaceTest::setupRegistry(Registry *registry)
0098 {
0099     connect(registry, &Registry::compositorAnnounced, this, [this, registry](quint32 name, quint32 version) {
0100         m_compositor = registry->createCompositor(name, version, this);
0101     });
0102     connect(registry, &Registry::shellAnnounced, this, [this, registry](quint32 name, quint32 version) {
0103         m_shell = registry->createShell(name, version, this);
0104     });
0105     connect(registry, &Registry::shmAnnounced, this, [this, registry](quint32 name, quint32 version) {
0106         m_shm = registry->createShmPool(name, version, this);
0107     });
0108     connect(registry, &Registry::plasmaShellAnnounced, this, [this, registry](quint32 name, quint32 version) {
0109         m_plasmaShell = registry->createPlasmaShell(name, version, this);
0110         m_plasmaShell->setEventQueue(m_eventQueue);
0111     });
0112     connect(registry, &Registry::interfacesAnnounced, this, [this] {
0113         Q_ASSERT(m_compositor);
0114         Q_ASSERT(m_shell);
0115         Q_ASSERT(m_shm);
0116         Q_ASSERT(m_plasmaShell);
0117         m_surface = m_compositor->createSurface(this);
0118         Q_ASSERT(m_surface);
0119         m_shellSurface = m_shell->createSurface(m_surface, this);
0120         Q_ASSERT(m_shellSurface);
0121         m_shellSurface->setToplevel();
0122         connect(m_shellSurface, &ShellSurface::sizeChanged, this, &PlasmaSurfaceTest::render);
0123         m_plasmaShellSurface = m_plasmaShell->createSurface(m_surface, this);
0124         Q_ASSERT(m_plasmaShellSurface);
0125         m_plasmaShellSurface->setSkipTaskbar(m_skipTaskbar);
0126         m_plasmaShellSurface->setSkipSwitcher(m_skipSwitcher);
0127         m_plasmaShellSurface->setRole(m_role);
0128         render();
0129     });
0130     registry->setEventQueue(m_eventQueue);
0131     registry->create(m_connectionThreadObject);
0132     registry->setup();
0133 }
0134 
0135 void PlasmaSurfaceTest::render()
0136 {
0137     const QSize &size = m_shellSurface->size().isValid() ? m_shellSurface->size() : QSize(300, 200);
0138     auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
0139     buffer->setUsed(true);
0140     QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
0141     image.fill(QColor(255, 255, 255, 128));
0142 
0143     m_surface->attachBuffer(*buffer);
0144     m_surface->damage(QRect(QPoint(0, 0), size));
0145     m_surface->commit(Surface::CommitFlag::None);
0146     buffer->setUsed(false);
0147 }
0148 
0149 int main(int argc, char **argv)
0150 {
0151     QCoreApplication app(argc, argv);
0152     QCommandLineParser parser;
0153     parser.addHelpOption();
0154     QCommandLineOption notificationOption(QStringLiteral("notification"));
0155     parser.addOption(notificationOption);
0156     QCommandLineOption criticalNotificationOption(QStringLiteral("criticalNotification"));
0157     parser.addOption(criticalNotificationOption);
0158     QCommandLineOption appletPopupOption(QStringLiteral("appletPopup"));
0159     parser.addOption(appletPopupOption);
0160     QCommandLineOption panelOption(QStringLiteral("panel"));
0161     parser.addOption(panelOption);
0162     QCommandLineOption desktopOption(QStringLiteral("desktop"));
0163     parser.addOption(desktopOption);
0164     QCommandLineOption osdOption(QStringLiteral("osd"));
0165     parser.addOption(osdOption);
0166     QCommandLineOption tooltipOption(QStringLiteral("tooltip"));
0167     parser.addOption(tooltipOption);
0168     QCommandLineOption skipTaskbarOption(QStringLiteral("skipTaskbar"));
0169     parser.addOption(skipTaskbarOption);
0170     parser.process(app);
0171     QCommandLineOption skipSwitcherOption(QStringLiteral("skipSwitcher"));
0172     parser.addOption(skipSwitcherOption);
0173     parser.process(app);
0174 
0175     PlasmaSurfaceTest client;
0176 
0177     if (parser.isSet(notificationOption)) {
0178         client.setRole(PlasmaShellSurface::Role::Notification);
0179     } else if (parser.isSet(criticalNotificationOption)) {
0180         client.setRole(PlasmaShellSurface::Role::CriticalNotification);
0181     } else if (parser.isSet(appletPopupOption)) {
0182         client.setRole(PlasmaShellSurface::Role::AppletPopup);
0183     } else if (parser.isSet(panelOption)) {
0184         client.setRole(PlasmaShellSurface::Role::Panel);
0185     } else if (parser.isSet(desktopOption)) {
0186         client.setRole(PlasmaShellSurface::Role::Desktop);
0187     } else if (parser.isSet(osdOption)) {
0188         client.setRole(PlasmaShellSurface::Role::OnScreenDisplay);
0189     } else if (parser.isSet(tooltipOption)) {
0190         client.setRole(PlasmaShellSurface::Role::ToolTip);
0191     }
0192     client.setSkipTaskbar(parser.isSet(skipTaskbarOption));
0193     client.setSkipSwitcher(parser.isSet(skipSwitcherOption));
0194 
0195     client.init();
0196 
0197     return app.exec();
0198 }
0199 
0200 #include "plasmasurfacetest.moc"