File: WinMouseScrollHandler.h

package info (click to toggle)
firefox 146.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,260 kB
  • sloc: cpp: 7,587,892; javascript: 6,509,455; ansic: 3,755,295; python: 1,410,813; xml: 629,201; asm: 438,677; java: 186,096; sh: 62,697; makefile: 18,086; objc: 13,087; perl: 12,811; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (501 lines) | stat: -rw-r--r-- 16,532 bytes parent folder | download | duplicates (13)
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
/* -*- 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/. */

#ifndef mozilla_widget_WinMouseScrollHandler_h__
#define mozilla_widget_WinMouseScrollHandler_h__

#include "nscore.h"
#include "nsDebug.h"
#include "mozilla/Assertions.h"
#include "mozilla/EventForwards.h"
#include "mozilla/TimeStamp.h"
#include "Units.h"
#include <windows.h>
#include "nsPoint.h"

class nsWindow;

namespace mozilla {
namespace widget {

class ModifierKeyState;

struct MSGResult;

class MouseScrollHandler {
 public:
  static MouseScrollHandler* GetInstance();

  static void Initialize();
  static void Shutdown();

  static bool ProcessMessage(nsWindow* aWidget, UINT msg, WPARAM wParam,
                             LPARAM lParam, MSGResult& aResult);

  /**
   * See nsIWidget::SynthesizeNativeMouseScrollEvent() for the detail about
   * this method.
   */
  static nsresult SynthesizeNativeMouseScrollEvent(
      nsWindow* aWidget, const LayoutDeviceIntPoint& aPoint,
      uint32_t aNativeMessage, int32_t aDelta, uint32_t aModifierFlags,
      uint32_t aAdditionalFlags);

 private:
  MouseScrollHandler();
  ~MouseScrollHandler();

  static void MaybeLogKeyState();

  static MouseScrollHandler* sInstance;

  /**
   * InitEvent() initializes the aEvent.  If aPoint is null, the result of
   * GetCurrentMessagePos() will be used.
   */
  static void InitEvent(nsWindow* aWidget, WidgetGUIEvent& aEvent,
                        LPARAM* aPoint);

  /**
   * GetModifierKeyState() returns current modifier key state.
   * Note that some devices need some hack for the modifier key state.
   * This method does it automatically.
   *
   * @param aMessage    Message being handled.
   */
  static ModifierKeyState GetModifierKeyState(UINT aMessage);

  /**
   * MozGetMessagePos() returns the mouse cursor position when GetMessage()
   * was called last time.  However, if we're sending a native message,
   * this returns the specified cursor position by
   * SynthesizeNativeMouseScrollEvent().
   */
  static POINTS GetCurrentMessagePos();

  /**
   * ProcessMouseMessage() processes mousewheel and scroll messages, dispatching
   * to other handlers as needed while dealing with common tasks.
   *
   * @param aMessage    WM_MOUSEWHEEL, WM_MOUSEHWHEEL, WM_VSCROLL or WM_HSCROLL.
   * @param aWParam     The wParam value of the message.
   * @param aLParam     The lParam value of the message.
   */
  bool ProcessMouseMessage(UINT msg, WPARAM wParam, LPARAM lParam,
                           MSGResult& aResult);

  /**
   * HandleMouseWheelMessage() processes WM_MOUSEWHEEL or WM_MOUSEHWHEEL.
   *
   * @param aWidget     The window which should process the wheel message.
   * @param aMessage    WM_MOUSEWHEEL or WM_MOUSEHWHEEL.
   * @param aWParam     The wParam value of the message.
   * @param aLParam     The lParam value of the message.
   * @return            TRUE if the message is processed.  Otherwise, FALSE.
   */
  bool HandleMouseWheelMessage(nsWindow* aWidget, UINT aMessage, WPARAM aWParam,
                               LPARAM aLParam);

  /**
   * HandleScrollMessageAsMouseWheelMessage() processes WM_VSCROLL or WM_HSCROLL
   * when the user wants them to emulate a mouse-wheel message's behavior
   * (_i.e._, when `mousewheel.emulate_at_wm_scroll` is true).
   *
   * @param aWidget     The window which should process the scroll message.
   * @param aMessage    WM_VSCROLL or WM_HSCROLL.
   * @param aWParam     The wParam value of the message.
   * @param aLParam     The lParam value of the message.
   * @return            TRUE if the message is processed.  Otherwise, FALSE.
   */
  bool HandleScrollMessageAsMouseWheelMessage(nsWindow* aWidget, UINT aMessage,
                                              WPARAM aWParam, LPARAM aLParam);

