Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(659)

Side by Side Diff: remoting/host/touch_injector_win.cc

Issue 991643002: Windows Host Touch Injection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix comments and check pressure clamping in unit test Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "remoting/host/touch_injector_win.h"
6
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"
12
13 namespace remoting {
14
15 using protocol::TouchEvent;
16 using protocol::TouchEventPoint;
17
18 namespace {
19
20 typedef BOOL(NTAPI* InitializeTouchInjectionFunction)(UINT32, DWORD);
21 typedef BOOL(NTAPI* InjectTouchInputFunction)(UINT32,
22 const POINTER_TOUCH_INFO*);
23 const uint32_t kMaxSimultaneousTouchCount = 10;
24
25 // This is used to reinject all points that have not changed as "move"ed point,
Wez 2015/03/21 01:10:09 typo: points
Rintaro Kuroiwa 2015/03/24 20:34:47 Done.
26 // even if it has not actually moved.
Wez 2015/03/21 01:10:09 s/it has/they have
Rintaro Kuroiwa 2015/03/24 20:34:47 Done.
27 // This is required for multi-touch to work, e.g. pinching and zooming gestures
28 // (handled by apps) won't work without reinjection the points even though the
Wez 2015/03/21 01:10:09 s/reinjection/reinjecting
Rintaro Kuroiwa 2015/03/24 20:34:47 Done.
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);
36 }
37 }
38
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;
47
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();
60
61 pointer_touch_info->orientation = touch_point.angle();
62
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 float clamped_touch_point_pressure = touch_point.pressure();
Wez 2015/03/21 01:10:08 Use std::min and std::max here?
Rintaro Kuroiwa 2015/03/24 20:34:47 Done.
68 if (clamped_touch_point_pressure < kMinimumPressure) {
69 clamped_touch_point_pressure = kMinimumPressure;
70 } else if (clamped_touch_point_pressure > kMaximumPressure) {
71 clamped_touch_point_pressure = kMaximumPressure;
72 }
73
74 const int kWindowsMaxTouchPressure = 1024; // Defined in MSDN.
75 const int pressure =
76 clamped_touch_point_pressure * kWindowsMaxTouchPressure;
77 pointer_touch_info->pressure = pressure;
78 }
79
80 pointer_touch_info->pointerInfo.pointerType = PT_TOUCH;
81 pointer_touch_info->pointerInfo.pointerId = touch_point.id();
82 pointer_touch_info->pointerInfo.ptPixelLocation.x = touch_point.x();
83 pointer_touch_info->pointerInfo.ptPixelLocation.y = touch_point.y();
84 }
85
86 } // namespace
87
88 TouchInjectorWinDelegate::~TouchInjectorWinDelegate() {}
89
90 // static.
91 scoped_ptr<TouchInjectorWinDelegate> TouchInjectorWinDelegate::Create() {
92 base::ScopedNativeLibrary library(base::FilePath(L"User32.dll"));
93 if (!library.is_valid()) {
94 PLOG(INFO) << "Failed to get library module for touch injection functions.";
95 return scoped_ptr<TouchInjectorWinDelegate>();
96 }
97
98 InitializeTouchInjectionFunction init_func =
99 reinterpret_cast<InitializeTouchInjectionFunction>(
100 library.GetFunctionPointer("InitializeTouchInjection"));
101 if (!init_func) {
102 PLOG(INFO) << "Failed to get InitializeTouchInjection function handle.";
103 return scoped_ptr<TouchInjectorWinDelegate>();
104 }
105
106 InjectTouchInputFunction inject_touch_func =
107 reinterpret_cast<InjectTouchInputFunction>(
108 library.GetFunctionPointer("InjectTouchInput"));
109 if (!inject_touch_func) {
110 PLOG(INFO) << "Failed to get InjectTouchInput.";
111 return scoped_ptr<TouchInjectorWinDelegate>();
112 }
113
114 return scoped_ptr<TouchInjectorWinDelegate>(
115 new TouchInjectorWinDelegate(
116 library.Release(), init_func, inject_touch_func));
117 }
118
119 TouchInjectorWinDelegate::TouchInjectorWinDelegate(
120 base::NativeLibrary library,
121 InitializeTouchInjectionFunction initialize_touch_injection_func,
122 InjectTouchInputFunction inject_touch_input_func)
123 : library_module_(library),
124 initialize_touch_injection_func_(initialize_touch_injection_func),
125 inject_touch_input_func_(inject_touch_input_func) {}
126
127 BOOL TouchInjectorWinDelegate::InitializeTouchInjection(UINT32 max_count,
128 DWORD dw_mode) {
129 return initialize_touch_injection_func_(max_count, dw_mode);
130 }
131
132 DWORD TouchInjectorWinDelegate::InjectTouchInput(
133 UINT32 count,
134 const POINTER_TOUCH_INFO* contacts) {
135 return inject_touch_input_func_(count, contacts);
136 }
137
138 TouchInjectorWin::TouchInjectorWin()
139 : delegate_(TouchInjectorWinDelegate::Create()) {}
140
141 TouchInjectorWin::~TouchInjectorWin() {}
142
143 // Note that TouchInjectorWinDelegate::Create() is not called in this method
144 // so that a mock delegate can be injected in tests and set expectations on the
145 // mock and return value of this method.
146 bool TouchInjectorWin::Init() {
147 if (!delegate_)
148 return false;
149
150 if (!delegate_->InitializeTouchInjection(
151 kMaxSimultaneousTouchCount, TOUCH_FEEDBACK_DEFAULT)) {
152 // delagate_ is reset here so that the function that need the delegate
153 // can check if it is null.
154 delegate_.reset();
155 PLOG(INFO) << "Failed to initialize touch injection.";
156 return false;
157 }
158
159 return true;
160 }
161
162 void TouchInjectorWin::Deinitialize() {
163 touches_in_contact_.clear();
164 // Same reason as TouchInjectorWin::Init(). For injecting mock delegates for
165 // tests, a new delegate is created here.
166 delegate_ = TouchInjectorWinDelegate::Create();
167 }
168
169 void TouchInjectorWin::InjectTouchEvent(const TouchEvent& event) {
170 if (!delegate_) {
171 VLOG(3) << "Touch injection functions are not initialized.";
172 return;
173 }
174
175 switch (event.event_type()) {
176 case TouchEvent::TOUCH_POINT_START:
177 AddNewTouchPoints(event);
178 break;
179 case TouchEvent::TOUCH_POINT_MOVE:
180 MoveTouchPoints(event);
181 break;
182 case TouchEvent::TOUCH_POINT_END:
183 EndTouchPoints(event);
184 break;
185 case TouchEvent::TOUCH_POINT_CANCEL:
186 CancelTouchPoints(event);
187 break;
188 default:
189 NOTREACHED();
190 return;
191 }
192 }
193
194 void TouchInjectorWin::SetInjectorDelegateForTest(
195 scoped_ptr<TouchInjectorWinDelegate> functions) {
196 delegate_ = functions.Pass();
197 }
198
199 void TouchInjectorWin::AddNewTouchPoints(const TouchEvent& event) {
200 DCHECK_EQ(event.event_type(), TouchEvent::TOUCH_POINT_START);
201
202 std::vector<POINTER_TOUCH_INFO> touches;
203 // Must inject already touching points as move events.
204 AppendMapValuesToVector(&touches_in_contact_, &touches);
205
206 for (const TouchEventPoint& touch_point : event.touch_points()) {
207 POINTER_TOUCH_INFO pointer_touch_info;
208 memset(&pointer_touch_info, 0, sizeof(pointer_touch_info));
209 pointer_touch_info.pointerInfo.pointerFlags =
210 POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_DOWN;
211 ConvertToPointerTouchInfo(touch_point, &pointer_touch_info);
212 touches.push_back(pointer_touch_info);
213
214 // All points in the map should be a move point.
215 pointer_touch_info.pointerInfo.pointerFlags =
216 POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_UPDATE;
217 touches_in_contact_[touch_point.id()] = pointer_touch_info;
218 }
219
220 if (delegate_->InjectTouchInput(touches.size(),
221 vector_as_array(&touches)) == 0) {
222 PLOG(ERROR) << "Failed to inject a touch start event.";
223 }
224 }
225
226 void TouchInjectorWin::MoveTouchPoints(const TouchEvent& event) {
227 DCHECK_EQ(event.event_type(), TouchEvent::TOUCH_POINT_MOVE);
228
229 for (const TouchEventPoint& touch_point : event.touch_points()) {
230 POINTER_TOUCH_INFO* pointer_touch_info =
231 &touches_in_contact_[touch_point.id()];
232 memset(pointer_touch_info, 0, sizeof(*pointer_touch_info));
233 pointer_touch_info->pointerInfo.pointerFlags =
234 POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT | POINTER_FLAG_UPDATE;
235 ConvertToPointerTouchInfo(touch_point, pointer_touch_info);
236 }
237
238 std::vector<POINTER_TOUCH_INFO> touches;
239 // Must inject already touching points as move events.
240 AppendMapValuesToVector(&touches_in_contact_, &touches);
241 if (delegate_->InjectTouchInput(touches.size(),
242 vector_as_array(&touches)) == 0) {
243 PLOG(ERROR) << "Failed to inject a touch move event.";
244 }
245 }
246
247 void TouchInjectorWin::EndTouchPoints(const TouchEvent& event) {
248 DCHECK_EQ(event.event_type(), TouchEvent::TOUCH_POINT_END);
249
250 std::vector<POINTER_TOUCH_INFO> touches;
251 for (const TouchEventPoint& touch_point : event.touch_points()) {
252 POINTER_TOUCH_INFO pointer_touch_info =
253 touches_in_contact_[touch_point.id()];
254 pointer_touch_info.pointerInfo.pointerFlags = POINTER_FLAG_UP;
255
256 touches_in_contact_.erase(touch_point.id());
257 touches.push_back(pointer_touch_info);
258 }
259
260 AppendMapValuesToVector(&touches_in_contact_, &touches);
261 if (delegate_->InjectTouchInput(touches.size(),
262 vector_as_array(&touches)) == 0) {
263 PLOG(ERROR) << "Failed to inject a touch end event.";
264 }
265 }
266
267 void TouchInjectorWin::CancelTouchPoints(const TouchEvent& event) {
268 DCHECK_EQ(event.event_type(), TouchEvent::TOUCH_POINT_CANCEL);
269
270 std::vector<POINTER_TOUCH_INFO> touches;
271 for (const TouchEventPoint& touch_point : event.touch_points()) {
272 POINTER_TOUCH_INFO pointer_touch_info =
273 touches_in_contact_[touch_point.id()];
274 pointer_touch_info.pointerInfo.pointerFlags =
275 POINTER_FLAG_UP | POINTER_FLAG_CANCELED;
276
277 touches_in_contact_.erase(touch_point.id());
278 touches.push_back(pointer_touch_info);
279 }
280
281 AppendMapValuesToVector(&touches_in_contact_, &touches);
282 if (delegate_->InjectTouchInput(touches.size(),
283 vector_as_array(&touches)) == 0) {
284 PLOG(ERROR) << "Failed to inject a touch cancel event.";
285 }
286 }
287
288 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698