bluetoothengine/bthid/keyboard/src/keyboard.cpp
changeset 40 997690c3397a
parent 17 f05641c183ff
equal deleted inserted replaced
37:91746b151f97 40:997690c3397a
    21 #include <w32std.h>
    21 #include <w32std.h>
    22 #include <coedef.h>
    22 #include <coedef.h>
    23 #include <eiksvdef.h>
    23 #include <eiksvdef.h>
    24 #include <apgcli.h>
    24 #include <apgcli.h>
    25 #include <apgtask.h>
    25 #include <apgtask.h>
    26 #include <apacmdln.h>
       
    27 #include <e32property.h>
       
    28 
    26 
    29 #include "hidtranslate.h"
    27 #include "hidtranslate.h"
    30 #include "finder.h"
    28 #include "finder.h"
    31 #include "keyboard.h"
    29 #include "keyboard.h"
    32 #include "layoutmgr.h"
    30 #include "layoutmgr.h"
    50 // Key repeat parameters to be configured
    48 // Key repeat parameters to be configured
    51 const TInt KRepeatEndTimeout = 5000000; // 5 seconds
    49 const TInt KRepeatEndTimeout = 5000000; // 5 seconds
    52 
    50 
    53 const TInt KKeyRepeatDelay = 500000;
    51 const TInt KKeyRepeatDelay = 500000;
    54 const TInt KKeyRepeatInterval = 75000;
    52 const TInt KKeyRepeatInterval = 75000;
    55 _LIT(KAppName, "PaintCursor.exe");
       
    56 //----------------------------------------------------------------------------
    53 //----------------------------------------------------------------------------
    57 // CHidKeyboardDriver::CHidKeyboardDriver
    54 // CHidKeyboardDriver::CHidKeyboardDriver
    58 //----------------------------------------------------------------------------
    55 //----------------------------------------------------------------------------
    59 //
    56 //
    60 CHidKeyboardDriver::CHidKeyboardDriver(MDriverAccess* aGenericHid) :
    57 CHidKeyboardDriver::CHidKeyboardDriver(MDriverAccess* aGenericHid) :
   110     iRepeatEndTimer = CTimeOutTimer::NewL(EPriorityNormal, *this);
   107     iRepeatEndTimer = CTimeOutTimer::NewL(EPriorityNormal, *this);
   111     iAppMenuId = AppMenuId();
   108     iAppMenuId = AppMenuId();
   112     iPhoneAppId = PhoneAppId();
   109     iPhoneAppId = PhoneAppId();
   113     iIdleAppId = IdleAppId();
   110     iIdleAppId = IdleAppId();
   114 
   111 
   115     iComboDevice = EFalse;
       
   116 
       
   117     iSettings = CBtHidSettings::NewL();
   112     iSettings = CBtHidSettings::NewL();
   118     }
   113     }
   119 
   114 
   120 //----------------------------------------------------------------------------
   115 //----------------------------------------------------------------------------
   121 // CHidKeyboardDriver::~CHidKeyboardDriver
   116 // CHidKeyboardDriver::~CHidKeyboardDriver
   134     for (TInt i = 0; i < KNumInputFieldTypes; ++i)
   129     for (TInt i = 0; i < KNumInputFieldTypes; ++i)
   135         {
   130         {
   136         iKeys[i].Reset();
   131         iKeys[i].Reset();
   137         }
   132         }
   138 
   133 
   139     iPointBufQueue.Close();
       
   140 
       
   141     if (iComboDevice)
       
   142         {
       
   143         RProperty::Set( KPSUidBthidSrv, KBTMouseCursorState, ECursorNotInitialized );
       
   144         }
       
   145 
       
   146     if (iSettings)
   134     if (iSettings)
   147         delete iSettings;
   135         delete iSettings;
   148     }
   136     }
   149 
   137 
   150 void CHidKeyboardDriver::MoveCursor(const TPoint& aPoint)
       
   151     {
       
   152     DBG(RDebug::Print(
       
   153                     _L("CHidKeyboard::MoveCursor")));
       
   154 
       
   155     PostPointer(aPoint);
       
   156     }
       
   157 // ---------------------------------------------------------------------------
       
   158 // CHidMouseDriver::PostPointer
       
   159 // Save the event to the buffer
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 TInt CHidKeyboardDriver::PostPointer(const TPoint& aPoint)
       
   163     {
       
   164     DBG(RDebug::Print(_L("CHidKeyboard::PostPointer")));
       
   165     iPointerBuffer.iPoint[iPointerBuffer.iNum] = aPoint;
       
   166     iPointerBuffer.iType[iPointerBuffer.iNum] = KBufferPlainPointer;
       
   167     iPointerBuffer.iNum++;
       
   168     TInt ret = KErrNone;
       
   169 
       
   170     if (iPointerBuffer.iNum > KPMaxEvent)
       
   171         {
       
   172         ret = iPointBufQueue.Send(iPointerBuffer);
       
   173         iPointerBuffer.iNum = 0;
       
   174         }
       
   175     return ret;
       
   176     }
       
   177 
       
   178 TInt CHidKeyboardDriver::SendButtonEvent(TBool aButtonDown)
       
   179     {
       
   180     DBG(RDebug::Print(
       
   181                     _L("CHidKeyboard::SendButtonEvent")));
       
   182     iPointerBuffer.iPoint[iPointerBuffer.iNum] = TPoint(0, 0);
       
   183     iPointerBuffer.iType[iPointerBuffer.iNum] = aButtonDown
       
   184                                                             ? KBufferPenDown
       
   185                                                                : KBufferPenUp;
       
   186     iPointerBuffer.iNum++;
       
   187     TInt ret = iPointBufQueue.Send(iPointerBuffer);
       
   188     iPointerBuffer.iNum = 0;
       
   189     return ret;
       
   190     }
       
   191 //----------------------------------------------------------------------------
   138 //----------------------------------------------------------------------------
   192 // CHidKeyboardDriver:::StartL
   139 // CHidKeyboardDriver:::StartL
   193 //----------------------------------------------------------------------------
   140 //----------------------------------------------------------------------------
   194 //
   141 //
   195 void CHidKeyboardDriver::StartL(TInt aConnectionId)
   142 void CHidKeyboardDriver::StartL(TInt aConnectionId)
   231     if (sameCategory)
   178     if (sameCategory)
   232         {
   179         {
   233         //Used the layoutID from CenRep
   180         //Used the layoutID from CenRep
   234         iLayoutMgr.SetLayout(iLastSelectedLayout);
   181         iLayoutMgr.SetLayout(iLastSelectedLayout);
   235         }
   182         }
   236     TInt err = iPointBufQueue.OpenGlobal(KMsgBTMouseBufferQueue);   
       
   237     if (err == KErrNotFound)
       
   238         {
       
   239         User::LeaveIfError(iPointBufQueue.CreateGlobal(KMsgBTMouseBufferQueue, KPointQueueLen));    
       
   240         }
       
   241     else
       
   242         {
       
   243         User::LeaveIfError( err );
       
   244         }    
       
   245 
   183 
   246     // Ready to process keyboard events:
   184     // Ready to process keyboard events:
   247     iDriverState = EInitialised;
   185     iDriverState = EInitialised;
   248 
       
   249     if (iComboDevice)
       
   250         {
       
   251         LaunchApplicationL(KAppName);
       
   252         RProperty::Set( KPSUidBthidSrv, KBTMouseCursorState, ECursorShow );
       
   253         }
       
   254 
   186 
   255     }
   187     }
   256 
   188 
   257 //----------------------------------------------------------------------------
   189 //----------------------------------------------------------------------------
   258 // CHidKeyboardDriver::InitialiseL
   190 // CHidKeyboardDriver::InitialiseL
   284 //
   216 //
   285 void CHidKeyboardDriver::Stop()
   217 void CHidKeyboardDriver::Stop()
   286     {
   218     {
   287     iDriverState = EDisabled;
   219     iDriverState = EDisabled;
   288     CancelAllKeys();
   220     CancelAllKeys();
   289     if (iComboDevice)
       
   290         {
       
   291         RProperty::Set( KPSUidBthidSrv, KBTMouseCursorState, ECursorNotInitialized );
       
   292         }
       
   293     }
       
   294 
       
   295 //----------------------------------------------------------------------------
       
   296 // CHidMouseDriver::LaunchApplicationL
       
   297 //----------------------------------------------------------------------------
       
   298 //
       
   299 void CHidKeyboardDriver::LaunchApplicationL(const TDesC& aName)
       
   300     {
       
   301     //Check if application is already running in the background
       
   302     TApaTaskList tasks(iWsSession);
       
   303     TApaTask task = tasks.FindApp(aName);
       
   304     if (task.Exists())
       
   305         {
       
   306         // Application is active, so just bring to foreground
       
   307         }
       
   308     else
       
   309         {
       
   310         // If application is not running, then create a new one
       
   311         CApaCommandLine* cmd = CApaCommandLine::NewLC();
       
   312         cmd->SetExecutableNameL(aName);
       
   313         cmd->SetCommandL(EApaCommandBackground); // EApaCommandRun
       
   314 
       
   315         RApaLsSession arcSession;
       
   316         //connect to AppArc server
       
   317         User::LeaveIfError(arcSession.Connect());
       
   318         CleanupClosePushL(arcSession);
       
   319         User::LeaveIfError(arcSession.StartApp(*cmd));
       
   320         arcSession.Close();
       
   321         CleanupStack::PopAndDestroy(2);
       
   322         }
       
   323 
       
   324     }
   221     }
   325 
   222 
   326 // ----------------------------------------------------------------------
   223 // ----------------------------------------------------------------------
   327 // CHidDriver mandatory functions:
   224 // CHidDriver mandatory functions:
   328 
   225 
   337     switch (aChannel)
   234     switch (aChannel)
   338         {
   235         {
   339         case CHidTransport::EHidChannelInt:
   236         case CHidTransport::EHidChannelInt:
   340             if (EInitialised == iDriverState)
   237             if (EInitialised == iDriverState)
   341                 {
   238                 {
   342                 if (iComboDevice)
       
   343                     {
       
   344                     TInt mouseStatus;
       
   345                     TInt err = RProperty::Get( KPSUidBthidSrv, KBTMouseCursorState, mouseStatus );
       
   346                     if ( !err &&
       
   347                         ((static_cast<THidMouseCursorState>(mouseStatus) == ECursorRedraw)|| 
       
   348                          (static_cast<THidMouseCursorState>(mouseStatus) == ECursorReset)) )
       
   349                         {
       
   350                         err = RProperty::Set( KPSUidBthidSrv, KBTMouseCursorState, ECursorShow );
       
   351                         DBG(RDebug::Print(
       
   352                                  _L("[BTHID]\tCHidKeyboardDriver::DataIn() ECursorRedraw ||ECursorReset ")) );
       
   353                         }
       
   354 
       
   355                     CursorRedraw();
       
   356                     }
       
   357                 InterruptData(aPayload);
   239                 InterruptData(aPayload);
   358                 }
   240                 }
   359             break;
   241             break;
   360 
   242 
   361         case CHidTransport::EHidChannelCtrl:
   243         case CHidTransport::EHidChannelCtrl:
   384     {
   266     {
   385     RDebug::Print(_L("CHidKeyboardDriver::Disconnected(%d)"), aReason);
   267     RDebug::Print(_L("CHidKeyboardDriver::Disconnected(%d)"), aReason);
   386     Stop();
   268     Stop();
   387     }
   269     }
   388 
   270 
   389 void CHidKeyboardDriver::UpdateXY(TInt aFieldIndex, const TDesC8& aReport)
       
   390     {
       
   391     //    DBG(RDebug::Print(_L("[HID]\tCHidMouseDriver::UpdateModifiers()")));
       
   392 
       
   393     // Translate the HID usage values into a boot protocol style
       
   394     // modifier bitmask:
       
   395     //
       
   396     TReportTranslator report(aReport, iMouseField[aFieldIndex]);
       
   397 
       
   398     TInt Xvalue = 0;
       
   399     TInt Yvalue = 0;
       
   400 
       
   401     TInt errX = report.GetValue(Xvalue, EGenericDesktopUsageX);
       
   402     TInt errY = report.GetValue(Yvalue, EGenericDesktopUsageY);
       
   403 
       
   404     DBG(RDebug::Print(_L("[HID]\tCHidMouseDriver::UpdateXY (%d,%d)"), Xvalue, Yvalue));
       
   405     if ((Xvalue != 0) || (Yvalue != 0))
       
   406         {
       
   407         MoveCursor(TPoint(Xvalue, Yvalue));
       
   408         }
       
   409     }
       
   410 
       
   411 void CHidKeyboardDriver::UpdateWheel(TInt aFieldIndex, const TDesC8& aReport)
       
   412     {
       
   413     TReportTranslator report(aReport, iMouseField[aFieldIndex]);
       
   414 
       
   415     TInt Yvalue = 0;
       
   416 
       
   417     TInt errY = report.GetValue(Yvalue, EGenericDesktopUsageWheel);
       
   418     DBG(RDebug::Print(_L("[HID]\tCHidMouseDriver::UpdateWheel (%d)"), Yvalue));
       
   419     TInt absValue(Abs(Yvalue));
       
   420     if ((errY == KErrNone) && (absValue >= 1))
       
   421         {
       
   422         TRawEvent rawEvent;
       
   423         for (TInt ii = 0; ii < absValue; ii++)
       
   424             {
       
   425             rawEvent.Set(TRawEvent::EKeyDown,
       
   426                     (Yvalue > 0) ? EStdKeyUpArrow : EStdKeyDownArrow);
       
   427             UserSvr::AddEvent(rawEvent);
       
   428             rawEvent.Set(TRawEvent::EKeyUp,
       
   429                     (Yvalue > 0) ? EStdKeyUpArrow : EStdKeyDownArrow);
       
   430             UserSvr::AddEvent(rawEvent);
       
   431             }
       
   432         }
       
   433     DBG(RDebug::Print(_L("[HID]\t  new iModifiers = %02x"), iModifiers));
       
   434     }
       
   435 
       
   436 void CHidKeyboardDriver::UpdateButtons(TInt aFieldIndex,
       
   437         const TDesC8& aReport)
       
   438     {
       
   439     DBG(RDebug::Print(_L("[HID]\tCHidMouseDriver::UpdateButtons()")));
       
   440     // Translate the HID usage values into a boot protocol style
       
   441     // modifier bitmask:
       
   442     //
       
   443     const TInt KButton1 = 1;
       
   444     const TInt KButton2 = 2;
       
   445     const TInt KButton3 = 3;
       
   446 
       
   447     TBool buttonPressed(EFalse);
       
   448 
       
   449     DBG(RDebug::Print(_L("[HID]\tCHidMouseDriver::UpdateButtons() %d, %d, %d"),
       
   450                     iMouseField[aFieldIndex]->UsagePage(),
       
   451                     iMouseField[aFieldIndex]->UsageMin(),
       
   452                     iMouseField[aFieldIndex]->UsageMax()));
       
   453     (void) aFieldIndex;
       
   454     // Hack but works
       
   455     // We dont come here if the report is wrong?
       
   456     TInt buttonByte = aReport[1];
       
   457     if (KButton1 == buttonByte)
       
   458         {
       
   459         DBG(RDebug::Print(_L("[HID]\tCHidMouseDriver::UpdateButtons() Button1")));
       
   460         buttonPressed = ETrue;
       
   461         }
       
   462 
       
   463     if (KButton2 == buttonByte)
       
   464         {
       
   465         DBG(RDebug::Print(_L("[HID]\tCHidMouseDriver::UpdateButtons() Button2")));
       
   466         if (!iButton2Down)
       
   467             {
       
   468             iButton2Down = ETrue;
       
   469             TRawEvent rawEvent;
       
   470             rawEvent.Set(TRawEvent::EKeyDown, EStdKeyApplication0);
       
   471             CursorRedraw();
       
   472             UserSvr::AddEvent(rawEvent);
       
   473             }
       
   474         }
       
   475     else
       
   476         {
       
   477         if (iButton2Down)
       
   478             {
       
   479             iButton2Down = EFalse;
       
   480             TRawEvent rawEvent;
       
   481             rawEvent.Set(TRawEvent::EKeyUp, EStdKeyApplication0);
       
   482             CursorRedraw();
       
   483             UserSvr::AddEvent(rawEvent);
       
   484             }
       
   485         }
       
   486 
       
   487     if (KButton3 == buttonByte)
       
   488         {
       
   489         DBG(RDebug::Print(_L("[HID]\tCHidMouseDriver::UpdateButtons() Button3")));
       
   490         buttonPressed = ETrue;
       
   491         }
       
   492 
       
   493     if (buttonPressed)
       
   494         {
       
   495         if (!iButtonDown)
       
   496             {
       
   497             iButtonDown = ETrue;
       
   498             SendButtonEvent(ETrue);//Send Mouse Button Down
       
   499             }
       
   500         }
       
   501     else
       
   502         {
       
   503         if (iButtonDown)
       
   504             {
       
   505             iButtonDown = EFalse;
       
   506             SendButtonEvent(EFalse); //Send Mouse Button Up
       
   507             }
       
   508         }
       
   509     }
       
   510 //----------------------------------------------------------------------------
   271 //----------------------------------------------------------------------------
   511 // CHidKeyboardDriver::InterruptData
   272 // CHidKeyboardDriver::InterruptData
   512 //----------------------------------------------------------------------------
   273 //----------------------------------------------------------------------------
   513 //
   274 //
   514 void CHidKeyboardDriver::InterruptData(const TDesC8& aPayload)
   275 void CHidKeyboardDriver::InterruptData(const TDesC8& aPayload)
   529         DBG(RDebug::Print(
   290         DBG(RDebug::Print(
   530                         _L("[HID]\tCHidKeyboardDriver::InterruptData()  report[%d] =  %d"),
   291                         _L("[HID]\tCHidKeyboardDriver::InterruptData()  report[%d] =  %d"),
   531                         ii, nextByte));
   292                         ii, nextByte));
   532         }
   293         }
   533 #endif
   294 #endif
   534     TBool mouseEvent(EFalse);
       
   535     if (iMouseField[EMouseXY] && iMouseField[EMouseXY]->IsInReport(firstByte))
       
   536         {
       
   537         UpdateXY(EMouseXY, aPayload);
       
   538         mouseEvent = ETrue;
       
   539         }
       
   540 
       
   541     if (iMouseField[EMouseButtons] && iMouseField[EMouseButtons]->IsInReport(
       
   542             firstByte))
       
   543         {
       
   544         UpdateButtons(EMouseButtons, aPayload);
       
   545         mouseEvent = ETrue;
       
   546         }
       
   547     if (iMouseField[EMouseXY] && iMouseField[EMouseXY]->IsInReport(firstByte))
       
   548         {
       
   549         UpdateWheel(EMouseWheel, aPayload);
       
   550         mouseEvent = ETrue;
       
   551         }
       
   552     DBG(RDebug::Print(_L("[HID]\tCHidKeyboardDriver::InterruptData()  mouseevent %d"),
       
   553                     mouseEvent));
       
   554     if (mouseEvent)
       
   555         {
       
   556         return;
       
   557         }
       
   558     // First check for any rollover errors:
   295     // First check for any rollover errors:
   559     //
   296     //
   560     TInt i;
   297     TInt i;
   561     for (i = 0; i < KNumInputFieldTypes; ++i)
   298     for (i = 0; i < KNumInputFieldTypes; ++i)
   562         {
   299         {
  1521 
  1258 
  1522     if ((iField[EStandardKeys] != 0) && (iField[EModifierKeys] != 0))
  1259     if ((iField[EStandardKeys] != 0) && (iField[EModifierKeys] != 0))
  1523         {
  1260         {
  1524         valid = KErrNone;
  1261         valid = KErrNone;
  1525         }
  1262         }
  1526 
       
  1527     TMouseFinder mousefinder;
       
  1528     search.SearchL(aReportRoot, &mousefinder);
       
  1529     iMouseField[EMouseButtons] = mousefinder.ButtonsField();
       
  1530     iMouseField[EMouseXY] = mousefinder.XYField();
       
  1531     iMouseField[EMouseWheel] = mousefinder.WheelField();
       
  1532 
       
  1533     if ((iMouseField[EMouseButtons] != 0) && (iMouseField[EMouseXY] != 0))
       
  1534         {
       
  1535         iComboDevice = ETrue;
       
  1536         }
       
  1537 
       
  1538         TRACE_INFO( (
  1263         TRACE_INFO( (
  1539                         _L("CHidKeyboard::CanHandleReport() returning %d"), valid));
  1264                         _L("CHidKeyboard::CanHandleReport() returning %d"), valid));
  1540 
  1265 
  1541     return valid;
  1266     return valid;
  1542     }
  1267     }
  1935     {
  1660     {
  1936     DBG(RDebug::Print(_L("[HID]\tCHidKeyboardDriver::TimerExpired, cancel all key presses")));
  1661     DBG(RDebug::Print(_L("[HID]\tCHidKeyboardDriver::TimerExpired, cancel all key presses")));
  1937     CancelAllKeys();
  1662     CancelAllKeys();
  1938     }
  1663     }
  1939 
  1664 
  1940 void CHidKeyboardDriver::CursorRedraw()
       
  1941     {
       
  1942     TInt mouseStatus;
       
  1943 
       
  1944     TInt err = RProperty::Get( KPSUidBthidSrv, KBTMouseCursorState, mouseStatus );
       
  1945     if ( !err )
       
  1946         {
       
  1947         err = RProperty::Set( KPSUidBthidSrv, KBTMouseCursorState, ECursorRedraw );
       
  1948         DBG(RDebug::Print(
       
  1949              _L("[BTHID]\tCHidKeyboardDriver::CursorRedraw() X->ECursorRedraw") ) );
       
  1950         }
       
  1951     }
       
  1952 
       
  1953 // ----------------------------------------------------------------------
  1665 // ----------------------------------------------------------------------
  1954 // End of file
  1666 // End of file