  /**
   * HandleScrollMessageAsItself() processes the WM_VSCROLL and WM_HSCROLL when
   * the user does _not_ want them to emulate a mouse-wheel message's behavior
   * (_i.e._, when `mousewheel.emulate_at_wm_scroll` is false).
   *
   * @param aWidget     The window which should process the scroll message.
   * @param aMessage    WM_VSCROLL or WM_HSCROLL.
   * @param aWParam     The wParam value of the message.
   * @param aLParam     The lParam value of the message.
   * @return            TRUE if the message is processed.  Otherwise, FALSE.
   */
  bool HandleScrollMessageAsItself(nsWindow* aWidget, UINT aMessage,
                                   WPARAM aWParam, LPARAM aLParam);

  /**
   * ComputeMessagePos() abstracts over ::GetMessagePos(), which is known to
   * yield bad data under some mouse drivers (particularly Logitech's SetPoint).
   *
   * @param aMessage    Current message.
   * @param aWParam     Current message's wParam.
   * @param aLParam     Current message's lParam.
   * @return            Mouse cursor position.
   */
  POINT ComputeMessagePos(UINT aMessage, WPARAM aWParam, LPARAM aLParam);

  /**
   * FindTargetWindow() finds the nsWindow which needs to process the current
   * scroll event. (This is the window underneath the cursor -- which is not
   * necessarily the window whose event queue it came in on!)
   *
   * @param aMessage    Current message.
   * @param aWParam     Current message's wParam.
   * @param aLParam     Current message's lParam.
   * @return            The relevant nsWindow, or nullptr if no appropriate
   *                    window could be identified.
   */
  nsWindow* FindTargetWindow(UINT aMessage, WPARAM aWParam, LPARAM aLParam);

  class EventInfo {
   public:
    /**
     * @param aWidget   An nsWindow which is handling the event.
     * @param aMessage  Must be WM_MOUSEWHEEL or WM_MOUSEHWHEEL.
     */
    EventInfo(nsWindow* aWidget, UINT aMessage, WPARAM aWParam, LPARAM aLParam);

    bool CanDispatchWheelEvent() const;

    int32_t GetNativeDelta() const { return mDelta; }
    HWND GetWindowHandle() const { return mWnd; }
    const TimeStamp& GetTimeStamp() const { return mTimeStamp; }
    bool IsVertical() const { return mIsVertical; }
    bool IsPositive() const { return (mDelta > 0); }
    bool IsPage() const { return mIsPage; }

    /**
     * @return          Number of lines or pages scrolled per WHEEL_DELTA.
     */
    int32_t GetScrollAmount() const;

   protected:
    EventInfo()
        : mIsVertical(false), mIsPage(false), mDelta(0), mWnd(nullptr) {}

    // TRUE if event is for vertical scroll.  Otherwise, FALSE.
    bool mIsVertical;
    // TRUE if event scrolls per page, otherwise, FALSE.
    bool mIsPage;
    // The native delta value.
    int32_t mDelta;
    // The window handle which is handling the event.
    HWND mWnd;
    // Timestamp of the event.
    TimeStamp mTimeStamp;
  };

  class LastEventInfo : public EventInfo {
   public:
    LastEventInfo() : EventInfo(), mAccumulatedDelta(0) {}

    /**
     * CanContinueTransaction() checks whether the new event can continue the
     * last transaction or not.  Note that if there is no transaction, this
     * returns true.
     */
    bool CanContinueTransaction(const EventInfo& aNewEvent);

    /**
     * ResetTransaction() resets the transaction, i.e., the instance forgets
     * the last event information.
     */
    void ResetTransaction();

    /**
     * RecordEvent() saves the information of new event.
     */
    void RecordEvent(const EventInfo& aEvent);

    /**
     * InitWheelEvent() initializes NS_WHEEL_WHEEL event and
     * recomputes the remaning detla for the event.
     * This must be called only once during handling a message and after
     * RecordEvent() is called.
     *
     * @param aWidget           A window which will dispatch the event.
     * @param aWheelEvent       An NS_WHEEL_WHEEL event, this will be
     *                          initialized.
     * @param aModKeyState      Current modifier key state.
     * @return                  TRUE if the event is ready to dispatch.
     *                          Otherwise, FALSE.
     */
    bool InitWheelEvent(nsWindow* aWidget, WidgetWheelEvent& aWheelEvent,
                        const ModifierKeyState& aModKeyState, LPARAM aLParam);

