1
/********************************************************************
2
KWin - the KDE window manager
3
This file is part of the KDE project.
5
Copyright (C) 2007 Lubos Lunak <l.lunak@kde.org>
6
Copyright (C) 2009 Martin Gräßlin <kde@martin-graesslin.com>
7
Copyright (C) 2009, 2010 Lucas Murray <lmurray@undefinedfire.com>
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License
20
along with this program. If not, see <http://www.gnu.org/licenses/>.
21
*********************************************************************/
25
#include "kwinglutils.h"
28
#include <kconfiggroup.h>
34
KWIN_EFFECT(logout, LogoutEffect)
36
LogoutEffect::LogoutEffect()
38
, displayEffect(false)
40
, logoutWindowClosed(true)
41
, logoutWindowPassed(false)
42
, canDoPersistent(false)
46
logoutAtom = XInternAtom(display(), "_KDE_LOGGING_OUT", False);
47
effects->registerPropertyType(logoutAtom, true);
49
// Block KSMServer's effect
50
char net_wm_cm_name[ 100 ];
51
sprintf(net_wm_cm_name, "_NET_WM_CM_S%d", DefaultScreen(display()));
52
Atom net_wm_cm = XInternAtom(display(), net_wm_cm_name, False);
53
Window sel = XGetSelectionOwner(display(), net_wm_cm);
54
Atom hack = XInternAtom(display(), "_KWIN_LOGOUT_EFFECT", False);
55
XChangeProperty(display(), sel, hack, hack, 8, PropModeReplace, (unsigned char*)&hack, 1);
56
// the atom is not removed when effect is destroyed, this is temporary anyway
58
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
62
reconfigure(ReconfigureAll);
63
connect(effects, SIGNAL(windowAdded(EffectWindow*)), this, SLOT(slotWindowAdded(EffectWindow*)));
64
connect(effects, SIGNAL(windowClosed(EffectWindow*)), this, SLOT(slotWindowClosed(EffectWindow*)));
65
connect(effects, SIGNAL(windowDeleted(EffectWindow*)), this, SLOT(slotWindowDeleted(EffectWindow*)));
66
connect(effects, SIGNAL(propertyNotify(EffectWindow*,long)), this, SLOT(slotPropertyNotify(EffectWindow*,long)));
69
LogoutEffect::~LogoutEffect()
71
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
77
void LogoutEffect::reconfigure(ReconfigureFlags)
79
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
81
KConfigGroup conf = effects->effectConfig("Logout");
82
useBlur = conf.readEntry("UseBlur", true);
87
blurSupported = false;
91
void LogoutEffect::prePaintScreen(ScreenPrePaintData& data, int time)
93
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
94
if (!displayEffect && progress == 0.0) {
100
blurSupported = false;
102
} else if (!blurTexture) {
103
blurSupported = false;
104
delete blurTarget; // catch as we just tested the texture ;-P
105
if (effects->compositingType() == OpenGLCompositing && GLTexture::NPOTTextureSupported() && useBlur) {
106
// TODO: It seems that it is not possible to create a GLRenderTarget that has
107
// a different size than the display right now. Most likely a KWin core bug.
108
// Create texture and render target
109
blurTexture = new GLTexture(displayWidth(), displayHeight());
110
blurTexture->setFilter(GL_LINEAR_MIPMAP_LINEAR);
111
blurTexture->setWrapMode(GL_CLAMP_TO_EDGE);
113
blurTarget = new GLRenderTarget(blurTexture);
114
if (blurTarget->valid())
115
blurSupported = true;
117
// As creating the render target takes time it can cause the first two frames of the
118
// blur animation to be jerky. For this reason we only start the animation after the
130
progress = qMin(1.0, progress + time / animationTime(2000.0));
131
else if (progress > 0.0)
132
progress = qMax(0.0, progress - time / animationTime(500.0));
135
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
136
if (blurSupported && progress > 0.0) {
137
data.mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS;
141
effects->prePaintScreen(data, time);
144
void LogoutEffect::paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data)
146
if (progress > 0.0) {
147
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
148
if (effects->compositingType() == KWin::OpenGLCompositing) {
149
// In OpenGL mode we add vignetting and, if supported, a slight blur
151
// When using blur we render everything to an FBO and as such don't do the vignetting
152
// until after we render the FBO to the screen.
153
if (w == logoutWindow) {
154
// Window is rendered after the FBO
155
windowOpacity = data.opacity;
156
data.opacity = 0.0; // Cheat, we need the opacity for later but don't want to blur it
158
if (logoutWindowPassed || ignoredWindows.contains(w)) {
159
// Window is rendered after the FBO
161
windowsOpacities[ w ] = data.opacity;
163
} else // Window is added to the FBO
164
data.saturation *= (1.0 - progress * 0.2);
167
// If we are not blurring then we are not rendering to an FBO
168
if (w == logoutWindow)
169
// This is the logout window don't alter it but render our vignetting now
171
else if (!logoutWindowPassed && !ignoredWindows.contains(w))
172
// Window is in the background, desaturate
173
data.saturation *= (1.0 - progress * 0.2);
174
// All other windows are unaltered
178
if (effects->compositingType() == KWin::XRenderCompositing) {
179
// Since we can't do vignetting in XRender just do a stronger desaturation and darken
180
if (w != logoutWindow && !logoutWindowPassed && !ignoredWindows.contains(w)) {
181
data.saturation *= (1.0 - progress * 0.8);
182
data.brightness *= (1.0 - progress * 0.3);
185
if (w == logoutWindow ||
186
ignoredWindows.contains(w)) // HACK: All windows past the first ignored one should not be
187
// blurred as it affects the stacking order.
188
// All following windows are on top of the logout window and should not be altered either
189
logoutWindowPassed = true;
191
effects->paintWindow(w, mask, region, data);
194
void LogoutEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
196
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
197
if (blurSupported && progress > 0.0)
198
GLRenderTarget::pushRenderTarget(blurTarget);
200
effects->paintScreen(mask, region, data);
202
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
203
if (effects->compositingType() == KWin::OpenGLCompositing && progress > 0.0) {
204
if (!blurSupported) {
205
if (!logoutWindowPassed)
206
// The logout window has been deleted but we still want to fade out the vignetting, thus
207
// render it on the top of everything if still animating. We don't check if logoutWindow
208
// is set as it may still be even if it wasn't rendered.
211
GLRenderTarget* target = GLRenderTarget::popRenderTarget();
212
assert(target == blurTarget);
215
//--------------------------
216
// Render the screen effect
218
// HACK: the GL code is still OpenGL 1, so we have to unbind the shader.
220
if (ShaderManager::instance()->isShaderBound()) {
221
glGetIntegerv(GL_CURRENT_PROGRAM, &shader);
224
glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TEXTURE_BIT);
226
// Unmodified base image
229
glTexCoord2f(0.0, 0.0);
230
glVertex2f(0.0, displayHeight());
231
glTexCoord2f(1.0, 0.0);
232
glVertex2f(displayWidth(), displayHeight());
233
glTexCoord2f(1.0, 1.0);
234
glVertex2f(displayWidth(), 0.0);
235
glTexCoord2f(0.0, 1.0);
236
glVertex2f(0.0, 0.0);
241
glGetTexEnvfv(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, bias);
242
glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, 1.75);
243
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
245
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
246
glColor4f(1.0f, 1.0f, 1.0f, progress * 0.4);
248
glTexCoord2f(0.0, 0.0);
249
glVertex2f(0.0, displayHeight());
250
glTexCoord2f(1.0, 0.0);
251
glVertex2f(displayWidth(), displayHeight());
252
glTexCoord2f(1.0, 1.0);
253
glVertex2f(displayWidth(), 0.0);
254
glTexCoord2f(0.0, 1.0);
255
glVertex2f(0.0, 0.0);
257
glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, bias[0]);
258
blurTexture->unbind();
260
// Vignetting (Radial gradient with transparent middle and black edges)
264
// HACK: rebind previously bound shader
265
if (ShaderManager::instance()->isShaderBound()) {
266
glUseProgram(shader);
269
//--------------------------
271
// Render the logout window
273
int winMask = logoutWindow->hasAlpha() ? PAINT_WINDOW_TRANSLUCENT : PAINT_WINDOW_OPAQUE;
274
WindowPaintData winData(logoutWindow);
275
winData.opacity = windowOpacity;
276
effects->drawWindow(logoutWindow, winMask, region, winData);
279
// Render all windows on top of logout window
280
foreach (EffectWindow * w, windows) {
281
int winMask = w->hasAlpha() ? PAINT_WINDOW_TRANSLUCENT : PAINT_WINDOW_OPAQUE;
282
WindowPaintData winData(w);
283
winData.opacity = windowsOpacities[ w ];
284
effects->drawWindow(w, winMask, region, winData);
288
windowsOpacities.clear();
294
void LogoutEffect::postPaintScreen()
296
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
297
if ((progress != 0.0 && progress != 1.0) || frameDelay)
298
effects->addRepaintFull();
300
if (progress != 0.0 && progress != 1.0)
301
effects->addRepaintFull();
305
logoutWindowPassed = false;
306
effects->postPaintScreen();
309
void LogoutEffect::slotWindowAdded(EffectWindow* w)
311
if (isLogoutDialog(w)) {
313
logoutWindowClosed = false; // So we don't blur the window on close
315
displayEffect = true;
316
ignoredWindows.clear();
317
effects->addRepaintFull();
318
} else if (canDoPersistent)
320
ignoredWindows.append(w);
323
void LogoutEffect::slotWindowClosed(EffectWindow* w)
325
if (w == logoutWindow) {
326
logoutWindowClosed = true;
327
if (!canDoPersistent)
328
displayEffect = false; // Fade back to normal
329
effects->addRepaintFull();
333
void LogoutEffect::slotWindowDeleted(EffectWindow* w)
335
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
336
windows.removeAll(w);
338
ignoredWindows.removeAll(w);
339
if (w == logoutWindow)
343
bool LogoutEffect::isLogoutDialog(EffectWindow* w)
345
// TODO there should be probably a better way (window type?)
346
if (w->windowClass() == "ksmserver ksmserver"
347
&& (w->windowRole() == "logoutdialog" || w->windowRole() == "logouteffect")) {
353
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
354
void LogoutEffect::renderVignetting()
356
glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TEXTURE_BIT);
357
glEnable(GL_BLEND); // If not already (Such as when rendered straight to the screen)
358
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
359
for (int screen = 0; screen < effects->numScreens(); screen++) {
360
QRect screenGeom = effects->clientArea(ScreenArea, screen, 0);
361
glScissor(screenGeom.x(), displayHeight() - screenGeom.y() - screenGeom.height(),
362
screenGeom.width(), screenGeom.height()); // GL coords are flipped
363
glEnable(GL_SCISSOR_TEST); // Geom must be set before enable
364
const float cenX = screenGeom.x() + screenGeom.width() / 2;
365
const float cenY = screenGeom.y() + screenGeom.height() / 2;
366
const float a = M_PI / 16.0f; // Angle of increment
367
const float r = float((screenGeom.width() > screenGeom.height())
368
? screenGeom.width() : screenGeom.height()) * 0.8f; // Radius
369
glBegin(GL_TRIANGLE_FAN);
370
glColor4f(0.0f, 0.0f, 0.0f, 0.0f);
371
glVertex3f(cenX, cenY, 0.0f);
372
glColor4f(0.0f, 0.0f, 0.0f, progress * 0.9f);
373
for (float i = 0.0f; i <= M_PI * 2.01f; i += a)
374
glVertex3f(cenX + r * cos(i), cenY + r * sin(i), 0.0f);
376
glDisable(GL_SCISSOR_TEST);
382
void LogoutEffect::slotPropertyNotify(EffectWindow* w, long a)
384
if (w || a != logoutAtom)
385
return; // Not our atom
387
QByteArray byteData = effects->readRootProperty(logoutAtom, logoutAtom, 8);
388
if (byteData.length() < 1) {
389
// Property was deleted
390
displayEffect = false;
394
// We are using a compatible KSMServer therefore only terminate the effect when the
395
// atom is deleted, not when the dialog is closed.
396
canDoPersistent = true;