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

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

Issue 1408263006: chromeos: Add ArcInputBridge to components/arc (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@arcxx
Patch Set: removed CL dependency Created 5 years 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_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/arc/input/arc_input_bridge_impl.h"
6
7 #include <linux/input.h>
8 #include <fcntl.h>
9
10 #include "base/logging.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/time/time.h"
14 #include "components/arc/arc_bridge_service.h"
15 #include "ui/aura/env.h"
16 #include "ui/aura/env_observer.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_observer.h"
19 #include "ui/events/event.h"
20 #include "ui/events/event_handler.h"
21 #include "ui/events/event_utils.h"
22 #include "ui/events/keycodes/dom/keycode_converter.h"
23
24 namespace {
25
26 // input_event values for keyboard events.
27 const int kKeyReleased = 0;
28 const int kKeyPressed = 1;
29 const int kKeyRepeated = 2;
30
31 // maximum number of supported multi-touch slots (simultaneous fingers).
32 const int kMaxSlots = 64;
33
34 // tracking id of an empty slot.
35 const int kEmptySlot = -1;
36
37 // maximum possible pressure as defined in EventHubARC.
38 // TODO(denniskempin): communicate maximum during initialization.
39 const int kMaxPressure = 65536;
40
41 // speed of the scroll emulation. Scroll events are reported at about 100 times
42 // the speed of a scroll wheel.
43 const float kScrollEmulationSpeed = 100.0f;
44
45 struct MouseButtonMapping {
46 int ui_flag;
47 int evdev_code;
48 } kMouseButtonMap[] = {
49 {ui::EF_LEFT_MOUSE_BUTTON, BTN_LEFT},
50 {ui::EF_RIGHT_MOUSE_BUTTON, BTN_RIGHT},
51 {ui::EF_MIDDLE_MOUSE_BUTTON, BTN_MIDDLE},
52 };
53
54 // Offset between evdev key codes and chrome native key codes
55 const int kXkbKeycodeOffset = 8;
56
57 } // namespace
58
59 namespace arc {
60
61 ArcInputBridgeImpl::ArcInputBridgeImpl(ArcBridgeService* arc_bridge_service)
62 : arc_bridge_service_(arc_bridge_service),
63 offset_x_acc_(0.5f),
64 offset_y_acc_(0.5f),
65 current_slot_(-1),
66 current_slot_tracking_ids_(kMaxSlots, kEmptySlot),
67 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()),
68 weak_factory_(this) {
69 DCHECK(arc_bridge_service->state() == ArcBridgeService::State::STOPPED);
70 arc_bridge_service->AddObserver(this);
71
72 DCHECK(aura::Env::GetInstance());
73 aura::Env::GetInstance()->AddObserver(this);
74 }
75
76 ArcInputBridgeImpl::~ArcInputBridgeImpl() {
77 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
78 arc_bridge_service_->RemoveObserver(this);
79 aura::Env::GetInstance()->RemoveObserver(this);
80
81 for (aura::Window* window : arc_windows_.windows()) {
82 window->RemovePreTargetHandler(this);
83 }
84 }
85
86 void ArcInputBridgeImpl::OnInstanceBootPhase(InstanceBootPhase phase) {
87 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
88 if (phase != INSTANCE_BOOT_PHASE_SYSTEM_SERVICES_READY)
89 return;
90
91 keyboard_fd_ = CreateBridgeInputDevice("ChromeOS Keyboard", "keyboard");
92 mouse_fd_ = CreateBridgeInputDevice("ChromeOS Mouse", "mouse");
93 touchscreen_fd_ =
94 CreateBridgeInputDevice("ChromeOS Touchscreen", "touchscreen");
95 }
96
97 // Translates and sends a ui::Event to the appropriate bridge device of the
98 // ARC instance. If the devices have not yet been initialized, the event
99 // will be ignored.
100 void ArcInputBridgeImpl::OnEvent(ui::Event* event) {
101 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
102 if (event->IsKeyEvent()) {
103 SendKeyEvent(static_cast<ui::KeyEvent*>(event));
104 } else if (event->IsMouseEvent() || event->IsScrollEvent()) {
105 SendMouseEvent(static_cast<ui::MouseEvent*>(event));
106 } else if (event->IsTouchEvent()) {
107 SendTouchEvent(static_cast<ui::TouchEvent*>(event));
108 }
109 }
110
111 // Attaches the input bridge to the window if it is marked as an ARC window.
112 void ArcInputBridgeImpl::OnWindowInitialized(aura::Window* new_window) {
113 if (new_window->name() == "ExoSurface") {
reveman 2015/12/08 17:02:08 Fyi, as an alternative to this you can now use exo
114 arc_windows_.Add(new_window);
115 new_window->AddPreTargetHandler(this);
116 }
117 }
118
119 void ArcInputBridgeImpl::SendKeyEvent(ui::KeyEvent* event) {
120 if (keyboard_fd_.get() < 0) {
121 VLOG(2) << "No keyboard bridge device available.";
122 return;
123 }
124
125 unsigned short evdev_code = DomCodeToEvdevCode(event->code());
126 int evdev_value;
127 if (event->type() == ui::ET_KEY_PRESSED) {
128 if (event->flags() & ui::EF_IS_REPEAT) {
129 evdev_value = kKeyRepeated;
130 } else {
131 evdev_value = kKeyPressed;
132 }
133 } else if (event->type() == ui::ET_KEY_RELEASED) {
134 evdev_value = kKeyReleased;
135 }
136
137 base::TimeDelta time_stamp = event->time_stamp();
138 SendKernelEvent(keyboard_fd_, time_stamp, EV_KEY, evdev_code, evdev_value);
139 SendSynReport(keyboard_fd_, time_stamp);
140 }
141
142 void ArcInputBridgeImpl::SendTouchEvent(ui::TouchEvent* event) {
143 if (touchscreen_fd_.get() < 0) {
144 VLOG(2) << "No touchscreen bridge device available.";
145 return;
146 }
147
148 ui::PointerDetails details = event->pointer_details();
149 base::TimeDelta time_stamp = event->time_stamp();
150
151 // find or assing a slot for this tracking id
152 int slot_id = AcquireTouchSlot(event);
153 if (slot_id < 0) {
154 VLOG(1) << "Ran out of slot IDs.";
155 return;
156 }
157
158 // we only need to send the slot ID when it has changed.
159 if (slot_id != current_slot_) {
160 current_slot_ = slot_id;
161 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_SLOT,
162 current_slot_);
163 }
164
165 // update tracking id
166 if (event->type() == ui::ET_TOUCH_PRESSED) {
167 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID,
168 event->touch_id());
169 } else if (event->type() == ui::ET_TOUCH_RELEASED) {
170 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID,
171 kEmptySlot);
172 }
173
174 // update touch information
175 if (event->type() == ui::ET_TOUCH_MOVED ||
176 event->type() == ui::ET_TOUCH_PRESSED) {
177 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_X,
178 event->x());
179 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_Y,
180 event->y());
181 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MAJOR,
182 details.radius_x());
183 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MINOR,
184 details.radius_y());
185 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_PRESSURE,
186 details.force() * kMaxPressure);
187 }
188 SendSynReport(touchscreen_fd_, time_stamp);
189 }
190
191 void ArcInputBridgeImpl::SendMouseEvent(ui::MouseEvent* event) {
192 if (mouse_fd_.get() < 0) {
193 VLOG(2) << "No mouse bridge device available.";
194 return;
195 }
196
197 base::TimeDelta time_stamp = event->time_stamp();
198
199 // update location
200 if (event->type() == ui::ET_MOUSE_MOVED ||
201 event->type() == ui::ET_MOUSE_DRAGGED) {
202 SendKernelEvent(mouse_fd_, time_stamp, EV_ABS, ABS_X, event->x());
203 SendKernelEvent(mouse_fd_, time_stamp, EV_ABS, ABS_Y, event->y());
204 }
205
206 // update buttons
207 if (event->type() == ui::ET_MOUSE_PRESSED ||
208 event->type() == ui::ET_MOUSE_RELEASED) {
209 int evdev_value = static_cast<int>(event->type() == ui::ET_MOUSE_PRESSED);
210 for (MouseButtonMapping mapping : kMouseButtonMap) {
211 if (event->changed_button_flags() & mapping.ui_flag) {
212 SendKernelEvent(mouse_fd_, time_stamp, EV_KEY, mapping.evdev_code,
213 evdev_value);
214 }
215 }
216 }
217
218 // update scroll wheel
219 if (event->type() == ui::ET_SCROLL) {
220 ui::ScrollEvent* scroll_event = static_cast<ui::ScrollEvent*>(event);
221 // accumulate floating point scroll offset since we can only send full
222 // integer
223 // wheel events.
224 offset_x_acc_ += scroll_event->x_offset_ordinal() / kScrollEmulationSpeed;
225 offset_y_acc_ += scroll_event->y_offset_ordinal() / kScrollEmulationSpeed;
226
227 int wheel = floor(offset_y_acc_);
228 if (wheel != 0) {
229 SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_WHEEL, wheel);
230 offset_y_acc_ -= static_cast<float>(wheel);
231 }
232
233 int hwheel = floor(offset_x_acc_);
234 if (hwheel != 0) {
235 SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_HWHEEL, hwheel);
236 offset_x_acc_ -= static_cast<float>(hwheel);
237 }
238 }
239
240 SendSynReport(mouse_fd_, time_stamp);
241 }
242
243 void ArcInputBridgeImpl::SendKernelEvent(const base::ScopedFD& fd,
244 base::TimeDelta time_stamp,
245 unsigned short type,
246 unsigned short code,
247 int value) {
248 DCHECK(fd.is_valid());
249
250 // Chrome does not always use the monotonic system time for event times.
251 // For now always force the event time to the current system time.
252 // TODO(denniskempin): To enable performance tracing of events we will want
253 // to use the kernel-provided monotonic time stamps of events.
254 time_stamp = ui::EventTimeForNow();
255
256 struct input_event event;
257 event.time.tv_sec = time_stamp.InSeconds();
258 base::TimeDelta remainder =
259 time_stamp - base::TimeDelta::FromSeconds(event.time.tv_sec);
260 event.time.tv_usec = remainder.InMicroseconds();
261 event.type = type;
262 event.code = code;
263 event.value = value;
264
265 // Write event to file descriptor
266 size_t num_written = write(fd.get(), reinterpret_cast<void*>(&event),
267 sizeof(struct input_event));
268 DCHECK_EQ(num_written, sizeof(struct input_event));
269 }
270
271 void ArcInputBridgeImpl::SendSynReport(const base::ScopedFD& fd,
272 base::TimeDelta time) {
273 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread());
274
275 SendKernelEvent(fd, time, EV_SYN, SYN_REPORT, 0);
276 }
277
278 int ArcInputBridgeImpl::AcquireTouchSlot(ui::TouchEvent* event) {
279 int slot_id;
280 if (event->type() == ui::ET_TOUCH_PRESSED) {
281 slot_id = FindTouchSlot(kEmptySlot);
282 } else {
283 slot_id = FindTouchSlot(event->touch_id());
284 }
285 if (slot_id < 0) {
286 return -1;
287 }
288
289 if (event->type() == ui::ET_TOUCH_RELEASED) {
290 current_slot_tracking_ids_[slot_id] = kEmptySlot;
291 } else if (event->type() == ui::ET_TOUCH_PRESSED) {
292 current_slot_tracking_ids_[slot_id] = event->touch_id();
293 }
294 return slot_id;
295 }
296
297 int ArcInputBridgeImpl::FindTouchSlot(int tracking_id) {
298 for (int i = 0; i < kMaxSlots; ++i) {
299 if (current_slot_tracking_ids_[i] == tracking_id) {
300 return i;
301 }
302 }
303 return -1;
304 }
305
306 unsigned short ArcInputBridgeImpl::DomCodeToEvdevCode(ui::DomCode dom_code) {
307 int native_code = ui::KeycodeConverter::DomCodeToNativeKeycode(dom_code);
308 if (native_code == ui::KeycodeConverter::InvalidNativeKeycode())
309 return KEY_RESERVED;
310
311 return native_code - kXkbKeycodeOffset;
312 }
313
314 base::ScopedFD ArcInputBridgeImpl::CreateBridgeInputDevice(
315 const std::string& name,
316 const std::string& device_type) {
317 if (!arc_bridge_service_) {
318 VLOG(1) << "ArcBridgeService disappeared.";
319 return base::ScopedFD();
320 }
321
322 // Create file descriptor pair for communication
323 int fd[2];
324 int res = HANDLE_EINTR(pipe(fd));
325 if (res < 0) {
326 VPLOG(1) << "Cannot create pipe";
327 return base::ScopedFD();
328 }
329 base::ScopedFD read_fd(fd[0]);
330 base::ScopedFD write_fd(fd[1]);
331
332 // The read end is sent to the instance, ownership of fd transfers.
333 if (!arc_bridge_service_->RegisterInputDevice(name, device_type,
334 std::move(read_fd))) {
335 VLOG(1) << "Cannot create bridge input device";
336 return base::ScopedFD();
337 }
338
339 // setup write end as non blocking
340 int flags = HANDLE_EINTR(fcntl(write_fd.get(), F_GETFL, 0));
341 if (res < 0) {
342 VPLOG(1) << "Cannot get file descriptor flags";
343 return base::ScopedFD();
344 }
345
346 res = HANDLE_EINTR(fcntl(write_fd.get(), F_SETFL, flags | O_NONBLOCK));
347 if (res < 0) {
348 VPLOG(1) << "Cannot set file descriptor flags";
349 return base::ScopedFD();
350 }
351 return std::move(write_fd);
352 }
353
354 scoped_ptr<ArcInputBridge> ArcInputBridge::Create(
355 ArcBridgeService* arc_bridge_service) {
356 return make_scoped_ptr(new ArcInputBridgeImpl(arc_bridge_service));
357 }
358
359 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/input/arc_input_bridge_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698