os/kernelhwsrv/kernel/eka/ewsrv/ky_tran.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1996-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\ewsrv\ky_tran.cpp
sl@0
    15
// The main code for setting modifiers and translating raw scanCodes into
sl@0
    16
// keyCodes. Also traps capture-keys
sl@0
    17
// 
sl@0
    18
//
sl@0
    19
sl@0
    20
sl@0
    21
#include <e32svr.h>
sl@0
    22
#include <k32keys.h>
sl@0
    23
#include <e32keys.h>
sl@0
    24
#include <e32uid.h>
sl@0
    25
sl@0
    26
enum	{EDummy,EKeyDataConv,EKeyDataFunc,EKeyDataSettings};
sl@0
    27
  
sl@0
    28
EXPORT_C CKeyTranslator* CKeyTranslator::New()
sl@0
    29
//
sl@0
    30
// Return the actual key translator
sl@0
    31
//
sl@0
    32
    {
sl@0
    33
sl@0
    34
    CKeyTranslatorX* pS=new CKeyTranslatorX;
sl@0
    35
	if (pS && pS->Initialise()!=KErrNone)
sl@0
    36
		{
sl@0
    37
		delete pS;
sl@0
    38
		pS=NULL;
sl@0
    39
		}
sl@0
    40
    return(pS);
sl@0
    41
    }
sl@0
    42
sl@0
    43
CKeyTranslatorX::CKeyTranslatorX()
sl@0
    44
#pragma warning (disable: 4705)
sl@0
    45
	{
sl@0
    46
#pragma warning (default: 4705)
sl@0
    47
sl@0
    48
    UpdateModifiers(0);
sl@0
    49
sl@0
    50
	}
sl@0
    51
sl@0
    52
TInt CKeyTranslatorX::Initialise()
sl@0
    53
	{
sl@0
    54
	return (ChangeKeyData(_L("")));	//Set default keydata
sl@0
    55
	}
sl@0
    56
sl@0
    57
TBool CKeyTranslatorX::currentlyUpperCase(void)
sl@0
    58
//
sl@0
    59
// Determines whether a letter should be returned as upper case given the
sl@0
    60
// current state of the modifiers. This is used for accented characters
sl@0
    61
// created, for example, by entering Ctrl-1 "a". Since the keyboard may be
sl@0
    62
// configured in different ways (e.g. shift AND capslock together may result
sl@0
    63
// in either upper or lower case letters), a dynamic function such as this
sl@0
    64
// is necessary
sl@0
    65
//
sl@0
    66
	{
sl@0
    67
	TInt modifiersAffectingUpperCase=0;
sl@0
    68
sl@0
    69
	if (iCurModifiers&EModifierCapsLock)
sl@0
    70
		modifiersAffectingUpperCase|=EModifierCapsLock;
sl@0
    71
sl@0
    72
	if (iCurModifiers&EModifierShift)
sl@0
    73
		modifiersAffectingUpperCase|=EModifierShift;
sl@0
    74
sl@0
    75
	for (TUint i=iConvTable.FirstScanCode(); i<=iConvTable.LastScanCode(); i++)
sl@0
    76
		{
sl@0
    77
		TChar ch=iConvTable.Convert(i, modifiersAffectingUpperCase).keyCode;
sl@0
    78
		if (ch.IsUpper())
sl@0
    79
			return ETrue;
sl@0
    80
		else if (ch.IsLower())
sl@0
    81
			return EFalse;
sl@0
    82
		}
sl@0
    83
	return EFalse;
sl@0
    84
	}
sl@0
    85
sl@0
    86
TUint CKeyTranslatorX::executeFunctionsAndSetState(TCharExtended aChar)
sl@0
    87
//
sl@0
    88
// Looks up and carries out the function required for the given
sl@0
    89
// key-code/modifiers/state
sl@0
    90
