navienginebsp/ne1_tb/specific/keyboard_interrupt.cpp
changeset 0 5de814552237
equal deleted inserted replaced
-1:000000000000 0:5de814552237
       
     1 /*
       
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 * ne1_tb\specific\keyboard.cpp
       
    16 * Access to NE1_TBVariant interruptible keyboard
       
    17 * The code here implements a simple interrupt-driven keyboard driver.
       
    18 * This is an alternative to the polled driver in keyboard.cpp.
       
    19 * This example assumes that we have an intelligent keyboard controller
       
    20 * which:
       
    21 * scans the keyboard automatically
       
    22 * generates an interrupt when a key is pressed or released
       
    23 * You can use this code as a starting point and modify it to suit
       
    24 * your hardware.
       
    25 *
       
    26 */
       
    27 
       
    28 
       
    29 
       
    30 #include <naviengine.h>
       
    31 #include <naviengine_priv.h>
       
    32 #include "iolines.h"
       
    33 #include "platform.h"
       
    34 #include <kernel/kpower.h>
       
    35 #include <e32keys.h>
       
    36 //
       
    37 //
       
    38 // TO DO: (optional)
       
    39 //
       
    40 // Modify this conversion table to suit your keyboard layout
       
    41 //
       
    42 // This example assumes that the following keyboard layout:
       
    43 //
       
    44 // <----Row0-----><-----Row1-------><---Row2-----><-----Row3-------><-------Row4-----------><------Row5------><-----Row6-------><---Row7--->
       
    45 //		LAlt																																Column0
       
    46 //		'				\				TAB				Z					A						X										Column1
       
    47 //						LShift																												Column2
       
    48 //		LCtrl																																Column3
       
    49 //		Fn																																	Column4
       
    50 //		Esc				Del				Q				CapLk				S						C				3						Column5
       
    51 //		1								W									D						V				4						Column6
       
    52 //		2				T				E									F						B				5						Column7
       
    53 //		9				Y				R				K					G						N				6						Column8
       
    54 //		0				U				O				L					H						M				7						Column9
       
    55 //		-				I				P				;					J						,				8						Column10
       
    56 //		=				Enter			[				'					/						.				Prog					Column11
       
    57 //						RShift																												Column12
       
    58 //		BkSp			DnArrow			]				UpArrow				LeftArrow				Space			RightArrow				Column13
       
    59 //																																			Column14
       
    60 //																																			Column15
       
    61 // EXAMPLE ONLY
       
    62 const TUint8 convertCode[] =
       
    63 	{
       
    64 	EStdKeyNull ,   EStdKeyLeftAlt  ,  EStdKeyNull   ,   EStdKeyNull    ,      EStdKeyNull      ,   EStdKeyNull     ,    EStdKeyNull    ,EStdKeyNull,       // Column0
       
    65 	EStdKeyNull ,   EStdKeyHash     ,EStdKeyBackSlash,   EStdKeyTab     ,          'Z'          ,       'A'         ,       'X'         ,EStdKeyNull,       // Column1
       
    66 	EStdKeyNull ,    EStdKeyNull    ,EStdKeyLeftShift,   EStdKeyNull    ,      EStdKeyNull      ,   EStdKeyNull     ,    EStdKeyNull    ,EStdKeyNull,       // Column2
       
    67 	EStdKeyNull ,  EStdKeyLeftCtrl  ,  EStdKeyNull   ,   EStdKeyNull    ,      EStdKeyNull      ,   EStdKeyNull     ,    EStdKeyNull    ,EStdKeyNull,       // Column3
       
    68 	EStdKeyNull ,  EStdKeyLeftFunc  ,  EStdKeyNull   ,   EStdKeyNull    ,      EStdKeyNull      ,   EStdKeyNull     ,    EStdKeyNull    ,EStdKeyNull,       // Column4
       
    69 	EStdKeyNull ,   EStdKeyEscape   , EStdKeyDelete  ,      'Q'         ,      EStdKeyCapsLock  ,       'S'         ,        'C'        ,   '3'     ,       // Column5
       
    70 	EStdKeyNull ,       '1'         ,  EStdKeyNull   ,      'W'         ,      EStdKeyNull      ,       'D'         ,        'V'        ,   '4'     ,       // Column6
       
    71 	EStdKeyNull ,       '2'         ,     'T'        ,      'E'         ,      EStdKeyNull      ,       'F'         ,        'B'        ,   '5'     ,       // Column7
       
    72 	EStdKeyNull ,       '9'         ,     'Y'        ,      'R'         ,          'K'          ,       'G'         ,        'N'        ,   '6'     ,       // Column8
       
    73 	EStdKeyNull ,       '0'         ,     'U'        ,      'O'         ,          'L'          ,       'H'         ,        'M'        ,   '7'     ,       // Column9
       
    74 	EStdKeyNull ,    EStdKeyMinus   ,     'I'        ,      'P'         ,    EStdKeySemiColon   ,       'J'         ,    EStdKeyComma   ,   '8'     ,       // Column10
       
    75 	EStdKeyNull ,    EStdKeyEquals  ,  EStdKeyEnter  ,EStdKeySquareBracketLeft,EStdKeySingleQuote,EStdKeyForwardSlash, EStdKeyFullStop  ,EStdKeyMenu,       // Column11
       
    76 	EStdKeyNull ,    EStdKeyNull    ,EStdKeyRightShift,   EStdKeyNull   ,      EStdKeyNull      ,   EStdKeyNull     ,    EStdKeyNull    ,EStdKeyNull,       // Column12
       
    77 	EStdKeyNull ,  EStdKeyBackspace ,EStdKeyDownArrow,EStdKeySquareBracketRight,EStdKeyUpArrow  , EStdKeyLeftArrow  ,    EStdKeySpace   ,EStdKeyRightArrow, // Column13
       
    78 	EStdKeyNull ,    EStdKeyNull    ,  EStdKeyNull   ,   EStdKeyNull    ,      EStdKeyNull      ,   EStdKeyNull     ,    EStdKeyNull    ,EStdKeyNull,       // Column14
       
    79 	EStdKeyNull ,    EStdKeyNull    ,  EStdKeyNull   ,   EStdKeyNull    ,      EStdKeyNull      ,   EStdKeyNull     ,    EStdKeyNull    ,EStdKeyNull        // Column15
       
    80 	};
       
    81 
       
    82 // EXAMPLE ONLY
       
    83 const TInt KFlagKeyPressed = 0x80;		// As an example, we'll assume the top bit indicates pressed/released
       
    84 
       
    85 // EXAMPLE ONLY
       
    86 const TKeyboard	KConfigKeyboardType = EKeyboard_Full;
       
    87 const TInt KConfigKeyboardDeviceKeys = 0;
       
    88 const TInt KConfigKeyboardAppsKeys = 0;
       
    89 //
       
    90 //
       
    91 _LIT(KLitKeyboard,"Keyboard");
       
    92 
       
    93 class DKeyboardNE1_TB : public DPowerHandler
       
    94 	{
       
    95 public:
       
    96 	DKeyboardNE1_TB();
       
    97 	TInt Create();
       
    98 public: // from DPowerHandler
       
    99 	void PowerUp();
       
   100 	void PowerDown(TPowerState);
       
   101 	TInt HalFunction(TInt aFunction, TAny* a1, TAny* a2);
       
   102 	void KeyboardInfo(TKeyboardInfoV01& aInfo);
       
   103 	void KeyboardOn();
       
   104 	void KeyboardOff();
       
   105 	void HandleMsg(TMessageBase* aMsg);
       
   106 private:
       
   107 	static void Isr(TAny* aPtr);
       
   108 	static void EventDfcFn(TAny* aPtr);
       
   109 	void EventDfc();
       
   110 
       
   111 	void PowerUpDfc();
       
   112 	static void PowerUpDfcFn(TAny* aPtr);
       
   113 	void PowerDownDfc();
       
   114 	static void PowerDownDfcFn(TAny* aPtr);
       
   115 private:
       
   116 	void KeyboardPowerUp();
       
   117 	TUint GetKeyCode();
       
   118 	TBool IsKeyReady();
       
   119 public:
       
   120 	TDfcQue* iDfcQ;
       
   121 	TMessageQue iMsgQ;	
       
   122 	TDfc iPowerUpDfc;
       
   123 	TDfc iPowerDownDfc;	
       
   124 private:
       
   125 	TDfc iEventDfc;
       
   126 	TBool iKeyboardOn;
       
   127 	TInt iIrqHandle;
       
   128 	};
       
   129 
       
   130 LOCAL_C TInt halFunction(TAny* aPtr, TInt aFunction, TAny* a1, TAny* a2)
       
   131 	{
       
   132 	DKeyboardNE1_TB* pH=(DKeyboardNE1_TB*)aPtr;
       
   133 	return pH->HalFunction(aFunction,a1,a2);
       
   134 	}
       
   135 
       
   136 void rxMsg(TAny* aPtr)
       
   137 	{
       
   138 	DKeyboardNE1_TB& h=*(DKeyboardNE1_TB*)aPtr;
       
   139 	TMessageBase* pM=h.iMsgQ.iMessage;
       
   140 	if (pM)
       
   141 		h.HandleMsg(pM);
       
   142 	}
       
   143 
       
   144 //
       
   145 //	Keyboard class
       
   146 //
       
   147 DKeyboardNE1_TB::DKeyboardNE1_TB()
       
   148 	:	DPowerHandler(KLitKeyboard), 
       
   149 		iMsgQ(rxMsg,this,NULL,1),
       
   150 		iPowerUpDfc(PowerUpDfcFn,this,6),
       
   151 		iPowerDownDfc(PowerDownDfcFn,this,7),
       
   152 		iEventDfc(EventDfcFn,this,1),
       
   153 		iIrqHandle(-1)
       
   154 	{
       
   155 	}
       
   156 
       
   157 TInt DKeyboardNE1_TB::Create()
       
   158 //
       
   159 // Initialisation. Bind and enable the interrupt.
       
   160 //
       
   161 	{
       
   162 	iDfcQ=Kern::DfcQue0();
       
   163 
       
   164 	iKeyboardOn = EFalse;	
       
   165 		// install the HAL function
       
   166 	TInt r=Kern::AddHalEntry(EHalGroupKeyboard,halFunction,this);
       
   167 	if (r!=KErrNone)
       
   168 		return r;
       
   169 
       
   170 	iEventDfc.SetDfcQ(iDfcQ);
       
   171 	iPowerUpDfc.SetDfcQ(iDfcQ);
       
   172 	iPowerDownDfc.SetDfcQ(iDfcQ);
       
   173 	iMsgQ.SetDfcQ(iDfcQ);
       
   174 	iMsgQ.Receive();
       
   175 
       
   176 	// Bind the key event interrupt
       
   177 	r=Interrupt::Bind(KIntIdKeyboard,Isr,this);
       
   178 	if (r>=0)
       
   179 		{
       
   180 		// install the power handler
       
   181 		iIrqHandle = r;
       
   182 		Add();
       
   183 		KeyboardPowerUp();
       
   184 		}
       
   185 	return KErrNone;
       
   186 	}
       
   187 
       
   188 void DKeyboardNE1_TB::Isr(TAny* aPtr)
       
   189 	{
       
   190 	DKeyboardNE1_TB& k=*(DKeyboardNE1_TB*)aPtr;
       
   191 	Interrupt::Disable(k.iIrqHandle);
       
   192 	k.iEventDfc.Add();
       
   193 	}
       
   194 
       
   195 void DKeyboardNE1_TB::EventDfcFn(TAny* aPtr)
       
   196 	{
       
   197 	((DKeyboardNE1_TB*)aPtr)->EventDfc();
       
   198 	}
       
   199 
       
   200 void DKeyboardNE1_TB::EventDfc()
       
   201 	{
       
   202 	__KTRACE_OPT(KHARDWARE,Kern::Printf("DKeyboardNE1_TB::EventDfc"));
       
   203 
       
   204 	TInt irq=NKern::DisableAllInterrupts();
       
   205 	while (IsKeyReady())						// while there are keys in the controller's output buffer
       
   206 		{
       
   207 		NKern::RestoreInterrupts(irq);
       
   208 		TRawEvent e;
       
   209 		TUint keyCode=GetKeyCode();				// Read keycodes from controller
       
   210 		__KTRACE_OPT(KHARDWARE,Kern::Printf("#%02x",keyCode));
       
   211 
       
   212 		//
       
   213 		// TO DO: (mandatory)
       
   214 		//
       
   215 		// Convert from hardware scancode to EPOC scancode and send the scancode as an event (key pressed or released)
       
   216 		// as per below EXAMPLE ONLY:
       
   217 		//
       
   218 		TUint bareCode=keyCode&~KFlagKeyPressed;
       
   219 		TUint8 stdKey=convertCode[bareCode];
       
   220 		if (keyCode&KFlagKeyPressed)
       
   221 			e.Set(TRawEvent::EKeyUp,stdKey,0);
       
   222 		else
       
   223 			e.Set(TRawEvent::EKeyDown,stdKey,0);
       
   224 		Kern::AddEvent(e);
       
   225 		NKern::Sleep(1);						// pause before reading more keycodes
       
   226 		irq=NKern::DisableAllInterrupts();
       
   227 		}
       
   228 	Interrupt::Enable(iIrqHandle);
       
   229 	NKern::RestoreInterrupts(irq);
       
   230 	}
       
   231 
       
   232 TBool DKeyboardNE1_TB::IsKeyReady()
       
   233 	{
       
   234 	//
       
   235 	// TO DO: (mandatory)
       
   236 	//
       
   237 	// Return ETrue if the keyboard controller has a key event waiting to be read
       
   238 	//
       
   239 
       
   240 	return EFalse;	// EXAMPLE ONLY
       
   241 	}
       
   242 
       
   243 TUint DKeyboardNE1_TB::GetKeyCode()
       
   244 	{
       
   245 	//
       
   246 	// TO DO: (mandatory)
       
   247 	//
       
   248 	// Read and return the next available keycode from the keyboard controller
       
   249 	//
       
   250 
       
   251 	return 0;	// EXAMPLE ONLY
       
   252 	}
       
   253 
       
   254 void DKeyboardNE1_TB::PowerUpDfcFn(TAny* aPtr)
       
   255 	{
       
   256 	((DKeyboardNE1_TB*)aPtr)->PowerUpDfc();
       
   257 	}
       
   258 
       
   259 void DKeyboardNE1_TB::PowerDownDfcFn(TAny* aPtr)
       
   260 	{
       
   261 	((DKeyboardNE1_TB*)aPtr)->PowerDownDfc();
       
   262 	}
       
   263 
       
   264 
       
   265 void DKeyboardNE1_TB::KeyboardPowerUp()
       
   266 	{
       
   267 	__KTRACE_OPT(KPOWER,Kern::Printf("DKeyboardNE1_TB::KeyboardPowerUp()"));
       
   268 
       
   269 	iKeyboardOn = ETrue;
       
   270 
       
   271 	// Send key up events for EStdKeyOff (Fn+Esc) event 
       
   272 	TRawEvent e;
       
   273 	e.Set(TRawEvent::EKeyUp,EStdKeyEscape,0);
       
   274 	Kern::AddEvent(e);
       
   275 	e.Set(TRawEvent::EKeyUp,EStdKeyLeftFunc,0);
       
   276 	Kern::AddEvent(e);
       
   277 	}
       
   278 
       
   279 void DKeyboardNE1_TB::PowerUp()
       
   280 	{
       
   281 	iPowerUpDfc.Enque();	// schedules DFC to execute on this driver's thread
       
   282 	}
       
   283 
       
   284 void DKeyboardNE1_TB::PowerUpDfc()
       
   285 	{
       
   286 	__KTRACE_OPT(KPOWER, Kern::Printf("DKeyboardNE1_TB::PowerUpDfc()"));
       
   287 	KeyboardOn();
       
   288 	PowerUpDone();		// must be called from a different thread than PowerUp()
       
   289 	}
       
   290 
       
   291 void DKeyboardNE1_TB::KeyboardOn()
       
   292 	{
       
   293 	__KTRACE_OPT(KPOWER,Kern::Printf("DKeyboardNE1_TB::KeyboardOn() iKeyboardOn=%d", iKeyboardOn));
       
   294 
       
   295 	if (!iKeyboardOn)	// may be powered up from Power Manager or HAL
       
   296 		{
       
   297 		KeyboardPowerUp();
       
   298 		}
       
   299 	}
       
   300 
       
   301 void DKeyboardNE1_TB::PowerDown(TPowerState)
       
   302 	{
       
   303 	iPowerDownDfc.Enque();	// schedules DFC to execute on this driver's thread
       
   304 	}
       
   305 
       
   306 void DKeyboardNE1_TB::PowerDownDfc()
       
   307 	{
       
   308 	__KTRACE_OPT(KPOWER, Kern::Printf("DKeyboardNE1_TB::PowerDownDfc()"));
       
   309 	KeyboardOff();
       
   310 	PowerDownDone();		// must be called from a different thread than PowerDown()
       
   311 	}
       
   312 
       
   313 void DKeyboardNE1_TB::KeyboardOff()
       
   314 	{
       
   315 	__KTRACE_OPT(KPOWER,Kern::Printf("DKeyboardNE1_TB::KeyboardOff() iKeyboardOn=%d", iKeyboardOn));
       
   316 
       
   317 	if (iKeyboardOn)	// may have already been powered down by the HAL
       
   318 		{
       
   319 		iKeyboardOn = EFalse;
       
   320 		Interrupt::Disable(iIrqHandle);
       
   321 		}
       
   322 	iEventDfc.Cancel();
       
   323 	}
       
   324 
       
   325 void DKeyboardNE1_TB::HandleMsg(TMessageBase* aMsg)
       
   326 	{
       
   327 	if (aMsg->iValue)
       
   328 		KeyboardOn();
       
   329 	else
       
   330 		KeyboardOff();
       
   331 	aMsg->Complete(KErrNone,ETrue);
       
   332 	}
       
   333 
       
   334 void DKeyboardNE1_TB::KeyboardInfo(TKeyboardInfoV01& aInfo)
       
   335 	{
       
   336 	__KTRACE_OPT(KEXTENSION,Kern::Printf("DKeyboardNE1_TB::KeyboardInfo"));
       
   337 
       
   338 	aInfo.iKeyboardType=KConfigKeyboardType;
       
   339 	aInfo.iDeviceKeys=KConfigKeyboardDeviceKeys;
       
   340 	aInfo.iAppsKeys=KConfigKeyboardAppsKeys;
       
   341 	}
       
   342 
       
   343 TInt DKeyboardNE1_TB::HalFunction(TInt aFunction, TAny* a1, TAny* a2)
       
   344 	{
       
   345 	TInt r=KErrNone;
       
   346 	__KTRACE_OPT(KEXTENSION,Kern::Printf("DKeyboardNE1_TB::HalFunction %d", aFunction));
       
   347 	switch(aFunction)
       
   348 		{
       
   349 		case EKeyboardHalKeyboardInfo:
       
   350 			{
       
   351 			TPckgBuf<TKeyboardInfoV01> kPckg;
       
   352 			KeyboardInfo(kPckg());
       
   353 			Kern::InfoCopy(*(TDes8*)a1,kPckg);
       
   354 			break;
       
   355 			}
       
   356 		case EKeyboardHalSetKeyboardState:
       
   357 			{
       
   358 			if(!Kern::CurrentThreadHasCapability(ECapabilityPowerMgmt,__PLATSEC_DIAGNOSTIC_STRING("Checked by Hal function EKeyboardHalSetKeyboardState")))
       
   359 				return KErrPermissionDenied;
       
   360 			if ((TBool)a1)
       
   361 				{
       
   362 				TThreadMessage& m=Kern::Message();
       
   363 				m.iValue = ETrue;
       
   364 				m.SendReceive(&iMsgQ);		// send a message and block Client thread until keyboard has been powered up
       
   365 				}
       
   366 			else
       
   367 				{
       
   368 				TThreadMessage& m=Kern::Message();
       
   369 				m.iValue = EFalse;
       
   370 				m.SendReceive(&iMsgQ);		// send a message and block Client thread until keyboard has been powered down
       
   371 				}
       
   372 			}
       
   373 			break;
       
   374 		case EKeyboardHalKeyboardState:
       
   375 			kumemput32(a1, &iKeyboardOn, sizeof(TBool));
       
   376 			break;
       
   377 		default:
       
   378 			r=KErrNotSupported;
       
   379 			break;
       
   380 		}
       
   381 	return r;
       
   382 	}
       
   383 
       
   384 DECLARE_STANDARD_EXTENSION()
       
   385 	{
       
   386 	__KTRACE_OPT(KEXTENSION,Kern::Printf("Starting keyboard driver"));
       
   387 
       
   388 	// create keyboard driver
       
   389 	TInt r=KErrNoMemory;
       
   390 	DKeyboardNE1_TB* pK=new DKeyboardNE1_TB;
       
   391 	if (pK)
       
   392 		r=pK->Create();
       
   393 
       
   394 	__KTRACE_OPT(KEXTENSION,Kern::Printf("Returns %d",r));
       
   395 	return r;
       
   396 	}