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