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"
35 struct TouchCalibration
{
42 void GetTouchCalibration(TouchCalibration
* cal
) {
43 std::vector
<std::string
> parts
;
44 if (Tokenize(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
45 switches::kTouchCalibration
),
47 if (!base::StringToInt(parts
[0], &cal
->bezel_left
))
48 DLOG(ERROR
) << "Incorrect left border calibration value passed.";
49 if (!base::StringToInt(parts
[1], &cal
->bezel_right
))
50 DLOG(ERROR
) << "Incorrect right border calibration value passed.";
51 if (!base::StringToInt(parts
[2], &cal
->bezel_top
))
52 DLOG(ERROR
) << "Incorrect top border calibration value passed.";
53 if (!base::StringToInt(parts
[3], &cal
->bezel_bottom
))
54 DLOG(ERROR
) << "Incorrect bottom border calibration value passed.";
62 TouchEventConverterEvdev::InProgressEvents::InProgressEvents()
74 TouchEventConverterEvdev::TouchEventConverterEvdev(
79 DeviceEventDispatcherEvdev
* dispatcher
)
80 : EventConverterEvdev(fd
, path
, id
, type
),
81 dispatcher_(dispatcher
),
87 TouchEventConverterEvdev::~TouchEventConverterEvdev() {
92 void TouchEventConverterEvdev::Initialize(const EventDeviceInfo
& info
) {
93 pressure_min_
= info
.GetAbsMinimum(ABS_MT_PRESSURE
);
94 pressure_max_
= info
.GetAbsMaximum(ABS_MT_PRESSURE
);
95 x_min_tuxels_
= info
.GetAbsMinimum(ABS_MT_POSITION_X
);
96 x_num_tuxels_
= info
.GetAbsMaximum(ABS_MT_POSITION_X
) - x_min_tuxels_
+ 1;
97 y_min_tuxels_
= info
.GetAbsMinimum(ABS_MT_POSITION_Y
);
98 y_num_tuxels_
= info
.GetAbsMaximum(ABS_MT_POSITION_Y
) - y_min_tuxels_
+ 1;
100 // Apply --touch-calibration.
101 if (type() == INPUT_DEVICE_INTERNAL
) {
102 TouchCalibration cal
= {};
103 GetTouchCalibration(&cal
);
104 x_min_tuxels_
+= cal
.bezel_left
;
105 x_num_tuxels_
-= cal
.bezel_left
+ cal
.bezel_right
;
106 y_min_tuxels_
+= cal
.bezel_top
;
107 y_num_tuxels_
-= cal
.bezel_top
+ cal
.bezel_bottom
;
109 VLOG(1) << "applying touch calibration: "
110 << base::StringPrintf("[%d, %d, %d, %d]", cal
.bezel_left
,
111 cal
.bezel_right
, cal
.bezel_top
,
115 native_size_
= gfx::Size(x_num_tuxels_
, y_num_tuxels_
);
118 std::min
<int>(info
.GetAbsMaximum(ABS_MT_SLOT
) + 1, MAX_FINGERS
));
119 for (size_t i
= 0; i
< events_
.size(); ++i
) {
120 events_
[i
].finger_
= info
.GetSlotValue(ABS_MT_TRACKING_ID
, i
);
122 events_
[i
].finger_
< 0 ? ET_TOUCH_RELEASED
: ET_TOUCH_PRESSED
;
123 events_
[i
].x_
= info
.GetSlotValue(ABS_MT_POSITION_X
, i
);
124 events_
[i
].y_
= info
.GetSlotValue(ABS_MT_POSITION_Y
, i
);
125 events_
[i
].radius_x_
= info
.GetSlotValue(ABS_MT_TOUCH_MAJOR
, i
);
126 events_
[i
].radius_y_
= info
.GetSlotValue(ABS_MT_TOUCH_MINOR
, i
);
127 events_
[i
].pressure_
= info
.GetSlotValue(ABS_MT_PRESSURE
, i
);
131 bool TouchEventConverterEvdev::Reinitialize() {
132 EventDeviceInfo info
;
133 if (info
.Initialize(fd_
)) {
140 bool TouchEventConverterEvdev::HasTouchscreen() const {
144 gfx::Size
TouchEventConverterEvdev::GetTouchscreenSize() const {
148 void TouchEventConverterEvdev::OnFileCanReadWithoutBlocking(int fd
) {
149 input_event inputs
[MAX_FINGERS
* 6 + 1];
150 ssize_t read_size
= read(fd
, inputs
, sizeof(inputs
));
152 if (errno
== EINTR
|| errno
== EAGAIN
)
155 PLOG(ERROR
) << "error reading device " << path_
.value();
163 for (unsigned i
= 0; i
< read_size
/ sizeof(*inputs
); i
++) {
164 ProcessInputEvent(inputs
[i
]);
168 void TouchEventConverterEvdev::ProcessInputEvent(const input_event
& input
) {
169 if (input
.type
== EV_SYN
) {
171 } else if(syn_dropped_
) {
172 // Do nothing. This branch indicates we have lost sync with the driver.
173 } else if (input
.type
== EV_ABS
) {
174 if (events_
.size() <= current_slot_
) {
175 LOG(ERROR
) << "current_slot_ (" << current_slot_
176 << ") >= events_.size() (" << events_
.size() << ")";
180 } else if (input
.type
== EV_KEY
) {
181 switch (input
.code
) {
185 NOTIMPLEMENTED() << "invalid code for EV_KEY: " << input
.code
;
188 NOTIMPLEMENTED() << "invalid type: " << input
.type
;
192 void TouchEventConverterEvdev::ProcessAbs(const input_event
& input
) {
193 switch (input
.code
) {
194 case ABS_MT_TOUCH_MAJOR
:
195 // TODO(spang): If we have all of major, minor, and orientation,
196 // we can scale the ellipse correctly. However on the Pixel we get
197 // neither minor nor orientation, so this is all we can do.
198 events_
[current_slot_
].radius_x_
= input
.value
/ 2.0f
;
200 case ABS_MT_TOUCH_MINOR
:
201 events_
[current_slot_
].radius_y_
= input
.value
/ 2.0f
;
203 case ABS_MT_POSITION_X
:
204 events_
[current_slot_
].x_
= input
.value
;
206 case ABS_MT_POSITION_Y
:
207 events_
[current_slot_
].y_
= input
.value
;
209 case ABS_MT_TRACKING_ID
:
210 if (input
.value
< 0) {
211 events_
[current_slot_
].type_
= ET_TOUCH_RELEASED
;
213 events_
[current_slot_
].finger_
= input
.value
;
214 events_
[current_slot_
].type_
= ET_TOUCH_PRESSED
;
217 case ABS_MT_PRESSURE
:
218 events_
[current_slot_
].pressure_
= input
.value
- pressure_min_
;
219 events_
[current_slot_
].pressure_
/= pressure_max_
- pressure_min_
;
222 if (input
.value
>= 0 &&
223 static_cast<size_t>(input
.value
) < events_
.size()) {
224 current_slot_
= input
.value
;
226 LOG(ERROR
) << "invalid touch event index: " << input
.value
;
231 DVLOG(5) << "unhandled code for EV_ABS: " << input
.code
;
234 events_
[current_slot_
].altered_
= true;
237 void TouchEventConverterEvdev::ProcessSyn(const input_event
& input
) {
238 switch (input
.code
) {
241 // Have to re-initialize.
242 if (Reinitialize()) {
243 syn_dropped_
= false;
244 for(InProgressEvents
& event
: events_
)
245 event
.altered_
= false;
247 LOG(ERROR
) << "failed to re-initialize device info";
250 ReportEvents(base::TimeDelta::FromMicroseconds(
251 input
.time
.tv_sec
* 1000000 + input
.time
.tv_usec
));
257 // For type A devices, we just get a stream of all current contacts,
258 // in some arbitrary order.
259 events_
[current_slot_
].type_
= ET_TOUCH_PRESSED
;
260 if (events_
.size() - 1 > current_slot_
)
265 // Some buffer has overrun. We ignore all events up to and
266 // including the next SYN_REPORT.
270 NOTIMPLEMENTED() << "invalid code for EV_SYN: " << input
.code
;
274 void TouchEventConverterEvdev::ReportEvent(int touch_id
,
275 const InProgressEvents
& event
,
276 const base::TimeDelta
& timestamp
) {
277 dispatcher_
->DispatchTouchEvent(TouchEventParams(
278 id_
, touch_id
, event
.type_
, gfx::PointF(event
.x_
, event
.y_
),
279 gfx::Vector2dF(event
.radius_x_
, event
.radius_y_
), event
.pressure_
,
283 void TouchEventConverterEvdev::ReportEvents(base::TimeDelta delta
) {
284 for (size_t i
= 0; i
< events_
.size(); i
++) {
285 if (events_
[i
].altered_
) {
286 ReportEvent(i
, events_
[i
], delta
);
288 // Subsequent events for this finger will be touch-move until it
290 events_
[i
].type_
= ET_TOUCH_MOVED
;
291 events_
[i
].altered_
= false;