widget/windows/WinMouseScrollHandler.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/windows/WinMouseScrollHandler.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1612 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "mozilla/DebugOnly.h"
    1.11 +
    1.12 +#ifdef MOZ_LOGGING
    1.13 +#define FORCE_PR_LOG /* Allow logging in the release build */
    1.14 +#endif // MOZ_LOGGING
    1.15 +#include "prlog.h"
    1.16 +
    1.17 +#include "WinMouseScrollHandler.h"
    1.18 +#include "nsWindow.h"
    1.19 +#include "nsWindowDefs.h"
    1.20 +#include "KeyboardLayout.h"
    1.21 +#include "WinUtils.h"
    1.22 +#include "nsGkAtoms.h"
    1.23 +#include "nsIDOMWindowUtils.h"
    1.24 +#include "nsIDOMWheelEvent.h"
    1.25 +
    1.26 +#include "mozilla/MiscEvents.h"
    1.27 +#include "mozilla/MouseEvents.h"
    1.28 +#include "mozilla/Preferences.h"
    1.29 +#include "mozilla/WindowsVersion.h"
    1.30 +
    1.31 +#include <psapi.h>
    1.32 +
    1.33 +namespace mozilla {
    1.34 +namespace widget {
    1.35 +
    1.36 +#ifdef PR_LOGGING
    1.37 +PRLogModuleInfo* gMouseScrollLog = nullptr;
    1.38 +
    1.39 +static const char* GetBoolName(bool aBool)
    1.40 +{
    1.41 +  return aBool ? "TRUE" : "FALSE";
    1.42 +}
    1.43 +
    1.44 +static void LogKeyStateImpl()
    1.45 +{
    1.46 +  if (!PR_LOG_TEST(gMouseScrollLog, PR_LOG_DEBUG)) {
    1.47 +    return;
    1.48 +  }
    1.49 +  BYTE keyboardState[256];
    1.50 +  if (::GetKeyboardState(keyboardState)) {
    1.51 +    for (size_t i = 0; i < ArrayLength(keyboardState); i++) {
    1.52 +      if (keyboardState[i]) {
    1.53 +        PR_LOG(gMouseScrollLog, PR_LOG_DEBUG,
    1.54 +          ("    Current key state: keyboardState[0x%02X]=0x%02X (%s)",
    1.55 +           i, keyboardState[i],
    1.56 +           ((keyboardState[i] & 0x81) == 0x81) ? "Pressed and Toggled" :
    1.57 +           (keyboardState[i] & 0x80) ? "Pressed" :
    1.58 +           (keyboardState[i] & 0x01) ? "Toggled" : "Unknown"));
    1.59 +      }
    1.60 +    }
    1.61 +  } else {
    1.62 +    PR_LOG(gMouseScrollLog, PR_LOG_DEBUG,
    1.63 +      ("MouseScroll::Device::Elantech::HandleKeyMessage(): Failed to print "
    1.64 +       "current keyboard state"));
    1.65 +  }
    1.66 +}
    1.67 +
    1.68 +#define LOG_KEYSTATE() LogKeyStateImpl()
    1.69 +#else // PR_LOGGING
    1.70 +#define LOG_KEYSTATE()
    1.71 +#endif
    1.72 +
    1.73 +MouseScrollHandler* MouseScrollHandler::sInstance = nullptr;
    1.74 +
    1.75 +bool MouseScrollHandler::Device::sFakeScrollableWindowNeeded = false;
    1.76 +
    1.77 +bool MouseScrollHandler::Device::Elantech::sUseSwipeHack = false;
    1.78 +bool MouseScrollHandler::Device::Elantech::sUsePinchHack = false;
    1.79 +DWORD MouseScrollHandler::Device::Elantech::sZoomUntil = 0;
    1.80 +
    1.81 +bool MouseScrollHandler::Device::SetPoint::sMightBeUsing = false;
    1.82 +
    1.83 +// The duration until timeout of events transaction.  The value is 1.5 sec,
    1.84 +// it's just a magic number, it was suggested by Logitech's engineer, see
    1.85 +// bug 605648 comment 90.
    1.86 +#define DEFAULT_TIMEOUT_DURATION 1500
    1.87 +
    1.88 +/******************************************************************************
    1.89 + *
    1.90 + * MouseScrollHandler
    1.91 + *
    1.92 + ******************************************************************************/
    1.93 +
    1.94 +/* static */
    1.95 +POINTS
    1.96 +MouseScrollHandler::GetCurrentMessagePos()
    1.97 +{
    1.98 +  if (SynthesizingEvent::IsSynthesizing()) {
    1.99 +    return sInstance->mSynthesizingEvent->GetCursorPoint();
   1.100 +  }
   1.101 +  DWORD pos = ::GetMessagePos();
   1.102 +  return MAKEPOINTS(pos);
   1.103 +}
   1.104 +
   1.105 +// Get rid of the GetMessagePos() API.
   1.106 +#define GetMessagePos()
   1.107 +
   1.108 +/* static */
   1.109 +void
   1.110 +MouseScrollHandler::Initialize()
   1.111 +{
   1.112 +#ifdef PR_LOGGING
   1.113 +  if (!gMouseScrollLog) {
   1.114 +    gMouseScrollLog = PR_NewLogModule("MouseScrollHandlerWidgets");
   1.115 +  }
   1.116 +#endif
   1.117 +  Device::Init();
   1.118 +}
   1.119 +
   1.120 +/* static */
   1.121 +void
   1.122 +MouseScrollHandler::Shutdown()
   1.123 +{
   1.124 +  delete sInstance;
   1.125 +  sInstance = nullptr;
   1.126 +}
   1.127 +
   1.128 +/* static */
   1.129 +MouseScrollHandler*
   1.130 +MouseScrollHandler::GetInstance()
   1.131 +{
   1.132 +  if (!sInstance) {
   1.133 +    sInstance = new MouseScrollHandler();
   1.134 +  }
   1.135 +  return sInstance;
   1.136 +}
   1.137 +
   1.138 +MouseScrollHandler::MouseScrollHandler() :
   1.139 +  mIsWaitingInternalMessage(false),
   1.140 +  mSynthesizingEvent(nullptr)
   1.141 +{
   1.142 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.143 +    ("MouseScroll: Creating an instance, this=%p, sInstance=%p",
   1.144 +     this, sInstance));
   1.145 +}
   1.146 +
   1.147 +MouseScrollHandler::~MouseScrollHandler()
   1.148 +{
   1.149 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.150 +    ("MouseScroll: Destroying an instance, this=%p, sInstance=%p",
   1.151 +     this, sInstance));
   1.152 +
   1.153 +  delete mSynthesizingEvent;
   1.154 +}
   1.155 +
   1.156 +/* static */
   1.157 +bool
   1.158 +MouseScrollHandler::NeedsMessage(UINT aMsg)
   1.159 +{
   1.160 +  switch (aMsg) {
   1.161 +    case WM_SETTINGCHANGE:
   1.162 +    case WM_MOUSEWHEEL:
   1.163 +    case WM_MOUSEHWHEEL:
   1.164 +    case WM_HSCROLL:
   1.165 +    case WM_VSCROLL:
   1.166 +    case MOZ_WM_MOUSEVWHEEL:
   1.167 +    case MOZ_WM_MOUSEHWHEEL:
   1.168 +    case MOZ_WM_HSCROLL:
   1.169 +    case MOZ_WM_VSCROLL:
   1.170 +    case WM_KEYDOWN:
   1.171 +    case WM_KEYUP:
   1.172 +      return true;
   1.173 +  }
   1.174 +  return false;
   1.175 +}
   1.176 +
   1.177 +/* static */
   1.178 +bool
   1.179 +MouseScrollHandler::ProcessMessage(nsWindowBase* aWidget, UINT msg,
   1.180 +                                   WPARAM wParam, LPARAM lParam,
   1.181 +                                   MSGResult& aResult)
   1.182 +{
   1.183 +  Device::Elantech::UpdateZoomUntil();
   1.184 +
   1.185 +  switch (msg) {
   1.186 +    case WM_SETTINGCHANGE:
   1.187 +      if (!sInstance) {
   1.188 +        return false;
   1.189 +      }
   1.190 +      if (wParam == SPI_SETWHEELSCROLLLINES ||
   1.191 +          wParam == SPI_SETWHEELSCROLLCHARS) {
   1.192 +        sInstance->mSystemSettings.MarkDirty();
   1.193 +      }
   1.194 +      return false;
   1.195 +
   1.196 +    case WM_MOUSEWHEEL:
   1.197 +    case WM_MOUSEHWHEEL:
   1.198 +      GetInstance()->
   1.199 +        ProcessNativeMouseWheelMessage(aWidget, msg, wParam, lParam);
   1.200 +      sInstance->mSynthesizingEvent->NotifyNativeMessageHandlingFinished();
   1.201 +      // We don't need to call next wndproc for WM_MOUSEWHEEL and
   1.202 +      // WM_MOUSEHWHEEL.  We should consume them always.  If the messages
   1.203 +      // would be handled by our window again, it caused making infinite
   1.204 +      // message loop.
   1.205 +      aResult.mConsumed = true;
   1.206 +      aResult.mResult = (msg != WM_MOUSEHWHEEL);
   1.207 +      return true;
   1.208 +
   1.209 +    case WM_HSCROLL:
   1.210 +    case WM_VSCROLL:
   1.211 +      aResult.mConsumed =
   1.212 +        GetInstance()->ProcessNativeScrollMessage(aWidget, msg, wParam, lParam);
   1.213 +      sInstance->mSynthesizingEvent->NotifyNativeMessageHandlingFinished();
   1.214 +      aResult.mResult = 0;
   1.215 +      return true;
   1.216 +
   1.217 +    case MOZ_WM_MOUSEVWHEEL:
   1.218 +    case MOZ_WM_MOUSEHWHEEL:
   1.219 +      GetInstance()->HandleMouseWheelMessage(aWidget, msg, wParam, lParam);
   1.220 +      sInstance->mSynthesizingEvent->NotifyInternalMessageHandlingFinished();
   1.221 +      // Doesn't need to call next wndproc for internal wheel message.
   1.222 +      aResult.mConsumed = true;
   1.223 +      return true;
   1.224 +
   1.225 +    case MOZ_WM_HSCROLL:
   1.226 +    case MOZ_WM_VSCROLL:
   1.227 +      GetInstance()->
   1.228 +        HandleScrollMessageAsMouseWheelMessage(aWidget, msg, wParam, lParam);
   1.229 +      sInstance->mSynthesizingEvent->NotifyInternalMessageHandlingFinished();
   1.230 +      // Doesn't need to call next wndproc for internal scroll message.
   1.231 +      aResult.mConsumed = true;
   1.232 +      return true;
   1.233 +
   1.234 +    case WM_KEYDOWN:
   1.235 +    case WM_KEYUP:
   1.236 +      PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.237 +        ("MouseScroll::ProcessMessage(): aWidget=%p, "
   1.238 +         "msg=%s(0x%04X), wParam=0x%02X, ::GetMessageTime()=%d",
   1.239 +         aWidget, msg == WM_KEYDOWN ? "WM_KEYDOWN" :
   1.240 +                    msg == WM_KEYUP ? "WM_KEYUP" : "Unknown", msg, wParam,
   1.241 +         ::GetMessageTime()));
   1.242 +      LOG_KEYSTATE();
   1.243 +      if (Device::Elantech::HandleKeyMessage(aWidget, msg, wParam)) {
   1.244 +        aResult.mResult = 0;
   1.245 +        aResult.mConsumed = true;
   1.246 +        return true;
   1.247 +      }
   1.248 +      return false;
   1.249 +
   1.250 +    default:
   1.251 +      return false;
   1.252 +  }
   1.253 +}
   1.254 +
   1.255 +/* static */
   1.256 +nsresult
   1.257 +MouseScrollHandler::SynthesizeNativeMouseScrollEvent(nsWindowBase* aWidget,
   1.258 +                                                     const nsIntPoint& aPoint,
   1.259 +                                                     uint32_t aNativeMessage,
   1.260 +                                                     int32_t aDelta,
   1.261 +                                                     uint32_t aModifierFlags,
   1.262 +                                                     uint32_t aAdditionalFlags)
   1.263 +{
   1.264 +  bool useFocusedWindow =
   1.265 +    !(aAdditionalFlags & nsIDOMWindowUtils::MOUSESCROLL_PREFER_WIDGET_AT_POINT);
   1.266 +
   1.267 +  POINT pt;
   1.268 +  pt.x = aPoint.x;
   1.269 +  pt.y = aPoint.y;
   1.270 +
   1.271 +  HWND target = useFocusedWindow ? ::WindowFromPoint(pt) : ::GetFocus();
   1.272 +  NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
   1.273 +
   1.274 +  WPARAM wParam = 0;
   1.275 +  LPARAM lParam = 0;
   1.276 +  switch (aNativeMessage) {
   1.277 +    case WM_MOUSEWHEEL:
   1.278 +    case WM_MOUSEHWHEEL: {
   1.279 +      lParam = MAKELPARAM(pt.x, pt.y);
   1.280 +      WORD mod = 0;
   1.281 +      if (aModifierFlags & (nsIWidget::CTRL_L | nsIWidget::CTRL_R)) {
   1.282 +        mod |= MK_CONTROL;
   1.283 +      }
   1.284 +      if (aModifierFlags & (nsIWidget::SHIFT_L | nsIWidget::SHIFT_R)) {
   1.285 +        mod |= MK_SHIFT;
   1.286 +      }
   1.287 +      wParam = MAKEWPARAM(mod, aDelta);
   1.288 +      break;
   1.289 +    }
   1.290 +    case WM_VSCROLL:
   1.291 +    case WM_HSCROLL:
   1.292 +      lParam = (aAdditionalFlags &
   1.293 +                  nsIDOMWindowUtils::MOUSESCROLL_WIN_SCROLL_LPARAM_NOT_NULL) ?
   1.294 +        reinterpret_cast<LPARAM>(target) : 0;
   1.295 +      wParam = aDelta;
   1.296 +      break;
   1.297 +    default:
   1.298 +      return NS_ERROR_INVALID_ARG;
   1.299 +  }
   1.300 +
   1.301 +  // Ensure to make the instance.
   1.302 +  GetInstance();
   1.303 +
   1.304 +  BYTE kbdState[256];
   1.305 +  memset(kbdState, 0, sizeof(kbdState));
   1.306 +
   1.307 +  nsAutoTArray<KeyPair,10> keySequence;
   1.308 +  WinUtils::SetupKeyModifiersSequence(&keySequence, aModifierFlags);
   1.309 +
   1.310 +  for (uint32_t i = 0; i < keySequence.Length(); ++i) {
   1.311 +    uint8_t key = keySequence[i].mGeneral;
   1.312 +    uint8_t keySpecific = keySequence[i].mSpecific;
   1.313 +    kbdState[key] = 0x81; // key is down and toggled on if appropriate
   1.314 +    if (keySpecific) {
   1.315 +      kbdState[keySpecific] = 0x81;
   1.316 +    }
   1.317 +  }
   1.318 +
   1.319 +  if (!sInstance->mSynthesizingEvent) {
   1.320 +    sInstance->mSynthesizingEvent = new SynthesizingEvent();
   1.321 +  }
   1.322 +
   1.323 +  POINTS pts;
   1.324 +  pts.x = static_cast<SHORT>(pt.x);
   1.325 +  pts.y = static_cast<SHORT>(pt.y);
   1.326 +  return sInstance->mSynthesizingEvent->
   1.327 +           Synthesize(pts, target, aNativeMessage, wParam, lParam, kbdState);
   1.328 +}
   1.329 +
   1.330 +/* static */
   1.331 +bool
   1.332 +MouseScrollHandler::DispatchEvent(nsWindowBase* aWidget,
   1.333 +                                  WidgetGUIEvent& aEvent)
   1.334 +{
   1.335 +  // note, in metrofx, this will always return false for now
   1.336 +  return aWidget->DispatchScrollEvent(&aEvent);
   1.337 +}
   1.338 +
   1.339 +/* static */
   1.340 +void
   1.341 +MouseScrollHandler::InitEvent(nsWindowBase* aWidget,
   1.342 +                              WidgetGUIEvent& aEvent,
   1.343 +                              nsIntPoint* aPoint)
   1.344 +{
   1.345 +  NS_ENSURE_TRUE_VOID(aWidget);
   1.346 +  nsIntPoint point;
   1.347 +  if (aPoint) {
   1.348 +    point = *aPoint;
   1.349 +  } else {
   1.350 +    POINTS pts = GetCurrentMessagePos();
   1.351 +    POINT pt;
   1.352 +    pt.x = pts.x;
   1.353 +    pt.y = pts.y;
   1.354 +    ::ScreenToClient(aWidget->GetWindowHandle(), &pt);
   1.355 +    point.x = pt.x;
   1.356 +    point.y = pt.y;
   1.357 +  }
   1.358 +  aWidget->InitEvent(aEvent, &point);
   1.359 +}
   1.360 +
   1.361 +/* static */
   1.362 +ModifierKeyState
   1.363 +MouseScrollHandler::GetModifierKeyState(UINT aMessage)
   1.364 +{
   1.365 +  ModifierKeyState result;
   1.366 +  // Assume the Control key is down if the Elantech touchpad has sent the
   1.367 +  // mis-ordered WM_KEYDOWN/WM_MOUSEWHEEL messages.  (See the comment in
   1.368 +  // MouseScrollHandler::Device::Elantech::HandleKeyMessage().)
   1.369 +  if ((aMessage == MOZ_WM_MOUSEVWHEEL || aMessage == WM_MOUSEWHEEL) &&
   1.370 +      !result.IsControl() && Device::Elantech::IsZooming()) {
   1.371 +    result.Set(MODIFIER_CONTROL);
   1.372 +  }
   1.373 +  return result;
   1.374 +}
   1.375 +
   1.376 +POINT
   1.377 +MouseScrollHandler::ComputeMessagePos(UINT aMessage,
   1.378 +                                      WPARAM aWParam,
   1.379 +                                      LPARAM aLParam)
   1.380 +{
   1.381 +  POINT point;
   1.382 +  if (Device::SetPoint::IsGetMessagePosResponseValid(aMessage,
   1.383 +                                                     aWParam, aLParam)) {
   1.384 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.385 +      ("MouseScroll::ComputeMessagePos: Using ::GetCursorPos()"));
   1.386 +    ::GetCursorPos(&point);
   1.387 +  } else {
   1.388 +    POINTS pts = GetCurrentMessagePos();
   1.389 +    point.x = pts.x;
   1.390 +    point.y = pts.y;
   1.391 +  }
   1.392 +  return point;
   1.393 +}
   1.394 +
   1.395 +void
   1.396 +MouseScrollHandler::ProcessNativeMouseWheelMessage(nsWindowBase* aWidget,
   1.397 +                                                   UINT aMessage,
   1.398 +                                                   WPARAM aWParam,
   1.399 +                                                   LPARAM aLParam)
   1.400 +{
   1.401 +  if (SynthesizingEvent::IsSynthesizing()) {
   1.402 +    mSynthesizingEvent->NativeMessageReceived(aWidget, aMessage,
   1.403 +                                              aWParam, aLParam);
   1.404 +  }
   1.405 +
   1.406 +  POINT point = ComputeMessagePos(aMessage, aWParam, aLParam);
   1.407 +
   1.408 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.409 +    ("MouseScroll::ProcessNativeMouseWheelMessage: aWidget=%p, "
   1.410 +     "aMessage=%s, wParam=0x%08X, lParam=0x%08X, point: { x=%d, y=%d }",
   1.411 +     aWidget, aMessage == WM_MOUSEWHEEL ? "WM_MOUSEWHEEL" :
   1.412 +              aMessage == WM_MOUSEHWHEEL ? "WM_MOUSEHWHEEL" :
   1.413 +              aMessage == WM_VSCROLL ? "WM_VSCROLL" : "WM_HSCROLL",
   1.414 +     aWParam, aLParam, point.x, point.y));
   1.415 +  LOG_KEYSTATE();
   1.416 +
   1.417 +  HWND underCursorWnd = ::WindowFromPoint(point);
   1.418 +  if (!underCursorWnd) {
   1.419 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.420 +      ("MouseScroll::ProcessNativeMouseWheelMessage: "
   1.421 +       "No window is not found under the cursor"));
   1.422 +    return;
   1.423 +  }
   1.424 +
   1.425 +  if (Device::Elantech::IsPinchHackNeeded() &&
   1.426 +      Device::Elantech::IsHelperWindow(underCursorWnd)) {
   1.427 +    // The Elantech driver places a window right underneath the cursor
   1.428 +    // when sending a WM_MOUSEWHEEL event to us as part of a pinch-to-zoom
   1.429 +    // gesture.  We detect that here, and search for our window that would
   1.430 +    // be beneath the cursor if that window wasn't there.
   1.431 +    underCursorWnd = WinUtils::FindOurWindowAtPoint(point);
   1.432 +    if (!underCursorWnd) {
   1.433 +      PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.434 +        ("MouseScroll::ProcessNativeMouseWheelMessage: "
   1.435 +         "Our window is not found under the Elantech helper window"));
   1.436 +      return;
   1.437 +    }
   1.438 +  }
   1.439 +
   1.440 +  // Handle most cases first.  If the window under mouse cursor is our window
   1.441 +  // except plugin window (MozillaWindowClass), we should handle the message
   1.442 +  // on the window.
   1.443 +  if (WinUtils::IsOurProcessWindow(underCursorWnd)) {
   1.444 +    nsWindowBase* destWindow = WinUtils::GetNSWindowBasePtr(underCursorWnd);
   1.445 +    if (!destWindow) {
   1.446 +      PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.447 +        ("MouseScroll::ProcessNativeMouseWheelMessage: "
   1.448 +         "Found window under the cursor isn't managed by nsWindow..."));
   1.449 +      HWND wnd = ::GetParent(underCursorWnd);
   1.450 +      for (; wnd; wnd = ::GetParent(wnd)) {
   1.451 +        destWindow = WinUtils::GetNSWindowBasePtr(wnd);
   1.452 +        if (destWindow) {
   1.453 +          break;
   1.454 +        }
   1.455 +      }
   1.456 +      if (!wnd) {
   1.457 +        PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.458 +          ("MouseScroll::ProcessNativeMouseWheelMessage: Our window which is "
   1.459 +           "managed by nsWindow is not found under the cursor"));
   1.460 +        return;
   1.461 +      }
   1.462 +    }
   1.463 +
   1.464 +    MOZ_ASSERT(destWindow, "destWindow must not be NULL");
   1.465 +
   1.466 +    // If the found window is our plugin window, it means that the message
   1.467 +    // has been handled by the plugin but not consumed.  We should handle the
   1.468 +    // message on its parent window.  However, note that the DOM event may
   1.469 +    // cause accessing the plugin.  Therefore, we should unlock the plugin
   1.470 +    // process by using PostMessage().
   1.471 +    if (destWindow->WindowType() == eWindowType_plugin) {
   1.472 +      destWindow = destWindow->GetParentWindowBase(false);
   1.473 +      if (!destWindow) {
   1.474 +        PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.475 +          ("MouseScroll::ProcessNativeMouseWheelMessage: "
   1.476 +           "Our window which is a parent of a plugin window is not found"));
   1.477 +        return;
   1.478 +      }
   1.479 +    }
   1.480 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.481 +      ("MouseScroll::ProcessNativeMouseWheelMessage: Succeeded, "
   1.482 +       "Posting internal message to an nsWindow (%p)...",
   1.483 +       destWindow));
   1.484 +    mIsWaitingInternalMessage = true;
   1.485 +    UINT internalMessage = WinUtils::GetInternalMessage(aMessage);
   1.486 +    ::PostMessage(destWindow->GetWindowHandle(), internalMessage,
   1.487 +                  aWParam, aLParam);
   1.488 +    return;
   1.489 +  }
   1.490 +
   1.491 +  // If the window under cursor is not in our process, it means:
   1.492 +  // 1. The window may be a plugin window (GeckoPluginWindow or its descendant).
   1.493 +  // 2. The window may be another application's window.
   1.494 +  HWND pluginWnd = WinUtils::FindOurProcessWindow(underCursorWnd);
   1.495 +  if (!pluginWnd) {
   1.496 +    // If there is no plugin window in ancestors of the window under cursor,
   1.497 +    // the window is for another applications (case 2).
   1.498 +    // We don't need to handle this message.
   1.499 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.500 +      ("MouseScroll::ProcessNativeMouseWheelMessage: "
   1.501 +       "Our window is not found under the cursor"));
   1.502 +    return;
   1.503 +  }
   1.504 +
   1.505 +  // If we're a plugin window (MozillaWindowClass) and cursor in this window,
   1.506 +  // the message shouldn't go to plugin's wndproc again.  So, we should handle
   1.507 +  // it on parent window.  However, note that the DOM event may cause accessing
   1.508 +  // the plugin.  Therefore, we should unlock the plugin process by using
   1.509 +  // PostMessage().
   1.510 +  if (aWidget->WindowType() == eWindowType_plugin &&
   1.511 +      aWidget->GetWindowHandle() == pluginWnd) {
   1.512 +    nsWindowBase* destWindow = aWidget->GetParentWindowBase(false);
   1.513 +    if (!destWindow) {
   1.514 +      PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.515 +        ("MouseScroll::ProcessNativeMouseWheelMessage: Our normal window which "
   1.516 +         "is a parent of this plugin window is not found"));
   1.517 +      return;
   1.518 +    }
   1.519 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.520 +      ("MouseScroll::ProcessNativeMouseWheelMessage: Succeeded, "
   1.521 +       "Posting internal message to an nsWindow (%p) which is parent of this "
   1.522 +       "plugin window...",
   1.523 +       destWindow));
   1.524 +    mIsWaitingInternalMessage = true;
   1.525 +    UINT internalMessage = WinUtils::GetInternalMessage(aMessage);
   1.526 +    ::PostMessage(destWindow->GetWindowHandle(), internalMessage,
   1.527 +                  aWParam, aLParam);
   1.528 +    return;
   1.529 +  }
   1.530 +
   1.531 +  // If the window is a part of plugin, we should post the message to it.
   1.532 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.533 +    ("MouseScroll::ProcessNativeMouseWheelMessage: Succeeded, "
   1.534 +     "Redirecting the message to a window which is a plugin child window"));
   1.535 +  ::PostMessage(underCursorWnd, aMessage, aWParam, aLParam);
   1.536 +}
   1.537 +
   1.538 +bool
   1.539 +MouseScrollHandler::ProcessNativeScrollMessage(nsWindowBase* aWidget,
   1.540 +                                               UINT aMessage,
   1.541 +                                               WPARAM aWParam,
   1.542 +                                               LPARAM aLParam)
   1.543 +{
   1.544 +  if (aLParam || mUserPrefs.IsScrollMessageHandledAsWheelMessage()) {
   1.545 +    // Scroll message generated by Thinkpad Trackpoint Driver or similar
   1.546 +    // Treat as a mousewheel message and scroll appropriately
   1.547 +    ProcessNativeMouseWheelMessage(aWidget, aMessage, aWParam, aLParam);
   1.548 +    // Always consume the scroll message if we try to emulate mouse wheel
   1.549 +    // action.
   1.550 +    return true;
   1.551 +  }
   1.552 +
   1.553 +  if (SynthesizingEvent::IsSynthesizing()) {
   1.554 +    mSynthesizingEvent->NativeMessageReceived(aWidget, aMessage,
   1.555 +                                              aWParam, aLParam);
   1.556 +  }
   1.557 +
   1.558 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.559 +    ("MouseScroll::ProcessNativeScrollMessage: aWidget=%p, "
   1.560 +     "aMessage=%s, wParam=0x%08X, lParam=0x%08X",
   1.561 +     aWidget, aMessage == WM_VSCROLL ? "WM_VSCROLL" : "WM_HSCROLL",
   1.562 +     aWParam, aLParam));
   1.563 +
   1.564 +  // Scroll message generated by external application
   1.565 +  WidgetContentCommandEvent commandEvent(true, NS_CONTENT_COMMAND_SCROLL,
   1.566 +                                         aWidget);
   1.567 +
   1.568 +  commandEvent.mScroll.mIsHorizontal = (aMessage == WM_HSCROLL);
   1.569 +
   1.570 +  switch (LOWORD(aWParam)) {
   1.571 +    case SB_LINEUP:   // SB_LINELEFT
   1.572 +      commandEvent.mScroll.mUnit =
   1.573 +        WidgetContentCommandEvent::eCmdScrollUnit_Line;
   1.574 +      commandEvent.mScroll.mAmount = -1;
   1.575 +      break;
   1.576 +    case SB_LINEDOWN: // SB_LINERIGHT
   1.577 +      commandEvent.mScroll.mUnit =
   1.578 +        WidgetContentCommandEvent::eCmdScrollUnit_Line;
   1.579 +      commandEvent.mScroll.mAmount = 1;
   1.580 +      break;
   1.581 +    case SB_PAGEUP:   // SB_PAGELEFT
   1.582 +      commandEvent.mScroll.mUnit =
   1.583 +        WidgetContentCommandEvent::eCmdScrollUnit_Page;
   1.584 +      commandEvent.mScroll.mAmount = -1;
   1.585 +      break;
   1.586 +    case SB_PAGEDOWN: // SB_PAGERIGHT
   1.587 +      commandEvent.mScroll.mUnit =
   1.588 +        WidgetContentCommandEvent::eCmdScrollUnit_Page;
   1.589 +      commandEvent.mScroll.mAmount = 1;
   1.590 +      break;
   1.591 +    case SB_TOP:      // SB_LEFT
   1.592 +      commandEvent.mScroll.mUnit =
   1.593 +        WidgetContentCommandEvent::eCmdScrollUnit_Whole;
   1.594 +      commandEvent.mScroll.mAmount = -1;
   1.595 +      break;
   1.596 +    case SB_BOTTOM:   // SB_RIGHT
   1.597 +      commandEvent.mScroll.mUnit =
   1.598 +        WidgetContentCommandEvent::eCmdScrollUnit_Whole;
   1.599 +      commandEvent.mScroll.mAmount = 1;
   1.600 +      break;
   1.601 +    default:
   1.602 +      return false;
   1.603 +  }
   1.604 +  // XXX If this is a plugin window, we should dispatch the event from
   1.605 +  //     parent window.
   1.606 +  DispatchEvent(aWidget, commandEvent);
   1.607 +  return true;
   1.608 +}
   1.609 +
   1.610 +void
   1.611 +MouseScrollHandler::HandleMouseWheelMessage(nsWindowBase* aWidget,
   1.612 +                                            UINT aMessage,
   1.613 +                                            WPARAM aWParam,
   1.614 +                                            LPARAM aLParam)
   1.615 +{
   1.616 +  NS_ABORT_IF_FALSE(
   1.617 +    (aMessage == MOZ_WM_MOUSEVWHEEL || aMessage == MOZ_WM_MOUSEHWHEEL),
   1.618 +    "HandleMouseWheelMessage must be called with "
   1.619 +    "MOZ_WM_MOUSEVWHEEL or MOZ_WM_MOUSEHWHEEL");
   1.620 +
   1.621 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.622 +    ("MouseScroll::HandleMouseWheelMessage: aWidget=%p, "
   1.623 +     "aMessage=MOZ_WM_MOUSE%sWHEEL, aWParam=0x%08X, aLParam=0x%08X",
   1.624 +     aWidget, aMessage == MOZ_WM_MOUSEVWHEEL ? "V" : "H",
   1.625 +     aWParam, aLParam));
   1.626 +
   1.627 +  mIsWaitingInternalMessage = false;
   1.628 +
   1.629 +  EventInfo eventInfo(aWidget, WinUtils::GetNativeMessage(aMessage),
   1.630 +                      aWParam, aLParam);
   1.631 +  if (!eventInfo.CanDispatchWheelEvent()) {
   1.632 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.633 +      ("MouseScroll::HandleMouseWheelMessage: Cannot dispatch the events"));
   1.634 +    mLastEventInfo.ResetTransaction();
   1.635 +    return;
   1.636 +  }
   1.637 +
   1.638 +  // Discard the remaining delta if current wheel message and last one are
   1.639 +  // received by different window or to scroll different direction or
   1.640 +  // different unit scroll.  Furthermore, if the last event was too old.
   1.641 +  if (!mLastEventInfo.CanContinueTransaction(eventInfo)) {
   1.642 +    mLastEventInfo.ResetTransaction();
   1.643 +  }
   1.644 +
   1.645 +  mLastEventInfo.RecordEvent(eventInfo);
   1.646 +
   1.647 +  ModifierKeyState modKeyState = GetModifierKeyState(aMessage);
   1.648 +
   1.649 +  // Grab the widget, it might be destroyed by a DOM event handler.
   1.650 +  nsRefPtr<nsWindowBase> kungFuDethGrip(aWidget);
   1.651 +
   1.652 +  WidgetWheelEvent wheelEvent(true, NS_WHEEL_WHEEL, aWidget);
   1.653 +  if (mLastEventInfo.InitWheelEvent(aWidget, wheelEvent, modKeyState)) {
   1.654 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.655 +      ("MouseScroll::HandleMouseWheelMessage: dispatching "
   1.656 +       "NS_WHEEL_WHEEL event"));
   1.657 +    DispatchEvent(aWidget, wheelEvent);
   1.658 +    if (aWidget->Destroyed()) {
   1.659 +      PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.660 +        ("MouseScroll::HandleMouseWheelMessage: The window was destroyed "
   1.661 +         "by NS_WHEEL_WHEEL event"));
   1.662 +      mLastEventInfo.ResetTransaction();
   1.663 +      return;
   1.664 +    }
   1.665 +  }
   1.666 +#ifdef PR_LOGGING
   1.667 +  else {
   1.668 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.669 +      ("MouseScroll::HandleMouseWheelMessage: NS_WHEEL_WHEEL event is not "
   1.670 +       "dispatched"));
   1.671 +  }
   1.672 +#endif
   1.673 +}
   1.674 +
   1.675 +void
   1.676 +MouseScrollHandler::HandleScrollMessageAsMouseWheelMessage(nsWindowBase* aWidget,
   1.677 +                                                           UINT aMessage,
   1.678 +                                                           WPARAM aWParam,
   1.679 +                                                           LPARAM aLParam)
   1.680 +{
   1.681 +  NS_ABORT_IF_FALSE(
   1.682 +    (aMessage == MOZ_WM_VSCROLL || aMessage == MOZ_WM_HSCROLL),
   1.683 +    "HandleScrollMessageAsMouseWheelMessage must be called with "
   1.684 +    "MOZ_WM_VSCROLL or MOZ_WM_HSCROLL");
   1.685 +
   1.686 +  mIsWaitingInternalMessage = false;
   1.687 +
   1.688 +  ModifierKeyState modKeyState = GetModifierKeyState(aMessage);
   1.689 +
   1.690 +  WidgetWheelEvent wheelEvent(true, NS_WHEEL_WHEEL, aWidget);
   1.691 +  double& delta =
   1.692 +   (aMessage == MOZ_WM_VSCROLL) ? wheelEvent.deltaY : wheelEvent.deltaX;
   1.693 +  int32_t& lineOrPageDelta =
   1.694 +   (aMessage == MOZ_WM_VSCROLL) ? wheelEvent.lineOrPageDeltaY :
   1.695 +                                  wheelEvent.lineOrPageDeltaX;
   1.696 +
   1.697 +  delta = 1.0;
   1.698 +  lineOrPageDelta = 1;
   1.699 +
   1.700 +  switch (LOWORD(aWParam)) {
   1.701 +    case SB_PAGEUP:
   1.702 +      delta = -1.0;
   1.703 +      lineOrPageDelta = -1;
   1.704 +    case SB_PAGEDOWN:
   1.705 +      wheelEvent.deltaMode = nsIDOMWheelEvent::DOM_DELTA_PAGE;
   1.706 +      break;
   1.707 +
   1.708 +    case SB_LINEUP:
   1.709 +      delta = -1.0;
   1.710 +      lineOrPageDelta = -1;
   1.711 +    case SB_LINEDOWN:
   1.712 +      wheelEvent.deltaMode = nsIDOMWheelEvent::DOM_DELTA_LINE;
   1.713 +      break;
   1.714 +
   1.715 +    default:
   1.716 +      return;
   1.717 +  }
   1.718 +  modKeyState.InitInputEvent(wheelEvent);
   1.719 +  // XXX Current mouse position may not be same as when the original message
   1.720 +  //     is received.  We need to know the actual mouse cursor position when
   1.721 +  //     the original message was received.
   1.722 +  InitEvent(aWidget, wheelEvent);
   1.723 +
   1.724 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.725 +    ("MouseScroll::HandleScrollMessageAsMouseWheelMessage: aWidget=%p, "
   1.726 +     "aMessage=MOZ_WM_%sSCROLL, aWParam=0x%08X, aLParam=0x%08X, "
   1.727 +     "wheelEvent { refPoint: { x: %d, y: %d }, deltaX: %f, deltaY: %f, "
   1.728 +     "lineOrPageDeltaX: %d, lineOrPageDeltaY: %d, "
   1.729 +     "isShift: %s, isControl: %s, isAlt: %s, isMeta: %s }",
   1.730 +     aWidget, (aMessage == MOZ_WM_VSCROLL) ? "V" : "H", aWParam, aLParam,
   1.731 +     wheelEvent.refPoint.x, wheelEvent.refPoint.y,
   1.732 +     wheelEvent.deltaX, wheelEvent.deltaY,
   1.733 +     wheelEvent.lineOrPageDeltaX, wheelEvent.lineOrPageDeltaY,
   1.734 +     GetBoolName(wheelEvent.IsShift()),
   1.735 +     GetBoolName(wheelEvent.IsControl()),
   1.736 +     GetBoolName(wheelEvent.IsAlt()),
   1.737 +     GetBoolName(wheelEvent.IsMeta())));
   1.738 +
   1.739 +  DispatchEvent(aWidget, wheelEvent);
   1.740 +}
   1.741 +
   1.742 +/******************************************************************************
   1.743 + *
   1.744 + * EventInfo
   1.745 + *
   1.746 + ******************************************************************************/
   1.747 +
   1.748 +MouseScrollHandler::EventInfo::EventInfo(nsWindowBase* aWidget,
   1.749 +                                         UINT aMessage,
   1.750 +                                         WPARAM aWParam, LPARAM aLParam)
   1.751 +{
   1.752 +  NS_ABORT_IF_FALSE(aMessage == WM_MOUSEWHEEL || aMessage == WM_MOUSEHWHEEL,
   1.753 +    "EventInfo must be initialized with WM_MOUSEWHEEL or WM_MOUSEHWHEEL");
   1.754 +
   1.755 +  MouseScrollHandler::GetInstance()->mSystemSettings.Init();
   1.756 +
   1.757 +  mIsVertical = (aMessage == WM_MOUSEWHEEL);
   1.758 +  mIsPage = MouseScrollHandler::sInstance->
   1.759 +              mSystemSettings.IsPageScroll(mIsVertical);
   1.760 +  mDelta = (short)HIWORD(aWParam);
   1.761 +  mWnd = aWidget->GetWindowHandle();
   1.762 +  mTimeStamp = TimeStamp::Now();
   1.763 +}
   1.764 +
   1.765 +bool
   1.766 +MouseScrollHandler::EventInfo::CanDispatchWheelEvent() const
   1.767 +{
   1.768 +  if (!GetScrollAmount()) {
   1.769 +    // XXX I think that we should dispatch mouse wheel events even if the
   1.770 +    // operation will not scroll because the wheel operation really happened
   1.771 +    // and web application may want to handle the event for non-scroll action.
   1.772 +    return false;
   1.773 +  }
   1.774 +
   1.775 +  return (mDelta != 0);
   1.776 +}
   1.777 +
   1.778 +int32_t
   1.779 +MouseScrollHandler::EventInfo::GetScrollAmount() const
   1.780 +{
   1.781 +  if (mIsPage) {
   1.782 +    return 1;
   1.783 +  }
   1.784 +  return MouseScrollHandler::sInstance->
   1.785 +           mSystemSettings.GetScrollAmount(mIsVertical);
   1.786 +}
   1.787 +
   1.788 +/******************************************************************************
   1.789 + *
   1.790 + * LastEventInfo
   1.791 + *
   1.792 + ******************************************************************************/
   1.793 +
   1.794 +bool
   1.795 +MouseScrollHandler::LastEventInfo::CanContinueTransaction(
   1.796 +                                     const EventInfo& aNewEvent)
   1.797 +{
   1.798 +  int32_t timeout = MouseScrollHandler::sInstance->
   1.799 +                      mUserPrefs.GetMouseScrollTransactionTimeout();
   1.800 +  return !mWnd ||
   1.801 +           (mWnd == aNewEvent.GetWindowHandle() &&
   1.802 +            IsPositive() == aNewEvent.IsPositive() &&
   1.803 +            mIsVertical == aNewEvent.IsVertical() &&
   1.804 +            mIsPage == aNewEvent.IsPage() &&
   1.805 +            (timeout < 0 ||
   1.806 +             TimeStamp::Now() - mTimeStamp <=
   1.807 +               TimeDuration::FromMilliseconds(timeout)));
   1.808 +}
   1.809 +
   1.810 +void
   1.811 +MouseScrollHandler::LastEventInfo::ResetTransaction()
   1.812 +{
   1.813 +  if (!mWnd) {
   1.814 +    return;
   1.815 +  }
   1.816 +
   1.817 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.818 +    ("MouseScroll::LastEventInfo::ResetTransaction()"));
   1.819 +
   1.820 +  mWnd = nullptr;
   1.821 +  mAccumulatedDelta = 0;
   1.822 +}
   1.823 +
   1.824 +void
   1.825 +MouseScrollHandler::LastEventInfo::RecordEvent(const EventInfo& aEvent)
   1.826 +{
   1.827 +  mWnd = aEvent.GetWindowHandle();
   1.828 +  mDelta = aEvent.GetNativeDelta();
   1.829 +  mIsVertical = aEvent.IsVertical();
   1.830 +  mIsPage = aEvent.IsPage();
   1.831 +  mTimeStamp = TimeStamp::Now();
   1.832 +}
   1.833 +
   1.834 +/* static */
   1.835 +int32_t
   1.836 +MouseScrollHandler::LastEventInfo::RoundDelta(double aDelta)
   1.837 +{
   1.838 +  return (aDelta >= 0) ? (int32_t)floor(aDelta) : (int32_t)ceil(aDelta);
   1.839 +}
   1.840 +
   1.841 +bool
   1.842 +MouseScrollHandler::LastEventInfo::InitWheelEvent(
   1.843 +                                     nsWindowBase* aWidget,
   1.844 +                                     WidgetWheelEvent& aWheelEvent,
   1.845 +                                     const ModifierKeyState& aModKeyState)
   1.846 +{
   1.847 +  MOZ_ASSERT(aWheelEvent.message == NS_WHEEL_WHEEL);
   1.848 +
   1.849 +  // XXX Why don't we use lParam value? We should use lParam value because
   1.850 +  //     our internal message is always posted by original message handler.
   1.851 +  //     So, GetMessagePos() may return different cursor position.
   1.852 +  InitEvent(aWidget, aWheelEvent);
   1.853 +
   1.854 +  aModKeyState.InitInputEvent(aWheelEvent);
   1.855 +
   1.856 +  // Our positive delta value means to bottom or right.
   1.857 +  // But positive native delta value means to top or right.
   1.858 +  // Use orienter for computing our delta value with native delta value.
   1.859 +  int32_t orienter = mIsVertical ? -1 : 1;
   1.860 +
   1.861 +  aWheelEvent.deltaMode = mIsPage ? nsIDOMWheelEvent::DOM_DELTA_PAGE :
   1.862 +                                    nsIDOMWheelEvent::DOM_DELTA_LINE;
   1.863 +
   1.864 +  double& delta = mIsVertical ? aWheelEvent.deltaY : aWheelEvent.deltaX;
   1.865 +  int32_t& lineOrPageDelta = mIsVertical ? aWheelEvent.lineOrPageDeltaY :
   1.866 +                                           aWheelEvent.lineOrPageDeltaX;
   1.867 +
   1.868 +  double nativeDeltaPerUnit =
   1.869 +    mIsPage ? static_cast<double>(WHEEL_DELTA) :
   1.870 +              static_cast<double>(WHEEL_DELTA) / GetScrollAmount();
   1.871 +
   1.872 +  delta = static_cast<double>(mDelta) * orienter / nativeDeltaPerUnit;
   1.873 +  mAccumulatedDelta += mDelta;
   1.874 +  lineOrPageDelta =
   1.875 +    mAccumulatedDelta * orienter / RoundDelta(nativeDeltaPerUnit);
   1.876 +  mAccumulatedDelta -=
   1.877 +    lineOrPageDelta * orienter * RoundDelta(nativeDeltaPerUnit);
   1.878 +
   1.879 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.880 +    ("MouseScroll::LastEventInfo::InitWheelEvent: aWidget=%p, "
   1.881 +     "aWheelEvent { refPoint: { x: %d, y: %d }, deltaX: %f, deltaY: %f, "
   1.882 +     "lineOrPageDeltaX: %d, lineOrPageDeltaY: %d, "
   1.883 +     "isShift: %s, isControl: %s, isAlt: %s, isMeta: %s }, "
   1.884 +     "mAccumulatedDelta: %d",
   1.885 +     aWidget, aWheelEvent.refPoint.x, aWheelEvent.refPoint.y,
   1.886 +     aWheelEvent.deltaX, aWheelEvent.deltaY,
   1.887 +     aWheelEvent.lineOrPageDeltaX, aWheelEvent.lineOrPageDeltaY,
   1.888 +     GetBoolName(aWheelEvent.IsShift()),
   1.889 +     GetBoolName(aWheelEvent.IsControl()),
   1.890 +     GetBoolName(aWheelEvent.IsAlt()),
   1.891 +     GetBoolName(aWheelEvent.IsMeta()), mAccumulatedDelta));
   1.892 +
   1.893 +  return (delta != 0);
   1.894 +}
   1.895 +
   1.896 +/******************************************************************************
   1.897 + *
   1.898 + * SystemSettings
   1.899 + *
   1.900 + ******************************************************************************/
   1.901 +
   1.902 +void
   1.903 +MouseScrollHandler::SystemSettings::Init()
   1.904 +{
   1.905 +  if (mInitialized) {
   1.906 +    return;
   1.907 +  }
   1.908 +
   1.909 +  mInitialized = true;
   1.910 +
   1.911 +  MouseScrollHandler::UserPrefs& userPrefs =
   1.912 +    MouseScrollHandler::sInstance->mUserPrefs;
   1.913 +
   1.914 +  mScrollLines = userPrefs.GetOverriddenVerticalScrollAmout();
   1.915 +  if (mScrollLines >= 0) {
   1.916 +    // overridden by the pref.
   1.917 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.918 +      ("MouseScroll::SystemSettings::Init(): mScrollLines is overridden by "
   1.919 +       "the pref: %d",
   1.920 +       mScrollLines));
   1.921 +  } else if (!::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
   1.922 +                                     &mScrollLines, 0)) {
   1.923 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.924 +      ("MouseScroll::SystemSettings::Init(): ::SystemParametersInfo("
   1.925 +         "SPI_GETWHEELSCROLLLINES) failed"));
   1.926 +    mScrollLines = 3;
   1.927 +  }
   1.928 +
   1.929 +  if (mScrollLines > WHEEL_DELTA) {
   1.930 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.931 +      ("MouseScroll::SystemSettings::Init(): the result of "
   1.932 +         "::SystemParametersInfo(SPI_GETWHEELSCROLLLINES) is too large: %d",
   1.933 +       mScrollLines));
   1.934 +    // sScrollLines usually equals 3 or 0 (for no scrolling)
   1.935 +    // However, if sScrollLines > WHEEL_DELTA, we assume that
   1.936 +    // the mouse driver wants a page scroll.  The docs state that
   1.937 +    // sScrollLines should explicitly equal WHEEL_PAGESCROLL, but
   1.938 +    // since some mouse drivers use an arbitrary large number instead,
   1.939 +    // we have to handle that as well.
   1.940 +    mScrollLines = WHEEL_PAGESCROLL;
   1.941 +  }
   1.942 +
   1.943 +  mScrollChars = userPrefs.GetOverriddenHorizontalScrollAmout();
   1.944 +  if (mScrollChars >= 0) {
   1.945 +    // overridden by the pref.
   1.946 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.947 +      ("MouseScroll::SystemSettings::Init(): mScrollChars is overridden by "
   1.948 +       "the pref: %d",
   1.949 +       mScrollChars));
   1.950 +  } else if (!::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0,
   1.951 +                                     &mScrollChars, 0)) {
   1.952 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.953 +      ("MouseScroll::SystemSettings::Init(): ::SystemParametersInfo("
   1.954 +         "SPI_GETWHEELSCROLLCHARS) failed, %s",
   1.955 +       IsVistaOrLater() ?
   1.956 +         "this is unexpected on Vista or later" :
   1.957 +         "but on XP or earlier, this is not a problem"));
   1.958 +    mScrollChars = 1;
   1.959 +  }
   1.960 +
   1.961 +  if (mScrollChars > WHEEL_DELTA) {
   1.962 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.963 +      ("MouseScroll::SystemSettings::Init(): the result of "
   1.964 +         "::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS) is too large: %d",
   1.965 +       mScrollChars));
   1.966 +    // See the comments for the case mScrollLines > WHEEL_DELTA.
   1.967 +    mScrollChars = WHEEL_PAGESCROLL;
   1.968 +  }
   1.969 +
   1.970 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.971 +    ("MouseScroll::SystemSettings::Init(): initialized, "
   1.972 +       "mScrollLines=%d, mScrollChars=%d",
   1.973 +     mScrollLines, mScrollChars));
   1.974 +}
   1.975 +
   1.976 +void
   1.977 +MouseScrollHandler::SystemSettings::MarkDirty()
   1.978 +{
   1.979 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
   1.980 +    ("MouseScrollHandler::SystemSettings::MarkDirty(): "
   1.981 +       "Marking SystemSettings dirty"));
   1.982 +  mInitialized = false;
   1.983 +  // When system settings are changed, we should reset current transaction.
   1.984 +  MOZ_ASSERT(sInstance,
   1.985 +    "Must not be called at initializing MouseScrollHandler");
   1.986 +  MouseScrollHandler::sInstance->mLastEventInfo.ResetTransaction();
   1.987 +}
   1.988 +
   1.989 +/******************************************************************************
   1.990 + *
   1.991 + * UserPrefs
   1.992 + *
   1.993 + ******************************************************************************/
   1.994 +
   1.995 +MouseScrollHandler::UserPrefs::UserPrefs() :
   1.996 +  mInitialized(false)
   1.997 +{
   1.998 +  // We need to reset mouse wheel transaction when all of mousewheel related
   1.999 +  // prefs are changed.
  1.1000 +  DebugOnly<nsresult> rv =
  1.1001 +    Preferences::RegisterCallback(OnChange, "mousewheel.", this);
  1.1002 +  MOZ_ASSERT(NS_SUCCEEDED(rv),
  1.1003 +    "Failed to register callback for mousewheel.");
  1.1004 +}
  1.1005 +
  1.1006 +MouseScrollHandler::UserPrefs::~UserPrefs()
  1.1007 +{
  1.1008 +  DebugOnly<nsresult> rv =
  1.1009 +    Preferences::UnregisterCallback(OnChange, "mousewheel.", this);
  1.1010 +  MOZ_ASSERT(NS_SUCCEEDED(rv),
  1.1011 +    "Failed to unregister callback for mousewheel.");
  1.1012 +}
  1.1013 +
  1.1014 +void
  1.1015 +MouseScrollHandler::UserPrefs::Init()
  1.1016 +{
  1.1017 +  if (mInitialized) {
  1.1018 +    return;
  1.1019 +  }
  1.1020 +
  1.1021 +  mInitialized = true;
  1.1022 +
  1.1023 +  mScrollMessageHandledAsWheelMessage =
  1.1024 +    Preferences::GetBool("mousewheel.emulate_at_wm_scroll", false);
  1.1025 +  mOverriddenVerticalScrollAmount =
  1.1026 +    Preferences::GetInt("mousewheel.windows.vertical_amount_override", -1);
  1.1027 +  mOverriddenHorizontalScrollAmount =
  1.1028 +    Preferences::GetInt("mousewheel.windows.horizontal_amount_override", -1);
  1.1029 +  mMouseScrollTransactionTimeout =
  1.1030 +    Preferences::GetInt("mousewheel.windows.transaction.timeout",
  1.1031 +                        DEFAULT_TIMEOUT_DURATION);
  1.1032 +
  1.1033 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1034 +    ("MouseScroll::UserPrefs::Init(): initialized, "
  1.1035 +       "mScrollMessageHandledAsWheelMessage=%s, "
  1.1036 +       "mOverriddenVerticalScrollAmount=%d, "
  1.1037 +       "mOverriddenHorizontalScrollAmount=%d, "
  1.1038 +       "mMouseScrollTransactionTimeout=%d",
  1.1039 +     GetBoolName(mScrollMessageHandledAsWheelMessage),
  1.1040 +     mOverriddenVerticalScrollAmount, mOverriddenHorizontalScrollAmount,
  1.1041 +     mMouseScrollTransactionTimeout));
  1.1042 +}
  1.1043 +
  1.1044 +void
  1.1045 +MouseScrollHandler::UserPrefs::MarkDirty()
  1.1046 +{
  1.1047 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1048 +    ("MouseScrollHandler::UserPrefs::MarkDirty(): Marking UserPrefs dirty"));
  1.1049 +  mInitialized = false;
  1.1050 +  // Some prefs might override system settings, so, we should mark them dirty.
  1.1051 +  MouseScrollHandler::sInstance->mSystemSettings.MarkDirty();
  1.1052 +  // When user prefs for mousewheel are changed, we should reset current
  1.1053 +  // transaction.
  1.1054 +  MOZ_ASSERT(sInstance,
  1.1055 +    "Must not be called at initializing MouseScrollHandler");
  1.1056 +  MouseScrollHandler::sInstance->mLastEventInfo.ResetTransaction();
  1.1057 +}
  1.1058 +
  1.1059 +/******************************************************************************
  1.1060 + *
  1.1061 + * Device
  1.1062 + *
  1.1063 + ******************************************************************************/
  1.1064 +
  1.1065 +/* static */
  1.1066 +bool
  1.1067 +MouseScrollHandler::Device::GetWorkaroundPref(const char* aPrefName,
  1.1068 +                                              bool aValueIfAutomatic)
  1.1069 +{
  1.1070 +  if (!aPrefName) {
  1.1071 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1072 +      ("MouseScroll::Device::GetWorkaroundPref(): Failed, aPrefName is NULL"));
  1.1073 +    return aValueIfAutomatic;
  1.1074 +  }
  1.1075 +
  1.1076 +  int32_t lHackValue = 0;
  1.1077 +  if (NS_FAILED(Preferences::GetInt(aPrefName, &lHackValue))) {
  1.1078 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1079 +      ("MouseScroll::Device::GetWorkaroundPref(): Preferences::GetInt() failed,"
  1.1080 +       " aPrefName=\"%s\", aValueIfAutomatic=%s",
  1.1081 +       aPrefName, GetBoolName(aValueIfAutomatic)));
  1.1082 +    return aValueIfAutomatic;
  1.1083 +  }
  1.1084 +
  1.1085 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1086 +    ("MouseScroll::Device::GetWorkaroundPref(): Succeeded, "
  1.1087 +     "aPrefName=\"%s\", aValueIfAutomatic=%s, lHackValue=%d",
  1.1088 +     aPrefName, GetBoolName(aValueIfAutomatic), lHackValue));
  1.1089 +
  1.1090 +  switch (lHackValue) {
  1.1091 +    case 0: // disabled
  1.1092 +      return false;
  1.1093 +    case 1: // enabled
  1.1094 +      return true;
  1.1095 +    default: // -1: autodetect
  1.1096 +      return aValueIfAutomatic;
  1.1097 +  }
  1.1098 +}
  1.1099 +
  1.1100 +/* static */
  1.1101 +void
  1.1102 +MouseScrollHandler::Device::Init()
  1.1103 +{
  1.1104 +  // Not supported in metro mode.
  1.1105 +  if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Metro) {
  1.1106 +    return;
  1.1107 +  }
  1.1108 +
  1.1109 +  sFakeScrollableWindowNeeded =
  1.1110 +    GetWorkaroundPref("ui.trackpoint_hack.enabled",
  1.1111 +                      (TrackPoint::IsDriverInstalled() ||
  1.1112 +                       UltraNav::IsObsoleteDriverInstalled()));
  1.1113 +
  1.1114 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1115 +    ("MouseScroll::Device::Init(): sFakeScrollableWindowNeeded=%s",
  1.1116 +     GetBoolName(sFakeScrollableWindowNeeded)));
  1.1117 +
  1.1118 +  Elantech::Init();
  1.1119 +}
  1.1120 +
  1.1121 +/******************************************************************************
  1.1122 + *
  1.1123 + * Device::Elantech
  1.1124 + *
  1.1125 + ******************************************************************************/
  1.1126 +
  1.1127 +/* static */
  1.1128 +void
  1.1129 +MouseScrollHandler::Device::Elantech::Init()
  1.1130 +{
  1.1131 +  int32_t version = GetDriverMajorVersion();
  1.1132 +  bool needsHack =
  1.1133 +    Device::GetWorkaroundPref("ui.elantech_gesture_hacks.enabled",
  1.1134 +                              version != 0);
  1.1135 +  sUseSwipeHack = needsHack && version <= 7;
  1.1136 +  sUsePinchHack = needsHack && version <= 8;
  1.1137 +
  1.1138 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1139 +    ("MouseScroll::Device::Elantech::Init(): version=%d, sUseSwipeHack=%s, "
  1.1140 +     "sUsePinchHack=%s",
  1.1141 +     version, GetBoolName(sUseSwipeHack), GetBoolName(sUsePinchHack)));
  1.1142 +}
  1.1143 +
  1.1144 +/* static */
  1.1145 +int32_t
  1.1146 +MouseScrollHandler::Device::Elantech::GetDriverMajorVersion()
  1.1147 +{
  1.1148 +  wchar_t buf[40];
  1.1149 +  // The driver version is found in one of these two registry keys.
  1.1150 +  bool foundKey =
  1.1151 +    WinUtils::GetRegistryKey(HKEY_CURRENT_USER,
  1.1152 +                             L"Software\\Elantech\\MainOption",
  1.1153 +                             L"DriverVersion",
  1.1154 +                             buf, sizeof buf);
  1.1155 +  if (!foundKey) {
  1.1156 +    foundKey =
  1.1157 +      WinUtils::GetRegistryKey(HKEY_CURRENT_USER,
  1.1158 +                               L"Software\\Elantech",
  1.1159 +                               L"DriverVersion",
  1.1160 +                               buf, sizeof buf);
  1.1161 +  }
  1.1162 +
  1.1163 +  if (!foundKey) {
  1.1164 +    return 0;
  1.1165 +  }
  1.1166 +
  1.1167 +  // Assume that the major version number can be found just after a space
  1.1168 +  // or at the start of the string.
  1.1169 +  for (wchar_t* p = buf; *p; p++) {
  1.1170 +    if (*p >= L'0' && *p <= L'9' && (p == buf || *(p - 1) == L' ')) {
  1.1171 +      return wcstol(p, nullptr, 10);
  1.1172 +    }
  1.1173 +  }
  1.1174 +
  1.1175 +  return 0;
  1.1176 +}
  1.1177 +
  1.1178 +/* static */
  1.1179 +bool
  1.1180 +MouseScrollHandler::Device::Elantech::IsHelperWindow(HWND aWnd)
  1.1181 +{
  1.1182 +  // The helper window cannot be distinguished based on its window class, so we
  1.1183 +  // need to check if it is owned by the helper process, ETDCtrl.exe.
  1.1184 +
  1.1185 +  const wchar_t* filenameSuffix = L"\\etdctrl.exe";
  1.1186 +  const int filenameSuffixLength = 12;
  1.1187 +
  1.1188 +  DWORD pid;
  1.1189 +  ::GetWindowThreadProcessId(aWnd, &pid);
  1.1190 +
  1.1191 +  HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
  1.1192 +  if (!hProcess) {
  1.1193 +    return false;
  1.1194 +  }
  1.1195 +
  1.1196 +  bool result = false;
  1.1197 +  wchar_t path[256] = {L'\0'};
  1.1198 +  if (::GetProcessImageFileNameW(hProcess, path, ArrayLength(path))) {
  1.1199 +    int pathLength = lstrlenW(path);
  1.1200 +    if (pathLength >= filenameSuffixLength) {
  1.1201 +      if (lstrcmpiW(path + pathLength - filenameSuffixLength,
  1.1202 +                    filenameSuffix) == 0) {
  1.1203 +        result = true;
  1.1204 +      }
  1.1205 +    }
  1.1206 +  }
  1.1207 +  ::CloseHandle(hProcess);
  1.1208 +
  1.1209 +  return result;
  1.1210 +}
  1.1211 +
  1.1212 +/* static */
  1.1213 +bool
  1.1214 +MouseScrollHandler::Device::Elantech::HandleKeyMessage(nsWindowBase* aWidget,
  1.1215 +                                                       UINT aMsg,
  1.1216 +                                                       WPARAM aWParam)
  1.1217 +{
  1.1218 +  // The Elantech touchpad driver understands three-finger swipe left and
  1.1219 +  // right gestures, and translates them into Page Up and Page Down key
  1.1220 +  // events for most applications.  For Firefox 3.6, it instead sends
  1.1221 +  // Alt+Left and Alt+Right to trigger browser back/forward actions.  As
  1.1222 +  // with the Thinkpad Driver hack in nsWindow::Create, the change in
  1.1223 +  // HWND structure makes Firefox not trigger the driver's heuristics
  1.1224 +  // any longer.
  1.1225 +  //
  1.1226 +  // The Elantech driver actually sends these messages for a three-finger
  1.1227 +  // swipe right:
  1.1228 +  //
  1.1229 +  //   WM_KEYDOWN virtual_key = 0xCC or 0xFF (depending on driver version)
  1.1230 +  //   WM_KEYDOWN virtual_key = VK_NEXT
  1.1231 +  //   WM_KEYUP   virtual_key = VK_NEXT
  1.1232 +  //   WM_KEYUP   virtual_key = 0xCC or 0xFF
  1.1233 +  //
  1.1234 +  // so we use the 0xCC or 0xFF key modifier to detect whether the Page Down
  1.1235 +  // is due to the gesture rather than a regular Page Down keypress.  We then
  1.1236 +  // pretend that we should dispatch "Go Forward" command.  Similarly
  1.1237 +  // for VK_PRIOR and "Go Back" command.
  1.1238 +  if (sUseSwipeHack &&
  1.1239 +      (aWParam == VK_NEXT || aWParam == VK_PRIOR) &&
  1.1240 +      (IS_VK_DOWN(0xFF) || IS_VK_DOWN(0xCC))) {
  1.1241 +    if (aMsg == WM_KEYDOWN) {
  1.1242 +      PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1243 +        ("MouseScroll::Device::Elantech::HandleKeyMessage(): Dispatching "
  1.1244 +         "%s command event",
  1.1245 +         aWParam == VK_NEXT ? "Forward" : "Back"));
  1.1246 +
  1.1247 +      WidgetCommandEvent commandEvent(true, nsGkAtoms::onAppCommand,
  1.1248 +        (aWParam == VK_NEXT) ? nsGkAtoms::Forward : nsGkAtoms::Back, aWidget);
  1.1249 +      InitEvent(aWidget, commandEvent);
  1.1250 +      MouseScrollHandler::DispatchEvent(aWidget, commandEvent);
  1.1251 +    }
  1.1252 +#ifdef PR_LOGGING
  1.1253 +    else {
  1.1254 +      PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1255 +        ("MouseScroll::Device::Elantech::HandleKeyMessage(): Consumed"));
  1.1256 +    }
  1.1257 +#endif
  1.1258 +    return true; // consume the message (doesn't need to dispatch key events)
  1.1259 +  }
  1.1260 +
  1.1261 +  // Version 8 of the Elantech touchpad driver sends these messages for
  1.1262 +  // zoom gestures:
  1.1263 +  //
  1.1264 +  //   WM_KEYDOWN    virtual_key = 0xCC        time = 10
  1.1265 +  //   WM_KEYDOWN    virtual_key = VK_CONTROL  time = 10
  1.1266 +  //   WM_MOUSEWHEEL                           time = ::GetTickCount()
  1.1267 +  //   WM_KEYUP      virtual_key = VK_CONTROL  time = 10
  1.1268 +  //   WM_KEYUP      virtual_key = 0xCC        time = 10
  1.1269 +  //
  1.1270 +  // The result of this is that we process all of the WM_KEYDOWN/WM_KEYUP
  1.1271 +  // messages first because their timestamps make them appear to have
  1.1272 +  // been sent before the WM_MOUSEWHEEL message.  To work around this,
  1.1273 +  // we store the current time when we process the WM_KEYUP message and
  1.1274 +  // assume that any WM_MOUSEWHEEL message with a timestamp before that
  1.1275 +  // time is one that should be processed as if the Control key was down.
  1.1276 +  if (sUsePinchHack && aMsg == WM_KEYUP &&
  1.1277 +      aWParam == VK_CONTROL && ::GetMessageTime() == 10) {
  1.1278 +    // We look only at the bottom 31 bits of the system tick count since
  1.1279 +    // GetMessageTime returns a LONG, which is signed, so we want values
  1.1280 +    // that are more easily comparable.
  1.1281 +    sZoomUntil = ::GetTickCount() & 0x7FFFFFFF;
  1.1282 +
  1.1283 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1284 +      ("MouseScroll::Device::Elantech::HandleKeyMessage(): sZoomUntil=%d",
  1.1285 +       sZoomUntil));
  1.1286 +  }
  1.1287 +
  1.1288 +  return false;
  1.1289 +}
  1.1290 +
  1.1291 +/* static */
  1.1292 +void
  1.1293 +MouseScrollHandler::Device::Elantech::UpdateZoomUntil()
  1.1294 +{
  1.1295 +  if (!sZoomUntil) {
  1.1296 +    return;
  1.1297 +  }
  1.1298 +
  1.1299 +  // For the Elantech Touchpad Zoom Gesture Hack, we should check that the
  1.1300 +  // system time (32-bit milliseconds) hasn't wrapped around.  Otherwise we
  1.1301 +  // might get into the situation where wheel events for the next 50 days of
  1.1302 +  // system uptime are assumed to be Ctrl+Wheel events.  (It is unlikely that
  1.1303 +  // we would get into that state, because the system would already need to be
  1.1304 +  // up for 50 days and the Control key message would need to be processed just
  1.1305 +  // before the system time overflow and the wheel message just after.)
  1.1306 +  //
  1.1307 +  // We also take the chance to reset sZoomUntil if we simply have passed that
  1.1308 +  // time.
  1.1309 +  LONG msgTime = ::GetMessageTime();
  1.1310 +  if ((sZoomUntil >= 0x3fffffffu && DWORD(msgTime) < 0x40000000u) ||
  1.1311 +      (sZoomUntil < DWORD(msgTime))) {
  1.1312 +    sZoomUntil = 0;
  1.1313 +
  1.1314 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1315 +      ("MouseScroll::Device::Elantech::UpdateZoomUntil(): "
  1.1316 +       "sZoomUntil was reset"));
  1.1317 +  }
  1.1318 +}
  1.1319 +
  1.1320 +/* static */
  1.1321 +bool
  1.1322 +MouseScrollHandler::Device::Elantech::IsZooming()
  1.1323 +{
  1.1324 +  // Assume the Control key is down if the Elantech touchpad has sent the
  1.1325 +  // mis-ordered WM_KEYDOWN/WM_MOUSEWHEEL messages.  (See the comment in
  1.1326 +  // OnKeyUp.)
  1.1327 +  return (sZoomUntil && static_cast<DWORD>(::GetMessageTime()) < sZoomUntil);
  1.1328 +}
  1.1329 +
  1.1330 +/******************************************************************************
  1.1331 + *
  1.1332 + * Device::TrackPoint
  1.1333 + *
  1.1334 + ******************************************************************************/
  1.1335 +
  1.1336 +/* static */
  1.1337 +bool
  1.1338 +MouseScrollHandler::Device::TrackPoint::IsDriverInstalled()
  1.1339 +{
  1.1340 +  if (WinUtils::HasRegistryKey(HKEY_CURRENT_USER,
  1.1341 +                               L"Software\\Lenovo\\TrackPoint")) {
  1.1342 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1343 +      ("MouseScroll::Device::TrackPoint::IsDriverInstalled(): "
  1.1344 +       "Lenovo's TrackPoint driver is found"));
  1.1345 +    return true;
  1.1346 +  }
  1.1347 +
  1.1348 +  if (WinUtils::HasRegistryKey(HKEY_CURRENT_USER,
  1.1349 +                               L"Software\\Alps\\Apoint\\TrackPoint")) {
  1.1350 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1351 +      ("MouseScroll::Device::TrackPoint::IsDriverInstalled(): "
  1.1352 +       "Alps's TrackPoint driver is found"));
  1.1353 +  }
  1.1354 +
  1.1355 +  return false;
  1.1356 +}
  1.1357 +
  1.1358 +/******************************************************************************
  1.1359 + *
  1.1360 + * Device::UltraNav
  1.1361 + *
  1.1362 + ******************************************************************************/
  1.1363 +
  1.1364 +/* static */
  1.1365 +bool
  1.1366 +MouseScrollHandler::Device::UltraNav::IsObsoleteDriverInstalled()
  1.1367 +{
  1.1368 +  if (WinUtils::HasRegistryKey(HKEY_CURRENT_USER,
  1.1369 +                               L"Software\\Lenovo\\UltraNav")) {
  1.1370 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1371 +      ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
  1.1372 +       "Lenovo's UltraNav driver is found"));
  1.1373 +    return true;
  1.1374 +  }
  1.1375 +
  1.1376 +  bool installed = false;
  1.1377 +  if (WinUtils::HasRegistryKey(HKEY_CURRENT_USER,
  1.1378 +        L"Software\\Synaptics\\SynTPEnh\\UltraNavUSB")) {
  1.1379 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1380 +      ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
  1.1381 +       "Synaptics's UltraNav (USB) driver is found"));
  1.1382 +    installed = true;
  1.1383 +  } else if (WinUtils::HasRegistryKey(HKEY_CURRENT_USER,
  1.1384 +               L"Software\\Synaptics\\SynTPEnh\\UltraNavPS2")) {
  1.1385 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1386 +      ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
  1.1387 +       "Synaptics's UltraNav (PS/2) driver is found"));
  1.1388 +    installed = true;
  1.1389 +  }
  1.1390 +
  1.1391 +  if (!installed) {
  1.1392 +    return false;
  1.1393 +  }
  1.1394 +
  1.1395 +  wchar_t buf[40];
  1.1396 +  bool foundKey =
  1.1397 +    WinUtils::GetRegistryKey(HKEY_LOCAL_MACHINE,
  1.1398 +                             L"Software\\Synaptics\\SynTP\\Install",
  1.1399 +                             L"DriverVersion",
  1.1400 +                             buf, sizeof buf);
  1.1401 +  if (!foundKey) {
  1.1402 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1403 +      ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
  1.1404 +       "Failed to get UltraNav driver version"));
  1.1405 +    return false;
  1.1406 +  }
  1.1407 +
  1.1408 +  int majorVersion = wcstol(buf, nullptr, 10);
  1.1409 +  int minorVersion = 0;
  1.1410 +  wchar_t* p = wcschr(buf, L'.');
  1.1411 +  if (p) {
  1.1412 +    minorVersion = wcstol(p + 1, nullptr, 10);
  1.1413 +  }
  1.1414 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1415 +    ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
  1.1416 +     "found driver version = %d.%d",
  1.1417 +     majorVersion, minorVersion));
  1.1418 +  return majorVersion < 15 || (majorVersion == 15 && minorVersion == 0);
  1.1419 +}
  1.1420 +
  1.1421 +/******************************************************************************
  1.1422 + *
  1.1423 + * Device::SetPoint
  1.1424 + *
  1.1425 + ******************************************************************************/
  1.1426 +
  1.1427 +/* static */
  1.1428 +bool
  1.1429 +MouseScrollHandler::Device::SetPoint::IsGetMessagePosResponseValid(
  1.1430 +                                        UINT aMessage,
  1.1431 +                                        WPARAM aWParam,
  1.1432 +                                        LPARAM aLParam)
  1.1433 +{
  1.1434 +  if (aMessage != WM_MOUSEHWHEEL) {
  1.1435 +    return false;
  1.1436 +  }
  1.1437 +
  1.1438 +  POINTS pts = MouseScrollHandler::GetCurrentMessagePos();
  1.1439 +  LPARAM messagePos = MAKELPARAM(pts.x, pts.y);
  1.1440 +
  1.1441 +  // XXX We should check whether SetPoint is installed or not by registry.
  1.1442 +
  1.1443 +  // SetPoint, Logitech (Logicool) mouse driver, (confirmed with 4.82.11 and
  1.1444 +  // MX-1100) always sets 0 to the lParam of WM_MOUSEHWHEEL.  The driver SENDs
  1.1445 +  // one message at first time, this time, ::GetMessagePos() works fine.
  1.1446 +  // Then, we will return 0 (0 means we process it) to the message. Then, the
  1.1447 +  // driver will POST the same messages continuously during the wheel tilted.
  1.1448 +  // But ::GetMessagePos() API always returns (0, 0) for them, even if the
  1.1449 +  // actual mouse cursor isn't 0,0.  Therefore, we cannot trust the result of
  1.1450 +  // ::GetMessagePos API if the sender is SetPoint.
  1.1451 +  if (!sMightBeUsing && !aLParam && aLParam != messagePos &&
  1.1452 +      ::InSendMessage()) {
  1.1453 +    sMightBeUsing = true;
  1.1454 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1455 +      ("MouseScroll::Device::SetPoint::IsGetMessagePosResponseValid(): "
  1.1456 +       "Might using SetPoint"));
  1.1457 +  } else if (sMightBeUsing && aLParam != 0 && ::InSendMessage()) {
  1.1458 +    // The user has changed the mouse from Logitech's to another one (e.g.,
  1.1459 +    // the user has changed to the touchpad of the notebook.
  1.1460 +    sMightBeUsing = false;
  1.1461 +    PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1462 +      ("MouseScroll::Device::SetPoint::IsGetMessagePosResponseValid(): "
  1.1463 +       "Might stop using SetPoint"));
  1.1464 +  }
  1.1465 +  return (sMightBeUsing && !aLParam && !messagePos);
  1.1466 +}
  1.1467 +
  1.1468 +/******************************************************************************
  1.1469 + *
  1.1470 + * SynthesizingEvent
  1.1471 + *
  1.1472 + ******************************************************************************/
  1.1473 +
  1.1474 +/* static */
  1.1475 +bool
  1.1476 +MouseScrollHandler::SynthesizingEvent::IsSynthesizing()
  1.1477 +{
  1.1478 +  return MouseScrollHandler::sInstance &&
  1.1479 +    MouseScrollHandler::sInstance->mSynthesizingEvent &&
  1.1480 +    MouseScrollHandler::sInstance->mSynthesizingEvent->mStatus !=
  1.1481 +      NOT_SYNTHESIZING;
  1.1482 +}
  1.1483 +
  1.1484 +nsresult
  1.1485 +MouseScrollHandler::SynthesizingEvent::Synthesize(const POINTS& aCursorPoint,
  1.1486 +                                                  HWND aWnd,
  1.1487 +                                                  UINT aMessage,
  1.1488 +                                                  WPARAM aWParam,
  1.1489 +                                                  LPARAM aLParam,
  1.1490 +                                                  const BYTE (&aKeyStates)[256])
  1.1491 +{
  1.1492 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1493 +    ("MouseScrollHandler::SynthesizingEvent::Synthesize(): aCursorPoint: { "
  1.1494 +     "x: %d, y: %d }, aWnd=0x%X, aMessage=0x%04X, aWParam=0x%08X, "
  1.1495 +     "aLParam=0x%08X, IsSynthesized()=%s, mStatus=%s",
  1.1496 +     aCursorPoint.x, aCursorPoint.y, aWnd, aMessage, aWParam, aLParam,
  1.1497 +     GetBoolName(IsSynthesizing()), GetStatusName()));
  1.1498 +
  1.1499 +  if (IsSynthesizing()) {
  1.1500 +    return NS_ERROR_NOT_AVAILABLE;
  1.1501 +  }
  1.1502 +
  1.1503 +  ::GetKeyboardState(mOriginalKeyState);
  1.1504 +
  1.1505 +  // Note that we cannot use ::SetCursorPos() because it works asynchronously.
  1.1506 +  // We should SEND the message for reducing the possibility of receiving
  1.1507 +  // unexpected message which were not sent from here.
  1.1508 +  mCursorPoint = aCursorPoint;
  1.1509 +
  1.1510 +  mWnd = aWnd;
  1.1511 +  mMessage = aMessage;
  1.1512 +  mWParam = aWParam;
  1.1513 +  mLParam = aLParam;
  1.1514 +
  1.1515 +  memcpy(mKeyState, aKeyStates, sizeof(mKeyState));
  1.1516 +  ::SetKeyboardState(mKeyState);
  1.1517 +
  1.1518 +  mStatus = SENDING_MESSAGE;
  1.1519 +
  1.1520 +  // Don't assume that aWnd is always managed by nsWindow.  It might be
  1.1521 +  // a plugin window.
  1.1522 +  ::SendMessage(aWnd, aMessage, aWParam, aLParam);
  1.1523 +
  1.1524 +  return NS_OK;
  1.1525 +}
  1.1526 +
  1.1527 +void
  1.1528 +MouseScrollHandler::SynthesizingEvent::NativeMessageReceived(nsWindowBase* aWidget,
  1.1529 +                                                             UINT aMessage,
  1.1530 +                                                             WPARAM aWParam,
  1.1531 +                                                             LPARAM aLParam)
  1.1532 +{
  1.1533 +  if (mStatus == SENDING_MESSAGE && mMessage == aMessage &&
  1.1534 +      mWParam == aWParam && mLParam == aLParam) {
  1.1535 +    mStatus = NATIVE_MESSAGE_RECEIVED;
  1.1536 +    if (aWidget && aWidget->GetWindowHandle() == mWnd) {
  1.1537 +      return;
  1.1538 +    }
  1.1539 +    // If the target window is not ours and received window is our plugin
  1.1540 +    // window, it comes from child window of the plugin.
  1.1541 +    if (aWidget && aWidget->WindowType() == eWindowType_plugin &&
  1.1542 +        !WinUtils::GetNSWindowBasePtr(mWnd)) {
  1.1543 +      return;
  1.1544 +    }
  1.1545 +    // Otherwise, the message may not be sent by us.
  1.1546 +  }
  1.1547 +
  1.1548 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1549 +    ("MouseScrollHandler::SynthesizingEvent::NativeMessageReceived(): "
  1.1550 +     "aWidget=%p, aWidget->GetWindowHandle()=0x%X, mWnd=0x%X, "
  1.1551 +     "aMessage=0x%04X, aWParam=0x%08X, aLParam=0x%08X, mStatus=%s",
  1.1552 +     aWidget, aWidget ? aWidget->GetWindowHandle() : 0, mWnd,
  1.1553 +     aMessage, aWParam, aLParam, GetStatusName()));
  1.1554 +
  1.1555 +  // We failed to receive our sent message, we failed to do the job.
  1.1556 +  Finish();
  1.1557 +
  1.1558 +  return;
  1.1559 +}
  1.1560 +
  1.1561 +void
  1.1562 +MouseScrollHandler::SynthesizingEvent::NotifyNativeMessageHandlingFinished()
  1.1563 +{
  1.1564 +  if (!IsSynthesizing()) {
  1.1565 +    return;
  1.1566 +  }
  1.1567 +
  1.1568 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1569 +    ("MouseScrollHandler::SynthesizingEvent::"
  1.1570 +     "NotifyNativeMessageHandlingFinished(): IsWaitingInternalMessage=%s",
  1.1571 +     GetBoolName(MouseScrollHandler::IsWaitingInternalMessage())));
  1.1572 +
  1.1573 +  if (MouseScrollHandler::IsWaitingInternalMessage()) {
  1.1574 +    mStatus = INTERNAL_MESSAGE_POSTED;
  1.1575 +    return;
  1.1576 +  }
  1.1577 +
  1.1578 +  // If the native message handler didn't post our internal message,
  1.1579 +  // we our job is finished.
  1.1580 +  // TODO: When we post the message to plugin window, there is remaning job.
  1.1581 +  Finish();
  1.1582 +}
  1.1583 +
  1.1584 +void
  1.1585 +MouseScrollHandler::SynthesizingEvent::NotifyInternalMessageHandlingFinished()
  1.1586 +{
  1.1587 +  if (!IsSynthesizing()) {
  1.1588 +    return;
  1.1589 +  }
  1.1590 +
  1.1591 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1592 +    ("MouseScrollHandler::SynthesizingEvent::"
  1.1593 +     "NotifyInternalMessageHandlingFinished()"));
  1.1594 +
  1.1595 +  Finish();
  1.1596 +}
  1.1597 +
  1.1598 +void
  1.1599 +MouseScrollHandler::SynthesizingEvent::Finish()
  1.1600 +{
  1.1601 +  if (!IsSynthesizing()) {
  1.1602 +    return;
  1.1603 +  }
  1.1604 +
  1.1605 +  PR_LOG(gMouseScrollLog, PR_LOG_ALWAYS,
  1.1606 +    ("MouseScrollHandler::SynthesizingEvent::Finish()"));
  1.1607 +
  1.1608 +  // Restore the original key state.
  1.1609 +  ::SetKeyboardState(mOriginalKeyState);
  1.1610 +
  1.1611 +  mStatus = NOT_SYNTHESIZING;
  1.1612 +}
  1.1613 +
  1.1614 +} // namespace widget
  1.1615 +} // namespace mozilla

mercurial