OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/arc/input/arc_input_bridge_impl.h" |
| 6 |
| 7 #include <linux/input.h> |
| 8 #include <fcntl.h> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/posix/eintr_wrapper.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" |
| 14 #include "components/arc/arc_bridge_service.h" |
| 15 #include "ui/aura/env.h" |
| 16 #include "ui/aura/env_observer.h" |
| 17 #include "ui/aura/window.h" |
| 18 #include "ui/aura/window_observer.h" |
| 19 #include "ui/events/event.h" |
| 20 #include "ui/events/event_handler.h" |
| 21 #include "ui/events/event_utils.h" |
| 22 #include "ui/events/keycodes/dom/keycode_converter.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 // input_event values for keyboard events. |
| 27 const int kKeyReleased = 0; |
| 28 const int kKeyPressed = 1; |
| 29 const int kKeyRepeated = 2; |
| 30 |
| 31 // maximum number of supported multi-touch slots (simultaneous fingers). |
| 32 const int kMaxSlots = 64; |
| 33 |
| 34 // tracking id of an empty slot. |
| 35 const int kEmptySlot = -1; |
| 36 |
| 37 // maximum possible pressure as defined in EventHubARC. |
| 38 // TODO(denniskempin): communicate maximum during initialization. |
| 39 const int kMaxPressure = 65536; |
| 40 |
| 41 // speed of the scroll emulation. Scroll events are reported at about 100 times |
| 42 // the speed of a scroll wheel. |
| 43 const float kScrollEmulationSpeed = 100.0f; |
| 44 |
| 45 struct MouseButtonMapping { |
| 46 int ui_flag; |
| 47 int evdev_code; |
| 48 } kMouseButtonMap[] = { |
| 49 {ui::EF_LEFT_MOUSE_BUTTON, BTN_LEFT}, |
| 50 {ui::EF_RIGHT_MOUSE_BUTTON, BTN_RIGHT}, |
| 51 {ui::EF_MIDDLE_MOUSE_BUTTON, BTN_MIDDLE}, |
| 52 }; |
| 53 |
| 54 // Offset between evdev key codes and chrome native key codes |
| 55 const int kXkbKeycodeOffset = 8; |
| 56 |
| 57 } // namespace |
| 58 |
| 59 namespace arc { |
| 60 |
| 61 ArcInputBridgeImpl::ArcInputBridgeImpl(ArcBridgeService* arc_bridge_service) |
| 62 : arc_bridge_service_(arc_bridge_service), |
| 63 offset_x_acc_(0.5f), |
| 64 offset_y_acc_(0.5f), |
| 65 current_slot_(-1), |
| 66 current_slot_tracking_ids_(kMaxSlots, kEmptySlot), |
| 67 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 68 weak_factory_(this) { |
| 69 DCHECK(arc_bridge_service->state() == ArcBridgeService::State::STOPPED); |
| 70 arc_bridge_service->AddObserver(this); |
| 71 |
| 72 aura::Env* env = aura::Env::GetInstanceDontCreate(); |
| 73 if (env) |
| 74 env->AddObserver(this); |
| 75 } |
| 76 |
| 77 ArcInputBridgeImpl::~ArcInputBridgeImpl() { |
| 78 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 79 arc_bridge_service_->RemoveObserver(this); |
| 80 |
| 81 aura::Env* env = aura::Env::GetInstanceDontCreate(); |
| 82 if (env) |
| 83 env->RemoveObserver(this); |
| 84 |
| 85 for (aura::Window* window : arc_windows_.windows()) { |
| 86 window->RemovePreTargetHandler(this); |
| 87 } |
| 88 } |
| 89 |
| 90 void ArcInputBridgeImpl::OnInstanceBootPhase(InstanceBootPhase phase) { |
| 91 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 92 if (phase != INSTANCE_BOOT_PHASE_SYSTEM_SERVICES_READY) |
| 93 return; |
| 94 |
| 95 keyboard_fd_ = CreateBridgeInputDevice("ChromeOS Keyboard", "keyboard"); |
| 96 mouse_fd_ = CreateBridgeInputDevice("ChromeOS Mouse", "mouse"); |
| 97 touchscreen_fd_ = |
| 98 CreateBridgeInputDevice("ChromeOS Touchscreen", "touchscreen"); |
| 99 } |
| 100 |
| 101 // Translates and sends a ui::Event to the appropriate bridge device of the |
| 102 // ARC instance. If the devices have not yet been initialized, the event |
| 103 // will be ignored. |
| 104 void ArcInputBridgeImpl::OnEvent(ui::Event* event) { |
| 105 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 106 if (event->IsKeyEvent()) { |
| 107 SendKeyEvent(static_cast<ui::KeyEvent*>(event)); |
| 108 } else if (event->IsMouseEvent() || event->IsScrollEvent()) { |
| 109 SendMouseEvent(static_cast<ui::MouseEvent*>(event)); |
| 110 } else if (event->IsTouchEvent()) { |
| 111 SendTouchEvent(static_cast<ui::TouchEvent*>(event)); |
| 112 } |
| 113 } |
| 114 |
| 115 // Attaches the input bridge to the window if it is marked as an ARC window. |
| 116 void ArcInputBridgeImpl::OnWindowInitialized(aura::Window* new_window) { |
| 117 if (new_window->name() == "ExoSurface") { |
| 118 arc_windows_.Add(new_window); |
| 119 new_window->AddPreTargetHandler(this); |
| 120 } |
| 121 } |
| 122 |
| 123 void ArcInputBridgeImpl::SendKeyEvent(ui::KeyEvent* event) { |
| 124 if (keyboard_fd_.get() < 0) { |
| 125 VLOG(2) << "No keyboard bridge device available."; |
| 126 return; |
| 127 } |
| 128 |
| 129 unsigned short evdev_code = DomCodeToEvdevCode(event->code()); |
| 130 int evdev_value = 0; |
| 131 if (event->type() == ui::ET_KEY_PRESSED) { |
| 132 if (event->flags() & ui::EF_IS_REPEAT) { |
| 133 evdev_value = kKeyRepeated; |
| 134 } else { |
| 135 evdev_value = kKeyPressed; |
| 136 } |
| 137 } else if (event->type() == ui::ET_KEY_RELEASED) { |
| 138 evdev_value = kKeyReleased; |
| 139 } else { |
| 140 NOTREACHED() << "Key should be either PRESSED or RELEASED."; |
| 141 } |
| 142 |
| 143 base::TimeDelta time_stamp = event->time_stamp(); |
| 144 SendKernelEvent(keyboard_fd_, time_stamp, EV_KEY, evdev_code, evdev_value); |
| 145 SendSynReport(keyboard_fd_, time_stamp); |
| 146 } |
| 147 |
| 148 void ArcInputBridgeImpl::SendTouchEvent(ui::TouchEvent* event) { |
| 149 if (touchscreen_fd_.get() < 0) { |
| 150 VLOG(2) << "No touchscreen bridge device available."; |
| 151 return; |
| 152 } |
| 153 |
| 154 ui::PointerDetails details = event->pointer_details(); |
| 155 base::TimeDelta time_stamp = event->time_stamp(); |
| 156 |
| 157 // find or assing a slot for this tracking id |
| 158 int slot_id = AcquireTouchSlot(event); |
| 159 if (slot_id < 0) { |
| 160 VLOG(1) << "Ran out of slot IDs."; |
| 161 return; |
| 162 } |
| 163 |
| 164 // we only need to send the slot ID when it has changed. |
| 165 if (slot_id != current_slot_) { |
| 166 current_slot_ = slot_id; |
| 167 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_SLOT, |
| 168 current_slot_); |
| 169 } |
| 170 |
| 171 // update tracking id |
| 172 if (event->type() == ui::ET_TOUCH_PRESSED) { |
| 173 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID, |
| 174 event->touch_id()); |
| 175 } else if (event->type() == ui::ET_TOUCH_RELEASED) { |
| 176 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID, |
| 177 kEmptySlot); |
| 178 } |
| 179 |
| 180 // update touch information |
| 181 if (event->type() == ui::ET_TOUCH_MOVED || |
| 182 event->type() == ui::ET_TOUCH_PRESSED) { |
| 183 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_X, |
| 184 event->x()); |
| 185 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_Y, |
| 186 event->y()); |
| 187 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MAJOR, |
| 188 details.radius_x()); |
| 189 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MINOR, |
| 190 details.radius_y()); |
| 191 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_PRESSURE, |
| 192 details.force() * kMaxPressure); |
| 193 } |
| 194 SendSynReport(touchscreen_fd_, time_stamp); |
| 195 } |
| 196 |
| 197 void ArcInputBridgeImpl::SendMouseEvent(ui::MouseEvent* event) { |
| 198 if (mouse_fd_.get() < 0) { |
| 199 VLOG(2) << "No mouse bridge device available."; |
| 200 return; |
| 201 } |
| 202 |
| 203 base::TimeDelta time_stamp = event->time_stamp(); |
| 204 |
| 205 // update location |
| 206 if (event->type() == ui::ET_MOUSE_MOVED || |
| 207 event->type() == ui::ET_MOUSE_DRAGGED) { |
| 208 SendKernelEvent(mouse_fd_, time_stamp, EV_ABS, ABS_X, event->x()); |
| 209 SendKernelEvent(mouse_fd_, time_stamp, EV_ABS, ABS_Y, event->y()); |
| 210 } |
| 211 |
| 212 // update buttons |
| 213 if (event->type() == ui::ET_MOUSE_PRESSED || |
| 214 event->type() == ui::ET_MOUSE_RELEASED) { |
| 215 int evdev_value = static_cast<int>(event->type() == ui::ET_MOUSE_PRESSED); |
| 216 for (MouseButtonMapping mapping : kMouseButtonMap) { |
| 217 if (event->changed_button_flags() & mapping.ui_flag) { |
| 218 SendKernelEvent(mouse_fd_, time_stamp, EV_KEY, mapping.evdev_code, |
| 219 evdev_value); |
| 220 } |
| 221 } |
| 222 } |
| 223 |
| 224 // update scroll wheel |
| 225 if (event->type() == ui::ET_SCROLL) { |
| 226 ui::ScrollEvent* scroll_event = static_cast<ui::ScrollEvent*>(event); |
| 227 // accumulate floating point scroll offset since we can only send full |
| 228 // integer |
| 229 // wheel events. |
| 230 offset_x_acc_ += scroll_event->x_offset_ordinal() / kScrollEmulationSpeed; |
| 231 offset_y_acc_ += scroll_event->y_offset_ordinal() / kScrollEmulationSpeed; |
| 232 |
| 233 int wheel = floor(offset_y_acc_); |
| 234 if (wheel != 0) { |
| 235 SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_WHEEL, wheel); |
| 236 offset_y_acc_ -= static_cast<float>(wheel); |
| 237 } |
| 238 |
| 239 int hwheel = floor(offset_x_acc_); |
| 240 if (hwheel != 0) { |
| 241 SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_HWHEEL, hwheel); |
| 242 offset_x_acc_ -= static_cast<float>(hwheel); |
| 243 } |
| 244 } |
| 245 |
| 246 SendSynReport(mouse_fd_, time_stamp); |
| 247 } |
| 248 |
| 249 void ArcInputBridgeImpl::SendKernelEvent(const base::ScopedFD& fd, |
| 250 base::TimeDelta time_stamp, |
| 251 unsigned short type, |
| 252 unsigned short code, |
| 253 int value) { |
| 254 DCHECK(fd.is_valid()); |
| 255 |
| 256 // Chrome does not always use the monotonic system time for event times. |
| 257 // For now always force the event time to the current system time. |
| 258 // TODO(denniskempin): To enable performance tracing of events we will want |
| 259 // to use the kernel-provided monotonic time stamps of events. |
| 260 time_stamp = ui::EventTimeForNow(); |
| 261 |
| 262 struct input_event event; |
| 263 event.time.tv_sec = time_stamp.InSeconds(); |
| 264 base::TimeDelta remainder = |
| 265 time_stamp - base::TimeDelta::FromSeconds(event.time.tv_sec); |
| 266 event.time.tv_usec = remainder.InMicroseconds(); |
| 267 event.type = type; |
| 268 event.code = code; |
| 269 event.value = value; |
| 270 |
| 271 // Write event to file descriptor |
| 272 size_t num_written = write(fd.get(), reinterpret_cast<void*>(&event), |
| 273 sizeof(struct input_event)); |
| 274 DCHECK_EQ(num_written, sizeof(struct input_event)); |
| 275 } |
| 276 |
| 277 void ArcInputBridgeImpl::SendSynReport(const base::ScopedFD& fd, |
| 278 base::TimeDelta time) { |
| 279 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 280 |
| 281 SendKernelEvent(fd, time, EV_SYN, SYN_REPORT, 0); |
| 282 } |
| 283 |
| 284 int ArcInputBridgeImpl::AcquireTouchSlot(ui::TouchEvent* event) { |
| 285 int slot_id; |
| 286 if (event->type() == ui::ET_TOUCH_PRESSED) { |
| 287 slot_id = FindTouchSlot(kEmptySlot); |
| 288 } else { |
| 289 slot_id = FindTouchSlot(event->touch_id()); |
| 290 } |
| 291 if (slot_id < 0) { |
| 292 return -1; |
| 293 } |
| 294 |
| 295 if (event->type() == ui::ET_TOUCH_RELEASED) { |
| 296 current_slot_tracking_ids_[slot_id] = kEmptySlot; |
| 297 } else if (event->type() == ui::ET_TOUCH_PRESSED) { |
| 298 current_slot_tracking_ids_[slot_id] = event->touch_id(); |
| 299 } |
| 300 return slot_id; |
| 301 } |
| 302 |
| 303 int ArcInputBridgeImpl::FindTouchSlot(int tracking_id) { |
| 304 for (int i = 0; i < kMaxSlots; ++i) { |
| 305 if (current_slot_tracking_ids_[i] == tracking_id) { |
| 306 return i; |
| 307 } |
| 308 } |
| 309 return -1; |
| 310 } |
| 311 |
| 312 unsigned short ArcInputBridgeImpl::DomCodeToEvdevCode(ui::DomCode dom_code) { |
| 313 int native_code = ui::KeycodeConverter::DomCodeToNativeKeycode(dom_code); |
| 314 if (native_code == ui::KeycodeConverter::InvalidNativeKeycode()) |
| 315 return KEY_RESERVED; |
| 316 |
| 317 return native_code - kXkbKeycodeOffset; |
| 318 } |
| 319 |
| 320 base::ScopedFD ArcInputBridgeImpl::CreateBridgeInputDevice( |
| 321 const std::string& name, |
| 322 const std::string& device_type) { |
| 323 if (!arc_bridge_service_) { |
| 324 VLOG(1) << "ArcBridgeService disappeared."; |
| 325 return base::ScopedFD(); |
| 326 } |
| 327 |
| 328 // Create file descriptor pair for communication |
| 329 int fd[2]; |
| 330 int res = HANDLE_EINTR(pipe(fd)); |
| 331 if (res < 0) { |
| 332 VPLOG(1) << "Cannot create pipe"; |
| 333 return base::ScopedFD(); |
| 334 } |
| 335 base::ScopedFD read_fd(fd[0]); |
| 336 base::ScopedFD write_fd(fd[1]); |
| 337 |
| 338 // The read end is sent to the instance, ownership of fd transfers. |
| 339 if (!arc_bridge_service_->RegisterInputDevice(name, device_type, |
| 340 std::move(read_fd))) { |
| 341 VLOG(1) << "Cannot create bridge input device"; |
| 342 return base::ScopedFD(); |
| 343 } |
| 344 |
| 345 // setup write end as non blocking |
| 346 int flags = HANDLE_EINTR(fcntl(write_fd.get(), F_GETFL, 0)); |
| 347 if (res < 0) { |
| 348 VPLOG(1) << "Cannot get file descriptor flags"; |
| 349 return base::ScopedFD(); |
| 350 } |
| 351 |
| 352 res = HANDLE_EINTR(fcntl(write_fd.get(), F_SETFL, flags | O_NONBLOCK)); |
| 353 if (res < 0) { |
| 354 VPLOG(1) << "Cannot set file descriptor flags"; |
| 355 return base::ScopedFD(); |
| 356 } |
| 357 return write_fd; |
| 358 } |
| 359 |
| 360 scoped_ptr<ArcInputBridge> ArcInputBridge::Create( |
| 361 ArcBridgeService* arc_bridge_service) { |
| 362 return make_scoped_ptr(new ArcInputBridgeImpl(arc_bridge_service)); |
| 363 } |
| 364 |
| 365 } // namespace arc |
OLD | NEW |