OLD | NEW |
---|---|
(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 "ash/shell.h" | |
reveman
2015/12/04 23:19:32
do you need this?
denniskempin
2015/12/05 00:05:19
not anymore. good catch. that'll get rid of some o
| |
11 #include "base/logging.h" | |
12 #include "base/posix/eintr_wrapper.h" | |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "base/time/time.h" | |
15 #include "components/arc/arc_bridge_service.h" | |
16 #include "components/exo/shell_surface.h" | |
reveman
2015/12/04 23:19:32
is this include needed?
denniskempin
2015/12/05 00:05:19
ditto
| |
17 #include "ui/aura/env.h" | |
18 #include "ui/aura/env_observer.h" | |
19 #include "ui/aura/window.h" | |
20 #include "ui/aura/window_observer.h" | |
21 #include "ui/events/event.h" | |
22 #include "ui/events/event_handler.h" | |
23 #include "ui/events/event_utils.h" | |
24 #include "ui/events/keycodes/dom/keycode_converter.h" | |
25 #include "ui/events/ozone/evdev/keyboard_util_evdev.h" | |
26 | |
27 namespace { | |
28 | |
29 // input_event values for keyboard events. | |
30 const int kKeyReleased = 0; | |
31 const int kKeyPressed = 1; | |
32 const int kKeyRepeated = 2; | |
33 | |
34 // maximum number of supported multi-touch slots (simultaneous fingers). | |
35 const int kMaxSlots = 64; | |
36 | |
37 // tracking id of an empty slot. | |
38 const int kEmptySlot = -1; | |
39 | |
40 // maximum possible pressure as defined in EventHubARC. | |
41 // TODO(denniskempin): communicate maximum during initialization. | |
42 const int kMaxPressure = 65536; | |
43 | |
44 // speed of the scroll emulation. Scroll events are reported at about 100 times | |
45 // the speed of a scroll wheel. | |
46 const float kScrollEmulationSpeed = 100.0f; | |
47 | |
48 struct MouseButtonMapping { | |
49 int ui_flag; | |
50 int evdev_code; | |
51 } kMouseButtonMap[] = { | |
52 {ui::EF_LEFT_MOUSE_BUTTON, BTN_LEFT}, | |
53 {ui::EF_RIGHT_MOUSE_BUTTON, BTN_RIGHT}, | |
54 {ui::EF_MIDDLE_MOUSE_BUTTON, BTN_MIDDLE}, | |
55 }; | |
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 origin_task_runner_->PostTask( | |
73 FROM_HERE, base::Bind(&ArcInputBridgeImpl::InitializeAuraObserver, | |
74 weak_factory_.GetWeakPtr())); | |
75 } | |
76 | |
77 ArcInputBridgeImpl::~ArcInputBridgeImpl() { | |
78 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | |
79 arc_bridge_service_->RemoveObserver(this); | |
80 } | |
81 | |
82 void ArcInputBridgeImpl::InitializeAuraObserver() { | |
83 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | |
84 aura::Env::GetInstance()->AddObserver(this); | |
85 } | |
86 | |
87 void ArcInputBridgeImpl::OnInstanceBootPhase(InstanceBootPhase phase) { | |
88 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | |
89 if (phase != InstanceBootPhase::SYSTEM_SERVICES_READY) | |
90 return; | |
91 | |
92 keyboard_fd_ = CreateBridgeInputDevice("ChromeOS Keyboard", "keyboard"); | |
93 mouse_fd_ = CreateBridgeInputDevice("ChromeOS Mouse", "mouse"); | |
94 touchscreen_fd_ = | |
95 CreateBridgeInputDevice("ChromeOS Touchscreen", "touchscreen"); | |
96 } | |
97 | |
98 // Translates and sends a ui::Event to the appropriate bridge device of the | |
99 // ARC instance. If the devices have not yet been initialized, the event | |
100 // will be ignored. | |
101 void ArcInputBridgeImpl::OnEvent(ui::Event* event) { | |
102 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | |
103 if (event->IsKeyEvent()) { | |
104 SendKeyEvent(static_cast<ui::KeyEvent*>(event)); | |
105 } else if (event->IsMouseEvent() || event->IsScrollEvent()) { | |
106 SendMouseEvent(static_cast<ui::MouseEvent*>(event)); | |
107 } else if (event->IsTouchEvent()) { | |
108 SendTouchEvent(static_cast<ui::TouchEvent*>(event)); | |
109 } | |
110 } | |
111 | |
112 // Attaches the input bridge to the window if it is marked as an ARC window. | |
113 void ArcInputBridgeImpl::OnWindowInitialized(aura::Window* new_window) { | |
114 if (new_window->name() == "ExoSurface") | |
115 new_window->AddPreTargetHandler(this); | |
116 } | |
117 | |
118 void ArcInputBridgeImpl::SendKeyEvent(ui::KeyEvent* event) { | |
119 if (keyboard_fd_.get() < 0) { | |
120 VLOG(2) << "No keyboard bridge device available."; | |
121 return; | |
122 } | |
123 | |
124 int native_code = ui::KeycodeConverter::DomCodeToNativeKeycode(event->code()); | |
125 | |
126 unsigned short evdev_code = ui::NativeCodeToEvdevCode(native_code); | |
127 int evdev_value; | |
128 if (event->type() == ui::ET_KEY_PRESSED) { | |
129 if (event->flags() & ui::EF_IS_REPEAT) { | |
130 evdev_value = kKeyRepeated; | |
131 } else { | |
132 evdev_value = kKeyPressed; | |
133 } | |
134 } else if (event->type() == ui::ET_KEY_RELEASED) { | |
135 evdev_value = kKeyReleased; | |
136 } | |
137 | |
138 base::TimeDelta time_stamp = event->time_stamp(); | |
139 SendKernelEvent(keyboard_fd_, time_stamp, EV_KEY, evdev_code, evdev_value); | |
140 SendSynReport(keyboard_fd_, time_stamp); | |
141 } | |
142 | |
143 void ArcInputBridgeImpl::SendTouchEvent(ui::TouchEvent* event) { | |
144 if (touchscreen_fd_.get() < 0) { | |
145 VLOG(2) << "No touchscreen bridge device available."; | |
146 return; | |
147 } | |
148 | |
149 ui::PointerDetails details = event->pointer_details(); | |
150 base::TimeDelta time_stamp = event->time_stamp(); | |
151 | |
152 // find or assing a slot for this tracking id | |
153 int slot_id = AcquireTouchSlot(event); | |
154 if (slot_id < 0) { | |
155 VLOG(1) << "Ran out of slot IDs."; | |
156 return; | |
157 } | |
158 | |
159 // we only need to send the slot ID when it has changed. | |
160 if (slot_id != current_slot_) { | |
161 current_slot_ = slot_id; | |
162 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_SLOT, | |
163 current_slot_); | |
164 } | |
165 | |
166 // update tracking id | |
167 if (event->type() == ui::ET_TOUCH_PRESSED) { | |
168 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID, | |
169 event->touch_id()); | |
170 } else if (event->type() == ui::ET_TOUCH_RELEASED) { | |
171 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID, | |
172 kEmptySlot); | |
173 } | |
174 | |
175 // update touch information | |
176 if (event->type() == ui::ET_TOUCH_MOVED || | |
177 event->type() == ui::ET_TOUCH_PRESSED) { | |
178 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_X, | |
179 event->x()); | |
180 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_Y, | |
181 event->y()); | |
182 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MAJOR, | |
183 details.radius_x()); | |
184 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MINOR, | |
185 details.radius_y()); | |
186 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_PRESSURE, | |
187 details.force() * kMaxPressure); | |
188 } | |
189 SendSynReport(touchscreen_fd_, time_stamp); | |
190 } | |
191 | |
192 void ArcInputBridgeImpl::SendMouseEvent(ui::MouseEvent* event) { | |
193 if (mouse_fd_.get() < 0) { | |
194 VLOG(2) << "No mouse bridge device available."; | |
195 return; | |
196 } | |
197 | |
198 base::TimeDelta time_stamp = event->time_stamp(); | |
199 | |
200 // update location | |
201 if (event->type() == ui::ET_MOUSE_MOVED) { | |
reveman
2015/12/04 23:19:32
MOUSE_DRAGGED too?
denniskempin
2015/12/05 00:05:19
Done.
| |
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_GE(fd.get(), 0); | |
Luis Héctor Chávez
2015/12/04 22:52:08
DCHECK(fd.is_valid())?
denniskempin
2015/12/05 00:05:19
Done.
| |
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 base::ScopedFD ArcInputBridgeImpl::CreateBridgeInputDevice( | |
307 const std::string& name, | |
308 const std::string& device_type) { | |
309 if (!arc_bridge_service_) { | |
310 VLOG(1) << "ArcBridgeService disappeared."; | |
311 return base::ScopedFD(); | |
312 } | |
313 | |
314 // Create file descriptor pair for communication | |
315 int fd[2]; | |
316 int res = HANDLE_EINTR(pipe(fd)); | |
317 if (res < 0) { | |
318 VPLOG(1) << "Cannot create pipe"; | |
319 return base::ScopedFD(); | |
320 } | |
321 | |
322 // The read end is sent to the instance, ownership of fd transfers. | |
323 if (!arc_bridge_service_->RegisterInputDevice(name, device_type, | |
324 base::ScopedFD(fd[0]))) { | |
325 close(fd[1]); // close write end. | |
Luis Héctor Chávez
2015/12/04 22:52:08
Maybe it's better to create the base::ScopedFD bef
denniskempin
2015/12/05 00:05:19
good idea. I would have missed some cases at the b
| |
326 VLOG(1) << "Cannot create bridge input device"; | |
327 return base::ScopedFD(); | |
328 } | |
329 | |
330 // setup write end as non blocking | |
331 int write_fd = fd[1]; | |
332 int flags = HANDLE_EINTR(fcntl(write_fd, F_GETFL, 0)); | |
333 if (res < 0) { | |
334 VPLOG(1) << "Cannot get file descriptor flags"; | |
335 return base::ScopedFD(); | |
336 } | |
337 | |
338 res = HANDLE_EINTR(fcntl(write_fd, F_SETFL, flags | O_NONBLOCK)); | |
339 if (res < 0) { | |
340 VPLOG(1) << "Cannot set file descriptor flags"; | |
341 return base::ScopedFD(); | |
342 } | |
343 return base::ScopedFD(write_fd).Pass(); | |
Luis Héctor Chávez
2015/12/04 22:52:07
We've moved to using std::move instead of Pass.
denniskempin
2015/12/05 00:05:19
Done.
| |
344 } | |
345 | |
346 scoped_ptr<ArcInputBridge> ArcInputBridge::Create( | |
347 ArcBridgeService* arc_bridge_service) { | |
348 return make_scoped_ptr(new ArcInputBridgeImpl(arc_bridge_service)); | |
349 } | |
350 | |
351 } // namespace arc | |
OLD | NEW |