//
sl@0
    91
	{
sl@0
    92
	TUint keyCode=EKeyNull;
sl@0
    93
	SFunc modifierFunc=iFuncTable.GetModifierFunc(aChar, iCurModifiers);
sl@0
    94
	SFuncAndState genFuncAndNewState=iFuncTable.GetGeneralFuncAndState(aChar, iCurModifiers, iCurState,
sl@0
    95
																			iCurCtrlDigits.GetRadix());
sl@0
    96
sl@0
    97
    SetModifierState((TEventModifier)modifierFunc.funcParam,(TModifierState)modifierFunc.func);
sl@0
    98
sl@0
    99
    if(!(iCurModifiers&(EModifierLeftAlt|EModifierRightAlt)))
sl@0
   100
        iCurModifiers&=~EModifierAlt;
sl@0
   101
    if(!(iCurModifiers&(EModifierLeftShift|EModifierRightShift)))
sl@0
   102
        iCurModifiers&=~EModifierShift;
sl@0
   103
    if(!(iCurModifiers&(EModifierLeftFunc|EModifierRightFunc)))
sl@0
   104
        iCurModifiers&=~EModifierFunc;
sl@0
   105
    if(!(iCurModifiers&(EModifierLeftCtrl|EModifierRightCtrl)))
sl@0
   106
        iCurModifiers&=~EModifierCtrl;
sl@0
   107
sl@0
   108
	switch (genFuncAndNewState.func)
sl@0
   109
		{
sl@0
   110
	case EDoNothing:
sl@0
   111
		break;
sl@0
   112
	case EPassKeyThru:
sl@0
   113
		keyCode=aChar;
sl@0
   114
		break;
sl@0
   115
	case EPassSpecialKeyThru:
sl@0
   116
		iCurCtrlDigits.Reset();
sl@0
   117
		keyCode=(currentlyUpperCase())?
sl@0
   118
					User::UpperCase(genFuncAndNewState.funcParam):
sl@0
   119
					genFuncAndNewState.funcParam;
sl@0
   120
		iCurModifiers|=(EModifierSpecial);
sl@0
   121
		break;
sl@0
   122
	case EPassCtrlDigitsThru:
sl@0
   123
		if (iCurCtrlDigits.WithinLimits())
sl@0
   124
			{
sl@0
   125
			keyCode=iCurCtrlDigits.GetDigits();
sl@0
   126
			iCurModifiers|=(EModifierSpecial);
sl@0
   127
			}
sl@0
   128
		iCurCtrlDigits.Reset();
sl@0
   129
		break;
sl@0
   130
	case EAddOnCtrlDigit:
sl@0
   131
		iCurCtrlDigits.AppendDigit(aChar, iCurModifiers);
sl@0
   132
		if (iCurCtrlDigits.Terminated(iCurModifiers) && !iCurCtrlDigits.Error() && iCurCtrlDigits.WithinLimits())
sl@0
   133
			{
sl@0
   134
			keyCode=iCurCtrlDigits.GetDigits();
sl@0
   135
			iCurModifiers|=(EModifierSpecial);
sl@0
   136
			}
sl@0
   137
		break;
sl@0
   138
	}
sl@0
   139
sl@0
   140
	switch (genFuncAndNewState.state)
sl@0
   141
		{
sl@0
   142
	case EStateUnchanged:
sl@0
   143
		break;
sl@0
   144
	case EStateDerivedFromDigitEntered:
sl@0
   145
		iCurState=aChar.DigitValue();
sl@0
   146
		break;
sl@0
   147
	case EStateCtrlDigits:
sl@0
   148
		if (iCurCtrlDigits.Terminated(iCurModifiers) || iCurCtrlDigits.Error())
sl@0
   149
	 		{
sl@0
   150
			iCurState=EStateNormal;
sl@0
   151
			iCurCtrlDigits.Reset();
sl@0
   152
			}
sl@0
   153
		else
sl@0
   154
			iCurState=iCurCtrlDigits.SetStateToCtrlDigits();
sl@0
   155
		break;
sl@0
   156
	default:
sl@0
   157
		iCurState=genFuncAndNewState.state;
sl@0
   158
		if (iCurState==EStateNormal)
sl@0
   159
			iCurCtrlDigits.Reset();
sl@0
   160
		break;
sl@0
   161
		}
sl@0
   162
	return keyCode;
sl@0
   163
	}
sl@0
   164
sl@0
   165
TInt CKeyTranslatorX::GetModifierState()
sl@0
   166
//
sl@0
   167
// Return the current modifier state
sl@0
   168
//
sl@0
   169
    {
sl@0
   170
sl@0
   171
    return(iCurModifiers);
sl@0
   172
    }
sl@0
   173
sl@0
   174
void CKeyTranslatorX::UpdateModifiers(TInt aModifiers)
sl@0
   175
//
sl@0
   176
//
sl@0
   177
//
sl@0
   178
    {
sl@0
   179
sl@0
   180
    if(aModifiers == EModifierCancelRotation || aModifiers & KRotationModifiers)
sl@0
   181
        iCurModifiers &= KPersistentModifiers;	// if a Rotation modifier is being updated, only keep persistent modifiers
sl@0
   182
	else
sl@0
   183
	    iCurModifiers &= KPersistentModifiers|KRotationModifiers;		// if not, keep Rotation modifiers also
sl@0
   184
	iCurModifiers |= aModifiers;
sl@0
   185
    iCurState = EStateNormal;
sl@0
   186
    }
sl@0
   187
sl@0
   188
sl@0
   189
void CKeyTranslatorX::SetModifierState(TEventModifier aModifier,TModifierState aState)
sl@0
   190
//
sl@0
   191
// Change a modifier state
sl@0
   192
