screengrabber/src/sgengine.cpp
branchRCL_3
changeset 22 fad26422216a
parent 21 b3cee849fa46
child 23 f8280f3bfeb7
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
     1 /*
       
     2 * Copyright (c) 2010 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 
       
    18 #include <s32file.h> 
       
    19 #include <bautils.h>
       
    20 #include <pathinfo.h>
       
    21 #include <apparc.h> 
       
    22 #include <imageconversion.h>
       
    23 #include <textresolver.h>
       
    24 #include <apgtask.h> 
       
    25 
       
    26 #include <hblabel.h>
       
    27 #include <hbmessagebox.h>
       
    28 
       
    29 #include "mainview.h"
       
    30 #include "sgengine.h"
       
    31 #include "enginewrapper.h"
       
    32 #include "gifanimator.h"
       
    33 
       
    34 
       
    35 #define SC_PRINTS
       
    36 
       
    37     #ifdef SC_PRINTS
       
    38      #define SC_DEBUG(a) RDebug::Print(a)
       
    39      #define SC_DEBUG2(a,b) RDebug::Print(a,b)
       
    40     #else
       
    41      #define SC_DEBUG(a) 
       
    42      #define SC_DEBUG2(a,b)
       
    43     #endif
       
    44 
       
    45 
       
    46 
       
    47 
       
    48 _LIT(KDefaultImageFileName, "Shot");
       
    49 _LIT(KDefaultVideoFileName, "Video");
       
    50 
       
    51 #define HIGH_QUALITY_JPEG 97
       
    52 #define LOW_QUALITY_JPEG 60
       
    53 #define DEFAULT_SEQ_CAPTURE_DELAY_MS 5000   // 5.000 secs
       
    54 #define VIDEO_CAPTURE_DELAY 250             // 0.25 secs
       
    55 #define VIDEO_CAPTURE_MINIMUM_DELAY 200     // 0.20 secs
       
    56 const TInt KSettingsDrive = EDriveC;
       
    57 _LIT(KSettingsFileName, "screengrabber_settings.ini");
       
    58 _LIT(KScreenShotsSubDirectory, "Screen Shots\\");
       
    59 _LIT(KSGTemporaryDirectory, "\\system\\temp\\screengrabber\\");
       
    60 
       
    61 
       
    62 #define KEY_CAPTURE_PRIORITY 100            // do not change, this is for window server
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 
       
    66 SGEngine::SGEngine() : CActive(EPriorityStandard)
       
    67     {
       
    68     // No implementation required
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 
       
    73 SGEngine::~SGEngine()
       
    74     {
       
    75  
       
    76     TRAP_IGNORE( CleanTemporaryFilesL() );
       
    77 
       
    78 
       
    79     if (iFrameImageData)
       
    80         delete iFrameImageData;
       
    81 
       
    82     if (iPreviouslyCapturedBitmap)
       
    83         delete iPreviouslyCapturedBitmap;
       
    84 
       
    85     if (iImageEncoder)
       
    86         delete iImageEncoder;
       
    87      
       
    88     delete iVideoFrameArray;
       
    89 
       
    90     Cancel();
       
    91     iTimer.Close();
       
    92 
       
    93     iFileSession.Close(); 
       
    94     
       
    95     }
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 
       
    99 SGEngine* SGEngine::NewLC(EngineWrapper *aEngineWrapper)
       
   100     {
       
   101     SGEngine* self = new (ELeave) SGEngine();
       
   102     CleanupStack::PushL(self);
       
   103     self->ConstructL(aEngineWrapper);
       
   104     return self;
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 
       
   109 SGEngine* SGEngine::NewL(EngineWrapper *aEngineWrapper)
       
   110     {
       
   111     SGEngine* self = SGEngine::NewLC(aEngineWrapper);
       
   112     CleanupStack::Pop(); // self;
       
   113     return self;
       
   114     }
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 
       
   118 void SGEngine::ConstructL(EngineWrapper *aEngineWrapper)
       
   119     {
       
   120 
       
   121     iEngineWrapper = aEngineWrapper;
       
   122    
       
   123     iHashKeyCapturingActivated = EFalse; // Check this
       
   124    
       
   125     User::LeaveIfError(iTimer.CreateLocal());
       
   126 
       
   127     iVideoFrameArray = new(ELeave) CVideoFrameArray(10000);
       
   128 
       
   129     iPreviouslyCapturedBitmap = new(ELeave) CFbsBitmap;
       
   130 
       
   131     iCapturingInProgress = EFalse;
       
   132     iStopCapturing = EFalse;
       
   133     iNumberOfTakenShots = 0;
       
   134     iCurrentFrameNumber = 0;
       
   135     iHashKeyDown = EFalse;
       
   136 	
       
   137 
       
   138 	CActiveScheduler::Add(this);
       
   139 	User::LeaveIfError(iFileSession.Connect());  
       
   140 	
       
   141 	// create window group.
       
   142     iRootWin = CEikonEnv::Static()->RootWin();
       
   143 	iRootWin.EnableReceiptOfFocus(EFalse);
       
   144     
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 
       
   149 void SGEngine::RunL(){
       
   150 
       
   151 
       
   152     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- RunL begin"));
       
   153     
       
   154     switch (iState) 
       
   155         {
       
   156         // encoding of the image is now finished
       
   157         case EEncodingImage: 
       
   158             {
       
   159             if (iGrabSettings.iCaptureMode == ECaptureModeSingleCapture)
       
   160                 {
       
   161                 // single shot done
       
   162                 CapturingFinishedL( iStatus.Int() );
       
   163                 }
       
   164 
       
   165             else if (iGrabSettings.iCaptureMode == ECaptureModeSequantialCapture)
       
   166                 {
       
   167                 // increase the counter    
       
   168                 iNumberOfTakenShots++;
       
   169                 
       
   170                 // check if we can take more shots or just finish
       
   171                 if (!iStopCapturing && iStatus.Int()==KErrNone)
       
   172                     {
       
   173         
       
   174                     // some delay before the next shot can be taken    
       
   175                     iState = ESequenceDelay;
       
   176                     
       
   177                     // some checking that the value of delay is valid
       
   178                     TInt delay(iGrabSettings.iSequantialCaptureDelay); //ms
       
   179                     if (delay<0 && delay > 999999)
       
   180                         delay = DEFAULT_SEQ_CAPTURE_DELAY_MS;
       
   181                       
       
   182                     iTimer.After(iStatus, delay*1000);
       
   183                     SetActive();  
       
   184                     }
       
   185                 else
       
   186                     {
       
   187                     // finished
       
   188                     CapturingFinishedL( iStatus.Int() );
       
   189                     }                       
       
   190                }
       
   191             else
       
   192                 User::Panic(_L("Wrong mode"), 32);            
       
   193 
       
   194             break;
       
   195             }
       
   196 
       
   197         // delay finished, ready to take the next shot
       
   198         case ESequenceDelay: 
       
   199             {
       
   200             TakeScreenShotAndSaveL();
       
   201 
       
   202             break;
       
   203             }
       
   204             
       
   205         // asked to cancel capturing    
       
   206         case ECancelCapturing:
       
   207             {
       
   208             // finished
       
   209             CapturingFinishedL( iStatus.Int() );
       
   210 
       
   211             break;
       
   212             }
       
   213             
       
   214         case ENextVideoFrame:
       
   215             {
       
   216             // increase the counter
       
   217             iCurrentFrameNumber++;
       
   218             
       
   219             // check if we can take more frames or just finish
       
   220             if (!iStopCapturing && iStatus.Int()==KErrNone)
       
   221                 {
       
   222                 // take next frame
       
   223                 CaptureFrameForVideoL();
       
   224                 }
       
   225             else
       
   226                 {
       
   227                 // finished, save video
       
   228                 SaveVideoL( iStatus.Int() );
       
   229                 }               
       
   230             }
       
   231             break;
       
   232 
       
   233         case ECancelVideoCapturing:
       
   234             {
       
   235             // finished, save video
       
   236             SaveVideoL( iStatus.Int() );                
       
   237             }
       
   238             break;
       
   239                                 
       
   240 
       
   241         default:
       
   242             {
       
   243             break;
       
   244             }
       
   245         }
       
   246     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- RunL end"));
       
   247     
       
   248 
       
   249 }
       
   250 
       
   251 
       
   252 void SGEngine::EnableRcpOfFoc(TBool aState)
       
   253 	{
       
   254 	iRootWin.EnableReceiptOfFocus(aState);
       
   255 	}
       
   256 
       
   257 
       
   258 // ---------------------------------------------------------------------------
       
   259 
       
   260 void SGEngine::DoCancel(){
       
   261     iTimer.Cancel();
       
   262 }
       
   263 
       
   264 // ---------------------------------------------------------------------------
       
   265 
       
   266 void SGEngine::LoadDFSValueL(CDictionaryFileStore* aDicFS, const TUid& aUid, TInt& aValue)
       
   267     {
       
   268     if (aDicFS->IsPresentL(aUid))
       
   269         {
       
   270         RDictionaryReadStream in;
       
   271         in.OpenLC(*aDicFS, aUid);
       
   272         aValue = in.ReadInt16L();
       
   273         CleanupStack::PopAndDestroy(); // in        
       
   274         }
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------------------------
       
   278 
       
   279 void SGEngine::LoadDFSValueL(CDictionaryFileStore* aDicFS, const TUid& aUid, TDes& aValue)
       
   280     {
       
   281     if (aDicFS->IsPresentL(aUid))
       
   282         {
       
   283         RDictionaryReadStream in;
       
   284         in.OpenLC(*aDicFS, aUid);
       
   285         TInt bufLength = in.ReadInt16L();   // get length of descriptor
       
   286         in.ReadL(aValue, bufLength);        // get the descriptor itself
       
   287         CleanupStack::PopAndDestroy(); // in
       
   288         }
       
   289     }
       
   290 // ----------------------------------------------------------------------------
       
   291 
       
   292 void SGEngine::LoadSettingsL()
       
   293     {
       
   294 	 
       
   295     // set defaults
       
   296     iGrabSettings.iCaptureMode = ECaptureModeSingleCapture;
       
   297     
       
   298     iGrabSettings.iSingleCaptureHotkey = EHotkeySendKey;
       
   299     iGrabSettings.iSingleCaptureImageFormat = EImageFormatPNG;
       
   300     iGrabSettings.iSingleCaptureMemoryInUseMultiDrive = EPhoneMemory;
       
   301     iGrabSettings.iSingleCaptureFileName.Copy( KDefaultImageFileName );
       
   302 
       
   303     iGrabSettings.iSequantialCaptureHotkey = EHotkeySendKey;
       
   304     iGrabSettings.iSequantialCaptureImageFormat = EImageFormatPNG;
       
   305     iGrabSettings.iSequantialCaptureDelay = DEFAULT_SEQ_CAPTURE_DELAY_MS;
       
   306     iGrabSettings.iSequantialCaptureMemoryInUseMultiDrive = EPhoneMemory;
       
   307     iGrabSettings.iSequantialCaptureFileName.Copy( KDefaultImageFileName );
       
   308 
       
   309     iGrabSettings.iVideoCaptureHotkey = EHotkeySendKey;
       
   310     iGrabSettings.iVideoCaptureVideoFormat = EVideoFormatAnimatedGIF;
       
   311     iGrabSettings.iVideoCaptureMemoryInUseMultiDrive = EPhoneMemory;    
       
   312     iGrabSettings.iVideoCaptureFileName.Copy( KDefaultVideoFileName );
       
   313        
       
   314         
       
   315     // make sure that the private path of this app in c-drive exists
       
   316     
       
   317 	iFileSession.CreatePrivatePath( KSettingsDrive ); // c:\\private\\101fb751\\
       
   318     
       
   319     // handle settings always in the private directory 
       
   320     
       
   321 	if (iFileSession.SetSessionToPrivate( KSettingsDrive ) == KErrNone)
       
   322         {
       
   323         // open or create a dictionary file store
       
   324         CDictionaryFileStore* settingsStore = CDictionaryFileStore::OpenLC(iFileSession, KSettingsFileName, KUidScreenGrabber);
       
   325 
       
   326         LoadDFSValueL(settingsStore, KSGSettingCaptureMode,                             iGrabSettings.iCaptureMode);
       
   327         
       
   328         LoadDFSValueL(settingsStore, KSGSettingSingleCaptureHotkey,                     iGrabSettings.iSingleCaptureHotkey);
       
   329         LoadDFSValueL(settingsStore, KSGSettingSingleCaptureImageFormat,                iGrabSettings.iSingleCaptureImageFormat);
       
   330         LoadDFSValueL(settingsStore, KSGSettingSingleCaptureMemoryInUseMultiDrive,      iGrabSettings.iSingleCaptureMemoryInUseMultiDrive);
       
   331         LoadDFSValueL(settingsStore, KSGSettingSingleCaptureFileName,                   iGrabSettings.iSingleCaptureFileName);
       
   332         
       
   333         LoadDFSValueL(settingsStore, KSGSettingSequantialCaptureHotkey,                 iGrabSettings.iSequantialCaptureHotkey);
       
   334         LoadDFSValueL(settingsStore, KSGSettingSequantialCaptureImageFormat,            iGrabSettings.iSequantialCaptureImageFormat);
       
   335         LoadDFSValueL(settingsStore, KSGSettingSequantialCaptureDelay,                  iGrabSettings.iSequantialCaptureDelay);
       
   336         LoadDFSValueL(settingsStore, KSGSettingSequantialCaptureMemoryInUseMultiDrive,  iGrabSettings.iSequantialCaptureMemoryInUseMultiDrive);
       
   337         LoadDFSValueL(settingsStore, KSGSettingSequantialCaptureFileName,               iGrabSettings.iSequantialCaptureFileName);
       
   338 
       
   339         LoadDFSValueL(settingsStore, KSGSettingVideoCaptureHotkey,                      iGrabSettings.iVideoCaptureHotkey);
       
   340         LoadDFSValueL(settingsStore, KSGSettingVideoCaptureVideoFormat,                 iGrabSettings.iVideoCaptureVideoFormat);
       
   341         LoadDFSValueL(settingsStore, KSGSettingVideoCaptureMemoryInUseMultiDrive,       iGrabSettings.iVideoCaptureMemoryInUseMultiDrive);
       
   342         LoadDFSValueL(settingsStore, KSGSettingVideoCaptureFileName,                    iGrabSettings.iVideoCaptureFileName);
       
   343 
       
   344         CleanupStack::PopAndDestroy(); // settingsStore         
       
   345         }
       
   346 //chesk if all settings are in range - for safety reason, not to set some controls out of range - it causes the crash
       
   347 	if (!(iGrabSettings.iCaptureMode >= ECaptureModeSingleCapture && iGrabSettings.iCaptureMode <= ECaptureModeVideoCapture))
       
   348 	    { //give it default value
       
   349         iGrabSettings.iCaptureMode = ECaptureModeSingleCapture;
       
   350 	    }
       
   351 	if (!(iGrabSettings.iSingleCaptureHotkey >= EHotkeySendKey && iGrabSettings.iSingleCaptureHotkey <= EHotkeyCameraKey1))
       
   352 	    {
       
   353         iGrabSettings.iSingleCaptureHotkey = EHotkeySendKey;
       
   354 	    }
       
   355 	if (!(iGrabSettings.iSingleCaptureImageFormat >= EImageFormatPNG && iGrabSettings.iSingleCaptureImageFormat <= EImageFormatGIF))
       
   356 	    {
       
   357         iGrabSettings.iSingleCaptureImageFormat = EImageFormatPNG;
       
   358 	    }
       
   359 	if (!(iGrabSettings.iSingleCaptureMemoryInUseMultiDrive >= EPhoneMemory && iGrabSettings.iSingleCaptureMemoryInUseMultiDrive <= EMemoryCard))
       
   360 	    {
       
   361         iGrabSettings.iSingleCaptureMemoryInUseMultiDrive = EPhoneMemory;
       
   362 	    }
       
   363 	
       
   364     if (!(iGrabSettings.iSequantialCaptureHotkey >= EHotkeySendKey && iGrabSettings.iSequantialCaptureHotkey <= EHotkeyCameraKey1))
       
   365         {
       
   366         iGrabSettings.iSequantialCaptureHotkey = EHotkeySendKey;
       
   367         }
       
   368     if (!(iGrabSettings.iSequantialCaptureImageFormat >= EImageFormatPNG && iGrabSettings.iSequantialCaptureImageFormat <= EImageFormatGIF))
       
   369         {
       
   370         iGrabSettings.iSequantialCaptureImageFormat = EImageFormatPNG;
       
   371         }
       
   372     if (!(iGrabSettings.iSequantialCaptureMemoryInUseMultiDrive >= EPhoneMemory && iGrabSettings.iSequantialCaptureMemoryInUseMultiDrive <= EMemoryCard))
       
   373         {
       
   374         iGrabSettings.iSequantialCaptureMemoryInUseMultiDrive = EPhoneMemory;
       
   375         }
       
   376 
       
   377     if (!(iGrabSettings.iVideoCaptureHotkey >= EHotkeySendKey && iGrabSettings.iVideoCaptureHotkey <= EHotkeyCameraKey1))
       
   378         {
       
   379         iGrabSettings.iVideoCaptureHotkey = EHotkeySendKey;
       
   380         }
       
   381     if (iGrabSettings.iVideoCaptureVideoFormat != EVideoFormatAnimatedGIF)
       
   382         {
       
   383         iGrabSettings.iVideoCaptureVideoFormat = EVideoFormatAnimatedGIF;
       
   384         }
       
   385     if (!(iGrabSettings.iVideoCaptureMemoryInUseMultiDrive >= EPhoneMemory && iGrabSettings.iVideoCaptureMemoryInUseMultiDrive <= EMemoryCard))
       
   386         {
       
   387         iGrabSettings.iVideoCaptureMemoryInUseMultiDrive = EPhoneMemory;
       
   388         }
       
   389 	
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------------------------
       
   393 
       
   394 void SGEngine::SaveDFSValueL(CDictionaryFileStore* aDicFS, const TUid& aUid, const TInt& aValue)
       
   395     {
       
   396     RDictionaryWriteStream out;
       
   397     out.AssignLC(*aDicFS, aUid);
       
   398     out.WriteInt16L(aValue);
       
   399     out.CommitL();  
       
   400     CleanupStack::PopAndDestroy(1);// out
       
   401     }
       
   402 
       
   403 // ---------------------------------------------------------------------------
       
   404 
       
   405 void SGEngine::SaveDFSValueL(CDictionaryFileStore* aDicFS, const TUid& aUid, const TDes& aValue)
       
   406     {
       
   407     RDictionaryWriteStream out;
       
   408     out.AssignLC(*aDicFS, aUid);
       
   409     out.WriteInt16L(aValue.Length());       // write length of the descriptor
       
   410     out.WriteL(aValue, aValue.Length());    // write the descriptor itself
       
   411     out.CommitL();  
       
   412     CleanupStack::PopAndDestroy(1);// out
       
   413     }
       
   414     
       
   415 // ---------------------------------------------------------------------------
       
   416 void SGEngine::SaveSettingsL(TGrabSettings aGrabSettings)
       
   417     {
       
   418 
       
   419 	
       
   420     // set the new settings
       
   421     iGrabSettings = aGrabSettings;
       
   422 
       
   423     // handle settings always in c:\\private\\101fb751\\
       
   424     if (iFileSession.SetSessionToPrivate( KSettingsDrive ) == KErrNone)
       
   425         {
       
   426         // delete existing store to make sure that it is clean and not eg corrupted
       
   427         if (BaflUtils::FileExists(iFileSession, KSettingsFileName))
       
   428             {
       
   429             iFileSession.Delete(KSettingsFileName);
       
   430             }
       
   431         
       
   432         // create a dictionary file store
       
   433         CDictionaryFileStore* settingsStore = CDictionaryFileStore::OpenLC(iFileSession, KSettingsFileName, KUidScreenGrabber);
       
   434 
       
   435         SaveDFSValueL(settingsStore, KSGSettingCaptureMode,                             iGrabSettings.iCaptureMode);
       
   436         
       
   437         SaveDFSValueL(settingsStore, KSGSettingSingleCaptureHotkey,                     iGrabSettings.iSingleCaptureHotkey);
       
   438         SaveDFSValueL(settingsStore, KSGSettingSingleCaptureImageFormat,                iGrabSettings.iSingleCaptureImageFormat);
       
   439         SaveDFSValueL(settingsStore, KSGSettingSingleCaptureMemoryInUseMultiDrive,      iGrabSettings.iSingleCaptureMemoryInUseMultiDrive);
       
   440         SaveDFSValueL(settingsStore, KSGSettingSingleCaptureFileName,                   iGrabSettings.iSingleCaptureFileName);
       
   441         
       
   442         SaveDFSValueL(settingsStore, KSGSettingSequantialCaptureHotkey,                 iGrabSettings.iSequantialCaptureHotkey);
       
   443         SaveDFSValueL(settingsStore, KSGSettingSequantialCaptureImageFormat,            iGrabSettings.iSequantialCaptureImageFormat);
       
   444         SaveDFSValueL(settingsStore, KSGSettingSequantialCaptureDelay,                  iGrabSettings.iSequantialCaptureDelay);
       
   445         SaveDFSValueL(settingsStore, KSGSettingSequantialCaptureMemoryInUseMultiDrive,  iGrabSettings.iSequantialCaptureMemoryInUseMultiDrive);
       
   446         SaveDFSValueL(settingsStore, KSGSettingSequantialCaptureFileName,               iGrabSettings.iSequantialCaptureFileName);
       
   447 
       
   448         SaveDFSValueL(settingsStore, KSGSettingVideoCaptureHotkey,                      iGrabSettings.iVideoCaptureHotkey);
       
   449         SaveDFSValueL(settingsStore, KSGSettingVideoCaptureVideoFormat,                 iGrabSettings.iVideoCaptureVideoFormat);
       
   450         SaveDFSValueL(settingsStore, KSGSettingVideoCaptureMemoryInUseMultiDrive,       iGrabSettings.iVideoCaptureMemoryInUseMultiDrive);
       
   451         SaveDFSValueL(settingsStore, KSGSettingVideoCaptureFileName,                    iGrabSettings.iVideoCaptureFileName);
       
   452         
       
   453         settingsStore->CommitL();
       
   454         CleanupStack::PopAndDestroy(); // settingsStore             
       
   455         }
       
   456 		
       
   457     }
       
   458         
       
   459 // ---------------------------------------------------------------------------
       
   460 
       
   461 void SGEngine::ActivateCaptureKeysL(TBool aChangeKey)
       
   462     {
       
   463 	
       
   464     // if changing the capture key, capturing needs to be cancelled first
       
   465     if (aChangeKey)
       
   466         {
       
   467         CancelCapturing();
       
   468         }
       
   469 
       
   470     // get hotkey of the capture
       
   471     TInt captureHotkey(0);
       
   472     if (iGrabSettings.iCaptureMode == ECaptureModeSingleCapture)
       
   473         captureHotkey = iGrabSettings.iSingleCaptureHotkey;
       
   474     else if (iGrabSettings.iCaptureMode == ECaptureModeSequantialCapture)
       
   475         captureHotkey = iGrabSettings.iSequantialCaptureHotkey;
       
   476     else if (iGrabSettings.iCaptureMode == ECaptureModeVideoCapture)
       
   477         captureHotkey = iGrabSettings.iVideoCaptureHotkey;
       
   478     else
       
   479         User::Panic(_L("Wrong mode"), 40);
       
   480     
       
   481     
       
   482 
       
   483     // start capturing based on user selected key
       
   484     switch (captureHotkey)
       
   485         {
       
   486         case EHotkeySendKey:
       
   487             {
       
   488             iCapturedKey    = iRootWin.CaptureKey(EStdKeyYes, EModifierCtrl|EModifierShift|EModifierFunc, 0, KEY_CAPTURE_PRIORITY);
       
   489             iCapturedKeyUnD = iRootWin.CaptureKeyUpAndDowns(EStdKeyYes, EModifierCtrl|EModifierShift|EModifierFunc, 0, KEY_CAPTURE_PRIORITY);
       
   490             break;
       
   491             }
       
   492         case EHotkeyPowerKey:
       
   493             {
       
   494             iCapturedKey    = iRootWin.CaptureKey(EStdKeyDevice2, EModifierCtrl|EModifierShift|EModifierFunc, 0, KEY_CAPTURE_PRIORITY);
       
   495             iCapturedKeyUnD = iRootWin.CaptureKeyUpAndDowns(EStdKeyDevice2, EModifierCtrl|EModifierShift|EModifierFunc, 0, KEY_CAPTURE_PRIORITY);
       
   496             break;
       
   497             }
       
   498         case EHotkeySideKey:
       
   499             {
       
   500             iCapturedKey    = iRootWin.CaptureKey(EStdKeyDevice6, EModifierCtrl|EModifierShift|EModifierFunc, 0, KEY_CAPTURE_PRIORITY);
       
   501             iCapturedKeyUnD = iRootWin.CaptureKeyUpAndDowns(EStdKeyDevice6, EModifierCtrl|EModifierShift|EModifierFunc, 0, KEY_CAPTURE_PRIORITY);
       
   502             break;            
       
   503             }
       
   504         case EHotkeyCameraKey1:
       
   505             {
       
   506             iCapturedKey    = iRootWin.CaptureKey(EStdKeyDevice7, EModifierCtrl|EModifierShift|EModifierFunc, 0, KEY_CAPTURE_PRIORITY);
       
   507             iCapturedKeyUnD = iRootWin.CaptureKeyUpAndDowns(EStdKeyDevice7, EModifierCtrl|EModifierShift|EModifierFunc, 0, KEY_CAPTURE_PRIORITY);
       
   508             break;            
       
   509             }
       
   510         default:
       
   511             {
       
   512             User::Panic(_L("Key not supported"), 100);
       
   513             break;
       
   514             }
       
   515         }
       
   516 		
       
   517     }
       
   518 
       
   519 // ---------------------------------------------------------------------------
       
   520 
       
   521 
       
   522 void SGEngine::CancelCapturing()
       
   523     {
       
   524 	
       
   525     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- CancelCapturing start"));
       
   526     // cancel all captures
       
   527     iRootWin.CancelCaptureKey(iCapturedKey);
       
   528     iRootWin.CancelCaptureKeyUpAndDowns(iCapturedKeyUnD);
       
   529     
       
   530     if (iHashKeyCapturingActivated)
       
   531         {
       
   532         iRootWin.CancelCaptureKey(iCapturedKeyHash);
       
   533         iRootWin.CancelCaptureKeyUpAndDowns(iCapturedKeyHashUnD);
       
   534         
       
   535         iHashKeyCapturingActivated = EFalse;
       
   536         }
       
   537     
       
   538     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- CancelCapturing end"));
       
   539 	
       
   540     }
       
   541 
       
   542 // ---------------------------------------------------------------------------
       
   543 
       
   544 bool SGEngine::TakeScreenShotAndSaveL()
       
   545     {
       
   546 	
       
   547     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- TakeSsAndSave start"));
       
   548     if ( IsActive() )
       
   549         {
       
   550         SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- TakeSsAndSave already active, ignored"));
       
   551         return false;
       
   552         }
       
   553     
       
   554     // take a screen shot
       
   555     CWsScreenDevice* screenDevice = new( ELeave ) CWsScreenDevice(CEikonEnv::Static()->WsSession() );
       
   556     CleanupStack::PushL( screenDevice );
       
   557 	
       
   558     User::LeaveIfError( screenDevice->Construct( CEikonEnv::Static()->WsSession().GetFocusScreen() ) );
       
   559 
       
   560 	
       
   561     User::LeaveIfError( iPreviouslyCapturedBitmap->Create(screenDevice->SizeInPixels(), screenDevice->DisplayMode()) );
       
   562     User::LeaveIfError( screenDevice->CopyScreenToBitmap(iPreviouslyCapturedBitmap) );
       
   563     CleanupStack::PopAndDestroy(); // screenDevice
       
   564 
       
   565 
       
   566     // get memory in use & image format of the screen capture
       
   567     TDriveNumber memoryInUse(EDriveC);
       
   568     TInt intMemInUse(0);
       
   569     TInt imageFormat(0);
       
   570     TFileName fileName;
       
   571     
       
   572     if (iGrabSettings.iCaptureMode == ECaptureModeSingleCapture)
       
   573         {        
       
   574 //        memoryInUse = iGrabSettings.iSingleCaptureMemoryInUseMultiDrive;
       
   575         intMemInUse = iGrabSettings.iSingleCaptureMemoryInUseMultiDrive;
       
   576         
       
   577         imageFormat = iGrabSettings.iSingleCaptureImageFormat;
       
   578         fileName = iGrabSettings.iSingleCaptureFileName;
       
   579         }
       
   580     else if (iGrabSettings.iCaptureMode == ECaptureModeSequantialCapture)
       
   581         {          
       
   582 //        memoryInUse = iGrabSettings.iSequantialCaptureMemoryInUseMultiDrive;
       
   583         intMemInUse = iGrabSettings.iSequantialCaptureMemoryInUseMultiDrive;
       
   584         
       
   585         imageFormat = iGrabSettings.iSequantialCaptureImageFormat;
       
   586         fileName = iGrabSettings.iSequantialCaptureFileName;
       
   587         }
       
   588     else
       
   589         User::Panic(_L("Wrong mode"), 30);  
       
   590 
       
   591 
       
   592     // init the path for saving the file
       
   593 	
       
   594 	iSaveFileName.Copy( PathInfo::PhoneMemoryRootPath() );
       
   595 	
       
   596 //    if (memoryInUse != EDriveC)//something different as PhoneMemory (memory card or mass memory)
       
   597     if (intMemInUse != 0)//something different as PhoneMemory (memory card or mass memory)
       
   598     	{
       
   599 		memoryInUse = EDriveY;	    
       
   600 		if (PathInfo::GetRootPath(iSaveFileName, memoryInUse) != KErrNone || !DriveOK(memoryInUse))
       
   601 			{
       
   602 			//we need to find first available memory card in EDriveE - EDriveY range
       
   603 			for (TInt i = EDriveY; i>=EDriveE; i--)
       
   604 				{
       
   605 				if ( DriveOK((TDriveNumber)(i)))
       
   606 					{
       
   607 					if (IsDriveMMC((TDriveNumber)(i)))
       
   608 						{
       
   609 						PathInfo::GetRootPath(iSaveFileName, (TDriveNumber)(i));
       
   610 						break;
       
   611 						}    
       
   612 					}
       
   613 				}
       
   614 			}
       
   615     	}
       
   616 
       
   617     iSaveFileName.Append( PathInfo::ImagesPath() );
       
   618     iSaveFileName.Append( KScreenShotsSubDirectory );
       
   619     
       
   620     
       
   621     // a quick check that filename is valid
       
   622     if (fileName.Length() > 0 && fileName.Length() <= 255) 
       
   623         iSaveFileName.Append( fileName );
       
   624     else
       
   625         iSaveFileName.Append( KDefaultImageFileName );
       
   626 
       
   627     iSaveFileName.Append( _L(".") );
       
   628 
       
   629 
       
   630     // reset the encoder
       
   631     if (iImageEncoder)
       
   632         {
       
   633         delete iImageEncoder;
       
   634         iImageEncoder = NULL;
       
   635         }
       
   636         
       
   637     
       
   638     switch (imageFormat)
       
   639         {
       
   640         case EImageFormatPNG:
       
   641             {
       
   642             // set filename
       
   643             iSaveFileName.Append(_L("png"));
       
   644             CApaApplication::GenerateFileName(iFileSession, iSaveFileName );  // unique filename
       
   645 
       
   646             // init & convert
       
   647             iImageEncoder = CImageEncoder::FileNewL(iFileSession, iSaveFileName, CImageEncoder::EOptionAlwaysThread, KImageTypePNGUid);
       
   648 			
       
   649             iImageEncoder->Convert( &iStatus, *iPreviouslyCapturedBitmap );
       
   650             }
       
   651             break;
       
   652         
       
   653         case EImageFormatJPGHQ:
       
   654         case EImageFormatJPGLQ:
       
   655             {
       
   656             // reset frameimagedata
       
   657             if (iFrameImageData)
       
   658                 {
       
   659                 delete iFrameImageData;
       
   660                 iFrameImageData = NULL;
       
   661                 }
       
   662             
       
   663             // set filename
       
   664             iSaveFileName.Append(_L("jpg"));
       
   665             CApaApplication::GenerateFileName(iFileSession, iSaveFileName );  // unique filename
       
   666 
       
   667             // init 
       
   668             iImageEncoder = CImageEncoder::FileNewL(iFileSession, iSaveFileName, CImageEncoder::EOptionAlwaysThread, KImageTypeJPGUid);
       
   669 
       
   670             // JPEG properties
       
   671             TJpegImageData* imageData = new(ELeave) TJpegImageData;
       
   672             imageData->iSampleScheme = TJpegImageData::EColor444;
       
   673             imageData->iQualityFactor = (imageFormat==EImageFormatJPGHQ) ? HIGH_QUALITY_JPEG : LOW_QUALITY_JPEG;
       
   674             iFrameImageData = CFrameImageData::NewL();
       
   675             User::LeaveIfError(iFrameImageData->AppendImageData(imageData));  //ownership of imageData is transferred
       
   676 
       
   677             // convert
       
   678             iImageEncoder->Convert( &iStatus, *iPreviouslyCapturedBitmap, iFrameImageData );
       
   679             }
       
   680             break;
       
   681         
       
   682         case EImageFormatBMP:
       
   683             {
       
   684             // set filename
       
   685             iSaveFileName.Append(_L("bmp"));
       
   686             CApaApplication::GenerateFileName(iFileSession, iSaveFileName );  // unique filename
       
   687 
       
   688             // init & convert
       
   689             iImageEncoder = CImageEncoder::FileNewL(iFileSession, iSaveFileName, CImageEncoder::EOptionAlwaysThread, KImageTypeBMPUid);
       
   690             iImageEncoder->Convert( &iStatus, *iPreviouslyCapturedBitmap );
       
   691             }
       
   692             break;
       
   693 
       
   694         case EImageFormatGIF:
       
   695             {
       
   696             // set filename
       
   697             iSaveFileName.Append(_L("gif"));
       
   698             CApaApplication::GenerateFileName(iFileSession, iSaveFileName );  // unique filename
       
   699 
       
   700             // init & convert
       
   701             iImageEncoder = CImageEncoder::FileNewL(iFileSession, iSaveFileName, CImageEncoder::EOptionAlwaysThread, KImageTypeGIFUid);
       
   702             iImageEncoder->Convert( &iStatus, *iPreviouslyCapturedBitmap );
       
   703             }
       
   704             break;
       
   705             
       
   706         case EImageFormatMBM:
       
   707             {
       
   708             // set filename
       
   709             iSaveFileName.Append(_L("mbm"));
       
   710             CApaApplication::GenerateFileName(iFileSession, iSaveFileName );  // unique filename
       
   711 
       
   712             // init & convert
       
   713             iImageEncoder = CImageEncoder::FileNewL(iFileSession, iSaveFileName, CImageEncoder::EOptionAlwaysThread, KImageTypeMBMUid);
       
   714             iImageEncoder->Convert( &iStatus, *iPreviouslyCapturedBitmap );
       
   715             }
       
   716             break;
       
   717             
       
   718         default:
       
   719             {
       
   720             User::Panic(_L("Invalid Img Type"), 20);
       
   721             }
       
   722         }
       
   723 
       
   724     // set the state of the active object
       
   725     iState = EEncodingImage;
       
   726 
       
   727     // indicate an outstanding request
       
   728     SetActive();
       
   729     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- TakeSsAndSave end"));
       
   730 	return true;
       
   731     }
       
   732     
       
   733 // ---------------------------------------------------------------------------
       
   734 
       
   735 
       
   736 TBool SGEngine::DriveOK(TDriveNumber aNumber)
       
   737     {
       
   738 	
       
   739     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- Driveok start"));
       
   740     TBool isOK(EFalse);
       
   741 
       
   742     TVolumeInfo vInfo;
       
   743 
       
   744     // check if we can access the drive
       
   745     if (iFileSession.Volume(vInfo, aNumber) == KErrNone)
       
   746         isOK = ETrue;
       
   747 
       
   748     // returns ETrue if memory card working properly
       
   749     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- DriveOK end"));
       
   750     return isOK;
       
   751 	
       
   752     }
       
   753 // ---------------------------------------------------------------------------
       
   754 
       
   755 
       
   756 TBool SGEngine::IsDriveMMC(TDriveNumber aDrive)
       
   757     {
       
   758     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- IsDriveMMC start"));
       
   759     TBool isOK(EFalse);
       
   760 
       
   761     TDriveInfo ii;   
       
   762     if (iFileSession.Drive(ii, aDrive)==KErrNone) 
       
   763         {      
       
   764             if (ii.iType!=EMediaNotPresent &&
       
   765                 ii.iType!=EMediaUnknown &&           
       
   766                 ii.iType!=EMediaCdRom &&         
       
   767                 ii.iType!=EMediaRom) 
       
   768                 {           // memory card          
       
   769                     isOK=ETrue;      
       
   770                 }   
       
   771         }    
       
   772                 
       
   773     
       
   774     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- IsDriveMMC end"));
       
   775     return isOK;
       
   776     }
       
   777 
       
   778 
       
   779 void SGEngine::CapturingFinishedL(TInt aErr)
       
   780     {
       
   781 	
       
   782     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- CapturingFinished start"));
       
   783     // display a global query to show the results
       
   784     
       
   785     if (aErr == KErrNone)
       
   786         {
       
   787         switch (iGrabSettings.iCaptureMode)
       
   788             {
       
   789             case ECaptureModeSingleCapture:
       
   790                 {
       
   791                 iEngineWrapper->ShowImageCapturedNote();
       
   792                 }
       
   793                 break;
       
   794             
       
   795             case ECaptureModeSequantialCapture:
       
   796                 {
       
   797                 iEngineWrapper->ShowSequantialImagesCapturedNote(iNumberOfTakenShots);
       
   798                 }
       
   799                 break;            
       
   800     
       
   801             case ECaptureModeVideoCapture:
       
   802                 {
       
   803                 iEngineWrapper->ShowVideoCapturedNote();
       
   804                 }
       
   805                 break;             
       
   806     
       
   807             default:
       
   808                 User::Panic(_L("Inv.capt.mode"), 51);
       
   809                 break;
       
   810             }            
       
   811        
       
   812         }
       
   813     else
       
   814         {
       
   815         
       
   816         // Get error message with CTextResolver
       
   817         CTextResolver* textResolver = CTextResolver::NewLC();
       
   818         iEngineWrapper->ShowErrorMessage(textResolver->ResolveErrorString(aErr));
       
   819 
       
   820         CleanupStack::PopAndDestroy();  //textResolver    
       
   821         
       
   822         }
       
   823 
       
   824     // capturing can now be restarted
       
   825     iState = EIdle;
       
   826     iCapturingInProgress = EFalse;
       
   827     iStopCapturing = EFalse;
       
   828     
       
   829     // reset values
       
   830     iNumberOfTakenShots = 0;
       
   831     iCurrentFrameNumber = 0;
       
   832     iVideoFrameArray->Reset();
       
   833     
       
   834     
       
   835     iState = EQueryDelay;
       
   836     iTimer.After(iStatus, 2000000);
       
   837     SetActive(); 
       
   838     
       
   839     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- capturingfinished end"));
       
   840 	
       
   841     }
       
   842 
       
   843 
       
   844 #if defined(HB_QT_S60_EVENT_FILTER)
       
   845     TBool SGEngine::HandleCaptureCommandsL(const TWsEvent* aEvent)
       
   846 	{
       
   847 #else
       
   848     TBool SGEngine::HandleCaptureCommandsL(const QSymbianEvent *event)
       
   849 	{
       
   850     if (event->type() != QSymbianEvent::WindowServerEvent) {
       
   851         return ETrue; //continueEventLoop
       
   852     }
       
   853     const TWsEvent *aEvent = event->windowServerEvent();
       
   854 #endif
       
   855 	
       
   856     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- HandleCaptureCommand start"));
       
   857 	
       
   858 	
       
   859     TBool continueEventLoop(ETrue);
       
   860     
       
   861     // get hotkey of the capture
       
   862     TInt captureHotkey(0);
       
   863     if (iGrabSettings.iCaptureMode == ECaptureModeSingleCapture)
       
   864         captureHotkey = iGrabSettings.iSingleCaptureHotkey;
       
   865     else if (iGrabSettings.iCaptureMode == ECaptureModeSequantialCapture)
       
   866         captureHotkey = iGrabSettings.iSequantialCaptureHotkey;
       
   867     else if (iGrabSettings.iCaptureMode == ECaptureModeVideoCapture)
       
   868         captureHotkey = iGrabSettings.iVideoCaptureHotkey;
       
   869     else
       
   870         User::Panic(_L("Wrong mode"), 41);
       
   871     
       
   872     // ignore any errors
       
   873     if (aEvent->Type()==EEventErrorMessage)
       
   874         {
       
   875         // error
       
   876         }
       
   877     
       
   878             
       
   879     // handle captured keys, we are interested here only of the keydown events
       
   880     else
       
   881         
       
   882         if (
       
   883               ( captureHotkey == EHotkeySendKey &&
       
   884                 aEvent->Type()==EEventKeyDown && aEvent->Key()->iScanCode==EStdKeyYes )
       
   885             ||
       
   886               ( captureHotkey == EHotkeyPowerKey &&
       
   887                 aEvent->Type()==EEventKeyDown && aEvent->Key()->iScanCode==EStdKeyDevice2 )
       
   888             ||
       
   889               ( captureHotkey == EHotkeySideKey &&
       
   890                 aEvent->Type()==EEventKeyDown && aEvent->Key()->iScanCode==EStdKeyDevice6 )
       
   891             ||
       
   892               ( captureHotkey == EHotkeyCameraKey1 &&
       
   893                 aEvent->Type()==EEventKeyDown && aEvent->Key()->iScanCode==EStdKeyDevice7 )
       
   894             )
       
   895         {
       
   896         
       
   897         // check if already capturing images in sequence
       
   898         if ( iCapturingInProgress && !iStopCapturing && iNumberOfTakenShots!=0 && iGrabSettings.iCaptureMode == ECaptureModeSequantialCapture )
       
   899             {
       
   900             // asking to stop capturing
       
   901             iStopCapturing = ETrue; 
       
   902             
       
   903             // cancel the active object, this will cancel any timer delays and ICL stuff
       
   904             Cancel();
       
   905             
       
   906             // set status
       
   907             iState = ECancelCapturing; 
       
   908             
       
   909             // jump smoothly to RunL()
       
   910             iTimer.After(iStatus, 50);
       
   911             SetActive(); 
       
   912 
       
   913             // do not continue the event loop in HandleWsEventL for these events
       
   914             continueEventLoop = EFalse;
       
   915             }
       
   916 
       
   917         // check if already capturing video
       
   918         else if ( iCapturingInProgress && !iStopCapturing && iGrabSettings.iCaptureMode == ECaptureModeVideoCapture )
       
   919             {
       
   920             // asking to stop capturing
       
   921             iStopCapturing = ETrue;
       
   922             
       
   923             // cancel the active object, this will cancel any timer delays and ICL stuff
       
   924             Cancel();
       
   925 
       
   926             // set status
       
   927             iState = ECancelVideoCapturing;
       
   928             
       
   929             // jump smoothly to RunL()
       
   930             iTimer.After(iStatus, 50);
       
   931             SetActive(); 
       
   932 
       
   933             // do not continue the event loop in HandleWsEventL for these events
       
   934             continueEventLoop = EFalse;
       
   935             }        
       
   936         else if (!iCapturingInProgress && (iGrabSettings.iCaptureMode == ECaptureModeSingleCapture || iGrabSettings.iCaptureMode == ECaptureModeSequantialCapture ))
       
   937             {
       
   938             
       
   939             // take a screen shot and save it
       
   940            if( TakeScreenShotAndSaveL())
       
   941                {
       
   942                // not capturing anything, so start doing that
       
   943                iCapturingInProgress = ETrue;    
       
   944                // do not continue the event loop in HandleWsEventL for these events
       
   945                continueEventLoop = EFalse;
       
   946                }
       
   947             }
       
   948         
       
   949         else if (!iCapturingInProgress && iGrabSettings.iCaptureMode == ECaptureModeVideoCapture )
       
   950             {
       
   951             // not capturing anything, so start doing that
       
   952             iCapturingInProgress = ETrue;
       
   953             
       
   954             // clean temporary files
       
   955             TRAP_IGNORE( CleanTemporaryFilesL() );
       
   956             
       
   957             // get initial dimensions for the video
       
   958             CWsScreenDevice* screenDevice = new(ELeave) CWsScreenDevice ( CEikonEnv::Static()->WsSession() );
       
   959             CleanupStack::PushL(screenDevice);
       
   960 			
       
   961             User::LeaveIfError( screenDevice->Construct( CEikonEnv::Static()->WsSession().GetFocusScreen() ) );
       
   962 			
       
   963             iVideoDimensions = screenDevice->SizeInPixels();
       
   964             iPreviousFrameScreenDimension = screenDevice->SizeInPixels();
       
   965             CleanupStack::PopAndDestroy(); // screenDevice
       
   966 
       
   967             // capture the first frame
       
   968             CaptureFrameForVideoL();              
       
   969 
       
   970             // do not continue the event loop in HandleWsEventL for these events
       
   971             continueEventLoop = EFalse;
       
   972             }
       
   973 
       
   974         }
       
   975 
       
   976     // catch other event types as well so that we can ignore them
       
   977     else if (
       
   978               ( captureHotkey == EHotkeySendKey &&
       
   979                 aEvent->Key()->iScanCode==EStdKeyYes )
       
   980             ||
       
   981               ( captureHotkey == EHotkeyPowerKey &&
       
   982                 aEvent->Key()->iScanCode==EStdKeyDevice2 )
       
   983             ||
       
   984               ( captureHotkey == EHotkeySideKey &&
       
   985                 aEvent->Key()->iScanCode==EStdKeyDevice6 )
       
   986             ||
       
   987               ( captureHotkey == EHotkeyCameraKey1 &&
       
   988                 aEvent->Key()->iScanCode==EStdKeyDevice7 )
       
   989            )
       
   990         {
       
   991         // do not continue the event loop in HandleWsEventL for these events
       
   992         continueEventLoop = EFalse;
       
   993         }
       
   994 
       
   995     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- HandleCapturcommand end"));
       
   996 
       
   997     
       
   998     return continueEventLoop;
       
   999     
       
  1000     }
       
  1001 
       
  1002 // ---------------------------------------------------------------------------
       
  1003 
       
  1004 void SGEngine::CleanTemporaryFilesL()
       
  1005     {
       
  1006 	
       
  1007     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- CleanTempFiles start"));
       
  1008     
       
  1009     // delete temporary files from C and D drives    
       
  1010     CFileMan* fileMan = CFileMan::NewL(iFileSession);
       
  1011 
       
  1012     TFileName delFilesPath;
       
  1013         
       
  1014     for (TInt i=0; i<1; i++)
       
  1015         {
       
  1016         delFilesPath.Copy(KNullDesC);
       
  1017         delFilesPath.Append('C'+i);
       
  1018         delFilesPath.Append(_L(":"));
       
  1019         delFilesPath.Append(KSGTemporaryDirectory);
       
  1020         delFilesPath.Append(_L("*.$$$"));
       
  1021         
       
  1022         fileMan->Delete(delFilesPath);
       
  1023         }
       
  1024 
       
  1025     delete fileMan;    
       
  1026     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- CleanTempfile end"));
       
  1027 	
       
  1028     }
       
  1029 
       
  1030 // ---------------------------------------------------------------------------
       
  1031 
       
  1032 void SGEngine::ActivateModelL()
       
  1033     {
       
  1034     // clean temporary files
       
  1035     TRAP_IGNORE( CleanTemporaryFilesL() );
       
  1036             
       
  1037     // load settings
       
  1038     TRAP_IGNORE( LoadSettingsL() );
       
  1039 
       
  1040     // start capturing
       
  1041     ActivateCaptureKeysL();
       
  1042     }
       
  1043 
       
  1044 // ---------------------------------------------------------------------------
       
  1045 
       
  1046 void SGEngine::DeActivateModelL()
       
  1047     {
       
  1048 	
       
  1049     CancelCapturing();
       
  1050 
       
  1051     // for a faster exit, send the application to background
       
  1052     TApaTask selfTask(CEikonEnv::Static()->WsSession());
       
  1053     selfTask.SetWgId(iRootWin.Identifier());
       
  1054     selfTask.SendToBackground();
       
  1055 	
       
  1056 	
       
  1057     }
       
  1058     
       
  1059 // ---------------------------------------------------------------------------
       
  1060 
       
  1061 
       
  1062 void SGEngine::CaptureFrameForVideoL()
       
  1063     {
       
  1064     
       
  1065     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- CaptureFrameforvide start"));
       
  1066     // record time
       
  1067     TTime timeNow;
       
  1068     timeNow.HomeTime();
       
  1069     
       
  1070     // take a screen shot   
       
  1071     CFbsBitmap* currentCapturedBitmap = new(ELeave) CFbsBitmap;
       
  1072     CleanupStack::PushL(currentCapturedBitmap);
       
  1073 
       
  1074     CWsScreenDevice* screenDevice = new(ELeave) CWsScreenDevice( CEikonEnv::Static()->WsSession() );
       
  1075     CleanupStack::PushL( screenDevice );
       
  1076     User::LeaveIfError( screenDevice->Construct( CEikonEnv::Static()->WsSession().GetFocusScreen() ) );
       
  1077     
       
  1078     TSize currentScreenSize = screenDevice->SizeInPixels();
       
  1079 
       
  1080     User::LeaveIfError( currentCapturedBitmap->Create(currentScreenSize, EColor256) );
       
  1081     User::LeaveIfError( screenDevice->CopyScreenToBitmap(currentCapturedBitmap) );
       
  1082     CleanupStack::PopAndDestroy(); // screenDevice
       
  1083 
       
  1084     // grow video's dimensions if the size has changed
       
  1085     if (currentScreenSize.iWidth > iVideoDimensions.iWidth)
       
  1086         {
       
  1087         iVideoDimensions.iWidth = currentScreenSize.iWidth;
       
  1088         }
       
  1089     if (currentScreenSize.iHeight > iVideoDimensions.iHeight)
       
  1090         {
       
  1091         iVideoDimensions.iHeight = currentScreenSize.iHeight;
       
  1092         }
       
  1093 
       
  1094     TInt64 currentDelay(0);
       
  1095  
       
  1096 
       
  1097     // create a new frame
       
  1098     TVideoFrame frame;
       
  1099     frame.iDelay = 500; // use default delay 5.00 secs
       
  1100     
       
  1101     // get info of the RAM drive
       
  1102     TDriveNumber ramDrive = EDriveD;
       
  1103     TVolumeInfo ramDriveInfo;
       
  1104     iFileSession.Volume(ramDriveInfo, ramDrive);
       
  1105     
       
  1106     // init the directory for saving the file, preferably use ram drive if there is enough disk space, otherwise use always C drive
       
  1107     TFileName tempDirectory;
       
  1108     TFileName sessionPath;
       
  1109     
       
  1110     if (ramDriveInfo.iFree > (iVideoDimensions.iWidth*iVideoDimensions.iHeight+50000))
       
  1111         sessionPath.Copy( _L("D:") );
       
  1112     else
       
  1113         sessionPath.Copy( _L("C:") );
       
  1114     
       
  1115     sessionPath.Append(KSGTemporaryDirectory);
       
  1116     tempDirectory.Copy(KSGTemporaryDirectory);
       
  1117     
       
  1118     iFileSession.MkDirAll(sessionPath);
       
  1119     iFileSession.SetSessionPath(sessionPath);
       
  1120 
       
  1121     // create a temp file, path to the bitmap is saved automatically to frame.iFileStorePath
       
  1122     RFile file;
       
  1123     User::LeaveIfError( file.Temp(iFileSession, tempDirectory, frame.iFileStorePath, EFileWrite) );
       
  1124     RFileWriteStream writeStream(file);
       
  1125     
       
  1126     TBool ignoreFrame(EFalse);
       
  1127     
       
  1128     // check if is this the first frame
       
  1129     if (iCurrentFrameNumber == 0)
       
  1130         {
       
  1131         // first frame is always the full one
       
  1132         frame.iWidth = currentScreenSize.iWidth;    
       
  1133         frame.iHeight = currentScreenSize.iHeight;  
       
  1134         frame.iXPos = 0;
       
  1135         frame.iYPos = 0;
       
  1136         frame.iEnableTransparency = EFalse;
       
  1137         frame.iFillsWholeScreen = ETrue;
       
  1138         
       
  1139         currentCapturedBitmap->ExternalizeL(writeStream);  
       
  1140 
       
  1141         }
       
  1142     
       
  1143     else
       
  1144         {
       
  1145         // next frame is a difference between the previous one
       
  1146         currentDelay = timeNow.MicroSecondsFrom(iPreviousFrameTaken).Int64();
       
  1147         
       
  1148         // get reference to previos frame
       
  1149         TVideoFrame& prevFrame = iVideoFrameArray->At(iVideoFrameArray->Count()-1);
       
  1150         
       
  1151         
       
  1152         // check if video dimensions have changed
       
  1153         if (currentScreenSize.iWidth != iPreviousFrameScreenDimension.iWidth
       
  1154             || currentScreenSize.iHeight != iPreviousFrameScreenDimension.iHeight)
       
  1155             {
       
  1156             // dimensions have changed -> save a full bitmap
       
  1157             frame.iWidth = currentScreenSize.iWidth;    
       
  1158             frame.iHeight = currentScreenSize.iHeight;  
       
  1159             frame.iXPos = 0;
       
  1160             frame.iYPos = 0;
       
  1161             frame.iEnableTransparency = EFalse;
       
  1162             frame.iFillsWholeScreen = ETrue;
       
  1163             
       
  1164             currentCapturedBitmap->ExternalizeL(writeStream);            
       
  1165 
       
  1166             // update the previous frame to contain the new delay value
       
  1167             prevFrame.iDelay = TUint( (double) currentDelay / 10000 );
       
  1168             }
       
  1169 
       
  1170         else
       
  1171             {
       
  1172             // compare the bitmaps
       
  1173             HBufC8* curImgScanLineBuf = HBufC8::NewLC(currentScreenSize.iWidth*3);
       
  1174             TPtr8 curImgScanLinePtr = curImgScanLineBuf->Des();
       
  1175             HBufC8* prevImgScanLineBuf = HBufC8::NewLC(currentScreenSize.iWidth*3);
       
  1176             TPtr8 prevImgScanLinePtr = prevImgScanLineBuf->Des();
       
  1177             
       
  1178             TPoint pt(0,0);
       
  1179             TBool differenceFound(EFalse);
       
  1180             TPoint leftTopDifferencePoint(0,0);
       
  1181             TPoint rightBottomDifferencePoint(currentScreenSize.iWidth,currentScreenSize.iHeight);
       
  1182             
       
  1183             // scan the image from top to bottom
       
  1184             for (TInt i=0; i<currentScreenSize.iHeight; i++)
       
  1185                 {
       
  1186                 pt.iY = i;
       
  1187                 
       
  1188                 currentCapturedBitmap->GetScanLine(curImgScanLinePtr, pt, currentScreenSize.iWidth, EColor256);
       
  1189                 iPreviouslyCapturedBitmap->GetScanLine(prevImgScanLinePtr, pt, currentScreenSize.iWidth, EColor256);
       
  1190                 
       
  1191                 if (curImgScanLinePtr != prevImgScanLinePtr)
       
  1192                     {
       
  1193                     differenceFound = ETrue;
       
  1194                     
       
  1195                     // get the y-coordinate
       
  1196                     leftTopDifferencePoint.iY = i;
       
  1197 
       
  1198                     break;    
       
  1199                     }
       
  1200                 }
       
  1201                 
       
  1202             if (differenceFound)
       
  1203                 {
       
  1204                 // now we know that there is some difference between those two captured frames,
       
  1205                 // get the bottom value by scaning from bottom to top
       
  1206                 for (TInt i=currentScreenSize.iHeight-1; i>=0; i--)
       
  1207                     {
       
  1208                     pt.iY = i;
       
  1209                     
       
  1210                     currentCapturedBitmap->GetScanLine(curImgScanLinePtr, pt, currentScreenSize.iWidth, EColor256);
       
  1211                     iPreviouslyCapturedBitmap->GetScanLine(prevImgScanLinePtr, pt, currentScreenSize.iWidth, EColor256);
       
  1212                     
       
  1213                     if (curImgScanLinePtr != prevImgScanLinePtr)
       
  1214                         {
       
  1215                         // get the y-coordinate
       
  1216                         rightBottomDifferencePoint.iY = i+1;
       
  1217 
       
  1218                         break;    
       
  1219                         }
       
  1220                     }
       
  1221                     
       
  1222                 // check that the height of the cropped image will be at least 1
       
  1223                 if (rightBottomDifferencePoint.iY <= leftTopDifferencePoint.iY)
       
  1224                     rightBottomDifferencePoint.iY = leftTopDifferencePoint.iY+1;  
       
  1225                 
       
  1226                       
       
  1227                 // get also the x-coordinates by scanning vertical scan lines
       
  1228                 HBufC8* curImgVerticalScanLineBuf = HBufC8::NewLC(currentScreenSize.iHeight*3);
       
  1229                 TPtr8 curImgVerticalScanLinePtr = curImgScanLineBuf->Des();
       
  1230                 HBufC8* prevImgVerticalScanLineBuf = HBufC8::NewLC(currentScreenSize.iHeight*3);
       
  1231                 TPtr8 prevImgVerticalScanLinePtr = prevImgScanLineBuf->Des();
       
  1232                         
       
  1233                 // first scan by from left to right
       
  1234                 for (TInt i=0; i<currentScreenSize.iWidth; i++)
       
  1235                     {
       
  1236                     currentCapturedBitmap->GetVerticalScanLine(curImgVerticalScanLinePtr, i, EColor256);
       
  1237                     iPreviouslyCapturedBitmap->GetVerticalScanLine(prevImgVerticalScanLinePtr, i, EColor256);
       
  1238                     
       
  1239                     if (curImgVerticalScanLinePtr != prevImgVerticalScanLinePtr)
       
  1240                         {
       
  1241                         leftTopDifferencePoint.iX = i;
       
  1242                         break;
       
  1243                         }
       
  1244                     }
       
  1245 
       
  1246                 // finally scan from right to left
       
  1247                 for (TInt i=currentScreenSize.iWidth-1; i>=0; i--)
       
  1248                     {
       
  1249                     currentCapturedBitmap->GetVerticalScanLine(curImgVerticalScanLinePtr, i, EColor256);
       
  1250                     iPreviouslyCapturedBitmap->GetVerticalScanLine(prevImgVerticalScanLinePtr, i, EColor256);
       
  1251                     
       
  1252                     if (curImgVerticalScanLinePtr != prevImgVerticalScanLinePtr)
       
  1253                         {
       
  1254                         rightBottomDifferencePoint.iX = i+1;
       
  1255                         break;
       
  1256                         }
       
  1257                     }
       
  1258                     
       
  1259                 CleanupStack::PopAndDestroy(2); //curImgVerticalScanLineBuf,prevImgVerticalScanLineBuf               
       
  1260 
       
  1261                 
       
  1262                 // check that the width of the cropped image will be at least 1
       
  1263                 if (rightBottomDifferencePoint.iX <= leftTopDifferencePoint.iX)
       
  1264                     rightBottomDifferencePoint.iX = leftTopDifferencePoint.iX+1;
       
  1265                    
       
  1266                 
       
  1267                 // record dimensions and position of the image           
       
  1268                 frame.iWidth = rightBottomDifferencePoint.iX - leftTopDifferencePoint.iX;    
       
  1269                 frame.iHeight = rightBottomDifferencePoint.iY - leftTopDifferencePoint.iY;  
       
  1270                 frame.iXPos = leftTopDifferencePoint.iX;
       
  1271                 frame.iYPos = leftTopDifferencePoint.iY;
       
  1272                 frame.iEnableTransparency = ETrue;
       
  1273                 frame.iFillsWholeScreen = EFalse;
       
  1274             
       
  1275             
       
  1276                 // take a copy of the current frame
       
  1277                 CFbsBitmap* workingBitmap = new(ELeave) CFbsBitmap;
       
  1278                 CleanupStack::PushL(workingBitmap);
       
  1279                 User::LeaveIfError( workingBitmap->Create(currentScreenSize, EColor256) );
       
  1280 
       
  1281                 HBufC8* tempScanLineBuf = HBufC8::NewLC(currentScreenSize.iWidth*3);
       
  1282                 TPtr8 tempScanLinePtr = tempScanLineBuf->Des();
       
  1283                 
       
  1284                 for (TInt i=0; i<currentScreenSize.iHeight; i++)
       
  1285                     {
       
  1286                     pt.iY = i;
       
  1287                     currentCapturedBitmap->GetScanLine(tempScanLinePtr, pt, currentScreenSize.iWidth, EColor256);
       
  1288                     workingBitmap->SetScanLine(tempScanLinePtr, i);
       
  1289                     }
       
  1290                     
       
  1291                 CleanupStack::PopAndDestroy(); //tempScanLineBuf
       
  1292                 
       
  1293                 
       
  1294                 // mark the non-changed areas with transparency color
       
  1295                 TUint8* curPtr = NULL;
       
  1296                 TUint8* prevPtr = NULL;
       
  1297                 for (TInt i=frame.iYPos; i<frame.iYPos+frame.iHeight; i++)
       
  1298                     {
       
  1299                     pt.iY = i;
       
  1300                     
       
  1301                     workingBitmap->GetScanLine(curImgScanLinePtr, pt, currentScreenSize.iWidth, EColor256);
       
  1302                     iPreviouslyCapturedBitmap->GetScanLine(prevImgScanLinePtr, pt, currentScreenSize.iWidth, EColor256);
       
  1303                     
       
  1304                     // check single pixels in the scanline
       
  1305                     for (TInt j=frame.iXPos; j<frame.iXPos+frame.iWidth; j++)
       
  1306                         {
       
  1307                         curPtr = &curImgScanLinePtr[j];
       
  1308                         prevPtr = &prevImgScanLinePtr[j];
       
  1309                         
       
  1310                         // check that our transparency index isn't already in use
       
  1311                         if (curPtr[0] == TRANSPARENCY_INDEX)
       
  1312                             curPtr[0] = TRANSPARENCY_ALTERNATIVE_INDEX;
       
  1313                         
       
  1314                         // replace with transparency index if there is no change compared to the previous frame
       
  1315                         if (curPtr[0] == prevPtr[0])
       
  1316                             curPtr[0] = TRANSPARENCY_INDEX;
       
  1317                         }
       
  1318                         
       
  1319                     // set new scanline    
       
  1320                     workingBitmap->SetScanLine(curImgScanLinePtr, i);
       
  1321                     }
       
  1322 
       
  1323 
       
  1324                 // externalize the bitmap
       
  1325                 TRect changedRect(leftTopDifferencePoint, rightBottomDifferencePoint);
       
  1326                 workingBitmap->ExternalizeRectangleL(writeStream, changedRect);
       
  1327                 
       
  1328                 CleanupStack::PopAndDestroy(); //workingBitmap
       
  1329  
       
  1330                 // update the previous frame to contain the new delay value
       
  1331                 prevFrame.iDelay = TUint( (double) currentDelay / 10000 );
       
  1332                 }
       
  1333 
       
  1334             else
       
  1335                 {
       
  1336                 // frames are identical, we can just ignore this one
       
  1337                 ignoreFrame = ETrue;     
       
  1338                 }
       
  1339             
       
  1340             CleanupStack::PopAndDestroy(2); //curImgScanLineBuf,prevImgScanLineBuf
       
  1341 
       
  1342             } // if (videoDimensionsHaveChanged)
       
  1343 
       
  1344         } //if (iCurrentFrameNumber == 0)
       
  1345             
       
  1346     // close the stream
       
  1347     writeStream.CommitL();
       
  1348     writeStream.Close();
       
  1349     file.Close();
       
  1350     
       
  1351 
       
  1352     if (ignoreFrame)
       
  1353         {
       
  1354         // delete the temp file since we don't need that
       
  1355             iFileSession.Delete(frame.iFileStorePath);
       
  1356         }
       
  1357     else
       
  1358         {
       
  1359         // remember for the next frame when this frame was taken
       
  1360         iPreviousFrameTaken = timeNow;
       
  1361 
       
  1362         // take a copy of currentCapturedBitmap to iPreviouslyCapturedBitmap
       
  1363         User::LeaveIfError( iPreviouslyCapturedBitmap->Create(iVideoDimensions, EColor256) );
       
  1364 
       
  1365         TPoint pt(0,0);
       
  1366         HBufC8* tempScanLineBuf = HBufC8::NewMaxLC(iVideoDimensions.iWidth);
       
  1367         TPtr8 tempScanLinePtr = tempScanLineBuf->Des();
       
  1368         
       
  1369         for (TInt i=0; i<iVideoDimensions.iHeight; i++)
       
  1370             {
       
  1371             pt.iY = i;
       
  1372             currentCapturedBitmap->GetScanLine(tempScanLinePtr, pt, iVideoDimensions.iWidth, EColor256);
       
  1373             iPreviouslyCapturedBitmap->SetScanLine(tempScanLinePtr, i);
       
  1374             }
       
  1375             
       
  1376         CleanupStack::PopAndDestroy(); //tempScanLineBuf
       
  1377         
       
  1378         // append frame information to the array
       
  1379         iVideoFrameArray->AppendL(frame);
       
  1380         
       
  1381         // remember screen size
       
  1382         iPreviousFrameScreenDimension = currentScreenSize;
       
  1383         }    
       
  1384 
       
  1385     
       
  1386     CleanupStack::PopAndDestroy(); //currentCapturedBitmap
       
  1387     
       
  1388 
       
  1389     // set the state of the active object
       
  1390     iState = ENextVideoFrame;
       
  1391     
       
  1392     // check time spent on the work above (probably this is not so important)
       
  1393     TTime timeNow2;
       
  1394     timeNow2.HomeTime();
       
  1395     TInt64 handlingDelay = timeNow2.MicroSecondsFrom(timeNow).Int64();
       
  1396     
       
  1397     // calculate delay till next frame
       
  1398     TUint idealDelay = VIDEO_CAPTURE_DELAY*1000;
       
  1399     TInt usedDelay; 
       
  1400     if (currentDelay > idealDelay)
       
  1401         usedDelay = idealDelay - (currentDelay - idealDelay) - handlingDelay;
       
  1402     else
       
  1403         usedDelay = idealDelay - handlingDelay;
       
  1404     
       
  1405     // check that the delay is atleast minimum delay anyway
       
  1406     if (usedDelay < VIDEO_CAPTURE_MINIMUM_DELAY*1000)
       
  1407         usedDelay = VIDEO_CAPTURE_MINIMUM_DELAY*1000;
       
  1408     
       
  1409     iTimer.After(iStatus, usedDelay);
       
  1410 
       
  1411     // indicate an outstanding request
       
  1412     SetActive();
       
  1413     
       
  1414     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- Captureframeforvideo end"));
       
  1415 	
       
  1416     }
       
  1417 
       
  1418 // ---------------------------------------------------------------------------
       
  1419 
       
  1420 void SGEngine::SaveVideoL(TInt aErr)
       
  1421     {
       
  1422 
       
  1423     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- SaveVideo start"));
       
  1424     
       
  1425     if (aErr)
       
  1426         CapturingFinishedL(aErr);   
       
  1427    
       
  1428     else if (iGrabSettings.iVideoCaptureVideoFormat == EVideoFormatAnimatedGIF)
       
  1429         {
       
  1430         TInt err(KErrNone);
       
  1431 
       
  1432         
       
  1433 			iSaveFileName.Copy( PathInfo::PhoneMemoryRootPath() );
       
  1434             if (iGrabSettings.iVideoCaptureMemoryInUseMultiDrive != 0)//something different as PhoneMemory (memory card or mass memory)
       
  1435             	{
       
  1436         		if (PathInfo::GetRootPath(iSaveFileName,EDriveY) != KErrNone || !DriveOK(EDriveY))
       
  1437         			{
       
  1438         			//we need to find first available memory card in EDriveE - EDriveY range
       
  1439         			for (TInt i = EDriveY; i>=EDriveE; i--)
       
  1440         				{
       
  1441         				if ( DriveOK((TDriveNumber)(i)))
       
  1442         					{
       
  1443         					if (IsDriveMMC((TDriveNumber)(i)))
       
  1444         						{
       
  1445         						PathInfo::GetRootPath(iSaveFileName, (TDriveNumber)(i));
       
  1446         						break;
       
  1447         						}    
       
  1448         					}
       
  1449         				}
       
  1450         			}
       
  1451             	}
       
  1452 
       
  1453         
       
  1454         iSaveFileName.Append( PathInfo::ImagesPath() );     // animated gif is actually an image, not a video
       
  1455         iSaveFileName.Append( KScreenShotsSubDirectory );
       
  1456         
       
  1457         // a quick check that filename is valid
       
  1458         if (iGrabSettings.iVideoCaptureFileName.Length() > 0 && iGrabSettings.iVideoCaptureFileName.Length() <= 255) 
       
  1459             iSaveFileName.Append( iGrabSettings.iVideoCaptureFileName );
       
  1460         else
       
  1461             iSaveFileName.Append( KDefaultVideoFileName );
       
  1462 
       
  1463         iSaveFileName.Append( _L(".gif") );
       
  1464 
       
  1465         CApaApplication::GenerateFileName(iFileSession, iSaveFileName );  // unique filename
       
  1466 
       
  1467         // create and save the gif animation
       
  1468         err = CGifAnimator::CreateGifAnimation(iSaveFileName, iVideoDimensions, iVideoFrameArray, *iEngineWrapper);
       
  1469         
       
  1470         // remove the saved file in case of errors since it's likely corrupted
       
  1471         if (err != KErrNone)
       
  1472             iFileSession.Delete(iSaveFileName);
       
  1473         
       
  1474         CapturingFinishedL(err);   
       
  1475         }
       
  1476         
       
  1477     else
       
  1478         CapturingFinishedL(KErrNotSupported);
       
  1479     
       
  1480     SC_DEBUG(_L("SCREENGRABBER ------------------------------------------------------- SaveVideo end"));
       
  1481     }
       
  1482 // ---------------------------------------------------------------------------
       
  1483