1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
6 
7 #include <stdio.h>
8 #include "nsBidiKeyboard.h"
9 #include "WidgetUtils.h"
10 #include "nsIWidget.h"
11 #include <tchar.h>
12 
NS_IMPL_ISUPPORTS(nsBidiKeyboard,nsIBidiKeyboard)13 NS_IMPL_ISUPPORTS(nsBidiKeyboard, nsIBidiKeyboard)
14 
15 nsBidiKeyboard::nsBidiKeyboard() : nsIBidiKeyboard() { Reset(); }
16 
~nsBidiKeyboard()17 nsBidiKeyboard::~nsBidiKeyboard() {}
18 
Reset()19 NS_IMETHODIMP nsBidiKeyboard::Reset() {
20   mInitialized = false;
21   mHaveBidiKeyboards = false;
22   mLTRKeyboard[0] = '\0';
23   mRTLKeyboard[0] = '\0';
24   mCurrentLocaleName[0] = '\0';
25   return NS_OK;
26 }
27 
IsLangRTL(bool * aIsRTL)28 NS_IMETHODIMP nsBidiKeyboard::IsLangRTL(bool* aIsRTL) {
29   *aIsRTL = false;
30 
31   nsresult result = SetupBidiKeyboards();
32   if (NS_FAILED(result)) return result;
33 
34   HKL currentLocale;
35 
36   currentLocale = ::GetKeyboardLayout(0);
37   *aIsRTL = IsRTLLanguage(currentLocale);
38 
39   if (!::GetKeyboardLayoutNameW(mCurrentLocaleName)) return NS_ERROR_FAILURE;
40 
41   NS_ASSERTION(*mCurrentLocaleName,
42                "GetKeyboardLayoutName return string length == 0");
43   NS_ASSERTION((wcslen(mCurrentLocaleName) < KL_NAMELENGTH),
44                "GetKeyboardLayoutName return string length >= KL_NAMELENGTH");
45 
46   // The language set by the user overrides the default language for that
47   // direction
48   if (*aIsRTL) {
49     wcsncpy(mRTLKeyboard, mCurrentLocaleName, KL_NAMELENGTH);
50     mRTLKeyboard[KL_NAMELENGTH - 1] = '\0';  // null terminate
51   } else {
52     wcsncpy(mLTRKeyboard, mCurrentLocaleName, KL_NAMELENGTH);
53     mLTRKeyboard[KL_NAMELENGTH - 1] = '\0';  // null terminate
54   }
55 
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");
60   return NS_OK;
61 }
62 
GetHaveBidiKeyboards(bool * aResult)63 NS_IMETHODIMP nsBidiKeyboard::GetHaveBidiKeyboards(bool* aResult) {
64   NS_ENSURE_ARG_POINTER(aResult);
65 
66   nsresult result = SetupBidiKeyboards();
67   if (NS_FAILED(result)) return result;
68 
69   *aResult = mHaveBidiKeyboards;
70   return NS_OK;
71 }
72 
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.
SetupBidiKeyboards()77 nsresult nsBidiKeyboard::SetupBidiKeyboards() {
78   if (mInitialized) return mHaveBidiKeyboards ? NS_OK : NS_ERROR_FAILURE;
79 
80   int keyboards;
81   HKL far* buf;
82   HKL locale;
83   wchar_t localeName[KL_NAMELENGTH];
84   bool isLTRKeyboardSet = false;
85   bool isRTLKeyboardSet = false;
86 
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;
91 
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;
95 
96   // Call again to fill the buffer
97   if (::GetKeyboardLayoutList(keyboards, buf) != keyboards) {
98     free(buf);
99     return NS_ERROR_UNEXPECTED;
100   }
101 
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;
109     } else {
110       _snwprintf(mLTRKeyboard, KL_NAMELENGTH, L"%.*x", KL_NAMELENGTH - 1,
111                  LANGIDFROMLCID((DWORD_PTR)locale));
112       isLTRKeyboardSet = true;
113     }
114   }
115   free(buf);
116   mInitialized = true;
117 
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;
122 
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;
129 
130   NS_ASSERTION(*localeName, "GetKeyboardLayoutName return string length == 0");
131   NS_ASSERTION((wcslen(localeName) < KL_NAMELENGTH),
132                "GetKeyboardLayout return string length >= KL_NAMELENGTH");
133 
134   if (IsRTLLanguage(locale)) {
135     wcsncpy(mRTLKeyboard, localeName, KL_NAMELENGTH);
136     mRTLKeyboard[KL_NAMELENGTH - 1] = '\0';  // null terminate
137   } else {
138     wcsncpy(mLTRKeyboard, localeName, KL_NAMELENGTH);
139     mLTRKeyboard[KL_NAMELENGTH - 1] = '\0';  // null terminate
140   }
141 
142   NS_ASSERTION(*mRTLKeyboard, "mLTRKeyboard has string length == 0");
143   NS_ASSERTION(*mLTRKeyboard, "mLTRKeyboard has string length == 0");
144 
145   return NS_OK;
146 }
147 
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
151 // See
152 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/unicode_63ub.asp
IsRTLLanguage(HKL aLocale)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));
159 }
160 
161 // static
OnLayoutChange()162 void nsBidiKeyboard::OnLayoutChange() {
163   mozilla::widget::WidgetUtils::SendBidiKeyboardInfoToContent();
164 }
165 
166 // static
CreateBidiKeyboardInner()167 already_AddRefed<nsIBidiKeyboard> nsIWidget::CreateBidiKeyboardInner() {
168   return do_AddRef(new nsBidiKeyboard());
169 }
170