textinput/peninputarc/src/peninputserverapp/peninputserversession.cpp
changeset 0 eb1f2e154e89
child 13 1bbdde98cc2d
equal deleted inserted replaced
-1:000000000000 0:eb1f2e154e89
       
     1 /*
       
     2 * Copyright (c) 2005-2006 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:  Implementation for peninput server session class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //INCLUDE FILES
       
    20 #include <e32svr.h>
       
    21 
       
    22 #include "peninputcmd.h"
       
    23 #include "peninputclientserver.h"
       
    24 #include "peninputanimcommand.h"
       
    25 #include "peninputserver.h"
       
    26 #include "peninputserversession.h"
       
    27 
       
    28 const TInt KAllEventIndex = -1;
       
    29 const TInt KFirstEventIndex = 0;
       
    30 const TInt KMaxEventBuf = 50;
       
    31 // ======== MEMBER FUNCTIONS ========
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // CPeninputServerSession::CPeninputServerSession
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CPeninputServerSession::CPeninputServerSession( RThread &aClient, 
       
    38                             CPeninputServer& aServer)
       
    39 	                        : iPenInputSvr( aServer ),
       
    40     	                      iObserverActive(EFalse),
       
    41     	                      iCombinedMsgNum(0),
       
    42     	                      iCombinedEventId(ESignalKeyEvent),
       
    43     	                      iAppInfoType(EAppIndicatorMsg),
       
    44     	                      iDisabledUiType(0)
       
    45     {
       
    46     iPenInputSvr.IncreaseSessionCount();
       
    47     iClientId = aClient.Id();
       
    48     RProcess process;
       
    49     if ( KErrNone == aClient.Process(  process ) )
       
    50         {
       
    51         iClientProcess = process.Id();   
       
    52         process.Close();
       
    53         }
       
    54     }
       
    55 // ---------------------------------------------------------------------------
       
    56 // CPeninputServerSession::ConstructL
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 void CPeninputServerSession::ConstructL()
       
    60     {
       
    61     iAppInfo = KNullDesC().AllocL();
       
    62     iCombinedEventBuf.CreateMaxL(KMaxEventBuf);
       
    63     iCombinedEventBuf.Zero();    
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CPeninputServerSession::NewL
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CPeninputServerSession* CPeninputServerSession::NewL( RThread &aClient, 
       
    71                             						CPeninputServer& aServer)
       
    72     {
       
    73     CPeninputServerSession* session = new(ELeave) CPeninputServerSession(
       
    74                                            aClient, aServer);
       
    75     CleanupStack::PushL(session);
       
    76     session->ConstructL();
       
    77     CleanupStack::Pop(session);
       
    78     return session;
       
    79     }
       
    80 
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // CPeninputServerSession::ServiceL
       
    84 // Possible leave is handled by the CPeninputServer::RunError
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CPeninputServerSession::ServiceL( const RMessage2& aMessage )
       
    88     {
       
    89     
       
    90     switch ( aMessage.Function() )
       
    91         {
       
    92 
       
    93         case EPeninputServerRequestMessageNotification:
       
    94             // asynchnorous observation request issued, store it
       
    95             // a client can have only one observation request at a time
       
    96             if(!iObserverActive)
       
    97                 {
       
    98                 iPendingRequest = aMessage;
       
    99                 iObserverActive = ETrue;
       
   100                 //if already some events buffered,send the event
       
   101                 if(iDataBuf.Count())
       
   102                     {
       
   103                     iPendingRequest.Complete(iSignalCodeArray[0]);                
       
   104                     iObserverActive = EFalse;
       
   105                     }
       
   106                 }
       
   107             else
       
   108                 {
       
   109                 // cancel this request because notify request has already been issued
       
   110                 aMessage.Complete(KErrCancel);
       
   111                 }
       
   112             
       
   113             break;
       
   114 
       
   115         case EPeninputServerCancelMessageNotification:
       
   116             {
       
   117             aMessage.Complete(KErrNone);  
       
   118             if(iObserverActive)
       
   119                 {
       
   120                 iPendingRequest.Complete(KErrCancel);
       
   121                 iObserverActive = EFalse;
       
   122                 }
       
   123             ProcessPendingEventBeforeExit();
       
   124             //iSemaphore.Signal();            
       
   125             }
       
   126             break;
       
   127             
       
   128         default:
       
   129             {
       
   130             
       
   131             //TRAPD(err, DoServiceL(aMessage));
       
   132             //if(KErrNone != err)
       
   133             //    aMessage.Complete(err);
       
   134             TInt ret = DoServiceL(aMessage);
       
   135             //aMessage.Complete(ret);
       
   136             
       
   137             }
       
   138         }
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // CPeninputServerSession::GetDisabledLayouts
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 const TProcessId& CPeninputServerSession::ClientProcessId( )
       
   146     {
       
   147     return iClientProcess ;
       
   148     }
       
   149     
       
   150 TInt CPeninputServerSession::BuildEventData()
       
   151     {
       
   152     if(iCombinedMsgNum > 0)     //the previous combined msg was not sent yet. No need to do again
       
   153         return iCombinedMsgNum;    
       
   154     TInt n = 0;
       
   155     //check how many signal can be combined
       
   156     while( n < iSignalCodeArray.Count() && iSignalCodeArray[n] == iCombinedEventId)
       
   157          ++n;
       
   158     if(n <= 0)
       
   159         return 0;
       
   160     iCombinedMsgNum = 0;
       
   161     for(; iCombinedMsgNum < n ; ++iCombinedMsgNum)
       
   162         {
       
   163         //check memory fist
       
   164         if(iCombinedEventBuf.MaxLength() < iCombinedEventBuf.Length() + iDataBuf[iCombinedMsgNum]->Length()) 
       
   165             {
       
   166             TInt err = iCombinedEventBuf.ReAlloc( KMaxEventBuf + 
       
   167                                       iCombinedEventBuf.Length() + 
       
   168                                       iDataBuf[iCombinedMsgNum]->Length());
       
   169             if(err != KErrNone)
       
   170                 {
       
   171                 break;
       
   172                 }
       
   173             }
       
   174         //copy data
       
   175         iCombinedEventBuf.Append(*(iDataBuf[iCombinedMsgNum]));        
       
   176         }
       
   177     return iCombinedMsgNum;
       
   178     }
       
   179     
       
   180 void CPeninputServerSession::RemoveEventDatas(TInt aNum)
       
   181     {
       
   182     aNum = aNum > 0 ? aNum : 1;
       
   183     for(TInt i = 0 ; i < aNum; ++i)
       
   184         RemoveEventDataByIdx(KFirstEventIndex); 
       
   185     iCombinedEventBuf.Zero();
       
   186     iCombinedMsgNum = 0;
       
   187     }
       
   188     
       
   189 TInt CPeninputServerSession::SendEventData(const RMessage2& aMessage, const TDesC& aData)
       
   190     {
       
   191     TInt ret = KErrNone;
       
   192     TInt maxBufLen = aMessage.Int0();    
       
   193     if(maxBufLen >= aData.Length())
       
   194         {
       
   195         TRAP_IGNORE(aMessage.WriteL(1,aData));
       
   196         
       
   197         //remove the data
       
   198         RemoveEventDatas(iCombinedMsgNum > 0 ? iCombinedMsgNum : 1);
       
   199         }
       
   200     else
       
   201         {
       
   202         ret = aData.Length();
       
   203         }
       
   204     
       
   205     return ret;    
       
   206     }
       
   207 // ---------------------------------------------------------------------------
       
   208 // CPeninputServerSession::DoServiceL
       
   209 // Possible leave is handled by the CPeninputServer::RunError
       
   210 // ---------------------------------------------------------------------------
       
   211 //
       
   212 TInt CPeninputServerSession::DoServiceL( const RMessage2& aMessage )
       
   213     {
       
   214     
       
   215     //TInt err = KErrNone;
       
   216     TInt ret = KErrNone;
       
   217     switch ( aMessage.Function() )
       
   218         {            
       
   219         case EPeninputRequestUiNotificationCompleted:
       
   220             {
       
   221             RemoveEventDataByIdx(KFirstEventIndex);
       
   222             iPenInputSvr.ExecutePendingAnimCmd();
       
   223             }
       
   224             break;
       
   225             
       
   226         case EPeninputServerClearServerEvent:
       
   227             {
       
   228             RemoveEventDataByIdx(KAllEventIndex);
       
   229             }
       
   230             break;
       
   231             
       
   232         case EPeninputRequestGetServerEventData:
       
   233             {
       
   234             if(iDataBuf.Count())
       
   235                 {                
       
   236                 TInt combinedEventNum = BuildEventData();
       
   237                 if(combinedEventNum)
       
   238                     ret = SendEventData(aMessage,iCombinedEventBuf);
       
   239                 else
       
   240                     ret = SendEventData(aMessage,*iDataBuf[KFirstEventIndex]);
       
   241                 
       
   242 //                if(ret == KErrNone)
       
   243   //                  RemoveEventDatas(combinedEventNum);
       
   244                 }
       
   245             else
       
   246                 {
       
   247                 aMessage.WriteL(1,KNullDesC);
       
   248                 }
       
   249             }
       
   250             break;        
       
   251             
       
   252         case EPeninputRequestGetServerEventDataNumber:
       
   253             {
       
   254             TInt num = iDataBuf.Count();
       
   255             TPckgC<TInt> msg(num);
       
   256             
       
   257             aMessage.WriteL(0,msg);
       
   258             }
       
   259             break;
       
   260           
       
   261         case EPeninputRequestGetDisableLayout:
       
   262             {
       
   263             //TInt layoyts = 0;
       
   264             //CPeninputServerSession* pSession = static_cast<CPeninputServerSession*>(aMessage.Session());
       
   265             //GetProcessLayouts( pSession->ClientProcessId(), layoyts );
       
   266             TInt dsaDisableUIType = iPenInputSvr.DisabledByDSA();
       
   267             TInt orientationDisableType = iPenInputSvr.DisabledByOrientation();
       
   268             if( dsaDisableUIType )
       
   269             	{
       
   270             	iDsaEverHappened = ETrue;
       
   271             	}
       
   272             TPckg<TInt> msg(iDisabledUiType | dsaDisableUIType | orientationDisableType);
       
   273 
       
   274             aMessage.WriteL(0,msg);            
       
   275             }
       
   276             break;            
       
   277         case EPeninputRequestDisableLayout:
       
   278             {
       
   279             TInt layouts  = aMessage.Int0(); 
       
   280             //CPeninputServerSession* pSession = static_cast<CPeninputServerSession*>(aMessage.Session());
       
   281             //SetProcessLayouts( pSession->ClientProcessId(), layouts );
       
   282             if(layouts < 0 )
       
   283                 {
       
   284                 iDisabledUiType = 0; //reset.
       
   285                 }
       
   286             else
       
   287                 {
       
   288                 iDisabledUiType |= layouts;
       
   289                 }
       
   290             }
       
   291             break;
       
   292             
       
   293         case EPeninputRequestUiIsVisible:
       
   294         case EPeninputServerRequestSetDisplayMode:
       
   295         case EPeninputRequestUiEditorMaxLength:
       
   296         case EPeninputRequestUiSetUiPos:        
       
   297         case EPeninputRequestUiGetUiPos:        
       
   298         case EPeninputRequestUiSendAppEditorText:        
       
   299         case EPeninputRequestActivateLayout:  
       
   300         case EPeninputRequestActivateLayoutInGlobalNotes:
       
   301         case EPeninputRequestDimLayout:
       
   302         case EPeninputRequestSetUiLayoutId:
       
   303         case EPeninputRequestSetUiLayoutIdWithData:
       
   304         case EPeninputRequestResourceChanged:
       
   305         case EPenInputRequestGetImePluginIdList:
       
   306         case EPenInputRequestGetPenSupportLanguages:
       
   307         case EPeninputRequestIsForeground:
       
   308         case EPeninputRequestSetForeground:
       
   309         case EPeninputRequestRelinquishForeground:
       
   310         case EPeninputRequestAddUiObserver:
       
   311         case EPeninputRequestIsLayoutDimmed:
       
   312         //case EPeninputRequestChangeUiObserverType:
       
   313         case EPeninputRequestRemoveUiObserver:            
       
   314         //case EPeninputRequestDisableLayout:   
       
   315         case EPeninputRequestServerThreadId:
       
   316         //case EPeninputRequestGetDisableLayout:
       
   317         case EPeninputBackgroudDefaultOri:
       
   318         case EPeninputRequestInternalDimLayout:
       
   319         case EPeninputRequestDimResChangeLayout:
       
   320         case EPeninputRequestSupportInputMode:
       
   321         case EPeninputRequestSetInputLanguage:
       
   322             ret = iPenInputSvr.HandleMessageL(aMessage);            
       
   323             break;
       
   324         case EPeninputRequestUpdateAppInfo:            
       
   325             {
       
   326             ret = HandleAppInfoChange(aMessage);
       
   327             if (ret == EBadRequest)
       
   328                 {
       
   329                 PanicClient(aMessage,EBadRequest );
       
   330                 return EBadRequest;                
       
   331                 }
       
   332             }
       
   333             break;
       
   334         case EPeninputRequestHandleClientCommand:
       
   335             {              
       
   336             ret = iPenInputSvr.HandleCommandL(aMessage);
       
   337             }
       
   338             break;        
       
   339             
       
   340         // requests we don't understand at all are a different thing,
       
   341         // so panic the client here, this function also completes the message
       
   342         default:
       
   343             {
       
   344             PanicClient(aMessage,EBadRequest );
       
   345             return EBadRequest;
       
   346             }
       
   347         }
       
   348 
       
   349     aMessage.Complete(ret);
       
   350     return ret;
       
   351     }
       
   352 
       
   353 
       
   354 // ---------------------------------------------------------------------------
       
   355 // CPeninputServerSession::SignalClientL
       
   356 // ---------------------------------------------------------------------------
       
   357 //
       
   358 TBool CPeninputServerSession::CompletePendingRequestL(TInt aSignalCode,
       
   359                                         const TDesC& aEventData, int aPos)
       
   360     {
       
   361     TBool ok = EFalse;
       
   362     HBufC* data = aEventData.AllocLC();
       
   363 //    iDataBuf.AppendL(data);
       
   364     if(aPos < 0) //append to the end
       
   365         {        
       
   366         iSignalCodeArray.Append(aSignalCode);
       
   367         iDataBuf.AppendL(data);
       
   368         }
       
   369     else
       
   370         {
       
   371         iSignalCodeArray.Insert(aSignalCode,aPos);
       
   372         iDataBuf.InsertL(data,aPos);        
       
   373         }    
       
   374     CleanupStack::Pop();//data
       
   375     
       
   376     if(iObserverActive)
       
   377         {             
       
   378         iPendingRequest.Complete(aSignalCode);
       
   379         iObserverActive = EFalse;
       
   380         ok = ETrue;
       
   381         }
       
   382     return ok;
       
   383     }
       
   384 // ---------------------------------------------------------------------------
       
   385 // CPeninputServerSession::SignalClientL
       
   386 // ---------------------------------------------------------------------------
       
   387 //
       
   388 TBool CPeninputServerSession::SignalClientL(TInt aSignalCode,
       
   389                                                 const TDesC& aEventData)
       
   390     {
       
   391     return CompletePendingRequestL(aSignalCode,aEventData);    
       
   392     }
       
   393 
       
   394 
       
   395 // ---------------------------------------------------------------------------
       
   396 // CPeninputServerSession::SignalUiActivationObserver
       
   397 // ---------------------------------------------------------------------------
       
   398 //
       
   399 TBool CPeninputServerSession::SignalUiActivationObserver(TInt aSignalCode, 
       
   400                                                          TInt aType)
       
   401     {    
       
   402     //check registerd UI type
       
   403     if((aType & iRegistedPenUiType) || (iRegistedPenUiType == EPluginInputModeAll))
       
   404         {        
       
   405         //insert the notification event before other event as anim object is waiting 
       
   406         // for client process.
       
   407         TInt i = iSignalCodeArray.Count() - 1;
       
   408         for( ; i >=0 ; i--)
       
   409             {
       
   410             if(ESignalPenUiActivated == iSignalCodeArray[i] ||
       
   411                         ESignalPenUiDeActivated == iSignalCodeArray[i])
       
   412                         break;
       
   413             }
       
   414         TRAP_IGNORE( CompletePendingRequestL(aSignalCode,KNullDesC,++i) );
       
   415         return ETrue;
       
   416         }    
       
   417         
       
   418     return EFalse;
       
   419     }
       
   420 
       
   421 // ---------------------------------------------------------------------------
       
   422 // CPeninputServerSession::ProcessPendingEventBeforeExit
       
   423 // ---------------------------------------------------------------------------
       
   424 //
       
   425 void CPeninputServerSession::ProcessPendingEventBeforeExit()
       
   426     {
       
   427     //only process pen ui notification event. Other events are discarded
       
   428     
       
   429     //for(TInt i = 0; i < iDelayedCmd.Count(); i++)
       
   430     //for(TInt i = 0; i < iDelayedCmd; i++)
       
   431         {
       
   432         iPenInputSvr.ExecutePendingAnimCmd();       
       
   433       //  iDelayedCmd --;
       
   434         }
       
   435     //remove any command
       
   436     for(TInt i = 0; i < iDataBuf.Count(); i++)
       
   437         {
       
   438         delete iDataBuf[i];
       
   439         }
       
   440     iDataBuf.Reset();    
       
   441     iSignalCodeArray.Reset();        
       
   442     //iDelayedCmd.Reset();
       
   443     iDelayedCmd = 0;
       
   444     }
       
   445 // ---------------------------------------------------------------------------
       
   446 // CPeninputServerSession::PanicClient
       
   447 // ---------------------------------------------------------------------------
       
   448 //
       
   449 void CPeninputServerSession::PanicClient(const RMessage2& aMessage,TInt aPanic)
       
   450     {
       
   451     aMessage.Panic(KPeninputServerName, aPanic);
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // CPeninputServerSession::~CPeninputServerSession
       
   456 // ---------------------------------------------------------------------------
       
   457 //
       
   458 CPeninputServerSession::~CPeninputServerSession()
       
   459     {
       
   460     ProcessPendingEventBeforeExit();
       
   461     iPenInputSvr.DecreaseSessionCount(this);
       
   462     iPenInputSvr.DelProcessLayouts( iClientProcess );
       
   463     iDataBuf.Close();
       
   464     iSignalCodeArray.Close();
       
   465     iCombinedEventBuf.Close();
       
   466     //iDelayedCmd.Close();
       
   467     delete iAppInfo;
       
   468     }
       
   469 
       
   470 // ---------------------------------------------------------------------------
       
   471 // CPeninputServerSession::ClientId
       
   472 // ---------------------------------------------------------------------------
       
   473 //
       
   474 TInt CPeninputServerSession::ClientId()
       
   475     {
       
   476     return iClientId;
       
   477     }
       
   478     
       
   479 void CPeninputServerSession::RemoveEventDataByIdx(TInt aIndex)
       
   480     {
       
   481     //remove all if aIndex < 0
       
   482     if( aIndex == KAllEventIndex )
       
   483         {
       
   484         for(TInt i = iDataBuf.Count() - 1; i >= 0; i--)
       
   485             {        
       
   486             HBufC* usedData = iDataBuf[i];
       
   487             iDataBuf.Remove(i);
       
   488             delete usedData;
       
   489             iSignalCodeArray.Remove(i);
       
   490             }
       
   491         }
       
   492     else
       
   493         {
       
   494         if( aIndex >= 0 && aIndex < iDataBuf.Count() ) 
       
   495             {
       
   496             HBufC* usedData = iDataBuf[aIndex];
       
   497             iDataBuf.Remove(aIndex);
       
   498             delete usedData;
       
   499             iSignalCodeArray.Remove(aIndex);
       
   500             }
       
   501         }        
       
   502     }
       
   503 
       
   504 //void CPeninputServerSession::AddDelayedCmd(TAnimationCommand aCmd)
       
   505 void CPeninputServerSession::AddDelayedCmd()
       
   506     {
       
   507     //iDelayedCmd.Append(aCmd);
       
   508     //iDelayedCmd++;
       
   509     }
       
   510     
       
   511 void CPeninputServerSession::RegisterPenUiType(TInt aType)
       
   512     {
       
   513     iRegistedPenUiType = aType;
       
   514     }
       
   515     
       
   516 TInt CPeninputServerSession::HandleAppInfoChange(const RMessage2& aMessage)
       
   517     {
       
   518     TInt ret = 0;
       
   519     TRAP_IGNORE(ret = HandleAppInfoChangeL(aMessage));	
       
   520     return ret;
       
   521     }
       
   522 
       
   523 TInt CPeninputServerSession::HandleAppInfoChangeL(const RMessage2& aMessage)
       
   524     {
       
   525     //TPeninputAppInfo type;
       
   526     if (aMessage.Ptr0() == NULL || aMessage.Ptr1() == NULL)
       
   527         {
       
   528         return EBadRequest;
       
   529         }
       
   530 
       
   531     TPckg<TPeninputAppInfo> typeData(iAppInfoType);            
       
   532     TInt rst = aMessage.Read(KMsgSlot0,typeData);
       
   533     if (rst != KErrNone)
       
   534         {
       
   535         return EBadRequest;
       
   536         }
       
   537     
       
   538     delete iAppInfo;
       
   539     iAppInfo = NULL;
       
   540     TInt len = aMessage.GetDesLength(KMsgSlot1);
       
   541     if (len < 0)
       
   542         {
       
   543         return EBadRequest;
       
   544         }
       
   545     iAppInfo = HBufC::NewL(len);
       
   546     TPtr appInfo = iAppInfo->Des();
       
   547     rst = aMessage.Read(KMsgSlot1,appInfo);
       
   548     if (rst != KErrNone)
       
   549         {
       
   550         return EBadRequest;
       
   551         }    
       
   552     return DoUpdateAppInfo();
       
   553     }
       
   554     
       
   555 TInt CPeninputServerSession::DoUpdateAppInfo()
       
   556     {
       
   557     return iPenInputSvr.HandleAppInfoChange(this,*iAppInfo,iAppInfoType);  
       
   558     }
       
   559     
       
   560     
       
   561 TInt CPeninputServerSession::DisabledUiType()
       
   562     {
       
   563     return iDisabledUiType;
       
   564     }
       
   565     
       
   566 //end of implementation of Class CPeninputServerSession
       
   567 
       
   568 // End of File