Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / events / ozone / evdev / touch_event_converter_evdev.cc
blob95d0d77101ae69eb477db66ed934e98626dc8b18
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/memory/scoped_vector.h"
22 #include "base/message_loop/message_loop.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_util.h"
25 #include "base/strings/stringprintf.h"
26 #include "ui/events/devices/device_data_manager.h"
27 #include "ui/events/devices/device_util_linux.h"
28 #include "ui/events/event.h"
29 #include "ui/events/event_constants.h"
30 #include "ui/events/event_switches.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"
35 namespace {
37 const int kMaxTrackingId = 0xffff; // TRKID_MAX in kernel.
39 struct TouchCalibration {
40 int bezel_left;
41 int bezel_right;
42 int bezel_top;
43 int bezel_bottom;
46 void GetTouchCalibration(TouchCalibration* cal) {
47 std::vector<std::string> parts;
48 if (Tokenize(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
49 switches::kTouchCalibration),
50 ",", &parts) >= 4) {
51 if (!base::StringToInt(parts[0], &cal->bezel_left))
52 LOG(ERROR) << "Incorrect left border calibration value passed.";
53 if (!base::StringToInt(parts[1], &cal->bezel_right))
54 LOG(ERROR) << "Incorrect right border calibration value passed.";
55 if (!base::StringToInt(parts[2], &cal->bezel_top))
56 LOG(ERROR) << "Incorrect top border calibration value passed.";
57 if (!base::StringToInt(parts[3], &cal->bezel_bottom))
58 LOG(ERROR) << "Incorrect bottom border calibration value passed.";
62 int32_t AbsCodeToMtCode(int32_t code) {
63 switch (code) {
64 case ABS_X:
65 return ABS_MT_POSITION_X;
66 case ABS_Y:
67 return ABS_MT_POSITION_Y;
68 case ABS_PRESSURE:
69 return ABS_MT_PRESSURE;
70 case ABS_DISTANCE:
71 return ABS_MT_DISTANCE;
72 default:
73 return -1;
77 } // namespace
79 namespace ui {
81 TouchEventConverterEvdev::TouchEventConverterEvdev(
82 int fd,
83 base::FilePath path,
84 int id,
85 InputDeviceType type,
86 DeviceEventDispatcherEvdev* dispatcher)
87 : EventConverterEvdev(fd, path, id, type),
88 dispatcher_(dispatcher),
89 syn_dropped_(false),
90 has_mt_(false),
91 touch_points_(0),
92 next_tracking_id_(0),
93 current_slot_(0) {
94 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
95 switches::kExtraTouchNoiseFiltering)) {
96 touch_noise_finder_.reset(new TouchNoiseFinder);
100 TouchEventConverterEvdev::~TouchEventConverterEvdev() {
101 Stop();
102 close(fd_);
105 void TouchEventConverterEvdev::Initialize(const EventDeviceInfo& info) {
106 has_mt_ = info.HasMultitouch();
108 if (has_mt_) {
109 pressure_min_ = info.GetAbsMinimum(ABS_MT_PRESSURE);
110 pressure_max_ = info.GetAbsMaximum(ABS_MT_PRESSURE);
111 x_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_X);
112 x_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_X) - x_min_tuxels_ + 1;
113 y_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_Y);
114 y_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_Y) - y_min_tuxels_ + 1;
115 touch_points_ =
116 std::min<int>(info.GetAbsMaximum(ABS_MT_SLOT) + 1, kNumTouchEvdevSlots);
117 current_slot_ = info.GetAbsValue(ABS_MT_SLOT);
118 } else {
119 pressure_min_ = info.GetAbsMinimum(ABS_PRESSURE);
120 pressure_max_ = info.GetAbsMaximum(ABS_PRESSURE);
121 x_min_tuxels_ = info.GetAbsMinimum(ABS_X);
122 x_num_tuxels_ = info.GetAbsMaximum(ABS_X) - x_min_tuxels_ + 1;
123 y_min_tuxels_ = info.GetAbsMinimum(ABS_Y);
124 y_num_tuxels_ = info.GetAbsMaximum(ABS_Y) - y_min_tuxels_ + 1;
125 touch_points_ = 1;
126 current_slot_ = 0;
129 // Apply --touch-calibration.
130 if (type() == INPUT_DEVICE_INTERNAL) {
131 TouchCalibration cal = {};
132 GetTouchCalibration(&cal);
133 x_min_tuxels_ += cal.bezel_left;
134 x_num_tuxels_ -= cal.bezel_left + cal.bezel_right;
135 y_min_tuxels_ += cal.bezel_top;
136 y_num_tuxels_ -= cal.bezel_top + cal.bezel_bottom;
138 VLOG(1) << "applying touch calibration: "
139 << base::StringPrintf("[%d, %d, %d, %d]", cal.bezel_left,
140 cal.bezel_right, cal.bezel_top,
141 cal.bezel_bottom);
144 events_.resize(touch_points_);
146 if (has_mt_) {
147 for (size_t i = 0; i < events_.size(); ++i) {
148 events_[i].x = info.GetAbsMtSlotValue(ABS_MT_POSITION_X, i);
149 events_[i].y = info.GetAbsMtSlotValue(ABS_MT_POSITION_Y, i);
150 events_[i].tracking_id = info.GetAbsMtSlotValue(ABS_MT_TRACKING_ID, i);
151 events_[i].touching = (events_[i].tracking_id >= 0);
152 events_[i].slot = i;
154 // Dirty the slot so we'll update the consumer at the first opportunity.
155 // We can't dispatch here as this is currently called on the worker pool.
156 // TODO(spang): Move initialization off worker pool.
157 events_[i].altered = true;
159 // Optional bits.
160 events_[i].radius_x =
161 info.GetAbsMtSlotValueWithDefault(ABS_MT_TOUCH_MAJOR, i, 0) / 2.0f;
162 events_[i].radius_y =
163 info.GetAbsMtSlotValueWithDefault(ABS_MT_TOUCH_MINOR, i, 0) / 2.0f;
164 events_[i].pressure =
165 ScalePressure(info.GetAbsMtSlotValue(ABS_MT_PRESSURE, i));
167 } else {
168 // TODO(spang): Add key state to EventDeviceInfo to allow initial contact.
169 events_[0].x = 0;
170 events_[0].y = 0;
171 events_[0].tracking_id = -1;
172 events_[0].touching = false;
173 events_[0].slot = 0;
174 events_[0].radius_x = 0;
175 events_[0].radius_y = 0;
176 events_[0].pressure = 0;
180 bool TouchEventConverterEvdev::Reinitialize() {
181 EventDeviceInfo info;
182 if (info.Initialize(fd_)) {
183 Initialize(info);
184 return true;
186 return false;
189 bool TouchEventConverterEvdev::HasTouchscreen() const {
190 return true;
193 gfx::Size TouchEventConverterEvdev::GetTouchscreenSize() const {
194 return gfx::Size(x_num_tuxels_, y_num_tuxels_);
197 int TouchEventConverterEvdev::GetTouchPoints() const {
198 return touch_points_;
201 void TouchEventConverterEvdev::OnFileCanReadWithoutBlocking(int fd) {
202 input_event inputs[kNumTouchEvdevSlots * 6 + 1];
203 ssize_t read_size = read(fd, inputs, sizeof(inputs));
204 if (read_size < 0) {
205 if (errno == EINTR || errno == EAGAIN)
206 return;
207 if (errno != ENODEV)
208 PLOG(ERROR) << "error reading device " << path_.value();
209 Stop();
210 return;
213 if (ignore_events_)
214 return;
216 for (unsigned i = 0; i < read_size / sizeof(*inputs); i++) {
217 if (!has_mt_) {
218 // Emulate the device as an MT device with only 1 slot by inserting extra
219 // MT protocol events in the stream.
220 EmulateMultitouchEvent(inputs[i]);
223 ProcessMultitouchEvent(inputs[i]);
227 void TouchEventConverterEvdev::ProcessMultitouchEvent(
228 const input_event& input) {
229 if (input.type == EV_SYN) {
230 ProcessSyn(input);
231 } else if (syn_dropped_) {
232 // Do nothing. This branch indicates we have lost sync with the driver.
233 } else if (input.type == EV_ABS) {
234 if (events_.size() <= current_slot_) {
235 LOG(ERROR) << "current_slot_ (" << current_slot_
236 << ") >= events_.size() (" << events_.size() << ")";
237 } else {
238 ProcessAbs(input);
240 } else if (input.type == EV_KEY) {
241 ProcessKey(input);
242 } else {
243 NOTIMPLEMENTED() << "invalid type: " << input.type;
247 void TouchEventConverterEvdev::EmulateMultitouchEvent(
248 const input_event& event) {
249 input_event emulated_event = event;
251 if (event.type == EV_ABS) {
252 emulated_event.code = AbsCodeToMtCode(event.code);
253 if (emulated_event.code >= 0)
254 ProcessMultitouchEvent(emulated_event);
255 } else if (event.type == EV_KEY && event.code == BTN_TOUCH) {
256 emulated_event.type = EV_ABS;
257 emulated_event.code = ABS_MT_TRACKING_ID;
258 emulated_event.value = event.value ? NextTrackingId() : -1;
259 ProcessMultitouchEvent(emulated_event);
263 void TouchEventConverterEvdev::ProcessKey(const input_event& input) {
264 switch (input.code) {
265 case BTN_TOUCH:
266 break;
267 default:
268 NOTIMPLEMENTED() << "invalid code for EV_KEY: " << input.code;
272 void TouchEventConverterEvdev::ProcessAbs(const input_event& input) {
273 switch (input.code) {
274 case ABS_MT_TOUCH_MAJOR:
275 // TODO(spang): If we have all of major, minor, and orientation,
276 // we can scale the ellipse correctly. However on the Pixel we get
277 // neither minor nor orientation, so this is all we can do.
278 events_[current_slot_].radius_x = input.value / 2.0f;
279 break;
280 case ABS_MT_TOUCH_MINOR:
281 events_[current_slot_].radius_y = input.value / 2.0f;
282 break;
283 case ABS_MT_POSITION_X:
284 events_[current_slot_].x = input.value;
285 break;
286 case ABS_MT_POSITION_Y:
287 events_[current_slot_].y = input.value;
288 break;
289 case ABS_MT_TRACKING_ID:
290 if (input.value < 0) {
291 events_[current_slot_].touching = false;
292 } else {
293 events_[current_slot_].touching = true;
294 events_[current_slot_].cancelled = false;
296 events_[current_slot_].tracking_id = input.value;
297 break;
298 case ABS_MT_PRESSURE:
299 events_[current_slot_].pressure = ScalePressure(input.value);
300 break;
301 case ABS_MT_SLOT:
302 if (input.value >= 0 &&
303 static_cast<size_t>(input.value) < events_.size()) {
304 current_slot_ = input.value;
305 } else {
306 LOG(ERROR) << "invalid touch event index: " << input.value;
307 return;
309 break;
310 default:
311 DVLOG(5) << "unhandled code for EV_ABS: " << input.code;
312 return;
314 events_[current_slot_].altered = true;
317 void TouchEventConverterEvdev::ProcessSyn(const input_event& input) {
318 switch (input.code) {
319 case SYN_REPORT:
320 if (syn_dropped_) {
321 // Have to re-initialize.
322 if (Reinitialize()) {
323 syn_dropped_ = false;
324 } else {
325 LOG(ERROR) << "failed to re-initialize device info";
327 } else {
328 ReportEvents(EventConverterEvdev::TimeDeltaFromInputEvent(input));
330 break;
331 case SYN_DROPPED:
332 // Some buffer has overrun. We ignore all events up to and
333 // including the next SYN_REPORT.
334 syn_dropped_ = true;
335 break;
336 default:
337 NOTIMPLEMENTED() << "invalid code for EV_SYN: " << input.code;
341 EventType TouchEventConverterEvdev::GetEventTypeForTouch(
342 const InProgressTouchEvdev& touch) {
343 if (touch.cancelled)
344 return ET_UNKNOWN;
346 if (touch_noise_finder_ && touch_noise_finder_->SlotHasNoise(touch.slot)) {
347 if (touch.touching && !touch.was_touching)
348 return ET_UNKNOWN;
349 return ET_TOUCH_CANCELLED;
352 if (touch.touching)
353 return touch.was_touching ? ET_TOUCH_MOVED : ET_TOUCH_PRESSED;
354 return touch.was_touching ? ET_TOUCH_RELEASED : ET_UNKNOWN;
357 void TouchEventConverterEvdev::ReportEvent(const InProgressTouchEvdev& event,
358 EventType event_type,
359 const base::TimeDelta& timestamp) {
360 dispatcher_->DispatchTouchEvent(TouchEventParams(
361 id_, event.slot, event_type, gfx::PointF(event.x, event.y),
362 gfx::Vector2dF(event.radius_x, event.radius_y), event.pressure,
363 timestamp));
366 void TouchEventConverterEvdev::ReportEvents(base::TimeDelta delta) {
367 if (touch_noise_finder_)
368 touch_noise_finder_->HandleTouches(events_, delta);
370 for (size_t i = 0; i < events_.size(); i++) {
371 InProgressTouchEvdev* event = &events_[i];
372 if (!event->altered)
373 continue;
375 EventType event_type = GetEventTypeForTouch(*event);
376 if (event_type == ET_UNKNOWN || event_type == ET_TOUCH_CANCELLED)
377 event->cancelled = true;
379 if (event_type != ET_UNKNOWN)
380 ReportEvent(*event, event_type, delta);
382 event->was_touching = event->touching;
383 event->altered = false;
387 float TouchEventConverterEvdev::ScalePressure(int32_t value) {
388 float pressure = value - pressure_min_;
389 if (pressure_max_ - pressure_min_)
390 pressure /= pressure_max_ - pressure_min_;
391 return pressure;
394 int TouchEventConverterEvdev::NextTrackingId() {
395 return next_tracking_id_++ & kMaxTrackingId;
398 } // namespace ui