KWindowSystem

platforms/xcb/kwindoweffects.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Marco Martin <notmart@gmail.com>
3 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include "kwindoweffects_x11.h"
9
10#include <QGuiApplication>
11#include <QVarLengthArray>
12
13#include "kx11extras.h"
14#include <config-kwindowsystem.h>
15
16#include <QMatrix4x4>
17#include <QWindow>
18#include <private/qtx11extras_p.h>
19
20#include <xcb/xcb.h>
21
22#include "cptr_p.h"
23#include <cmath>
24
25using namespace KWindowEffects;
26
27KWindowEffectsPrivateX11::KWindowEffectsPrivateX11()
28{
29}
30
31KWindowEffectsPrivateX11::~KWindowEffectsPrivateX11()
32{
33}
34
35bool KWindowEffectsPrivateX11::isEffectAvailable(Effect effect)
36{
37 if (!KX11Extras::self()->compositingActive()) {
38 return false;
39 }
40 QByteArray effectName;
41
42 switch (effect) {
43 case Slide:
44 effectName = QByteArrayLiteral("_KDE_SLIDE");
45 break;
46 case BlurBehind:
47 effectName = QByteArrayLiteral("_KDE_NET_WM_BLUR_BEHIND_REGION");
48 break;
49 case BackgroundContrast:
50 effectName = QByteArrayLiteral("_KDE_NET_WM_BACKGROUND_CONTRAST_REGION");
51 break;
52 default:
53 return false;
54 }
55
56 // hackish way to find out if KWin has the effect enabled,
57 // TODO provide proper support
58 xcb_connection_t *c = QX11Info::connection();
59 xcb_list_properties_cookie_t propsCookie = xcb_list_properties_unchecked(c, QX11Info::appRootWindow());
60 xcb_intern_atom_cookie_t atomCookie = xcb_intern_atom_unchecked(c, false, effectName.length(), effectName.constData());
61
62 UniqueCPointer<xcb_list_properties_reply_t> props(xcb_list_properties_reply(c, propsCookie, nullptr));
63 UniqueCPointer<xcb_intern_atom_reply_t> atom(xcb_intern_atom_reply(c, atomCookie, nullptr));
64 if (!atom || !props) {
65 return false;
66 }
67 xcb_atom_t *atoms = xcb_list_properties_atoms(props.get());
68 for (int i = 0; i < props->atoms_len; ++i) {
69 if (atoms[i] == atom->atom) {
70 return true;
71 }
72 }
73 return false;
74}
75
76void KWindowEffectsPrivateX11::slideWindow(QWindow *window, SlideFromLocation location, int offset)
77{
78 xcb_connection_t *c = QX11Info::connection();
79 if (!c) {
80 return;
81 }
82
83 const QByteArray effectName = QByteArrayLiteral("_KDE_SLIDE");
84 xcb_intern_atom_cookie_t atomCookie = xcb_intern_atom_unchecked(c, false, effectName.length(), effectName.constData());
85
86 const int size = 2;
87 int32_t data[size];
88 data[0] = offset;
89
90 switch (location) {
91 case LeftEdge:
92 data[1] = 0;
93 break;
94 case TopEdge:
95 data[1] = 1;
96 break;
97 case RightEdge:
98 data[1] = 2;
99 break;
100 case BottomEdge:
101 data[1] = 3;
102 default:
103 break;
104 }
105
106 UniqueCPointer<xcb_intern_atom_reply_t> atom(xcb_intern_atom_reply(c, atomCookie, nullptr));
107 if (!atom) {
108 return;
109 }
110 if (location == NoEdge) {
111 xcb_delete_property(c, window->winId(), atom->atom);
112 } else {
113 xcb_change_property(c, XCB_PROP_MODE_REPLACE, window->winId(), atom->atom, atom->atom, 32, size, data);
114 }
115}
116
117void KWindowEffectsPrivateX11::enableBlurBehind(QWindow *window, bool enable, const QRegion &region)
118{
119 xcb_connection_t *c = QX11Info::connection();
120 if (!c) {
121 return;
122 }
123 const QByteArray effectName = QByteArrayLiteral("_KDE_NET_WM_BLUR_BEHIND_REGION");
124 xcb_intern_atom_cookie_t atomCookie = xcb_intern_atom_unchecked(c, false, effectName.length(), effectName.constData());
125 UniqueCPointer<xcb_intern_atom_reply_t> atom(xcb_intern_atom_reply(c, atomCookie, nullptr));
126 if (!atom) {
127 return;
128 }
129
130 if (enable) {
131 QList<uint32_t> data;
132 data.reserve(region.rectCount() * 4);
133 for (const QRect &r : region) {
134 // kwin on X uses device pixels, convert from logical
135 auto dpr = qApp->devicePixelRatio();
136 data << std::floor(r.x() * dpr) << std::floor(r.y() * dpr) << std::ceil(r.width() * dpr) << std::ceil(r.height() * dpr);
137 }
138
139 xcb_change_property(c, XCB_PROP_MODE_REPLACE, window->winId(), atom->atom, XCB_ATOM_CARDINAL, 32, data.size(), data.constData());
140 } else {
141 xcb_delete_property(c, window->winId(), atom->atom);
142 }
143}
144
145void KWindowEffectsPrivateX11::enableBackgroundContrast(QWindow *window, bool enable, qreal contrast, qreal intensity, qreal saturation, const QRegion &region)
146{
147 xcb_connection_t *c = QX11Info::connection();
148 const QByteArray effectName = QByteArrayLiteral("_KDE_NET_WM_BACKGROUND_CONTRAST_REGION");
149 xcb_intern_atom_cookie_t atomCookie = xcb_intern_atom_unchecked(c, false, effectName.length(), effectName.constData());
150 UniqueCPointer<xcb_intern_atom_reply_t> atom(xcb_intern_atom_reply(c, atomCookie, nullptr));
151 if (!atom) {
152 return;
153 }
154
155 if (enable) {
156 QList<uint32_t> data;
157 data.reserve(region.rectCount() * 4 + 16);
158 for (const QRect &r : region) {
159 auto dpr = qApp->devicePixelRatio();
160 data << std::floor(r.x() * dpr) << std::floor(r.y() * dpr) << std::ceil(r.width() * dpr) << std::ceil(r.height() * dpr);
161 }
162
163 QMatrix4x4 satMatrix; // saturation
164 QMatrix4x4 intMatrix; // intensity
165 QMatrix4x4 contMatrix; // contrast
166
167 // clang-format off
168
169 //Saturation matrix
170 if (!qFuzzyCompare(saturation, 1.0)) {
171 const qreal rval = (1.0 - saturation) * .2126;
172 const qreal gval = (1.0 - saturation) * .7152;
173 const qreal bval = (1.0 - saturation) * .0722;
174
175 satMatrix = QMatrix4x4(rval + saturation, rval, rval, 0.0,
176 gval, gval + saturation, gval, 0.0,
177 bval, bval, bval + saturation, 0.0,
178 0, 0, 0, 1.0);
179 }
180
181 //IntensityMatrix
182 if (!qFuzzyCompare(intensity, 1.0)) {
183 intMatrix.scale(intensity, intensity, intensity);
184 }
185
186 //Contrast Matrix
187 if (!qFuzzyCompare(contrast, 1.0)) {
188 const float transl = (1.0 - contrast) / 2.0;
189
190 contMatrix = QMatrix4x4(contrast, 0, 0, 0.0,
191 0, contrast, 0, 0.0,
192 0, 0, contrast, 0.0,
193 transl, transl, transl, 1.0);
194 }
195
196 // clang-format on
197
198 QMatrix4x4 colorMatrix = contMatrix * satMatrix * intMatrix;
199 colorMatrix = colorMatrix.transposed();
200
201 uint32_t *rawData = reinterpret_cast<uint32_t *>(colorMatrix.data());
202
203 for (int i = 0; i < 16; ++i) {
204 data << rawData[i];
205 }
206
207 xcb_change_property(c, XCB_PROP_MODE_REPLACE, window->winId(), atom->atom, atom->atom, 32, data.size(), data.constData());
208 } else {
209 xcb_delete_property(c, window->winId(), atom->atom);
210 }
211}
QWidget * window(QObject *job)
Namespace for common standardized window effects.
const char * constData() const const
qsizetype length() const const
const_pointer constData() const const
void reserve(qsizetype size)
qsizetype size() const const
float * data()
void scale(const QVector3D &vector)
QMatrix4x4 transposed() const const
int rectCount() const const
LeftEdge
WId winId() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:04 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.