//
sl@0
   193
    {
sl@0
   194
sl@0
   195
    switch(aState)
sl@0
   196
        {
sl@0
   197
        case ETurnOffModifier:
sl@0
   198
            iCurModifiers&=~aModifier;
sl@0
   199
            break;
sl@0
   200
        case ETurnOnModifier:
sl@0
   201
            iCurModifiers|=aModifier;
sl@0
   202
            break;
sl@0
   203
        case EToggleModifier:
sl@0
   204
            iCurModifiers^=aModifier;
sl@0
   205
        }
sl@0
   206
    }
sl@0
   207
sl@0
   208
TBool CKeyTranslatorX::TranslateKey(TUint aScanCode, TBool aKeyUp,
sl@0
   209
											const CCaptureKeys &aCaptureKeys, TKeyData &aKeyData)
sl@0
   210
//
sl@0
   211
// The function called for every keyup/keydown converting the aScanCode into a
sl@0
   212
// keyCode, carrying out the function specified in the keyboard configuration
sl@0
   213
// tables and setting the new state of the keyboard
sl@0
   214
//
sl@0
   215
	{
sl@0
   216
sl@0
   217
#if defined(__WINS__)
sl@0
   218
	// This code extracts the character code if there is one munged
sl@0
   219
	// with the scan code.  Code which does not take advantage of this
sl@0
   220
	// new facility to pass a character code as part of aScanCode should
sl@0
   221
	// be unaffected
sl@0
   222
	// 
sl@0
   223
	// extract the character code
sl@0
   224
	TUint charCode=(aScanCode&0xFFFF0000)>>16;
sl@0
   225
	// extract the scan code
sl@0
   226
	aScanCode&=0x0000FFFF;
sl@0
   227
#endif
sl@0
   228
sl@0
   229
	TUint oldState=iCurState;
sl@0
   230
   	TCharExtended ch;
sl@0
   231
sl@0
   232
    iCurModifiers&=~(EModifierPureKeycode);
sl@0
   233
sl@0
   234
    if(aScanCode<ESpecialKeyBase || aScanCode>=(ESpecialKeyBase+ESpecialKeyCount))
sl@0
   235
        {
sl@0
   236
    	SConvKeyData convKeyData=(iCurState==EStateNormal)?
sl@0
   237
					iConvTable.Convert(aScanCode, iCurModifiers):
sl@0
   238
					iConvTable.ConvertBaseCase(aScanCode, iCurModifiers);
sl@0
   239
sl@0
   240
    	TMaskedModifiers convModifiers;
sl@0
   241
    	convModifiers.iMask=KConvTableSettableModifiers;
sl@0
   242
    	convModifiers.iValue=convKeyData.modifiers;
sl@0
   243
sl@0
   244
    	MergeModifiers(iCurModifiers,convModifiers);
sl@0
   245
        ch=convKeyData.keyCode;
sl@0
   246
        }
sl@0
   247
    else
sl@0
   248
        ch=aScanCode;
sl@0
   249
sl@0
   250
 	if (aKeyUp)
sl@0
   251
		iCurModifiers|=(EModifierKeyUp);
sl@0
   252
	else
sl@0
   253
		iCurModifiers&=~(EModifierKeyUp);
sl@0
   254
sl@0
   255
	aKeyData.iKeyCode=executeFunctionsAndSetState(ch);
sl@0
   256
sl@0
   257
    ch=aKeyData.iKeyCode;
sl@0
   258
    // prevent modifier keys returning as keypresses
sl@0
   259
    if(ch.IsModifier())
sl@0
   260
        {
sl@0
   261
        aKeyData.iKeyCode=EKeyNull;
sl@0
   262
        iCurModifiers&=~EModifierPureKeycode;
sl@0
   263
        }
sl@0
   264
sl@0
   265
	TBool ret;
sl@0
   266
sl@0
   267
	ret=(aKeyData.iKeyCode!=EKeyNull);
sl@0
   268
sl@0
   269
#if defined(__WINS__)
sl@0
   270
	// see comments in __WINS__ block above
sl@0
   271
	if (charCode)
sl@0
   272
		{
sl@0
   273
        if (!(iCurModifiers & KRotationModifiers)) // if rotation modifiers not set we trust the WINDOWS translation
sl@0
   274
            {
sl@0
   275
		    aKeyData.iKeyCode=charCode;
sl@0
   276
		    iCurModifiers|=EModifierAutorepeatable;
sl@0
   277
            }
sl@0
   278
        ret = ETrue;
sl@0
   279
		}
sl@0
   280
#endif
sl@0
   281
sl@0
   282
	if (aKeyUp
sl@0
   283
		|| (aKeyData.iKeyCode==EKeyNull)
sl@0
   284
		|| (iCurState!=EStateNormal)
sl@0
   285
		|| (iCurState!=oldState))
sl@0
   286
		{
sl@0
   287
		iCurModifiers&=~(EModifierAutorepeatable);
sl@0
   288
		}
sl@0
   289
sl@0
   290
    // convert ctrl-space to EKeyNull and clear PureKeycode modifier
sl@0
   291
    if(aKeyData.iKeyCode==EKeySpace && iCurModifiers&EModifierCtrl)
sl@0
   292
        {
sl@0
   293
        aKeyData.iKeyCode=EKeyNull;
sl@0
   294
        iCurModifiers&=~EModifierPureKeycode;
sl@0
   295
        }
sl@0
   296
sl@0
   297
    aKeyData.iModifiers=iCurModifiers;
sl@0
   298
sl@0
   299
    iCurModifiers&=(KPersistentModifiers|KRotationModifiers); // only keep persistent and rotation modifiers
sl@0
   300
sl@0
   301
#if defined(__WINS__)
sl@0
   302
	if (ret)
sl@0
   303
        {
sl@0
   304
        if (charCode && (iCurModifiers & KRotationModifiers))
sl@0
   305
            {
sl@0
   306
            TKeyData keyData = aKeyData;
sl@0
   307
            keyData.iKeyCode = charCode;
sl@0
   308
            aCaptureKeys.ProcessCaptureKeys(keyData);
sl@0
   309
			// Pass the key capture data to the argument
sl@0
   310
			aKeyData.iApp = keyData.iApp;
sl@0
   311
			aKeyData.iHandle = keyData.iHandle;
sl@0
   312
			aKeyData.iIsCaptureKey = keyData.iIsCaptureKey;
sl@0
   313
            }
sl@0
   314
        else
sl@0
   315
            aCaptureKeys.ProcessCaptureKeys(aKeyData);
sl@0
   316
        }
sl@0
   317
#else
sl@0
   318
	if (ret)
sl@0
   319
		aCaptureKeys.ProcessCaptureKeys(aKeyData);
sl@0
   320
#endif
sl@0
   321
sl@0
   322
	return(ret);
sl@0
   323
	}
