Bug 1671598 [wpt PR 26128] - [AspectRatio] Fix divide by zero with a small float...
[gecko.git] / widget / windows / nsBidiKeyboard.cpp
blob87d81d458e20d1011b8c85ddb8d322a98b26f0bd
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/. */
7 #include <stdio.h>
8 #include "nsBidiKeyboard.h"
9 #include "WidgetUtils.h"
10 #include "nsIWidget.h"
11 #include <tchar.h>
13 NS_IMPL_ISUPPORTS(nsBidiKeyboard, nsIBidiKeyboard)
15 nsBidiKeyboard::nsBidiKeyboard() : nsIBidiKeyboard() { Reset(); }
17 nsBidiKeyboard::~nsBidiKeyboard() {}
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;
28 NS_IMETHODIMP nsBidiKeyboard::IsLangRTL(bool* aIsRTL) {
29 *aIsRTL = false;
31 nsresult result = SetupBidiKeyboards();
32 if (NS_FAILED(result)) return result;
34 HKL currentLocale;
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
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
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;
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;
70 return NS_OK;
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;
80 int keyboards;
81 HKL far* buf;
82 HKL locale;
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) {
98 free(buf);
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;
109 } else {
110 _snwprintf(mLTRKeyboard, KL_NAMELENGTH, L"%.*x", KL_NAMELENGTH - 1,
111 LANGIDFROMLCID((DWORD_PTR)locale));
112 isLTRKeyboardSet = true;
115 free(buf);
116 mInitialized = 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
137 } else {
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");
145 return NS_OK;
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
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));
161 // static
162 void nsBidiKeyboard::OnLayoutChange() {
163 mozilla::widget::WidgetUtils::SendBidiKeyboardInfoToContent();
166 // static
167 already_AddRefed<nsIBidiKeyboard> nsIWidget::CreateBidiKeyboardInner() {
168 return do_AddRef(new nsBidiKeyboard());