1
/********************************************************************
2
KWin - the KDE window manager
3
This file is part of the KDE project.
5
Copyright (C) 2010 Martin Gräßlin <kde@martin-graesslin.com>
6
Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
*********************************************************************/
21
#include "screenshot.h"
22
#include <kwinglutils.h>
24
#include <QtDBus/QDBusConnection>
25
#include <QtCore/QVarLengthArray>
26
#include <QtGui/QPainter>
28
#include <X11/extensions/Xfixes.h>
34
KWIN_EFFECT(screenshot, ScreenShotEffect)
35
KWIN_EFFECT_SUPPORTED(screenshot, ScreenShotEffect::supported())
37
bool ScreenShotEffect::supported()
39
return effects->compositingType() == KWin::OpenGLCompositing && GLRenderTarget::supported();
42
ScreenShotEffect::ScreenShotEffect()
43
: m_scheduledScreenshot(0)
45
QDBusConnection::sessionBus().registerObject("/Screenshot", this, QDBusConnection::ExportScriptableContents);
46
QDBusConnection::sessionBus().registerService("org.kde.kwin.Screenshot");
49
ScreenShotEffect::~ScreenShotEffect()
51
QDBusConnection::sessionBus().unregisterObject("/Screenshot");
52
QDBusConnection::sessionBus().unregisterService("org.kde.kwin.Screenshot");
54
void ScreenShotEffect::postPaintScreen()
56
effects->postPaintScreen();
57
if (m_scheduledScreenshot) {
58
int w = displayWidth();
59
int h = displayHeight();
60
if (!GLTexture::NPOTTextureSupported()) {
61
w = nearestPowerOfTwo(w);
62
h = nearestPowerOfTwo(h);
64
GLTexture* offscreenTexture = new GLTexture(w, h);
65
offscreenTexture->setFilter(GL_LINEAR);
66
offscreenTexture->setWrapMode(GL_CLAMP_TO_EDGE);
67
GLRenderTarget* target = new GLRenderTarget(offscreenTexture);
68
if (target->valid()) {
69
WindowPaintData d(m_scheduledScreenshot);
72
double right = m_scheduledScreenshot->width();
73
double bottom = m_scheduledScreenshot->height();
74
if (m_scheduledScreenshot->hasDecoration() && m_type & INCLUDE_DECORATION) {
75
foreach (const WindowQuad & quad, d.quads) {
76
// we need this loop to include the decoration padding
77
left = qMin(left, quad.left());
78
top = qMin(top, quad.top());
79
right = qMax(right, quad.right());
80
bottom = qMax(bottom, quad.bottom());
82
} else if (m_scheduledScreenshot->hasDecoration()) {
83
WindowQuadList newQuads;
84
left = m_scheduledScreenshot->width();
85
top = m_scheduledScreenshot->height();
88
foreach (const WindowQuad & quad, d.quads) {
89
if (quad.type() == WindowQuadContents) {
91
left = qMin(left, quad.left());
92
top = qMin(top, quad.top());
93
right = qMax(right, quad.right());
94
bottom = qMax(bottom, quad.bottom());
99
int width = right - left;
100
int height = bottom - top;
101
d.xTranslate = -m_scheduledScreenshot->x() - left;
102
d.yTranslate = -m_scheduledScreenshot->y() - top;
103
// render window into offscreen texture
104
int mask = PAINT_WINDOW_TRANSFORMED | PAINT_WINDOW_TRANSLUCENT;
105
GLRenderTarget::pushRenderTarget(target);
106
glClearColor(0.0, 0.0, 0.0, 0.0);
107
glClear(GL_COLOR_BUFFER_BIT);
108
glClearColor(0.0, 0.0, 0.0, 1.0);
109
effects->drawWindow(m_scheduledScreenshot, mask, QRegion(0, 0, width, height), d);
110
// copy content from framebuffer into image
111
QImage img(QSize(width, height), QImage::Format_ARGB32);
112
glReadPixels(0, offscreenTexture->height() - height, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)img.bits());
113
GLRenderTarget::popRenderTarget();
114
ScreenShotEffect::convertFromGLImage(img, width, height);
115
if (m_type & INCLUDE_CURSOR) {
116
grabPointerImage(img, m_scheduledScreenshot->x() + left, m_scheduledScreenshot->y() + top);
118
m_lastScreenshot = QPixmap::fromImage(img);
119
if (m_lastScreenshot.handle() == 0) {
120
Pixmap xpix = XCreatePixmap(display(), rootWindow(), m_lastScreenshot.width(),
121
m_lastScreenshot.height(), XDefaultDepth(display(), QX11Info::appScreen()));
122
m_lastScreenshot = QPixmap::fromX11Pixmap(xpix, QPixmap::ExplicitlyShared);
123
QPainter p(&m_lastScreenshot);
124
p.drawImage(QPoint(0, 0), img);
126
emit screenshotCreated(m_lastScreenshot.handle());
128
delete offscreenTexture;
130
m_scheduledScreenshot = NULL;
134
void ScreenShotEffect::screenshotWindowUnderCursor(int mask)
136
m_type = (ScreenShotType)mask;
137
const QPoint cursor = effects->cursorPos();
138
foreach (EffectWindow * w, effects->stackingOrder()) {
139
if (w->geometry().contains(cursor) && w->isOnCurrentDesktop() && !w->isMinimized()) {
140
m_scheduledScreenshot = w;
143
if (m_scheduledScreenshot) {
144
m_scheduledScreenshot->addRepaintFull();
148
void ScreenShotEffect::grabPointerImage(QImage& snapshot, int offsetx, int offsety)
149
// Uses the X11_EXTENSIONS_XFIXES_H extension to grab the pointer image, and overlays it onto the snapshot.
151
XFixesCursorImage *xcursorimg = XFixesGetCursorImage(QX11Info::display());
155
//Annoyingly, xfixes specifies the data to be 32bit, but places it in an unsigned long *
156
//which can be 64 bit. So we need to iterate over a 64bit structure to put it in a 32bit
158
QVarLengthArray< quint32 > pixels(xcursorimg->width * xcursorimg->height);
159
for (int i = 0; i < xcursorimg->width * xcursorimg->height; ++i)
160
pixels[i] = xcursorimg->pixels[i] & 0xffffffff;
162
QImage qcursorimg((uchar *) pixels.data(), xcursorimg->width, xcursorimg->height,
163
QImage::Format_ARGB32_Premultiplied);
165
QPainter painter(&snapshot);
166
painter.drawImage(QPointF(xcursorimg->x - xcursorimg->xhot - offsetx, xcursorimg->y - xcursorimg ->yhot - offsety), qcursorimg);
171
void ScreenShotEffect::convertFromGLImage(QImage &img, int w, int h)
173
// from QtOpenGL/qgl.cpp
174
// Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
175
// see http://qt.gitorious.org/qt/qt/blobs/master/src/opengl/qgl.cpp
176
if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
177
// OpenGL gives RGBA; Qt wants ARGB
178
uint *p = (uint*)img.bits();
179
uint *end = p + w * h;
186
// OpenGL gives ABGR (i.e. RGBA backwards); Qt wants ARGB
187
for (int y = 0; y < h; y++) {
188
uint *q = (uint*)img.scanLine(y);
189
for (int x = 0; x < w; ++x) {
190
const uint pixel = *q;
191
*q = ((pixel << 16) & 0xff0000) | ((pixel >> 16) & 0xff)
192
| (pixel & 0xff00ff00);
199
img = img.mirrored();