Bug 574340 Cleaning up nsKeyboardLayout which doesn't use our coding style. Part.2 Remove unnecessary white spaces and wrap long lines r=jmatheis, a2=joe

This commit is contained in:
Masayuki Nakano 2010-07-27 21:48:59 +09:00
Родитель 0783d8fb07
Коммит e0801246f6
2 изменённых файлов: 265 добавлений и 203 удалений

Просмотреть файл

@ -62,34 +62,35 @@ class DeadKeyTable
friend class KeyboardLayout;
PRUint16 mEntries;
// KeyboardLayout::AddDeadKeyTable () will allocate as many entries as
// KeyboardLayout::AddDeadKeyTable() will allocate as many entries as
// required. It is the only way to create new DeadKeyTable instances.
DeadKeyEntry mTable [1];
DeadKeyEntry mTable[1];
void Init (const DeadKeyEntry* aDeadKeyArray, PRUint32 aEntries)
void Init(const DeadKeyEntry* aDeadKeyArray, PRUint32 aEntries)
{
mEntries = aEntries;
memcpy (mTable, aDeadKeyArray, aEntries * sizeof (DeadKeyEntry));
memcpy(mTable, aDeadKeyArray, aEntries * sizeof(DeadKeyEntry));
}
static PRUint32 SizeInBytes (PRUint32 aEntries)
static PRUint32 SizeInBytes(PRUint32 aEntries)
{
return offsetof (DeadKeyTable, mTable) + aEntries * sizeof (DeadKeyEntry);
return offsetof(DeadKeyTable, mTable) + aEntries * sizeof(DeadKeyEntry);
}
public:
PRUint32 Entries () const
PRUint32 Entries() const
{
return mEntries;
}
PRBool IsEqual (const DeadKeyEntry* aDeadKeyArray, PRUint32 aEntries) const
PRBool IsEqual(const DeadKeyEntry* aDeadKeyArray, PRUint32 aEntries) const
{
return (mEntries == aEntries &&
memcmp (mTable, aDeadKeyArray, aEntries * sizeof (DeadKeyEntry)) == 0);
memcmp(mTable, aDeadKeyArray,
aEntries * sizeof(DeadKeyEntry)) == 0);
}
PRUnichar GetCompositeChar (PRUnichar aBaseChar) const;
PRUnichar GetCompositeChar(PRUnichar aBaseChar) const;
};
@ -97,7 +98,7 @@ public:
inline PRUnichar
VirtualKey::GetCompositeChar(PRUint8 aShiftState, PRUnichar aBaseChar) const
{
return mShiftStates [aShiftState].DeadKey.Table->GetCompositeChar (aBaseChar);
return mShiftStates[aShiftState].DeadKey.Table->GetCompositeChar(aBaseChar);
}
const DeadKeyTable*
@ -109,11 +110,11 @@ VirtualKey::MatchingDeadKeyTable(const DeadKeyEntry* aDeadKeyArray,
for (PRUint32 shiftState = 0; shiftState < 16; shiftState++)
{
if (IsDeadKey (shiftState))
if (IsDeadKey(shiftState))
{
const DeadKeyTable* dkt = mShiftStates [shiftState].DeadKey.Table;
const DeadKeyTable* dkt = mShiftStates[shiftState].DeadKey.Table;
if (dkt && dkt->IsEqual (aDeadKeyArray, aEntries))
if (dkt && dkt->IsEqual(aDeadKeyArray, aEntries))
return dkt;
}
}
@ -126,29 +127,31 @@ VirtualKey::SetNormalChars(PRUint8 aShiftState,
const PRUnichar* aChars,
PRUint32 aNumOfChars)
{
NS_ASSERTION (aShiftState < NS_ARRAY_LENGTH (mShiftStates), "invalid index");
NS_ASSERTION(aShiftState < NS_ARRAY_LENGTH(mShiftStates), "invalid index");
SetDeadKey (aShiftState, PR_FALSE);
SetDeadKey(aShiftState, PR_FALSE);
for (PRUint32 c1 = 0; c1 < aNumOfChars; c1++)
{
// Ignore legacy non-printable control characters
mShiftStates [aShiftState].Normal.Chars [c1] = (aChars [c1] >= 0x20) ? aChars [c1] : 0;
mShiftStates[aShiftState].Normal.Chars[c1] =
(aChars[c1] >= 0x20) ? aChars[c1] : 0;
}
for (PRUint32 c2 = aNumOfChars; c2 < NS_ARRAY_LENGTH (mShiftStates [aShiftState].Normal.Chars); c2++)
mShiftStates [aShiftState].Normal.Chars [c2] = 0;
for (PRUint32 c2 = aNumOfChars;
c2 < NS_ARRAY_LENGTH(mShiftStates[aShiftState].Normal.Chars); c2++)
mShiftStates[aShiftState].Normal.Chars[c2] = 0;
}
void
VirtualKey::SetDeadChar(PRUint8 aShiftState, PRUnichar aDeadChar)
{
NS_ASSERTION (aShiftState < NS_ARRAY_LENGTH (mShiftStates), "invalid index");
SetDeadKey (aShiftState, PR_TRUE);
mShiftStates [aShiftState].DeadKey.DeadChar = aDeadChar;
mShiftStates [aShiftState].DeadKey.Table = nsnull;
NS_ASSERTION(aShiftState < NS_ARRAY_LENGTH(mShiftStates), "invalid index");
SetDeadKey(aShiftState, PR_TRUE);
mShiftStates[aShiftState].DeadKey.DeadChar = aDeadChar;
mShiftStates[aShiftState].DeadKey.Table = nsnull;
}
PRUint32
@ -157,12 +160,13 @@ VirtualKey::GetUniChars(PRUint8 aShiftState,
PRUint8* aFinalShiftState) const
{
*aFinalShiftState = aShiftState;
PRUint32 numOfChars = GetNativeUniChars (aShiftState, aUniChars);
PRUint32 numOfChars = GetNativeUniChars(aShiftState, aUniChars);
if (aShiftState & (eAlt | eCtrl))
{
PRUnichar unshiftedChars [5];
PRUint32 numOfUnshiftedChars = GetNativeUniChars (aShiftState & ~(eAlt | eCtrl), unshiftedChars);
PRUnichar unshiftedChars[5];
PRUint32 numOfUnshiftedChars =
GetNativeUniChars(aShiftState & ~(eAlt | eCtrl), unshiftedChars);
if (numOfChars)
{
@ -174,8 +178,8 @@ VirtualKey::GetUniChars(PRUint8 aShiftState,
// characters with this key combination.)
*aFinalShiftState &= ~(eAlt | eCtrl);
} else if (!(numOfChars == numOfUnshiftedChars &&
memcmp (aUniChars, unshiftedChars,
numOfChars * sizeof (PRUnichar)) == 0)) {
memcmp(aUniChars, unshiftedChars,
numOfChars * sizeof(PRUnichar)) == 0)) {
// Otherwise, we should consume the Alt key state and the Ctrl key state
// only when the shifted chars and unshifted chars are different.
*aFinalShiftState &= ~(eAlt | eCtrl);
@ -184,7 +188,8 @@ VirtualKey::GetUniChars(PRUint8 aShiftState,
{
if (numOfUnshiftedChars)
{
memcpy (aUniChars, unshiftedChars, numOfUnshiftedChars * sizeof (PRUnichar));
memcpy(aUniChars, unshiftedChars,
numOfUnshiftedChars * sizeof(PRUnichar));
numOfChars = numOfUnshiftedChars;
}
}
@ -198,21 +203,21 @@ PRUint32
VirtualKey::GetNativeUniChars(PRUint8 aShiftState,
PRUnichar* aUniChars) const
{
if (IsDeadKey (aShiftState))
if (IsDeadKey(aShiftState))
{
if (aUniChars)
aUniChars [0] = mShiftStates [aShiftState].DeadKey.DeadChar;
aUniChars[0] = mShiftStates[aShiftState].DeadKey.DeadChar;
return 1;
} else
{
PRUint32 cnt = 0;
while (cnt < NS_ARRAY_LENGTH (mShiftStates [aShiftState].Normal.Chars) &&
mShiftStates [aShiftState].Normal.Chars [cnt])
while (cnt < NS_ARRAY_LENGTH(mShiftStates[aShiftState].Normal.Chars) &&
mShiftStates[aShiftState].Normal.Chars[cnt])
{
if (aUniChars)
aUniChars [cnt] = mShiftStates [aShiftState].Normal.Chars [cnt];
aUniChars[cnt] = mShiftStates[aShiftState].Normal.Chars[cnt];
cnt++;
}
@ -233,13 +238,13 @@ KeyboardLayout::KeyboardLayout() :
KeyboardLayout::~KeyboardLayout()
{
ReleaseDeadKeyTables ();
ReleaseDeadKeyTables();
}
PRBool
KeyboardLayout::IsPrintableCharKey(PRUint8 aVirtualKey)
{
return GetKeyIndex (aVirtualKey) >= 0;
return GetKeyIndex(aVirtualKey) >= 0;
}
PRBool
@ -251,31 +256,36 @@ KeyboardLayout::IsNumpadKey(PRUint8 aVirtualKey)
void
KeyboardLayout::OnKeyDown(PRUint8 aVirtualKey)
{
mLastVirtualKeyIndex = GetKeyIndex (aVirtualKey);
mLastVirtualKeyIndex = GetKeyIndex(aVirtualKey);
if (mLastVirtualKeyIndex < 0)
{
// Does not produce any printable characters, but still preserves the dead-key state.
// Does not produce any printable characters, but still preserves the
// dead-key state.
mNumOfChars = 0;
} else
{
BYTE kbdState [256];
BYTE kbdState[256];
if (::GetKeyboardState (kbdState))
if (::GetKeyboardState(kbdState))
{
mLastShiftState = GetShiftState (kbdState);
mLastShiftState = GetShiftState(kbdState);
if (mVirtualKeys [mLastVirtualKeyIndex].IsDeadKey (mLastShiftState))
if (mVirtualKeys[mLastVirtualKeyIndex].IsDeadKey(mLastShiftState))
{
if (mActiveDeadKey >= 0)
{
// Dead-key followed by another dead-key. Reset dead-key state and return both dead-key characters.
PRInt32 activeDeadKeyIndex = GetKeyIndex (mActiveDeadKey);
mVirtualKeys [activeDeadKeyIndex].GetUniChars (mDeadKeyShiftState, mChars, mShiftStates);
mVirtualKeys [mLastVirtualKeyIndex].GetUniChars (mLastShiftState, &mChars [1], &mShiftStates [1]);
// Dead-key followed by another dead-key. Reset dead-key state and
// return both dead-key characters.
PRInt32 activeDeadKeyIndex = GetKeyIndex(mActiveDeadKey);
mVirtualKeys[activeDeadKeyIndex].GetUniChars(mDeadKeyShiftState,
mChars, mShiftStates);
mVirtualKeys[mLastVirtualKeyIndex].GetUniChars(mLastShiftState,
&mChars[1],
&mShiftStates[1]);
mNumOfChars = 2;
DeactivateDeadKeyState ();
DeactivateDeadKeyState();
} else
{
// Dead-key state activated. No characters generated.
@ -286,38 +296,46 @@ KeyboardLayout::OnKeyDown(PRUint8 aVirtualKey)
} else
{
PRUint8 finalShiftState;
PRUnichar uniChars [5];
PRUint32 numOfBaseChars = mVirtualKeys [mLastVirtualKeyIndex].GetUniChars (mLastShiftState, uniChars, &finalShiftState);
PRUnichar uniChars[5];
PRUint32 numOfBaseChars =
mVirtualKeys[mLastVirtualKeyIndex].GetUniChars(mLastShiftState,
uniChars,
&finalShiftState);
if (mActiveDeadKey >= 0)
{
PRInt32 activeDeadKeyIndex = GetKeyIndex (mActiveDeadKey);
PRInt32 activeDeadKeyIndex = GetKeyIndex(mActiveDeadKey);
// Dead-key was active. See if pressed base character does produce valid composite character.
PRUnichar compositeChar = (numOfBaseChars == 1 && uniChars [0]) ?
mVirtualKeys [activeDeadKeyIndex].GetCompositeChar (mDeadKeyShiftState, uniChars [0]) : 0;
// Dead-key was active. See if pressed base character does produce
// valid composite character.
PRUnichar compositeChar = (numOfBaseChars == 1 && uniChars[0]) ?
mVirtualKeys[activeDeadKeyIndex].
GetCompositeChar(mDeadKeyShiftState, uniChars[0]) : 0;
if (compositeChar)
{
// Active dead-key and base character does produce exactly one composite character.
mChars [0] = compositeChar;
mShiftStates [0] = finalShiftState;
// Active dead-key and base character does produce exactly one
// composite character.
mChars[0] = compositeChar;
mShiftStates[0] = finalShiftState;
mNumOfChars = 1;
} else
{
// There is no valid dead-key and base character combination. Return dead-key character followed by base character.
mVirtualKeys [activeDeadKeyIndex].GetUniChars (mDeadKeyShiftState, mChars, mShiftStates);
memcpy (&mChars [1], uniChars, numOfBaseChars * sizeof (PRUnichar));
memset (&mShiftStates [1], finalShiftState, numOfBaseChars);
// There is no valid dead-key and base character combination.
// Return dead-key character followed by base character.
mVirtualKeys[activeDeadKeyIndex].GetUniChars(mDeadKeyShiftState,
mChars, mShiftStates);
memcpy(&mChars[1], uniChars, numOfBaseChars * sizeof(PRUnichar));
memset(&mShiftStates[1], finalShiftState, numOfBaseChars);
mNumOfChars = numOfBaseChars + 1;
}
DeactivateDeadKeyState ();
DeactivateDeadKeyState();
} else
{
// No dead-keys are active. Just return the produced characters.
memcpy (mChars, uniChars, numOfBaseChars * sizeof (PRUnichar));
memset (mShiftStates, finalShiftState, numOfBaseChars);
memcpy(mChars, uniChars, numOfBaseChars * sizeof(PRUnichar));
memset(mShiftStates, finalShiftState, numOfBaseChars);
mNumOfChars = numOfBaseChars;
}
}
@ -330,10 +348,10 @@ KeyboardLayout::GetUniChars(PRUnichar* aUniChars,
PRUint8* aShiftStates,
PRUint32 aMaxChars) const
{
PRUint32 chars = PR_MIN (mNumOfChars, aMaxChars);
PRUint32 chars = PR_MIN(mNumOfChars, aMaxChars);
memcpy (aUniChars, mChars, chars * sizeof (PRUnichar));
memcpy (aShiftStates, mShiftStates, chars);
memcpy(aUniChars, mChars, chars * sizeof(PRUnichar));
memcpy(aShiftStates, mShiftStates, chars);
return chars;
}
@ -353,7 +371,7 @@ KeyboardLayout::GetUniCharsWithShiftState(PRUint8 aVirtualKey,
mVirtualKeys[key].GetUniChars(aShiftStates, uniChars, &finalShiftState);
PRUint32 chars = PR_MIN(numOfBaseChars, aMaxChars);
memcpy(aUniChars, uniChars, chars * sizeof (PRUnichar));
memcpy(aUniChars, uniChars, chars * sizeof(PRUnichar));
return chars;
}
@ -367,104 +385,116 @@ KeyboardLayout::LoadLayout(HKL aLayout)
mKeyboardLayout = aLayout;
PRUint32 shiftState;
BYTE kbdState [256];
BYTE originalKbdState [256];
PRUint16 shiftStatesWithDeadKeys = 0; // Bitfield with all shift states that have at least one dead-key.
PRUint16 shiftStatesWithBaseChars = 0; // Bitfield with all shift states that produce any possible dead-key base characters.
BYTE kbdState[256];
BYTE originalKbdState[256];
// Bitfield with all shift states that have at least one dead-key.
PRUint16 shiftStatesWithDeadKeys = 0;
// Bitfield with all shift states that produce any possible dead-key base
// characters.
PRUint16 shiftStatesWithBaseChars = 0;
memset (kbdState, 0, sizeof (kbdState));
memset(kbdState, 0, sizeof(kbdState));
mActiveDeadKey = -1;
mNumOfChars = 0;
ReleaseDeadKeyTables ();
ReleaseDeadKeyTables();
::GetKeyboardState (originalKbdState);
::GetKeyboardState(originalKbdState);
// For each shift state gather all printable characters that are produced
// for normal case when no any dead-key is active.
for (shiftState = 0; shiftState < 16; shiftState++)
{
SetShiftState (kbdState, shiftState);
SetShiftState(kbdState, shiftState);
for (PRUint32 virtualKey = 0; virtualKey < 256; virtualKey++)
{
PRInt32 vki = GetKeyIndex (virtualKey);
PRInt32 vki = GetKeyIndex(virtualKey);
if (vki < 0)
continue;
NS_ASSERTION (vki < NS_ARRAY_LENGTH (mVirtualKeys), "invalid index");
NS_ASSERTION(vki < NS_ARRAY_LENGTH(mVirtualKeys), "invalid index");
PRUnichar uniChars [5];
PRUnichar uniChars[5];
PRInt32 rv;
rv = ::ToUnicodeEx (virtualKey, 0, kbdState, (LPWSTR)uniChars, NS_ARRAY_LENGTH (uniChars), 0, mKeyboardLayout);
rv = ::ToUnicodeEx(virtualKey, 0, kbdState, (LPWSTR)uniChars,
NS_ARRAY_LENGTH(uniChars), 0, mKeyboardLayout);
if (rv < 0) // dead-key
{
shiftStatesWithDeadKeys |= 1 << shiftState;
// Repeat dead-key to deactivate it and get its character representation.
PRUnichar deadChar [2];
// Repeat dead-key to deactivate it and get its character
// representation.
PRUnichar deadChar[2];
rv = ::ToUnicodeEx (virtualKey, 0, kbdState, (LPWSTR)deadChar, NS_ARRAY_LENGTH (deadChar), 0, mKeyboardLayout);
rv = ::ToUnicodeEx(virtualKey, 0, kbdState, (LPWSTR)deadChar,
NS_ARRAY_LENGTH(deadChar), 0, mKeyboardLayout);
NS_ASSERTION (rv == 2, "Expecting twice repeated dead-key character");
NS_ASSERTION(rv == 2, "Expecting twice repeated dead-key character");
mVirtualKeys [vki].SetDeadChar (shiftState, deadChar [0]);
mVirtualKeys[vki].SetDeadChar(shiftState, deadChar[0]);
} else
{
if (rv == 1) // dead-key can pair only with exactly one base character.
if (rv == 1) {
// dead-key can pair only with exactly one base character.
shiftStatesWithBaseChars |= 1 << shiftState;
}
mVirtualKeys [vki].SetNormalChars (shiftState, uniChars, rv);
mVirtualKeys[vki].SetNormalChars(shiftState, uniChars, rv);
}
}
}
// Now process each dead-key to find all its base characters and resulting composite characters.
// Now process each dead-key to find all its base characters and resulting
// composite characters.
for (shiftState = 0; shiftState < 16; shiftState++)
{
if (!(shiftStatesWithDeadKeys & (1 << shiftState)))
continue;
SetShiftState (kbdState, shiftState);
SetShiftState(kbdState, shiftState);
for (PRUint32 virtualKey = 0; virtualKey < 256; virtualKey++)
{
PRInt32 vki = GetKeyIndex (virtualKey);
PRInt32 vki = GetKeyIndex(virtualKey);
if (vki >= 0 && mVirtualKeys [vki].IsDeadKey (shiftState))
if (vki >= 0 && mVirtualKeys[vki].IsDeadKey(shiftState))
{
DeadKeyEntry deadKeyArray [256];
DeadKeyEntry deadKeyArray[256];
PRInt32 n = GetDeadKeyCombinations (virtualKey, kbdState, shiftStatesWithBaseChars,
deadKeyArray, NS_ARRAY_LENGTH (deadKeyArray));
PRInt32 n = GetDeadKeyCombinations(virtualKey, kbdState,
shiftStatesWithBaseChars,
deadKeyArray,
NS_ARRAY_LENGTH(deadKeyArray));
const DeadKeyTable* dkt =
mVirtualKeys[vki].MatchingDeadKeyTable(deadKeyArray, n);
const DeadKeyTable* dkt = mVirtualKeys [vki].MatchingDeadKeyTable (deadKeyArray, n);
if (!dkt)
dkt = AddDeadKeyTable (deadKeyArray, n);
dkt = AddDeadKeyTable(deadKeyArray, n);
mVirtualKeys [vki].AttachDeadKeyTable (shiftState, dkt);
mVirtualKeys[vki].AttachDeadKeyTable(shiftState, dkt);
}
}
}
::SetKeyboardState (originalKbdState);
::SetKeyboardState(originalKbdState);
}
PRUint8
KeyboardLayout::GetShiftState(const PBYTE aKbdState)
{
PRBool isShift = (aKbdState [VK_SHIFT] & 0x80) != 0;
PRBool isCtrl = (aKbdState [VK_CONTROL] & 0x80) != 0;
PRBool isAlt = (aKbdState [VK_MENU] & 0x80) != 0;
PRBool isCaps = (aKbdState [VK_CAPITAL] & 0x01) != 0;
PRBool isShift = (aKbdState[VK_SHIFT] & 0x80) != 0;
PRBool isCtrl = (aKbdState[VK_CONTROL] & 0x80) != 0;
PRBool isAlt = (aKbdState[VK_MENU] & 0x80) != 0;
PRBool isCaps = (aKbdState[VK_CAPITAL] & 0x01) != 0;
return ((isCaps << 3) | (isAlt << 2) | (isCtrl << 1) | isShift);
}
@ -472,39 +502,39 @@ KeyboardLayout::GetShiftState(const PBYTE aKbdState)
void
KeyboardLayout::SetShiftState(PBYTE aKbdState, PRUint8 aShiftState)
{
NS_ASSERTION (aShiftState < 16, "aShiftState out of range");
NS_ASSERTION(aShiftState < 16, "aShiftState out of range");
if (aShiftState & eShift)
aKbdState [VK_SHIFT] |= 0x80;
aKbdState[VK_SHIFT] |= 0x80;
else
{
aKbdState [VK_SHIFT] &= ~0x80;
aKbdState [VK_LSHIFT] &= ~0x80;
aKbdState [VK_RSHIFT] &= ~0x80;
aKbdState[VK_SHIFT] &= ~0x80;
aKbdState[VK_LSHIFT] &= ~0x80;
aKbdState[VK_RSHIFT] &= ~0x80;
}
if (aShiftState & eCtrl)
aKbdState [VK_CONTROL] |= 0x80;
aKbdState[VK_CONTROL] |= 0x80;
else
{
aKbdState [VK_CONTROL] &= ~0x80;
aKbdState [VK_LCONTROL] &= ~0x80;
aKbdState [VK_RCONTROL] &= ~0x80;
aKbdState[VK_CONTROL] &= ~0x80;
aKbdState[VK_LCONTROL] &= ~0x80;
aKbdState[VK_RCONTROL] &= ~0x80;
}
if (aShiftState & eAlt)
aKbdState [VK_MENU] |= 0x80;
aKbdState[VK_MENU] |= 0x80;
else
{
aKbdState [VK_MENU] &= ~0x80;
aKbdState [VK_LMENU] &= ~0x80;
aKbdState [VK_RMENU] &= ~0x80;
aKbdState[VK_MENU] &= ~0x80;
aKbdState[VK_LMENU] &= ~0x80;
aKbdState[VK_RMENU] &= ~0x80;
}
if (aShiftState & eCapsLock)
aKbdState [VK_CAPITAL] |= 0x01;
aKbdState[VK_CAPITAL] |= 0x01;
else
aKbdState [VK_CAPITAL] &= ~0x01;
aKbdState[VK_CAPITAL] &= ~0x01;
}
inline PRInt32
@ -529,7 +559,7 @@ KeyboardLayout::GetKeyIndex(PRUint8 aVirtualKey)
// 0xDE - VK_OEM_7 ''"' for US
// 0xDF - VK_OEM_8
static const PRInt8 xlat [256] =
static const PRInt8 xlat[256] =
{
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
//-----------------------------------------------------------------------
@ -551,7 +581,7 @@ KeyboardLayout::GetKeyIndex(PRUint8 aVirtualKey)
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // F0
};
return xlat [aVirtualKey];
return xlat[aVirtualKey];
}
int
@ -571,15 +601,17 @@ KeyboardLayout::AddDeadKeyTable(const DeadKeyEntry* aDeadKeyArray,
{
DeadKeyTableListEntry* next = mDeadKeyTableListHead;
const size_t bytes = offsetof (DeadKeyTableListEntry, data) + DeadKeyTable::SizeInBytes (aEntries);
PRUint8* p = new PRUint8 [bytes];
const size_t bytes = offsetof(DeadKeyTableListEntry, data) +
DeadKeyTable::SizeInBytes(aEntries);
PRUint8* p = new PRUint8[bytes];
mDeadKeyTableListHead = reinterpret_cast<DeadKeyTableListEntry*>(p);
mDeadKeyTableListHead->next = next;
DeadKeyTable* dkt = reinterpret_cast<DeadKeyTable*>(mDeadKeyTableListHead->data);
dkt->Init (aDeadKeyArray, aEntries);
DeadKeyTable* dkt =
reinterpret_cast<DeadKeyTable*>(mDeadKeyTableListHead->data);
dkt->Init(aDeadKeyArray, aEntries);
return dkt;
}
@ -605,14 +637,17 @@ KeyboardLayout::EnsureDeadKeyActive(PRBool aIsActive,
do
{
PRUnichar dummyChars [5];
PRUnichar dummyChars[5];
rv = ::ToUnicodeEx (aDeadKey, 0, (PBYTE)aDeadKeyKbdState, (LPWSTR)dummyChars, NS_ARRAY_LENGTH (dummyChars), 0, mKeyboardLayout);
rv = ::ToUnicodeEx(aDeadKey, 0, (PBYTE)aDeadKeyKbdState, (LPWSTR)dummyChars,
NS_ARRAY_LENGTH(dummyChars), 0, mKeyboardLayout);
// returned values:
// <0 - Dead key state is active. The keyboard driver will wait for next character.
// 1 - Previous pressed key was a valid base character that produced exactly one composite character.
// >1 - Previous pressed key does not produce any composite characters. Return dead-key character
// followed by base character(s).
// <0 - Dead key state is active. The keyboard driver will wait for next
// character.
// 1 - Previous pressed key was a valid base character that produced
// exactly one composite character.
// >1 - Previous pressed key does not produce any composite characters.
// Return dead-key character followed by base character(s).
} while ((rv < 0) != aIsActive);
return (rv < 0);
@ -624,12 +659,12 @@ KeyboardLayout::DeactivateDeadKeyState()
if (mActiveDeadKey < 0)
return;
BYTE kbdState [256];
BYTE kbdState[256];
memset (kbdState, 0, sizeof (kbdState));
SetShiftState (kbdState, mDeadKeyShiftState);
memset(kbdState, 0, sizeof(kbdState));
SetShiftState(kbdState, mDeadKeyShiftState);
EnsureDeadKeyActive (PR_FALSE, mActiveDeadKey, kbdState);
EnsureDeadKeyActive(PR_FALSE, mActiveDeadKey, kbdState);
mActiveDeadKey = -1;
}
@ -640,11 +675,11 @@ KeyboardLayout::AddDeadKeyEntry(PRUnichar aBaseChar,
PRUint32 aEntries)
{
for (PRUint32 cnt = 0; cnt < aEntries; cnt++)
if (aDeadKeyArray [cnt].BaseChar == aBaseChar)
if (aDeadKeyArray[cnt].BaseChar == aBaseChar)
return PR_FALSE;
aDeadKeyArray [aEntries].BaseChar = aBaseChar;
aDeadKeyArray [aEntries].CompositeChar = aCompositeChar;
aDeadKeyArray[aEntries].BaseChar = aBaseChar;
aDeadKeyArray[aEntries].CompositeChar = aCompositeChar;
return PR_TRUE;
}
@ -658,53 +693,63 @@ KeyboardLayout::GetDeadKeyCombinations(PRUint8 aDeadKey,
{
PRBool deadKeyActive = PR_FALSE;
PRUint32 entries = 0;
BYTE kbdState [256];
BYTE kbdState[256];
memset (kbdState, 0, sizeof (kbdState));
memset(kbdState, 0, sizeof(kbdState));
for (PRUint32 shiftState = 0; shiftState < 16; shiftState++)
{
if (!(aShiftStatesWithBaseChars & (1 << shiftState)))
continue;
SetShiftState (kbdState, shiftState);
SetShiftState(kbdState, shiftState);
for (PRUint32 virtualKey = 0; virtualKey < 256; virtualKey++)
{
PRInt32 vki = GetKeyIndex (virtualKey);
PRInt32 vki = GetKeyIndex(virtualKey);
// Dead-key can pair only with such key that produces exactly one base character.
if (vki >= 0 && mVirtualKeys [vki].GetNativeUniChars (shiftState) == 1)
// Dead-key can pair only with such key that produces exactly one base
// character.
if (vki >= 0 && mVirtualKeys[vki].GetNativeUniChars(shiftState) == 1)
{
// Ensure dead-key is in active state, when it swallows entered character and waits for the next pressed key.
if (!deadKeyActive)
deadKeyActive = EnsureDeadKeyActive (PR_TRUE, aDeadKey, aDeadKeyKbdState);
// Ensure dead-key is in active state, when it swallows entered
// character and waits for the next pressed key.
if (!deadKeyActive) {
deadKeyActive = EnsureDeadKeyActive(PR_TRUE, aDeadKey,
aDeadKeyKbdState);
}
// Depending on the character the followed the dead-key, the keyboard driver can produce
// one composite character, or a dead-key character followed by a second character.
PRUnichar compositeChars [5];
// Depending on the character the followed the dead-key, the keyboard
// driver can produce one composite character, or a dead-key character
// followed by a second character.
PRUnichar compositeChars[5];
PRInt32 rv;
rv = ::ToUnicodeEx (virtualKey, 0, kbdState, (LPWSTR)compositeChars, NS_ARRAY_LENGTH (compositeChars), 0, mKeyboardLayout);
rv = ::ToUnicodeEx(virtualKey, 0, kbdState, (LPWSTR)compositeChars,
NS_ARRAY_LENGTH(compositeChars), 0, mKeyboardLayout);
switch (rv)
{
case 0:
// This key combination does not produce any characters. The dead-key is still in active state.
// This key combination does not produce any characters. The
// dead-key is still in active state.
break;
case 1:
{
// Exactly one composite character produced. Now, when dead-key is not active, repeat the last
// character one more time to determine the base character.
PRUnichar baseChars [5];
// Exactly one composite character produced. Now, when dead-key
// is not active, repeat the last character one more time to
// determine the base character.
PRUnichar baseChars[5];
rv = ::ToUnicodeEx (virtualKey, 0, kbdState, (LPWSTR)baseChars, NS_ARRAY_LENGTH (baseChars), 0, mKeyboardLayout);
rv = ::ToUnicodeEx(virtualKey, 0, kbdState, (LPWSTR)baseChars,
NS_ARRAY_LENGTH(baseChars), 0, mKeyboardLayout);
NS_ASSERTION (rv == 1, "One base character expected");
NS_ASSERTION(rv == 1, "One base character expected");
if (rv == 1 && entries < aMaxEntries)
if (AddDeadKeyEntry (baseChars [0], compositeChars [0], aDeadKeyArray, entries))
if (AddDeadKeyEntry(baseChars[0], compositeChars[0],
aDeadKeyArray, entries))
entries++;
deadKeyActive = PR_FALSE;
@ -713,7 +758,8 @@ KeyboardLayout::GetDeadKeyCombinations(PRUint8 aDeadKey,
default:
// 1. Unexpected dead-key. Dead-key chaining is not supported.
// 2. More than one character generated. This is not a valid dead-key and base character combination.
// 2. More than one character generated. This is not a valid
// dead-key and base character combination.
deadKeyActive = PR_FALSE;
break;
}
@ -722,9 +768,10 @@ KeyboardLayout::GetDeadKeyCombinations(PRUint8 aDeadKey,
}
if (deadKeyActive)
deadKeyActive = EnsureDeadKeyActive (PR_FALSE, aDeadKey, aDeadKeyKbdState);
deadKeyActive = EnsureDeadKeyActive(PR_FALSE, aDeadKey, aDeadKeyKbdState);
NS_QuickSort (aDeadKeyArray, entries, sizeof (DeadKeyEntry), CompareDeadKeyEntries, nsnull);
NS_QuickSort(aDeadKeyArray, entries, sizeof(DeadKeyEntry),
CompareDeadKeyEntries, nsnull);
return entries;
}
@ -738,9 +785,9 @@ DeadKeyTable::GetCompositeChar(PRUnichar aBaseChar) const
for (PRUint32 cnt = 0; cnt < mEntries; cnt++)
{
if (mTable [cnt].BaseChar == aBaseChar)
return mTable [cnt].CompositeChar;
else if (mTable [cnt].BaseChar > aBaseChar)
if (mTable[cnt].BaseChar == aBaseChar)
return mTable[cnt].CompositeChar;
else if (mTable[cnt].BaseChar > aBaseChar)
break;
}

Просмотреть файл

@ -83,7 +83,7 @@ class VirtualKey
{
struct
{
PRUnichar Chars [4];
PRUnichar Chars[4];
} Normal;
struct
{
@ -92,10 +92,10 @@ class VirtualKey
} DeadKey;
};
KeyShiftState mShiftStates [16];
KeyShiftState mShiftStates[16];
PRUint16 mIsDeadKey;
void SetDeadKey (PRUint8 aShiftState, PRBool aIsDeadKey)
void SetDeadKey(PRUint8 aShiftState, PRBool aIsDeadKey)
{
if (aIsDeadKey)
mIsDeadKey |= 1 << aShiftState;
@ -104,22 +104,28 @@ class VirtualKey
}
public:
PRBool IsDeadKey (PRUint8 aShiftState) const
PRBool IsDeadKey(PRUint8 aShiftState) const
{
return (mIsDeadKey & (1 << aShiftState)) != 0;
}
void AttachDeadKeyTable (PRUint8 aShiftState, const DeadKeyTable* aDeadKeyTable)
void AttachDeadKeyTable(PRUint8 aShiftState,
const DeadKeyTable* aDeadKeyTable)
{
mShiftStates [aShiftState].DeadKey.Table = aDeadKeyTable;
mShiftStates[aShiftState].DeadKey.Table = aDeadKeyTable;
}
void SetNormalChars (PRUint8 aShiftState, const PRUnichar* aChars, PRUint32 aNumOfChars);
void SetDeadChar (PRUint8 aShiftState, PRUnichar aDeadChar);
const DeadKeyTable* MatchingDeadKeyTable (const DeadKeyEntry* aDeadKeyArray, PRUint32 aEntries) const;
inline PRUnichar GetCompositeChar (PRUint8 aShiftState, PRUnichar aBaseChar) const;
PRUint32 GetNativeUniChars (PRUint8 aShiftState, PRUnichar* aUniChars = nsnull) const;
PRUint32 GetUniChars (PRUint8 aShiftState, PRUnichar* aUniChars, PRUint8* aFinalShiftState) const;
void SetNormalChars(PRUint8 aShiftState, const PRUnichar* aChars,
PRUint32 aNumOfChars);
void SetDeadChar(PRUint8 aShiftState, PRUnichar aDeadChar);
const DeadKeyTable* MatchingDeadKeyTable(const DeadKeyEntry* aDeadKeyArray,
PRUint32 aEntries) const;
inline PRUnichar GetCompositeChar(PRUint8 aShiftState,
PRUnichar aBaseChar) const;
PRUint32 GetNativeUniChars(PRUint8 aShiftState,
PRUnichar* aUniChars = nsnull) const;
PRUint32 GetUniChars(PRUint8 aShiftState, PRUnichar* aUniChars,
PRUint8* aFinalShiftState) const;
};
@ -128,50 +134,59 @@ class KeyboardLayout
struct DeadKeyTableListEntry
{
DeadKeyTableListEntry* next;
PRUint8 data [1];
PRUint8 data[1];
};
#define NUM_OF_KEYS 50
HKL mKeyboardLayout;
VirtualKey mVirtualKeys [NUM_OF_KEYS];
VirtualKey mVirtualKeys[NUM_OF_KEYS];
DeadKeyTableListEntry* mDeadKeyTableListHead;
PRInt32 mActiveDeadKey; // -1 = no active dead-key
PRUint8 mDeadKeyShiftState;
PRInt32 mLastVirtualKeyIndex;
PRUint8 mLastShiftState;
PRUnichar mChars [5]; // Dead-key + up to 4 characters
PRUint8 mShiftStates [5];
PRUnichar mChars[5]; // Dead-key + up to 4 characters
PRUint8 mShiftStates[5];
PRUint8 mNumOfChars;
static PRUint8 GetShiftState (const PBYTE aKbdState);
static void SetShiftState (PBYTE aKbdState, PRUint8 aShiftState);
static inline PRInt32 GetKeyIndex (PRUint8 aVirtualKey);
static int CompareDeadKeyEntries (const void* aArg1, const void* aArg2, void* aData);
static PRBool AddDeadKeyEntry (PRUnichar aBaseChar, PRUnichar aCompositeChar, DeadKeyEntry* aDeadKeyArray, PRUint32 aEntries);
PRBool EnsureDeadKeyActive (PRBool aIsActive, PRUint8 aDeadKey, const PBYTE aDeadKeyKbdState);
PRUint32 GetDeadKeyCombinations (PRUint8 aDeadKey, const PBYTE aDeadKeyKbdState, PRUint16 aShiftStatesWithBaseChars,
DeadKeyEntry* aDeadKeyArray, PRUint32 aMaxEntries);
void DeactivateDeadKeyState ();
const DeadKeyTable* AddDeadKeyTable (const DeadKeyEntry* aDeadKeyArray, PRUint32 aEntries);
void ReleaseDeadKeyTables ();
static PRUint8 GetShiftState(const PBYTE aKbdState);
static void SetShiftState(PBYTE aKbdState, PRUint8 aShiftState);
static inline PRInt32 GetKeyIndex(PRUint8 aVirtualKey);
static int CompareDeadKeyEntries(const void* aArg1, const void* aArg2,
void* aData);
static PRBool AddDeadKeyEntry(PRUnichar aBaseChar, PRUnichar aCompositeChar,
DeadKeyEntry* aDeadKeyArray, PRUint32 aEntries);
PRBool EnsureDeadKeyActive(PRBool aIsActive, PRUint8 aDeadKey,
const PBYTE aDeadKeyKbdState);
PRUint32 GetDeadKeyCombinations(PRUint8 aDeadKey,
const PBYTE aDeadKeyKbdState,
PRUint16 aShiftStatesWithBaseChars,
DeadKeyEntry* aDeadKeyArray,
PRUint32 aMaxEntries);
void DeactivateDeadKeyState();
const DeadKeyTable* AddDeadKeyTable(const DeadKeyEntry* aDeadKeyArray,
PRUint32 aEntries);
void ReleaseDeadKeyTables();
public:
KeyboardLayout ();
~KeyboardLayout ();
KeyboardLayout();
~KeyboardLayout();
static PRBool IsPrintableCharKey (PRUint8 aVirtualKey);
static PRBool IsNumpadKey (PRUint8 aVirtualKey);
static PRBool IsPrintableCharKey(PRUint8 aVirtualKey);
static PRBool IsNumpadKey(PRUint8 aVirtualKey);
PRBool IsDeadKey () const
PRBool IsDeadKey() const
{
return (mLastVirtualKeyIndex >= 0) ? mVirtualKeys [mLastVirtualKeyIndex].IsDeadKey (mLastShiftState) : PR_FALSE;
return (mLastVirtualKeyIndex >= 0) ?
mVirtualKeys[mLastVirtualKeyIndex].IsDeadKey(mLastShiftState) : PR_FALSE;
}
void LoadLayout (HKL aLayout);
void OnKeyDown (PRUint8 aVirtualKey);
PRUint32 GetUniChars (PRUnichar* aUniChars, PRUint8* aShiftStates, PRUint32 aMaxChars) const;
void LoadLayout(HKL aLayout);
void OnKeyDown(PRUint8 aVirtualKey);
PRUint32 GetUniChars(PRUnichar* aUniChars, PRUint8* aShiftStates,
PRUint32 aMaxChars) const;
PRUint32 GetUniCharsWithShiftState(PRUint8 aVirtualKey, PRUint8 aShiftStates,
PRUnichar* aUniChars,
PRUint32 aMaxChars) const;