2006-01-07 18:46:46 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Dainis Jonitis, <Dainis_Jonitis@exigengroup.lv>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2006
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
#include "KeyboardLayout.h"
|
2011-06-02 12:56:50 +00:00
|
|
|
|
|
|
|
#include "nsMemory.h"
|
2006-01-07 18:46:46 +00:00
|
|
|
#include "nsToolkit.h"
|
|
|
|
#include "nsQuickSort.h"
|
2011-06-02 12:56:50 +00:00
|
|
|
#include "nsAlgorithm.h"
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2007-01-02 20:33:23 +00:00
|
|
|
#include <winuser.h>
|
2009-01-05 18:17:44 +00:00
|
|
|
|
2007-01-02 20:33:23 +00:00
|
|
|
#ifndef WINABLEAPI
|
2006-04-04 20:57:13 +00:00
|
|
|
#include <winable.h>
|
2007-01-02 20:33:23 +00:00
|
|
|
#endif
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
|
|
|
|
2006-01-07 18:46:46 +00:00
|
|
|
struct DeadKeyEntry
|
|
|
|
{
|
2008-12-03 12:22:14 +00:00
|
|
|
PRUnichar BaseChar;
|
|
|
|
PRUnichar CompositeChar;
|
2006-01-07 18:46:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class DeadKeyTable
|
|
|
|
{
|
2010-07-22 02:11:34 +00:00
|
|
|
friend class KeyboardLayout;
|
2006-01-07 18:46:46 +00:00
|
|
|
|
|
|
|
PRUint16 mEntries;
|
2010-07-27 12:48:59 +00:00
|
|
|
// KeyboardLayout::AddDeadKeyTable() will allocate as many entries as
|
2010-07-27 12:47:42 +00:00
|
|
|
// required. It is the only way to create new DeadKeyTable instances.
|
2010-07-27 12:48:59 +00:00
|
|
|
DeadKeyEntry mTable[1];
|
2010-07-21 23:49:54 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
void Init(const DeadKeyEntry* aDeadKeyArray, PRUint32 aEntries)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
|
|
|
mEntries = aEntries;
|
2010-07-27 12:48:59 +00:00
|
|
|
memcpy(mTable, aDeadKeyArray, aEntries * sizeof(DeadKeyEntry));
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
static PRUint32 SizeInBytes(PRUint32 aEntries)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:48:59 +00:00
|
|
|
return offsetof(DeadKeyTable, mTable) + aEntries * sizeof(DeadKeyEntry);
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2010-07-27 12:48:59 +00:00
|
|
|
PRUint32 Entries() const
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
|
|
|
return mEntries;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsEqual(const DeadKeyEntry* aDeadKeyArray, PRUint32 aEntries) const
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
|
|
|
return (mEntries == aEntries &&
|
2010-07-27 12:50:03 +00:00
|
|
|
!memcmp(mTable, aDeadKeyArray,
|
|
|
|
aEntries * sizeof(DeadKeyEntry)));
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
PRUnichar GetCompositeChar(PRUnichar aBaseChar) const;
|
2006-01-07 18:46:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
inline PRUnichar
|
|
|
|
VirtualKey::GetCompositeChar(PRUint8 aShiftState, PRUnichar aBaseChar) const
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:48:59 +00:00
|
|
|
return mShiftStates[aShiftState].DeadKey.Table->GetCompositeChar(aBaseChar);
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
const DeadKeyTable*
|
|
|
|
VirtualKey::MatchingDeadKeyTable(const DeadKeyEntry* aDeadKeyArray,
|
|
|
|
PRUint32 aEntries) const
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:49:37 +00:00
|
|
|
if (!mIsDeadKey) {
|
2006-01-07 18:46:46 +00:00
|
|
|
return nsnull;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2010-07-27 12:49:37 +00:00
|
|
|
for (PRUint32 shiftState = 0; shiftState < 16; shiftState++) {
|
2010-07-27 12:50:21 +00:00
|
|
|
if (!IsDeadKey(shiftState)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const DeadKeyTable* dkt = mShiftStates[shiftState].DeadKey.Table;
|
|
|
|
if (dkt && dkt->IsEqual(aDeadKeyArray, aEntries)) {
|
|
|
|
return dkt;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
void
|
|
|
|
VirtualKey::SetNormalChars(PRUint8 aShiftState,
|
|
|
|
const PRUnichar* aChars,
|
|
|
|
PRUint32 aNumOfChars)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2011-10-11 05:50:08 +00:00
|
|
|
NS_ASSERTION(aShiftState < ArrayLength(mShiftStates), "invalid index");
|
2010-07-21 23:51:49 +00:00
|
|
|
|
2011-10-02 02:16:19 +00:00
|
|
|
SetDeadKey(aShiftState, false);
|
2010-07-27 12:49:37 +00:00
|
|
|
|
2010-07-27 12:50:21 +00:00
|
|
|
for (PRUint32 index = 0; index < aNumOfChars; index++) {
|
2006-01-07 18:46:46 +00:00
|
|
|
// Ignore legacy non-printable control characters
|
2010-07-27 12:50:21 +00:00
|
|
|
mShiftStates[aShiftState].Normal.Chars[index] =
|
|
|
|
(aChars[index] >= 0x20) ? aChars[index] : 0;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
PRUint32 len = ArrayLength(mShiftStates[aShiftState].Normal.Chars);
|
2010-07-27 12:50:21 +00:00
|
|
|
for (PRUint32 index = aNumOfChars; index < len; index++) {
|
|
|
|
mShiftStates[aShiftState].Normal.Chars[index] = 0;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
void
|
|
|
|
VirtualKey::SetDeadChar(PRUint8 aShiftState, PRUnichar aDeadChar)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2011-10-11 05:50:08 +00:00
|
|
|
NS_ASSERTION(aShiftState < ArrayLength(mShiftStates), "invalid index");
|
2010-07-27 12:48:59 +00:00
|
|
|
|
2011-10-02 02:16:19 +00:00
|
|
|
SetDeadKey(aShiftState, true);
|
2010-07-27 12:48:59 +00:00
|
|
|
|
|
|
|
mShiftStates[aShiftState].DeadKey.DeadChar = aDeadChar;
|
|
|
|
mShiftStates[aShiftState].DeadKey.Table = nsnull;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
PRUint32
|
|
|
|
VirtualKey::GetUniChars(PRUint8 aShiftState,
|
|
|
|
PRUnichar* aUniChars,
|
|
|
|
PRUint8* aFinalShiftState) const
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
|
|
|
*aFinalShiftState = aShiftState;
|
2010-07-27 12:48:59 +00:00
|
|
|
PRUint32 numOfChars = GetNativeUniChars(aShiftState, aUniChars);
|
2010-07-27 12:50:03 +00:00
|
|
|
|
|
|
|
if (!(aShiftState & (eAlt | eCtrl))) {
|
|
|
|
return numOfChars;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUnichar unshiftedChars[5];
|
|
|
|
PRUint32 numOfUnshiftedChars =
|
|
|
|
GetNativeUniChars(aShiftState & ~(eAlt | eCtrl), unshiftedChars);
|
|
|
|
|
|
|
|
if (!numOfChars) {
|
|
|
|
if (!numOfUnshiftedChars) {
|
|
|
|
return 0;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
2010-07-27 12:50:03 +00:00
|
|
|
memcpy(aUniChars, unshiftedChars,
|
|
|
|
numOfUnshiftedChars * sizeof(PRUnichar));
|
|
|
|
return numOfUnshiftedChars;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:50:03 +00:00
|
|
|
if ((aShiftState & (eAlt | eCtrl)) == (eAlt | eCtrl)) {
|
|
|
|
// Even if the shifted chars and the unshifted chars are same, we
|
|
|
|
// should consume the Alt key state and the Ctrl key state when
|
|
|
|
// AltGr key is pressed. Because if we don't consume them, the input
|
|
|
|
// events are ignored on nsEditor. (I.e., Users cannot input the
|
|
|
|
// characters with this key combination.)
|
|
|
|
*aFinalShiftState &= ~(eAlt | eCtrl);
|
|
|
|
} else if (!(numOfChars == numOfUnshiftedChars &&
|
|
|
|
!memcmp(aUniChars, unshiftedChars,
|
|
|
|
numOfChars * sizeof(PRUnichar)))) {
|
|
|
|
// 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);
|
|
|
|
}
|
2006-01-07 18:46:46 +00:00
|
|
|
return numOfChars;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
PRUint32
|
|
|
|
VirtualKey::GetNativeUniChars(PRUint8 aShiftState,
|
|
|
|
PRUnichar* aUniChars) const
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:49:37 +00:00
|
|
|
if (IsDeadKey(aShiftState)) {
|
|
|
|
if (aUniChars) {
|
2010-07-27 12:48:59 +00:00
|
|
|
aUniChars[0] = mShiftStates[aShiftState].DeadKey.DeadChar;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2006-01-07 18:46:46 +00:00
|
|
|
return 1;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2010-07-22 02:11:34 +00:00
|
|
|
|
2010-07-27 12:50:21 +00:00
|
|
|
PRUint32 index;
|
2011-10-11 05:50:08 +00:00
|
|
|
PRUint32 len = ArrayLength(mShiftStates[aShiftState].Normal.Chars);
|
2010-07-27 12:50:21 +00:00
|
|
|
for (index = 0;
|
|
|
|
index < len && mShiftStates[aShiftState].Normal.Chars[index]; index++) {
|
2010-07-27 12:49:37 +00:00
|
|
|
if (aUniChars) {
|
2010-07-27 12:50:21 +00:00
|
|
|
aUniChars[index] = mShiftStates[aShiftState].Normal.Chars[index];
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-27 12:50:21 +00:00
|
|
|
return index;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
KeyboardLayout::KeyboardLayout() :
|
2009-05-03 22:20:11 +00:00
|
|
|
mKeyboardLayout(0)
|
2006-03-16 21:02:22 +00:00
|
|
|
{
|
|
|
|
mDeadKeyTableListHead = nsnull;
|
|
|
|
|
2009-03-04 09:09:09 +00:00
|
|
|
// Note: Don't call LoadLayout from here. Because an instance of this class
|
|
|
|
// can be static. In that case, we cannot use any services in LoadLayout,
|
|
|
|
// e.g., pref service.
|
2006-03-16 21:02:22 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
KeyboardLayout::~KeyboardLayout()
|
2006-03-16 21:02:22 +00:00
|
|
|
{
|
2010-07-27 12:48:59 +00:00
|
|
|
ReleaseDeadKeyTables();
|
2006-03-16 21:02:22 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-07-27 12:47:42 +00:00
|
|
|
KeyboardLayout::IsPrintableCharKey(PRUint8 aVirtualKey)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:48:59 +00:00
|
|
|
return GetKeyIndex(aVirtualKey) >= 0;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-07-27 12:47:42 +00:00
|
|
|
KeyboardLayout::IsNumpadKey(PRUint8 aVirtualKey)
|
2006-05-21 08:16:27 +00:00
|
|
|
{
|
|
|
|
return VK_NUMPAD0 <= aVirtualKey && aVirtualKey <= VK_DIVIDE;
|
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
void
|
|
|
|
KeyboardLayout::OnKeyDown(PRUint8 aVirtualKey)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:48:59 +00:00
|
|
|
mLastVirtualKeyIndex = GetKeyIndex(aVirtualKey);
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2010-07-27 12:49:37 +00:00
|
|
|
if (mLastVirtualKeyIndex < 0) {
|
2010-07-27 12:48:59 +00:00
|
|
|
// Does not produce any printable characters, but still preserves the
|
|
|
|
// dead-key state.
|
2006-01-07 18:46:46 +00:00
|
|
|
mNumOfChars = 0;
|
2010-07-27 12:50:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BYTE kbdState[256];
|
|
|
|
if (!::GetKeyboardState(kbdState)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastShiftState = GetShiftState(kbdState);
|
|
|
|
|
|
|
|
if (mVirtualKeys[mLastVirtualKeyIndex].IsDeadKey(mLastShiftState)) {
|
|
|
|
if (mActiveDeadKey < 0) {
|
|
|
|
// Dead-key state activated. No characters generated.
|
|
|
|
mActiveDeadKey = aVirtualKey;
|
|
|
|
mDeadKeyShiftState = mLastShiftState;
|
|
|
|
mNumOfChars = 0;
|
|
|
|
return;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
2010-07-27 12:50:03 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint8 finalShiftState;
|
|
|
|
PRUnichar uniChars[5];
|
|
|
|
PRUint32 numOfBaseChars =
|
|
|
|
mVirtualKeys[mLastVirtualKeyIndex].GetUniChars(mLastShiftState, uniChars,
|
|
|
|
&finalShiftState);
|
|
|
|
if (mActiveDeadKey < 0) {
|
|
|
|
// No dead-keys are active. Just return the produced characters.
|
|
|
|
memcpy(mChars, uniChars, numOfBaseChars * sizeof(PRUnichar));
|
|
|
|
memset(mShiftStates, finalShiftState, numOfBaseChars);
|
|
|
|
mNumOfChars = numOfBaseChars;
|
|
|
|
return;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
2010-07-27 12:50:03 +00:00
|
|
|
|
|
|
|
// Dead-key was active. See if pressed base character does produce
|
|
|
|
// valid composite character.
|
|
|
|
PRInt32 activeDeadKeyIndex = GetKeyIndex(mActiveDeadKey);
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
mNumOfChars = numOfBaseChars + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeactivateDeadKeyState();
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
PRUint32
|
|
|
|
KeyboardLayout::GetUniChars(PRUnichar* aUniChars,
|
|
|
|
PRUint8* aShiftStates,
|
|
|
|
PRUint32 aMaxChars) const
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2011-06-02 12:56:50 +00:00
|
|
|
PRUint32 chars = NS_MIN<PRUint32>(mNumOfChars, aMaxChars);
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
memcpy(aUniChars, mChars, chars * sizeof(PRUnichar));
|
|
|
|
memcpy(aShiftStates, mShiftStates, chars);
|
2006-01-07 18:46:46 +00:00
|
|
|
|
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
2008-04-15 04:16:24 +00:00
|
|
|
PRUint32
|
2010-07-22 02:11:34 +00:00
|
|
|
KeyboardLayout::GetUniCharsWithShiftState(PRUint8 aVirtualKey,
|
|
|
|
PRUint8 aShiftStates,
|
|
|
|
PRUnichar* aUniChars,
|
|
|
|
PRUint32 aMaxChars) const
|
2008-04-15 04:16:24 +00:00
|
|
|
{
|
|
|
|
PRInt32 key = GetKeyIndex(aVirtualKey);
|
2010-07-27 12:49:37 +00:00
|
|
|
if (key < 0) {
|
2008-04-15 04:16:24 +00:00
|
|
|
return 0;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2008-04-15 04:16:24 +00:00
|
|
|
PRUint8 finalShiftState;
|
2008-12-03 12:22:14 +00:00
|
|
|
PRUnichar uniChars[5];
|
2008-04-15 04:16:24 +00:00
|
|
|
PRUint32 numOfBaseChars =
|
|
|
|
mVirtualKeys[key].GetUniChars(aShiftStates, uniChars, &finalShiftState);
|
2011-06-02 12:56:50 +00:00
|
|
|
PRUint32 chars = NS_MIN(numOfBaseChars, aMaxChars);
|
2010-07-27 12:48:59 +00:00
|
|
|
memcpy(aUniChars, uniChars, chars * sizeof(PRUnichar));
|
2008-04-15 04:16:24 +00:00
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
void
|
|
|
|
KeyboardLayout::LoadLayout(HKL aLayout)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:49:37 +00:00
|
|
|
if (mKeyboardLayout == aLayout) {
|
2008-12-06 09:27:30 +00:00
|
|
|
return;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2008-12-06 09:27:30 +00:00
|
|
|
|
|
|
|
mKeyboardLayout = aLayout;
|
|
|
|
|
2006-01-07 18:46:46 +00:00
|
|
|
PRUint32 shiftState;
|
2010-07-27 12:50:21 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
BYTE kbdState[256];
|
2010-07-27 12:50:21 +00:00
|
|
|
memset(kbdState, 0, sizeof(kbdState));
|
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
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;
|
2010-07-21 23:52:52 +00:00
|
|
|
|
2006-01-07 18:46:46 +00:00
|
|
|
mActiveDeadKey = -1;
|
|
|
|
mNumOfChars = 0;
|
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
ReleaseDeadKeyTables();
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
::GetKeyboardState(originalKbdState);
|
2006-01-07 18:46:46 +00:00
|
|
|
|
|
|
|
// For each shift state gather all printable characters that are produced
|
|
|
|
// for normal case when no any dead-key is active.
|
2010-07-27 12:50:21 +00:00
|
|
|
|
2010-07-27 12:49:37 +00:00
|
|
|
for (shiftState = 0; shiftState < 16; shiftState++) {
|
2010-07-27 12:48:59 +00:00
|
|
|
SetShiftState(kbdState, shiftState);
|
2010-07-27 12:49:37 +00:00
|
|
|
for (PRUint32 virtualKey = 0; virtualKey < 256; virtualKey++) {
|
2010-07-27 12:48:59 +00:00
|
|
|
PRInt32 vki = GetKeyIndex(virtualKey);
|
2010-07-27 12:49:37 +00:00
|
|
|
if (vki < 0) {
|
2006-01-07 18:46:46 +00:00
|
|
|
continue;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2011-10-11 05:50:08 +00:00
|
|
|
NS_ASSERTION(PRUint32(vki) < ArrayLength(mVirtualKeys), "invalid index");
|
2010-07-27 12:48:59 +00:00
|
|
|
PRUnichar uniChars[5];
|
2010-07-27 12:50:21 +00:00
|
|
|
PRInt32 ret =
|
|
|
|
::ToUnicodeEx(virtualKey, 0, kbdState, (LPWSTR)uniChars,
|
2011-10-11 05:50:08 +00:00
|
|
|
ArrayLength(uniChars), 0, mKeyboardLayout);
|
2010-07-27 12:49:37 +00:00
|
|
|
// dead-key
|
2010-07-27 12:50:21 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
shiftStatesWithDeadKeys |= (1 << shiftState);
|
2010-07-27 12:48:59 +00:00
|
|
|
// Repeat dead-key to deactivate it and get its character
|
|
|
|
// representation.
|
|
|
|
PRUnichar deadChar[2];
|
2010-07-27 12:50:21 +00:00
|
|
|
ret = ::ToUnicodeEx(virtualKey, 0, kbdState, (LPWSTR)deadChar,
|
2011-10-11 05:50:08 +00:00
|
|
|
ArrayLength(deadChar), 0, mKeyboardLayout);
|
2010-07-27 12:50:21 +00:00
|
|
|
NS_ASSERTION(ret == 2, "Expecting twice repeated dead-key character");
|
2010-07-27 12:48:59 +00:00
|
|
|
mVirtualKeys[vki].SetDeadChar(shiftState, deadChar[0]);
|
2010-07-27 12:49:37 +00:00
|
|
|
} else {
|
2010-07-27 12:50:21 +00:00
|
|
|
if (ret == 1) {
|
2010-07-27 12:48:59 +00:00
|
|
|
// dead-key can pair only with exactly one base character.
|
2010-07-27 12:50:21 +00:00
|
|
|
shiftStatesWithBaseChars |= (1 << shiftState);
|
2010-07-27 12:48:59 +00:00
|
|
|
}
|
2010-07-27 12:50:21 +00:00
|
|
|
mVirtualKeys[vki].SetNormalChars(shiftState, uniChars, ret);
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
// Now process each dead-key to find all its base characters and resulting
|
|
|
|
// composite characters.
|
2010-07-27 12:49:37 +00:00
|
|
|
for (shiftState = 0; shiftState < 16; shiftState++) {
|
|
|
|
if (!(shiftStatesWithDeadKeys & (1 << shiftState))) {
|
2006-01-07 18:46:46 +00:00
|
|
|
continue;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
SetShiftState(kbdState, shiftState);
|
2010-07-22 02:11:34 +00:00
|
|
|
|
2010-07-27 12:49:37 +00:00
|
|
|
for (PRUint32 virtualKey = 0; virtualKey < 256; virtualKey++) {
|
2010-07-27 12:48:59 +00:00
|
|
|
PRInt32 vki = GetKeyIndex(virtualKey);
|
2010-07-27 12:49:37 +00:00
|
|
|
if (vki >= 0 && mVirtualKeys[vki].IsDeadKey(shiftState)) {
|
2010-07-27 12:48:59 +00:00
|
|
|
DeadKeyEntry deadKeyArray[256];
|
|
|
|
PRInt32 n = GetDeadKeyCombinations(virtualKey, kbdState,
|
2010-07-27 12:50:21 +00:00
|
|
|
shiftStatesWithBaseChars,
|
2010-07-27 12:48:59 +00:00
|
|
|
deadKeyArray,
|
2011-10-11 05:50:08 +00:00
|
|
|
ArrayLength(deadKeyArray));
|
2010-07-27 12:48:59 +00:00
|
|
|
const DeadKeyTable* dkt =
|
|
|
|
mVirtualKeys[vki].MatchingDeadKeyTable(deadKeyArray, n);
|
2010-07-27 12:49:37 +00:00
|
|
|
if (!dkt) {
|
2010-07-27 12:48:59 +00:00
|
|
|
dkt = AddDeadKeyTable(deadKeyArray, n);
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2010-07-27 12:48:59 +00:00
|
|
|
mVirtualKeys[vki].AttachDeadKeyTable(shiftState, dkt);
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
::SetKeyboardState(originalKbdState);
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
PRUint8
|
|
|
|
KeyboardLayout::GetShiftState(const PBYTE aKbdState)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isShift = (aKbdState[VK_SHIFT] & 0x80) != 0;
|
|
|
|
bool isCtrl = (aKbdState[VK_CONTROL] & 0x80) != 0;
|
|
|
|
bool isAlt = (aKbdState[VK_MENU] & 0x80) != 0;
|
|
|
|
bool isCaps = (aKbdState[VK_CAPITAL] & 0x01) != 0;
|
2006-01-07 18:46:46 +00:00
|
|
|
|
|
|
|
return ((isCaps << 3) | (isAlt << 2) | (isCtrl << 1) | isShift);
|
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
void
|
|
|
|
KeyboardLayout::SetShiftState(PBYTE aKbdState, PRUint8 aShiftState)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:48:59 +00:00
|
|
|
NS_ASSERTION(aShiftState < 16, "aShiftState out of range");
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2010-07-27 12:49:37 +00:00
|
|
|
if (aShiftState & eShift) {
|
2010-07-27 12:48:59 +00:00
|
|
|
aKbdState[VK_SHIFT] |= 0x80;
|
2010-07-27 12:49:37 +00:00
|
|
|
} else {
|
2010-07-27 12:48:59 +00:00
|
|
|
aKbdState[VK_SHIFT] &= ~0x80;
|
|
|
|
aKbdState[VK_LSHIFT] &= ~0x80;
|
|
|
|
aKbdState[VK_RSHIFT] &= ~0x80;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:49:37 +00:00
|
|
|
if (aShiftState & eCtrl) {
|
2010-07-27 12:48:59 +00:00
|
|
|
aKbdState[VK_CONTROL] |= 0x80;
|
2010-07-27 12:49:37 +00:00
|
|
|
} else {
|
2010-07-27 12:48:59 +00:00
|
|
|
aKbdState[VK_CONTROL] &= ~0x80;
|
|
|
|
aKbdState[VK_LCONTROL] &= ~0x80;
|
|
|
|
aKbdState[VK_RCONTROL] &= ~0x80;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:49:37 +00:00
|
|
|
if (aShiftState & eAlt) {
|
2010-07-27 12:48:59 +00:00
|
|
|
aKbdState[VK_MENU] |= 0x80;
|
2010-07-27 12:49:37 +00:00
|
|
|
} else {
|
2010-07-27 12:48:59 +00:00
|
|
|
aKbdState[VK_MENU] &= ~0x80;
|
|
|
|
aKbdState[VK_LMENU] &= ~0x80;
|
|
|
|
aKbdState[VK_RMENU] &= ~0x80;
|
2010-07-21 23:51:49 +00:00
|
|
|
}
|
2010-07-22 02:11:34 +00:00
|
|
|
|
2010-07-27 12:49:37 +00:00
|
|
|
if (aShiftState & eCapsLock) {
|
2010-07-27 12:48:59 +00:00
|
|
|
aKbdState[VK_CAPITAL] |= 0x01;
|
2010-07-27 12:49:37 +00:00
|
|
|
} else {
|
2010-07-27 12:48:59 +00:00
|
|
|
aKbdState[VK_CAPITAL] &= ~0x01;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
inline PRInt32
|
|
|
|
KeyboardLayout::GetKeyIndex(PRUint8 aVirtualKey)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:50:21 +00:00
|
|
|
// Currently these 50 (NS_NUM_OF_KEYS) virtual keys are assumed
|
2010-07-22 02:11:34 +00:00
|
|
|
// to produce visible representation:
|
2006-01-07 18:46:46 +00:00
|
|
|
// 0x20 - VK_SPACE ' '
|
|
|
|
// 0x30..0x39 '0'..'9'
|
|
|
|
// 0x41..0x5A 'A'..'Z'
|
2006-03-16 21:02:22 +00:00
|
|
|
// 0x6E - VK_DECIMAL '.'
|
2006-01-07 18:46:46 +00:00
|
|
|
// 0xBA - VK_OEM_1 ';:' for US
|
|
|
|
// 0xBB - VK_OEM_PLUS '+' any country
|
|
|
|
// 0xBC - VK_OEM_COMMA ',' any country
|
|
|
|
// 0xBD - VK_OEM_MINUS '-' any country
|
|
|
|
// 0xBE - VK_OEM_PERIOD '.' any country
|
|
|
|
// 0xBF - VK_OEM_2 '/?' for US
|
|
|
|
// 0xC0 - VK_OEM_3 '`~' for US
|
|
|
|
// 0xDB - VK_OEM_4 '[{' for US
|
|
|
|
// 0xDC - VK_OEM_5 '\|' for US
|
|
|
|
// 0xDD - VK_OEM_6 ']}' for US
|
|
|
|
// 0xDE - VK_OEM_7 ''"' for US
|
2010-07-27 12:50:21 +00:00
|
|
|
// 0xDF - VK_OEM_8
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
static const PRInt8 xlat[256] =
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
|
|
|
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10
|
|
|
|
0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20
|
|
|
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1, -1, -1, -1, -1, -1, // 30
|
|
|
|
-1, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 40
|
|
|
|
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, -1, -1, -1, -1, -1, // 50
|
2006-03-16 21:02:22 +00:00
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 37, -1, // 60
|
2006-01-07 18:46:46 +00:00
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 70
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 90
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // A0
|
2006-03-16 21:02:22 +00:00
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 38, 39, 40, 41, 42, 43, // B0
|
|
|
|
44, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // C0
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 45, 46, 47, 48, 49, // D0
|
2006-01-07 18:46:46 +00:00
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // E0
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // F0
|
|
|
|
};
|
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
return xlat[aVirtualKey];
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
int
|
|
|
|
KeyboardLayout::CompareDeadKeyEntries(const void* aArg1,
|
|
|
|
const void* aArg2,
|
|
|
|
void*)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
const DeadKeyEntry* arg1 = static_cast<const DeadKeyEntry*>(aArg1);
|
|
|
|
const DeadKeyEntry* arg2 = static_cast<const DeadKeyEntry*>(aArg2);
|
2006-01-07 18:46:46 +00:00
|
|
|
|
|
|
|
return arg1->BaseChar - arg2->BaseChar;
|
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
const DeadKeyTable*
|
|
|
|
KeyboardLayout::AddDeadKeyTable(const DeadKeyEntry* aDeadKeyArray,
|
|
|
|
PRUint32 aEntries)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
|
|
|
DeadKeyTableListEntry* next = mDeadKeyTableListHead;
|
2010-07-27 12:49:37 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
const size_t bytes = offsetof(DeadKeyTableListEntry, data) +
|
|
|
|
DeadKeyTable::SizeInBytes(aEntries);
|
|
|
|
PRUint8* p = new PRUint8[bytes];
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2007-07-08 07:08:04 +00:00
|
|
|
mDeadKeyTableListHead = reinterpret_cast<DeadKeyTableListEntry*>(p);
|
2006-01-07 18:46:46 +00:00
|
|
|
mDeadKeyTableListHead->next = next;
|
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
DeadKeyTable* dkt =
|
|
|
|
reinterpret_cast<DeadKeyTable*>(mDeadKeyTableListHead->data);
|
|
|
|
|
|
|
|
dkt->Init(aDeadKeyArray, aEntries);
|
2006-01-07 18:46:46 +00:00
|
|
|
|
|
|
|
return dkt;
|
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
void
|
|
|
|
KeyboardLayout::ReleaseDeadKeyTables()
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:49:37 +00:00
|
|
|
while (mDeadKeyTableListHead) {
|
2007-07-08 07:08:04 +00:00
|
|
|
PRUint8* p = reinterpret_cast<PRUint8*>(mDeadKeyTableListHead);
|
2006-01-07 18:46:46 +00:00
|
|
|
mDeadKeyTableListHead = mDeadKeyTableListHead->next;
|
|
|
|
|
|
|
|
delete [] p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
|
|
|
KeyboardLayout::EnsureDeadKeyActive(bool aIsActive,
|
2010-07-27 12:47:42 +00:00
|
|
|
PRUint8 aDeadKey,
|
|
|
|
const PBYTE aDeadKeyKbdState)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:50:21 +00:00
|
|
|
PRInt32 ret;
|
2010-07-27 12:49:37 +00:00
|
|
|
do {
|
2010-07-27 12:48:59 +00:00
|
|
|
PRUnichar dummyChars[5];
|
2010-07-27 12:50:21 +00:00
|
|
|
ret = ::ToUnicodeEx(aDeadKey, 0, (PBYTE)aDeadKeyKbdState,
|
2011-10-11 05:50:08 +00:00
|
|
|
(LPWSTR)dummyChars, ArrayLength(dummyChars), 0,
|
2010-07-27 12:50:21 +00:00
|
|
|
mKeyboardLayout);
|
2006-01-07 18:46:46 +00:00
|
|
|
// returned values:
|
2010-07-27 12:48:59 +00:00
|
|
|
// <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).
|
2010-07-27 12:50:21 +00:00
|
|
|
} while ((ret < 0) != aIsActive);
|
2010-07-22 02:11:34 +00:00
|
|
|
|
2010-07-27 12:50:21 +00:00
|
|
|
return (ret < 0);
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
void
|
|
|
|
KeyboardLayout::DeactivateDeadKeyState()
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:49:37 +00:00
|
|
|
if (mActiveDeadKey < 0) {
|
2006-01-07 18:46:46 +00:00
|
|
|
return;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
BYTE kbdState[256];
|
|
|
|
memset(kbdState, 0, sizeof(kbdState));
|
2010-07-27 12:50:21 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
SetShiftState(kbdState, mDeadKeyShiftState);
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2011-10-02 02:16:19 +00:00
|
|
|
EnsureDeadKeyActive(false, mActiveDeadKey, kbdState);
|
2006-01-07 18:46:46 +00:00
|
|
|
mActiveDeadKey = -1;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-07-27 12:47:42 +00:00
|
|
|
KeyboardLayout::AddDeadKeyEntry(PRUnichar aBaseChar,
|
|
|
|
PRUnichar aCompositeChar,
|
|
|
|
DeadKeyEntry* aDeadKeyArray,
|
|
|
|
PRUint32 aEntries)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2010-07-27 12:50:21 +00:00
|
|
|
for (PRUint32 index = 0; index < aEntries; index++) {
|
|
|
|
if (aDeadKeyArray[index].BaseChar == aBaseChar) {
|
2011-10-02 02:16:19 +00:00
|
|
|
return false;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
aDeadKeyArray[aEntries].BaseChar = aBaseChar;
|
|
|
|
aDeadKeyArray[aEntries].CompositeChar = aCompositeChar;
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2011-10-02 02:16:19 +00:00
|
|
|
return true;
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
PRUint32
|
|
|
|
KeyboardLayout::GetDeadKeyCombinations(PRUint8 aDeadKey,
|
|
|
|
const PBYTE aDeadKeyKbdState,
|
|
|
|
PRUint16 aShiftStatesWithBaseChars,
|
|
|
|
DeadKeyEntry* aDeadKeyArray,
|
|
|
|
PRUint32 aMaxEntries)
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool deadKeyActive = false;
|
2006-01-07 18:46:46 +00:00
|
|
|
PRUint32 entries = 0;
|
2010-07-27 12:48:59 +00:00
|
|
|
BYTE kbdState[256];
|
|
|
|
memset(kbdState, 0, sizeof(kbdState));
|
2010-07-27 12:49:37 +00:00
|
|
|
|
|
|
|
for (PRUint32 shiftState = 0; shiftState < 16; shiftState++) {
|
|
|
|
if (!(aShiftStatesWithBaseChars & (1 << shiftState))) {
|
2006-01-07 18:46:46 +00:00
|
|
|
continue;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2006-01-07 18:46:46 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
SetShiftState(kbdState, shiftState);
|
2010-07-22 02:11:34 +00:00
|
|
|
|
2010-07-27 12:49:37 +00:00
|
|
|
for (PRUint32 virtualKey = 0; virtualKey < 256; virtualKey++) {
|
2010-07-27 12:48:59 +00:00
|
|
|
PRInt32 vki = GetKeyIndex(virtualKey);
|
|
|
|
// Dead-key can pair only with such key that produces exactly one base
|
|
|
|
// character.
|
2010-07-27 12:49:37 +00:00
|
|
|
if (vki >= 0 && mVirtualKeys[vki].GetNativeUniChars(shiftState) == 1) {
|
2010-07-27 12:48:59 +00:00
|
|
|
// Ensure dead-key is in active state, when it swallows entered
|
|
|
|
// character and waits for the next pressed key.
|
|
|
|
if (!deadKeyActive) {
|
2011-10-02 02:16:19 +00:00
|
|
|
deadKeyActive = EnsureDeadKeyActive(true, aDeadKey,
|
2010-07-27 12:48:59 +00:00
|
|
|
aDeadKeyKbdState);
|
|
|
|
}
|
2010-07-22 02:11:34 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
// 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];
|
2010-07-27 12:50:21 +00:00
|
|
|
PRInt32 ret =
|
|
|
|
::ToUnicodeEx(virtualKey, 0, kbdState, (LPWSTR)compositeChars,
|
2011-10-11 05:50:08 +00:00
|
|
|
ArrayLength(compositeChars), 0, mKeyboardLayout);
|
2010-07-27 12:50:21 +00:00
|
|
|
switch (ret) {
|
2006-01-07 18:46:46 +00:00
|
|
|
case 0:
|
2010-07-27 12:48:59 +00:00
|
|
|
// This key combination does not produce any characters. The
|
|
|
|
// dead-key is still in active state.
|
2006-01-07 18:46:46 +00:00
|
|
|
break;
|
2010-07-27 12:49:37 +00:00
|
|
|
case 1: {
|
2010-07-27 12:48:59 +00:00
|
|
|
// 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];
|
2010-07-27 12:50:21 +00:00
|
|
|
ret = ::ToUnicodeEx(virtualKey, 0, kbdState, (LPWSTR)baseChars,
|
2011-10-11 05:50:08 +00:00
|
|
|
ArrayLength(baseChars), 0, mKeyboardLayout);
|
2010-07-27 12:50:21 +00:00
|
|
|
NS_ASSERTION(ret == 1, "One base character expected");
|
|
|
|
if (ret == 1 && entries < aMaxEntries &&
|
2010-07-27 12:50:03 +00:00
|
|
|
AddDeadKeyEntry(baseChars[0], compositeChars[0],
|
|
|
|
aDeadKeyArray, entries)) {
|
|
|
|
entries++;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2011-10-02 02:16:19 +00:00
|
|
|
deadKeyActive = false;
|
2006-01-07 18:46:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2006-03-16 21:02:22 +00:00
|
|
|
// 1. Unexpected dead-key. Dead-key chaining is not supported.
|
2010-07-27 12:48:59 +00:00
|
|
|
// 2. More than one character generated. This is not a valid
|
|
|
|
// dead-key and base character combination.
|
2011-10-02 02:16:19 +00:00
|
|
|
deadKeyActive = false;
|
2006-01-07 18:46:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-27 12:49:37 +00:00
|
|
|
if (deadKeyActive) {
|
2011-10-02 02:16:19 +00:00
|
|
|
deadKeyActive = EnsureDeadKeyActive(false, aDeadKey, aDeadKeyKbdState);
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2010-07-22 02:11:34 +00:00
|
|
|
|
2010-07-27 12:48:59 +00:00
|
|
|
NS_QuickSort(aDeadKeyArray, entries, sizeof(DeadKeyEntry),
|
|
|
|
CompareDeadKeyEntries, nsnull);
|
2006-01-07 18:46:46 +00:00
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 12:47:42 +00:00
|
|
|
PRUnichar
|
|
|
|
DeadKeyTable::GetCompositeChar(PRUnichar aBaseChar) const
|
2006-01-07 18:46:46 +00:00
|
|
|
{
|
|
|
|
// Dead-key table is sorted by BaseChar in ascending order.
|
|
|
|
// Usually they are too small to use binary search.
|
|
|
|
|
2010-07-27 12:50:21 +00:00
|
|
|
for (PRUint32 index = 0; index < mEntries; index++) {
|
|
|
|
if (mTable[index].BaseChar == aBaseChar) {
|
|
|
|
return mTable[index].CompositeChar;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2010-07-27 12:50:21 +00:00
|
|
|
if (mTable[index].BaseChar > aBaseChar) {
|
2006-01-07 18:46:46 +00:00
|
|
|
break;
|
2010-07-27 12:49:37 +00:00
|
|
|
}
|
2006-01-07 18:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-07-27 12:47:42 +00:00
|
|
|
|
|
|
|
} // namespace widget
|
|
|
|
} // namespace mozilla
|
|
|
|
|