Bug 962140 Use PeekMessage() instead of GetMessage() in KeyboardLayout.cpp r=jimm
authorMasayuki Nakano <masayuki@d-toybox.com>
Sat, 01 Feb 2014 01:31:01 +0900
changeset 182290 8ded1c0760cc86b2f6cf1236a2a186415b8e87a2
parent 182289 074e87ffb410965abc94fcf098ce0e1eb6e1a761
child 182342 3c2673cd1e3c8b97e0a59162b38eaca396075983
child 182388 d02efae4db3a863c2d0ffaa479c735a128ca9bf7
child 182391 c108d4419d9a60e2609a65c36db4a988ed2bc07e
push id3343
push userffxbld
push dateMon, 17 Mar 2014 21:55:32 +0000
treeherdermozilla-beta@2f7d3415f79f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimm
bugs962140
milestone29.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 962140 Use PeekMessage() instead of GetMessage() in KeyboardLayout.cpp r=jimm
widget/windows/KeyboardLayout.cpp
widget/windows/KeyboardLayout.h
--- a/widget/windows/KeyboardLayout.cpp
+++ b/widget/windows/KeyboardLayout.cpp
@@ -748,34 +748,32 @@ NativeKey::IsFollowedByCharMessage() con
   if (mFakeCharMsgs) {
     nextMsg = mFakeCharMsgs->ElementAt(0).GetCharMsg(mMsg.hwnd);
   } else {
     if (!WinUtils::PeekMessage(&nextMsg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
                                PM_NOREMOVE | PM_NOYIELD)) {
       return false;
     }
   }
-  return (nextMsg.message == WM_CHAR ||
-          nextMsg.message == WM_SYSCHAR ||
-          nextMsg.message == WM_DEADCHAR);
+  return IsCharMessage(nextMsg);
 }
 
 bool
 NativeKey::IsFollowedByDeadCharMessage() const
 {
   MSG nextMsg;
   if (mFakeCharMsgs) {
     nextMsg = mFakeCharMsgs->ElementAt(0).GetCharMsg(mMsg.hwnd);
   } else {
     if (!WinUtils::PeekMessage(&nextMsg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
                                PM_NOREMOVE | PM_NOYIELD)) {
       return false;
     }
   }
-  return (nextMsg.message == WM_DEADCHAR);
+  return IsDeadCharMessage(nextMsg);
 }
 
 bool
 NativeKey::IsIMEDoingKakuteiUndo() const
 {
   // Following message pattern is caused by "Kakutei-Undo" of ATOK or WXG:
   // ---------------------------------------------------------------------------
   // WM_KEYDOWN              * n (wParam = VK_BACK, lParam = 0x1)
@@ -979,17 +977,17 @@ NativeKey::DispatchKeyEvent(WidgetKeyboa
   }
 
   return (mWidget->DispatchKeyboardEvent(&aKeyEvent) || mWidget->Destroyed());
 }
 
 bool
 NativeKey::HandleKeyDownMessage(bool* aEventDispatched) const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(IsKeyDownMessage());
 
   if (aEventDispatched) {
     *aEventDispatched = false;
   }
 
   bool defaultPrevented = false;
   if (mFakeCharMsgs ||
       !RedirectedKeyDownMessageManager::IsRedirectedMessage(mMsg)) {
@@ -1103,19 +1101,18 @@ NativeKey::HandleKeyDownMessage(bool* aE
 
   return DispatchKeyPressEventsWithKeyboardLayout();
 }
 
 bool
 NativeKey::HandleCharMessage(const MSG& aCharMsg,
                              bool* aEventDispatched) const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN ||
-             mMsg.message == WM_CHAR || mMsg.message == WM_SYSCHAR);
-  MOZ_ASSERT(aCharMsg.message == WM_CHAR || aCharMsg.message == WM_SYSCHAR);
+  MOZ_ASSERT(IsKeyDownMessage() || IsPrintableCharMessage(mMsg));
+  MOZ_ASSERT(IsPrintableCharMessage(aCharMsg.message));
 
   if (aEventDispatched) {
     *aEventDispatched = false;
   }
 
   // Alt+Space key is handled by OS, we shouldn't touch it.
   if (mModKeyState.IsAlt() && !mModKeyState.IsControl() &&
       mVirtualKeyCode == VK_SPACE) {
@@ -1215,17 +1212,17 @@ NativeKey::HandleCharMessage(const MSG& 
     *aEventDispatched = true;
   }
   return DispatchKeyEvent(keypressEvent, &aCharMsg);
 }
 
 bool
 NativeKey::HandleKeyUpMessage(bool* aEventDispatched) const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYUP || mMsg.message == WM_SYSKEYUP);
