javauis/mmapi_akn/baseline/src/cmmacamerawindow.cpp
branchRCL_3
changeset 18 9ac0a0a7da70
parent 17 0fd27995241b
child 19 71c436fe3ce0
equal deleted inserted replaced
17:0fd27995241b 18:9ac0a0a7da70
     1 /*
       
     2 * Copyright (c) 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:  This class implements MMMADisplayWindow functionality
       
    15 *                for Camera viewfinder usage.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 //  Include Files
       
    21 #include <eikenv.h>
       
    22 #include <gdi.h>
       
    23 #include <jdebug.h>
       
    24 #include <AknsUtils.h>
       
    25 #include <AknsDrawUtils.h>
       
    26 
       
    27 #include <cameraapp.mbg>
       
    28 
       
    29 // Used for iDisplay member
       
    30 
       
    31 #include "cmmacamerawindow.h"
       
    32 
       
    33 // Camera viewfinder start delay
       
    34 const TInt KStarterTimeout = 100000; //100 msecs
       
    35 
       
    36 // Margin of icon displayed instead of viewfinder in case of error
       
    37 const TInt KErrorIconMargin = 5; //5 pixels
       
    38 
       
    39 
       
    40 _LIT(KCameraAppBitmapFile, "z:\\resource\\apps\\cameraapp.mif");
       
    41 
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CMMACameraWindow::NewL
       
    45 // Two-phased constructor.
       
    46 // Use this method to create a CMMACameraWindow instance.
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CMMACameraWindow* CMMACameraWindow::NewL(TInt aCameraHandle)
       
    50 {
       
    51     CMMACameraWindow* self = new(ELeave)CMMACameraWindow(aCameraHandle);
       
    52     return self;
       
    53 }
       
    54 
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // CMMACameraWindow::~CMMACameraWindow
       
    58 // Destructor.
       
    59 // NOTE: iUICamera have to be deleted by ReleaseUiResources() called
       
    60 //       from UI thread
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CMMACameraWindow::~CMMACameraWindow()
       
    64 {
       
    65     // It's not allowed to delete any nonsharable object here.
       
    66     // This must be done in Destroy().
       
    67     // If the iDisplay is set,
       
    68     // instance should be deleted by sending
       
    69     // event from UI to be received by MUiEventConsumer.
       
    70 }
       
    71 
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // CMMACameraWindow::ViewFinderActive
       
    75 // Gets an information if the viewfinder is started.
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 TBool CMMACameraWindow::ViewFinderActive()
       
    79 {
       
    80     return iUICamera && iViewFinderVisible;
       
    81 }
       
    82 
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // CMMACameraWindow::SetStarted
       
    86 // Notifies window about Started state change.
       
    87 // If started, tries to start viewfinder.
       
    88 // If not started, stops viewfinder.
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CMMACameraWindow::SetStarted(TBool aStarted)
       
    92 {
       
    93     iStarted = aStarted;
       
    94     if (iDisplay)
       
    95     {
       
    96         if (iStarted)
       
    97         {
       
    98             iDisplay->UIGetCallback(
       
    99                 *this, CMMACameraWindow::EShowViewFinder);
       
   100         }
       
   101         else
       
   102         {
       
   103             iDisplay->UIGetCallback(
       
   104                 *this, CMMACameraWindow::EHideViewFinder);
       
   105         }
       
   106     }
       
   107 }
       
   108 
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // CMMACameraWindow::SetDisplay
       
   112 // Sets iDisplay used to invoke callbacks from UI thread.
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void CMMACameraWindow::SetDisplay(MMMADisplay *aDisplay)
       
   116 {
       
   117     if (iDisplay != aDisplay)
       
   118     {
       
   119         if (iDisplay)
       
   120         {
       
   121             // Clear the resources created within the old Display
       
   122             iDisplay->UIGetCallback(
       
   123                 *this, CMMACameraWindow::EDeleteViewFinder);
       
   124         }
       
   125 
       
   126         // Set the new Display
       
   127         iDisplay = aDisplay;
       
   128 
       
   129         if (iDisplay)
       
   130         {
       
   131             // Get a DSA resources for the new Display
       
   132             iDisplay->UIGetDSAResources(*this, MMMADisplay::EMmaThread);
       
   133         }
       
   134     }
       
   135 }
       
   136 
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // From class MMMADisplayWindow
       
   140 // CMMACameraWindow::SetDestinationBitmapL
       
   141 //
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CMMACameraWindow::SetDestinationBitmapL(CFbsBitmap* /*aBitmap*/)
       
   145 {
       
   146     // Ignored, this window will not be used for actual drawing
       
   147     DEBUG("CMMACameraWindow::SetDestinationBitmapL");
       
   148 }
       
   149 
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // From class MMMADisplayWindow
       
   153 // CMMACameraWindow::DrawFrameL
       
   154 //
       
   155 // ---------------------------------------------------------------------------
       
   156 //
       
   157 void CMMACameraWindow::DrawFrameL(const CFbsBitmap* /*aBitmap*/)
       
   158 {
       
   159     // Ignored, this window will not be used for actual drawing
       
   160 }
       
   161 
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 // From class MMMADisplayWindow
       
   165 // CMMACameraWindow::SetDrawRect
       
   166 //
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CMMACameraWindow::SetDrawRect(const TRect& aRect)
       
   170 {
       
   171     // MMAPI thread
       
   172     DEBUG_INT2("MMA::CMMACameraWindow::SetDrawRect TL:%d,%d",
       
   173                aRect.iTl.iX, aRect.iTl.iY);
       
   174     DEBUG_INT2("MMA::CMMACameraWindow::SetDrawRect BR:%d,%d",
       
   175                aRect.iBr.iX, aRect.iBr.iY);
       
   176 
       
   177     iDrawRect = aRect;
       
   178 
       
   179     if (iDisplay)
       
   180     {
       
   181         iDisplay->UIGetCallback(*this, CMMACameraWindow::EResetViewFinder);
       
   182     }
       
   183 }
       
   184 
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // From class MMMADisplayWindow
       
   188 // CMMACameraWindow::SetDrawRectThread
       
   189 //
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 void CMMACameraWindow::SetDrawRectThread(const TRect& aRect)
       
   193 {
       
   194     DEBUG_INT2("MMA::CMMACameraWindow::SetDrawRectThread TL:%d,%d",
       
   195                aRect.iTl.iX, aRect.iTl.iY);
       
   196     DEBUG_INT2("MMA::CMMACameraWindow::SetDrawRectThread BR:%d,%d",
       
   197                aRect.iBr.iX, aRect.iBr.iY);
       
   198 
       
   199     // Note: Runs in UI thread only
       
   200 
       
   201     // Bitmap window's rect can be set in any thread.
       
   202     if (iDrawRect != aRect)
       
   203     {
       
   204         iDrawRect = aRect;
       
   205 
       
   206         // Stop and start ViewFinder to update position
       
   207         ResetViewFinder();
       
   208     }
       
   209 }
       
   210 
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // From class MMMADisplayWindow
       
   214 // CMMACameraWindow::DrawRect
       
   215 //
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 const TRect& CMMACameraWindow::DrawRect()
       
   219 {
       
   220     return iDrawRect;
       
   221 }
       
   222 
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // From class MMMADisplayWindow
       
   226 // CMMACameraWindow::WindowSize
       
   227 //
       
   228 // ---------------------------------------------------------------------------
       
   229 //
       
   230 TSize CMMACameraWindow::WindowSize()
       
   231 {
       
   232     return iDrawRect.Size();
       
   233 }
       
   234 
       
   235 
       
   236 // ---------------------------------------------------------------------------
       
   237 // From class MMMADisplayWindow
       
   238 // CMMACameraWindow::SetPosition
       
   239 //
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 void CMMACameraWindow::SetPosition(const TPoint& aPosition)
       
   243 {
       
   244     // Note: Runs in UI thread only
       
   245     SetDrawRectThread(TRect(aPosition, iDrawRect.Size()));
       
   246 }
       
   247 
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // From class MMMADisplayWindow
       
   251 // CMMACameraWindow::SetVisible
       
   252 //
       
   253 // ---------------------------------------------------------------------------
       
   254 //
       
   255 void CMMACameraWindow::SetVisible(TBool aVisible, TBool aUseEventServer)
       
   256 {
       
   257     iVisible = aVisible;
       
   258 
       
   259     if (aUseEventServer)
       
   260     {
       
   261         // We are in UI thread now.
       
   262         SetViewFinderVisibility(aVisible);
       
   263     }
       
   264     else
       
   265     {
       
   266         // MMAPI thread
       
   267         if (iDisplay)
       
   268         {
       
   269             if (aVisible)
       
   270             {
       
   271                 iDisplay->UIGetCallback(
       
   272                     *this, CMMACameraWindow::EShowViewFinder);
       
   273             }
       
   274             else
       
   275             {
       
   276                 iDisplay->UIGetCallback(
       
   277                     *this, CMMACameraWindow::EHideViewFinder);
       
   278             }
       
   279         }
       
   280     }
       
   281 }
       
   282 
       
   283 
       
   284 // ---------------------------------------------------------------------------
       
   285 // From class MMMADisplayWindow
       
   286 // CMMACameraWindow::SetWindowRect
       
   287 //
       
   288 // ---------------------------------------------------------------------------
       
   289 //
       
   290 void CMMACameraWindow::SetWindowRect(
       
   291     const TRect& aRect,
       
   292     MMMADisplay::TThreadType aThreadType)
       
   293 {
       
   294     DEBUG_INT2("MMA::CMMACameraWindow::SetWindowRect TL:%d,%d",
       
   295                aRect.iTl.iX, aRect.iTl.iY);
       
   296     DEBUG_INT2("MMA::CMMACameraWindow::SetWindowRect BR:%d,%d",
       
   297                aRect.iBr.iX, aRect.iBr.iY);
       
   298 
       
   299     if (iClientRect != aRect)
       
   300     {
       
   301         iClientRect = aRect;
       
   302 
       
   303         if (aThreadType == MMMADisplay::EUiThread)
       
   304         {
       
   305             ResetViewFinder();
       
   306         }
       
   307         else
       
   308         {
       
   309             iDisplay->UIGetCallback(
       
   310                 *this, CMMACameraWindow::EResetViewFinder);
       
   311         }
       
   312     }
       
   313 }
       
   314 
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // From class MMMADisplayWindow
       
   318 // CMMACameraWindow::WindowRect
       
   319 //
       
   320 // ---------------------------------------------------------------------------
       
   321 //
       
   322 const TRect& CMMACameraWindow::WindowRect()
       
   323 {
       
   324     return iClientRect;
       
   325 }
       
   326 
       
   327 
       
   328 // ---------------------------------------------------------------------------
       
   329 // From class MMMADisplayWindow
       
   330 // CMMACameraWindow::GetDisplayWindowType
       
   331 //
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 MMMADisplayWindow::TDisplayWindowType
       
   335 CMMACameraWindow::GetDisplayWindowType() const
       
   336 {
       
   337     return EDisplayWindowTypeIsCamera;
       
   338 }
       
   339 
       
   340 
       
   341 // ---------------------------------------------------------------------------
       
   342 // From class MMMADisplayWindow
       
   343 // CMMACameraWindow::IsVisible
       
   344 //
       
   345 // ---------------------------------------------------------------------------
       
   346 //
       
   347 TBool CMMACameraWindow::IsVisible() const
       
   348 {
       
   349     return iVisible;
       
   350 }
       
   351 
       
   352 
       
   353 // ---------------------------------------------------------------------------
       
   354 // From class MMMADisplayWindow
       
   355 // CMMACameraWindow::ContainerSet
       
   356 //
       
   357 // ---------------------------------------------------------------------------
       
   358 //
       
   359 void CMMACameraWindow::ContainerSet()
       
   360 {
       
   361     // We are in UI thread now
       
   362 
       
   363     // Container was probably not set when
       
   364     // iDisplay was set,
       
   365     // we can now try get the DSA stuff again
       
   366     if (!iUICamera && iDisplay)
       
   367     {
       
   368         // Get a DSA stuff for the new Display
       
   369         iDisplay->UIGetDSAResources(*this, MMMADisplay::EUiThread);
       
   370     }
       
   371 }
       
   372 
       
   373 
       
   374 // ---------------------------------------------------------------------------
       
   375 // From class MMMADisplayWindow
       
   376 // CMMACameraWindow::ContainerDestroyed
       
   377 //
       
   378 // ---------------------------------------------------------------------------
       
   379 //
       
   380 void CMMACameraWindow::ContainerDestroyed()
       
   381 {
       
   382     // We are in UI thread now
       
   383     ReleaseUiResources();
       
   384 }
       
   385 
       
   386 
       
   387 // ---------------------------------------------------------------------------
       
   388 // From class MCameraObserver
       
   389 // CMMACameraWindow::ReserveComplete
       
   390 //
       
   391 // ---------------------------------------------------------------------------
       
   392 //
       
   393 void CMMACameraWindow::ReserveComplete(TInt aError)
       
   394 {
       
   395     DEBUG_INT("MMA::CMMACameraWindow::ReserveComplete %d", aError);
       
   396 
       
   397     if (aError == KErrNone)
       
   398     {
       
   399         // camera will notify completion with PowerOnComplete method.
       
   400         iUICamera->PowerOn();
       
   401     }
       
   402 }
       
   403 
       
   404 
       
   405 // ---------------------------------------------------------------------------
       
   406 // From class MCameraObserver
       
   407 // CMMACameraWindow::PowerOnComplete
       
   408 //
       
   409 // ---------------------------------------------------------------------------
       
   410 //
       
   411 void CMMACameraWindow::PowerOnComplete(TInt aError)
       
   412 {
       
   413     DEBUG_INT("MMA::CMMACameraWindow::PowerOnComplete %d", aError);
       
   414 
       
   415     if (aError == KErrNone)
       
   416     {
       
   417         iCameraPowerOn = ETrue;
       
   418         SetViewFinderVisibility(ETrue);
       
   419     }
       
   420 }
       
   421 
       
   422 
       
   423 // ---------------------------------------------------------------------------
       
   424 // From class MCameraObserver
       
   425 // CMMACameraWindow::ViewFinderFrameReady
       
   426 //
       
   427 // ---------------------------------------------------------------------------
       
   428 //
       
   429 void CMMACameraWindow::ViewFinderFrameReady(CFbsBitmap& /*aFrame*/)
       
   430 {
       
   431     // Empty implementation of the interface method
       
   432     DEBUG("MMA::CMMACameraWindow::ViewFinderFrameReady");
       
   433 }
       
   434 
       
   435 
       
   436 // ---------------------------------------------------------------------------
       
   437 // From class MCameraObserver
       
   438 // CMMACameraWindow::ImageReady
       
   439 //
       
   440 // ---------------------------------------------------------------------------
       
   441 //
       
   442 void CMMACameraWindow::ImageReady(CFbsBitmap* /*aBitmap*/,
       
   443                                   HBufC8* /*aData*/,
       
   444                                   TInt /*aError*/)
       
   445 {
       
   446     // Empty implementation of the interface method
       
   447     DEBUG("MMA::CMMACameraWindow::ImageReady");
       
   448 }
       
   449 
       
   450 
       
   451 // ---------------------------------------------------------------------------
       
   452 // From class MCameraObserver
       
   453 // CMMACameraWindow::FrameBufferReady
       
   454 //
       
   455 // ---------------------------------------------------------------------------
       
   456 //
       
   457 void CMMACameraWindow::FrameBufferReady(MFrameBuffer* /*aFrameBuffer*/,
       
   458                                         TInt /*aError*/)
       
   459 {
       
   460     // Empty implementation of the interface method
       
   461     DEBUG("MMA::CMMACameraWindow::FrameBufferReady");
       
   462 }
       
   463 
       
   464 
       
   465 // ---------------------------------------------------------------------------
       
   466 // From class MDirectScreenAccess.
       
   467 // CMMACameraWindow::AbortNow
       
   468 //
       
   469 // ---------------------------------------------------------------------------
       
   470 //
       
   471 void CMMACameraWindow::AbortNow(
       
   472     RDirectScreenAccess::TTerminationReasons /*aReasons*/)
       
   473 {
       
   474     SetViewFinderVisibility(EFalse);
       
   475 }
       
   476 
       
   477 
       
   478 // ---------------------------------------------------------------------------
       
   479 // From class MDirectScreenAccess.
       
   480 // CMMACameraWindow::Restart
       
   481 //
       
   482 // ---------------------------------------------------------------------------
       
   483 //
       
   484 void CMMACameraWindow::Restart(
       
   485     RDirectScreenAccess::TTerminationReasons /*aReasons*/)
       
   486 {
       
   487     SetViewFinderVisibility(ETrue);
       
   488 }
       
   489 
       
   490 
       
   491 // ---------------------------------------------------------------------------
       
   492 // CMMACameraWindow::CMMACameraWindow
       
   493 // C++ constructor.
       
   494 // ---------------------------------------------------------------------------
       
   495 //
       
   496 CMMACameraWindow::CMMACameraWindow(TInt aCameraHandle):
       
   497         iVisible(EFalse),
       
   498         iStarted(EFalse),
       
   499         iViewFinderVisible(EFalse),
       
   500         iCameraPowerOn(EFalse),
       
   501         iDrawRect(0, 0, 0, 0),
       
   502         iClientRect(0, 0, 0, 0),
       
   503         iCameraHandle(aCameraHandle),
       
   504         iUICamera(NULL),
       
   505         iDisplay(NULL),
       
   506         iStarterTimer(NULL),
       
   507         iDirectAccess(NULL),
       
   508         iErrorIconBitmap(NULL),
       
   509         iErrorIconMaskBitmap(NULL),
       
   510         iRWindowRect(0, 0, 0, 0)
       
   511 {
       
   512 }
       
   513 
       
   514 
       
   515 // ---------------------------------------------------------------------------
       
   516 // CMMACameraWindow::UIStartViewFinder
       
   517 // Creates UI Camera.
       
   518 // This CCamera instance is duplicated from handle given to the constructor.
       
   519 // Have to be called from the thread which UI Camera will be controlled from.
       
   520 // ---------------------------------------------------------------------------
       
   521 //
       
   522 void CMMACameraWindow::UIStartViewFinder(
       
   523     RWsSession &aWs,
       
   524     CWsScreenDevice &aScreenDevice,
       
   525     RWindowBase &aWindow)
       
   526 {
       
   527     DEBUG("MMA::CMMACameraWindow::UIStartViewFinder");
       
   528 
       
   529     iWs = &aWs;
       
   530     iScreenDevice = &aScreenDevice;
       
   531     iWindow = &aWindow;
       
   532 
       
   533     if (!iUICamera)
       
   534     {
       
   535         TRAPD(error, iUICamera =
       
   536                   CCamera::NewDuplicateL(*this, iCameraHandle));
       
   537 
       
   538         DEBUG_INT(
       
   539             "MMA::CMMACameraWindow::UIStartViewFinder - NewDuplicateL %d",
       
   540             error);
       
   541 
       
   542         if (error == KErrNone)
       
   543         {
       
   544             iUICamera->Reserve();
       
   545         }
       
   546     }
       
   547 }
       
   548 
       
   549 
       
   550 // ---------------------------------------------------------------------------
       
   551 // CMMACameraWindow::SetViewFinderVisibility
       
   552 // If UI Camera have been created, it starts/hides the DirectViewFinder.
       
   553 // Have to be called from UI thread in which iUICamera instance was created.
       
   554 // ---------------------------------------------------------------------------
       
   555 //
       
   556 void CMMACameraWindow::SetViewFinderVisibility(TBool aVisible)
       
   557 {
       
   558     DEBUG_INT("MMA::CMMACameraWindow::SetViewFinderVisibility - %d",
       
   559               aVisible);
       
   560 
       
   561     if (!iUICamera || !iCameraPowerOn || iViewFinderVisible == aVisible)
       
   562     {
       
   563         return;
       
   564     }
       
   565 
       
   566     if (aVisible)
       
   567     {
       
   568         // Viewfinder is going to start
       
   569         if (!iVisible || !iStarted)
       
   570         {
       
   571             return;
       
   572         }
       
   573 
       
   574         iViewFinderVisible = ETrue;
       
   575 
       
   576         if (!iStarterTimer)
       
   577         {
       
   578             // If starter timer haven't been created yet, create an instance
       
   579             TRAPD(
       
   580                 timerErr,
       
   581                 iStarterTimer = CPeriodic::NewL(CActive::EPriorityIdle));
       
   582 
       
   583             if (timerErr != KErrNone)
       
   584             {
       
   585                 DEBUG_INT(
       
   586                     "MMA::CMMACameraWindow::SetViewFinderVisibility - timer error = %d",
       
   587                     timerErr);
       
   588                 __ASSERT_DEBUG(EFalse, User::Invariant());
       
   589             }
       
   590         }
       
   591 
       
   592         if (!iStarterTimer->IsActive())
       
   593         {
       
   594             iStarterTimer->Start(
       
   595                 KStarterTimeout,
       
   596                 0,
       
   597                 TCallBack(StarterTimerCallback, this));
       
   598         }
       
   599     }
       
   600     else
       
   601     {
       
   602         // Viewfinder is going to be cancelled
       
   603         DEBUG(
       
   604             "MMA::CMMACameraWindow::SetViewFinderVisibility - Stopping VF");
       
   605 
       
   606         if (iStarterTimer && iStarterTimer->IsActive())
       
   607         {
       
   608             // Cancel the running starter timer
       
   609             iStarterTimer->Cancel();
       
   610         }
       
   611 
       
   612         if (iUICamera->ViewFinderActive())
       
   613         {
       
   614             iUICamera->StopViewFinder();
       
   615         }
       
   616 
       
   617         iViewFinderVisible = EFalse;
       
   618     }
       
   619 }
       
   620 
       
   621 
       
   622 // ---------------------------------------------------------------------------
       
   623 // CMMACameraWindow::StarterTimerCallback
       
   624 // Executed at the expiry of the iStartedTimer.
       
   625 // ---------------------------------------------------------------------------
       
   626 //
       
   627 TInt CMMACameraWindow::StarterTimerCallback(TAny* aThis)
       
   628 {
       
   629     CMMACameraWindow* self = static_cast<CMMACameraWindow*>(aThis);
       
   630     ASSERT(self);
       
   631 
       
   632     self->StartViewFinder();
       
   633 
       
   634     // We don't want to run the callback again
       
   635     return EFalse;
       
   636 }
       
   637 
       
   638 
       
   639 // ---------------------------------------------------------------------------
       
   640 // From class MMMADisplayWindow
       
   641 // CMMACameraWindow::SetRWindowRect
       
   642 //
       
   643 // ---------------------------------------------------------------------------
       
   644 //
       
   645 void CMMACameraWindow::SetRWindowRect(const TRect& aRect,
       
   646                                       MMMADisplay::TThreadType aThreadType)
       
   647 {
       
   648     DEBUG_INT2("MID::CMMACameraWindow::SetRWindowRect TL %d %d",
       
   649                aRect.iTl.iX, aRect.iTl.iY);
       
   650     DEBUG_INT2("MID::CMMACameraWindow::SetRWindowRect BR %d %d",
       
   651                aRect.iBr.iX, aRect.iBr.iY);
       
   652 
       
   653     iRWindowRect = aRect;
       
   654 
       
   655     if (MMMADisplay::EMmaThread == aThreadType)
       
   656     {
       
   657         if (iDisplay)
       
   658         {
       
   659             iDisplay->UIGetCallback(*this,
       
   660                                     CMMACameraWindow::EResetViewFinder);
       
   661         }
       
   662     }
       
   663     else if (MMMADisplay::EUiThread == aThreadType)
       
   664     {
       
   665         ResetViewFinder();
       
   666     }
       
   667 }
       
   668 
       
   669 
       
   670 // ---------------------------------------------------------------------------
       
   671 // CMMACameraWindow::StartViewFinder
       
   672 // Starts viewfinder.
       
   673 // ---------------------------------------------------------------------------
       
   674 //
       
   675 void CMMACameraWindow::StartViewFinder()
       
   676 {
       
   677     DEBUG(" < StartViewFinder");
       
   678 
       
   679     ASSERT(iUICamera);
       
   680     ASSERT(iCameraPowerOn);
       
   681     ASSERT(iStarted);
       
   682 
       
   683     iStarterTimer->Cancel();
       
   684 
       
   685     // align client rect to RWindow, viewfinder
       
   686     // display co-ordinates is w.r.t to RWindow
       
   687     TRect relativeClientRect;
       
   688     relativeClientRect = iClientRect;
       
   689     relativeClientRect.Move(-iRWindowRect.iTl);
       
   690 
       
   691     // Set the drawing area
       
   692     TRect drawRect(iDrawRect);
       
   693     drawRect.Move(relativeClientRect.iTl);
       
   694 
       
   695     DEBUG_INT2(
       
   696         "MMA::CMMACameraWindow::StartViewFinder - Starting VF TL:%d,%d",
       
   697         drawRect.iTl.iX,
       
   698         drawRect.iTl.iY);
       
   699     DEBUG_INT2(
       
   700         "MMA::CMMACameraWindow::StartViewFinder - Starting VF BR:%d,%d",
       
   701         drawRect.iBr.iX,
       
   702         drawRect.iBr.iY);
       
   703 
       
   704     TRAPD(vfError, iUICamera->StartViewFinderDirectL(
       
   705               *iWs, *iScreenDevice, *iWindow, drawRect));
       
   706 
       
   707     if (vfError == KErrNone)
       
   708     {
       
   709         DEBUG("MMA::CMMACameraWindow::StartViewFinder - Start OK");
       
   710     }
       
   711     else
       
   712     {
       
   713         DEBUG_INT("MMA::CMMACameraWindow::StartViewFinder()  - \
       
   714             StartViewFinderDirectL error=%d", vfError);
       
   715 
       
   716         TRAPD(error, DrawViewFinderErrorL(vfError, drawRect));
       
   717         DEBUG_INT("MMA::CMMACameraWindow::StartViewFinder, \
       
   718             DrawViewFinderErrorL error = %d", error);
       
   719     }
       
   720 
       
   721     DEBUG(" > StartViewFinder");
       
   722 }
       
   723 
       
   724 
       
   725 // ---------------------------------------------------------------------------
       
   726 // CMMACameraWindow::DrawViewFinderError()
       
   727 // Draws the error message to specified area.
       
   728 // Used in cases when viewfinder is unable to start.
       
   729 // ---------------------------------------------------------------------------
       
   730 //
       
   731 void CMMACameraWindow::DrawViewFinderErrorL(
       
   732     const TInt /*aError*/,
       
   733     const TRect& aDrawRect)
       
   734 {
       
   735     if (!iDirectAccess)
       
   736     {
       
   737         if (!iWs || !iScreenDevice || !iWindow) return;
       
   738 
       
   739         DEBUG("MMA::CMMACameraWindow::DrawViewFinderErrorL - \
       
   740             Instantiating CDirectScreenAccess");
       
   741         iDirectAccess = CDirectScreenAccess::NewL(*iWs,
       
   742                                      *iScreenDevice,
       
   743                                      *iWindow,
       
   744                                      *this);
       
   745     }
       
   746 
       
   747     TInt dcError = KErrNone;
       
   748     if (!iDirectAccess->IsActive())
       
   749     {
       
   750         TRAP(dcError, iDirectAccess->StartL());
       
   751     }
       
   752 
       
   753     TRect drawRect(aDrawRect);
       
   754 
       
   755     if (dcError == KErrNone)
       
   756     {
       
   757         drawRect.Intersection(iClientRect);
       
   758         drawRect.Move(-iWindow->AbsPosition());
       
   759 
       
   760         CFbsBitGc* directGc = iDirectAccess->Gc();
       
   761         directGc->SetClippingRect(drawRect);
       
   762         directGc->SetBrushColor(TRgb(128,128,128));
       
   763         directGc->SetPenColor(TRgb(128,0,0));
       
   764         directGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   765         directGc->SetPenStyle(CGraphicsContext::ESolidPen);
       
   766         directGc->SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
       
   767         directGc->DrawRect(drawRect);
       
   768 
       
   769         if (!iErrorIconBitmap || !iErrorIconMaskBitmap)
       
   770         {
       
   771             if (iErrorIconBitmap)
       
   772             {
       
   773                 delete iErrorIconBitmap;
       
   774                 iErrorIconBitmap = NULL;
       
   775             }
       
   776 
       
   777             if (iErrorIconMaskBitmap)
       
   778             {
       
   779                 delete iErrorIconMaskBitmap;
       
   780                 iErrorIconMaskBitmap = NULL;
       
   781             }
       
   782 
       
   783             AknsUtils::CreateIconL(
       
   784                 AknsUtils::SkinInstance(),
       
   785                 KAknsIIDQgnIndiCam4Camera,
       
   786                 iErrorIconBitmap,
       
   787                 iErrorIconMaskBitmap,
       
   788                 KCameraAppBitmapFile,
       
   789                 EMbmCameraappQgn_indi_cam4_camera,
       
   790                 EMbmCameraappQgn_indi_cam4_camera_mask
       
   791             );
       
   792         }
       
   793 
       
   794         //TRect iconRect
       
   795         drawRect.iTl.iX += KErrorIconMargin;
       
   796         drawRect.iTl.iY += KErrorIconMargin;
       
   797         drawRect.iBr.iX -= KErrorIconMargin;
       
   798         drawRect.iBr.iY -= KErrorIconMargin;
       
   799 
       
   800         if (iErrorIconBitmap->SizeInPixels() != drawRect.Size())
       
   801         {
       
   802             AknIconUtils::SetSize(iErrorIconBitmap, drawRect.Size());
       
   803             AknIconUtils::SetSize(iErrorIconMaskBitmap, drawRect.Size());
       
   804         }
       
   805 
       
   806         directGc->BitBltMasked(
       
   807             drawRect.iTl, iErrorIconBitmap,
       
   808             TRect(iErrorIconBitmap->SizeInPixels()),
       
   809             iErrorIconMaskBitmap, EFalse);
       
   810 
       
   811         iDirectAccess->ScreenDevice()->Update();
       
   812     }
       
   813 
       
   814 }
       
   815 
       
   816 
       
   817 // ---------------------------------------------------------------------------
       
   818 // CMMACameraWindow::ReleaseUiResources()
       
   819 // Stops the DirectViewFinder and deletes UI Camera instance
       
   820 // Have to be called from UI thread in which UI Camera
       
   821 // have been created before the CMMACameraWindow instance
       
   822 // is deleted.
       
   823 // ---------------------------------------------------------------------------
       
   824 //
       
   825 void CMMACameraWindow::ReleaseUiResources()
       
   826 {
       
   827     if (iUICamera)
       
   828     {
       
   829         SetViewFinderVisibility(EFalse);
       
   830         // iStarterTimer is cancelled by previous call
       
   831         // it can be deleted now
       
   832         delete iStarterTimer;
       
   833         iStarterTimer = NULL;
       
   834         iUICamera->Release();
       
   835         delete iUICamera;
       
   836         iUICamera = NULL;
       
   837         iCameraPowerOn = EFalse;
       
   838         if (iDirectAccess)
       
   839         {
       
   840             iDirectAccess->Cancel();
       
   841             delete iDirectAccess;
       
   842             iDirectAccess = NULL;
       
   843         }
       
   844         delete iErrorIconBitmap;
       
   845         iErrorIconBitmap = NULL;
       
   846         delete iErrorIconMaskBitmap;
       
   847         iErrorIconMaskBitmap = NULL;
       
   848     }
       
   849 }
       
   850 
       
   851 
       
   852 // ---------------------------------------------------------------------------
       
   853 // CMMACameraWindow::ResetViewFinder
       
   854 // Resets (stops and starts) viewfinder
       
   855 // ---------------------------------------------------------------------------
       
   856 //
       
   857 void CMMACameraWindow::ResetViewFinder()
       
   858 {
       
   859 
       
   860     if (iStarterTimer && !iStarterTimer->IsActive() && iStarted)
       
   861     {
       
   862         SetViewFinderVisibility(EFalse);
       
   863         SetViewFinderVisibility(ETrue);
       
   864     }
       
   865 }
       
   866 
       
   867 
       
   868 // ---------------------------------------------------------------------------
       
   869 // CMMACameraWindow::Destroy
       
   870 // Deletes this object.
       
   871 // ---------------------------------------------------------------------------
       
   872 //
       
   873 void CMMACameraWindow::Destroy()
       
   874 {
       
   875     // Delete itself
       
   876     delete this;
       
   877 }
       
   878 
       
   879 
       
   880 // ---------------------------------------------------------------------------
       
   881 // From class MUiEventConsumer.
       
   882 // CMMACameraWindow::MdcDSAResourcesCallback
       
   883 // Callback to iDisplay->UIGetDSAResources().
       
   884 // Invoked asynchronously from UI Event Server thread.
       
   885 // ---------------------------------------------------------------------------
       
   886 //
       
   887 void CMMACameraWindow::MdcDSAResourcesCallback(
       
   888     RWsSession &aWs,
       
   889     CWsScreenDevice &aScreenDevice,
       
   890     RWindowBase &aWindow)
       
   891 {
       
   892     this->UIStartViewFinder(aWs, aScreenDevice, aWindow);
       
   893 }
       
   894 
       
   895 
       
   896 // ---------------------------------------------------------------------------
       
   897 // From class MUiEventConsumer.
       
   898 // CMMACameraWindow::MdcUICallback
       
   899 // Callback to iDisplay->UIGetCallback().
       
   900 // Invoked asynchronously from UI Event Server thread.
       
   901 // ---------------------------------------------------------------------------
       
   902 //
       
   903 void CMMACameraWindow::MdcUICallback(TInt aCallbackId)
       
   904 {
       
   905     DEBUG("MMA::CMMACameraWindow::MdcUICallback");
       
   906 
       
   907     switch (aCallbackId)
       
   908     {
       
   909     case EDeleteViewFinder:
       
   910         ReleaseUiResources();
       
   911         break;
       
   912     case EHideViewFinder:
       
   913         SetViewFinderVisibility(EFalse);
       
   914         break;
       
   915     case EShowViewFinder:
       
   916         SetViewFinderVisibility(ETrue);
       
   917         break;
       
   918     case EResetViewFinder:
       
   919         ResetViewFinder();
       
   920         break;
       
   921     case EDestroyWindow:
       
   922         Destroy();
       
   923         break;
       
   924     }
       
   925 }
       
   926 
       
   927 
       
   928 //  END OF FILE