Bug 1306549 part.3 KeyboardLayout::InitNativeKey() should use GetUniCharsAndModifiers() instead of using VirtualKey::GetUniChars() directly r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 16 Sep 2016 18:22:12 +0900
changeset 420503 5a24c8c4bfaa75ffd7c9c5a72ed2476699a94291
parent 420502 771247191a87e9388dfa1841a3aaf9a91ceb22b0
child 420504 e27946e826e921b9a7bf4e6254a6cb8ae9e760bf
push id31215
push usermasayuki@d-toybox.com
push dateTue, 04 Oct 2016 07:44:41 +0000
reviewersm_kato
bugs1306549
milestone52.0a1
Bug 1306549 part.3 KeyboardLayout::InitNativeKey() should use GetUniCharsAndModifiers() instead of using VirtualKey::GetUniChars() directly r?m_kato When InitNativeKey() retrieves UniCharsAndModifiers for a key, it needs key index for the given virtual keycode. Therefore, wrapping the code with GetUniCharsAndModifiers() makes InitNativeKey() code simpler since each call specifies the virtual keycode to the method instead of key index. MozReview-Commit-ID: Azy8chXexaz
widget/windows/KeyboardLayout.cpp
widget/windows/KeyboardLayout.h
--- a/widget/windows/KeyboardLayout.cpp
+++ b/widget/windows/KeyboardLayout.cpp
@@ -3674,27 +3674,27 @@ KeyboardLayout::InitNativeKey(NativeKey&
 
     // At keydown message handling, we need to forget the first dead key
     // because there is no guarantee coming WM_KEYUP for the second dead
     // key before next WM_KEYDOWN.  E.g., due to auto key repeat or pressing
     // another dead key before releasing current key.  Therefore, we can
     // set only a character for current key for keyup event.
     if (mActiveDeadKey < 0) {
       aNativeKey.mCommittedCharsAndModifiers =
-        mVirtualKeys[virtualKeyIndex].GetUniChars(aModKeyState);
+        GetUniCharsAndModifiers(virtualKey, aModKeyState);
       return;
     }
 
-    int32_t activeDeadKeyIndex = GetKeyIndex(mActiveDeadKey);
-    if (activeDeadKeyIndex < 0 || activeDeadKeyIndex >= NS_NUM_OF_KEYS) {
+    if (NS_WARN_IF(!IsPrintableCharKey(mActiveDeadKey))) {
 #if defined(DEBUG) || defined(MOZ_CRASHREPORTER)
       nsPrintfCString warning("The virtual key index (%d) of mActiveDeadKey "
                               "(0x%02X) is not a printable key (virtualKey="
                               "0x%02X)",
-                              activeDeadKeyIndex, mActiveDeadKey, virtualKey);
+                              GetKeyIndex(mActiveDeadKey), mActiveDeadKey,
+                              virtualKey);
       NS_WARNING(warning.get());
 #ifdef MOZ_CRASHREPORTER
       CrashReporter::AppendAppNotesToCrashReport(
                        NS_LITERAL_CSTRING("\n") + warning);
 #endif // #ifdef MOZ_CRASHREPORTER
 #endif // #if defined(DEBUG) || defined(MOZ_CRASHREPORTER)
       MOZ_CRASH("Trying to reference out of range of mVirtualKeys");
     }
@@ -3703,48 +3703,47 @@ KeyboardLayout::InitNativeKey(NativeKey&
     // (e.g., "Russian - Mnemonic" keyboard layout's 's' -> 'c').
     if (MaybeInitNativeKeyWithCompositeChar(aNativeKey, aModKeyState)) {
       return;
     }
 
     // Otherwise, dead key followed by another dead key causes inputting both
     // character.
     UniCharsAndModifiers prevDeadChars =
-      mVirtualKeys[activeDeadKeyIndex].GetUniChars(mDeadKeyShiftState);
+      GetUniCharsAndModifiers(mActiveDeadKey, mDeadKeyShiftState);
     UniCharsAndModifiers newChars =
-      mVirtualKeys[virtualKeyIndex].GetUniChars(aModKeyState);
+      GetUniCharsAndModifiers(virtualKey, aModKeyState);
     // But keypress events should be fired for each committed character.
     aNativeKey.mCommittedCharsAndModifiers = prevDeadChars + newChars;
     if (isKeyDown) {
       DeactivateDeadKeyState();
     }
     return;
   }
 
   if (MaybeInitNativeKeyWithCompositeChar(aNativeKey, aModKeyState)) {
     return;
   }
 
   UniCharsAndModifiers baseChars =
-    mVirtualKeys[virtualKeyIndex].GetUniChars(aModKeyState);
+    GetUniCharsAndModifiers(virtualKey, aModKeyState);
   if (mActiveDeadKey < 0) {
     // No dead-keys are active. Just return the produced characters.
     aNativeKey.mCommittedCharsAndModifiers = baseChars;
     return;
   }
 
-  int32_t activeDeadKeyIndex = GetKeyIndex(mActiveDeadKey);
-  if (NS_WARN_IF(activeDeadKeyIndex < 0)) {
+  if (NS_WARN_IF(!IsPrintableCharKey(mActiveDeadKey))) {
     return;
   }
 
   // There is no valid dead-key and base character combination.
   // Return dead-key character followed by base character.
   UniCharsAndModifiers deadChars =
-    mVirtualKeys[activeDeadKeyIndex].GetUniChars(mDeadKeyShiftState);
+    GetUniCharsAndModifiers(mActiveDeadKey, mDeadKeyShiftState);
   // But keypress events should be fired for each committed character.
   aNativeKey.mCommittedCharsAndModifiers = deadChars + baseChars;
   if (isKeyDown) {
     DeactivateDeadKeyState();
   }
 }
 
 bool
@@ -3756,23 +3755,22 @@ KeyboardLayout::MaybeInitNativeKeyWithCo
     return false;
   }
 
   int32_t activeDeadKeyIndex = GetKeyIndex(mActiveDeadKey);
   if (NS_WARN_IF(activeDeadKeyIndex < 0)) {
     return false;
   }
 
