00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 #include <assert.h>
00076
00077 #include <kdebug.h>
00078
00079 #include "utils.h"
00080 #include "client.h"
00081 #include "workspace.h"
00082 #include "tabbox.h"
00083 #include "group.h"
00084 #include "rules.h"
00085 #include "unmanaged.h"
00086 #include "deleted.h"
00087 #include <QX11Info>
00088
00089 namespace KWin
00090 {
00091
00092
00093
00094
00095
00096 void Workspace::updateClientLayer( Client* c )
00097 {
00098 if( c == NULL )
00099 return;
00100 if( c->layer() == c->belongsToLayer())
00101 return;
00102 StackingUpdatesBlocker blocker( this );
00103 c->invalidateLayer();
00104 for( ClientList::ConstIterator it = c->transients().constBegin();
00105 it != c->transients().constEnd();
00106 ++it )
00107 updateClientLayer( *it );
00108 }
00109
00110 void Workspace::updateStackingOrder( bool propagate_new_clients )
00111 {
00112 if( block_stacking_updates > 0 )
00113 {
00114 if( propagate_new_clients )
00115 blocked_propagating_new_clients = true;
00116 return;
00117 }
00118 ClientList new_stacking_order = constrainedStackingOrder();
00119 bool changed = ( new_stacking_order != stacking_order || force_restacking );
00120 force_restacking = false;
00121 stacking_order = new_stacking_order;
00122 #if 0
00123 kDebug(1212) << "stacking:" << changed;
00124 if( changed || propagate_new_clients )
00125 {
00126 for( ClientList::ConstIterator it = stacking_order.begin();
00127 it != stacking_order.end();
00128 ++it )
00129 kDebug(1212) << (void*)(*it) << *it << ":" << (*it)->layer();
00130 }
00131 #endif
00132 if( changed || propagate_new_clients )
00133 {
00134 propagateClients( propagate_new_clients );
00135 addRepaintFull();
00136 if( active_client )
00137 active_client->updateMouseGrab();
00138 }
00139 }
00140
00145 void Workspace::propagateClients( bool propagate_new_clients )
00146 {
00147 Window *cl;
00148
00149
00150
00151
00152 Window* new_stack = new Window[ stacking_order.count() + 1 + 1 + 8 ];
00153 int pos = 0;
00154
00155
00156
00157
00158
00159 new_stack[ pos++ ] = supportWindow->winId();
00160 for( int i = 0;
00161 i < ELECTRIC_COUNT;
00162 ++i )
00163 if( electric_windows[ i ] != None )
00164 new_stack[ pos++ ] = electric_windows[ i ];
00165 int topmenu_space_pos = 1;
00166 for ( int i = stacking_order.size() - 1; i >= 0; i-- )
00167 {
00168 if( stacking_order.at( i )->hiddenPreview())
00169 continue;
00170 new_stack[ pos++ ] = stacking_order.at( i )->frameId();
00171 if( stacking_order.at( i )->belongsToLayer() >= DockLayer )
00172 topmenu_space_pos = pos;
00173 }
00174 if( topmenu_space != NULL )
00175 {
00176 for( int i = pos;
00177 i > topmenu_space_pos;
00178 --i )
00179 new_stack[ i ] = new_stack[ i - 1 ];
00180 new_stack[ topmenu_space_pos ] = topmenu_space->winId();
00181 ++pos;
00182 }
00183
00184
00185
00186 for ( int i = stacking_order.size() - 1; i >= 0; i-- )
00187 {
00188 if( !stacking_order.at( i )->hiddenPreview())
00189 continue;
00190 new_stack[ pos++ ] = stacking_order.at( i )->frameId();
00191 if( stacking_order.at( i )->belongsToLayer() >= DockLayer )
00192 topmenu_space_pos = pos;
00193 }
00194
00195
00196 assert( new_stack[ 0 ] == supportWindow->winId());
00197 XRestackWindows(display(), new_stack, pos);
00198 delete [] new_stack;
00199
00200 if ( propagate_new_clients )
00201 {
00202 cl = new Window[ desktops.count() + clients.count()];
00203 pos = 0;
00204
00205 for ( ClientList::ConstIterator it = desktops.constBegin(); it != desktops.constEnd(); ++it )
00206 cl[pos++] = (*it)->window();
00207 for ( ClientList::ConstIterator it = clients.constBegin(); it != clients.constEnd(); ++it )
00208 cl[pos++] = (*it)->window();
00209 rootInfo->setClientList( cl, pos );
00210 delete [] cl;
00211 }
00212
00213 cl = new Window[ stacking_order.count()];
00214 pos = 0;
00215 for ( ClientList::ConstIterator it = stacking_order.constBegin(); it != stacking_order.constEnd(); ++it)
00216 cl[pos++] = (*it)->window();
00217 rootInfo->setClientListStacking( cl, pos );
00218 delete [] cl;
00219
00220
00221
00222 x_stacking_dirty = true;
00223 }
00224
00225
00231
00232 Client* Workspace::topClientOnDesktop( int desktop, int screen, bool unconstrained, bool only_normal ) const
00233 {
00234
00235 ClientList list;
00236 if( !unconstrained )
00237 list = stacking_order;
00238 else
00239 list = unconstrained_stacking_order;
00240 for( int i = list.size() - 1;
00241 i >= 0;
00242 --i )
00243 {
00244 if( list.at( i )->isOnDesktop( desktop ) && list.at( i )->isShown( false ))
00245 {
00246 if( screen != -1 && list.at( i )->screen() != screen )
00247 continue;
00248 if( !only_normal )
00249 return list.at( i );
00250 if( list.at( i )->wantsTabFocus() && !list.at( i )->isSpecialWindow())
00251 return list.at( i );
00252 }
00253 }
00254 return 0;
00255 }
00256
00257 Client* Workspace::findDesktop( bool topmost, int desktop ) const
00258 {
00259
00260 if( topmost )
00261 {
00262 for ( int i = stacking_order.size() - 1; i>=0; i-- )
00263 {
00264 if ( stacking_order.at( i )->isOnDesktop( desktop ) && stacking_order.at( i )->isDesktop()
00265 && stacking_order.at( i )->isShown( true ))
00266 return stacking_order.at( i );
00267 }
00268 }
00269 else
00270 {
00271 foreach ( Client* c, stacking_order )
00272 {
00273 if ( c->isOnDesktop( desktop ) && c->isDesktop()
00274 && c->isShown( true ))
00275 return c;
00276 }
00277 }
00278 return NULL;
00279 }
00280
00281 void Workspace::raiseOrLowerClient( Client *c)
00282 {
00283 if (!c) return;
00284 Client* topmost = NULL;
00285
00286 if ( most_recently_raised && stacking_order.contains( most_recently_raised ) &&
00287 most_recently_raised->isShown( true ) && c->isOnCurrentDesktop())
00288 topmost = most_recently_raised;
00289 else
00290 topmost = topClientOnDesktop( c->isOnAllDesktops() ? currentDesktop() : c->desktop(),
00291 options->separateScreenFocus ? c->screen() : -1 );
00292
00293 if( c == topmost)
00294 lowerClient(c);
00295 else
00296 raiseClient(c);
00297 }
00298
00299
00300 void Workspace::lowerClient( Client* c, bool nogroup )
00301 {
00302 if ( !c )
00303 return;
00304 if( c->isTopMenu())
00305 return;
00306
00307 c->cancelAutoRaise();
00308
00309 StackingUpdatesBlocker blocker( this );
00310
00311 unconstrained_stacking_order.removeAll( c );
00312 unconstrained_stacking_order.prepend( c );
00313 if( !nogroup && c->isTransient() )
00314 {
00315
00316 ClientList wins = ensureStackingOrder( c->group()->members());
00317 for( int i = wins.size() - 1;
00318 i >= 0;
00319 --i )
00320 {
00321 if( wins[ i ] != c )
00322 lowerClient( wins[ i ], true );
00323 }
00324 }
00325
00326 if ( c == most_recently_raised )
00327 most_recently_raised = 0;
00328 }
00329
00330 void Workspace::lowerClientWithinApplication( Client* c )
00331 {
00332 if ( !c )
00333 return;
00334 if( c->isTopMenu())
00335 return;
00336
00337 c->cancelAutoRaise();
00338
00339 StackingUpdatesBlocker blocker( this );
00340
00341 unconstrained_stacking_order.removeAll( c );
00342 bool lowered = false;
00343
00344 for( ClientList::Iterator it = unconstrained_stacking_order.begin();
00345 it != unconstrained_stacking_order.end();
00346 ++it )
00347 if( Client::belongToSameApplication( *it, c ))
00348 {
00349 unconstrained_stacking_order.insert( it, c );
00350 lowered = true;
00351 break;
00352 }
00353 if( !lowered )
00354 unconstrained_stacking_order.prepend( c );
00355
00356 }
00357
00358 void Workspace::raiseClient( Client* c, bool nogroup )
00359 {
00360 if ( !c )
00361 return;
00362 if( c->isTopMenu())
00363 return;
00364
00365 c->cancelAutoRaise();
00366
00367 StackingUpdatesBlocker blocker( this );
00368
00369 if( !nogroup && c->isTransient())
00370 {
00371 ClientList wins = ensureStackingOrder( c->group()->members());
00372 foreach( Client* c2, wins )
00373 if( c2 != c )
00374 raiseClient( c2, true );
00375 }
00376
00377 unconstrained_stacking_order.removeAll( c );
00378 unconstrained_stacking_order.append( c );
00379
00380 if( !c->isSpecialWindow())
00381 {
00382 most_recently_raised = c;
00383 pending_take_activity = NULL;
00384 }
00385 }
00386
00387 void Workspace::raiseClientWithinApplication( Client* c )
00388 {
00389 if ( !c )
00390 return;
00391 if( c->isTopMenu())
00392 return;
00393
00394 c->cancelAutoRaise();
00395
00396 StackingUpdatesBlocker blocker( this );
00397
00398
00399
00400 for ( int i = unconstrained_stacking_order.size() - 1; i>= 0 ; i-- )
00401 {
00402 if( unconstrained_stacking_order.at( i ) == c )
00403 return;
00404 if( Client::belongToSameApplication( unconstrained_stacking_order.at( i ), c ))
00405 {
00406 unconstrained_stacking_order.removeAll( c );
00407 unconstrained_stacking_order.insert( ++i, c );
00408 return;
00409 }
00410 }
00411 }
00412
00413 void Workspace::raiseClientRequest( Client* c, NET::RequestSource src, Time timestamp )
00414 {
00415 if( src == NET::FromTool || allowFullClientRaising( c, timestamp ))
00416 raiseClient( c );
00417 else
00418 {
00419 raiseClientWithinApplication( c );
00420 c->demandAttention();
00421 }
00422 }
00423
00424 void Workspace::lowerClientRequest( Client* c, NET::RequestSource src, Time )
00425 {
00426
00427
00428
00429
00430 if( src == NET::FromTool || !c->hasUserTimeSupport())
00431 lowerClient( c );
00432 else
00433 lowerClientWithinApplication( c );
00434 }
00435
00436 void Workspace::restackClientUnderActive( Client* c )
00437 {
00438 if( c->isTopMenu())
00439 return;
00440 if( !active_client || active_client == c )
00441 {
00442 raiseClient( c );
00443 return;
00444 }
00445
00446 assert( unconstrained_stacking_order.contains( active_client ));
00447 if( Client::belongToSameApplication( active_client, c ))
00448 {
00449 unconstrained_stacking_order.removeAll( c );
00450 unconstrained_stacking_order.insert( unconstrained_stacking_order.indexOf( active_client ), c );
00451 }
00452 else
00453 {
00454 for( ClientList::Iterator it = unconstrained_stacking_order.begin();
00455 it != unconstrained_stacking_order.end();
00456 ++it )
00457 {
00458 if( Client::belongToSameApplication( active_client, *it ))
00459 {
00460 if( *it != c )
00461 {
00462 unconstrained_stacking_order.removeAll( c );
00463 unconstrained_stacking_order.insert( it, c );
00464 }
00465 break;
00466 }
00467 }
00468 }
00469 assert( unconstrained_stacking_order.contains( c ));
00470 for( int desktop = 1;
00471 desktop <= numberOfDesktops();
00472 ++desktop )
00473 {
00474 if( c->wantsTabFocus() && c->isOnDesktop( desktop ) && focus_chain[ desktop ].contains( active_client ))
00475 {
00476 if( Client::belongToSameApplication( active_client, c ))
00477 {
00478 focus_chain[ desktop ].removeAll( c );
00479 focus_chain[ desktop ].insert( focus_chain[ desktop ].indexOf( active_client ), c );
00480 }
00481 else
00482 {
00483 focus_chain[ desktop ].removeAll( c );
00484 for( int i = focus_chain[ desktop ].size() - 1;
00485 i >= 0;
00486 --i )
00487 {
00488 if( Client::belongToSameApplication( active_client, focus_chain[ desktop ].at( i )))
00489 {
00490 focus_chain[ desktop ].insert( i, c );
00491 break;
00492 }
00493 }
00494 }
00495 }
00496 }
00497
00498 if( c->wantsTabFocus() && global_focus_chain.contains( active_client ))
00499 {
00500 if( Client::belongToSameApplication( active_client, c ))
00501 {
00502 global_focus_chain.removeAll( c );
00503 global_focus_chain.insert( global_focus_chain.indexOf( active_client ), c );
00504 }
00505 else
00506 {
00507 global_focus_chain.removeAll( c );
00508 for ( int i = global_focus_chain.size() - 1;
00509 i >= 0;
00510 --i )
00511 {
00512 if( Client::belongToSameApplication( active_client, global_focus_chain.at( i ) ))
00513 {
00514 global_focus_chain.insert( i, c );
00515 break;
00516 }
00517 }
00518 }
00519 }
00520 updateStackingOrder();
00521 }
00522
00523 void Workspace::restoreSessionStackingOrder( Client* c )
00524 {
00525 if( c->sessionStackingOrder() < 0 )
00526 return;
00527 StackingUpdatesBlocker blocker( this );
00528 unconstrained_stacking_order.removeAll( c );
00529 ClientList::Iterator best_pos = unconstrained_stacking_order.end();
00530 for( ClientList::Iterator it = unconstrained_stacking_order.begin();
00531 it != unconstrained_stacking_order.end();
00532 ++it )
00533 {
00534 if( (*it)->sessionStackingOrder() > c->sessionStackingOrder() )
00535 {
00536 unconstrained_stacking_order.insert( it, c );
00537 return;
00538 }
00539 }
00540 unconstrained_stacking_order.append( c );
00541 }
00542
00543 void Workspace::circulateDesktopApplications()
00544 {
00545 if ( desktops.count() > 1 )
00546 {
00547 bool change_active = activeClient()->isDesktop();
00548 raiseClient( findDesktop( false, currentDesktop()));
00549 if( change_active )
00550 activateClient( findDesktop( true, currentDesktop()));
00551 }
00552
00553 if( desktops.count() > 0 && activeClient() == NULL && should_get_focus.count() == 0 )
00554 activateClient( findDesktop( true, currentDesktop()));
00555 }
00556
00557
00561 ClientList Workspace::constrainedStackingOrder()
00562 {
00563 ClientList layer[ NumLayers ];
00564
00565 #if 0
00566 kDebug(1212) << "stacking1:";
00567 for( ClientList::ConstIterator it = unconstrained_stacking_order.begin();
00568 it != unconstrained_stacking_order.end();
00569 ++it )
00570 kDebug(1212) << (void*)(*it) << *it << ":" << (*it)->layer();
00571 #endif
00572
00573 QHash< Group*, Layer > minimum_layer;
00574 for( ClientList::ConstIterator it = unconstrained_stacking_order.constBegin();
00575 it != unconstrained_stacking_order.constEnd();
00576 ++it )
00577 {
00578 Layer l = (*it)->layer();
00579
00580
00581
00582 if( minimum_layer.contains( (*it)->group())
00583 && minimum_layer[ (*it)->group() ] == ActiveLayer
00584 && ( l == NormalLayer || l == AboveLayer ))
00585 {
00586 l = minimum_layer[ (*it)->group() ];
00587 }
00588 minimum_layer[ (*it)->group() ] = l;
00589 layer[ l ].append( *it );
00590 }
00591 ClientList stacking;
00592 for( Layer lay = FirstLayer;
00593 lay < NumLayers;
00594 ++lay )
00595 stacking += layer[ lay ];
00596 #if 0
00597 kDebug(1212) << "stacking2:";
00598 for( ClientList::ConstIterator it = stacking.begin();
00599 it != stacking.end();
00600 ++it )
00601 kDebug(1212) << (void*)(*it) << *it << ":" << (*it)->layer();
00602 #endif
00603
00604
00605 for( int i = stacking.size() - 1;
00606 i >= 0;
00607 )
00608 {
00609 if( !stacking[ i ]->isTransient())
00610 {
00611 --i;
00612 continue;
00613 }
00614 int i2 = -1;
00615 if( stacking[ i ]->groupTransient())
00616 {
00617 if( stacking[ i ]->group()->members().count() > 0 )
00618 {
00619 for( i2 = stacking.size() - 1;
00620 i2 >= 0;
00621 --i2 )
00622 {
00623 if( stacking[ i2 ] == stacking[ i ] )
00624 {
00625 i2 = -1;
00626 break;
00627 }
00628 if( stacking[ i2 ]->hasTransient( stacking[ i ], true )
00629 && keepTransientAbove( stacking[ i2 ], stacking[ i ] ))
00630 break;
00631 }
00632 }
00633 }
00634 else
00635 {
00636 for( i2 = stacking.size() - 1;
00637 i2 >= 0;
00638 --i2 )
00639 {
00640 if( stacking[ i2 ] == stacking[ i ] )
00641 {
00642 i2 = -1;
00643 break;
00644 }
00645 if( stacking[ i2 ] == stacking[ i ]->transientFor()
00646 && keepTransientAbove( stacking[ i2 ], stacking[ i ] ))
00647 break;
00648 }
00649 }
00650 if( i2 == -1 )
00651 {
00652 --i;
00653 continue;
00654 }
00655 Client* current = stacking[ i ];
00656 stacking.removeAt( i );
00657 --i;
00658 --i2;
00659 if( !current->transients().isEmpty())
00660 i = i2;
00661 ++i2;
00662 stacking.insert( i2, current );
00663 }
00664 #if 0
00665 kDebug(1212) << "stacking3:";
00666 for( ClientList::ConstIterator it = stacking.begin();
00667 it != stacking.end();
00668 ++it )
00669 kDebug(1212) << (void*)(*it) << *it << ":" << (*it)->layer();
00670 kDebug(1212) << "\n\n";
00671 #endif
00672 return stacking;
00673 }
00674
00675 void Workspace::blockStackingUpdates( bool block )
00676 {
00677 if( block )
00678 {
00679 if( block_stacking_updates == 0 )
00680 blocked_propagating_new_clients = false;
00681 ++block_stacking_updates;
00682 }
00683 else
00684 if( --block_stacking_updates == 0 )
00685 updateStackingOrder( blocked_propagating_new_clients );
00686 }
00687
00688
00689 ClientList Workspace::ensureStackingOrder( const ClientList& list ) const
00690 {
00691
00692 if( list.count() < 2 )
00693 return list;
00694
00695 ClientList result = list;
00696 for( ClientList::ConstIterator it = stacking_order.constBegin();
00697 it != stacking_order.constEnd();
00698 ++it )
00699 if( result.removeAll( *it ) != 0 )
00700 result.append( *it );
00701 return result;
00702 }
00703
00704
00705
00706 bool Workspace::keepTransientAbove( const Client* mainwindow, const Client* transient )
00707 {
00708
00709
00710
00711
00712 if( mainwindow->isTopMenu() && transient->groupTransient())
00713 return false;
00714
00715 if( transient->isSplash() && mainwindow->isDialog())
00716 return false;
00717
00718
00719
00720
00721 if( transient->isDialog() && !transient->isModal() && transient->groupTransient())
00722 return false;
00723
00724
00725 if( mainwindow->isDock())
00726 return false;
00727 return true;
00728 }
00729
00730
00731 ToplevelList Workspace::xStackingOrder() const
00732 {
00733 if( !x_stacking_dirty )
00734 return x_stacking;
00735 x_stacking_dirty = false;
00736 x_stacking.clear();
00737 Window dummy;
00738 Window* windows = NULL;
00739 unsigned int count = 0;
00740 XQueryTree( display(), rootWindow(), &dummy, &dummy, &windows, &count );
00741
00742 foreach( Client* c, stacking_order )
00743 x_stacking.append( c );
00744 for( unsigned int i = 0;
00745 i < count;
00746 ++i )
00747 {
00748 if( Unmanaged* c = findUnmanaged( WindowMatchPredicate( windows[ i ] )))
00749 x_stacking.append( c );
00750 }
00751 foreach( Deleted* c, deleted )
00752 x_stacking.append( c );
00753 if( windows != NULL )
00754 XFree( windows );
00755 const_cast< Workspace* >( this )->checkUnredirect();
00756 return x_stacking;
00757 }
00758
00759
00760
00761
00762
00763 void Client::restackWindow( Window , int detail, NET::RequestSource src, Time timestamp, bool send_event )
00764 {
00765 switch ( detail )
00766 {
00767 case Above:
00768 case TopIf:
00769 workspace()->raiseClientRequest( this, src, timestamp );
00770 break;
00771 case Below:
00772 case BottomIf:
00773 workspace()->lowerClientRequest( this, src, timestamp );
00774 break;
00775 case Opposite:
00776 default:
00777 break;
00778 }
00779 if( send_event )
00780 sendSyntheticConfigureNotify();
00781 }
00782
00783 void Client::setKeepAbove( bool b )
00784 {
00785 b = rules()->checkKeepAbove( b );
00786 if( b && !rules()->checkKeepBelow( false ))
00787 setKeepBelow( false );
00788 if ( b == keepAbove())
00789 {
00790 if( bool( info->state() & NET::KeepAbove ) != keepAbove())
00791 info->setState( keepAbove() ? NET::KeepAbove : 0, NET::KeepAbove );
00792 return;
00793 }
00794 keep_above = b;
00795 info->setState( keepAbove() ? NET::KeepAbove : 0, NET::KeepAbove );
00796 if( decoration != NULL )
00797 decoration->emitKeepAboveChanged( keepAbove());
00798 workspace()->updateClientLayer( this );
00799 updateWindowRules();
00800 }
00801
00802 void Client::setKeepBelow( bool b )
00803 {
00804 b = rules()->checkKeepBelow( b );
00805 if( b && !rules()->checkKeepAbove( false ))
00806 setKeepAbove( false );
00807 if ( b == keepBelow())
00808 {
00809 if( bool( info->state() & NET::KeepBelow ) != keepBelow())
00810 info->setState( keepBelow() ? NET::KeepBelow : 0, NET::KeepBelow );
00811 return;
00812 }
00813 keep_below = b;
00814 info->setState( keepBelow() ? NET::KeepBelow : 0, NET::KeepBelow );
00815 if( decoration != NULL )
00816 decoration->emitKeepBelowChanged( keepBelow());
00817 workspace()->updateClientLayer( this );
00818 updateWindowRules();
00819 }
00820
00821 Layer Client::layer() const
00822 {
00823 if( in_layer == UnknownLayer )
00824 const_cast< Client* >( this )->in_layer = belongsToLayer();
00825 return in_layer;
00826 }
00827
00828 Layer Client::belongsToLayer() const
00829 {
00830 if( isDesktop())
00831 return DesktopLayer;
00832 if( isSplash())
00833 return NormalLayer;
00834 if( isDock() && keepBelow())
00835
00836
00837
00838 return NormalLayer;
00839 if( keepBelow())
00840 return BelowLayer;
00841 if( isDock() && !keepBelow())
00842 return DockLayer;
00843 if( isTopMenu())
00844 return DockLayer;
00845 if( isActiveFullScreen())
00846 return ActiveLayer;
00847 if( keepAbove())
00848 return AboveLayer;
00849 return NormalLayer;
00850 }
00851
00852 bool Client::isActiveFullScreen() const
00853 {
00854
00855
00856 const Client* ac = workspace()->mostRecentlyActivatedClient();
00857 const Client* top = workspace()->topClientOnDesktop( workspace()->currentDesktop(), screen(), true, false );
00858 return( isFullScreen() && ac != NULL && top != NULL
00859
00860 && ( top == this || this->group() == top->group()));
00861 }
00862
00863 }