sl@0
   324
//
sl@0
   325
// A miscellaneous collection of classes used in key translation
sl@0
   326
//
sl@0
   327
TCharExtended &TCharExtended::operator=(TUint aChar)
sl@0
   328
	{
sl@0
   329
	SetChar(aChar);
sl@0
   330
	return *this;
sl@0
   331
	}
sl@0
   332
//
sl@0
   333
TBool TCharExtended::IsDigitGivenRadix(TRadix aRadix) const
sl@0
   334
// Returns true if the character is a digit given the aRadix
sl@0
   335
	{
sl@0
   336
	switch (aRadix)
sl@0
   337
		{
sl@0
   338
	case EBinary:
sl@0
   339
		return (TBool)((TUint(*this)==(TUint)'0') || (TUint(*this)==(TUint)'1'));
sl@0
   340
	case EOctal:
sl@0
   341
		return (TBool)(IsDigit() && (TUint(*this)!=(TUint)'8') && (TUint(*this)!=(TUint)'9'));
sl@0
   342
	case EDecimal:
sl@0
   343
		return IsDigit();
sl@0
   344
	case EHex:
sl@0
   345
		return IsHexDigit();
sl@0
   346
	default:
sl@0
   347
		return EFalse;
sl@0
   348
		}
sl@0
   349
	}
sl@0
   350
//
sl@0
   351
TBool TCharExtended::IsModifier() const
sl@0
   352
	{
sl@0
   353
	switch ((TUint)(*this))
sl@0
   354
		{
sl@0
   355
		case EKeyLeftShift:
sl@0
   356
		case EKeyLeftFunc:
sl@0
   357
		case EKeyLeftCtrl:
sl@0
   358
		case EKeyLeftAlt:
sl@0
   359
		case EKeyRightShift:
sl@0
   360
		case EKeyRightFunc:
sl@0
   361
		case EKeyRightCtrl:
sl@0
   362
		case EKeyRightAlt:
sl@0
   363
		case EKeyCapsLock:
sl@0
   364
		case EKeyNumLock:
sl@0
   365
		case EKeyScrollLock:
sl@0
   366
		case EKeyKeyboardExtend:
sl@0
   367
			return ETrue;
sl@0
   368
		default:
sl@0
   369
			return EFalse;
sl@0
   370
		}
sl@0
   371
	}
sl@0
   372
//
sl@0
   373
TInt TCharExtended::DigitValue() const
sl@0
   374
// Return the numeric value of the character if it is a digit, otherwise an errorcode
sl@0
   375
	{
sl@0
   376
	if (IsDigit())
sl@0
   377
		
sl@0
   378
		return (TInt(*this))-48;
sl@0
   379
	else if ((TInt(*this)>='A') && (TUint(*this)<='F'))
sl@0
   380
		return (TInt(*this))+10-'A';
sl@0
   381
	else if ((TInt(*this)>='a') && (TUint(*this)<='f'))
sl@0
   382
		return (TInt(*this))+10-'a';
sl@0
   383
	else
sl@0
   384
		return KErrArgument;
sl@0
   385
	}
