2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2007-07-04 09:59:07 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2007 Rivo Laks <rivolaks@hot.ee>
|
|
|
|
SPDX-FileCopyrightText: 2008 Lucas Murray <lmurray@undefinedfire.com>
|
2007-07-04 09:59:07 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2007-07-04 09:59:07 +00:00
|
|
|
|
|
|
|
#include "invert.h"
|
|
|
|
|
2013-08-14 19:13:12 +00:00
|
|
|
#include <QAction>
|
2015-11-27 15:50:21 +00:00
|
|
|
#include <QFile>
|
2008-08-27 12:20:34 +00:00
|
|
|
#include <kwinglutils.h>
|
2011-02-19 10:08:43 +00:00
|
|
|
#include <kwinglplatform.h>
|
2014-03-17 15:24:10 +00:00
|
|
|
#include <KGlobalAccel>
|
|
|
|
#include <KLocalizedString>
|
2013-08-09 12:19:09 +00:00
|
|
|
#include <QStandardPaths>
|
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
|
|
|
|
{
|
|
|
|
|
2008-08-27 12:20:34 +00:00
|
|
|
InvertEffect::InvertEffect()
|
2011-01-30 14:34:42 +00:00
|
|
|
: m_inited(false),
|
|
|
|
m_valid(true),
|
Use nullptr everywhere
Summary:
Because KWin is a very old project, we use three kinds of null pointer
literals: 0, NULL, and nullptr. Since C++11, it's recommended to use
nullptr keyword.
This change converts all usages of 0 and NULL literal to nullptr. Even
though it breaks git history, we need to do it in order to have consistent
code as well to ease code reviews (it's very tempting for some people to
add unrelated changes to their patches, e.g. converting NULL to nullptr).
Test Plan: Compiles.
Reviewers: #kwin, davidedmundson, romangg
Reviewed By: #kwin, davidedmundson, romangg
Subscribers: romangg, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D23618
2019-09-19 14:46:54 +00:00
|
|
|
m_shader(nullptr),
|
2011-01-30 14:34:42 +00:00
|
|
|
m_allWindows(false)
|
|
|
|
{
|
2013-12-10 10:45:33 +00:00
|
|
|
QAction* a = new QAction(this);
|
|
|
|
a->setObjectName(QStringLiteral("Invert"));
|
2011-01-30 14:34:42 +00:00
|
|
|
a->setText(i18n("Toggle Invert Effect"));
|
2013-08-14 19:13:12 +00:00
|
|
|
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);
|
2013-07-10 10:26:50 +00:00
|
|
|
effects->registerGlobalShortcut(Qt::CTRL + Qt::META + Qt::Key_I, a);
|
2019-01-01 20:48:53 +00:00
|
|
|
connect(a, &QAction::triggered, this, &InvertEffect::toggleScreenInversion);
|
2011-01-30 14:34:42 +00:00
|
|
|
|
2013-12-10 10:45:33 +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"));
|
2013-08-14 19:13:12 +00:00
|
|
|
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);
|
2013-07-10 10:26:50 +00:00
|
|
|
effects->registerGlobalShortcut(Qt::CTRL + Qt::META + Qt::Key_U, b);
|
2019-01-01 20:48:53 +00:00
|
|
|
connect(b, &QAction::triggered, this, &InvertEffect::toggleWindow);
|
2013-08-14 19:13:12 +00:00
|
|
|
|
2019-01-01 20:48:53 +00:00
|
|
|
connect(effects, &EffectsHandler::windowClosed, this, &InvertEffect::slotWindowClosed);
|
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
|
|
|
|
2016-01-26 14:38:42 +00:00
|
|
|
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!";
|
2008-08-27 12:20:34 +00:00
|
|
|
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
|
|
|
|
2019-10-29 22:04:15 +00:00
|
|
|
void InvertEffect::drawWindow(EffectWindow* w, int mask, const QRegion ®ion, WindowPaintData& data)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
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();
|
|
|
|
shaderManager->pushShader(m_shader);
|
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
|
|
|
|
2019-10-29 22:04:15 +00:00
|
|
|
void InvertEffect::paintEffectFrame(KWin::EffectFrame* frame, const QRegion ®ion, double opacity, double frameOpacity)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (m_valid && m_allWindows) {
|
|
|
|
frame->setShader(m_shader);
|
2012-09-21 09:25:08 +00:00
|
|
|
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);
|
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
|
|
|
|
|