2010-07-31 20:55:48 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2010 Martin Gräßlin <kde@martin-graesslin.com>
|
|
|
|
Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
|
|
|
|
|
|
|
|
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 "screenshot.h"
|
|
|
|
#include <kwinglutils.h>
|
2012-03-04 08:53:12 +00:00
|
|
|
#include <kwinxrenderutils.h>
|
2010-09-14 19:52:44 +00:00
|
|
|
#include <KDE/KDebug>
|
2011-08-17 17:20:15 +00:00
|
|
|
#include <KDE/KTemporaryFile>
|
2010-09-14 19:52:44 +00:00
|
|
|
#include <QtDBus/QDBusConnection>
|
|
|
|
#include <QtCore/QVarLengthArray>
|
|
|
|
#include <QtGui/QPainter>
|
|
|
|
|
|
|
|
#include <X11/extensions/Xfixes.h>
|
|
|
|
#include <QX11Info>
|
2010-07-31 20:55:48 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
KWIN_EFFECT(screenshot, ScreenShotEffect)
|
|
|
|
KWIN_EFFECT_SUPPORTED(screenshot, ScreenShotEffect::supported())
|
2010-07-31 20:55:48 +00:00
|
|
|
|
|
|
|
bool ScreenShotEffect::supported()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-03-04 08:53:12 +00:00
|
|
|
return effects->compositingType() == XRenderCompositing ||
|
|
|
|
(effects->compositingType() == KWin::OpenGLCompositing && GLRenderTarget::supported());
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-07-31 20:55:48 +00:00
|
|
|
|
|
|
|
ScreenShotEffect::ScreenShotEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
: m_scheduledScreenshot(0)
|
|
|
|
{
|
2012-01-29 11:29:24 +00:00
|
|
|
connect ( effects, SIGNAL(windowClosed(KWin::EffectWindow*)), SLOT(windowClosed(KWin::EffectWindow*)) );
|
2011-01-30 14:34:42 +00:00
|
|
|
QDBusConnection::sessionBus().registerObject("/Screenshot", this, QDBusConnection::ExportScriptableContents);
|
|
|
|
QDBusConnection::sessionBus().registerService("org.kde.kwin.Screenshot");
|
|
|
|
}
|
2010-07-31 20:55:48 +00:00
|
|
|
|
|
|
|
ScreenShotEffect::~ScreenShotEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
QDBusConnection::sessionBus().unregisterObject("/Screenshot");
|
|
|
|
QDBusConnection::sessionBus().unregisterService("org.kde.kwin.Screenshot");
|
|
|
|
}
|
2010-07-31 20:55:48 +00:00
|
|
|
void ScreenShotEffect::postPaintScreen()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-07-31 20:55:48 +00:00
|
|
|
effects->postPaintScreen();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (m_scheduledScreenshot) {
|
2010-07-31 20:55:48 +00:00
|
|
|
int w = displayWidth();
|
|
|
|
int h = displayHeight();
|
2012-03-04 08:53:12 +00:00
|
|
|
bool validTarget = true;
|
|
|
|
GLTexture* offscreenTexture = 0;
|
|
|
|
GLRenderTarget* target = 0;
|
|
|
|
if (effects->compositingType() == KWin::OpenGLCompositing) {
|
|
|
|
if (!GLTexture::NPOTTextureSupported()) {
|
|
|
|
w = nearestPowerOfTwo(w);
|
|
|
|
h = nearestPowerOfTwo(h);
|
|
|
|
}
|
|
|
|
offscreenTexture = new GLTexture(w, h);
|
|
|
|
offscreenTexture->setFilter(GL_LINEAR);
|
|
|
|
offscreenTexture->setWrapMode(GL_CLAMP_TO_EDGE);
|
|
|
|
target = new GLRenderTarget(*offscreenTexture);
|
|
|
|
validTarget = target->valid();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2012-03-04 08:53:12 +00:00
|
|
|
if (validTarget) {
|
2011-01-30 14:34:42 +00:00
|
|
|
WindowPaintData d(m_scheduledScreenshot);
|
2010-07-31 20:55:48 +00:00
|
|
|
double left = 0;
|
|
|
|
double top = 0;
|
|
|
|
double right = m_scheduledScreenshot->width();
|
|
|
|
double bottom = m_scheduledScreenshot->height();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (m_scheduledScreenshot->hasDecoration() && m_type & INCLUDE_DECORATION) {
|
|
|
|
foreach (const WindowQuad & quad, d.quads) {
|
2010-09-14 19:52:44 +00:00
|
|
|
// we need this loop to include the decoration padding
|
|
|
|
left = qMin(left, quad.left());
|
|
|
|
top = qMin(top, quad.top());
|
|
|
|
right = qMax(right, quad.right());
|
|
|
|
bottom = qMax(bottom, quad.bottom());
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
} else if (m_scheduledScreenshot->hasDecoration()) {
|
2010-09-14 19:52:44 +00:00
|
|
|
WindowQuadList newQuads;
|
|
|
|
left = m_scheduledScreenshot->width();
|
|
|
|
top = m_scheduledScreenshot->height();
|
|
|
|
right = 0;
|
|
|
|
bottom = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
foreach (const WindowQuad & quad, d.quads) {
|
|
|
|
if (quad.type() == WindowQuadContents) {
|
2010-09-14 19:52:44 +00:00
|
|
|
newQuads << quad;
|
|
|
|
left = qMin(left, quad.left());
|
|
|
|
top = qMin(top, quad.top());
|
|
|
|
right = qMax(right, quad.right());
|
|
|
|
bottom = qMax(bottom, quad.bottom());
|
|
|
|
}
|
2010-07-31 20:55:48 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
d.quads = newQuads;
|
|
|
|
}
|
2010-07-31 20:55:48 +00:00
|
|
|
int width = right - left;
|
|
|
|
int height = bottom - top;
|
|
|
|
d.xTranslate = -m_scheduledScreenshot->x() - left;
|
|
|
|
d.yTranslate = -m_scheduledScreenshot->y() - top;
|
2012-03-04 08:53:12 +00:00
|
|
|
|
2010-07-31 20:55:48 +00:00
|
|
|
// render window into offscreen texture
|
|
|
|
int mask = PAINT_WINDOW_TRANSFORMED | PAINT_WINDOW_TRANSLUCENT;
|
2012-03-04 08:53:12 +00:00
|
|
|
QImage img;
|
|
|
|
if (effects->compositingType() == KWin::OpenGLCompositing) {
|
|
|
|
GLRenderTarget::pushRenderTarget(target);
|
|
|
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glClearColor(0.0, 0.0, 0.0, 1.0);
|
|
|
|
effects->drawWindow(m_scheduledScreenshot, mask, QRegion(0, 0, width, height), d);
|
|
|
|
// copy content from framebuffer into image
|
|
|
|
img = QImage(QSize(width, height), QImage::Format_ARGB32);
|
|
|
|
glReadPixels(0, offscreenTexture->height() - height, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)img.bits());
|
|
|
|
GLRenderTarget::popRenderTarget();
|
|
|
|
ScreenShotEffect::convertFromGLImage(img, width, height);
|
|
|
|
}
|
2012-03-24 07:08:12 +00:00
|
|
|
#ifdef KWIN_HAVE_XRENDER_COMPOSITING
|
2012-03-04 08:53:12 +00:00
|
|
|
if (effects->compositingType() == XRenderCompositing) {
|
|
|
|
setXRenderOffscreen(true);
|
|
|
|
effects->drawWindow(m_scheduledScreenshot, mask, QRegion(0, 0, width, height), d);
|
|
|
|
if (xRenderOffscreenTarget())
|
|
|
|
img = xRenderOffscreenTarget()->toImage().copy(0, 0, width, height);
|
|
|
|
setXRenderOffscreen(false);
|
|
|
|
}
|
2012-03-24 07:08:12 +00:00
|
|
|
#endif
|
2012-03-04 08:53:12 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (m_type & INCLUDE_CURSOR) {
|
|
|
|
grabPointerImage(img, m_scheduledScreenshot->x() + left, m_scheduledScreenshot->y() + top);
|
2010-07-31 20:55:48 +00:00
|
|
|
}
|
2012-03-04 08:53:12 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
m_lastScreenshot = QPixmap::fromImage(img);
|
2011-05-12 16:52:38 +00:00
|
|
|
if (m_lastScreenshot.handle() == 0) {
|
|
|
|
Pixmap xpix = XCreatePixmap(display(), rootWindow(), m_lastScreenshot.width(),
|
2011-07-03 10:23:11 +00:00
|
|
|
m_lastScreenshot.height(), 32);
|
2011-05-12 16:52:38 +00:00
|
|
|
m_lastScreenshot = QPixmap::fromX11Pixmap(xpix, QPixmap::ExplicitlyShared);
|
|
|
|
QPainter p(&m_lastScreenshot);
|
2011-07-03 10:23:11 +00:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Source);
|
2011-05-12 16:52:38 +00:00
|
|
|
p.drawImage(QPoint(0, 0), img);
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
emit screenshotCreated(m_lastScreenshot.handle());
|
|
|
|
}
|
2010-07-31 20:55:48 +00:00
|
|
|
delete offscreenTexture;
|
|
|
|
delete target;
|
|
|
|
m_scheduledScreenshot = NULL;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-07-31 20:55:48 +00:00
|
|
|
|
2010-09-14 19:52:44 +00:00
|
|
|
void ScreenShotEffect::screenshotWindowUnderCursor(int mask)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-09-14 19:52:44 +00:00
|
|
|
m_type = (ScreenShotType)mask;
|
|
|
|
const QPoint cursor = effects->cursorPos();
|
2011-12-07 14:04:11 +00:00
|
|
|
EffectWindowList order = effects->stackingOrder();
|
|
|
|
EffectWindowList::const_iterator it = order.constEnd(), first = order.constBegin();
|
|
|
|
while( it != first ) {
|
|
|
|
m_scheduledScreenshot = *(--it);
|
|
|
|
if (m_scheduledScreenshot->isOnCurrentDesktop() &&
|
|
|
|
!m_scheduledScreenshot->isMinimized() && !m_scheduledScreenshot->isDeleted() &&
|
|
|
|
m_scheduledScreenshot->geometry().contains(cursor))
|
|
|
|
break;
|
|
|
|
m_scheduledScreenshot = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
if (m_scheduledScreenshot) {
|
2010-09-14 19:52:44 +00:00
|
|
|
m_scheduledScreenshot->addRepaintFull();
|
2010-07-31 20:55:48 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-08-17 17:20:15 +00:00
|
|
|
|
2012-04-12 11:13:49 +00:00
|
|
|
void ScreenShotEffect::screenshotForWindow(qulonglong winid, int mask)
|
|
|
|
{
|
|
|
|
m_type = (ScreenShotType) mask;
|
|
|
|
EffectWindow* w = effects->findWindow(winid);
|
|
|
|
if(w && !w->isMinimized() && !w->isDeleted()) {
|
|
|
|
m_scheduledScreenshot = w;
|
|
|
|
m_scheduledScreenshot->addRepaintFull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 17:20:15 +00:00
|
|
|
QString ScreenShotEffect::screenshotFullscreen()
|
|
|
|
{
|
|
|
|
return blitScreenshot(QRect(0, 0, displayWidth(), displayHeight()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ScreenShotEffect::screenshotScreen(int screen)
|
|
|
|
{
|
|
|
|
return blitScreenshot(effects->clientArea(FullScreenArea, screen, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ScreenShotEffect::screenshotArea(int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
return blitScreenshot(QRect(x, y, width, height));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ScreenShotEffect::blitScreenshot(const QRect &geometry)
|
|
|
|
{
|
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
Q_UNUSED(geometry)
|
2012-03-04 08:53:12 +00:00
|
|
|
kDebug(1212) << "Framebuffer Blit not supported";
|
2011-08-17 17:20:15 +00:00
|
|
|
return QString();
|
|
|
|
#else
|
2012-03-04 08:53:12 +00:00
|
|
|
QImage img;
|
|
|
|
if (effects->compositingType() == KWin::OpenGLCompositing)
|
|
|
|
{
|
|
|
|
if (!GLRenderTarget::blitSupported()) {
|
|
|
|
kDebug(1212) << "Framebuffer Blit not supported";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
GLTexture tex(geometry.width(), geometry.height());
|
|
|
|
GLRenderTarget target(tex);
|
|
|
|
target.blitFromFramebuffer(geometry);
|
|
|
|
// copy content from framebuffer into image
|
|
|
|
tex.bind();
|
|
|
|
img = QImage(geometry.size(), QImage::Format_ARGB32);
|
|
|
|
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)img.bits());
|
|
|
|
tex.unbind();
|
|
|
|
ScreenShotEffect::convertFromGLImage(img, geometry.width(), geometry.height());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (effects->compositingType() == XRenderCompositing) {
|
|
|
|
QPixmap buffer(geometry.size());
|
|
|
|
if (buffer.handle() == 0) {
|
|
|
|
Pixmap xpix = XCreatePixmap(display(), rootWindow(), geometry.width(), geometry.height(), 32);
|
|
|
|
buffer = QPixmap::fromX11Pixmap(xpix, QPixmap::ExplicitlyShared);
|
|
|
|
}
|
|
|
|
XRenderComposite(display(), PictOpSrc, effects->xrenderBufferPicture(), None, buffer.x11PictureHandle(),
|
|
|
|
0, 0, 0, 0, geometry.x(), geometry.y(), geometry.width(), geometry.height());
|
|
|
|
img = buffer.toImage();
|
|
|
|
}
|
|
|
|
|
2011-08-17 17:20:15 +00:00
|
|
|
KTemporaryFile temp;
|
|
|
|
temp.setSuffix(".png");
|
|
|
|
temp.setAutoRemove(false);
|
|
|
|
if (!temp.open()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
img.save(&temp);
|
|
|
|
temp.close();
|
|
|
|
return temp.fileName();
|
|
|
|
#endif
|
|
|
|
}
|
2010-07-31 20:55:48 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void ScreenShotEffect::grabPointerImage(QImage& snapshot, int offsetx, int offsety)
|
2010-09-14 19:52:44 +00:00
|
|
|
// Uses the X11_EXTENSIONS_XFIXES_H extension to grab the pointer image, and overlays it onto the snapshot.
|
|
|
|
{
|
2011-01-30 14:34:42 +00:00
|
|
|
XFixesCursorImage *xcursorimg = XFixesGetCursorImage(QX11Info::display());
|
|
|
|
if (!xcursorimg)
|
|
|
|
return;
|
2010-09-14 19:52:44 +00:00
|
|
|
|
|
|
|
//Annoyingly, xfixes specifies the data to be 32bit, but places it in an unsigned long *
|
|
|
|
//which can be 64 bit. So we need to iterate over a 64bit structure to put it in a 32bit
|
|
|
|
//structure.
|
2011-01-30 14:34:42 +00:00
|
|
|
QVarLengthArray< quint32 > pixels(xcursorimg->width * xcursorimg->height);
|
2010-09-14 19:52:44 +00:00
|
|
|
for (int i = 0; i < xcursorimg->width * xcursorimg->height; ++i)
|
|
|
|
pixels[i] = xcursorimg->pixels[i] & 0xffffffff;
|
|
|
|
|
|
|
|
QImage qcursorimg((uchar *) pixels.data(), xcursorimg->width, xcursorimg->height,
|
2011-01-30 14:34:42 +00:00
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
2010-09-14 19:52:44 +00:00
|
|
|
|
|
|
|
QPainter painter(&snapshot);
|
|
|
|
painter.drawImage(QPointF(xcursorimg->x - xcursorimg->xhot - offsetx, xcursorimg->y - xcursorimg ->yhot - offsety), qcursorimg);
|
|
|
|
|
|
|
|
XFree(xcursorimg);
|
|
|
|
}
|
|
|
|
|
2010-07-31 20:55:48 +00:00
|
|
|
void ScreenShotEffect::convertFromGLImage(QImage &img, int w, int h)
|
|
|
|
{
|
|
|
|
// from QtOpenGL/qgl.cpp
|
|
|
|
// Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
|
|
|
|
// see http://qt.gitorious.org/qt/qt/blobs/master/src/opengl/qgl.cpp
|
|
|
|
if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
|
|
|
|
// OpenGL gives RGBA; Qt wants ARGB
|
|
|
|
uint *p = (uint*)img.bits();
|
2011-01-30 14:34:42 +00:00
|
|
|
uint *end = p + w * h;
|
2010-07-31 20:55:48 +00:00
|
|
|
while (p < end) {
|
|
|
|
uint a = *p << 24;
|
|
|
|
*p = (*p >> 8) | a;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// OpenGL gives ABGR (i.e. RGBA backwards); Qt wants ARGB
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
uint *q = (uint*)img.scanLine(y);
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int x = 0; x < w; ++x) {
|
2010-07-31 20:55:48 +00:00
|
|
|
const uint pixel = *q;
|
|
|
|
*q = ((pixel << 16) & 0xff0000) | ((pixel >> 16) & 0xff)
|
2011-01-30 14:34:42 +00:00
|
|
|
| (pixel & 0xff00ff00);
|
2010-07-31 20:55:48 +00:00
|
|
|
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
img = img.mirrored();
|
|
|
|
}
|
|
|
|
|
2011-08-27 09:21:31 +00:00
|
|
|
bool ScreenShotEffect::isActive() const
|
|
|
|
{
|
|
|
|
return m_scheduledScreenshot != NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-07 14:04:11 +00:00
|
|
|
void ScreenShotEffect::windowClosed( EffectWindow* w )
|
|
|
|
{
|
|
|
|
if (w == m_scheduledScreenshot) {
|
|
|
|
m_scheduledScreenshot = NULL;
|
|
|
|
screenshotWindowUnderCursor(m_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-31 20:55:48 +00:00
|
|
|
} // namespace
|