kwin/effects/invert/invert.cpp

152 lines
4.4 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2007 Rivo Laks <rivolaks@hot.ee>
Copyright (C) 2008 Lucas Murray <lmurray@undefinedfire.com>
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 "invert.h"
#include <QAction>
#include <QFile>
#include <kwinglutils.h>
#include <kwinglplatform.h>
#include <KGlobalAccel>
#include <KLocalizedString>
#include <QStandardPaths>
#include <QMatrix4x4>
namespace KWin
{
InvertEffect::InvertEffect()
2011-01-30 14:34:42 +00:00
: m_inited(false),
m_valid(true),
m_shader(NULL),
m_allWindows(false)
{
QAction* a = new QAction(this);
a->setObjectName(QStringLiteral("Invert"));
2011-01-30 14:34:42 +00:00
a->setText(i18n("Toggle Invert Effect"));
KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << Qt::CTRL + Qt::META + Qt::Key_I);
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << Qt::CTRL + Qt::META + Qt::Key_I);
effects->registerGlobalShortcut(Qt::CTRL + Qt::META + Qt::Key_I, a);
connect(a, SIGNAL(triggered(bool)), this, SLOT(toggleScreenInversion()));
2011-01-30 14:34:42 +00:00
QAction* b = new QAction(this);
b->setObjectName(QStringLiteral("InvertWindow"));
2011-01-30 14:34:42 +00:00
b->setText(i18n("Toggle Invert Effect on Window"));
KGlobalAccel::self()->setDefaultShortcut(b, QList<QKeySequence>() << Qt::CTRL + Qt::META + Qt::Key_U);
KGlobalAccel::self()->setShortcut(b, QList<QKeySequence>() << Qt::CTRL + Qt::META + Qt::Key_U);
effects->registerGlobalShortcut(Qt::CTRL + Qt::META + Qt::Key_U, b);
2011-01-30 14:34:42 +00:00
connect(b, SIGNAL(triggered(bool)), this, SLOT(toggleWindow()));
connect(effects, SIGNAL(windowClosed(KWin::EffectWindow*)), this, SLOT(slotWindowClosed(KWin::EffectWindow*)));
2011-01-30 14:34:42 +00:00
}
InvertEffect::~InvertEffect()
2011-01-30 14:34:42 +00:00
{
delete m_shader;
2011-01-30 14:34:42 +00:00
}
bool InvertEffect::supported()
{
return effects->compositingType() == OpenGL2Compositing;
}
bool InvertEffect::loadData()
{
m_inited = true;
m_shader = ShaderManager::instance()->generateShaderFromResources(ShaderTrait::MapTexture, QString(), QStringLiteral("invert.frag"));
2011-01-30 14:34:42 +00:00
if (!m_shader->isValid()) {
2015-07-31 08:17:43 +00:00
qCCritical(KWINEFFECTS) << "The shader failed to load!";
return false;
2011-01-30 14:34:42 +00:00
}
return true;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void InvertEffect::drawWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data)
{
// Load if we haven't already
2011-01-30 14:34:42 +00:00
if (m_valid && !m_inited)
m_valid = loadData();
2011-01-30 14:34:42 +00:00
bool useShader = m_valid && (m_allWindows != m_windows.contains(w));
if (useShader) {
ShaderManager *shaderManager = ShaderManager::instance();
shaderManager->pushShader(m_shader);
data.shader = m_shader;
}
2011-01-30 14:34:42 +00:00
effects->drawWindow(w, mask, region, data);
if (useShader) {
ShaderManager::instance()->popShader();
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void InvertEffect::paintEffectFrame(KWin::EffectFrame* frame, QRegion region, double opacity, double frameOpacity)
{
if (m_valid && m_allWindows) {
frame->setShader(m_shader);
ShaderBinder binder(m_shader);
2011-01-30 14:34:42 +00:00
effects->paintEffectFrame(frame, region, opacity, frameOpacity);
} else {
effects->paintEffectFrame(frame, region, opacity, frameOpacity);
}
2011-01-30 14:34:42 +00:00
}
void InvertEffect::slotWindowClosed(EffectWindow* w)
2011-01-30 14:34:42 +00:00
{
m_windows.removeOne(w);
}
void InvertEffect::toggleScreenInversion()
2011-01-30 14:34:42 +00:00
{
m_allWindows = !m_allWindows;
effects->addRepaintFull();
2011-01-30 14:34:42 +00:00
}
void InvertEffect::toggleWindow()
2011-01-30 14:34:42 +00:00
{
if (!effects->activeWindow()) {
return;
}
2011-01-30 14:34:42 +00:00
if (!m_windows.contains(effects->activeWindow()))
m_windows.append(effects->activeWindow());
else
2011-01-30 14:34:42 +00:00
m_windows.removeOne(effects->activeWindow());
effects->activeWindow()->addRepaintFull();
2011-01-30 14:34:42 +00:00
}
bool InvertEffect::isActive() const
{
return m_valid && (m_allWindows || !m_windows.isEmpty());
}
bool InvertEffect::provides(Feature f)
{
return f == ScreenInversion;
}
} // namespace