| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "ui/base/ozone/touch_event_converter_ozone.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <linux/input.h> |
| 9 #include <poll.h> |
| 10 #include <stdio.h> |
| 11 #include <unistd.h> |
| 12 |
| 13 #include <cmath> |
| 14 |
| 15 #include "base/bind.h" |
| 16 #include "base/callback.h" |
| 17 #include "base/logging.h" |
| 18 #include "base/message_loop.h" |
| 19 #include "base/message_pump_ozone.h" |
| 20 #include "ui/base/events/event.h" |
| 21 #include "ui/base/events/event_constants.h" |
| 22 #include "ui/base/ozone/surface_factory_ozone.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 // Number is determined empirically. |
| 27 // TODO(rjkroege): Configure this per device. |
| 28 const float kFingerWidth = 25.f; |
| 29 |
| 30 } // namespace |
| 31 |
| 32 namespace ui { |
| 33 |
| 34 TouchEventConverterOzone::TouchEventConverterOzone(int fd, int id) |
| 35 : pressure_min_(0), |
| 36 pressure_max_(0), |
| 37 x_scale_(1.), |
| 38 y_scale_(1.), |
| 39 current_slot_(0), |
| 40 fd_(fd), |
| 41 id_(id) { |
| 42 Init(); |
| 43 } |
| 44 |
| 45 TouchEventConverterOzone::~TouchEventConverterOzone() { |
| 46 if (close(fd_) < 0) |
| 47 DLOG(WARNING) << "failed close on /dev/input/event" << id_; |
| 48 } |
| 49 |
| 50 void TouchEventConverterOzone::Init() { |
| 51 input_absinfo abs = {}; |
| 52 if (ioctl(fd_, EVIOCGABS(ABS_MT_SLOT), &abs) != -1) { |
| 53 CHECK_GE(abs.maximum, abs.minimum); |
| 54 CHECK_GE(abs.minimum, 0); |
| 55 } else { |
| 56 DLOG(WARNING) << "failed ioctl EVIOCGABS ABS_MT_SLOT event" << id_; |
| 57 } |
| 58 if (ioctl(fd_, EVIOCGABS(ABS_MT_PRESSURE), &abs) != -1) { |
| 59 pressure_min_ = abs.minimum; |
| 60 pressure_max_ = abs.maximum; |
| 61 } else { |
| 62 DLOG(WARNING) << "failed ioctl EVIOCGABS ABS_MT_PRESSURE event" << id_; |
| 63 } |
| 64 int x_min = 0, x_max = 0; |
| 65 if (ioctl(fd_, EVIOCGABS(ABS_MT_POSITION_X), &abs) != -1) { |
| 66 x_min = abs.minimum; |
| 67 x_max = abs.maximum; |
| 68 } else { |
| 69 LOG(WARNING) << "failed ioctl EVIOCGABS ABS_X event" << id_; |
| 70 } |
| 71 int y_min = 0, y_max = 0; |
| 72 if (ioctl(fd_, EVIOCGABS(ABS_MT_POSITION_Y), &abs) != -1) { |
| 73 y_min = abs.minimum; |
| 74 y_max = abs.maximum; |
| 75 } else { |
| 76 LOG(WARNING) << "failed ioctl EVIOCGABS ABS_Y event" << id_; |
| 77 } |
| 78 if (x_max && y_max && SurfaceFactoryOzone::GetInstance()) { |
| 79 const char* display = |
| 80 SurfaceFactoryOzone::GetInstance()->DefaultDisplaySpec(); |
| 81 int screen_width, screen_height; |
| 82 int sc = sscanf(display, "%dx%d", &screen_width, &screen_height); |
| 83 if (sc == 2) { |
| 84 x_scale_ = (double)screen_width / (x_max - x_min); |
| 85 y_scale_ = (double)screen_height / (y_max - y_min); |
| 86 LOG(INFO) << "touch input x_scale=" << x_scale_ |
| 87 << " y_scale=" << y_scale_; |
| 88 } else { |
| 89 LOG(WARNING) << "malformed display spec from " |
| 90 << "SurfaceFactoryOzone::DefaultDisplaySpec"; |
| 91 } |
| 92 } |
| 93 } |
| 94 |
| 95 void TouchEventConverterOzone::OnFileCanWriteWithoutBlocking(int /* fd */) { |
| 96 // Read-only file-descriptors. |
| 97 NOTREACHED(); |
| 98 } |
| 99 |
| 100 void TouchEventConverterOzone::OnFileCanReadWithoutBlocking(int fd) { |
| 101 input_event inputs[MAX_FINGERS * 6 + 1]; |
| 102 ssize_t read_size = read(fd, inputs, sizeof(inputs)); |
| 103 if (read_size <= 0) |
| 104 return; |
| 105 |
| 106 for (unsigned i = 0; i < read_size / sizeof(*inputs); i++) { |
| 107 const input_event& input = inputs[i]; |
| 108 if (input.type == EV_ABS) { |
| 109 switch (input.code) { |
| 110 case ABS_MT_TOUCH_MAJOR: |
| 111 altered_slots_.set(current_slot_); |
| 112 events_[current_slot_].major_ = input.value; |
| 113 break; |
| 114 case ABS_X: |
| 115 case ABS_MT_POSITION_X: |
| 116 altered_slots_.set(current_slot_); |
| 117 events_[current_slot_].x_ = roundf(input.value * x_scale_); |
| 118 break; |
| 119 case ABS_Y: |
| 120 case ABS_MT_POSITION_Y: |
| 121 altered_slots_.set(current_slot_); |
| 122 events_[current_slot_].y_ = roundf(input.value * y_scale_); |
| 123 break; |
| 124 case ABS_MT_TRACKING_ID: |
| 125 altered_slots_.set(current_slot_); |
| 126 if (input.value < 0) { |
| 127 events_[current_slot_].type_ = ET_TOUCH_RELEASED; |
| 128 } else { |
| 129 events_[current_slot_].finger_ = input.value; |
| 130 events_[current_slot_].type_ = ET_TOUCH_PRESSED; |
| 131 } |
| 132 break; |
| 133 case ABS_MT_PRESSURE: |
| 134 case ABS_PRESSURE: |
| 135 altered_slots_.set(current_slot_); |
| 136 events_[current_slot_].pressure_ = input.value - pressure_min_; |
| 137 events_[current_slot_].pressure_ /= pressure_max_ - pressure_min_; |
| 138 break; |
| 139 case ABS_MT_SLOT: |
| 140 current_slot_ = input.value; |
| 141 altered_slots_.set(current_slot_); |
| 142 break; |
| 143 default: |
| 144 NOTREACHED(); |
| 145 } |
| 146 } else if (input.type == EV_SYN) { |
| 147 switch (input.code) { |
| 148 case SYN_REPORT: |
| 149 for (int j = 0; j < MAX_FINGERS; j++) { |
| 150 if (altered_slots_[j]) { |
| 151 // TODO(rjkroege): Support elliptical finger regions. |
| 152 TouchEvent* tev = new TouchEvent( |
| 153 events_[j].type_, |
| 154 gfx::Point(events_[j].x_, events_[j].y_), |
| 155 /* flags */ 0, |
| 156 /* touch_id */ j, |
| 157 base::TimeDelta::FromMicroseconds( |
| 158 input.time.tv_sec * 1000000 + input.time.tv_usec), |
| 159 events_[j].pressure_ * kFingerWidth, |
| 160 events_[j].pressure_ * kFingerWidth, |
| 161 /* angle */ 0., |
| 162 events_[j].pressure_); |
| 163 events_[j].type_ = ET_TOUCH_MOVED; |
| 164 DispatchEvent(tev); |
| 165 |
| 166 } |
| 167 } |
| 168 altered_slots_.reset(); |
| 169 break; |
| 170 case SYN_MT_REPORT: |
| 171 case SYN_CONFIG: |
| 172 case SYN_DROPPED: |
| 173 NOTREACHED() << "Nexus Galaxy doesn't generate SYN_MT events."; |
| 174 break; |
| 175 } |
| 176 } else { |
| 177 NOTREACHED(); |
| 178 } |
| 179 } |
| 180 } |
| 181 |
| 182 } // namespace ui |
| OLD | NEW |