forked from matinlotfali/KDE-Rounded-Corners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapecorners.cpp
More file actions
233 lines (213 loc) · 7.97 KB
/
shapecorners.cpp
File metadata and controls
233 lines (213 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright © 2015 Robert Metsäranta <therealestrob@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. if not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "shapecorners.h"
#include <kwingltexture.h>
#include <kwinglutils.h>
#if KWIN_EFFECT_API_VERSION >= 235
#include <KX11Extras>
#else
#include <kwindowsystem.h>
#endif
#define DEBUG_BACKGROUND 0
ShapeCornersEffect::ShapeCornersEffect() : KWin::Effect()
{
if(m_shaderManager.IsValid()) {
#if KWIN_EFFECT_API_VERSION >= 235
const auto& windowList = KX11Extras::windows();
#else
const auto& windowList = KWindowSystem::windows();
#endif
for (const auto& id: windowList)
if (auto win = KWin::effects->findWindow(id))
windowAdded(win);
connect(KWin::effects, &KWin::EffectsHandler::windowAdded, this, &ShapeCornersEffect::windowAdded);
connect(KWin::effects, &KWin::EffectsHandler::windowClosed, this, &ShapeCornersEffect::windowRemoved);
connect(KWin::effects, &KWin::EffectsHandler::windowActivated, this,&ShapeCornersEffect::windowGetBackground);
connect(KWin::effects, &KWin::EffectsHandler::windowFrameGeometryChanged, this,&ShapeCornersEffect::windowGetBackground);
}
m_config.Load();
}
ShapeCornersEffect::~ShapeCornersEffect() = default;
void
ShapeCornersEffect::windowAdded(KWin::EffectWindow *w)
{
if (m_managed.contains(w)
|| w->isDesktop()
|| w->isPopupMenu()
|| w->isDropdownMenu()
|| w->isTooltip()
|| w->isMenu()
|| w->isOnScreenDisplay()
|| w->isDock())
return;
qDebug() << w->windowRole() << w->windowType() << w->windowClass();
if (!w->hasDecoration()
&& (w->windowClass().contains("plasma", Qt::CaseInsensitive)
|| w->windowClass().contains("krunner", Qt::CaseInsensitive)
|| w->windowClass().contains("latte-dock", Qt::CaseInsensitive)
|| w->windowClass().contains("telegram", Qt::CaseInsensitive)))
return;
m_managed.insert(w, nullptr);
}
void ShapeCornersEffect::windowRemoved(KWin::EffectWindow *w)
{
m_managed.remove(w);
}
void
ShapeCornersEffect::reconfigure(ReconfigureFlags flags)
{
Q_UNUSED(flags)
m_config.Load();
}
#if KWIN_EFFECT_API_VERSION < 233
static bool hasShadow(KWin::WindowQuadList &qds)
{
for (int i = 0; i < qds.count(); ++i)
if (qds.at(i).type() == KWin::WindowQuadShadow)
return true;
return false;
}
#endif
bool isMaximized(KWin::EffectWindow *w) {
#if KWIN_EFFECT_API_VERSION >= 233
auto screenGeometry = KWin::effects->findScreen(w->screen()->name())->geometry();
return (w->x() == screenGeometry.x() && w->width() == screenGeometry.width()) ||
(w->y() == screenGeometry.y() && w->height() == screenGeometry.height());
#else
return w->isFullScreen();
#endif
}
QRectF operator *(QRect r, qreal scale) { return {r.x() * scale, r.y() * scale, r.width() * scale, r.height() * scale}; }
QRectF operator *(QRectF r, qreal scale) { return {r.x() * scale, r.y() * scale, r.width() * scale, r.height() * scale}; }
QRect toRect(const QRectF& r) { return {(int)r.x(), (int)r.y(), (int)r.width(), (int)r.height()}; }
const QRect& toRect(const QRect& r) { return r; }
#if KWIN_EFFECT_API_VERSION >= 233
void ShapeCornersEffect::prePaintWindow(KWin::EffectWindow *w, KWin::WindowPrePaintData &data, std::chrono::milliseconds time)
#else
void ShapeCornersEffect::prePaintWindow(KWin::EffectWindow *w, KWin::WindowPrePaintData &data, int time)
#endif
{
if (!m_shaderManager.IsValid()
|| !m_managed.contains(w)
|| isMaximized(w)
#if KWIN_EFFECT_API_VERSION < 234
|| !w->isPaintingEnabled()
#elif KWIN_EFFECT_API_VERSION < 233
|| data.quads.isTransformed()
|| !hasShadow(data.quads)
#endif
)
{
KWin::effects->prePaintWindow(w, data, time);
return;
}
const auto geo = w->frameGeometry();
#if KWIN_EFFECT_API_VERSION >= 234
data.opaque -= toRect(geo);
#endif
data.paint += toRect(geo);
KWin::effects->prePaintWindow(w, data, time);
}
void
ShapeCornersEffect::paintWindow(KWin::EffectWindow *w, int mask, QRegion region, KWin::WindowPaintData &data)
{
if (!m_shaderManager.IsValid()
|| !m_managed.contains(w)
|| isMaximized(w)
|| mask & PAINT_WINDOW_TRANSFORMED
#if KWIN_EFFECT_API_VERSION < 234
|| !w->isPaintingEnabled()
#elif KWIN_EFFECT_API_VERSION < 233
|| data.quads.isTransformed()
|| !hasShadow(data.quads)
#endif
)
{
KWin::effects->paintWindow(w, mask, region, data);
return;
}
const bool clipping = region != KWin::infiniteRegion();
#if KWIN_EFFECT_API_VERSION >= 234
const qreal scale = KWin::effects->renderTargetScale();
const QRectF renderRect = KWin::effects->renderTargetRect() * scale;
const QRegion clipRegion = clipping ? KWin::effects->mapToRenderTarget(region) : KWin::infiniteRegion();
#else
const qreal scale = 1;
const QRectF renderRect = {0, 0, 0, 0};
const QRegion clipRegion = region;
#endif
const QRectF geo = w->frameGeometry() * scale;
//copy the background
if(!m_managed[w]) {
m_managed[w].reset(new KWin::GLTexture(GL_RGBA8, geo.width(), geo.height()));
m_managed[w]->bind();
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
geo.x() - renderRect.x(),
renderRect.height() - (geo.y() - renderRect.y() + geo.height()),
geo.width(), geo.height());
m_managed[w]->unbind();
}
KWin::effects->paintWindow(w, mask, region, data);
//'shape' the corners
if (clipping) {
glEnable(GL_SCISSOR_TEST);
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
{
QMatrix4x4 mvp;
mvp.ortho(0, renderRect.width(), renderRect.height(), 0, 0, 65535);
mvp.translate(geo.x()-renderRect.x(), geo.y()-renderRect.y());
m_shaderManager.Bind(mvp, w, m_config);
m_managed[w]->bind();
m_managed[w]->render(clipRegion, toRect(geo), true);
m_managed[w]->unbind();
m_shaderManager.Unbind();
}
glDisable(GL_BLEND);
if (clipping) {
glDisable(GL_SCISSOR_TEST);
}
#if DEBUG_BACKGROUND == 1
if (KWin::effects->activeWindow() == w) {
auto s = KWin::ShaderManager::instance()->pushShader(KWin::ShaderTrait::MapTexture);
const QRectF screenRect = w->screen()->geometry() * scale;
QMatrix4x4 mvp;
mvp.ortho(0, renderRect.width(), renderRect.height(), 0, 0, 65535);
mvp.translate(screenRect.x()-renderRect.x(), screenRect.y()-renderRect.y());
s->setUniform(KWin::GLShader::ModelViewProjectionMatrix, mvp);
m_managed[w]->bind();
m_managed[w]->render(region, toRect(QRect(0,0,500,500)*scale));
m_managed[w]->unbind();
KWin::ShaderManager::instance()->popShader();
}
#endif
}
bool ShapeCornersEffect::supported()
{
#if KWIN_EFFECT_API_VERSION < 234
return KWin::effects->isOpenGLCompositing() && KWin::GLRenderTarget::supported();
#else
return KWin::effects->isOpenGLCompositing();
#endif
}
void ShapeCornersEffect::windowGetBackground(KWin::EffectWindow *window) {
if(m_managed.contains(window))
m_managed[window].reset();
}