2001-02-15 02:13:07 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-02-15 02:13:07 +03:00
|
|
|
|
2003-12-13 02:43:05 +03:00
|
|
|
#include <stdio.h>
|
2001-02-15 02:13:07 +03:00
|
|
|
#include "nsBidiKeyboard.h"
|
|
|
|
#include "prmem.h"
|
2008-08-30 01:26:56 +04:00
|
|
|
#include <tchar.h>
|
2001-02-15 02:13:07 +03:00
|
|
|
|
2001-08-21 05:48:11 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsBidiKeyboard, nsIBidiKeyboard)
|
2001-02-15 02:13:07 +03:00
|
|
|
|
|
|
|
nsBidiKeyboard::nsBidiKeyboard() : nsIBidiKeyboard()
|
2013-07-10 11:57:33 +04:00
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsBidiKeyboard::~nsBidiKeyboard()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsBidiKeyboard::Reset()
|
2001-02-15 02:13:07 +03:00
|
|
|
{
|
2011-10-02 06:16:19 +04:00
|
|
|
mInitialized = false;
|
|
|
|
mHaveBidiKeyboards = false;
|
2001-07-06 03:36:33 +04:00
|
|
|
mLTRKeyboard[0] = '\0';
|
|
|
|
mRTLKeyboard[0] = '\0';
|
|
|
|
mCurrentLocaleName[0] = '\0';
|
2013-07-10 11:57:33 +04:00
|
|
|
return NS_OK;
|
2001-02-15 02:13:07 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsBidiKeyboard::SetLangFromBidiLevel(uint8_t aLevel)
|
2001-02-15 02:13:07 +03:00
|
|
|
{
|
2006-07-17 19:30:09 +04:00
|
|
|
nsresult result = SetupBidiKeyboards();
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
return result;
|
2001-02-15 02:13:07 +03:00
|
|
|
|
2001-05-22 08:07:07 +04:00
|
|
|
// call LoadKeyboardLayout() only if the target keyboard layout is different from the current
|
2008-08-30 01:26:56 +04:00
|
|
|
PRUnichar currentLocaleName[KL_NAMELENGTH];
|
|
|
|
wcsncpy(currentLocaleName, (aLevel & 1) ? mRTLKeyboard : mLTRKeyboard, KL_NAMELENGTH);
|
2001-07-06 03:36:33 +04:00
|
|
|
currentLocaleName[KL_NAMELENGTH-1] = '\0'; // null terminate
|
|
|
|
|
2002-11-17 08:16:49 +03:00
|
|
|
NS_ASSERTION(*currentLocaleName,
|
2001-07-06 03:36:33 +04:00
|
|
|
"currentLocaleName has string length == 0");
|
2005-01-28 23:01:46 +03:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* This implementation of automatic keyboard layout switching is too buggy to be useful
|
|
|
|
and the feature itself is inconsistent with Windows. See Bug 162242 */
|
2001-05-22 08:07:07 +04:00
|
|
|
if (strcmp(mCurrentLocaleName, currentLocaleName)) {
|
|
|
|
if (!::LoadKeyboardLayout(currentLocaleName, KLF_ACTIVATE | KLF_SUBSTITUTE_OK)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2005-01-28 23:01:46 +03:00
|
|
|
}
|
|
|
|
#endif
|
2004-01-03 23:01:28 +03:00
|
|
|
|
2001-02-15 02:13:07 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsBidiKeyboard::IsLangRTL(bool *aIsRTL)
|
2001-02-15 02:13:07 +03:00
|
|
|
{
|
2011-10-02 06:16:19 +04:00
|
|
|
*aIsRTL = false;
|
2006-07-17 19:30:09 +04:00
|
|
|
|
|
|
|
nsresult result = SetupBidiKeyboards();
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
return result;
|
|
|
|
|
2001-02-15 02:13:07 +03:00
|
|
|
HKL currentLocale;
|
2001-05-22 08:07:07 +04:00
|
|
|
|
2001-02-15 02:13:07 +03:00
|
|
|
currentLocale = ::GetKeyboardLayout(0);
|
|
|
|
*aIsRTL = IsRTLLanguage(currentLocale);
|
|
|
|
|
2008-08-30 01:26:56 +04:00
|
|
|
if (!::GetKeyboardLayoutNameW(mCurrentLocaleName))
|
2001-05-22 08:07:07 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2002-11-17 08:16:49 +03:00
|
|
|
NS_ASSERTION(*mCurrentLocaleName,
|
2001-07-06 03:36:33 +04:00
|
|
|
"GetKeyboardLayoutName return string length == 0");
|
2008-08-30 01:26:56 +04:00
|
|
|
NS_ASSERTION((wcslen(mCurrentLocaleName) < KL_NAMELENGTH),
|
2001-07-06 03:36:33 +04:00
|
|
|
"GetKeyboardLayoutName return string length >= KL_NAMELENGTH");
|
|
|
|
|
2001-02-15 02:13:07 +03:00
|
|
|
// The language set by the user overrides the default language for that direction
|
2001-07-06 03:36:33 +04:00
|
|
|
if (*aIsRTL) {
|
2008-08-30 01:26:56 +04:00
|
|
|
wcsncpy(mRTLKeyboard, mCurrentLocaleName, KL_NAMELENGTH);
|
2001-07-06 03:36:33 +04:00
|
|
|
mRTLKeyboard[KL_NAMELENGTH-1] = '\0'; // null terminate
|
|
|
|
} else {
|
2008-08-30 01:26:56 +04:00
|
|
|
wcsncpy(mLTRKeyboard, mCurrentLocaleName, KL_NAMELENGTH);
|
2001-07-06 03:36:33 +04:00
|
|
|
mLTRKeyboard[KL_NAMELENGTH-1] = '\0'; // null terminate
|
|
|
|
}
|
|
|
|
|
2008-08-30 01:26:56 +04:00
|
|
|
NS_ASSERTION((wcslen(mRTLKeyboard) < KL_NAMELENGTH),
|
2001-07-06 03:36:33 +04:00
|
|
|
"mLTRKeyboard has string length >= KL_NAMELENGTH");
|
2008-08-30 01:26:56 +04:00
|
|
|
NS_ASSERTION((wcslen(mLTRKeyboard) < KL_NAMELENGTH),
|
2001-07-06 03:36:33 +04:00
|
|
|
"mRTLKeyboard has string length >= KL_NAMELENGTH");
|
2001-02-15 02:13:07 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsBidiKeyboard::GetHaveBidiKeyboards(bool* aResult)
|
2011-09-15 18:54:50 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
|
|
|
|
nsresult result = SetupBidiKeyboards();
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
return result;
|
|
|
|
|
|
|
|
*aResult = mHaveBidiKeyboards;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-15 02:13:07 +03:00
|
|
|
|
|
|
|
// Get the list of keyboard layouts available in the system
|
|
|
|
// Set mLTRKeyboard to the first LTR keyboard in the list and mRTLKeyboard to the first RTL keyboard in the list
|
|
|
|
// These defaults will be used unless the user explicitly sets something else.
|
2006-07-17 19:30:09 +04:00
|
|
|
nsresult nsBidiKeyboard::SetupBidiKeyboards()
|
2001-02-15 02:13:07 +03:00
|
|
|
{
|
2006-07-17 19:30:09 +04:00
|
|
|
if (mInitialized)
|
|
|
|
return mHaveBidiKeyboards ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
|
2001-02-15 02:13:07 +03:00
|
|
|
int keyboards;
|
|
|
|
HKL far* buf;
|
|
|
|
HKL locale;
|
2008-08-30 01:26:56 +04:00
|
|
|
PRUnichar localeName[KL_NAMELENGTH];
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isLTRKeyboardSet = false;
|
|
|
|
bool isRTLKeyboardSet = false;
|
2001-02-15 02:13:07 +03:00
|
|
|
|
|
|
|
// GetKeyboardLayoutList with 0 as first parameter returns the number of keyboard layouts available
|
2012-07-30 18:20:58 +04:00
|
|
|
keyboards = ::GetKeyboardLayoutList(0, nullptr);
|
2001-02-15 02:13:07 +03:00
|
|
|
if (!keyboards)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// allocate a buffer to hold the list
|
|
|
|
buf = (HKL far*) PR_Malloc(keyboards * sizeof(HKL));
|
|
|
|
if (!buf)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// Call again to fill the buffer
|
|
|
|
if (::GetKeyboardLayoutList(keyboards, buf) != keyboards) {
|
|
|
|
PR_Free(buf);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through the list and pick a default LTR and RTL keyboard layout
|
|
|
|
while (keyboards--) {
|
|
|
|
locale = buf[keyboards];
|
2001-07-06 03:36:33 +04:00
|
|
|
if (IsRTLLanguage(locale)) {
|
2008-08-30 01:26:56 +04:00
|
|
|
_snwprintf(mRTLKeyboard, KL_NAMELENGTH, L"%.*x", KL_NAMELENGTH - 1,
|
2010-06-25 16:13:17 +04:00
|
|
|
LANGIDFROMLCID((DWORD_PTR)locale));
|
2011-10-02 06:16:19 +04:00
|
|
|
isRTLKeyboardSet = true;
|
2001-07-06 03:36:33 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-30 01:26:56 +04:00
|
|
|
_snwprintf(mLTRKeyboard, KL_NAMELENGTH, L"%.*x", KL_NAMELENGTH - 1,
|
2010-06-25 16:13:17 +04:00
|
|
|
LANGIDFROMLCID((DWORD_PTR)locale));
|
2011-10-02 06:16:19 +04:00
|
|
|
isLTRKeyboardSet = true;
|
2001-07-06 03:36:33 +04:00
|
|
|
}
|
2001-02-15 02:13:07 +03:00
|
|
|
}
|
|
|
|
PR_Free(buf);
|
2011-10-02 06:16:19 +04:00
|
|
|
mInitialized = true;
|
2006-07-17 19:30:09 +04:00
|
|
|
|
|
|
|
// If there is not at least one keyboard of each directionality, Bidi
|
|
|
|
// keyboard functionality will be disabled.
|
|
|
|
mHaveBidiKeyboards = (isRTLKeyboardSet && isLTRKeyboardSet);
|
|
|
|
if (!mHaveBidiKeyboards)
|
|
|
|
return NS_ERROR_FAILURE;
|
2001-05-22 08:07:07 +04:00
|
|
|
|
|
|
|
// Get the current keyboard layout and use it for either mRTLKeyboard or
|
|
|
|
// mLTRKeyboard as appropriate. If the user has many keyboard layouts
|
|
|
|
// installed this prevents us from arbitrarily resetting the current
|
|
|
|
// layout (bug 80274)
|
|
|
|
locale = ::GetKeyboardLayout(0);
|
2008-08-30 01:26:56 +04:00
|
|
|
if (!::GetKeyboardLayoutNameW(localeName))
|
2001-05-22 08:07:07 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2002-11-17 08:16:49 +03:00
|
|
|
NS_ASSERTION(*localeName,
|
2001-07-06 03:36:33 +04:00
|
|
|
"GetKeyboardLayoutName return string length == 0");
|
2008-08-30 01:26:56 +04:00
|
|
|
NS_ASSERTION((wcslen(localeName) < KL_NAMELENGTH),
|
2001-07-06 03:36:33 +04:00
|
|
|
"GetKeyboardLayout return string length >= KL_NAMELENGTH");
|
|
|
|
|
2001-05-22 08:07:07 +04:00
|
|
|
if (IsRTLLanguage(locale)) {
|
2008-08-30 01:26:56 +04:00
|
|
|
wcsncpy(mRTLKeyboard, localeName, KL_NAMELENGTH);
|
2001-07-06 03:36:33 +04:00
|
|
|
mRTLKeyboard[KL_NAMELENGTH-1] = '\0'; // null terminate
|
2001-05-22 08:07:07 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-08-30 01:26:56 +04:00
|
|
|
wcsncpy(mLTRKeyboard, localeName, KL_NAMELENGTH);
|
2001-07-06 03:36:33 +04:00
|
|
|
mLTRKeyboard[KL_NAMELENGTH-1] = '\0'; // null terminate
|
2001-05-22 08:07:07 +04:00
|
|
|
}
|
|
|
|
|
2002-11-17 08:16:49 +03:00
|
|
|
NS_ASSERTION(*mRTLKeyboard,
|
2001-07-06 03:36:33 +04:00
|
|
|
"mLTRKeyboard has string length == 0");
|
2002-11-17 08:16:49 +03:00
|
|
|
NS_ASSERTION(*mLTRKeyboard,
|
2001-07-06 03:36:33 +04:00
|
|
|
"mLTRKeyboard has string length == 0");
|
|
|
|
|
2001-02-15 02:13:07 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-07-17 19:30:09 +04:00
|
|
|
// Test whether the language represented by this locale identifier is a
|
|
|
|
// right-to-left language, using bit 123 of the Unicode subset bitfield in
|
|
|
|
// the LOCALESIGNATURE
|
|
|
|
// See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/unicode_63ub.asp
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsBidiKeyboard::IsRTLLanguage(HKL aLocale)
|
2001-02-15 02:13:07 +03:00
|
|
|
{
|
2006-07-17 19:30:09 +04:00
|
|
|
LOCALESIGNATURE localesig;
|
2010-06-25 16:13:17 +04:00
|
|
|
return (::GetLocaleInfoW(PRIMARYLANGID((DWORD_PTR)aLocale),
|
2006-07-17 19:30:09 +04:00
|
|
|
LOCALE_FONTSIGNATURE,
|
|
|
|
(LPWSTR)&localesig,
|
|
|
|
(sizeof(localesig)/sizeof(WCHAR))) &&
|
|
|
|
(localesig.lsUsb[3] & 0x08000000));
|
2001-02-15 02:13:07 +03:00
|
|
|
}
|