1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2007 Rivo Laks <rivolaks@hot.ee>
6 Copyright (C) 2008 Lucas Murray <lmurray@undefinedfire.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *********************************************************************/
24 #include <kwinglutils.h>
25 #include <kactioncollection.h>
29 #include <kwinshadereffect.h>
30 #include <KStandardDirs>
35 KWIN_EFFECT( invert
, InvertEffect
)
36 KWIN_EFFECT_SUPPORTED( invert
, ShaderEffect::supported() )
38 InvertEffect::InvertEffect()
44 KActionCollection
* actionCollection
= new KActionCollection( this );
46 KAction
* a
= (KAction
*)actionCollection
->addAction( "Invert" );
47 a
->setText( i18n( "Toggle Invert Effect" ));
48 a
->setGlobalShortcut( KShortcut( Qt::CTRL
+ Qt::META
+ Qt::Key_I
));
49 connect(a
, SIGNAL( triggered(bool) ), this, SLOT( toggle() ));
51 KAction
* b
= (KAction
*)actionCollection
->addAction( "InvertWindow" );
52 b
->setText( i18n( "Toggle Invert Effect on Window" ));
53 b
->setGlobalShortcut( KShortcut( Qt::CTRL
+ Qt::META
+ Qt::Key_U
));
54 connect(b
, SIGNAL( triggered(bool) ), this, SLOT( toggleWindow() ));
57 InvertEffect::~InvertEffect()
62 bool InvertEffect::loadData()
66 QString fragmentshader
= KGlobal::dirs()->findResource("data", "kwin/invert.frag");
67 QString vertexshader
= KGlobal::dirs()->findResource("data", "kwin/invert.vert");
68 if(fragmentshader
.isEmpty() || vertexshader
.isEmpty())
70 kError(1212) << "Couldn't locate shader files" << endl
;
74 m_shader
= new GLShader(vertexshader
, fragmentshader
);
75 if( !m_shader
->isValid() )
77 kError(1212) << "The shader failed to load!" << endl
;
83 m_shader
->setUniform("winTexture", 0);
90 void InvertEffect::paintWindow( EffectWindow
* w
, int mask
, QRegion region
, WindowPaintData
& data
)
92 // Load if we haven't already
93 if( m_valid
&& !m_inited
)
96 bool useShader
= m_valid
&& ( m_allWindows
!= m_windows
.contains( w
));
101 int texw
= w
->width();
102 int texh
= w
->height();
103 if( !GLTexture::NPOTTextureSupported() )
105 kWarning( 1212 ) << "NPOT textures not supported, wasting some memory" ;
106 texw
= nearestPowerOfTwo(texw
);
107 texh
= nearestPowerOfTwo(texh
);
109 m_shader
->setUniform("textureWidth", (float)texw
);
110 m_shader
->setUniform("textureHeight", (float)texh
);
112 data
.shader
= m_shader
;
115 effects
->paintWindow( w
, mask
, region
, data
);
121 void InvertEffect::windowClosed( EffectWindow
* w
)
123 m_windows
.removeOne( w
);
126 void InvertEffect::toggle()
128 m_allWindows
= !m_allWindows
;
129 effects
->addRepaintFull();
132 void InvertEffect::toggleWindow()
134 if( !m_windows
.contains( effects
->activeWindow() ))
135 m_windows
.append( effects
->activeWindow() );
137 m_windows
.removeOne( effects
->activeWindow() );
138 effects
->activeWindow()->addRepaintFull();
143 #include "invert.moc"