contextframework/cfwplugins/displaysourceplugin/src/displayserviceuilayout.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2009-2009 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:   CDisplayServiceUILayout class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // SYSTEM INCLUDES
       
    20 #include <cfcontextobject.h>
       
    21 #include <cfcontextinterface.h>
       
    22 #include <apgwgnam.h>
       
    23 #include <coedef.h>
       
    24 
       
    25 // USER INCLUDES
       
    26 #include "displayserviceuilayout.h"
       
    27 #include "displaysourcecontextdef.h"
       
    28 #include "displaysourcepluginpanic.h"
       
    29 #include "trace.h"
       
    30 
       
    31 // ======================== LOCAL FUNCTIONS ========================
       
    32 
       
    33 #ifdef _DEBUG
       
    34 
       
    35 /** Rotation constants */
       
    36 _LIT( KGraphicsOrientationNormal, "Normal" );
       
    37 _LIT( KGraphicsOrientationRotated90, "Rotated 90" );
       
    38 _LIT( KGraphicsOrientationRotated180, "Rotated 180" );
       
    39 _LIT( KGraphicsOrientationRotated270, "Rotated 270" );
       
    40 
       
    41 LOCAL_C const TDesC& RotationAsDesC( TInt aRotation )
       
    42     {
       
    43     switch( aRotation )
       
    44         {
       
    45         case CFbsBitGc::EGraphicsOrientationNormal:
       
    46             {
       
    47             return KGraphicsOrientationNormal;
       
    48             }
       
    49         case CFbsBitGc::EGraphicsOrientationRotated90:
       
    50             {
       
    51             return KGraphicsOrientationRotated90;
       
    52             }
       
    53         case CFbsBitGc::EGraphicsOrientationRotated180:
       
    54             {
       
    55             return KGraphicsOrientationRotated180;
       
    56             }
       
    57         case CFbsBitGc::EGraphicsOrientationRotated270:
       
    58             {
       
    59             return KGraphicsOrientationRotated270;
       
    60             }
       
    61         default:
       
    62             {
       
    63             return KNullDesC;
       
    64             }
       
    65         }
       
    66     }
       
    67 
       
    68 #endif
       
    69 
       
    70 // ======================== MEMBER FUNCTIONS ========================
       
    71 
       
    72 // ======================== CDisplayServiceUILayout ========================
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // C++ constructor.
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 CDisplayServiceUILayout::CDisplayServiceUILayout( MCFContextInterface& aCF ):
       
    79     CDisplayServiceBase( CDisplayServiceBase::EDisplayUILayout, aCF ),
       
    80     iCurrentLayout( EUILayoutValueUnknown )
       
    81     {
       
    82     FUNC_LOG;
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // Symbian 2nd phase constructor.
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 void CDisplayServiceUILayout::ConstructL()
       
    90     {
       
    91     FUNC_LOG;
       
    92     
       
    93     // Call base class
       
    94     CDisplayServiceBase::ConstructL();
       
    95     
       
    96     // Configure context source and type
       
    97     iContext->SetSourceL( KDisplaySource );
       
    98     iContext->SetTypeL( KDisplayUILayoutType );
       
    99     
       
   100     // Define context:
       
   101     // Device: Display.UI.Layout
       
   102     TInt err = iCF.DefineContext(
       
   103         KDisplaySource, KDisplayUILayoutType, KDisplayUILayoutSec );
       
   104     User::LeaveIfError( err );
       
   105     
       
   106     // Define context:
       
   107     // Device: Display.UI.Rotation
       
   108     err = iCF.DefineContext(
       
   109         KDisplaySource, KDisplayUIRotationType, KDisplayUIRotationSec );
       
   110     User::LeaveIfError( err );
       
   111     
       
   112     // Create window server event handler
       
   113     iWsEventHandler = CWsEventHandler::NewL( *this );
       
   114     }
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // Symbian two phased constructor.
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 CDisplayServiceUILayout* CDisplayServiceUILayout::NewL(
       
   121     MCFContextInterface& aCF )
       
   122     {
       
   123     FUNC_LOG;
       
   124 
       
   125     CDisplayServiceUILayout* self = CDisplayServiceUILayout::NewLC( aCF );
       
   126     CleanupStack::Pop( self );
       
   127     
       
   128     return self;
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // Symbian two phased constructor.
       
   133 // Leaves pointer in the cleanup stack.
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 CDisplayServiceUILayout* CDisplayServiceUILayout::NewLC(
       
   137     MCFContextInterface& aCF )
       
   138     {
       
   139     FUNC_LOG;
       
   140 
       
   141     CDisplayServiceUILayout* self =
       
   142         new ( ELeave ) CDisplayServiceUILayout( aCF );
       
   143     CleanupStack::PushL( self );
       
   144     self->ConstructL();
       
   145     
       
   146     return self;
       
   147     }
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // C++ destructor.
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 CDisplayServiceUILayout::~CDisplayServiceUILayout()
       
   154     {
       
   155     FUNC_LOG;
       
   156 
       
   157     delete iWsEventHandler;
       
   158     }
       
   159 
       
   160 //------------------------------------------------------------------------------
       
   161 // CDisplayServiceUILayout::StartL
       
   162 //------------------------------------------------------------------------------
       
   163 //
       
   164 void CDisplayServiceUILayout::StartL()
       
   165     {
       
   166     FUNC_LOG;
       
   167 
       
   168     // Get initial values
       
   169     TDisplayUILayoutValues layout;
       
   170     CFbsBitGc::TGraphicsOrientation rotation;
       
   171     iWsEventHandler->ResolveUILayout( layout, rotation );
       
   172 
       
   173     // Publish initial UI layout context value
       
   174     PublishContextL( layout, ETrue );
       
   175     
       
   176     // Publish initial UI rotation context value
       
   177     PublishContextL( rotation, ETrue );
       
   178     
       
   179     //  Start listening window server events
       
   180     iWsEventHandler->IssueRequest();
       
   181     }
       
   182 
       
   183 //------------------------------------------------------------------------------
       
   184 // CDisplayServiceUILayout::Stop
       
   185 //------------------------------------------------------------------------------
       
   186 //
       
   187 void CDisplayServiceUILayout::Stop()
       
   188     {
       
   189     FUNC_LOG;
       
   190     
       
   191     iWsEventHandler->Cancel();
       
   192     }
       
   193 
       
   194 //------------------------------------------------------------------------------
       
   195 // CDisplayServiceUILayout::HandleWsEventL
       
   196 //------------------------------------------------------------------------------
       
   197 //
       
   198 void CDisplayServiceUILayout::HandleWsEventL( const TWsEvent &aEvent )
       
   199     {
       
   200     FUNC_LOG;
       
   201     
       
   202     INFO_1( "New window server event received: Event: [%d]", aEvent.Type() );
       
   203 
       
   204     // New window server event received, solve the event type
       
   205     switch( aEvent.Type() )
       
   206         {
       
   207         case EEventWindowGroupsChanged:
       
   208         case EEventScreenDeviceChanged:
       
   209             {
       
   210             TDisplayUILayoutValues layout = EUILayoutValueUnknown;
       
   211             CFbsBitGc::TGraphicsOrientation rotation =
       
   212                 CFbsBitGc::EGraphicsOrientationNormal;
       
   213             iWsEventHandler->ResolveUILayout( layout, rotation );
       
   214             PublishContextL( layout );
       
   215             PublishContextL( rotation );
       
   216             break;
       
   217             }
       
   218         default:
       
   219             {
       
   220             // Nothing to do here
       
   221             break;
       
   222             }
       
   223         }
       
   224     }
       
   225 
       
   226 //------------------------------------------------------------------------------
       
   227 // CDisplayServiceUILayout::HandleWsEventErrorL
       
   228 //------------------------------------------------------------------------------
       
   229 //
       
   230 void CDisplayServiceUILayout::HandleWsEventErrorL( TInt aError )
       
   231     {
       
   232     FUNC_LOG;
       
   233     
       
   234     // Remove warnings in urel builds
       
   235     (void)aError;
       
   236     
       
   237     ERROR( aError,
       
   238         "Window server event error occured. UI layout set to 'unknown" );
       
   239 
       
   240     // Error occured and it is impossible to know the current UI layout.
       
   241     // Publish context:
       
   242     // Device: Display.UI.Layout: Unknown
       
   243     iContext->SetValueL( TPtrC(
       
   244         KDisplayUILayoutValues[EUILayoutValueUnknown] ) );
       
   245     iCF.PublishContext( *iContext, iThread );
       
   246     }
       
   247 
       
   248 //------------------------------------------------------------------------------
       
   249 // CDisplayServiceUILayout::PublishContextL
       
   250 //------------------------------------------------------------------------------
       
   251 //
       
   252 void CDisplayServiceUILayout::PublishContextL( TDisplayUILayoutValues aLayout,
       
   253     TBool aForce )
       
   254     {
       
   255     FUNC_LOG;
       
   256 
       
   257     if( aLayout != iCurrentLayout || aForce )
       
   258         {
       
   259         iContext->SetTypeL( KDisplayUILayoutType );
       
   260         iContext->SetValueL( TPtrC( KDisplayUILayoutValues[aLayout] ) );
       
   261         iCF.PublishContext( *iContext, iThread );
       
   262         iCurrentLayout = aLayout;
       
   263         }
       
   264     }
       
   265 
       
   266 //------------------------------------------------------------------------------
       
   267 // CDisplayServiceUILayout::PublishContextL
       
   268 //------------------------------------------------------------------------------
       
   269 //
       
   270 void CDisplayServiceUILayout::PublishContextL(
       
   271     CFbsBitGc::TGraphicsOrientation aRotation,
       
   272     TBool aForce )
       
   273     {
       
   274     FUNC_LOG;
       
   275 
       
   276     if( aRotation != iCurrentRotation || aForce )
       
   277         {
       
   278         iContext->SetTypeL( KDisplayUIRotationType );
       
   279         iContext->SetValueL( TPtrC( KDisplayUIRotationValues[aRotation] ) );
       
   280         iCF.PublishContext( *iContext, iThread );
       
   281         iCurrentRotation = aRotation;
       
   282         }
       
   283     }
       
   284 
       
   285 // ======================== CWsEventHandler ========================
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // C++ constructor.
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 CWsEventHandler::CWsEventHandler( MWsEventObserver& aObserver ):
       
   292     CActive( CActive::EPriorityStandard ),
       
   293     iObserver( aObserver )
       
   294     {
       
   295     FUNC_LOG;
       
   296     
       
   297     CActiveScheduler::Add( this );
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // Symbian 2nd phase constructor.
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 void CWsEventHandler::ConstructL()
       
   305     {
       
   306     FUNC_LOG;
       
   307     
       
   308     // Connect to window server server
       
   309     User::LeaveIfError( iWsSession.Connect() );
       
   310     
       
   311     // Construct window group
       
   312     iWindowGroup = new( ELeave ) RWindowGroup( iWsSession );
       
   313     User::LeaveIfError( iWindowGroup->Construct( (TUint32)this, EFalse ) );
       
   314     User::LeaveIfError( iWindowGroup->EnableScreenChangeEvents() );
       
   315     User::LeaveIfError( iWindowGroup->EnableGroupChangeEvents() );
       
   316     iWindowGroup->SetOrdinalPosition( 0, ECoeWinPriorityNeverAtFront );
       
   317     iWindowGroup->EnableReceiptOfFocus( EFalse );
       
   318     
       
   319     // Hide the invisible window from the task manager
       
   320     iWindowGroupName = CApaWindowGroupName::NewL( iWsSession );
       
   321     iWindowGroupName->SetHidden( ETrue );
       
   322     iWindowGroupName->SetWindowGroupName( *iWindowGroup );
       
   323 
       
   324     // Construct screen device
       
   325     iWsScreenDevice = new( ELeave ) CWsScreenDevice( iWsSession );
       
   326     User::LeaveIfError( iWsScreenDevice->Construct() );
       
   327     }
       
   328 
       
   329 // ---------------------------------------------------------------------------
       
   330 // Symbian two phased constructor.
       
   331 // ---------------------------------------------------------------------------
       
   332 //
       
   333 CWsEventHandler* CWsEventHandler::NewL( MWsEventObserver& aObserver )
       
   334     {
       
   335     FUNC_LOG;
       
   336 
       
   337     CWsEventHandler* self = CWsEventHandler::NewLC( aObserver );
       
   338     CleanupStack::Pop( self );
       
   339     
       
   340     return self;
       
   341     }
       
   342 
       
   343 // ---------------------------------------------------------------------------
       
   344 // Symbian two phased constructor.
       
   345 // Leaves pointer in the cleanup stack.
       
   346 // ---------------------------------------------------------------------------
       
   347 //
       
   348 CWsEventHandler* CWsEventHandler::NewLC( MWsEventObserver& aObserver )
       
   349     {
       
   350     FUNC_LOG;
       
   351 
       
   352     CWsEventHandler* self = new ( ELeave ) CWsEventHandler( aObserver );
       
   353     CleanupStack::PushL( self );
       
   354     self->ConstructL();
       
   355     
       
   356     return self;
       
   357     }
       
   358 
       
   359 // ---------------------------------------------------------------------------
       
   360 // C++ destructor.
       
   361 // ---------------------------------------------------------------------------
       
   362 //
       
   363 CWsEventHandler::~CWsEventHandler()
       
   364     {
       
   365     FUNC_LOG;
       
   366     
       
   367     Cancel();
       
   368     
       
   369     // Cleanup screen device
       
   370     delete iWsScreenDevice;
       
   371     
       
   372     // Cleanup window group name
       
   373     delete iWindowGroupName;
       
   374        
       
   375     // Cleanup window group
       
   376     if( iWindowGroup )
       
   377         {
       
   378         iWindowGroup->DisableGroupChangeEvents();
       
   379         iWindowGroup->DisableScreenChangeEvents();
       
   380         iWindowGroup->Close();
       
   381         delete iWindowGroup;
       
   382         }
       
   383     
       
   384     // Cleanup window server session
       
   385     iWsSession.Close();
       
   386     }
       
   387 
       
   388 //------------------------------------------------------------------------------
       
   389 // CWsEventHandler::IssueRequest
       
   390 //------------------------------------------------------------------------------
       
   391 //
       
   392 void CWsEventHandler::IssueRequest()
       
   393     {
       
   394     FUNC_LOG;
       
   395 
       
   396     __ASSERT_DEBUG( !IsActive(), Panic( ERequestAlreadyIssued ) );
       
   397     
       
   398     // Request events from window server
       
   399     iWsSession.EventReady( &iStatus );
       
   400     SetActive();
       
   401     }
       
   402 
       
   403 //------------------------------------------------------------------------------
       
   404 // CWsEventHandler::ResolveUILayout
       
   405 //------------------------------------------------------------------------------
       
   406 //
       
   407 void CWsEventHandler::ResolveUILayout( TDisplayUILayoutValues& aLayout,
       
   408     CFbsBitGc::TGraphicsOrientation& aRotation ) const
       
   409     {
       
   410     FUNC_LOG;
       
   411     
       
   412     // Resolve the current UI layout from screen dimensions
       
   413     TInt mode = iWsScreenDevice->CurrentScreenMode();
       
   414     INFO_1( "Current screen mode: [%d]", mode );
       
   415     
       
   416     TPixelsAndRotation pixelsAndRotation;
       
   417     iWsScreenDevice->GetScreenModeSizeAndRotation( mode, pixelsAndRotation );
       
   418     INFO_3( "Current screen mode size and rotation: Size: [W=%d, H=%d], Rotation: [%S]",
       
   419         pixelsAndRotation.iPixelSize.iWidth,
       
   420         pixelsAndRotation.iPixelSize.iHeight,
       
   421         &RotationAsDesC( pixelsAndRotation.iRotation ) );
       
   422     
       
   423     // Resolve current UI rotation
       
   424     aRotation = pixelsAndRotation.iRotation;
       
   425     
       
   426     // Resovle current UI layout mode
       
   427     TSize screenSize( pixelsAndRotation.iPixelSize );
       
   428     if( screenSize.iWidth < screenSize.iHeight )
       
   429         {
       
   430         // Assuming portrait layout
       
   431         INFO( "Current UI layout resolved to be in 'Portrait' mode" );
       
   432         aLayout = EUILayoutValuePortrait;
       
   433         }
       
   434     else if( screenSize.iWidth > screenSize.iHeight )
       
   435         {
       
   436         // Assuming landscape layout
       
   437         INFO( "Current UI layout resolved to be in 'Landscape' mode" );
       
   438         aLayout = EUILayoutValueLandscape;
       
   439         }
       
   440     else
       
   441         {
       
   442         // Need to check the rotation even though this might not be correct
       
   443         // if wsini.ini has not been configured properly.
       
   444         CFbsBitGc::TGraphicsOrientation orientation(
       
   445             pixelsAndRotation.iRotation );
       
   446         if( orientation == CFbsBitGc::EGraphicsOrientationNormal ||
       
   447             orientation == CFbsBitGc::EGraphicsOrientationRotated180 )
       
   448             {
       
   449             // Assuming portrait layout
       
   450             INFO( "Current UI layout resolved to be in 'Portrait' mode" );
       
   451             aLayout = EUILayoutValuePortrait;
       
   452             }
       
   453         else
       
   454             {
       
   455             // Assuming landscape layout
       
   456             INFO( "Current UI layout resolved to be in 'Landscape' mode" );
       
   457             aLayout = EUILayoutValueLandscape;
       
   458             }
       
   459         }
       
   460     }
       
   461 
       
   462 //------------------------------------------------------------------------------
       
   463 // CWsEventHandler::RunL
       
   464 //------------------------------------------------------------------------------
       
   465 //
       
   466 void CWsEventHandler::RunL()
       
   467     {
       
   468     FUNC_LOG;
       
   469     
       
   470     TInt err = iStatus.Int();
       
   471     if( err == KErrNone )
       
   472         {
       
   473         // No errors occured, fetch event
       
   474         TWsEvent wsEvent;
       
   475         iWsSession.GetEvent( wsEvent );
       
   476         
       
   477         // Continue listening
       
   478         IssueRequest();
       
   479         
       
   480         // Forward event to observer
       
   481         iObserver.HandleWsEventL( wsEvent );
       
   482         }
       
   483     else
       
   484         {
       
   485         // Continue listening
       
   486         IssueRequest();
       
   487         
       
   488         // Forward error to observer
       
   489         iObserver.HandleWsEventErrorL( err );
       
   490         }    
       
   491     }
       
   492 
       
   493 //------------------------------------------------------------------------------
       
   494 // CWsEventHandler::DoCancel
       
   495 //------------------------------------------------------------------------------
       
   496 //
       
   497 void CWsEventHandler::DoCancel()
       
   498     {
       
   499     FUNC_LOG;
       
   500     
       
   501     // Cancel event ready from window server
       
   502     iWsSession.EventReadyCancel();
       
   503     }
       
   504 
       
   505 //------------------------------------------------------------------------------
       
   506 // CWsEventHandler::RunError
       
   507 //------------------------------------------------------------------------------
       
   508 //
       
   509 TInt CWsEventHandler::RunError( TInt /*aError*/ )
       
   510     {
       
   511     FUNC_LOG;
       
   512 
       
   513     // Issue a new request, other error handling is not performed since the
       
   514     // problem has occured in the observer code
       
   515     IssueRequest();
       
   516     
       
   517     return KErrNone;
       
   518     }
       
   519 
       
   520 // End of file