hwrmhaptics/hapticspluginmanager/src/hwrmhapticscommondata.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Haptics common data class definition
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "hwrmhapticstrace.h"
       
    20 #include "hwrmhapticscommondata.h"
       
    21 
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 // Two-phased constructor.
       
    25 // ---------------------------------------------------------------------------
       
    26 //
       
    27 EXPORT_C CHWRMHapticsCommonData* CHWRMHapticsCommonData::NewL()
       
    28     {
       
    29     COMPONENT_TRACE( ( _L( "CHWRMHapticsCommonData::NewL()" ) ) );
       
    30 
       
    31     CHWRMHapticsCommonData* self = new( ELeave ) CHWRMHapticsCommonData();
       
    32     CleanupStack::PushL( self );
       
    33 
       
    34     self->ConstructL();
       
    35 
       
    36     CleanupStack::Pop( self );
       
    37 
       
    38     COMPONENT_TRACE( ( _L( "CHWRMHapticsCommonData::NewL - return 0x%x" ), self ) );
       
    39     
       
    40     return self;
       
    41     }
       
    42    
       
    43 // ---------------------------------------------------------------------------
       
    44 // Destructor
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CHWRMHapticsCommonData::~CHWRMHapticsCommonData()
       
    48     {
       
    49     COMPONENT_TRACE( ( _L( "CHWRMHapticsCommonData::~CHWRMHapticsCommonData()" ) ) );
       
    50 
       
    51     iClientArray.ResetAndDestroy();
       
    52     iClientArray.Close();
       
    53 
       
    54     iActuatorEvents.Close();
       
    55     
       
    56     COMPONENT_TRACE( ( _L( "CHWRMHapticsCommonData::~CHWRMHapticsCommonData - return" ) ) );
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // Adds a new session to client array.
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 void CHWRMHapticsCommonData::AddSessionL( const CSession2* aSession )
       
    64     {
       
    65     // create new client status data
       
    66     TClientStatus* clientStatus = 
       
    67         new (ELeave) TClientStatus( aSession, iGeneralStatus );
       
    68     CleanupStack::PushL( clientStatus );
       
    69     
       
    70     iClientArray.AppendL( clientStatus );
       
    71     
       
    72     CleanupStack::Pop( clientStatus );
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // Removes a session from the client array.
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 void CHWRMHapticsCommonData::RemoveSession( const CSession2* aSession )
       
    80     {
       
    81     // find the index of the client
       
    82     TInt index = FindClient( aSession );
       
    83     
       
    84     // remove status related data of the session/client
       
    85     if ( index != KErrNotFound )
       
    86         {
       
    87         // complete the observer, if it has been set
       
    88         if ( iClientArray[index]->iStatusObserver.Handle() )
       
    89             {
       
    90             // with completion code KErrCancel the client should stop 
       
    91             // requesting notifications
       
    92             iClientArray[index]->iStatusObserver.Complete( KErrCancel );
       
    93             }
       
    94 
       
    95         // delete and remove session/client from the array
       
    96         delete iClientArray[index];
       
    97         iClientArray.Remove( index );
       
    98         }
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // Adds the given message as a haptics status observer.
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 void CHWRMHapticsCommonData::AddStatusObserver( 
       
   106                                         const RMessage2& aStatusObserverMsg )
       
   107     {
       
   108     // find the index of the client
       
   109     TInt index = FindClient( aStatusObserverMsg.Session() );
       
   110     
       
   111     if ( index != KErrNotFound )
       
   112         {
       
   113         // set new observer message
       
   114         iClientArray[index]->iStatusObserver = aStatusObserverMsg;
       
   115         
       
   116         // read and set request types value
       
   117         THWRMHapticsStatusTypes reqTypes = 
       
   118             static_cast<THWRMHapticsStatusTypes>( aStatusObserverMsg.Int0() );
       
   119 
       
   120         iClientArray[index]->iStatusTypes = reqTypes;
       
   121         
       
   122         // send notification of the current status, if it is needed
       
   123         // for this client
       
   124         if ( iClientArray[index]->iNotificationRequired )
       
   125             {
       
   126             NotifyStatus( iClientArray[index]->iStatus, 
       
   127                           aStatusObserverMsg.Session() );
       
   128 
       
   129             iClientArray[index]->iNotificationRequired = EFalse;
       
   130             }
       
   131         else if ( iClientArray[index]->iRequiredActuators.Count() )
       
   132             {
       
   133             // there are unsent actuator event notifications, 
       
   134             // send immediately
       
   135             THWRMLogicalActuators actuator = 
       
   136                 iClientArray[index]->iRequiredActuators[0];
       
   137             
       
   138             // search for the event
       
   139             TInt eventIndex = FindActuatorEvent( actuator );
       
   140             
       
   141             // send notification, if event found
       
   142             if ( eventIndex != KErrNotFound )
       
   143                 {
       
   144                 NotifyActuatorEventToClient( 
       
   145                                 index, 
       
   146                                 iActuatorEvents[eventIndex].iActuatorEvent, 
       
   147                                 iActuatorEvents[eventIndex].iActuator );
       
   148                 }
       
   149             
       
   150             // remove the actuator from required event notification array
       
   151             iClientArray[index]->iRequiredActuators.Remove( 0 );
       
   152             }
       
   153         }
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // Notifies the given haptics status to the observer identified
       
   158 // with the given session, if the observer message exists.
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CHWRMHapticsCommonData::NotifyStatus( 
       
   162                             MHWRMHapticsObserver::THWRMHapticsStatus aStatus,
       
   163                             const CSession2* aSession )
       
   164     {
       
   165     // get the index of the observer to be notified
       
   166     TInt index = FindClient( aSession );
       
   167     
       
   168     // notify observer and store status
       
   169     NotifyStatusToClient( index, aStatus );
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // Notifies the last event of the given actuator to the observer
       
   174 // identified with the given session.
       
   175 // ---------------------------------------------------------------------------
       
   176 //
       
   177 void CHWRMHapticsCommonData::NotifyActuatorEvent( 
       
   178                                             THWRMLogicalActuators aActuator,
       
   179                                             const CSession2* aSession )
       
   180     {
       
   181     // get the index of the observer to be notified
       
   182     TInt clientIndex = FindClient( aSession );
       
   183     
       
   184     // try to find the event data
       
   185     TInt eventIndex = FindActuatorEvent( aActuator );
       
   186 
       
   187     // if client and event were found, notify client
       
   188     if ( clientIndex != KErrNotFound && eventIndex != KErrNotFound )
       
   189         {
       
   190         NotifyActuatorEventToClient( 
       
   191                         clientIndex, 
       
   192                         iActuatorEvents[eventIndex].iActuatorEvent, 
       
   193                         iActuatorEvents[eventIndex].iActuator );
       
   194         }
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // Notifies all observers except the one identified with the given
       
   199 // session using the given haptics status.
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 void CHWRMHapticsCommonData::BroadcastStatus( 
       
   203                             MHWRMHapticsObserver::THWRMHapticsStatus aStatus,
       
   204                             const CSession2* aSession )
       
   205     {
       
   206     // notify all clients, except the one with given session
       
   207     for ( TInt i = 0; i < iClientArray.Count(); ++i )
       
   208         {
       
   209         if ( iClientArray[i]->iSession != aSession )
       
   210             {
       
   211             NotifyStatusToClient( i, aStatus );
       
   212             }
       
   213         }
       
   214 
       
   215     iGeneralStatus = aStatus;
       
   216     }
       
   217 
       
   218 // ---------------------------------------------------------------------------
       
   219 // Notifies all observers listening to actuator events using the 
       
   220 // given event and actuator type values.
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 void CHWRMHapticsCommonData::BroadcastActuatorEvent( 
       
   224                 MHWRMHapticsActuatorObserver::THWRMActuatorEvents aEvent,
       
   225                 THWRMLogicalActuators aActuator )
       
   226     {
       
   227     // notify all clients listening to actuator events
       
   228     for ( TInt i = 0; i < iClientArray.Count(); ++i )
       
   229         {
       
   230         if ( iClientArray[i]->iStatusTypes == EHWRMHapticsActuatorStatus ||
       
   231              iClientArray[i]->iStatusTypes == EHWRMHapticsBothStatus )
       
   232             {
       
   233             NotifyActuatorEventToClient( i, aEvent, aActuator );
       
   234             }
       
   235         }
       
   236     
       
   237     // store actuator event, so that it can be sent, if notification did not
       
   238     // succeed above
       
   239     StoreActuatorEvent( aEvent, aActuator );
       
   240     }
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 // Returns the current status of the given session.
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 MHWRMHapticsObserver::THWRMHapticsStatus
       
   247 CHWRMHapticsCommonData::CurrentStatus( const CSession2* aSession ) const
       
   248     {
       
   249     // get the index of the client
       
   250     TInt index = FindClient( aSession );
       
   251 
       
   252     return iClientArray[index]->iStatus;
       
   253     }
       
   254 
       
   255 // ---------------------------------------------------------------------------
       
   256 // Returns the index of the observer with given session.
       
   257 // ---------------------------------------------------------------------------
       
   258 //
       
   259 TInt CHWRMHapticsCommonData::FindClient( const CSession2* aSession ) const
       
   260     {
       
   261     TInt ret = KErrNotFound;
       
   262     
       
   263     // find the observer with the given session
       
   264     for ( TInt i = 0; i < iClientArray.Count() && ret == KErrNotFound; ++i )
       
   265         {
       
   266         if ( iClientArray[i]->iSession == aSession )
       
   267             {
       
   268             ret = i;
       
   269             }
       
   270         }
       
   271     
       
   272     return ret;
       
   273     }
       
   274 
       
   275 // ---------------------------------------------------------------------------
       
   276 // Returns the index of the actuator event in the event array.
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 TInt CHWRMHapticsCommonData::FindActuatorEvent( 
       
   280                                     THWRMLogicalActuators aActuator ) const
       
   281     {
       
   282     TInt ret = KErrNotFound;
       
   283     
       
   284     // find the event with the given actuator
       
   285     for ( TInt i = 0; i < iActuatorEvents.Count() && ret == KErrNotFound; ++i )
       
   286         {
       
   287         if ( iActuatorEvents[i].iActuator == aActuator )
       
   288             {
       
   289             ret = i;
       
   290             }
       
   291         }
       
   292     
       
   293     return ret;
       
   294     }
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // Updates given actuators status, or if not found, inserts new
       
   298 // data for the actuator.
       
   299 // ---------------------------------------------------------------------------
       
   300 //
       
   301 void CHWRMHapticsCommonData::StoreActuatorEvent( 
       
   302                                         TActuatorStatus aStatus, 
       
   303                                         THWRMLogicalActuators aActuator )
       
   304     {
       
   305     // try to find the event data
       
   306     TInt index = FindActuatorEvent( aActuator );
       
   307 
       
   308     if ( index != KErrNotFound )
       
   309         {
       
   310         // data was found, update it
       
   311         iActuatorEvents[index].iActuatorEvent = aStatus;
       
   312         iActuatorEvents[index].iActuator = aActuator;
       
   313         }
       
   314     else
       
   315         {
       
   316         // data was not found, create new data
       
   317         TActuatorEvent event( aStatus, aActuator );
       
   318         iActuatorEvents.Append( event );
       
   319         }
       
   320     }
       
   321 
       
   322 // ---------------------------------------------------------------------------
       
   323 // Implements the actual notification to the client and stores the status
       
   324 // for the indexed client data.
       
   325 // ---------------------------------------------------------------------------
       
   326 //
       
   327 void CHWRMHapticsCommonData::NotifyStatusToClient( 
       
   328                         TInt aIndex,
       
   329                         MHWRMHapticsObserver::THWRMHapticsStatus aStatus )
       
   330     {
       
   331     // notify only if the client has an observer and client has requested
       
   332     // haptics status notification
       
   333     if ( iClientArray[aIndex]->iStatusObserver.Handle() && 
       
   334          ( iClientArray[aIndex]->iStatusTypes == EHWRMHapticsSessionStatus ||
       
   335            iClientArray[aIndex]->iStatusTypes == EHWRMHapticsBothStatus ) )
       
   336         {
       
   337         // create status package
       
   338         TPckg<THWRMHapticsStatusTypes> statusTypePckg( EHWRMHapticsSessionStatus );
       
   339         TPckg<TInt> statusPckg( aStatus );
       
   340         
       
   341         // write type and status to message and complete it. Completing the 
       
   342         // message sets the handle in the message to zero, which is used to 
       
   343         // notice that the message has already been completed.
       
   344         TInt err = iClientArray[aIndex]->iStatusObserver.Write( 1, statusTypePckg, 0 );
       
   345         err = iClientArray[aIndex]->iStatusObserver.Write( 2, statusPckg, 0 );
       
   346         iClientArray[aIndex]->iStatusObserver.Complete( err );
       
   347         }
       
   348     else
       
   349         {
       
   350         // status was not sent to client, so it needs to be sent, when
       
   351         // the client requests status notification for the next time
       
   352         iClientArray[aIndex]->iNotificationRequired = ETrue;
       
   353         }
       
   354     
       
   355     // store the status value for the client even if it was not sent to
       
   356     // an observer so that it can be fetched by a direct get status -command
       
   357     iClientArray[aIndex]->iStatus = aStatus;
       
   358     }
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // Implements the actual actuator event notification to the client.
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 void CHWRMHapticsCommonData::NotifyActuatorEventToClient( 
       
   365                                             TInt aIndex, 
       
   366                                             TActuatorStatus aStatus,
       
   367                                             THWRMLogicalActuators aActuator )
       
   368     {
       
   369     // notify or store data for client only if client has requested actuator
       
   370     // event notification
       
   371     if ( iClientArray[aIndex]->iStatusTypes == EHWRMHapticsActuatorStatus ||
       
   372          iClientArray[aIndex]->iStatusTypes == EHWRMHapticsBothStatus )
       
   373         {
       
   374         // notify only if the client has an observer
       
   375         if ( iClientArray[aIndex]->iStatusObserver.Handle() )
       
   376             {
       
   377             // create type, status and actuator packages
       
   378             TPckg<THWRMHapticsStatusTypes> statusTypePckg( EHWRMHapticsActuatorStatus );
       
   379             TPckg<TInt> statusPckg( aStatus );
       
   380             TPckg<THWRMLogicalActuators> actuatorPckg( aActuator );
       
   381             
       
   382             // write type, status and actuator to message and complete it. 
       
   383             // Completing the message sets the handle in the message to zero, 
       
   384             // which is used to notice that the message has already been 
       
   385             // completed.
       
   386             TInt err = iClientArray[aIndex]->iStatusObserver.Write( 1, statusTypePckg, 0 );
       
   387             err = iClientArray[aIndex]->iStatusObserver.Write( 2, statusPckg, 0 );
       
   388             err = iClientArray[aIndex]->iStatusObserver.Write( 3, actuatorPckg, 0 );
       
   389             iClientArray[aIndex]->iStatusObserver.Complete( err );
       
   390             }
       
   391         else
       
   392             {
       
   393             // event was not sent to client, so it needs to be sent, when
       
   394             // the client requests notification for the next time
       
   395             TInt index = iClientArray[aIndex]->iRequiredActuators.Find( aActuator );
       
   396             if ( index == KErrNotFound )
       
   397                 {
       
   398                 // actuator was not in the list already --> needs to be added
       
   399                 iClientArray[aIndex]->iRequiredActuators.Append( aActuator );
       
   400                 }
       
   401             }
       
   402         }
       
   403     }
       
   404 
       
   405 // ---------------------------------------------------------------------------
       
   406 // C++ constructor.
       
   407 // ---------------------------------------------------------------------------
       
   408 //
       
   409 CHWRMHapticsCommonData::CHWRMHapticsCommonData()
       
   410     : iGeneralStatus( MHWRMHapticsObserver::EHWRMHapticsStatusAvailable )
       
   411     {
       
   412     COMPONENT_TRACE( ( _L( "CHWRMHapticsCommonData::CHWRMHapticsCommonData()" ) ) );
       
   413     COMPONENT_TRACE( ( _L( "CHWRMHapticsCommonData::CHWRMHapticsCommonData - return" ) ) );
       
   414     }
       
   415 
       
   416 // ---------------------------------------------------------------------------
       
   417 // Symbian 2nd phase constructor can leave.
       
   418 // ---------------------------------------------------------------------------
       
   419 //
       
   420 void CHWRMHapticsCommonData::ConstructL()
       
   421     {    
       
   422     COMPONENT_TRACE( ( _L( "CHWRMHapticsCommonData::ConstructL()" ) ) );
       
   423     COMPONENT_TRACE( ( _L( "CHWRMHapticsCommonData::ConstructL - return" ) ) );
       
   424     }
       
   425 
       
   426 //  End of File