OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" | 5 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <linux/input.h> | 9 #include <linux/input.h> |
10 #include <poll.h> | 10 #include <poll.h> |
(...skipping 11 matching lines...) Expand all Loading... | |
22 #include "base/message_loop/message_loop.h" | 22 #include "base/message_loop/message_loop.h" |
23 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
26 #include "ui/events/devices/device_data_manager.h" | 26 #include "ui/events/devices/device_data_manager.h" |
27 #include "ui/events/devices/device_util_linux.h" | 27 #include "ui/events/devices/device_util_linux.h" |
28 #include "ui/events/event.h" | 28 #include "ui/events/event.h" |
29 #include "ui/events/event_constants.h" | 29 #include "ui/events/event_constants.h" |
30 #include "ui/events/event_switches.h" | 30 #include "ui/events/event_switches.h" |
31 #include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h" | 31 #include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h" |
32 #include "ui/events/ozone/evdev/touch_evdev_types.h" | |
33 #include "ui/events/ozone/evdev/touch_noise/touch_noise_finder.h" | |
32 | 34 |
33 namespace { | 35 namespace { |
34 | 36 |
35 struct TouchCalibration { | 37 struct TouchCalibration { |
36 int bezel_left; | 38 int bezel_left; |
37 int bezel_right; | 39 int bezel_right; |
38 int bezel_top; | 40 int bezel_top; |
39 int bezel_bottom; | 41 int bezel_bottom; |
40 }; | 42 }; |
41 | 43 |
(...skipping 10 matching lines...) Expand all Loading... | |
52 DLOG(ERROR) << "Incorrect top border calibration value passed."; | 54 DLOG(ERROR) << "Incorrect top border calibration value passed."; |
53 if (!base::StringToInt(parts[3], &cal->bezel_bottom)) | 55 if (!base::StringToInt(parts[3], &cal->bezel_bottom)) |
54 DLOG(ERROR) << "Incorrect bottom border calibration value passed."; | 56 DLOG(ERROR) << "Incorrect bottom border calibration value passed."; |
55 } | 57 } |
56 } | 58 } |
57 | 59 |
58 } // namespace | 60 } // namespace |
59 | 61 |
60 namespace ui { | 62 namespace ui { |
61 | 63 |
62 TouchEventConverterEvdev::InProgressEvents::InProgressEvents() | |
63 : altered_(false), | |
64 x_(0), | |
65 y_(0), | |
66 id_(-1), | |
67 finger_(-1), | |
68 type_(ET_UNKNOWN), | |
69 radius_x_(0), | |
70 radius_y_(0), | |
71 pressure_(0) { | |
72 } | |
73 | |
74 TouchEventConverterEvdev::TouchEventConverterEvdev( | 64 TouchEventConverterEvdev::TouchEventConverterEvdev( |
75 int fd, | 65 int fd, |
76 base::FilePath path, | 66 base::FilePath path, |
77 int id, | 67 int id, |
78 InputDeviceType type, | 68 InputDeviceType type, |
79 DeviceEventDispatcherEvdev* dispatcher) | 69 DeviceEventDispatcherEvdev* dispatcher) |
80 : EventConverterEvdev(fd, path, id, type), | 70 : EventConverterEvdev(fd, path, id, type), |
81 dispatcher_(dispatcher), | 71 dispatcher_(dispatcher), |
82 syn_dropped_(false), | 72 syn_dropped_(false), |
83 is_type_a_(false), | 73 is_type_a_(false), |
84 touch_points_(0), | 74 touch_points_(0), |
85 current_slot_(0) { | 75 current_slot_(0) { |
76 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
77 switches::kExtraTouchNoiseFiltering)) { | |
78 touch_noise_finder_.reset(new TouchNoiseFinder); | |
79 } | |
86 } | 80 } |
87 | 81 |
88 TouchEventConverterEvdev::~TouchEventConverterEvdev() { | 82 TouchEventConverterEvdev::~TouchEventConverterEvdev() { |
89 Stop(); | 83 Stop(); |
90 close(fd_); | 84 close(fd_); |
91 } | 85 } |
92 | 86 |
93 void TouchEventConverterEvdev::Initialize(const EventDeviceInfo& info) { | 87 void TouchEventConverterEvdev::Initialize(const EventDeviceInfo& info) { |
94 pressure_min_ = info.GetAbsMinimum(ABS_MT_PRESSURE); | 88 pressure_min_ = info.GetAbsMinimum(ABS_MT_PRESSURE); |
95 pressure_max_ = info.GetAbsMaximum(ABS_MT_PRESSURE); | 89 pressure_max_ = info.GetAbsMaximum(ABS_MT_PRESSURE); |
96 x_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_X); | 90 x_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_X); |
97 x_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_X) - x_min_tuxels_ + 1; | 91 x_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_X) - x_min_tuxels_ + 1; |
98 y_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_Y); | 92 y_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_Y); |
99 y_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_Y) - y_min_tuxels_ + 1; | 93 y_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_Y) - y_min_tuxels_ + 1; |
100 touch_points_ = | 94 touch_points_ = |
101 std::min<int>(info.GetAbsMaximum(ABS_MT_SLOT) + 1, MAX_FINGERS); | 95 std::min<int>(info.GetAbsMaximum(ABS_MT_SLOT) + 1, kNumTouchEvdevSlots); |
102 | 96 |
103 // Apply --touch-calibration. | 97 // Apply --touch-calibration. |
104 if (type() == INPUT_DEVICE_INTERNAL) { | 98 if (type() == INPUT_DEVICE_INTERNAL) { |
105 TouchCalibration cal = {}; | 99 TouchCalibration cal = {}; |
106 GetTouchCalibration(&cal); | 100 GetTouchCalibration(&cal); |
107 x_min_tuxels_ += cal.bezel_left; | 101 x_min_tuxels_ += cal.bezel_left; |
108 x_num_tuxels_ -= cal.bezel_left + cal.bezel_right; | 102 x_num_tuxels_ -= cal.bezel_left + cal.bezel_right; |
109 y_min_tuxels_ += cal.bezel_top; | 103 y_min_tuxels_ += cal.bezel_top; |
110 y_num_tuxels_ -= cal.bezel_top + cal.bezel_bottom; | 104 y_num_tuxels_ -= cal.bezel_top + cal.bezel_bottom; |
111 | 105 |
112 VLOG(1) << "applying touch calibration: " | 106 VLOG(1) << "applying touch calibration: " |
113 << base::StringPrintf("[%d, %d, %d, %d]", cal.bezel_left, | 107 << base::StringPrintf("[%d, %d, %d, %d]", cal.bezel_left, |
114 cal.bezel_right, cal.bezel_top, | 108 cal.bezel_right, cal.bezel_top, |
115 cal.bezel_bottom); | 109 cal.bezel_bottom); |
116 } | 110 } |
117 | 111 |
118 native_size_ = gfx::Size(x_num_tuxels_, y_num_tuxels_); | 112 native_size_ = gfx::Size(x_num_tuxels_, y_num_tuxels_); |
119 | 113 |
120 events_.resize(touch_points_); | 114 events_.resize(touch_points_); |
121 for (size_t i = 0; i < events_.size(); ++i) { | 115 for (size_t i = 0; i < events_.size(); ++i) { |
122 events_[i].finger_ = info.GetSlotValue(ABS_MT_TRACKING_ID, i); | 116 events_[i].x = info.GetSlotValue(ABS_MT_POSITION_X, i); |
123 events_[i].type_ = | 117 events_[i].y = info.GetSlotValue(ABS_MT_POSITION_Y, i); |
124 events_[i].finger_ < 0 ? ET_TOUCH_RELEASED : ET_TOUCH_PRESSED; | 118 events_[i].tracking_id = info.GetSlotValue(ABS_MT_TRACKING_ID, i); |
125 events_[i].x_ = info.GetSlotValue(ABS_MT_POSITION_X, i); | 119 events_[i].slot = i; |
126 events_[i].y_ = info.GetSlotValue(ABS_MT_POSITION_Y, i); | 120 events_[i].type = |
127 events_[i].radius_x_ = info.GetSlotValue(ABS_MT_TOUCH_MAJOR, i); | 121 events_[i].tracking_id < 0 ? ET_TOUCH_RELEASED : ET_TOUCH_PRESSED; |
128 events_[i].radius_y_ = info.GetSlotValue(ABS_MT_TOUCH_MINOR, i); | 122 events_[i].radius_x = info.GetSlotValue(ABS_MT_TOUCH_MAJOR, i); |
129 events_[i].pressure_ = info.GetSlotValue(ABS_MT_PRESSURE, i); | 123 events_[i].radius_y = info.GetSlotValue(ABS_MT_TOUCH_MINOR, i); |
124 events_[i].pressure = info.GetSlotValue(ABS_MT_PRESSURE, i); | |
130 } | 125 } |
131 } | 126 } |
132 | 127 |
133 bool TouchEventConverterEvdev::Reinitialize() { | 128 bool TouchEventConverterEvdev::Reinitialize() { |
134 EventDeviceInfo info; | 129 EventDeviceInfo info; |
135 if (info.Initialize(fd_)) { | 130 if (info.Initialize(fd_)) { |
136 Initialize(info); | 131 Initialize(info); |
137 return true; | 132 return true; |
138 } | 133 } |
139 return false; | 134 return false; |
140 } | 135 } |
141 | 136 |
142 bool TouchEventConverterEvdev::HasTouchscreen() const { | 137 bool TouchEventConverterEvdev::HasTouchscreen() const { |
143 return true; | 138 return true; |
144 } | 139 } |
145 | 140 |
146 gfx::Size TouchEventConverterEvdev::GetTouchscreenSize() const { | 141 gfx::Size TouchEventConverterEvdev::GetTouchscreenSize() const { |
147 return native_size_; | 142 return native_size_; |
148 } | 143 } |
149 | 144 |
150 int TouchEventConverterEvdev::GetTouchPoints() const { | 145 int TouchEventConverterEvdev::GetTouchPoints() const { |
151 return touch_points_; | 146 return touch_points_; |
152 } | 147 } |
153 | 148 |
154 void TouchEventConverterEvdev::OnFileCanReadWithoutBlocking(int fd) { | 149 void TouchEventConverterEvdev::OnFileCanReadWithoutBlocking(int fd) { |
155 input_event inputs[MAX_FINGERS * 6 + 1]; | 150 input_event inputs[kNumTouchEvdevSlots * 6 + 1]; |
156 ssize_t read_size = read(fd, inputs, sizeof(inputs)); | 151 ssize_t read_size = read(fd, inputs, sizeof(inputs)); |
157 if (read_size < 0) { | 152 if (read_size < 0) { |
158 if (errno == EINTR || errno == EAGAIN) | 153 if (errno == EINTR || errno == EAGAIN) |
159 return; | 154 return; |
160 if (errno != ENODEV) | 155 if (errno != ENODEV) |
161 PLOG(ERROR) << "error reading device " << path_.value(); | 156 PLOG(ERROR) << "error reading device " << path_.value(); |
162 Stop(); | 157 Stop(); |
163 return; | 158 return; |
164 } | 159 } |
165 | 160 |
166 if (ignore_events_) | 161 if (ignore_events_) |
167 return; | 162 return; |
168 | 163 |
169 for (unsigned i = 0; i < read_size / sizeof(*inputs); i++) { | 164 for (unsigned i = 0; i < read_size / sizeof(*inputs); i++) { |
170 ProcessInputEvent(inputs[i]); | 165 ProcessInputEvent(inputs[i]); |
171 } | 166 } |
172 } | 167 } |
173 | 168 |
174 void TouchEventConverterEvdev::ProcessInputEvent(const input_event& input) { | 169 void TouchEventConverterEvdev::ProcessInputEvent(const input_event& input) { |
175 if (input.type == EV_SYN) { | 170 if (input.type == EV_SYN) { |
176 ProcessSyn(input); | 171 ProcessSyn(input); |
177 } else if(syn_dropped_) { | 172 } else if (syn_dropped_) { |
178 // Do nothing. This branch indicates we have lost sync with the driver. | 173 // Do nothing. This branch indicates we have lost sync with the driver. |
179 } else if (input.type == EV_ABS) { | 174 } else if (input.type == EV_ABS) { |
180 if (events_.size() <= current_slot_) { | 175 if (events_.size() <= current_slot_) { |
181 LOG(ERROR) << "current_slot_ (" << current_slot_ | 176 LOG(ERROR) << "current_slot_ (" << current_slot_ |
182 << ") >= events_.size() (" << events_.size() << ")"; | 177 << ") >= events_.size() (" << events_.size() << ")"; |
183 } else { | 178 } else { |
184 ProcessAbs(input); | 179 ProcessAbs(input); |
185 } | 180 } |
186 } else if (input.type == EV_KEY) { | 181 } else if (input.type == EV_KEY) { |
187 switch (input.code) { | 182 switch (input.code) { |
188 case BTN_TOUCH: | 183 case BTN_TOUCH: |
189 break; | 184 break; |
190 default: | 185 default: |
191 NOTIMPLEMENTED() << "invalid code for EV_KEY: " << input.code; | 186 NOTIMPLEMENTED() << "invalid code for EV_KEY: " << input.code; |
192 } | 187 } |
193 } else { | 188 } else { |
194 NOTIMPLEMENTED() << "invalid type: " << input.type; | 189 NOTIMPLEMENTED() << "invalid type: " << input.type; |
195 } | 190 } |
196 } | 191 } |
197 | 192 |
198 void TouchEventConverterEvdev::ProcessAbs(const input_event& input) { | 193 void TouchEventConverterEvdev::ProcessAbs(const input_event& input) { |
199 switch (input.code) { | 194 switch (input.code) { |
200 case ABS_MT_TOUCH_MAJOR: | 195 case ABS_MT_TOUCH_MAJOR: |
201 // TODO(spang): If we have all of major, minor, and orientation, | 196 // TODO(spang): If we have all of major, minor, and orientation, |
202 // we can scale the ellipse correctly. However on the Pixel we get | 197 // we can scale the ellipse correctly. However on the Pixel we get |
203 // neither minor nor orientation, so this is all we can do. | 198 // neither minor nor orientation, so this is all we can do. |
204 events_[current_slot_].radius_x_ = input.value / 2.0f; | 199 events_[current_slot_].radius_x = input.value / 2.0f; |
205 break; | 200 break; |
206 case ABS_MT_TOUCH_MINOR: | 201 case ABS_MT_TOUCH_MINOR: |
207 events_[current_slot_].radius_y_ = input.value / 2.0f; | 202 events_[current_slot_].radius_y = input.value / 2.0f; |
208 break; | 203 break; |
209 case ABS_MT_POSITION_X: | 204 case ABS_MT_POSITION_X: |
210 events_[current_slot_].x_ = input.value; | 205 events_[current_slot_].x = input.value; |
211 break; | 206 break; |
212 case ABS_MT_POSITION_Y: | 207 case ABS_MT_POSITION_Y: |
213 events_[current_slot_].y_ = input.value; | 208 events_[current_slot_].y = input.value; |
214 break; | 209 break; |
215 case ABS_MT_TRACKING_ID: | 210 case ABS_MT_TRACKING_ID: |
216 if (input.value < 0) { | 211 if (input.value < 0) { |
217 events_[current_slot_].type_ = ET_TOUCH_RELEASED; | 212 events_[current_slot_].type = ET_TOUCH_RELEASED; |
218 } else { | 213 } else { |
219 events_[current_slot_].finger_ = input.value; | 214 events_[current_slot_].tracking_id = input.value; |
220 events_[current_slot_].type_ = ET_TOUCH_PRESSED; | 215 events_[current_slot_].type = ET_TOUCH_PRESSED; |
221 } | 216 } |
222 break; | 217 break; |
223 case ABS_MT_PRESSURE: | 218 case ABS_MT_PRESSURE: |
224 events_[current_slot_].pressure_ = input.value - pressure_min_; | 219 events_[current_slot_].pressure = input.value - pressure_min_; |
225 events_[current_slot_].pressure_ /= pressure_max_ - pressure_min_; | 220 events_[current_slot_].pressure /= pressure_max_ - pressure_min_; |
226 break; | 221 break; |
227 case ABS_MT_SLOT: | 222 case ABS_MT_SLOT: |
228 if (input.value >= 0 && | 223 if (input.value >= 0 && |
229 static_cast<size_t>(input.value) < events_.size()) { | 224 static_cast<size_t>(input.value) < events_.size()) { |
230 current_slot_ = input.value; | 225 current_slot_ = input.value; |
231 } else { | 226 } else { |
232 LOG(ERROR) << "invalid touch event index: " << input.value; | 227 LOG(ERROR) << "invalid touch event index: " << input.value; |
233 return; | 228 return; |
234 } | 229 } |
235 break; | 230 break; |
236 default: | 231 default: |
237 DVLOG(5) << "unhandled code for EV_ABS: " << input.code; | 232 DVLOG(5) << "unhandled code for EV_ABS: " << input.code; |
238 return; | 233 return; |
239 } | 234 } |
240 events_[current_slot_].altered_ = true; | 235 events_[current_slot_].altered = true; |
241 } | 236 } |
242 | 237 |
243 void TouchEventConverterEvdev::ProcessSyn(const input_event& input) { | 238 void TouchEventConverterEvdev::ProcessSyn(const input_event& input) { |
244 switch (input.code) { | 239 switch (input.code) { |
245 case SYN_REPORT: | 240 case SYN_REPORT: |
246 if (syn_dropped_) { | 241 if (syn_dropped_) { |
247 // Have to re-initialize. | 242 // Have to re-initialize. |
248 if (Reinitialize()) { | 243 if (Reinitialize()) { |
249 syn_dropped_ = false; | 244 syn_dropped_ = false; |
250 for(InProgressEvents& event: events_) | 245 for(InProgressTouchEvdev& event : events_) |
251 event.altered_ = false; | 246 event.altered = false; |
252 } else { | 247 } else { |
253 LOG(ERROR) << "failed to re-initialize device info"; | 248 LOG(ERROR) << "failed to re-initialize device info"; |
254 } | 249 } |
255 } else { | 250 } else { |
256 ReportEvents(EventConverterEvdev::TimeDeltaFromInputEvent(input)); | 251 ReportEvents(EventConverterEvdev::TimeDeltaFromInputEvent(input)); |
257 } | 252 } |
258 if (is_type_a_) | 253 if (is_type_a_) |
259 current_slot_ = 0; | 254 current_slot_ = 0; |
260 break; | 255 break; |
261 case SYN_MT_REPORT: | 256 case SYN_MT_REPORT: |
262 // For type A devices, we just get a stream of all current contacts, | 257 // For type A devices, we just get a stream of all current contacts, |
263 // in some arbitrary order. | 258 // in some arbitrary order. |
264 events_[current_slot_].type_ = ET_TOUCH_PRESSED; | 259 events_[current_slot_].type = ET_TOUCH_PRESSED; |
265 if (events_.size() - 1 > current_slot_) | 260 if (events_.size() - 1 > current_slot_) |
266 current_slot_++; | 261 current_slot_++; |
267 is_type_a_ = true; | 262 is_type_a_ = true; |
263 | |
264 // TouchNoiseFinder does not support type A devices. | |
265 touch_noise_finder_.reset(); | |
268 break; | 266 break; |
269 case SYN_DROPPED: | 267 case SYN_DROPPED: |
270 // Some buffer has overrun. We ignore all events up to and | 268 // Some buffer has overrun. We ignore all events up to and |
271 // including the next SYN_REPORT. | 269 // including the next SYN_REPORT. |
272 syn_dropped_ = true; | 270 syn_dropped_ = true; |
273 break; | 271 break; |
274 default: | 272 default: |
275 NOTIMPLEMENTED() << "invalid code for EV_SYN: " << input.code; | 273 NOTIMPLEMENTED() << "invalid code for EV_SYN: " << input.code; |
276 } | 274 } |
277 } | 275 } |
278 | 276 |
279 void TouchEventConverterEvdev::ReportEvent(int touch_id, | 277 void TouchEventConverterEvdev::ReportEvent(const InProgressTouchEvdev& touch, |
280 const InProgressEvents& event, | |
281 const base::TimeDelta& timestamp) { | 278 const base::TimeDelta& timestamp) { |
282 dispatcher_->DispatchTouchEvent(TouchEventParams( | 279 dispatcher_->DispatchTouchEvent(TouchEventParams( |
283 id_, touch_id, event.type_, gfx::PointF(event.x_, event.y_), | 280 id_, touch.slot, touch.type, gfx::PointF(touch.x, touch.y), |
284 gfx::Vector2dF(event.radius_x_, event.radius_y_), event.pressure_, | 281 gfx::Vector2dF(touch.radius_x, touch.radius_y), touch.pressure, |
285 timestamp)); | 282 timestamp)); |
286 } | 283 } |
287 | 284 |
288 void TouchEventConverterEvdev::ReportEvents(base::TimeDelta delta) { | 285 void TouchEventConverterEvdev::ReportEvents(base::TimeDelta delta) { |
286 if (touch_noise_finder_) | |
287 touch_noise_finder_->HandleTouches(events_, delta); | |
288 | |
289 for (size_t i = 0; i < events_.size(); i++) { | 289 for (size_t i = 0; i < events_.size(); i++) { |
290 if (events_[i].altered_) { | 290 InProgressTouchEvdev* event = &events_[i]; |
291 ReportEvent(i, events_[i], delta); | 291 if (!event->altered) |
292 continue; | |
292 | 293 |
293 // Subsequent events for this finger will be touch-move until it | 294 EventType pre_cancel_event_type = event->type; |
294 // is released. | 295 if (touch_noise_finder_ && |
295 events_[i].type_ = ET_TOUCH_MOVED; | 296 touch_noise_finder_->SlotHasNoise(event->slot)) { |
296 events_[i].altered_ = false; | 297 VLOG(2) << base::StringPrintf("TrackingID %d cancelled", |
298 event->tracking_id); | |
299 event->type = ET_TOUCH_CANCELLED; | |
flackr
2015/03/13 15:08:36
I think we should only report cancelled once for t
| |
297 } | 300 } |
301 | |
302 ReportEvent(*event, delta); | |
303 | |
304 switch (pre_cancel_event_type) { | |
305 case ET_TOUCH_PRESSED: | |
306 event->type = ET_TOUCH_MOVED; | |
307 break; | |
308 case ET_TOUCH_RELEASED: | |
309 event->type = ET_UNKNOWN; | |
310 break; | |
311 default: | |
312 break; | |
313 } | |
314 | |
315 events_[i].altered = false; | |
298 } | 316 } |
299 } | 317 } |
300 | 318 |
301 } // namespace ui | 319 } // namespace ui |
OLD | NEW |