Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / host / touch_injector_win.cc
blob3eaadf476ef1cefdb88dbcfe27acb7cd12552aec
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/host/touch_injector_win.h"
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/native_library.h"
10 #include "base/stl_util.h"
11 #include "remoting/proto/event.pb.h"
13 namespace remoting {
15 using protocol::TouchEvent;
16 using protocol::TouchEventPoint;
18 namespace {
20 typedef BOOL(NTAPI* InitializeTouchInjectionFunction)(UINT32, DWORD);
21 typedef BOOL(NTAPI* InjectTouchInputFunction)(UINT32,
22 const POINTER_TOUCH_INFO*);
23 const uint32_t kMaxSimultaneousTouchCount = 10;
25 // This is used to reinject all points that have not changed as "move"ed points,
26 // even if they have not actually moved.
27 // This is required for multi-touch to work, e.g. pinching and zooming gestures
28 // (handled by apps) won't work without reinjecting the points, even though the
29 // user moved only one finger and held the other finger in place.
30 void AppendMapValuesToVector(
31 std::map<uint32_t, POINTER_TOUCH_INFO>* touches_in_contact,
32 std::vector<POINTER_TOUCH_INFO>* output_vector) {
33 for (auto& id_and_pointer_touch_info : *touches_in_contact) {
34 POINTER_TOUCH_INFO& pointer_touch_info = id_and_pointer_touch_info.second;
35 output_vector->push_back(pointer_touch_info);
39 // The caller should set memset(0) the struct and set
40 // pointer_touch_info->pointerInfo.pointerFlags.
41 void ConvertToPointerTouchInfo(
42 const TouchEventPoint& touch_point,
43 POINTER_TOUCH_INFO* pointer_touch_info) {
44 pointer_touch_info->touchMask =
45 TOUCH_MASK_CONTACTAREA | TOUCH_MASK_ORIENTATION;
46 pointer_touch_info->touchFlags = TOUCH_FLAG_NONE;
48 // Although radius_{x,y} can be undefined (i.e. has_radius_{x,y} == false),
49 // the default value (0.0) will set the area correctly.
50 // MSDN mentions that if the digitizer does not detect the size of the touch
51 // point, rcContact should be set to 0 by 0 rectangle centered at the
52 // coordinate.
53 pointer_touch_info->rcContact.left =
54 touch_point.x() - touch_point.radius_x();
55 pointer_touch_info->rcContact.top = touch_point.y() - touch_point.radius_y();
56 pointer_touch_info->rcContact.right =
57 touch_point.x() + touch_point.radius_x();
58 pointer_touch_info->rcContact.bottom =
59 touch_point.y() + touch_point.radius_y();
61 pointer_touch_info->orientation = touch_point.angle();
63 if (touch_point.has_pressure()) {
64 pointer_touch_info->touchMask |= TOUCH_MASK_PRESSURE;
65 const float kMinimumPressure = 0.0;
66 const float kMaximumPressure = 1.0;
67 const float clamped_touch_point_pressure =
68 std::max(kMinimumPressure,
69 std::min(kMaximumPressure, touch_point.pressure()));
71 const int kWindowsMaxTouchPressure = 1024; // Defined in MSDN.
72 const int pressure =
73 clamped_touch_point_pressure * kWindowsMaxTouchPressure;
74 pointer_touch_info->pressure = pressure;
77 pointer_touch_info->pointerInfo.pointerType = PT_TOUCH;
78 pointer_touch_info->pointerInfo.pointerId = touch_point.id();
79 pointer_touch_info->pointerInfo.ptPixelLocation.x = touch_point.x();
80 pointer_touch_info->pointerInfo.ptPixelLocation.y = touch_point.y();
83 } // namespace
85 TouchInjectorWinDelegate::~TouchInjectorWinDelegate() {}
87 // static.
88 scoped_ptr<TouchInjectorWinDelegate> TouchInjectorWinDelegate::Create() {
89 base::ScopedNativeLibrary library(base::FilePath(L"User32.dll"));
90 if (!library.is_valid()) {
91 PLOG(INFO) << "Failed to get library module for touch injection functions.";
92 return scoped_ptr<TouchInjectorWinDelegate>();
95 InitializeTouchInjectionFunction init_func =
96 reinterpret_cast<InitializeTouchInjectionFunction>(
97 library.GetFunctionPointer("InitializeTouchInjection"));
98 if (!init_func) {
99 PLOG(INFO) << "Failed to get InitializeTouchInjection function handle.";
100 return scoped_ptr<TouchInjectorWinDelegate>();
103 InjectTouchInputFunction inject_touch_func =
104 reinterpret_cast<InjectTouchInputFunction>(
105 library.GetFunctionPointer("InjectTouchInput"));
106 if (!inject_touch_func) {
107 PLOG(INFO) << "Failed to get InjectTouchInput.";
108 return scoped_ptr<TouchInjectorWinDelegate>();
111 return scoped_ptr<TouchInjectorWinDelegate>(
112 new TouchInjectorWinDelegate(
113 library.Release(), init_func, inject_touch_func));
116 TouchInjectorWinDelegate::TouchInjectorWinDelegate(
117 base::NativeLibrary library,
118 InitializeTouchInjectionFunction initialize_touch_injection_func,
119 InjectTouchInputFunction inject_touch_input_func)
120 : library_module_(library),
121 initialize_touch_injection_func_(initialize_touch_injection_func),
122 inject_touch_input_func_(inject_touch_input_func) {}
124 BOOL TouchInjectorWinDelegate::InitializeTouchInjection(UINT32 max_count,
125 DWORD dw_mode) {
126 return initialize_touch_injection_func_(max_count, dw_mode);
129 DWORD TouchInjectorWinDelegate::InjectTouchInput(
130 UINT32 count,
131 const POINTER_TOUCH_INFO* contacts) {
132 return inject_touch_input_func_(count, contacts);
135 TouchInjectorWin::TouchInjectorWin()
136 : delegate_(TouchInjectorWinDelegate::Create()) {}
138 TouchInjectorWin::~TouchInjectorWin() {}
140 // Note that TouchInjectorWinDelegate::Create() is not called in this method
141 // so that a mock delegate can be injected in tests and set expectations on the
142 // mock and return value of this method.
143 bool TouchInjectorWin::Init() {
144 if (!delegate_)
145 return false;
147 if (!delegate_->InitializeTouchInjection(
148 kMaxSimultaneousTouchCount, TOUCH_FEEDBACK_DEFAULT)) {
149 // delagate_ is reset here so that the function that need the delegate
150 // can check if it is null.
151 delegate_.reset();
152 PLOG(INFO) << "Failed to initialize touch injection.";
153 return false;
156 return true;
159 void TouchInjectorWin::Deinitialize() {
160 touches_in_contact_.clear();
161 // Same reason as TouchInjectorWin::Init(). For injecting mock delegates for
162 // tests, a new delegate is created here.
163 delegate_ = TouchInjectorWinDelegate::Create();
166 void TouchInjectorWin::InjectTouchEvent(const TouchEvent& event) {
167 if (!delegate_) {
168 VLOG(3) << "Touch injection functions are not initialized.";
169 return;
172 switch (event.event_type()) {
173 case TouchEvent::TOUCH_POINT_START:
174 AddNewTouchPoints(event);
175 break;
176 case TouchEvent::TOUCH_POINT_MOVE:
177 MoveTouchPoints(event);
178 break;
179 case TouchEvent::TOUCH_POINT_END:
180 EndTouchPoints(event);
181 break;
182 case TouchEvent::TOUCH_POINT_CANCEL:
183 CancelTouchPoints(event);
184 break;
185 default:
186 NOTREACHED();
187 return;
191 void TouchInjectorWin::SetInjectorDelegateForTest(
192 scoped_ptr<TouchInjectorWinDelegate> functions) {
193 delegate_ = functions.Pass();
196 void TouchInjectorWin::AddNewTouchPoints(const TouchEvent& event) {
197 DCHECK_EQ(event.event_type(), TouchEvent::TOUCH_POINT_START);
199 std::vector<POINTER_TOUCH_INFO> touches;
200 // Must inject already touching points as move events.
201 AppendMapValuesToVector(&touches_in_contact_, &touches);
203 for (const TouchEventPoint& touch_point : event.touch_points()) {
204 POINTER_TOUCH_INFO pointer_touch_info;
205 memset(&pointer_touch_info, 0, sizeof(pointer_touch_info));
206 pointer_touch_info.pointerInfo.pointerFlags =
207 POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_DOWN;
208 ConvertToPointerTouchInfo(touch_point, &pointer_touch_info);
209 touches.push_back(pointer_touch_info);
211 // All points in the map should be a move point.
212 pointer_touch_info.pointerInfo.pointerFlags =
213 POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_UPDATE;
214 touches_in_contact_[touch_point.id()] = pointer_touch_info;
217 if (delegate_->InjectTouchInput(touches.size(),
218 vector_as_array(&touches)) == 0) {
219 PLOG(ERROR) << "Failed to inject a touch start event.";
223 void TouchInjectorWin::MoveTouchPoints(const TouchEvent& event) {
224 DCHECK_EQ(event.event_type(), TouchEvent::TOUCH_POINT_MOVE);
226 for (const TouchEventPoint& touch_point : event.touch_points()) {
227 POINTER_TOUCH_INFO* pointer_touch_info =
228 &touches_in_contact_[touch_point.id()];
229 memset(pointer_touch_info, 0, sizeof(*pointer_touch_info));
230 pointer_touch_info->pointerInfo.pointerFlags =
231 POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_UPDATE;
232 ConvertToPointerTouchInfo(touch_point, pointer_touch_info);
235 std::vector<POINTER_TOUCH_INFO> touches;
236 // Must inject already touching points as move events.
237 AppendMapValuesToVector(&touches_in_contact_, &touches);
238 if (delegate_->InjectTouchInput(touches.size(),
239 vector_as_array(&touches)) == 0) {
240 PLOG(ERROR) << "Failed to inject a touch move event.";
244 void TouchInjectorWin::EndTouchPoints(const TouchEvent& event) {
245 DCHECK_EQ(event.event_type(), TouchEvent::TOUCH_POINT_END);
247 std::vector<POINTER_TOUCH_INFO> touches;
248 for (const TouchEventPoint& touch_point : event.touch_points()) {
249 POINTER_TOUCH_INFO pointer_touch_info =
250 touches_in_contact_[touch_point.id()];
251 pointer_touch_info.pointerInfo.pointerFlags = POINTER_FLAG_UP;
253 touches_in_contact_.erase(touch_point.id());
254 touches.push_back(pointer_touch_info);
257 AppendMapValuesToVector(&touches_in_contact_, &touches);
258 if (delegate_->InjectTouchInput(touches.size(),
259 vector_as_array(&touches)) == 0) {
260 PLOG(ERROR) << "Failed to inject a touch end event.";
264 void TouchInjectorWin::CancelTouchPoints(const TouchEvent& event) {
265 DCHECK_EQ(event.event_type(), TouchEvent::TOUCH_POINT_CANCEL);
267 std::vector<POINTER_TOUCH_INFO> touches;
268 for (const TouchEventPoint& touch_point : event.touch_points()) {
269 POINTER_TOUCH_INFO pointer_touch_info =
270 touches_in_contact_[touch_point.id()];
271 pointer_touch_info.pointerInfo.pointerFlags =
272 POINTER_FLAG_UP | POINTER_FLAG_CANCELED;
274 touches_in_contact_.erase(touch_point.id());
275 touches.push_back(pointer_touch_info);
278 AppendMapValuesToVector(&touches_in_contact_, &touches);
279 if (delegate_->InjectTouchInput(touches.size(),
280 vector_as_array(&touches)) == 0) {
281 PLOG(ERROR) << "Failed to inject a touch cancel event.";
285 } // namespace remoting