-  int32_t virtualKeyIndex = GetKeyIndex(aNativeKey.mOriginalVirtualKeyCode);
-  if (NS_WARN_IF(virtualKeyIndex < 0)) {
+  if (NS_WARN_IF(!IsPrintableCharKey(aNativeKey.mOriginalVirtualKeyCode))) {
     return false;
   }
 
   UniCharsAndModifiers baseChars =
-    mVirtualKeys[virtualKeyIndex].GetUniChars(aModKeyState);
+    GetUniCharsAndModifiers(aNativeKey.mOriginalVirtualKeyCode, aModKeyState);
   if (baseChars.IsEmpty() || !baseChars.mChars[0]) {
     return false;
   }
 
   char16_t compositeChar =
     mVirtualKeys[activeDeadKeyIndex].GetCompositeChar(mDeadKeyShiftState,
                                                       baseChars.mChars[0]);
   if (!compositeChar) {
@@ -3787,25 +3785,24 @@ KeyboardLayout::MaybeInitNativeKeyWithCo
     DeactivateDeadKeyState();
   }
   return true;
 }
 
 UniCharsAndModifiers
 KeyboardLayout::GetUniCharsAndModifiers(
                   uint8_t aVirtualKey,
-                  const ModifierKeyState& aModKeyState) const
+                  VirtualKey::ShiftState aShiftState) const
 {
   UniCharsAndModifiers result;
   int32_t key = GetKeyIndex(aVirtualKey);
   if (key < 0) {
     return result;
   }
-  return mVirtualKeys[key].
-    GetUniChars(VirtualKey::ModifiersToShiftState(aModKeyState.GetModifiers()));
+  return mVirtualKeys[key].GetUniChars(aShiftState);
 }
 
 void
 KeyboardLayout::LoadLayout(HKL aLayout)
 {
   mIsPendingToRestoreKeyboardLayout = false;
 
   if (mKeyboardLayout == aLayout) {
--- a/widget/windows/KeyboardLayout.h
+++ b/widget/windows/KeyboardLayout.h
@@ -656,16 +656,23 @@ private:
    * InitNativeKey() must be called when actually widget receives WM_KEYDOWN or
    * WM_KEYUP.  This method is stateful.  This saves current dead key state at
    * WM_KEYDOWN.  Additionally, computes current inputted character(s) and set
    * them to the aNativeKey.
    */
   void InitNativeKey(NativeKey& aNativeKey,
                      const ModifierKeyState& aModKeyState);
 
+  /**
+   * See the comment of GetUniCharsAndModifiers() below.
+   */
+  UniCharsAndModifiers GetUniCharsAndModifiers(
+                         uint8_t aVirtualKey,
+                         VirtualKey::ShiftState aShiftState) const;
+
 public:
   static KeyboardLayout* GetInstance();
   static void Shutdown();
   static void NotifyIdleServiceOfUserActivity();
 
   static bool IsPrintableCharKey(uint8_t aVirtualKey);
 
   /**
@@ -689,20 +696,27 @@ public:
    */
   bool MaybeInitNativeKeyWithCompositeChar(
          NativeKey& aNativeKey,
          const ModifierKeyState& aModKeyState);
 
   /**
    * GetUniCharsAndModifiers() returns characters which is inputted by the
    * aVirtualKey with aModKeyState.  This method isn't stateful.
+   * Note that if the combination causes text input, the result's Ctrl and
+   * Alt key state are never active.
    */
   UniCharsAndModifiers GetUniCharsAndModifiers(
                          uint8_t aVirtualKey,
-                         const ModifierKeyState& aModKeyState) const;
+                         const ModifierKeyState& aModKeyState) const
+  {
+    VirtualKey::ShiftState shiftState =
+      VirtualKey::ModifierKeyStateToShiftState(aModKeyState);
+    return GetUniCharsAndModifiers(aVirtualKey, shiftState);
+  }
 
   /**
    * OnLayoutChange() must be called before the first keydown message is
    * received.  LoadLayout() changes the keyboard state, that causes breaking
    * dead key state.  Therefore, we need to load the layout before the first
    * keydown message.
    */
   void OnLayoutChange(HKL aKeyboardLayout)