mediakeys/KeyPublisherPlugin/src/KeyPublisher.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *       Animation plugin implementation file
       
    16 *       
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <e32std.h>
       
    23 #include <e32cmn.h>
       
    24 #include <mediakeysinternalpskeys.h>
       
    25 #include <mmkeybearerscancodes.h>
       
    26 #include <remconcoreapi.h>
       
    27 #include <uikon.hrh>
       
    28 
       
    29 #include "KeyPublisher.h"
       
    30 
       
    31 // CONSTANTS
       
    32 
       
    33 const TInt KScanCodeMask = 0xFFFF;
       
    34 
       
    35 #if defined(__WINS__)
       
    36 const TInt KVolumeUpKeyEmulator   = 0xAC;
       
    37 const TInt KVolumeDownKeyEmulator = 0xAD;
       
    38 #endif
       
    39 // Change these values once peoblem with keys is fixed
       
    40 const TInt KVolumeUpKeyHardware   = EStdKeyIncVolume;   //0xa3; 
       
    41 const TInt KVolumeDownKeyHardware = EStdKeyDecVolume;  //0xa2; 
       
    42 
       
    43 const TInt KMusicPlayHarware     = EStdKeyApplication2;
       
    44 const TInt KMusicStopHarware     = EStdKeyApplication3;
       
    45 const TInt KMusicForwardHardware = EStdKeyApplication4;
       
    46 const TInt KMusicRewindHardware  = EStdKeyApplication5;
       
    47 
       
    48 // Scan codes in this table are blocked by the Key Publisher Anim plugin
       
    49 // Table KBlockedKeyCodes[] and KBlockedKeyCodesEvents[][3] must have same 
       
    50 // number of rows and match correspondingly
       
    51 const TInt KBlockedKeyCodes[]=
       
    52     {
       
    53 #if defined(__WINS__)    
       
    54     /*0*/KVolumeUpKeyEmulator,
       
    55     /*1*/KVolumeDownKeyEmulator,
       
    56 #endif
       
    57     /*2*/KVolumeUpKeyHardware,
       
    58     /*3*/KVolumeDownKeyHardware,
       
    59 
       
    60     /*4*/ KMusicPlayHarware,    
       
    61     /*5*/ KMusicStopHarware,    
       
    62     /*6*/ KMusicForwardHardware,
       
    63     /*7*/ KMusicRewindHardware 
       
    64 
       
    65     };
       
    66 const TInt KBlockedKeyCodesEvents[][3]=
       
    67     {     /* Press - 0 */       /* Release - 1 */      /* Click - 2 */
       
    68 #if defined(__WINS__)    
       
    69     /*0*/{KPSVolumeUpPressed,   KPSVolumeUpReleased,   KPSVolumeUpClicked },
       
    70     /*1*/{KPSVolumeDownPressed, KPSVolumeDownReleased, KPSVolumeDownClicked },
       
    71 #endif
       
    72     /*2*/{KPSVolumeUpPressed,   KPSVolumeUpReleased,   KPSVolumeUpClicked },
       
    73     /*3*/{KPSVolumeDownPressed, KPSVolumeDownReleased, KPSVolumeDownClicked }, 
       
    74 
       
    75     /*4*/{EPlayPressed,         EPlayReleased,         EPlayClicked },
       
    76     /*5*/{EStopPressed,         EStopReleased,         EStopClicked },
       
    77     /*6*/{EFastForwardPressed,  EFastForwardReleased,  EForwardClicked },
       
    78     /*7*/{ERewindPressed,       ERewindReleased,       EBackwardClicked }
       
    79 
       
    80     };
       
    81 
       
    82 const TInt KBlockedKeyCodeTableSize = sizeof(KBlockedKeyCodes) / sizeof(TInt);
       
    83 
       
    84 const TInt KTimerMilliSecond = 1000; // in micro seconds
       
    85 
       
    86 _LIT_SECURITY_POLICY_C1(KKeyPublisherReadPolicy,  ECapability_None);
       
    87 _LIT_SECURITY_POLICY_C1(KKeyPublisherWritePolicy, ECapabilityWriteDeviceData);
       
    88 
       
    89 // Following constants can subject change if duration is not suitable
       
    90 
       
    91 const TInt KFirstTimerExpiryInterval = 600 * KTimerMilliSecond; // 600mSec
       
    92 // For second time onwards ,the duration of the time interval, is below
       
    93 // Should not be grater than 1 Minute
       
    94 const TInt KTimerExpiryInterval      = 500 * KTimerMilliSecond; // 500mSec
       
    95 
       
    96 // -----------------------------------------------------------------------
       
    97 // CKeyPublisher::CKeyPublisher
       
    98 // Description: Constuctor
       
    99 // -----------------------------------------------------------------------
       
   100 //
       
   101 CKeyPublisher::CKeyPublisher()
       
   102 : iTimerStarted (EFalse) ,iPressEventSent(EFalse) ,iReleaseEventSent(ETrue),
       
   103   iTimerCount(0), iConnected(EFalse)
       
   104     {
       
   105     }
       
   106 // -----------------------------------------------------------------------
       
   107 // CKeyPublisher::~CKeyPublisher
       
   108 // Description: Destructor
       
   109 // -----------------------------------------------------------------------
       
   110 //
       
   111 CKeyPublisher::~CKeyPublisher()
       
   112     {
       
   113     
       
   114     iSoundSession.Close();
       
   115     
       
   116     if (iFunctions)
       
   117         {
       
   118         //switch OFF the animation raw event receiving 
       
   119         iFunctions->GetRawEvents(EFalse);
       
   120         }
       
   121     if(iTimer)
       
   122         {
       
   123         delete iTimer;
       
   124         }
       
   125     }
       
   126 // -----------------------------------------------------------------------
       
   127 // CKeyPublisher::ConstructL   (part of CWindowAnim)
       
   128 // Description: Enable the Raw Event handling from Win server. and 
       
   129 //              create timer (not starting)
       
   130 // -----------------------------------------------------------------------
       
   131 //
       
   132 void CKeyPublisher::ConstructL(TAny* /*aArgs*/, TBool /*aHasFocus*/)
       
   133     {
       
   134     
       
   135     /* iFunction: protected member of CAnim /CWindowAnim
       
   136      * Pointer to a class containing functions implemented by the window 
       
   137      * server. These are available to any CAnim derived class. Note that 
       
   138      * this value is automatically set for you by the animation framework. 
       
   139      * we do not need to assign a value to this pointer.
       
   140      */
       
   141     
       
   142     /* This function switches animation raw event handling on and off. If 
       
   143      * raw event handling is switched on(ETrue), then raw events, 
       
   144      * e.g. pen events, key presses, power events etc., are all offered 
       
   145      * to the animation event handling code - MEventHandler::OfferRawEventL().
       
   146      */ 
       
   147     iFunctions->GetRawEvents(ETrue);
       
   148     
       
   149     TInt error = iSoundSession.Connect();
       
   150     if (error == KErrNone)
       
   151         {
       
   152         iConnected = ETrue;
       
   153 		}
       
   154     
       
   155     // Timer for implementing Pressed/Released/Clicked feature.
       
   156     iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
   157     
       
   158     //Define the P&S for Volume keys and Media Keys
       
   159     RProperty::Define(KPSUidMediaKeysEventNotifier, KMediaKeysVolumeKeyEvent, 
       
   160            RProperty::EInt, KKeyPublisherReadPolicy, KKeyPublisherWritePolicy);
       
   161     RProperty::Define(KPSUidMediaKeysEventNotifier, KMediaKeysControlKeyEvent,
       
   162            RProperty::EInt, KKeyPublisherReadPolicy, KKeyPublisherWritePolicy);
       
   163     // Define Vol Sync Notifier in P & S
       
   164     RProperty::Define(KPSUidMediaKeysEventNotifier,KMediaKeysCurrentVolume, 
       
   165            RProperty::EInt, KKeyPublisherReadPolicy, KKeyPublisherWritePolicy);
       
   166     // Define P&S Key for Volume keys from Accessory device
       
   167     RProperty::Define(KPSUidMediaKeysEventNotifier,KMediaKeysAccessoryVolumeEvent, 
       
   168            RProperty::EInt, KKeyPublisherReadPolicy, KKeyPublisherWritePolicy);
       
   169     }
       
   170 // -----------------------------------------------------------------------
       
   171 // CKeyPublisher::IsBlockedKeyCode 
       
   172 // Description: Search the list of blocked keys by comparing scan codes
       
   173 // return : ETrue if key is blocked, else EFalse
       
   174 // -----------------------------------------------------------------------
       
   175 //
       
   176 TBool CKeyPublisher::IsBlockedKeyCode(TInt aScanCode)
       
   177     {
       
   178     for (TInt index=0; index < KBlockedKeyCodeTableSize; index++)
       
   179         {
       
   180         if (aScanCode == KBlockedKeyCodes[index])
       
   181             {
       
   182             // Found the Key 
       
   183             return ETrue;
       
   184             }
       
   185         }
       
   186     // Not found
       
   187     return EFalse;
       
   188     }
       
   189 // -----------------------------------------------------------------------
       
   190 // CKeyPublisher::GetEnumVal
       
   191 // Description: Based on the scancode and event, find the Enum value(which
       
   192 //              are declared in the TRemConCoreApiButtonAction).
       
   193 //              MMBearerPlugin will decript this enum value.
       
   194 // return : 
       
   195 // -----------------------------------------------------------------------
       
   196 //
       
   197 TInt CKeyPublisher::GetEnumVal(TInt aScanCode, 
       
   198                                TRemConCoreApiButtonAction aEvent)
       
   199     {
       
   200     TInt result = KPSVolumeNoKey;
       
   201     TInt index;
       
   202 
       
   203     // Find the Index of the Scan code in the KBlockedKeyCodes[] 
       
   204     // and use the same index in KBlockedKeyCodesEvents[][] 
       
   205     // for getting enum values
       
   206     for (index=0; index < KBlockedKeyCodeTableSize; index++)
       
   207         {
       
   208         if (aScanCode == KBlockedKeyCodes[index])
       
   209             {
       
   210             break;
       
   211             }
       
   212         }
       
   213 
       
   214     if (index == KBlockedKeyCodeTableSize)
       
   215     {
       
   216         // Not found 
       
   217         return result;
       
   218     }
       
   219 
       
   220     result = KBlockedKeyCodesEvents[index][aEvent];
       
   221     return result;
       
   222     }
       
   223 // -----------------------------------------------------------------------
       
   224 // CKeyPublisher::PublishEvent
       
   225 // Description: Publish the event to P&S based on the Scancode, event
       
   226 // return : 
       
   227 // -----------------------------------------------------------------------
       
   228 //
       
   229 void CKeyPublisher::PublishEvent(TInt aScanCode, 
       
   230                                  TRemConCoreApiButtonAction aEvent)
       
   231     {
       
   232     TInt uidMMKey;
       
   233     
       
   234     if ( 
       
   235 #if defined(__WINS__)        
       
   236          (aScanCode == KVolumeUpKeyEmulator)   ||
       
   237          (aScanCode == KVolumeDownKeyEmulator) ||
       
   238 #endif         
       
   239          (aScanCode == KVolumeUpKeyHardware)   ||
       
   240          (aScanCode == KVolumeDownKeyHardware)  
       
   241        )
       
   242         {
       
   243         uidMMKey = KMediaKeysVolumeKeyEvent; // Volume Keys - Up / down
       
   244         }
       
   245     else
       
   246         {
       
   247         uidMMKey = KMediaKeysControlKeyEvent; //Media Keys - play ,stop ,etc
       
   248         }
       
   249 
       
   250     iProperty.Set(KPSUidMediaKeysEventNotifier,
       
   251                   uidMMKey,
       
   252                   GetEnumVal(aScanCode, aEvent));
       
   253 
       
   254     }   
       
   255     
       
   256 // -----------------------------------------------------------------------
       
   257 // CKeyPublisher::OfferRawEvent
       
   258 // Description: This function starts getting called when animation raw 
       
   259 //              event is switched on by calling GetRawEvents(ETrue)
       
   260 // return : ETrue  - if key is handled and blocks it to applications
       
   261 //          EFalse - if not handled here, Apps/other anim plugins will 
       
   262 //               receive this key event.
       
   263 // -----------------------------------------------------------------------
       
   264 //
       
   265 TBool CKeyPublisher::OfferRawEvent(const TRawEvent &aRawEvent)
       
   266     {
       
   267     TBool returnValue = EFalse;        // Key not blocked
       
   268     TInt scan;
       
   269     
       
   270     switch (aRawEvent.Type())
       
   271         {
       
   272         case TRawEvent::EKeyDown:
       
   273             {
       
   274             scan = aRawEvent.ScanCode() & KScanCodeMask;
       
   275             
       
   276             if (IsBlockedKeyCode(scan))
       
   277                 {
       
   278                 if (iConnected)
       
   279                     {
       
   280                     iSoundSession.KeyPressed(scan);  	
       
   281                     }
       
   282                 
       
   283                 if ((! iTimerStarted) && (iReleaseEventSent))
       
   284                     {
       
   285                     
       
   286                     if ((scan != KMusicForwardHardware) && (scan != KMusicRewindHardware))
       
   287                     {
       
   288 	                    // Send Clicked Event
       
   289 	                    PublishEvent(scan,ERemConCoreApiButtonClick);
       
   290                     }
       
   291 
       
   292                     //Start Timer
       
   293                     iTimer->Start(KFirstTimerExpiryInterval,
       
   294                                   KTimerExpiryInterval, 
       
   295                                   TCallBack(TimerCallback, this));
       
   296                     iTimerStarted  = ETrue;
       
   297                     iPrevScanCode    = scan;
       
   298                     iPressEventSent = EFalse;
       
   299                     returnValue     = ETrue; // Block   
       
   300                     iReleaseEventSent = EFalse; // For blocking multiple key
       
   301                                                 // presses
       
   302                     }
       
   303                 }
       
   304             }
       
   305             break;
       
   306         case TRawEvent::EKeyUp:
       
   307             {
       
   308             scan = aRawEvent.ScanCode() & KScanCodeMask;
       
   309 
       
   310             if ((iPrevScanCode == scan) && IsBlockedKeyCode(scan))
       
   311                 { 
       
   312             
       
   313                 // Clicked Scenario
       
   314                 if ( iPressEventSent == EFalse ) 
       
   315                     {
       
   316                     
       
   317                     if ((iPrevScanCode == KMusicForwardHardware) ||  
       
   318                         (iPrevScanCode == KMusicRewindHardware))
       
   319                     {
       
   320 	                    // Send Clicked Event
       
   321 	                    PublishEvent(iPrevScanCode, ERemConCoreApiButtonClick);
       
   322                     }
       
   323                     
       
   324                     // Since Click event has been sent when pressed down,
       
   325                     // (except Frwd & Rewind)
       
   326                     // Just stop the timer
       
   327                     if (iTimerStarted)
       
   328                         {
       
   329                         iTimer->Cancel();
       
   330                         iTimerStarted  = EFalse;
       
   331                         }
       
   332                     iReleaseEventSent = ETrue;
       
   333                     iPrevScanCode     = 0;
       
   334                     returnValue       = ETrue; //Block it
       
   335                     }
       
   336                 else  // Released Scenario
       
   337                     {
       
   338                     PublishEvent(scan,ERemConCoreApiButtonRelease);
       
   339 
       
   340                     iPrevScanCode   = 0;
       
   341                     iPressEventSent = EFalse;
       
   342                     returnValue     = ETrue; //Block it
       
   343                     iReleaseEventSent = ETrue;
       
   344 
       
   345                     }
       
   346                     
       
   347                 } // end of if ((iPrevScanCode == scan) && IsBlockedK...
       
   348             }
       
   349             break;
       
   350         default:
       
   351             break;
       
   352         }
       
   353     return returnValue; // to allow the event to be passed back to app
       
   354     }
       
   355 // -----------------------------------------------------------------------
       
   356 // CKeyPublisher::HandleTimerEvent
       
   357 // Description: When Timer is expired, this function is called from 
       
   358 //              callback function. If the timer is expired for the first
       
   359 //              time then KeyPressed event is published to P&S. And 
       
   360 //              second and further expires, only count is incremented.
       
   361 //              Once the count reaches some predefined value, Released 
       
   362 //              Key event is posted to P&S. and timer is switched off.
       
   363 // return : 
       
   364 // -----------------------------------------------------------------------
       
   365 //
       
   366 void CKeyPublisher::HandleTimerEvent()
       
   367     {
       
   368     
       
   369     // Send Pressed Key Event
       
   370     PublishEvent(iPrevScanCode,ERemConCoreApiButtonPress);  
       
   371     iPressEventSent = ETrue;
       
   372     iTimer->Cancel();
       
   373     iTimerStarted  = EFalse;
       
   374 
       
   375     }
       
   376 
       
   377 // -----------------------------------------------------------------------
       
   378 // CKeyPublisher::TimerCallback
       
   379 // Description: When Timer is expired, this function is called 
       
   380 // return : 
       
   381 // -----------------------------------------------------------------------
       
   382 //
       
   383 TInt CKeyPublisher::TimerCallback(TAny* aThis)
       
   384     {
       
   385     
       
   386     //STATIC_CAST(CKeyPublisher*,aThis)->HandleTimerEvent();
       
   387     static_cast<CKeyPublisher*>(aThis)->HandleTimerEvent();
       
   388     return 0;
       
   389     
       
   390     }
       
   391 
       
   392 // -----------------------------------------------------------------------
       
   393 // CKeyPublisher::Redraw   (part of CWindowAnim - pure virtual function) 
       
   394 // Description: 
       
   395 // -----------------------------------------------------------------------
       
   396 //
       
   397 void CKeyPublisher::Redraw()
       
   398     {
       
   399     //Pass
       
   400     }
       
   401 // -----------------------------------------------------------------------
       
   402 // CKeyPublisher::FocusChanged   (part of CWindowAnim - pure virtual function)
       
   403 // Description: 
       
   404 // -----------------------------------------------------------------------
       
   405 //
       
   406 void CKeyPublisher::FocusChanged(TBool /*aState*/)
       
   407     {
       
   408     //Pass
       
   409     }
       
   410 // -----------------------------------------------------------------------
       
   411 // CKeyPublisher::CommandReplyL  (part of CAnim - pure virtual function)
       
   412 // Description: 
       
   413 // -----------------------------------------------------------------------
       
   414 //
       
   415 TInt CKeyPublisher::CommandReplyL(TInt /*aOpcode*/, TAny* /*aArgs*/)
       
   416     {
       
   417     //Pass
       
   418     return KErrNone;
       
   419     }
       
   420 // -----------------------------------------------------------------------
       
   421 // CKeyPublisher::Command  (part of CAnim - pure virtual function)
       
   422 // Description: 
       
   423 // -----------------------------------------------------------------------
       
   424 //
       
   425 void CKeyPublisher::Command(TInt /*aOpcode*/, TAny* /*aArgs*/)
       
   426     {
       
   427     //Pass
       
   428     }
       
   429 // -----------------------------------------------------------------------
       
   430 // CKeyPublisher::Animate  (part of CAnim - pure virtual function)
       
   431 // Description: 
       
   432 // -----------------------------------------------------------------------
       
   433 //
       
   434 void CKeyPublisher::Animate(TDateTime* /*aDateTime*/)
       
   435     {
       
   436     //Pass
       
   437     }
       
   438 
       
   439 // End of File