summaryrefslogtreecommitdiffstats
path: root/widget/windows/WinPointerEvents.cpp
blob: c0465da7d79627db777358c0b599ec501b15a8d8 (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

/*
 * WinPointerEvents - Helper functions to retrieve PointerEvent's attributes
 */

#include "nscore.h"
#include "nsWindowDefs.h"
#include "WinPointerEvents.h"
#include "WinUtils.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/WindowsVersion.h"
#include "mozilla/dom/MouseEventBinding.h"

using namespace mozilla;
using namespace mozilla::widget;

const wchar_t WinPointerEvents::kPointerLibraryName[] = L"user32.dll";
HMODULE WinPointerEvents::sLibraryHandle = nullptr;
WinPointerEvents::GetPointerTypePtr WinPointerEvents::getPointerType = nullptr;
WinPointerEvents::GetPointerInfoPtr WinPointerEvents::getPointerInfo = nullptr;
WinPointerEvents::GetPointerPenInfoPtr WinPointerEvents::getPointerPenInfo =
    nullptr;

WinPointerEvents::WinPointerEvents() { InitLibrary(); }

/* Load and shutdown */
void WinPointerEvents::InitLibrary() {
  MOZ_ASSERT(XRE_IsParentProcess());
  if (!IsWin8OrLater()) {
    // Only Win8 or later supports WM_POINTER*
    return;
  }
  if (getPointerType) {
    // Return if we already initialized the PointerEvent related interfaces
    return;
  }
  sLibraryHandle = ::LoadLibraryW(kPointerLibraryName);
  MOZ_ASSERT(sLibraryHandle, "cannot load pointer library");
  if (sLibraryHandle) {
    getPointerType =
        (GetPointerTypePtr)GetProcAddress(sLibraryHandle, "GetPointerType");
    getPointerInfo =
        (GetPointerInfoPtr)GetProcAddress(sLibraryHandle, "GetPointerInfo");
    getPointerPenInfo = (GetPointerPenInfoPtr)GetProcAddress(
        sLibraryHandle, "GetPointerPenInfo");
  }

  if (!getPointerType || !getPointerInfo || !getPointerPenInfo) {
    MOZ_ASSERT(false, "get PointerEvent interfaces failed");
    getPointerType = nullptr;
    getPointerInfo = nullptr;
    getPointerPenInfo = nullptr;
    return;
  }
}

bool WinPointerEvents::ShouldHandleWinPointerMessages(UINT aMsg,
                                                      WPARAM aWParam) {
  MOZ_ASSERT(aMsg == WM_POINTERDOWN || aMsg == WM_POINTERUP ||
             aMsg == WM_POINTERUPDATE || aMsg == WM_POINTERLEAVE);
  if (!sLibraryHandle) {
    return false;
  }

  // We only handle WM_POINTER* when the input source is pen. This is because
  // we need some information (e.g. tiltX, tiltY) which can't be retrieved by
  // WM_*BUTTONDOWN.
  uint32_t pointerId = GetPointerId(aWParam);
  POINTER_INPUT_TYPE pointerType = PT_POINTER;
  if (!GetPointerType(pointerId, &pointerType)) {
    MOZ_ASSERT(false, "cannot find PointerType");
    return false;
  }
  return (pointerType == PT_PEN);
}

bool WinPointerEvents::GetPointerType(uint32_t aPointerId,
                                      POINTER_INPUT_TYPE* aPointerType) {
  if (!getPointerType) {
    return false;
  }
  return getPointerType(aPointerId, aPointerType);
}

POINTER_INPUT_TYPE
WinPointerEvents::GetPointerType(uint32_t aPointerId) {
  POINTER_INPUT_TYPE pointerType = PT_POINTER;
  Unused << GetPointerType(aPointerId, &pointerType);
  return pointerType;
}

bool WinPointerEvents::GetPointerInfo(uint32_t aPointerId,
                                      POINTER_INFO* aPointerInfo) {
  if (!getPointerInfo) {
    return false;
  }
  return getPointerInfo(aPointerId, aPointerInfo);
}

bool WinPointerEvents::GetPointerPenInfo(uint32_t aPointerId,
                                         POINTER_PEN_INFO* aPenInfo) {
  if (!getPointerPenInfo) {
    return false;
  }
  return getPointerPenInfo(aPointerId, aPenInfo);
}

bool WinPointerEvents::ShouldEnableInkCollector() {
  // We need InkCollector on Win7. For Win8 or later, we handle WM_POINTER* for
  // pen.
  return !IsWin8OrLater();
}

bool WinPointerEvents::ShouldRollupOnPointerEvent(UINT aMsg, WPARAM aWParam) {
  MOZ_ASSERT(aMsg == WM_POINTERDOWN);
  // Only roll up popups when we handling WM_POINTER* to fire Gecko
  // WidgetMouseEvent and suppress Windows WM_*BUTTONDOWN.
  return ShouldHandleWinPointerMessages(aMsg, aWParam) &&
         ShouldFirePointerEventByWinPointerMessages();
}

bool WinPointerEvents::ShouldFirePointerEventByWinPointerMessages() {
  MOZ_ASSERT(sLibraryHandle);
  return StaticPrefs::dom_w3c_pointer_events_dispatch_by_pointer_messages();
}

WinPointerInfo* WinPointerEvents::GetCachedPointerInfo(UINT aMsg,
                                                       WPARAM aWParam) {
  if (!sLibraryHandle ||
      MOUSE_INPUT_SOURCE() != dom::MouseEvent_Binding::MOZ_SOURCE_PEN ||
      ShouldFirePointerEventByWinPointerMessages()) {
    return nullptr;
  }
  switch (aMsg) {
    case WM_LBUTTONDOWN:
    case WM_MBUTTONDOWN:
    case WM_RBUTTONDOWN:
      return &mPenPointerDownInfo;
    case WM_LBUTTONUP:
    case WM_MBUTTONUP:
    case WM_RBUTTONUP:
      return &mPenPointerDownInfo;
    case WM_MOUSEMOVE:
      return &mPenPointerUpdateInfo;
    default:
      MOZ_ASSERT(false);
  }
  return nullptr;
}

void WinPointerEvents::ConvertAndCachePointerInfo(UINT aMsg, WPARAM aWParam) {
  MOZ_ASSERT(
      !StaticPrefs::dom_w3c_pointer_events_dispatch_by_pointer_messages());
  // Windows doesn't support chorded buttons for pen, so we can simply keep the
  // latest information from pen generated pointer messages and use them when
  // handling mouse messages. Used different pointer info for pointerdown,
  // pointerupdate, and pointerup because Windows doesn't always interleave
  // pointer messages and mouse messages.
  switch (aMsg) {
    case WM_POINTERDOWN:
      ConvertAndCachePointerInfo(aWParam, &mPenPointerDownInfo);
      break;
    case WM_POINTERUP:
      ConvertAndCachePointerInfo(aWParam, &mPenPointerUpInfo);
      break;
    case WM_POINTERUPDATE:
      ConvertAndCachePointerInfo(aWParam, &mPenPointerUpdateInfo);
      break;
    default:
      break;
  }
}

void WinPointerEvents::ConvertAndCachePointerInfo(WPARAM aWParam,
                                                  WinPointerInfo* aInfo) {
  MOZ_ASSERT(
      !StaticPrefs::dom_w3c_pointer_events_dispatch_by_pointer_messages());
  aInfo->pointerId = GetPointerId(aWParam);
  MOZ_ASSERT(GetPointerType(aInfo->pointerId) == PT_PEN);
  POINTER_PEN_INFO penInfo;
  GetPointerPenInfo(aInfo->pointerId, &penInfo);
  aInfo->tiltX = penInfo.tiltX;
  aInfo->tiltY = penInfo.tiltY;
  // Windows defines the pen pressure is normalized to a range between 0 and
  // 1024. Convert it to float.
  aInfo->mPressure = penInfo.pressure ? (float)penInfo.pressure / 1024 : 0;
}