1/*
2 SPDX-FileCopyrightText: 2014 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 "compositor.h"
7#include "event_queue.h"
8#include "region.h"
9#include "surface.h"
10#include "wayland_pointer_p.h"
11// Qt
12#include <QGuiApplication>
13#include <QRegion>
14#include <qpa/qplatformnativeinterface.h>
15
16#include <wayland-client-protocol.h>
17
18namespace KWayland
19{
20namespace Client
21{
22class Q_DECL_HIDDEN Compositor::Private
23{
24public:
25 Private() = default;
26
27 WaylandPointer<wl_compositor, wl_compositor_destroy> compositor;
28 EventQueue *queue = nullptr;
29};
30
31Compositor::Compositor(QObject *parent)
32 : QObject(parent)
33 , d(new Private)
34{
35}
36
37Compositor::~Compositor()
38{
39 release();
40}
41
42Compositor *Compositor::fromApplication(QObject *parent)
43{
44 QPlatformNativeInterface *native = qApp->platformNativeInterface();
45 if (!native) {
46 return nullptr;
47 }
48 wl_compositor *compositor = reinterpret_cast<wl_compositor *>(native->nativeResourceForIntegration(QByteArrayLiteral("compositor")));
49 if (!compositor) {
50 return nullptr;
51 }
52 Compositor *c = new Compositor(parent);
53 c->d->compositor.setup(pointer: compositor, foreign: true);
54 return c;
55}
56
57void Compositor::setup(wl_compositor *compositor)
58{
59 Q_ASSERT(compositor);
60 Q_ASSERT(!d->compositor);
61 d->compositor.setup(pointer: compositor);
62}
63
64void Compositor::release()
65{
66 d->compositor.release();
67}
68
69void Compositor::destroy()
70{
71 d->compositor.destroy();
72}
73
74void Compositor::setEventQueue(EventQueue *queue)
75{
76 d->queue = queue;
77}
78
79EventQueue *Compositor::eventQueue()
80{
81 return d->queue;
82}
83
84Surface *Compositor::createSurface(QObject *parent)
85{
86 Q_ASSERT(isValid());
87 Surface *s = new Surface(parent);
88 auto w = wl_compositor_create_surface(wl_compositor: d->compositor);
89 if (d->queue) {
90 d->queue->addProxy(proxy: w);
91 }
92 s->setup(w);
93 return s;
94}
95
96Region *Compositor::createRegion(QObject *parent)
97{
98 return createRegion(region: QRegion(), parent);
99}
100
101Region *Compositor::createRegion(const QRegion &region, QObject *parent)
102{
103 Q_ASSERT(isValid());
104 Region *r = new Region(region, parent);
105 auto w = wl_compositor_create_region(wl_compositor: d->compositor);
106 if (d->queue) {
107 d->queue->addProxy(proxy: w);
108 }
109 r->setup(w);
110 return r;
111}
112
113std::unique_ptr<Region> Compositor::createRegion(const QRegion &region)
114{
115 return std::unique_ptr<Region>(createRegion(region, parent: nullptr));
116}
117
118Compositor::operator wl_compositor *()
119{
120 return d->compositor;
121}
122
123Compositor::operator wl_compositor *() const
124{
125 return d->compositor;
126}
127
128bool Compositor::isValid() const
129{
130 return d->compositor.isValid();
131}
132
133}
134}
135
136#include "moc_compositor.cpp"
137

source code of kwayland/src/client/compositor.cpp