sl@0
   386
//
sl@0
   387
TBool TCharExtended::MatchesPattern(const TKeyCodePattern &aKeyCodePattern, TRadix aRadix) const
sl@0
   388
// Return true if the character matches the given pattern
sl@0
   389
	{
sl@0
   390
	switch (aKeyCodePattern.iPattern)
sl@0
   391
		{
sl@0
   392
	case EAnyKey:
sl@0
   393
		return ETrue;
sl@0
   394
	case EAnyAlphaNumeric:
sl@0
   395
		return IsAlphaDigit();
sl@0
   396
	case EAnyAlpha:
sl@0
   397
		return IsAlpha();
sl@0
   398
	case EAnyAlphaLowerCase:
sl@0
   399
		return IsLower();
sl@0
   400
	case EAnyAlphaUpperCase:
sl@0
   401
		return IsUpper();
sl@0
   402
	case EAnyDecimalDigit:
sl@0
   403
		return IsDigit();
sl@0
   404
	case EAnyDigitGivenRadix:
sl@0
   405
		return IsDigitGivenRadix(aRadix);
sl@0
   406
	case EAnyModifierKey:
sl@0
   407
		return IsModifier();
sl@0
   408
    case EMatchLeftOrRight:
sl@0
   409
        return (TBool)(TUint(*this)==aKeyCodePattern.iKeyCode || TUint(*this)==(aKeyCodePattern.iKeyCode+(TUint)1));
sl@0
   410
	case EMatchKey:
sl@0
   411
		return (TBool)(TUint(*this)==aKeyCodePattern.iKeyCode);
sl@0
   412
	case EMatchKeyCaseInsens:
sl@0
   413
		return (TBool)(User::LowerCase((TUint)*this)==User::LowerCase(aKeyCodePattern.iKeyCode));
sl@0
   414
	default:
sl@0
   415
		return EFalse;
sl@0
   416
		}
sl@0
   417
	}
sl@0
   418
//
sl@0
   419
typedef void (*TLibFnDataSetting)(TRadix &aRadix,TCtrlDigitsTermination &aCtrlDigitsTermination,TInt &aDefaultCtrlDigitsMaxCount,
sl@0
   420
							      TInt &aMaximumCtrlDigitsMaxCount);
sl@0
   421
sl@0
   422
void TCtrlDigits::Update(RLibrary aLibrary)
sl@0
   423
	{
sl@0
   424
sl@0
   425
	((TLibFnDataSetting)aLibrary.Lookup(EKeyDataSettings))(iRadix,iTermination,iMaxCount,iMaximumCtrlDigitsMaxCount);
sl@0
   426
	iCount=0;
sl@0
   427
	iErrorFlag=EFalse;
sl@0
   428
	iDigits=0L;
sl@0
   429
	};
sl@0
   430
//
sl@0
   431
TCtrlDigits::TCtrlDigits()
sl@0
   432
	{
sl@0
   433
	};
sl@0
   434
//
sl@0
   435
void TCtrlDigits::Reset()
sl@0
   436
// Reset to 0
sl@0
   437
	{
sl@0
   438
sl@0
   439
	iCount=0;
sl@0
   440
	iErrorFlag=EFalse;
sl@0
   441
	iDigits=0L;
sl@0
   442
	};
sl@0
   443
//
sl@0
   444
void TCtrlDigits::AppendDigit(TUint aKeyCode, TUint aModifiers)
sl@0
   445
// Append the given digit to the current digits
sl@0
   446
	{
sl@0
   447
sl@0
   448
	TCharExtended ch=aKeyCode;
sl@0
   449
	iCount++;
sl@0
   450
	iDigits*=iRadix;
sl@0
   451
	iDigits+=ch.DigitValue();
sl@0
   452
	iErrorFlag=(TBool)(iErrorFlag
sl@0
   453
					  || !ch.IsDigitGivenRadix(iRadix)
sl@0
   454
					  || (iTermination==ETerminationByCtrlUp)
sl@0
   455
						  && ((aModifiers&EModifierCtrl)==0));
sl@0
   456
	}
sl@0
   457
//
sl@0
   458
TBool TCtrlDigits::Terminated(TInt aModifiers) const
sl@0
   459
// Return true if the digits have been terminated and are ready to return as a keyCode
sl@0
   460
	{
sl@0
   461
	return (TBool)( ((iTermination==ETerminationByCount) && (iCount>=iMaxCount))
sl@0
   462
				 || ((iTermination==ETerminationByCtrlUp) && (aModifiers&EModifierCtrl)==0)
sl@0
   463
                 || (iCount>=iMaximumCtrlDigitsMaxCount) );
sl@0
   464
	}