   private:
    static int32_t RoundDelta(double aDelta);

    int32_t mAccumulatedDelta;
  };

  LastEventInfo mLastEventInfo;

  class SystemSettings {
   public:
    SystemSettings() : mInitialized(false) {}

    void Init();
    void MarkDirty();
    void NotifyUserPrefsMayOverrideSystemSettings();

    // On some environments, SystemParametersInfo() may be hooked by touchpad
    // utility or something.  In such case, when user changes active pointing
    // device to another one, the result of SystemParametersInfo() may be
    // changed without WM_SETTINGCHANGE message.  For avoiding this trouble,
    // we need to modify cache of system settings at every wheel message
    // handling if we meet known device whose utility may hook the API.
    void TrustedScrollSettingsDriver();

    int32_t GetScrollAmount(bool aForVertical) const {
      MOZ_ASSERT(mInitialized, "SystemSettings must be initialized");
      return aForVertical ? mScrollLines : mScrollChars;
    }

    bool IsPageScroll(bool aForVertical) const {
      MOZ_ASSERT(mInitialized, "SystemSettings must be initialized");
      return aForVertical ? (uint32_t(mScrollLines) == WHEEL_PAGESCROLL)
                          : (uint32_t(mScrollChars) == WHEEL_PAGESCROLL);
    }

    // The default vertical and horizontal scrolling speed is 3, this is defined
    // on the document of SystemParametersInfo in MSDN.
    static int32_t DefaultScrollLines() { return 3; }

   private:
    bool mInitialized;
    // The result of SystemParametersInfo() may not be reliable since it may
    // be hooked.  So, if the values are initialized with prefs, we can trust
    // the value.  Following mIsReliableScroll* are set true when mScroll* are
    // initialized with prefs.
    bool mIsReliableScrollLines;
    bool mIsReliableScrollChars;

    int32_t mScrollLines;
    int32_t mScrollChars;

    // Returns true if cached value is changed.
    bool InitScrollLines();
    bool InitScrollChars();

    void RefreshCache();
  };

  SystemSettings mSystemSettings;

  class UserPrefs {
   public:
    UserPrefs();
    ~UserPrefs();

    void MarkDirty();

    bool IsScrollMessageHandledAsWheelMessage() {
      Init();
      return mScrollMessageHandledAsWheelMessage;
    }

    bool IsSystemSettingCacheEnabled() {
      Init();
      return mEnableSystemSettingCache;
    }

    bool IsSystemSettingCacheForciblyEnabled() {
      Init();
      return mForceEnableSystemSettingCache;
    }

    bool ShouldEmulateToMakeWindowUnderCursorForeground() {
      Init();
      return mEmulateToMakeWindowUnderCursorForeground;
    }

    int32_t GetOverriddenVerticalScrollAmout() {
      Init();
      return mOverriddenVerticalScrollAmount;
    }

    int32_t GetOverriddenHorizontalScrollAmout() {
      Init();
      return mOverriddenHorizontalScrollAmount;
    }

    int32_t GetMouseScrollTransactionTimeout() {
      Init();
      return mMouseScrollTransactionTimeout;
    }

   private:
    void Init();

    static void OnChange(const char* aPrefName, void* aSelf) {
      static_cast<UserPrefs*>(aSelf)->MarkDirty();
    }

    bool mInitialized;
    bool mScrollMessageHandledAsWheelMessage;
    bool mEnableSystemSettingCache;
    bool mForceEnableSystemSettingCache;
    bool mEmulateToMakeWindowUnderCursorForeground;
    int32_t mOverriddenVerticalScrollAmount;
    int32_t mOverriddenHorizontalScrollAmount;
    int32_t mMouseScrollTransactionTimeout;
  };

  UserPrefs mUserPrefs;

  // only used in tests
  class SynthesizingEvent;
  UniquePtr<SynthesizingEvent> mSynthesizingEvent;
  static SynthesizingEvent* GetActiveSynthEvent();

