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