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 "shadow.h" |
7 | #include "event_queue.h" |
8 | #include "surface.h" |
9 | #include "wayland_pointer_p.h" |
10 | |
11 | #include <QMarginsF> |
12 | |
13 | #include <wayland-shadow-client-protocol.h> |
14 | |
15 | namespace KWayland |
16 | { |
17 | namespace Client |
18 | { |
19 | class Q_DECL_HIDDEN ShadowManager::Private |
20 | { |
21 | public: |
22 | Private() = default; |
23 | |
24 | WaylandPointer<org_kde_kwin_shadow_manager, org_kde_kwin_shadow_manager_destroy> manager; |
25 | EventQueue *queue = nullptr; |
26 | }; |
27 | |
28 | ShadowManager::ShadowManager(QObject *parent) |
29 | : QObject(parent) |
30 | , d(new Private) |
31 | { |
32 | } |
33 | |
34 | ShadowManager::~ShadowManager() |
35 | { |
36 | release(); |
37 | } |
38 | |
39 | void ShadowManager::release() |
40 | { |
41 | d->manager.release(); |
42 | } |
43 | |
44 | void ShadowManager::destroy() |
45 | { |
46 | d->manager.destroy(); |
47 | } |
48 | |
49 | bool ShadowManager::isValid() const |
50 | { |
51 | return d->manager.isValid(); |
52 | } |
53 | |
54 | void ShadowManager::setup(org_kde_kwin_shadow_manager *manager) |
55 | { |
56 | Q_ASSERT(manager); |
57 | Q_ASSERT(!d->manager); |
58 | d->manager.setup(pointer: manager); |
59 | } |
60 | |
61 | void ShadowManager::setEventQueue(EventQueue *queue) |
62 | { |
63 | d->queue = queue; |
64 | } |
65 | |
66 | EventQueue *ShadowManager::eventQueue() |
67 | { |
68 | return d->queue; |
69 | } |
70 | |
71 | Shadow *ShadowManager::createShadow(Surface *surface, QObject *parent) |
72 | { |
73 | Q_ASSERT(isValid()); |
74 | Shadow *s = new Shadow(parent); |
75 | auto w = org_kde_kwin_shadow_manager_create(org_kde_kwin_shadow_manager: d->manager, surface: *surface); |
76 | if (d->queue) { |
77 | d->queue->addProxy(proxy: w); |
78 | } |
79 | s->setup(w); |
80 | return s; |
81 | } |
82 | |
83 | void ShadowManager::removeShadow(Surface *surface) |
84 | { |
85 | Q_ASSERT(isValid()); |
86 | org_kde_kwin_shadow_manager_unset(org_kde_kwin_shadow_manager: d->manager, surface: *surface); |
87 | } |
88 | |
89 | ShadowManager::operator org_kde_kwin_shadow_manager *() |
90 | { |
91 | return d->manager; |
92 | } |
93 | |
94 | ShadowManager::operator org_kde_kwin_shadow_manager *() const |
95 | { |
96 | return d->manager; |
97 | } |
98 | |
99 | class Shadow::Private |
100 | { |
101 | public: |
102 | WaylandPointer<org_kde_kwin_shadow, org_kde_kwin_shadow_destroy> shadow; |
103 | }; |
104 | |
105 | Shadow::Shadow(QObject *parent) |
106 | : QObject(parent) |
107 | , d(new Private) |
108 | { |
109 | } |
110 | |
111 | Shadow::~Shadow() |
112 | { |
113 | release(); |
114 | } |
115 | |
116 | void Shadow::release() |
117 | { |
118 | d->shadow.release(); |
119 | } |
120 | |
121 | void Shadow::setup(org_kde_kwin_shadow *shadow) |
122 | { |
123 | Q_ASSERT(shadow); |
124 | Q_ASSERT(!d->shadow); |
125 | d->shadow.setup(pointer: shadow); |
126 | } |
127 | |
128 | void Shadow::destroy() |
129 | { |
130 | d->shadow.destroy(); |
131 | } |
132 | |
133 | bool Shadow::isValid() const |
134 | { |
135 | return d->shadow.isValid(); |
136 | } |
137 | |
138 | void Shadow::setOffsets(const QMarginsF &margins) |
139 | { |
140 | Q_ASSERT(isValid()); |
141 | org_kde_kwin_shadow_set_left_offset(org_kde_kwin_shadow: d->shadow, offset: wl_fixed_from_double(d: margins.left())); |
142 | org_kde_kwin_shadow_set_top_offset(org_kde_kwin_shadow: d->shadow, offset: wl_fixed_from_double(d: margins.top())); |
143 | org_kde_kwin_shadow_set_right_offset(org_kde_kwin_shadow: d->shadow, offset: wl_fixed_from_double(d: margins.right())); |
144 | org_kde_kwin_shadow_set_bottom_offset(org_kde_kwin_shadow: d->shadow, offset: wl_fixed_from_double(d: margins.bottom())); |
145 | } |
146 | |
147 | void Shadow::commit() |
148 | { |
149 | Q_ASSERT(isValid()); |
150 | org_kde_kwin_shadow_commit(org_kde_kwin_shadow: d->shadow); |
151 | } |
152 | |
153 | // clang-format off |
154 | #ifndef K_DOXYGEN |
155 | #define attach( __PART__, __WAYLAND_PART__ ) \ |
156 | void Shadow::attach##__PART__(wl_buffer *buffer) \ |
157 | { \ |
158 | Q_ASSERT(isValid()); \ |
159 | org_kde_kwin_shadow_attach_##__WAYLAND_PART__(d->shadow, buffer); \ |
160 | } \ |
161 | void Shadow::attach##__PART__(Buffer *buffer) \ |
162 | { \ |
163 | if (!buffer) {\ |
164 | return;\ |
165 | }\ |
166 | attach##__PART__(buffer->buffer()); \ |
167 | } \ |
168 | void Shadow::attach##__PART__(Buffer::Ptr buffer) \ |
169 | { \ |
170 | attach##__PART__(buffer.toStrongRef().data()); \ |
171 | } |
172 | // clang-format on |
173 | |
174 | attach(Left, left) attach(TopLeft, top_left) attach(Top, top) attach(TopRight, top_right) attach(Right, right) attach(BottomRight, bottom_right) |
175 | attach(Bottom, bottom) attach(BottomLeft, bottom_left) |
176 | |
177 | #undef attach |
178 | #endif |
179 | |
180 | Shadow::operator org_kde_kwin_shadow *() |
181 | { |
182 | return d->shadow; |
183 | } |
184 | |
185 | Shadow::operator org_kde_kwin_shadow *() const |
186 | { |
187 | return d->shadow; |
188 | } |
189 | |
190 | } |
191 | } |
192 | |
193 | #include "moc_shadow.cpp" |
194 | |