Chromium Code Reviews| Index: components/arc/input/arc_input_bridge.cc |
| diff --git a/components/arc/input/arc_input_bridge.cc b/components/arc/input/arc_input_bridge.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..49bb67d8d42d29f4737595d3be711b4faa0a329c |
| --- /dev/null |
| +++ b/components/arc/input/arc_input_bridge.cc |
| @@ -0,0 +1,350 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/arc/input/arc_input_bridge.h" |
| + |
| +#include <linux/input.h> |
| + |
| +#include "ash/shell.h" |
| +#include "base/logging.h" |
| +#include "base/posix/eintr_wrapper.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "base/time/time.h" |
| +#include "components/arc/arc_bridge_service.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/events/event_utils.h" |
| +#include "ui/events/keycodes/dom/keycode_converter.h" |
| +#include "ui/events/ozone/evdev/keyboard_util_evdev.h" |
| +#include "components/exo/shell_surface.h" |
| + |
| +namespace { |
| + |
| +// input_event values for keyboard events |
|
reveman
2015/12/04 03:21:24
nit: missing punctuation
denniskempin
2015/12/04 22:35:29
Done.
|
| +const int kKeyReleased = 0; |
| +const int kKeyPressed = 1; |
| +const int kKeyRepeated = 2; |
| + |
| +// maximum number of supported multi-touch slots (simultaneous fingers) |
|
reveman
2015/12/04 03:21:24
nit: missing punctuation
denniskempin
2015/12/04 22:35:29
Done.
|
| +const int kMaxSlots = 64; |
| + |
| +// tracking id of an empty slot |
|
reveman
2015/12/04 03:21:23
nit: missing punctuation
denniskempin
2015/12/04 22:35:29
Done.
|
| +const int kEmptySlot = -1; |
| + |
| +// maximum possible pressure as defined in EventHubARC. |
| +// todo(denniskempin): communicate maximum during initialization. |
|
reveman
2015/12/04 03:21:24
nit: s/todo/TODO
denniskempin
2015/12/04 22:35:29
Done.
|
| +const int kMaxPressure = 65536; |
| + |
| +// speed of the scroll emulation. Scroll events are reported at about 100 times |
| +// the speed of a scroll wheel. |
| +const float kScrollEmulationSpeed = 100.0f; |
| + |
| +struct MouseButtonMapping { |
| + int ui_flag; |
| + int evdev_code; |
| +}; |
| + |
| +MouseButtonMapping kMouseButtonMap[] = { |
|
reveman
2015/12/04 03:21:23
nit: we could save a few lines by:
struct MouseBu
denniskempin
2015/12/04 22:35:29
Done.
|
| + {ui::EF_LEFT_MOUSE_BUTTON, BTN_LEFT}, |
| + {ui::EF_RIGHT_MOUSE_BUTTON, BTN_RIGHT}, |
| + {ui::EF_MIDDLE_MOUSE_BUTTON, BTN_MIDDLE}, |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace arc { |
| + |
| +ArcInputBridge::ArcInputBridge( |
| + ArcBridgeService* arc_bridge_service, |
| + const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| + : arc_bridge_service_(arc_bridge_service), |
| + offset_x_acc(0.5f), |
| + offset_y_acc(0.5f), |
| + current_slot_(-1), |
| + current_slot_tracking_ids_(kMaxSlots, kEmptySlot), |
| + origin_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| + io_task_runner_(io_task_runner), |
|
reveman
2015/12/04 03:21:23
what happens if origin_task_runner_ == io_task_run
denniskempin
2015/12/04 22:35:29
no longer necessary.
|
| + weak_factory_(this) { |
| + DCHECK(arc_bridge_service->state() == ArcBridgeService::State::STOPPED); |
| + arc_bridge_service->AddObserver(this); |
| + |
| + origin_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&ArcInputBridge::InitializeAuraObserver, |
| + weak_factory_.GetWeakPtr())); |
|
reveman
2015/12/04 03:21:23
why do we need to post a task for this if we're al
denniskempin
2015/12/04 22:35:29
aura::Env is created after this class is. Unfortun
reveman
2015/12/04 23:19:31
hm, calling aura::Env::GetInstance() here and havi
|
| +} |
| + |
| +ArcInputBridge::~ArcInputBridge() { |
| + if (arc_bridge_service_) |
|
reveman
2015/12/04 03:21:23
I'm failing to see how arc_bridge_service_ can eve
denniskempin
2015/12/04 22:35:29
Yes, now that's guaranteed. done.
|
| + arc_bridge_service_->RemoveObserver(this); |
| +} |
| + |
| +void ArcInputBridge::InitializeAuraObserver() { |
| + aura::Env::GetInstance()->AddObserver(this); |
|
reveman
2015/12/04 03:21:24
where is the matching RemoveObserver call?
denniskempin
2015/12/04 22:35:29
I don't think we can. aura::Env is destroyed befor
|
| +} |
| + |
| +void ArcInputBridge::OnInstanceBootPhase(InstanceBootPhase phase) { |
| + DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| + if (phase != InstanceBootPhase::SYSTEM_SERVICES_READY) |
| + return; |
| + |
| + keyboard_fd_ = CreateBridgeInputDevice("ChromeOS Keyboard", "keyboard"); |
| + mouse_fd_ = CreateBridgeInputDevice("ChromeOS Mouse", "mouse"); |
| + touchscreen_fd_ = |
| + CreateBridgeInputDevice("ChromeOS Touchscreen", "touchscreen"); |
|
reveman
2015/12/04 03:21:23
these "*_fd_" members seem to also be used on the
denniskempin
2015/12/04 22:35:29
Done.
|
| +} |
| + |
| +void ArcInputBridge::OnEvent(ui::Event* event) { |
| + DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| + scoped_ptr<ui::Event> copy = ui::Event::Clone(*event); |
|
reveman
2015/12/04 03:21:23
Would it make sense to do more processing on the U
denniskempin
2015/12/04 22:35:30
Done.
|
| + io_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&ArcInputBridge::SendInputEventIO, |
| + weak_factory_.GetWeakPtr(), base::Passed(©))); |
|
reveman
2015/12/04 03:21:24
This doesn't work as weak pointers are not thread
denniskempin
2015/12/04 22:35:29
Done.
|
| +} |
| + |
| +void ArcInputBridge::OnWindowInitialized(aura::Window* new_window) { |
| + if (new_window->name() == "ExoSurface") |
| + new_window->AddPreTargetHandler(this); |
|
reveman
2015/12/04 03:21:24
nit: call RemovePreTargetHandler before the window
denniskempin
2015/12/04 22:35:29
As we talked over in hangouts. It's common practic
|
| +} |
| + |
| +void ArcInputBridge::SendInputEventIO(scoped_ptr<ui::Event> event) { |
| + DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + if (event->IsKeyEvent()) { |
| + SendKeyEventIO(static_cast<ui::KeyEvent*>(event.get())); |
| + } else if (event->IsMouseEvent() || event->IsScrollEvent()) { |
| + SendMouseEventIO(static_cast<ui::MouseEvent*>(event.get())); |
| + } else if (event->IsTouchEvent()) { |
| + SendTouchEventIO(static_cast<ui::TouchEvent*>(event.get())); |
| + } |
|
jochen (gone - plz use gerrit)
2015/12/04 14:18:12
else {
UNREACHABLE();
}?
denniskempin
2015/12/04 22:35:29
It is actually reachable if we are receiving an ui
|
| +} |
| + |
| +void ArcInputBridge::SendKeyEventIO(ui::KeyEvent* event) { |
| + DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + if (keyboard_fd_.get() < 0) { |
| + LOG(WARNING) << "No keyboard bridge device available."; |
|
jochen (gone - plz use gerrit)
2015/12/04 14:18:11
please use VLOG/DVLOG for logging.
note that logg
denniskempin
2015/12/04 22:35:29
Done. I searched the code for other occurrences an
|
| + return; |
| + } |
| + |
| + int native_code = ui::KeycodeConverter::DomCodeToNativeKeycode(event->code()); |
| + |
| + unsigned short evdev_code = ui::NativeCodeToEvdevCode(native_code); |
| + int evdev_value; |
| + if (event->type() == ui::ET_KEY_PRESSED) { |
| + if (event->flags() & ui::EF_IS_REPEAT) { |
| + evdev_value = kKeyRepeated; |
| + } else { |
| + evdev_value = kKeyPressed; |
| + } |
| + } else if (event->type() == ui::ET_KEY_RELEASED) { |
| + evdev_value = kKeyReleased; |
| + } |
| + |
| + base::TimeDelta time_stamp = event->time_stamp(); |
| + SendKernelEventIO(keyboard_fd_, time_stamp, EV_KEY, evdev_code, evdev_value); |
| + SendSynReportIO(keyboard_fd_, time_stamp); |
| +} |
| + |
| +void ArcInputBridge::SendTouchEventIO(ui::TouchEvent* event) { |
| + DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + if (touchscreen_fd_.get() < 0) { |
| + LOG(WARNING) << "No touchscreen bridge device available."; |
| + return; |
| + } |
| + |
| + ui::PointerDetails details = event->pointer_details(); |
| + base::TimeDelta time_stamp = event->time_stamp(); |
| + |
| + // find or assing a slot for this tracking id |
| + int slot_id = AcquireTouchSlotIO(event); |
| + if (slot_id < 0) { |
| + LOG(ERROR) << "Ran out of slot IDs."; |
| + return; |
| + } |
| + |
| + // we only need to send the slot ID when it has changed. |
| + if (slot_id != current_slot_) { |
| + current_slot_ = slot_id; |
| + SendKernelEventIO(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_SLOT, |
| + current_slot_); |
| + } |
| + |
| + // update tracking id |
| + if (event->type() == ui::ET_TOUCH_PRESSED) { |
| + SendKernelEventIO(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID, |
| + event->touch_id()); |
| + } else if (event->type() == ui::ET_TOUCH_RELEASED) { |
| + SendKernelEventIO(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID, |
| + kEmptySlot); |
| + } |
| + |
| + // update touch information |
| + if (event->type() == ui::ET_TOUCH_MOVED || |
| + event->type() == ui::ET_TOUCH_PRESSED) { |
| + SendKernelEventIO(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_X, |
| + event->x()); |
| + SendKernelEventIO(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_Y, |
| + event->y()); |
| + SendKernelEventIO(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MAJOR, |
| + details.radius_x()); |
| + SendKernelEventIO(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MINOR, |
| + details.radius_y()); |
| + SendKernelEventIO(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_PRESSURE, |
| + details.force() * kMaxPressure); |
| + } |
| + SendSynReportIO(touchscreen_fd_, time_stamp); |
| +} |
| + |
| +void ArcInputBridge::SendMouseEventIO(ui::MouseEvent* event) { |
| + DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + if (mouse_fd_.get() < 0) { |
| + LOG(WARNING) << "No mouse bridge device available."; |
|
jochen (gone - plz use gerrit)
2015/12/04 14:18:12
same here.
denniskempin
2015/12/04 22:35:29
Done.
|
| + return; |
| + } |
| + |
| + base::TimeDelta time_stamp = event->time_stamp(); |
| + |
| + // update location |
| + if (event->type() == ui::ET_MOUSE_MOVED) { |
| + SendKernelEventIO(mouse_fd_, time_stamp, EV_ABS, ABS_X, event->x()); |
| + SendKernelEventIO(mouse_fd_, time_stamp, EV_ABS, ABS_Y, event->y()); |
| + } |
| + |
| + // update buttons |
| + if (event->type() == ui::ET_MOUSE_PRESSED || |
| + event->type() == ui::ET_MOUSE_RELEASED) { |
| + int evdev_value = static_cast<int>(event->type() == ui::ET_MOUSE_PRESSED); |
| + for (MouseButtonMapping mapping : kMouseButtonMap) { |
| + if (event->changed_button_flags() & mapping.ui_flag) { |
| + SendKernelEventIO(mouse_fd_, time_stamp, EV_KEY, mapping.evdev_code, |
| + evdev_value); |
| + } |
| + } |
| + } |
| + |
| + // update scroll wheel |
| + if (event->type() == ui::ET_SCROLL) { |
| + ui::ScrollEvent* scroll_event = static_cast<ui::ScrollEvent*>(event); |
| + // accumulate floating point scroll offset since we can only send full |
| + // integer |
| + // wheel events. |
| + offset_x_acc += scroll_event->x_offset_ordinal() / kScrollEmulationSpeed; |
| + offset_y_acc += scroll_event->y_offset_ordinal() / kScrollEmulationSpeed; |
| + |
| + int wheel = floor(offset_y_acc); |
| + if (wheel != 0) { |
| + SendKernelEventIO(mouse_fd_, time_stamp, EV_REL, REL_WHEEL, wheel); |
| + offset_y_acc -= static_cast<float>(wheel); |
| + } |
| + |
| + int hwheel = floor(offset_x_acc); |
| + if (hwheel != 0) { |
| + SendKernelEventIO(mouse_fd_, time_stamp, EV_REL, REL_HWHEEL, hwheel); |
| + offset_x_acc -= static_cast<float>(hwheel); |
| + } |
| + } |
| + |
| + SendSynReportIO(mouse_fd_, time_stamp); |
| +} |
| + |
| +void ArcInputBridge::SendKernelEventIO(const base::ScopedFD& fd, |
| + base::TimeDelta time_stamp, |
| + unsigned short type, |
| + unsigned short code, |
| + int value) { |
| + DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| + DCHECK_GE(fd.get(), 0); |
| + |
| + // Chrome does not always use the monotonic system time for event times. |
| + // For now always force the event time to the current system time. |
| + // todo(denniskempin): To enable performance tracing of events we will want |
|
reveman
2015/12/04 03:21:23
nit: s/todo/TODO/
denniskempin
2015/12/04 22:35:29
Done.
|
| + // to use the kernel-provided monotonic time stamps of events. |
| + time_stamp = ui::EventTimeForNow(); |
| + |
| + struct input_event event; |
| + event.time.tv_sec = time_stamp.InSeconds(); |
| + base::TimeDelta remainder = |
| + time_stamp - base::TimeDelta::FromSeconds(event.time.tv_sec); |
| + event.time.tv_usec = remainder.InMicroseconds(); |
| + event.type = type; |
| + event.code = code; |
| + event.value = value; |
| + |
| + // Write event to file descriptor |
| + int num_written = write(fd.get(), reinterpret_cast<void*>(&event), |
| + sizeof(struct input_event)); |
| + if (num_written != sizeof(struct input_event)) |
|
reveman
2015/12/04 03:21:23
can this be a DCHECK?
denniskempin
2015/12/04 22:35:29
Done.
|
| + LOG(ERROR) << "Can't write to file descriptor"; |
| +} |
| + |
| +void ArcInputBridge::SendSynReportIO(const base::ScopedFD& fd, |
| + base::TimeDelta time) { |
| + DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
|
reveman
2015/12/04 03:21:24
origin_task_runner?
denniskempin
2015/12/04 22:35:30
Done.
|
| + |
| + SendKernelEventIO(fd, time, EV_SYN, SYN_REPORT, 0); |
| +} |
| + |
| +int ArcInputBridge::AcquireTouchSlotIO(ui::TouchEvent* event) { |
| + DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + int slot_id; |
| + if (event->type() == ui::ET_TOUCH_PRESSED) { |
| + slot_id = FindTouchSlotIO(kEmptySlot); |
| + } else { |
| + slot_id = FindTouchSlotIO(event->touch_id()); |
| + } |
| + if (slot_id < 0) { |
| + return -1; |
| + } |
| + |
| + if (event->type() == ui::ET_TOUCH_RELEASED) { |
| + current_slot_tracking_ids_[slot_id] = kEmptySlot; |
| + } else if (event->type() == ui::ET_TOUCH_PRESSED) { |
| + current_slot_tracking_ids_[slot_id] = event->touch_id(); |
| + } |
| + return slot_id; |
| +} |
| + |
| +int ArcInputBridge::FindTouchSlotIO(int tracking_id) { |
| + DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + for (int i = 0; i < kMaxSlots; ++i) { |
| + if (current_slot_tracking_ids_[i] == tracking_id) { |
| + return i; |
| + } |
| + } |
| + return -1; |
| +} |
| + |
| +base::ScopedFD ArcInputBridge::CreateBridgeInputDevice( |
| + const std::string& name, |
| + const std::string& device_type) { |
| + DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + if (!arc_bridge_service_) { |
| + LOG(ERROR) << "ArcBridgeService disappeared."; |
| + return base::ScopedFD(); |
| + } |
| + |
| + // Create file descriptor pair for communication |
| + int fd[2]; |
| + int res = HANDLE_EINTR(pipe(fd)); |
| + if (res < 0) |
| + PLOG(ERROR) << "Cannot create pipe"; |
| + |
| + // The read end is sent to the instance, ownership of fd transfers. |
| + if (!arc_bridge_service_->RegisterInputDevice(name, device_type, |
| + base::ScopedFD(fd[0]))) { |
| + close(fd[1]); // close write end. |
| + LOG(ERROR) << "Cannot create bridge input device"; |
| + return base::ScopedFD(); |
| + } |
| + |
| + // return the write end |
| + return base::ScopedFD(fd[1]).Pass(); |
| +} |
| + |
| +} // namespace arc |