Bug 855975 part.1 Make widget::KeyboardLayout a singleton class r=jimm

This commit is contained in:
Masayuki Nakano
2013-05-29 15:34:47 +09:00
parent 8bd238b57a
commit c5113cfb1e
5 changed files with 135 additions and 85 deletions

View File

@@ -381,12 +381,13 @@ VirtualKey::FillKbdState(PBYTE aKbdState,
* mozilla::widget::NativeKey
*****************************************************************************/
NativeKey::NativeKey(const KeyboardLayout& aKeyboardLayout,
nsWindow* aWindow,
const MSG& aKeyOrCharMessage) :
NativeKey::NativeKey(nsWindow* aWindow,
const MSG& aKeyOrCharMessage,
const ModifierKeyState& aModKeyState) :
mDOMKeyCode(0), mMessage(aKeyOrCharMessage.message),
mVirtualKeyCode(0), mOriginalVirtualKeyCode(0)
{
KeyboardLayout* keyboardLayout = KeyboardLayout::GetInstance();
mScanCode = WinUtils::GetScanCode(aKeyOrCharMessage.lParam);
mIsExtended = WinUtils::IsExtendedScanCode(aKeyOrCharMessage.lParam);
// On WinXP and WinServer2003, we cannot compute the virtual keycode for
@@ -486,7 +487,7 @@ NativeKey::NativeKey(const KeyboardLayout& aKeyboardLayout,
// Otherwise, compute the virtual keycode with MapVirtualKeyEx().
mVirtualKeyCode = static_cast<uint8_t>(
::MapVirtualKeyEx(GetScanCodeWithExtendedFlag(),
MAPVK_VSC_TO_VK_EX, aKeyboardLayout.GetLayout()));
MAPVK_VSC_TO_VK_EX, keyboardLayout->GetLayout()));
// The result might be unexpected value due to the scan code is
// wrong. For example, any key messages can be generated by
@@ -528,7 +529,7 @@ NativeKey::NativeKey(const KeyboardLayout& aKeyboardLayout,
}
mVirtualKeyCode = mOriginalVirtualKeyCode = static_cast<uint8_t>(
::MapVirtualKeyEx(GetScanCodeWithExtendedFlag(),
MAPVK_VSC_TO_VK_EX, aKeyboardLayout.GetLayout()));
MAPVK_VSC_TO_VK_EX, keyboardLayout->GetLayout()));
break;
default:
MOZ_NOT_REACHED("Unsupported message");
@@ -540,9 +541,11 @@ NativeKey::NativeKey(const KeyboardLayout& aKeyboardLayout,
}
mDOMKeyCode =
aKeyboardLayout.ConvertNativeKeyCodeToDOMKeyCode(mOriginalVirtualKeyCode);
keyboardLayout->ConvertNativeKeyCodeToDOMKeyCode(mOriginalVirtualKeyCode);
mKeyNameIndex =
aKeyboardLayout.ConvertNativeKeyCodeToKeyNameIndex(mOriginalVirtualKeyCode);
keyboardLayout->ConvertNativeKeyCodeToKeyNameIndex(mOriginalVirtualKeyCode);
keyboardLayout->InitNativeKey(*this, aModKeyState);
}
UINT
@@ -628,14 +631,33 @@ NativeKey::GetKeyLocation() const
* mozilla::widget::KeyboardLayout
*****************************************************************************/
KeyboardLayout* KeyboardLayout::sInstance = nullptr;
// static
KeyboardLayout*
KeyboardLayout::GetInstance()
{
if (!sInstance) {
sInstance = new KeyboardLayout();
}
return sInstance;
}
// static
void
KeyboardLayout::Shutdown()
{
delete sInstance;
sInstance = nullptr;
}
KeyboardLayout::KeyboardLayout() :
mKeyboardLayout(0), mPendingKeyboardLayout(0)
mKeyboardLayout(0), mIsOverridden(false),
mIsPendingToRestoreKeyboardLayout(false)
{
mDeadKeyTableListHead = nullptr;
// 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.
// NOTE: LoadLayout() should be called via OnLayoutChange().
}
KeyboardLayout::~KeyboardLayout()
@@ -666,8 +688,8 @@ void
KeyboardLayout::InitNativeKey(NativeKey& aNativeKey,
const ModifierKeyState& aModKeyState)
{
if (mPendingKeyboardLayout) {
LoadLayout(mPendingKeyboardLayout);
if (mIsPendingToRestoreKeyboardLayout) {
LoadLayout(::GetKeyboardLayout(0));
}
uint8_t virtualKey = aNativeKey.GetOriginalVirtualKeyCode();
@@ -769,14 +791,9 @@ KeyboardLayout::GetUniCharsAndModifiers(
}
void
KeyboardLayout::LoadLayout(HKL aLayout, bool aLoadLater)
KeyboardLayout::LoadLayout(HKL aLayout)
{
if (aLoadLater) {
mPendingKeyboardLayout = aLayout;
return;
}
mPendingKeyboardLayout = 0;
mIsPendingToRestoreKeyboardLayout = false;
if (mKeyboardLayout == aLayout) {
return;