+  MOZ_ASSERT(IsKeyUpMessage());
 
   if (aEventDispatched) {
     *aEventDispatched = false;
   }
 
   // Ignore [shift+]alt+space so the OS can handle it.
   if (mModKeyState.IsAlt() && !mModKeyState.IsControl() &&
       mVirtualKeyCode == VK_SPACE) {
@@ -1238,17 +1235,17 @@ NativeKey::HandleKeyUpMessage(bool* aEve
     *aEventDispatched = true;
   }
   return DispatchKeyEvent(keyupEvent, &mMsg);
 }
 
 bool
 NativeKey::NeedsToHandleWithoutFollowingCharMessages() const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(IsKeyDownMessage());
 
   // Enter and backspace are always handled here to avoid for example the
   // confusion between ctrl-enter and ctrl-J.
   if (mDOMKeyCode == NS_VK_RETURN || mDOMKeyCode == NS_VK_BACK) {
     return true;
   }
 
   // If any modifier keys which may cause printable keys becoming non-printable
@@ -1277,19 +1274,19 @@ NativeKey::RemoveFollowingCharMessage() 
   if (mFakeCharMsgs) {
     MOZ_ASSERT(!mFakeCharMsgs->ElementAt(0).mConsumed,
       "Doesn't assume that it's used for removing two or more char messages");
     mFakeCharMsgs->ElementAt(0).mConsumed = true;
     return mFakeCharMsgs->ElementAt(0).GetCharMsg(mMsg.hwnd);
   }
 
   MSG msg;
-  if (!WinUtils::GetMessage(&msg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST) ||
-      !(msg.message == WM_CHAR || msg.message == WM_SYSCHAR ||
-        msg.message == WM_DEADCHAR)) {
+  if (!WinUtils::PeekMessage(&msg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
+                             PM_REMOVE | PM_NOYIELD) ||
+      !IsCharMessage(msg)) {
     MOZ_CRASH("We lost the following char message");
   }
 
   return msg;
 }
 
 bool
 NativeKey::RemoveMessageAndDispatchPluginEvent(UINT aFirstMsg,
@@ -1309,29 +1306,30 @@ NativeKey::RemoveMessageAndDispatchPlugi
       }
       fakeCharMsg.mConsumed = true;
       msg = fakeMsg;
       found = true;
       break;
     }
     MOZ_ASSERT(found, "Fake char message must be found");
   } else {
-    WinUtils::GetMessage(&msg, mMsg.hwnd, aFirstMsg, aLastMsg);
+    WinUtils::PeekMessage(&msg, mMsg.hwnd, aFirstMsg, aLastMsg,
+                          PM_REMOVE | PM_NOYIELD);
   }
   if (mWidget->Destroyed()) {
     MOZ_CRASH("NativeKey tries to dispatch a plugin event on destroyed widget");
   }
   mWidget->DispatchPluginEvent(msg);
   return mWidget->Destroyed();
 }
 
 bool
 NativeKey::DispatchPluginEventsAndDiscardsCharMessages() const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(IsKeyDownMessage());
 
   if (mFakeCharMsgs) {
     for (uint32_t i = 0; i < mFakeCharMsgs->Length(); i++) {
       if (RemoveMessageAndDispatchPluginEvent(WM_KEYFIRST, WM_KEYLAST)) {
         return true;
       }
     }
     return false;
@@ -1341,19 +1339,17 @@ NativeKey::DispatchPluginEventsAndDiscar
   // They can be more than one because of:
   //  * Dead-keys not pairing with base character
   //  * Some keyboard layouts may map up to 4 characters to the single key
   bool anyCharMessagesRemoved = true;
   MSG msg;
   bool gotMsg =
     WinUtils::PeekMessage(&msg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
                           PM_NOREMOVE | PM_NOYIELD);
-  while (gotMsg &&
-         (msg.message == WM_CHAR || msg.message == WM_SYSCHAR ||
-          msg.message == WM_DEADCHAR)) {
+  while (gotMsg && IsCharMessage(msg)) {
     if (RemoveMessageAndDispatchPluginEvent(WM_KEYFIRST, WM_KEYLAST)) {
       return true;
     }
     anyCharMessagesRemoved = true;
     gotMsg = WinUtils::PeekMessage(&msg, mMsg.hwnd, WM_KEYFIRST, WM_KEYLAST,
                                    PM_NOREMOVE | PM_NOYIELD);
   }
 
@@ -1364,17 +1360,17 @@ NativeKey::DispatchPluginEventsAndDiscar
   }
 
   return false;
 }
 
 bool
 NativeKey::DispatchKeyPressEventsWithKeyboardLayout() const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(IsKeyDownMessage());
   MOZ_ASSERT(!mIsDeadKey);
 
   KeyboardLayout* keyboardLayout = KeyboardLayout::GetInstance();
 
   UniCharsAndModifiers inputtingChars(mCommittedCharsAndModifiers);
   UniCharsAndModifiers shiftedChars;
   UniCharsAndModifiers unshiftedChars;
   uint32_t shiftedLatinChar = 0;
