Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: components/arc/input/arc_input_bridge.cc

Issue 1596663002: arc-bridge: Introduce the ArcService class (Closed) Base URL: https://chromium.googlesource.com/a/chromium/src.git@master
Patch Set: Rebased + protected ArcService direct instantiation Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/arc/input/arc_input_bridge.h ('k') | components/arc/input/arc_input_bridge_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.h"
6 6
7 #include <linux/input.h> 7 #include <linux/input.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 10
11 #include <string> 11 #include <string>
12 12
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/posix/eintr_wrapper.h" 14 #include "base/posix/eintr_wrapper.h"
15 #include "base/thread_task_runner_handle.h" 15 #include "base/thread_task_runner_handle.h"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 {ui::EF_MIDDLE_MOUSE_BUTTON, BTN_MIDDLE}, 69 {ui::EF_MIDDLE_MOUSE_BUTTON, BTN_MIDDLE},
70 }; 70 };
71 71
72 // Offset between evdev key codes and chrome native key codes 72 // Offset between evdev key codes and chrome native key codes
73 const int kXkbKeycodeOffset = 8; 73 const int kXkbKeycodeOffset = 8;
74 74
75 } // namespace 75 } // namespace
76 76
77 namespace arc { 77 namespace arc {
78 78
79 ArcInputBridgeImpl::ArcInputBridgeImpl(ArcBridgeService* arc_bridge_service) 79 ArcInputBridge::ArcInputBridge(ArcBridgeService* bridge_service)
80 : arc_bridge_service_(arc_bridge_service), 80 : ArcService(bridge_service),
81 offset_x_acc_(0.5f), 81 offset_x_acc_(0.5f),
82 offset_y_acc_(0.5f), 82 offset_y_acc_(0.5f),
83 current_slot_(-1), 83 current_slot_(-1),
84 current_slot_tracking_ids_(kMaxSlots, kEmptySlot), 84 current_slot_tracking_ids_(kMaxSlots, kEmptySlot),
85 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()), 85 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()),
86 weak_factory_(this) { 86 weak_factory_(this) {
87 arc_bridge_service->AddObserver(this); 87 arc_bridge_service()->AddObserver(this);
88 if (arc_bridge_service->input_instance())
89 OnInputInstanceReady();
90 88
91 aura::Env* env = aura::Env::GetInstanceDontCreate(); 89 aura::Env* env = aura::Env::GetInstanceDontCreate();
92 if (env) 90 if (env)
93 env->AddObserver(this); 91 env->AddObserver(this);
94 } 92 }
95 93
96 ArcInputBridgeImpl::~ArcInputBridgeImpl() { 94 ArcInputBridge::~ArcInputBridge() {
97 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); 95 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
98 arc_bridge_service_->RemoveObserver(this); 96 arc_bridge_service()->RemoveObserver(this);
99 97
100 aura::Env* env = aura::Env::GetInstanceDontCreate(); 98 aura::Env* env = aura::Env::GetInstanceDontCreate();
101 if (env) 99 if (env)
102 env->RemoveObserver(this); 100 env->RemoveObserver(this);
103 101
104 for (aura::Window* window : arc_windows_.windows()) { 102 for (aura::Window* window : arc_windows_.windows()) {
105 window->RemovePreTargetHandler(this); 103 window->RemovePreTargetHandler(this);
106 } 104 }
107 } 105 }
108 106
109 void ArcInputBridgeImpl::OnInputInstanceReady() { 107 void ArcInputBridge::OnInputInstanceReady() {
110 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); 108 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
111 109
112 keyboard_fd_ = CreateBridgeInputDevice("ChromeOS Keyboard", "keyboard"); 110 keyboard_fd_ = CreateBridgeInputDevice("ChromeOS Keyboard", "keyboard");
113 mouse_fd_ = CreateBridgeInputDevice("ChromeOS Mouse", "mouse"); 111 mouse_fd_ = CreateBridgeInputDevice("ChromeOS Mouse", "mouse");
114 touchscreen_fd_ = 112 touchscreen_fd_ =
115 CreateBridgeInputDevice("ChromeOS Touchscreen", "touchscreen"); 113 CreateBridgeInputDevice("ChromeOS Touchscreen", "touchscreen");
116 } 114 }
117 115
118 // Translates and sends a ui::Event to the appropriate bridge device of the 116 // Translates and sends a ui::Event to the appropriate bridge device of the
119 // ARC instance. If the devices have not yet been initialized, the event 117 // ARC instance. If the devices have not yet been initialized, the event
120 // will be ignored. 118 // will be ignored.
121 void ArcInputBridgeImpl::OnEvent(ui::Event* event) { 119 void ArcInputBridge::OnEvent(ui::Event* event) {
122 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); 120 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
123 if (event->IsKeyEvent()) { 121 if (event->IsKeyEvent()) {
124 SendKeyEvent(static_cast<ui::KeyEvent*>(event)); 122 SendKeyEvent(static_cast<ui::KeyEvent*>(event));
125 } else if (event->IsMouseEvent() || event->IsScrollEvent()) { 123 } else if (event->IsMouseEvent() || event->IsScrollEvent()) {
126 SendMouseEvent(static_cast<ui::MouseEvent*>(event)); 124 SendMouseEvent(static_cast<ui::MouseEvent*>(event));
127 } else if (event->IsTouchEvent()) { 125 } else if (event->IsTouchEvent()) {
128 SendTouchEvent(static_cast<ui::TouchEvent*>(event)); 126 SendTouchEvent(static_cast<ui::TouchEvent*>(event));
129 } 127 }
130 } 128 }
131 129
132 // Attaches the input bridge to the window if it is marked as an ARC window. 130 // Attaches the input bridge to the window if it is marked as an ARC window.
133 void ArcInputBridgeImpl::OnWindowInitialized(aura::Window* new_window) { 131 void ArcInputBridge::OnWindowInitialized(aura::Window* new_window) {
134 if (new_window->name() == "ExoSurface") { 132 if (new_window->name() == "ExoSurface") {
135 arc_windows_.Add(new_window); 133 arc_windows_.Add(new_window);
136 new_window->AddPreTargetHandler(this); 134 new_window->AddPreTargetHandler(this);
137 } 135 }
138 } 136 }
139 137
140 void ArcInputBridgeImpl::SendKeyEvent(ui::KeyEvent* event) { 138 void ArcInputBridge::SendKeyEvent(ui::KeyEvent* event) {
141 if (keyboard_fd_.get() < 0) { 139 if (keyboard_fd_.get() < 0) {
142 VLOG(2) << "No keyboard bridge device available."; 140 VLOG(2) << "No keyboard bridge device available.";
143 return; 141 return;
144 } 142 }
145 143
146 uint16_t evdev_code = DomCodeToEvdevCode(event->code()); 144 uint16_t evdev_code = DomCodeToEvdevCode(event->code());
147 int evdev_value = 0; 145 int evdev_value = 0;
148 if (event->type() == ui::ET_KEY_PRESSED) { 146 if (event->type() == ui::ET_KEY_PRESSED) {
149 if (event->flags() & ui::EF_IS_REPEAT) { 147 if (event->flags() & ui::EF_IS_REPEAT) {
150 evdev_value = kKeyRepeated; 148 evdev_value = kKeyRepeated;
151 } else { 149 } else {
152 evdev_value = kKeyPressed; 150 evdev_value = kKeyPressed;
153 } 151 }
154 } else if (event->type() == ui::ET_KEY_RELEASED) { 152 } else if (event->type() == ui::ET_KEY_RELEASED) {
155 evdev_value = kKeyReleased; 153 evdev_value = kKeyReleased;
156 } else { 154 } else {
157 NOTREACHED() << "Key should be either PRESSED or RELEASED."; 155 NOTREACHED() << "Key should be either PRESSED or RELEASED.";
158 } 156 }
159 157
160 base::TimeDelta time_stamp = event->time_stamp(); 158 base::TimeDelta time_stamp = event->time_stamp();
161 SendKernelEvent(keyboard_fd_, time_stamp, EV_KEY, evdev_code, evdev_value); 159 SendKernelEvent(keyboard_fd_, time_stamp, EV_KEY, evdev_code, evdev_value);
162 SendSynReport(keyboard_fd_, time_stamp); 160 SendSynReport(keyboard_fd_, time_stamp);
163 } 161 }
164 162
165 void ArcInputBridgeImpl::SendTouchEvent(ui::TouchEvent* event) { 163 void ArcInputBridge::SendTouchEvent(ui::TouchEvent* event) {
166 if (touchscreen_fd_.get() < 0) { 164 if (touchscreen_fd_.get() < 0) {
167 VLOG(2) << "No touchscreen bridge device available."; 165 VLOG(2) << "No touchscreen bridge device available.";
168 return; 166 return;
169 } 167 }
170 168
171 ui::PointerDetails details = event->pointer_details(); 169 ui::PointerDetails details = event->pointer_details();
172 base::TimeDelta time_stamp = event->time_stamp(); 170 base::TimeDelta time_stamp = event->time_stamp();
173 171
174 // find or assing a slot for this tracking id 172 // find or assing a slot for this tracking id
175 int slot_id = AcquireTouchSlot(event); 173 int slot_id = AcquireTouchSlot(event);
(...skipping 28 matching lines...) Expand all
204 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MAJOR, 202 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MAJOR,
205 details.radius_x()); 203 details.radius_x());
206 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MINOR, 204 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MINOR,
207 details.radius_y()); 205 details.radius_y());
208 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_PRESSURE, 206 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_PRESSURE,
209 details.force() * kMaxPressure); 207 details.force() * kMaxPressure);
210 } 208 }
211 SendSynReport(touchscreen_fd_, time_stamp); 209 SendSynReport(touchscreen_fd_, time_stamp);
212 } 210 }
213 211
214 void ArcInputBridgeImpl::SendMouseEvent(ui::MouseEvent* event) { 212 void ArcInputBridge::SendMouseEvent(ui::MouseEvent* event) {
215 if (mouse_fd_.get() < 0) { 213 if (mouse_fd_.get() < 0) {
216 VLOG(2) << "No mouse bridge device available."; 214 VLOG(2) << "No mouse bridge device available.";
217 return; 215 return;
218 } 216 }
219 217
220 base::TimeDelta time_stamp = event->time_stamp(); 218 base::TimeDelta time_stamp = event->time_stamp();
221 219
222 // update location 220 // update location
223 if (event->type() == ui::ET_MOUSE_MOVED || 221 if (event->type() == ui::ET_MOUSE_MOVED ||
224 event->type() == ui::ET_MOUSE_DRAGGED) { 222 event->type() == ui::ET_MOUSE_DRAGGED) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 int hwheel = floor(offset_x_acc_); 254 int hwheel = floor(offset_x_acc_);
257 if (hwheel != 0) { 255 if (hwheel != 0) {
258 SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_HWHEEL, hwheel); 256 SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_HWHEEL, hwheel);
259 offset_x_acc_ -= static_cast<float>(hwheel); 257 offset_x_acc_ -= static_cast<float>(hwheel);
260 } 258 }
261 } 259 }
262 260
263 SendSynReport(mouse_fd_, time_stamp); 261 SendSynReport(mouse_fd_, time_stamp);
264 } 262 }
265 263
266 void ArcInputBridgeImpl::SendKernelEvent(const base::ScopedFD& fd, 264 void ArcInputBridge::SendKernelEvent(const base::ScopedFD& fd,
267 base::TimeDelta time_stamp, 265 base::TimeDelta time_stamp,
268 uint16_t type, 266 uint16_t type,
269 uint16_t code, 267 uint16_t code,
270 int value) { 268 int value) {
271 DCHECK(fd.is_valid()); 269 DCHECK(fd.is_valid());
272 270
273 struct input_event32 event; 271 struct input_event32 event;
274 event.time.tv_sec = time_stamp.InSeconds(); 272 event.time.tv_sec = time_stamp.InSeconds();
275 base::TimeDelta remainder = 273 base::TimeDelta remainder =
276 time_stamp - base::TimeDelta::FromSeconds(event.time.tv_sec); 274 time_stamp - base::TimeDelta::FromSeconds(event.time.tv_sec);
277 event.time.tv_usec = remainder.InMicroseconds(); 275 event.time.tv_usec = remainder.InMicroseconds();
278 event.type = type; 276 event.type = type;
279 event.code = code; 277 event.code = code;
280 event.value = value; 278 event.value = value;
281 279
282 // Write event to file descriptor 280 // Write event to file descriptor
283 size_t num_written = write(fd.get(), reinterpret_cast<void*>(&event), 281 size_t num_written = write(fd.get(), reinterpret_cast<void*>(&event),
284 sizeof(struct input_event32)); 282 sizeof(struct input_event32));
285 DCHECK_EQ(num_written, sizeof(struct input_event32)); 283 DCHECK_EQ(num_written, sizeof(struct input_event32));
286 } 284 }
287 285
288 void ArcInputBridgeImpl::SendSynReport(const base::ScopedFD& fd, 286 void ArcInputBridge::SendSynReport(const base::ScopedFD& fd,
289 base::TimeDelta time) { 287 base::TimeDelta time) {
290 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); 288 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
291 289
292 SendKernelEvent(fd, time, EV_SYN, SYN_REPORT, 0); 290 SendKernelEvent(fd, time, EV_SYN, SYN_REPORT, 0);
293 } 291 }
294 292
295 int ArcInputBridgeImpl::AcquireTouchSlot(ui::TouchEvent* event) { 293 int ArcInputBridge::AcquireTouchSlot(ui::TouchEvent* event) {
296 int slot_id; 294 int slot_id;
297 if (event->type() == ui::ET_TOUCH_PRESSED) { 295 if (event->type() == ui::ET_TOUCH_PRESSED) {
298 slot_id = FindTouchSlot(kEmptySlot); 296 slot_id = FindTouchSlot(kEmptySlot);
299 } else { 297 } else {
300 slot_id = FindTouchSlot(event->touch_id()); 298 slot_id = FindTouchSlot(event->touch_id());
301 } 299 }
302 if (slot_id < 0) { 300 if (slot_id < 0) {
303 return -1; 301 return -1;
304 } 302 }
305 303
306 if (event->type() == ui::ET_TOUCH_RELEASED) { 304 if (event->type() == ui::ET_TOUCH_RELEASED) {
307 current_slot_tracking_ids_[slot_id] = kEmptySlot; 305 current_slot_tracking_ids_[slot_id] = kEmptySlot;
308 } else if (event->type() == ui::ET_TOUCH_PRESSED) { 306 } else if (event->type() == ui::ET_TOUCH_PRESSED) {
309 current_slot_tracking_ids_[slot_id] = event->touch_id(); 307 current_slot_tracking_ids_[slot_id] = event->touch_id();
310 } 308 }
311 return slot_id; 309 return slot_id;
312 } 310 }
313 311
314 int ArcInputBridgeImpl::FindTouchSlot(int tracking_id) { 312 int ArcInputBridge::FindTouchSlot(int tracking_id) {
315 for (int i = 0; i < kMaxSlots; ++i) { 313 for (int i = 0; i < kMaxSlots; ++i) {
316 if (current_slot_tracking_ids_[i] == tracking_id) { 314 if (current_slot_tracking_ids_[i] == tracking_id) {
317 return i; 315 return i;
318 } 316 }
319 } 317 }
320 return -1; 318 return -1;
321 } 319 }
322 320
323 uint16_t ArcInputBridgeImpl::DomCodeToEvdevCode(ui::DomCode dom_code) { 321 uint16_t ArcInputBridge::DomCodeToEvdevCode(ui::DomCode dom_code) {
324 int native_code = ui::KeycodeConverter::DomCodeToNativeKeycode(dom_code); 322 int native_code = ui::KeycodeConverter::DomCodeToNativeKeycode(dom_code);
325 if (native_code == ui::KeycodeConverter::InvalidNativeKeycode()) 323 if (native_code == ui::KeycodeConverter::InvalidNativeKeycode())
326 return KEY_RESERVED; 324 return KEY_RESERVED;
327 325
328 return native_code - kXkbKeycodeOffset; 326 return native_code - kXkbKeycodeOffset;
329 } 327 }
330 328
331 base::ScopedFD ArcInputBridgeImpl::CreateBridgeInputDevice( 329 base::ScopedFD ArcInputBridge::CreateBridgeInputDevice(
332 const std::string& name, 330 const std::string& name,
333 const std::string& device_type) { 331 const std::string& device_type) {
334 if (!arc_bridge_service_) {
335 VLOG(1) << "ArcBridgeService disappeared.";
336 return base::ScopedFD();
337 }
338
339 // Create file descriptor pair for communication 332 // Create file descriptor pair for communication
340 int fd[2]; 333 int fd[2];
341 int res = HANDLE_EINTR(pipe(fd)); 334 int res = HANDLE_EINTR(pipe(fd));
342 if (res < 0) { 335 if (res < 0) {
343 VPLOG(1) << "Cannot create pipe"; 336 VPLOG(1) << "Cannot create pipe";
344 return base::ScopedFD(); 337 return base::ScopedFD();
345 } 338 }
346 base::ScopedFD read_fd(fd[0]); 339 base::ScopedFD read_fd(fd[0]);
347 base::ScopedFD write_fd(fd[1]); 340 base::ScopedFD write_fd(fd[1]);
348 341
349 // The read end is sent to the instance, ownership of fd transfers. 342 // The read end is sent to the instance, ownership of fd transfers.
350 InputInstance* input_instance = arc_bridge_service_->input_instance(); 343 InputInstance* input_instance = arc_bridge_service()->input_instance();
351 if (!input_instance) { 344 if (!input_instance) {
352 VLOG(1) << "ArcBridgeService InputInstance disappeared."; 345 VLOG(1) << "ArcBridgeService InputInstance disappeared.";
353 return base::ScopedFD(); 346 return base::ScopedFD();
354 } 347 }
355 MojoHandle wrapped_handle; 348 MojoHandle wrapped_handle;
356 MojoResult wrap_result = mojo::embedder::CreatePlatformHandleWrapper( 349 MojoResult wrap_result = mojo::embedder::CreatePlatformHandleWrapper(
357 mojo::embedder::ScopedPlatformHandle( 350 mojo::embedder::ScopedPlatformHandle(
358 mojo::embedder::PlatformHandle(read_fd.release())), 351 mojo::embedder::PlatformHandle(read_fd.release())),
359 &wrapped_handle); 352 &wrapped_handle);
360 if (wrap_result != MOJO_RESULT_OK) { 353 if (wrap_result != MOJO_RESULT_OK) {
(...skipping 11 matching lines...) Expand all
372 } 365 }
373 366
374 res = HANDLE_EINTR(fcntl(write_fd.get(), F_SETFL, flags | O_NONBLOCK)); 367 res = HANDLE_EINTR(fcntl(write_fd.get(), F_SETFL, flags | O_NONBLOCK));
375 if (res < 0) { 368 if (res < 0) {
376 VPLOG(1) << "Cannot set file descriptor flags"; 369 VPLOG(1) << "Cannot set file descriptor flags";
377 return base::ScopedFD(); 370 return base::ScopedFD();
378 } 371 }
379 return write_fd; 372 return write_fd;
380 } 373 }
381 374
382 scoped_ptr<ArcInputBridge> ArcInputBridge::Create(
383 ArcBridgeService* arc_bridge_service) {
384 return make_scoped_ptr(new ArcInputBridgeImpl(arc_bridge_service));
385 }
386
387 } // namespace arc 375 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/input/arc_input_bridge.h ('k') | components/arc/input/arc_input_bridge_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698