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