KWin
Loading...
Searching...
No Matches
invert.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2007 Rivo Laks <rivolaks@hot.ee>
6 SPDX-FileCopyrightText: 2008 Lucas Murray <lmurray@undefinedfire.com>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10
11#include "invert.h"
12
14#include "opengl/glplatform.h"
15#include "opengl/glutils.h"
16#include <KGlobalAccel>
17#include <KLocalizedString>
18#include <QAction>
19#include <QFile>
20#include <QStandardPaths>
21
22#include <QMatrix4x4>
23
24Q_LOGGING_CATEGORY(KWIN_INVERT, "kwin_effect_invert", QtWarningMsg)
25
26static void ensureResources()
27{
28 // Must initialize resources manually because the effect is a static lib.
29 Q_INIT_RESOURCE(invert);
30}
31
32namespace KWin
33{
34
36 : m_inited(false)
37 , m_valid(true)
38 , m_shader(nullptr)
39 , m_allWindows(false)
40{
41 QAction *a = new QAction(this);
42 a->setObjectName(QStringLiteral("Invert"));
43 a->setText(i18n("Toggle Invert Effect"));
44 KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << (Qt::CTRL | Qt::META | Qt::Key_I));
45 KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << (Qt::CTRL | Qt::META | Qt::Key_I));
46 connect(a, &QAction::triggered, this, &InvertEffect::toggleScreenInversion);
47
48 QAction *b = new QAction(this);
49 b->setObjectName(QStringLiteral("InvertWindow"));
50 b->setText(i18n("Toggle Invert Effect on Window"));
51 KGlobalAccel::self()->setDefaultShortcut(b, QList<QKeySequence>() << (Qt::CTRL | Qt::META | Qt::Key_U));
52 KGlobalAccel::self()->setShortcut(b, QList<QKeySequence>() << (Qt::CTRL | Qt::META | Qt::Key_U));
53 connect(b, &QAction::triggered, this, &InvertEffect::toggleWindow);
54
55 QAction *c = new QAction(this);
56 c->setObjectName(QStringLiteral("Invert Screen Colors"));
57 c->setText(i18n("Invert Screen Colors"));
58 KGlobalAccel::self()->setDefaultShortcut(c, QList<QKeySequence>());
59 KGlobalAccel::self()->setShortcut(c, QList<QKeySequence>());
60 connect(c, &QAction::triggered, this, &InvertEffect::toggleScreenInversion);
61
64}
65
67
72
73bool InvertEffect::isInvertable(EffectWindow *window) const
74{
75 return m_allWindows != m_windows.contains(window);
76}
77
78void InvertEffect::invert(EffectWindow *window)
79{
80 if (m_valid && !m_inited) {
81 m_valid = loadData();
82 }
83
84 redirect(window);
85 setShader(window, m_shader.get());
86}
87
88void InvertEffect::uninvert(EffectWindow *window)
89{
90 unredirect(window);
91}
92
94{
95 ensureResources();
96 m_inited = true;
97
98 m_shader = ShaderManager::instance()->generateShaderFromFile(ShaderTrait::MapTexture, QString(), QStringLiteral(":/effects/invert/shaders/invert.frag"));
99 if (!m_shader->isValid()) {
100 qCCritical(KWIN_INVERT) << "The shader failed to load!";
101 return false;
102 }
103
104 return true;
105}
106
108{
109 if (isInvertable(w)) {
110 invert(w);
111 }
112}
113
115{
116 m_windows.removeOne(w);
117}
118
120{
121 m_allWindows = !m_allWindows;
122
123 const auto windows = effects->stackingOrder();
124 for (EffectWindow *window : windows) {
125 if (isInvertable(window)) {
126 invert(window);
127 } else {
128 uninvert(window);
129 }
130 }
131
133}
134
136{
137 if (!effects->activeWindow()) {
138 return;
139 }
140 if (!m_windows.contains(effects->activeWindow())) {
141 m_windows.append(effects->activeWindow());
142 } else {
143 m_windows.removeOne(effects->activeWindow());
144 }
145 if (isInvertable(effects->activeWindow())) {
146 invert(effects->activeWindow());
147 } else {
148 uninvert(effects->activeWindow());
149 }
151}
152
154{
155 return m_valid && (m_allWindows || !m_windows.isEmpty());
156}
157
159{
160 return f == ScreenInversion;
161}
162
163} // namespace
164
165#include "moc_invert.cpp"
Representation of a window used by/for Effect classes.
Q_SCRIPTABLE void addRepaintFull()
void windowClosed(KWin::EffectWindow *w)
QList< EffectWindow * > stackingOrder
CompositingType compositingType
KWin::EffectWindow * activeWindow
void windowAdded(KWin::EffectWindow *w)
Q_SCRIPTABLE void addRepaintFull()
static bool supported()
Definition invert.cpp:68
void slotWindowAdded(KWin::EffectWindow *w)
Definition invert.cpp:107
void slotWindowClosed(KWin::EffectWindow *w)
Definition invert.cpp:114
bool provides(Feature) override
Definition invert.cpp:158
~InvertEffect() override
bool isActive() const override
Definition invert.cpp:153
void toggleScreenInversion()
Definition invert.cpp:119
void unredirect(EffectWindow *window)
void redirect(EffectWindow *window)
void setShader(EffectWindow *window, GLShader *shader)
std::unique_ptr< GLShader > generateShaderFromFile(ShaderTraits traits, const QString &vertexFile=QString(), const QString &fragmentFile=QString())
static ShaderManager * instance()
@ ScreenInversion
Definition effect.h:576
@ OpenGLCompositing
Definition globals.h:37
EffectsHandler * effects