2011-03-27 10:33:07 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2011 Martin Gräßlin <mgraesslin@kde.org>
|
|
|
|
|
|
|
|
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. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
#include "shadow.h"
|
|
|
|
// kwin
|
|
|
|
#include "atoms.h"
|
2011-04-03 09:31:33 +00:00
|
|
|
#include "effects.h"
|
2011-03-27 10:33:07 +00:00
|
|
|
#include "toplevel.h"
|
|
|
|
#include "scene_opengl.h"
|
2011-04-28 15:22:17 +00:00
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
#include "scene_xrender.h"
|
|
|
|
#endif
|
2011-03-27 10:33:07 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
Shadow::Shadow(Toplevel *toplevel)
|
2011-04-03 09:31:33 +00:00
|
|
|
: m_topLevel(toplevel)
|
2011-04-03 10:43:57 +00:00
|
|
|
, m_cachedSize(toplevel->geometry().size())
|
2011-03-27 10:33:07 +00:00
|
|
|
{
|
2011-04-03 10:43:57 +00:00
|
|
|
connect(m_topLevel, SIGNAL(geometryChanged()), SLOT(geometryChanged()));
|
2011-03-27 10:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Shadow::~Shadow()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Shadow *Shadow::createShadow(Toplevel *toplevel)
|
|
|
|
{
|
|
|
|
if (!effects) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
QVector<long> data = Shadow::readX11ShadowProperty(toplevel->window());
|
|
|
|
if (!data.isEmpty()) {
|
|
|
|
Shadow *shadow = NULL;
|
2012-09-20 09:33:32 +00:00
|
|
|
if (effects->isOpenGLCompositing()) {
|
2011-03-27 10:33:07 +00:00
|
|
|
shadow = new SceneOpenGLShadow(toplevel);
|
2011-04-28 15:22:17 +00:00
|
|
|
} else if (effects->compositingType() == XRenderCompositing) {
|
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
|
|
|
shadow = new SceneXRenderShadow(toplevel);
|
2011-03-27 10:33:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
2011-04-28 15:22:17 +00:00
|
|
|
|
2011-03-27 10:33:07 +00:00
|
|
|
if (shadow) {
|
2011-04-02 17:01:16 +00:00
|
|
|
if (!shadow->init(data)) {
|
|
|
|
delete shadow;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-03 09:31:33 +00:00
|
|
|
if (toplevel->effectWindow() && toplevel->effectWindow()->sceneWindow()) {
|
|
|
|
toplevel->effectWindow()->sceneWindow()->updateShadow(shadow);
|
|
|
|
}
|
2011-03-27 10:33:07 +00:00
|
|
|
}
|
|
|
|
return shadow;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector< long > Shadow::readX11ShadowProperty(WId id)
|
|
|
|
{
|
|
|
|
QVector<long> ret;
|
|
|
|
Atom type;
|
|
|
|
int format, status;
|
|
|
|
unsigned long nitems = 0;
|
|
|
|
unsigned long extra = 0;
|
|
|
|
unsigned char *data = 0;
|
|
|
|
status = XGetWindowProperty(display(), id, atoms->kde_net_wm_shadow, 0, 12, false, XA_CARDINAL, &type, &format, &nitems, &extra, &data);
|
|
|
|
if (status == Success && type == XA_CARDINAL && format == 32 && nitems == 12) {
|
|
|
|
long* shadow = reinterpret_cast< long* >(data);
|
|
|
|
ret.reserve(12);
|
|
|
|
for (int i=0; i<12; ++i) {
|
|
|
|
ret << shadow[i];
|
|
|
|
}
|
|
|
|
XFree(data);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-02 17:01:16 +00:00
|
|
|
bool Shadow::init(const QVector< long > &data)
|
2011-03-27 10:33:07 +00:00
|
|
|
{
|
2013-07-31 13:25:39 +00:00
|
|
|
QVector<Xcb::WindowGeometry> pixmapGeometries(ShadowElementsCount);
|
|
|
|
QVector<xcb_get_image_cookie_t> getImageCookies(ShadowElementsCount);
|
|
|
|
auto *c = connection();
|
|
|
|
for (int i = 0; i < ShadowElementsCount; ++i) {
|
|
|
|
pixmapGeometries[i] = Xcb::WindowGeometry(data[i]);
|
|
|
|
}
|
|
|
|
auto discardReplies = [&getImageCookies](int start) {
|
|
|
|
for (int i = start; i < getImageCookies.size(); ++i) {
|
|
|
|
xcb_discard_reply(connection(), getImageCookies.at(i).sequence);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (int i = 0; i < ShadowElementsCount; ++i) {
|
|
|
|
auto &geo = pixmapGeometries[i];
|
|
|
|
if (geo.isNull()) {
|
|
|
|
discardReplies(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
getImageCookies[i] = xcb_get_image_unchecked(c, XCB_IMAGE_FORMAT_Z_PIXMAP, data[i],
|
|
|
|
0, 0, geo->width, geo->height, ~0);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < ShadowElementsCount; ++i) {
|
|
|
|
auto *reply = xcb_get_image_reply(c, getImageCookies.at(i), nullptr);
|
|
|
|
if (!reply) {
|
|
|
|
discardReplies(i+1);
|
2011-04-02 17:01:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-31 13:25:39 +00:00
|
|
|
auto &geo = pixmapGeometries[i];
|
|
|
|
QImage image(xcb_get_image_data(reply), geo->width, geo->height, QImage::Format_ARGB32);
|
|
|
|
m_shadowElements[i] = QPixmap::fromImage(image);
|
|
|
|
free(reply);
|
2011-03-27 10:33:07 +00:00
|
|
|
}
|
|
|
|
m_topOffset = data[ShadowElementsCount];
|
|
|
|
m_rightOffset = data[ShadowElementsCount+1];
|
|
|
|
m_bottomOffset = data[ShadowElementsCount+2];
|
|
|
|
m_leftOffset = data[ShadowElementsCount+3];
|
2011-04-03 10:43:57 +00:00
|
|
|
updateShadowRegion();
|
2011-06-23 17:06:12 +00:00
|
|
|
if (!prepareBackend()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-03 10:43:57 +00:00
|
|
|
buildQuads();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shadow::updateShadowRegion()
|
|
|
|
{
|
2011-11-25 14:21:12 +00:00
|
|
|
const QRect top(0, - m_topOffset, m_topLevel->width(), m_topOffset);
|
|
|
|
const QRect right(m_topLevel->width(), - m_topOffset, m_rightOffset, m_topLevel->height() + m_topOffset + m_bottomOffset);
|
|
|
|
const QRect bottom(0, m_topLevel->height(), m_topLevel->width(), m_bottomOffset);
|
|
|
|
const QRect left(- m_leftOffset, - m_topOffset, m_leftOffset, m_topLevel->height() + m_topOffset + m_bottomOffset);
|
|
|
|
m_shadowRegion = QRegion(top).united(right).united(bottom).united(left);
|
2011-03-27 10:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shadow::buildQuads()
|
|
|
|
{
|
|
|
|
// prepare window quads
|
|
|
|
m_shadowQuads.clear();
|
2011-11-25 14:21:12 +00:00
|
|
|
const QSize top(m_shadowElements[ShadowElementTop].size());
|
|
|
|
const QSize topRight(m_shadowElements[ShadowElementTopRight].size());
|
|
|
|
const QSize right(m_shadowElements[ShadowElementRight].size());
|
|
|
|
const QSize bottomRight(m_shadowElements[ShadowElementBottomRight].size());
|
|
|
|
const QSize bottom(m_shadowElements[ShadowElementBottom].size());
|
|
|
|
const QSize bottomLeft(m_shadowElements[ShadowElementBottomLeft].size());
|
|
|
|
const QSize left(m_shadowElements[ShadowElementLeft].size());
|
|
|
|
const QSize topLeft(m_shadowElements[ShadowElementTopLeft].size());
|
|
|
|
if ((left.width() - m_leftOffset > m_topLevel->width()) ||
|
|
|
|
(right.width() - m_rightOffset > m_topLevel->width()) ||
|
|
|
|
(top.height() - m_topOffset > m_topLevel->height()) ||
|
|
|
|
(bottom.height() - m_bottomOffset > m_topLevel->height())) {
|
2011-04-03 11:05:35 +00:00
|
|
|
// if our shadow is bigger than the window, we don't render the shadow
|
|
|
|
m_shadowRegion = QRegion();
|
|
|
|
return;
|
|
|
|
}
|
2011-11-25 14:21:12 +00:00
|
|
|
|
|
|
|
const QRect outerRect(QPoint(-m_leftOffset, -m_topOffset), QPoint(m_topLevel->width() + m_rightOffset, m_topLevel->height() + m_bottomOffset));
|
|
|
|
|
2011-03-27 10:33:07 +00:00
|
|
|
WindowQuad topLeftQuad(WindowQuadShadowTopLeft);
|
2011-11-25 14:21:12 +00:00
|
|
|
topLeftQuad[ 0 ] = WindowVertex(outerRect.x(), outerRect.y(), 0.0, 0.0);
|
|
|
|
topLeftQuad[ 1 ] = WindowVertex(outerRect.x() + topLeft.width(), outerRect.y(), 1.0, 0.0);
|
|
|
|
topLeftQuad[ 2 ] = WindowVertex(outerRect.x() + topLeft.width(), outerRect.y() + topLeft.height(), 1.0, 1.0);
|
|
|
|
topLeftQuad[ 3 ] = WindowVertex(outerRect.x(), outerRect.y() + topLeft.height(), 0.0, 1.0);
|
2011-03-27 10:33:07 +00:00
|
|
|
m_shadowQuads.append(topLeftQuad);
|
2011-11-25 14:21:12 +00:00
|
|
|
|
2011-03-27 10:33:07 +00:00
|
|
|
WindowQuad topQuad(WindowQuadShadowTop);
|
2011-11-25 14:21:12 +00:00
|
|
|
topQuad[ 0 ] = WindowVertex(outerRect.x() + topLeft.width(), outerRect.y(), 0.0, 0.0);
|
|
|
|
topQuad[ 1 ] = WindowVertex(outerRect.right() - topRight.width(), outerRect.y(), 1.0, 0.0);
|
|
|
|
topQuad[ 2 ] = WindowVertex(outerRect.right() - topRight.width(), outerRect.y() + top.height(), 1.0, 1.0);
|
|
|
|
topQuad[ 3 ] = WindowVertex(outerRect.x() + topLeft.width(), outerRect.y() + top.height(), 0.0, 1.0);
|
2011-03-27 10:33:07 +00:00
|
|
|
m_shadowQuads.append(topQuad);
|
2011-11-25 14:21:12 +00:00
|
|
|
|
2011-03-27 10:33:07 +00:00
|
|
|
WindowQuad topRightQuad(WindowQuadShadowTopRight);
|
2011-11-25 14:21:12 +00:00
|
|
|
topRightQuad[ 0 ] = WindowVertex(outerRect.right() - topRight.width(), outerRect.y(), 0.0, 0.0);
|
|
|
|
topRightQuad[ 1 ] = WindowVertex(outerRect.right(), outerRect.y(), 1.0, 0.0);
|
|
|
|
topRightQuad[ 2 ] = WindowVertex(outerRect.right(), outerRect.y() + topRight.height(), 1.0, 1.0);
|
|
|
|
topRightQuad[ 3 ] = WindowVertex(outerRect.right() - topRight.width(), outerRect.y() + topRight.height(), 0.0, 1.0);
|
2011-03-27 10:33:07 +00:00
|
|
|
m_shadowQuads.append(topRightQuad);
|
2011-11-25 14:21:12 +00:00
|
|
|
|
2011-03-27 10:33:07 +00:00
|
|
|
WindowQuad rightQuad(WindowQuadShadowRight);
|
2011-11-25 14:21:12 +00:00
|
|
|
rightQuad[ 0 ] = WindowVertex(outerRect.right() - right.width(), outerRect.y() + topRight.height(), 0.0, 0.0);
|
|
|
|
rightQuad[ 1 ] = WindowVertex(outerRect.right(), outerRect.y() + topRight.height(), 1.0, 0.0);
|
|
|
|
rightQuad[ 2 ] = WindowVertex(outerRect.right(), outerRect.bottom() - bottomRight.height(), 1.0, 1.0);
|
|
|
|
rightQuad[ 3 ] = WindowVertex(outerRect.right() - right.width(), outerRect.bottom() - bottomRight.height(), 0.0, 1.0);
|
2011-03-27 10:33:07 +00:00
|
|
|
m_shadowQuads.append(rightQuad);
|
2011-11-25 14:21:12 +00:00
|
|
|
|
2011-03-27 10:33:07 +00:00
|
|
|
WindowQuad bottomRightQuad(WindowQuadShadowBottomRight);
|
2011-11-25 14:21:12 +00:00
|
|
|
bottomRightQuad[ 0 ] = WindowVertex(outerRect.right() - bottomRight.width(), outerRect.bottom() - bottomRight.height(), 0.0, 0.0);
|
|
|
|
bottomRightQuad[ 1 ] = WindowVertex(outerRect.right(), outerRect.bottom() - bottomRight.height(), 1.0, 0.0);
|
|
|
|
bottomRightQuad[ 2 ] = WindowVertex(outerRect.right(), outerRect.bottom(), 1.0, 1.0);
|
|
|
|
bottomRightQuad[ 3 ] = WindowVertex(outerRect.right() - bottomRight.width(), outerRect.bottom(), 0.0, 1.0);
|
2011-03-27 10:33:07 +00:00
|
|
|
m_shadowQuads.append(bottomRightQuad);
|
2011-11-25 14:21:12 +00:00
|
|
|
|
2011-03-27 10:33:07 +00:00
|
|
|
WindowQuad bottomQuad(WindowQuadShadowBottom);
|
2011-11-25 14:21:12 +00:00
|
|
|
bottomQuad[ 0 ] = WindowVertex(outerRect.x() + bottomLeft.width(), outerRect.bottom() - bottom.height(), 0.0, 0.0);
|
|
|
|
bottomQuad[ 1 ] = WindowVertex(outerRect.right() - bottomRight.width(), outerRect.bottom() - bottom.height(), 1.0, 0.0);
|
|
|
|
bottomQuad[ 2 ] = WindowVertex(outerRect.right() - bottomRight.width(), outerRect.bottom(), 1.0, 1.0);
|
|
|
|
bottomQuad[ 3 ] = WindowVertex(outerRect.x() + bottomLeft.width(), outerRect.bottom(), 0.0, 1.0);
|
2011-03-27 10:33:07 +00:00
|
|
|
m_shadowQuads.append(bottomQuad);
|
2011-11-25 14:21:12 +00:00
|
|
|
|
2011-03-27 10:33:07 +00:00
|
|
|
WindowQuad bottomLeftQuad(WindowQuadShadowBottomLeft);
|
2011-11-25 14:21:12 +00:00
|
|
|
bottomLeftQuad[ 0 ] = WindowVertex(outerRect.x(), outerRect.bottom() - bottomLeft.height(), 0.0, 0.0);
|
|
|
|
bottomLeftQuad[ 1 ] = WindowVertex(outerRect.x() + bottomLeft.width(), outerRect.bottom() - bottomLeft.height(), 1.0, 0.0);
|
|
|
|
bottomLeftQuad[ 2 ] = WindowVertex(outerRect.x() + bottomLeft.width(), outerRect.bottom(), 1.0, 1.0);
|
|
|
|
bottomLeftQuad[ 3 ] = WindowVertex(outerRect.x(), outerRect.bottom(), 0.0, 1.0);
|
2011-03-27 10:33:07 +00:00
|
|
|
m_shadowQuads.append(bottomLeftQuad);
|
2011-11-25 14:21:12 +00:00
|
|
|
|
2011-03-27 10:33:07 +00:00
|
|
|
WindowQuad leftQuad(WindowQuadShadowLeft);
|
2011-11-25 14:21:12 +00:00
|
|
|
leftQuad[ 0 ] = WindowVertex(outerRect.x(), outerRect.y() + topLeft.height(), 0.0, 0.0);
|
|
|
|
leftQuad[ 1 ] = WindowVertex(outerRect.x() + left.width(), outerRect.y() + topLeft.height(), 1.0, 0.0);
|
|
|
|
leftQuad[ 2 ] = WindowVertex(outerRect.x() + left.width(), outerRect.bottom() - bottomLeft.height(), 1.0, 1.0);
|
|
|
|
leftQuad[ 3 ] = WindowVertex(outerRect.x(), outerRect.bottom() - bottomLeft.height(), 0.0, 1.0);
|
2011-03-27 10:33:07 +00:00
|
|
|
m_shadowQuads.append(leftQuad);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Shadow::updateShadow()
|
|
|
|
{
|
|
|
|
QVector<long> data = Shadow::readX11ShadowProperty(m_topLevel->window());
|
|
|
|
if (data.isEmpty()) {
|
2013-06-07 18:15:09 +00:00
|
|
|
if (m_topLevel && m_topLevel->effectWindow() && m_topLevel->effectWindow()->sceneWindow() &&
|
|
|
|
m_topLevel->effectWindow()->sceneWindow()->shadow()) {
|
2012-01-24 15:58:33 +00:00
|
|
|
m_topLevel->effectWindow()->sceneWindow()->updateShadow(0);
|
2013-06-07 18:15:09 +00:00
|
|
|
m_topLevel->effectWindow()->buildQuads(true);
|
|
|
|
}
|
2012-01-24 15:58:33 +00:00
|
|
|
deleteLater();
|
2011-03-27 10:33:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
init(data);
|
2011-11-25 00:00:54 +00:00
|
|
|
if (m_topLevel && m_topLevel->effectWindow())
|
|
|
|
m_topLevel->effectWindow()->buildQuads(true);
|
2011-03-27 10:33:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-01 19:49:44 +00:00
|
|
|
void Shadow::setToplevel(Toplevel *topLevel)
|
|
|
|
{
|
|
|
|
m_topLevel = topLevel;
|
2011-04-03 10:43:57 +00:00
|
|
|
connect(m_topLevel, SIGNAL(geometryChanged()), SLOT(geometryChanged()));
|
|
|
|
}
|
|
|
|
void Shadow::geometryChanged()
|
|
|
|
{
|
|
|
|
if (m_cachedSize == m_topLevel->geometry().size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_cachedSize = m_topLevel->geometry().size();
|
|
|
|
updateShadowRegion();
|
|
|
|
buildQuads();
|
2011-04-01 19:49:44 +00:00
|
|
|
}
|
|
|
|
|
2011-03-27 10:33:07 +00:00
|
|
|
} // namespace
|