sl@0
   465
//
sl@0
   466
TUint TCtrlDigits::SetStateToCtrlDigits() const
sl@0
   467
// Return either "EStateCtrlDigitsUntilCount" or "EStateCtrlDigitsUntilCtrlUp"
sl@0
   468
// according to the current termination type
sl@0
   469
	{
sl@0
   470
	switch (iTermination)
sl@0
   471
		{
sl@0
   472
	case ETerminationByCount:
sl@0
   473
		return EStateCtrlDigitsUntilCount;
sl@0
   474
	case ETerminationByCtrlUp:
sl@0
   475
		return EStateCtrlDigitsUntilCtrlUp;
sl@0
   476
	default:
sl@0
   477
		return EStateNormal;
sl@0
   478
		}
sl@0
   479
	}
sl@0
   480
//
sl@0
   481
// Two classes that provide operations for accessing the keyboard configuration tables
sl@0
   482
//
sl@0
   483
typedef void (*TLibFnDataConv)(SConvTable &aConvTable, TUint &aConvTableFirstScanCode,TUint &aConvTableLastScanCode,
sl@0
   484
							   SScanCodeBlockList &aKeypadScanCode,SKeyCodeList &aNonAutorepKeyCodes);
sl@0
   485
//
sl@0
   486
void TConvTable::Update(RLibrary aLibrary)
sl@0
   487
#pragma warning (disable: 4705)
sl@0
   488
	{
sl@0
   489
#pragma warning (default: 4705)
sl@0
   490
	((TLibFnDataConv)aLibrary.Lookup(EKeyDataConv))(iConvTable,iFirstScanCode,iLastScanCode,iKeypadScanCodes,iNonAutorepKeyCodes);
sl@0
   491
	}
sl@0
   492
//
sl@0
   493
//
sl@0
   494
TConvTable::TConvTable()
sl@0
   495
#pragma warning (disable: 4705)
sl@0
   496
	{
sl@0
   497
#pragma warning (default: 4705)
sl@0
   498
	}
sl@0
   499
//
sl@0
   500
TBool TConvTable::onKeypad(TUint aScanCode) const
sl@0
   501
// Return True if the given aScanCode is on the keypad
sl@0
   502
	{
sl@0
   503
	for (TUint i=0; i<iKeypadScanCodes.numBlocks; i++)
sl@0
   504
		if ((aScanCode>=iKeypadScanCodes.pblocks[i].firstScanCode)
sl@0
   505
			&& (aScanCode<=iKeypadScanCodes.pblocks[i].lastScanCode))
sl@0
   506
			{
sl@0
   507
			return ETrue;
sl@0
   508
			}
sl@0
   509
sl@0
   510
	return EFalse;
sl@0
   511
	}
sl@0
   512
//
sl@0
   513
TBool TConvTable::autorepeatable(TUint aKeyCode) const
sl@0
   514
// Return True if the given aKeyCode is autorepeatable
sl@0
   515
	{
sl@0
   516
	for (TUint i=0; i<iNonAutorepKeyCodes.numKeyCodes; i++)
sl@0
   517
		if (aKeyCode==iNonAutorepKeyCodes.pkeyCodes[i])
sl@0
   518
			return EFalse;
sl@0
   519
sl@0
   520
	return ETrue;
sl@0
   521
	}
sl@0
   522
//
sl@0
   523
SConvKeyData TConvTable::Convert(TUint aScanCode, const TInt &aModifiers) const
sl@0
   524
