kwin/effects/lookingglass/lookingglass.cpp
Martin Gräßlin 3f0ff9b8d5 Migrate effects from displayWidth()/displayHeight() to virtualScreenSize()/virtualScreenGeometry()
Rational behind this change is that displayWidth and displayHeight are
X specific API calls in kwinglobals. For the future it's easier to only
rely on functionality which goes through the EffectsHandler API which
allows easier adjustments in KWin core.

displayWidth() and displayHeight() are only used to get the size or the
complete rect of all screens. This is also provided by:

effects->virtualScreenGeometry() or
effects->virtualScreenSize()

REVIEW: 116021
2014-03-07 08:01:44 +01:00

264 lines
8.7 KiB
C++

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2007 Rivo Laks <rivolaks@hot.ee>
Copyright (C) 2007 Christian Nitschkowski <christian.nitschkowski@kdemail.net>
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 "lookingglass.h"
// KConfigSkeleton
#include "lookingglassconfig.h"
#include <QAction>
#include <kwinglutils.h>
#include <kwinglplatform.h>
#include <KDE/KStandardAction>
#include <KDE/KGlobalAccel>
#include <KDE/KLocalizedString>
#include <QVector2D>
#include <kmessagebox.h>
namespace KWin
{
LookingGlassEffect::LookingGlassEffect()
: zoom(1.0f)
, target_zoom(1.0f)
, polling(false)
, m_texture(NULL)
, m_fbo(NULL)
, m_vbo(NULL)
, m_shader(NULL)
, m_enabled(false)
, m_valid(false)
{
QAction* a;
a = KStandardAction::zoomIn(this, SLOT(zoomIn()), this);
KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_Equal);
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_Equal);
a = KStandardAction::zoomOut(this, SLOT(zoomOut()), this);
KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_Minus);
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_Minus);
a = KStandardAction::actualSize(this, SLOT(toggle()), this);
KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_0);
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_0);
connect(effects, SIGNAL(mouseChanged(QPoint,QPoint,Qt::MouseButtons,Qt::MouseButtons,Qt::KeyboardModifiers,Qt::KeyboardModifiers)),
this, SLOT(slotMouseChanged(QPoint,QPoint,Qt::MouseButtons,Qt::MouseButtons,Qt::KeyboardModifiers,Qt::KeyboardModifiers)));
reconfigure(ReconfigureAll);
}
LookingGlassEffect::~LookingGlassEffect()
{
delete m_texture;
delete m_fbo;
delete m_shader;
delete m_vbo;
}
bool LookingGlassEffect::supported()
{
return effects->compositingType() == OpenGL2Compositing;
}
void LookingGlassEffect::reconfigure(ReconfigureFlags)
{
LookingGlassConfig::self()->readConfig();
initialradius = LookingGlassConfig::radius();
radius = initialradius;
qCDebug(KWINEFFECTS) << QStringLiteral("Radius from config: %1").arg(radius) << endl;
m_valid = loadData();
}
bool LookingGlassEffect::loadData()
{
// If NPOT textures are not supported, use nearest power-of-two sized
// texture. It wastes memory, but it's possible to support systems without
// NPOT textures that way
const QSize screenSize = effects->virtualScreenSize();
int texw = screenSize.width();
int texh = screenSize.height();
if (!GLTexture::NPOTTextureSupported()) {
qWarning() << "NPOT textures not supported, wasting some memory" ;
texw = nearestPowerOfTwo(texw);
texh = nearestPowerOfTwo(texh);
}
// Create texture and render target
m_texture = new GLTexture(texw, texh);
m_texture->setFilter(GL_LINEAR_MIPMAP_LINEAR);
m_texture->setWrapMode(GL_CLAMP_TO_EDGE);
m_fbo = new GLRenderTarget(*m_texture);
if (!m_fbo->valid()) {
return false;
}
QString shadersDir = QStringLiteral("kwin/shaders/1.10/");
#ifdef KWIN_HAVE_OPENGLES
const qint64 coreVersionNumber = kVersionNumber(3, 0);
#else
const qint64 coreVersionNumber = kVersionNumber(1, 40);
#endif
if (GLPlatform::instance()->glslVersion() >= coreVersionNumber)
shadersDir = QStringLiteral("kwin/shaders/1.40/");
const QString fragmentshader = QStandardPaths::locate(QStandardPaths::GenericDataLocation, shadersDir + QStringLiteral("lookingglass.frag"));
m_shader = ShaderManager::instance()->loadFragmentShader(ShaderManager::SimpleShader, fragmentshader);
if (m_shader->isValid()) {
ShaderBinder binder(m_shader);
m_shader->setUniform("u_textureSize", QVector2D(screenSize.width(), screenSize.height()));
} else {
qCritical() << "The shader failed to load!" << endl;
return false;
}
m_vbo = new GLVertexBuffer(GLVertexBuffer::Static);
QVector<float> verts;
QVector<float> texcoords;
texcoords << screenSize.width() << 0.0;
verts << screenSize.width() << 0.0;
texcoords << 0.0 << 0.0;
verts << 0.0 << 0.0;
texcoords << 0.0 << screenSize.height();
verts << 0.0 << screenSize.height();
texcoords << 0.0 << screenSize.height();
verts << 0.0 << screenSize.height();
texcoords << screenSize.width() << screenSize.height();
verts << screenSize.width() << screenSize.height();
texcoords << screenSize.width() << 0.0;
verts << screenSize.width() << 0.0;
m_vbo->setData(6, 2, verts.constData(), texcoords.constData());
return true;
}
void LookingGlassEffect::toggle()
{
if (target_zoom == 1.0f) {
target_zoom = 2.0f;
if (!polling) {
polling = true;
effects->startMousePolling();
}
m_enabled = true;
} else {
target_zoom = 1.0f;
if (polling) {
polling = false;
effects->stopMousePolling();
}
if (zoom == target_zoom) {
m_enabled = false;
}
}
effects->addRepaint(cursorPos().x() - radius, cursorPos().y() - radius, 2 * radius, 2 * radius);
}
void LookingGlassEffect::zoomIn()
{
target_zoom = qMin(7.0, target_zoom + 0.5);
m_enabled = true;
if (!polling) {
polling = true;
effects->startMousePolling();
}
effects->addRepaint(cursorPos().x() - radius, cursorPos().y() - radius, 2 * radius, 2 * radius);
}
void LookingGlassEffect::zoomOut()
{
target_zoom -= 0.5;
if (target_zoom < 1) {
target_zoom = 1;
if (polling) {
polling = false;
effects->stopMousePolling();
}
if (zoom == target_zoom) {
m_enabled = false;
}
}
effects->addRepaint(cursorPos().x() - radius, cursorPos().y() - radius, 2 * radius, 2 * radius);
}
void LookingGlassEffect::prePaintScreen(ScreenPrePaintData& data, int time)
{
if (zoom != target_zoom) {
double diff = time / animationTime(500.0);
if (target_zoom > zoom)
zoom = qMin(zoom * qMax(1.0 + diff, 1.2), target_zoom);
else
zoom = qMax(zoom * qMin(1.0 - diff, 0.8), target_zoom);
qCDebug(KWINEFFECTS) << "zoom is now " << zoom;
radius = qBound((double)initialradius, initialradius * zoom, 3.5 * initialradius);
if (zoom <= 1.0f) {
m_enabled = false;
}
effects->addRepaint(cursorPos().x() - radius, cursorPos().y() - radius, 2 * radius, 2 * radius);
}
if (m_valid && m_enabled) {
data.mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS;
// Start rendering to texture
GLRenderTarget::pushRenderTarget(m_fbo);
}
effects->prePaintScreen(data, time);
}
void LookingGlassEffect::slotMouseChanged(const QPoint& pos, const QPoint& old, Qt::MouseButtons,
Qt::MouseButtons, Qt::KeyboardModifiers, Qt::KeyboardModifiers)
{
if (pos != old && m_enabled) {
effects->addRepaint(pos.x() - radius, pos.y() - radius, 2 * radius, 2 * radius);
effects->addRepaint(old.x() - radius, old.y() - radius, 2 * radius, 2 * radius);
}
}
void LookingGlassEffect::postPaintScreen()
{
// Call the next effect.
effects->postPaintScreen();
if (m_valid && m_enabled) {
// Disable render texture
GLRenderTarget* target = GLRenderTarget::popRenderTarget();
assert(target == m_fbo);
Q_UNUSED(target);
m_texture->bind();
// Use the shader
ShaderBinder binder(m_shader);
m_shader->setUniform("u_zoom", (float)zoom);
m_shader->setUniform("u_radius", (float)radius);
m_shader->setUniform("u_cursor", QVector2D(cursorPos().x(), cursorPos().y()));
m_vbo->render(GL_TRIANGLES);
m_texture->unbind();
}
}
bool LookingGlassEffect::isActive() const
{
return m_valid && m_enabled;
}
} // namespace
#include "lookingglass.moc"