1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsBidiKeyboard.h"
9 #include "WidgetUtils.h"
10 #include "nsIWidget.h"
13 NS_IMPL_ISUPPORTS(nsBidiKeyboard
, nsIBidiKeyboard
)
15 nsBidiKeyboard::nsBidiKeyboard() : nsIBidiKeyboard() { Reset(); }
17 nsBidiKeyboard::~nsBidiKeyboard() {}
19 NS_IMETHODIMP
nsBidiKeyboard::Reset() {
21 mHaveBidiKeyboards
= false;
22 mLTRKeyboard
[0] = '\0';
23 mRTLKeyboard
[0] = '\0';
24 mCurrentLocaleName
[0] = '\0';
28 NS_IMETHODIMP
nsBidiKeyboard::IsLangRTL(bool* aIsRTL
) {
31 nsresult result
= SetupBidiKeyboards();
32 if (NS_FAILED(result
)) return result
;
36 currentLocale
= ::GetKeyboardLayout(0);
37 *aIsRTL
= IsRTLLanguage(currentLocale
);
39 if (!::GetKeyboardLayoutNameW(mCurrentLocaleName
)) return NS_ERROR_FAILURE
;
41 NS_ASSERTION(*mCurrentLocaleName
,
42 "GetKeyboardLayoutName return string length == 0");
43 NS_ASSERTION((wcslen(mCurrentLocaleName
) < KL_NAMELENGTH
),
44 "GetKeyboardLayoutName return string length >= KL_NAMELENGTH");
46 // The language set by the user overrides the default language for that
49 wcsncpy(mRTLKeyboard
, mCurrentLocaleName
, KL_NAMELENGTH
);
50 mRTLKeyboard
[KL_NAMELENGTH
- 1] = '\0'; // null terminate
52 wcsncpy(mLTRKeyboard
, mCurrentLocaleName
, KL_NAMELENGTH
);
53 mLTRKeyboard
[KL_NAMELENGTH
- 1] = '\0'; // null terminate
56 NS_ASSERTION((wcslen(mRTLKeyboard
) < KL_NAMELENGTH
),
57 "mLTRKeyboard has string length >= KL_NAMELENGTH");
58 NS_ASSERTION((wcslen(mLTRKeyboard
) < KL_NAMELENGTH
),
59 "mRTLKeyboard has string length >= KL_NAMELENGTH");
63 NS_IMETHODIMP
nsBidiKeyboard::GetHaveBidiKeyboards(bool* aResult
) {
64 NS_ENSURE_ARG_POINTER(aResult
);
66 nsresult result
= SetupBidiKeyboards();
67 if (NS_FAILED(result
)) return result
;
69 *aResult
= mHaveBidiKeyboards
;
73 // Get the list of keyboard layouts available in the system
74 // Set mLTRKeyboard to the first LTR keyboard in the list and mRTLKeyboard to
75 // the first RTL keyboard in the list These defaults will be used unless the
76 // user explicitly sets something else.
77 nsresult
nsBidiKeyboard::SetupBidiKeyboards() {
78 if (mInitialized
) return mHaveBidiKeyboards
? NS_OK
: NS_ERROR_FAILURE
;
83 wchar_t localeName
[KL_NAMELENGTH
];
84 bool isLTRKeyboardSet
= false;
85 bool isRTLKeyboardSet
= false;
87 // GetKeyboardLayoutList with 0 as first parameter returns the number of
88 // keyboard layouts available
89 keyboards
= ::GetKeyboardLayoutList(0, nullptr);
90 if (!keyboards
) return NS_ERROR_FAILURE
;
92 // allocate a buffer to hold the list
93 buf
= (HKL far
*)malloc(keyboards
* sizeof(HKL
));
94 if (!buf
) return NS_ERROR_OUT_OF_MEMORY
;
96 // Call again to fill the buffer
97 if (::GetKeyboardLayoutList(keyboards
, buf
) != keyboards
) {
99 return NS_ERROR_UNEXPECTED
;
102 // Go through the list and pick a default LTR and RTL keyboard layout
103 while (keyboards
--) {
104 locale
= buf
[keyboards
];
105 if (IsRTLLanguage(locale
)) {
106 _snwprintf(mRTLKeyboard
, KL_NAMELENGTH
, L
"%.*x", KL_NAMELENGTH
- 1,
107 LANGIDFROMLCID((DWORD_PTR
)locale
));
108 isRTLKeyboardSet
= true;
110 _snwprintf(mLTRKeyboard
, KL_NAMELENGTH
, L
"%.*x", KL_NAMELENGTH
- 1,
111 LANGIDFROMLCID((DWORD_PTR
)locale
));
112 isLTRKeyboardSet
= true;
118 // If there is not at least one keyboard of each directionality, Bidi
119 // keyboard functionality will be disabled.
120 mHaveBidiKeyboards
= (isRTLKeyboardSet
&& isLTRKeyboardSet
);
121 if (!mHaveBidiKeyboards
) return NS_ERROR_FAILURE
;
123 // Get the current keyboard layout and use it for either mRTLKeyboard or
124 // mLTRKeyboard as appropriate. If the user has many keyboard layouts
125 // installed this prevents us from arbitrarily resetting the current
126 // layout (bug 80274)
127 locale
= ::GetKeyboardLayout(0);
128 if (!::GetKeyboardLayoutNameW(localeName
)) return NS_ERROR_FAILURE
;
130 NS_ASSERTION(*localeName
, "GetKeyboardLayoutName return string length == 0");
131 NS_ASSERTION((wcslen(localeName
) < KL_NAMELENGTH
),
132 "GetKeyboardLayout return string length >= KL_NAMELENGTH");
134 if (IsRTLLanguage(locale
)) {
135 wcsncpy(mRTLKeyboard
, localeName
, KL_NAMELENGTH
);
136 mRTLKeyboard
[KL_NAMELENGTH
- 1] = '\0'; // null terminate
138 wcsncpy(mLTRKeyboard
, localeName
, KL_NAMELENGTH
);
139 mLTRKeyboard
[KL_NAMELENGTH
- 1] = '\0'; // null terminate
142 NS_ASSERTION(*mRTLKeyboard
, "mLTRKeyboard has string length == 0");
143 NS_ASSERTION(*mLTRKeyboard
, "mLTRKeyboard has string length == 0");
148 // Test whether the language represented by this locale identifier is a
149 // right-to-left language, using bit 123 of the Unicode subset bitfield in
150 // the LOCALESIGNATURE
152 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/unicode_63ub.asp
153 bool nsBidiKeyboard::IsRTLLanguage(HKL aLocale
) {
154 LOCALESIGNATURE localesig
;
155 return (::GetLocaleInfoW(PRIMARYLANGID((DWORD_PTR
)aLocale
),
156 LOCALE_FONTSIGNATURE
, (LPWSTR
)&localesig
,
157 (sizeof(localesig
) / sizeof(WCHAR
))) &&
158 (localesig
.lsUsb
[3] & 0x08000000));
162 void nsBidiKeyboard::OnLayoutChange() {
163 mozilla::widget::WidgetUtils::SendBidiKeyboardInfoToContent();
167 already_AddRefed
<nsIBidiKeyboard
> nsIWidget::CreateBidiKeyboardInner() {
168 return do_AddRef(new nsBidiKeyboard());