Bug 1882714 [wpt PR 44850] - Update wpt metadata, a=testonly
[gecko.git] / widget / windows / WinMouseScrollHandler.cpp
blobe10f7b29dcd513d675995c29784686e3c3f39ecd
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/DebugOnly.h"
9 #include "mozilla/Logging.h"
11 #include "WinMouseScrollHandler.h"
12 #include "nsWindow.h"
13 #include "nsWindowDefs.h"
14 #include "KeyboardLayout.h"
15 #include "WinUtils.h"
16 #include "nsGkAtoms.h"
17 #include "nsIDOMWindowUtils.h"
19 #include "mozilla/MiscEvents.h"
20 #include "mozilla/MouseEvents.h"
21 #include "mozilla/Preferences.h"
22 #include "mozilla/dom/WheelEventBinding.h"
23 #include "mozilla/StaticPrefs_mousewheel.h"
24 #include "mozilla/widget/WinRegistry.h"
26 #include <psapi.h>
28 namespace mozilla {
29 namespace widget {
31 LazyLogModule gMouseScrollLog("MouseScrollHandlerWidgets");
33 static const char* GetBoolName(bool aBool) { return aBool ? "TRUE" : "FALSE"; }
35 MouseScrollHandler* MouseScrollHandler::sInstance = nullptr;
37 bool MouseScrollHandler::Device::sFakeScrollableWindowNeeded = false;
39 bool MouseScrollHandler::Device::SynTP::sInitialized = false;
40 int32_t MouseScrollHandler::Device::SynTP::sMajorVersion = 0;
41 int32_t MouseScrollHandler::Device::SynTP::sMinorVersion = -1;
43 bool MouseScrollHandler::Device::Elantech::sUseSwipeHack = false;
44 bool MouseScrollHandler::Device::Elantech::sUsePinchHack = false;
45 DWORD MouseScrollHandler::Device::Elantech::sZoomUntil = 0;
47 bool MouseScrollHandler::Device::Apoint::sInitialized = false;
48 int32_t MouseScrollHandler::Device::Apoint::sMajorVersion = 0;
49 int32_t MouseScrollHandler::Device::Apoint::sMinorVersion = -1;
51 bool MouseScrollHandler::Device::SetPoint::sMightBeUsing = false;
53 // The duration until timeout of events transaction. The value is 1.5 sec,
54 // it's just a magic number, it was suggested by Logitech's engineer, see
55 // bug 605648 comment 90.
56 #define DEFAULT_TIMEOUT_DURATION 1500
58 /******************************************************************************
60 * MouseScrollHandler
62 ******************************************************************************/
64 /* static */
65 POINTS
66 MouseScrollHandler::GetCurrentMessagePos() {
67 if (SynthesizingEvent::IsSynthesizing()) {
68 return sInstance->mSynthesizingEvent->GetCursorPoint();
70 DWORD pos = ::GetMessagePos();
71 return MAKEPOINTS(pos);
74 // Get rid of the GetMessagePos() API.
75 #define GetMessagePos()
77 /* static */
78 void MouseScrollHandler::Initialize() { Device::Init(); }
80 /* static */
81 void MouseScrollHandler::Shutdown() {
82 delete sInstance;
83 sInstance = nullptr;
86 /* static */
87 MouseScrollHandler* MouseScrollHandler::GetInstance() {
88 if (!sInstance) {
89 sInstance = new MouseScrollHandler();
91 return sInstance;
94 MouseScrollHandler::MouseScrollHandler()
95 : mIsWaitingInternalMessage(false), mSynthesizingEvent(nullptr) {
96 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
97 ("MouseScroll: Creating an instance, this=%p, sInstance=%p", this,
98 sInstance));
101 MouseScrollHandler::~MouseScrollHandler() {
102 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
103 ("MouseScroll: Destroying an instance, this=%p, sInstance=%p", this,
104 sInstance));
106 delete mSynthesizingEvent;
109 /* static */
110 void MouseScrollHandler::MaybeLogKeyState() {
111 if (!MOZ_LOG_TEST(gMouseScrollLog, LogLevel::Debug)) {
112 return;
114 BYTE keyboardState[256];
115 if (::GetKeyboardState(keyboardState)) {
116 for (size_t i = 0; i < ArrayLength(keyboardState); i++) {
117 if (keyboardState[i]) {
118 MOZ_LOG(gMouseScrollLog, LogLevel::Debug,
119 (" Current key state: keyboardState[0x%02zX]=0x%02X (%s)", i,
120 keyboardState[i],
121 ((keyboardState[i] & 0x81) == 0x81) ? "Pressed and Toggled"
122 : (keyboardState[i] & 0x80) ? "Pressed"
123 : (keyboardState[i] & 0x01) ? "Toggled"
124 : "Unknown"));
127 } else {
128 MOZ_LOG(
129 gMouseScrollLog, LogLevel::Debug,
130 ("MouseScroll::MaybeLogKeyState(): Failed to print current keyboard "
131 "state"));
135 /* static */
136 bool MouseScrollHandler::NeedsMessage(UINT aMsg) {
137 switch (aMsg) {
138 case WM_SETTINGCHANGE:
139 case WM_MOUSEWHEEL:
140 case WM_MOUSEHWHEEL:
141 case WM_HSCROLL:
142 case WM_VSCROLL:
143 case MOZ_WM_MOUSEVWHEEL:
144 case MOZ_WM_MOUSEHWHEEL:
145 case MOZ_WM_HSCROLL:
146 case MOZ_WM_VSCROLL:
147 case WM_KEYDOWN:
148 case WM_KEYUP:
149 return true;
151 return false;
154 /* static */
155 bool MouseScrollHandler::ProcessMessage(nsWindow* aWidget, UINT msg,
156 WPARAM wParam, LPARAM lParam,
157 MSGResult& aResult) {
158 Device::Elantech::UpdateZoomUntil();
160 switch (msg) {
161 case WM_SETTINGCHANGE:
162 if (!sInstance) {
163 return false;
165 if (wParam == SPI_SETWHEELSCROLLLINES ||
166 wParam == SPI_SETWHEELSCROLLCHARS) {
167 sInstance->mSystemSettings.MarkDirty();
169 return false;
171 case WM_MOUSEWHEEL:
172 case WM_MOUSEHWHEEL:
173 GetInstance()->ProcessNativeMouseWheelMessage(aWidget, msg, wParam,
174 lParam);
175 sInstance->mSynthesizingEvent->NotifyNativeMessageHandlingFinished();
176 // We don't need to call next wndproc for WM_MOUSEWHEEL and
177 // WM_MOUSEHWHEEL. We should consume them always. If the messages
178 // would be handled by our window again, it caused making infinite
179 // message loop.
180 aResult.mConsumed = true;
181 aResult.mResult = (msg != WM_MOUSEHWHEEL);
182 return true;
184 case WM_HSCROLL:
185 case WM_VSCROLL:
186 aResult.mConsumed = GetInstance()->ProcessNativeScrollMessage(
187 aWidget, msg, wParam, lParam);
188 sInstance->mSynthesizingEvent->NotifyNativeMessageHandlingFinished();
189 aResult.mResult = 0;
190 return true;
192 case MOZ_WM_MOUSEVWHEEL:
193 case MOZ_WM_MOUSEHWHEEL:
194 GetInstance()->HandleMouseWheelMessage(aWidget, msg, wParam, lParam);
195 sInstance->mSynthesizingEvent->NotifyInternalMessageHandlingFinished();
196 // Doesn't need to call next wndproc for internal wheel message.
197 aResult.mConsumed = true;
198 return true;
200 case MOZ_WM_HSCROLL:
201 case MOZ_WM_VSCROLL:
202 GetInstance()->HandleScrollMessageAsMouseWheelMessage(aWidget, msg,
203 wParam, lParam);
204 sInstance->mSynthesizingEvent->NotifyInternalMessageHandlingFinished();
205 // Doesn't need to call next wndproc for internal scroll message.
206 aResult.mConsumed = true;
207 return true;
209 case WM_KEYDOWN:
210 case WM_KEYUP:
211 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
212 ("MouseScroll::ProcessMessage(): aWidget=%p, "
213 "msg=%s(0x%04X), wParam=0x%02zX, ::GetMessageTime()=%ld",
214 aWidget,
215 msg == WM_KEYDOWN ? "WM_KEYDOWN"
216 : msg == WM_KEYUP ? "WM_KEYUP"
217 : "Unknown",
218 msg, wParam, ::GetMessageTime()));
219 MaybeLogKeyState();
220 if (Device::Elantech::HandleKeyMessage(aWidget, msg, wParam, lParam)) {
221 aResult.mResult = 0;
222 aResult.mConsumed = true;
223 return true;
225 return false;
227 default:
228 return false;
232 /* static */
233 nsresult MouseScrollHandler::SynthesizeNativeMouseScrollEvent(
234 nsWindow* aWidget, const LayoutDeviceIntPoint& aPoint,
235 uint32_t aNativeMessage, int32_t aDelta, uint32_t aModifierFlags,
236 uint32_t aAdditionalFlags) {
237 bool useFocusedWindow = !(
238 aAdditionalFlags & nsIDOMWindowUtils::MOUSESCROLL_PREFER_WIDGET_AT_POINT);
240 POINT pt;
241 pt.x = aPoint.x;
242 pt.y = aPoint.y;
244 HWND target = useFocusedWindow ? ::WindowFromPoint(pt) : ::GetFocus();
245 NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
247 WPARAM wParam = 0;
248 LPARAM lParam = 0;
249 switch (aNativeMessage) {
250 case WM_MOUSEWHEEL:
251 case WM_MOUSEHWHEEL: {
252 lParam = MAKELPARAM(pt.x, pt.y);
253 WORD mod = 0;
254 if (aModifierFlags & (nsIWidget::CTRL_L | nsIWidget::CTRL_R)) {
255 mod |= MK_CONTROL;
257 if (aModifierFlags & (nsIWidget::SHIFT_L | nsIWidget::SHIFT_R)) {
258 mod |= MK_SHIFT;
260 wParam = MAKEWPARAM(mod, aDelta);
261 break;
263 case WM_VSCROLL:
264 case WM_HSCROLL:
265 lParam = (aAdditionalFlags &
266 nsIDOMWindowUtils::MOUSESCROLL_WIN_SCROLL_LPARAM_NOT_NULL)
267 ? reinterpret_cast<LPARAM>(target)
268 : 0;
269 wParam = aDelta;
270 break;
271 default:
272 return NS_ERROR_INVALID_ARG;
275 // Ensure to make the instance.
276 GetInstance();
278 BYTE kbdState[256];
279 memset(kbdState, 0, sizeof(kbdState));
281 AutoTArray<KeyPair, 10> keySequence;
282 WinUtils::SetupKeyModifiersSequence(&keySequence, aModifierFlags,
283 aNativeMessage);
285 for (uint32_t i = 0; i < keySequence.Length(); ++i) {
286 uint8_t key = keySequence[i].mGeneral;
287 uint8_t keySpecific = keySequence[i].mSpecific;
288 kbdState[key] = 0x81; // key is down and toggled on if appropriate
289 if (keySpecific) {
290 kbdState[keySpecific] = 0x81;
294 if (!sInstance->mSynthesizingEvent) {
295 sInstance->mSynthesizingEvent = new SynthesizingEvent();
298 POINTS pts;
299 pts.x = static_cast<SHORT>(pt.x);
300 pts.y = static_cast<SHORT>(pt.y);
301 return sInstance->mSynthesizingEvent->Synthesize(pts, target, aNativeMessage,
302 wParam, lParam, kbdState);
305 /* static */
306 void MouseScrollHandler::InitEvent(nsWindow* aWidget, WidgetGUIEvent& aEvent,
307 LPARAM* aPoint) {
308 NS_ENSURE_TRUE_VOID(aWidget);
310 // If a point is provided, use it; otherwise, get current message point or
311 // synthetic point
312 POINTS pointOnScreen;
313 if (aPoint != nullptr) {
314 pointOnScreen = MAKEPOINTS(*aPoint);
315 } else {
316 pointOnScreen = GetCurrentMessagePos();
319 // InitEvent expects the point to be in window coordinates, so translate the
320 // point from screen coordinates.
321 POINT pointOnWindow;
322 POINTSTOPOINT(pointOnWindow, pointOnScreen);
323 ::ScreenToClient(aWidget->GetWindowHandle(), &pointOnWindow);
325 LayoutDeviceIntPoint point;
326 point.x = pointOnWindow.x;
327 point.y = pointOnWindow.y;
329 aWidget->InitEvent(aEvent, &point);
332 /* static */
333 ModifierKeyState MouseScrollHandler::GetModifierKeyState(UINT aMessage) {
334 ModifierKeyState result;
335 // Assume the Control key is down if the Elantech touchpad has sent the
336 // mis-ordered WM_KEYDOWN/WM_MOUSEWHEEL messages. (See the comment in
337 // MouseScrollHandler::Device::Elantech::HandleKeyMessage().)
338 if ((aMessage == MOZ_WM_MOUSEVWHEEL || aMessage == WM_MOUSEWHEEL) &&
339 !result.IsControl() && Device::Elantech::IsZooming()) {
340 // XXX Do we need to unset MODIFIER_SHIFT, MODIFIER_ALT, MODIFIER_META too?
341 // If one of them are true, the default action becomes not zooming.
342 result.Unset(MODIFIER_ALTGRAPH);
343 result.Set(MODIFIER_CONTROL);
345 return result;
348 POINT
349 MouseScrollHandler::ComputeMessagePos(UINT aMessage, WPARAM aWParam,
350 LPARAM aLParam) {
351 POINT point;
352 if (Device::SetPoint::IsGetMessagePosResponseValid(aMessage, aWParam,
353 aLParam)) {
354 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
355 ("MouseScroll::ComputeMessagePos: Using ::GetCursorPos()"));
356 ::GetCursorPos(&point);
357 } else {
358 POINTS pts = GetCurrentMessagePos();
359 point.x = pts.x;
360 point.y = pts.y;
362 return point;
365 void MouseScrollHandler::ProcessNativeMouseWheelMessage(nsWindow* aWidget,
366 UINT aMessage,
367 WPARAM aWParam,
368 LPARAM aLParam) {
369 if (SynthesizingEvent::IsSynthesizing()) {
370 mSynthesizingEvent->NativeMessageReceived(aWidget, aMessage, aWParam,
371 aLParam);
374 POINT point = ComputeMessagePos(aMessage, aWParam, aLParam);
376 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
377 ("MouseScroll::ProcessNativeMouseWheelMessage: aWidget=%p, "
378 "aMessage=%s, wParam=0x%08zX, lParam=0x%08" PRIXLPTR
379 ", point: { x=%ld, y=%ld }",
380 aWidget,
381 aMessage == WM_MOUSEWHEEL ? "WM_MOUSEWHEEL"
382 : aMessage == WM_MOUSEHWHEEL ? "WM_MOUSEHWHEEL"
383 : aMessage == WM_VSCROLL ? "WM_VSCROLL"
384 : "WM_HSCROLL",
385 aWParam, aLParam, point.x, point.y));
386 MaybeLogKeyState();
388 HWND underCursorWnd = ::WindowFromPoint(point);
389 if (!underCursorWnd) {
390 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
391 ("MouseScroll::ProcessNativeMouseWheelMessage: "
392 "No window is not found under the cursor"));
393 return;
396 if (Device::Elantech::IsPinchHackNeeded() &&
397 Device::Elantech::IsHelperWindow(underCursorWnd)) {
398 // The Elantech driver places a window right underneath the cursor
399 // when sending a WM_MOUSEWHEEL event to us as part of a pinch-to-zoom
400 // gesture. We detect that here, and search for our window that would
401 // be beneath the cursor if that window wasn't there.
402 underCursorWnd = WinUtils::FindOurWindowAtPoint(point);
403 if (!underCursorWnd) {
404 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
405 ("MouseScroll::ProcessNativeMouseWheelMessage: "
406 "Our window is not found under the Elantech helper window"));
407 return;
411 // Handle most cases first. If the window under mouse cursor is our window
412 // except plugin window (MozillaWindowClass), we should handle the message
413 // on the window.
414 if (WinUtils::IsOurProcessWindow(underCursorWnd)) {
415 nsWindow* destWindow = WinUtils::GetNSWindowPtr(underCursorWnd);
416 if (!destWindow) {
417 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
418 ("MouseScroll::ProcessNativeMouseWheelMessage: "
419 "Found window under the cursor isn't managed by nsWindow..."));
420 HWND wnd = ::GetParent(underCursorWnd);
421 for (; wnd; wnd = ::GetParent(wnd)) {
422 destWindow = WinUtils::GetNSWindowPtr(wnd);
423 if (destWindow) {
424 break;
427 if (!wnd) {
428 MOZ_LOG(
429 gMouseScrollLog, LogLevel::Info,
430 ("MouseScroll::ProcessNativeMouseWheelMessage: Our window which is "
431 "managed by nsWindow is not found under the cursor"));
432 return;
436 MOZ_ASSERT(destWindow, "destWindow must not be NULL");
438 // Some odd touchpad utils sets focus to window under the mouse cursor.
439 // this emulates the odd behavior for debug.
440 if (mUserPrefs.ShouldEmulateToMakeWindowUnderCursorForeground() &&
441 (aMessage == WM_MOUSEWHEEL || aMessage == WM_MOUSEHWHEEL) &&
442 ::GetForegroundWindow() != destWindow->GetWindowHandle()) {
443 ::SetForegroundWindow(destWindow->GetWindowHandle());
446 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
447 ("MouseScroll::ProcessNativeMouseWheelMessage: Succeeded, "
448 "Posting internal message to an nsWindow (%p)...",
449 destWindow));
450 mIsWaitingInternalMessage = true;
451 UINT internalMessage = WinUtils::GetInternalMessage(aMessage);
452 ::PostMessage(destWindow->GetWindowHandle(), internalMessage, aWParam,
453 aLParam);
454 return;
457 // If the window under cursor is not in our process, it means:
458 // 1. The window may be a plugin window (GeckoPluginWindow or its descendant).
459 // 2. The window may be another application's window.
460 HWND pluginWnd = WinUtils::FindOurProcessWindow(underCursorWnd);
461 if (!pluginWnd) {
462 // If there is no plugin window in ancestors of the window under cursor,
463 // the window is for another applications (case 2).
464 // We don't need to handle this message.
465 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
466 ("MouseScroll::ProcessNativeMouseWheelMessage: "
467 "Our window is not found under the cursor"));
468 return;
471 // If the window is a part of plugin, we should post the message to it.
472 MOZ_LOG(
473 gMouseScrollLog, LogLevel::Info,
474 ("MouseScroll::ProcessNativeMouseWheelMessage: Succeeded, "
475 "Redirecting the message to a window which is a plugin child window"));
476 ::PostMessage(underCursorWnd, aMessage, aWParam, aLParam);
479 bool MouseScrollHandler::ProcessNativeScrollMessage(nsWindow* aWidget,
480 UINT aMessage,
481 WPARAM aWParam,
482 LPARAM aLParam) {
483 if (aLParam || mUserPrefs.IsScrollMessageHandledAsWheelMessage()) {
484 // Scroll message generated by Thinkpad Trackpoint Driver or similar
485 // Treat as a mousewheel message and scroll appropriately
486 ProcessNativeMouseWheelMessage(aWidget, aMessage, aWParam, aLParam);
487 // Always consume the scroll message if we try to emulate mouse wheel
488 // action.
489 return true;
492 if (SynthesizingEvent::IsSynthesizing()) {
493 mSynthesizingEvent->NativeMessageReceived(aWidget, aMessage, aWParam,
494 aLParam);
497 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
498 ("MouseScroll::ProcessNativeScrollMessage: aWidget=%p, "
499 "aMessage=%s, wParam=0x%08zX, lParam=0x%08" PRIXLPTR,
500 aWidget, aMessage == WM_VSCROLL ? "WM_VSCROLL" : "WM_HSCROLL",
501 aWParam, aLParam));
503 // Scroll message generated by external application
504 WidgetContentCommandEvent commandEvent(true, eContentCommandScroll, aWidget);
505 commandEvent.mScroll.mIsHorizontal = (aMessage == WM_HSCROLL);
507 switch (LOWORD(aWParam)) {
508 case SB_LINEUP: // SB_LINELEFT
509 commandEvent.mScroll.mUnit =
510 WidgetContentCommandEvent::eCmdScrollUnit_Line;
511 commandEvent.mScroll.mAmount = -1;
512 break;
513 case SB_LINEDOWN: // SB_LINERIGHT
514 commandEvent.mScroll.mUnit =
515 WidgetContentCommandEvent::eCmdScrollUnit_Line;
516 commandEvent.mScroll.mAmount = 1;
517 break;
518 case SB_PAGEUP: // SB_PAGELEFT
519 commandEvent.mScroll.mUnit =
520 WidgetContentCommandEvent::eCmdScrollUnit_Page;
521 commandEvent.mScroll.mAmount = -1;
522 break;
523 case SB_PAGEDOWN: // SB_PAGERIGHT
524 commandEvent.mScroll.mUnit =
525 WidgetContentCommandEvent::eCmdScrollUnit_Page;
526 commandEvent.mScroll.mAmount = 1;
527 break;
528 case SB_TOP: // SB_LEFT
529 commandEvent.mScroll.mUnit =
530 WidgetContentCommandEvent::eCmdScrollUnit_Whole;
531 commandEvent.mScroll.mAmount = -1;
532 break;
533 case SB_BOTTOM: // SB_RIGHT
534 commandEvent.mScroll.mUnit =
535 WidgetContentCommandEvent::eCmdScrollUnit_Whole;
536 commandEvent.mScroll.mAmount = 1;
537 break;
538 default:
539 return false;
541 // XXX If this is a plugin window, we should dispatch the event from
542 // parent window.
543 aWidget->DispatchContentCommandEvent(&commandEvent);
544 return true;
547 void MouseScrollHandler::HandleMouseWheelMessage(nsWindow* aWidget,
548 UINT aMessage, WPARAM aWParam,
549 LPARAM aLParam) {
550 MOZ_ASSERT((aMessage == MOZ_WM_MOUSEVWHEEL || aMessage == MOZ_WM_MOUSEHWHEEL),
551 "HandleMouseWheelMessage must be called with "
552 "MOZ_WM_MOUSEVWHEEL or MOZ_WM_MOUSEHWHEEL");
554 MOZ_LOG(
555 gMouseScrollLog, LogLevel::Info,
556 ("MouseScroll::HandleMouseWheelMessage: aWidget=%p, "
557 "aMessage=MOZ_WM_MOUSE%sWHEEL, aWParam=0x%08zX, aLParam=0x%08" PRIXLPTR,
558 aWidget, aMessage == MOZ_WM_MOUSEVWHEEL ? "V" : "H", aWParam, aLParam));
560 mIsWaitingInternalMessage = false;
562 // If it's not allowed to cache system settings, we need to reset the cache
563 // before handling the mouse wheel message.
564 mSystemSettings.TrustedScrollSettingsDriver();
566 EventInfo eventInfo(aWidget, WinUtils::GetNativeMessage(aMessage), aWParam,
567 aLParam);
568 if (!eventInfo.CanDispatchWheelEvent()) {
569 MOZ_LOG(
570 gMouseScrollLog, LogLevel::Info,
571 ("MouseScroll::HandleMouseWheelMessage: Cannot dispatch the events"));
572 mLastEventInfo.ResetTransaction();
573 return;
576 // Discard the remaining delta if current wheel message and last one are
577 // received by different window or to scroll different direction or
578 // different unit scroll. Furthermore, if the last event was too old.
579 if (!mLastEventInfo.CanContinueTransaction(eventInfo)) {
580 mLastEventInfo.ResetTransaction();
583 mLastEventInfo.RecordEvent(eventInfo);
585 ModifierKeyState modKeyState = GetModifierKeyState(aMessage);
587 // Grab the widget, it might be destroyed by a DOM event handler.
588 RefPtr<nsWindow> kungFuDethGrip(aWidget);
590 WidgetWheelEvent wheelEvent(true, eWheel, aWidget);
591 if (mLastEventInfo.InitWheelEvent(aWidget, wheelEvent, modKeyState,
592 aLParam)) {
593 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
594 ("MouseScroll::HandleMouseWheelMessage: dispatching "
595 "eWheel event"));
596 aWidget->DispatchWheelEvent(&wheelEvent);
597 if (aWidget->Destroyed()) {
598 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
599 ("MouseScroll::HandleMouseWheelMessage: The window was destroyed "
600 "by eWheel event"));
601 mLastEventInfo.ResetTransaction();
602 return;
604 } else {
605 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
606 ("MouseScroll::HandleMouseWheelMessage: eWheel event is not "
607 "dispatched"));
611 void MouseScrollHandler::HandleScrollMessageAsMouseWheelMessage(
612 nsWindow* aWidget, UINT aMessage, WPARAM aWParam, LPARAM aLParam) {
613 MOZ_ASSERT((aMessage == MOZ_WM_VSCROLL || aMessage == MOZ_WM_HSCROLL),
614 "HandleScrollMessageAsMouseWheelMessage must be called with "
615 "MOZ_WM_VSCROLL or MOZ_WM_HSCROLL");
617 mIsWaitingInternalMessage = false;
619 ModifierKeyState modKeyState = GetModifierKeyState(aMessage);
621 WidgetWheelEvent wheelEvent(true, eWheel, aWidget);
622 double& delta =
623 (aMessage == MOZ_WM_VSCROLL) ? wheelEvent.mDeltaY : wheelEvent.mDeltaX;
624 int32_t& lineOrPageDelta = (aMessage == MOZ_WM_VSCROLL)
625 ? wheelEvent.mLineOrPageDeltaY
626 : wheelEvent.mLineOrPageDeltaX;
628 delta = 1.0;
629 lineOrPageDelta = 1;
631 switch (LOWORD(aWParam)) {
632 case SB_PAGEUP:
633 delta = -1.0;
634 lineOrPageDelta = -1;
635 [[fallthrough]];
636 case SB_PAGEDOWN:
637 wheelEvent.mDeltaMode = dom::WheelEvent_Binding::DOM_DELTA_PAGE;
638 break;
640 case SB_LINEUP:
641 delta = -1.0;
642 lineOrPageDelta = -1;
643 [[fallthrough]];
644 case SB_LINEDOWN:
645 wheelEvent.mDeltaMode = dom::WheelEvent_Binding::DOM_DELTA_LINE;
646 break;
648 default:
649 return;
651 modKeyState.InitInputEvent(wheelEvent);
653 // Current mouse position may not be same as when the original message
654 // is received. However, this data is not available with the original
655 // message, which is why nullptr is passed in. We need to know the actual
656 // mouse cursor position when the original message was received.
657 InitEvent(aWidget, wheelEvent, nullptr);
659 MOZ_LOG(
660 gMouseScrollLog, LogLevel::Info,
661 ("MouseScroll::HandleScrollMessageAsMouseWheelMessage: aWidget=%p, "
662 "aMessage=MOZ_WM_%sSCROLL, aWParam=0x%08zX, aLParam=0x%08" PRIXLPTR ", "
663 "wheelEvent { mRefPoint: { x: %d, y: %d }, mDeltaX: %f, mDeltaY: %f, "
664 "mLineOrPageDeltaX: %d, mLineOrPageDeltaY: %d, "
665 "isShift: %s, isControl: %s, isAlt: %s, isMeta: %s }",
666 aWidget, (aMessage == MOZ_WM_VSCROLL) ? "V" : "H", aWParam, aLParam,
667 wheelEvent.mRefPoint.x.value, wheelEvent.mRefPoint.y.value,
668 wheelEvent.mDeltaX, wheelEvent.mDeltaY, wheelEvent.mLineOrPageDeltaX,
669 wheelEvent.mLineOrPageDeltaY, GetBoolName(wheelEvent.IsShift()),
670 GetBoolName(wheelEvent.IsControl()), GetBoolName(wheelEvent.IsAlt()),
671 GetBoolName(wheelEvent.IsMeta())));
673 aWidget->DispatchWheelEvent(&wheelEvent);
676 /******************************************************************************
678 * EventInfo
680 ******************************************************************************/
682 MouseScrollHandler::EventInfo::EventInfo(nsWindow* aWidget, UINT aMessage,
683 WPARAM aWParam, LPARAM aLParam) {
684 MOZ_ASSERT(
685 aMessage == WM_MOUSEWHEEL || aMessage == WM_MOUSEHWHEEL,
686 "EventInfo must be initialized with WM_MOUSEWHEEL or WM_MOUSEHWHEEL");
688 MouseScrollHandler::GetInstance()->mSystemSettings.Init();
690 mIsVertical = (aMessage == WM_MOUSEWHEEL);
691 mIsPage =
692 MouseScrollHandler::sInstance->mSystemSettings.IsPageScroll(mIsVertical);
693 mDelta = (short)HIWORD(aWParam);
694 mWnd = aWidget->GetWindowHandle();
695 mTimeStamp = TimeStamp::Now();
698 bool MouseScrollHandler::EventInfo::CanDispatchWheelEvent() const {
699 if (!GetScrollAmount()) {
700 // XXX I think that we should dispatch mouse wheel events even if the
701 // operation will not scroll because the wheel operation really happened
702 // and web application may want to handle the event for non-scroll action.
703 return false;
706 return (mDelta != 0);
709 int32_t MouseScrollHandler::EventInfo::GetScrollAmount() const {
710 if (mIsPage) {
711 return 1;
713 return MouseScrollHandler::sInstance->mSystemSettings.GetScrollAmount(
714 mIsVertical);
717 /******************************************************************************
719 * LastEventInfo
721 ******************************************************************************/
723 bool MouseScrollHandler::LastEventInfo::CanContinueTransaction(
724 const EventInfo& aNewEvent) {
725 int32_t timeout = MouseScrollHandler::sInstance->mUserPrefs
726 .GetMouseScrollTransactionTimeout();
727 return !mWnd ||
728 (mWnd == aNewEvent.GetWindowHandle() &&
729 IsPositive() == aNewEvent.IsPositive() &&
730 mIsVertical == aNewEvent.IsVertical() &&
731 mIsPage == aNewEvent.IsPage() &&
732 (timeout < 0 || TimeStamp::Now() - mTimeStamp <=
733 TimeDuration::FromMilliseconds(timeout)));
736 void MouseScrollHandler::LastEventInfo::ResetTransaction() {
737 if (!mWnd) {
738 return;
741 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
742 ("MouseScroll::LastEventInfo::ResetTransaction()"));
744 mWnd = nullptr;
745 mAccumulatedDelta = 0;
748 void MouseScrollHandler::LastEventInfo::RecordEvent(const EventInfo& aEvent) {
749 mWnd = aEvent.GetWindowHandle();
750 mDelta = aEvent.GetNativeDelta();
751 mIsVertical = aEvent.IsVertical();
752 mIsPage = aEvent.IsPage();
753 mTimeStamp = TimeStamp::Now();
756 /* static */
757 int32_t MouseScrollHandler::LastEventInfo::RoundDelta(double aDelta) {
758 return (aDelta >= 0) ? (int32_t)floor(aDelta) : (int32_t)ceil(aDelta);
761 bool MouseScrollHandler::LastEventInfo::InitWheelEvent(
762 nsWindow* aWidget, WidgetWheelEvent& aWheelEvent,
763 const ModifierKeyState& aModKeyState, LPARAM aLParam) {
764 MOZ_ASSERT(aWheelEvent.mMessage == eWheel);
766 if (StaticPrefs::mousewheel_ignore_cursor_position_in_lparam()) {
767 InitEvent(aWidget, aWheelEvent, nullptr);
768 } else {
769 InitEvent(aWidget, aWheelEvent, &aLParam);
772 aModKeyState.InitInputEvent(aWheelEvent);
774 // Our positive delta value means to bottom or right.
775 // But positive native delta value means to top or right.
776 // Use orienter for computing our delta value with native delta value.
777 int32_t orienter = mIsVertical ? -1 : 1;
779 aWheelEvent.mDeltaMode = mIsPage ? dom::WheelEvent_Binding::DOM_DELTA_PAGE
780 : dom::WheelEvent_Binding::DOM_DELTA_LINE;
782 double ticks = double(mDelta) * orienter / double(WHEEL_DELTA);
783 if (mIsVertical) {
784 aWheelEvent.mWheelTicksY = ticks;
785 } else {
786 aWheelEvent.mWheelTicksX = ticks;
789 double& delta = mIsVertical ? aWheelEvent.mDeltaY : aWheelEvent.mDeltaX;
790 int32_t& lineOrPageDelta = mIsVertical ? aWheelEvent.mLineOrPageDeltaY
791 : aWheelEvent.mLineOrPageDeltaX;
793 double nativeDeltaPerUnit =
794 mIsPage ? double(WHEEL_DELTA) : double(WHEEL_DELTA) / GetScrollAmount();
796 delta = double(mDelta) * orienter / nativeDeltaPerUnit;
797 mAccumulatedDelta += mDelta;
798 lineOrPageDelta =
799 mAccumulatedDelta * orienter / RoundDelta(nativeDeltaPerUnit);
800 mAccumulatedDelta -=
801 lineOrPageDelta * orienter * RoundDelta(nativeDeltaPerUnit);
803 if (aWheelEvent.mDeltaMode != dom::WheelEvent_Binding::DOM_DELTA_LINE) {
804 // If the scroll delta mode isn't per line scroll, we shouldn't allow to
805 // override the system scroll speed setting.
806 aWheelEvent.mAllowToOverrideSystemScrollSpeed = false;
809 MOZ_LOG(
810 gMouseScrollLog, LogLevel::Info,
811 ("MouseScroll::LastEventInfo::InitWheelEvent: aWidget=%p, "
812 "aWheelEvent { mRefPoint: { x: %d, y: %d }, mDeltaX: %f, mDeltaY: %f, "
813 "mLineOrPageDeltaX: %d, mLineOrPageDeltaY: %d, "
814 "isShift: %s, isControl: %s, isAlt: %s, isMeta: %s, "
815 "mAllowToOverrideSystemScrollSpeed: %s }, "
816 "mAccumulatedDelta: %d",
817 aWidget, aWheelEvent.mRefPoint.x.value, aWheelEvent.mRefPoint.y.value,
818 aWheelEvent.mDeltaX, aWheelEvent.mDeltaY, aWheelEvent.mLineOrPageDeltaX,
819 aWheelEvent.mLineOrPageDeltaY, GetBoolName(aWheelEvent.IsShift()),
820 GetBoolName(aWheelEvent.IsControl()), GetBoolName(aWheelEvent.IsAlt()),
821 GetBoolName(aWheelEvent.IsMeta()),
822 GetBoolName(aWheelEvent.mAllowToOverrideSystemScrollSpeed),
823 mAccumulatedDelta));
825 return (delta != 0);
828 /******************************************************************************
830 * SystemSettings
832 ******************************************************************************/
834 void MouseScrollHandler::SystemSettings::Init() {
835 if (mInitialized) {
836 return;
839 InitScrollLines();
840 InitScrollChars();
842 mInitialized = true;
844 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
845 ("MouseScroll::SystemSettings::Init(): initialized, "
846 "mScrollLines=%d, mScrollChars=%d",
847 mScrollLines, mScrollChars));
850 bool MouseScrollHandler::SystemSettings::InitScrollLines() {
851 int32_t oldValue = mInitialized ? mScrollLines : 0;
852 mIsReliableScrollLines = false;
853 mScrollLines = MouseScrollHandler::sInstance->mUserPrefs
854 .GetOverriddenVerticalScrollAmout();
855 if (mScrollLines >= 0) {
856 // overridden by the pref.
857 mIsReliableScrollLines = true;
858 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
859 ("MouseScroll::SystemSettings::InitScrollLines(): mScrollLines is "
860 "overridden by the pref: %d",
861 mScrollLines));
862 } else if (!::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &mScrollLines,
863 0)) {
864 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
865 ("MouseScroll::SystemSettings::InitScrollLines(): "
866 "::SystemParametersInfo("
867 "SPI_GETWHEELSCROLLLINES) failed"));
868 mScrollLines = DefaultScrollLines();
871 if (mScrollLines > WHEEL_DELTA) {
872 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
873 ("MouseScroll::SystemSettings::InitScrollLines(): the result of "
874 "::SystemParametersInfo(SPI_GETWHEELSCROLLLINES) is too large: %d",
875 mScrollLines));
876 // sScrollLines usually equals 3 or 0 (for no scrolling)
877 // However, if sScrollLines > WHEEL_DELTA, we assume that
878 // the mouse driver wants a page scroll. The docs state that
879 // sScrollLines should explicitly equal WHEEL_PAGESCROLL, but
880 // since some mouse drivers use an arbitrary large number instead,
881 // we have to handle that as well.
882 mScrollLines = WHEEL_PAGESCROLL;
885 return oldValue != mScrollLines;
888 bool MouseScrollHandler::SystemSettings::InitScrollChars() {
889 int32_t oldValue = mInitialized ? mScrollChars : 0;
890 mIsReliableScrollChars = false;
891 mScrollChars = MouseScrollHandler::sInstance->mUserPrefs
892 .GetOverriddenHorizontalScrollAmout();
893 if (mScrollChars >= 0) {
894 // overridden by the pref.
895 mIsReliableScrollChars = true;
896 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
897 ("MouseScroll::SystemSettings::InitScrollChars(): mScrollChars is "
898 "overridden by the pref: %d",
899 mScrollChars));
900 } else if (!::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &mScrollChars,
901 0)) {
902 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
903 ("MouseScroll::SystemSettings::InitScrollChars(): "
904 "::SystemParametersInfo("
905 "SPI_GETWHEELSCROLLCHARS) failed, this is unexpected on Vista or "
906 "later"));
907 // XXX Should we use DefaultScrollChars()?
908 mScrollChars = 1;
911 if (mScrollChars > WHEEL_DELTA) {
912 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
913 ("MouseScroll::SystemSettings::InitScrollChars(): the result of "
914 "::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS) is too large: %d",
915 mScrollChars));
916 // See the comments for the case mScrollLines > WHEEL_DELTA.
917 mScrollChars = WHEEL_PAGESCROLL;
920 return oldValue != mScrollChars;
923 void MouseScrollHandler::SystemSettings::MarkDirty() {
924 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
925 ("MouseScrollHandler::SystemSettings::MarkDirty(): "
926 "Marking SystemSettings dirty"));
927 mInitialized = false;
928 // When system settings are changed, we should reset current transaction.
929 MOZ_ASSERT(sInstance,
930 "Must not be called at initializing MouseScrollHandler");
931 MouseScrollHandler::sInstance->mLastEventInfo.ResetTransaction();
934 void MouseScrollHandler::SystemSettings::RefreshCache() {
935 bool isChanged = InitScrollLines();
936 isChanged = InitScrollChars() || isChanged;
937 if (!isChanged) {
938 return;
940 // If the scroll amount is changed, we should reset current transaction.
941 MOZ_ASSERT(sInstance,
942 "Must not be called at initializing MouseScrollHandler");
943 MouseScrollHandler::sInstance->mLastEventInfo.ResetTransaction();
946 void MouseScrollHandler::SystemSettings::TrustedScrollSettingsDriver() {
947 if (!mInitialized) {
948 return;
951 // if the cache is initialized with prefs, we don't need to refresh it.
952 if (mIsReliableScrollLines && mIsReliableScrollChars) {
953 return;
956 MouseScrollHandler::UserPrefs& userPrefs =
957 MouseScrollHandler::sInstance->mUserPrefs;
959 // If system settings cache is disabled, we should always refresh them.
960 if (!userPrefs.IsSystemSettingCacheEnabled()) {
961 RefreshCache();
962 return;
965 // If pref is set to as "always trust the cache", we shouldn't refresh them
966 // in any environments.
967 if (userPrefs.IsSystemSettingCacheForciblyEnabled()) {
968 return;
971 // If SynTP of Synaptics or Apoint of Alps is installed, it may hook
972 // ::SystemParametersInfo() and returns different value from system settings.
973 if (Device::SynTP::IsDriverInstalled() ||
974 Device::Apoint::IsDriverInstalled()) {
975 RefreshCache();
976 return;
979 // XXX We're not sure about other touchpad drivers...
982 /******************************************************************************
984 * UserPrefs
986 ******************************************************************************/
988 MouseScrollHandler::UserPrefs::UserPrefs() : mInitialized(false) {
989 // We need to reset mouse wheel transaction when all of mousewheel related
990 // prefs are changed.
991 DebugOnly<nsresult> rv =
992 Preferences::RegisterPrefixCallback(OnChange, "mousewheel.", this);
993 MOZ_ASSERT(NS_SUCCEEDED(rv), "Failed to register callback for mousewheel.");
996 MouseScrollHandler::UserPrefs::~UserPrefs() {
997 DebugOnly<nsresult> rv =
998 Preferences::UnregisterPrefixCallback(OnChange, "mousewheel.", this);
999 MOZ_ASSERT(NS_SUCCEEDED(rv), "Failed to unregister callback for mousewheel.");
1002 void MouseScrollHandler::UserPrefs::Init() {
1003 if (mInitialized) {
1004 return;
1007 mInitialized = true;
1009 mScrollMessageHandledAsWheelMessage =
1010 Preferences::GetBool("mousewheel.emulate_at_wm_scroll", false);
1011 mEnableSystemSettingCache =
1012 Preferences::GetBool("mousewheel.system_settings_cache.enabled", true);
1013 mForceEnableSystemSettingCache = Preferences::GetBool(
1014 "mousewheel.system_settings_cache.force_enabled", false);
1015 mEmulateToMakeWindowUnderCursorForeground = Preferences::GetBool(
1016 "mousewheel.debug.make_window_under_cursor_foreground", false);
1017 mOverriddenVerticalScrollAmount =
1018 Preferences::GetInt("mousewheel.windows.vertical_amount_override", -1);
1019 mOverriddenHorizontalScrollAmount =
1020 Preferences::GetInt("mousewheel.windows.horizontal_amount_override", -1);
1021 mMouseScrollTransactionTimeout = Preferences::GetInt(
1022 "mousewheel.windows.transaction.timeout", DEFAULT_TIMEOUT_DURATION);
1024 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1025 ("MouseScroll::UserPrefs::Init(): initialized, "
1026 "mScrollMessageHandledAsWheelMessage=%s, "
1027 "mEnableSystemSettingCache=%s, "
1028 "mForceEnableSystemSettingCache=%s, "
1029 "mEmulateToMakeWindowUnderCursorForeground=%s, "
1030 "mOverriddenVerticalScrollAmount=%d, "
1031 "mOverriddenHorizontalScrollAmount=%d, "
1032 "mMouseScrollTransactionTimeout=%d",
1033 GetBoolName(mScrollMessageHandledAsWheelMessage),
1034 GetBoolName(mEnableSystemSettingCache),
1035 GetBoolName(mForceEnableSystemSettingCache),
1036 GetBoolName(mEmulateToMakeWindowUnderCursorForeground),
1037 mOverriddenVerticalScrollAmount, mOverriddenHorizontalScrollAmount,
1038 mMouseScrollTransactionTimeout));
1041 void MouseScrollHandler::UserPrefs::MarkDirty() {
1042 MOZ_LOG(
1043 gMouseScrollLog, LogLevel::Info,
1044 ("MouseScrollHandler::UserPrefs::MarkDirty(): Marking UserPrefs dirty"));
1045 mInitialized = false;
1046 // Some prefs might override system settings, so, we should mark them dirty.
1047 MouseScrollHandler::sInstance->mSystemSettings.MarkDirty();
1048 // When user prefs for mousewheel are changed, we should reset current
1049 // transaction.
1050 MOZ_ASSERT(sInstance,
1051 "Must not be called at initializing MouseScrollHandler");
1052 MouseScrollHandler::sInstance->mLastEventInfo.ResetTransaction();
1055 /******************************************************************************
1057 * Device
1059 ******************************************************************************/
1061 /* static */
1062 bool MouseScrollHandler::Device::GetWorkaroundPref(const char* aPrefName,
1063 bool aValueIfAutomatic) {
1064 if (!aPrefName) {
1065 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1066 ("MouseScroll::Device::GetWorkaroundPref(): Failed, aPrefName is "
1067 "NULL"));
1068 return aValueIfAutomatic;
1071 int32_t lHackValue = 0;
1072 if (NS_FAILED(Preferences::GetInt(aPrefName, &lHackValue))) {
1073 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1074 ("MouseScroll::Device::GetWorkaroundPref(): Preferences::GetInt() "
1075 "failed,"
1076 " aPrefName=\"%s\", aValueIfAutomatic=%s",
1077 aPrefName, GetBoolName(aValueIfAutomatic)));
1078 return aValueIfAutomatic;
1081 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1082 ("MouseScroll::Device::GetWorkaroundPref(): Succeeded, "
1083 "aPrefName=\"%s\", aValueIfAutomatic=%s, lHackValue=%d",
1084 aPrefName, GetBoolName(aValueIfAutomatic), lHackValue));
1086 switch (lHackValue) {
1087 case 0: // disabled
1088 return false;
1089 case 1: // enabled
1090 return true;
1091 default: // -1: autodetect
1092 return aValueIfAutomatic;
1096 /* static */
1097 void MouseScrollHandler::Device::Init() {
1098 // FYI: Thinkpad's TrackPoint is Apoint of Alps and UltraNav is SynTP of
1099 // Synaptics. So, those drivers' information should be initialized
1100 // before calling methods of TrackPoint and UltraNav.
1101 SynTP::Init();
1102 Elantech::Init();
1103 Apoint::Init();
1105 sFakeScrollableWindowNeeded = GetWorkaroundPref(
1106 "ui.trackpoint_hack.enabled", (TrackPoint::IsDriverInstalled() ||
1107 UltraNav::IsObsoleteDriverInstalled()));
1109 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1110 ("MouseScroll::Device::Init(): sFakeScrollableWindowNeeded=%s",
1111 GetBoolName(sFakeScrollableWindowNeeded)));
1114 /******************************************************************************
1116 * Device::SynTP
1118 ******************************************************************************/
1120 /* static */
1121 void MouseScrollHandler::Device::SynTP::Init() {
1122 if (sInitialized) {
1123 return;
1126 sInitialized = true;
1127 sMajorVersion = 0;
1128 sMinorVersion = -1;
1130 wchar_t buf[40];
1131 if (!WinRegistry::GetString(
1132 HKEY_LOCAL_MACHINE, u"Software\\Synaptics\\SynTP\\Install"_ns,
1133 u"DriverVersion"_ns, buf, WinRegistry::kLegacyWinUtilsStringFlags)) {
1134 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1135 ("MouseScroll::Device::SynTP::Init(): "
1136 "SynTP driver is not found"));
1137 return;
1140 sMajorVersion = wcstol(buf, nullptr, 10);
1141 sMinorVersion = 0;
1142 wchar_t* p = wcschr(buf, L'.');
1143 if (p) {
1144 sMinorVersion = wcstol(p + 1, nullptr, 10);
1146 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1147 ("MouseScroll::Device::SynTP::Init(): "
1148 "found driver version = %d.%d",
1149 sMajorVersion, sMinorVersion));
1152 /******************************************************************************
1154 * Device::Elantech
1156 ******************************************************************************/
1158 /* static */
1159 void MouseScrollHandler::Device::Elantech::Init() {
1160 int32_t version = GetDriverMajorVersion();
1161 bool needsHack = Device::GetWorkaroundPref(
1162 "ui.elantech_gesture_hacks.enabled", version != 0);
1163 sUseSwipeHack = needsHack && version <= 7;
1164 sUsePinchHack = needsHack && version <= 8;
1166 MOZ_LOG(
1167 gMouseScrollLog, LogLevel::Info,
1168 ("MouseScroll::Device::Elantech::Init(): version=%d, sUseSwipeHack=%s, "
1169 "sUsePinchHack=%s",
1170 version, GetBoolName(sUseSwipeHack), GetBoolName(sUsePinchHack)));
1173 /* static */
1174 int32_t MouseScrollHandler::Device::Elantech::GetDriverMajorVersion() {
1175 wchar_t buf[40];
1176 // The driver version is found in one of these two registry keys.
1177 if (!WinRegistry::GetString(
1178 HKEY_CURRENT_USER, u"Software\\Elantech\\MainOption"_ns,
1179 u"DriverVersion"_ns, buf, WinRegistry::kLegacyWinUtilsStringFlags) &&
1180 !WinRegistry::GetString(HKEY_CURRENT_USER, u"Software\\Elantech"_ns,
1181 u"DriverVersion"_ns, buf,
1182 WinRegistry::kLegacyWinUtilsStringFlags)) {
1183 return 0;
1186 // Assume that the major version number can be found just after a space
1187 // or at the start of the string.
1188 for (wchar_t* p = buf; *p; p++) {
1189 if (*p >= L'0' && *p <= L'9' && (p == buf || *(p - 1) == L' ')) {
1190 return wcstol(p, nullptr, 10);
1194 return 0;
1197 /* static */
1198 bool MouseScrollHandler::Device::Elantech::IsHelperWindow(HWND aWnd) {
1199 // The helper window cannot be distinguished based on its window class, so we
1200 // need to check if it is owned by the helper process, ETDCtrl.exe.
1202 const wchar_t* filenameSuffix = L"\\etdctrl.exe";
1203 const int filenameSuffixLength = 12;
1205 DWORD pid;
1206 ::GetWindowThreadProcessId(aWnd, &pid);
1208 HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
1209 if (!hProcess) {
1210 return false;
1213 bool result = false;
1214 wchar_t path[256] = {L'\0'};
1215 if (::GetProcessImageFileNameW(hProcess, path, ArrayLength(path))) {
1216 int pathLength = lstrlenW(path);
1217 if (pathLength >= filenameSuffixLength) {
1218 if (lstrcmpiW(path + pathLength - filenameSuffixLength, filenameSuffix) ==
1219 0) {
1220 result = true;
1224 ::CloseHandle(hProcess);
1226 return result;
1229 /* static */
1230 bool MouseScrollHandler::Device::Elantech::HandleKeyMessage(nsWindow* aWidget,
1231 UINT aMsg,
1232 WPARAM aWParam,
1233 LPARAM aLParam) {
1234 // The Elantech touchpad driver understands three-finger swipe left and
1235 // right gestures, and translates them into Page Up and Page Down key
1236 // events for most applications. For Firefox 3.6, it instead sends
1237 // Alt+Left and Alt+Right to trigger browser back/forward actions. As
1238 // with the Thinkpad Driver hack in nsWindow::Create, the change in
1239 // HWND structure makes Firefox not trigger the driver's heuristics
1240 // any longer.
1242 // The Elantech driver actually sends these messages for a three-finger
1243 // swipe right:
1245 // WM_KEYDOWN virtual_key = 0xCC or 0xFF ScanCode = 00
1246 // WM_KEYDOWN virtual_key = VK_NEXT ScanCode = 00
1247 // WM_KEYUP virtual_key = VK_NEXT ScanCode = 00
1248 // WM_KEYUP virtual_key = 0xCC or 0xFF ScanCode = 00
1250 // Whether 0xCC or 0xFF is sent is suspected to depend on the driver
1251 // version. 7.0.4.12_14Jul09_WHQL, 7.0.5.10, and 7.0.6.0 generate 0xCC.
1252 // 7.0.4.3 from Asus on EeePC generates 0xFF.
1254 // On some hardware, IS_VK_DOWN(0xFF) returns true even when Elantech
1255 // messages are not involved, meaning that alone is not enough to
1256 // distinguish the gesture from a regular Page Up or Page Down key press.
1257 // The ScanCode is therefore also tested to detect the gesture.
1258 // We then pretend that we should dispatch "Go Forward" command. Similarly
1259 // for VK_PRIOR and "Go Back" command.
1260 if (sUseSwipeHack && (aWParam == VK_NEXT || aWParam == VK_PRIOR) &&
1261 WinUtils::GetScanCode(aLParam) == 0 &&
1262 (IS_VK_DOWN(0xFF) || IS_VK_DOWN(0xCC))) {
1263 if (aMsg == WM_KEYDOWN) {
1264 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1265 ("MouseScroll::Device::Elantech::HandleKeyMessage(): Dispatching "
1266 "%s command event",
1267 aWParam == VK_NEXT ? "Forward" : "Back"));
1269 WidgetCommandEvent appCommandEvent(
1270 true, (aWParam == VK_NEXT) ? nsGkAtoms::Forward : nsGkAtoms::Back,
1271 aWidget);
1273 // In this scenario, the coordinate of the event isn't supplied, so pass
1274 // nullptr as an argument to indicate using the coordinate from the last
1275 // available window message.
1276 InitEvent(aWidget, appCommandEvent, nullptr);
1277 aWidget->DispatchWindowEvent(appCommandEvent);
1278 } else {
1279 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1280 ("MouseScroll::Device::Elantech::HandleKeyMessage(): Consumed"));
1282 return true; // consume the message (doesn't need to dispatch key events)
1285 // Version 8 of the Elantech touchpad driver sends these messages for
1286 // zoom gestures:
1288 // WM_KEYDOWN virtual_key = 0xCC time = 10
1289 // WM_KEYDOWN virtual_key = VK_CONTROL time = 10
1290 // WM_MOUSEWHEEL time = ::GetTickCount()
1291 // WM_KEYUP virtual_key = VK_CONTROL time = 10
1292 // WM_KEYUP virtual_key = 0xCC time = 10
1294 // The result of this is that we process all of the WM_KEYDOWN/WM_KEYUP
1295 // messages first because their timestamps make them appear to have
1296 // been sent before the WM_MOUSEWHEEL message. To work around this,
1297 // we store the current time when we process the WM_KEYUP message and
1298 // assume that any WM_MOUSEWHEEL message with a timestamp before that
1299 // time is one that should be processed as if the Control key was down.
1300 if (sUsePinchHack && aMsg == WM_KEYUP && aWParam == VK_CONTROL &&
1301 ::GetMessageTime() == 10) {
1302 // We look only at the bottom 31 bits of the system tick count since
1303 // GetMessageTime returns a LONG, which is signed, so we want values
1304 // that are more easily comparable.
1305 sZoomUntil = ::GetTickCount() & 0x7FFFFFFF;
1307 MOZ_LOG(
1308 gMouseScrollLog, LogLevel::Info,
1309 ("MouseScroll::Device::Elantech::HandleKeyMessage(): sZoomUntil=%lu",
1310 sZoomUntil));
1313 return false;
1316 /* static */
1317 void MouseScrollHandler::Device::Elantech::UpdateZoomUntil() {
1318 if (!sZoomUntil) {
1319 return;
1322 // For the Elantech Touchpad Zoom Gesture Hack, we should check that the
1323 // system time (32-bit milliseconds) hasn't wrapped around. Otherwise we
1324 // might get into the situation where wheel events for the next 50 days of
1325 // system uptime are assumed to be Ctrl+Wheel events. (It is unlikely that
1326 // we would get into that state, because the system would already need to be
1327 // up for 50 days and the Control key message would need to be processed just
1328 // before the system time overflow and the wheel message just after.)
1330 // We also take the chance to reset sZoomUntil if we simply have passed that
1331 // time.
1332 LONG msgTime = ::GetMessageTime();
1333 if ((sZoomUntil >= 0x3fffffffu && DWORD(msgTime) < 0x40000000u) ||
1334 (sZoomUntil < DWORD(msgTime))) {
1335 sZoomUntil = 0;
1337 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1338 ("MouseScroll::Device::Elantech::UpdateZoomUntil(): "
1339 "sZoomUntil was reset"));
1343 /* static */
1344 bool MouseScrollHandler::Device::Elantech::IsZooming() {
1345 // Assume the Control key is down if the Elantech touchpad has sent the
1346 // mis-ordered WM_KEYDOWN/WM_MOUSEWHEEL messages. (See the comment in
1347 // OnKeyUp.)
1348 return (sZoomUntil && static_cast<DWORD>(::GetMessageTime()) < sZoomUntil);
1351 /******************************************************************************
1353 * Device::Apoint
1355 ******************************************************************************/
1357 /* static */
1358 void MouseScrollHandler::Device::Apoint::Init() {
1359 if (sInitialized) {
1360 return;
1363 sInitialized = true;
1364 sMajorVersion = 0;
1365 sMinorVersion = -1;
1367 wchar_t buf[40];
1368 if (!WinRegistry::GetString(HKEY_LOCAL_MACHINE, u"Software\\Alps\\Apoint"_ns,
1369 u"ProductVer"_ns, buf,
1370 WinRegistry::kLegacyWinUtilsStringFlags)) {
1371 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1372 ("MouseScroll::Device::Apoint::Init(): "
1373 "Apoint driver is not found"));
1374 return;
1377 sMajorVersion = wcstol(buf, nullptr, 10);
1378 sMinorVersion = 0;
1379 wchar_t* p = wcschr(buf, L'.');
1380 if (p) {
1381 sMinorVersion = wcstol(p + 1, nullptr, 10);
1383 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1384 ("MouseScroll::Device::Apoint::Init(): "
1385 "found driver version = %d.%d",
1386 sMajorVersion, sMinorVersion));
1389 /******************************************************************************
1391 * Device::TrackPoint
1393 ******************************************************************************/
1395 /* static */
1396 bool MouseScrollHandler::Device::TrackPoint::IsDriverInstalled() {
1397 if (WinRegistry::HasKey(HKEY_CURRENT_USER,
1398 u"Software\\Lenovo\\TrackPoint"_ns)) {
1399 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1400 ("MouseScroll::Device::TrackPoint::IsDriverInstalled(): "
1401 "Lenovo's TrackPoint driver is found"));
1402 return true;
1405 if (WinRegistry::HasKey(HKEY_CURRENT_USER,
1406 u"Software\\Alps\\Apoint\\TrackPoint"_ns)) {
1407 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1408 ("MouseScroll::Device::TrackPoint::IsDriverInstalled(): "
1409 "Alps's TrackPoint driver is found"));
1410 return true;
1413 return false;
1416 /******************************************************************************
1418 * Device::UltraNav
1420 ******************************************************************************/
1422 /* static */
1423 bool MouseScrollHandler::Device::UltraNav::IsObsoleteDriverInstalled() {
1424 if (WinRegistry::HasKey(HKEY_CURRENT_USER,
1425 u"Software\\Lenovo\\UltraNav"_ns)) {
1426 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1427 ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
1428 "Lenovo's UltraNav driver is found"));
1429 return true;
1432 bool installed = false;
1433 if (WinRegistry::HasKey(HKEY_CURRENT_USER,
1434 u"Software\\Synaptics\\SynTPEnh\\UltraNavUSB"_ns)) {
1435 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1436 ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
1437 "Synaptics's UltraNav (USB) driver is found"));
1438 installed = true;
1439 } else if (WinRegistry::HasKey(
1440 HKEY_CURRENT_USER,
1441 u"Software\\Synaptics\\SynTPEnh\\UltraNavPS2"_ns)) {
1442 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1443 ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
1444 "Synaptics's UltraNav (PS/2) driver is found"));
1445 installed = true;
1448 if (!installed) {
1449 return false;
1452 int32_t majorVersion = Device::SynTP::GetDriverMajorVersion();
1453 if (!majorVersion) {
1454 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1455 ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
1456 "Failed to get UltraNav driver version"));
1457 return false;
1459 int32_t minorVersion = Device::SynTP::GetDriverMinorVersion();
1460 return majorVersion < 15 || (majorVersion == 15 && minorVersion == 0);
1463 /******************************************************************************
1465 * Device::SetPoint
1467 ******************************************************************************/
1469 /* static */
1470 bool MouseScrollHandler::Device::SetPoint::IsGetMessagePosResponseValid(
1471 UINT aMessage, WPARAM aWParam, LPARAM aLParam) {
1472 if (aMessage != WM_MOUSEHWHEEL) {
1473 return false;
1476 POINTS pts = MouseScrollHandler::GetCurrentMessagePos();
1477 LPARAM messagePos = MAKELPARAM(pts.x, pts.y);
1479 // XXX We should check whether SetPoint is installed or not by registry.
1481 // SetPoint, Logitech (Logicool) mouse driver, (confirmed with 4.82.11 and
1482 // MX-1100) always sets 0 to the lParam of WM_MOUSEHWHEEL. The driver SENDs
1483 // one message at first time, this time, ::GetMessagePos() works fine.
1484 // Then, we will return 0 (0 means we process it) to the message. Then, the
1485 // driver will POST the same messages continuously during the wheel tilted.
1486 // But ::GetMessagePos() API always returns (0, 0) for them, even if the
1487 // actual mouse cursor isn't 0,0. Therefore, we cannot trust the result of
1488 // ::GetMessagePos API if the sender is SetPoint.
1489 if (!sMightBeUsing && !aLParam && aLParam != messagePos &&
1490 ::InSendMessage()) {
1491 sMightBeUsing = true;
1492 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1493 ("MouseScroll::Device::SetPoint::IsGetMessagePosResponseValid(): "
1494 "Might using SetPoint"));
1495 } else if (sMightBeUsing && aLParam != 0 && ::InSendMessage()) {
1496 // The user has changed the mouse from Logitech's to another one (e.g.,
1497 // the user has changed to the touchpad of the notebook.
1498 sMightBeUsing = false;
1499 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1500 ("MouseScroll::Device::SetPoint::IsGetMessagePosResponseValid(): "
1501 "Might stop using SetPoint"));
1503 return (sMightBeUsing && !aLParam && !messagePos);
1506 /******************************************************************************
1508 * SynthesizingEvent
1510 ******************************************************************************/
1512 /* static */
1513 bool MouseScrollHandler::SynthesizingEvent::IsSynthesizing() {
1514 return MouseScrollHandler::sInstance &&
1515 MouseScrollHandler::sInstance->mSynthesizingEvent &&
1516 MouseScrollHandler::sInstance->mSynthesizingEvent->mStatus !=
1517 NOT_SYNTHESIZING;
1520 nsresult MouseScrollHandler::SynthesizingEvent::Synthesize(
1521 const POINTS& aCursorPoint, HWND aWnd, UINT aMessage, WPARAM aWParam,
1522 LPARAM aLParam, const BYTE (&aKeyStates)[256]) {
1523 MOZ_LOG(
1524 gMouseScrollLog, LogLevel::Info,
1525 ("MouseScrollHandler::SynthesizingEvent::Synthesize(): aCursorPoint: { "
1526 "x: %d, y: %d }, aWnd=0x%p, aMessage=0x%04X, aWParam=0x%08zX, "
1527 "aLParam=0x%08" PRIXLPTR ", IsSynthesized()=%s, mStatus=%s",
1528 aCursorPoint.x, aCursorPoint.y, aWnd, aMessage, aWParam, aLParam,
1529 GetBoolName(IsSynthesizing()), GetStatusName()));
1531 if (IsSynthesizing()) {
1532 return NS_ERROR_NOT_AVAILABLE;
1535 ::GetKeyboardState(mOriginalKeyState);
1537 // Note that we cannot use ::SetCursorPos() because it works asynchronously.
1538 // We should SEND the message for reducing the possibility of receiving
1539 // unexpected message which were not sent from here.
1540 mCursorPoint = aCursorPoint;
1542 mWnd = aWnd;
1543 mMessage = aMessage;
1544 mWParam = aWParam;
1545 mLParam = aLParam;
1547 memcpy(mKeyState, aKeyStates, sizeof(mKeyState));
1548 ::SetKeyboardState(mKeyState);
1550 mStatus = SENDING_MESSAGE;
1552 // Don't assume that aWnd is always managed by nsWindow. It might be
1553 // a plugin window.
1554 ::SendMessage(aWnd, aMessage, aWParam, aLParam);
1556 return NS_OK;
1559 void MouseScrollHandler::SynthesizingEvent::NativeMessageReceived(
1560 nsWindow* aWidget, UINT aMessage, WPARAM aWParam, LPARAM aLParam) {
1561 if (mStatus == SENDING_MESSAGE && mMessage == aMessage &&
1562 mWParam == aWParam && mLParam == aLParam) {
1563 mStatus = NATIVE_MESSAGE_RECEIVED;
1564 if (aWidget && aWidget->GetWindowHandle() == mWnd) {
1565 return;
1567 // Otherwise, the message may not be sent by us.
1570 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1571 ("MouseScrollHandler::SynthesizingEvent::NativeMessageReceived(): "
1572 "aWidget=%p, aWidget->GetWindowHandle()=0x%p, mWnd=0x%p, "
1573 "aMessage=0x%04X, aWParam=0x%08zX, aLParam=0x%08" PRIXLPTR
1574 ", mStatus=%s",
1575 aWidget, aWidget ? aWidget->GetWindowHandle() : nullptr, mWnd,
1576 aMessage, aWParam, aLParam, GetStatusName()));
1578 // We failed to receive our sent message, we failed to do the job.
1579 Finish();
1581 return;
1584 void MouseScrollHandler::SynthesizingEvent::
1585 NotifyNativeMessageHandlingFinished() {
1586 if (!IsSynthesizing()) {
1587 return;
1590 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1591 ("MouseScrollHandler::SynthesizingEvent::"
1592 "NotifyNativeMessageHandlingFinished(): IsWaitingInternalMessage=%s",
1593 GetBoolName(MouseScrollHandler::IsWaitingInternalMessage())));
1595 if (MouseScrollHandler::IsWaitingInternalMessage()) {
1596 mStatus = INTERNAL_MESSAGE_POSTED;
1597 return;
1600 // If the native message handler didn't post our internal message,
1601 // we our job is finished.
1602 // TODO: When we post the message to plugin window, there is remaning job.
1603 Finish();
1606 void MouseScrollHandler::SynthesizingEvent::
1607 NotifyInternalMessageHandlingFinished() {
1608 if (!IsSynthesizing()) {
1609 return;
1612 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1613 ("MouseScrollHandler::SynthesizingEvent::"
1614 "NotifyInternalMessageHandlingFinished()"));
1616 Finish();
1619 void MouseScrollHandler::SynthesizingEvent::Finish() {
1620 if (!IsSynthesizing()) {
1621 return;
1624 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1625 ("MouseScrollHandler::SynthesizingEvent::Finish()"));
1627 // Restore the original key state.
1628 ::SetKeyboardState(mOriginalKeyState);
1630 mStatus = NOT_SYNTHESIZING;
1633 } // namespace widget
1634 } // namespace mozilla