forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WinMouseScrollHandler.cpp
1688 lines (1469 loc) · 60.3 KB
/
WinMouseScrollHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/DebugOnly.h"
#include "mozilla/Logging.h"
#include "WinMouseScrollHandler.h"
#include "nsWindow.h"
#include "nsWindowDefs.h"
#include "KeyboardLayout.h"
#include "WinUtils.h"
#include "nsGkAtoms.h"
#include "nsIDOMWindowUtils.h"
#include "mozilla/MiscEvents.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/Preferences.h"
#include "mozilla/dom/WheelEventBinding.h"
#include <psapi.h>
namespace mozilla {
namespace widget {
LazyLogModule gMouseScrollLog("MouseScrollHandlerWidgets");
static const char* GetBoolName(bool aBool) { return aBool ? "TRUE" : "FALSE"; }
MouseScrollHandler* MouseScrollHandler::sInstance = nullptr;
bool MouseScrollHandler::Device::sFakeScrollableWindowNeeded = false;
bool MouseScrollHandler::Device::SynTP::sInitialized = false;
int32_t MouseScrollHandler::Device::SynTP::sMajorVersion = 0;
int32_t MouseScrollHandler::Device::SynTP::sMinorVersion = -1;
bool MouseScrollHandler::Device::Elantech::sUseSwipeHack = false;
bool MouseScrollHandler::Device::Elantech::sUsePinchHack = false;
DWORD MouseScrollHandler::Device::Elantech::sZoomUntil = 0;
bool MouseScrollHandler::Device::Apoint::sInitialized = false;
int32_t MouseScrollHandler::Device::Apoint::sMajorVersion = 0;
int32_t MouseScrollHandler::Device::Apoint::sMinorVersion = -1;
bool MouseScrollHandler::Device::SetPoint::sMightBeUsing = false;
// The duration until timeout of events transaction. The value is 1.5 sec,
// it's just a magic number, it was suggested by Logitech's engineer, see
// bug 605648 comment 90.
#define DEFAULT_TIMEOUT_DURATION 1500
/******************************************************************************
*
* MouseScrollHandler
*
******************************************************************************/
/* static */
POINTS
MouseScrollHandler::GetCurrentMessagePos() {
if (SynthesizingEvent::IsSynthesizing()) {
return sInstance->mSynthesizingEvent->GetCursorPoint();
}
DWORD pos = ::GetMessagePos();
return MAKEPOINTS(pos);
}
// Get rid of the GetMessagePos() API.
#define GetMessagePos()
/* static */
void MouseScrollHandler::Initialize() { Device::Init(); }
/* static */
void MouseScrollHandler::Shutdown() {
delete sInstance;
sInstance = nullptr;
}
/* static */
MouseScrollHandler* MouseScrollHandler::GetInstance() {
if (!sInstance) {
sInstance = new MouseScrollHandler();
}
return sInstance;
}
MouseScrollHandler::MouseScrollHandler()
: mIsWaitingInternalMessage(false), mSynthesizingEvent(nullptr) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll: Creating an instance, this=%p, sInstance=%p", this,
sInstance));
}
MouseScrollHandler::~MouseScrollHandler() {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll: Destroying an instance, this=%p, sInstance=%p", this,
sInstance));
delete mSynthesizingEvent;
}
/* static */
void MouseScrollHandler::MaybeLogKeyState() {
if (!MOZ_LOG_TEST(gMouseScrollLog, LogLevel::Debug)) {
return;
}
BYTE keyboardState[256];
if (::GetKeyboardState(keyboardState)) {
for (size_t i = 0; i < ArrayLength(keyboardState); i++) {
if (keyboardState[i]) {
MOZ_LOG(
gMouseScrollLog, LogLevel::Debug,
(" Current key state: keyboardState[0x%02X]=0x%02X (%s)", i,
keyboardState[i],
((keyboardState[i] & 0x81) == 0x81)
? "Pressed and Toggled"
: (keyboardState[i] & 0x80)
? "Pressed"
: (keyboardState[i] & 0x01) ? "Toggled" : "Unknown"));
}
}
} else {
MOZ_LOG(
gMouseScrollLog, LogLevel::Debug,
("MouseScroll::MaybeLogKeyState(): Failed to print current keyboard "
"state"));
}
}
/* static */
bool MouseScrollHandler::NeedsMessage(UINT aMsg) {
switch (aMsg) {
case WM_SETTINGCHANGE:
case WM_MOUSEWHEEL:
case WM_MOUSEHWHEEL:
case WM_HSCROLL:
case WM_VSCROLL:
case MOZ_WM_MOUSEVWHEEL:
case MOZ_WM_MOUSEHWHEEL:
case MOZ_WM_HSCROLL:
case MOZ_WM_VSCROLL:
case WM_KEYDOWN:
case WM_KEYUP:
return true;
}
return false;
}
/* static */
bool MouseScrollHandler::ProcessMessage(nsWindowBase* aWidget, UINT msg,
WPARAM wParam, LPARAM lParam,
MSGResult& aResult) {
Device::Elantech::UpdateZoomUntil();
switch (msg) {
case WM_SETTINGCHANGE:
if (!sInstance) {
return false;
}
if (wParam == SPI_SETWHEELSCROLLLINES ||
wParam == SPI_SETWHEELSCROLLCHARS) {
sInstance->mSystemSettings.MarkDirty();
}
return false;
case WM_MOUSEWHEEL:
case WM_MOUSEHWHEEL:
GetInstance()->ProcessNativeMouseWheelMessage(aWidget, msg, wParam,
lParam);
sInstance->mSynthesizingEvent->NotifyNativeMessageHandlingFinished();
// We don't need to call next wndproc for WM_MOUSEWHEEL and
// WM_MOUSEHWHEEL. We should consume them always. If the messages
// would be handled by our window again, it caused making infinite
// message loop.
aResult.mConsumed = true;
aResult.mResult = (msg != WM_MOUSEHWHEEL);
return true;
case WM_HSCROLL:
case WM_VSCROLL:
aResult.mConsumed = GetInstance()->ProcessNativeScrollMessage(
aWidget, msg, wParam, lParam);
sInstance->mSynthesizingEvent->NotifyNativeMessageHandlingFinished();
aResult.mResult = 0;
return true;
case MOZ_WM_MOUSEVWHEEL:
case MOZ_WM_MOUSEHWHEEL:
GetInstance()->HandleMouseWheelMessage(aWidget, msg, wParam, lParam);
sInstance->mSynthesizingEvent->NotifyInternalMessageHandlingFinished();
// Doesn't need to call next wndproc for internal wheel message.
aResult.mConsumed = true;
return true;
case MOZ_WM_HSCROLL:
case MOZ_WM_VSCROLL:
GetInstance()->HandleScrollMessageAsMouseWheelMessage(aWidget, msg,
wParam, lParam);
sInstance->mSynthesizingEvent->NotifyInternalMessageHandlingFinished();
// Doesn't need to call next wndproc for internal scroll message.
aResult.mConsumed = true;
return true;
case WM_KEYDOWN:
case WM_KEYUP:
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessMessage(): aWidget=%p, "
"msg=%s(0x%04X), wParam=0x%02X, ::GetMessageTime()=%d",
aWidget,
msg == WM_KEYDOWN ? "WM_KEYDOWN"
: msg == WM_KEYUP ? "WM_KEYUP" : "Unknown",
msg, wParam, ::GetMessageTime()));
MaybeLogKeyState();
if (Device::Elantech::HandleKeyMessage(aWidget, msg, wParam, lParam)) {
aResult.mResult = 0;
aResult.mConsumed = true;
return true;
}
return false;
default:
return false;
}
}
/* static */
nsresult MouseScrollHandler::SynthesizeNativeMouseScrollEvent(
nsWindowBase* aWidget, const LayoutDeviceIntPoint& aPoint,
uint32_t aNativeMessage, int32_t aDelta, uint32_t aModifierFlags,
uint32_t aAdditionalFlags) {
bool useFocusedWindow = !(
aAdditionalFlags & nsIDOMWindowUtils::MOUSESCROLL_PREFER_WIDGET_AT_POINT);
POINT pt;
pt.x = aPoint.x;
pt.y = aPoint.y;
HWND target = useFocusedWindow ? ::WindowFromPoint(pt) : ::GetFocus();
NS_ENSURE_TRUE(target, NS_ERROR_FAILURE);
WPARAM wParam = 0;
LPARAM lParam = 0;
switch (aNativeMessage) {
case WM_MOUSEWHEEL:
case WM_MOUSEHWHEEL: {
lParam = MAKELPARAM(pt.x, pt.y);
WORD mod = 0;
if (aModifierFlags & (nsIWidget::CTRL_L | nsIWidget::CTRL_R)) {
mod |= MK_CONTROL;
}
if (aModifierFlags & (nsIWidget::SHIFT_L | nsIWidget::SHIFT_R)) {
mod |= MK_SHIFT;
}
wParam = MAKEWPARAM(mod, aDelta);
break;
}
case WM_VSCROLL:
case WM_HSCROLL:
lParam = (aAdditionalFlags &
nsIDOMWindowUtils::MOUSESCROLL_WIN_SCROLL_LPARAM_NOT_NULL)
? reinterpret_cast<LPARAM>(target)
: 0;
wParam = aDelta;
break;
default:
return NS_ERROR_INVALID_ARG;
}
// Ensure to make the instance.
GetInstance();
BYTE kbdState[256];
memset(kbdState, 0, sizeof(kbdState));
AutoTArray<KeyPair, 10> keySequence;
WinUtils::SetupKeyModifiersSequence(&keySequence, aModifierFlags,
aNativeMessage);
for (uint32_t i = 0; i < keySequence.Length(); ++i) {
uint8_t key = keySequence[i].mGeneral;
uint8_t keySpecific = keySequence[i].mSpecific;
kbdState[key] = 0x81; // key is down and toggled on if appropriate
if (keySpecific) {
kbdState[keySpecific] = 0x81;
}
}
if (!sInstance->mSynthesizingEvent) {
sInstance->mSynthesizingEvent = new SynthesizingEvent();
}
POINTS pts;
pts.x = static_cast<SHORT>(pt.x);
pts.y = static_cast<SHORT>(pt.y);
return sInstance->mSynthesizingEvent->Synthesize(pts, target, aNativeMessage,
wParam, lParam, kbdState);
}
/* static */
void MouseScrollHandler::InitEvent(nsWindowBase* aWidget,
WidgetGUIEvent& aEvent,
LayoutDeviceIntPoint* aPoint) {
NS_ENSURE_TRUE_VOID(aWidget);
LayoutDeviceIntPoint point;
if (aPoint) {
point = *aPoint;
} else {
POINTS pts = GetCurrentMessagePos();
POINT pt;
pt.x = pts.x;
pt.y = pts.y;
::ScreenToClient(aWidget->GetWindowHandle(), &pt);
point.x = pt.x;
point.y = pt.y;
}
aWidget->InitEvent(aEvent, &point);
}
/* static */
ModifierKeyState MouseScrollHandler::GetModifierKeyState(UINT aMessage) {
ModifierKeyState result;
// Assume the Control key is down if the Elantech touchpad has sent the
// mis-ordered WM_KEYDOWN/WM_MOUSEWHEEL messages. (See the comment in
// MouseScrollHandler::Device::Elantech::HandleKeyMessage().)
if ((aMessage == MOZ_WM_MOUSEVWHEEL || aMessage == WM_MOUSEWHEEL) &&
!result.IsControl() && Device::Elantech::IsZooming()) {
// XXX Do we need to unset MODIFIER_SHIFT, MODIFIER_ALT, MODIFIER_OS too?
// If one of them are true, the default action becomes not zooming.
result.Unset(MODIFIER_ALTGRAPH);
result.Set(MODIFIER_CONTROL);
}
return result;
}
POINT
MouseScrollHandler::ComputeMessagePos(UINT aMessage, WPARAM aWParam,
LPARAM aLParam) {
POINT point;
if (Device::SetPoint::IsGetMessagePosResponseValid(aMessage, aWParam,
aLParam)) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::ComputeMessagePos: Using ::GetCursorPos()"));
::GetCursorPos(&point);
} else {
POINTS pts = GetCurrentMessagePos();
point.x = pts.x;
point.y = pts.y;
}
return point;
}
void MouseScrollHandler::ProcessNativeMouseWheelMessage(nsWindowBase* aWidget,
UINT aMessage,
WPARAM aWParam,
LPARAM aLParam) {
if (SynthesizingEvent::IsSynthesizing()) {
mSynthesizingEvent->NativeMessageReceived(aWidget, aMessage, aWParam,
aLParam);
}
POINT point = ComputeMessagePos(aMessage, aWParam, aLParam);
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: aWidget=%p, "
"aMessage=%s, wParam=0x%08X, lParam=0x%08X, point: { x=%d, y=%d }",
aWidget,
aMessage == WM_MOUSEWHEEL
? "WM_MOUSEWHEEL"
: aMessage == WM_MOUSEHWHEEL
? "WM_MOUSEHWHEEL"
: aMessage == WM_VSCROLL ? "WM_VSCROLL" : "WM_HSCROLL",
aWParam, aLParam, point.x, point.y));
MaybeLogKeyState();
HWND underCursorWnd = ::WindowFromPoint(point);
if (!underCursorWnd) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: "
"No window is not found under the cursor"));
return;
}
if (Device::Elantech::IsPinchHackNeeded() &&
Device::Elantech::IsHelperWindow(underCursorWnd)) {
// The Elantech driver places a window right underneath the cursor
// when sending a WM_MOUSEWHEEL event to us as part of a pinch-to-zoom
// gesture. We detect that here, and search for our window that would
// be beneath the cursor if that window wasn't there.
underCursorWnd = WinUtils::FindOurWindowAtPoint(point);
if (!underCursorWnd) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: "
"Our window is not found under the Elantech helper window"));
return;
}
}
// Handle most cases first. If the window under mouse cursor is our window
// except plugin window (MozillaWindowClass), we should handle the message
// on the window.
if (WinUtils::IsOurProcessWindow(underCursorWnd)) {
nsWindowBase* destWindow = WinUtils::GetNSWindowBasePtr(underCursorWnd);
if (!destWindow) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: "
"Found window under the cursor isn't managed by nsWindow..."));
HWND wnd = ::GetParent(underCursorWnd);
for (; wnd; wnd = ::GetParent(wnd)) {
destWindow = WinUtils::GetNSWindowBasePtr(wnd);
if (destWindow) {
break;
}
}
if (!wnd) {
MOZ_LOG(
gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: Our window which is "
"managed by nsWindow is not found under the cursor"));
return;
}
}
MOZ_ASSERT(destWindow, "destWindow must not be NULL");
// Some odd touchpad utils sets focus to window under the mouse cursor.
// this emulates the odd behavior for debug.
if (mUserPrefs.ShouldEmulateToMakeWindowUnderCursorForeground() &&
(aMessage == WM_MOUSEWHEEL || aMessage == WM_MOUSEHWHEEL) &&
::GetForegroundWindow() != destWindow->GetWindowHandle()) {
::SetForegroundWindow(destWindow->GetWindowHandle());
}
// If the found window is our plugin window, it means that the message
// has been handled by the plugin but not consumed. We should handle the
// message on its parent window. However, note that the DOM event may
// cause accessing the plugin. Therefore, we should unlock the plugin
// process by using PostMessage().
if (destWindow->IsPlugin()) {
destWindow = destWindow->GetParentWindowBase(false);
if (!destWindow) {
MOZ_LOG(
gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: "
"Our window which is a parent of a plugin window is not found"));
return;
}
}
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: Succeeded, "
"Posting internal message to an nsWindow (%p)...",
destWindow));
mIsWaitingInternalMessage = true;
UINT internalMessage = WinUtils::GetInternalMessage(aMessage);
::PostMessage(destWindow->GetWindowHandle(), internalMessage, aWParam,
aLParam);
return;
}
// If the window under cursor is not in our process, it means:
// 1. The window may be a plugin window (GeckoPluginWindow or its descendant).
// 2. The window may be another application's window.
HWND pluginWnd = WinUtils::FindOurProcessWindow(underCursorWnd);
if (!pluginWnd) {
// If there is no plugin window in ancestors of the window under cursor,
// the window is for another applications (case 2).
// We don't need to handle this message.
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: "
"Our window is not found under the cursor"));
return;
}
// If we're a plugin window (MozillaWindowClass) and cursor in this window,
// the message shouldn't go to plugin's wndproc again. So, we should handle
// it on parent window. However, note that the DOM event may cause accessing
// the plugin. Therefore, we should unlock the plugin process by using
// PostMessage().
if (aWidget->IsPlugin() && aWidget->GetWindowHandle() == pluginWnd) {
nsWindowBase* destWindow = aWidget->GetParentWindowBase(false);
if (!destWindow) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: Our normal window "
"which "
"is a parent of this plugin window is not found"));
return;
}
MOZ_LOG(
gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: Succeeded, "
"Posting internal message to an nsWindow (%p) which is parent of this "
"plugin window...",
destWindow));
mIsWaitingInternalMessage = true;
UINT internalMessage = WinUtils::GetInternalMessage(aMessage);
::PostMessage(destWindow->GetWindowHandle(), internalMessage, aWParam,
aLParam);
return;
}
// If the window is a part of plugin, we should post the message to it.
MOZ_LOG(
gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeMouseWheelMessage: Succeeded, "
"Redirecting the message to a window which is a plugin child window"));
::PostMessage(underCursorWnd, aMessage, aWParam, aLParam);
}
bool MouseScrollHandler::ProcessNativeScrollMessage(nsWindowBase* aWidget,
UINT aMessage,
WPARAM aWParam,
LPARAM aLParam) {
if (aLParam || mUserPrefs.IsScrollMessageHandledAsWheelMessage()) {
// Scroll message generated by Thinkpad Trackpoint Driver or similar
// Treat as a mousewheel message and scroll appropriately
ProcessNativeMouseWheelMessage(aWidget, aMessage, aWParam, aLParam);
// Always consume the scroll message if we try to emulate mouse wheel
// action.
return true;
}
if (SynthesizingEvent::IsSynthesizing()) {
mSynthesizingEvent->NativeMessageReceived(aWidget, aMessage, aWParam,
aLParam);
}
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::ProcessNativeScrollMessage: aWidget=%p, "
"aMessage=%s, wParam=0x%08X, lParam=0x%08X",
aWidget, aMessage == WM_VSCROLL ? "WM_VSCROLL" : "WM_HSCROLL",
aWParam, aLParam));
// Scroll message generated by external application
WidgetContentCommandEvent commandEvent(true, eContentCommandScroll, aWidget);
commandEvent.mScroll.mIsHorizontal = (aMessage == WM_HSCROLL);
switch (LOWORD(aWParam)) {
case SB_LINEUP: // SB_LINELEFT
commandEvent.mScroll.mUnit =
WidgetContentCommandEvent::eCmdScrollUnit_Line;
commandEvent.mScroll.mAmount = -1;
break;
case SB_LINEDOWN: // SB_LINERIGHT
commandEvent.mScroll.mUnit =
WidgetContentCommandEvent::eCmdScrollUnit_Line;
commandEvent.mScroll.mAmount = 1;
break;
case SB_PAGEUP: // SB_PAGELEFT
commandEvent.mScroll.mUnit =
WidgetContentCommandEvent::eCmdScrollUnit_Page;
commandEvent.mScroll.mAmount = -1;
break;
case SB_PAGEDOWN: // SB_PAGERIGHT
commandEvent.mScroll.mUnit =
WidgetContentCommandEvent::eCmdScrollUnit_Page;
commandEvent.mScroll.mAmount = 1;
break;
case SB_TOP: // SB_LEFT
commandEvent.mScroll.mUnit =
WidgetContentCommandEvent::eCmdScrollUnit_Whole;
commandEvent.mScroll.mAmount = -1;
break;
case SB_BOTTOM: // SB_RIGHT
commandEvent.mScroll.mUnit =
WidgetContentCommandEvent::eCmdScrollUnit_Whole;
commandEvent.mScroll.mAmount = 1;
break;
default:
return false;
}
// XXX If this is a plugin window, we should dispatch the event from
// parent window.
aWidget->DispatchContentCommandEvent(&commandEvent);
return true;
}
void MouseScrollHandler::HandleMouseWheelMessage(nsWindowBase* aWidget,
UINT aMessage, WPARAM aWParam,
LPARAM aLParam) {
MOZ_ASSERT((aMessage == MOZ_WM_MOUSEVWHEEL || aMessage == MOZ_WM_MOUSEHWHEEL),
"HandleMouseWheelMessage must be called with "
"MOZ_WM_MOUSEVWHEEL or MOZ_WM_MOUSEHWHEEL");
MOZ_LOG(
gMouseScrollLog, LogLevel::Info,
("MouseScroll::HandleMouseWheelMessage: aWidget=%p, "
"aMessage=MOZ_WM_MOUSE%sWHEEL, aWParam=0x%08X, aLParam=0x%08X",
aWidget, aMessage == MOZ_WM_MOUSEVWHEEL ? "V" : "H", aWParam, aLParam));
mIsWaitingInternalMessage = false;
// If it's not allowed to cache system settings, we need to reset the cache
// before handling the mouse wheel message.
mSystemSettings.TrustedScrollSettingsDriver();
EventInfo eventInfo(aWidget, WinUtils::GetNativeMessage(aMessage), aWParam,
aLParam);
if (!eventInfo.CanDispatchWheelEvent()) {
MOZ_LOG(
gMouseScrollLog, LogLevel::Info,
("MouseScroll::HandleMouseWheelMessage: Cannot dispatch the events"));
mLastEventInfo.ResetTransaction();
return;
}
// Discard the remaining delta if current wheel message and last one are
// received by different window or to scroll different direction or
// different unit scroll. Furthermore, if the last event was too old.
if (!mLastEventInfo.CanContinueTransaction(eventInfo)) {
mLastEventInfo.ResetTransaction();
}
mLastEventInfo.RecordEvent(eventInfo);
ModifierKeyState modKeyState = GetModifierKeyState(aMessage);
// Grab the widget, it might be destroyed by a DOM event handler.
RefPtr<nsWindowBase> kungFuDethGrip(aWidget);
WidgetWheelEvent wheelEvent(true, eWheel, aWidget);
if (mLastEventInfo.InitWheelEvent(aWidget, wheelEvent, modKeyState)) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::HandleMouseWheelMessage: dispatching "
"eWheel event"));
aWidget->DispatchWheelEvent(&wheelEvent);
if (aWidget->Destroyed()) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::HandleMouseWheelMessage: The window was destroyed "
"by eWheel event"));
mLastEventInfo.ResetTransaction();
return;
}
} else {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::HandleMouseWheelMessage: eWheel event is not "
"dispatched"));
}
}
void MouseScrollHandler::HandleScrollMessageAsMouseWheelMessage(
nsWindowBase* aWidget, UINT aMessage, WPARAM aWParam, LPARAM aLParam) {
MOZ_ASSERT((aMessage == MOZ_WM_VSCROLL || aMessage == MOZ_WM_HSCROLL),
"HandleScrollMessageAsMouseWheelMessage must be called with "
"MOZ_WM_VSCROLL or MOZ_WM_HSCROLL");
mIsWaitingInternalMessage = false;
ModifierKeyState modKeyState = GetModifierKeyState(aMessage);
WidgetWheelEvent wheelEvent(true, eWheel, aWidget);
double& delta =
(aMessage == MOZ_WM_VSCROLL) ? wheelEvent.mDeltaY : wheelEvent.mDeltaX;
int32_t& lineOrPageDelta = (aMessage == MOZ_WM_VSCROLL)
? wheelEvent.mLineOrPageDeltaY
: wheelEvent.mLineOrPageDeltaX;
delta = 1.0;
lineOrPageDelta = 1;
switch (LOWORD(aWParam)) {
case SB_PAGEUP:
delta = -1.0;
lineOrPageDelta = -1;
case SB_PAGEDOWN:
wheelEvent.mDeltaMode = dom::WheelEvent_Binding::DOM_DELTA_PAGE;
break;
case SB_LINEUP:
delta = -1.0;
lineOrPageDelta = -1;
case SB_LINEDOWN:
wheelEvent.mDeltaMode = dom::WheelEvent_Binding::DOM_DELTA_LINE;
break;
default:
return;
}
modKeyState.InitInputEvent(wheelEvent);
// XXX Current mouse position may not be same as when the original message
// is received. We need to know the actual mouse cursor position when
// the original message was received.
InitEvent(aWidget, wheelEvent);
MOZ_LOG(
gMouseScrollLog, LogLevel::Info,
("MouseScroll::HandleScrollMessageAsMouseWheelMessage: aWidget=%p, "
"aMessage=MOZ_WM_%sSCROLL, aWParam=0x%08X, aLParam=0x%08X, "
"wheelEvent { mRefPoint: { x: %d, y: %d }, mDeltaX: %f, mDeltaY: %f, "
"mLineOrPageDeltaX: %d, mLineOrPageDeltaY: %d, "
"isShift: %s, isControl: %s, isAlt: %s, isMeta: %s }",
aWidget, (aMessage == MOZ_WM_VSCROLL) ? "V" : "H", aWParam, aLParam,
wheelEvent.mRefPoint.x, wheelEvent.mRefPoint.y, wheelEvent.mDeltaX,
wheelEvent.mDeltaY, wheelEvent.mLineOrPageDeltaX,
wheelEvent.mLineOrPageDeltaY, GetBoolName(wheelEvent.IsShift()),
GetBoolName(wheelEvent.IsControl()), GetBoolName(wheelEvent.IsAlt()),
GetBoolName(wheelEvent.IsMeta())));
aWidget->DispatchWheelEvent(&wheelEvent);
}
/******************************************************************************
*
* EventInfo
*
******************************************************************************/
MouseScrollHandler::EventInfo::EventInfo(nsWindowBase* aWidget, UINT aMessage,
WPARAM aWParam, LPARAM aLParam) {
MOZ_ASSERT(
aMessage == WM_MOUSEWHEEL || aMessage == WM_MOUSEHWHEEL,
"EventInfo must be initialized with WM_MOUSEWHEEL or WM_MOUSEHWHEEL");
MouseScrollHandler::GetInstance()->mSystemSettings.Init();
mIsVertical = (aMessage == WM_MOUSEWHEEL);
mIsPage =
MouseScrollHandler::sInstance->mSystemSettings.IsPageScroll(mIsVertical);
mDelta = (short)HIWORD(aWParam);
mWnd = aWidget->GetWindowHandle();
mTimeStamp = TimeStamp::Now();
}
bool MouseScrollHandler::EventInfo::CanDispatchWheelEvent() const {
if (!GetScrollAmount()) {
// XXX I think that we should dispatch mouse wheel events even if the
// operation will not scroll because the wheel operation really happened
// and web application may want to handle the event for non-scroll action.
return false;
}
return (mDelta != 0);
}
int32_t MouseScrollHandler::EventInfo::GetScrollAmount() const {
if (mIsPage) {
return 1;
}
return MouseScrollHandler::sInstance->mSystemSettings.GetScrollAmount(
mIsVertical);
}
/******************************************************************************
*
* LastEventInfo
*
******************************************************************************/
bool MouseScrollHandler::LastEventInfo::CanContinueTransaction(
const EventInfo& aNewEvent) {
int32_t timeout = MouseScrollHandler::sInstance->mUserPrefs
.GetMouseScrollTransactionTimeout();
return !mWnd ||
(mWnd == aNewEvent.GetWindowHandle() &&
IsPositive() == aNewEvent.IsPositive() &&
mIsVertical == aNewEvent.IsVertical() &&
mIsPage == aNewEvent.IsPage() &&
(timeout < 0 || TimeStamp::Now() - mTimeStamp <=
TimeDuration::FromMilliseconds(timeout)));
}
void MouseScrollHandler::LastEventInfo::ResetTransaction() {
if (!mWnd) {
return;
}
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::LastEventInfo::ResetTransaction()"));
mWnd = nullptr;
mAccumulatedDelta = 0;
}
void MouseScrollHandler::LastEventInfo::RecordEvent(const EventInfo& aEvent) {
mWnd = aEvent.GetWindowHandle();
mDelta = aEvent.GetNativeDelta();
mIsVertical = aEvent.IsVertical();
mIsPage = aEvent.IsPage();
mTimeStamp = TimeStamp::Now();
}
/* static */
int32_t MouseScrollHandler::LastEventInfo::RoundDelta(double aDelta) {
return (aDelta >= 0) ? (int32_t)floor(aDelta) : (int32_t)ceil(aDelta);
}
bool MouseScrollHandler::LastEventInfo::InitWheelEvent(
nsWindowBase* aWidget, WidgetWheelEvent& aWheelEvent,
const ModifierKeyState& aModKeyState) {
MOZ_ASSERT(aWheelEvent.mMessage == eWheel);
// XXX Why don't we use lParam value? We should use lParam value because
// our internal message is always posted by original message handler.
// So, GetMessagePos() may return different cursor position.
InitEvent(aWidget, aWheelEvent);
aModKeyState.InitInputEvent(aWheelEvent);
// Our positive delta value means to bottom or right.
// But positive native delta value means to top or right.
// Use orienter for computing our delta value with native delta value.
int32_t orienter = mIsVertical ? -1 : 1;
aWheelEvent.mDeltaMode = mIsPage ? dom::WheelEvent_Binding::DOM_DELTA_PAGE
: dom::WheelEvent_Binding::DOM_DELTA_LINE;
double& delta = mIsVertical ? aWheelEvent.mDeltaY : aWheelEvent.mDeltaX;
int32_t& lineOrPageDelta = mIsVertical ? aWheelEvent.mLineOrPageDeltaY
: aWheelEvent.mLineOrPageDeltaX;
double nativeDeltaPerUnit =
mIsPage ? static_cast<double>(WHEEL_DELTA)
: static_cast<double>(WHEEL_DELTA) / GetScrollAmount();
delta = static_cast<double>(mDelta) * orienter / nativeDeltaPerUnit;
mAccumulatedDelta += mDelta;
lineOrPageDelta =
mAccumulatedDelta * orienter / RoundDelta(nativeDeltaPerUnit);
mAccumulatedDelta -=
lineOrPageDelta * orienter * RoundDelta(nativeDeltaPerUnit);
if (aWheelEvent.mDeltaMode != dom::WheelEvent_Binding::DOM_DELTA_LINE) {
// If the scroll delta mode isn't per line scroll, we shouldn't allow to
// override the system scroll speed setting.
aWheelEvent.mAllowToOverrideSystemScrollSpeed = false;
} else if (!MouseScrollHandler::sInstance->mSystemSettings
.IsOverridingSystemScrollSpeedAllowed()) {
// If the system settings are customized by either the user or
// the mouse utility, we shouldn't allow to override the system scroll
// speed setting.
aWheelEvent.mAllowToOverrideSystemScrollSpeed = false;
} else {
// For suppressing too fast scroll, we should ensure that the maximum
// overridden delta value should be less than overridden scroll speed
// with default scroll amount.
double defaultScrollAmount = mIsVertical
? SystemSettings::DefaultScrollLines()
: SystemSettings::DefaultScrollChars();
double maxDelta = WidgetWheelEvent::ComputeOverriddenDelta(
defaultScrollAmount, mIsVertical);
if (maxDelta != defaultScrollAmount) {
double overriddenDelta =
WidgetWheelEvent::ComputeOverriddenDelta(Abs(delta), mIsVertical);
if (overriddenDelta > maxDelta) {
// Suppress to fast scroll since overriding system scroll speed with
// current delta value causes too big delta value.
aWheelEvent.mAllowToOverrideSystemScrollSpeed = false;
}
}
}
MOZ_LOG(
gMouseScrollLog, LogLevel::Info,
("MouseScroll::LastEventInfo::InitWheelEvent: aWidget=%p, "
"aWheelEvent { mRefPoint: { x: %d, y: %d }, mDeltaX: %f, mDeltaY: %f, "
"mLineOrPageDeltaX: %d, mLineOrPageDeltaY: %d, "
"isShift: %s, isControl: %s, isAlt: %s, isMeta: %s, "
"mAllowToOverrideSystemScrollSpeed: %s }, "
"mAccumulatedDelta: %d",
aWidget, aWheelEvent.mRefPoint.x, aWheelEvent.mRefPoint.y,
aWheelEvent.mDeltaX, aWheelEvent.mDeltaY, aWheelEvent.mLineOrPageDeltaX,
aWheelEvent.mLineOrPageDeltaY, GetBoolName(aWheelEvent.IsShift()),
GetBoolName(aWheelEvent.IsControl()), GetBoolName(aWheelEvent.IsAlt()),
GetBoolName(aWheelEvent.IsMeta()),
GetBoolName(aWheelEvent.mAllowToOverrideSystemScrollSpeed),
mAccumulatedDelta));
return (delta != 0);
}
/******************************************************************************
*
* SystemSettings
*
******************************************************************************/
void MouseScrollHandler::SystemSettings::Init() {
if (mInitialized) {
return;
}
InitScrollLines();
InitScrollChars();
mInitialized = true;
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::SystemSettings::Init(): initialized, "
"mScrollLines=%d, mScrollChars=%d",
mScrollLines, mScrollChars));
}
bool MouseScrollHandler::SystemSettings::InitScrollLines() {
int32_t oldValue = mInitialized ? mScrollLines : 0;
mIsReliableScrollLines = false;
mScrollLines = MouseScrollHandler::sInstance->mUserPrefs
.GetOverriddenVerticalScrollAmout();
if (mScrollLines >= 0) {
// overridden by the pref.
mIsReliableScrollLines = true;
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::SystemSettings::InitScrollLines(): mScrollLines is "
"overridden by the pref: %d",
mScrollLines));
} else if (!::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &mScrollLines,
0)) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::SystemSettings::InitScrollLines(): "
"::SystemParametersInfo("
"SPI_GETWHEELSCROLLLINES) failed"));
mScrollLines = DefaultScrollLines();
}
if (mScrollLines > WHEEL_DELTA) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::SystemSettings::InitScrollLines(): the result of "
"::SystemParametersInfo(SPI_GETWHEELSCROLLLINES) is too large: %d",
mScrollLines));
// sScrollLines usually equals 3 or 0 (for no scrolling)
// However, if sScrollLines > WHEEL_DELTA, we assume that
// the mouse driver wants a page scroll. The docs state that
// sScrollLines should explicitly equal WHEEL_PAGESCROLL, but
// since some mouse drivers use an arbitrary large number instead,
// we have to handle that as well.
mScrollLines = WHEEL_PAGESCROLL;
}
return oldValue != mScrollLines;
}
bool MouseScrollHandler::SystemSettings::InitScrollChars() {
int32_t oldValue = mInitialized ? mScrollChars : 0;
mIsReliableScrollChars = false;
mScrollChars = MouseScrollHandler::sInstance->mUserPrefs
.GetOverriddenHorizontalScrollAmout();
if (mScrollChars >= 0) {
// overridden by the pref.
mIsReliableScrollChars = true;
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::SystemSettings::InitScrollChars(): mScrollChars is "
"overridden by the pref: %d",
mScrollChars));
} else if (!::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &mScrollChars,
0)) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::SystemSettings::InitScrollChars(): "
"::SystemParametersInfo("
"SPI_GETWHEELSCROLLCHARS) failed, this is unexpected on Vista or "
"later"));
// XXX Should we use DefaultScrollChars()?
mScrollChars = 1;
}
if (mScrollChars > WHEEL_DELTA) {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScroll::SystemSettings::InitScrollChars(): the result of "
"::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS) is too large: %d",
mScrollChars));
// See the comments for the case mScrollLines > WHEEL_DELTA.
mScrollChars = WHEEL_PAGESCROLL;
}
return oldValue != mScrollChars;
}
void MouseScrollHandler::SystemSettings::MarkDirty() {
MOZ_LOG(gMouseScrollLog, LogLevel::Info,
("MouseScrollHandler::SystemSettings::MarkDirty(): "
"Marking SystemSettings dirty"));
mInitialized = false;
// When system settings are changed, we should reset current transaction.
MOZ_ASSERT(sInstance,
"Must not be called at initializing MouseScrollHandler");
MouseScrollHandler::sInstance->mLastEventInfo.ResetTransaction();
}
void MouseScrollHandler::SystemSettings::RefreshCache() {
bool isChanged = InitScrollLines();
isChanged = InitScrollChars() || isChanged;
if (!isChanged) {
return;
}
// If the scroll amount is changed, we should reset current transaction.
MOZ_ASSERT(sInstance,
"Must not be called at initializing MouseScrollHandler");
MouseScrollHandler::sInstance->mLastEventInfo.ResetTransaction();
}
void MouseScrollHandler::SystemSettings::TrustedScrollSettingsDriver() {
if (!mInitialized) {
return;
}
// if the cache is initialized with prefs, we don't need to refresh it.
if (mIsReliableScrollLines && mIsReliableScrollChars) {
return;
}