| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "components/arc/input/arc_input_bridge_impl.h" | 5 #include "components/arc/input/arc_input_bridge_impl.h" |
| 6 | 6 |
| 7 #include <linux/input.h> | 7 #include <linux/input.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 | 9 |
| 10 #include <string> |
| 11 |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/posix/eintr_wrapper.h" | 13 #include "base/posix/eintr_wrapper.h" |
| 12 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 14 #include "components/arc/arc_bridge_service.h" | 16 #include "components/arc/arc_bridge_service.h" |
| 17 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" |
| 15 #include "ui/aura/env.h" | 18 #include "ui/aura/env.h" |
| 16 #include "ui/aura/env_observer.h" | 19 #include "ui/aura/env_observer.h" |
| 17 #include "ui/aura/window.h" | 20 #include "ui/aura/window.h" |
| 18 #include "ui/aura/window_observer.h" | 21 #include "ui/aura/window_observer.h" |
| 19 #include "ui/events/event.h" | 22 #include "ui/events/event.h" |
| 20 #include "ui/events/event_handler.h" | 23 #include "ui/events/event_handler.h" |
| 21 #include "ui/events/event_utils.h" | 24 #include "ui/events/event_utils.h" |
| 22 #include "ui/events/keycodes/dom/keycode_converter.h" | 25 #include "ui/events/keycodes/dom/keycode_converter.h" |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 namespace arc { | 62 namespace arc { |
| 60 | 63 |
| 61 ArcInputBridgeImpl::ArcInputBridgeImpl(ArcBridgeService* arc_bridge_service) | 64 ArcInputBridgeImpl::ArcInputBridgeImpl(ArcBridgeService* arc_bridge_service) |
| 62 : arc_bridge_service_(arc_bridge_service), | 65 : arc_bridge_service_(arc_bridge_service), |
| 63 offset_x_acc_(0.5f), | 66 offset_x_acc_(0.5f), |
| 64 offset_y_acc_(0.5f), | 67 offset_y_acc_(0.5f), |
| 65 current_slot_(-1), | 68 current_slot_(-1), |
| 66 current_slot_tracking_ids_(kMaxSlots, kEmptySlot), | 69 current_slot_tracking_ids_(kMaxSlots, kEmptySlot), |
| 67 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 70 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 68 weak_factory_(this) { | 71 weak_factory_(this) { |
| 69 DCHECK(arc_bridge_service->state() == ArcBridgeService::State::STOPPED); | |
| 70 arc_bridge_service->AddObserver(this); | 72 arc_bridge_service->AddObserver(this); |
| 73 if (arc_bridge_service->input_instance()) |
| 74 OnInputInstanceReady(); |
| 71 | 75 |
| 72 aura::Env* env = aura::Env::GetInstanceDontCreate(); | 76 aura::Env* env = aura::Env::GetInstanceDontCreate(); |
| 73 if (env) | 77 if (env) |
| 74 env->AddObserver(this); | 78 env->AddObserver(this); |
| 75 } | 79 } |
| 76 | 80 |
| 77 ArcInputBridgeImpl::~ArcInputBridgeImpl() { | 81 ArcInputBridgeImpl::~ArcInputBridgeImpl() { |
| 78 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 82 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 79 arc_bridge_service_->RemoveObserver(this); | 83 arc_bridge_service_->RemoveObserver(this); |
| 80 | 84 |
| 81 aura::Env* env = aura::Env::GetInstanceDontCreate(); | 85 aura::Env* env = aura::Env::GetInstanceDontCreate(); |
| 82 if (env) | 86 if (env) |
| 83 env->RemoveObserver(this); | 87 env->RemoveObserver(this); |
| 84 | 88 |
| 85 for (aura::Window* window : arc_windows_.windows()) { | 89 for (aura::Window* window : arc_windows_.windows()) { |
| 86 window->RemovePreTargetHandler(this); | 90 window->RemovePreTargetHandler(this); |
| 87 } | 91 } |
| 88 } | 92 } |
| 89 | 93 |
| 90 void ArcInputBridgeImpl::OnInstanceBootPhase(InstanceBootPhase phase) { | 94 void ArcInputBridgeImpl::OnInputInstanceReady() { |
| 91 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 95 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 92 if (phase != INSTANCE_BOOT_PHASE_SYSTEM_SERVICES_READY) | |
| 93 return; | |
| 94 | 96 |
| 95 keyboard_fd_ = CreateBridgeInputDevice("ChromeOS Keyboard", "keyboard"); | 97 keyboard_fd_ = CreateBridgeInputDevice("ChromeOS Keyboard", "keyboard"); |
| 96 mouse_fd_ = CreateBridgeInputDevice("ChromeOS Mouse", "mouse"); | 98 mouse_fd_ = CreateBridgeInputDevice("ChromeOS Mouse", "mouse"); |
| 97 touchscreen_fd_ = | 99 touchscreen_fd_ = |
| 98 CreateBridgeInputDevice("ChromeOS Touchscreen", "touchscreen"); | 100 CreateBridgeInputDevice("ChromeOS Touchscreen", "touchscreen"); |
| 99 } | 101 } |
| 100 | 102 |
| 101 // Translates and sends a ui::Event to the appropriate bridge device of the | 103 // 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 | 104 // ARC instance. If the devices have not yet been initialized, the event |
| 103 // will be ignored. | 105 // will be ignored. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 119 new_window->AddPreTargetHandler(this); | 121 new_window->AddPreTargetHandler(this); |
| 120 } | 122 } |
| 121 } | 123 } |
| 122 | 124 |
| 123 void ArcInputBridgeImpl::SendKeyEvent(ui::KeyEvent* event) { | 125 void ArcInputBridgeImpl::SendKeyEvent(ui::KeyEvent* event) { |
| 124 if (keyboard_fd_.get() < 0) { | 126 if (keyboard_fd_.get() < 0) { |
| 125 VLOG(2) << "No keyboard bridge device available."; | 127 VLOG(2) << "No keyboard bridge device available."; |
| 126 return; | 128 return; |
| 127 } | 129 } |
| 128 | 130 |
| 129 unsigned short evdev_code = DomCodeToEvdevCode(event->code()); | 131 uint16_t evdev_code = DomCodeToEvdevCode(event->code()); |
| 130 int evdev_value = 0; | 132 int evdev_value = 0; |
| 131 if (event->type() == ui::ET_KEY_PRESSED) { | 133 if (event->type() == ui::ET_KEY_PRESSED) { |
| 132 if (event->flags() & ui::EF_IS_REPEAT) { | 134 if (event->flags() & ui::EF_IS_REPEAT) { |
| 133 evdev_value = kKeyRepeated; | 135 evdev_value = kKeyRepeated; |
| 134 } else { | 136 } else { |
| 135 evdev_value = kKeyPressed; | 137 evdev_value = kKeyPressed; |
| 136 } | 138 } |
| 137 } else if (event->type() == ui::ET_KEY_RELEASED) { | 139 } else if (event->type() == ui::ET_KEY_RELEASED) { |
| 138 evdev_value = kKeyReleased; | 140 evdev_value = kKeyReleased; |
| 139 } else { | 141 } else { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_HWHEEL, hwheel); | 243 SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_HWHEEL, hwheel); |
| 242 offset_x_acc_ -= static_cast<float>(hwheel); | 244 offset_x_acc_ -= static_cast<float>(hwheel); |
| 243 } | 245 } |
| 244 } | 246 } |
| 245 | 247 |
| 246 SendSynReport(mouse_fd_, time_stamp); | 248 SendSynReport(mouse_fd_, time_stamp); |
| 247 } | 249 } |
| 248 | 250 |
| 249 void ArcInputBridgeImpl::SendKernelEvent(const base::ScopedFD& fd, | 251 void ArcInputBridgeImpl::SendKernelEvent(const base::ScopedFD& fd, |
| 250 base::TimeDelta time_stamp, | 252 base::TimeDelta time_stamp, |
| 251 unsigned short type, | 253 uint16_t type, |
| 252 unsigned short code, | 254 uint16_t code, |
| 253 int value) { | 255 int value) { |
| 254 DCHECK(fd.is_valid()); | 256 DCHECK(fd.is_valid()); |
| 255 | 257 |
| 256 struct input_event event; | 258 struct input_event event; |
| 257 event.time.tv_sec = time_stamp.InSeconds(); | 259 event.time.tv_sec = time_stamp.InSeconds(); |
| 258 base::TimeDelta remainder = | 260 base::TimeDelta remainder = |
| 259 time_stamp - base::TimeDelta::FromSeconds(event.time.tv_sec); | 261 time_stamp - base::TimeDelta::FromSeconds(event.time.tv_sec); |
| 260 event.time.tv_usec = remainder.InMicroseconds(); | 262 event.time.tv_usec = remainder.InMicroseconds(); |
| 261 event.type = type; | 263 event.type = type; |
| 262 event.code = code; | 264 event.code = code; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 | 298 |
| 297 int ArcInputBridgeImpl::FindTouchSlot(int tracking_id) { | 299 int ArcInputBridgeImpl::FindTouchSlot(int tracking_id) { |
| 298 for (int i = 0; i < kMaxSlots; ++i) { | 300 for (int i = 0; i < kMaxSlots; ++i) { |
| 299 if (current_slot_tracking_ids_[i] == tracking_id) { | 301 if (current_slot_tracking_ids_[i] == tracking_id) { |
| 300 return i; | 302 return i; |
| 301 } | 303 } |
| 302 } | 304 } |
| 303 return -1; | 305 return -1; |
| 304 } | 306 } |
| 305 | 307 |
| 306 unsigned short ArcInputBridgeImpl::DomCodeToEvdevCode(ui::DomCode dom_code) { | 308 uint16_t ArcInputBridgeImpl::DomCodeToEvdevCode(ui::DomCode dom_code) { |
| 307 int native_code = ui::KeycodeConverter::DomCodeToNativeKeycode(dom_code); | 309 int native_code = ui::KeycodeConverter::DomCodeToNativeKeycode(dom_code); |
| 308 if (native_code == ui::KeycodeConverter::InvalidNativeKeycode()) | 310 if (native_code == ui::KeycodeConverter::InvalidNativeKeycode()) |
| 309 return KEY_RESERVED; | 311 return KEY_RESERVED; |
| 310 | 312 |
| 311 return native_code - kXkbKeycodeOffset; | 313 return native_code - kXkbKeycodeOffset; |
| 312 } | 314 } |
| 313 | 315 |
| 314 base::ScopedFD ArcInputBridgeImpl::CreateBridgeInputDevice( | 316 base::ScopedFD ArcInputBridgeImpl::CreateBridgeInputDevice( |
| 315 const std::string& name, | 317 const std::string& name, |
| 316 const std::string& device_type) { | 318 const std::string& device_type) { |
| 317 if (!arc_bridge_service_) { | 319 if (!arc_bridge_service_) { |
| 318 VLOG(1) << "ArcBridgeService disappeared."; | 320 VLOG(1) << "ArcBridgeService disappeared."; |
| 319 return base::ScopedFD(); | 321 return base::ScopedFD(); |
| 320 } | 322 } |
| 321 | 323 |
| 322 // Create file descriptor pair for communication | 324 // Create file descriptor pair for communication |
| 323 int fd[2]; | 325 int fd[2]; |
| 324 int res = HANDLE_EINTR(pipe(fd)); | 326 int res = HANDLE_EINTR(pipe(fd)); |
| 325 if (res < 0) { | 327 if (res < 0) { |
| 326 VPLOG(1) << "Cannot create pipe"; | 328 VPLOG(1) << "Cannot create pipe"; |
| 327 return base::ScopedFD(); | 329 return base::ScopedFD(); |
| 328 } | 330 } |
| 329 base::ScopedFD read_fd(fd[0]); | 331 base::ScopedFD read_fd(fd[0]); |
| 330 base::ScopedFD write_fd(fd[1]); | 332 base::ScopedFD write_fd(fd[1]); |
| 331 | 333 |
| 332 // The read end is sent to the instance, ownership of fd transfers. | 334 // The read end is sent to the instance, ownership of fd transfers. |
| 333 if (!arc_bridge_service_->RegisterInputDevice(name, device_type, | 335 InputInstance* input_instance = arc_bridge_service_->input_instance(); |
| 334 std::move(read_fd))) { | 336 if (!input_instance) { |
| 335 VLOG(1) << "Cannot create bridge input device"; | 337 VLOG(1) << "ArcBridgeService InputInstance disappeared."; |
| 336 return base::ScopedFD(); | 338 return base::ScopedFD(); |
| 337 } | 339 } |
| 340 MojoHandle wrapped_handle; |
| 341 MojoResult wrap_result = mojo::embedder::CreatePlatformHandleWrapper( |
| 342 mojo::embedder::ScopedPlatformHandle( |
| 343 mojo::embedder::PlatformHandle(read_fd.release())), |
| 344 &wrapped_handle); |
| 345 if (wrap_result != MOJO_RESULT_OK) { |
| 346 LOG(WARNING) << "Pipe failed to wrap handles. Closing: " << wrap_result; |
| 347 return base::ScopedFD(); |
| 348 } |
| 349 input_instance->RegisterInputDevice( |
| 350 name, device_type, mojo::ScopedHandle(mojo::Handle(wrapped_handle))); |
| 338 | 351 |
| 339 // setup write end as non blocking | 352 // setup write end as non blocking |
| 340 int flags = HANDLE_EINTR(fcntl(write_fd.get(), F_GETFL, 0)); | 353 int flags = HANDLE_EINTR(fcntl(write_fd.get(), F_GETFL, 0)); |
| 341 if (res < 0) { | 354 if (res < 0) { |
| 342 VPLOG(1) << "Cannot get file descriptor flags"; | 355 VPLOG(1) << "Cannot get file descriptor flags"; |
| 343 return base::ScopedFD(); | 356 return base::ScopedFD(); |
| 344 } | 357 } |
| 345 | 358 |
| 346 res = HANDLE_EINTR(fcntl(write_fd.get(), F_SETFL, flags | O_NONBLOCK)); | 359 res = HANDLE_EINTR(fcntl(write_fd.get(), F_SETFL, flags | O_NONBLOCK)); |
| 347 if (res < 0) { | 360 if (res < 0) { |
| 348 VPLOG(1) << "Cannot set file descriptor flags"; | 361 VPLOG(1) << "Cannot set file descriptor flags"; |
| 349 return base::ScopedFD(); | 362 return base::ScopedFD(); |
| 350 } | 363 } |
| 351 return write_fd; | 364 return write_fd; |
| 352 } | 365 } |
| 353 | 366 |
| 354 scoped_ptr<ArcInputBridge> ArcInputBridge::Create( | 367 scoped_ptr<ArcInputBridge> ArcInputBridge::Create( |
| 355 ArcBridgeService* arc_bridge_service) { | 368 ArcBridgeService* arc_bridge_service) { |
| 356 return make_scoped_ptr(new ArcInputBridgeImpl(arc_bridge_service)); | 369 return make_scoped_ptr(new ArcInputBridgeImpl(arc_bridge_service)); |
| 357 } | 370 } |
| 358 | 371 |
| 359 } // namespace arc | 372 } // namespace arc |
| OLD | NEW |