taskswitcher/screenshotplugin/src/tsscreenshotplugin.cpp
changeset 121 0b3699f6c654
child 119 50e220be30d1
equal deleted inserted replaced
115:3ab5c078b490 121:0b3699f6c654
       
     1 /*
       
     2 * Copyright (c) 2008 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:
       
    15 *
       
    16 */
       
    17 #include <graphics/wsscreendevice.h>
       
    18 #include <s32mem.h>
       
    19 #include <e32property.h>
       
    20 #include <wspublishandsubscribedata.h>
       
    21 #include <tstaskmonitorglobals.h>
       
    22 #include <stddef.h>
       
    23 #include <qnamespace.h>
       
    24 
       
    25 #include "tsscreenshotplugin.h"
       
    26 #include "tsorientationmonitor.h"
       
    27 #include "tsscreenshotmsg.h"
       
    28 
       
    29 const TInt KInvalidGroupId(~0);
       
    30 const TUid KHbPsHardwareCoarseOrientationCategoryUid = {0x20022E82};
       
    31 const TUint KHbPsHardwareCoarseWsOrientationKey = 0x4F726965;
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 /**
       
    35  * Two phase constructor 
       
    36  */
       
    37 CTsScreenshotPlugin* CTsScreenshotPlugin::NewL()
       
    38     {
       
    39     //no second step construction is required here
       
    40     //window server will initialize plugin later
       
    41     return new(ELeave)CTsScreenshotPlugin();
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 /**
       
    46  * From CWsGraphicDrawer
       
    47  * @see CWsGraphicDrawer::ConstructL(MWsGraphicDrawerEnvironment&, const TGraphicDrawerId& , MWsClient&, const TDesC8&)
       
    48  */
       
    49 void CTsScreenshotPlugin::ConstructL( MWsGraphicDrawerEnvironment& aEnv, 
       
    50                                       const TGraphicDrawerId& iId, 
       
    51                                       MWsClient& aOwner, 
       
    52                                       const TDesC8& /*data*/ )
       
    53     {
       
    54     BaseConstructL( aEnv, iId, aOwner );
       
    55     aEnv.RegisterEventHandler(this, 
       
    56                              this, 
       
    57                              TWservCrEvent::EWindowGroupChanged |
       
    58                              TWservCrEvent::EDeviceOrientationChanged);
       
    59     iWindowGroupId = KInvalidGroupId;
       
    60     TRAP_IGNORE( ConstructL() );
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 void CTsScreenshotPlugin::ConstructL()
       
    65     {
       
    66     iMonitor = CTsOrientationMonitor::NewL( *this );
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 /**
       
    71  * Destructor
       
    72  */
       
    73 CTsScreenshotPlugin::~CTsScreenshotPlugin()
       
    74     {
       
    75     delete iMonitor;
       
    76     Env().UnregisterEventHandler(this);
       
    77     iCache.ResetAndDestroy();
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 /**
       
    82  * From CWsGraphicDrawer.
       
    83  * @see CWsGraphicDrawer::DoDraw(MWsGc&, const TRect&, const TDesC8&) const
       
    84  */ 
       
    85 void CTsScreenshotPlugin::DoDraw(MWsGc&, const TRect&, const TDesC8&) const
       
    86     {
       
    87     //plugin is not a real drawer
       
    88     //no implementation required
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 /**
       
    93  * From CWsGraphicDrawer
       
    94  * @see CWsGraphicDrawer::HandleMessage(const TDesC8&)
       
    95  */
       
    96 void CTsScreenshotPlugin::HandleMessage( const TDesC8& aMsg )
       
    97     {
       
    98     TRAP_IGNORE( HandleMessageL( aMsg ) );
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // 
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 void CTsScreenshotPlugin::HandleMessageL( const TDesC8& aMsg )
       
   106     {
       
   107     RDesReadStream msgStream(aMsg);
       
   108     CleanupClosePushL(msgStream);
       
   109     TInt function = msgStream.ReadInt32L();
       
   110     if(RegisterScreenshotMessage == function)
       
   111         {
       
   112         CTsScreenshotMsg *screenshotMsg = CTsScreenshotMsg::NewLC(msgStream);
       
   113         for( TInt iter(0); iter < iCache.Count(); ++iter )
       
   114             {
       
   115             if(iCache[iter]->Handle() == screenshotMsg->Screenshot().Handle())
       
   116                 {
       
   117                 //bitmap is not needed no more
       
   118                 delete iCache[iter];
       
   119                 iCache.Remove(iter);
       
   120                 break;
       
   121                 }
       
   122             }
       
   123         CleanupStack::PopAndDestroy( screenshotMsg );
       
   124         }
       
   125     CleanupStack::PopAndDestroy( &msgStream );
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 /**
       
   130  * From MWsEventHandler.
       
   131  * @see MWsEventHandler::DoHandleEvent(const TWservCrEvent&)
       
   132  */
       
   133 void CTsScreenshotPlugin::DoHandleEvent(const TWservCrEvent& aEvent)
       
   134     {
       
   135     switch (aEvent.Type()) {
       
   136     case TWservCrEvent::EWindowGroupChanged:
       
   137         if( KInvalidGroupId != iWindowGroupId )
       
   138             {
       
   139             NotifyWindowGroupToBackground( iWindowGroupId );
       
   140             TakeScreenshot( iWindowGroupId );
       
   141             }
       
   142         iWindowGroupId = aEvent.WindowGroupIdentifier();
       
   143         break;
       
   144     case TWservCrEvent::EDeviceOrientationChanged:
       
   145         TakeScreenshot( iWindowGroupId );
       
   146         break;
       
   147         }
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // 
       
   152 /**
       
   153  * Function take screenshot od current display
       
   154  * @param screenshot identyfier
       
   155  */
       
   156 void CTsScreenshotPlugin::TakeScreenshot( TInt aId )
       
   157     {
       
   158     TRAP_IGNORE( TakeScreenshotL( aId ); )
       
   159     }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 /**
       
   163  * Function take screenshot od current display
       
   164  * @param screenshot identyfier
       
   165  */
       
   166 void CTsScreenshotPlugin::TakeScreenshotL( TInt aId )
       
   167     {
       
   168     if( 0 >= Env().ScreenCount() )
       
   169         {
       
   170         User::Leave(KErrCorrupt);
       
   171         }
       
   172     const TInt screenId(0); //use local variable in case of changing screen selection policy
       
   173     const MWsScreenConfig* const screenConfig = 
       
   174         Env().Screen(screenId)->ObjectInterface<MWsScreenConfig>();
       
   175     const MWsScreenDevice* const screenDevice = 
       
   176         static_cast<MWsScreenDevice*>(Env().Screen(screenId)->ResolveObjectInterface(MWsScreenDevice::EWsObjectInterfaceId));
       
   177     
       
   178     User::LeaveIfNull(screenConfig);
       
   179     User::LeaveIfNull(screenDevice);
       
   180     
       
   181     //prepare destination bitmap
       
   182     CFbsBitmap *bitmap = new (ELeave) CFbsBitmap();
       
   183     CleanupStack::PushL(bitmap);
       
   184     
       
   185     
       
   186     User::LeaveIfError(bitmap->Create(screenConfig->SizeInPixels(), 
       
   187                                 screenConfig->DisplayMode()));
       
   188     
       
   189     screenDevice->CopyScreenToBitmapL(bitmap, 
       
   190                                       screenConfig->SizeInPixels());
       
   191     
       
   192     
       
   193     //prepare and send message
       
   194     RBuf8 message;
       
   195     CleanupClosePushL(message);
       
   196     message.CreateL(CTsScreenshotMsg::Size() + sizeof(TInt));
       
   197     RDesWriteStream stream(message);
       
   198     CleanupClosePushL(stream);
       
   199     stream.WriteInt32L(RegisterScreenshotMessage);
       
   200     
       
   201     CTsScreenshotMsg * screenshotMsg = 
       
   202         CTsScreenshotMsg::NewLC(aId, 
       
   203                                 *bitmap, 
       
   204                                 Low, 
       
   205                                 OrientationToAngle());
       
   206     stream << (*screenshotMsg);
       
   207     CleanupStack::PopAndDestroy(screenshotMsg);
       
   208     CleanupStack::PopAndDestroy(&stream);
       
   209     User::LeaveIfError(SendMessage(message));
       
   210     CleanupStack::PopAndDestroy(&message);
       
   211     iCache.AppendL(bitmap);
       
   212     CleanupStack::Pop(bitmap);
       
   213     }
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 //
       
   217 TInt CTsScreenshotPlugin::OrientationToAngle()
       
   218     {
       
   219     TInt retVal(0), orientation(0), sensor(0);
       
   220     RProperty::Get( KRenderOrientationCategory, 
       
   221                     KRenderOrientationKey, 
       
   222                     orientation );
       
   223     
       
   224     switch( orientation )
       
   225         {
       
   226         case EDisplayOrientation90CW: retVal = 270;break;
       
   227         case EDisplayOrientation180: retVal = 180;break;
       
   228         case EDisplayOrientation270CW: retVal = 90;break;
       
   229         case EDisplayOrientationNormal:retVal = iAngle; break;
       
   230         case EDisplayOrientationAuto:
       
   231             {
       
   232             RProperty::Get( KHbPsHardwareCoarseOrientationCategoryUid, 
       
   233                             KHbPsHardwareCoarseWsOrientationKey, sensor );
       
   234             retVal = ( sensor == Qt::Vertical ) ? 270 : 0;
       
   235             break;
       
   236             }
       
   237             
       
   238         }
       
   239     return retVal;
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 void CTsScreenshotPlugin::OrientationChanged( TInt aAngle )
       
   244     {
       
   245     iAngle = aAngle;
       
   246     }
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 void CTsScreenshotPlugin::NotifyWindowGroupToBackground( TInt windowGroupId )
       
   250     {
       
   251     TRAP_IGNORE( NotifyWindowGroupToBackgroundL(windowGroupId); )
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // 
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 void CTsScreenshotPlugin::NotifyWindowGroupToBackgroundL( TInt windowGroupId )
       
   259     {
       
   260     RBuf8 message;
       
   261     CleanupClosePushL(message);    
       
   262     message.CreateL(2 * sizeof(TInt));
       
   263         {
       
   264         RDesWriteStream stream(message);
       
   265         CleanupClosePushL(stream);
       
   266         stream.WriteInt32L(WindowGroupToBackgroundMessage);
       
   267         stream.WriteInt32L(windowGroupId);
       
   268         CleanupStack::PopAndDestroy(&stream);
       
   269         }
       
   270     User::LeaveIfError(SendMessage(message));
       
   271     CleanupStack::PopAndDestroy(&message);
       
   272     }