tactilefeedback/tactilefeedbackresolver/src/tactilefeedbackresolver.cpp
changeset 0 d54f32e146dd
child 12 63c33341dc19
child 22 4838b44af342
equal deleted inserted replaced
-1:000000000000 0:d54f32e146dd
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Class for resolving physical feedback type based on 
       
    15 *                tactile feedback settings and current device state.
       
    16 * Part of:      Tactile Feedback.
       
    17 *
       
    18 */
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <e32std.h>
       
    22 #include <e32debug.h>
       
    23 
       
    24 #include <centralrepository.h>
       
    25 #include <ecom/implementationinformation.h>
       
    26 
       
    27 #include "tactilefeedbackprivatecrkeys.h"
       
    28 #include "tactilefeedbackinternalpskeys.h"
       
    29 #include "tactilefeedbacktrace.h"
       
    30 
       
    31 #include "tactilefeedbackresolver.h"
       
    32 #include "tactilepropertywatcher.h"
       
    33 #include "tactileplayer.h"
       
    34 #include "OstTraceDefinitions.h"
       
    35 #ifdef OST_TRACE_COMPILER_IN_USE
       
    36 #include "tactilefeedbackresolverTraces.h"
       
    37 #endif
       
    38 
       
    39 
       
    40 // Security policies for reading and writing to our P&S property
       
    41 _LIT_SECURITY_POLICY_PASS( KTactileReadPolicy );
       
    42 _LIT_SECURITY_POLICY_C1(   KTactileWritePolicy, ECapabilityWriteDeviceData );
       
    43 // ======== MEMBER FUNCTIONS ========
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // Consructor.
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CTactileFeedbackResolver::CTactileFeedbackResolver() : 
       
    50     iFeedbackStarted( EFalse )
       
    51     {
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // 2nd phase costructor.
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 void CTactileFeedbackResolver::ConstructL()
       
    59     {
       
    60     TRACE("CTactileFeedbackResolver::ConstructL - Begin");
       
    61 
       
    62     InitializeCrKeysL();
       
    63 
       
    64     TInt hapticsUid(0);  
       
    65     TInt audioUid(0);    
       
    66     User::LeaveIfError( iRepository->Get( KTactileHapticsPlugin, hapticsUid ) );
       
    67     User::LeaveIfError( iRepository->Get( KTactileAudioPlugin, audioUid ) );
       
    68 
       
    69     if ( hapticsUid )
       
    70         {
       
    71         CreateHapticsPlayerL( TUid::Uid( hapticsUid ) );
       
    72         }
       
    73 
       
    74     if ( audioUid )
       
    75         {
       
    76         CreateAudioPlayerL( TUid::Uid( audioUid ) );
       
    77         }    
       
    78 
       
    79     InitializePsKeysL();
       
    80     
       
    81     TRACE("CTactileFeedbackResolver::ConstructL - End");
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // 2-phased constructor.
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CTactileFeedbackResolver* CTactileFeedbackResolver::NewL()
       
    89     {
       
    90     CTactileFeedbackResolver* self = new ( ELeave ) CTactileFeedbackResolver;
       
    91     CleanupStack::PushL( self );
       
    92     self->ConstructL();
       
    93     CleanupStack::Pop( self );
       
    94     return self;
       
    95     }
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // Destructor.
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 CTactileFeedbackResolver::~CTactileFeedbackResolver()
       
   102     {
       
   103     delete iCenRepNotifier;
       
   104     delete iRepository;
       
   105     delete iPropertyWatcher;
       
   106     delete iHapticsPlayer;
       
   107     delete iAudioPlayer;
       
   108     REComSession::FinalClose();
       
   109     }
       
   110     
       
   111 // ---------------------------------------------------------------------------
       
   112 // We play feedback in case all three conditions are met:
       
   113 // 
       
   114 // #1 Either vibra or audio feedback is requested and globally enabled
       
   115 // #2 Given feedback type is different than "None"
       
   116 // #3 Player instance exists
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 void CTactileFeedbackResolver::PlayFeedback( 
       
   120     TTouchLogicalFeedback aFeedback,
       
   121     TBool aPlayVibra,
       
   122     TBool aPlayAudio )
       
   123     {
       
   124     // TRACE("CTactileFeedbackResolver::PlayFeedback - Begin");
       
   125     // Feedback filtering in order to prevent too many feedbacks
       
   126     // in a short time (e.g. when doing text selection).    
       
   127     TTime now;
       
   128     now.UniversalTime();
       
   129     
       
   130     TTimeIntervalMicroSeconds interval = 
       
   131         now.MicroSecondsFrom( iLastFeedbackTimeStamp );
       
   132     
       
   133      if ( iMinimumInterval == TTimeIntervalMicroSeconds( 0 ) || 
       
   134           now < iLastFeedbackTimeStamp ||                       
       
   135           iMinimumInterval <= interval )                        
       
   136         {
       
   137         // First store the timestamp of this feedback playing moment.
       
   138         // This really needs to be done when 
       
   139         // actually playing feedback (not when feedback was requested
       
   140         // but filtered out).
       
   141         iLastFeedbackTimeStamp = now;      
       
   142     
       
   143     
       
   144         // Force vibra- and audio feedback off if those are globally disabled
       
   145         if ( !iVibraEnabled )
       
   146             {
       
   147             aPlayVibra = EFalse;
       
   148             }
       
   149             
       
   150         if ( !iAudioEnabled )
       
   151             {
       
   152             aPlayAudio = EFalse;
       
   153             }
       
   154         
       
   155         if ( ( aPlayVibra || aPlayAudio ) &&        // #1
       
   156                aFeedback != ETouchFeedbackNone )    // #2
       
   157             {
       
   158             OstTrace0( TACTILE_PERFORMANCE, TACTILE_RESOLVER_PLAY_FEEDBACK_1, "e_TACTILE_RESOLVER_PLAY_FEEDBACK 1");
       
   159             
       
   160             if ( iVibraFirst )   
       
   161                 {
       
   162                 if ( aPlayVibra && iHapticsPlayer )
       
   163                     {
       
   164                     iHapticsPlayer->PlayFeedback( aFeedback );    
       
   165                     }
       
   166                 if ( aPlayAudio && iAudioPlayer )
       
   167                     {
       
   168                     iAudioPlayer->PlayFeedback( aFeedback );    
       
   169                     }                
       
   170                 }
       
   171             else
       
   172                 {
       
   173                 if ( aPlayAudio && iAudioPlayer )
       
   174                     {
       
   175                     iAudioPlayer->PlayFeedback( aFeedback );    
       
   176                     }                
       
   177                 if ( aPlayVibra && iHapticsPlayer )
       
   178                     {
       
   179                     iHapticsPlayer->PlayFeedback( aFeedback );    
       
   180                     }
       
   181                 }
       
   182 
       
   183             OstTrace0( TACTILE_PERFORMANCE, TACTILE_RESOLVER_PLAY_FEEDBACK_0, "e_TACTILE_RESOLVER_PLAY_FEEDBACK 0");
       
   184             }
       
   185         }
       
   186     // TRACE("CTactileFeedbackResolver::PlayFeedback - End");
       
   187     }
       
   188     
       
   189 // ---------------------------------------------------------------------------
       
   190 // Central Repository related initializations.
       
   191 // 
       
   192 // ---------------------------------------------------------------------------
       
   193 //   
       
   194 void CTactileFeedbackResolver::InitializeCrKeysL()    
       
   195     {
       
   196     if ( !iRepository )
       
   197         {
       
   198         iRepository = CRepository::NewL( KCRUidTactileFeedback );    
       
   199         }    
       
   200     
       
   201     TInt minInterval(0);
       
   202     // Read and store minimun feedback interfal
       
   203     User::LeaveIfError( iRepository->Get( KTactileFeedbackMinimumInterval, 
       
   204                                           minInterval ) );
       
   205                                           
       
   206     // Time is in milliseconds in Central Repository
       
   207     iMinimumInterval = TTimeIntervalMicroSeconds( 1000*minInterval );
       
   208     
       
   209     
       
   210     TInt internalSettings(0); 
       
   211     
       
   212     User::LeaveIfError( iRepository->Get( KTactileInternalSettings, 
       
   213                                           internalSettings ) );
       
   214 
       
   215     iVibraFirst = internalSettings & KTactileInternalSettingsVibraPlayedFirst;
       
   216 
       
   217     // Notifier for Central Repository changes.
       
   218     if ( !iCenRepNotifier )
       
   219         {
       
   220         iCenRepNotifier = CCenRepNotifyHandler::NewL( *this, 
       
   221                                                       *iRepository );
       
   222         iCenRepNotifier->StartListeningL();             
       
   223         }
       
   224     }
       
   225     
       
   226 // ---------------------------------------------------------------------------
       
   227 // P&S -related initializations have been moved here for making
       
   228 // ConstructL more clear.
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 void CTactileFeedbackResolver::InitializePsKeysL()
       
   232     {
       
   233     TInt err = RProperty::Define( 
       
   234                 KPSUidTactileFeedback,
       
   235                 KTactileFeedbackEnabled,
       
   236                 RProperty::EInt,
       
   237                 KTactileReadPolicy,
       
   238                 KTactileWritePolicy );
       
   239 
       
   240     if ( err != KErrNone && err != KErrAlreadyExists )
       
   241         {
       
   242         User::Leave( err );
       
   243         }
       
   244         
       
   245     // Enable feedback by default.
       
   246     TInt playerType(0);
       
   247     playerType |= ETouchFeedbackVibra;
       
   248     playerType |= ETouchFeedbackAudio;    
       
   249     
       
   250     RProperty::Set( KPSUidTactileFeedback, KTactileFeedbackEnabled, playerType );    
       
   251     
       
   252     // Watcher will start monitoring immediately, i.e. we don't have to
       
   253     // start it separately.
       
   254     iPropertyWatcher = CTactilePropertyWatcher::NewL(
       
   255         *this,
       
   256         KPSUidTactileFeedback,
       
   257         KTactileFeedbackEnabled,
       
   258         CActive::EPriorityStandard );
       
   259                 
       
   260     ReadFeedbackEnabledFromPS();    
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // We only change our bookkeeping in case we succeed in reading the P&S
       
   265 // property
       
   266 // ---------------------------------------------------------------------------
       
   267 //
       
   268 void CTactileFeedbackResolver::ReadFeedbackEnabledFromPS()
       
   269     {
       
   270     TInt tmpVal = 0;
       
   271     TInt err = iPropertyWatcher->Get( tmpVal );
       
   272     
       
   273     if ( err == KErrNone )
       
   274         {
       
   275         iVibraEnabled = EFalse;
       
   276         iAudioEnabled = EFalse;
       
   277         
       
   278         // Check if vibra feedback is enabled (bit 0x01).
       
   279         if ( (tmpVal & 0x01) )
       
   280             {
       
   281             iVibraEnabled = ETrue;
       
   282             }
       
   283         // Check if audio feedback is enabled (bit 0x02).            
       
   284         if ( (tmpVal & 0x02) )
       
   285             {
       
   286             iAudioEnabled = ETrue;            
       
   287             }
       
   288         }
       
   289     }   
       
   290     
       
   291 // ---------------------------------------------------------------------------
       
   292 // From class MTactilePropertyObserver
       
   293 // Called when the property for globally enabling or disabling feedback
       
   294 // changes
       
   295 // ---------------------------------------------------------------------------
       
   296 //
       
   297 void CTactileFeedbackResolver::PropertyChangedL( const TUid aCategory, 
       
   298                                                  const TUint aKey )
       
   299     {
       
   300     if ( aCategory == KPSUidTactileFeedback && 
       
   301          aKey == KTactileFeedbackEnabled )
       
   302         {
       
   303         ReadFeedbackEnabledFromPS();
       
   304         }
       
   305     }    
       
   306     
       
   307 // ---------------------------------------------------------------------------
       
   308 // Start continuous feedback.
       
   309 // ---------------------------------------------------------------------------
       
   310 //   
       
   311 void CTactileFeedbackResolver::StartFeedback( TTouchContinuousFeedback aType,
       
   312                                               TInt aIntensity )
       
   313     {
       
   314     if ( iHapticsPlayer && !iFeedbackStarted && iVibraEnabled )
       
   315         {
       
   316         iHapticsPlayer->StartFeedback( aType, aIntensity );
       
   317         iFeedbackStarted = ETrue;
       
   318         }
       
   319     }
       
   320         
       
   321 // ---------------------------------------------------------------------------
       
   322 // Modify continuous feedback.
       
   323 // ---------------------------------------------------------------------------
       
   324 //                        
       
   325 void CTactileFeedbackResolver::ModifyFeedback( TInt aIntensity )
       
   326     {
       
   327     if ( iHapticsPlayer )
       
   328         {
       
   329         iHapticsPlayer->ModifyFeedback( aIntensity );
       
   330         }    
       
   331     }
       
   332     
       
   333 // ---------------------------------------------------------------------------
       
   334 // Stop continuous feedback.
       
   335 // ---------------------------------------------------------------------------
       
   336 //    
       
   337 void CTactileFeedbackResolver::StopFeedback()
       
   338     {
       
   339     if ( iHapticsPlayer )
       
   340         {
       
   341         iHapticsPlayer->StopFeedback();
       
   342         iFeedbackStarted = EFalse;
       
   343         }  
       
   344     }
       
   345 
       
   346 // ---------------------------------------------------------------------------
       
   347 // Play preview feedback.
       
   348 // ---------------------------------------------------------------------------
       
   349 //    
       
   350 void CTactileFeedbackResolver::PlayPreviewFeedback( TInt aLevel,
       
   351                           TTouchLogicalFeedback aFeedback,
       
   352                           TTouchFeedbackType aType )
       
   353     {
       
   354     if ( aType & ETouchFeedbackVibra && iHapticsPlayer )
       
   355         {
       
   356         iHapticsPlayer->PlayPreviewFeedback( aLevel, aFeedback );
       
   357         }
       
   358     if ( aType & ETouchFeedbackAudio && iAudioPlayer )
       
   359         {
       
   360         iAudioPlayer->PlayPreviewFeedback( aLevel, aFeedback );
       
   361         }    
       
   362     }
       
   363 
       
   364 // ---------------------------------------------------------------------------
       
   365 // Start preview of continuous feedback.
       
   366 // ---------------------------------------------------------------------------
       
   367 //
       
   368 void CTactileFeedbackResolver::StartPreviewFeedback( TInt aLevel,
       
   369                            TTouchContinuousFeedback aFeedback,
       
   370                            TInt aIntensity,
       
   371                            TTouchFeedbackType aType )
       
   372     {
       
   373     if ( aType & ETouchFeedbackVibra && iHapticsPlayer )
       
   374         {
       
   375         iHapticsPlayer->StartPreviewFeedback( aLevel, aFeedback, aIntensity );
       
   376         }    
       
   377     }
       
   378 
       
   379 // ---------------------------------------------------------------------------
       
   380 // Stop preview of continuous feedback.
       
   381 // ---------------------------------------------------------------------------
       
   382 //
       
   383 void CTactileFeedbackResolver::StopPreviewFeedback()
       
   384     {
       
   385     if ( iHapticsPlayer )
       
   386         {
       
   387         iHapticsPlayer->StopPreviewFeedback();
       
   388         }        
       
   389     }
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // Create haptics player.
       
   393 // ---------------------------------------------------------------------------
       
   394 //    
       
   395 void CTactileFeedbackResolver::CreateHapticsPlayerL( TUid aHapticsUid )
       
   396     {
       
   397     RImplInfoPtrArray implArray;
       
   398     CleanupClosePushL( implArray );
       
   399     CTactilePlayer::ListImplementationsL( ETactilePlayerHaptics, implArray );
       
   400     
       
   401     if (!implArray.Count())
       
   402         {
       
   403         TRACE( "CTactileFeedbackResolver::ConstructL - No Haptics plugins found" );
       
   404         }
       
   405     for ( TInt i(0) ; i < implArray.Count() && !iHapticsPlayer; i++ )
       
   406         {
       
   407         // Create haptics player. 
       
   408         if ( !iHapticsPlayer &&
       
   409            ( implArray[i]->ImplementationUid() == aHapticsUid ) )
       
   410             {
       
   411             TUid pluginUid = implArray[i]->ImplementationUid();
       
   412         
       
   413             // delete implementation info list contents
       
   414             implArray.ResetAndDestroy();
       
   415             
       
   416             iHapticsPlayer = CTactilePlayer::NewL( pluginUid, *iRepository );
       
   417             }
       
   418         }
       
   419     
       
   420     // Make sure that implArray is reseted also if plugin wasn't found.    
       
   421     if ( !iHapticsPlayer ) 
       
   422         {
       
   423         implArray.ResetAndDestroy();
       
   424         }
       
   425         
       
   426     CleanupStack::PopAndDestroy( &implArray );            
       
   427     }
       
   428 
       
   429 // ---------------------------------------------------------------------------
       
   430 // Create audio player.
       
   431 // ---------------------------------------------------------------------------
       
   432 //        
       
   433 void CTactileFeedbackResolver::CreateAudioPlayerL( TUid aAudioUid )
       
   434     {
       
   435     RImplInfoPtrArray implArray;
       
   436     CleanupClosePushL( implArray );    
       
   437     CTactilePlayer::ListImplementationsL( ETactilePlayerAudio, implArray );
       
   438     
       
   439     if (!implArray.Count())
       
   440         {
       
   441         TRACE( "CTactileFeedbackResolver::ConstructL - No Audio plugins found" );
       
   442         }
       
   443     for ( TInt i(0) ; i < implArray.Count() && !iAudioPlayer; i++ )
       
   444         {            
       
   445         // Create audio player.           
       
   446         if ( !iAudioPlayer &&
       
   447              ( implArray[i]->ImplementationUid() == aAudioUid ) )
       
   448             {
       
   449             TUid pluginUid = implArray[i]->ImplementationUid();
       
   450         
       
   451             // delete implementation info list contents
       
   452             implArray.ResetAndDestroy();     
       
   453                    
       
   454             iAudioPlayer = CTactilePlayer::NewL( pluginUid, *iRepository );
       
   455             }                
       
   456         }
       
   457         
       
   458     // Make sure that implArray is reseted also if plugin wasn't found.
       
   459     if ( !iAudioPlayer ) 
       
   460         {
       
   461         implArray.ResetAndDestroy();
       
   462         }        
       
   463         
       
   464     CleanupStack::PopAndDestroy( &implArray );    
       
   465     }
       
   466 
       
   467 // ---------------------------------------------------------------------------
       
   468 // From MCenRepNotifyHandlerCallback.
       
   469 // ---------------------------------------------------------------------------
       
   470 //
       
   471 void CTactileFeedbackResolver::HandleNotifyGeneric( TUint32 aId )
       
   472     {
       
   473     TRAP_IGNORE( DoHandleNotifyGenericL(aId ) );
       
   474     }
       
   475 
       
   476 // ---------------------------------------------------------------------------
       
   477 // Handle changes in tactile feedback settings.
       
   478 // ---------------------------------------------------------------------------
       
   479 //
       
   480 void CTactileFeedbackResolver::DoHandleNotifyGenericL( TUint32 aId )
       
   481     {
       
   482     switch ( aId )
       
   483         {
       
   484         case KTactileFeedbackMinimumInterval:
       
   485             InitializeCrKeysL();
       
   486             break;
       
   487         case KTactileHapticsPlugin:
       
   488             {
       
   489             TInt hapticsUid(0);
       
   490             iRepository->Get( KTactileHapticsPlugin, hapticsUid );
       
   491             if ( iHapticsPlayer )
       
   492                 {
       
   493                 delete iHapticsPlayer;
       
   494                 iHapticsPlayer = NULL;
       
   495                 }
       
   496             if ( hapticsUid )
       
   497                 {
       
   498                 CreateHapticsPlayerL( TUid::Uid( hapticsUid ) );    
       
   499                 }                    
       
   500             }
       
   501             break;
       
   502         case KTactileAudioPlugin:
       
   503             {
       
   504             TInt audioUid(0);
       
   505             iRepository->Get( KTactileAudioPlugin, audioUid );
       
   506             if ( iAudioPlayer )
       
   507                 {
       
   508                 delete iAudioPlayer;
       
   509                 iAudioPlayer = NULL;
       
   510                 }
       
   511             if ( audioUid )
       
   512                 {
       
   513                 CreateAudioPlayerL( TUid::Uid( audioUid ) );
       
   514                 }
       
   515             }            
       
   516             break;
       
   517         default:
       
   518             break;
       
   519         }
       
   520     }