Bug 593372 - Part 3: Always process mouse and keyboard events in the right order. r=jmathies a=blocking-betaN
authorCameron McCormack <cam@mcc.id.au>
Fri, 11 Feb 2011 20:56:12 -0500
changeset 62479 fa7c98cbc33959fd77d3939a81941945d53b062b
parent 62478 ace573ee54218966898d9b9aae0cc8e1e5951869
child 62480 e9899b77c6ea78d8c178f9d8c64ece1766f1e778
push id18737
push usereakhgari@mozilla.com
push dateSat, 12 Feb 2011 19:37:20 +0000
treeherdermozilla-central@e9899b77c6ea [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjmathies, blocking-betaN
bugs593372
milestone2.0b12pre
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 593372 - Part 3: Always process mouse and keyboard events in the right order. r=jmathies a=blocking-betaN
widget/src/windows/nsAppShell.cpp
--- a/widget/src/windows/nsAppShell.cpp
+++ b/widget/src/windows/nsAppShell.cpp
@@ -84,39 +84,45 @@ namespace crashreporter {
 void LSPAnnotate();
 } // namespace crashreporter
 } // namespace mozilla
 
 using mozilla::crashreporter::LSPAnnotate;
 
 //-------------------------------------------------------------------------
 
-static BOOL PeekKeyAndIMEMessage(LPMSG msg, HWND hwnd)
+static PRBool PeekUIMessage(MSG* aMsg)
 {
-  MSG msg1, msg2, *lpMsg;
-  BOOL b1, b2;
-  b1 = ::PeekMessageW(&msg1, NULL, WM_KEYFIRST, WM_IME_KEYLAST, PM_NOREMOVE);
-  b2 = ::PeekMessageW(&msg2, NULL, NS_WM_IMEFIRST, NS_WM_IMELAST, PM_NOREMOVE);
-  if (b1 || b2) {
-    if (b1 && b2) {
-      if (msg1.time < msg2.time)
-        lpMsg = &msg1;
-      else
-        lpMsg = &msg2;
-    } else if (b1)
-      lpMsg = &msg1;
-    else
-      lpMsg = &msg2;
-    if (!nsIMM32Handler::CanOptimizeKeyAndIMEMessages(lpMsg)) {
-      return false;
-    }
-    return ::PeekMessageW(msg, hwnd, lpMsg->message, lpMsg->message, PM_REMOVE);
+  MSG keyMsg, imeMsg, mouseMsg, *pMsg = 0;
+  PRBool haveKeyMsg, haveIMEMsg, haveMouseMsg;
+
+  haveKeyMsg = ::PeekMessageW(&keyMsg, NULL, WM_KEYFIRST, WM_IME_KEYLAST, PM_NOREMOVE);
+  haveIMEMsg = ::PeekMessageW(&imeMsg, NULL, NS_WM_IMEFIRST, NS_WM_IMELAST, PM_NOREMOVE);
+  haveMouseMsg = ::PeekMessageW(&mouseMsg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_NOREMOVE);
+
+  if (haveKeyMsg) {
+    pMsg = &keyMsg;
+  }
+  if (haveIMEMsg && (!pMsg || imeMsg.time < pMsg->time)) {
+    pMsg = &imeMsg;
   }
 
-  return false;
+  if (pMsg && !nsIMM32Handler::CanOptimizeKeyAndIMEMessages(pMsg)) {
+    return PR_FALSE;
+  }
+
+  if (haveMouseMsg && (!pMsg || mouseMsg.time < pMsg->time)) {
+    pMsg = &mouseMsg;
+  }
+
+  if (!pMsg) {
+    return PR_FALSE;
+  }
+
+  return ::PeekMessageW(aMsg, NULL, pMsg->message, pMsg->message, PM_REMOVE);
 }
 
 /*static*/ LRESULT CALLBACK
 nsAppShell::EventWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 {
   if (uMsg == sMsgId) {
     nsAppShell *as = reinterpret_cast<nsAppShell *>(lParam);
     as->NativeEventCallback();
@@ -316,20 +322,18 @@ nsAppShell::ProcessNextNativeEvent(PRBoo
     CollectNewLoadedModules();
   }
 #endif
 
   PRBool gotMessage = PR_FALSE;
 
   do {
     MSG msg;
-    // Give priority to system messages (in particular keyboard, mouse, timer,
-    // and paint messages).
-    if (PeekKeyAndIMEMessage(&msg, NULL) ||
-        ::PeekMessageW(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE) || 
+    // Give priority to keyboard and mouse messages.
+    if (PeekUIMessage(&msg) ||
         ::PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
       gotMessage = PR_TRUE;
       if (msg.message == WM_QUIT) {
         ::PostQuitMessage(msg.wParam);
         Exit();
       } else {
         ::TranslateMessage(&msg);
         ::DispatchMessageW(&msg);