Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / widget / windows / WinMouseScrollHandler.cpp
blob89185fbf23a3dad722e51ecc4a4927233e48a633
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"
25 #include <psapi.h>
27 namespace mozilla {
28 namespace widget {
30 LazyLogModule gMouseScrollLog("MouseScrollHandlerWidgets");
32 static const char* GetBoolName(bool aBool) { return aBool ? "TRUE" : "FALSE"; }
34 MouseScrollHandler* MouseScrollHandler::sInstance = nullptr;
36 bool MouseScrollHandler::Device::sFakeScrollableWindowNeeded = false;
38 bool MouseScrollHandler::Device::SynTP::sInitialized = false;
39 int32_t MouseScrollHandler::Device::SynTP::sMajorVersion = 0;
40 int32_t MouseScrollHandler::Device::SynTP::sMinorVersion = -1;
42 bool MouseScrollHandler::Device::Elantech::sUseSwipeHack = false;
43 bool MouseScrollHandler::Device::Elantech::sUsePinchHack = false;
44 DWORD MouseScrollHandler::Device::Elantech::sZoomUntil = 0;
46 bool MouseScrollHandler::Device::Apoint::sInitialized = false;
47 int32_t MouseScrollHandler::Device::Apoint::sMajorVersion = 0;
48 int32_t MouseScrollHandler::Device::Apoint::sMinorVersion = -1;
50 bool MouseScrollHandler::Device::SetPoint::sMightBeUsing = false;
52 // The duration until timeout of events transaction. The value is 1.5 sec,
53 // it's just a magic number, it was suggested by Logitech's engineer, see
54 // bug 605648 comment 90.
55 #define DEFAULT_TIMEOUT_DURATION 1500
57 /******************************************************************************
59 * MouseScrollHandler
61 ******************************************************************************/
63 /* static */
64 POINTS
65 MouseScrollHandler::GetCurrentMessagePos() {
66 if (SynthesizingEvent::IsSynthesizing()) {
67 return sInstance->mSynthesizingEvent->GetCursorPoint();
69 DWORD pos = ::GetMessagePos();
70 return MAKEPOINTS(pos);
73 // Get rid of the GetMessagePos() API.
74 #define GetMessagePos()
76 /* static */
77 void MouseScrollHandler::Initialize() { Device::Init(); }
79 /* static */
80 void MouseScrollHandler::Shutdown() {
81 delete sInstance;
82 sInstance = nullptr;
85 /* static */
86 MouseScrollHandler* MouseScrollHandler::GetInstance() {
87 if (!sInstance) {
88 sInstance = new MouseScrollHandler();
90 return sInstance;
93 MouseScrollHandler::MouseScrollHandler()
94 : mIsWaitingInternalMessage(false), mSynthesizingEvent(nullptr) {
95 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
96 ("MouseScroll: Creating an instance, this=%p, sInstance=%p", this,
97 sInstance));
100 MouseScrollHandler::~MouseScrollHandler() {
101 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
102 ("MouseScroll: Destroying an instance, this=%p, sInstance=%p", this,
103 sInstance));
105 delete mSynthesizingEvent;
108 /* static */
109 void MouseScrollHandler::MaybeLogKeyState() {
110 if (!MOZ_LOG_TEST(gMouseScrollLog, LogLevel::Debug)) {
111 return;
113 BYTE keyboardState[256];
114 if (::GetKeyboardState(keyboardState)) {
115 for (size_t i = 0; i < ArrayLength(keyboardState); i++) {
116 if (keyboardState[i]) {
117 MOZ_LOG(gMouseScrollLog, LogLevel::Debug,
118 (" Current key state: keyboardState[0x%02zX]=0x%02X (%s)", i,
119 keyboardState[i],
120 ((keyboardState[i] & 0x81) == 0x81) ? "Pressed and Toggled"
121 : (keyboardState[i] & 0x80) ? "Pressed"
122 : (keyboardState[i] & 0x01) ? "Toggled"
123 : "Unknown"));
126 } else {
127 MOZ_LOG(
128 gMouseScrollLog, LogLevel::Debug,
129 ("MouseScroll::MaybeLogKeyState(): Failed to print current keyboard "
130 "state"));
134 /* static */
135 bool MouseScrollHandler::NeedsMessage(UINT aMsg) {
136 switch (aMsg) {
137 case WM_SETTINGCHANGE:
138 case WM_MOUSEWHEEL:
139 case WM_MOUSEHWHEEL:
140 case WM_HSCROLL:
141 case WM_VSCROLL:
142 case MOZ_WM_MOUSEVWHEEL:
143 case MOZ_WM_MOUSEHWHEEL:
144 case MOZ_WM_HSCROLL:
145 case MOZ_WM_VSCROLL:
146 case WM_KEYDOWN:
147 case WM_KEYUP:
148 return true;
150 return false;
153 /* static */
154 bool MouseScrollHandler::ProcessMessage(nsWindow* aWidget, UINT msg,
155 WPARAM wParam, LPARAM lParam,
156 MSGResult& aResult) {
157 Device::Elantech::UpdateZoomUntil();
159 switch (msg) {
160 case WM_SETTINGCHANGE:
161 if (!sInstance) {
162 return false;
164 if (wParam == SPI_SETWHEELSCROLLLINES ||
165 wParam == SPI_SETWHEELSCROLLCHARS) {
166 sInstance->mSystemSettings.MarkDirty();
168 return false;
170 case WM_MOUSEWHEEL:
171 case WM_MOUSEHWHEEL:
172 GetInstance()->ProcessNativeMouseWheelMessage(aWidget, msg, wParam,
173 lParam);
174 sInstance->mSynthesizingEvent->NotifyNativeMessageHandlingFinished();
175 // We don't need to call next wndproc for WM_MOUSEWHEEL and
176 // WM_MOUSEHWHEEL. We should consume them always. If the messages
177 // would be handled by our window again, it caused making infinite
178 // message loop.
179 aResult.mConsumed = true;
180 aResult.mResult = (msg != WM_MOUSEHWHEEL);
181 return true;
183 case WM_HSCROLL:
184 case WM_VSCROLL:
185 aResult.mConsumed = GetInstance()->ProcessNativeScrollMessage(
186 aWidget, msg, wParam, lParam);
187 sInstance->mSynthesizingEvent->NotifyNativeMessageHandlingFinished();
188 aResult.mResult = 0;
189 return true;
191 case MOZ_WM_MOUSEVWHEEL:
192 case MOZ_WM_MOUSEHWHEEL:
193 GetInstance()->HandleMouseWheelMessage(aWidget, msg, wParam, lParam);
194 sInstance->mSynthesizingEvent->NotifyInternalMessageHandlingFinished();
195 // Doesn't need to call next wndproc for internal wheel message.
196 aResult.mConsumed = true;
197 return true;
199 case MOZ_WM_HSCROLL:
200 case MOZ_WM_VSCROLL:
201 GetInstance()->HandleScrollMessageAsMouseWheelMessage(aWidget, msg,
202 wParam, lParam);
203 sInstance->mSynthesizingEvent->NotifyInternalMessageHandlingFinished();
204 // Doesn't need to call next wndproc for internal scroll message.
205 aResult.mConsumed = true;
206 return true;
208 case WM_KEYDOWN:
209 case WM_KEYUP:
210 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
211 ("MouseScroll::ProcessMessage(): aWidget=%p, "
212 "msg=%s(0x%04X), wParam=0x%02zX, ::GetMessageTime()=%ld",
213 aWidget,
214 msg == WM_KEYDOWN ? "WM_KEYDOWN"
215 : msg == WM_KEYUP ? "WM_KEYUP"
216 : "Unknown",
217 msg, wParam, ::GetMessageTime()));
218 MaybeLogKeyState();
219 if (Device::Elantech::HandleKeyMessage(aWidget, msg, wParam, lParam)) {
220 aResult.mResult = 0;
221 aResult.mConsumed = true;
222 return true;
224 return false;
226 default:
227 return false;
231 /* static */
232 nsresult MouseScrollHandler::SynthesizeNativeMouseScrollEvent(
233 nsWindow* aWidget, const LayoutDeviceIntPoint& aPoint,
234 uint32_t aNativeMessage, int32_t aDelta, uint32_t aModifierFlags,
235 uint32_t aAdditionalFlags) {
236 bool useFocusedWindow = !(
237 aAdditionalFlags & nsIDOMWindowUtils::MOUSESCROLL_PREFER_WIDGET_AT_POINT);
239 POINT pt;
240 pt.x = aPoint.x;
241 pt.y = aPoint.y;
243 HWND target = useFocusedWindow ? ::WindowFromPoint(pt) : ::GetFocus();
244 NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
246 WPARAM wParam = 0;
247 LPARAM lParam = 0;
248 switch (aNativeMessage) {
249 case WM_MOUSEWHEEL:
250 case WM_MOUSEHWHEEL: {
251 lParam = MAKELPARAM(pt.x, pt.y);
252 WORD mod = 0;
253 if (aModifierFlags & (nsIWidget::CTRL_L | nsIWidget::CTRL_R)) {
254 mod |= MK_CONTROL;
256 if (aModifierFlags & (nsIWidget::SHIFT_L | nsIWidget::SHIFT_R)) {
257 mod |= MK_SHIFT;
259 wParam = MAKEWPARAM(mod, aDelta);
260 break;
262 case WM_VSCROLL:
263 case WM_HSCROLL:
264 lParam = (aAdditionalFlags &
265 nsIDOMWindowUtils::MOUSESCROLL_WIN_SCROLL_LPARAM_NOT_NULL)
266 ? reinterpret_cast<LPARAM>(target)
267 : 0;
268 wParam = aDelta;
269 break;
270 default:
271 return NS_ERROR_INVALID_ARG;
274 // Ensure to make the instance.
275 GetInstance();
277 BYTE kbdState[256];
278 memset(kbdState, 0, sizeof(kbdState));
280 AutoTArray<KeyPair, 10> keySequence;
281 WinUtils::SetupKeyModifiersSequence(&keySequence, aModifierFlags,
282 aNativeMessage);
284 for (uint32_t i = 0; i < keySequence.Length(); ++i) {
285 uint8_t key = keySequence[i].mGeneral;
286 uint8_t keySpecific = keySequence[i].mSpecific;
287 kbdState[key] = 0x81; // key is down and toggled on if appropriate
288 if (keySpecific) {
289 kbdState[keySpecific] = 0x81;
293 if (!sInstance->mSynthesizingEvent) {
294 sInstance->mSynthesizingEvent = new SynthesizingEvent();
297 POINTS pts;
298 pts.x = static_cast<SHORT>(pt.x);
299 pts.y = static_cast<SHORT>(pt.y);
300 return sInstance->mSynthesizingEvent->Synthesize(pts, target, aNativeMessage,
301 wParam, lParam, kbdState);
304 /* static */
305 void MouseScrollHandler::InitEvent(nsWindow* aWidget, WidgetGUIEvent& aEvent,
306 LPARAM* aPoint) {
307 NS_ENSURE_TRUE_VOID(aWidget);
309 // If a point is provided, use it; otherwise, get current message point or
310 // synthetic point
311 POINTS pointOnScreen;
312 if (aPoint != nullptr) {
313 pointOnScreen = MAKEPOINTS(*aPoint);
314 } else {
315 pointOnScreen = GetCurrentMessagePos();
318 // InitEvent expects the point to be in window coordinates, so translate the
319 // point from screen coordinates.
320 POINT pointOnWindow;
321 POINTSTOPOINT(pointOnWindow, pointOnScreen);
322 ::ScreenToClient(aWidget->GetWindowHandle(), &pointOnWindow);
324 LayoutDeviceIntPoint point;
325 point.x = pointOnWindow.x;
326 point.y = pointOnWindow.y;
328 aWidget->InitEvent(aEvent, &point);
331 /* static */
332 ModifierKeyState MouseScrollHandler::GetModifierKeyState(UINT aMessage) {
333 ModifierKeyState result;
334 // Assume the Control key is down if the Elantech touchpad has sent the
335 // mis-ordered WM_KEYDOWN/WM_MOUSEWHEEL messages. (See the comment in
336 // MouseScrollHandler::Device::Elantech::HandleKeyMessage().)
337 if ((aMessage == MOZ_WM_MOUSEVWHEEL || aMessage == WM_MOUSEWHEEL) &&
338 !result.IsControl() && Device::Elantech::IsZooming()) {
339 // XXX Do we need to unset MODIFIER_SHIFT, MODIFIER_ALT, MODIFIER_META too?
340 // If one of them are true, the default action becomes not zooming.
341 result.Unset(MODIFIER_ALTGRAPH);
342 result.Set(MODIFIER_CONTROL);
344 return result;
347 POINT
348 MouseScrollHandler::ComputeMessagePos(UINT aMessage, WPARAM aWParam,
349 LPARAM aLParam) {
350 POINT point;
351 if (Device::SetPoint::IsGetMessagePosResponseValid(aMessage, aWParam,
352 aLParam)) {
353 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
354 ("MouseScroll::ComputeMessagePos: Using ::GetCursorPos()"));
355 ::GetCursorPos(&point);
356 } else {
357 POINTS pts = GetCurrentMessagePos();
358 point.x = pts.x;
359 point.y = pts.y;
361 return point;
364 void MouseScrollHandler::ProcessNativeMouseWheelMessage(nsWindow* aWidget,
365 UINT aMessage,
366 WPARAM aWParam,
367 LPARAM aLParam) {
368 if (SynthesizingEvent::IsSynthesizing()) {
369 mSynthesizingEvent->NativeMessageReceived(aWidget, aMessage, aWParam,
370 aLParam);
373 POINT point = ComputeMessagePos(aMessage, aWParam, aLParam);
375 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
376 ("MouseScroll::ProcessNativeMouseWheelMessage: aWidget=%p, "
377 "aMessage=%s, wParam=0x%08zX, lParam=0x%08" PRIXLPTR
378 ", point: { x=%ld, y=%ld }",
379 aWidget,
380 aMessage == WM_MOUSEWHEEL ? "WM_MOUSEWHEEL"
381 : aMessage == WM_MOUSEHWHEEL ? "WM_MOUSEHWHEEL"
382 : aMessage == WM_VSCROLL ? "WM_VSCROLL"
383 : "WM_HSCROLL",
384 aWParam, aLParam, point.x, point.y));
385 MaybeLogKeyState();
387 HWND underCursorWnd = ::WindowFromPoint(point);
388 if (!underCursorWnd) {
389 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
390 ("MouseScroll::ProcessNativeMouseWheelMessage: "
391 "No window is not found under the cursor"));
392 return;
395 if (Device::Elantech::IsPinchHackNeeded() &&
396 Device::Elantech::IsHelperWindow(underCursorWnd)) {
397 // The Elantech driver places a window right underneath the cursor
398 // when sending a WM_MOUSEWHEEL event to us as part of a pinch-to-zoom
399 // gesture. We detect that here, and search for our window that would
400 // be beneath the cursor if that window wasn't there.
401 underCursorWnd = WinUtils::FindOurWindowAtPoint(point);
402 if (!underCursorWnd) {
403 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
404 ("MouseScroll::ProcessNativeMouseWheelMessage: "
405 "Our window is not found under the Elantech helper window"));
406 return;
410 // Handle most cases first. If the window under mouse cursor is our window
411 // except plugin window (MozillaWindowClass), we should handle the message
412 // on the window.
413 if (WinUtils::IsOurProcessWindow(underCursorWnd)) {
414 nsWindow* destWindow = WinUtils::GetNSWindowPtr(underCursorWnd);
415 if (!destWindow) {
416 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
417 ("MouseScroll::ProcessNativeMouseWheelMessage: "
418 "Found window under the cursor isn't managed by nsWindow..."));
419 HWND wnd = ::GetParent(underCursorWnd);
420 for (; wnd; wnd = ::GetParent(wnd)) {
421 destWindow = WinUtils::GetNSWindowPtr(wnd);
422 if (destWindow) {
423 break;
426 if (!wnd) {
427 MOZ_LOG(
428 gMouseScrollLog, LogLevel::Info,
429 ("MouseScroll::ProcessNativeMouseWheelMessage: Our window which is "
430 "managed by nsWindow is not found under the cursor"));
431 return;
435 MOZ_ASSERT(destWindow, "destWindow must not be NULL");
437 // Some odd touchpad utils sets focus to window under the mouse cursor.
438 // this emulates the odd behavior for debug.
439 if (mUserPrefs.ShouldEmulateToMakeWindowUnderCursorForeground() &&
440 (aMessage == WM_MOUSEWHEEL || aMessage == WM_MOUSEHWHEEL) &&
441 ::GetForegroundWindow() != destWindow->GetWindowHandle()) {
442 ::SetForegroundWindow(destWindow->GetWindowHandle());
445 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
446 ("MouseScroll::ProcessNativeMouseWheelMessage: Succeeded, "
447 "Posting internal message to an nsWindow (%p)...",
448 destWindow));
449 mIsWaitingInternalMessage = true;
450 UINT internalMessage = WinUtils::GetInternalMessage(aMessage);
451 ::PostMessage(destWindow->GetWindowHandle(), internalMessage, aWParam,
452 aLParam);
453 return;
456 // If the window under cursor is not in our process, it means:
457 // 1. The window may be a plugin window (GeckoPluginWindow or its descendant).
458 // 2. The window may be another application's window.
459 HWND pluginWnd = WinUtils::FindOurProcessWindow(underCursorWnd);
460 if (!pluginWnd) {
461 // If there is no plugin window in ancestors of the window under cursor,
462 // the window is for another applications (case 2).
463 // We don't need to handle this message.
464 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
465 ("MouseScroll::ProcessNativeMouseWheelMessage: "
466 "Our window is not found under the cursor"));
467 return;
470 // If the window is a part of plugin, we should post the message to it.
471 MOZ_LOG(
472 gMouseScrollLog, LogLevel::Info,
473 ("MouseScroll::ProcessNativeMouseWheelMessage: Succeeded, "
474 "Redirecting the message to a window which is a plugin child window"));
475 ::PostMessage(underCursorWnd, aMessage, aWParam, aLParam);
478 bool MouseScrollHandler::ProcessNativeScrollMessage(nsWindow* aWidget,
479 UINT aMessage,
480 WPARAM aWParam,
481 LPARAM aLParam) {
482 if (aLParam || mUserPrefs.IsScrollMessageHandledAsWheelMessage()) {
483 // Scroll message generated by Thinkpad Trackpoint Driver or similar
484 // Treat as a mousewheel message and scroll appropriately
485 ProcessNativeMouseWheelMessage(aWidget, aMessage, aWParam, aLParam);
486 // Always consume the scroll message if we try to emulate mouse wheel
487 // action.
488 return true;
491 if (SynthesizingEvent::IsSynthesizing()) {
492 mSynthesizingEvent->NativeMessageReceived(aWidget, aMessage, aWParam,
493 aLParam);
496 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
497 ("MouseScroll::ProcessNativeScrollMessage: aWidget=%p, "
498 "aMessage=%s, wParam=0x%08zX, lParam=0x%08" PRIXLPTR,
499 aWidget, aMessage == WM_VSCROLL ? "WM_VSCROLL" : "WM_HSCROLL",
500 aWParam, aLParam));
502 // Scroll message generated by external application
503 WidgetContentCommandEvent commandEvent(true, eContentCommandScroll, aWidget);
504 commandEvent.mScroll.mIsHorizontal = (aMessage == WM_HSCROLL);
506 switch (LOWORD(aWParam)) {
507 case SB_LINEUP: // SB_LINELEFT
508 commandEvent.mScroll.mUnit =
509 WidgetContentCommandEvent::eCmdScrollUnit_Line;
510 commandEvent.mScroll.mAmount = -1;
511 break;
512 case SB_LINEDOWN: // SB_LINERIGHT
513 commandEvent.mScroll.mUnit =
514 WidgetContentCommandEvent::eCmdScrollUnit_Line;
515 commandEvent.mScroll.mAmount = 1;
516 break;
517 case SB_PAGEUP: // SB_PAGELEFT
518 commandEvent.mScroll.mUnit =
519 WidgetContentCommandEvent::eCmdScrollUnit_Page;
520 commandEvent.mScroll.mAmount = -1;
521 break;
522 case SB_PAGEDOWN: // SB_PAGERIGHT
523 commandEvent.mScroll.mUnit =
524 WidgetContentCommandEvent::eCmdScrollUnit_Page;
525 commandEvent.mScroll.mAmount = 1;
526 break;
527 case SB_TOP: // SB_LEFT
528 commandEvent.mScroll.mUnit =
529 WidgetContentCommandEvent::eCmdScrollUnit_Whole;
530 commandEvent.mScroll.mAmount = -1;
531 break;
532 case SB_BOTTOM: // SB_RIGHT
533 commandEvent.mScroll.mUnit =
534 WidgetContentCommandEvent::eCmdScrollUnit_Whole;
535 commandEvent.mScroll.mAmount = 1;
536 break;
537 default:
538 return false;
540 // XXX If this is a plugin window, we should dispatch the event from
541 // parent window.
542 aWidget->DispatchContentCommandEvent(&commandEvent);
543 return true;
546 void MouseScrollHandler::HandleMouseWheelMessage(nsWindow* aWidget,
547 UINT aMessage, WPARAM aWParam,
548 LPARAM aLParam) {
549 MOZ_ASSERT((aMessage == MOZ_WM_MOUSEVWHEEL || aMessage == MOZ_WM_MOUSEHWHEEL),
550 "HandleMouseWheelMessage must be called with "
551 "MOZ_WM_MOUSEVWHEEL or MOZ_WM_MOUSEHWHEEL");
553 MOZ_LOG(
554 gMouseScrollLog, LogLevel::Info,
555 ("MouseScroll::HandleMouseWheelMessage: aWidget=%p, "
556 "aMessage=MOZ_WM_MOUSE%sWHEEL, aWParam=0x%08zX, aLParam=0x%08" PRIXLPTR,
557 aWidget, aMessage == MOZ_WM_MOUSEVWHEEL ? "V" : "H", aWParam, aLParam));
559 mIsWaitingInternalMessage = false;
561 // If it's not allowed to cache system settings, we need to reset the cache
562 // before handling the mouse wheel message.
563 mSystemSettings.TrustedScrollSettingsDriver();
565 EventInfo eventInfo(aWidget, WinUtils::GetNativeMessage(aMessage), aWParam,
566 aLParam);
567 if (!eventInfo.CanDispatchWheelEvent()) {
568 MOZ_LOG(
569 gMouseScrollLog, LogLevel::Info,
570 ("MouseScroll::HandleMouseWheelMessage: Cannot dispatch the events"));
571 mLastEventInfo.ResetTransaction();
572 return;
575 // Discard the remaining delta if current wheel message and last one are
576 // received by different window or to scroll different direction or
577 // different unit scroll. Furthermore, if the last event was too old.
578 if (!mLastEventInfo.CanContinueTransaction(eventInfo)) {
579 mLastEventInfo.ResetTransaction();
582 mLastEventInfo.RecordEvent(eventInfo);
584 ModifierKeyState modKeyState = GetModifierKeyState(aMessage);
586 // Grab the widget, it might be destroyed by a DOM event handler.
587 RefPtr<nsWindow> kungFuDethGrip(aWidget);
589 WidgetWheelEvent wheelEvent(true, eWheel, aWidget);
590 if (mLastEventInfo.InitWheelEvent(aWidget, wheelEvent, modKeyState,
591 aLParam)) {
592 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
593 ("MouseScroll::HandleMouseWheelMessage: dispatching "
594 "eWheel event"));
595 aWidget->DispatchWheelEvent(&wheelEvent);
596 if (aWidget->Destroyed()) {
597 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
598 ("MouseScroll::HandleMouseWheelMessage: The window was destroyed "
599 "by eWheel event"));
600 mLastEventInfo.ResetTransaction();
601 return;
603 } else {
604 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
605 ("MouseScroll::HandleMouseWheelMessage: eWheel event is not "
606 "dispatched"));
610 void MouseScrollHandler::HandleScrollMessageAsMouseWheelMessage(
611 nsWindow* aWidget, UINT aMessage, WPARAM aWParam, LPARAM aLParam) {
612 MOZ_ASSERT((aMessage == MOZ_WM_VSCROLL || aMessage == MOZ_WM_HSCROLL),
613 "HandleScrollMessageAsMouseWheelMessage must be called with "
614 "MOZ_WM_VSCROLL or MOZ_WM_HSCROLL");
616 mIsWaitingInternalMessage = false;
618 ModifierKeyState modKeyState = GetModifierKeyState(aMessage);
620 WidgetWheelEvent wheelEvent(true, eWheel, aWidget);
621 double& delta =
622 (aMessage == MOZ_WM_VSCROLL) ? wheelEvent.mDeltaY : wheelEvent.mDeltaX;
623 int32_t& lineOrPageDelta = (aMessage == MOZ_WM_VSCROLL)
624 ? wheelEvent.mLineOrPageDeltaY
625 : wheelEvent.mLineOrPageDeltaX;
627 delta = 1.0;
628 lineOrPageDelta = 1;
630 switch (LOWORD(aWParam)) {
631 case SB_PAGEUP:
632 delta = -1.0;
633 lineOrPageDelta = -1;
634 [[fallthrough]];
635 case SB_PAGEDOWN:
636 wheelEvent.mDeltaMode = dom::WheelEvent_Binding::DOM_DELTA_PAGE;
637 break;
639 case SB_LINEUP:
640 delta = -1.0;
641 lineOrPageDelta = -1;
642 [[fallthrough]];
643 case SB_LINEDOWN:
644 wheelEvent.mDeltaMode = dom::WheelEvent_Binding::DOM_DELTA_LINE;
645 break;
647 default:
648 return;
650 modKeyState.InitInputEvent(wheelEvent);
652 // Current mouse position may not be same as when the original message
653 // is received. However, this data is not available with the original
654 // message, which is why nullptr is passed in. We need to know the actual
655 // mouse cursor position when the original message was received.
656 InitEvent(aWidget, wheelEvent, nullptr);
658 MOZ_LOG(
659 gMouseScrollLog, LogLevel::Info,
660 ("MouseScroll::HandleScrollMessageAsMouseWheelMessage: aWidget=%p, "
661 "aMessage=MOZ_WM_%sSCROLL, aWParam=0x%08zX, aLParam=0x%08" PRIXLPTR ", "
662 "wheelEvent { mRefPoint: { x: %d, y: %d }, mDeltaX: %f, mDeltaY: %f, "
663 "mLineOrPageDeltaX: %d, mLineOrPageDeltaY: %d, "
664 "isShift: %s, isControl: %s, isAlt: %s, isMeta: %s }",
665 aWidget, (aMessage == MOZ_WM_VSCROLL) ? "V" : "H", aWParam, aLParam,
666 wheelEvent.mRefPoint.x.value, wheelEvent.mRefPoint.y.value,
667 wheelEvent.mDeltaX, wheelEvent.mDeltaY, wheelEvent.mLineOrPageDeltaX,
668 wheelEvent.mLineOrPageDeltaY, GetBoolName(wheelEvent.IsShift()),
669 GetBoolName(wheelEvent.IsControl()), GetBoolName(wheelEvent.IsAlt()),
670 GetBoolName(wheelEvent.IsMeta())));
672 aWidget->DispatchWheelEvent(&wheelEvent);
675 /******************************************************************************
677 * EventInfo
679 ******************************************************************************/
681 MouseScrollHandler::EventInfo::EventInfo(nsWindow* aWidget, UINT aMessage,
682 WPARAM aWParam, LPARAM aLParam) {
683 MOZ_ASSERT(
684 aMessage == WM_MOUSEWHEEL || aMessage == WM_MOUSEHWHEEL,
685 "EventInfo must be initialized with WM_MOUSEWHEEL or WM_MOUSEHWHEEL");
687 MouseScrollHandler::GetInstance()->mSystemSettings.Init();
689 mIsVertical = (aMessage == WM_MOUSEWHEEL);
690 mIsPage =
691 MouseScrollHandler::sInstance->mSystemSettings.IsPageScroll(mIsVertical);
692 mDelta = (short)HIWORD(aWParam);
693 mWnd = aWidget->GetWindowHandle();
694 mTimeStamp = TimeStamp::Now();
697 bool MouseScrollHandler::EventInfo::CanDispatchWheelEvent() const {
698 if (!GetScrollAmount()) {
699 // XXX I think that we should dispatch mouse wheel events even if the
700 // operation will not scroll because the wheel operation really happened
701 // and web application may want to handle the event for non-scroll action.
702 return false;
705 return (mDelta != 0);
708 int32_t MouseScrollHandler::EventInfo::GetScrollAmount() const {
709 if (mIsPage) {
710 return 1;
712 return MouseScrollHandler::sInstance->mSystemSettings.GetScrollAmount(
713 mIsVertical);
716 /******************************************************************************
718 * LastEventInfo
720 ******************************************************************************/
722 bool MouseScrollHandler::LastEventInfo::CanContinueTransaction(
723 const EventInfo& aNewEvent) {
724 int32_t timeout = MouseScrollHandler::sInstance->mUserPrefs
725 .GetMouseScrollTransactionTimeout();
726 return !mWnd ||
727 (mWnd == aNewEvent.GetWindowHandle() &&
728 IsPositive() == aNewEvent.IsPositive() &&
729 mIsVertical == aNewEvent.IsVertical() &&
730 mIsPage == aNewEvent.IsPage() &&
731 (timeout < 0 || TimeStamp::Now() - mTimeStamp <=
732 TimeDuration::FromMilliseconds(timeout)));
735 void MouseScrollHandler::LastEventInfo::ResetTransaction() {
736 if (!mWnd) {
737 return;
740 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
741 ("MouseScroll::LastEventInfo::ResetTransaction()"));
743 mWnd = nullptr;
744 mAccumulatedDelta = 0;
747 void MouseScrollHandler::LastEventInfo::RecordEvent(const EventInfo& aEvent) {
748 mWnd = aEvent.GetWindowHandle();
749 mDelta = aEvent.GetNativeDelta();
750 mIsVertical = aEvent.IsVertical();
751 mIsPage = aEvent.IsPage();
752 mTimeStamp = TimeStamp::Now();
755 /* static */
756 int32_t MouseScrollHandler::LastEventInfo::RoundDelta(double aDelta) {
757 return (aDelta >= 0) ? (int32_t)floor(aDelta) : (int32_t)ceil(aDelta);
760 bool MouseScrollHandler::LastEventInfo::InitWheelEvent(
761 nsWindow* aWidget, WidgetWheelEvent& aWheelEvent,
762 const ModifierKeyState& aModKeyState, LPARAM aLParam) {
763 MOZ_ASSERT(aWheelEvent.mMessage == eWheel);
765 if (StaticPrefs::mousewheel_ignore_cursor_position_in_lparam()) {
766 InitEvent(aWidget, aWheelEvent, nullptr);
767 } else {
768 InitEvent(aWidget, aWheelEvent, &aLParam);
771 aModKeyState.InitInputEvent(aWheelEvent);
773 // Our positive delta value means to bottom or right.
774 // But positive native delta value means to top or right.
775 // Use orienter for computing our delta value with native delta value.
776 int32_t orienter = mIsVertical ? -1 : 1;
778 aWheelEvent.mDeltaMode = mIsPage ? dom::WheelEvent_Binding::DOM_DELTA_PAGE
779 : dom::WheelEvent_Binding::DOM_DELTA_LINE;
781 double ticks = double(mDelta) * orienter / double(WHEEL_DELTA);
782 if (mIsVertical) {
783 aWheelEvent.mWheelTicksY = ticks;
784 } else {
785 aWheelEvent.mWheelTicksX = ticks;
788 double& delta = mIsVertical ? aWheelEvent.mDeltaY : aWheelEvent.mDeltaX;
789 int32_t& lineOrPageDelta = mIsVertical ? aWheelEvent.mLineOrPageDeltaY
790 : aWheelEvent.mLineOrPageDeltaX;
792 double nativeDeltaPerUnit =
793 mIsPage ? double(WHEEL_DELTA) : double(WHEEL_DELTA) / GetScrollAmount();
795 delta = double(mDelta) * orienter / nativeDeltaPerUnit;
796 mAccumulatedDelta += mDelta;
797 lineOrPageDelta =
798 mAccumulatedDelta * orienter / RoundDelta(nativeDeltaPerUnit);
799 mAccumulatedDelta -=
800 lineOrPageDelta * orienter * RoundDelta(nativeDeltaPerUnit);
802 if (aWheelEvent.mDeltaMode != dom::WheelEvent_Binding::DOM_DELTA_LINE) {
803 // If the scroll delta mode isn't per line scroll, we shouldn't allow to
804 // override the system scroll speed setting.
805 aWheelEvent.mAllowToOverrideSystemScrollSpeed = false;
808 MOZ_LOG(
809 gMouseScrollLog, LogLevel::Info,
810 ("MouseScroll::LastEventInfo::InitWheelEvent: aWidget=%p, "
811 "aWheelEvent { mRefPoint: { x: %d, y: %d }, mDeltaX: %f, mDeltaY: %f, "
812 "mLineOrPageDeltaX: %d, mLineOrPageDeltaY: %d, "
813 "isShift: %s, isControl: %s, isAlt: %s, isMeta: %s, "
814 "mAllowToOverrideSystemScrollSpeed: %s }, "
815 "mAccumulatedDelta: %d",
816 aWidget, aWheelEvent.mRefPoint.x.value, aWheelEvent.mRefPoint.y.value,
817 aWheelEvent.mDeltaX, aWheelEvent.mDeltaY, aWheelEvent.mLineOrPageDeltaX,
818 aWheelEvent.mLineOrPageDeltaY, GetBoolName(aWheelEvent.IsShift()),
819 GetBoolName(aWheelEvent.IsControl()), GetBoolName(aWheelEvent.IsAlt()),
820 GetBoolName(aWheelEvent.IsMeta()),
821 GetBoolName(aWheelEvent.mAllowToOverrideSystemScrollSpeed),
822 mAccumulatedDelta));
824 return (delta != 0);
827 /******************************************************************************
829 * SystemSettings
831 ******************************************************************************/
833 void MouseScrollHandler::SystemSettings::Init() {
834 if (mInitialized) {
835 return;
838 InitScrollLines();
839 InitScrollChars();
841 mInitialized = true;
843 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
844 ("MouseScroll::SystemSettings::Init(): initialized, "
845 "mScrollLines=%d, mScrollChars=%d",
846 mScrollLines, mScrollChars));
849 bool MouseScrollHandler::SystemSettings::InitScrollLines() {
850 int32_t oldValue = mInitialized ? mScrollLines : 0;
851 mIsReliableScrollLines = false;
852 mScrollLines = MouseScrollHandler::sInstance->mUserPrefs
853 .GetOverriddenVerticalScrollAmout();
854 if (mScrollLines >= 0) {
855 // overridden by the pref.
856 mIsReliableScrollLines = true;
857 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
858 ("MouseScroll::SystemSettings::InitScrollLines(): mScrollLines is "
859 "overridden by the pref: %d",
860 mScrollLines));
861 } else if (!::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &mScrollLines,
862 0)) {
863 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
864 ("MouseScroll::SystemSettings::InitScrollLines(): "
865 "::SystemParametersInfo("
866 "SPI_GETWHEELSCROLLLINES) failed"));
867 mScrollLines = DefaultScrollLines();
870 if (mScrollLines > WHEEL_DELTA) {
871 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
872 ("MouseScroll::SystemSettings::InitScrollLines(): the result of "
873 "::SystemParametersInfo(SPI_GETWHEELSCROLLLINES) is too large: %d",
874 mScrollLines));
875 // sScrollLines usually equals 3 or 0 (for no scrolling)
876 // However, if sScrollLines > WHEEL_DELTA, we assume that
877 // the mouse driver wants a page scroll. The docs state that
878 // sScrollLines should explicitly equal WHEEL_PAGESCROLL, but
879 // since some mouse drivers use an arbitrary large number instead,
880 // we have to handle that as well.
881 mScrollLines = WHEEL_PAGESCROLL;
884 return oldValue != mScrollLines;
887 bool MouseScrollHandler::SystemSettings::InitScrollChars() {
888 int32_t oldValue = mInitialized ? mScrollChars : 0;
889 mIsReliableScrollChars = false;
890 mScrollChars = MouseScrollHandler::sInstance->mUserPrefs
891 .GetOverriddenHorizontalScrollAmout();
892 if (mScrollChars >= 0) {
893 // overridden by the pref.
894 mIsReliableScrollChars = true;
895 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
896 ("MouseScroll::SystemSettings::InitScrollChars(): mScrollChars is "
897 "overridden by the pref: %d",
898 mScrollChars));
899 } else if (!::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &mScrollChars,
900 0)) {
901 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
902 ("MouseScroll::SystemSettings::InitScrollChars(): "
903 "::SystemParametersInfo("
904 "SPI_GETWHEELSCROLLCHARS) failed, this is unexpected on Vista or "
905 "later"));
906 // XXX Should we use DefaultScrollChars()?
907 mScrollChars = 1;
910 if (mScrollChars > WHEEL_DELTA) {
911 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
912 ("MouseScroll::SystemSettings::InitScrollChars(): the result of "
913 "::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS) is too large: %d",
914 mScrollChars));
915 // See the comments for the case mScrollLines > WHEEL_DELTA.
916 mScrollChars = WHEEL_PAGESCROLL;
919 return oldValue != mScrollChars;
922 void MouseScrollHandler::SystemSettings::MarkDirty() {
923 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
924 ("MouseScrollHandler::SystemSettings::MarkDirty(): "
925 "Marking SystemSettings dirty"));
926 mInitialized = false;
927 // When system settings are changed, we should reset current transaction.
928 MOZ_ASSERT(sInstance,
929 "Must not be called at initializing MouseScrollHandler");
930 MouseScrollHandler::sInstance->mLastEventInfo.ResetTransaction();
933 void MouseScrollHandler::SystemSettings::RefreshCache() {
934 bool isChanged = InitScrollLines();
935 isChanged = InitScrollChars() || isChanged;
936 if (!isChanged) {
937 return;
939 // If the scroll amount is changed, we should reset current transaction.
940 MOZ_ASSERT(sInstance,
941 "Must not be called at initializing MouseScrollHandler");
942 MouseScrollHandler::sInstance->mLastEventInfo.ResetTransaction();
945 void MouseScrollHandler::SystemSettings::TrustedScrollSettingsDriver() {
946 if (!mInitialized) {
947 return;
950 // if the cache is initialized with prefs, we don't need to refresh it.
951 if (mIsReliableScrollLines && mIsReliableScrollChars) {
952 return;
955 MouseScrollHandler::UserPrefs& userPrefs =
956 MouseScrollHandler::sInstance->mUserPrefs;
958 // If system settings cache is disabled, we should always refresh them.
959 if (!userPrefs.IsSystemSettingCacheEnabled()) {
960 RefreshCache();
961 return;
964 // If pref is set to as "always trust the cache", we shouldn't refresh them
965 // in any environments.
966 if (userPrefs.IsSystemSettingCacheForciblyEnabled()) {
967 return;
970 // If SynTP of Synaptics or Apoint of Alps is installed, it may hook
971 // ::SystemParametersInfo() and returns different value from system settings.
972 if (Device::SynTP::IsDriverInstalled() ||
973 Device::Apoint::IsDriverInstalled()) {
974 RefreshCache();
975 return;
978 // XXX We're not sure about other touchpad drivers...
981 /******************************************************************************
983 * UserPrefs
985 ******************************************************************************/
987 MouseScrollHandler::UserPrefs::UserPrefs() : mInitialized(false) {
988 // We need to reset mouse wheel transaction when all of mousewheel related
989 // prefs are changed.
990 DebugOnly<nsresult> rv =
991 Preferences::RegisterPrefixCallback(OnChange, "mousewheel.", this);
992 MOZ_ASSERT(NS_SUCCEEDED(rv), "Failed to register callback for mousewheel.");
995 MouseScrollHandler::UserPrefs::~UserPrefs() {
996 DebugOnly<nsresult> rv =
997 Preferences::UnregisterPrefixCallback(OnChange, "mousewheel.", this);
998 MOZ_ASSERT(NS_SUCCEEDED(rv), "Failed to unregister callback for mousewheel.");
1001 void MouseScrollHandler::UserPrefs::Init() {
1002 if (mInitialized) {
1003 return;
1006 mInitialized = true;
1008 mScrollMessageHandledAsWheelMessage =
1009 Preferences::GetBool("mousewheel.emulate_at_wm_scroll", false);
1010 mEnableSystemSettingCache =
1011 Preferences::GetBool("mousewheel.system_settings_cache.enabled", true);
1012 mForceEnableSystemSettingCache = Preferences::GetBool(
1013 "mousewheel.system_settings_cache.force_enabled", false);
1014 mEmulateToMakeWindowUnderCursorForeground = Preferences::GetBool(
1015 "mousewheel.debug.make_window_under_cursor_foreground", false);
1016 mOverriddenVerticalScrollAmount =
1017 Preferences::GetInt("mousewheel.windows.vertical_amount_override", -1);
1018 mOverriddenHorizontalScrollAmount =
1019 Preferences::GetInt("mousewheel.windows.horizontal_amount_override", -1);
1020 mMouseScrollTransactionTimeout = Preferences::GetInt(
1021 "mousewheel.windows.transaction.timeout", DEFAULT_TIMEOUT_DURATION);
1023 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1024 ("MouseScroll::UserPrefs::Init(): initialized, "
1025 "mScrollMessageHandledAsWheelMessage=%s, "
1026 "mEnableSystemSettingCache=%s, "
1027 "mForceEnableSystemSettingCache=%s, "
1028 "mEmulateToMakeWindowUnderCursorForeground=%s, "
1029 "mOverriddenVerticalScrollAmount=%d, "
1030 "mOverriddenHorizontalScrollAmount=%d, "
1031 "mMouseScrollTransactionTimeout=%d",
1032 GetBoolName(mScrollMessageHandledAsWheelMessage),
1033 GetBoolName(mEnableSystemSettingCache),
1034 GetBoolName(mForceEnableSystemSettingCache),
1035 GetBoolName(mEmulateToMakeWindowUnderCursorForeground),
1036 mOverriddenVerticalScrollAmount, mOverriddenHorizontalScrollAmount,
1037 mMouseScrollTransactionTimeout));
1040 void MouseScrollHandler::UserPrefs::MarkDirty() {
1041 MOZ_LOG(
1042 gMouseScrollLog, LogLevel::Info,
1043 ("MouseScrollHandler::UserPrefs::MarkDirty(): Marking UserPrefs dirty"));
1044 mInitialized = false;
1045 // Some prefs might override system settings, so, we should mark them dirty.
1046 MouseScrollHandler::sInstance->mSystemSettings.MarkDirty();
1047 // When user prefs for mousewheel are changed, we should reset current
1048 // transaction.
1049 MOZ_ASSERT(sInstance,
1050 "Must not be called at initializing MouseScrollHandler");
1051 MouseScrollHandler::sInstance->mLastEventInfo.ResetTransaction();
1054 /******************************************************************************
1056 * Device
1058 ******************************************************************************/
1060 /* static */
1061 bool MouseScrollHandler::Device::GetWorkaroundPref(const char* aPrefName,
1062 bool aValueIfAutomatic) {
1063 if (!aPrefName) {
1064 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1065 ("MouseScroll::Device::GetWorkaroundPref(): Failed, aPrefName is "
1066 "NULL"));
1067 return aValueIfAutomatic;
1070 int32_t lHackValue = 0;
1071 if (NS_FAILED(Preferences::GetInt(aPrefName, &lHackValue))) {
1072 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1073 ("MouseScroll::Device::GetWorkaroundPref(): Preferences::GetInt() "
1074 "failed,"
1075 " aPrefName=\"%s\", aValueIfAutomatic=%s",
1076 aPrefName, GetBoolName(aValueIfAutomatic)));
1077 return aValueIfAutomatic;
1080 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1081 ("MouseScroll::Device::GetWorkaroundPref(): Succeeded, "
1082 "aPrefName=\"%s\", aValueIfAutomatic=%s, lHackValue=%d",
1083 aPrefName, GetBoolName(aValueIfAutomatic), lHackValue));
1085 switch (lHackValue) {
1086 case 0: // disabled
1087 return false;
1088 case 1: // enabled
1089 return true;
1090 default: // -1: autodetect
1091 return aValueIfAutomatic;
1095 /* static */
1096 void MouseScrollHandler::Device::Init() {
1097 // FYI: Thinkpad's TrackPoint is Apoint of Alps and UltraNav is SynTP of
1098 // Synaptics. So, those drivers' information should be initialized
1099 // before calling methods of TrackPoint and UltraNav.
1100 SynTP::Init();
1101 Elantech::Init();
1102 Apoint::Init();
1104 sFakeScrollableWindowNeeded = GetWorkaroundPref(
1105 "ui.trackpoint_hack.enabled", (TrackPoint::IsDriverInstalled() ||
1106 UltraNav::IsObsoleteDriverInstalled()));
1108 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1109 ("MouseScroll::Device::Init(): sFakeScrollableWindowNeeded=%s",
1110 GetBoolName(sFakeScrollableWindowNeeded)));
1113 /******************************************************************************
1115 * Device::SynTP
1117 ******************************************************************************/
1119 /* static */
1120 void MouseScrollHandler::Device::SynTP::Init() {
1121 if (sInitialized) {
1122 return;
1125 sInitialized = true;
1126 sMajorVersion = 0;
1127 sMinorVersion = -1;
1129 wchar_t buf[40];
1130 bool foundKey = WinUtils::GetRegistryKey(
1131 HKEY_LOCAL_MACHINE, L"Software\\Synaptics\\SynTP\\Install",
1132 L"DriverVersion", buf, sizeof buf);
1133 if (!foundKey) {
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 bool foundKey = WinUtils::GetRegistryKey(HKEY_CURRENT_USER,
1178 L"Software\\Elantech\\MainOption",
1179 L"DriverVersion", buf, sizeof buf);
1180 if (!foundKey) {
1181 foundKey =
1182 WinUtils::GetRegistryKey(HKEY_CURRENT_USER, L"Software\\Elantech",
1183 L"DriverVersion", buf, sizeof buf);
1186 if (!foundKey) {
1187 return 0;
1190 // Assume that the major version number can be found just after a space
1191 // or at the start of the string.
1192 for (wchar_t* p = buf; *p; p++) {
1193 if (*p >= L'0' && *p <= L'9' && (p == buf || *(p - 1) == L' ')) {
1194 return wcstol(p, nullptr, 10);
1198 return 0;
1201 /* static */
1202 bool MouseScrollHandler::Device::Elantech::IsHelperWindow(HWND aWnd) {
1203 // The helper window cannot be distinguished based on its window class, so we
1204 // need to check if it is owned by the helper process, ETDCtrl.exe.
1206 const wchar_t* filenameSuffix = L"\\etdctrl.exe";
1207 const int filenameSuffixLength = 12;
1209 DWORD pid;
1210 ::GetWindowThreadProcessId(aWnd, &pid);
1212 HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
1213 if (!hProcess) {
1214 return false;
1217 bool result = false;
1218 wchar_t path[256] = {L'\0'};
1219 if (::GetProcessImageFileNameW(hProcess, path, ArrayLength(path))) {
1220 int pathLength = lstrlenW(path);
1221 if (pathLength >= filenameSuffixLength) {
1222 if (lstrcmpiW(path + pathLength - filenameSuffixLength, filenameSuffix) ==
1223 0) {
1224 result = true;
1228 ::CloseHandle(hProcess);
1230 return result;
1233 /* static */
1234 bool MouseScrollHandler::Device::Elantech::HandleKeyMessage(nsWindow* aWidget,
1235 UINT aMsg,
1236 WPARAM aWParam,
1237 LPARAM aLParam) {
1238 // The Elantech touchpad driver understands three-finger swipe left and
1239 // right gestures, and translates them into Page Up and Page Down key
1240 // events for most applications. For Firefox 3.6, it instead sends
1241 // Alt+Left and Alt+Right to trigger browser back/forward actions. As
1242 // with the Thinkpad Driver hack in nsWindow::Create, the change in
1243 // HWND structure makes Firefox not trigger the driver's heuristics
1244 // any longer.
1246 // The Elantech driver actually sends these messages for a three-finger
1247 // swipe right:
1249 // WM_KEYDOWN virtual_key = 0xCC or 0xFF ScanCode = 00
1250 // WM_KEYDOWN virtual_key = VK_NEXT ScanCode = 00
1251 // WM_KEYUP virtual_key = VK_NEXT ScanCode = 00
1252 // WM_KEYUP virtual_key = 0xCC or 0xFF ScanCode = 00
1254 // Whether 0xCC or 0xFF is sent is suspected to depend on the driver
1255 // version. 7.0.4.12_14Jul09_WHQL, 7.0.5.10, and 7.0.6.0 generate 0xCC.
1256 // 7.0.4.3 from Asus on EeePC generates 0xFF.
1258 // On some hardware, IS_VK_DOWN(0xFF) returns true even when Elantech
1259 // messages are not involved, meaning that alone is not enough to
1260 // distinguish the gesture from a regular Page Up or Page Down key press.
1261 // The ScanCode is therefore also tested to detect the gesture.
1262 // We then pretend that we should dispatch "Go Forward" command. Similarly
1263 // for VK_PRIOR and "Go Back" command.
1264 if (sUseSwipeHack && (aWParam == VK_NEXT || aWParam == VK_PRIOR) &&
1265 WinUtils::GetScanCode(aLParam) == 0 &&
1266 (IS_VK_DOWN(0xFF) || IS_VK_DOWN(0xCC))) {
1267 if (aMsg == WM_KEYDOWN) {
1268 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1269 ("MouseScroll::Device::Elantech::HandleKeyMessage(): Dispatching "
1270 "%s command event",
1271 aWParam == VK_NEXT ? "Forward" : "Back"));
1273 WidgetCommandEvent appCommandEvent(
1274 true, (aWParam == VK_NEXT) ? nsGkAtoms::Forward : nsGkAtoms::Back,
1275 aWidget);
1277 // In this scenario, the coordinate of the event isn't supplied, so pass
1278 // nullptr as an argument to indicate using the coordinate from the last
1279 // available window message.
1280 InitEvent(aWidget, appCommandEvent, nullptr);
1281 aWidget->DispatchWindowEvent(appCommandEvent);
1282 } else {
1283 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1284 ("MouseScroll::Device::Elantech::HandleKeyMessage(): Consumed"));
1286 return true; // consume the message (doesn't need to dispatch key events)
1289 // Version 8 of the Elantech touchpad driver sends these messages for
1290 // zoom gestures:
1292 // WM_KEYDOWN virtual_key = 0xCC time = 10
1293 // WM_KEYDOWN virtual_key = VK_CONTROL time = 10
1294 // WM_MOUSEWHEEL time = ::GetTickCount()
1295 // WM_KEYUP virtual_key = VK_CONTROL time = 10
1296 // WM_KEYUP virtual_key = 0xCC time = 10
1298 // The result of this is that we process all of the WM_KEYDOWN/WM_KEYUP
1299 // messages first because their timestamps make them appear to have
1300 // been sent before the WM_MOUSEWHEEL message. To work around this,
1301 // we store the current time when we process the WM_KEYUP message and
1302 // assume that any WM_MOUSEWHEEL message with a timestamp before that
1303 // time is one that should be processed as if the Control key was down.
1304 if (sUsePinchHack && aMsg == WM_KEYUP && aWParam == VK_CONTROL &&
1305 ::GetMessageTime() == 10) {
1306 // We look only at the bottom 31 bits of the system tick count since
1307 // GetMessageTime returns a LONG, which is signed, so we want values
1308 // that are more easily comparable.
1309 sZoomUntil = ::GetTickCount() & 0x7FFFFFFF;
1311 MOZ_LOG(
1312 gMouseScrollLog, LogLevel::Info,
1313 ("MouseScroll::Device::Elantech::HandleKeyMessage(): sZoomUntil=%lu",
1314 sZoomUntil));
1317 return false;
1320 /* static */
1321 void MouseScrollHandler::Device::Elantech::UpdateZoomUntil() {
1322 if (!sZoomUntil) {
1323 return;
1326 // For the Elantech Touchpad Zoom Gesture Hack, we should check that the
1327 // system time (32-bit milliseconds) hasn't wrapped around. Otherwise we
1328 // might get into the situation where wheel events for the next 50 days of
1329 // system uptime are assumed to be Ctrl+Wheel events. (It is unlikely that
1330 // we would get into that state, because the system would already need to be
1331 // up for 50 days and the Control key message would need to be processed just
1332 // before the system time overflow and the wheel message just after.)
1334 // We also take the chance to reset sZoomUntil if we simply have passed that
1335 // time.
1336 LONG msgTime = ::GetMessageTime();
1337 if ((sZoomUntil >= 0x3fffffffu && DWORD(msgTime) < 0x40000000u) ||
1338 (sZoomUntil < DWORD(msgTime))) {
1339 sZoomUntil = 0;
1341 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1342 ("MouseScroll::Device::Elantech::UpdateZoomUntil(): "
1343 "sZoomUntil was reset"));
1347 /* static */
1348 bool MouseScrollHandler::Device::Elantech::IsZooming() {
1349 // Assume the Control key is down if the Elantech touchpad has sent the
1350 // mis-ordered WM_KEYDOWN/WM_MOUSEWHEEL messages. (See the comment in
1351 // OnKeyUp.)
1352 return (sZoomUntil && static_cast<DWORD>(::GetMessageTime()) < sZoomUntil);
1355 /******************************************************************************
1357 * Device::Apoint
1359 ******************************************************************************/
1361 /* static */
1362 void MouseScrollHandler::Device::Apoint::Init() {
1363 if (sInitialized) {
1364 return;
1367 sInitialized = true;
1368 sMajorVersion = 0;
1369 sMinorVersion = -1;
1371 wchar_t buf[40];
1372 bool foundKey =
1373 WinUtils::GetRegistryKey(HKEY_LOCAL_MACHINE, L"Software\\Alps\\Apoint",
1374 L"ProductVer", buf, sizeof buf);
1375 if (!foundKey) {
1376 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1377 ("MouseScroll::Device::Apoint::Init(): "
1378 "Apoint driver is not found"));
1379 return;
1382 sMajorVersion = wcstol(buf, nullptr, 10);
1383 sMinorVersion = 0;
1384 wchar_t* p = wcschr(buf, L'.');
1385 if (p) {
1386 sMinorVersion = wcstol(p + 1, nullptr, 10);
1388 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1389 ("MouseScroll::Device::Apoint::Init(): "
1390 "found driver version = %d.%d",
1391 sMajorVersion, sMinorVersion));
1394 /******************************************************************************
1396 * Device::TrackPoint
1398 ******************************************************************************/
1400 /* static */
1401 bool MouseScrollHandler::Device::TrackPoint::IsDriverInstalled() {
1402 if (WinUtils::HasRegistryKey(HKEY_CURRENT_USER,
1403 L"Software\\Lenovo\\TrackPoint")) {
1404 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1405 ("MouseScroll::Device::TrackPoint::IsDriverInstalled(): "
1406 "Lenovo's TrackPoint driver is found"));
1407 return true;
1410 if (WinUtils::HasRegistryKey(HKEY_CURRENT_USER,
1411 L"Software\\Alps\\Apoint\\TrackPoint")) {
1412 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1413 ("MouseScroll::Device::TrackPoint::IsDriverInstalled(): "
1414 "Alps's TrackPoint driver is found"));
1417 return false;
1420 /******************************************************************************
1422 * Device::UltraNav
1424 ******************************************************************************/
1426 /* static */
1427 bool MouseScrollHandler::Device::UltraNav::IsObsoleteDriverInstalled() {
1428 if (WinUtils::HasRegistryKey(HKEY_CURRENT_USER,
1429 L"Software\\Lenovo\\UltraNav")) {
1430 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1431 ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
1432 "Lenovo's UltraNav driver is found"));
1433 return true;
1436 bool installed = false;
1437 if (WinUtils::HasRegistryKey(HKEY_CURRENT_USER,
1438 L"Software\\Synaptics\\SynTPEnh\\UltraNavUSB")) {
1439 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1440 ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
1441 "Synaptics's UltraNav (USB) driver is found"));
1442 installed = true;
1443 } else if (WinUtils::HasRegistryKey(
1444 HKEY_CURRENT_USER,
1445 L"Software\\Synaptics\\SynTPEnh\\UltraNavPS2")) {
1446 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1447 ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
1448 "Synaptics's UltraNav (PS/2) driver is found"));
1449 installed = true;
1452 if (!installed) {
1453 return false;
1456 int32_t majorVersion = Device::SynTP::GetDriverMajorVersion();
1457 if (!majorVersion) {
1458 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1459 ("MouseScroll::Device::UltraNav::IsObsoleteDriverInstalled(): "
1460 "Failed to get UltraNav driver version"));
1461 return false;
1463 int32_t minorVersion = Device::SynTP::GetDriverMinorVersion();
1464 return majorVersion < 15 || (majorVersion == 15 && minorVersion == 0);
1467 /******************************************************************************
1469 * Device::SetPoint
1471 ******************************************************************************/
1473 /* static */
1474 bool MouseScrollHandler::Device::SetPoint::IsGetMessagePosResponseValid(
1475 UINT aMessage, WPARAM aWParam, LPARAM aLParam) {
1476 if (aMessage != WM_MOUSEHWHEEL) {
1477 return false;
1480 POINTS pts = MouseScrollHandler::GetCurrentMessagePos();
1481 LPARAM messagePos = MAKELPARAM(pts.x, pts.y);
1483 // XXX We should check whether SetPoint is installed or not by registry.
1485 // SetPoint, Logitech (Logicool) mouse driver, (confirmed with 4.82.11 and
1486 // MX-1100) always sets 0 to the lParam of WM_MOUSEHWHEEL. The driver SENDs
1487 // one message at first time, this time, ::GetMessagePos() works fine.
1488 // Then, we will return 0 (0 means we process it) to the message. Then, the
1489 // driver will POST the same messages continuously during the wheel tilted.
1490 // But ::GetMessagePos() API always returns (0, 0) for them, even if the
1491 // actual mouse cursor isn't 0,0. Therefore, we cannot trust the result of
1492 // ::GetMessagePos API if the sender is SetPoint.
1493 if (!sMightBeUsing && !aLParam && aLParam != messagePos &&
1494 ::InSendMessage()) {
1495 sMightBeUsing = true;
1496 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1497 ("MouseScroll::Device::SetPoint::IsGetMessagePosResponseValid(): "
1498 "Might using SetPoint"));
1499 } else if (sMightBeUsing && aLParam != 0 && ::InSendMessage()) {
1500 // The user has changed the mouse from Logitech's to another one (e.g.,
1501 // the user has changed to the touchpad of the notebook.
1502 sMightBeUsing = false;
1503 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1504 ("MouseScroll::Device::SetPoint::IsGetMessagePosResponseValid(): "
1505 "Might stop using SetPoint"));
1507 return (sMightBeUsing && !aLParam && !messagePos);
1510 /******************************************************************************
1512 * SynthesizingEvent
1514 ******************************************************************************/
1516 /* static */
1517 bool MouseScrollHandler::SynthesizingEvent::IsSynthesizing() {
1518 return MouseScrollHandler::sInstance &&
1519 MouseScrollHandler::sInstance->mSynthesizingEvent &&
1520 MouseScrollHandler::sInstance->mSynthesizingEvent->mStatus !=
1521 NOT_SYNTHESIZING;
1524 nsresult MouseScrollHandler::SynthesizingEvent::Synthesize(
1525 const POINTS& aCursorPoint, HWND aWnd, UINT aMessage, WPARAM aWParam,
1526 LPARAM aLParam, const BYTE (&aKeyStates)[256]) {
1527 MOZ_LOG(
1528 gMouseScrollLog, LogLevel::Info,
1529 ("MouseScrollHandler::SynthesizingEvent::Synthesize(): aCursorPoint: { "
1530 "x: %d, y: %d }, aWnd=0x%p, aMessage=0x%04X, aWParam=0x%08zX, "
1531 "aLParam=0x%08" PRIXLPTR ", IsSynthesized()=%s, mStatus=%s",
1532 aCursorPoint.x, aCursorPoint.y, aWnd, aMessage, aWParam, aLParam,
1533 GetBoolName(IsSynthesizing()), GetStatusName()));
1535 if (IsSynthesizing()) {
1536 return NS_ERROR_NOT_AVAILABLE;
1539 ::GetKeyboardState(mOriginalKeyState);
1541 // Note that we cannot use ::SetCursorPos() because it works asynchronously.
1542 // We should SEND the message for reducing the possibility of receiving
1543 // unexpected message which were not sent from here.
1544 mCursorPoint = aCursorPoint;
1546 mWnd = aWnd;
1547 mMessage = aMessage;
1548 mWParam = aWParam;
1549 mLParam = aLParam;
1551 memcpy(mKeyState, aKeyStates, sizeof(mKeyState));
1552 ::SetKeyboardState(mKeyState);
1554 mStatus = SENDING_MESSAGE;
1556 // Don't assume that aWnd is always managed by nsWindow. It might be
1557 // a plugin window.
1558 ::SendMessage(aWnd, aMessage, aWParam, aLParam);
1560 return NS_OK;
1563 void MouseScrollHandler::SynthesizingEvent::NativeMessageReceived(
1564 nsWindow* aWidget, UINT aMessage, WPARAM aWParam, LPARAM aLParam) {
1565 if (mStatus == SENDING_MESSAGE && mMessage == aMessage &&
1566 mWParam == aWParam && mLParam == aLParam) {
1567 mStatus = NATIVE_MESSAGE_RECEIVED;
1568 if (aWidget && aWidget->GetWindowHandle() == mWnd) {
1569 return;
1571 // Otherwise, the message may not be sent by us.
1574 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1575 ("MouseScrollHandler::SynthesizingEvent::NativeMessageReceived(): "
1576 "aWidget=%p, aWidget->GetWindowHandle()=0x%p, mWnd=0x%p, "
1577 "aMessage=0x%04X, aWParam=0x%08zX, aLParam=0x%08" PRIXLPTR
1578 ", mStatus=%s",
1579 aWidget, aWidget ? aWidget->GetWindowHandle() : nullptr, mWnd,
1580 aMessage, aWParam, aLParam, GetStatusName()));
1582 // We failed to receive our sent message, we failed to do the job.
1583 Finish();
1585 return;
1588 void MouseScrollHandler::SynthesizingEvent::
1589 NotifyNativeMessageHandlingFinished() {
1590 if (!IsSynthesizing()) {
1591 return;
1594 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1595 ("MouseScrollHandler::SynthesizingEvent::"
1596 "NotifyNativeMessageHandlingFinished(): IsWaitingInternalMessage=%s",
1597 GetBoolName(MouseScrollHandler::IsWaitingInternalMessage())));
1599 if (MouseScrollHandler::IsWaitingInternalMessage()) {
1600 mStatus = INTERNAL_MESSAGE_POSTED;
1601 return;
1604 // If the native message handler didn't post our internal message,
1605 // we our job is finished.
1606 // TODO: When we post the message to plugin window, there is remaning job.
1607 Finish();
1610 void MouseScrollHandler::SynthesizingEvent::
1611 NotifyInternalMessageHandlingFinished() {
1612 if (!IsSynthesizing()) {
1613 return;
1616 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1617 ("MouseScrollHandler::SynthesizingEvent::"
1618 "NotifyInternalMessageHandlingFinished()"));
1620 Finish();
1623 void MouseScrollHandler::SynthesizingEvent::Finish() {
1624 if (!IsSynthesizing()) {
1625 return;
1628 MOZ_LOG(gMouseScrollLog, LogLevel::Info,
1629 ("MouseScrollHandler::SynthesizingEvent::Finish()"));
1631 // Restore the original key state.
1632 ::SetKeyboardState(mOriginalKeyState);
1634 mStatus = NOT_SYNTHESIZING;
1637 } // namespace widget
1638 } // namespace mozilla