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