summaryrefslogtreecommitdiffstats
path: root/widget/windows/InkCollector.cpp
blob: 9871b966155ad7e6834c927728b9a58938bb93c3 (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
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
/* -*- 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/.
*/

#include "InkCollector.h"

using namespace mozilla;

// Msinkaut_i.c and Msinkaut.h should both be included
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms695519.aspx
#include <msinkaut_i.c>

StaticAutoPtr<InkCollector> InkCollector::sInkCollector;

InkCollector::~InkCollector()
{
  Shutdown();
  MOZ_ASSERT(!mCookie && !mEnabled && !mComInitialized
              && !mMarshaller && !mInkCollector
              && !mConnectionPoint && !mInkCollectorEvent);
}

void InkCollector::Initialize()
{
  // Possibly, we can use mConnectionPoint for checking,
  // But if errors exist (perhaps COM object is unavailable),
  // Initialize() will be called more times.
  static bool sInkCollectorCreated = false;
  if (sInkCollectorCreated) {
    return;
  }
  sInkCollectorCreated = true;

  // COM could get uninitialized due to previous initialization.
  mComInitialized = SUCCEEDED(::CoInitialize(nullptr));

  // Set up instance of InkCollectorEvent.
  mInkCollectorEvent = new InkCollectorEvent();

  // Set up a free threaded marshaler.
  if (FAILED(::CoCreateFreeThreadedMarshaler(mInkCollectorEvent, getter_AddRefs(mMarshaller)))) {
    return;
  }

  // Create the ink collector.
  if (FAILED(::CoCreateInstance(CLSID_InkCollector, NULL, CLSCTX_INPROC_SERVER,
                                IID_IInkCollector, getter_AddRefs(mInkCollector)))) {
    return;
  }

  // Set up connection between sink and InkCollector.
  RefPtr<IConnectionPointContainer> connPointContainer;

  // Get the connection point container.
  if (SUCCEEDED(mInkCollector->QueryInterface(IID_IConnectionPointContainer,
                                              getter_AddRefs(connPointContainer)))) {

    // Find the connection point for Ink Collector events.
    if (SUCCEEDED(connPointContainer->FindConnectionPoint(__uuidof(_IInkCollectorEvents),
                                                          getter_AddRefs(mConnectionPoint)))) {

      // Hook up sink to connection point.
      if (SUCCEEDED(mConnectionPoint->Advise(mInkCollectorEvent, &mCookie))) {
        OnInitialize();
      }
    }
  }
}

void InkCollector::Shutdown()
{
  Enable(false);
  if (mConnectionPoint) {
    // Remove the connection of the sink to the Ink Collector.
    mConnectionPoint->Unadvise(mCookie);
    mCookie = 0;
    mConnectionPoint = nullptr;
  }
  mInkCollector = nullptr;
  mMarshaller = nullptr;
  mInkCollectorEvent = nullptr;

  // Let uninitialization get handled in a place where it got inited.
  if (mComInitialized) {
    CoUninitialize();
    mComInitialized = false;
  }
}

void InkCollector::OnInitialize()
{
  // Suppress all events to do not allow performance decreasing.
  // https://msdn.microsoft.com/en-us/library/ms820347.aspx
  mInkCollector->SetEventInterest(InkCollectorEventInterest::ICEI_AllEvents, VARIANT_FALSE);

  // Sets a value that indicates whether an object or control has interest in a specified event.
  mInkCollector->SetEventInterest(InkCollectorEventInterest::ICEI_CursorOutOfRange, VARIANT_TRUE);

  // If the MousePointer property is set to IMP_Custom and the MouseIcon property is NULL,
  // Then the ink collector no longer handles mouse cursor settings.
  // https://msdn.microsoft.com/en-us/library/windows/desktop/ms700686.aspx
  mInkCollector->put_MouseIcon(nullptr);
  mInkCollector->put_MousePointer(InkMousePointer::IMP_Custom);

  // This mode allows an ink collector to collect ink from any tablet attached to the Tablet PC.
  // The Boolean value that indicates whether to use the mouse as an input device.
  // If TRUE, the mouse is used for input.
  // https://msdn.microsoft.com/en-us/library/ms820346.aspx
  mInkCollector->SetAllTabletsMode(VARIANT_FALSE);

  // Sets the value that specifies whether ink is rendered as it is drawn.
  // VARIANT_TRUE to render ink as it is drawn on the display.
  // VARIANT_FALSE to not have the ink appear on the display as strokes are made.
  // https://msdn.microsoft.com/en-us/library/windows/desktop/dd314598.aspx
  mInkCollector->put_DynamicRendering(VARIANT_FALSE);
}

// Sets a value that specifies whether the InkCollector object collects pen input.
// This property must be set to FALSE before setting or
// calling specific properties and methods of the object.
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms701721.aspx
void InkCollector::Enable(bool aNewState)
{
  if (aNewState != mEnabled) {
    if (mInkCollector) {
      if (SUCCEEDED(mInkCollector->put_Enabled(aNewState ? VARIANT_TRUE : VARIANT_FALSE))) {
        mEnabled = aNewState;
      } else {
        NS_WARNING("InkCollector did not change status successfully");
      }
    } else {
      NS_WARNING("InkCollector should be exist");
    }
  }
}

HWND InkCollector::GetTarget()
{
  return mTargetWindow;
}

void InkCollector::SetTarget(HWND aTargetWindow)
{
  NS_ASSERTION(aTargetWindow, "aTargetWindow should be exist");
  if (aTargetWindow && (aTargetWindow != mTargetWindow)) {
    Initialize();
    if (mInkCollector) {
      Enable(false);
      if (SUCCEEDED(mInkCollector->put_hWnd((LONG_PTR)aTargetWindow))) {
        mTargetWindow = aTargetWindow;
      } else {
        NS_WARNING("InkCollector did not change window property successfully");
      }
      Enable(true);
    }
  }
}

void InkCollector::ClearTarget()
{
  if (mTargetWindow && mInkCollector) {
    Enable(false);
    if (SUCCEEDED(mInkCollector->put_hWnd(0))) {
      mTargetWindow = 0;
    } else {
      NS_WARNING("InkCollector did not clear window property successfully");
    }
  }
}

uint16_t InkCollector::GetPointerId()
{
  return mPointerId;
}

void InkCollector::SetPointerId(uint16_t aPointerId)
{
  mPointerId = aPointerId;
}

void InkCollector::ClearPointerId()
{
  mPointerId = 0;
}

// The display and the digitizer have quite different properties.
// The display has CursorMustTouch, the mouse pointer alway touches the display surface.
// The digitizer lists Integrated and HardProximity.
// When the stylus is in the proximity of the tablet its movements are also detected.
// An external tablet will only list HardProximity.
bool InkCollectorEvent::IsHardProximityTablet(IInkTablet* aTablet) const
{
  if (aTablet) {
    TabletHardwareCapabilities caps;
    if (SUCCEEDED(aTablet->get_HardwareCapabilities(&caps))) {
      return (TabletHardwareCapabilities::THWC_HardProximity & caps);
    }
  }
  return false;
}

HRESULT __stdcall InkCollectorEvent::QueryInterface(REFIID aRiid, void **aObject)
{
  // Validate the input
  if (!aObject) {
    return E_POINTER;
  }
  HRESULT result = E_NOINTERFACE;
  // This object supports IUnknown/IDispatch/IInkCollectorEvents
  if ((IID_IUnknown == aRiid) ||
      (IID_IDispatch == aRiid) ||
      (DIID__IInkCollectorEvents == aRiid)) {
    *aObject = this;
    // AddRef should be called when we give info about interface
    NS_ADDREF_THIS();
    result = S_OK;
  }
  return result;
}

HRESULT InkCollectorEvent::Invoke(DISPID aDispIdMember, REFIID /*aRiid*/,
                                  LCID /*aId*/, WORD /*wFlags*/,
                                  DISPPARAMS* aDispParams, VARIANT* /*aVarResult*/,
                                  EXCEPINFO* /*aExcepInfo*/, UINT* /*aArgErr*/)
{
  switch (aDispIdMember) {
    case DISPID_ICECursorOutOfRange: {
      if (aDispParams && aDispParams->cArgs) {
        CursorOutOfRange(static_cast<IInkCursor*>(aDispParams->rgvarg[0].pdispVal));
      }
      break;
    }
  }
  return S_OK;
}

void InkCollectorEvent::CursorOutOfRange(IInkCursor* aCursor) const
{
  IInkTablet* curTablet = nullptr;
  if (FAILED(aCursor->get_Tablet(&curTablet))) {
    return;
  }
  // All events should be suppressed except
  // from tablets with hard proximity.
  if (!IsHardProximityTablet(curTablet)) {
    return;
  }
  // Notify current target window.
  if (HWND targetWindow = InkCollector::sInkCollector->GetTarget()) {
    ::SendMessage(targetWindow, MOZ_WM_PEN_LEAVES_HOVER_OF_DIGITIZER, 0, 0);
  }
}