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/event_utils.h"
32 #include "ui/events/ozone/evdev/device_event_dispatcher_evdev.h"
33 #include "ui/events/ozone/evdev/touch_evdev_types.h"
34 #include "ui/events/ozone/evdev/touch_noise/touch_noise_finder.h"
38 const int kMaxTrackingId
= 0xffff; // TRKID_MAX in kernel.
40 struct TouchCalibration
{
47 void GetTouchCalibration(TouchCalibration
* cal
) {
48 std::vector
<std::string
> parts
;
49 if (Tokenize(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
50 switches::kTouchCalibration
),
52 if (!base::StringToInt(parts
[0], &cal
->bezel_left
))
53 LOG(ERROR
) << "Incorrect left border calibration value passed.";
54 if (!base::StringToInt(parts
[1], &cal
->bezel_right
))
55 LOG(ERROR
) << "Incorrect right border calibration value passed.";
56 if (!base::StringToInt(parts
[2], &cal
->bezel_top
))
57 LOG(ERROR
) << "Incorrect top border calibration value passed.";
58 if (!base::StringToInt(parts
[3], &cal
->bezel_bottom
))
59 LOG(ERROR
) << "Incorrect bottom border calibration value passed.";
63 int32_t AbsCodeToMtCode(int32_t code
) {
66 return ABS_MT_POSITION_X
;
68 return ABS_MT_POSITION_Y
;
70 return ABS_MT_PRESSURE
;
72 return ABS_MT_DISTANCE
;
78 const int kTrackingIdForUnusedSlot
= -1;
84 TouchEventConverterEvdev::TouchEventConverterEvdev(
89 DeviceEventDispatcherEvdev
* dispatcher
)
90 : EventConverterEvdev(fd
, path
, id
, type
),
91 dispatcher_(dispatcher
),
97 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
98 switches::kExtraTouchNoiseFiltering
)) {
99 touch_noise_finder_
.reset(new TouchNoiseFinder
);
103 TouchEventConverterEvdev::~TouchEventConverterEvdev() {
108 void TouchEventConverterEvdev::Initialize(const EventDeviceInfo
& info
) {
109 has_mt_
= info
.HasMultitouch();
112 pressure_min_
= info
.GetAbsMinimum(ABS_MT_PRESSURE
);
113 pressure_max_
= info
.GetAbsMaximum(ABS_MT_PRESSURE
);
114 x_min_tuxels_
= info
.GetAbsMinimum(ABS_MT_POSITION_X
);
115 x_num_tuxels_
= info
.GetAbsMaximum(ABS_MT_POSITION_X
) - x_min_tuxels_
+ 1;
116 y_min_tuxels_
= info
.GetAbsMinimum(ABS_MT_POSITION_Y
);
117 y_num_tuxels_
= info
.GetAbsMaximum(ABS_MT_POSITION_Y
) - y_min_tuxels_
+ 1;
119 std::min
<int>(info
.GetAbsMaximum(ABS_MT_SLOT
) + 1, kNumTouchEvdevSlots
);
120 current_slot_
= info
.GetAbsValue(ABS_MT_SLOT
);
122 pressure_min_
= info
.GetAbsMinimum(ABS_PRESSURE
);
123 pressure_max_
= info
.GetAbsMaximum(ABS_PRESSURE
);
124 x_min_tuxels_
= info
.GetAbsMinimum(ABS_X
);
125 x_num_tuxels_
= info
.GetAbsMaximum(ABS_X
) - x_min_tuxels_
+ 1;
126 y_min_tuxels_
= info
.GetAbsMinimum(ABS_Y
);
127 y_num_tuxels_
= info
.GetAbsMaximum(ABS_Y
) - y_min_tuxels_
+ 1;
132 // Apply --touch-calibration.
133 if (type() == INPUT_DEVICE_INTERNAL
) {
134 TouchCalibration cal
= {};
135 GetTouchCalibration(&cal
);
136 x_min_tuxels_
+= cal
.bezel_left
;
137 x_num_tuxels_
-= cal
.bezel_left
+ cal
.bezel_right
;
138 y_min_tuxels_
+= cal
.bezel_top
;
139 y_num_tuxels_
-= cal
.bezel_top
+ cal
.bezel_bottom
;
141 VLOG(1) << "applying touch calibration: "
142 << base::StringPrintf("[%d, %d, %d, %d]", cal
.bezel_left
,
143 cal
.bezel_right
, cal
.bezel_top
,
147 events_
.resize(touch_points_
);
150 for (size_t i
= 0; i
< events_
.size(); ++i
) {
151 events_
[i
].x
= info
.GetAbsMtSlotValueWithDefault(ABS_MT_POSITION_X
, i
, 0);
152 events_
[i
].y
= info
.GetAbsMtSlotValueWithDefault(ABS_MT_POSITION_Y
, i
, 0);
153 events_
[i
].tracking_id
= info
.GetAbsMtSlotValueWithDefault(
154 ABS_MT_TRACKING_ID
, i
, kTrackingIdForUnusedSlot
);
155 events_
[i
].touching
= (events_
[i
].tracking_id
>= 0);
158 // Dirty the slot so we'll update the consumer at the first opportunity.
159 // We can't dispatch here as this is currently called on the worker pool.
160 // TODO(spang): Move initialization off worker pool.
161 events_
[i
].altered
= true;
164 events_
[i
].radius_x
=
165 info
.GetAbsMtSlotValueWithDefault(ABS_MT_TOUCH_MAJOR
, i
, 0) / 2.0f
;
166 events_
[i
].radius_y
=
167 info
.GetAbsMtSlotValueWithDefault(ABS_MT_TOUCH_MINOR
, i
, 0) / 2.0f
;
168 events_
[i
].pressure
= ScalePressure(
169 info
.GetAbsMtSlotValueWithDefault(ABS_MT_PRESSURE
, i
, 0));
172 // TODO(spang): Add key state to EventDeviceInfo to allow initial contact.
175 events_
[0].tracking_id
= kTrackingIdForUnusedSlot
;
176 events_
[0].touching
= false;
178 events_
[0].radius_x
= 0;
179 events_
[0].radius_y
= 0;
180 events_
[0].pressure
= 0;
184 bool TouchEventConverterEvdev::Reinitialize() {
185 EventDeviceInfo info
;
186 if (info
.Initialize(fd_
)) {
193 bool TouchEventConverterEvdev::HasTouchscreen() const {
197 gfx::Size
TouchEventConverterEvdev::GetTouchscreenSize() const {
198 return gfx::Size(x_num_tuxels_
, y_num_tuxels_
);
201 int TouchEventConverterEvdev::GetTouchPoints() const {
202 return touch_points_
;
205 void TouchEventConverterEvdev::OnStopped() {
209 void TouchEventConverterEvdev::OnFileCanReadWithoutBlocking(int fd
) {
210 input_event inputs
[kNumTouchEvdevSlots
* 6 + 1];
211 ssize_t read_size
= read(fd
, inputs
, sizeof(inputs
));
213 if (errno
== EINTR
|| errno
== EAGAIN
)
216 PLOG(ERROR
) << "error reading device " << path_
.value();
224 for (unsigned i
= 0; i
< read_size
/ sizeof(*inputs
); i
++) {
226 // Emulate the device as an MT device with only 1 slot by inserting extra
227 // MT protocol events in the stream.
228 EmulateMultitouchEvent(inputs
[i
]);
231 ProcessMultitouchEvent(inputs
[i
]);
235 void TouchEventConverterEvdev::ProcessMultitouchEvent(
236 const input_event
& input
) {
237 if (input
.type
== EV_SYN
) {
239 } else if (syn_dropped_
) {
240 // Do nothing. This branch indicates we have lost sync with the driver.
241 } else if (input
.type
== EV_ABS
) {
242 if (events_
.size() <= current_slot_
) {
243 LOG(ERROR
) << "current_slot_ (" << current_slot_
244 << ") >= events_.size() (" << events_
.size() << ")";
248 } else if (input
.type
== EV_KEY
) {
251 NOTIMPLEMENTED() << "invalid type: " << input
.type
;
255 void TouchEventConverterEvdev::EmulateMultitouchEvent(
256 const input_event
& event
) {
257 input_event emulated_event
= event
;
259 if (event
.type
== EV_ABS
) {
260 emulated_event
.code
= AbsCodeToMtCode(event
.code
);
261 if (emulated_event
.code
>= 0)
262 ProcessMultitouchEvent(emulated_event
);
263 } else if (event
.type
== EV_KEY
&& event
.code
== BTN_TOUCH
) {
264 emulated_event
.type
= EV_ABS
;
265 emulated_event
.code
= ABS_MT_TRACKING_ID
;
266 emulated_event
.value
=
267 event
.value
? NextTrackingId() : kTrackingIdForUnusedSlot
;
268 ProcessMultitouchEvent(emulated_event
);
272 void TouchEventConverterEvdev::ProcessKey(const input_event
& input
) {
273 switch (input
.code
) {
277 NOTIMPLEMENTED() << "invalid code for EV_KEY: " << input
.code
;
281 void TouchEventConverterEvdev::ProcessAbs(const input_event
& input
) {
282 switch (input
.code
) {
283 case ABS_MT_TOUCH_MAJOR
:
284 // TODO(spang): If we have all of major, minor, and orientation,
285 // we can scale the ellipse correctly. However on the Pixel we get
286 // neither minor nor orientation, so this is all we can do.
287 events_
[current_slot_
].radius_x
= input
.value
/ 2.0f
;
289 case ABS_MT_TOUCH_MINOR
:
290 events_
[current_slot_
].radius_y
= input
.value
/ 2.0f
;
292 case ABS_MT_POSITION_X
:
293 events_
[current_slot_
].x
= input
.value
;
295 case ABS_MT_POSITION_Y
:
296 events_
[current_slot_
].y
= input
.value
;
298 case ABS_MT_TRACKING_ID
:
299 UpdateTrackingId(current_slot_
, input
.value
);
301 case ABS_MT_PRESSURE
:
302 events_
[current_slot_
].pressure
= ScalePressure(input
.value
);
305 if (input
.value
>= 0 &&
306 static_cast<size_t>(input
.value
) < events_
.size()) {
307 current_slot_
= input
.value
;
309 LOG(ERROR
) << "invalid touch event index: " << input
.value
;
314 DVLOG(5) << "unhandled code for EV_ABS: " << input
.code
;
317 events_
[current_slot_
].altered
= true;
320 void TouchEventConverterEvdev::ProcessSyn(const input_event
& input
) {
321 switch (input
.code
) {
324 // Have to re-initialize.
325 if (Reinitialize()) {
326 syn_dropped_
= false;
328 LOG(ERROR
) << "failed to re-initialize device info";
331 ReportEvents(EventConverterEvdev::TimeDeltaFromInputEvent(input
));
335 // Some buffer has overrun. We ignore all events up to and
336 // including the next SYN_REPORT.
340 NOTIMPLEMENTED() << "invalid code for EV_SYN: " << input
.code
;
344 EventType
TouchEventConverterEvdev::GetEventTypeForTouch(
345 const InProgressTouchEvdev
& touch
) {
349 if (touch_noise_finder_
&& touch_noise_finder_
->SlotHasNoise(touch
.slot
)) {
350 if (touch
.touching
&& !touch
.was_touching
)
352 return ET_TOUCH_CANCELLED
;
356 return touch
.was_touching
? ET_TOUCH_MOVED
: ET_TOUCH_PRESSED
;
357 return touch
.was_touching
? ET_TOUCH_RELEASED
: ET_UNKNOWN
;
360 void TouchEventConverterEvdev::ReportEvent(const InProgressTouchEvdev
& event
,
361 EventType event_type
,
362 const base::TimeDelta
& timestamp
) {
363 dispatcher_
->DispatchTouchEvent(TouchEventParams(
364 id_
, event
.slot
, event_type
, gfx::PointF(event
.x
, event
.y
),
365 gfx::Vector2dF(event
.radius_x
, event
.radius_y
), event
.pressure
,
369 void TouchEventConverterEvdev::ReportEvents(base::TimeDelta delta
) {
370 if (touch_noise_finder_
)
371 touch_noise_finder_
->HandleTouches(events_
, delta
);
373 for (size_t i
= 0; i
< events_
.size(); i
++) {
374 InProgressTouchEvdev
* event
= &events_
[i
];
378 EventType event_type
= GetEventTypeForTouch(*event
);
379 if (event_type
== ET_UNKNOWN
|| event_type
== ET_TOUCH_CANCELLED
)
380 event
->cancelled
= true;
382 if (event_type
!= ET_UNKNOWN
)
383 ReportEvent(*event
, event_type
, delta
);
385 event
->was_touching
= event
->touching
;
386 event
->altered
= false;
390 void TouchEventConverterEvdev::UpdateTrackingId(int slot
, int tracking_id
) {
391 InProgressTouchEvdev
* event
= &events_
[slot
];
393 if (event
->tracking_id
== tracking_id
)
396 event
->tracking_id
= tracking_id
;
397 event
->touching
= (tracking_id
>= 0);
398 event
->altered
= true;
400 if (tracking_id
>= 0)
401 event
->cancelled
= false;
404 void TouchEventConverterEvdev::ReleaseTouches() {
405 for (size_t slot
= 0; slot
< events_
.size(); slot
++)
406 UpdateTrackingId(slot
, kTrackingIdForUnusedSlot
);
408 ReportEvents(EventTimeForNow());
411 float TouchEventConverterEvdev::ScalePressure(int32_t value
) {
412 float pressure
= value
- pressure_min_
;
413 if (pressure_max_
- pressure_min_
)
414 pressure
/= pressure_max_
- pressure_min_
;
418 int TouchEventConverterEvdev::NextTrackingId() {
419 return next_tracking_id_
++ & kMaxTrackingId
;