2007-11-27 19:40:25 +00:00
|
|
|
/********************************************************************
|
2007-07-13 13:22:09 +00:00
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2007 Rivo Laks <rivolaks@hot.ee>
|
2007-11-13 16:20:52 +00:00
|
|
|
Copyright (C) 2007 Christian Nitschkowski <christian.nitschkowski@kdemail.net>
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2007-11-27 19:40:25 +00:00
|
|
|
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.
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2007-11-27 19:40:25 +00:00
|
|
|
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/>.
|
|
|
|
*********************************************************************/
|
2007-07-13 13:22:09 +00:00
|
|
|
|
|
|
|
#include "lookingglass.h"
|
|
|
|
|
2012-09-14 08:47:50 +00:00
|
|
|
// KConfigSkeleton
|
|
|
|
#include "lookingglassconfig.h"
|
|
|
|
|
2013-08-14 19:13:12 +00:00
|
|
|
#include <QAction>
|
2007-07-13 13:22:09 +00:00
|
|
|
#include <kwinglutils.h>
|
2011-02-19 10:08:43 +00:00
|
|
|
#include <kwinglplatform.h>
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2014-03-17 15:24:10 +00:00
|
|
|
#include <KStandardAction>
|
|
|
|
#include <KGlobalAccel>
|
|
|
|
#include <KLocalizedString>
|
2011-01-06 16:18:27 +00:00
|
|
|
#include <QVector2D>
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2007-11-13 16:20:52 +00:00
|
|
|
#include <kmessagebox.h>
|
2007-07-13 13:22:09 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2011-01-06 16:18:27 +00:00
|
|
|
LookingGlassEffect::LookingGlassEffect()
|
2011-02-25 19:25:21 +00:00
|
|
|
: zoom(1.0f)
|
2011-01-30 14:34:42 +00:00
|
|
|
, target_zoom(1.0f)
|
|
|
|
, polling(false)
|
|
|
|
, m_texture(NULL)
|
|
|
|
, m_fbo(NULL)
|
|
|
|
, m_vbo(NULL)
|
|
|
|
, m_shader(NULL)
|
|
|
|
, m_enabled(false)
|
|
|
|
, m_valid(false)
|
|
|
|
{
|
2013-08-14 19:13:12 +00:00
|
|
|
QAction* a;
|
2013-12-10 10:45:33 +00:00
|
|
|
a = KStandardAction::zoomIn(this, SLOT(zoomIn()), this);
|
2013-08-14 19:13:12 +00:00
|
|
|
KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_Equal);
|
|
|
|
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_Equal);
|
2013-07-10 10:26:50 +00:00
|
|
|
effects->registerGlobalShortcut(Qt::META + Qt::Key_Equal, a);
|
2013-08-14 19:13:12 +00:00
|
|
|
|
2013-12-10 10:45:33 +00:00
|
|
|
a = KStandardAction::zoomOut(this, SLOT(zoomOut()), this);
|
2013-08-14 19:13:12 +00:00
|
|
|
KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_Minus);
|
|
|
|
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_Minus);
|
2013-07-10 10:26:50 +00:00
|
|
|
effects->registerGlobalShortcut(Qt::META + Qt::Key_Minus, a);
|
2013-08-14 19:13:12 +00:00
|
|
|
|
2013-12-10 10:45:33 +00:00
|
|
|
a = KStandardAction::actualSize(this, SLOT(toggle()), this);
|
2013-08-14 19:13:12 +00:00
|
|
|
KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_0);
|
|
|
|
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << Qt::META + Qt::Key_0);
|
2013-07-10 10:26:50 +00:00
|
|
|
effects->registerGlobalShortcut(Qt::META + Qt::Key_0, a);
|
2013-08-14 19:13:12 +00:00
|
|
|
|
2011-03-12 13:37:30 +00:00
|
|
|
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)));
|
2011-01-30 14:34:42 +00:00
|
|
|
reconfigure(ReconfigureAll);
|
|
|
|
}
|
2007-11-13 16:20:52 +00:00
|
|
|
|
|
|
|
LookingGlassEffect::~LookingGlassEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-01-06 16:18:27 +00:00
|
|
|
delete m_texture;
|
|
|
|
delete m_fbo;
|
|
|
|
delete m_shader;
|
|
|
|
delete m_vbo;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2011-01-06 16:18:27 +00:00
|
|
|
bool LookingGlassEffect::supported()
|
|
|
|
{
|
2012-09-20 09:33:32 +00:00
|
|
|
return effects->compositingType() == OpenGL2Compositing;
|
2011-01-06 16:18:27 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void LookingGlassEffect::reconfigure(ReconfigureFlags)
|
|
|
|
{
|
2014-03-25 15:29:03 +00:00
|
|
|
LookingGlassConfig::self()->read();
|
2012-09-14 08:47:50 +00:00
|
|
|
initialradius = LookingGlassConfig::radius();
|
2008-10-02 09:27:32 +00:00
|
|
|
radius = initialradius;
|
2013-11-29 05:18:28 +00:00
|
|
|
qCDebug(KWINEFFECTS) << QStringLiteral("Radius from config: %1").arg(radius) << endl;
|
2011-01-06 16:18:27 +00:00
|
|
|
m_valid = loadData();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-01-06 16:18:27 +00:00
|
|
|
|
|
|
|
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
|
2014-02-24 15:13:30 +00:00
|
|
|
const QSize screenSize = effects->virtualScreenSize();
|
|
|
|
int texw = screenSize.width();
|
|
|
|
int texh = screenSize.height();
|
2011-01-06 16:18:27 +00:00
|
|
|
if (!GLTexture::NPOTTextureSupported()) {
|
2013-09-02 09:03:29 +00:00
|
|
|
qWarning() << "NPOT textures not supported, wasting some memory" ;
|
2011-01-06 16:18:27 +00:00
|
|
|
texw = nearestPowerOfTwo(texw);
|
|
|
|
texh = nearestPowerOfTwo(texh);
|
2008-10-02 09:27:32 +00:00
|
|
|
}
|
2011-01-06 16:18:27 +00:00
|
|
|
// 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);
|
|
|
|
|
2011-07-17 15:57:30 +00:00
|
|
|
m_fbo = new GLRenderTarget(*m_texture);
|
2011-01-06 16:18:27 +00:00
|
|
|
if (!m_fbo->valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-23 05:02:52 +00:00
|
|
|
QString shadersDir = QStringLiteral("kwin/shaders/1.10/");
|
2013-05-23 07:55:16 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
const qint64 coreVersionNumber = kVersionNumber(3, 0);
|
|
|
|
#else
|
|
|
|
const qint64 coreVersionNumber = kVersionNumber(1, 40);
|
2013-05-21 08:56:10 +00:00
|
|
|
#endif
|
2013-05-23 07:55:16 +00:00
|
|
|
if (GLPlatform::instance()->glslVersion() >= coreVersionNumber)
|
2013-07-23 05:02:52 +00:00
|
|
|
shadersDir = QStringLiteral("kwin/shaders/1.40/");
|
2013-08-09 12:19:09 +00:00
|
|
|
const QString fragmentshader = QStandardPaths::locate(QStandardPaths::GenericDataLocation, shadersDir + QStringLiteral("lookingglass.frag"));
|
2011-01-06 16:18:27 +00:00
|
|
|
m_shader = ShaderManager::instance()->loadFragmentShader(ShaderManager::SimpleShader, fragmentshader);
|
|
|
|
if (m_shader->isValid()) {
|
2012-09-21 09:25:08 +00:00
|
|
|
ShaderBinder binder(m_shader);
|
2014-02-24 15:13:30 +00:00
|
|
|
m_shader->setUniform("u_textureSize", QVector2D(screenSize.width(), screenSize.height()));
|
2011-01-06 16:18:27 +00:00
|
|
|
} else {
|
2013-09-02 09:03:29 +00:00
|
|
|
qCritical() << "The shader failed to load!" << endl;
|
2011-01-06 16:18:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_vbo = new GLVertexBuffer(GLVertexBuffer::Static);
|
|
|
|
QVector<float> verts;
|
|
|
|
QVector<float> texcoords;
|
2014-02-24 15:13:30 +00:00
|
|
|
texcoords << screenSize.width() << 0.0;
|
|
|
|
verts << screenSize.width() << 0.0;
|
2011-01-06 16:18:27 +00:00
|
|
|
texcoords << 0.0 << 0.0;
|
|
|
|
verts << 0.0 << 0.0;
|
2014-02-24 15:13:30 +00:00
|
|
|
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;
|
2011-01-06 16:18:27 +00:00
|
|
|
m_vbo->setData(6, 2, verts.constData(), texcoords.constData());
|
|
|
|
return true;
|
|
|
|
}
|
2008-10-02 09:27:32 +00:00
|
|
|
|
2007-07-13 13:22:09 +00:00
|
|
|
void LookingGlassEffect::toggle()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (target_zoom == 1.0f) {
|
2007-07-13 13:22:09 +00:00
|
|
|
target_zoom = 2.0f;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!polling) {
|
2009-04-27 20:20:05 +00:00
|
|
|
polling = true;
|
|
|
|
effects->startMousePolling();
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
m_enabled = true;
|
|
|
|
} else {
|
2007-07-13 13:22:09 +00:00
|
|
|
target_zoom = 1.0f;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (polling) {
|
2009-04-27 20:20:05 +00:00
|
|
|
polling = false;
|
|
|
|
effects->stopMousePolling();
|
|
|
|
}
|
2011-09-07 07:40:57 +00:00
|
|
|
if (zoom == target_zoom) {
|
|
|
|
m_enabled = false;
|
|
|
|
}
|
2007-07-13 13:22:09 +00:00
|
|
|
}
|
2011-09-07 07:40:57 +00:00
|
|
|
effects->addRepaint(cursorPos().x() - radius, cursorPos().y() - radius, 2 * radius, 2 * radius);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-07-13 13:22:09 +00:00
|
|
|
|
|
|
|
void LookingGlassEffect::zoomIn()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-09-03 15:00:43 +00:00
|
|
|
target_zoom = qMin(7.0, target_zoom + 0.5);
|
2011-01-06 16:18:27 +00:00
|
|
|
m_enabled = true;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!polling) {
|
2009-02-01 15:16:52 +00:00
|
|
|
polling = true;
|
|
|
|
effects->startMousePolling();
|
2007-07-13 13:22:09 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
effects->addRepaint(cursorPos().x() - radius, cursorPos().y() - radius, 2 * radius, 2 * radius);
|
|
|
|
}
|
2007-07-13 13:22:09 +00:00
|
|
|
|
|
|
|
void LookingGlassEffect::zoomOut()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-07-13 13:22:09 +00:00
|
|
|
target_zoom -= 0.5;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (target_zoom < 1) {
|
2007-07-13 13:22:09 +00:00
|
|
|
target_zoom = 1;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (polling) {
|
2009-04-27 20:20:05 +00:00
|
|
|
polling = false;
|
|
|
|
effects->stopMousePolling();
|
2009-02-01 15:16:52 +00:00
|
|
|
}
|
2011-09-07 07:40:57 +00:00
|
|
|
if (zoom == target_zoom) {
|
|
|
|
m_enabled = false;
|
|
|
|
}
|
2007-07-13 13:22:09 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
effects->addRepaint(cursorPos().x() - radius, cursorPos().y() - radius, 2 * radius, 2 * radius);
|
|
|
|
}
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
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);
|
2007-07-13 13:22:09 +00:00
|
|
|
else
|
2011-01-30 14:34:42 +00:00
|
|
|
zoom = qMax(zoom * qMin(1.0 - diff, 0.8), target_zoom);
|
2013-11-29 05:18:28 +00:00
|
|
|
qCDebug(KWINEFFECTS) << "zoom is now " << zoom;
|
2011-01-30 14:34:42 +00:00
|
|
|
radius = qBound((double)initialradius, initialradius * zoom, 3.5 * initialradius);
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (zoom <= 1.0f) {
|
2011-01-06 16:18:27 +00:00
|
|
|
m_enabled = false;
|
|
|
|
}
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
effects->addRepaint(cursorPos().x() - radius, cursorPos().y() - radius, 2 * radius, 2 * radius);
|
|
|
|
}
|
2011-01-06 16:18:27 +00:00
|
|
|
if (m_valid && m_enabled) {
|
|
|
|
data.mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS;
|
|
|
|
// Start rendering to texture
|
2011-03-13 13:34:30 +00:00
|
|
|
GLRenderTarget::pushRenderTarget(m_fbo);
|
2011-01-06 16:18:27 +00:00
|
|
|
}
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
effects->prePaintScreen(data, time);
|
|
|
|
}
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2011-03-12 13:37:30 +00:00
|
|
|
void LookingGlassEffect::slotMouseChanged(const QPoint& pos, const QPoint& old, Qt::MouseButtons,
|
2011-01-30 14:34:42 +00:00
|
|
|
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);
|
2007-07-13 13:22:09 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-07-13 13:22:09 +00:00
|
|
|
|
2011-01-06 16:18:27 +00:00
|
|
|
void LookingGlassEffect::postPaintScreen()
|
|
|
|
{
|
|
|
|
// Call the next effect.
|
|
|
|
effects->postPaintScreen();
|
|
|
|
if (m_valid && m_enabled) {
|
|
|
|
// Disable render texture
|
2011-03-13 13:34:30 +00:00
|
|
|
GLRenderTarget* target = GLRenderTarget::popRenderTarget();
|
2011-01-30 14:34:42 +00:00
|
|
|
assert(target == m_fbo);
|
|
|
|
Q_UNUSED(target);
|
2011-01-06 16:18:27 +00:00
|
|
|
m_texture->bind();
|
|
|
|
|
|
|
|
// Use the shader
|
2012-09-21 09:25:08 +00:00
|
|
|
ShaderBinder binder(m_shader);
|
2011-01-06 16:18:27 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-27 09:21:31 +00:00
|
|
|
bool LookingGlassEffect::isActive() const
|
|
|
|
{
|
|
|
|
return m_valid && m_enabled;
|
|
|
|
}
|
|
|
|
|
2007-07-13 13:22:09 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#include "lookingglass.moc"
|