1 /*
2     SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
3 
4     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "KWayland/Client/compositor.h"
7 #include "KWayland/Client/connection_thread.h"
8 #include "KWayland/Client/datadevice.h"
9 #include "KWayland/Client/datadevicemanager.h"
10 #include "KWayland/Client/dataoffer.h"
11 #include "KWayland/Client/event_queue.h"
12 #include "KWayland/Client/keyboard.h"
13 #include "KWayland/Client/plasmashell.h"
14 #include "KWayland/Client/plasmawindowmanagement.h"
15 #include "KWayland/Client/pointer.h"
16 #include "KWayland/Client/registry.h"
17 #include "KWayland/Client/seat.h"
18 #include "KWayland/Client/shell.h"
19 #include "KWayland/Client/shm_pool.h"
20 #include "KWayland/Client/surface.h"
21 // Qt
22 #include <QDebug>
23 #include <QFile>
24 #include <QGuiApplication>
25 #include <QImage>
26 #include <QMimeType>
27 #include <QThread>
28 // system
29 #include <unistd.h>
30 
31 #include <linux/input.h>
32 
33 using namespace KWayland::Client;
34 
35 class PanelTest : public QObject
36 {
37     Q_OBJECT
38 public:
39     explicit PanelTest(QObject *parent = nullptr);
40     virtual ~PanelTest();
41 
42     void init();
43 
44 private:
45     void setupRegistry(Registry *registry);
46     void render();
47     void showTooltip(const QPointF &pos);
48     void hideTooltip();
49     void moveTooltip(const QPointF &pos);
50     QThread *m_connectionThread;
51     ConnectionThread *m_connectionThreadObject;
52     EventQueue *m_eventQueue = nullptr;
53     Compositor *m_compositor = nullptr;
54     Seat *m_seat = nullptr;
55     Shell *m_shell = nullptr;
56     ShellSurface *m_shellSurface = nullptr;
57     ShmPool *m_shm = nullptr;
58     Surface *m_surface = nullptr;
59     PlasmaShell *m_plasmaShell = nullptr;
60     PlasmaShellSurface *m_plasmaShellSurface = nullptr;
61     PlasmaWindowManagement *m_windowManagement = nullptr;
62     struct {
63         Surface *surface = nullptr;
64         ShellSurface *shellSurface = nullptr;
65         PlasmaShellSurface *plasmaSurface = nullptr;
66         bool visible = false;
67     } m_tooltip;
68 };
69 
PanelTest(QObject * parent)70 PanelTest::PanelTest(QObject *parent)
71     : QObject(parent)
72     , m_connectionThread(new QThread(this))
73     , m_connectionThreadObject(new ConnectionThread())
74 {
75 }
76 
~PanelTest()77 PanelTest::~PanelTest()
78 {
79     m_connectionThread->quit();
80     m_connectionThread->wait();
81     m_connectionThreadObject->deleteLater();
82 }
83 
init()84 void PanelTest::init()
85 {
86     connect(
87         m_connectionThreadObject,
88         &ConnectionThread::connected,
89         this,
90         [this] {
91             m_eventQueue = new EventQueue(this);
92             m_eventQueue->setup(m_connectionThreadObject);
93 
94             Registry *registry = new Registry(this);
95             setupRegistry(registry);
96         },
97         Qt::QueuedConnection);
98     m_connectionThreadObject->moveToThread(m_connectionThread);
99     m_connectionThread->start();
100 
101     m_connectionThreadObject->initConnection();
102 }
103 
showTooltip(const QPointF & pos)104 void PanelTest::showTooltip(const QPointF &pos)
105 {
106     if (!m_tooltip.surface) {
107         m_tooltip.surface = m_compositor->createSurface(this);
108         m_tooltip.shellSurface = m_shell->createSurface(m_tooltip.surface, this);
109         if (m_plasmaShell) {
110             m_tooltip.plasmaSurface = m_plasmaShell->createSurface(m_tooltip.surface, this);
111         }
112     }
113     m_tooltip.shellSurface->setTransient(m_surface, pos.toPoint());
114 
115     if (!m_tooltip.visible) {
116         const QSize size(100, 50);
117         auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
118         buffer->setUsed(true);
119         QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
120         image.fill(Qt::red);
121 
122         m_tooltip.surface->attachBuffer(*buffer);
123         m_tooltip.surface->damage(QRect(QPoint(0, 0), size));
124         m_tooltip.surface->commit(Surface::CommitFlag::None);
125         m_tooltip.visible = true;
126     }
127 }
128 
hideTooltip()129 void PanelTest::hideTooltip()
130 {
131     if (!m_tooltip.visible) {
132         return;
133     }
134     m_tooltip.surface->attachBuffer(Buffer::Ptr());
135     m_tooltip.surface->commit(Surface::CommitFlag::None);
136     m_tooltip.visible = false;
137 }
138 
moveTooltip(const QPointF & pos)139 void PanelTest::moveTooltip(const QPointF &pos)
140 {
141     if (m_tooltip.plasmaSurface) {
142         m_tooltip.plasmaSurface->setPosition(QPoint(10, 0) + pos.toPoint());
143     }
144 }
145 
setupRegistry(Registry * registry)146 void PanelTest::setupRegistry(Registry *registry)
147 {
148     connect(registry, &Registry::compositorAnnounced, this, [this, registry](quint32 name, quint32 version) {
149         m_compositor = registry->createCompositor(name, version, this);
150     });
151     connect(registry, &Registry::shellAnnounced, this, [this, registry](quint32 name, quint32 version) {
152         m_shell = registry->createShell(name, version, this);
153     });
154     connect(registry, &Registry::shmAnnounced, this, [this, registry](quint32 name, quint32 version) {
155         m_shm = registry->createShmPool(name, version, this);
156     });
157     connect(registry, &Registry::seatAnnounced, this, [this, registry](quint32 name, quint32 version) {
158         m_seat = registry->createSeat(name, version, this);
159         connect(m_seat, &Seat::hasPointerChanged, this, [this](bool has) {
160             if (!has) {
161                 return;
162             }
163             auto p = m_seat->createPointer(this);
164             connect(p, &Pointer::buttonStateChanged, this, [this](quint32 serial, quint32 time, quint32 button, KWayland::Client::Pointer::ButtonState state) {
165                 Q_UNUSED(time)
166                 Q_UNUSED(serial)
167                 if (!m_windowManagement) {
168                     return;
169                 }
170                 if (state == Pointer::ButtonState::Released) {
171                     return;
172                 }
173                 if (button == BTN_LEFT) {
174                     m_windowManagement->showDesktop();
175                 } else if (button == BTN_RIGHT) {
176                     m_windowManagement->hideDesktop();
177                 }
178             });
179             connect(p, &Pointer::entered, this, [this, p](quint32 serial, const QPointF &relativeToSurface) {
180                 Q_UNUSED(serial)
181                 if (p->enteredSurface() == m_surface) {
182                     showTooltip(relativeToSurface);
183                 }
184             });
185             connect(p, &Pointer::motion, this, [this, p](const QPointF &relativeToSurface) {
186                 if (p->enteredSurface() == m_surface) {
187                     moveTooltip(relativeToSurface);
188                 }
189             });
190             connect(p, &Pointer::left, this, [this] {
191                 hideTooltip();
192             });
193         });
194     });
195     connect(registry, &Registry::plasmaShellAnnounced, this, [this, registry](quint32 name, quint32 version) {
196         m_plasmaShell = registry->createPlasmaShell(name, version, this);
197     });
198     connect(registry, &Registry::plasmaWindowManagementAnnounced, this, [this, registry](quint32 name, quint32 version) {
199         m_windowManagement = registry->createPlasmaWindowManagement(name, version, this);
200         connect(m_windowManagement, &PlasmaWindowManagement::showingDesktopChanged, this, [](bool set) {
201             qDebug() << "Showing desktop changed, new state: " << set;
202         });
203         connect(m_windowManagement, &PlasmaWindowManagement::windowCreated, this, [this](PlasmaWindow *w) {
204             connect(w, &PlasmaWindow::titleChanged, this, [w] {
205                 qDebug() << "Window title changed to: " << w->title();
206             });
207             connect(w, &PlasmaWindow::activeChanged, this, [w] {
208                 qDebug() << "Window active changed: " << w->isActive();
209             });
210             connect(w, &PlasmaWindow::maximizedChanged, this, [w] {
211                 qDebug() << "Window maximized changed: " << w->isMaximized();
212             });
213             connect(w, &PlasmaWindow::maximizedChanged, this, [w] {
214                 qDebug() << "Window minimized changed: " << w->isMinimized();
215             });
216             connect(w, &PlasmaWindow::keepAboveChanged, this, [w] {
217                 qDebug() << "Window keep above changed: " << w->isKeepAbove();
218             });
219             connect(w, &PlasmaWindow::keepBelowChanged, this, [w] {
220                 qDebug() << "Window keep below changed: " << w->isKeepBelow();
221             });
222             connect(w, &PlasmaWindow::onAllDesktopsChanged, this, [w] {
223                 qDebug() << "Window on all desktops changed: " << w->isOnAllDesktops();
224             });
225             connect(w, &PlasmaWindow::fullscreenChanged, this, [w] {
226                 qDebug() << "Window full screen changed: " << w->isFullscreen();
227             });
228             connect(w, &PlasmaWindow::demandsAttentionChanged, this, [w] {
229                 qDebug() << "Window demands attention changed: " << w->isDemandingAttention();
230             });
231             connect(w, &PlasmaWindow::closeableChanged, this, [w] {
232                 qDebug() << "Window is closeable changed: " << w->isCloseable();
233             });
234             connect(w, &PlasmaWindow::minimizeableChanged, this, [w] {
235                 qDebug() << "Window is minimizeable changed: " << w->isMinimizeable();
236             });
237             connect(w, &PlasmaWindow::maximizeableChanged, this, [w] {
238                 qDebug() << "Window is maximizeable changed: " << w->isMaximizeable();
239             });
240             connect(w, &PlasmaWindow::fullscreenableChanged, this, [w] {
241                 qDebug() << "Window is fullscreenable changed: " << w->isFullscreenable();
242             });
243             connect(w, &PlasmaWindow::iconChanged, this, [w] {
244                 qDebug() << "Window icon changed: " << w->icon().name();
245             });
246         });
247     });
248     connect(registry, &Registry::interfacesAnnounced, this, [this] {
249         Q_ASSERT(m_compositor);
250         Q_ASSERT(m_seat);
251         Q_ASSERT(m_shell);
252         Q_ASSERT(m_shm);
253         m_surface = m_compositor->createSurface(this);
254         Q_ASSERT(m_surface);
255         m_shellSurface = m_shell->createSurface(m_surface, this);
256         Q_ASSERT(m_shellSurface);
257         m_shellSurface->setToplevel();
258         connect(m_shellSurface, &ShellSurface::sizeChanged, this, &PanelTest::render);
259         if (m_plasmaShell) {
260             m_plasmaShellSurface = m_plasmaShell->createSurface(m_surface, this);
261             m_plasmaShellSurface->setPosition(QPoint(10, 0));
262             m_plasmaShellSurface->setRole(PlasmaShellSurface::Role::Panel);
263         }
264         render();
265     });
266     registry->setEventQueue(m_eventQueue);
267     registry->create(m_connectionThreadObject);
268     registry->setup();
269 }
270 
render()271 void PanelTest::render()
272 {
273     const QSize &size = m_shellSurface->size().isValid() ? m_shellSurface->size() : QSize(300, 20);
274     auto buffer = m_shm->getBuffer(size, size.width() * 4).toStrongRef();
275     buffer->setUsed(true);
276     QImage image(buffer->address(), size.width(), size.height(), QImage::Format_ARGB32_Premultiplied);
277     image.fill(Qt::blue);
278 
279     m_surface->attachBuffer(*buffer);
280     m_surface->damage(QRect(QPoint(0, 0), size));
281     m_surface->commit(Surface::CommitFlag::None);
282     buffer->setUsed(false);
283 }
284 
main(int argc,char ** argv)285 int main(int argc, char **argv)
286 {
287     QGuiApplication app(argc, argv);
288     PanelTest client;
289     client.init();
290 
291     return app.exec();
292 }
293 
294 #include "paneltest.moc"
295