cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / ui / events / ozone / evdev / touch_event_converter_evdev.cc
blobd14e499335b96bce1fa6a261f9c48efa52824873
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
3 // found in the LICENSE file.
5 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <linux/input.h>
10 #include <poll.h>
11 #include <stdio.h>
12 #include <unistd.h>
14 #include <cmath>
15 #include <limits>
17 #include "base/bind.h"
18 #include "base/callback.h"
19 #include "base/command_line.h"
20 #include "base/logging.h"
21 #include "base/message_loop/message_loop.h"
22 #include "base/strings/string_number_conversions.h"
23 #include "base/strings/string_split.h"
24 #include "base/strings/string_util.h"
25 #include "base/strings/stringprintf.h"
26 #include "base/trace_event/trace_event.h"
27 #include "ui/events/devices/device_data_manager.h"
28 #include "ui/events/devices/device_util_linux.h"
29 #include "ui/events/event.h"
30 #include "ui/events/event_constants.h"
31 #include "ui/events/event_switches.h"
32 #include "ui/events/event_utils.h"
33 #include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h"
34 #include "ui/events/ozone/evdev/touch_evdev_types.h"
35 #include "ui/events/ozone/evdev/touch_noise/touch_noise_finder.h"
36 #include "ui/ozone/public/input_controller.h"
37 #include "ui/ozone/public/ozone_platform.h"
39 namespace {
41 const int kMaxTrackingId = 0xffff; // TRKID_MAX in kernel.
43 struct TouchCalibration {
44 int bezel_left;
45 int bezel_right;
46 int bezel_top;
47 int bezel_bottom;
50 void GetTouchCalibration(TouchCalibration* cal) {
51 std::vector<std::string> parts = base::SplitString(
52 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
53 switches::kTouchCalibration),
54 ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
55 if (parts.size() >= 4) {
56 if (!base::StringToInt(parts[0], &cal->bezel_left))
57 LOG(ERROR) << "Incorrect left border calibration value passed.";
58 if (!base::StringToInt(parts[1], &cal->bezel_right))
59 LOG(ERROR) << "Incorrect right border calibration value passed.";
60 if (!base::StringToInt(parts[2], &cal->bezel_top))
61 LOG(ERROR) << "Incorrect top border calibration value passed.";
62 if (!base::StringToInt(parts[3], &cal->bezel_bottom))
63 LOG(ERROR) << "Incorrect bottom border calibration value passed.";
67 int32_t AbsCodeToMtCode(int32_t code) {
68 switch (code) {
69 case ABS_X:
70 return ABS_MT_POSITION_X;
71 case ABS_Y:
72 return ABS_MT_POSITION_Y;
73 case ABS_PRESSURE:
74 return ABS_MT_PRESSURE;
75 case ABS_DISTANCE:
76 return ABS_MT_DISTANCE;
77 default:
78 return -1;
82 const int kTrackingIdForUnusedSlot = -1;
84 } // namespace
86 namespace ui {
88 TouchEventConverterEvdev::TouchEventConverterEvdev(
89 int fd,
90 base::FilePath path,
91 int id,
92 const EventDeviceInfo& devinfo,
93 DeviceEventDispatcherEvdev* dispatcher)
94 : EventConverterEvdev(fd,
95 path,
96 id,
97 devinfo.device_type(),
98 devinfo.name(),
99 devinfo.vendor_id(),
100 devinfo.product_id()),
101 dispatcher_(dispatcher) {
102 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
103 switches::kExtraTouchNoiseFiltering)) {
104 touch_noise_finder_.reset(new TouchNoiseFinder);
106 touch_evdev_debug_buffer_.Initialize(devinfo);
109 TouchEventConverterEvdev::~TouchEventConverterEvdev() {
112 void TouchEventConverterEvdev::Initialize(const EventDeviceInfo& info) {
113 has_mt_ = info.HasMultitouch();
115 if (has_mt_) {
116 pressure_min_ = info.GetAbsMinimum(ABS_MT_PRESSURE);
117 pressure_max_ = info.GetAbsMaximum(ABS_MT_PRESSURE);
118 x_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_X);
119 x_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_X) - x_min_tuxels_ + 1;
120 y_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_Y);
121 y_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_Y) - y_min_tuxels_ + 1;
122 touch_points_ =
123 std::min<int>(info.GetAbsMaximum(ABS_MT_SLOT) + 1, kNumTouchEvdevSlots);
124 current_slot_ = info.GetAbsValue(ABS_MT_SLOT);
125 } else {
126 pressure_min_ = info.GetAbsMinimum(ABS_PRESSURE);
127 pressure_max_ = info.GetAbsMaximum(ABS_PRESSURE);
128 x_min_tuxels_ = info.GetAbsMinimum(ABS_X);
129 x_num_tuxels_ = info.GetAbsMaximum(ABS_X) - x_min_tuxels_ + 1;
130 y_min_tuxels_ = info.GetAbsMinimum(ABS_Y);
131 y_num_tuxels_ = info.GetAbsMaximum(ABS_Y) - y_min_tuxels_ + 1;
132 touch_points_ = 1;
133 current_slot_ = 0;
136 quirk_left_mouse_button_ =
137 !has_mt_ && !info.HasKeyEvent(BTN_TOUCH) && info.HasKeyEvent(BTN_LEFT);
139 // Apply --touch-calibration.
140 if (type() == INPUT_DEVICE_INTERNAL) {
141 TouchCalibration cal = {};
142 GetTouchCalibration(&cal);
143 x_min_tuxels_ += cal.bezel_left;
144 x_num_tuxels_ -= cal.bezel_left + cal.bezel_right;
145 y_min_tuxels_ += cal.bezel_top;
146 y_num_tuxels_ -= cal.bezel_top + cal.bezel_bottom;
148 VLOG(1) << "applying touch calibration: "
149 << base::StringPrintf("[%d, %d, %d, %d]", cal.bezel_left,
150 cal.bezel_right, cal.bezel_top,
151 cal.bezel_bottom);
154 events_.resize(touch_points_);
156 if (has_mt_) {
157 for (size_t i = 0; i < events_.size(); ++i) {
158 events_[i].x = info.GetAbsMtSlotValueWithDefault(ABS_MT_POSITION_X, i, 0);
159 events_[i].y = info.GetAbsMtSlotValueWithDefault(ABS_MT_POSITION_Y, i, 0);
160 events_[i].tracking_id = info.GetAbsMtSlotValueWithDefault(
161 ABS_MT_TRACKING_ID, i, kTrackingIdForUnusedSlot);
162 events_[i].touching = (events_[i].tracking_id >= 0);
163 events_[i].slot = i;
165 // Dirty the slot so we'll update the consumer at the first opportunity.
166 // We can't dispatch here as this is currently called on the worker pool.
167 // TODO(spang): Move initialization off worker pool.
168 events_[i].altered = true;
170 // Optional bits.
171 events_[i].radius_x =
172 info.GetAbsMtSlotValueWithDefault(ABS_MT_TOUCH_MAJOR, i, 0) / 2.0f;
173 events_[i].radius_y =
174 info.GetAbsMtSlotValueWithDefault(ABS_MT_TOUCH_MINOR, i, 0) / 2.0f;
175 events_[i].pressure = ScalePressure(
176 info.GetAbsMtSlotValueWithDefault(ABS_MT_PRESSURE, i, 0));
178 } else {
179 // TODO(spang): Add key state to EventDeviceInfo to allow initial contact.
180 // (and make sure to take into account quirk_left_mouse_button_)
181 events_[0].x = 0;
182 events_[0].y = 0;
183 events_[0].tracking_id = kTrackingIdForUnusedSlot;
184 events_[0].touching = false;
185 events_[0].slot = 0;
186 events_[0].radius_x = 0;
187 events_[0].radius_y = 0;
188 events_[0].pressure = 0;
192 void TouchEventConverterEvdev::Reinitialize() {
193 EventDeviceInfo info;
194 if (!info.Initialize(fd_, path_)) {
195 LOG(ERROR) << "Failed to synchronize state for touch device: "
196 << path_.value();
197 Stop();
198 return;
201 Initialize(info);
204 bool TouchEventConverterEvdev::HasTouchscreen() const {
205 return true;
208 gfx::Size TouchEventConverterEvdev::GetTouchscreenSize() const {
209 return gfx::Size(x_num_tuxels_, y_num_tuxels_);
212 int TouchEventConverterEvdev::GetTouchPoints() const {
213 return touch_points_;
216 void TouchEventConverterEvdev::OnEnabled() {
217 ReportEvents(EventTimeForNow());
220 void TouchEventConverterEvdev::OnDisabled() {
221 ReleaseTouches();
224 void TouchEventConverterEvdev::OnFileCanReadWithoutBlocking(int fd) {
225 TRACE_EVENT1("evdev",
226 "TouchEventConverterEvdev::OnFileCanReadWithoutBlocking", "fd",
227 fd);
229 input_event inputs[kNumTouchEvdevSlots * 6 + 1];
230 ssize_t read_size = read(fd, inputs, sizeof(inputs));
231 if (read_size < 0) {
232 if (errno == EINTR || errno == EAGAIN)
233 return;
234 if (errno != ENODEV)
235 PLOG(ERROR) << "error reading device " << path_.value();
236 Stop();
237 return;
240 if (!enabled_) {
241 dropped_events_ = true;
242 return;
245 for (unsigned i = 0; i < read_size / sizeof(*inputs); i++) {
246 if (!has_mt_) {
247 // Emulate the device as an MT device with only 1 slot by inserting extra
248 // MT protocol events in the stream.
249 EmulateMultitouchEvent(inputs[i]);
252 ProcessMultitouchEvent(inputs[i]);
256 void TouchEventConverterEvdev::DumpTouchEventLog(const char* filename) {
257 touch_evdev_debug_buffer_.DumpLog(filename);
260 void TouchEventConverterEvdev::SetTouchEventLoggingEnabled(bool enabled) {
261 touch_logging_enabled_ = enabled;
264 void TouchEventConverterEvdev::ProcessMultitouchEvent(
265 const input_event& input) {
266 if (touch_logging_enabled_)
267 touch_evdev_debug_buffer_.ProcessEvent(current_slot_, &input);
269 if (input.type == EV_SYN) {
270 ProcessSyn(input);
271 } else if (dropped_events_) {
272 // Do nothing. This branch indicates we have lost sync with the driver.
273 } else if (input.type == EV_ABS) {
274 if (events_.size() <= current_slot_) {
275 LOG(ERROR) << "current_slot_ (" << current_slot_
276 << ") >= events_.size() (" << events_.size() << ")";
277 } else {
278 ProcessAbs(input);
280 } else if (input.type == EV_KEY) {
281 ProcessKey(input);
282 } else if (input.type == EV_MSC) {
283 // Ignored.
284 } else {
285 NOTIMPLEMENTED() << "invalid type: " << input.type;
289 void TouchEventConverterEvdev::EmulateMultitouchEvent(
290 const input_event& event) {
291 input_event emulated_event = event;
293 if (event.type == EV_ABS) {
294 emulated_event.code = AbsCodeToMtCode(event.code);
295 if (emulated_event.code >= 0)
296 ProcessMultitouchEvent(emulated_event);
297 } else if (event.type == EV_KEY) {
298 if (event.code == BTN_TOUCH ||
299 (quirk_left_mouse_button_ && event.code == BTN_LEFT)) {
300 emulated_event.type = EV_ABS;
301 emulated_event.code = ABS_MT_TRACKING_ID;
302 emulated_event.value =
303 event.value ? NextTrackingId() : kTrackingIdForUnusedSlot;
304 ProcessMultitouchEvent(emulated_event);
309 void TouchEventConverterEvdev::ProcessKey(const input_event& input) {
310 switch (input.code) {
311 case BTN_TOUCH:
312 case BTN_LEFT:
313 break;
314 default:
315 NOTIMPLEMENTED() << "invalid code for EV_KEY: " << input.code;
319 void TouchEventConverterEvdev::ProcessAbs(const input_event& input) {
320 switch (input.code) {
321 case ABS_MT_TOUCH_MAJOR:
322 // TODO(spang): If we have all of major, minor, and orientation,
323 // we can scale the ellipse correctly. However on the Pixel we get
324 // neither minor nor orientation, so this is all we can do.
325 events_[current_slot_].radius_x = input.value / 2.0f;
326 break;
327 case ABS_MT_TOUCH_MINOR:
328 events_[current_slot_].radius_y = input.value / 2.0f;
329 break;
330 case ABS_MT_POSITION_X:
331 events_[current_slot_].x = input.value;
332 break;
333 case ABS_MT_POSITION_Y:
334 events_[current_slot_].y = input.value;
335 break;
336 case ABS_MT_TRACKING_ID:
337 UpdateTrackingId(current_slot_, input.value);
338 break;
339 case ABS_MT_PRESSURE:
340 events_[current_slot_].pressure = ScalePressure(input.value);
341 break;
342 case ABS_MT_SLOT:
343 if (input.value >= 0 &&
344 static_cast<size_t>(input.value) < events_.size()) {
345 current_slot_ = input.value;
346 } else {
347 LOG(ERROR) << "invalid touch event index: " << input.value;
348 return;
350 break;
351 default:
352 DVLOG(5) << "unhandled code for EV_ABS: " << input.code;
353 return;
355 events_[current_slot_].altered = true;
358 void TouchEventConverterEvdev::ProcessSyn(const input_event& input) {
359 switch (input.code) {
360 case SYN_REPORT:
361 ReportEvents(EventConverterEvdev::TimeDeltaFromInputEvent(input));
362 break;
363 case SYN_DROPPED:
364 // Some buffer has overrun. We ignore all events up to and
365 // including the next SYN_REPORT.
366 dropped_events_ = true;
367 break;
368 default:
369 NOTIMPLEMENTED() << "invalid code for EV_SYN: " << input.code;
373 EventType TouchEventConverterEvdev::GetEventTypeForTouch(
374 const InProgressTouchEvdev& touch) {
375 if (touch.cancelled)
376 return ET_UNKNOWN;
378 if (touch_noise_finder_ && touch_noise_finder_->SlotHasNoise(touch.slot)) {
379 if (touch.touching && !touch.was_touching)
380 return ET_UNKNOWN;
381 return ET_TOUCH_CANCELLED;
384 if (touch.touching)
385 return touch.was_touching ? ET_TOUCH_MOVED : ET_TOUCH_PRESSED;
386 return touch.was_touching ? ET_TOUCH_RELEASED : ET_UNKNOWN;
389 void TouchEventConverterEvdev::ReportEvent(const InProgressTouchEvdev& event,
390 EventType event_type,
391 const base::TimeDelta& timestamp) {
392 dispatcher_->DispatchTouchEvent(TouchEventParams(
393 input_device_.id, event.slot, event_type, gfx::PointF(event.x, event.y),
394 gfx::Vector2dF(event.radius_x, event.radius_y), event.pressure,
395 timestamp));
398 void TouchEventConverterEvdev::ReportEvents(base::TimeDelta delta) {
399 if (dropped_events_) {
400 Reinitialize();
401 dropped_events_ = false;
404 if (touch_noise_finder_)
405 touch_noise_finder_->HandleTouches(events_, delta);
407 for (size_t i = 0; i < events_.size(); i++) {
408 InProgressTouchEvdev* event = &events_[i];
409 if (!event->altered)
410 continue;
412 EventType event_type = GetEventTypeForTouch(*event);
413 if (event_type == ET_UNKNOWN || event_type == ET_TOUCH_CANCELLED)
414 event->cancelled = true;
416 if (event_type != ET_UNKNOWN)
417 ReportEvent(*event, event_type, delta);
419 event->was_touching = event->touching;
420 event->altered = false;
424 void TouchEventConverterEvdev::UpdateTrackingId(int slot, int tracking_id) {
425 InProgressTouchEvdev* event = &events_[slot];
427 if (event->tracking_id == tracking_id)
428 return;
430 event->tracking_id = tracking_id;
431 event->touching = (tracking_id >= 0);
432 event->altered = true;
434 if (tracking_id >= 0)
435 event->cancelled = false;
438 void TouchEventConverterEvdev::ReleaseTouches() {
439 for (size_t slot = 0; slot < events_.size(); slot++)
440 UpdateTrackingId(slot, kTrackingIdForUnusedSlot);
442 ReportEvents(EventTimeForNow());
445 float TouchEventConverterEvdev::ScalePressure(int32_t value) {
446 float pressure = value - pressure_min_;
447 if (pressure_max_ - pressure_min_)
448 pressure /= pressure_max_ - pressure_min_;
449 return pressure;
452 int TouchEventConverterEvdev::NextTrackingId() {
453 return next_tracking_id_++ & kMaxTrackingId;
456 } // namespace ui