 public:
  class Device {
   public:
    // SynTP is a touchpad driver of Synaptics.
    class SynTP {
     public:
      static bool IsDriverInstalled() { return sMajorVersion != 0; }
      /**
       * GetDriverMajorVersion() returns the installed driver's major version.
       * If SynTP driver isn't installed, this returns 0.
       */
      static int32_t GetDriverMajorVersion() { return sMajorVersion; }
      /**
       * GetDriverMinorVersion() returns the installed driver's minor version.
       * If SynTP driver isn't installed, this returns -1.
       */
      static int32_t GetDriverMinorVersion() { return sMinorVersion; }

      static void Init();

     private:
      static bool sInitialized;
      static int32_t sMajorVersion;
      static int32_t sMinorVersion;
    };

    class Elantech {
     public:
      /**
       * GetDriverMajorVersion() returns the installed driver's major version.
       * If Elantech's driver was installed, returns 0.
       */
      static int32_t GetDriverMajorVersion();

      /**
       * IsHelperWindow() checks whether aWnd is a helper window of Elantech's
       * touchpad.  Returns TRUE if so.  Otherwise, FALSE.
       */
      static bool IsHelperWindow(HWND aWnd);

      /**
       * Key message handler for Elantech's hack.  Returns TRUE if the message
       * is consumed by this handler.  Otherwise, FALSE.
       */
      static bool HandleKeyMessage(nsWindow* aWidget, UINT aMsg, WPARAM aWParam,
                                   LPARAM aLParam);

      static void UpdateZoomUntil();
      static bool IsZooming();

      static void Init();

      static bool IsPinchHackNeeded() { return sUsePinchHack; }

     private:
      // Whether to enable the Elantech swipe gesture hack.
      static bool sUseSwipeHack;
      // Whether to enable the Elantech pinch-to-zoom gesture hack.
      static bool sUsePinchHack;
      static DWORD sZoomUntil;
    };  // class Elantech

    // Apoint is a touchpad driver of Alps.
    class Apoint {
     public:
      static bool IsDriverInstalled() { return sMajorVersion != 0; }
      /**
       * GetDriverMajorVersion() returns the installed driver's major version.
       * If Apoint driver isn't installed, this returns 0.
       */
      static int32_t GetDriverMajorVersion() { return sMajorVersion; }
      /**
       * GetDriverMinorVersion() returns the installed driver's minor version.
       * If Apoint driver isn't installed, this returns -1.
       */
      static int32_t GetDriverMinorVersion() { return sMinorVersion; }

      static void Init();

     private:
      static bool sInitialized;
      static int32_t sMajorVersion;
      static int32_t sMinorVersion;
    };

    class TrackPoint {
     public:
      /**
       * IsDriverInstalled() returns TRUE if TrackPoint's driver is installed.
       * Otherwise, returns FALSE.
       */
      static bool IsDriverInstalled();
    };  // class TrackPoint

    class UltraNav {
     public:
      /**
       * IsObsoleteDriverInstalled() checks whether obsoleted UltraNav
       * is installed on the environment.
       * Returns TRUE if it was installed.  Otherwise, FALSE.
       */
      static bool IsObsoleteDriverInstalled();
    };  // class UltraNav

    class SetPoint {
     public:
      /**
       * SetPoint, Logitech's mouse driver, may report wrong cursor position
       * for WM_MOUSEHWHEEL message.  See comment in the implementation for
       * the detail.
       */
      static bool IsGetMessagePosResponseValid(UINT aMessage, WPARAM aWParam,
                                               LPARAM aLParam);

     private:
      static bool sMightBeUsing;
    };

    static void Init();

    static bool IsFakeScrollableWindowNeeded() {
      return sFakeScrollableWindowNeeded;
    }

   private:
    /**
     * Gets the bool value of aPrefName used to enable or disable an input
     * workaround (like the Trackpoint hack).  The pref can take values 0 (for
     * disabled), 1 (for enabled) or -1 (to automatically detect whether to
     * enable the workaround).
     *
     * @param aPrefName The name of the pref.
     * @param aValueIfAutomatic Whether the given input workaround should be
     *                          enabled by default.
     */
    static bool GetWorkaroundPref(const char* aPrefName,
                                  bool aValueIfAutomatic);

    static bool sFakeScrollableWindowNeeded;
  };  // class Device
};

}  // namespace widget
}  // namespace mozilla

#endif  // mozilla_widget_WinMouseScrollHandler_h__