• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • @topname@
  • Sitemap
  • Contact Us
 

kwin

activation.cpp

00001 /*****************************************************************
00002  KWin - the KDE window manager
00003  This file is part of the KDE project.
00004 
00005 Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org>
00006 Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
00007 
00008 You can Freely distribute this program under the GNU General Public
00009 License. See the file "COPYING" for the exact licensing terms.
00010 ******************************************************************/
00011 
00012 /*
00013 
00014  This file contains things relevant to window activation and focus
00015  stealing prevention.
00016 
00017 */
00018 
00019 #include "client.h"
00020 #include "workspace.h"
00021 
00022 #include <fixx11h.h>
00023 #include <qpopupmenu.h>
00024 #include <kxerrorhandler.h>
00025 #include <kstartupinfo.h>
00026 #include <kstringhandler.h>
00027 #include <klocale.h>
00028 
00029 #include "notifications.h"
00030 #include "atoms.h"
00031 #include "group.h"
00032 #include "rules.h"
00033 
00034 extern Time qt_x_time;
00035 
00036 namespace KWinInternal
00037 {
00038 
00039 /*
00040  Prevention of focus stealing:
00041 
00042  KWin tries to prevent unwanted changes of focus, that would result
00043  from mapping a new window. Also, some nasty applications may try
00044  to force focus change even in cases when ICCCM 4.2.7 doesn't allow it
00045  (e.g. they may try to activate their main window because the user
00046  definitely "needs" to see something happened - misusing
00047  of QWidget::setActiveWindow() may be such case).
00048 
00049  There are 4 ways how a window may become active:
00050  - the user changes the active window (e.g. focus follows mouse, clicking
00051    on some window's titlebar) - the change of focus will
00052    be done by KWin, so there's nothing to solve in this case
00053  - the change of active window will be requested using the _NET_ACTIVE_WINDOW
00054    message (handled in RootInfo::changeActiveWindow()) - such requests
00055    will be obeyed, because this request is meant mainly for e.g. taskbar
00056    asking the WM to change the active window as a result of some user action.
00057    Normal applications should use this request only rarely in special cases.
00058    See also below the discussion of _NET_ACTIVE_WINDOW_TRANSFER.
00059  - the change of active window will be done by performing XSetInputFocus()
00060    on a window that's not currently active. ICCCM 4.2.7 describes when
00061    the application may perform change of input focus. In order to handle
00062    misbehaving applications, KWin will try to detect focus changes to
00063    windows that don't belong to currently active application, and restore
00064    focus back to the currently active window, instead of activating the window
00065    that got focus (unfortunately there's no way to FocusChangeRedirect similar
00066    to e.g. SubstructureRedirect, so there will be short time when the focus
00067    will be changed). The check itself that's done is
00068    Workspace::allowClientActivation() (see below).
00069  - a new window will be mapped - this is the most complicated case. If
00070    the new window belongs to the currently active application, it may be safely
00071    mapped on top and activated. The same if there's no active window,
00072    or the active window is the desktop. These checks are done by
00073    Workspace::allowClientActivation().
00074     Following checks need to compare times. One time is the timestamp
00075    of last user action in the currently active window, the other time is
00076    the timestamp of the action that originally caused mapping of the new window
00077    (e.g. when the application was started). If the first time is newer than
00078    the second one, the window will not be activated, as that indicates
00079    futher user actions took place after the action leading to this new
00080    mapped window. This check is done by Workspace::allowClientActivation().
00081     There are several ways how to get the timestamp of action that caused
00082    the new mapped window (done in Client::readUserTimeMapTimestamp()) :
00083      - the window may have the _NET_WM_USER_TIME property. This way
00084        the application may either explicitly request that the window is not
00085        activated (by using 0 timestamp), or the property contains the time
00086        of last user action in the application.
00087      - KWin itself tries to detect time of last user action in every window,
00088        by watching KeyPress and ButtonPress events on windows. This way some
00089        events may be missed (if they don't propagate to the toplevel window),
00090        but it's good as a fallback for applications that don't provide
00091        _NET_WM_USER_TIME, and missing some events may at most lead
00092        to unwanted focus stealing.
00093      - the timestamp may come from application startup notification.
00094        Application startup notification, if it exists for the new mapped window,
00095        should include time of the user action that caused it.
00096      - if there's no timestamp available, it's checked whether the new window
00097        belongs to some already running application - if yes, the timestamp
00098        will be 0 (i.e. refuse activation)
00099      - if the window is from session restored window, the timestamp will
00100        be 0 too, unless this application was the active one at the time
00101        when the session was saved, in which case the window will be
00102        activated if there wasn't any user interaction since the time
00103        KWin was started.
00104      - as the last resort, the _KDE_NET_USER_CREATION_TIME timestamp
00105        is used. For every toplevel window that is created (see CreateNotify
00106        handling), this property is set to the at that time current time.
00107        Since at this time it's known that the new window doesn't belong
00108        to any existing application (better said, the application doesn't
00109        have any other window mapped), it is either the very first window
00110        of the application, or its the only window of the application
00111        that was hidden before. The latter case is handled by removing
00112        the property from windows before withdrawing them, making
00113        the timestamp empty for next mapping of the window. In the sooner
00114        case, the timestamp will be used. This helps in case when
00115        an application is launched without application startup notification,
00116        it creates its mainwindow, and starts its initialization (that
00117        may possibly take long time). The timestamp used will be older
00118        than any user action done after launching this application.
00119      - if no timestamp is found at all, the window is activated.
00120     The check whether two windows belong to the same application (same
00121    process) is done in Client::belongToSameApplication(). Not 100% reliable,
00122    but hopefully 99,99% reliable.
00123 
00124  As a somewhat special case, window activation is always enabled when
00125  session saving is in progress. When session saving, the session
00126  manager allows only one application to interact with the user.
00127  Not allowing window activation in such case would result in e.g. dialogs
00128  not becoming active, so focus stealing prevention would cause here
00129  more harm than good.
00130 
00131  Windows that attempted to become active but KWin prevented this will
00132  be marked as demanding user attention. They'll get
00133  the _NET_WM_STATE_DEMANDS_ATTENTION state, and the taskbar should mark
00134  them specially (blink, etc.). The state will be reset when the window
00135  eventually really becomes active.
00136 
00137  There are one more ways how a window can become obstrusive, window stealing
00138  focus: By showing above the active window, by either raising itself,
00139  or by moving itself on the active desktop.
00140      - KWin will refuse raising non-active window above the active one,
00141          unless they belong to the same application. Applications shouldn't
00142          raise their windows anyway (unless the app wants to raise one
00143          of its windows above another of its windows).
00144      - KWin activates windows moved to the current desktop (as that seems
00145          logical from the user's point of view, after sending the window
00146          there directly from KWin, or e.g. using pager). This means
00147          applications shouldn't send their windows to another desktop
00148          (SELI TODO - but what if they do?)
00149 
00150  Special cases I can think of:
00151     - konqueror reusing, i.e. kfmclient tells running Konqueror instance
00152         to open new window
00153         - without focus stealing prevention - no problem
00154         - with ASN (application startup notification) - ASN is forwarded,
00155             and because it's newer than the instance's user timestamp,
00156             it takes precedence
00157         - without ASN - user timestamp needs to be reset, otherwise it would
00158             be used, and it's old; moreover this new window mustn't be detected
00159             as window belonging to already running application, or it wouldn't
00160             be activated - see Client::sameAppWindowRoleMatch() for the (rather ugly)
00161             hack
00162     - konqueror preloading, i.e. window is created in advance, and kfmclient
00163         tells this Konqueror instance to show it later
00164         - without focus stealing prevention - no problem
00165         - with ASN - ASN is forwarded, and because it's newer than the instance's
00166             user timestamp, it takes precedence
00167         - without ASN - user timestamp needs to be reset, otherwise it would
00168             be used, and it's old; also, creation timestamp is changed to
00169             the time the instance starts (re-)initializing the window,
00170             this ensures creation timestamp will still work somewhat even in this case
00171     - KUniqueApplication - when the window is already visible, and the new instance
00172         wants it to activate
00173         - without focus stealing prevention - _NET_ACTIVE_WINDOW - no problem
00174         - with ASN - ASN is forwarded, and set on the already visible window, KWin
00175             treats the window as new with that ASN
00176         - without ASN - _NET_ACTIVE_WINDOW as application request is used,
00177                 and there's no really usable timestamp, only timestamp
00178                 from the time the (new) application instance was started,
00179                 so KWin will activate the window *sigh*
00180                 - the bad thing here is that there's absolutely no chance to recognize
00181                     the case of starting this KUniqueApp from Konsole (and thus wanting
00182                     the already visible window to become active) from the case
00183                     when something started this KUniqueApp without ASN (in which case
00184                     the already visible window shouldn't become active)
00185                 - the only solution is using ASN for starting applications, at least silent
00186                     (i.e. without feedback)
00187     - when one application wants to activate another application's window (e.g. KMail
00188         activating already running KAddressBook window ?)
00189         - without focus stealing prevention - _NET_ACTIVE_WINDOW - no problem
00190         - with ASN - can't be here, it's the KUniqueApp case then
00191         - without ASN - _NET_ACTIVE_WINDOW as application request should be used,
00192             KWin will activate the new window depending on the timestamp and
00193             whether it belongs to the currently active application
00194 
00195  _NET_ACTIVE_WINDOW usage:
00196  data.l[0]= 1 ->app request
00197           = 2 ->pager request
00198           = 0 - backwards compatibility
00199  data.l[1]= timestamp
00200 */
00201 
00202 
00203 //****************************************
00204 // Workspace
00205 //****************************************
00206 
00207 
00216 void Workspace::setActiveClient( Client* c, allowed_t )
00217     {
00218     if ( active_client == c )
00219         return;
00220     if( active_popup && active_popup_client != c && set_active_client_recursion == 0 ) 
00221         closeActivePopup();
00222     StackingUpdatesBlocker blocker( this );
00223     ++set_active_client_recursion;
00224     updateFocusMousePosition( QCursor::pos());
00225     if( active_client != NULL )
00226         { // note that this may call setActiveClient( NULL ), therefore the recursion counter
00227         active_client->setActive( false, !c || !c->isModal() || c != active_client->transientFor() );
00228         }
00229     active_client = c;
00230     Q_ASSERT( c == NULL || c->isActive());
00231     if( active_client != NULL )
00232         last_active_client = active_client;
00233     if ( active_client ) 
00234         {
00235         updateFocusChains( active_client, FocusChainMakeFirst );
00236         active_client->demandAttention( false );
00237         }
00238     pending_take_activity = NULL;
00239 
00240     updateCurrentTopMenu();
00241     updateToolWindows( false );
00242     if( c )
00243         disableGlobalShortcutsForClient( c->rules()->checkDisableGlobalShortcuts( false ));
00244     else
00245         disableGlobalShortcutsForClient( false );
00246 
00247     updateStackingOrder(); // e.g. fullscreens have different layer when active/not-active
00248 
00249     rootInfo->setActiveWindow( active_client? active_client->window() : 0 );
00250     updateColormap();
00251     --set_active_client_recursion;
00252     }
00253 
00265 void Workspace::activateClient( Client* c, bool force )
00266     {
00267     if( c == NULL )
00268         {
00269         focusToNull();
00270         setActiveClient( NULL, Allowed );
00271         return;
00272         }
00273     raiseClient( c );
00274     if (!c->isOnDesktop(currentDesktop()) )
00275         {
00276         ++block_focus;
00277         setCurrentDesktop( c->desktop() );
00278         --block_focus;
00279         }
00280     if( c->isMinimized())
00281         c->unminimize();
00282 
00283 // TODO force should perhaps allow this only if the window already contains the mouse
00284     if( options->focusPolicyIsReasonable() || force )
00285         requestFocus( c, force );
00286 
00287     // Don't update user time for clients that have focus stealing workaround.
00288     // As they usually belong to the current active window but fail to provide
00289     // this information, updating their user time would make the user time
00290     // of the currently active window old, and reject further activation for it.
00291     // E.g. typing URL in minicli which will show kio_uiserver dialog (with workaround),
00292     // and then kdesktop shows dialog about SSL certificate.
00293     // This needs also avoiding user creation time in Client::readUserTimeMapTimestamp().
00294     if( !c->ignoreFocusStealing())
00295         c->updateUserTime();
00296     }
00297 
00305 void Workspace::requestFocus( Client* c, bool force )
00306     {
00307     takeActivity( c, ActivityFocus | ( force ? ActivityFocusForce : 0 ), false);
00308     }
00309     
00310 void Workspace::takeActivity( Client* c, int flags, bool handled )
00311     {
00312      // the 'if( c == active_client ) return;' optimization mustn't be done here
00313     if (!focusChangeEnabled() && ( c != active_client) )
00314         flags &= ~ActivityFocus;
00315 
00316     if ( !c ) 
00317         {
00318         focusToNull();
00319         return;
00320         }
00321 
00322     if( flags & ActivityFocus )
00323         {
00324         Client* modal = c->findModal();
00325         if( modal != NULL && modal != c )   
00326             { 
00327             if( !modal->isOnDesktop( c->desktop()))
00328                 {
00329                 modal->setDesktop( c->desktop());
00330                 if( modal->desktop() != c->desktop()) // forced desktop
00331                     activateClient( modal );
00332                 }
00333             // if the click was inside the window (i.e. handled is set),
00334             // but it has a modal, there's no need to use handled mode, because
00335             // the modal doesn't get the click anyway
00336             // raising of the original window needs to be still done
00337             if( flags & ActivityRaise )
00338                 raiseClient( c );
00339             c = modal;
00340             handled = false;
00341             }
00342         cancelDelayFocus();
00343         }
00344     if ( !( flags & ActivityFocusForce ) && ( c->isTopMenu() || c->isDock() || c->isSplash()) )
00345         flags &= ~ActivityFocus; // toplevel menus and dock windows don't take focus if not forced
00346     if( c->isShade())
00347         {
00348         if( c->wantsInput() && ( flags & ActivityFocus ))
00349             {
00350         // client cannot accept focus, but at least the window should be active (window menu, et. al. )
00351             c->setActive( true );
00352             focusToNull();
00353             }
00354         flags &= ~ActivityFocus;
00355         handled = false; // no point, can't get clicks
00356         }
00357     if( !c->isShown( true )) // shouldn't happen, call activateClient() if needed
00358         {
00359         kdWarning( 1212 ) << "takeActivity: not shown" << endl;
00360         return;
00361         }
00362     c->takeActivity( flags, handled, Allowed );
00363     if( !c->isOnScreen( active_screen ))
00364         active_screen = c->screen();
00365     }
00366 
00367 void Workspace::handleTakeActivity( Client* c, Time /*timestamp*/, int flags )
00368     {
00369     if( pending_take_activity != c ) // pending_take_activity is reset when doing restack or activation
00370         return;
00371     if(( flags & ActivityRaise ) != 0 )
00372         raiseClient( c );
00373     if(( flags & ActivityFocus ) != 0 && c->isShown( false ))
00374         c->takeFocus( Allowed );
00375     pending_take_activity = NULL;
00376     }
00377 
00385 void Workspace::clientHidden( Client* c )
00386     {
00387     assert( !c->isShown( true ) || !c->isOnCurrentDesktop());
00388     activateNextClient( c );
00389     }
00390 
00391 // deactivates 'c' and activates next client
00392 bool Workspace::activateNextClient( Client* c )
00393     {
00394     // if 'c' is not the active or the to-become active one, do nothing
00395     if( !( c == active_client
00396             || ( should_get_focus.count() > 0 && c == should_get_focus.last())))
00397         return false;
00398     closeActivePopup();
00399     if( c != NULL )
00400         {
00401         if( c == active_client )
00402             setActiveClient( NULL, Allowed );
00403         should_get_focus.remove( c );
00404         }
00405     if( focusChangeEnabled())
00406         {
00407         if ( options->focusPolicyIsReasonable())
00408             { // search the focus_chain for a client to transfer focus to
00409               // if 'c' is transient, transfer focus to the first suitable mainwindow
00410             Client* get_focus = NULL;
00411             const ClientList mainwindows = ( c != NULL ? c->mainClients() : ClientList());
00412             for( ClientList::ConstIterator it = focus_chain[currentDesktop()].fromLast();
00413                  it != focus_chain[currentDesktop()].end();
00414                  --it )
00415                 {
00416                 if( !(*it)->isShown( false ) || !(*it)->isOnCurrentDesktop())
00417                     continue;
00418                 if( options->separateScreenFocus )
00419                     {
00420                     if( c != NULL && !(*it)->isOnScreen( c->screen()))
00421                         continue;
00422                     if( c == NULL && !(*it)->isOnScreen( activeScreen()))
00423                         continue;
00424                     }
00425                 if( mainwindows.contains( *it ))
00426                     {
00427                     get_focus = *it;
00428                     break;
00429                     }
00430                 if( get_focus == NULL )
00431                     get_focus = *it;
00432                 }
00433             if( get_focus == NULL )
00434                 get_focus = findDesktop( true, currentDesktop());
00435             if( get_focus != NULL )
00436                 requestFocus( get_focus );
00437             else
00438                 focusToNull();
00439             }
00440             else
00441                 return false;
00442         }
00443     else
00444         // if blocking focus, move focus to the desktop later if needed
00445         // in order to avoid flickering
00446         focusToNull();
00447     return true;
00448     }
00449 
00450 void Workspace::setCurrentScreen( int new_screen )
00451     {
00452     if (new_screen < 0 || new_screen > numScreens())
00453         return;
00454     if ( !options->focusPolicyIsReasonable())
00455         return;
00456     closeActivePopup();
00457     Client* get_focus = NULL;
00458     for( ClientList::ConstIterator it = focus_chain[currentDesktop()].fromLast();
00459          it != focus_chain[currentDesktop()].end();
00460          --it )
00461         {
00462         if( !(*it)->isShown( false ) || !(*it)->isOnCurrentDesktop())
00463             continue;
00464         if( !(*it)->screen() == new_screen )
00465             continue;
00466         get_focus = *it;
00467         break;
00468         }
00469     if( get_focus == NULL )
00470         get_focus = findDesktop( true, currentDesktop());
00471     if( get_focus != NULL && get_focus != mostRecentlyActivatedClient())
00472         requestFocus( get_focus );
00473     active_screen = new_screen;
00474     }
00475 
00476 void Workspace::gotFocusIn( const Client* c )
00477     {
00478     if( should_get_focus.contains( const_cast< Client* >( c )))
00479         { // remove also all sooner elements that should have got FocusIn,
00480       // but didn't for some reason (and also won't anymore, because they were sooner)
00481         while( should_get_focus.first() != c )
00482             should_get_focus.pop_front();
00483         should_get_focus.pop_front(); // remove 'c'
00484         }
00485     }
00486 
00487 void Workspace::setShouldGetFocus( Client* c )
00488     {
00489     should_get_focus.append( c );
00490     updateStackingOrder(); // e.g. fullscreens have different layer when active/not-active
00491     }
00492 
00493 // focus_in -> the window got FocusIn event
00494 // session_active -> the window was active when saving session
00495 bool Workspace::allowClientActivation( const Client* c, Time time, bool focus_in )
00496     {
00497     // options->focusStealingPreventionLevel :
00498     // 0 - none    - old KWin behaviour, new windows always get focus
00499     // 1 - low     - focus stealing prevention is applied normally, when unsure, activation is allowed
00500     // 2 - normal  - focus stealing prevention is applied normally, when unsure, activation is not allowed,
00501     //              this is the default
00502     // 3 - high    - new window gets focus only if it belongs to the active application,
00503     //              or when no window is currently active
00504     // 4 - extreme - no window gets focus without user intervention
00505     if( time == -1U )
00506         time = c->userTime();
00507     int level = c->rules()->checkFSP( options->focusStealingPreventionLevel );
00508     if( session_saving && level <= 2 ) // <= normal
00509         {
00510         return true;
00511         }
00512     Client* ac = mostRecentlyActivatedClient();
00513     if( focus_in )
00514         {
00515         if( should_get_focus.contains( const_cast< Client* >( c )))
00516             return true; // FocusIn was result of KWin's action
00517         // Before getting FocusIn, the active Client already
00518         // got FocusOut, and therefore got deactivated.
00519         ac = last_active_client;
00520         }
00521     if( time == 0 ) // explicitly asked not to get focus
00522         return false;
00523     if( level == 0 ) // none
00524         return true;
00525     if( level == 4 ) // extreme
00526         return false;
00527     if( !c->isOnCurrentDesktop())
00528         return false; // allow only with level == 0
00529     if( c->ignoreFocusStealing())
00530         return true;
00531     if( ac == NULL || ac->isDesktop())
00532         {
00533 //        kdDebug( 1212 ) << "Activation: No client active, allowing" << endl;
00534         return true; // no active client -> always allow
00535         }
00536     // TODO window urgency  -> return true?
00537     if( Client::belongToSameApplication( c, ac, true ))
00538         {
00539 //        kdDebug( 1212 ) << "Activation: Belongs to active application" << endl;
00540         return true;
00541         }
00542     if( level == 3 ) // high
00543         return false;
00544     if( time == -1U )  // no time known
00545         {
00546 //        kdDebug( 1212 ) << "Activation: No timestamp at all" << endl;
00547         if( level == 1 ) // low
00548             return true;
00549         // no timestamp at all, don't activate - because there's also creation timestamp
00550         // done on CreateNotify, this case should happen only in case application
00551         // maps again already used window, i.e. this won't happen after app startup
00552         return false; 
00553         }
00554     // level == 2 // normal
00555     Time user_time = ac->userTime();
00556 //    kdDebug( 1212 ) << "Activation, compared:" << c << ":" << time << ":" << user_time
00557 //        << ":" << ( timestampCompare( time, user_time ) >= 0 ) << endl;
00558     return timestampCompare( time, user_time ) >= 0; // time >= user_time
00559     }
00560 
00561 // basically the same like allowClientActivation(), this time allowing
00562 // a window to be fully raised upon its own request (XRaiseWindow),
00563 // if refused, it will be raised only on top of windows belonging
00564 // to the same application
00565 bool Workspace::allowFullClientRaising( const Client* c, Time time )
00566     {
00567     int level = c->rules()->checkFSP( options->focusStealingPreventionLevel );
00568     if( session_saving && level <= 2 ) // <= normal
00569         {
00570         return true;
00571         }
00572     Client* ac = mostRecentlyActivatedClient();
00573     if( level == 0 ) // none
00574         return true;
00575     if( level == 4 ) // extreme
00576         return false;
00577     if( ac == NULL || ac->isDesktop())
00578         {
00579 //        kdDebug( 1212 ) << "Raising: No client active, allowing" << endl;
00580         return true; // no active client -> always allow
00581         }
00582     if( c->ignoreFocusStealing())
00583         return true;
00584     // TODO window urgency  -> return true?
00585     if( Client::belongToSameApplication( c, ac, true ))
00586         {
00587 //        kdDebug( 1212 ) << "Raising: Belongs to active application" << endl;
00588         return true;
00589         }
00590     if( level == 3 ) // high
00591         return false;
00592     Time user_time = ac->userTime();
00593 //    kdDebug( 1212 ) << "Raising, compared:" << time << ":" << user_time
00594 //        << ":" << ( timestampCompare( time, user_time ) >= 0 ) << endl;
00595     return timestampCompare( time, user_time ) >= 0; // time >= user_time
00596     }
00597 
00598 // called from Client after FocusIn that wasn't initiated by KWin and the client
00599 // wasn't allowed to activate
00600 void Workspace::restoreFocus()
00601     {
00602     // this updateXTime() is necessary - as FocusIn events don't have
00603     // a timestamp *sigh*, kwin's timestamp would be older than the timestamp
00604     // that was used by whoever caused the focus change, and therefore
00605     // the attempt to restore the focus would fail due to old timestamp
00606     updateXTime();
00607     if( should_get_focus.count() > 0 )
00608         requestFocus( should_get_focus.last());
00609     else if( last_active_client )
00610         requestFocus( last_active_client );
00611     }
00612 
00613 void Workspace::clientAttentionChanged( Client* c, bool set )
00614     {
00615     if( set )
00616         {
00617         attention_chain.remove( c );
00618         attention_chain.prepend( c );
00619         }
00620     else
00621         attention_chain.remove( c );
00622     }
00623 
00624 // This is used when a client should be shown active immediately after requestFocus(),
00625 // without waiting for the matching FocusIn that will really make the window the active one.
00626 // Used only in special cases, e.g. for MouseActivateRaiseandMove with transparent windows,
00627 bool Workspace::fakeRequestedActivity( Client* c )
00628     {
00629     if( should_get_focus.count() > 0 && should_get_focus.last() == c )
00630         {
00631         if( c->isActive())
00632             return false;
00633         c->setActive( true );
00634         return true;
00635         }
00636     return false;
00637     }
00638 
00639 void Workspace::unfakeActivity( Client* c )
00640     {
00641     if( should_get_focus.count() > 0 && should_get_focus.last() == c )
00642         { // TODO this will cause flicker, and probably is not needed
00643         if( last_active_client != NULL )
00644             last_active_client->setActive( true );
00645         else
00646             c->setActive( false );
00647         }
00648     }
00649 
00650 
00651 //********************************************
00652 // Client
00653 //********************************************
00654 
00661 void Client::updateUserTime( Time time )
00662     { // copied in Group::updateUserTime
00663     if( time == CurrentTime )
00664         time = qt_x_time;
00665     if( time != -1U
00666         && ( user_time == CurrentTime
00667             || timestampCompare( time, user_time ) > 0 )) // time > user_time
00668         user_time = time;
00669     group()->updateUserTime( user_time );
00670     }
00671 
00672 Time Client::readUserCreationTime() const
00673     {
00674     long result = -1; // Time == -1 means none
00675     Atom type;
00676     int format, status;
00677     unsigned long nitems = 0;
00678     unsigned long extra = 0;
00679     unsigned char *data = 0;
00680     KXErrorHandler handler; // ignore errors?
00681     status = XGetWindowProperty( qt_xdisplay(), window(),
00682         atoms->kde_net_wm_user_creation_time, 0, 10000, FALSE, XA_CARDINAL,
00683         &type, &format, &nitems, &extra, &data );
00684     if (status  == Success )
00685         {
00686         if (data && nitems > 0)
00687             result = *((long*) data);
00688         XFree(data);
00689         }
00690     return result;       
00691     }
00692 
00693 void Client::demandAttention( bool set )
00694     {
00695     if( isActive())
00696         set = false;
00697     if( demands_attention == set )
00698         return;
00699     demands_attention = set;
00700     if( demands_attention )
00701         {
00702         // Demand attention flag is often set right from manage(), when focus stealing prevention
00703         // steps in. At that time the window has no taskbar entry yet, so KNotify cannot place
00704         // e.g. the passive popup next to it. So wait up to 1 second for the icon geometry
00705         // to be set.
00706         // Delayed call to KNotify also solves the problem of having X server grab in manage(),
00707         // which may deadlock when KNotify (or KLauncher when launching KNotify) need to access X.
00708         Notify::Event e = isOnCurrentDesktop() ? Notify::DemandAttentionCurrent : Notify::DemandAttentionOther;
00709         // Setting the demands attention state needs to be done directly in KWin, because
00710         // KNotify would try to set it, resulting in a call to KNotify again, etc.
00711         if( Notify::makeDemandAttention( e ))
00712             info->setState( set ? NET::DemandsAttention : 0, NET::DemandsAttention );
00713 
00714         if( demandAttentionKNotifyTimer == NULL )
00715             {
00716             demandAttentionKNotifyTimer = new QTimer( this );
00717             connect( demandAttentionKNotifyTimer, SIGNAL( timeout()), SLOT( demandAttentionKNotify()));
00718             }
00719         demandAttentionKNotifyTimer->start( 1000, true );
00720         }
00721     else
00722         info->setState( set ? NET::DemandsAttention : 0, NET::DemandsAttention );
00723     workspace()->clientAttentionChanged( this, set );
00724     }
00725 
00726 void Client::demandAttentionKNotify()
00727     {
00728     Notify::Event e = isOnCurrentDesktop() ? Notify::DemandAttentionCurrent : Notify::DemandAttentionOther;
00729     Notify::raise( e, i18n( "Window '%1' demands attention." ).arg( KStringHandler::csqueeze(caption())), this );
00730     demandAttentionKNotifyTimer->stop();
00731     demandAttentionKNotifyTimer->deleteLater();
00732     demandAttentionKNotifyTimer = NULL;
00733     }
00734 
00735 // TODO I probably shouldn't be lazy here and do it without the macro, so that people can read it
00736 KWIN_COMPARE_PREDICATE( SameApplicationActiveHackPredicate, const Client*,
00737     // ignore already existing splashes, toolbars, utilities, menus and topmenus,
00738     // as the app may show those before the main window
00739     !cl->isSplash() && !cl->isToolbar() && !cl->isTopMenu() && !cl->isUtility() && !cl->isMenu()
00740     && Client::belongToSameApplication( cl, value, true ) && cl != value);
00741 
00742 Time Client::readUserTimeMapTimestamp( const KStartupInfoId* asn_id, const KStartupInfoData* asn_data,
00743     bool session ) const
00744     {
00745     Time time = info->userTime();
00746 //    kdDebug( 1212 ) << "User timestamp, initial:" << time << endl;
00747     // newer ASN timestamp always replaces user timestamp, unless user timestamp is 0
00748     // helps e.g. with konqy reusing
00749     if( asn_data != NULL && time != 0 )
00750         {
00751         // prefer timestamp from ASN id (timestamp from data is obsolete way)
00752         if( asn_id->timestamp() != 0
00753             && ( time == -1U || timestampCompare( asn_id->timestamp(), time ) > 0 ))
00754             {
00755             time = asn_id->timestamp();
00756             }
00757         else if( asn_data->timestamp() != -1U
00758             && ( time == -1U || timestampCompare( asn_data->timestamp(), time ) > 0 ))
00759             {
00760             time = asn_data->timestamp();
00761             }
00762         }
00763 //    kdDebug( 1212 ) << "User timestamp, ASN:" << time << endl;
00764     if( time == -1U )
00765         { // The window doesn't have any timestamp.
00766       // If it's the first window for its application
00767       // (i.e. there's no other window from the same app),
00768       // use the _KDE_NET_WM_USER_CREATION_TIME trick.
00769       // Otherwise, refuse activation of a window
00770       // from already running application if this application
00771       // is not the active one (unless focus stealing prevention is turned off).
00772         Client* act = workspace()->mostRecentlyActivatedClient();
00773         if( act != NULL && !belongToSameApplication( act, this, true ))
00774             {
00775             bool first_window = true;
00776             if( isTransient())
00777                 {
00778                 if( act->hasTransient( this, true ))
00779                     ; // is transient for currently active window, even though it's not
00780                       // the same app (e.g. kcookiejar dialog) -> allow activation
00781                 else if( groupTransient() &&
00782                     findClientInList( mainClients(), SameApplicationActiveHackPredicate( this )) == NULL )
00783                     ; // standalone transient
00784                 else
00785                     first_window = false;
00786                 }
00787             else
00788                 {
00789                 if( workspace()->findClient( SameApplicationActiveHackPredicate( this )))
00790                     first_window = false;
00791                 }
00792             // don't refuse if focus stealing prevention is turned off
00793             if( !first_window && rules()->checkFSP( options->focusStealingPreventionLevel ) > 0 )
00794                 {
00795 //                kdDebug( 1212 ) << "User timestamp, already exists:" << 0 << endl;
00796                 return 0; // refuse activation
00797                 }
00798             }
00799         // Creation time would just mess things up during session startup,
00800         // as possibly many apps are started up at the same time.
00801         // If there's no active window yet, no timestamp will be needed,
00802         // as plain Workspace::allowClientActivation() will return true
00803         // in such case. And if there's already active window,
00804         // it's better not to activate the new one.
00805         // Unless it was the active window at the time
00806         // of session saving and there was no user interaction yet,
00807         // this check will be done in manage().
00808         if( session )
00809             return -1U;
00810         if( ignoreFocusStealing() && act != NULL )
00811             time = act->userTime();
00812         else
00813             time = readUserCreationTime();
00814         }
00815 //    kdDebug( 1212 ) << "User timestamp, final:" << this << ":" << time << endl;
00816     return time;
00817     }
00818 
00819 Time Client::userTime() const
00820     {
00821     Time time = user_time;
00822     if( time == 0 ) // doesn't want focus after showing
00823         return 0;
00824     assert( group() != NULL );
00825     if( time == -1U
00826          || ( group()->userTime() != -1U
00827                  && timestampCompare( group()->userTime(), time ) > 0 ))
00828         time = group()->userTime();
00829     return time;
00830     }
00831 
00843 void Client::setActive( bool act, bool updateOpacity_)
00844     {
00845     if ( active == act )
00846         return;
00847     active = act;
00848     workspace()->setActiveClient( act ? this : NULL, Allowed );
00849     
00850     if (updateOpacity_) updateOpacity();
00851     if (isModal() && transientFor())
00852     {
00853         if (!act) transientFor()->updateOpacity();
00854         else if (!transientFor()->custom_opacity) transientFor()->setOpacity(options->translucentActiveWindows, options->activeWindowOpacity);
00855     }
00856     updateShadowSize();
00857     
00858     if ( active )
00859         Notify::raise( Notify::Activate );
00860 
00861     if( !active )
00862         cancelAutoRaise();
00863 
00864     if( !active && shade_mode == ShadeActivated )
00865         setShade( ShadeNormal );
00866         
00867     StackingUpdatesBlocker blocker( workspace());
00868     workspace()->updateClientLayer( this ); // active windows may get different layer
00869     // TODO optimize? mainClients() may be a bit expensive
00870     ClientList mainclients = mainClients();
00871     for( ClientList::ConstIterator it = mainclients.begin();
00872          it != mainclients.end();
00873          ++it )
00874         if( (*it)->isFullScreen()) // fullscreens go high even if their transient is active
00875             workspace()->updateClientLayer( *it );
00876     if( decoration != NULL )
00877         decoration->activeChange();
00878     updateMouseGrab();
00879     updateUrgency(); // demand attention again if it's still urgent
00880     }
00881 
00882 void Client::startupIdChanged()
00883     {
00884     KStartupInfoId asn_id;
00885     KStartupInfoData asn_data;
00886     bool asn_valid = workspace()->checkStartupNotification( window(), asn_id, asn_data );
00887     if( !asn_valid )
00888         return;
00889     // If the ASN contains desktop, move it to the desktop, otherwise move it to the current
00890     // desktop (since the new ASN should make the window act like if it's a new application
00891     // launched). However don't affect the window's desktop if it's set to be on all desktops.
00892     int desktop = workspace()->currentDesktop();
00893     if( asn_data.desktop() != 0 )
00894         desktop = asn_data.desktop();
00895     if( !isOnAllDesktops())
00896         workspace()->sendClientToDesktop( this, desktop, true );
00897     if( asn_data.xinerama() != -1 )
00898         workspace()->sendClientToScreen( this, asn_data.xinerama());
00899     Time timestamp = asn_id.timestamp();
00900     if( timestamp == 0 && asn_data.timestamp() != -1U )
00901         timestamp = asn_data.timestamp();
00902     if( timestamp != 0 )
00903         {
00904         bool activate = workspace()->allowClientActivation( this, timestamp );
00905         if( asn_data.desktop() != 0 && !isOnCurrentDesktop())
00906             activate = false; // it was started on different desktop than current one
00907         if( activate )
00908             workspace()->activateClient( this );
00909         else
00910             demandAttention();
00911         }
00912     }
00913 
00914 void Client::updateUrgency()
00915     {
00916     if( urgency )
00917         demandAttention();
00918     }
00919 
00920 void Client::shortcutActivated()
00921     {
00922     workspace()->activateClient( this, true ); // force
00923     }
00924 
00925 //****************************************
00926 // Group
00927 //****************************************
00928     
00929 void Group::startupIdChanged()
00930     {
00931     KStartupInfoId asn_id;
00932     KStartupInfoData asn_data;
00933     bool asn_valid = workspace()->checkStartupNotification( leader_wid, asn_id, asn_data );
00934     if( !asn_valid )
00935         return;
00936     if( asn_id.timestamp() != 0 && user_time != -1U
00937         && timestampCompare( asn_id.timestamp(), user_time ) > 0 )
00938         {
00939         user_time = asn_id.timestamp();
00940         }
00941     else if( asn_data.timestamp() != -1U && user_time != -1U
00942         && timestampCompare( asn_data.timestamp(), user_time ) > 0 )
00943         {
00944         user_time = asn_data.timestamp();
00945         }
00946     }
00947 
00948 void Group::updateUserTime( Time time )
00949     { // copy of Client::updateUserTime
00950     if( time == CurrentTime )
00951         time = qt_x_time;
00952     if( time != -1U
00953         && ( user_time == CurrentTime
00954             || timestampCompare( time, user_time ) > 0 )) // time > user_time
00955         user_time = time;
00956     }
00957 
00958 } // namespace

kwin

Skip menu "kwin"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members

@topname@

Skip menu "@topname@"
  • kate
  • kwin
  •   lib
  • libkonq
Generated for @topname@ by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal