2007-11-27 19:40:25 +00:00
|
|
|
/********************************************************************
|
2007-07-04 09:59:07 +00:00
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2007 Rivo Laks <rivolaks@hot.ee>
|
2008-08-27 12:20:34 +00:00
|
|
|
Copyright (C) 2008 Lucas Murray <lmurray@undefinedfire.com>
|
2007-07-04 09:59:07 +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-04 09:59:07 +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-04 09:59:07 +00:00
|
|
|
|
|
|
|
#include "invert.h"
|
|
|
|
|
2008-08-27 12:20:34 +00:00
|
|
|
#include <kwinglutils.h>
|
2011-02-19 10:08:43 +00:00
|
|
|
#include <kwinglplatform.h>
|
2007-07-04 09:59:07 +00:00
|
|
|
#include <kactioncollection.h>
|
|
|
|
#include <kaction.h>
|
2013-03-07 12:28:39 +00:00
|
|
|
#include <KDE/KGlobal>
|
2013-03-07 11:55:39 +00:00
|
|
|
#include <KDE/KLocalizedString>
|
2008-08-27 12:20:34 +00:00
|
|
|
#include <kdebug.h>
|
|
|
|
#include <KStandardDirs>
|
2007-07-04 09:59:07 +00:00
|
|
|
|
2011-01-06 19:00:03 +00:00
|
|
|
#include <QMatrix4x4>
|
|
|
|
|
2007-07-04 09:59:07 +00:00
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
KWIN_EFFECT(invert, InvertEffect)
|
|
|
|
KWIN_EFFECT_SUPPORTED(invert, InvertEffect::supported())
|
2007-07-04 09:59:07 +00:00
|
|
|
|
2008-08-27 12:20:34 +00:00
|
|
|
InvertEffect::InvertEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
: m_inited(false),
|
|
|
|
m_valid(true),
|
|
|
|
m_shader(NULL),
|
|
|
|
m_allWindows(false)
|
|
|
|
{
|
|
|
|
KActionCollection* actionCollection = new KActionCollection(this);
|
|
|
|
|
|
|
|
KAction* a = (KAction*)actionCollection->addAction("Invert");
|
|
|
|
a->setText(i18n("Toggle Invert Effect"));
|
|
|
|
a->setGlobalShortcut(KShortcut(Qt::CTRL + Qt::META + Qt::Key_I));
|
2012-03-23 01:06:46 +00:00
|
|
|
connect(a, SIGNAL(triggered(bool)), this, SLOT(toggleScreenInversion()));
|
2011-01-30 14:34:42 +00:00
|
|
|
|
|
|
|
KAction* b = (KAction*)actionCollection->addAction("InvertWindow");
|
|
|
|
b->setText(i18n("Toggle Invert Effect on Window"));
|
|
|
|
b->setGlobalShortcut(KShortcut(Qt::CTRL + Qt::META + Qt::Key_U));
|
|
|
|
connect(b, SIGNAL(triggered(bool)), this, SLOT(toggleWindow()));
|
2012-01-29 11:29:24 +00:00
|
|
|
connect(effects, SIGNAL(windowClosed(KWin::EffectWindow*)), this, SLOT(slotWindowClosed(KWin::EffectWindow*)));
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-08-27 12:20:34 +00:00
|
|
|
|
|
|
|
InvertEffect::~InvertEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2008-08-27 12:20:34 +00:00
|
|
|
delete m_shader;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-08-27 12:20:34 +00:00
|
|
|
|
2011-01-06 19:00:03 +00:00
|
|
|
bool InvertEffect::supported()
|
|
|
|
{
|
2012-09-20 09:33:32 +00:00
|
|
|
return effects->compositingType() == OpenGL2Compositing;
|
2011-01-06 19:00:03 +00:00
|
|
|
}
|
|
|
|
|
2008-08-27 12:20:34 +00:00
|
|
|
bool InvertEffect::loadData()
|
|
|
|
{
|
|
|
|
m_inited = true;
|
2011-01-06 19:00:03 +00:00
|
|
|
|
2013-05-21 08:39:21 +00:00
|
|
|
QString shadersDir = "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:39:21 +00:00
|
|
|
#endif
|
2013-05-23 07:55:16 +00:00
|
|
|
if (GLPlatform::instance()->glslVersion() >= coreVersionNumber)
|
|
|
|
shadersDir = "kwin/shaders/1.40/";
|
2013-05-21 08:39:21 +00:00
|
|
|
const QString fragmentshader = KGlobal::dirs()->findResource("data", shadersDir + "invert.frag");
|
2008-08-27 12:20:34 +00:00
|
|
|
|
2011-01-06 19:00:03 +00:00
|
|
|
m_shader = ShaderManager::instance()->loadFragmentShader(ShaderManager::GenericShader, fragmentshader);
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!m_shader->isValid()) {
|
2008-08-27 12:20:34 +00:00
|
|
|
kError(1212) << "The shader failed to load!" << endl;
|
|
|
|
return false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-08-27 12:20:34 +00:00
|
|
|
|
|
|
|
return true;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-08-27 12:20:34 +00:00
|
|
|
|
2011-01-06 19:00:03 +00:00
|
|
|
void InvertEffect::prePaintScreen(ScreenPrePaintData &data, int time)
|
|
|
|
{
|
|
|
|
effects->prePaintScreen(data, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InvertEffect::prePaintWindow(EffectWindow *w, WindowPrePaintData &data, int time)
|
|
|
|
{
|
2011-01-30 14:34:42 +00:00
|
|
|
if (m_valid && (m_allWindows != m_windows.contains(w))) {
|
2011-01-06 19:00:03 +00:00
|
|
|
data.mask |= PAINT_WINDOW_TRANSFORMED;
|
|
|
|
}
|
|
|
|
effects->prePaintWindow(w, data, time);
|
|
|
|
}
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void InvertEffect::drawWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data)
|
|
|
|
{
|
2008-08-27 12:20:34 +00:00
|
|
|
// Load if we haven't already
|
2011-01-30 14:34:42 +00:00
|
|
|
if (m_valid && !m_inited)
|
2008-08-27 12:20:34 +00:00
|
|
|
m_valid = loadData();
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
bool useShader = m_valid && (m_allWindows != m_windows.contains(w));
|
2011-01-06 19:00:03 +00:00
|
|
|
if (useShader) {
|
|
|
|
ShaderManager *shaderManager = ShaderManager::instance();
|
|
|
|
GLShader *genericShader = shaderManager->pushShader(ShaderManager::GenericShader);
|
|
|
|
QMatrix4x4 screenTransformation = genericShader->getUniformMatrix4x4("screenTransformation");
|
|
|
|
shaderManager->popShader();
|
|
|
|
shaderManager->pushShader(m_shader);
|
|
|
|
m_shader->setUniform("screenTransformation", screenTransformation);
|
2008-08-27 12:20:34 +00:00
|
|
|
|
|
|
|
data.shader = m_shader;
|
2011-01-06 19:00:03 +00:00
|
|
|
}
|
2008-08-27 12:20:34 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
effects->drawWindow(w, mask, region, data);
|
2008-08-27 12:20:34 +00:00
|
|
|
|
2011-01-06 19:00:03 +00:00
|
|
|
if (useShader) {
|
|
|
|
ShaderManager::instance()->popShader();
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-08-27 12:20:34 +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);
|
2012-09-21 09:25:08 +00:00
|
|
|
ShaderBinder binder(m_shader);
|
2011-01-06 19:00:03 +00:00
|
|
|
m_shader->setUniform("screenTransformation", QMatrix4x4());
|
|
|
|
m_shader->setUniform("windowTransformation", QMatrix4x4());
|
2011-01-30 14:34:42 +00:00
|
|
|
effects->paintEffectFrame(frame, region, opacity, frameOpacity);
|
|
|
|
} else {
|
|
|
|
effects->paintEffectFrame(frame, region, opacity, frameOpacity);
|
2010-07-24 16:29:16 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-07-24 16:29:16 +00:00
|
|
|
|
2011-02-27 08:25:45 +00:00
|
|
|
void InvertEffect::slotWindowClosed(EffectWindow* w)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
m_windows.removeOne(w);
|
|
|
|
}
|
2007-07-04 09:59:07 +00:00
|
|
|
|
2012-03-23 01:06:46 +00:00
|
|
|
void InvertEffect::toggleScreenInversion()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2008-08-27 12:20:34 +00:00
|
|
|
m_allWindows = !m_allWindows;
|
|
|
|
effects->addRepaintFull();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-08-27 12:20:34 +00:00
|
|
|
|
|
|
|
void InvertEffect::toggleWindow()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2013-02-07 11:18:17 +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());
|
2008-08-27 12:20:34 +00:00
|
|
|
else
|
2011-01-30 14:34:42 +00:00
|
|
|
m_windows.removeOne(effects->activeWindow());
|
2008-08-27 12:20:34 +00:00
|
|
|
effects->activeWindow()->addRepaintFull();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-07-04 09:59:07 +00:00
|
|
|
|
2011-08-27 09:21:31 +00:00
|
|
|
bool InvertEffect::isActive() const
|
|
|
|
{
|
|
|
|
return m_valid && (m_allWindows || !m_windows.isEmpty());
|
|
|
|
}
|
|
|
|
|
2012-03-23 01:06:46 +00:00
|
|
|
bool InvertEffect::provides(Feature f)
|
|
|
|
{
|
|
|
|
return f == ScreenInversion;
|
|
|
|
}
|
|
|
|
|
2007-07-04 09:59:07 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#include "invert.moc"
|