00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <tqpopupmenu.h>
00020 #include <kxerrorhandler.h>
00021 #include <kstartupinfo.h>
00022 #include <kstringhandler.h>
00023 #include <klocale.h>
00024
00025 #include "client.h"
00026 #include "workspace.h"
00027 #include <fixx11h.h>
00028
00029 #include "notifications.h"
00030 #include "atoms.h"
00031 #include "group.h"
00032 #include "rules.h"
00033
00034 namespace KWinInternal
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
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00214 void Workspace::setActiveClient( Client* c, allowed_t )
00215 {
00216 if ( active_client == c )
00217 return;
00218 if( active_popup && active_popup_client != c && set_active_client_recursion == 0 )
00219 closeActivePopup();
00220 StackingUpdatesBlocker blocker( this );
00221 ++set_active_client_recursion;
00222 updateFocusMousePosition( TQCursor::pos());
00223 if( active_client != NULL )
00224 {
00225 active_client->setActive( false, !c || !c->isModal() || c != active_client->transientFor() );
00226 }
00227 active_client = c;
00228 if (set_active_client_recursion == 1)
00229 {
00230
00231
00232
00233 next_active_client = NULL;
00234 }
00235 Q_ASSERT( c == NULL || c->isActive());
00236 if( active_client != NULL )
00237 last_active_client = active_client;
00238 if ( active_client )
00239 {
00240 updateFocusChains( active_client, FocusChainMakeFirst );
00241 active_client->demandAttention( false );
00242 }
00243 pending_take_activity = NULL;
00244
00245 updateCurrentTopMenu();
00246 updateToolWindows( false );
00247 if( c )
00248 disableGlobalShortcutsForClient( c->rules()->checkDisableGlobalShortcuts( false ));
00249 else
00250 disableGlobalShortcutsForClient( false );
00251
00252 updateStackingOrder();
00253
00254 rootInfo->setActiveWindow( active_client? active_client->window() : 0 );
00255 updateColormap();
00256 --set_active_client_recursion;
00257 }
00258
00270 void Workspace::activateClient( Client* c, bool force )
00271 {
00272 if( c == NULL )
00273 {
00274 focusToNull();
00275 setActiveClient( NULL, Allowed );
00276 return;
00277 }
00278 raiseClient( c );
00279 if (!c->isOnDesktop(currentDesktop()) )
00280 {
00281 ++block_focus;
00282 setCurrentDesktop( c->desktop() );
00283 --block_focus;
00284 }
00285 if( c->isMinimized())
00286 c->unminimize();
00287
00288
00289 if( options->focusPolicyIsReasonable() || force )
00290 requestFocus( c, force );
00291
00292
00293
00294
00295
00296
00297
00298
00299 if( !c->ignoreFocusStealing())
00300 c->updateUserTime();
00301 }
00302
00310 void Workspace::requestFocus( Client* c, bool force )
00311 {
00312 takeActivity( c, ActivityFocus | ( force ? ActivityFocusForce : 0 ), false);
00313 }
00314
00315 void Workspace::takeActivity( Client* c, int flags, bool handled )
00316 {
00317
00318 if (!focusChangeEnabled() && ( c != active_client) )
00319 flags &= ~ActivityFocus;
00320
00321 if ( !c )
00322 {
00323 focusToNull();
00324 return;
00325 }
00326
00327 if( flags & ActivityFocus )
00328 {
00329 Client* modal = c->findModal();
00330 if( modal != NULL && modal != c )
00331 {
00332 next_active_client = modal;
00333 if( !modal->isOnDesktop( c->desktop()))
00334 {
00335 modal->setDesktop( c->desktop());
00336 if( modal->desktop() != c->desktop())
00337 activateClient( modal );
00338 }
00339
00340
00341
00342
00343 if( flags & ActivityRaise )
00344 raiseClient( c );
00345 c = modal;
00346 handled = false;
00347 }
00348 cancelDelayFocus();
00349 }
00350 if ( !( flags & ActivityFocusForce ) && ( c->isTopMenu() || c->isDock() || c->isSplash()) )
00351 flags &= ~ActivityFocus;
00352 if( c->isShade())
00353 {
00354 if( c->wantsInput() && ( flags & ActivityFocus ))
00355 {
00356
00357 c->setActive( true );
00358 focusToNull();
00359 }
00360 if( c->wantsInput())
00361 next_active_client = c;
00362 flags &= ~ActivityFocus;
00363 handled = false;
00364 }
00365 if( !c->isShown( true ))
00366 {
00367 next_active_client = c;
00368 kdWarning( 1212 ) << "takeActivity: not shown" << endl;
00369 return;
00370 }
00371 c->takeActivity( flags, handled, Allowed );
00372 if( !c->isOnScreen( active_screen ))
00373 active_screen = c->screen();
00374 }
00375
00376 void Workspace::handleTakeActivity( Client* c, Time , int flags )
00377 {
00378 if( pending_take_activity != c )
00379 return;
00380 if(( flags & ActivityRaise ) != 0 )
00381 raiseClient( c );
00382 if(( flags & ActivityFocus ) != 0 && c->isShown( false ))
00383 c->takeFocus( Allowed );
00384 pending_take_activity = NULL;
00385 }
00386
00394 void Workspace::clientHidden( Client* c )
00395 {
00396 assert( !c->isShown( true ) || !c->isOnCurrentDesktop());
00397 activateNextClient( c );
00398 }
00399
00400
00401 bool Workspace::activateNextClient( Client* c )
00402 {
00403
00404 if( !( c == active_client
00405 || ( should_get_focus.count() > 0 && c == should_get_focus.last())))
00406 return false;
00407 closeActivePopup();
00408 if( c != NULL )
00409 {
00410 if( c == active_client )
00411 setActiveClient( NULL, Allowed );
00412 should_get_focus.remove( c );
00413 }
00414 if( focusChangeEnabled())
00415 {
00416 if ( options->focusPolicyIsReasonable())
00417 {
00418
00419 Client* get_focus = NULL;
00420 const ClientList mainwindows = ( c != NULL ? c->mainClients() : ClientList());
00421 for( ClientList::ConstIterator it = focus_chain[currentDesktop()].fromLast();
00422 it != focus_chain[currentDesktop()].end();
00423 --it )
00424 {
00425 if( !(*it)->isShown( false ) || !(*it)->isOnCurrentDesktop())
00426 continue;
00427 if( options->separateScreenFocus )
00428 {
00429 if( c != NULL && !(*it)->isOnScreen( c->screen()))
00430 continue;
00431 if( c == NULL && !(*it)->isOnScreen( activeScreen()))
00432 continue;
00433 }
00434 if( mainwindows.contains( *it ))
00435 {
00436 get_focus = *it;
00437 break;
00438 }
00439 if( get_focus == NULL )
00440 get_focus = *it;
00441 }
00442 if( get_focus == NULL )
00443 get_focus = findDesktop( true, currentDesktop());
00444 if( get_focus != NULL )
00445 requestFocus( get_focus );
00446 else
00447 focusToNull();
00448 }
00449 else
00450 return false;
00451 }
00452 else
00453
00454
00455 focusToNull();
00456 return true;
00457 }
00458
00459 void Workspace::setCurrentScreen( int new_screen )
00460 {
00461 if (new_screen < 0 || new_screen > numScreens())
00462 return;
00463 if ( !options->focusPolicyIsReasonable())
00464 return;
00465 closeActivePopup();
00466 Client* get_focus = NULL;
00467 for( ClientList::ConstIterator it = focus_chain[currentDesktop()].fromLast();
00468 it != focus_chain[currentDesktop()].end();
00469 --it )
00470 {
00471 if( !(*it)->isShown( false ) || !(*it)->isOnCurrentDesktop())
00472 continue;
00473 if( !(*it)->screen() == new_screen )
00474 continue;
00475 get_focus = *it;
00476 break;
00477 }
00478 if( get_focus == NULL )
00479 get_focus = findDesktop( true, currentDesktop());
00480 if( get_focus != NULL && get_focus != mostRecentlyActivatedClient())
00481 requestFocus( get_focus );
00482 active_screen = new_screen;
00483 }
00484
00485 void Workspace::gotFocusIn( const Client* c )
00486 {
00487 if( should_get_focus.contains( const_cast< Client* >( c )))
00488 {
00489
00490 while( should_get_focus.first() != c )
00491 should_get_focus.pop_front();
00492 should_get_focus.pop_front();
00493 }
00494 }
00495
00496 void Workspace::setShouldGetFocus( Client* c )
00497 {
00498 should_get_focus.append( c );
00499 updateStackingOrder();
00500 }
00501
00502
00503
00504 bool Workspace::allowClientActivation( const Client* c, Time time, bool focus_in )
00505 {
00506
00507
00508
00509
00510
00511
00512
00513
00514 if( time == -1U )
00515 time = c->userTime();
00516 int level = c->rules()->checkFSP( options->focusStealingPreventionLevel );
00517 if( session_saving && level <= 2 )
00518 {
00519 return true;
00520 }
00521 Client* ac = mostRecentlyActivatedClient();
00522 if( focus_in )
00523 {
00524 if( should_get_focus.contains( const_cast< Client* >( c )))
00525 return true;
00526
00527
00528 ac = last_active_client;
00529 }
00530 if( time == 0 )
00531 return false;
00532 if( level == 0 )
00533 return true;
00534 if( level == 4 )
00535 return false;
00536 if( !c->isOnCurrentDesktop())
00537 return false;
00538 if( c->ignoreFocusStealing())
00539 return true;
00540 if( ac == NULL || ac->isDesktop())
00541 {
00542
00543 return true;
00544 }
00545
00546 if( Client::belongToSameApplication( c, ac, true ))
00547 {
00548
00549 return true;
00550 }
00551 if( level == 3 )
00552 return false;
00553 if( time == -1U )
00554 {
00555
00556 if( level == 1 )
00557 return true;
00558
00559
00560
00561 return false;
00562 }
00563
00564 Time user_time = ac->userTime();
00565
00566
00567 return timestampCompare( time, user_time ) >= 0;
00568 }
00569
00570
00571
00572
00573
00574 bool Workspace::allowFullClientRaising( const Client* c, Time time )
00575 {
00576 int level = c->rules()->checkFSP( options->focusStealingPreventionLevel );
00577 if( session_saving && level <= 2 )
00578 {
00579 return true;
00580 }
00581 Client* ac = mostRecentlyActivatedClient();
00582 if( level == 0 )
00583 return true;
00584 if( level == 4 )
00585 return false;
00586 if( ac == NULL || ac->isDesktop())
00587 {
00588
00589 return true;
00590 }
00591 if( c->ignoreFocusStealing())
00592 return true;
00593
00594 if( Client::belongToSameApplication( c, ac, true ))
00595 {
00596
00597 return true;
00598 }
00599 if( level == 3 )
00600 return false;
00601 Time user_time = ac->userTime();
00602
00603
00604 return timestampCompare( time, user_time ) >= 0;
00605 }
00606
00607
00608
00609 void Workspace::restoreFocus()
00610 {
00611
00612
00613
00614
00615 updateXTime();
00616 if( should_get_focus.count() > 0 )
00617 requestFocus( should_get_focus.last());
00618 else if( last_active_client )
00619 requestFocus( last_active_client );
00620 }
00621
00622 void Workspace::clientAttentionChanged( Client* c, bool set )
00623 {
00624 if( set )
00625 {
00626 attention_chain.remove( c );
00627 attention_chain.prepend( c );
00628 }
00629 else
00630 attention_chain.remove( c );
00631 }
00632
00633
00634
00635
00636 bool Workspace::fakeRequestedActivity( Client* c )
00637 {
00638 if( should_get_focus.count() > 0 && should_get_focus.last() == c )
00639 {
00640 if( c->isActive())
00641 return false;
00642 c->setActive( true );
00643 return true;
00644 }
00645 return false;
00646 }
00647
00648 void Workspace::unfakeActivity( Client* c )
00649 {
00650 if( should_get_focus.count() > 0 && should_get_focus.last() == c )
00651 {
00652 if( last_active_client != NULL )
00653 last_active_client->setActive( true );
00654 else
00655 c->setActive( false );
00656 }
00657 }
00658
00659
00660
00661
00662
00663
00670 void Client::updateUserTime( Time time )
00671 {
00672 if( time == CurrentTime )
00673 time = GET_QT_X_TIME();
00674 if( time != -1U
00675 && ( user_time == CurrentTime
00676 || timestampCompare( time, user_time ) > 0 ))
00677 user_time = time;
00678 group()->updateUserTime( user_time );
00679 }
00680
00681 Time Client::readUserCreationTime() const
00682 {
00683 long result = -1;
00684 Atom type;
00685 int format, status;
00686 unsigned long nitems = 0;
00687 unsigned long extra = 0;
00688 unsigned char *data = 0;
00689 KXErrorHandler handler;
00690 status = XGetWindowProperty( qt_xdisplay(), window(),
00691 atoms->kde_net_wm_user_creation_time, 0, 10000, FALSE, XA_CARDINAL,
00692 &type, &format, &nitems, &extra, &data );
00693 if (status == Success )
00694 {
00695 if (data && nitems > 0)
00696 result = *((long*) data);
00697 XFree(data);
00698 }
00699 return result;
00700 }
00701
00702 void Client::demandAttention( bool set )
00703 {
00704 if( isActive())
00705 set = false;
00706 if( demands_attention == set )
00707 return;
00708 demands_attention = set;
00709 if( demands_attention )
00710 {
00711
00712
00713
00714
00715
00716
00717 Notify::Event e = isOnCurrentDesktop() ? Notify::DemandAttentionCurrent : Notify::DemandAttentionOther;
00718
00719
00720 if( Notify::makeDemandAttention( e ))
00721 info->setState( set ? NET::DemandsAttention : 0, NET::DemandsAttention );
00722
00723 if( demandAttentionKNotifyTimer == NULL )
00724 {
00725 demandAttentionKNotifyTimer = new TQTimer( this );
00726 connect( demandAttentionKNotifyTimer, TQT_SIGNAL( timeout()), TQT_SLOT( demandAttentionKNotify()));
00727 }
00728 demandAttentionKNotifyTimer->start( 1000, true );
00729 }
00730 else
00731 info->setState( set ? NET::DemandsAttention : 0, NET::DemandsAttention );
00732 workspace()->clientAttentionChanged( this, set );
00733 }
00734
00735 void Client::demandAttentionKNotify()
00736 {
00737 Notify::Event e = isOnCurrentDesktop() ? Notify::DemandAttentionCurrent : Notify::DemandAttentionOther;
00738 Notify::raise( e, i18n( "Window '%1' demands attention." ).arg( KStringHandler::csqueeze(caption())), this );
00739 demandAttentionKNotifyTimer->stop();
00740 demandAttentionKNotifyTimer->deleteLater();
00741 demandAttentionKNotifyTimer = NULL;
00742 }
00743
00744
00745 KWIN_COMPARE_PREDICATE( SameApplicationActiveHackPredicate, const Client*,
00746
00747
00748 !cl->isSplash() && !cl->isToolbar() && !cl->isTopMenu() && !cl->isUtility() && !cl->isMenu()
00749 && Client::belongToSameApplication( cl, value, true ) && cl != value);
00750
00751 Time Client::readUserTimeMapTimestamp( const KStartupInfoId* asn_id, const KStartupInfoData* asn_data,
00752 bool session ) const
00753 {
00754 Time time = info->userTime();
00755
00756
00757
00758 if( asn_data != NULL && time != 0 )
00759 {
00760
00761 if( asn_id->timestamp() != 0
00762 && ( time == -1U || timestampCompare( asn_id->timestamp(), time ) > 0 ))
00763 {
00764 time = asn_id->timestamp();
00765 }
00766 else if( asn_data->timestamp() != -1U
00767 && ( time == -1U || timestampCompare( asn_data->timestamp(), time ) > 0 ))
00768 {
00769 time = asn_data->timestamp();
00770 }
00771 }
00772
00773 if( time == -1U )
00774 {
00775
00776
00777
00778
00779
00780
00781 Client* act = workspace()->mostRecentlyActivatedClient();
00782 if( act != NULL && !belongToSameApplication( act, this, true ))
00783 {
00784 bool first_window = true;
00785 if( isTransient())
00786 {
00787 if( act->hasTransient( this, true ))
00788 ;
00789
00790 else if( groupTransient() &&
00791 findClientInList( mainClients(), SameApplicationActiveHackPredicate( this )) == NULL )
00792 ;
00793 else
00794 first_window = false;
00795 }
00796 else
00797 {
00798 if( workspace()->findClient( SameApplicationActiveHackPredicate( this )))
00799 first_window = false;
00800 }
00801
00802 if( !first_window && rules()->checkFSP( options->focusStealingPreventionLevel ) > 0 )
00803 {
00804
00805 return 0;
00806 }
00807 }
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817 if( session )
00818 return -1U;
00819 if( ignoreFocusStealing() && act != NULL )
00820 time = act->userTime();
00821 else
00822 time = readUserCreationTime();
00823 }
00824
00825 return time;
00826 }
00827
00828 Time Client::userTime() const
00829 {
00830 Time time = user_time;
00831 if( time == 0 )
00832 return 0;
00833 assert( group() != NULL );
00834 if( time == -1U
00835 || ( group()->userTime() != -1U
00836 && timestampCompare( group()->userTime(), time ) > 0 ))
00837 time = group()->userTime();
00838 return time;
00839 }
00840
00852 void Client::setActive( bool act, bool updateOpacity_)
00853 {
00854 if ( active == act )
00855 return;
00856 active = act;
00857 workspace()->setActiveClient( act ? this : NULL, Allowed );
00858
00859 if (updateOpacity_) updateOpacity();
00860 if (isModal() && transientFor())
00861 {
00862 if (!act) transientFor()->updateOpacity();
00863 else if (!transientFor()->custom_opacity) transientFor()->setOpacity(options->translucentActiveWindows, options->activeWindowOpacity);
00864 }
00865 updateShadowSize();
00866
00867 if ( active )
00868 {
00869 Notify::raise( Notify::Activate );
00870 if (options->shadowEnabled(true))
00871 {
00872 if (options->shadowEnabled(false))
00873 {
00874
00875
00876 removeShadow();
00877 drawDelayedShadow();
00878 if (!isDesktop() &&
00879 this != workspace()->topClientOnDesktop(desktop()))
00880
00881
00882
00883 drawOverlappingShadows(true);
00884 }
00885 else
00886 drawShadow();
00887 }
00888 }
00889 else
00890 {
00891 removeShadow();
00892
00893 if (options->shadowEnabled(false))
00894 if (this == workspace()->topClientOnDesktop(desktop()))
00895 {
00896
00897
00898
00899
00900
00901 if ((shadowAfterClient = workspace()->activeClient()))
00902 drawShadowAfter(shadowAfterClient);
00903 }
00904 else
00905 drawDelayedShadow();
00906 }
00907
00908 if( !active )
00909 cancelAutoRaise();
00910
00911 if( !active && shade_mode == ShadeActivated )
00912 setShade( ShadeNormal );
00913
00914 StackingUpdatesBlocker blocker( workspace());
00915 workspace()->updateClientLayer( this );
00916
00917 ClientList mainclients = mainClients();
00918 for( ClientList::ConstIterator it = mainclients.begin();
00919 it != mainclients.end();
00920 ++it )
00921 if( (*it)->isFullScreen())
00922 workspace()->updateClientLayer( *it );
00923 if( decoration != NULL )
00924 decoration->activeChange();
00925 updateMouseGrab();
00926 updateUrgency();
00927 }
00928
00929 void Client::startupIdChanged()
00930 {
00931 KStartupInfoId asn_id;
00932 KStartupInfoData asn_data;
00933 bool asn_valid = workspace()->checkStartupNotification( window(), asn_id, asn_data );
00934 if( !asn_valid )
00935 return;
00936
00937
00938
00939 int desktop = workspace()->currentDesktop();
00940 if( asn_data.desktop() != 0 )
00941 desktop = asn_data.desktop();
00942 if( !isOnAllDesktops())
00943 workspace()->sendClientToDesktop( this, desktop, true );
00944 if( asn_data.xinerama() != -1 )
00945 workspace()->sendClientToScreen( this, asn_data.xinerama());
00946 Time timestamp = asn_id.timestamp();
00947 if( timestamp == 0 && asn_data.timestamp() != -1U )
00948 timestamp = asn_data.timestamp();
00949 if( timestamp != 0 )
00950 {
00951 bool activate = workspace()->allowClientActivation( this, timestamp );
00952 if( asn_data.desktop() != 0 && !isOnCurrentDesktop())
00953 activate = false;
00954 if( activate )
00955 workspace()->activateClient( this );
00956 else
00957 demandAttention();
00958 }
00959 }
00960
00961 void Client::updateUrgency()
00962 {
00963 if( urgency )
00964 demandAttention();
00965 }
00966
00967 void Client::shortcutActivated()
00968 {
00969 workspace()->activateClient( this, true );
00970 }
00971
00972
00973
00974
00975
00976 void Group::startupIdChanged()
00977 {
00978 KStartupInfoId asn_id;
00979 KStartupInfoData asn_data;
00980 bool asn_valid = workspace()->checkStartupNotification( leader_wid, asn_id, asn_data );
00981 if( !asn_valid )
00982 return;
00983 if( asn_id.timestamp() != 0 && user_time != -1U
00984 && timestampCompare( asn_id.timestamp(), user_time ) > 0 )
00985 {
00986 user_time = asn_id.timestamp();
00987 }
00988 else if( asn_data.timestamp() != -1U && user_time != -1U
00989 && timestampCompare( asn_data.timestamp(), user_time ) > 0 )
00990 {
00991 user_time = asn_data.timestamp();
00992 }
00993 }
00994
00995 void Group::updateUserTime( Time time )
00996 {
00997 if( time == CurrentTime )
00998 time = GET_QT_X_TIME();
00999 if( time != -1U
01000 && ( user_time == CurrentTime
01001 || timestampCompare( time, user_time ) > 0 ))
01002 user_time = time;
01003 }
01004
01005 }