diff --git a/effects.cpp b/effects.cpp index 2762cd2e8..3bbb687f9 100644 --- a/effects.cpp +++ b/effects.cpp @@ -1,1863 +1,1883 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2006 Lubos Lunak 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 . *********************************************************************/ #include "effects.h" #include "deleted.h" #include "client.h" #include "group.h" #include "scene_xrender.h" #include "scene_opengl.h" #include "unmanaged.h" #include "workspace.h" #include "kwinglutils.h" #include #include "kdebug.h" #include "klibrary.h" #include "kdesktopfile.h" #include "kconfiggroup.h" #include "kstandarddirs.h" #include #include #include #include namespace KWin { //--------------------- // Static static QByteArray readWindowProperty( Window win, long atom, long type, int format ) { int len = 32768; for(;;) { unsigned char* data; Atom rtype; int rformat; unsigned long nitems, after; if( XGetWindowProperty( QX11Info::display(), win, atom, 0, len, False, AnyPropertyType, &rtype, &rformat, &nitems, &after, &data ) == Success ) { if( after > 0 ) { XFree( data ); len *= 2; continue; } if( long( rtype ) == type && rformat == format ) { int bytelen = format == 8 ? nitems : format == 16 ? nitems * sizeof( short ) : nitems * sizeof( long ); QByteArray ret( reinterpret_cast< const char* >( data ), bytelen ); XFree( data ); return ret; } else // wrong format, type or something { XFree( data ); return QByteArray(); } } else // XGetWindowProperty() failed return QByteArray(); } } static void deleteWindowProperty( Window win, long int atom ) { XDeleteProperty( QX11Info::display(), win, atom ); } //--------------------- EffectsHandlerImpl::EffectsHandlerImpl(CompositingType type) : EffectsHandler(type) , keyboard_grab_effect( NULL ) , fullscreen_effect( 0 ) , next_window_quad_type( EFFECT_QUAD_TYPE_START ) , mouse_poll_ref_count( 0 ) + , current_paint_effectframe( 0 ) { reconfigure(); } EffectsHandlerImpl::~EffectsHandlerImpl() { if( keyboard_grab_effect != NULL ) ungrabKeyboard(); foreach( const EffectPair &ep, loaded_effects ) unloadEffect( ep.first ); foreach( const InputWindowPair &pos, input_windows ) XDestroyWindow( display(), pos.second ); } void EffectsHandlerImpl::reconfigure() { KSharedConfig::Ptr _config = KGlobal::config(); KConfigGroup conf(_config, "Plugins"); KService::List offers = KServiceTypeTrader::self()->query("KWin/Effect"); QStringList effectsToBeLoaded; // First unload necessary effects foreach( const KService::Ptr &service, offers ) { KPluginInfo plugininfo( service ); plugininfo.load( conf ); bool isloaded = isEffectLoaded( plugininfo.pluginName() ); bool shouldbeloaded = plugininfo.isPluginEnabled(); if( !shouldbeloaded && isloaded ) unloadEffect( plugininfo.pluginName() ); if( shouldbeloaded ) effectsToBeLoaded.append( plugininfo.pluginName() ); } QStringList newLoaded; // Then load those that should be loaded foreach( const QString &effectName, effectsToBeLoaded ) { if( !isEffectLoaded( effectName )) { loadEffect( effectName ); newLoaded.append( effectName ); } } foreach( const EffectPair &ep, loaded_effects ) { if( !newLoaded.contains( ep.first )) // don't reconfigure newly loaded effects ep.second->reconfigure( Effect::ReconfigureAll ); } } // the idea is that effects call this function again which calls the next one void EffectsHandlerImpl::prePaintScreen( ScreenPrePaintData& data, int time ) { if( current_paint_screen < loaded_effects.size()) { loaded_effects[current_paint_screen++].second->prePaintScreen( data, time ); --current_paint_screen; } // no special final code } void EffectsHandlerImpl::paintScreen( int mask, QRegion region, ScreenPaintData& data ) { if( current_paint_screen < loaded_effects.size()) { loaded_effects[current_paint_screen++].second->paintScreen( mask, region, data ); --current_paint_screen; } else scene->finalPaintScreen( mask, region, data ); } void EffectsHandlerImpl::postPaintScreen() { if( current_paint_screen < loaded_effects.size()) { loaded_effects[current_paint_screen++].second->postPaintScreen(); --current_paint_screen; } // no special final code } void EffectsHandlerImpl::prePaintWindow( EffectWindow* w, WindowPrePaintData& data, int time ) { if( current_paint_window < loaded_effects.size()) { loaded_effects[current_paint_window++].second->prePaintWindow( w, data, time ); --current_paint_window; } // no special final code } void EffectsHandlerImpl::paintWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ) { if( current_paint_window < loaded_effects.size()) { loaded_effects[current_paint_window++].second->paintWindow( w, mask, region, data ); --current_paint_window; } else scene->finalPaintWindow( static_cast( w ), mask, region, data ); } +void EffectsHandlerImpl::paintEffectFrame( EffectFrame* frame, QRegion region, double opacity, double frameOpacity ) + { + if( current_paint_effectframe < loaded_effects.size()) + { + loaded_effects[current_paint_effectframe++].second->paintEffectFrame( frame, region, opacity, frameOpacity ); + --current_paint_effectframe; + } + else + { + const EffectFrameImpl* frameImpl = static_cast( frame ); + frameImpl->finalRender( region, opacity, frameOpacity ); + } + } + void EffectsHandlerImpl::postPaintWindow( EffectWindow* w ) { if( current_paint_window < loaded_effects.size()) { loaded_effects[current_paint_window++].second->postPaintWindow( w ); --current_paint_window; } // no special final code } bool EffectsHandlerImpl::providesResizeEffect() { for( int i = 0; i < loaded_effects.size(); ++i ) if( loaded_effects.at(i).second->isResizeEffect() ) return true; return false; } void EffectsHandlerImpl::drawWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ) { if( current_draw_window < loaded_effects.size()) { loaded_effects[current_draw_window++].second->drawWindow( w, mask, region, data ); --current_draw_window; } else scene->finalDrawWindow( static_cast( w ), mask, region, data ); } void EffectsHandlerImpl::buildQuads( EffectWindow* w, WindowQuadList& quadList ) { if( current_build_quads < loaded_effects.size()) { loaded_effects[current_build_quads++].second->buildQuads( w, quadList ); --current_build_quads; } } bool EffectsHandlerImpl::hasDecorationShadows() const { return Workspace::self()->hasDecorationShadows(); } bool EffectsHandlerImpl::decorationsHaveAlpha() const { return Workspace::self()->decorationHasAlpha(); } // start another painting pass void EffectsHandlerImpl::startPaint() { assert( current_paint_screen == 0 ); assert( current_paint_window == 0 ); assert( current_draw_window == 0 ); assert( current_build_quads == 0 ); assert( current_transform == 0 ); } void EffectsHandlerImpl::windowUserMovedResized( EffectWindow* c, bool first, bool last ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->windowUserMovedResized( c, first, last ); } void EffectsHandlerImpl::windowMoveResizeGeometryUpdate( EffectWindow* c, const QRect& geometry ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->windowMoveResizeGeometryUpdate( c, geometry ); } void EffectsHandlerImpl::windowOpacityChanged( EffectWindow* c, double old_opacity ) { if (!c) return; if( static_cast(c)->window()->opacity() == old_opacity ) return; foreach( const EffectPair &ep, loaded_effects ) ep.second->windowOpacityChanged( c, old_opacity ); } void EffectsHandlerImpl::windowAdded( EffectWindow* c ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->windowAdded( c ); } void EffectsHandlerImpl::windowDeleted( EffectWindow* c ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->windowDeleted( c ); elevated_windows.removeAll( c ); } void EffectsHandlerImpl::windowClosed( EffectWindow* c ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->windowClosed( c ); } void EffectsHandlerImpl::windowActivated( EffectWindow* c ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->windowActivated( c ); } void EffectsHandlerImpl::windowMinimized( EffectWindow* c ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->windowMinimized( c ); } void EffectsHandlerImpl::windowUnminimized( EffectWindow* c ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->windowUnminimized( c ); } void EffectsHandlerImpl::clientGroupItemSwitched( EffectWindow* from, EffectWindow* to ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->clientGroupItemSwitched( from, to ); } void EffectsHandlerImpl::clientGroupItemAdded( EffectWindow* from, EffectWindow* to ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->clientGroupItemAdded( from, to ); } void EffectsHandlerImpl::clientGroupItemRemoved( EffectWindow* c, EffectWindow* group ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->clientGroupItemRemoved( c, group ); } void EffectsHandlerImpl::desktopChanged( int old ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->desktopChanged( old ); } void EffectsHandlerImpl::windowDamaged( EffectWindow* w, const QRect& r ) { if( w == NULL ) return; foreach( const EffectPair &ep, loaded_effects ) ep.second->windowDamaged( w, r ); } void EffectsHandlerImpl::windowGeometryShapeChanged( EffectWindow* w, const QRect& old ) { if( w == NULL ) // during late cleanup effectWindow() may be already NULL return; // in some functions that may still call this foreach( const EffectPair &ep, loaded_effects ) ep.second->windowGeometryShapeChanged( w, old ); } void EffectsHandlerImpl::tabBoxAdded( int mode ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->tabBoxAdded( mode ); } void EffectsHandlerImpl::tabBoxClosed() { foreach( const EffectPair &ep, loaded_effects ) ep.second->tabBoxClosed(); } void EffectsHandlerImpl::tabBoxUpdated() { foreach( const EffectPair &ep, loaded_effects ) ep.second->tabBoxUpdated(); } void EffectsHandlerImpl::tabBoxKeyEvent( QKeyEvent* event ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->tabBoxKeyEvent( event ); } void EffectsHandlerImpl::setActiveFullScreenEffect( Effect* e ) { fullscreen_effect = e; Workspace::self()->checkUnredirect(); } Effect* EffectsHandlerImpl::activeFullScreenEffect() const { return fullscreen_effect; } bool EffectsHandlerImpl::borderActivated( ElectricBorder border ) { bool ret = false; foreach( const EffectPair &ep, loaded_effects ) if( ep.second->borderActivated( border )) ret = true; // bail out or tell all? return ret; } void EffectsHandlerImpl::mouseChanged( const QPoint& pos, const QPoint& oldpos, Qt::MouseButtons buttons, Qt::MouseButtons oldbuttons, Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->mouseChanged( pos, oldpos, buttons, oldbuttons, modifiers, oldmodifiers ); } bool EffectsHandlerImpl::grabKeyboard( Effect* effect ) { if( keyboard_grab_effect != NULL ) return false; bool ret = grabXKeyboard(); if( !ret ) return false; keyboard_grab_effect = effect; return true; } void EffectsHandlerImpl::ungrabKeyboard() { assert( keyboard_grab_effect != NULL ); ungrabXKeyboard(); keyboard_grab_effect = NULL; } void EffectsHandlerImpl::grabbedKeyboardEvent( QKeyEvent* e ) { if( keyboard_grab_effect != NULL ) keyboard_grab_effect->grabbedKeyboardEvent( e ); } void* EffectsHandlerImpl::getProxy( QString name ) { // All effects start with "kwin4_effect_", prepend it to the name name.prepend( "kwin4_effect_" ); for( QVector< EffectPair >::iterator it = loaded_effects.begin(); it != loaded_effects.end(); ++it) if ( (*it).first == name ) return (*it).second->proxy(); return NULL; } void EffectsHandlerImpl::startMousePolling() { if( !mouse_poll_ref_count ) // Start timer if required Workspace::self()->startMousePolling(); mouse_poll_ref_count++; } void EffectsHandlerImpl::stopMousePolling() { assert( mouse_poll_ref_count ); mouse_poll_ref_count--; if( !mouse_poll_ref_count ) // Stop timer if required Workspace::self()->stopMousePolling(); } bool EffectsHandlerImpl::hasKeyboardGrab() const { return keyboard_grab_effect != NULL; } void EffectsHandlerImpl::propertyNotify( EffectWindow* c, long atom ) { if( !registered_atoms.contains( atom )) return; foreach( const EffectPair &ep, loaded_effects ) ep.second->propertyNotify( c, atom ); } void EffectsHandlerImpl::numberDesktopsChanged( int old ) { foreach( const EffectPair &ep, loaded_effects ) ep.second->numberDesktopsChanged( old ); } void EffectsHandlerImpl::registerPropertyType( long atom, bool reg ) { if( reg ) ++registered_atoms[ atom ]; // initialized to 0 if not present yet else { if( --registered_atoms[ atom ] == 0 ) registered_atoms.remove( atom ); } } QByteArray EffectsHandlerImpl::readRootProperty( long atom, long type, int format ) const { return readWindowProperty( rootWindow(), atom, type, format ); } void EffectsHandlerImpl::deleteRootProperty( long atom ) const { deleteWindowProperty( rootWindow(), atom ); } void EffectsHandlerImpl::activateWindow( EffectWindow* c ) { if( Client* cl = dynamic_cast< Client* >( static_cast(c)->window())) Workspace::self()->activateClient( cl, true ); } EffectWindow* EffectsHandlerImpl::activeWindow() const { return Workspace::self()->activeClient() ? Workspace::self()->activeClient()->effectWindow() : NULL; } void EffectsHandlerImpl::moveWindow( EffectWindow* w, const QPoint& pos, bool snap, double snapAdjust ) { Client* cl = dynamic_cast< Client* >( static_cast(w)->window()); if (!cl || !cl->isMovable()) return; if (snap) cl->move(Workspace::self()->adjustClientPosition(cl, pos, true, snapAdjust)); else cl->move( pos ); } void EffectsHandlerImpl::windowToDesktop( EffectWindow* w, int desktop ) { Client* cl = dynamic_cast< Client* >( static_cast(w)->window()); if( cl && !cl->isDesktop() && !cl->isDock() && !cl->isTopMenu()) Workspace::self()->sendClientToDesktop( cl, desktop, true ); } void EffectsHandlerImpl::windowToScreen( EffectWindow* w, int screen ) { Client* cl = dynamic_cast< Client* >( static_cast(w)->window()); if( cl && !cl->isDesktop() && !cl->isDock() && !cl->isTopMenu()) Workspace::self()->sendClientToScreen( cl, screen ); } void EffectsHandlerImpl::setShowingDesktop( bool showing ) { Workspace::self()->setShowingDesktop( showing ); } int EffectsHandlerImpl::currentDesktop() const { return Workspace::self()->currentDesktop(); } int EffectsHandlerImpl::numberOfDesktops() const { return Workspace::self()->numberOfDesktops(); } void EffectsHandlerImpl::setCurrentDesktop( int desktop ) { Workspace::self()->setCurrentDesktop( desktop ); } void EffectsHandlerImpl::setNumberOfDesktops( int desktops ) { Workspace::self()->setNumberOfDesktops( desktops ); } QSize EffectsHandlerImpl::desktopGridSize() const { return Workspace::self()->desktopGridSize(); } int EffectsHandlerImpl::desktopGridWidth() const { return Workspace::self()->desktopGridWidth(); } int EffectsHandlerImpl::desktopGridHeight() const { return Workspace::self()->desktopGridHeight(); } int EffectsHandlerImpl::workspaceWidth() const { return Workspace::self()->workspaceWidth(); } int EffectsHandlerImpl::workspaceHeight() const { return Workspace::self()->workspaceHeight(); } int EffectsHandlerImpl::desktopAtCoords( QPoint coords ) const { return Workspace::self()->desktopAtCoords( coords ); } QPoint EffectsHandlerImpl::desktopGridCoords( int id ) const { return Workspace::self()->desktopGridCoords( id ); } QPoint EffectsHandlerImpl::desktopCoords( int id ) const { return Workspace::self()->desktopCoords( id ); } int EffectsHandlerImpl::desktopAbove( int desktop, bool wrap ) const { return Workspace::self()->desktopAbove( desktop, wrap ); } int EffectsHandlerImpl::desktopToRight( int desktop, bool wrap ) const { return Workspace::self()->desktopToRight( desktop, wrap ); } int EffectsHandlerImpl::desktopBelow( int desktop, bool wrap ) const { return Workspace::self()->desktopBelow( desktop, wrap ); } int EffectsHandlerImpl::desktopToLeft( int desktop, bool wrap ) const { return Workspace::self()->desktopToLeft( desktop, wrap ); } bool EffectsHandlerImpl::isDesktopLayoutDynamic() const { return Workspace::self()->isDesktopLayoutDynamic(); } int EffectsHandlerImpl::addDesktop( QPoint coords ) { return Workspace::self()->addDesktop( coords ); } void EffectsHandlerImpl::deleteDesktop( int id ) { Workspace::self()->deleteDesktop( id ); } QString EffectsHandlerImpl::desktopName( int desktop ) const { return Workspace::self()->desktopName( desktop ); } bool EffectsHandlerImpl::optionRollOverDesktops() const { return options->rollOverDesktops; } double EffectsHandlerImpl::animationTimeFactor() const { return options->animationTimeFactor(); } WindowQuadType EffectsHandlerImpl::newWindowQuadType() { return WindowQuadType( next_window_quad_type++ ); } int EffectsHandlerImpl::displayWidth() const { return KWin::displayWidth(); } int EffectsHandlerImpl::displayHeight() const { return KWin::displayWidth(); } EffectWindow* EffectsHandlerImpl::findWindow( WId id ) const { if( Client* w = Workspace::self()->findClient( WindowMatchPredicate( id ))) return w->effectWindow(); if( Unmanaged* w = Workspace::self()->findUnmanaged( WindowMatchPredicate( id ))) return w->effectWindow(); return NULL; } EffectWindowList EffectsHandlerImpl::stackingOrder() const { ClientList list = Workspace::self()->stackingOrder(); EffectWindowList ret; foreach( Client* c, list ) ret.append( effectWindow( c )); return ret; } void EffectsHandlerImpl::setElevatedWindow( EffectWindow* w, bool set ) { elevated_windows.removeAll( w ); if( set ) elevated_windows.append( w ); } void EffectsHandlerImpl::setTabBoxWindow(EffectWindow* w) { if( Client* c = dynamic_cast< Client* >( static_cast< EffectWindowImpl* >( w )->window())) Workspace::self()->setTabBoxClient( c ); } void EffectsHandlerImpl::setTabBoxDesktop(int desktop) { Workspace::self()->setTabBoxDesktop( desktop ); } EffectWindowList EffectsHandlerImpl::currentTabBoxWindowList() const { EffectWindowList ret; ClientList clients = Workspace::self()->currentTabBoxClientList(); foreach( Client* c, clients ) ret.append( c->effectWindow()); return ret; } void EffectsHandlerImpl::refTabBox() { Workspace::self()->refTabBox(); } void EffectsHandlerImpl::unrefTabBox() { Workspace::self()->unrefTabBox(); } void EffectsHandlerImpl::closeTabBox() { Workspace::self()->closeTabBox(); } QList< int > EffectsHandlerImpl::currentTabBoxDesktopList() const { return Workspace::self()->currentTabBoxDesktopList(); } int EffectsHandlerImpl::currentTabBoxDesktop() const { return Workspace::self()->currentTabBoxDesktop(); } EffectWindow* EffectsHandlerImpl::currentTabBoxWindow() const { if( Client* c = Workspace::self()->currentTabBoxClient()) return c->effectWindow(); return NULL; } void EffectsHandlerImpl::pushRenderTarget(GLRenderTarget* target) { #ifdef KWIN_HAVE_OPENGL_COMPOSITING target->enable(); render_targets.push(target); #endif } GLRenderTarget* EffectsHandlerImpl::popRenderTarget() { #ifdef KWIN_HAVE_OPENGL_COMPOSITING GLRenderTarget* ret = render_targets.pop(); ret->disable(); if( !render_targets.isEmpty() ) render_targets.top()->enable(); return ret; #else return 0; #endif } void EffectsHandlerImpl::addRepaintFull() { Workspace::self()->addRepaintFull(); } void EffectsHandlerImpl::addRepaint( const QRect& r ) { Workspace::self()->addRepaint( r ); } void EffectsHandlerImpl::addRepaint( const QRegion& r ) { Workspace::self()->addRepaint( r ); } void EffectsHandlerImpl::addRepaint( int x, int y, int w, int h ) { Workspace::self()->addRepaint( x, y, w, h ); } int EffectsHandlerImpl::activeScreen() const { return Workspace::self()->activeScreen(); } int EffectsHandlerImpl::numScreens() const { return Workspace::self()->numScreens(); } int EffectsHandlerImpl::screenNumber( const QPoint& pos ) const { return Workspace::self()->screenNumber( pos ); } QRect EffectsHandlerImpl::clientArea( clientAreaOption opt, int screen, int desktop ) const { return Workspace::self()->clientArea( opt, screen, desktop ); } QRect EffectsHandlerImpl::clientArea( clientAreaOption opt, const EffectWindow* c ) const { const Toplevel* t = static_cast< const EffectWindowImpl* >(c)->window(); if( const Client* cl = dynamic_cast< const Client* >( t )) return Workspace::self()->clientArea( opt, cl ); else return Workspace::self()->clientArea( opt, t->geometry().center(), Workspace::self()->currentDesktop()); } QRect EffectsHandlerImpl::clientArea( clientAreaOption opt, const QPoint& p, int desktop ) const { return Workspace::self()->clientArea( opt, p, desktop ); } Window EffectsHandlerImpl::createInputWindow( Effect* e, int x, int y, int w, int h, const QCursor& cursor ) { XSetWindowAttributes attrs; attrs.override_redirect = True; Window win = XCreateWindow( display(), rootWindow(), x, y, w, h, 0, 0, InputOnly, CopyFromParent, CWOverrideRedirect, &attrs ); // TODO keeping on top? // TODO enter/leave notify? XSelectInput( display(), win, ButtonPressMask | ButtonReleaseMask | PointerMotionMask ); XDefineCursor( display(), win, cursor.handle()); XMapWindow( display(), win ); input_windows.append( qMakePair( e, win )); // Raise electric border windows above the input windows // so they can still be triggered. Workspace::self()->raiseElectricBorderWindows(); return win; } void EffectsHandlerImpl::destroyInputWindow( Window w ) { foreach( const InputWindowPair &pos, input_windows ) { if( pos.second == w ) { input_windows.removeAll( pos ); XDestroyWindow( display(), w ); return; } } abort(); } bool EffectsHandlerImpl::checkInputWindowEvent( XEvent* e ) { if( e->type != ButtonPress && e->type != ButtonRelease && e->type != MotionNotify ) return false; foreach( const InputWindowPair &pos, input_windows ) { if( pos.second == e->xany.window ) { switch( e->type ) { case ButtonPress: { XButtonEvent* e2 = &e->xbutton; Qt::MouseButton button = x11ToQtMouseButton( e2->button ); Qt::MouseButtons buttons = x11ToQtMouseButtons( e2->state ) | button; QMouseEvent ev( QEvent::MouseButtonPress, QPoint( e2->x, e2->y ), QPoint( e2->x_root, e2->y_root ), button, buttons, x11ToQtKeyboardModifiers( e2->state )); pos.first->windowInputMouseEvent( pos.second, &ev ); break; // ---> } case ButtonRelease: { XButtonEvent* e2 = &e->xbutton; Qt::MouseButton button = x11ToQtMouseButton( e2->button ); Qt::MouseButtons buttons = x11ToQtMouseButtons( e2->state ) & ~button; QMouseEvent ev( QEvent::MouseButtonRelease, QPoint( e2->x, e2->y ), QPoint( e2->x_root, e2->y_root ), button, buttons, x11ToQtKeyboardModifiers( e2->state )); pos.first->windowInputMouseEvent( pos.second, &ev ); break; // ---> } case MotionNotify: { XMotionEvent* e2 = &e->xmotion; QMouseEvent ev( QEvent::MouseMove, QPoint( e2->x, e2->y ), QPoint( e2->x_root, e2->y_root ), Qt::NoButton, x11ToQtMouseButtons( e2->state ), x11ToQtKeyboardModifiers( e2->state )); pos.first->windowInputMouseEvent( pos.second, &ev ); break; // ---> } } return true; // eat event } } return false; } void EffectsHandlerImpl::checkInputWindowStacking() { if( input_windows.count() == 0 ) return; Window* wins = new Window[ input_windows.count() ]; int pos = 0; foreach( const InputWindowPair &it, input_windows ) wins[ pos++ ] = it.second; XRaiseWindow( display(), wins[ 0 ] ); XRestackWindows( display(), wins, pos ); delete[] wins; // Raise electric border windows above the input windows // so they can still be triggered. TODO: Do both at once. Workspace::self()->raiseElectricBorderWindows(); } QPoint EffectsHandlerImpl::cursorPos() const { return Workspace::self()->cursorPos(); } void EffectsHandlerImpl::checkElectricBorder(const QPoint &pos, Time time) { Workspace::self()->checkElectricBorder( pos, time ); } void EffectsHandlerImpl::reserveElectricBorder( ElectricBorder border ) { Workspace::self()->reserveElectricBorder( border ); } void EffectsHandlerImpl::unreserveElectricBorder( ElectricBorder border ) { Workspace::self()->unreserveElectricBorder( border ); } void EffectsHandlerImpl::reserveElectricBorderSwitching( bool reserve ) { Workspace::self()->reserveElectricBorderSwitching( reserve ); } unsigned long EffectsHandlerImpl::xrenderBufferPicture() { #ifdef KWIN_HAVE_XRENDER_COMPOSITING if( SceneXrender* s = dynamic_cast< SceneXrender* >( scene )) return s->bufferPicture(); #endif return None; } KLibrary* EffectsHandlerImpl::findEffectLibrary( KService* service ) { QString libname = service->library(); KLibrary* library = new KLibrary(libname); if( !library ) { kError( 1212 ) << "couldn't open library for effect '" << service->name() << "'" << endl; return 0; } return library; } void EffectsHandlerImpl::toggleEffect( const QString& name ) { if( isEffectLoaded( name )) unloadEffect( name ); else loadEffect( name ); } QStringList EffectsHandlerImpl::loadedEffects() const { QStringList listModules; for(QVector< EffectPair >::const_iterator it = loaded_effects.constBegin(); it != loaded_effects.constEnd(); ++it) { listModules <<(*it).first; } return listModules; } QStringList EffectsHandlerImpl::listOfEffects() const { KService::List offers = KServiceTypeTrader::self()->query("KWin/Effect"); QStringList listOfModules; // First unload necessary effects foreach( const KService::Ptr &service, offers ) { KPluginInfo plugininfo( service ); listOfModules<addRepaintFull(); assert( current_paint_screen == 0 ); assert( current_paint_window == 0 ); assert( current_draw_window == 0 ); assert( current_build_quads == 0 ); assert( current_transform == 0 ); if( !name.startsWith("kwin4_effect_") ) kWarning( 1212 ) << "Effect names usually have kwin4_effect_ prefix" ; // Make sure a single effect won't be loaded multiple times for(QVector< EffectPair >::const_iterator it = loaded_effects.constBegin(); it != loaded_effects.constEnd(); ++it) { if( (*it).first == name ) { kDebug( 1212 ) << "EffectsHandler::loadEffect : Effect already loaded : " << name; return true; } } kDebug( 1212 ) << "Trying to load " << name; QString internalname = name.toLower(); QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(internalname); KService::List offers = KServiceTypeTrader::self()->query("KWin/Effect", constraint); if(offers.isEmpty()) { kError( 1212 ) << "Couldn't find effect " << name << endl; return false; } KService::Ptr service = offers.first(); KLibrary* library = findEffectLibrary( service.data() ); if( !library ) { return false; } QString version_symbol = "effect_version_" + name; KLibrary::void_function_ptr version_func = library->resolveFunction(version_symbol.toAscii()); if( version_func == NULL ) { kWarning( 1212 ) << "Effect " << name << " does not provide required API version, ignoring."; return false; } typedef int (*t_versionfunc)(); int version = reinterpret_cast< t_versionfunc >( version_func )(); // call it // Version must be the same or less, but major must be the same. // With major 0 minor must match exactly. if( version > KWIN_EFFECT_API_VERSION || ( version >> 8 ) != KWIN_EFFECT_API_VERSION_MAJOR || ( KWIN_EFFECT_API_VERSION_MAJOR == 0 && version != KWIN_EFFECT_API_VERSION )) { kWarning( 1212 ) << "Effect " << name << " requires unsupported API version " << version; return false; } QString supported_symbol = "effect_supported_" + name; KLibrary::void_function_ptr supported_func = library->resolveFunction(supported_symbol.toAscii().data()); QString create_symbol = "effect_create_" + name; KLibrary::void_function_ptr create_func = library->resolveFunction(create_symbol.toAscii().data()); if( supported_func ) { typedef bool (*t_supportedfunc)(); t_supportedfunc supported = reinterpret_cast(supported_func); if(!supported()) { kWarning( 1212 ) << "EffectsHandler::loadEffect : Effect " << name << " is not supported" ; library->unload(); return false; } } if(!create_func) { kError( 1212 ) << "EffectsHandler::loadEffect : effect_create function not found" << endl; library->unload(); return false; } typedef Effect* (*t_createfunc)(); t_createfunc create = reinterpret_cast(create_func); // Make sure all depenedencies have been loaded // TODO: detect circular deps KPluginInfo plugininfo( service ); QStringList dependencies = plugininfo.dependencies(); foreach( const QString &depName, dependencies ) { if( !loadEffect(depName)) { kError(1212) << "EffectsHandler::loadEffect : Couldn't load dependencies for effect " << name << endl; library->unload(); return false; } } Effect* e = create(); effect_order.insert( service->property( "X-KDE-Ordering" ).toInt(), EffectPair( name, e )); effectsChanged(); effect_libraries[ name ] = library; return true; } void EffectsHandlerImpl::unloadEffect( const QString& name ) { Workspace::self()->addRepaintFull(); assert( current_paint_screen == 0 ); assert( current_paint_window == 0 ); assert( current_draw_window == 0 ); assert( current_build_quads == 0 ); assert( current_transform == 0 ); for( QMap< int, EffectPair >::iterator it = effect_order.begin(); it != effect_order.end(); ++it) { if ( it.value().first == name ) { kDebug( 1212 ) << "EffectsHandler::unloadEffect : Unloading Effect : " << name; if( activeFullScreenEffect() == it.value().second ) { setActiveFullScreenEffect( 0 ); } delete it.value().second; effect_order.erase(it); effectsChanged(); effect_libraries[ name ]->unload(); return; } } kDebug( 1212 ) << "EffectsHandler::unloadEffect : Effect not loaded : " << name; } void EffectsHandlerImpl::reconfigureEffect( const QString& name ) { for( QVector< EffectPair >::iterator it = loaded_effects.begin(); it != loaded_effects.end(); ++it) if ( (*it).first == name ) { (*it).second->reconfigure( Effect::ReconfigureAll ); return; } } bool EffectsHandlerImpl::isEffectLoaded( const QString& name ) { for( QVector< EffectPair >::iterator it = loaded_effects.begin(); it != loaded_effects.end(); ++it) if ( (*it).first == name ) return true; return false; } void EffectsHandlerImpl::effectsChanged() { loaded_effects.clear(); // kDebug(1212) << "Recreating effects' list:"; foreach( const EffectPair &effect, effect_order ) { // kDebug(1212) << effect.first; loaded_effects.append( effect ); } } EffectFrame* EffectsHandlerImpl::effectFrame( EffectFrameStyle style, bool staticSize, const QPoint& position, Qt::Alignment alignment ) const { return new EffectFrameImpl( style, staticSize, position, alignment ); } //**************************************** // EffectWindowImpl //**************************************** EffectWindowImpl::EffectWindowImpl() : EffectWindow() , toplevel( NULL ) , sw( NULL ) { } EffectWindowImpl::~EffectWindowImpl() { } bool EffectWindowImpl::isPaintingEnabled() { return sceneWindow()->isPaintingEnabled(); } void EffectWindowImpl::enablePainting( int reason ) { sceneWindow()->enablePainting( reason ); } void EffectWindowImpl::disablePainting( int reason ) { sceneWindow()->disablePainting( reason ); } void EffectWindowImpl::addRepaint( const QRect& r ) { toplevel->addRepaint( r ); } void EffectWindowImpl::addRepaint( int x, int y, int w, int h ) { toplevel->addRepaint( x, y, w, h ); } void EffectWindowImpl::addRepaintFull() { toplevel->addRepaintFull(); } int EffectWindowImpl::desktop() const { return toplevel->desktop(); } bool EffectWindowImpl::isOnAllDesktops() const { return desktop() == NET::OnAllDesktops; } QString EffectWindowImpl::caption() const { if( Client* c = dynamic_cast( toplevel )) return c->caption(); else return ""; } QString EffectWindowImpl::windowClass() const { return toplevel->resourceName() + ' ' + toplevel->resourceClass(); } QString EffectWindowImpl::windowRole() const { return toplevel->windowRole(); } QPixmap EffectWindowImpl::icon() const { if( Client* c = dynamic_cast( toplevel )) return c->icon(); return QPixmap(); // TODO } const EffectWindowGroup* EffectWindowImpl::group() const { if( Client* c = dynamic_cast< Client* >( toplevel )) return c->group()->effectGroup(); return NULL; // TODO } bool EffectWindowImpl::isMinimized() const { if( Client* c = dynamic_cast( toplevel )) return c->isMinimized(); else return false; } double EffectWindowImpl::opacity() const { return toplevel->opacity(); } bool EffectWindowImpl::hasAlpha() const { return toplevel->hasAlpha(); } bool EffectWindowImpl::isDeleted() const { return (dynamic_cast( toplevel ) != 0); } void EffectWindowImpl::refWindow() { if( Deleted* d = dynamic_cast< Deleted* >( toplevel )) return d->refWindow(); abort(); // TODO } void EffectWindowImpl::unrefWindow() { if( Deleted* d = dynamic_cast< Deleted* >( toplevel )) return d->unrefWindow( true ); // delayed abort(); // TODO } void EffectWindowImpl::setWindow( Toplevel* w ) { toplevel = w; } void EffectWindowImpl::setSceneWindow( Scene::Window* w ) { sw = w; } int EffectWindowImpl::x() const { return toplevel->x(); } int EffectWindowImpl::y() const { return toplevel->y(); } int EffectWindowImpl::width() const { return toplevel->width(); } int EffectWindowImpl::height() const { return toplevel->height(); } QRect EffectWindowImpl::geometry() const { return toplevel->geometry(); } QRegion EffectWindowImpl::shape() const { return sw ? sw->shape() : geometry(); } int EffectWindowImpl::screen() const { return toplevel->screen(); } bool EffectWindowImpl::hasOwnShape() const { return toplevel->shape(); } QSize EffectWindowImpl::size() const { return toplevel->size(); } QPoint EffectWindowImpl::pos() const { return toplevel->pos(); } QRect EffectWindowImpl::rect() const { return toplevel->rect(); } QRect EffectWindowImpl::contentsRect() const { return QRect( toplevel->clientPos(), toplevel->clientSize()); } QRect EffectWindowImpl::decorationInnerRect() const { Client *client = dynamic_cast(toplevel); return client ? client->transparentRect() : contentsRect(); } QByteArray EffectWindowImpl::readProperty( long atom, long type, int format ) const { return readWindowProperty( window()->window(), atom, type, format ); } void EffectWindowImpl::deleteProperty(long int atom) const { deleteWindowProperty( window()->window(), atom ); } bool EffectWindowImpl::isMovable() const { if( Client* c = dynamic_cast< Client* >( toplevel )) return c->isMovable(); return false; } bool EffectWindowImpl::isMovableAcrossScreens() const { if( Client* c = dynamic_cast< Client* >( toplevel )) return c->isMovableAcrossScreens(); return false; } bool EffectWindowImpl::isUserMove() const { if( Client* c = dynamic_cast< Client* >( toplevel )) return c->isMove(); return false; } bool EffectWindowImpl::isUserResize() const { if( Client* c = dynamic_cast< Client* >( toplevel )) return c->isResize(); return false; } QRect EffectWindowImpl::iconGeometry() const { if( Client* c = dynamic_cast< Client* >( toplevel )) return c->iconGeometry(); return QRect(); } bool EffectWindowImpl::isDesktop() const { return toplevel->isDesktop(); } bool EffectWindowImpl::isDock() const { return toplevel->isDock(); } bool EffectWindowImpl::isToolbar() const { return toplevel->isToolbar(); } bool EffectWindowImpl::isTopMenu() const { return toplevel->isTopMenu(); } bool EffectWindowImpl::isMenu() const { return toplevel->isMenu(); } bool EffectWindowImpl::isNormalWindow() const { return toplevel->isNormalWindow(); } bool EffectWindowImpl::isSpecialWindow() const { if( Client* c = dynamic_cast( toplevel )) return c->isSpecialWindow(); else return true; } bool EffectWindowImpl::isDialog() const { return toplevel->isDialog(); } bool EffectWindowImpl::isSplash() const { return toplevel->isSplash(); } bool EffectWindowImpl::isUtility() const { return toplevel->isUtility(); } bool EffectWindowImpl::isDropdownMenu() const { return toplevel->isDropdownMenu(); } bool EffectWindowImpl::isPopupMenu() const { return toplevel->isPopupMenu(); } bool EffectWindowImpl::isTooltip() const { return toplevel->isTooltip(); } bool EffectWindowImpl::isNotification() const { return toplevel->isNotification(); } bool EffectWindowImpl::isComboBox() const { return toplevel->isComboBox(); } bool EffectWindowImpl::isDNDIcon() const { return toplevel->isDNDIcon(); } bool EffectWindowImpl::isManaged() const { return dynamic_cast< const Client* >( toplevel ) != NULL; } bool EffectWindowImpl::acceptsFocus() const { const Client* client = dynamic_cast< const Client* >( toplevel ); if( !client ) return true; // We don't actually know... return client->wantsInput(); } bool EffectWindowImpl::keepAbove() const { const Client* client = dynamic_cast< const Client* >( toplevel ); if( !client ) return true; return client->keepAbove(); } bool EffectWindowImpl::isModal() const { if( Client* c = dynamic_cast< Client* >( toplevel )) return c->isModal(); return false; } EffectWindow* EffectWindowImpl::findModal() { if( Client* c = dynamic_cast< Client* >( toplevel )) { if( Client* c2 = c->findModal()) return c2->effectWindow(); } return NULL; } EffectWindowList EffectWindowImpl::mainWindows() const { if( Client* c = dynamic_cast< Client* >( toplevel )) { EffectWindowList ret; ClientList mainclients = c->mainClients(); foreach( Client* tmp, mainclients ) ret.append( tmp->effectWindow()); return ret; } return EffectWindowList(); } bool EffectWindowImpl::isSkipSwitcher() const { if( Client* c = dynamic_cast< Client* >( toplevel )) return c->skipSwitcher(); return false; } WindowQuadList EffectWindowImpl::buildQuads( bool force ) const { return sceneWindow()->buildQuads( force ); } void EffectWindowImpl::minimize() const { if( Client* c = dynamic_cast< Client* >( toplevel )) { c->minimize(); } } void EffectWindowImpl::unminimize() const { if( Client* c = dynamic_cast< Client* >( toplevel )) { c->unminimize(); } } void EffectWindowImpl::closeWindow() const { if( Client* c = dynamic_cast< Client* >( toplevel )) { c->closeWindow(); } } bool EffectWindowImpl::visibleInClientGroup() const { if( Client* c = dynamic_cast< Client* >( toplevel )) { if( !c->clientGroup() ) return true; return c == c->clientGroup()->visible(); } return false; } void EffectWindowImpl::setData( int role, const QVariant &data ) { if ( !data.isNull() ) dataMap[ role ] = data; else dataMap.remove( role ); } QVariant EffectWindowImpl::data( int role ) const { if( !dataMap.contains( role ) ) return QVariant(); return dataMap[ role ]; } EffectWindow* effectWindow( Toplevel* w ) { EffectWindowImpl* ret = w->effectWindow(); return ret; } EffectWindow* effectWindow( Scene::Window* w ) { EffectWindowImpl* ret = w->window()->effectWindow(); ret->setSceneWindow( w ); return ret; } //**************************************** // EffectWindowGroupImpl //**************************************** EffectWindowList EffectWindowGroupImpl::members() const { EffectWindowList ret; foreach( Toplevel* c, group->members()) ret.append( c->effectWindow()); return ret; } //**************************************** // EffectFrameImpl //**************************************** EffectFrameImpl::EffectFrameImpl( EffectFrameStyle style, bool staticSize, QPoint position, Qt::Alignment alignment ) : QObject ( 0 ) , EffectFrame() , m_style( style ) , m_static( staticSize ) , m_point( position ) , m_alignment( alignment ) + , m_shader( NULL ) { if( m_style == Styled ) { m_frame.setImagePath( "widgets/background" ); m_frame.setCacheAllRenderedFrames( true ); connect( Plasma::Theme::defaultTheme(), SIGNAL( themeChanged() ), this, SLOT( plasmaThemeChanged() )); } if( effects->compositingType() == OpenGLCompositing ) { m_sceneFrame = new SceneOpenGL::EffectFrame( this ); } else if( effects->compositingType() == XRenderCompositing ) { m_sceneFrame = new SceneXrender::EffectFrame( this ); } else { // that should not happen and will definitely crash! m_sceneFrame = NULL; } } EffectFrameImpl::~EffectFrameImpl() { delete m_sceneFrame; } const QFont& EffectFrameImpl::font() const { return m_font; } void EffectFrameImpl::setFont( const QFont& font ) { if (m_font == font) { return; } m_font = font; QRect oldGeom = m_geometry; if( !m_text.isEmpty() ) { autoResize(); } if( oldGeom == m_geometry ) { // Wasn't updated in autoResize() m_sceneFrame->freeTextFrame(); } } void EffectFrameImpl::free() { m_sceneFrame->free(); } const QRect& EffectFrameImpl::geometry() const { return m_geometry; } void EffectFrameImpl::setGeometry( const QRect& geometry, bool force ) { QRect oldGeom = m_geometry; m_geometry = geometry; if( m_geometry == oldGeom && !force ) { return; } effects->addRepaint( oldGeom ); effects->addRepaint( m_geometry ); if( m_geometry.size() == oldGeom.size() && !force ) { return; } if( m_style == Styled ) { qreal left, top, right, bottom; m_frame.getMargins( left, top, right, bottom ); // m_geometry is the inner geometry m_frame.resizeFrame( m_geometry.adjusted( -left, -top, right, bottom ).size() ); } free(); } const QPixmap& EffectFrameImpl::icon() const { return m_icon; } void EffectFrameImpl::setIcon( const QPixmap& icon ) { m_icon = icon; if( m_iconSize.isEmpty() ) // Set a size if we don't already have one { setIconSize( m_icon.size() ); } m_sceneFrame->freeIconFrame(); } const QSize& EffectFrameImpl::iconSize() const { return m_iconSize; } void EffectFrameImpl::setIconSize( const QSize& size ) { if( m_iconSize == size ) { return; } m_iconSize = size; autoResize(); m_sceneFrame->freeIconFrame(); } void EffectFrameImpl::plasmaThemeChanged() { free(); } void EffectFrameImpl::render( QRegion region, double opacity, double frameOpacity ) { if( m_geometry.isEmpty() ) { return; // Nothing to display } + m_shader = NULL; + effects->paintEffectFrame( this, region, opacity, frameOpacity ); + } +void EffectFrameImpl::finalRender( QRegion region, double opacity, double frameOpacity ) const + { region = infiniteRegion(); // TODO: Old region doesn't seem to work with OpenGL - // TODO: pass through all effects m_sceneFrame->render( region, opacity, frameOpacity ); } Qt::Alignment EffectFrameImpl::alignment() const { return m_alignment; } void EffectFrameImpl::setAlignment( Qt::Alignment alignment ) { m_alignment = alignment; } void EffectFrameImpl::setPosition( const QPoint& point ) { if( m_point == point ) { return; } m_point = point; autoResize(); free(); } const QString& EffectFrameImpl::text() const { return m_text; } void EffectFrameImpl::setText( const QString& text ) { if( m_text == text ) { return; } m_text = text; QRect oldGeom = m_geometry; autoResize(); if( oldGeom == m_geometry ) { // Wasn't updated in autoResize() m_sceneFrame->freeTextFrame(); } } void EffectFrameImpl::autoResize() { if( m_static ) return; // Not automatically resizing QRect geometry; // Set size if( !m_text.isEmpty() ) { QFontMetrics metrics( m_font ); geometry.setSize( metrics.size( 0, m_text )); } if( !m_icon.isNull() && !m_iconSize.isEmpty() ) { geometry.setLeft( -m_iconSize.width() ); if( m_iconSize.height() > geometry.height() ) geometry.setHeight( m_iconSize.height() ); } // Set position if( m_alignment & Qt::AlignLeft ) geometry.moveLeft( m_point.x() ); else if( m_alignment & Qt::AlignRight ) geometry.moveLeft( m_point.x() - geometry.width() ); else geometry.moveLeft( m_point.x() - geometry.width() / 2 ); if( m_alignment & Qt::AlignTop ) geometry.moveTop( m_point.y() ); else if( m_alignment & Qt::AlignBottom ) geometry.moveTop( m_point.y() - geometry.height() ); else geometry.moveTop( m_point.y() - geometry.height() / 2 ); setGeometry( geometry ); } } // namespace diff --git a/effects.h b/effects.h index c5c413d5c..de21c0738 100644 --- a/effects.h +++ b/effects.h @@ -1,418 +1,430 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2006 Lubos Lunak 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 . *********************************************************************/ #ifndef KWIN_EFFECTSIMPL_H #define KWIN_EFFECTSIMPL_H #include "kwineffects.h" #include "scene.h" #include #include class KService; namespace KWin { class EffectsHandlerImpl : public EffectsHandler { public: EffectsHandlerImpl(CompositingType type); virtual ~EffectsHandlerImpl(); virtual void prePaintScreen( ScreenPrePaintData& data, int time ); virtual void paintScreen( int mask, QRegion region, ScreenPaintData& data ); virtual void postPaintScreen(); virtual void prePaintWindow( EffectWindow* w, WindowPrePaintData& data, int time ); virtual void paintWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ); virtual void postPaintWindow( EffectWindow* w ); + virtual void paintEffectFrame( EffectFrame* frame, QRegion region, double opacity, double frameOpacity ); bool providesResizeEffect(); virtual void drawWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ); virtual void buildQuads( EffectWindow* w, WindowQuadList& quadList ); virtual void activateWindow( EffectWindow* c ); virtual EffectWindow* activeWindow() const; virtual void moveWindow( EffectWindow* w, const QPoint& pos, bool snap = false, double snapAdjust = 1.0 ); virtual void windowToDesktop( EffectWindow* w, int desktop ); virtual void windowToScreen( EffectWindow* w, int screen ); virtual void setShowingDesktop( bool showing ); virtual int currentDesktop() const; virtual int numberOfDesktops() const; virtual void setCurrentDesktop( int desktop ); virtual void setNumberOfDesktops( int desktops ); virtual QSize desktopGridSize() const; virtual int desktopGridWidth() const; virtual int desktopGridHeight() const; virtual int workspaceWidth() const; virtual int workspaceHeight() const; virtual int desktopAtCoords( QPoint coords ) const; virtual QPoint desktopGridCoords( int id ) const; virtual QPoint desktopCoords( int id ) const; virtual int desktopAbove( int desktop = 0, bool wrap = true ) const; virtual int desktopToRight( int desktop = 0, bool wrap = true ) const; virtual int desktopBelow( int desktop = 0, bool wrap = true ) const; virtual int desktopToLeft( int desktop = 0, bool wrap = true ) const; virtual bool isDesktopLayoutDynamic() const; virtual int addDesktop( QPoint coords ); virtual void deleteDesktop( int id ); virtual QString desktopName( int desktop ) const; virtual bool optionRollOverDesktops() const; virtual int displayWidth() const; virtual int displayHeight() const; virtual QPoint cursorPos() const; virtual bool grabKeyboard( Effect* effect ); virtual void ungrabKeyboard(); virtual void* getProxy( QString name ); virtual void startMousePolling(); virtual void stopMousePolling(); virtual EffectWindow* findWindow( WId id ) const; virtual EffectWindowList stackingOrder() const; virtual void setElevatedWindow( EffectWindow* w, bool set ); virtual void setTabBoxWindow(EffectWindow*); virtual void setTabBoxDesktop(int); virtual EffectWindowList currentTabBoxWindowList() const; virtual void refTabBox(); virtual void unrefTabBox(); virtual void closeTabBox(); virtual QList< int > currentTabBoxDesktopList() const; virtual int currentTabBoxDesktop() const; virtual EffectWindow* currentTabBoxWindow() const; virtual void setActiveFullScreenEffect( Effect* e ); virtual Effect* activeFullScreenEffect() const; virtual void pushRenderTarget(GLRenderTarget* target); virtual GLRenderTarget* popRenderTarget(); virtual void addRepaintFull(); virtual void addRepaint( const QRect& r ); virtual void addRepaint( const QRegion& r ); virtual void addRepaint( int x, int y, int w, int h ); virtual int activeScreen() const; virtual int numScreens() const; virtual int screenNumber( const QPoint& pos ) const; virtual QRect clientArea( clientAreaOption, int screen, int desktop ) const; virtual QRect clientArea( clientAreaOption, const EffectWindow* c ) const; virtual QRect clientArea( clientAreaOption, const QPoint& p, int desktop ) const; virtual double animationTimeFactor() const; virtual WindowQuadType newWindowQuadType(); virtual Window createInputWindow( Effect* e, int x, int y, int w, int h, const QCursor& cursor ); using EffectsHandler::createInputWindow; virtual void destroyInputWindow( Window w ); virtual bool checkInputWindowEvent( XEvent* e ); virtual void checkInputWindowStacking(); virtual void checkElectricBorder(const QPoint &pos, Time time); virtual void reserveElectricBorder( ElectricBorder border ); virtual void unreserveElectricBorder( ElectricBorder border ); virtual void reserveElectricBorderSwitching( bool reserve ); virtual unsigned long xrenderBufferPicture(); virtual void reconfigure(); virtual void registerPropertyType( long atom, bool reg ); virtual QByteArray readRootProperty( long atom, long type, int format ) const; virtual void deleteRootProperty( long atom ) const; virtual bool hasDecorationShadows() const; virtual bool decorationsHaveAlpha() const; virtual EffectFrame* effectFrame( EffectFrameStyle style, bool staticSize, const QPoint& position, Qt::Alignment alignment ) const; // internal (used by kwin core or compositing code) void startPaint(); void windowUserMovedResized( EffectWindow* c, bool first, bool last ); void windowMoveResizeGeometryUpdate( EffectWindow* c, const QRect& geometry ); void windowOpacityChanged( EffectWindow* c, double old_opacity ); void windowAdded( EffectWindow* c ); void windowClosed( EffectWindow* c ); void windowDeleted( EffectWindow* c ); void windowActivated( EffectWindow* c ); void windowMinimized( EffectWindow* c ); void windowUnminimized( EffectWindow* c ); void clientGroupItemSwitched( EffectWindow* from, EffectWindow* to ); void clientGroupItemAdded( EffectWindow* from, EffectWindow* to ); void clientGroupItemRemoved( EffectWindow* c, EffectWindow* group ); void desktopChanged( int old ); void windowDamaged( EffectWindow* w, const QRect& r ); void windowGeometryShapeChanged( EffectWindow* w, const QRect& old ); void tabBoxAdded( int mode ); void tabBoxClosed(); void tabBoxUpdated(); void tabBoxKeyEvent( QKeyEvent* event ); bool borderActivated( ElectricBorder border ); void mouseChanged( const QPoint& pos, const QPoint& oldpos, Qt::MouseButtons buttons, Qt::MouseButtons oldbuttons, Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers ); void grabbedKeyboardEvent( QKeyEvent* e ); bool hasKeyboardGrab() const; void propertyNotify( EffectWindow* c, long atom ); void numberDesktopsChanged( int old ); bool loadEffect( const QString& name ); void toggleEffect( const QString& name ); void unloadEffect( const QString& name ); void reconfigureEffect( const QString& name ); bool isEffectLoaded( const QString& name ); QStringList loadedEffects() const; QStringList listOfEffects() const; QList elevatedWindows() const; protected: KLibrary* findEffectLibrary( KService* service ); void effectsChanged(); Effect* keyboard_grab_effect; QStack render_targets; Effect* fullscreen_effect; QList elevated_windows; QMultiMap< int, EffectPair > effect_order; QHash< long, int > registered_atoms; int next_window_quad_type; int mouse_poll_ref_count; + int current_paint_effectframe; }; class EffectWindowImpl : public EffectWindow { public: EffectWindowImpl(); virtual ~EffectWindowImpl(); virtual void enablePainting( int reason ); virtual void disablePainting( int reason ); virtual bool isPaintingEnabled(); virtual void addRepaint( const QRect& r ); virtual void addRepaint( int x, int y, int w, int h ); virtual void addRepaintFull(); virtual void refWindow(); virtual void unrefWindow(); virtual bool isDeleted() const; virtual bool isOnAllDesktops() const; virtual int desktop() const; // prefer isOnXXX() virtual bool isMinimized() const; virtual double opacity() const; virtual bool hasAlpha() const; virtual QString caption() const; virtual QPixmap icon() const; virtual QString windowClass() const; virtual QString windowRole() const; virtual const EffectWindowGroup* group() const; virtual int x() const; virtual int y() const; virtual int width() const; virtual int height() const; virtual QRect geometry() const; virtual QRegion shape() const; virtual int screen() const; virtual bool hasOwnShape() const; virtual QPoint pos() const; virtual QSize size() const; virtual QRect rect() const; virtual bool isMovable() const; virtual bool isMovableAcrossScreens() const; virtual bool isUserMove() const; virtual bool isUserResize() const; virtual QRect iconGeometry() const; virtual QRect contentsRect() const; virtual QRect decorationInnerRect() const; virtual QByteArray readProperty( long atom, long type, int format ) const; virtual void deleteProperty( long atom ) const; virtual bool isDesktop() const; virtual bool isDock() const; virtual bool isToolbar() const; virtual bool isTopMenu() const; virtual bool isMenu() const; virtual bool isNormalWindow() const; // normal as in 'NET::Normal or NET::Unknown non-transient' virtual bool isSpecialWindow() const; virtual bool isDialog() const; virtual bool isSplash() const; virtual bool isUtility() const; virtual bool isDropdownMenu() const; virtual bool isPopupMenu() const; // a context popup, not dropdown, not torn-off virtual bool isTooltip() const; virtual bool isNotification() const; virtual bool isComboBox() const; virtual bool isDNDIcon() const; virtual bool isManaged() const; // managed or override-redirect virtual bool acceptsFocus() const; virtual bool keepAbove() const; virtual bool isModal() const; virtual EffectWindow* findModal(); virtual EffectWindowList mainWindows() const; virtual bool isSkipSwitcher() const; virtual WindowQuadList buildQuads( bool force = false ) const; virtual void minimize() const; virtual void unminimize() const; virtual void closeWindow() const; virtual bool visibleInClientGroup() const; const Toplevel* window() const; Toplevel* window(); void setWindow( Toplevel* w ); // internal void setSceneWindow( Scene::Window* w ); // internal const Scene::Window* sceneWindow() const; // internal Scene::Window* sceneWindow(); // internal void setData( int role, const QVariant &data ); QVariant data( int role ) const; private: Toplevel* toplevel; Scene::Window* sw; // This one is used only during paint pass. QHash dataMap; }; class EffectWindowGroupImpl : public EffectWindowGroup { public: EffectWindowGroupImpl( Group* g ); virtual EffectWindowList members() const; private: Group* group; }; class EffectFrameImpl : public QObject, public EffectFrame { Q_OBJECT public: explicit EffectFrameImpl( EffectFrameStyle style, bool staticSize = true, QPoint position = QPoint( -1, -1 ), Qt::Alignment alignment = Qt::AlignCenter ); virtual ~EffectFrameImpl(); virtual void free(); virtual void render( QRegion region = infiniteRegion(), double opacity = 1.0, double frameOpacity = 1.0 ); virtual Qt::Alignment alignment() const; virtual void setAlignment( Qt::Alignment alignment ); virtual const QFont& font() const; virtual void setFont( const QFont& font ); virtual const QRect& geometry() const; virtual void setGeometry( const QRect& geometry, bool force = false ); virtual const QPixmap& icon() const; virtual void setIcon( const QPixmap& icon ); virtual const QSize& iconSize() const; virtual void setIconSize( const QSize& size ); virtual void setPosition( const QPoint& point ); virtual const QString& text() const; virtual void setText( const QString& text ); EffectFrameStyle style() const { return m_style; }; Plasma::FrameSvg& frame() { return m_frame; } bool isStatic() const { return m_static; }; + void finalRender( QRegion region, double opacity, double frameOpacity ) const; + virtual void setShader( GLShader* shader ) + { + m_shader = shader; + } + virtual GLShader* shader() const + { + return m_shader; + } private Q_SLOTS: void plasmaThemeChanged(); private: Q_DISABLE_COPY( EffectFrameImpl ) // As we need to use Qt slots we cannot copy this class void autoResize(); // Auto-resize if not a static size EffectFrameStyle m_style; Plasma::FrameSvg m_frame; // TODO: share between all EffectFrames // Position bool m_static; QPoint m_point; Qt::Alignment m_alignment; QRect m_geometry; // Contents QString m_text; QFont m_font; QPixmap m_icon; QSize m_iconSize; Scene::EffectFrame* m_sceneFrame; + GLShader* m_shader; }; inline QList EffectsHandlerImpl::elevatedWindows() const { return elevated_windows; } inline EffectWindowGroupImpl::EffectWindowGroupImpl( Group* g ) : group( g ) { } EffectWindow* effectWindow( Toplevel* w ); EffectWindow* effectWindow( Scene::Window* w ); inline const Scene::Window* EffectWindowImpl::sceneWindow() const { return sw; } inline Scene::Window* EffectWindowImpl::sceneWindow() { return sw; } inline const Toplevel* EffectWindowImpl::window() const { return toplevel; } inline Toplevel* EffectWindowImpl::window() { return toplevel; } } // namespace #endif diff --git a/effects/invert/invert.cpp b/effects/invert/invert.cpp index 7a8815b8a..fd076e385 100644 --- a/effects/invert/invert.cpp +++ b/effects/invert/invert.cpp @@ -1,143 +1,156 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2007 Rivo Laks Copyright (C) 2008 Lucas Murray 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 . *********************************************************************/ #include "invert.h" #include #include #include #include #include #include #include namespace KWin { KWIN_EFFECT( invert, InvertEffect ) KWIN_EFFECT_SUPPORTED( invert, ShaderEffect::supported() ) InvertEffect::InvertEffect() : m_inited( false ), m_valid( true ), m_shader( NULL ), m_allWindows( false ) { KActionCollection* actionCollection = new KActionCollection( this ); KAction* a = (KAction*)actionCollection->addAction( "Invert" ); a->setText( i18n( "Toggle Invert Effect" )); a->setGlobalShortcut( KShortcut( Qt::CTRL + Qt::META + Qt::Key_I )); connect(a, SIGNAL( triggered(bool) ), this, SLOT( toggle() )); KAction* b = (KAction*)actionCollection->addAction( "InvertWindow" ); b->setText( i18n( "Toggle Invert Effect on Window" )); b->setGlobalShortcut( KShortcut( Qt::CTRL + Qt::META + Qt::Key_U )); connect(b, SIGNAL( triggered(bool) ), this, SLOT( toggleWindow() )); } InvertEffect::~InvertEffect() { delete m_shader; } bool InvertEffect::loadData() { m_inited = true; QString fragmentshader = KGlobal::dirs()->findResource("data", "kwin/invert.frag"); QString vertexshader = KGlobal::dirs()->findResource("data", "kwin/invert.vert"); if(fragmentshader.isEmpty() || vertexshader.isEmpty()) { kError(1212) << "Couldn't locate shader files" << endl; return false; } m_shader = new GLShader(vertexshader, fragmentshader); if( !m_shader->isValid() ) { kError(1212) << "The shader failed to load!" << endl; return false; } else { m_shader->bind(); m_shader->setUniform("winTexture", 0); m_shader->unbind(); } return true; } void InvertEffect::drawWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ) { // Load if we haven't already if( m_valid && !m_inited ) m_valid = loadData(); bool useShader = m_valid && ( m_allWindows != m_windows.contains( w )); if( useShader ) { m_shader->bind(); int texw = w->width(); int texh = w->height(); if( !GLTexture::NPOTTextureSupported() ) { kWarning( 1212 ) << "NPOT textures not supported, wasting some memory" ; texw = nearestPowerOfTwo(texw); texh = nearestPowerOfTwo(texh); } m_shader->setTextureWidth( (float)texw ); m_shader->setTextureHeight( (float)texh ); data.shader = m_shader; } effects->drawWindow( w, mask, region, data ); if( useShader ) m_shader->unbind(); } +void InvertEffect::paintEffectFrame( KWin::EffectFrame* frame, QRegion region, double opacity, double frameOpacity ) + { + if( m_valid && m_allWindows ) + { + frame->setShader( m_shader ); + effects->paintEffectFrame( frame, region, opacity, frameOpacity ); + } + else + { + effects->paintEffectFrame( frame, region, opacity, frameOpacity ); + } + } + void InvertEffect::windowClosed( EffectWindow* w ) { m_windows.removeOne( w ); } void InvertEffect::toggle() { m_allWindows = !m_allWindows; effects->addRepaintFull(); } void InvertEffect::toggleWindow() { if( !m_windows.contains( effects->activeWindow() )) m_windows.append( effects->activeWindow() ); else m_windows.removeOne( effects->activeWindow() ); effects->activeWindow()->addRepaintFull(); } } // namespace #include "invert.moc" diff --git a/effects/invert/invert.h b/effects/invert/invert.h index 2ce9b4302..461487f3e 100644 --- a/effects/invert/invert.h +++ b/effects/invert/invert.h @@ -1,63 +1,64 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2007 Rivo Laks Copyright (C) 2008 Lucas Murray 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 . *********************************************************************/ #ifndef KWIN_INVERT_H #define KWIN_INVERT_H #include namespace KWin { class GLShader; /** * Inverts desktop's colors **/ class InvertEffect : public QObject, public Effect { Q_OBJECT public: InvertEffect(); ~InvertEffect(); virtual void drawWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ); + virtual void paintEffectFrame( KWin::EffectFrame* frame, QRegion region, double opacity, double frameOpacity ); virtual void windowClosed( EffectWindow* w ); public slots: void toggle(); void toggleWindow(); protected: bool loadData(); private: bool m_inited; bool m_valid; GLShader* m_shader; bool m_allWindows; QList m_windows; }; } // namespace #endif diff --git a/lib/kwineffects.cpp b/lib/kwineffects.cpp index 7a287d820..f97c251bb 100644 --- a/lib/kwineffects.cpp +++ b/lib/kwineffects.cpp @@ -1,1305 +1,1310 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2006 Lubos Lunak Copyright (C) 2009 Lucas Murray 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 . *********************************************************************/ #include "kwineffects.h" #include "kwinglutils.h" #include "kwinxrenderutils.h" #include #include #include #include #include #include #include #include #include #include #include #include #ifdef KWIN_HAVE_XRENDER_COMPOSITING #include #include #endif namespace KWin { void WindowPrePaintData::setTranslucent() { mask |= Effect::PAINT_WINDOW_TRANSLUCENT; mask &= ~Effect::PAINT_WINDOW_OPAQUE; clip = QRegion(); // cannot clip, will be transparent } void WindowPrePaintData::setTransformed() { mask |= Effect::PAINT_WINDOW_TRANSFORMED; } WindowPaintData::WindowPaintData( EffectWindow* w ) : opacity( w->opacity()) , contents_opacity( 1.0 ) , decoration_opacity( 1.0 ) , xScale( 1 ) , yScale( 1 ) , zScale( 1 ) , xTranslate( 0 ) , yTranslate( 0 ) , zTranslate( 0 ) , saturation( 1 ) , brightness( 1 ) , shader( NULL ) , rotation( NULL ) { quads = w->buildQuads(); } ScreenPaintData::ScreenPaintData() : xScale( 1 ) , yScale( 1 ) , zScale( 1 ) , xTranslate( 0 ) , yTranslate( 0 ) , zTranslate( 0 ) , rotation( NULL ) { } RotationData::RotationData() : axis( ZAxis ) , angle( 0.0 ) , xRotationPoint( 0.0 ) , yRotationPoint( 0.0 ) , zRotationPoint( 0.0 ) { } //**************************************** // Effect //**************************************** Effect::Effect() { } Effect::~Effect() { } void Effect::reconfigure( ReconfigureFlags ) { } void* Effect::proxy() { return NULL; } void Effect::windowUserMovedResized( EffectWindow* , bool, bool ) { } void Effect::windowMoveResizeGeometryUpdate( EffectWindow* , const QRect& ) { } void Effect::windowOpacityChanged( EffectWindow*, double ) { } void Effect::windowAdded( EffectWindow* ) { } void Effect::windowClosed( EffectWindow* ) { } void Effect::windowDeleted( EffectWindow* ) { } void Effect::windowActivated( EffectWindow* ) { } void Effect::windowMinimized( EffectWindow* ) { } void Effect::windowUnminimized( EffectWindow* ) { } void Effect::clientGroupItemSwitched( EffectWindow*, EffectWindow* ) { } void Effect::clientGroupItemAdded( EffectWindow*, EffectWindow* ) { } void Effect::clientGroupItemRemoved( EffectWindow*, EffectWindow* ) { } void Effect::windowInputMouseEvent( Window, QEvent* ) { } void Effect::grabbedKeyboardEvent( QKeyEvent* ) { } void Effect::propertyNotify( EffectWindow* , long ) { } void Effect::desktopChanged( int ) { } void Effect::windowDamaged( EffectWindow*, const QRect& ) { } void Effect::windowGeometryShapeChanged( EffectWindow*, const QRect& ) { } void Effect::tabBoxAdded( int ) { } void Effect::tabBoxClosed() { } void Effect::tabBoxUpdated() { } void Effect::tabBoxKeyEvent( QKeyEvent* ) { } bool Effect::borderActivated( ElectricBorder ) { return false; } void Effect::mouseChanged( const QPoint&, const QPoint&, Qt::MouseButtons, Qt::MouseButtons, Qt::KeyboardModifiers, Qt::KeyboardModifiers ) { } void Effect::prePaintScreen( ScreenPrePaintData& data, int time ) { effects->prePaintScreen( data, time ); } void Effect::paintScreen( int mask, QRegion region, ScreenPaintData& data ) { effects->paintScreen( mask, region, data ); } void Effect::postPaintScreen() { effects->postPaintScreen(); } void Effect::prePaintWindow( EffectWindow* w, WindowPrePaintData& data, int time ) { effects->prePaintWindow( w, data, time ); } void Effect::paintWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ) { effects->paintWindow( w, mask, region, data ); } void Effect::postPaintWindow( EffectWindow* w ) { effects->postPaintWindow( w ); } +void Effect::paintEffectFrame( KWin::EffectFrame* frame, QRegion region, double opacity, double frameOpacity ) + { + effects->paintEffectFrame( frame, region, opacity, frameOpacity ); + } + void Effect::drawWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ) { effects->drawWindow( w, mask, region, data ); } void Effect::buildQuads( EffectWindow* w, WindowQuadList& quadList ) { effects->buildQuads( w, quadList ); } QRect Effect::transformWindowDamage( EffectWindow* w, const QRect& r ) { return effects->transformWindowDamage( w, r ); } void Effect::setPositionTransformations( WindowPaintData& data, QRect& region, EffectWindow* w, const QRect& r, Qt::AspectRatioMode aspect ) { QSize size = w->size(); size.scale( r.size(), aspect ); data.xScale = size.width() / double( w->width()); data.yScale = size.height() / double( w->height()); int width = int( w->width() * data.xScale ); int height = int( w->height() * data.yScale ); int x = r.x() + ( r.width() - width ) / 2; int y = r.y() + ( r.height() - height ) / 2; region = QRect( x, y, width, height ); data.xTranslate = x - w->x(); data.yTranslate = y - w->y(); } int Effect::displayWidth() { return KWin::displayWidth(); } int Effect::displayHeight() { return KWin::displayHeight(); } QPoint Effect::cursorPos() { return effects->cursorPos(); } double Effect::animationTime( const KConfigGroup& cfg, const QString& key, int defaultTime ) { int time = cfg.readEntry( key, 0 ); return time != 0 ? time : qMax( defaultTime * effects->animationTimeFactor(), 1. ); } double Effect::animationTime( int defaultTime ) { // at least 1ms, otherwise 0ms times can break some things return qMax( defaultTime * effects->animationTimeFactor(), 1. ); } void Effect::numberDesktopsChanged( int ) { } //**************************************** // EffectsHandler //**************************************** EffectsHandler::EffectsHandler(CompositingType type) : current_paint_screen( 0 ) , current_paint_window( 0 ) , current_draw_window( 0 ) , current_build_quads( 0 ) , current_transform( 0 ) , compositing_type( type ) { if( compositing_type == NoCompositing ) return; KWin::effects = this; } EffectsHandler::~EffectsHandler() { // All effects should already be unloaded by Impl dtor assert( loaded_effects.count() == 0 ); } QRect EffectsHandler::transformWindowDamage( EffectWindow* w, const QRect& r ) { if( current_transform < loaded_effects.size()) { QRect rr = loaded_effects[current_transform++].second->transformWindowDamage( w, r ); --current_transform; return rr; } else return r; } Window EffectsHandler::createInputWindow( Effect* e, const QRect& r, const QCursor& cursor ) { return createInputWindow( e, r.x(), r.y(), r.width(), r.height(), cursor ); } Window EffectsHandler::createFullScreenInputWindow( Effect* e, const QCursor& cursor ) { return createInputWindow( e, 0, 0, displayWidth(), displayHeight(), cursor ); } CompositingType EffectsHandler::compositingType() const { return compositing_type; } bool EffectsHandler::saturationSupported() const { switch( compositing_type ) { #ifdef KWIN_HAVE_OPENGL_COMPOSITING case OpenGLCompositing: return GLTexture::saturationSupported(); #endif case XRenderCompositing: return false; // never default: abort(); } } void EffectsHandler::sendReloadMessage( const QString& effectname ) { QDBusMessage message = QDBusMessage::createMethodCall("org.kde.kwin", "/KWin", "org.kde.KWin", "reconfigureEffect"); message << QString("kwin4_effect_" + effectname); QDBusConnection::sessionBus().send(message); } KConfigGroup EffectsHandler::effectConfig( const QString& effectname ) { KSharedConfig::Ptr kwinconfig = KSharedConfig::openConfig( "kwinrc", KConfig::NoGlobals ); return kwinconfig->group( "Effect-" + effectname ); } bool EffectsHandler::checkDriverBlacklist( const KConfigGroup& blacklist ) { #ifdef KWIN_HAVE_OPENGL_COMPOSITING if( effects->compositingType() == OpenGLCompositing ) { QString vendor = QString((const char*)glGetString( GL_VENDOR )); QString renderer = QString((const char*)glGetString( GL_RENDERER )); QString version = QString((const char*)glGetString( GL_VERSION )); foreach( const QString& key, blacklist.keyList() ) { // the key is a word in the renderer string or vendor referrencing the vendor in case of mesa // e.g. "Intel" or "Ati" if( renderer.contains( key, Qt::CaseInsensitive ) || vendor.contains( key, Qt::CaseInsensitive ) ) { // the value for current key contains a string list of driver versions which have to be blacklisted QStringList versions = blacklist.readEntry< QStringList >( key, QStringList() ); foreach( const QString& entry, versions ) { QStringList parts = entry.split( ":-:" ); if( parts.size() != 2 ) { continue; } if( renderer.contains(parts[0], Qt::CaseInsensitive) && version.contains(parts[1], Qt::CaseInsensitive) ) { // the version matches the renderer string - this driver is blacklisted, return return true; } } } } } return false; #else return false; #endif } EffectsHandler* effects = 0; //**************************************** // EffectWindow //**************************************** EffectWindow::EffectWindow() { } EffectWindow::~EffectWindow() { } bool EffectWindow::isOnCurrentDesktop() const { return isOnDesktop( effects->currentDesktop()); } bool EffectWindow::isOnDesktop( int d ) const { return desktop() == d || isOnAllDesktops(); } bool EffectWindow::hasDecoration() const { return contentsRect() != QRect( 0, 0, width(), height()); } //**************************************** // EffectWindowGroup //**************************************** EffectWindowGroup::~EffectWindowGroup() { } //**************************************** // GlobalShortcutsEditor //**************************************** GlobalShortcutsEditor::GlobalShortcutsEditor( QWidget *parent ) : KShortcutsEditor( parent, GlobalAction ) { } /*************************************************************** WindowQuad ***************************************************************/ WindowQuad WindowQuad::makeSubQuad( double x1, double y1, double x2, double y2 ) const { assert( x1 < x2 && y1 < y2 && x1 >= left() && x2 <= right() && y1 >= top() && y2 <= bottom()); #ifndef NDEBUG if( isTransformed()) kFatal( 1212 ) << "Splitting quads is allowed only in pre-paint calls!" ; #endif WindowQuad ret( *this ); // vertices are clockwise starting from topleft ret.verts[ 0 ].px = x1; ret.verts[ 3 ].px = x1; ret.verts[ 1 ].px = x2; ret.verts[ 2 ].px = x2; ret.verts[ 0 ].py = y1; ret.verts[ 1 ].py = y1; ret.verts[ 2 ].py = y2; ret.verts[ 3 ].py = y2; // original x/y are supposed to be the same, no transforming is done here ret.verts[ 0 ].ox = x1; ret.verts[ 3 ].ox = x1; ret.verts[ 1 ].ox = x2; ret.verts[ 2 ].ox = x2; ret.verts[ 0 ].oy = y1; ret.verts[ 1 ].oy = y1; ret.verts[ 2 ].oy = y2; ret.verts[ 3 ].oy = y2; double my_tleft = verts[ 0 ].tx; double my_tright = verts[ 2 ].tx; double my_ttop = verts[ 0 ].ty; double my_tbottom = verts[ 2 ].ty; double tleft = ( x1 - left()) / ( right() - left()) * ( my_tright - my_tleft ) + my_tleft; double tright = ( x2 - left()) / ( right() - left()) * ( my_tright - my_tleft ) + my_tleft; double ttop = ( y1 - top()) / ( bottom() - top()) * ( my_tbottom - my_ttop ) + my_ttop; double tbottom = ( y2 - top()) / ( bottom() - top()) * ( my_tbottom - my_ttop ) + my_ttop; ret.verts[ 0 ].tx = tleft; ret.verts[ 3 ].tx = tleft; ret.verts[ 1 ].tx = tright; ret.verts[ 2 ].tx = tright; ret.verts[ 0 ].ty = ttop; ret.verts[ 1 ].ty = ttop; ret.verts[ 2 ].ty = tbottom; ret.verts[ 3 ].ty = tbottom; return ret; } bool WindowQuad::smoothNeeded() const { // smoothing is needed if the width or height of the quad does not match the original size double width = verts[ 1 ].ox - verts[ 0 ].ox; double height = verts[ 2 ].oy - verts[ 1 ].oy; return( verts[ 1 ].px - verts[ 0 ].px != width || verts[ 2 ].px - verts[ 3 ].px != width || verts[ 2 ].py - verts[ 1 ].py != height || verts[ 3 ].py - verts[ 0 ].py != height ); } /*************************************************************** WindowQuadList ***************************************************************/ WindowQuadList WindowQuadList::splitAtX( double x ) const { WindowQuadList ret; foreach( const WindowQuad &quad, *this ) { #ifndef NDEBUG if( quad.isTransformed()) kFatal( 1212 ) << "Splitting quads is allowed only in pre-paint calls!" ; #endif bool wholeleft = true; bool wholeright = true; for( int i = 0; i < 4; ++i ) { if( quad[ i ].x() < x ) wholeright = false; if( quad[ i ].x() > x ) wholeleft = false; } if( wholeleft || wholeright ) // is whole in one split part { ret.append( quad ); continue; } if( quad.left() == quad.right() ) // quad has no size { ret.append( quad ); continue; } ret.append( quad.makeSubQuad( quad.left(), quad.top(), x, quad.bottom())); ret.append( quad.makeSubQuad( x, quad.top(), quad.right(), quad.bottom())); } return ret; } WindowQuadList WindowQuadList::splitAtY( double y ) const { WindowQuadList ret; foreach( const WindowQuad &quad, *this ) { #ifndef NDEBUG if( quad.isTransformed()) kFatal( 1212 ) << "Splitting quads is allowed only in pre-paint calls!" ; #endif bool wholetop = true; bool wholebottom = true; for( int i = 0; i < 4; ++i ) { if( quad[ i ].y() < y ) wholebottom = false; if( quad[ i ].y() > y ) wholetop = false; } if( wholetop || wholebottom ) // is whole in one split part { ret.append( quad ); continue; } if( quad.top() == quad.bottom() ) // quad has no size { ret.append( quad ); continue; } ret.append( quad.makeSubQuad( quad.left(), quad.top(), quad.right(), y )); ret.append( quad.makeSubQuad( quad.left(), y, quad.right(), quad.bottom())); } return ret; } WindowQuadList WindowQuadList::makeGrid( int maxquadsize ) const { if( empty()) return *this; // find the bounding rectangle double left = first().left(); double right = first().right(); double top = first().top(); double bottom = first().bottom(); foreach( const WindowQuad &quad, *this ) { #ifndef NDEBUG if( quad.isTransformed()) kFatal( 1212 ) << "Splitting quads is allowed only in pre-paint calls!" ; #endif left = qMin( left, quad.left()); right = qMax( right, quad.right()); top = qMin( top, quad.top()); bottom = qMax( bottom, quad.bottom()); } WindowQuadList ret; for( double x = left; x < right; x += maxquadsize ) { for( double y = top; y < bottom; y += maxquadsize ) { foreach( const WindowQuad &quad, *this ) { if( QRectF( QPointF( quad.left(), quad.top()), QPointF( quad.right(), quad.bottom())) .intersects( QRectF( x, y, maxquadsize, maxquadsize ))) { ret.append( quad.makeSubQuad( qMax( x, quad.left()), qMax( y, quad.top()), qMin( quad.right(), x + maxquadsize ), qMin( quad.bottom(), y + maxquadsize ))); } } } } return ret; } WindowQuadList WindowQuadList::makeRegularGrid( int xSubdivisions, int ySubdivisions ) const { if( empty()) return *this; // find the bounding rectangle double left = first().left(); double right = first().right(); double top = first().top(); double bottom = first().bottom(); foreach( const WindowQuad &quad, *this ) { #ifndef NDEBUG if( quad.isTransformed()) kFatal( 1212 ) << "Splitting quads is allowed only in pre-paint calls!" ; #endif left = qMin( left, quad.left()); right = qMax( right, quad.right()); top = qMin( top, quad.top()); bottom = qMax( bottom, quad.bottom()); } double xincrement = (right - left) / xSubdivisions; double yincrement = (bottom - top) / ySubdivisions; WindowQuadList ret; for( double y = top; y < bottom; y += yincrement ) { for( double x = left; x < right; x += xincrement) { foreach( const WindowQuad &quad, *this ) { if( QRectF( QPointF( quad.left(), quad.top()), QPointF( quad.right(), quad.bottom())) .intersects( QRectF( x, y, xincrement, yincrement ))) { ret.append( quad.makeSubQuad( qMax( x, quad.left()), qMax( y, quad.top()), qMin( quad.right(), x + xincrement ), qMin( quad.bottom(), y + yincrement ))); } } } } return ret; } void WindowQuadList::makeArrays( float** vertices, float** texcoords ) const { *vertices = new float[ count() * 4 * 2 ]; *texcoords = new float[ count() * 4 * 2 ]; float* vpos = *vertices; float* tpos = *texcoords; for( int i = 0; i < count(); ++i ) for( int j = 0; j < 4; ++j ) { *vpos++ = at( i )[ j ].x(); *vpos++ = at( i )[ j ].y(); *tpos++ = at( i )[ j ].tx; *tpos++ = at( i )[ j ].ty; } } WindowQuadList WindowQuadList::select( WindowQuadType type ) const { foreach( const WindowQuad &q, *this ) { if( q.type() != type ) // something else than ones to select, make a copy and filter { WindowQuadList ret; foreach( const WindowQuad &q, *this ) { if( q.type() == type ) ret.append( q ); } return ret; } } return *this; // nothing to filter out } WindowQuadList WindowQuadList::filterOut( WindowQuadType type ) const { foreach( const WindowQuad &q, *this ) { if( q.type() == type ) // something to filter out, make a copy and filter { WindowQuadList ret; foreach( const WindowQuad &q, *this ) { if( q.type() != type ) ret.append( q ); } return ret; } } return *this; // nothing to filter out } bool WindowQuadList::smoothNeeded() const { foreach( const WindowQuad &q, *this ) if( q.smoothNeeded()) return true; return false; } bool WindowQuadList::isTransformed() const { foreach( const WindowQuad &q, *this ) if( q.isTransformed()) return true; return false; } /*************************************************************** PaintClipper ***************************************************************/ QStack< QRegion >* PaintClipper::areas = NULL; PaintClipper::PaintClipper( const QRegion& allowed_area ) : area( allowed_area ) { push( area ); } PaintClipper::~PaintClipper() { pop( area ); } void PaintClipper::push( const QRegion& allowed_area ) { if( allowed_area == infiniteRegion()) // don't push these return; if( areas == NULL ) areas = new QStack< QRegion >; areas->push( allowed_area ); } void PaintClipper::pop( const QRegion& allowed_area ) { if( allowed_area == infiniteRegion()) return; Q_ASSERT( areas != NULL ); Q_ASSERT( areas->top() == allowed_area ); areas->pop(); if( areas->isEmpty()) { delete areas; areas = NULL; } } bool PaintClipper::clip() { return areas != NULL; } QRegion PaintClipper::paintArea() { assert( areas != NULL ); // can be called only with clip() == true QRegion ret = QRegion( 0, 0, displayWidth(), displayHeight()); foreach( const QRegion &r, *areas ) ret &= r; return ret; } struct PaintClipper::Iterator::Data { Data() : index( 0 ) {} int index; #ifdef KWIN_HAVE_OPENGL_COMPOSITING QVector< QRect > rects; #endif }; PaintClipper::Iterator::Iterator() : data( new Data ) { #ifdef KWIN_HAVE_OPENGL_COMPOSITING if( clip() && effects->compositingType() == OpenGLCompositing ) { glPushAttrib( GL_SCISSOR_BIT ); glEnable( GL_SCISSOR_TEST ); data->rects = paintArea().rects(); data->index = -1; next(); // move to the first one } #endif #ifdef KWIN_HAVE_XRENDER_COMPOSITING if( clip() && effects->compositingType() == XRenderCompositing ) { XserverRegion region = toXserverRegion( paintArea()); XFixesSetPictureClipRegion( display(), effects->xrenderBufferPicture(), 0, 0, region ); XFixesDestroyRegion( display(), region ); // it's ref-counted } #endif } PaintClipper::Iterator::~Iterator() { #ifdef KWIN_HAVE_OPENGL_COMPOSITING if( clip() && effects->compositingType() == OpenGLCompositing ) glPopAttrib(); #endif #ifdef KWIN_HAVE_XRENDER_COMPOSITING if( clip() && effects->compositingType() == XRenderCompositing ) XFixesSetPictureClipRegion( display(), effects->xrenderBufferPicture(), 0, 0, None ); #endif delete data; } bool PaintClipper::Iterator::isDone() { if( !clip()) return data->index == 1; // run once #ifdef KWIN_HAVE_OPENGL_COMPOSITING if( effects->compositingType() == OpenGLCompositing ) return data->index >= data->rects.count(); // run once per each area #endif #ifdef KWIN_HAVE_XRENDER_COMPOSITING if( effects->compositingType() == XRenderCompositing ) return data->index == 1; // run once #endif abort(); } void PaintClipper::Iterator::next() { data->index++; #ifdef KWIN_HAVE_OPENGL_COMPOSITING if( clip() && effects->compositingType() == OpenGLCompositing && data->index < data->rects.count()) { const QRect& r = data->rects[ data->index ]; // Scissor rect has to be given in OpenGL coords glScissor( r.x(), displayHeight() - r.y() - r.height(), r.width(), r.height()); } #endif } QRect PaintClipper::Iterator::boundingRect() const { if( !clip()) return infiniteRegion(); #ifdef KWIN_HAVE_OPENGL_COMPOSITING if( effects->compositingType() == OpenGLCompositing ) return data->rects[ data->index ]; #endif #ifdef KWIN_HAVE_XRENDER_COMPOSITING if( effects->compositingType() == XRenderCompositing ) return paintArea().boundingRect(); #endif abort(); return infiniteRegion(); } /*************************************************************** TimeLine ***************************************************************/ TimeLine::TimeLine(const int duration) { m_Time = 0; m_Duration = duration; m_TimeLine = new QTimeLine(m_Duration ? m_Duration : 1); // (avoid QTimeLine warning) m_TimeLine->setFrameRange(0, m_Duration); setCurveShape(EaseInCurve); } TimeLine::TimeLine(const TimeLine &other) { m_Time = other.m_Time; m_Duration = other.m_Duration; m_TimeLine = new QTimeLine(m_Duration ? m_Duration : 1); m_TimeLine->setFrameRange(0, m_Duration); setCurveShape(other.m_CurveShape); if( m_Duration != 0 ) setProgress(m_Progress); } TimeLine::~TimeLine() { delete m_TimeLine; } int TimeLine::duration() const { return m_Duration; } void TimeLine::setDuration(const int msec) { m_Duration = msec; m_TimeLine->setDuration(m_Duration); m_TimeLine->setFrameRange(0, m_Duration); } double TimeLine::value() const { Q_ASSERT( m_Duration != 0 ); return valueForTime(m_Time); } double TimeLine::valueForTime(const int msec) const { Q_ASSERT( m_Duration != 0 ); // Catch non QTimeLine CurveShapes here, (but there are none right now) // else use QTimeLine ... return m_TimeLine->valueForTime(msec); } void TimeLine::addTime(const int msec) { Q_ASSERT( m_Duration != 0 ); m_Time = qMin(m_Duration, m_Time + msec); m_Progress = (double)m_Time / m_Duration; } void TimeLine::removeTime(const int msec) { Q_ASSERT( m_Duration != 0 ); m_Time = qMax(0, m_Time - msec); m_Progress = (double)m_Time / m_Duration; } void TimeLine::setProgress(const double progress) { Q_ASSERT( m_Duration != 0 ); m_Progress = progress; m_Time = qRound(m_Duration * progress); } double TimeLine::progress() const { Q_ASSERT( m_Duration != 0 ); return m_Progress; } int TimeLine::time() const { Q_ASSERT( m_Duration != 0 ); return m_Time; } void TimeLine::addProgress(const double progress) { Q_ASSERT( m_Duration != 0 ); m_Progress += progress; m_Time = (int)(m_Duration * m_Progress); } void TimeLine::setCurveShape(CurveShape curveShape) { switch (curveShape) { case EaseInCurve: m_TimeLine->setCurveShape(QTimeLine::EaseInCurve); break; case EaseOutCurve: m_TimeLine->setCurveShape(QTimeLine::EaseOutCurve); break; case EaseInOutCurve: m_TimeLine->setCurveShape(QTimeLine::EaseInOutCurve); break; case LinearCurve: m_TimeLine->setCurveShape(QTimeLine::LinearCurve); break; case SineCurve: m_TimeLine->setCurveShape(QTimeLine::SineCurve); break; } m_CurveShape = curveShape; } /*************************************************************** Motion1D ***************************************************************/ Motion1D::Motion1D( double initial, double strength, double smoothness ) : Motion( initial, strength, smoothness ) { } Motion1D::Motion1D( const Motion1D &other ) : Motion( other ) { } Motion1D::~Motion1D() { } /*************************************************************** Motion2D ***************************************************************/ Motion2D::Motion2D( QPointF initial, double strength, double smoothness ) : Motion( initial, strength, smoothness ) { } Motion2D::Motion2D( const Motion2D &other ) : Motion( other ) { } Motion2D::~Motion2D() { } /*************************************************************** WindowMotionManager ***************************************************************/ WindowMotionManager::WindowMotionManager( bool useGlobalAnimationModifier ) : m_useGlobalAnimationModifier( useGlobalAnimationModifier ) { // TODO: Allow developer to modify motion attributes } // TODO: What happens when the window moves by an external force? WindowMotionManager::~WindowMotionManager() { } void WindowMotionManager::manage( EffectWindow *w ) { if( m_managedWindows.contains( w )) return; double strength = 0.08; double smoothness = 4.0; if( m_useGlobalAnimationModifier && effects->animationTimeFactor() ) { // If the factor is == 0 then we just skip the calculation completely strength = 0.08 / effects->animationTimeFactor(); smoothness = effects->animationTimeFactor() * 4.0; } m_managedWindows[ w ] = WindowMotion(); m_managedWindows[ w ].translation.setStrength( strength ); m_managedWindows[ w ].translation.setSmoothness( smoothness ); m_managedWindows[ w ].scale.setStrength( strength * 1.33 ); m_managedWindows[ w ].scale.setSmoothness( smoothness / 2.0 ); m_managedWindows[ w ].translation.setValue( w->pos() ); m_managedWindows[ w ].scale.setValue( QPointF( 1.0, 1.0 )); } void WindowMotionManager::unmanage( EffectWindow *w ) { if( !m_managedWindows.contains( w )) return; QPointF diffT = m_managedWindows[ w ].translation.distance(); QPointF diffS = m_managedWindows[ w ].scale.distance(); m_movingWindowsSet.remove( w ); m_managedWindows.remove( w ); } void WindowMotionManager::unmanageAll() { m_managedWindows.clear(); m_movingWindowsSet.clear(); } void WindowMotionManager::calculate( int time ) { if( !effects->animationTimeFactor() ) { // Just skip it completely if the user wants no animation m_movingWindowsSet.clear(); QHash::iterator it = m_managedWindows.begin(); for(; it != m_managedWindows.end(); it++ ) { WindowMotion *motion = &it.value(); motion->translation.finish(); motion->scale.finish(); } } QHash::iterator it = m_managedWindows.begin(); for(; it != m_managedWindows.end(); it++ ) { WindowMotion *motion = &it.value(); bool stopped = false; // TODO: What happens when distance() == 0 but we are still moving fast? // TODO: Motion needs to be calculated from the window's center QPointF diffT = motion->translation.distance(); if( diffT != QPoint( 0.0, 0.0 )) { // Still moving motion->translation.calculate( time ); diffT = motion->translation.distance(); if( qAbs( diffT.x() ) < 0.5 && qAbs( motion->translation.velocity().x() ) < 0.2 && qAbs( diffT.y() ) < 0.5 && qAbs( motion->translation.velocity().y() ) < 0.2 ) { // Hide tiny oscillations motion->translation.finish(); diffT = QPoint( 0.0, 0.0 ); stopped = true; } } QPointF diffS = motion->scale.distance(); if( diffS != QPoint( 0.0, 0.0 )) { // Still scaling motion->scale.calculate( time ); diffS = motion->scale.distance(); if( qAbs( diffS.x() ) < 0.001 && qAbs( motion->scale.velocity().x() ) < 0.05 && qAbs( diffS.y() ) < 0.001 && qAbs( motion->scale.velocity().y() ) < 0.05 ) { // Hide tiny oscillations motion->scale.finish(); diffS = QPoint( 0.0, 0.0 ); stopped = true; } } // We just finished this window's motion if( stopped && diffT == QPoint( 0.0, 0.0 ) && diffS == QPoint( 0.0, 0.0 )) m_movingWindowsSet.remove( it.key() ); } } void WindowMotionManager::reset() { if( !m_managedWindows.count() ) return; EffectWindowList windows = m_managedWindows.keys(); for( int i = 0; i < windows.size(); i++ ) { EffectWindow *w = windows.at( i ); m_managedWindows[ w ].translation.setTarget( w->pos() ); m_managedWindows[ w ].translation.finish(); m_managedWindows[ w ].scale.setTarget( QPointF( 1.0, 1.0 )); m_managedWindows[ w ].scale.finish(); } } void WindowMotionManager::reset( EffectWindow *w ) { if( !m_managedWindows.contains( w )) return; m_managedWindows[ w ].translation.setTarget( w->pos() ); m_managedWindows[ w ].translation.finish(); m_managedWindows[ w ].scale.setTarget( QPointF( 1.0, 1.0 )); m_managedWindows[ w ].scale.finish(); } void WindowMotionManager::apply( EffectWindow *w, WindowPaintData &data ) { if( !m_managedWindows.contains( w )) return; // TODO: Take into account existing scale so that we can work with multiple managers (E.g. Present windows + grid) data.xTranslate += m_managedWindows[ w ].translation.value().x() - w->x(); data.yTranslate += m_managedWindows[ w ].translation.value().y() - w->y(); data.xScale *= m_managedWindows[ w ].scale.value().x(); data.yScale *= m_managedWindows[ w ].scale.value().y(); } void WindowMotionManager::moveWindow( EffectWindow *w, QPoint target, double scale, double yScale ) { if( !m_managedWindows.contains( w )) abort(); // Notify the effect author that they did something wrong if( yScale == 0.0 ) yScale = scale; QPointF scalePoint( scale, yScale ); if( m_managedWindows[ w ].translation.value() == target && m_managedWindows[ w ].scale.value() == scalePoint ) return; // Window already at that position m_managedWindows[ w ].translation.setTarget( target ); m_managedWindows[ w ].scale.setTarget( scalePoint ); m_movingWindowsSet << w; } QRectF WindowMotionManager::transformedGeometry( EffectWindow *w ) const { QRectF geometry( w->geometry() ); // TODO: Take into account existing scale so that we can work with multiple managers (E.g. Present windows + grid) geometry.moveTo( m_managedWindows[ w ].translation.value() ); geometry.setWidth( geometry.width() * m_managedWindows[ w ].scale.value().x() ); geometry.setHeight( geometry.height() * m_managedWindows[ w ].scale.value().y() ); return geometry; } void WindowMotionManager::setTransformedGeometry( EffectWindow *w, const QRectF &geometry ) { m_managedWindows[ w ].translation.setValue( geometry.topLeft() ); m_managedWindows[ w ].scale.setValue( QPointF( geometry.width()/qreal(w->width()), geometry.height()/qreal(w->height()))); } QRectF WindowMotionManager::targetGeometry( EffectWindow *w ) const { QRectF geometry( w->geometry() ); // TODO: Take into account existing scale so that we can work with multiple managers (E.g. Present windows + grid) geometry.moveTo( m_managedWindows[ w ].translation.target() ); geometry.setWidth( geometry.width() * m_managedWindows[ w ].scale.target().x() ); geometry.setHeight( geometry.height() * m_managedWindows[ w ].scale.target().y() ); return geometry; } EffectWindow* WindowMotionManager::windowAtPoint( QPoint point, bool useStackingOrder ) const { Q_UNUSED( useStackingOrder ); // TODO: Stacking order uses EffectsHandler::stackingOrder() then filters by m_managedWindows QHash< EffectWindow*, WindowMotion >::ConstIterator it = m_managedWindows.constBegin(); while( it != m_managedWindows.constEnd() ) { if( transformedGeometry( it.key() ).contains( point ) ) return it.key(); ++it; } return NULL; } /*************************************************************** EffectFrame ***************************************************************/ EffectFrame::EffectFrame() { } EffectFrame::~EffectFrame() { } QColor EffectFrame::styledTextColor() { return Plasma::Theme::defaultTheme()->color( Plasma::Theme::TextColor ); } } // namespace diff --git a/lib/kwineffects.h b/lib/kwineffects.h index 3b69f73ce..f816546c1 100644 --- a/lib/kwineffects.h +++ b/lib/kwineffects.h @@ -1,1975 +1,1997 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2006 Lubos Lunak Copyright (C) 2009 Lucas Murray 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 . *********************************************************************/ #ifndef KWINEFFECTS_H #define KWINEFFECTS_H #include #include #include "kdecoration.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include class KLibrary; class KConfigGroup; class KActionCollection; class QKeyEvent; namespace KWin { class EffectWindow; class EffectWindowGroup; class EffectFrame; class Effect; class WindowQuad; class GLRenderTarget; class GLShader; class GLTexture; class XRenderPicture; class RotationData; class WindowQuadList; class WindowPrePaintData; class WindowPaintData; class ScreenPrePaintData; class ScreenPaintData; typedef QPair< QString, Effect* > EffectPair; typedef QPair< Effect*, Window > InputWindowPair; typedef QList< EffectWindow* > EffectWindowList; /** @defgroup kwineffects KWin effects library * KWin effects library contains necessary classes for creating new KWin * compositing effects. * * @section creating Creating new effects * This example will demonstrate the basics of creating an effect. We'll use * CoolEffect as the class name, cooleffect as internal name and * "Cool Effect" as user-visible name of the effect. * * This example doesn't demonstrate how to write the effect's code. For that, * see the documentation of the Effect class. * * @subsection creating-class CoolEffect class * First you need to create CoolEffect class which has to be a subclass of * @ref KWin::Effect. In that class you can reimplement various virtual * methods to control how and where the windows are drawn. * * @subsection creating-macro KWIN_EFFECT macro * To make KWin aware of your new effect, you first need to use the * @ref KWIN_EFFECT macro to connect your effect's class to it's internal * name. The internal name is used by KWin to identify your effect. It can be * freely chosen (although it must be a single word), must be unique and won't * be shown to the user. For our example, you would use the macro like this: * @code * KWIN_EFFECT(cooleffect, CoolEffect) * @endcode * * @subsection creating-buildsystem Buildsystem * To build the effect, you can use the KWIN_ADD_EFFECT() cmake macro which * can be found in effects/CMakeLists.txt file in KWin's source. First * argument of the macro is the name of the library that will contain * your effect. Although not strictly required, it is usually a good idea to * use the same name as your effect's internal name there. Following arguments * to the macro are the files containing your effect's source. If our effect's * source is in cooleffect.cpp, we'd use following: * @code * KWIN_ADD_EFFECT(cooleffect cooleffect.cpp) * @endcode * * This macro takes care of compiling your effect. You'll also need to install * your effect's .desktop file, so the example CMakeLists.txt file would be * as follows: * @code * KWIN_ADD_EFFECT(cooleffect cooleffect.cpp) * install( FILES cooleffect.desktop DESTINATION ${SERVICES_INSTALL_DIR}/kwin ) * @endcode * * @subsection creating-desktop Effect's .desktop file * You will also need to create .desktop file to set name, description, icon * and other properties of your effect. Important fields of the .desktop file * are: * @li Name User-visible name of your effect * @li Icon Name of the icon of the effect * @li Comment Short description of the effect * @li Type must be "Service" * @li X-KDE-ServiceTypes must be "KWin/Effect" * @li X-KDE-PluginInfo-Name effect's internal name as passed to the KWIN_EFFECT macro plus "kwin4_effect_" prefix * @li X-KDE-PluginInfo-Category effect's category. Should be one of Appearance, Accessibility, Window Management, Demos, Tests, Misc * @li X-KDE-PluginInfo-EnabledByDefault whether the effect should be enabled by default (use sparingly). Default is false * @li X-KDE-Library name of the library containing the effect. This is the first argument passed to the KWIN_ADD_EFFECT macro in cmake file plus "kwin4_effect_" prefix. * * Example cooleffect.desktop file follows: * @code [Desktop Entry] Name=Cool Effect Comment=The coolest effect you've ever seen Icon=preferences-system-windows-effect-cooleffect Type=Service X-KDE-ServiceTypes=KWin/Effect X-KDE-PluginInfo-Author=My Name X-KDE-PluginInfo-Email=my@email.here X-KDE-PluginInfo-Name=kwin4_effect_cooleffect X-KDE-PluginInfo-Category=Misc X-KDE-Library=kwin4_effect_cooleffect * @endcode * * * @section accessing Accessing windows and workspace * Effects can gain access to the properties of windows and workspace via * EffectWindow and EffectsHandler classes. * * There is one global EffectsHandler object which you can access using the * @ref effects pointer. * For each window, there is an EffectWindow object which can be used to read * window properties such as position and also to change them. * * For more information about this, see the documentation of the corresponding * classes. * * @{ **/ #define KWIN_EFFECT_API_MAKE_VERSION( major, minor ) (( major ) << 8 | ( minor )) #define KWIN_EFFECT_API_VERSION_MAJOR 0 -#define KWIN_EFFECT_API_VERSION_MINOR 153 +#define KWIN_EFFECT_API_VERSION_MINOR 154 #define KWIN_EFFECT_API_VERSION KWIN_EFFECT_API_MAKE_VERSION( \ KWIN_EFFECT_API_VERSION_MAJOR, KWIN_EFFECT_API_VERSION_MINOR ) enum WindowQuadType { WindowQuadError, // for the stupid default ctor WindowQuadContents, WindowQuadDecoration, EFFECT_QUAD_TYPE_START = 100 ///< @internal }; /** * EffectWindow::setData() and EffectWindow::data() global roles. * All values between 0 and 999 are reserved for global roles. */ enum DataRole { // Grab roles are used to force all other animations to ignore the window. // The value of the data is set to the Effect's `this` value. WindowAddedGrabRole = 1, WindowClosedGrabRole, WindowMinimizedGrabRole, WindowUnminimizedGrabRole, WindowForceBlurRole, ///< For fullscreen effects to enforce blurring of windows, WindowBlurBehindRole ///< For single windows to blur behind }; /** * Style types used by @ref EffectFrame. * @since 4.6 */ enum EffectFrameStyle { None, ///< Displays no frame around the contents. Unstyled, ///< Displays a basic box around the contents. Styled ///< Displays a Plasma-styled frame around the contents. }; /** * Infinite region (i.e. a special region type saying that everything needs to be painted). */ KWIN_EXPORT inline QRect infiniteRegion() { // INT_MIN / 2 because width/height is used (INT_MIN+INT_MAX==-1) return QRect( INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX ); } /** * @short Base class for all KWin effects * * This is the base class for all effects. By reimplementing virtual methods * of this class, you can customize how the windows are painted. * * The virtual methods of this class can broadly be divided into two * categories: the methods used for painting and those you can use to be * notified and react to certain events, e.g. that a window was closed. * * @section Chaining * Most methods of this class are called in chain style. This means that when * effects A and B area active then first e.g. A::paintWindow() is called and * then from within that method B::paintWindow() is called (although * indirectly). To achieve this, you need to make sure to call corresponding * method in EffectsHandler class from each such method (using @ref effects * pointer): * @code * void MyEffect::postPaintScreen() * { * // Do your own processing here * ... * // Call corresponding EffectsHandler method * effects->postPaintScreen(); * } * @endcode * * @section Effectsptr Effects pointer * @ref effects pointer points to the global EffectsHandler object that you can * use to interact with the windows. * * @section painting Painting stages * Painting of windows is done in three stages: * @li First, the prepaint pass.
* Here you can specify how the windows will be painted, e.g. that they will * be translucent and transformed. * @li Second, the paint pass.
* Here the actual painting takes place. You can change attributes such as * opacity of windows as well as apply transformations to them. You can also * paint something onto the screen yourself. * @li Finally, the postpaint pass.
* Here you can mark windows, part of windows or even the entire screen for * repainting to create animations. * * For each stage there are *Screen() and *Window() methods. The window method * is called for every window which the screen method is usually called just * once. **/ class KWIN_EXPORT Effect { public: /** Flags controlling how painting is done. */ // TODO: is that ok here? enum { /** * Window (or at least part of it) will be painted opaque. **/ PAINT_WINDOW_OPAQUE = 1 << 0, /** * Window (or at least part of it) will be painted translucent. **/ PAINT_WINDOW_TRANSLUCENT = 1 << 1, /** * Window will be painted with transformed geometry. **/ PAINT_WINDOW_TRANSFORMED = 1 << 2, /** * Paint only a region of the screen (can be optimized, cannot * be used together with TRANSFORMED flags). **/ PAINT_SCREEN_REGION = 1 << 3, /** * The whole screen will be painted with transformed geometry. * Forces the entire screen to be painted. **/ PAINT_SCREEN_TRANSFORMED = 1 << 4, /** * At least one window will be painted with transformed geometry. * Forces the entire screen to be painted. **/ PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS = 1 << 5, /** * Clear whole background as the very first step, without optimizing it **/ PAINT_SCREEN_BACKGROUND_FIRST = 1 << 6, /** * Temporary solution since (_OPAQUE | _TRANSLUCENT) is not working currently. **/ PAINT_DECORATION_ONLY = 1 << 7, /** * Window will be painted with a lanczos filter. **/ PAINT_WINDOW_LANCZOS = 1 << 8 }; /** * Constructs new Effect object. **/ Effect(); /** * Destructs the Effect object. **/ virtual ~Effect(); /** * Flags describing which parts of configuration have changed. */ enum ReconfigureFlag { ReconfigureAll = 1 << 0 /// Everything needs to be reconfigured. }; Q_DECLARE_FLAGS( ReconfigureFlags, ReconfigureFlag ) /** * Called when configuration changes (either the effect's or KWin's global). */ virtual void reconfigure( ReconfigureFlags flags ); /** * Called when another effect requests the proxy for this effect. */ virtual void* proxy(); /** * Called before starting to paint the screen. * In this method you can: * @li set whether the windows or the entire screen will be transformed * @li change the region of the screen that will be painted * @li do various housekeeping tasks such as initing your effect's variables for the upcoming paint pass or updating animation's progress **/ virtual void prePaintScreen( ScreenPrePaintData& data, int time ); /** * In this method you can: * @li paint something on top of the windows (by painting after calling * effects->paintScreen()) * @li paint multiple desktops and/or multiple copies of the same desktop * by calling effects->paintScreen() multiple times **/ virtual void paintScreen( int mask, QRegion region, ScreenPaintData& data ); /** * Called after all the painting has been finished. * In this method you can: * @li schedule next repaint in case of animations * You shouldn't paint anything here. **/ virtual void postPaintScreen(); /** * Called for every window before the actual paint pass * In this method you can: * @li enable or disable painting of the window (e.g. enable paiting of minimized window) * @li set window to be painted with translucency * @li set window to be transformed * @li request the window to be divided into multiple parts **/ virtual void prePaintWindow( EffectWindow* w, WindowPrePaintData& data, int time ); /** * This is the main method for painting windows. * In this method you can: * @li do various transformations * @li change opacity of the window * @li change brightness and/or saturation, if it's supported **/ virtual void paintWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ); /** * Called for every window after all painting has been finished. * In this method you can: * @li schedule next repaint for individual window(s) in case of animations * You shouldn't paint anything here. **/ virtual void postPaintWindow( EffectWindow* w ); + /** + * This method is called directly before painting an @ref EffectFrame. + * You can implement this method if you need to bind a shader or perform + * other operations before the frame is rendered. + * @param frame The EffectFrame which will be rendered + * @param region Region to restrict painting to + * @param opacity Opacity of text/icon + * @param frameOpacity Opacity of background + * @since 4.6 + **/ + virtual void paintEffectFrame( EffectFrame* frame, QRegion region, double opacity, double frameOpacity ); + /** * Called on Transparent resizes. * return true if your effect substitutes the XOR rubberband */ virtual bool isResizeEffect() { return false; } /** * Can be called to draw multiple copies (e.g. thumbnails) of a window. * You can change window's opacity/brightness/etc here, but you can't * do any transformations **/ virtual void drawWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ); /** * Define new window quads so that they can be transformed by other effects. * It's up to the effect to keep track of them. **/ virtual void buildQuads( EffectWindow* w, WindowQuadList& quadList ); /** * This function is used e.g. by the shadow effect which adds area around windows * that needs to be painted as well - e.g. when a window is hidden and the workspace needs * to be repainted at that area, shadow's transformWindowDamage() adds the shadow area * to it, so that it is repainted as well. **/ virtual QRect transformWindowDamage( EffectWindow* w, const QRect& r ); /** called when moved/resized or once after it's finished */ virtual void windowUserMovedResized( EffectWindow* c, bool first, bool last ); /** called when the geometry changed during moving/resizing. */ virtual void windowMoveResizeGeometryUpdate( EffectWindow* c, const QRect& geometry ); virtual void windowOpacityChanged( EffectWindow* c, double old_opacity ); virtual void windowAdded( EffectWindow* c ); virtual void windowClosed( EffectWindow* c ); virtual void windowDeleted( EffectWindow* c ); virtual void windowActivated( EffectWindow* c ); virtual void windowMinimized( EffectWindow* c ); virtual void windowUnminimized( EffectWindow* c ); virtual void clientGroupItemSwitched( EffectWindow* from, EffectWindow* to ); virtual void clientGroupItemAdded( EffectWindow* from, EffectWindow* to ); // from merged with to virtual void clientGroupItemRemoved( EffectWindow* c, EffectWindow* group ); // c removed from group virtual void windowInputMouseEvent( Window w, QEvent* e ); virtual void desktopChanged( int old ); virtual void windowDamaged( EffectWindow* w, const QRect& r ); virtual void windowGeometryShapeChanged( EffectWindow* w, const QRect& old ); virtual void mouseChanged( const QPoint& pos, const QPoint& oldpos, Qt::MouseButtons buttons, Qt::MouseButtons oldbuttons, Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers ); virtual void grabbedKeyboardEvent( QKeyEvent* e ); /** Receives events registered for using EffectsHandler::registerPropertyType(). Use readProperty() to get the property data. Note that the property may be already set on the window, so doing the same processing from windowAdded() (e.g. simply calling propertyNotify() from it) is usually needed. */ virtual void propertyNotify( EffectWindow* w, long atom ); virtual void tabBoxAdded( int mode ); virtual void tabBoxClosed(); virtual void tabBoxUpdated(); virtual void tabBoxKeyEvent( QKeyEvent* event ); virtual bool borderActivated( ElectricBorder border ); /** * called when the number of currently existing desktops is changed. * @param old The previous number of desktops in used. * @see EffectsHandler::numberOfDesktops. */ virtual void numberDesktopsChanged( int old ); static int displayWidth(); static int displayHeight(); static QPoint cursorPos(); /** * Read animation time from the configuration and possibly adjust using animationTimeFactor(). * The configuration value in the effect should also have special value 'default' (set using * QSpinBox::setSpecialValueText()) with the value 0. This special value is adjusted * using the global animation speed, otherwise the exact time configured is returned. * @param cfg configuration group to read value from * @param key configuration key to read value from * @param defaultTime default animation time in milliseconds */ // return type is intentionally double so that one can divide using it without losing data static double animationTime( const KConfigGroup& cfg, const QString& key, int defaultTime ); /** * @overload Use this variant if the animation time is hardcoded and not configurable * in the effect itself. */ static double animationTime( int defaultTime ); /** * Linearly interpolates between @p x and @p y. * * Returns @p x when @p a = 0; returns @p y when @p a = 1. **/ static double interpolate(double x, double y, double a) { return x * (1 - a) + y * a; } /** Helper to set WindowPaintData and QRegion to necessary transformations so that * a following drawWindow() would put the window at the requested geometry (useful for thumbnails) **/ static void setPositionTransformations( WindowPaintData& data, QRect& region, EffectWindow* w, const QRect& r, Qt::AspectRatioMode aspect ); }; /** * Defines the class to be used for effect with given name. * The name must be same as effect's X-KDE-PluginInfo-Name values in .desktop * file, but without the "kwin4_effect_" prefix. * E.g. KWIN_EFFECT( flames, MyFlameEffect ) * In this case object of MyFlameEffect class would be created when effect * "flames" (which has X-KDE-PluginInfo-Name=kwin4_effect_flames in .desktop * file) is loaded. **/ #define KWIN_EFFECT( name, classname ) \ extern "C" { \ KWIN_EXPORT Effect* effect_create_kwin4_effect_##name() { return new classname; } \ KWIN_EXPORT int effect_version_kwin4_effect_##name() { return KWIN_EFFECT_API_VERSION; } \ } /** * Defines the function used to check whether an effect is supported * E.g. KWIN_EFFECT_SUPPORTED( flames, MyFlameEffect::supported() ) **/ #define KWIN_EFFECT_SUPPORTED( name, function ) \ extern "C" { \ KWIN_EXPORT bool effect_supported_kwin4_effect_##name() { return function; } \ } /** * Defines the function used to retrieve an effect's config widget * E.g. KWIN_EFFECT_CONFIG( flames, MyFlameEffectConfig ) **/ #define KWIN_EFFECT_CONFIG( name, classname ) \ K_PLUGIN_FACTORY(EffectFactory, registerPlugin(#name);) \ K_EXPORT_PLUGIN(EffectFactory("kcm_kwin4_effect_" #name)) /** * Defines the function used to retrieve multiple effects' config widget * E.g. KWIN_EFFECT_CONFIG_MULTIPLE( flames, * KWIN_EFFECT_CONFIG_SINGLE( flames, MyFlameEffectConfig ) * KWIN_EFFECT_CONFIG_SINGLE( fire, MyFireEffectConfig ) * ) **/ #define KWIN_EFFECT_CONFIG_MULTIPLE( name, singles ) \ K_PLUGIN_FACTORY(EffectFactory, singles) \ K_EXPORT_PLUGIN(EffectFactory("kcm_kwin4_effect_" #name)) /** * @see KWIN_EFFECT_CONFIG_MULTIPLE */ #define KWIN_EFFECT_CONFIG_SINGLE( name, classname ) \ registerPlugin(#name); /** * The declaration of the factory to export the effect */ #define KWIN_EFFECT_CONFIG_FACTORY K_PLUGIN_FACTORY_DECLARATION(EffectFactory) /** * @short Manager class that handles all the effects. * * This class creates Effect objects and calls it's appropriate methods. * * Effect objects can call methods of this class to interact with the * workspace, e.g. to activate or move a specific window, change current * desktop or create a special input window to receive mouse and keyboard * events. **/ class KWIN_EXPORT EffectsHandler { friend class Effect; public: EffectsHandler(CompositingType type); virtual ~EffectsHandler(); // for use by effects virtual void prePaintScreen( ScreenPrePaintData& data, int time ) = 0; virtual void paintScreen( int mask, QRegion region, ScreenPaintData& data ) = 0; virtual void postPaintScreen() = 0; virtual void prePaintWindow( EffectWindow* w, WindowPrePaintData& data, int time ) = 0; virtual void paintWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ) = 0; virtual void postPaintWindow( EffectWindow* w ) = 0; + virtual void paintEffectFrame( EffectFrame* frame, QRegion region, double opacity, double frameOpacity ) = 0; virtual void drawWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data ) = 0; virtual void buildQuads( EffectWindow* w, WindowQuadList& quadList ) = 0; virtual QRect transformWindowDamage( EffectWindow* w, const QRect& r ); // Functions for handling input - e.g. when an Expose-like effect is shown, an input window // covering the whole screen is created and all mouse events will be intercepted by it. // The effect's windowInputMouseEvent() will get called with such events. virtual Window createInputWindow( Effect* e, int x, int y, int w, int h, const QCursor& cursor ) = 0; Window createInputWindow( Effect* e, const QRect& r, const QCursor& cursor ); virtual Window createFullScreenInputWindow( Effect* e, const QCursor& cursor ); virtual void destroyInputWindow( Window w ) = 0; virtual QPoint cursorPos() const = 0; virtual bool grabKeyboard( Effect* effect ) = 0; virtual void ungrabKeyboard() = 0; /** * Retrieve the proxy class for an effect if it has one. Will return NULL if * the effect isn't loaded or doesn't have a proxy class. */ virtual void* getProxy( QString name ) = 0; // Mouse polling virtual void startMousePolling() = 0; virtual void stopMousePolling() = 0; virtual void checkElectricBorder(const QPoint &pos, Time time) = 0; virtual void reserveElectricBorder( ElectricBorder border ) = 0; virtual void unreserveElectricBorder( ElectricBorder border ) = 0; virtual void reserveElectricBorderSwitching( bool reserve ) = 0; // functions that allow controlling windows/desktop virtual void activateWindow( EffectWindow* c ) = 0; virtual EffectWindow* activeWindow() const = 0 ; virtual void moveWindow( EffectWindow* w, const QPoint& pos, bool snap = false, double snapAdjust = 1.0 ) = 0; virtual void windowToDesktop( EffectWindow* w, int desktop ) = 0; virtual void windowToScreen( EffectWindow* w, int screen ) = 0; virtual void setShowingDesktop( bool showing ) = 0; // Desktops /** * @returns The ID of the current desktop. */ virtual int currentDesktop() const = 0; /** * @returns Total number of desktops currently in existence. */ virtual int numberOfDesktops() const = 0; /** * Set the current desktop to @a desktop. */ virtual void setCurrentDesktop( int desktop ) = 0; /** * Sets the total number of desktops to @a desktops. */ virtual void setNumberOfDesktops( int desktops ) = 0; /** * @returns The size of desktop layout in grid units. */ virtual QSize desktopGridSize() const = 0; /** * @returns The width of desktop layout in grid units. */ virtual int desktopGridWidth() const = 0; /** * @returns The height of desktop layout in grid units. */ virtual int desktopGridHeight() const = 0; /** * @returns The width of desktop layout in pixels. */ virtual int workspaceWidth() const = 0; /** * @returns The height of desktop layout in pixels. */ virtual int workspaceHeight() const = 0; /** * @returns The ID of the desktop at the point @a coords or 0 if no desktop exists at that * point. @a coords is to be in grid units. */ virtual int desktopAtCoords( QPoint coords ) const = 0; /** * @returns The coords of desktop @a id in grid units. */ virtual QPoint desktopGridCoords( int id ) const = 0; /** * @returns The coords of the top-left corner of desktop @a id in pixels. */ virtual QPoint desktopCoords( int id ) const = 0; /** * @returns The ID of the desktop above desktop @a id. Wraps around to the bottom of * the layout if @a wrap is set. If @a id is not set use the current one. */ virtual int desktopAbove( int desktop = 0, bool wrap = true ) const = 0; /** * @returns The ID of the desktop to the right of desktop @a id. Wraps around to the * left of the layout if @a wrap is set. If @a id is not set use the current one. */ virtual int desktopToRight( int desktop = 0, bool wrap = true ) const = 0; /** * @returns The ID of the desktop below desktop @a id. Wraps around to the top of the * layout if @a wrap is set. If @a id is not set use the current one. */ virtual int desktopBelow( int desktop = 0, bool wrap = true ) const = 0; /** * @returns The ID of the desktop to the left of desktop @a id. Wraps around to the * right of the layout if @a wrap is set. If @a id is not set use the current one. */ virtual int desktopToLeft( int desktop = 0, bool wrap = true ) const = 0; /** * @returns Whether or not the desktop layout is allowed to be modified by the user. */ virtual bool isDesktopLayoutDynamic() const = 0; /** * Create new desktop at the point @a coords * @returns The ID of the created desktop */ virtual int addDesktop( QPoint coords ) = 0; /** * Deletes the desktop with the ID @a id. All desktops with an ID greater than the one that * was deleted will have their IDs' decremented. */ virtual void deleteDesktop( int id ) = 0; virtual QString desktopName( int desktop ) const = 0; virtual bool optionRollOverDesktops() const = 0; virtual int activeScreen() const = 0; // Xinerama virtual int numScreens() const = 0; // Xinerama virtual int screenNumber( const QPoint& pos ) const = 0; // Xinerama virtual QRect clientArea( clientAreaOption, int screen, int desktop ) const = 0; virtual QRect clientArea( clientAreaOption, const EffectWindow* c ) const = 0; virtual QRect clientArea( clientAreaOption, const QPoint& p, int desktop ) const = 0; /** * Factor by which animation speed in the effect should be modified (multiplied). * If configurable in the effect itself, the option should have also 'default' * animation speed. The actual value should be determined using animationTime(). * Note: The factor can be also 0, so make sure your code can cope with 0ms time * if used manually. */ virtual double animationTimeFactor() const = 0; virtual WindowQuadType newWindowQuadType() = 0; virtual EffectWindow* findWindow( WId id ) const = 0; virtual EffectWindowList stackingOrder() const = 0; // window will be temporarily painted as if being at the top of the stack virtual void setElevatedWindow( EffectWindow* w, bool set ) = 0; virtual void setTabBoxWindow(EffectWindow*) = 0; virtual void setTabBoxDesktop(int) = 0; virtual EffectWindowList currentTabBoxWindowList() const = 0; virtual void refTabBox() = 0; virtual void unrefTabBox() = 0; virtual void closeTabBox() = 0; virtual QList< int > currentTabBoxDesktopList() const = 0; virtual int currentTabBoxDesktop() const = 0; virtual EffectWindow* currentTabBoxWindow() const = 0; virtual void setActiveFullScreenEffect( Effect* e ) = 0; virtual Effect* activeFullScreenEffect() const = 0; virtual void pushRenderTarget(GLRenderTarget* target) = 0; virtual GLRenderTarget* popRenderTarget() = 0; /** * Schedules the entire workspace to be repainted next time. * If you call it during painting (including prepaint) then it does not * affect the current painting. **/ virtual void addRepaintFull() = 0; virtual void addRepaint( const QRect& r ) = 0; virtual void addRepaint( const QRegion& r ) = 0; virtual void addRepaint( int x, int y, int w, int h ) = 0; CompositingType compositingType() const; virtual unsigned long xrenderBufferPicture() = 0; bool saturationSupported() const; virtual void reconfigure() = 0; /** Makes KWin core watch PropertyNotify events for the given atom, or stops watching if reg is false (must be called the same number of times as registering). Events are sent using Effect::propertyNotify(). Note that even events that haven't been registered for can be received. */ virtual void registerPropertyType( long atom, bool reg ) = 0; virtual QByteArray readRootProperty( long atom, long type, int format ) const = 0; virtual void deleteRootProperty( long atom ) const = 0; /** * Returns @a true if the active window decoration has shadow API hooks. */ virtual bool hasDecorationShadows() const = 0; /** * Returns @a true if the window decorations use the alpha channel, and @a false otherwise. * @since 4.5 */ virtual bool decorationsHaveAlpha() const = 0; /** * Checks if the driver is on given blacklist. * The format of the blacklist is driver identifier as key (e.g. Intel) with a list of * concrete driver render strings as the values. The renderer string consists of GL_RENDERER * and GL_VERSION separated by colon dash colon. E.g.: GeForce 9400M/PCI/SSE2:-:3.2.0 NVIDIA 195.36.24 * This method is only used for OpenGL compositing. If not using OpenGL it will return false. * @returns true if the driver is blacklisted, false otherwise * @since 4.5 */ bool checkDriverBlacklist( const KConfigGroup& blacklist ); /** * Creates a new frame object. If the frame does not have a static size * then it will be located at @a position with @a alignment. A * non-static frame will automatically adjust its size to fit the contents. * @returns A new @ref EffectFrame. It is the responsibility of the caller to delete the * EffectFrame. * @since 4.6 */ virtual EffectFrame* effectFrame( EffectFrameStyle style, bool staticSize = true, const QPoint& position = QPoint( -1, -1 ), Qt::Alignment alignment = Qt::AlignCenter ) const = 0; /** * Sends message over DCOP to reload given effect. * @param effectname effect's name without "kwin4_effect_" prefix. * Can be called from effect's config module to apply config changes. **/ static void sendReloadMessage( const QString& effectname ); /** * @return @ref KConfigGroup which holds given effect's config options **/ static KConfigGroup effectConfig( const QString& effectname ); protected: QVector< EffectPair > loaded_effects; QHash< QString, KLibrary* > effect_libraries; QList< InputWindowPair > input_windows; //QHash< QString, EffectFactory* > effect_factories; int current_paint_screen; int current_paint_window; int current_draw_window; int current_build_quads; int current_transform; CompositingType compositing_type; }; /** * @short Representation of a window used by/for Effect classes. * * The purpose is to hide internal data and also to serve as a single * representation for the case when Client/Unmanaged becomes Deleted. **/ class KWIN_EXPORT EffectWindow { public: /** Flags explaining why painting should be disabled */ enum { /** Window will not be painted */ PAINT_DISABLED = 1 << 0, /** Window will not be painted because it is deleted */ PAINT_DISABLED_BY_DELETE = 1 << 1, /** Window will not be painted because of which desktop it's on */ PAINT_DISABLED_BY_DESKTOP = 1 << 2, /** Window will not be painted because it is minimized */ PAINT_DISABLED_BY_MINIMIZE = 1 << 3, /** Window will not be painted because it is not the active window in a client group */ PAINT_DISABLED_BY_CLIENT_GROUP = 1 << 4, /** Window will not be painted because it's not on the current activity */ PAINT_DISABLED_BY_ACTIVITY = 1 << 5 }; EffectWindow(); virtual ~EffectWindow(); virtual void enablePainting( int reason ) = 0; virtual void disablePainting( int reason ) = 0; virtual bool isPaintingEnabled() = 0; virtual void addRepaint( const QRect& r ) = 0; virtual void addRepaint( int x, int y, int w, int h ) = 0; virtual void addRepaintFull() = 0; virtual void refWindow() = 0; virtual void unrefWindow() = 0; virtual bool isDeleted() const = 0; virtual bool isMinimized() const = 0; virtual double opacity() const = 0; virtual bool hasAlpha() const = 0; virtual bool isOnDesktop( int d ) const; virtual bool isOnCurrentDesktop() const; virtual bool isOnAllDesktops() const = 0; virtual int desktop() const = 0; // prefer isOnXXX() virtual int x() const = 0; virtual int y() const = 0; virtual int width() const = 0; virtual int height() const = 0; virtual QRect geometry() const = 0; virtual QRegion shape() const = 0; virtual int screen() const = 0; /** @internal Do not use */ virtual bool hasOwnShape() const = 0; // only for shadow effect, for now virtual QPoint pos() const = 0; virtual QSize size() const = 0; virtual QRect rect() const = 0; virtual bool isMovable() const = 0; virtual bool isMovableAcrossScreens() const = 0; virtual bool isUserMove() const = 0; virtual bool isUserResize() const = 0; virtual QRect iconGeometry() const = 0; /** * Geometry of the actual window contents inside the whole (including decorations) window. */ virtual QRect contentsRect() const = 0; /** * Geometry of the transparent rect in the decoration. * May be different from contentsRect() if the decoration is extended into the client area. * @since 4.5 */ virtual QRect decorationInnerRect() const = 0; bool hasDecoration() const; virtual QByteArray readProperty( long atom, long type, int format ) const = 0; virtual void deleteProperty( long atom ) const = 0; virtual QString caption() const = 0; virtual QPixmap icon() const = 0; virtual QString windowClass() const = 0; virtual QString windowRole() const = 0; virtual const EffectWindowGroup* group() const = 0; /** * Returns whether the window is a desktop background window (the one with wallpaper). * See _NET_WM_WINDOW_TYPE_DESKTOP at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isDesktop() const = 0; /** * Returns whether the window is a dock (i.e. a panel). * See _NET_WM_WINDOW_TYPE_DOCK at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isDock() const = 0; /** * Returns whether the window is a standalone (detached) toolbar window. * See _NET_WM_WINDOW_TYPE_TOOLBAR at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isToolbar() const = 0; /** * Returns whether the window is standalone menubar (AKA macmenu). * This window type is a KDE extension. */ virtual bool isTopMenu() const = 0; /** * Returns whether the window is a torn-off menu. * See _NET_WM_WINDOW_TYPE_MENU at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isMenu() const = 0; /** * Returns whether the window is a "normal" window, i.e. an application or any other window * for which none of the specialized window types fit. * See _NET_WM_WINDOW_TYPE_NORMAL at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isNormalWindow() const = 0; // normal as in 'NET::Normal or NET::Unknown non-transient' /** * Returns whether the window is any of special windows types (desktop, dock, splash, ...), * i.e. window types that usually don't have a window frame and the user does not use window * management (moving, raising,...) on them. */ virtual bool isSpecialWindow() const = 0; /** * Returns whether the window is a dialog window. * See _NET_WM_WINDOW_TYPE_DIALOG at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isDialog() const = 0; /** * Returns whether the window is a splashscreen. Note that many (especially older) applications * do not support marking their splash windows with this type. * See _NET_WM_WINDOW_TYPE_SPLASH at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isSplash() const = 0; /** * Returns whether the window is a utility window, such as a tool window. * See _NET_WM_WINDOW_TYPE_UTILITY at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isUtility() const = 0; /** * Returns whether the window is a dropdown menu (i.e. a popup directly or indirectly open * from the applications menubar). * See _NET_WM_WINDOW_TYPE_DROPDOWN_MENU at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isDropdownMenu() const = 0; /** * Returns whether the window is a popup menu (that is not a torn-off or dropdown menu). * See _NET_WM_WINDOW_TYPE_POPUP_MENU at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isPopupMenu() const = 0; // a context popup, not dropdown, not torn-off /** * Returns whether the window is a tooltip. * See _NET_WM_WINDOW_TYPE_TOOLTIP at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isTooltip() const = 0; /** * Returns whether the window is a window with a notification. * See _NET_WM_WINDOW_TYPE_NOTIFICATION at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isNotification() const = 0; /** * Returns whether the window is a combobox popup. * See _NET_WM_WINDOW_TYPE_COMBO at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isComboBox() const = 0; /** * Returns whether the window is a Drag&Drop icon. * See _NET_WM_WINDOW_TYPE_DND at http://standards.freedesktop.org/wm-spec/wm-spec-latest.html . */ virtual bool isDNDIcon() const = 0; /** * Returns whether the window is managed by KWin (it has control over its placement and other * aspects, as opposed to override-redirect windows that are entirely handled by the application). */ virtual bool isManaged() const = 0; // whether it's managed or override-redirect /** * Returns whether or not the window can accept keyboard focus. */ virtual bool acceptsFocus() const = 0; /** * Returns whether or not the window is kept above all other windows. */ virtual bool keepAbove() const = 0; virtual bool isModal() const = 0; virtual EffectWindow* findModal() = 0; virtual EffectWindowList mainWindows() const = 0; /** * Returns whether the window should be excluded from window switching effects. * @since 4.5 */ virtual bool isSkipSwitcher() const = 0; /** * Returns the unmodified window quad list. Can also be used to force rebuilding. */ virtual WindowQuadList buildQuads( bool force = false ) const = 0; virtual void minimize() const = 0; virtual void unminimize() const = 0; virtual void closeWindow() const = 0; virtual bool visibleInClientGroup() const = 0; /** * Can be used to by effects to store arbitrary data in the EffectWindow. */ virtual void setData( int role, const QVariant &data ) = 0; virtual QVariant data( int role ) const = 0; }; class KWIN_EXPORT EffectWindowGroup { public: virtual ~EffectWindowGroup(); virtual EffectWindowList members() const = 0; }; class KWIN_EXPORT GlobalShortcutsEditor : public KShortcutsEditor { public: GlobalShortcutsEditor( QWidget *parent ); }; /** * @short Vertex class * * A vertex is one position in a window. WindowQuad consists of four WindowVertex objects * and represents one part of a window. **/ class KWIN_EXPORT WindowVertex { public: double x() const; double y() const; void move( double x, double y ); void setX( double x ); void setY( double y ); double originalX() const; double originalY() const; double textureX() const; double textureY() const; WindowVertex(); WindowVertex( double x, double y, double tx, double ty ); private: friend class WindowQuad; friend class WindowQuadList; double px, py; // position double ox, oy; // origional position double tx, ty; // texture coords }; /** * @short Class representing one area of a window. * * WindowQuads consists of four WindowVertex objects and represents one part of a window. */ // NOTE: This class expects the (original) vertices to be in the clockwise order starting from topleft. class KWIN_EXPORT WindowQuad { public: explicit WindowQuad( WindowQuadType type, int id = -1 ); WindowQuad makeSubQuad( double x1, double y1, double x2, double y2 ) const; WindowVertex& operator[]( int index ); const WindowVertex& operator[]( int index ) const; WindowQuadType type() const; int id() const; bool decoration() const; bool effect() const; double left() const; double right() const; double top() const; double bottom() const; double originalLeft() const; double originalRight() const; double originalTop() const; double originalBottom() const; bool smoothNeeded() const; bool isTransformed() const; private: friend class WindowQuadList; WindowVertex verts[ 4 ]; WindowQuadType quadType; // 0 - contents, 1 - decoration int quadID; }; class KWIN_EXPORT WindowQuadList : public QList< WindowQuad > { public: WindowQuadList splitAtX( double x ) const; WindowQuadList splitAtY( double y ) const; WindowQuadList makeGrid( int maxquadsize ) const; WindowQuadList makeRegularGrid( int xSubdivisions, int ySubdivisions ) const; WindowQuadList select( WindowQuadType type ) const; WindowQuadList filterOut( WindowQuadType type ) const; bool smoothNeeded() const; void makeArrays( float** vertices, float** texcoords ) const; bool isTransformed() const; }; class KWIN_EXPORT WindowPrePaintData { public: int mask; /** * Region that will be painted, in screen coordinates. **/ QRegion paint; /** * The clip region will be substracted from paint region of following windows. * I.e. window will definitely cover it's clip region **/ QRegion clip; WindowQuadList quads; /** * Simple helper that sets data to say the window will be painted as non-opaque. * Takes also care of changing the regions. */ void setTranslucent(); /** * Helper to mark that this window will be transformed **/ void setTransformed(); }; class KWIN_EXPORT WindowPaintData { public: WindowPaintData( EffectWindow* w ); /** * Window opacity, in range 0 = transparent to 1 = fully opaque * Opacity for contents is opacity*contents_opacity, the same * way for decoration. */ double opacity; double contents_opacity; double decoration_opacity; double xScale; double yScale; double zScale; int xTranslate; int yTranslate; double zTranslate; /** * Saturation of the window, in range [0; 1] * 1 means that the window is unchanged, 0 means that it's completely * unsaturated (greyscale). 0.5 would make the colors less intense, * but not completely grey * Use EffectsHandler::saturationSupported() to find out whether saturation * is supported by the system, otherwise this value has no effect. **/ double saturation; /** * Brightness of the window, in range [0; 1] * 1 means that the window is unchanged, 0 means that it's completely * black. 0.5 would make it 50% darker than usual **/ double brightness; WindowQuadList quads; /** * Shader to be used for rendering, if any. */ GLShader* shader; RotationData* rotation; }; class KWIN_EXPORT ScreenPaintData { public: ScreenPaintData(); double xScale; double yScale; double zScale; int xTranslate; int yTranslate; double zTranslate; RotationData* rotation; }; class KWIN_EXPORT ScreenPrePaintData { public: int mask; QRegion paint; }; class KWIN_EXPORT RotationData { public: RotationData(); enum RotationAxis { XAxis, YAxis, ZAxis }; RotationAxis axis; float angle; float xRotationPoint; float yRotationPoint; float zRotationPoint; }; /** * @short Helper class for restricting painting area only to allowed area. * * This helper class helps specifying areas that should be painted, clipping * out the rest. The simplest usage is creating an object on the stack * and giving it the area that is allowed to be painted to. When the object * is destroyed, the restriction will be removed. * Note that all painting code must use paintArea() to actually perform the clipping. */ class KWIN_EXPORT PaintClipper { public: /** * Calls push(). */ PaintClipper( const QRegion& allowed_area ); /** * Calls pop(). */ ~PaintClipper(); /** * Allows painting only in the given area. When areas have been already * specified, painting is allowed only in the intersection of all areas. */ static void push( const QRegion& allowed_area ); /** * Removes the given area. It must match the top item in the stack. */ static void pop( const QRegion& allowed_area ); /** * Returns true if any clipping should be performed. */ static bool clip(); /** * If clip() returns true, this function gives the resulting area in which * painting is allowed. It is usually simpler to use the helper Iterator class. */ static QRegion paintArea(); /** * Helper class to perform the clipped painting. The usage is: * @code * for( PaintClipper::Iterator iterator; * !iterator.isDone(); * iterator.next()) * { // do the painting, possibly use iterator.boundingRect() * } * @endcode */ class KWIN_EXPORT Iterator { public: Iterator(); ~Iterator(); bool isDone(); void next(); QRect boundingRect() const; private: struct Data; Data* data; }; private: QRegion area; static QStack< QRegion >* areas; }; /** * @short Wrapper class for using timelines in KWin effects. * * This class provides an easy and specialized interface for * effects that want a non-linear timeline. Currently, most * it does is wrapping QTimeLine. In the future, this class * could help using physics animations in KWin. * * KWin effects will usually use this feature by keeping one * TimeLine per animation, for example per effect (when only * one animation is done by the effect) or per windows (in * the case that multiple animations can take place at the * same time (such as minizing multiple windows with a short * time offset. Increasing the internal time state of the * TimeLine is done either by changing the 'current' time in * the TimeLine, which is an int value between 0 and the * duration set (defaulting to 250msec). The current time * can also be changed by setting the 'progress' of the * TimeLine, a double between 0.0 and 1.0. setProgress(), * addProgress(), addTime(), removeTime() can all be used to * manipulate the 'current' time (and at the same time * progress) of the TimeLine. * * The internal state of the TimeLine is determined by the * duration of the TimeLine, int in milliseconds, defaulting. * the 'current' time and the current 'progress' are * interchangeable, both determine the x-axis of a graph * of a TimeLine. The value() returned represents the y-axis * in this graph. * * m_TimeLine.setProgress(0.5) would change the 'current' * time of a default TimeLine to 125 msec. m_TimeLine.value() * would then return the progress value (a double between 0.0 * and 1.0), which can be lower than 0.5 in case the animation * should start slowly, such as for the EaseInCurve. * In KWin effect, the prePaintWindow() or prePaintScreen() * methods have int time as one of their arguments. This int * can be used to increase the 'current' time in the TimeLine. * The double the is subsequently returned by value() (usually * in paintWindow() or paintScreen() methods can then be used * to manipulate windows, or their positions. */ class KWIN_EXPORT TimeLine { //Q_ENUMS( CurveShape ) // Requires Q_OBJECT public: /** * The CurveShape describes the relationship between time * and values. We can pass some of them through to QTimeLine * but also invent our own ones. */ enum CurveShape { EaseInCurve = 0, EaseOutCurve, EaseInOutCurve, LinearCurve, SineCurve }; /** * Creates a TimeLine and computes the progress data. Usually, for larger * animations you want to choose values more towards 300 milliseconds. * For small animations, values around 150 milliseconds are sensible. * Note that duration 0 is not valid. */ explicit TimeLine(const int duration = 0); /** * Creates a copy of the TimeLine so we can have the state copied * as well. */ TimeLine(const TimeLine &other); /** * Cleans up. */ ~TimeLine(); /** * Returns the duration of the timeline in msec. */ int duration() const; /** * Set the duration of the TimeLine. */ void setDuration(const int msec); /** * Returns the Value at the time set, this method will * usually be used to get the progress in the paintWindow() * and related methods. The value represents the y-axis' value * corresponding to the current progress (or time) set by * setProgress(), addProgress(), addTime(), removeTime() */ double value() const; /** * Returns the Value at the time provided, this method will * usually be used to get the progress in the paintWindow() * and related methods, the y value of the current state x. */ double valueForTime(const int msec) const; /** * Returns the current time of the TimeLine, between 0 and duration() * The value returned is equivalent to the x-axis on a curve. */ int time() const; /** * Returns the progress of the TimeLine, between 0.0 and 1.0. * The value returned is equivalent to the y-axis on a curve. */ double progress() const; /** * Increases the internal progress accounting of the timeline. */ void addProgress(const double progress); /** * Increases the internal counter, this is usually done in * prePaintWindow(). */ void addTime(const int msec); /** * Decreases the internal counter, this is usually done in * prePaintWindow(). This function comes handy for reverse * animations. */ void removeTime(const int msec); /** * Set the time to progress * duration. This will change the * internal time in the TimeLine. It's usually used in * prePaintWindow() or prePaintScreen() so the value() * taken in paint* is increased. */ void setProgress(const double progress); /** * Set the CurveShape. The CurveShape describes the relation * between the value and the time. progress is between 0 and 1 * It's used as input for the timeline, the x axis of the curve. */ void setCurveShape(CurveShape curveShape); /** * Set the CurveShape. The CurveShape describes the relation * between the value and the time. */ //void setCurveShape(CurveShape curveShape); private: QTimeLine* m_TimeLine; int m_Time; double m_Progress; int m_Duration; CurveShape m_CurveShape; //Q_DISABLE_COPY(TimeLine) }; /** * @internal */ template class KWIN_EXPORT Motion { public: /** * Creates a new motion object. "Strength" is the amount of * acceleration that is applied to the object when the target * changes and "smoothness" relates to how fast the object * can change its direction and speed. */ explicit Motion( T initial, double strength, double smoothness ); /** * Creates an exact copy of another motion object, including * position, target and velocity. */ Motion( const Motion &other ); ~Motion(); inline T value() const { return m_value; } inline void setValue( const T value ) { m_value = value; } inline T target() const { return m_target; } inline void setTarget( const T target ) { m_target = target; } inline T velocity() const { return m_velocity; } inline void setVelocity( const T velocity ) { m_velocity = velocity; } inline double strength() const { return m_strength; } inline void setStrength( const double strength ) { m_strength = strength; } inline double smoothness() const { return m_smoothness; } inline void setSmoothness( const double smoothness ) { m_smoothness = smoothness; } /** * The distance between the current position and the target. */ inline T distance() const { return m_target - m_value; } /** * Calculates the new position if not at the target. Called * once per frame only. */ void calculate( const int msec ); /** * Place the object on top of the target immediately, * bypassing all movement calculation. */ void finish(); private: T m_value; T m_target; T m_velocity; double m_strength; double m_smoothness; }; /** * @short A single 1D motion dynamics object. * * This class represents a single object that can be moved around a * 1D space. Although it can be used directly by itself it is * recommended to use a motion manager instead. */ class KWIN_EXPORT Motion1D : public Motion { public: explicit Motion1D( double initial = 0.0, double strength = 0.08, double smoothness = 4.0 ); Motion1D( const Motion1D &other ); ~Motion1D(); }; /** * @short A single 2D motion dynamics object. * * This class represents a single object that can be moved around a * 2D space. Although it can be used directly by itself it is * recommended to use a motion manager instead. */ class KWIN_EXPORT Motion2D : public Motion { public: explicit Motion2D( QPointF initial = QPointF(), double strength = 0.08, double smoothness = 4.0 ); Motion2D( const Motion2D &other ); ~Motion2D(); }; /** * @short Helper class for motion dynamics in KWin effects. * * This motion manager class is intended to help KWin effect authors * move windows across the screen smoothly and naturally. Once * windows are registered by the manager the effect can issue move * commands with the moveWindow() methods. The position of any * managed window can be determined in realtime by the * transformedGeometry() method. As the manager knows if any windows * are moving at any given time it can also be used as a notifier as * to see whether the effect is active or not. */ class KWIN_EXPORT WindowMotionManager { public: /** * Creates a new window manager object. */ explicit WindowMotionManager( bool useGlobalAnimationModifier = true ); ~WindowMotionManager(); /** * Register a window for managing. */ void manage( EffectWindow *w ); /** * Register a list of windows for managing. */ inline void manage( EffectWindowList list ) { for( int i = 0; i < list.size(); i++ ) manage( list.at( i )); } /** * Deregister a window. All transformations applied to the * window will be permanently removed and cannot be recovered. */ void unmanage( EffectWindow *w ); /** * Deregister all windows, returning the manager to its * originally initiated state. */ void unmanageAll(); /** * Determine the new positions for windows that have not * reached their target. Called once per frame, usually in * prePaintScreen(). Remember to set the * Effect::PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS flag. */ void calculate( int time ); /** * Modify a registered window's paint data to make it appear * at its real location on the screen. Usually called in * paintWindow(). Remember to flag the window as having been * transformed in prePaintWindow() by calling * WindowPrePaintData::setTransformed() */ void apply( EffectWindow *w, WindowPaintData &data ); /** * Set all motion targets and values back to where the * windows were before transformations. The same as * unmanaging then remanaging all windows. */ void reset(); /** * Resets the motion target and current value of a single * window. */ void reset( EffectWindow *w ); /** * Ask the manager to move the window to the target position * with the specified scale. If `yScale` is not provided or * set to 0.0, `scale` will be used as the scale in the * vertical direction as well as in the horizontal direction. */ void moveWindow( EffectWindow *w, QPoint target, double scale = 1.0, double yScale = 0.0 ); /** * This is an overloaded method, provided for convenience. * * Ask the manager to move the window to the target rectangle. * Automatically determines scale. */ inline void moveWindow( EffectWindow *w, QRect target ) { // TODO: Scale might be slightly different in the comparison due to rounding moveWindow( w, target.topLeft(), target.width() / double( w->width() ), target.height() / double( w->height() )); } /** * Retrieve the current tranformed geometry of a registered * window. */ QRectF transformedGeometry( EffectWindow *w ) const; /** * Sets the current transformed geometry of a registered window to the given geometry. * @see transformedGeometry * @since 4.5 */ void setTransformedGeometry( EffectWindow *w, const QRectF &geometry ); /** * Retrieve the current target geometry of a registered * window. */ QRectF targetGeometry( EffectWindow *w ) const; /** * Return the window that has its transformed geometry under * the specified point. It is recommended to use the stacking * order as it's what the user sees, but it is slightly * slower to process. */ EffectWindow* windowAtPoint( QPoint point, bool useStackingOrder = true ) const; /** * Return a list of all currently registered windows. */ inline EffectWindowList managedWindows() const { return m_managedWindows.keys(); } /** * Returns whether or not a specified window is being managed * by this manager object. */ inline bool isManaging( EffectWindow *w ) { return m_managedWindows.contains( w ); } /** * Returns whether or not this manager object is actually * managing any windows or not. */ inline bool managingWindows() { return !m_managedWindows.empty(); } /** * Returns whether all windows have reached their targets yet * or not. Can be used to see if an effect should be * processed and displayed or not. */ inline bool areWindowsMoving() { return !m_movingWindowsSet.isEmpty(); } private: bool m_useGlobalAnimationModifier; struct WindowMotion { // TODO: Rotation, etc? Motion2D translation; // Absolute position Motion2D scale; // xScale and yScale }; QHash m_managedWindows; QSet m_movingWindowsSet; }; /** * @short Helper class for displaying text and icons in frames. * * Paints text and/or and icon with an optional frame around them. The * available frames includes one that follows the default Plasma theme and * another that doesn't. * It is recommended to use this class whenever displaying text. */ class KWIN_EXPORT EffectFrame { public: EffectFrame(); virtual ~EffectFrame(); /** * Delete any existing textures to free up graphics memory. They will * be automatically recreated the next time they are required. */ virtual void free() = 0; /** * Render the frame. */ virtual void render( QRegion region = infiniteRegion(), double opacity = 1.0, double frameOpacity = 1.0 ) = 0; virtual void setPosition( const QPoint& point ) = 0; /** * Set the text alignment for static frames and the position alignment * for non-static. */ virtual void setAlignment( Qt::Alignment alignment ) = 0; virtual Qt::Alignment alignment() const = 0; virtual void setGeometry( const QRect& geometry, bool force = false ) = 0; virtual const QRect& geometry() const = 0; virtual void setText( const QString& text ) = 0; virtual const QString& text() const = 0; virtual void setFont( const QFont& font ) = 0; virtual const QFont& font() const = 0; /** * Set the icon that will appear on the left-hand size of the frame. */ virtual void setIcon( const QPixmap& icon ) = 0; virtual const QPixmap& icon() const = 0; virtual void setIconSize( const QSize& size ) = 0; virtual const QSize& iconSize() const = 0; + /** + * @param shader The GLShader for rendering. + **/ + virtual void setShader( GLShader* shader ) = 0; + /** + * @returns The GLShader used for rendering or null if none. + **/ + virtual GLShader* shader() const = 0; + /** * The foreground text color as specified by the default Plasma theme. */ static QColor styledTextColor(); }; /** * Pointer to the global EffectsHandler object. **/ extern KWIN_EXPORT EffectsHandler* effects; /*************************************************************** WindowVertex ***************************************************************/ inline WindowVertex::WindowVertex() : px( 0 ), py( 0 ), tx( 0 ), ty( 0 ) { } inline WindowVertex::WindowVertex( double _x, double _y, double _tx, double _ty ) : px( _x ), py( _y ), ox( _x ), oy( _y ), tx( _tx ), ty( _ty ) { } inline double WindowVertex::x() const { return px; } inline double WindowVertex::y() const { return py; } inline double WindowVertex::originalX() const { return ox; } inline double WindowVertex::originalY() const { return oy; } inline double WindowVertex::textureX() const { return tx; } inline double WindowVertex::textureY() const { return ty; } inline void WindowVertex::move( double x, double y ) { px = x; py = y; } inline void WindowVertex::setX( double x ) { px = x; } inline void WindowVertex::setY( double y ) { py = y; } /*************************************************************** WindowQuad ***************************************************************/ inline WindowQuad::WindowQuad( WindowQuadType t, int id ) : quadType( t ) , quadID( id ) { } inline WindowVertex& WindowQuad::operator[]( int index ) { assert( index >= 0 && index < 4 ); return verts[ index ]; } inline const WindowVertex& WindowQuad::operator[]( int index ) const { assert( index >= 0 && index < 4 ); return verts[ index ]; } inline WindowQuadType WindowQuad::type() const { assert( quadType != WindowQuadError ); return quadType; } inline int WindowQuad::id() const { return quadID; } inline bool WindowQuad::decoration() const { assert( quadType != WindowQuadError ); return quadType == WindowQuadDecoration; } inline bool WindowQuad::effect() const { assert( quadType != WindowQuadError ); return quadType >= EFFECT_QUAD_TYPE_START; } inline bool WindowQuad::isTransformed() const { return !( verts[ 0 ].px == verts[ 0 ].ox && verts[ 0 ].py == verts[ 0 ].oy && verts[ 1 ].px == verts[ 1 ].ox && verts[ 1 ].py == verts[ 1 ].oy && verts[ 2 ].px == verts[ 2 ].ox && verts[ 2 ].py == verts[ 2 ].oy && verts[ 3 ].px == verts[ 3 ].ox && verts[ 3 ].py == verts[ 3 ].oy ); } inline double WindowQuad::left() const { return qMin( verts[ 0 ].px, qMin( verts[ 1 ].px, qMin( verts[ 2 ].px, verts[ 3 ].px ))); } inline double WindowQuad::right() const { return qMax( verts[ 0 ].px, qMax( verts[ 1 ].px, qMax( verts[ 2 ].px, verts[ 3 ].px ))); } inline double WindowQuad::top() const { return qMin( verts[ 0 ].py, qMin( verts[ 1 ].py, qMin( verts[ 2 ].py, verts[ 3 ].py ))); } inline double WindowQuad::bottom() const { return qMax( verts[ 0 ].py, qMax( verts[ 1 ].py, qMax( verts[ 2 ].py, verts[ 3 ].py ))); } inline double WindowQuad::originalLeft() const { return verts[ 0 ].ox; } inline double WindowQuad::originalRight() const { return verts[ 2 ].ox; } inline double WindowQuad::originalTop() const { return verts[ 0 ].oy; } inline double WindowQuad::originalBottom() const { return verts[ 2 ].oy; } /*************************************************************** Motion ***************************************************************/ template Motion::Motion( T initial, double strength, double smoothness ) : m_value( initial ) , m_target( initial ) , m_velocity() , m_strength( strength ) , m_smoothness( smoothness ) { } template Motion::Motion( const Motion &other ) : m_value( other.value() ) , m_target( other.target() ) , m_velocity( other.velocity() ) , m_strength( other.strength() ) , m_smoothness( other.smoothness() ) { } template Motion::~Motion() { } template void Motion::calculate( const int msec ) { if( m_value == m_target && m_velocity == T() ) // At target and not moving return; // Poor man's time independent calculation int steps = qMax( 1, msec / 5 ); for( int i = 0; i < steps; i++ ) { T diff = m_target - m_value; T strength = diff * m_strength; m_velocity = ( m_smoothness * m_velocity + strength ) / ( m_smoothness + 1.0 ); m_value += m_velocity; } } template void Motion::finish() { m_value = m_target; m_velocity = T(); } } // namespace /** @} */ #endif // KWINEFFECTS_H diff --git a/scene_opengl.cpp b/scene_opengl.cpp index 4130407ac..7af4440da 100644 --- a/scene_opengl.cpp +++ b/scene_opengl.cpp @@ -1,2203 +1,2227 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2006 Lubos Lunak Based on glcompmgr code by Felix Bellaby. Using code from Compiz and Beryl. 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 . *********************************************************************/ /* This is the OpenGL-based compositing code. It is the primary and most powerful compositing backend. Sources and other compositing managers: ======================================= - http://opengl.org - documentation - OpenGL Redbook (http://opengl.org/documentation/red_book/ - note it's only version 1.1) - GLX docs (http://opengl.org/documentation/specs/glx/glx1.4.pdf) - extensions docs (http://www.opengl.org/registry/) - glcompmgr - http://lists.freedesktop.org/archives/xorg/2006-July/017006.html , - http://www.mail-archive.com/compiz%40lists.freedesktop.org/msg00023.html - simple and easy to understand - works even without texture_from_pixmap extension - claims to support several different gfx cards - compile with something like "gcc -Wall glcompmgr-0.5.c `pkg-config --cflags --libs glib-2.0` -lGL -lXcomposite -lXdamage -L/usr/X11R6/lib" - compiz - git clone git://anongit.freedesktop.org/git/xorg/app/compiz - the ultimate - glxcompmgr - git clone git://anongit.freedesktop.org/git/xorg/app/glxcompgr - a rather old version of compiz, but also simpler and as such simpler to understand - beryl - a fork of Compiz - http://beryl-project.org - git clone git://anongit.beryl-project.org/beryl/beryl-core (or beryl-plugins etc. , the full list should be at git://anongit.beryl-project.org/beryl/) - libcm (metacity) - cvs -d :pserver:anonymous@anoncvs.gnome.org:/cvs/gnome co libcm - not much idea about it, the model differs a lot from KWin/Compiz/Beryl - does not seem to be very powerful or with that much development going on */ #include "scene_opengl.h" #include #include "utils.h" #include "client.h" #include "deleted.h" #include "effects.h" #include #include #include // turns on checks for opengl errors in various places (for easier finding of them) // normally only few of them are enabled //#define CHECK_GL_ERROR #ifdef KWIN_HAVE_OPENGL_COMPOSITING #include #include namespace KWin { //**************************************** // SceneOpenGL //**************************************** // the configs used for the destination GLXFBConfig SceneOpenGL::fbcbuffer_db; GLXFBConfig SceneOpenGL::fbcbuffer_nondb; // the configs used for windows SceneOpenGL::FBConfigInfo SceneOpenGL::fbcdrawableinfo[ 32 + 1 ]; // GLX content GLXContext SceneOpenGL::ctxbuffer; GLXContext SceneOpenGL::ctxdrawable; // the destination drawable where the compositing is done GLXDrawable SceneOpenGL::glxbuffer = None; GLXDrawable SceneOpenGL::last_pixmap = None; bool SceneOpenGL::tfp_mode; // using glXBindTexImageEXT (texture_from_pixmap) bool SceneOpenGL::db; // destination drawable is double-buffered bool SceneOpenGL::shm_mode; #ifdef HAVE_XSHM XShmSegmentInfo SceneOpenGL::shm; #endif SceneOpenGL::SceneOpenGL( Workspace* ws ) : Scene( ws ) , init_ok( false ) , selfCheckDone( false ) { if( !Extensions::glxAvailable()) { kDebug( 1212 ) << "No glx extensions available"; return; // error } initGLX(); // check for FBConfig support if( !hasGLExtension( "GLX_SGIX_fbconfig" ) || !glXGetFBConfigAttrib || !glXGetFBConfigs || !glXGetVisualFromFBConfig || !glXCreatePixmap || !glXDestroyPixmap || !glXCreateWindow || !glXDestroyWindow ) { kError( 1212 ) << "GLX_SGIX_fbconfig or required GLX functions missing"; return; // error } if( !selectMode()) return; // error if( !initBuffer()) // create destination buffer return; // error if( !initRenderingContext()) return; // error // Initialize OpenGL initGL(); if( !hasGLExtension( "GL_ARB_texture_non_power_of_two" ) && !hasGLExtension( "GL_ARB_texture_rectangle" )) { kError( 1212 ) << "GL_ARB_texture_non_power_of_two and GL_ARB_texture_rectangle missing"; return; // error } if( db ) glDrawBuffer( GL_BACK ); // Check whether certain features are supported has_waitSync = false; if( glXGetVideoSync && glXIsDirect( display(), ctxbuffer ) && options->glVSync ) { unsigned int sync; if( glXGetVideoSync( &sync ) == 0 ) { if( glXWaitVideoSync( 1, 0, &sync ) == 0 ) has_waitSync = true; } } // OpenGL scene setup glMatrixMode( GL_PROJECTION ); glLoadIdentity(); float fovy = 60.0f; float aspect = 1.0f; float zNear = 0.1f; float zFar = 100.0f; float ymax = zNear * tan( fovy * M_PI / 360.0f ); float ymin = -ymax; float xmin = ymin * aspect; float xmax = ymax * aspect; // swap top and bottom to have OpenGL coordinate system match X system glFrustum( xmin, xmax, ymin, ymax, zNear, zFar ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); float scaleFactor = 1.1 * tan( fovy * M_PI / 360.0f )/ymax; glTranslatef( xmin*scaleFactor, ymax*scaleFactor, -1.1 ); glScalef( (xmax-xmin)*scaleFactor/displayWidth(), -(ymax-ymin)*scaleFactor/displayHeight(), 0.001 ); if( checkGLError( "Init" )) { kError( 1212 ) << "OpenGL compositing setup failed"; return; // error } // Do self-check immediatelly during compositing setup only when it's not KWin startup // at the same time (in other words, only when activating compositing using the kcm). // Currently selfcheck causes bad flicker (due to X mapping the overlay window // for too long?) which looks bad during KDE startup. if( !initting ) { if( !selfCheck()) return; selfCheckDone = true; } kDebug( 1212 ) << "DB:" << db << ", TFP:" << tfp_mode << ", SHM:" << shm_mode << ", Direct:" << bool( glXIsDirect( display(), ctxbuffer )) << endl; init_ok = true; } SceneOpenGL::~SceneOpenGL() { if( !init_ok ) { // TODO this probably needs to clean up whatever has been created until the failure wspace->destroyOverlay(); return; } foreach( Window* w, windows ) delete w; // do cleanup after initBuffer() glXMakeCurrent( display(), None, NULL ); glXDestroyContext( display(), ctxbuffer ); if( wspace->overlayWindow()) { if( hasGLXVersion( 1, 3 )) glXDestroyWindow( display(), glxbuffer ); XDestroyWindow( display(), buffer ); wspace->destroyOverlay(); } else { glXDestroyPixmap( display(), glxbuffer ); XFreeGC( display(), gcroot ); XFreePixmap( display(), buffer ); } if( shm_mode ) cleanupShm(); if( !tfp_mode && !shm_mode ) { if( last_pixmap != None ) glXDestroyPixmap( display(), last_pixmap ); glXDestroyContext( display(), ctxdrawable ); } SceneOpenGL::EffectFrame::cleanup(); checkGLError( "Cleanup" ); } bool SceneOpenGL::initFailed() const { return !init_ok; } bool SceneOpenGL::selectMode() { // select mode - try TFP first, then SHM, otherwise fallback mode shm_mode = false; tfp_mode = false; if( options->glMode == Options::GLTFP ) { if( initTfp()) tfp_mode = true; else if( initShm()) shm_mode = true; } else if( options->glMode == Options::GLSHM ) { if( initShm()) shm_mode = true; else if( initTfp()) tfp_mode = true; } if( !initDrawableConfigs()) return false; return true; } bool SceneOpenGL::initTfp() { if( glXBindTexImageEXT == NULL || glXReleaseTexImageEXT == NULL ) return false; return true; } bool SceneOpenGL::initShm() { #ifdef HAVE_XSHM int major, minor; Bool pixmaps; if( !XShmQueryVersion( display(), &major, &minor, &pixmaps ) || !pixmaps ) return false; if( XShmPixmapFormat( display()) != ZPixmap ) return false; const int MAXSIZE = 4096 * 2048 * 4; // TODO check there are not larger windows // TODO check that bytes_per_line doesn't involve padding? shm.readOnly = False; shm.shmid = shmget( IPC_PRIVATE, MAXSIZE, IPC_CREAT | 0600 ); if( shm.shmid < 0 ) return false; shm.shmaddr = ( char* ) shmat( shm.shmid, NULL, 0 ); if( shm.shmaddr == ( void * ) -1 ) { shmctl( shm.shmid, IPC_RMID, 0 ); return false; } #ifdef __linux__ // mark as deleted to automatically free the memory in case // of a crash (but this doesn't work e.g. on Solaris ... oh well) shmctl( shm.shmid, IPC_RMID, 0 ); #endif KXErrorHandler errs; XShmAttach( display(), &shm ); if( errs.error( true )) { #ifndef __linux__ shmctl( shm.shmid, IPC_RMID, 0 ); #endif shmdt( shm.shmaddr ); return false; } return true; #else return false; #endif } void SceneOpenGL::cleanupShm() { #ifdef HAVE_XSHM shmdt( shm.shmaddr ); #ifndef __linux__ shmctl( shm.shmid, IPC_RMID, 0 ); #endif #endif } bool SceneOpenGL::initRenderingContext() { bool direct_rendering = options->glDirect; if( !tfp_mode && !shm_mode ) direct_rendering = false; // fallback doesn't seem to work with direct rendering KXErrorHandler errs1; ctxbuffer = glXCreateNewContext( display(), fbcbuffer, GLX_RGBA_TYPE, NULL, direct_rendering ? GL_TRUE : GL_FALSE ); bool failed = ( ctxbuffer == NULL || !glXMakeCurrent( display(), glxbuffer, ctxbuffer )); if( errs1.error( true )) // always check for error( having it all in one if() could skip failed = true; // it due to evaluation short-circuiting if( failed ) { if( !direct_rendering ) { kDebug( 1212 ).nospace() << "Couldn't initialize rendering context (" << KXErrorHandler::errorMessage( errs1.errorEvent()) << ")"; return false; } glXMakeCurrent( display(), None, NULL ); if( ctxbuffer != NULL ) glXDestroyContext( display(), ctxbuffer ); direct_rendering = false; // try again KXErrorHandler errs2; ctxbuffer = glXCreateNewContext( display(), fbcbuffer, GLX_RGBA_TYPE, NULL, GL_FALSE ); bool failed = ( ctxbuffer == NULL || !glXMakeCurrent( display(), glxbuffer, ctxbuffer )); if( errs2.error( true )) failed = true; if( failed ) { kDebug( 1212 ).nospace() << "Couldn't initialize rendering context (" << KXErrorHandler::errorMessage( errs2.errorEvent()) << ")"; return false; } } if( !tfp_mode && !shm_mode ) { ctxdrawable = glXCreateNewContext( display(), fbcdrawableinfo[ QX11Info::appDepth() ].fbconfig, GLX_RGBA_TYPE, ctxbuffer, direct_rendering ? GL_TRUE : GL_FALSE ); } return true; } // create destination buffer bool SceneOpenGL::initBuffer() { if( !initBufferConfigs()) return false; if( fbcbuffer_db != NULL && wspace->createOverlay()) { // we have overlay, try to create double-buffered window in it fbcbuffer = fbcbuffer_db; XVisualInfo* visual = glXGetVisualFromFBConfig( display(), fbcbuffer ); XSetWindowAttributes attrs; attrs.colormap = XCreateColormap( display(), rootWindow(), visual->visual, AllocNone ); buffer = XCreateWindow( display(), wspace->overlayWindow(), 0, 0, displayWidth(), displayHeight(), 0, visual->depth, InputOutput, visual->visual, CWColormap, &attrs ); if( hasGLXVersion( 1, 3 )) glxbuffer = glXCreateWindow( display(), fbcbuffer, buffer, NULL ); else glxbuffer = buffer; wspace->setupOverlay( buffer ); db = true; XFree( visual ); } else if( fbcbuffer_nondb != NULL ) { // cannot get any double-buffered drawable, will double-buffer using a pixmap fbcbuffer = fbcbuffer_nondb; XVisualInfo* visual = glXGetVisualFromFBConfig( display(), fbcbuffer ); XGCValues gcattr; gcattr.subwindow_mode = IncludeInferiors; gcroot = XCreateGC( display(), rootWindow(), GCSubwindowMode, &gcattr ); buffer = XCreatePixmap( display(), rootWindow(), displayWidth(), displayHeight(), visual->depth ); glxbuffer = glXCreatePixmap( display(), fbcbuffer, buffer, NULL ); db = false; XFree( visual ); } else { kError( 1212 ) << "Couldn't create output buffer (failed to create overlay window?) !"; return false; // error } int vis_buffer; glXGetFBConfigAttrib( display(), fbcbuffer, GLX_VISUAL_ID, &vis_buffer ); XVisualInfo* visinfo_buffer = glXGetVisualFromFBConfig( display(), fbcbuffer ); kDebug( 1212 ) << "Buffer visual (depth " << visinfo_buffer->depth << "): 0x" << QString::number( vis_buffer, 16 ); XFree( visinfo_buffer ); return true; } // choose the best configs for the destination buffer bool SceneOpenGL::initBufferConfigs() { int cnt; GLXFBConfig *fbconfigs = glXGetFBConfigs( display(), DefaultScreen( display() ), &cnt ); fbcbuffer_db = NULL; fbcbuffer_nondb = NULL; for( int i = 0; i < 2; i++ ) { int back, stencil, depth, caveat, alpha; back = i > 0 ? INT_MAX : 1; stencil = INT_MAX; depth = INT_MAX; caveat = INT_MAX; alpha = 0; for( int j = 0; j < cnt; j++ ) { XVisualInfo *vi; int visual_depth; vi = glXGetVisualFromFBConfig( display(), fbconfigs[ j ] ); if( vi == NULL ) continue; visual_depth = vi->depth; XFree( vi ); if( visual_depth != DefaultDepth( display(), DefaultScreen( display()))) continue; int value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_ALPHA_SIZE, &alpha ); glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_BUFFER_SIZE, &value ); if( value != visual_depth && ( value - alpha ) != visual_depth ) continue; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_RENDER_TYPE, &value ); if( !( value & GLX_RGBA_BIT )) continue; int back_value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_DOUBLEBUFFER, &back_value ); if( i > 0 ) { if( back_value > back ) continue; } else { if( back_value < back ) continue; } int stencil_value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_STENCIL_SIZE, &stencil_value ); if( stencil_value > stencil ) continue; int depth_value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_DEPTH_SIZE, &depth_value ); if( depth_value > depth ) continue; int caveat_value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_CONFIG_CAVEAT, &caveat_value ); if( caveat_value > caveat ) continue; back = back_value; stencil = stencil_value; depth = depth_value; caveat = caveat_value; if( i > 0 ) fbcbuffer_nondb = fbconfigs[ j ]; else fbcbuffer_db = fbconfigs[ j ]; } } if( cnt ) XFree( fbconfigs ); if( fbcbuffer_db == NULL && fbcbuffer_nondb == NULL ) { kError( 1212 ) << "Couldn't find framebuffer configuration for buffer!"; return false; } for( int i = 0; i <= 32; i++ ) { if( fbcdrawableinfo[ i ].fbconfig == NULL ) continue; int vis_drawable = 0; glXGetFBConfigAttrib( display(), fbcdrawableinfo[ i ].fbconfig, GLX_VISUAL_ID, &vis_drawable ); kDebug( 1212 ) << "Drawable visual (depth " << i << "): 0x" << QString::number( vis_drawable, 16 ); } return true; } // make a list of the best configs for windows by depth bool SceneOpenGL::initDrawableConfigs() { int cnt; GLXFBConfig *fbconfigs = glXGetFBConfigs( display(), DefaultScreen( display() ), &cnt ); for( int i = 0; i <= 32; i++ ) { int back, stencil, depth, caveat, alpha, mipmap, rgba; back = INT_MAX; stencil = INT_MAX; depth = INT_MAX; caveat = INT_MAX; mipmap = 0; rgba = 0; fbcdrawableinfo[ i ].fbconfig = NULL; fbcdrawableinfo[ i ].bind_texture_format = 0; fbcdrawableinfo[ i ].y_inverted = 0; fbcdrawableinfo[ i ].mipmap = 0; for( int j = 0; j < cnt; j++ ) { XVisualInfo *vi; int visual_depth; vi = glXGetVisualFromFBConfig( display(), fbconfigs[ j ] ); if( vi == NULL ) continue; visual_depth = vi->depth; XFree( vi ); if( visual_depth != i ) continue; int value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_ALPHA_SIZE, &alpha ); glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_BUFFER_SIZE, &value ); if( value != i && ( value - alpha ) != i ) continue; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_RENDER_TYPE, &value ); if( !( value & GLX_RGBA_BIT )) continue; if( tfp_mode ) { value = 0; if( i == 32 ) { glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_BIND_TO_TEXTURE_RGBA_EXT, &value ); if( value ) { // TODO I think this should be set only after the config passes all tests rgba = 1; fbcdrawableinfo[ i ].bind_texture_format = GLX_TEXTURE_FORMAT_RGBA_EXT; } } if( !value ) { if( rgba ) continue; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_BIND_TO_TEXTURE_RGB_EXT, &value ); if( !value ) continue; fbcdrawableinfo[ i ].bind_texture_format = GLX_TEXTURE_FORMAT_RGB_EXT; } } int back_value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_DOUBLEBUFFER, &back_value ); if( back_value > back ) continue; int stencil_value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_STENCIL_SIZE, &stencil_value ); if( stencil_value > stencil ) continue; int depth_value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_DEPTH_SIZE, &depth_value ); if( depth_value > depth ) continue; int mipmap_value = -1; if( tfp_mode && GLTexture::framebufferObjectSupported()) { glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_BIND_TO_MIPMAP_TEXTURE_EXT, &mipmap_value ); if( mipmap_value < mipmap ) continue; } int caveat_value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_CONFIG_CAVEAT, &caveat_value ); if( caveat_value > caveat ) continue; // ok, config passed all tests, it's the best one so far fbcdrawableinfo[ i ].fbconfig = fbconfigs[ j ]; caveat = caveat_value; back = back_value; stencil = stencil_value; depth = depth_value; mipmap = mipmap_value; glXGetFBConfigAttrib( display(), fbconfigs[ j ], GLX_Y_INVERTED_EXT, &value ); fbcdrawableinfo[ i ].y_inverted = value; fbcdrawableinfo[ i ].mipmap = mipmap; } } if( cnt ) XFree( fbconfigs ); if( fbcdrawableinfo[ DefaultDepth( display(), DefaultScreen( display())) ].fbconfig == NULL ) { kError( 1212 ) << "Couldn't find framebuffer configuration for default depth!"; return false; } if( fbcdrawableinfo[ 32 ].fbconfig == NULL ) { kError( 1212 ) << "Couldn't find framebuffer configuration for depth 32 (no ARGB GLX visual)!"; return false; } return true; } // Test if compositing actually _really_ works, by creating a texture from a testing // window, drawing it on the screen, reading the contents back and comparing. This // should test whether compositing really works. // This function does the whole selfcheck, it can be done also in two parts // during actual drawing (to avoid flicker, see selfCheck() call from the ctor). bool SceneOpenGL::selfCheck() { QRegion reg = selfCheckRegion(); if( wspace->overlayWindow()) { // avoid covering the whole screen too soon wspace->setOverlayShape( reg ); wspace->showOverlay(); } selfCheckSetup(); flushBuffer( PAINT_SCREEN_REGION, reg ); bool ok = selfCheckFinish(); if( wspace->overlayWindow()) wspace->hideOverlay(); return ok; } void SceneOpenGL::selfCheckSetup() { KXErrorHandler err; QImage img( selfCheckWidth(), selfCheckHeight(), QImage::Format_RGB32 ); img.setPixel( 0, 0, QColor( Qt::red ).rgb()); img.setPixel( 1, 0, QColor( Qt::green ).rgb()); img.setPixel( 2, 0, QColor( Qt::blue ).rgb()); img.setPixel( 0, 1, QColor( Qt::white ).rgb()); img.setPixel( 1, 1, QColor( Qt::black ).rgb()); img.setPixel( 2, 1, QColor( Qt::white ).rgb()); QPixmap pix = QPixmap::fromImage( img ); foreach( const QPoint& p, selfCheckPoints()) { XSetWindowAttributes wa; wa.override_redirect = True; ::Window window = XCreateWindow( display(), rootWindow(), 0, 0, selfCheckWidth(), selfCheckHeight(), 0, QX11Info::appDepth(), CopyFromParent, CopyFromParent, CWOverrideRedirect, &wa ); XSetWindowBackgroundPixmap( display(), window, pix.handle()); XClearWindow( display(), window ); XMapWindow( display(), window ); // move the window one down to where the result will be rendered too, just in case // the render would fail completely and eventual check would try to read this window's contents XMoveWindow( display(), window, p.x() + 1, p.y()); XCompositeRedirectWindow( display(), window, CompositeRedirectAutomatic ); Pixmap wpix = XCompositeNameWindowPixmap( display(), window ); glXWaitX(); Texture texture; texture.load( wpix, QSize( selfCheckWidth(), selfCheckHeight()), QX11Info::appDepth()); texture.bind(); QRect rect( p.x(), p.y(), selfCheckWidth(), selfCheckHeight()); texture.render( infiniteRegion(), rect ); texture.unbind(); glXWaitGL(); XFreePixmap( display(), wpix ); XDestroyWindow( display(), window ); } err.error( true ); // just sync and discard } bool SceneOpenGL::selfCheckFinish() { glXWaitGL(); KXErrorHandler err; bool ok = true; foreach( const QPoint& p, selfCheckPoints()) { QPixmap pix = QPixmap::grabWindow( rootWindow(), p.x(), p.y(), selfCheckWidth(), selfCheckHeight()); QImage img = pix.toImage(); // kDebug(1212) << "P:" << QColor( img.pixel( 0, 0 )).name(); // kDebug(1212) << "P:" << QColor( img.pixel( 1, 0 )).name(); // kDebug(1212) << "P:" << QColor( img.pixel( 2, 0 )).name(); // kDebug(1212) << "P:" << QColor( img.pixel( 0, 1 )).name(); // kDebug(1212) << "P:" << QColor( img.pixel( 1, 1 )).name(); // kDebug(1212) << "P:" << QColor( img.pixel( 2, 1 )).name(); if( img.pixel( 0, 0 ) != QColor( Qt::red ).rgb() || img.pixel( 1, 0 ) != QColor( Qt::green ).rgb() || img.pixel( 2, 0 ) != QColor( Qt::blue ).rgb() || img.pixel( 0, 1 ) != QColor( Qt::white ).rgb() || img.pixel( 1, 1 ) != QColor( Qt::black ).rgb() || img.pixel( 2, 1 ) != QColor( Qt::white ).rgb()) { kError( 1212 ) << "OpenGL compositing self-check failed, disabling compositing."; ok = false; break; } } if( err.error( true )) ok = false; if( ok ) kDebug( 1212 ) << "OpenGL compositing self-check passed."; if( !ok && options->disableCompositingChecks ) { kWarning( 1212 ) << "Compositing checks disabled, proceeding regardless of self-check failure."; return true; } return ok; } // the entry function for painting void SceneOpenGL::paint( QRegion damage, ToplevelList toplevels ) { foreach( Toplevel* c, toplevels ) { assert( windows.contains( c )); stacking_order.append( windows[ c ] ); } grabXServer(); glXWaitX(); glPushMatrix(); int mask = 0; #ifdef CHECK_GL_ERROR checkGLError( "Paint1" ); #endif paintScreen( &mask, &damage ); // call generic implementation #ifdef CHECK_GL_ERROR checkGLError( "Paint2" ); #endif glPopMatrix(); ungrabXServer(); // ungrab before flushBuffer(), it may wait for vsync if( wspace->overlayWindow()) // show the window only after the first pass, since wspace->showOverlay(); // that pass may take long if( !selfCheckDone ) { selfCheckSetup(); damage |= selfCheckRegion(); } flushBuffer( mask, damage ); if( !selfCheckDone ) { if( !selfCheckFinish()) QTimer::singleShot( 0, Workspace::self(), SLOT( fallbackToXRenderCompositing())); selfCheckDone = true; } // do cleanup stacking_order.clear(); checkGLError( "PostPaint" ); } // wait for vblank signal before painting void SceneOpenGL::waitSync() { // NOTE that vsync has no effect with indirect rendering if( waitSyncAvailable()) { unsigned int sync; glFlush(); glXGetVideoSync( &sync ); glXWaitVideoSync( 2, ( sync + 1 ) % 2, &sync ); } } // actually paint to the screen (double-buffer swap or copy from pixmap buffer) void SceneOpenGL::flushBuffer( int mask, QRegion damage ) { if( db ) { if( mask & PAINT_SCREEN_REGION ) { waitSync(); if( glXCopySubBuffer ) { foreach( const QRect &r, damage.rects()) { // convert to OpenGL coordinates int y = displayHeight() - r.y() - r.height(); glXCopySubBuffer( display(), glxbuffer, r.x(), y, r.width(), r.height()); } } else { // no idea why glScissor() is used, but Compiz has it and it doesn't seem to hurt glEnable( GL_SCISSOR_TEST ); glDrawBuffer( GL_FRONT ); int xpos = 0; int ypos = 0; foreach( const QRect &r, damage.rects()) { // convert to OpenGL coordinates int y = displayHeight() - r.y() - r.height(); // Move raster position relatively using glBitmap() rather // than using glRasterPos2f() - the latter causes drawing // artefacts at the bottom screen edge with some gfx cards // glRasterPos2f( r.x(), r.y() + r.height()); glBitmap( 0, 0, 0, 0, r.x() - xpos, y - ypos, NULL ); xpos = r.x(); ypos = y; glScissor( r.x(), y, r.width(), r.height()); glCopyPixels( r.x(), y, r.width(), r.height(), GL_COLOR ); } glBitmap( 0, 0, 0, 0, -xpos, -ypos, NULL ); // move position back to 0,0 glDrawBuffer( GL_BACK ); glDisable( GL_SCISSOR_TEST ); } } else { waitSync(); glXSwapBuffers( display(), glxbuffer ); } glXWaitGL(); XFlush( display()); } else { glFlush(); glXWaitGL(); waitSync(); if( mask & PAINT_SCREEN_REGION ) foreach( const QRect &r, damage.rects()) XCopyArea( display(), buffer, rootWindow(), gcroot, r.x(), r.y(), r.width(), r.height(), r.x(), r.y()); else XCopyArea( display(), buffer, rootWindow(), gcroot, 0, 0, displayWidth(), displayHeight(), 0, 0 ); XFlush( display()); } } void SceneOpenGL::paintGenericScreen( int mask, ScreenPaintData data ) { if( mask & PAINT_SCREEN_TRANSFORMED ) { // apply screen transformations glPushMatrix(); glTranslatef( data.xTranslate, data.yTranslate, data.zTranslate ); if( data.rotation ) { // translate to rotation point, rotate, translate back glTranslatef( data.rotation->xRotationPoint, data.rotation->yRotationPoint, data.rotation->zRotationPoint ); float xAxis = 0.0; float yAxis = 0.0; float zAxis = 0.0; switch( data.rotation->axis ) { case RotationData::XAxis: xAxis = 1.0; break; case RotationData::YAxis: yAxis = 1.0; break; case RotationData::ZAxis: zAxis = 1.0; break; } glRotatef( data.rotation->angle, xAxis, yAxis, zAxis ); glTranslatef( -data.rotation->xRotationPoint, -data.rotation->yRotationPoint, -data.rotation->zRotationPoint ); } glScalef( data.xScale, data.yScale, data.zScale ); } Scene::paintGenericScreen( mask, data ); if( mask & PAINT_SCREEN_TRANSFORMED ) glPopMatrix(); } void SceneOpenGL::paintBackground( QRegion region ) { PaintClipper pc( region ); if( !PaintClipper::clip()) { glPushAttrib( GL_COLOR_BUFFER_BIT ); glClearColor( 0, 0, 0, 1 ); // black glClear( GL_COLOR_BUFFER_BIT ); glPopAttrib(); return; } if( pc.clip() && pc.paintArea().isEmpty()) return; // no background to paint glPushAttrib( GL_CURRENT_BIT ); glColor4f( 0, 0, 0, 1 ); // black for( PaintClipper::Iterator iterator; !iterator.isDone(); iterator.next()) { glBegin( GL_QUADS ); QRect r = iterator.boundingRect(); glVertex2i( r.x(), r.y()); glVertex2i( r.x() + r.width(), r.y()); glVertex2i( r.x() + r.width(), r.y() + r.height()); glVertex2i( r.x(), r.y() + r.height()); glEnd(); } glPopAttrib(); } void SceneOpenGL::windowAdded( Toplevel* c ) { assert( !windows.contains( c )); windows[ c ] = new Window( c ); c->effectWindow()->setSceneWindow( windows[ c ]); } void SceneOpenGL::windowClosed( Toplevel* c, Deleted* deleted ) { assert( windows.contains( c )); if( deleted != NULL ) { // replace c with deleted Window* w = windows.take( c ); w->updateToplevel( deleted ); windows[ deleted ] = w; } else { delete windows.take( c ); c->effectWindow()->setSceneWindow( NULL ); } } void SceneOpenGL::windowDeleted( Deleted* c ) { assert( windows.contains( c )); delete windows.take( c ); c->effectWindow()->setSceneWindow( NULL ); } void SceneOpenGL::windowGeometryShapeChanged( Toplevel* c ) { if( !windows.contains( c )) // this is ok, shape is not valid return; // by default Window* w = windows[ c ]; w->discardShape(); w->checkTextureSize(); } void SceneOpenGL::windowOpacityChanged( Toplevel* ) { #if 0 // not really needed, windows are painted on every repaint // and opacity is used when applying texture, not when // creating it if( !windows.contains( c )) // this is ok, texture is created return; // on demand Window* w = windows[ c ]; w->discardTexture(); #endif } //**************************************** // SceneOpenGL::Texture //**************************************** SceneOpenGL::Texture::Texture() : GLTexture() { init(); } SceneOpenGL::Texture::Texture( const Pixmap& pix, const QSize& size, int depth ) : GLTexture() { init(); load( pix, size, depth ); } SceneOpenGL::Texture::~Texture() { discard(); } void SceneOpenGL::Texture::init() { bound_glxpixmap = None; } void SceneOpenGL::Texture::createTexture() { glGenTextures( 1, &mTexture ); } void SceneOpenGL::Texture::discard() { if( mTexture != None ) release(); GLTexture::discard(); } void SceneOpenGL::Texture::release() { if( tfp_mode && bound_glxpixmap != None ) { if( !options->glStrictBinding ) glXReleaseTexImageEXT( display(), bound_glxpixmap, GLX_FRONT_LEFT_EXT ); glXDestroyPixmap( display(), bound_glxpixmap ); bound_glxpixmap = None; } } void SceneOpenGL::Texture::findTarget() { unsigned int new_target = 0; if( tfp_mode && glXQueryDrawable && bound_glxpixmap != None ) glXQueryDrawable( display(), bound_glxpixmap, GLX_TEXTURE_TARGET_EXT, &new_target ); // Hack for XGL - this should not be a fallback for glXQueryDrawable() but instead the case // when glXQueryDrawable is not available. However this call fails with XGL, unless KWin // is compiled statically with the libGL that Compiz is built against (without which neither // Compiz works with XGL). Falling back to doing this manually makes this work. if( new_target == 0 ) { if( NPOTTextureSupported() || ( isPowerOfTwo( mSize.width()) && isPowerOfTwo( mSize.height()))) new_target = GLX_TEXTURE_2D_EXT; else new_target = GLX_TEXTURE_RECTANGLE_EXT; } switch( new_target ) { case GLX_TEXTURE_2D_EXT: mTarget = GL_TEXTURE_2D; mScale.setWidth( 1.0f / mSize.width()); mScale.setHeight( 1.0f / mSize.height()); break; case GLX_TEXTURE_RECTANGLE_EXT: mTarget = GL_TEXTURE_RECTANGLE_ARB; mScale.setWidth( 1.0f ); mScale.setHeight( 1.0f ); break; default: abort(); } } QRegion SceneOpenGL::Texture::optimizeBindDamage( const QRegion& reg, int limit ) { if( reg.rects().count() <= 1 ) return reg; // try to reduce the number of rects, as especially with SHM mode every rect // causes X roundtrip, even for very small areas - so, when the size difference // between all the areas and the bounding rectangle is small, simply use // only the bounding rectangle int size = 0; foreach( const QRect &r, reg.rects()) size += r.width() * r.height(); if( reg.boundingRect().width() * reg.boundingRect().height() - size < limit ) return reg.boundingRect(); return reg; } bool SceneOpenGL::Texture::load( const Pixmap& pix, const QSize& size, int depth, QRegion region ) { #ifdef CHECK_GL_ERROR checkGLError( "TextureLoad1" ); #endif if( pix == None || size.isEmpty() || depth < 1 ) return false; if( tfp_mode ) { if( fbcdrawableinfo[ depth ].fbconfig == NULL ) { kDebug( 1212 ) << "No framebuffer configuration for depth " << depth << "; not binding pixmap" << endl; return false; } } mSize = size; if( mTexture == None || !region.isEmpty()) { // new texture, or texture contents changed; mipmaps now invalid setDirty(); } #ifdef CHECK_GL_ERROR checkGLError( "TextureLoad2" ); #endif if( tfp_mode ) { // tfp mode, simply bind the pixmap to texture if( mTexture == None ) createTexture(); // when the pixmap is bound to the texture, they share the same data, so the texture // updates automatically - no need to do anything in such case if( bound_glxpixmap != None ) glBindTexture( mTarget, mTexture ); else { int attrs[] = { GLX_TEXTURE_FORMAT_EXT, fbcdrawableinfo[ depth ].bind_texture_format, GLX_MIPMAP_TEXTURE_EXT, fbcdrawableinfo[ depth ].mipmap, None }; // the GLXPixmap will reference the X pixmap, so it will be freed automatically // when no longer needed bound_glxpixmap = glXCreatePixmap( display(), fbcdrawableinfo[ depth ].fbconfig, pix, attrs ); #ifdef CHECK_GL_ERROR checkGLError( "TextureLoadTFP1" ); #endif findTarget(); y_inverted = fbcdrawableinfo[ depth ].y_inverted ? true : false; can_use_mipmaps = fbcdrawableinfo[ depth ].mipmap ? true : false; glBindTexture( mTarget, mTexture ); #ifdef CHECK_GL_ERROR checkGLError( "TextureLoadTFP2" ); #endif if( !options->glStrictBinding ) glXBindTexImageEXT( display(), bound_glxpixmap, GLX_FRONT_LEFT_EXT, NULL ); } } else if( shm_mode ) { // copy pixmap contents to a texture via shared memory #ifdef HAVE_XSHM GLenum pixfmt, type; if( depth >= 24 ) { pixfmt = GL_BGRA; type = GL_UNSIGNED_BYTE; } else { // depth 16 pixfmt = GL_RGB; type = GL_UNSIGNED_SHORT_5_6_5; } findTarget(); #ifdef CHECK_GL_ERROR checkGLError( "TextureLoadSHM1" ); #endif if( mTexture == None ) { createTexture(); glBindTexture( mTarget, mTexture ); y_inverted = false; glTexImage2D( mTarget, 0, depth == 32 ? GL_RGBA : GL_RGB, mSize.width(), mSize.height(), 0, pixfmt, type, NULL ); } else glBindTexture( mTarget, mTexture ); if( !region.isEmpty()) { XGCValues xgcv; xgcv.graphics_exposures = False; xgcv.subwindow_mode = IncludeInferiors; GC gc = XCreateGC( display(), pix, GCGraphicsExposures | GCSubwindowMode, &xgcv ); Pixmap p = XShmCreatePixmap( display(), rootWindow(), shm.shmaddr, &shm, mSize.width(), mSize.height(), depth ); QRegion damage = optimizeBindDamage( region, 100 * 100 ); glPixelStorei( GL_UNPACK_ROW_LENGTH, mSize.width()); foreach( const QRect &r, damage.rects()) { // TODO for small areas it might be faster to not use SHM to avoid the XSync() XCopyArea( display(), pix, p, gc, r.x(), r.y(), r.width(), r.height(), 0, 0 ); glXWaitX(); glTexSubImage2D( mTarget, 0, r.x(), r.y(), r.width(), r.height(), pixfmt, type, shm.shmaddr ); glXWaitGL(); } glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 ); XFreePixmap( display(), p ); XFreeGC( display(), gc ); } #ifdef CHECK_GL_ERROR checkGLError( "TextureLoadSHM2" ); #endif y_inverted = true; can_use_mipmaps = true; #endif } else { // fallback, copy pixmap contents to a texture // note that if depth is not QX11Info::appDepth(), this may // not work (however, it does seem to work with nvidia) findTarget(); GLXDrawable pixmap = glXCreatePixmap( display(), fbcdrawableinfo[ QX11Info::appDepth() ].fbconfig, pix, NULL ); glXMakeCurrent( display(), pixmap, ctxdrawable ); if( last_pixmap != None ) glXDestroyPixmap( display(), last_pixmap ); // workaround for ATI - it leaks/crashes when the pixmap is destroyed immediately // here (http://lists.kde.org/?l=kwin&m=116353772208535&w=2) last_pixmap = pixmap; glReadBuffer( GL_FRONT ); glDrawBuffer( GL_FRONT ); if( mTexture == None ) { createTexture(); glBindTexture( mTarget, mTexture ); y_inverted = false; glCopyTexImage2D( mTarget, 0, depth == 32 ? GL_RGBA : GL_RGB, 0, 0, mSize.width(), mSize.height(), 0 ); } else { glBindTexture( mTarget, mTexture ); QRegion damage = optimizeBindDamage( region, 30 * 30 ); foreach( const QRect &r, damage.rects()) { // convert to OpenGL coordinates (this is mapping // the pixmap to a texture, this is not affected // by using glOrtho() for the OpenGL scene) int gly = mSize.height() - r.y() - r.height(); glCopyTexSubImage2D( mTarget, 0, r.x(), gly, r.x(), gly, r.width(), r.height()); } } glXWaitGL(); if( db ) glDrawBuffer( GL_BACK ); glXMakeCurrent( display(), glxbuffer, ctxbuffer ); glBindTexture( mTarget, mTexture ); y_inverted = false; can_use_mipmaps = true; } #ifdef CHECK_GL_ERROR checkGLError( "TextureLoad0" ); #endif return true; } bool SceneOpenGL::Texture::load( const Pixmap& pix, const QSize& size, int depth ) { return load( pix, size, depth, QRegion( 0, 0, size.width(), size.height())); } bool SceneOpenGL::Texture::load( const QImage& image, GLenum target ) { if( image.isNull()) return false; return load( QPixmap::fromImage( image ), target ); } bool SceneOpenGL::Texture::load( const QPixmap& pixmap, GLenum target ) { Q_UNUSED( target ); // SceneOpenGL::Texture::findTarget() detects the target if( pixmap.isNull()) return false; return load( pixmap.handle(), pixmap.size(), pixmap.depth()); } void SceneOpenGL::Texture::bind() { glEnable( mTarget ); glBindTexture( mTarget, mTexture ); if( tfp_mode && options->glStrictBinding ) { assert( bound_glxpixmap != None ); glXBindTexImageEXT( display(), bound_glxpixmap, GLX_FRONT_LEFT_EXT, NULL ); } enableFilter(); if( hasGLVersion( 1, 4, 0 )) { // Lod bias makes the trilinear-filtered texture look a bit sharper glTexEnvf( GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, -1.0f ); } } void SceneOpenGL::Texture::unbind() { if( hasGLVersion( 1, 4, 0 )) { glTexEnvf( GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, 0.0f ); } if( tfp_mode && options->glStrictBinding ) { assert( bound_glxpixmap != None ); glBindTexture( mTarget, mTexture ); glXReleaseTexImageEXT( display(), bound_glxpixmap, GLX_FRONT_LEFT_EXT ); } GLTexture::unbind(); } //**************************************** // SceneOpenGL::Window //**************************************** SceneOpenGL::Window::Window( Toplevel* c ) : Scene::Window( c ) , texture() , topTexture() , leftTexture() , rightTexture() , bottomTexture() { } SceneOpenGL::Window::~Window() { discardTexture(); } // Bind the window pixmap to an OpenGL texture. bool SceneOpenGL::Window::bindTexture() { if( texture.texture() != None && toplevel->damage().isEmpty()) { // texture doesn't need updating, just bind it glBindTexture( texture.target(), texture.texture()); return true; } // Get the pixmap with the window contents Pixmap pix = toplevel->windowPixmap(); if( pix == None ) return false; bool success = texture.load( pix, toplevel->size(), toplevel->depth(), toplevel->damage()); if( success ) toplevel->resetDamage( QRect( toplevel->clientPos(), toplevel->clientSize() ) ); else kDebug( 1212 ) << "Failed to bind window"; return success; } void SceneOpenGL::Window::discardTexture() { texture.discard(); topTexture.discard(); leftTexture.discard(); rightTexture.discard(); bottomTexture.discard(); } // This call is used in SceneOpenGL::windowGeometryShapeChanged(), // which originally called discardTexture(), however this was causing performance // problems with the launch feedback icon - large number of texture rebinds. // Since the launch feedback icon does not resize, only changes shape, it // is not necessary to rebind the texture (with no strict binding), therefore // discard the texture only if size changes. void SceneOpenGL::Window::checkTextureSize() { if( texture.size() != size()) discardTexture(); } // when the window's composite pixmap is discarded, undo binding it to the texture void SceneOpenGL::Window::pixmapDiscarded() { texture.release(); } // paint the window void SceneOpenGL::Window::performPaint( int mask, QRegion region, WindowPaintData data ) { // check if there is something to paint (e.g. don't paint if the window // is only opaque and only PAINT_WINDOW_TRANSLUCENT is requested) /* HACK: It seems this causes painting glitches, disable temporarily bool opaque = isOpaque() && data.opacity == 1.0; if(( mask & PAINT_WINDOW_OPAQUE ) ^ ( mask & PAINT_WINDOW_TRANSLUCENT )) { // We are only painting either opaque OR translucent windows, not both if( mask & PAINT_WINDOW_OPAQUE && !opaque ) return; // Only painting opaque and window is translucent if( mask & PAINT_WINDOW_TRANSLUCENT && opaque ) return; // Only painting translucent and window is opaque }*/ // paint only requested areas if( region != infiniteRegion()) // avoid integer overflow region.translate( -x(), -y()); if( region.isEmpty()) return; if( !bindTexture()) return; glPushMatrix(); // set texture filter if( options->glSmoothScale != 0 ) // default to yes { if( mask & PAINT_WINDOW_TRANSFORMED ) filter = ImageFilterGood; else if( mask & PAINT_SCREEN_TRANSFORMED ) filter = ImageFilterGood; else filter = ImageFilterFast; } else filter = ImageFilterFast; if( filter == ImageFilterGood ) { // avoid unneeded mipmap generation by only using trilinear // filtering when it actually makes a difference, that is with // minification or changed vertices if( options->glSmoothScale == 2 && ( data.quads.smoothNeeded() || data.xScale < 1 || data.yScale < 1 )) { texture.setFilter( GL_LINEAR_MIPMAP_LINEAR ); } else texture.setFilter( GL_LINEAR ); } else texture.setFilter( GL_NEAREST ); // do required transformations int x = toplevel->x(); int y = toplevel->y(); double z = 0.0; if( mask & PAINT_WINDOW_TRANSFORMED ) { x += data.xTranslate; y += data.yTranslate; z += data.zTranslate; } glTranslatef( x, y, z ); if(( mask & PAINT_WINDOW_TRANSFORMED ) && ( data.xScale != 1 || data.yScale != 1 || data.zScale != 1 )) glScalef( data.xScale, data.yScale, data.zScale ); if(( mask & PAINT_WINDOW_TRANSFORMED ) && data.rotation ) { glTranslatef( data.rotation->xRotationPoint, data.rotation->yRotationPoint, data.rotation->zRotationPoint ); float xAxis = 0.0; float yAxis = 0.0; float zAxis = 0.0; switch( data.rotation->axis ) { case RotationData::XAxis: xAxis = 1.0; break; case RotationData::YAxis: yAxis = 1.0; break; case RotationData::ZAxis: zAxis = 1.0; break; } glRotatef( data.rotation->angle, xAxis, yAxis, zAxis ); glTranslatef( -data.rotation->xRotationPoint, -data.rotation->yRotationPoint, -data.rotation->zRotationPoint ); } region.translate( toplevel->x(), toplevel->y() ); // Back to screen coords WindowQuadList decoration = data.quads.select( WindowQuadDecoration ); // decorations Client *client = dynamic_cast(toplevel); Deleted *deleted = dynamic_cast(toplevel); if( client || deleted ) { bool noBorder = true; bool updateDeco = false; const QPixmap *left = NULL; const QPixmap *top = NULL; const QPixmap *right = NULL; const QPixmap *bottom = NULL; QRect topRect, leftRect, rightRect, bottomRect; if( client && !client->noBorder() ) { noBorder = false; updateDeco = client->decorationPixmapRequiresRepaint(); client->ensureDecorationPixmapsPainted(); client->layoutDecorationRects(leftRect, topRect, rightRect, bottomRect, Client::WindowRelative); left = client->leftDecoPixmap(); top = client->topDecoPixmap(); right = client->rightDecoPixmap(); bottom = client->bottomDecoPixmap(); } if( deleted && !deleted->noBorder() ) { noBorder = false; left = deleted->leftDecoPixmap(); top = deleted->topDecoPixmap(); right = deleted->rightDecoPixmap(); bottom = deleted->bottomDecoPixmap(); deleted->layoutDecorationRects(leftRect, topRect, rightRect, bottomRect); } if( !noBorder ) { WindowQuadList topList, leftList, rightList, bottomList; foreach( const WindowQuad& quad, decoration ) { if( topRect.contains( QPoint( quad.originalLeft(), quad.originalTop() ) ) ) { topList.append( quad ); continue; } if( bottomRect.contains( QPoint( quad.originalLeft(), quad.originalTop() ) ) ) { bottomList.append( quad ); continue; } if( leftRect.contains( QPoint( quad.originalLeft(), quad.originalTop() ) ) ) { leftList.append( quad ); continue; } if( rightRect.contains( QPoint( quad.originalLeft(), quad.originalTop() ) ) ) { rightList.append( quad ); continue; } } paintDecoration( top, DecorationTop, region, topRect, data, topList, updateDeco ); paintDecoration( left, DecorationLeft, region, leftRect, data, leftList, updateDeco ); paintDecoration( right, DecorationRight, region, rightRect, data, rightList, updateDeco ); paintDecoration( bottom, DecorationBottom, region, bottomRect, data, bottomList, updateDeco ); } } texture.bind(); texture.enableUnnormalizedTexCoords(); // paint the content if ( !(mask & PAINT_DECORATION_ONLY) ) { prepareStates( Content, data.opacity * data.contents_opacity, data.brightness, data.saturation, data.shader ); renderQuads( mask, region, data.quads.select( WindowQuadContents )); restoreStates( Content, data.opacity * data.contents_opacity, data.brightness, data.saturation, data.shader ); } texture.disableUnnormalizedTexCoords(); texture.unbind(); glPopMatrix(); } void SceneOpenGL::Window::paintDecoration( const QPixmap* decoration, TextureType decorationType, const QRegion& region, const QRect& rect, const WindowPaintData& data, const WindowQuadList& quads, bool updateDeco ) { if( quads.isEmpty()) return; SceneOpenGL::Texture* decorationTexture; switch( decorationType ) { case DecorationTop: decorationTexture = &topTexture; break; case DecorationLeft: decorationTexture = &leftTexture; break; case DecorationRight: decorationTexture = &rightTexture; break; case DecorationBottom: decorationTexture = &bottomTexture; break; default: return; } if( decorationTexture->texture() != None && !updateDeco ) { // texture doesn't need updating, just bind it glBindTexture( decorationTexture->target(), decorationTexture->texture()); } else if( !decoration->isNull() ) { bool success = decorationTexture->load( decoration->handle(), decoration->size(), decoration->depth() ); if( !success ) { kDebug( 1212 ) << "Failed to bind decoartion"; return; } } else return; if( filter == ImageFilterGood ) { // avoid unneeded mipmap generation by only using trilinear // filtering when it actually makes a difference, that is with // minification or changed vertices if( options->glSmoothScale == 2 && ( data.quads.smoothNeeded() || data.xScale < 1 || data.yScale < 1 )) { decorationTexture->setFilter( GL_LINEAR_MIPMAP_LINEAR ); } else decorationTexture->setFilter( GL_LINEAR ); } else decorationTexture->setFilter( GL_NEAREST ); decorationTexture->setWrapMode( GL_CLAMP_TO_EDGE ); decorationTexture->bind(); decorationTexture->enableUnnormalizedTexCoords(); prepareStates( decorationType, data.opacity * data.decoration_opacity, data.brightness, data.saturation, data.shader ); float* vertices; float* texcoords; makeDecorationArrays( &vertices, &texcoords, quads, rect ); if( data.shader ) { int texw = decoration->width(); int texh = decoration->height(); if( !GLTexture::NPOTTextureSupported() ) { kWarning( 1212 ) << "NPOT textures not supported, wasting some memory" ; texw = nearestPowerOfTwo(texw); texh = nearestPowerOfTwo(texh); } data.shader->setUniform("textureWidth", (float)texw); data.shader->setUniform("textureHeight", (float)texh); } renderGLGeometry( region, quads.count() * 4, vertices, texcoords, NULL, 2, 0 ); delete[] vertices; delete[] texcoords; restoreStates( decorationType, data.opacity * data.decoration_opacity, data.brightness, data.saturation, data.shader ); decorationTexture->disableUnnormalizedTexCoords(); decorationTexture->unbind(); } void SceneOpenGL::Window::makeDecorationArrays( float** vertices, float** texcoords, const WindowQuadList& quads, const QRect& rect ) const { *vertices = new float[ quads.count() * 4 * 2 ]; *texcoords = new float[ quads.count() * 4 * 2 ]; float* vpos = *vertices; float* tpos = *texcoords; foreach( const WindowQuad& quad, quads ) { *vpos++ = quad[ 0 ].x(); *vpos++ = quad[ 0 ].y(); *vpos++ = quad[ 1 ].x(); *vpos++ = quad[ 1 ].y(); *vpos++ = quad[ 2 ].x(); *vpos++ = quad[ 2 ].y(); *vpos++ = quad[ 3 ].x(); *vpos++ = quad[ 3 ].y(); *tpos++ = quad.originalLeft()-rect.x(); *tpos++ = quad.originalTop()-rect.y(); *tpos++ = quad.originalRight()-rect.x(); *tpos++ = quad.originalTop()-rect.y(); *tpos++ = quad.originalRight()-rect.x(); *tpos++ = quad.originalBottom()-rect.y(); *tpos++ = quad.originalLeft()-rect.x(); *tpos++ = quad.originalBottom()-rect.y(); } } void SceneOpenGL::Window::renderQuads( int, const QRegion& region, const WindowQuadList& quads ) { if( quads.isEmpty()) return; // Render geometry float* vertices; float* texcoords; quads.makeArrays( &vertices, &texcoords ); renderGLGeometry( region, quads.count() * 4, vertices, texcoords, NULL, 2, 0 ); delete[] vertices; delete[] texcoords; } void SceneOpenGL::Window::prepareStates( TextureType type, double opacity, double brightness, double saturation, GLShader* shader ) { if(shader) prepareShaderRenderStates( type, opacity, brightness, saturation, shader ); else prepareRenderStates( type, opacity, brightness, saturation ); } void SceneOpenGL::Window::prepareShaderRenderStates( TextureType type, double opacity, double brightness, double saturation, GLShader* shader ) { // setup blending of transparent windows glPushAttrib( GL_ENABLE_BIT ); bool opaque = isOpaque() && opacity == 1.0; if( type != Content ) opaque = false; if( !opaque ) { glEnable( GL_BLEND ); glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ); } shader->setUniform("opacity", (float)opacity); shader->setUniform("saturation", (float)saturation); shader->setUniform("brightness", (float)brightness); // setting texture width and heiht stored in shader // only set if it is set by an effect that is not negative float texw = shader->textureWidth(); if( texw >= 0.0f ) shader->setUniform("textureWidth", texw); float texh = shader->textureHeight(); if( texh >= 0.0f ) shader->setUniform("textureHeight", texh); } void SceneOpenGL::Window::prepareRenderStates( TextureType type, double opacity, double brightness, double saturation ) { Texture* tex; bool alpha = false; bool opaque = true; switch( type ) { case Content: tex = &texture; alpha = toplevel->hasAlpha(); opaque = isOpaque() && opacity == 1.0; break; case DecorationTop: tex = &topTexture; alpha = true; opaque = false; break; case DecorationLeft: tex = &leftTexture; alpha = true; opaque = false; break; case DecorationRight: tex = &rightTexture; alpha = true; opaque = false; break; case DecorationBottom: tex = &bottomTexture; alpha = true; opaque = false; break; default: return; } // setup blending of transparent windows glPushAttrib( GL_ENABLE_BIT ); if( !opaque ) { glEnable( GL_BLEND ); glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ); } if( saturation != 1.0 && tex->saturationSupported()) { // First we need to get the color from [0; 1] range to [0.5; 1] range glActiveTexture( GL_TEXTURE0 ); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_CONSTANT ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA ); const float scale_constant[] = { 1.0, 1.0, 1.0, 0.5}; glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, scale_constant ); tex->bind(); // Then we take dot product of the result of previous pass and // saturation_constant. This gives us completely unsaturated // (greyscale) image // Note that both operands have to be in range [0.5; 1] since opengl // automatically substracts 0.5 from them glActiveTexture( GL_TEXTURE1 ); float saturation_constant[] = { 0.5 + 0.5*0.30, 0.5 + 0.5*0.59, 0.5 + 0.5*0.11, saturation }; glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR ); glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, saturation_constant ); tex->bind(); // Finally we need to interpolate between the original image and the // greyscale image to get wanted level of saturation glActiveTexture( GL_TEXTURE2 ); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE0 ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_CONSTANT ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA ); glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, saturation_constant ); // Also replace alpha by primary color's alpha here glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA ); // And make primary color contain the wanted opacity glColor4f( opacity, opacity, opacity, opacity ); tex->bind(); if( alpha || brightness != 1.0f ) { glActiveTexture( GL_TEXTURE3 ); glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PRIMARY_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR ); // The color has to be multiplied by both opacity and brightness float opacityByBrightness = opacity * brightness; glColor4f( opacityByBrightness, opacityByBrightness, opacityByBrightness, opacity ); if( alpha ) { // Multiply original texture's alpha by our opacity glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE0 ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PRIMARY_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA ); } else { // Alpha will be taken from previous stage glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA ); } tex->bind(); } glActiveTexture(GL_TEXTURE0 ); } else if( opacity != 1.0 || brightness != 1.0 ) { // the window is additionally configured to have its opacity adjusted, // do it float opacityByBrightness = opacity * brightness; if( alpha) { glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); glColor4f( opacityByBrightness, opacityByBrightness, opacityByBrightness, opacity); } else { // Multiply color by brightness and replace alpha by opacity float constant[] = { opacityByBrightness, opacityByBrightness, opacityByBrightness, opacity }; glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_CONSTANT ); glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR ); glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_CONSTANT ); glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, constant ); } } else if( !alpha && opaque ) { float constant[] = { 1.0, 1.0, 1.0, 1.0 }; glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE ); glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE ); glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_CONSTANT ); glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, constant ); } } void SceneOpenGL::Window::restoreStates( TextureType type, double opacity, double brightness, double saturation, GLShader* shader ) { if(shader) restoreShaderRenderStates( type, opacity, brightness, saturation, shader ); else restoreRenderStates( type, opacity, brightness, saturation ); } void SceneOpenGL::Window::restoreShaderRenderStates( TextureType type, double opacity, double brightness, double saturation, GLShader* shader ) { Q_UNUSED( type ); Q_UNUSED( opacity ); Q_UNUSED( brightness ); Q_UNUSED( saturation ); Q_UNUSED( shader ); glPopAttrib(); // ENABLE_BIT } void SceneOpenGL::Window::restoreRenderStates( TextureType type, double opacity, double brightness, double saturation ) { Texture* tex; switch( type ) { case Content: tex = &texture; break; case DecorationTop: tex = &topTexture; break; case DecorationLeft: tex = &leftTexture; break; case DecorationRight: tex = &rightTexture; break; case DecorationBottom: tex = &bottomTexture; break; default: return; } if( opacity != 1.0 || saturation != 1.0 || brightness != 1.0f ) { if( saturation != 1.0 && tex->saturationSupported()) { glActiveTexture(GL_TEXTURE3); glDisable( tex->target()); glActiveTexture(GL_TEXTURE2); glDisable( tex->target()); glActiveTexture(GL_TEXTURE1); glDisable( tex->target()); glActiveTexture(GL_TEXTURE0); } } glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); glColor4f( 0, 0, 0, 0 ); glPopAttrib(); // ENABLE_BIT } //**************************************** // SceneOpenGL::EffectFrame //**************************************** GLTexture* SceneOpenGL::EffectFrame::m_unstyledTexture = NULL; SceneOpenGL::EffectFrame::EffectFrame( EffectFrameImpl* frame ) : Scene::EffectFrame( frame ) , m_texture( NULL ) , m_textTexture( NULL ) , m_iconTexture( NULL ) , m_unstyledVBO( NULL ) { if( m_effectFrame->style() == Unstyled && !m_unstyledTexture ) { updateUnstyledTexture(); } } SceneOpenGL::EffectFrame::~EffectFrame() { delete m_texture; delete m_textTexture; delete m_iconTexture; delete m_unstyledVBO; } void SceneOpenGL::EffectFrame::free() { delete m_texture; m_texture = NULL; delete m_textTexture; m_textTexture = NULL; delete m_iconTexture; m_iconTexture = NULL; delete m_unstyledVBO; m_unstyledVBO = NULL; } void SceneOpenGL::EffectFrame::freeIconFrame() { delete m_iconTexture; m_iconTexture = NULL; } void SceneOpenGL::EffectFrame::freeTextFrame() { delete m_textTexture; m_textTexture = NULL; } void SceneOpenGL::EffectFrame::render( QRegion region, double opacity, double frameOpacity ) { if( m_effectFrame->geometry().isEmpty() ) return; // Nothing to display region = infiniteRegion(); // TODO: Old region doesn't seem to work with OpenGL + GLShader* shader = m_effectFrame->shader(); + if( shader ) + { + shader->bind(); + shader->setUniform("saturation", 1.0f); + shader->setUniform("brightness", 1.0f); + + shader->setUniform("textureWidth", 1.0f); + shader->setUniform("textureHeight", 1.0f); + } + glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TEXTURE_BIT ); glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); - glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); + if( !shader ) + glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); glPushMatrix(); // Render the actual frame if( m_effectFrame->style() == Unstyled ) { if( !m_unstyledVBO ) { m_unstyledVBO = new GLVertexBuffer( GLVertexBuffer::Static ); const QRect& area = m_effectFrame->geometry().adjusted( -5, -5, 5, 5 ); const int roundness = 5; QVector verts, texCoords; verts.reserve( 84 ); texCoords.reserve( 84 ); // top left verts << area.left() << area.top(); texCoords << 0.0f << 0.0f; verts << area.left() << area.top() + roundness; texCoords << 0.0f << 0.5f; verts << area.left() + roundness << area.top(); texCoords << 0.5f << 0.0f; verts << area.left() + roundness << area.top() + roundness; texCoords << 0.5f << 0.5f; verts << area.left() << area.top() + roundness; texCoords << 0.0f << 0.5f; verts << area.left() + roundness << area.top(); texCoords << 0.5f << 0.0f; // top verts << area.left() + roundness << area.top(); texCoords << 0.5f << 0.0f; verts << area.left() + roundness << area.top() + roundness; texCoords << 0.5f << 0.5f; verts << area.right() - roundness << area.top(); texCoords << 0.5f << 0.0f; verts << area.left() + roundness << area.top() + roundness; texCoords << 0.5f << 0.5f; verts << area.right() - roundness << area.top() + roundness; texCoords << 0.5f << 0.5f; verts << area.right() - roundness << area.top(); texCoords << 0.5f << 0.0f; // top right verts << area.right() - roundness << area.top(); texCoords << 0.5f << 0.0f; verts << area.right() - roundness << area.top() + roundness; texCoords << 0.5f << 0.5f; verts << area.right() << area.top(); texCoords << 1.0f << 0.0f; verts << area.right() - roundness << area.top() + roundness; texCoords << 0.5f << 0.5f; verts << area.right() << area.top() + roundness; texCoords << 1.0f << 0.5f; verts << area.right() << area.top(); texCoords << 1.0f << 0.0f; // bottom left verts << area.left() << area.bottom() - roundness; texCoords << 0.0f << 0.5f; verts << area.left() << area.bottom(); texCoords << 0.0f << 1.0f; verts << area.left() + roundness << area.bottom() - roundness; texCoords << 0.5f << 0.5f; verts << area.left() + roundness << area.bottom(); texCoords << 0.5f << 1.0f; verts << area.left() << area.bottom(); texCoords << 0.0f << 1.0f; verts << area.left() + roundness << area.bottom() - roundness; texCoords << 0.5f << 0.5f; // bottom verts << area.left() + roundness << area.bottom() - roundness; texCoords << 0.5f << 0.5f; verts << area.left() + roundness << area.bottom(); texCoords << 0.5f << 1.0f; verts << area.right() - roundness << area.bottom() - roundness; texCoords << 0.5f << 0.5f; verts << area.left() + roundness << area.bottom(); texCoords << 0.5f << 1.0f; verts << area.right() - roundness << area.bottom(); texCoords << 0.5f << 1.0f; verts << area.right() - roundness << area.bottom() - roundness; texCoords << 0.5f << 0.5f; // bottom right verts << area.right() - roundness << area.bottom() - roundness; texCoords << 0.5f << 0.5f; verts << area.right() - roundness << area.bottom(); texCoords << 0.5f << 1.0f; verts << area.right() << area.bottom() - roundness; texCoords << 1.0f << 0.5f; verts << area.right() - roundness << area.bottom(); texCoords << 0.5f << 1.0f; verts << area.right() << area.bottom(); texCoords << 1.0f << 1.0f; verts << area.right() << area.bottom() - roundness; texCoords << 1.0f << 0.5f; // center verts << area.left() << area.top() + roundness; texCoords << 0.0f << 0.5f; verts << area.left() << area.bottom() - roundness; texCoords << 0.0f << 0.5f; verts << area.right() << area.top() + roundness; texCoords << 1.0f << 0.5f; verts << area.left() << area.bottom() - roundness; texCoords << 0.0f << 0.5f; verts << area.right() << area.bottom() - roundness; texCoords << 1.0f << 0.5f; verts << area.right() << area.top() + roundness; texCoords << 1.0f << 0.5f; m_unstyledVBO->setData( verts.count() / 2, 2, verts.data(), texCoords.data() ); } - glColor4f( 0.0, 0.0, 0.0, opacity * frameOpacity ); + if( shader ) + shader->setUniform( "opacity", (float)(opacity * frameOpacity) ); + else + glColor4f( 0.0, 0.0, 0.0, opacity * frameOpacity ); m_unstyledTexture->bind(); m_unstyledVBO->render( region, GL_TRIANGLES ); m_unstyledTexture->unbind(); } else if( m_effectFrame->style() == Styled ) { if( !m_texture ) // Lazy creation updateTexture(); - glColor4f( 1.0, 1.0, 1.0, opacity * frameOpacity ); + if( shader ) + shader->setUniform( "opacity", (float)(opacity * frameOpacity) ); + else + glColor4f( 1.0, 1.0, 1.0, opacity * frameOpacity ); m_texture->bind(); qreal left, top, right, bottom; m_effectFrame->frame().getMargins( left, top, right, bottom ); // m_geometry is the inner geometry m_texture->render( region, m_effectFrame->geometry().adjusted( -left, -top, right, bottom )); m_texture->unbind(); } - glColor4f( 1.0, 1.0, 1.0, opacity ); + if( shader ) + shader->setUniform( "opacity", (float)opacity ); + else + glColor4f( 1.0, 1.0, 1.0, opacity ); // Render icon if( !m_effectFrame->icon().isNull() && !m_effectFrame->iconSize().isEmpty() ) { QPoint topLeft( m_effectFrame->geometry().x(), m_effectFrame->geometry().center().y() - m_effectFrame->iconSize().height() / 2 ); if( !m_iconTexture ) // lazy creation { m_iconTexture = new Texture( m_effectFrame->icon().handle(), m_effectFrame->icon().size(), m_effectFrame->icon().depth() ); } m_iconTexture->bind(); m_iconTexture->render( region, QRect( topLeft, m_effectFrame->iconSize() )); m_iconTexture->unbind(); } // Render text if( !m_effectFrame->text().isEmpty() ) { if( !m_textTexture ) // Lazy creation updateTextTexture(); m_textTexture->bind(); m_textTexture->render( region, m_effectFrame->geometry() ); m_textTexture->unbind(); } + if( shader ) + shader->unbind(); + glPopMatrix(); glPopAttrib(); } void SceneOpenGL::EffectFrame::updateTexture() { delete m_texture; if( m_effectFrame->style() == Styled ) m_texture = new GLTexture( m_effectFrame->frame().framePixmap() ); } void SceneOpenGL::EffectFrame::updateTextTexture() { delete m_textTexture; if( m_effectFrame->text().isEmpty() ) return; // Determine position on texture to paint text QRect rect( QPoint( 0, 0 ), m_effectFrame->geometry().size() ); if( !m_effectFrame->icon().isNull() && !m_effectFrame->iconSize().isEmpty() ) rect.setLeft( m_effectFrame->iconSize().width() ); // If static size elide text as required QString text = m_effectFrame->text(); if( m_effectFrame->isStatic() ) { QFontMetrics metrics( m_effectFrame->font() ); text = metrics.elidedText( text, Qt::ElideRight, rect.width() ); } QImage image( m_effectFrame->geometry().size(), QImage::Format_ARGB32 ); image.fill( Qt::transparent ); QPainter p( &image ); p.setFont( m_effectFrame->font() ); if( m_effectFrame->style() == Styled ) p.setPen( m_effectFrame->styledTextColor() ); else // TODO: What about no frame? Custom color setting required p.setPen( Qt::white ); p.drawText( rect, m_effectFrame->alignment(), text ); p.end(); m_textTexture = new GLTexture( image ); } void SceneOpenGL::EffectFrame::updateUnstyledTexture() { delete m_unstyledTexture; // Based off circle() from kwinxrenderutils.cpp #define CS 8 QImage tmp( 2 * CS, 2 * CS, QImage::Format_ARGB32 ); tmp.fill( Qt::transparent ); QPainter p( &tmp ); p.setRenderHint( QPainter::Antialiasing ); p.setPen( Qt::NoPen ); p.setBrush( Qt::black ); p.drawEllipse( tmp.rect() ); p.end(); #undef CS m_unstyledTexture = new GLTexture( tmp ); } void SceneOpenGL::EffectFrame::cleanup() { delete m_unstyledTexture; m_unstyledTexture = NULL; } } // namespace #endif