os/kernelhwsrv/kernel/eka/drivers/xyin/d_xyin.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32\drivers\xyin\d_xyin.cpp
    15 // Generic digitiser driver
    16 //
    17 //
    18 
    19 
    20 #include <drivers/xyin.h>
    21 #include <kernel/kern_priv.h>
    22 
    23 _LIT(KLitDigitiser,"Digitiser");
    24 
    25 LOCAL_C void sampleDfc(TAny* aPtr)
    26 	{
    27 	((DDigitiser*)aPtr)->ProcessRawSample();
    28 	}
    29 
    30 LOCAL_C void penUpDfc(TAny* aPtr)
    31 	{
    32 	((DDigitiser*)aPtr)->ProcessPenUp();
    33 	}
    34 
    35 LOCAL_C TInt halFunction(TAny* aPtr, TInt aFunction, TAny* a1, TAny* a2)
    36 	{
    37 	DDigitiser* pH=(DDigitiser*)aPtr;
    38 	return pH->HalFunction(aFunction,a1,a2);
    39 	}
    40 
    41 LOCAL_C void rxMsg(TAny* aPtr)
    42 	{
    43 	DDigitiser& h=*(DDigitiser*)aPtr;
    44 	TMessageBase* pM=h.iMsgQ.iMessage;
    45 	if (pM)
    46 		h.HandleMsg(pM);
    47 	}
    48 
    49 DDigitiser::DDigitiser()
    50 	:	DPowerHandler(KLitDigitiser),
    51 		iMsgQ(rxMsg,this,NULL,1),
    52 		iSampleDfc(sampleDfc,this,5),
    53 		iPenUpDfc(penUpDfc,this,5)
    54 	{
    55 //	iBufferIndex=0;
    56 //	iLastPos=TPoint(0,0);
    57 //	iState=EIdle;
    58 //	iCount=0;
    59 //	iPointerOn=EFalse
    60 	}
    61 
    62 TInt DDigitiser::Create()
    63 	{
    64 	TInt r=DoCreate();				// do hardware-dependent initialisation
    65 
    66 	if (r!=KErrNone)
    67 		return r;
    68 
    69 	__ASSERT_DEBUG(iDfcQ, Kern::Fault("DDigitiser::Create iDfcQ not set", __LINE__));
    70 	iMsgQ.SetDfcQ(iDfcQ);
    71 	iSampleDfc.SetDfcQ(iDfcQ);
    72 	iPenUpDfc.SetDfcQ(iDfcQ);
    73 
    74 	TInt n=iCfg.iPenUpDiscard;		// number of samples to delay
    75 	iBuffer=(TPoint*)Kern::Alloc(n*sizeof(TPoint));
    76 	if (!iBuffer)
    77 		return KErrNoMemory;
    78 
    79 	// install the HAL function
    80 	r=Kern::AddHalEntry(EHalGroupDigitiser,halFunction,this);
    81 	if (r!=KErrNone)
    82 		return r;
    83 
    84 	iMsgQ.Receive();
    85 
    86 	// wait for pen down
    87 	WaitForPenDown();
    88 
    89 	return r;
    90 	}
    91 
    92 void DDigitiser::RawSampleValid()
    93 //
    94 // Called by hardware-dependent code when a raw sample is available
    95 //
    96 	{
    97 	iSampleDfc.Add();
    98 	}
    99 
   100 void DDigitiser::PenUp()
   101 //
   102 // Called by hardware-dependent code when the pen goes up
   103 //
   104 	{
   105 	iPenUpDfc.Add();
   106 	}
   107 
   108 void DDigitiser::ProcessRawSample()
   109 //
   110 // DFC to process a raw sample
   111 //
   112 	{
   113 	TPoint p;
   114 	TInt r;
   115 	TBool ok=SamplesToPoint(p);
   116 	if (!ok)
   117 		{
   118 		// wait for pen to stabilise
   119 		__KTRACE_XY2(Kern::Printf("BS"));
   120 		WaitForPenUpDebounce();
   121 		return;
   122 		}
   123 	__KTRACE_XY2(Kern::Printf("GS (%d,%d) %d",p.iX,p.iY,iState));
   124 	switch (iState)
   125 		{
   126 		case EIdle:
   127 			// pen has just gone down
   128 			iCount=iCfg.iPenDownDiscard;
   129 			iState=EDiscardOnPenDown;
   130 			// fall through
   131 		case EDiscardOnPenDown:
   132 			if (iCount)
   133 				{
   134 				// still discarding
   135 				iCount--;
   136 				break;
   137 				}
   138 			iState=EBufferFilling;
   139 			iBufferIndex=0;
   140 			iCount=iCfg.iPenUpDiscard;
   141 			// fall through
   142 		case EBufferFilling:
   143 			if (iCount)
   144 				{
   145 				// buffer still filling
   146 				iCount--;
   147 				iBuffer[iBufferIndex++]=p;
   148 				if (iBufferIndex==iCfg.iPenUpDiscard)
   149 					iBufferIndex=0;
   150 				break;
   151 				}
   152 			iState=EBufferFull;
   153 			// fall through
   154 		case EBufferFull:
   155 			r=DelayAndConvertSample(p,iLastPos);
   156 			if (r!=KErrNone)
   157 				break;					// off the screen, so don't issue Pen Down Event
   158 			iState=EPenDown;
   159 			ResetPenMoveFilter();
   160 			IssuePenDownEvent();
   161 			break;
   162 		case EPenDown:
   163 			r=DelayAndConvertSample(p,p);
   164 			if (r!=KErrNone)
   165 				{
   166 				iState=EIdle;			// off the screen, so treat as pen-up
   167 				IssuePenUpEvent();
   168 				break;
   169 				}
   170 			FilterPenMove(p);
   171 			break;
   172 		};
   173 	WaitForPenUp();		// request another sample from the hardware
   174 	}
   175 
   176 void DDigitiser::ProcessPenUp()
   177 //
   178 // DFC to process pen-up events
   179 //
   180 	{
   181 	__KTRACE_XY2(Kern::Printf("up %d",iState));
   182 	switch (iState)
   183 		{
   184 		case EIdle:
   185 		case EDiscardOnPenDown:
   186 		case EBufferFilling:
   187 		case EBufferFull:
   188 			iState=EIdle;
   189 			break;
   190 		case EPenDown:
   191 			iState=EIdle;
   192 			IssuePenUpEvent();
   193 			break;
   194 		}
   195 	WaitForPenDown();	// tell the hardware to watch for another pen-down
   196 	}
   197 
   198 TBool DDigitiser::SamplesToPoint(TPoint& aPoint)
   199 //
   200 // Average and validate the raw samples from the hardware
   201 //
   202 	{
   203 #if defined(__DIGITISER_DEBUG2__)
   204 	TBuf<80> buf;
   205 #endif
   206 	TInt i;
   207 	TInt minx=KMaxTInt;
   208 	TInt miny=KMaxTInt;
   209 	TInt maxx=KMinTInt;
   210 	TInt maxy=KMinTInt;
   211 	TInt sumx=0;
   212 	TInt sumy=0;
   213 	TInt n=iCfg.iNumXYSamples;
   214 	for (i=0; i<n; i++)
   215 		{
   216 		TInt x=iX[i];
   217 		if (x<minx)
   218 			minx=x;
   219 		if (x>maxx)
   220 			maxx=x;
   221 		sumx+=x;
   222 		TInt y=iY[i];
   223 		if (y<miny)
   224 			miny=y;
   225 		if (y>maxy)
   226 			maxy=y;
   227 		sumy+=y;
   228 //		__KTRACE_XY2(buf.AppendFormat(_L("(%d,%d) "),x,y));
   229 		__KTRACE_XY2(Kern::Printf("(%d,%d) ",x,y));
   230 		}
   231 //	__KTRACE_XY2(Kern::Printf("%S", buf));
   232 
   233 	TInt spreadx=maxx-minx;
   234 	TInt spready=maxy-miny;
   235 	if (iCfg.iDisregardMinMax)
   236 		{
   237 		sumx-=minx;					// disregard extremal values in average
   238 		sumx-=maxx;
   239 		sumy-=miny;
   240 		sumy-=maxy;
   241 		n-=2;
   242 		}
   243 	sumx/=n;	// average the values
   244 	sumy/=n;
   245 	if (spreadx<iCfg.iSpreadX && spready<iCfg.iSpreadY && sumx>=iCfg.iMinX && sumx<=iCfg.iMaxX && sumy>=iCfg.iMinY && sumy<=iCfg.iMaxY)
   246 		{
   247 		// samples are OK
   248 		aPoint.iX=sumx;
   249 		aPoint.iY=sumy;
   250 		return ETrue;
   251 		}
   252 	// samples are dodgy
   253 	return EFalse;
   254 	}
   255 
   256 TInt DDigitiser::DelayAndConvertSample(const TPoint& aSample, TPoint& aScreenPoint)
   257 //
   258 // Pass a sample through the delay line and convert to screen coordinates
   259 //
   260 	{
   261 	if (iCfg.iPenUpDiscard != 0)
   262 		{
   263 		TPoint p=iBuffer[iBufferIndex];		// sample leaving delay line
   264 		iBuffer[iBufferIndex++]=aSample;	// sample entering delay line
   265 		if (iBufferIndex==iCfg.iPenUpDiscard)
   266 			iBufferIndex=0;
   267 		return DigitiserToScreen(p,aScreenPoint);
   268 		}
   269 	return DigitiserToScreen(aSample,aScreenPoint);
   270 	}
   271 
   272 void DDigitiser::IssuePenDownEvent()
   273 	{
   274 	TRawEvent e;
   275 	e.Set(TRawEvent::EButton1Down,iLastPos.iX,iLastPos.iY);
   276 	Kern::AddEvent(e);
   277 	__KTRACE_XY2(Kern::Printf("D %d,%d",e.Pos().iX,e.Pos().iY));
   278 	}
   279 
   280 void DDigitiser::IssuePenUpEvent()
   281 	{
   282 	TRawEvent e;
   283 	e.Set(TRawEvent::EButton1Up,iLastPos.iX,iLastPos.iY);
   284 	Kern::AddEvent(e);
   285 	__KTRACE_XY2(Kern::Printf("U %d,%d",e.Pos().iX,e.Pos().iY));
   286 	}
   287 
   288 void DDigitiser::IssuePenMoveEvent(const TPoint& aPoint)
   289 	{
   290 	TRawEvent e;
   291 	e.Set(TRawEvent::EPointerMove,aPoint.iX,aPoint.iY);
   292 	Kern::AddEvent(e);
   293 	__KTRACE_XY2(Kern::Printf("M %d,%d",e.Pos().iX,e.Pos().iY));
   294 	}
   295 
   296 void DDigitiser::HandleMsg(TMessageBase* aMsg)
   297 	{
   298 	if (aMsg->iValue)
   299 		DigitiserOn();
   300 	else
   301 		DigitiserOff();
   302 	aMsg->Complete(KErrNone,ETrue);
   303 	}
   304 
   305 TInt DDigitiser::HalFunction(TInt aFunction, TAny* a1, TAny* a2)
   306 	{
   307 	TInt r=KErrNone;
   308 __KTRACE_OPT(KEXTENSION,Kern::Printf("HalFunction %d", aFunction));
   309 	switch(aFunction)
   310 		{
   311 		case EDigitiserHalXYInfo:
   312 			{
   313 			TPckgBuf<TDigitiserInfoV01> vPckg;
   314 			DigitiserInfo(vPckg());
   315 			Kern::InfoCopy(*(TDes8*)a1,vPckg);
   316 			break;
   317 			}
   318 		case EDigitiserHalSetXYInputCalibration:
   319 			{
   320 			if(!Kern::CurrentThreadHasCapability(ECapabilityWriteDeviceData,__PLATSEC_DIAGNOSTIC_STRING("Checked by Hal function EDigitiserHalSetXYInputCalibration")))
   321 				return KErrPermissionDenied;
   322 			TDigitizerCalibration cal;
   323 			kumemget32(&cal,a1,sizeof(TDigitizerCalibration));
   324 			r=SetXYInputCalibration(cal);
   325 			break;
   326 			}
   327 		case EDigitiserHalCalibrationPoints:
   328 			TDigitizerCalibration cal;
   329 			r=CalibrationPoints(cal);
   330 			kumemput32(a1,&cal,sizeof(TDigitizerCalibration));
   331 			break;
   332 		case EDigitiserHalSaveXYInputCalibration:
   333 			r=SaveXYInputCalibration();
   334 			break;
   335 		case EDigitiserHalRestoreXYInputCalibration:
   336 			if(!Kern::CurrentThreadHasCapability(ECapabilityWriteDeviceData,__PLATSEC_DIAGNOSTIC_STRING("Checked by Hal function EDigitiserHalRestoreXYInputCalibration")))
   337 				return KErrPermissionDenied;
   338 			r=RestoreXYInputCalibration((TDigitizerCalibrationType)(TInt)a1);
   339 			break;
   340 		case EDigitiserHalSetXYState:
   341 			{
   342 			if(!Kern::CurrentThreadHasCapability(ECapabilityPowerMgmt,__PLATSEC_DIAGNOSTIC_STRING("Checked by Hal function EDigitiserHalSetXYState")))
   343 				return KErrPermissionDenied;
   344 			if ((TBool)a1)
   345 				{
   346 				TThreadMessage& m=Kern::Message();
   347 				m.iValue = ETrue;
   348 				m.SendReceive(&iMsgQ);
   349 				}
   350 			else
   351 				{
   352 				TThreadMessage& m=Kern::Message();
   353 				m.iValue = EFalse;
   354 				m.SendReceive(&iMsgQ);
   355 				}
   356 			}
   357 			break;
   358 		case EDigitiserHalXYState:
   359 			kumemput32(a1, (TBool*)&iPointerOn, sizeof(TBool));
   360 			break;
   361 		default:
   362 			r=KErrNotSupported;
   363 			break;
   364 		}
   365 	return r;
   366 	}
   367 
   368 DECLARE_STANDARD_EXTENSION()
   369 	{
   370 	__KTRACE_OPT(KEXTENSION,Kern::Printf("Starting digitiser driver"));
   371 	if (Kern::SuperPage().iCpuId & KCpuIdISS)
   372 		return KErrNone;	// no digitiser on ARMULATOR
   373 	DDigitiser* pD=DDigitiser::New();
   374 	TInt r=KErrNoMemory;
   375 	if (pD)
   376 		r=pD->Create();
   377 	__KTRACE_OPT(KEXTENSION,Kern::Printf("Returning %d",r));
   378 	return r;
   379 	}
   380 
   381 #ifdef __BUILD_DEVICE_DRIVER__
   382 class DDigitiserPdd : public DPhysicalDevice
   383 	{
   384 public:
   385 	virtual TInt Install();
   386 	virtual void GetCaps(TDes8& aDes) const;
   387 	virtual TInt Create(DBase*& aChannel, TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
   388 	virtual TInt Validate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
   389 	};
   390 
   391 _LIT(KPddName,"XYInput");
   392 
   393 TInt DDigitiserPdd::Install()
   394 	{
   395 	return SetName(&KPddName);
   396 	}
   397 
   398 void DDigitiserPdd::GetCaps(TDes8&) const
   399 	{
   400 	}
   401 
   402 TInt DDigitiserPdd::Create(DBase*& aChannel, TInt, const TDesC8*, const TVersion&)
   403 	{
   404 	aChannel=NULL;
   405 	return KErrNone;
   406 	}
   407 
   408 TInt DDigitiserPdd::Validate(TInt, const TDesC8*, const TVersion&)
   409 	{
   410 	return KErrNotSupported;
   411 	}
   412 
   413 DECLARE_EXTENSION_PDD()
   414 	{
   415 	return new DDigitiserPdd;
   416 	}
   417 #endif