not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kwin / effects / dimscreen.cpp
blobcee4df31b706f53201bd2819c7a913511d2375f8
1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2008 Martin Gräßlin <ubuntu@martin-graesslin.com
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
20 #include "dimscreen.h"
22 #include <kwinglutils.h>
24 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
25 #include <GL/gl.h>
26 #endif
28 namespace KWin
31 KWIN_EFFECT( dimscreen, DimScreenEffect )
33 DimScreenEffect::DimScreenEffect()
34 : mActivated( false )
35 , animation( false )
36 , deactivate( false )
38 reconfigure( ReconfigureAll );
39 #ifdef KWIN_HAVE_XRENDER_COMPOSITING
40 alphaFormat = XRenderFindStandardFormat( display(), PictStandardARGB32 );
41 #endif
44 DimScreenEffect::~DimScreenEffect()
48 void DimScreenEffect::reconfigure( ReconfigureFlags )
50 animationDuration = Effect::animationTime( 300 );
53 void DimScreenEffect::prePaintScreen( ScreenPrePaintData& data, int time )
55 effects->prePaintScreen( data, time );
58 void DimScreenEffect::paintScreen( int mask, QRegion region, ScreenPaintData& data )
60 effects->paintScreen( mask, region, data );
61 if( mActivated || deactivate )
63 float opacity = 0.4;
64 int height = Effect::displayHeight();
65 if( animation )
67 int elapsed = animationTime.elapsed();
68 float timeFactor = (float)((float)elapsed/(float)animationDuration);
69 if( timeFactor > 1.0 )
70 timeFactor = 1.0;
71 if( deactivate )
73 opacity = opacity - opacity * timeFactor;
75 else
77 opacity = opacity * timeFactor;
80 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
81 if( effects->compositingType() == OpenGLCompositing )
83 glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
84 glEnable( GL_BLEND );
85 glBlendFunc( GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA );
86 glPolygonMode( GL_FRONT, GL_FILL );
87 glColor4f( 0.0, 0.0, 0.0, opacity );
88 float vertices[] = { 0.0, 0.0, 0.0, height, Effect::displayWidth(), height, Effect::displayWidth(), 0.0 };
89 renderGLGeometry( 4, vertices );
90 glDisable( GL_BLEND );
91 glPopAttrib();
93 #endif
94 #ifdef KWIN_HAVE_XRENDER_COMPOSITING
95 if( effects->compositingType() == XRenderCompositing )
97 Pixmap pixmap = XCreatePixmap( display(), rootWindow(),
98 Effect::displayWidth(), Effect::displayHeight(), 32 );
99 Picture pic = XRenderCreatePicture( display(), pixmap, alphaFormat, 0, NULL );
100 XFreePixmap( display(), pixmap );
101 XRenderColor col;
102 col.alpha = int( opacity * 0xffff );
103 col.red = int( 0.0 * opacity * 0xffff );
104 col.green = int( 0.0 * opacity * 0xffff );
105 col.blue = int( 0.0 * opacity * 0xffff );
106 XRenderFillRectangle( display(), PictOpSrc, pic, &col, 0, 0,
107 Effect::displayWidth(), height );
108 XRenderComposite( display(), PictOpOver,
109 pic, None, effects->xrenderBufferPicture(),
110 0, 0, 0, 0, 0, 0, Effect::displayWidth(), height );
111 XRenderFreePicture( display(), pic );
113 #endif
114 // re-paint active window
115 EffectWindow* activeWindow = effects->activeWindow();
116 if( activeWindow )
118 WindowPaintData data( activeWindow );
119 effects->drawWindow( activeWindow, 0, activeWindow->geometry(), data );
124 void DimScreenEffect::postPaintScreen()
126 if( animation )
128 if( animationTime.elapsed() >= animationDuration )
130 animation = false;
131 deactivate = false;
133 effects->addRepaintFull();
135 effects->postPaintScreen();
138 void DimScreenEffect::paintWindow( EffectWindow *w, int mask, QRegion region, WindowPaintData &data )
140 if( mActivated && ( w == effects->activeWindow() ) )
142 return;
144 effects->paintWindow( w, mask, region, data );
147 void DimScreenEffect::windowActivated( EffectWindow *w )
149 if( !w ) return;
150 QStringList check;
151 check << "kdesu kdesu";
152 check << "kdesudo kdesudo";
153 check << "polkit-kde-manager polkit-kde-manager";
154 bool before = mActivated;
155 if( check.contains( w->windowClass() ) )
157 mActivated = true;
159 else
161 mActivated = false;
163 if( before != mActivated )
165 if( !mActivated )
167 deactivate = true;
169 animation = true;
170 animationTime.restart();
171 effects->addRepaintFull();
174 } // namespace