highlightwindow.cpp 5.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/********************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

Copyright (C) 2009 Lucas Murray <lmurray@undefinedfire.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/

#include "highlightwindow.h"

#include <kdebug.h>

namespace KWin
{

KWIN_EFFECT( highlightwindow, HighlightWindowEffect )

HighlightWindowEffect::HighlightWindowEffect()
31 32 33
    : m_finishing( false )
    , m_fadeDuration( double( animationTime( 150 )))
    , m_highlightedWindow( NULL )
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    , m_monitorWindow( NULL )
    {
    m_atom = XInternAtom( display(), "_KDE_WINDOW_HIGHLIGHT", False );
    effects->registerPropertyType( m_atom, true );

    // Announce support by creating a dummy version on the root window
    unsigned char dummy = 0;
    XChangeProperty( display(), rootWindow(), m_atom, m_atom, 8, PropModeReplace, &dummy, 1 );
    }

HighlightWindowEffect::~HighlightWindowEffect()
    {
    XDeleteProperty( display(), rootWindow(), m_atom );
    effects->registerPropertyType( m_atom, false );
    }

void HighlightWindowEffect::prePaintWindow( EffectWindow* w, WindowPrePaintData& data, int time )
    {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    // Calculate window opacities
    if( m_highlightedWindow )
        { // Initial fade out and changing highlight animation
        double oldOpacity = m_windowOpacity[w];
        if( m_highlightedWindow == w )
            m_windowOpacity[w] = qMin( 1.0, m_windowOpacity[w] + time / m_fadeDuration );
        else if( w->isNormalWindow() ) // Only fade out normal windows
            m_windowOpacity[w] = qMax( 0.15, m_windowOpacity[w] - time / m_fadeDuration );

        if( m_windowOpacity[w] != 1.0 )
            data.setTranslucent();
        if( oldOpacity != m_windowOpacity[w] )
            w->addRepaintFull();
        }
    else if( m_finishing && m_windowOpacity.contains( w ))
        { // Final fading back in animation
        double oldOpacity = m_windowOpacity[w];
        m_windowOpacity[w] = qMin( 1.0, m_windowOpacity[w] + time / m_fadeDuration );

        if( m_windowOpacity[w] != 1.0 )
            data.setTranslucent();
        if( oldOpacity != m_windowOpacity[w] )
            w->addRepaintFull();

        if( m_windowOpacity[w] == 1.0 )
            m_windowOpacity.remove( w ); // We default to 1.0
        }

80 81 82 83 84
    effects->prePaintWindow( w, data, time );
    }

void HighlightWindowEffect::paintWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data )
    {
85 86
    if( m_windowOpacity.contains( w ))
        data.opacity *= m_windowOpacity[w];
87 88 89 90 91
    effects->paintWindow( w, mask, region, data );
    }

void HighlightWindowEffect::windowAdded( EffectWindow* w )
    {
92 93 94 95 96 97 98
    if( m_highlightedWindow )
        { // The effect is activated thus we need to add it to the opacity hash
        if( w->isNormalWindow() ) // Only fade out normal windows
            m_windowOpacity[w] = 0.15;
        else
            m_windowOpacity[w] = 1.0;
        }
99 100 101 102 103
    propertyNotify( w, m_atom ); // Check initial value
    }

void HighlightWindowEffect::windowDeleted( EffectWindow* w )
    {
104 105 106
    m_windowOpacity.remove( w );
    if( m_monitorWindow == w ) // The monitoring window was destroyed
        finishHighlighting();
107 108 109 110 111 112 113 114 115 116 117 118 119 120
    }

void HighlightWindowEffect::propertyNotify( EffectWindow* w, long a )
    {
    if( a != m_atom )
        return; // Not our atom

    QByteArray byteData = w->readProperty( m_atom, m_atom, 32 );
    if( byteData.length() < 1 )
        return; // Invalid length
    long* data = reinterpret_cast<long*>( byteData.data() );

    if( !data[0] )
        { // Purposely clearing highlight
121
        finishHighlighting();
122 123 124 125 126
        return;
        }
    m_monitorWindow = w;
    m_highlightedWindow = effects->findWindow( data[0] );
    if( !m_highlightedWindow )
127
        {
128
        kDebug(1212) << "Invalid window targetted for highlight. Requested:" << data[0];
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        return;
        }
    prepareHighlighting();
    m_windowOpacity[w] = 1.0; // Because it's not in stackingOrder() yet
    }

void HighlightWindowEffect::prepareHighlighting()
    {
    // Create window data for every window. Just calling [w] creates it.
    m_finishing = false;
    foreach( EffectWindow *w, effects->stackingOrder() )
        if( !m_windowOpacity.contains( w )) // Just in case we are still finishing from last time
            m_windowOpacity[w] = 1.0;
    }

void HighlightWindowEffect::finishHighlighting()
    {
    m_finishing = true;
    m_monitorWindow = NULL;
    m_highlightedWindow = NULL;
149 150 151
    }

} // namespace