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"
9 #include <linux/input.h>
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"
37 const int kMaxTrackingId
= 0xffff; // TRKID_MAX in kernel.
39 struct TouchCalibration
{
46 void GetTouchCalibration(TouchCalibration
* cal
) {
47 std::vector
<std::string
> parts
;
48 if (Tokenize(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
49 switches::kTouchCalibration
),
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
) {
65 return ABS_MT_POSITION_X
;
67 return ABS_MT_POSITION_Y
;
69 return ABS_MT_PRESSURE
;
71 return ABS_MT_DISTANCE
;
81 TouchEventConverterEvdev::TouchEventConverterEvdev(
86 DeviceEventDispatcherEvdev
* dispatcher
)
87 : EventConverterEvdev(fd
, path
, id
, type
),
88 dispatcher_(dispatcher
),
94 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
95 switches::kExtraTouchNoiseFiltering
)) {
96 touch_noise_finder_
.reset(new TouchNoiseFinder
);
100 TouchEventConverterEvdev::~TouchEventConverterEvdev() {
105 void TouchEventConverterEvdev::Initialize(const EventDeviceInfo
& info
) {
106 has_mt_
= info
.HasMultitouch();
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;
116 std::min
<int>(info
.GetAbsMaximum(ABS_MT_SLOT
) + 1, kNumTouchEvdevSlots
);
117 current_slot_
= info
.GetAbsValue(ABS_MT_SLOT
);
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;
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
,
144 events_
.resize(touch_points_
);
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);
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;
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
));
168 // TODO(spang): Add key state to EventDeviceInfo to allow initial contact.
171 events_
[0].tracking_id
= -1;
172 events_
[0].touching
= false;
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_
)) {
189 bool TouchEventConverterEvdev::HasTouchscreen() const {
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
));
205 if (errno
== EINTR
|| errno
== EAGAIN
)
208 PLOG(ERROR
) << "error reading device " << path_
.value();
216 for (unsigned i
= 0; i
< read_size
/ sizeof(*inputs
); i
++) {
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
) {
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() << ")";
240 } else if (input
.type
== EV_KEY
) {
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
) {
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
;
280 case ABS_MT_TOUCH_MINOR
:
281 events_
[current_slot_
].radius_y
= input
.value
/ 2.0f
;
283 case ABS_MT_POSITION_X
:
284 events_
[current_slot_
].x
= input
.value
;
286 case ABS_MT_POSITION_Y
:
287 events_
[current_slot_
].y
= input
.value
;
289 case ABS_MT_TRACKING_ID
:
290 if (input
.value
< 0) {
291 events_
[current_slot_
].touching
= false;
293 events_
[current_slot_
].touching
= true;
294 events_
[current_slot_
].cancelled
= false;
296 events_
[current_slot_
].tracking_id
= input
.value
;
298 case ABS_MT_PRESSURE
:
299 events_
[current_slot_
].pressure
= ScalePressure(input
.value
);
302 if (input
.value
>= 0 &&
303 static_cast<size_t>(input
.value
) < events_
.size()) {
304 current_slot_
= input
.value
;
306 LOG(ERROR
) << "invalid touch event index: " << input
.value
;
311 DVLOG(5) << "unhandled code for EV_ABS: " << input
.code
;
314 events_
[current_slot_
].altered
= true;
317 void TouchEventConverterEvdev::ProcessSyn(const input_event
& input
) {
318 switch (input
.code
) {
321 // Have to re-initialize.
322 if (Reinitialize()) {
323 syn_dropped_
= false;
325 LOG(ERROR
) << "failed to re-initialize device info";
328 ReportEvents(EventConverterEvdev::TimeDeltaFromInputEvent(input
));
332 // Some buffer has overrun. We ignore all events up to and
333 // including the next SYN_REPORT.
337 NOTIMPLEMENTED() << "invalid code for EV_SYN: " << input
.code
;
341 EventType
TouchEventConverterEvdev::GetEventTypeForTouch(
342 const InProgressTouchEvdev
& touch
) {
346 if (touch_noise_finder_
&& touch_noise_finder_
->SlotHasNoise(touch
.slot
)) {
347 if (touch
.touching
&& !touch
.was_touching
)
349 return ET_TOUCH_CANCELLED
;
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
,
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
];
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_
;
394 int TouchEventConverterEvdev::NextTrackingId() {
395 return next_tracking_id_
++ & kMaxTrackingId
;