// Convert the given aScanCode and aModifiers into a keyCode and aModifiers
sl@0
   525
	{
sl@0
   526
	SConvKeyData returnVal;
sl@0
   527
	returnVal.keyCode=EKeyNull;
sl@0
   528
	returnVal.modifiers=0;
sl@0
   529
sl@0
   530
	for (TUint i=0; i<iConvTable.numNodes; i++)
sl@0
   531
        {
sl@0
   532
		if (MatchesMaskedValue(aModifiers,iConvTable.pnodes[i].maskedModifiers))
sl@0
   533
            {
sl@0
   534
			for (TUint j=0; j<iConvTable.pnodes[i].numSubTables; j++)
sl@0
   535
				{
sl@0
   536
				TUint offset=0;
sl@0
   537
				for (TUint k=0; k<iConvTable.pnodes[i].ppsubTables[j]->scanCodes.numBlocks; k++)
sl@0
   538
                    {
sl@0
   539
					if ((aScanCode>=iConvTable.pnodes[i].ppsubTables[j]->scanCodes.pblocks[k].firstScanCode) &&
sl@0
   540
						(aScanCode<=iConvTable.pnodes[i].ppsubTables[j]->scanCodes.pblocks[k].lastScanCode))
sl@0
   541
						{
sl@0
   542
						returnVal.keyCode=iConvTable.pnodes[i].ppsubTables[j]->pkeyCode[offset+
sl@0
   543
										(aScanCode-iConvTable.pnodes[i].ppsubTables[j]->scanCodes.pblocks[k].firstScanCode)];
sl@0
   544
						if (onKeypad(aScanCode))
sl@0
   545
							returnVal.modifiers|=(EModifierKeypad);
sl@0
   546
						if (autorepeatable(returnVal.keyCode))
sl@0
   547
							returnVal.modifiers|=(EModifierAutorepeatable);
sl@0
   548
sl@0
   549
                        // check if ctrl key pressed and keycode has not been modified due to ctrl key
sl@0
   550
                        if (aModifiers&EModifierCtrl && !(iConvTable.pnodes[i].maskedModifiers.iMask&EModifierCtrl))
sl@0
   551
                            returnVal.modifiers|=(EModifierPureKeycode);
sl@0
   552
						return returnVal;
sl@0
   553
						}
sl@0
   554
					else
sl@0
   555
						offset+=iConvTable.pnodes[i].ppsubTables[j]->scanCodes.pblocks[k].lastScanCode-
sl@0
   556
								iConvTable.pnodes[i].ppsubTables[j]->scanCodes.pblocks[k].firstScanCode+1;
sl@0
   557
                    }
sl@0
   558
				}
sl@0
   559
            }
sl@0
   560
        }
sl@0
   561
	return returnVal;
sl@0
   562
	}
sl@0
   563
//
sl@0
   564
SConvKeyData TConvTable::ConvertBaseCase(TUint aScanCode, const TInt &aModifiers) const
sl@0
   565
// As TConvTable::Convert above, except that all input aModifiers are ignored except for EModifierNumlock
sl@0
   566
	{
sl@0
   567
	if (aModifiers&EModifierNumLock)
sl@0
   568
		return Convert(aScanCode, EModifierNumLock);
sl@0
   569
	else
sl@0
   570
		return Convert(aScanCode, 0);
sl@0
   571
	}
sl@0
   572
sl@0
   573
typedef void (*TLibFnDataFunc)(SFuncTables &aFuncTables);
sl@0
   574
sl@0
   575
void TFuncTable::Update(RLibrary aLibrary)
sl@0
   576
#pragma warning (disable: 4705)
sl@0
   577
	{
sl@0
   578
#pragma warning (default: 4705)
sl@0
   579
	((TLibFnDataFunc)aLibrary.Lookup(EKeyDataFunc))(iFuncTables);
sl@0
   580
	}
sl@0
   581
//
sl@0
   582
TFuncTable::TFuncTable()
sl@0
   583
#pragma warning (disable: 4705)
sl@0
   584
	{
sl@0
   585
#pragma warning (default: 4705)
sl@0
   586
	}
sl@0
   587
//
sl@0
   588
SFuncTableEntry TFuncTable::getDefault(const TCharExtended &aChar, const TInt &aModifiers) const
sl@0
   589
// Get the default table entry. Should be called if passing through a normal function-table did
sl@0
   590
// not trap these parameters.
sl@0
   591
	{
sl@0
   592
    TUint i=0;
sl@0
   593
	while(i<iFuncTables.defaultTable.numEntries)
sl@0
   594
        {
sl@0
   595
		if (aChar.MatchesPattern(iFuncTables.defaultTable.pentries[i].keyCodePattern)
sl@0
   596
			&& MatchesMaskedValue(aModifiers,iFuncTables.defaultTable.pentries[i].maskedModifiers))
sl@0
   597
			{
sl@0
   598
		   	break;
sl@0
   599
			}
sl@0
   600
        i++;
sl@0
   601
        }
sl@0
   602
   	return iFuncTables.defaultTable.pentries[i];
sl@0
   603
	}
sl@0
   604
sl@0
   605
SFunc TFuncTable::GetModifierFunc(const TCharExtended &aChar, const TInt &aModifiers) const
sl@0
   606
// Pass through the modifier table, returning the first table entry matching the given parameters.
sl@0
   607
	{
sl@0
   608
	SFuncTableEntry defaultTableEntry=getDefault(aChar, aModifiers);
sl@0
   609
	SFunc returnVal = { 0, 0, 0 };
sl@0
   610
	returnVal.func=defaultTableEntry.funcAndNewState.func;
sl@0
   611
	returnVal.funcParam=defaultTableEntry.funcAndNewState.funcParam;
sl@0
   612
sl@0
   613
	for (TUint i=0; i<iFuncTables.modifierTable.numEntries; i++)
sl@0
   614
		if (aChar.MatchesPattern(iFuncTables.modifierTable.pentries[i].keyCodePattern)
sl@0
   615
			&& MatchesMaskedValue(aModifiers,iFuncTables.modifierTable.pentries[i].maskedModifiers))
sl@0
   616
			{
sl@0
   617
			returnVal.func=iFuncTables.modifierTable.pentries[i].funcAndNewState.func;
sl@0
   618
			returnVal.funcParam=iFuncTables.modifierTable.pentries[i].funcAndNewState.funcParam;
sl@0
   619
		   	return returnVal;
sl@0
   620
			}
sl@0
   621
   	return returnVal;
sl@0
   622
	}
