backsteppingsrv/src/bsserver.cpp
changeset 0 79c6a41cd166
child 101 9e077f9a342c
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  BS Server - implemenration of CServer2 
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <eikenv.h>
       
    20 #include <eikappui.h>
       
    21 #include <apgwgnam.h>
       
    22 #include <e32base.h>
       
    23 
       
    24 #include "bsengineglobals.h"
       
    25 #include "bsengine.h"
       
    26 #include "bsserversession.h"
       
    27 #include "bsdebug.h"
       
    28 #include "bsserver.h"
       
    29 
       
    30 const TInt idsArrayGranularity = 30;
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // C++ default constructor can NOT contain any code, that might leave.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CBSServer::CBSServer( TInt aPriority ) :
       
    39     CServer2(aPriority)
       
    40     {
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // Symbian 2nd phase constructor can leave.
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 void CBSServer::ConstructL()
       
    48     {
       
    49     DEBUG(("Start BS Server" ));
       
    50     iEngine = CBSEngine::NewL( );
       
    51     StartL( KBSEngineName );
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // Two-phased constructor.
       
    56 // -------------------------------------- ---------------------------------------
       
    57 //
       
    58 CBSServer* CBSServer::NewLC()
       
    59     {
       
    60     CBSServer* self = new( ELeave ) CBSServer( EPriorityNormal );
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL( );
       
    63     return self;
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // Destructor.
       
    68 // -------------------------------------- ---------------------------------------
       
    69 //
       
    70 CBSServer::~CBSServer()
       
    71     {
       
    72     DEBUG(("Destroy BS Server" ));
       
    73     delete iEngine;
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // Create new session.
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 CSession2* CBSServer::NewSessionL( const TVersion& aVersion,
       
    81     const RMessage2& /*aMessage*/ ) const
       
    82     {
       
    83     // Check we're the right version
       
    84     if ( !User::QueryVersionSupported( TVersion( KBSEngineMajorVersionNumber,
       
    85         KBSEngineMinorVersionNumber, KBSEngineBuildVersionNumber ), aVersion ) )
       
    86         {
       
    87         User::Leave( KErrNotSupported );
       
    88         }
       
    89 
       
    90     return CBSServerSession::NewL( ( CBSServer* ) this );
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // Increment sessions.
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 void CBSServer::IncrementSessions()
       
    98     {
       
    99     iSessionCount++;
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // Decrement sessions.
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 void CBSServer::DecrementSessions()
       
   107     {
       
   108     iSessionCount--;
       
   109     }
       
   110 // -----------------------------------------------------------------------------
       
   111 // RunError is called when RunL leaves.
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 TInt CBSServer::RunError( TInt aError )
       
   115     {
       
   116     DEBUG(("CBSServer::RunError %d", aError ));
       
   117     if ( aError == KErrBadDescriptor )
       
   118         {
       
   119         PanicClient( Message( ), EBSEngineBadDescriptor );
       
   120         }
       
   121     else
       
   122         {
       
   123         Message().Complete( aError );
       
   124         }
       
   125 
       
   126     // The leave will result in an early return from CServer::RunL(), skipping
       
   127     // the call to request another message. So do that now in order to keep the
       
   128     // server running.
       
   129     ReStart( );
       
   130     // Handled the error fully
       
   131     return KErrNone;
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // Panic client.
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void CBSServer::PanicClient( const RMessage2& aMessage, TBSEnginePanic aPanic )
       
   139     {
       
   140     aMessage.Panic( KBSEngineName, aPanic );
       
   141     }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // Panic server.
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 void CBSServer::PanicServer( TBSEnginePanic aPanic )
       
   148     {
       
   149     User::Panic( KBSEngineName, aPanic );
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // Create and start the server.
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 void CBSServer::ThreadFunctionL()
       
   157     {
       
   158     CBSAppUi* ui = new (ELeave) CBSAppUi;
       
   159     ui->ConstructL( );
       
   160 
       
   161     // Construct our server        
       
   162     CBSServer* server = CBSServer::NewLC( );
       
   163 
       
   164     // set engine to AppUI to get notification about focus changes
       
   165     ui->iEngine = &(server->Engine( ));
       
   166 
       
   167     RProcess::Rendezvous( KErrNone );
       
   168 
       
   169     // Start handling requests
       
   170     CActiveScheduler::Start( );
       
   171 
       
   172     CleanupStack::PopAndDestroy( server );
       
   173 
       
   174     ui->PrepareToExit( );
       
   175 
       
   176     }
       
   177 // -----------------------------------------------------------------------------
       
   178 // Create and start the server.
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 TInt CBSServer::ThreadFunction()
       
   182     {
       
   183     __UHEAP_MARK;
       
   184 
       
   185     User::RenameThread( KBSEngineThreadName );
       
   186 
       
   187     CBSEikonEnv* env = new CBSEikonEnv;
       
   188     __ASSERT_ALWAYS( env, PanicServer( EBSEngineEIkonEnv ) );
       
   189 
       
   190     TRAPD(error, env->ConstructL(););
       
   191     __ASSERT_ALWAYS( !error, PanicServer( EBSEngineEIkonEnv ) );
       
   192 
       
   193     TRAPD( err, ThreadFunctionL() );
       
   194     if ( err != KErrNone )
       
   195         {
       
   196         PanicServer( EBSEngineSrvCreateServer );
       
   197         }
       
   198 
       
   199     env->DestroyEnvironment( );
       
   200 
       
   201     __UHEAP_MARKEND;
       
   202     return KErrNone;
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // Getter of engine
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 CBSEngine& CBSServer::Engine()
       
   210     {
       
   211     return *iEngine;
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // 
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CBSServer::CBSEikonEnv::DestroyEnvironment()
       
   219     {
       
   220     CEikonEnv::DestroyEnvironment( );
       
   221     }
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // 
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 void CBSServer::CBSEikonEnv::ConstructL()
       
   228     {
       
   229     CEikonEnv::ConstructL( EFalse ); 
       
   230     SetAutoForwarding(ETrue); 
       
   231     User::SetPriorityControl(EFalse);
       
   232     }
       
   233 // -----------------------------------------------------------------------------
       
   234 // 
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 CBSServer::CBSAppUi::~CBSAppUi()
       
   238     {
       
   239     }
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // 
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 void CBSServer::CBSAppUi::HandleWsEventL( const TWsEvent& aEvent,
       
   246     CCoeControl* /*aDestination*/)
       
   247     {
       
   248     if ( aEvent.Type( ) == EEventFocusGroupChanged 
       
   249         || aEvent.Type( ) == EEventWindowGroupsChanged )
       
   250         {
       
   251 
       
   252         TInt windowsGroupID = iCoeEnv->WsSession().GetFocusWindowGroup( );
       
   253         CApaWindowGroupName* apaWGName = CApaWindowGroupName::NewLC(
       
   254             iCoeEnv->WsSession( ), windowsGroupID );
       
   255         TUid uid = apaWGName->AppUid( );
       
   256         CleanupStack::PopAndDestroy( apaWGName );
       
   257         // if UID is 0, ignore event. 
       
   258         // Next event should bring correct information
       
   259         if ( uid.iUid )
       
   260             {
       
   261             iEngine->HandleFocusChangeL( uid );
       
   262             }
       
   263         }
       
   264     else if( aEvent.Type( ) == EEventKeyDown ) 
       
   265         {
       
   266         iEngine->ApplicationKeyWasPressed( );
       
   267         ForwardEventL( aEvent );     
       
   268         }
       
   269     else if( aEvent.Type( ) == EEventKeyUp )
       
   270         {
       
   271         ForwardEventL( aEvent );     
       
   272         }
       
   273     }
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 // 
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 void CBSServer::CBSAppUi::ForwardEventL( const TWsEvent& aEvent )
       
   280     {
       
   281     CArrayFixFlat<TInt>* idArray = 
       
   282                     new (ELeave) CArrayFixFlat<TInt>( idsArrayGranularity );
       
   283     iCoeEnv->WsSession().WindowGroupList( idArray );
       
   284     
       
   285     TInt ownId = iCoeEnv->RootWin().Identifier();
       
   286     for( TInt x(0); x < idArray->Count(); x++ )
       
   287         {
       
   288         if( (*idArray)[x] != ownId )
       
   289             {
       
   290             iCoeEnv->WsSession().SendEventToWindowGroup( 
       
   291                                                     (*idArray)[x], aEvent );
       
   292             }
       
   293         }
       
   294     delete idArray;
       
   295     }
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // 
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 void CBSServer::CBSAppUi::ConstructL()
       
   302     {
       
   303     CEikAppUi::BaseConstructL( ENoAppResourceFile | ENoScreenFurniture );
       
   304     //get notifications about focus change events    
       
   305     iCoeEnv->RootWin().EnableFocusChangeEvents( );
       
   306     //get notifications about windows group change events    
       
   307     iCoeEnv->RootWin().EnableGroupChangeEvents( );
       
   308     //disable notifications about layout change
       
   309     iCoeEnv->RootWin().DisableScreenChangeEvents();
       
   310     //get notifications about application key event
       
   311     iCoeEnv->RootWin().CaptureKeyUpAndDowns( EStdKeyApplication0, 0, 0 );
       
   312     }
       
   313 
       
   314 // ============================= LOCAL FUNCTIONS ===============================
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // E32Main item point.
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 TInt E32Main()
       
   321     {
       
   322     return CBSServer::ThreadFunction( );
       
   323     }
       
   324 
       
   325 // End of File
       
   326