| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" | 5 #include "ui/events/ozone/evdev/touch_event_converter_evdev.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <linux/input.h> | 9 #include <linux/input.h> |
| 10 #include <poll.h> | 10 #include <poll.h> |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 | 13 |
| 14 #include <cmath> | 14 #include <cmath> |
| 15 #include <limits> | 15 #include <limits> |
| 16 | 16 |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/callback.h" | 18 #include "base/callback.h" |
| 19 #include "base/command_line.h" | |
| 20 #include "base/logging.h" | 19 #include "base/logging.h" |
| 21 #include "base/memory/scoped_vector.h" | 20 #include "base/memory/scoped_vector.h" |
| 22 #include "base/message_loop/message_loop.h" | 21 #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/event.h" | 22 #include "ui/events/event.h" |
| 27 #include "ui/events/event_constants.h" | 23 #include "ui/events/event_constants.h" |
| 28 #include "ui/events/event_switches.h" | 24 #include "ui/events/event_switches.h" |
| 29 #include "ui/gfx/screen.h" | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 struct TouchCalibration { | |
| 34 int bezel_left; | |
| 35 int bezel_right; | |
| 36 int bezel_top; | |
| 37 int bezel_bottom; | |
| 38 }; | |
| 39 | |
| 40 void GetTouchCalibration(TouchCalibration* cal) { | |
| 41 std::vector<std::string> parts; | |
| 42 if (Tokenize(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 43 switches::kTouchCalibration), | |
| 44 ",", | |
| 45 &parts) >= 4) { | |
| 46 if (!base::StringToInt(parts[0], &cal->bezel_left)) | |
| 47 DLOG(ERROR) << "Incorrect left border calibration value passed."; | |
| 48 if (!base::StringToInt(parts[1], &cal->bezel_right)) | |
| 49 DLOG(ERROR) << "Incorrect right border calibration value passed."; | |
| 50 if (!base::StringToInt(parts[2], &cal->bezel_top)) | |
| 51 DLOG(ERROR) << "Incorrect top border calibration value passed."; | |
| 52 if (!base::StringToInt(parts[3], &cal->bezel_bottom)) | |
| 53 DLOG(ERROR) << "Incorrect bottom border calibration value passed."; | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 float TuxelsToPixels(float val, | |
| 58 float min_tuxels, | |
| 59 float num_tuxels, | |
| 60 float min_pixels, | |
| 61 float num_pixels) { | |
| 62 // Map [min_tuxels, min_tuxels + num_tuxels) to | |
| 63 // [min_pixels, min_pixels + num_pixels). | |
| 64 return min_pixels + (val - min_tuxels) * num_pixels / num_tuxels; | |
| 65 } | |
| 66 | |
| 67 float TuxelToPixelSize(float val, float num_tuxels, float num_pixels) { | |
| 68 return val * num_pixels / num_tuxels; | |
| 69 } | |
| 70 | |
| 71 } // namespace | |
| 72 | 25 |
| 73 namespace ui { | 26 namespace ui { |
| 74 | 27 |
| 75 TouchEventConverterEvdev::TouchEventConverterEvdev( | 28 TouchEventConverterEvdev::TouchEventConverterEvdev( |
| 76 int fd, | 29 int fd, |
| 77 base::FilePath path, | 30 base::FilePath path, |
| 78 int id, | 31 int id, |
| 79 const EventDeviceInfo& info, | 32 const EventDeviceInfo& info, |
| 80 const EventDispatchCallback& callback) | 33 const EventDispatchCallback& callback) |
| 81 : EventConverterEvdev(fd, path, id), | 34 : EventConverterEvdev(fd, path, id), |
| 82 callback_(callback), | 35 callback_(callback), |
| 83 syn_dropped_(false), | 36 syn_dropped_(false), |
| 84 is_type_a_(false), | 37 is_type_a_(false), |
| 85 current_slot_(0) { | 38 current_slot_(0) { |
| 86 Init(info); | 39 Init(info); |
| 87 } | 40 } |
| 88 | 41 |
| 89 TouchEventConverterEvdev::~TouchEventConverterEvdev() { | 42 TouchEventConverterEvdev::~TouchEventConverterEvdev() { |
| 90 Stop(); | 43 Stop(); |
| 91 close(fd_); | 44 close(fd_); |
| 92 } | 45 } |
| 93 | 46 |
| 94 void TouchEventConverterEvdev::Init(const EventDeviceInfo& info) { | 47 void TouchEventConverterEvdev::Init(const EventDeviceInfo& info) { |
| 95 gfx::Screen* screen = gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE); | |
| 96 if (!screen) | |
| 97 return; // No scaling. | |
| 98 gfx::Display display = screen->GetPrimaryDisplay(); | |
| 99 gfx::Size size = display.GetSizeInPixel(); | |
| 100 | |
| 101 pressure_min_ = info.GetAbsMinimum(ABS_MT_PRESSURE); | 48 pressure_min_ = info.GetAbsMinimum(ABS_MT_PRESSURE); |
| 102 pressure_max_ = info.GetAbsMaximum(ABS_MT_PRESSURE); | 49 pressure_max_ = info.GetAbsMaximum(ABS_MT_PRESSURE); |
| 103 x_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_X); | 50 x_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_X); |
| 104 x_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_X) - x_min_tuxels_ + 1; | 51 x_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_X) - x_min_tuxels_ + 1; |
| 105 y_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_Y); | 52 y_min_tuxels_ = info.GetAbsMinimum(ABS_MT_POSITION_Y); |
| 106 y_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_Y) - y_min_tuxels_ + 1; | 53 y_num_tuxels_ = info.GetAbsMaximum(ABS_MT_POSITION_Y) - y_min_tuxels_ + 1; |
| 107 native_size_ = gfx::Size(x_num_tuxels_, y_num_tuxels_); | 54 native_size_ = gfx::Size(x_num_tuxels_, y_num_tuxels_); |
| 108 | |
| 109 // Map coordinates onto screen. | |
| 110 x_min_pixels_ = 0; | |
| 111 y_min_pixels_ = 0; | |
| 112 x_num_pixels_ = size.width(); | |
| 113 y_num_pixels_ = size.height(); | |
| 114 | |
| 115 VLOG(1) << "mapping touch coordinates to screen coordinates: " | |
| 116 << base::StringPrintf("%dx%d", size.width(), size.height()); | |
| 117 | |
| 118 // Apply --touch-calibration. | |
| 119 TouchCalibration cal = {}; | |
| 120 GetTouchCalibration(&cal); | |
| 121 x_min_tuxels_ += cal.bezel_left; | |
| 122 x_num_tuxels_ -= cal.bezel_left + cal.bezel_right; | |
| 123 y_min_tuxels_ += cal.bezel_top; | |
| 124 y_num_tuxels_ -= cal.bezel_top + cal.bezel_bottom; | |
| 125 | |
| 126 VLOG(1) << "applying touch calibration: " | |
| 127 << base::StringPrintf("[%d, %d, %d, %d]", | |
| 128 cal.bezel_left, | |
| 129 cal.bezel_right, | |
| 130 cal.bezel_top, | |
| 131 cal.bezel_bottom); | |
| 132 } | 55 } |
| 133 | 56 |
| 134 bool TouchEventConverterEvdev::Reinitialize() { | 57 bool TouchEventConverterEvdev::Reinitialize() { |
| 135 EventDeviceInfo info; | 58 EventDeviceInfo info; |
| 136 if (info.Initialize(fd_)) { | 59 if (info.Initialize(fd_)) { |
| 137 Init(info); | 60 Init(info); |
| 138 return true; | 61 return true; |
| 139 } | 62 } |
| 140 return false; | 63 return false; |
| 141 } | 64 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 } | 111 } |
| 189 } | 112 } |
| 190 | 113 |
| 191 void TouchEventConverterEvdev::ProcessAbs(const input_event& input) { | 114 void TouchEventConverterEvdev::ProcessAbs(const input_event& input) { |
| 192 switch (input.code) { | 115 switch (input.code) { |
| 193 case ABS_MT_TOUCH_MAJOR: | 116 case ABS_MT_TOUCH_MAJOR: |
| 194 altered_slots_.set(current_slot_); | 117 altered_slots_.set(current_slot_); |
| 195 // TODO(spang): If we have all of major, minor, and orientation, | 118 // TODO(spang): If we have all of major, minor, and orientation, |
| 196 // we can scale the ellipse correctly. However on the Pixel we get | 119 // we can scale the ellipse correctly. However on the Pixel we get |
| 197 // neither minor nor orientation, so this is all we can do. | 120 // neither minor nor orientation, so this is all we can do. |
| 198 events_[current_slot_].radius_x_ = | 121 events_[current_slot_].radius_x_ = input.value / 2.0f; |
| 199 TuxelToPixelSize(input.value, x_num_tuxels_, x_num_pixels_) / 2.0f; | |
| 200 break; | 122 break; |
| 201 case ABS_MT_TOUCH_MINOR: | 123 case ABS_MT_TOUCH_MINOR: |
| 202 altered_slots_.set(current_slot_); | 124 altered_slots_.set(current_slot_); |
| 203 events_[current_slot_].radius_y_ = | 125 events_[current_slot_].radius_y_ = input.value / 2.0f; |
| 204 TuxelToPixelSize(input.value, y_num_tuxels_, y_num_pixels_) / 2.0f; | |
| 205 break; | 126 break; |
| 206 case ABS_MT_POSITION_X: | 127 case ABS_MT_POSITION_X: |
| 207 altered_slots_.set(current_slot_); | 128 altered_slots_.set(current_slot_); |
| 208 events_[current_slot_].x_ = TuxelsToPixels(input.value, | 129 events_[current_slot_].x_ = input.value - x_min_tuxels_; |
| 209 x_min_tuxels_, | |
| 210 x_num_tuxels_, | |
| 211 x_min_pixels_, | |
| 212 x_num_pixels_); | |
| 213 break; | 130 break; |
| 214 case ABS_MT_POSITION_Y: | 131 case ABS_MT_POSITION_Y: |
| 215 altered_slots_.set(current_slot_); | 132 altered_slots_.set(current_slot_); |
| 216 events_[current_slot_].y_ = TuxelsToPixels(input.value, | 133 events_[current_slot_].y_ = input.value - y_min_tuxels_; |
| 217 y_min_tuxels_, | |
| 218 y_num_tuxels_, | |
| 219 y_min_pixels_, | |
| 220 y_num_pixels_); | |
| 221 break; | 134 break; |
| 222 case ABS_MT_TRACKING_ID: | 135 case ABS_MT_TRACKING_ID: |
| 223 altered_slots_.set(current_slot_); | 136 altered_slots_.set(current_slot_); |
| 224 if (input.value < 0) { | 137 if (input.value < 0) { |
| 225 events_[current_slot_].type_ = ET_TOUCH_RELEASED; | 138 events_[current_slot_].type_ = ET_TOUCH_RELEASED; |
| 226 } else { | 139 } else { |
| 227 events_[current_slot_].finger_ = input.value; | 140 events_[current_slot_].finger_ = input.value; |
| 228 events_[current_slot_].type_ = ET_TOUCH_PRESSED; | 141 events_[current_slot_].type_ = ET_TOUCH_PRESSED; |
| 229 } | 142 } |
| 230 break; | 143 break; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 | 207 |
| 295 // Subsequent events for this finger will be touch-move until it | 208 // Subsequent events for this finger will be touch-move until it |
| 296 // is released. | 209 // is released. |
| 297 events_[i].type_ = ET_TOUCH_MOVED; | 210 events_[i].type_ = ET_TOUCH_MOVED; |
| 298 } | 211 } |
| 299 } | 212 } |
| 300 altered_slots_.reset(); | 213 altered_slots_.reset(); |
| 301 } | 214 } |
| 302 | 215 |
| 303 } // namespace ui | 216 } // namespace ui |
| OLD | NEW |