sl@0
   623
sl@0
   624
SFuncAndState TFuncTable::GetGeneralFuncAndState(const TCharExtended &aChar, const TInt &aModifiers,
sl@0
   625
																		TUint aCurState, TRadix aRadix) const
sl@0
   626
// Pass through the table corresponding to the current keyboard state, returning the first
sl@0
   627
// table entry matching the given parameters.
sl@0
   628
	{
sl@0
   629
	for (TUint i=0; i<iFuncTables.pgenFuncTables[aCurState].numEntries; i++)
sl@0
   630
		if (aChar.MatchesPattern(iFuncTables.pgenFuncTables[aCurState].pentries[i].keyCodePattern, aRadix)
sl@0
   631
			&& MatchesMaskedValue(aModifiers,iFuncTables.pgenFuncTables[aCurState].pentries[i].maskedModifiers))
sl@0
   632
			{
sl@0
   633
			return iFuncTables.pgenFuncTables[aCurState].pentries[i].funcAndNewState;
sl@0
   634
			}
sl@0
   635
   	return getDefault(aChar, aModifiers).funcAndNewState;
sl@0
   636
	}
sl@0
   637
sl@0
   638
sl@0
   639
TInt CKeyTranslatorX::ChangeKeyData(const TDesC& aLibName)
sl@0
   640
//
sl@0
   641
// change keydata
sl@0
   642
//
sl@0
   643
    {	
sl@0
   644
sl@0
   645
	if(aLibName.Length()==0) // Back to default KeyData
sl@0
   646
		{
sl@0
   647
		if (!iIsdefaultKeyData)
sl@0
   648
			{
sl@0
   649
			_LIT(KEkData,"EKDATA.DLL");
sl@0
   650
			TInt r=iDefaultKeyDataLib.Load(KEkData);
sl@0
   651
			if (r!=KErrNone && r!=KErrAlreadyExists)
sl@0
   652
				return r;
sl@0
   653
			// Check for valid KeyboardData dll type
sl@0
   654
			if(iDefaultKeyDataLib.Type()[2]!=KKeyboardDataUid) 
sl@0
   655
				{	
sl@0
   656
				iDefaultKeyDataLib.Close();
sl@0
   657
				return(KErrCorrupt);//Only due to bad rom.
sl@0
   658
				}
sl@0
   659
			iConvTable.Update(iDefaultKeyDataLib);
sl@0
   660
			iCurCtrlDigits.Update(iDefaultKeyDataLib);
sl@0
   661
			iFuncTable.Update(iDefaultKeyDataLib);
sl@0
   662
			iIsdefaultKeyData = ETrue;						// EKeyData status	 
sl@0
   663
			iKeyDataLib.Close();							// Close previously loaded keydata
sl@0
   664
			}
sl@0
   665
		return(KErrNone);		
sl@0
   666
		}
sl@0
   667
//	
sl@0
   668
	RLibrary lib;
sl@0
   669
	TInt res=lib.Load(aLibName); 
sl@0
   670
	if (res!=KErrNone && res!=KErrAlreadyExists)
sl@0
   671
		return(res);
sl@0
   672
//
sl@0
   673
// Check for valid KeyboardData dll type
sl@0
   674
//
sl@0
   675
	if(lib.Type()[2]!=KKeyboardDataUid) 
sl@0
   676
		{	
sl@0
   677
		lib.Close();
sl@0
   678
		return(KErrArgument);
sl@0
   679
		}
sl@0
   680
sl@0
   681
	// Close previously loaded keydata
sl@0
   682
	if (iIsdefaultKeyData)
sl@0
   683
		{
sl@0
   684
		iIsdefaultKeyData = EFalse;							// EKeyData status
sl@0
   685
		iDefaultKeyDataLib.Close();
sl@0
   686
		}
sl@0
   687
    else
sl@0
   688
		iKeyDataLib.Close();
sl@0
   689
sl@0
   690
	iKeyDataLib=lib;
sl@0
   691
	iConvTable.Update(lib);
sl@0
   692
	iCurCtrlDigits.Update(lib);
sl@0
   693
	iFuncTable.Update(lib);
sl@0
   694
	return(KErrNone);		
sl@0
   695
    }
sl@0
   696