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