1 // Copyright 2013 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.h"
8 #include <linux/input.h>
16 #include "base/bind.h"
17 #include "base/callback.h"
18 #include "base/logging.h"
19 #include "base/message_loop/message_loop.h"
20 #include "base/message_loop/message_pump_ozone.h"
21 #include "ui/events/event.h"
22 #include "ui/events/event_constants.h"
23 #include "ui/gfx/ozone/surface_factory_ozone.h"
27 // Number is determined empirically.
28 // TODO(rjkroege): Configure this per device.
29 const float kFingerWidth = 25.f;
35 TouchEventConverterEvdev::TouchEventConverterEvdev(int fd, int id)
40 x_max_(std::numeric_limits<int>::max()),
41 y_max_(std::numeric_limits<int>::max()),
48 TouchEventConverterEvdev::~TouchEventConverterEvdev() {
50 DLOG(WARNING) << "failed close on /dev/input/event" << id_;
53 void TouchEventConverterEvdev::Init() {
54 input_absinfo abs = {};
55 if (ioctl(fd_, EVIOCGABS(ABS_MT_SLOT), &abs) != -1) {
56 CHECK_GE(abs.maximum, abs.minimum);
57 CHECK_GE(abs.minimum, 0);
59 DLOG(WARNING) << "failed ioctl EVIOCGABS ABS_MT_SLOT event" << id_;
61 if (ioctl(fd_, EVIOCGABS(ABS_MT_PRESSURE), &abs) != -1) {
62 pressure_min_ = abs.minimum;
63 pressure_max_ = abs.maximum;
65 DLOG(WARNING) << "failed ioctl EVIOCGABS ABS_MT_PRESSURE event" << id_;
67 int x_min = 0, x_max = 0;
68 if (ioctl(fd_, EVIOCGABS(ABS_MT_POSITION_X), &abs) != -1) {
72 LOG(WARNING) << "failed ioctl EVIOCGABS ABS_X event" << id_;
74 int y_min = 0, y_max = 0;
75 if (ioctl(fd_, EVIOCGABS(ABS_MT_POSITION_Y), &abs) != -1) {
79 LOG(WARNING) << "failed ioctl EVIOCGABS ABS_Y event" << id_;
81 if (x_max && y_max && gfx::SurfaceFactoryOzone::GetInstance()) {
83 gfx::SurfaceFactoryOzone::GetInstance()->DefaultDisplaySpec();
84 int screen_width, screen_height;
85 int sc = sscanf(display, "%dx%d", &screen_width, &screen_height);
87 x_scale_ = (double)screen_width / (x_max - x_min);
88 y_scale_ = (double)screen_height / (y_max - y_min);
89 x_max_ = screen_width - 1;
90 y_max_ = screen_height - 1;
91 LOG(INFO) << "touch input x_scale=" << x_scale_
92 << " y_scale=" << y_scale_;
94 LOG(WARNING) << "malformed display spec from "
95 << "SurfaceFactoryOzone::DefaultDisplaySpec";
100 void TouchEventConverterEvdev::OnFileCanWriteWithoutBlocking(int /* fd */) {
101 // Read-only file-descriptors.
105 void TouchEventConverterEvdev::OnFileCanReadWithoutBlocking(int fd) {
106 input_event inputs[MAX_FINGERS * 6 + 1];
107 ssize_t read_size = read(fd, inputs, sizeof(inputs));
111 for (unsigned i = 0; i < read_size / sizeof(*inputs); i++) {
112 const input_event& input = inputs[i];
113 if (input.type == EV_ABS) {
114 switch (input.code) {
115 case ABS_MT_TOUCH_MAJOR:
116 altered_slots_.set(current_slot_);
117 events_[current_slot_].major_ = input.value;
120 case ABS_MT_POSITION_X:
121 altered_slots_.set(current_slot_);
122 events_[current_slot_].x_ = roundf(input.value * x_scale_);
125 case ABS_MT_POSITION_Y:
126 altered_slots_.set(current_slot_);
127 events_[current_slot_].y_ = roundf(input.value * y_scale_);
129 case ABS_MT_TRACKING_ID:
130 altered_slots_.set(current_slot_);
131 if (input.value < 0) {
132 events_[current_slot_].type_ = ET_TOUCH_RELEASED;
134 events_[current_slot_].finger_ = input.value;
135 events_[current_slot_].type_ = ET_TOUCH_PRESSED;
138 case ABS_MT_PRESSURE:
140 altered_slots_.set(current_slot_);
141 events_[current_slot_].pressure_ = input.value - pressure_min_;
142 events_[current_slot_].pressure_ /= pressure_max_ - pressure_min_;
145 current_slot_ = input.value;
146 altered_slots_.set(current_slot_);
151 } else if (input.type == EV_SYN) {
152 switch (input.code) {
154 for (int j = 0; j < MAX_FINGERS; j++) {
155 if (altered_slots_[j]) {
156 // TODO(rjkroege): Support elliptical finger regions.
157 scoped_ptr<TouchEvent> tev(new TouchEvent(
159 gfx::Point(std::min(x_max_, events_[j].x_),
160 std::min(y_max_, events_[j].y_)),
163 base::TimeDelta::FromMicroseconds(
164 input.time.tv_sec * 1000000 + input.time.tv_usec),
165 events_[j].pressure_ * kFingerWidth,
166 events_[j].pressure_ * kFingerWidth,
168 events_[j].pressure_));
169 DispatchEvent(tev.PassAs<ui::Event>());
171 // Subsequent events for this finger will be touch-move until it
173 events_[j].type_ = ET_TOUCH_MOVED;
176 altered_slots_.reset();
181 NOTREACHED() << "SYN_MT events not supported.";