1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef WinPointerEvents_h__
7 #define WinPointerEvents_h__
8 
9 #include "mozilla/MouseEvents.h"
10 #include "nsWindowBase.h"
11 
12 // Define PointerEvent related macros and structures when building code on
13 // Windows version before Win8.
14 #if WINVER < 0x0602
15 
16 // These definitions are copied from WinUser.h. Some of them are not used but
17 // keep them here for future usage.
18 #define WM_NCPOINTERUPDATE 0x0241
19 #define WM_NCPOINTERDOWN 0x0242
20 #define WM_NCPOINTERUP 0x0243
21 #define WM_POINTERUPDATE 0x0245
22 #define WM_POINTERDOWN 0x0246
23 #define WM_POINTERUP 0x0247
24 #define WM_POINTERENTER 0x0249
25 #define WM_POINTERLEAVE 0x024A
26 #define WM_POINTERACTIVATE 0x024B
27 #define WM_POINTERCAPTURECHANGED 0x024C
28 #define WM_TOUCHHITTESTING 0x024D
29 #define WM_POINTERWHEEL 0x024E
30 #define WM_POINTERHWHEEL 0x024F
31 #define DM_POINTERHITTEST 0x0250
32 
33 typedef UINT32 PEN_FLAGS;
34 #define PEN_FLAG_NONE 0x00000000      // Default
35 #define PEN_FLAG_BARREL 0x00000001    // The barrel button is pressed
36 #define PEN_FLAG_INVERTED 0x00000002  // The pen is inverted
37 #define PEN_FLAG_ERASER 0x00000004    // The eraser button is pressed
38 
39 typedef UINT32 PEN_MASK;
40 #define PEN_MASK_NONE \
41   0x00000000  // Default - none of the optional fields are valid
42 #define PEN_MASK_PRESSURE 0x00000001  // The pressure field is valid
43 #define PEN_MASK_ROTATION 0x00000002  // The rotation field is valid
44 #define PEN_MASK_TILT_X 0x00000004    // The tiltX field is valid
45 #define PEN_MASK_TILT_Y 0x00000008    // The tiltY field is valid
46 
47 typedef struct tagPOINTER_PEN_INFO {
48   POINTER_INFO pointerInfo;
49   PEN_FLAGS penFlags;
50   PEN_MASK penMask;
51   UINT32 pressure;
52   UINT32 rotation;
53   INT32 tiltX;
54   INT32 tiltY;
55 } POINTER_PEN_INFO;
56 
57 /*
58  * Flags that appear in pointer input message parameters
59  */
60 #define POINTER_MESSAGE_FLAG_NEW 0x00000001          // New pointer
61 #define POINTER_MESSAGE_FLAG_INRANGE 0x00000002      // Pointer has not departed
62 #define POINTER_MESSAGE_FLAG_INCONTACT 0x00000004    // Pointer is in contact
63 #define POINTER_MESSAGE_FLAG_FIRSTBUTTON 0x00000010  // Primary action
64 #define POINTER_MESSAGE_FLAG_SECONDBUTTON 0x00000020  // Secondary action
65 #define POINTER_MESSAGE_FLAG_THIRDBUTTON 0x00000040   // Third button
66 #define POINTER_MESSAGE_FLAG_FOURTHBUTTON 0x00000080  // Fourth button
67 #define POINTER_MESSAGE_FLAG_FIFTHBUTTON 0x00000100   // Fifth button
68 #define POINTER_MESSAGE_FLAG_PRIMARY 0x00002000       // Pointer is primary
69 #define POINTER_MESSAGE_FLAG_CONFIDENCE \
70   0x00004000  // Pointer is considered unlikely to be accidental
71 #define POINTER_MESSAGE_FLAG_CANCELED \
72   0x00008000  // Pointer is departing in an abnormal manner
73 
74 /*
75  * Macros to retrieve information from pointer input message parameters
76  */
77 #define GET_POINTERID_WPARAM(wParam) (LOWORD(wParam))
78 #define IS_POINTER_FLAG_SET_WPARAM(wParam, flag) \
79   (((DWORD)HIWORD(wParam) & (flag)) == (flag))
80 #define IS_POINTER_NEW_WPARAM(wParam) \
81   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_NEW)
82 #define IS_POINTER_INRANGE_WPARAM(wParam) \
83   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_INRANGE)
84 #define IS_POINTER_INCONTACT_WPARAM(wParam) \
85   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_INCONTACT)
86 #define IS_POINTER_FIRSTBUTTON_WPARAM(wParam) \
87   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_FIRSTBUTTON)
88 #define IS_POINTER_SECONDBUTTON_WPARAM(wParam) \
89   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_SECONDBUTTON)
90 #define IS_POINTER_THIRDBUTTON_WPARAM(wParam) \
91   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_THIRDBUTTON)
92 #define IS_POINTER_FOURTHBUTTON_WPARAM(wParam) \
93   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_FOURTHBUTTON)
94 #define IS_POINTER_FIFTHBUTTON_WPARAM(wParam) \
95   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_FIFTHBUTTON)
96 #define IS_POINTER_PRIMARY_WPARAM(wParam) \
97   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_PRIMARY)
98 #define HAS_POINTER_CONFIDENCE_WPARAM(wParam) \
99   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_CONFIDENCE)
100 #define IS_POINTER_CANCELED_WPARAM(wParam) \
101   IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_CANCELED)
102 
103 /*
104  * WM_POINTERACTIVATE return codes
105  */
106 #define PA_ACTIVATE MA_ACTIVATE
107 #define PA_NOACTIVATE MA_NOACTIVATE
108 
109 #endif  // WINVER < 0x0602
110 
111 /******************************************************************************
112  * WinPointerInfo
113  *
114  * This is a helper class to handle WM_POINTER*. It only supports Win8 or later.
115  *
116  ******************************************************************************/
117 class WinPointerInfo final : public mozilla::WidgetPointerHelper {
118  public:
WinPointerInfo()119   WinPointerInfo() : WidgetPointerHelper(), mPressure(0), mButtons(0) {}
120 
WinPointerInfo(uint32_t aPointerId,uint32_t aTiltX,uint32_t aTiltY,float aPressure,int16_t aButtons)121   WinPointerInfo(uint32_t aPointerId, uint32_t aTiltX, uint32_t aTiltY,
122                  float aPressure, int16_t aButtons)
123       : WidgetPointerHelper(aPointerId, aTiltX, aTiltY),
124         mPressure(aPressure),
125         mButtons(aButtons) {}
126 
127   float mPressure;
128   int16_t mButtons;
129 };
130 
131 class WinPointerEvents final {
132  public:
133   explicit WinPointerEvents();
134 
135  public:
136   bool ShouldHandleWinPointerMessages(UINT aMsg, WPARAM aWParam);
137 
GetPointerId(WPARAM aWParam)138   uint32_t GetPointerId(WPARAM aWParam) {
139     return GET_POINTERID_WPARAM(aWParam);
140   }
141   bool GetPointerType(uint32_t aPointerId, POINTER_INPUT_TYPE *aPointerType);
142   POINTER_INPUT_TYPE GetPointerType(uint32_t aPointerId);
143   bool GetPointerInfo(uint32_t aPointerId, POINTER_INFO *aPointerInfo);
144   bool GetPointerPenInfo(uint32_t aPointerId, POINTER_PEN_INFO *aPenInfo);
145   bool ShouldEnableInkCollector();
146   bool ShouldRollupOnPointerEvent(UINT aMsg, WPARAM aWParam);
147   bool ShouldFirePointerEventByWinPointerMessages();
148   WinPointerInfo *GetCachedPointerInfo(UINT aMsg, WPARAM aWParam);
149   void ConvertAndCachePointerInfo(UINT aMsg, WPARAM aWParam);
150   void ConvertAndCachePointerInfo(WPARAM aWParam, WinPointerInfo *aInfo);
151 
152  private:
153   // Function prototypes
154   typedef BOOL(WINAPI *GetPointerTypePtr)(uint32_t aPointerId,
155                                           POINTER_INPUT_TYPE *aPointerType);
156   typedef BOOL(WINAPI *GetPointerInfoPtr)(uint32_t aPointerId,
157                                           POINTER_INFO *aPointerInfo);
158   typedef BOOL(WINAPI *GetPointerPenInfoPtr)(uint32_t aPointerId,
159                                              POINTER_PEN_INFO *aPenInfo);
160 
161   void InitLibrary();
162 
163   static HMODULE sLibraryHandle;
164   static const wchar_t kPointerLibraryName[];
165   static bool sPointerEventEnabled;
166   static bool sFirePointerEventsByWinPointerMessages;
167   // Static function pointers
168   static GetPointerTypePtr getPointerType;
169   static GetPointerInfoPtr getPointerInfo;
170   static GetPointerPenInfoPtr getPointerPenInfo;
171   WinPointerInfo mPenPointerDownInfo;
172   WinPointerInfo mPenPointerUpInfo;
173   WinPointerInfo mPenPointerUpdateInfo;
174 };
175 
176 #endif  // #ifndef WinPointerEvents_h__
177