@@ -1528,21 +1524,21 @@ NativeKey::DispatchKeyPressEventsWithKey
   }
 
   return defaultPrevented;
 }
 
 bool
 NativeKey::DispatchKeyPressEventForFollowingCharMessage() const
 {
-  MOZ_ASSERT(mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
+  MOZ_ASSERT(IsKeyDownMessage());
 
   MSG msg = RemoveFollowingCharMessage();
   if (mFakeCharMsgs) {
-    if (msg.message == WM_DEADCHAR) {
+    if (IsDeadCharMessage(msg)) {
       return false;
     }
 #ifdef DEBUG
     if (mIsPrintableKey) {
       nsPrintfCString log(
         "mOriginalVirtualKeyCode=0x%02X, mCommittedCharsAndModifiers={ "
         "mChars=[ 0x%04X, 0x%04X, 0x%04X, 0x%04X, 0x%04X ], mLength=%d }, "
         "wParam=0x%04X",
@@ -1561,27 +1557,27 @@ NativeKey::DispatchKeyPressEventForFollo
         NS_ERROR(log.get());
         NS_ABORT();
       }
     }
 #endif // #ifdef DEBUG
     return HandleCharMessage(msg);
   }
 
-  if (msg.message == WM_DEADCHAR) {
+  if (IsDeadCharMessage(msg)) {
     if (!mWidget->PluginHasFocus()) {
       return false;
     }
     return (mWidget->DispatchPluginEvent(msg) || mWidget->Destroyed());
   }
 
   bool defaultPrevented = HandleCharMessage(msg);
   // If a syschar keypress wasn't processed, Windows may want to
   // handle it to activate a native menu.
-  if (!defaultPrevented && msg.message == WM_SYSCHAR) {
+  if (!defaultPrevented && IsSysCharMessage(msg)) {
     ::DefWindowProcW(msg.hwnd, msg.message, msg.wParam, msg.lParam);
   }
   return defaultPrevented;
 }
 
 /*****************************************************************************
  * mozilla::widget::KeyboardLayout
  *****************************************************************************/
@@ -2646,15 +2642,16 @@ RedirectedKeyDownMessageManager::IsRedir
 // static
 void
 RedirectedKeyDownMessageManager::RemoveNextCharMessage(HWND aWnd)
 {
   MSG msg;
   if (WinUtils::PeekMessage(&msg, aWnd, WM_KEYFIRST, WM_KEYLAST,
                             PM_NOREMOVE | PM_NOYIELD) &&
       (msg.message == WM_CHAR || msg.message == WM_SYSCHAR)) {
-    WinUtils::GetMessage(&msg, aWnd, msg.message, msg.message);
+    WinUtils::PeekMessage(&msg, aWnd, msg.message, msg.message,
+                          PM_REMOVE | PM_NOYIELD);
   }
 }
 
 } // namespace widget
 } // namespace mozilla
 
--- a/widget/windows/KeyboardLayout.h
+++ b/widget/windows/KeyboardLayout.h
@@ -333,16 +333,52 @@ private:
    * Returns true if the widget is destoyed.  Otherwise, false.
    */
   bool RemoveMessageAndDispatchPluginEvent(UINT aFirstMsg, UINT aLastMsg) const;
 
   bool IsKeyDownMessage() const
   {
     return (mMsg.message == WM_KEYDOWN || mMsg.message == WM_SYSKEYDOWN);
   }
+  bool IsKeyUpMessage() const
+  {
+    return (mMsg.message == WM_KEYUP || mMsg.message == WM_SYSKEYUP);
+  }
+  bool IsPrintableCharMessage(const MSG& aMSG) const
+  {
+    return IsPrintableCharMessage(aMSG.message);
+  }
+  bool IsPrintableCharMessage(UINT aMessage) const
+  {
+    return (aMessage == WM_CHAR || aMessage == WM_SYSCHAR);
+  }
+  bool IsCharMessage(const MSG& aMSG) const
+  {
+    return IsCharMessage(aMSG.message);
+  }
+  bool IsCharMessage(UINT aMessage) const
+  {
+    return (IsPrintableCharMessage(aMessage) || IsDeadCharMessage(aMessage));
+  }
+  bool IsDeadCharMessage(const MSG& aMSG) const
+  {
+    return IsDeadCharMessage(aMSG.message);
+  }
+  bool IsDeadCharMessage(UINT aMessage) const
+  {
+    return (aMessage == WM_DEADCHAR || aMessage == WM_SYSDEADCHAR);
+  }
+  bool IsSysCharMessage(const MSG& aMSG) const
+  {
+    return IsSysCharMessage(aMSG.message);
+  }
+  bool IsSysCharMessage(UINT aMessage) const
+  {
+    return (aMessage == WM_SYSCHAR || aMessage == WM_SYSDEADCHAR);
+  }
   bool IsFollowedByCharMessage() const;
   bool IsFollowedByDeadCharMessage() const;
   MSG RemoveFollowingCharMessage() const;
 
   /**
    * Wraps MapVirtualKeyEx() with MAPVK_VSC_TO_VK.
    */
   uint8_t ComputeVirtualKeyCodeFromScanCode() const;