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..e9b70467f4acbd5210d80c4e9274566597a93bac |
| --- /dev/null |
| +++ b/components/arc/input/arc_input_bridge.cc |
| @@ -0,0 +1,301 @@ |
| +// 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 "base/logging.h" |
| +#include "base/posix/eintr_wrapper.h" |
| +#include "base/time/time.h" |
| +#include "components/arc/arc_bridge_service.h" |
| +#include "ui/events/event_utils.h" |
| +#include "ui/events/keycodes/dom/keycode_converter.h" |
| +#include "ui/events/ozone/evdev/keyboard_util_evdev.h" |
| + |
| +namespace { |
| +// input_event values for keyboard events |
|
jochen (gone - plz use gerrit)
2015/12/02 13:09:02
empty line before this one
denniskempin
2015/12/03 01:26:41
Done.
|
| +static const int kKeyReleased = 0; |
|
jochen (gone - plz use gerrit)
2015/12/02 13:09:02
no static required
should those be an enum?
denniskempin
2015/12/03 01:26:41
removed static. I would argue against an enum sinc
|
| +static const int kKeyPressed = 1; |
| +static const int kKeyRepeated = 2; |
| + |
| +// maximum number of supported multi-touch slots (simultaneous fingers) |
| +static const int kMaxSlots = 64; |
| + |
| +// tracking id of an empty slot |
| +static const int kEmptySlot = -1; |
| + |
| +// maximum possible pressure as defined in EventHubARC. |
| +// todo(denniskempin): communicate maximum during initialization. |
| +static const int kMaxPressure = 65536; |
| + |
| +struct MouseButtonMapping { |
| + int ui_flag; |
| + int evdev_code; |
| +}; |
| +MouseButtonMapping kMouseButtonMap[] = { |
|
jochen (gone - plz use gerrit)
2015/12/02 13:09:01
empty line before this one
denniskempin
2015/12/03 01:26:42
Done.
|
| + {ui::EF_LEFT_MOUSE_BUTTON, BTN_LEFT}, |
| + {ui::EF_RIGHT_MOUSE_BUTTON, BTN_RIGHT}, |
| + {ui::EF_MIDDLE_MOUSE_BUTTON, BTN_MIDDLE}, |
| +}; |
| + |
| +// Weak pointer. This class is owned by ChromeBrowserMainPartsChromeos. |
| +arc::ArcInputBridge* g_arc_input_bridge = nullptr; |
| +} |
|
jochen (gone - plz use gerrit)
2015/12/02 13:09:01
empty line before this one, also } // namespace
denniskempin
2015/12/03 01:26:42
Done.
|
| + |
| +namespace arc { |
| + |
| +ArcInputBridge::ArcInputBridge( |
| + base::WeakPtr<ArcBridgeService> arc_bridge_service) |
| + : arc_bridge_service_(arc_bridge_service), |
| + current_slot_(-1), |
| + current_slot_tracking_ids_(kMaxSlots, kEmptySlot) { |
| + if (arc_bridge_service) { |
| + DCHECK(arc_bridge_service->state() == ArcBridgeService::State::STOPPED); |
|
jochen (gone - plz use gerrit)
2015/12/02 13:09:01
DCHECK_EQ
denniskempin
2015/12/03 01:26:42
Unfortunately DCHECK_EQ can't compare enums. Well
|
| + arc_bridge_service->AddObserver(this); |
| + } |
| + g_arc_input_bridge = this; |
|
jochen (gone - plz use gerrit)
2015/12/02 13:09:02
using observers and weak ptrs implies some weak co
denniskempin
2015/12/03 01:26:42
I agree. I would like the bridge service to own th
|
| +} |
| + |
| +ArcInputBridge::~ArcInputBridge() { |
| + if (arc_bridge_service_) { |
|
jochen (gone - plz use gerrit)
2015/12/02 13:09:01
no { } required for single line body
denniskempin
2015/12/03 01:26:41
Done. In other parts of the code as well.
|
| + arc_bridge_service_->RemoveObserver(this); |
| + } |
| +} |
| + |
| +// static |
| +ArcInputBridge* ArcInputBridge::Get() { |
| + DCHECK(g_arc_input_bridge); |
| + return g_arc_input_bridge; |
| +} |
| + |
| +void ArcInputBridge::OnInstanceBootPhase(InstanceBootPhase phase) { |
| + // todo(denniskempin): Investigate how to turn around the communication |
| + // and initiate the creation of devices from the instance-side. This would |
| + // reduce the amount of state-tracking necessary. |
| + 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"); |
| +} |
| + |
| +void ArcInputBridge::SendInputEvent(ui::Event* event) { |
| + if (event->IsKeyEvent()) { |
| + SendKeyEvent(static_cast<ui::KeyEvent*>(event)); |
| + } else if (event->IsMouseEvent()) { |
| + SendMouseEvent(static_cast<ui::MouseEvent*>(event)); |
| + } else if (event->IsTouchEvent()) { |
| + SendTouchEvent(static_cast<ui::TouchEvent*>(event)); |
| + } |
| +} |
| + |
| +void ArcInputBridge::SendKeyEvent(ui::KeyEvent* event) { |
| + if (keyboard_fd_.get() < 0) { |
| + LOG(WARNING) << "No keyboard bridge device available."; |
| + 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(); |
| + SendKernelEvent(keyboard_fd_, time_stamp, EV_KEY, evdev_code, evdev_value); |
| + SendSynReport(keyboard_fd_, time_stamp); |
| +} |
| + |
| +void ArcInputBridge::SendTouchEvent(ui::TouchEvent* event) { |
| + 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 = AcquireTouchSlot(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; |
| + SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_SLOT, |
| + current_slot_); |
| + } |
| + |
| + // update tracking id |
| + if (event->type() == ui::ET_TOUCH_PRESSED) { |
| + SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID, |
| + event->touch_id()); |
| + } else if (event->type() == ui::ET_TOUCH_RELEASED) { |
| + SendKernelEvent(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) { |
| + SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_X, |
| + event->x()); |
| + SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_Y, |
| + event->y()); |
| + SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MAJOR, |
| + details.radius_x()); |
| + SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MINOR, |
| + details.radius_y()); |
| + SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_PRESSURE, |
| + details.force() * kMaxPressure); |
| + } |
| + SendSynReport(touchscreen_fd_, time_stamp); |
| +} |
| + |
| +void ArcInputBridge::SendMouseEvent(ui::MouseEvent* event) { |
| + if (mouse_fd_.get() < 0) { |
| + LOG(WARNING) << "No mouse bridge device available."; |
| + return; |
| + } |
| + |
| + base::TimeDelta time_stamp = event->time_stamp(); |
| + |
| + // update location |
| + if (event->type() == ui::ET_MOUSE_MOVED) { |
| + SendKernelEvent(mouse_fd_, time_stamp, EV_ABS, ABS_X, event->x()); |
| + SendKernelEvent(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) { |
| + SendKernelEvent(mouse_fd_, time_stamp, EV_KEY, mapping.evdev_code, |
| + evdev_value); |
| + } |
| + } |
| + } |
| + |
| + // update scroll wheel |
| + if (event->IsMouseWheelEvent()) { |
| + ui::MouseWheelEvent* wheel_event = static_cast<ui::MouseWheelEvent*>(event); |
| + SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_WHEEL, |
| + wheel_event->y_offset()); |
| + SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_HWHEEL, |
| + wheel_event->x_offset()); |
| + } |
| + |
| + SendSynReport(mouse_fd_, time_stamp); |
| +} |
| + |
| +void ArcInputBridge::SendKernelEvent(const base::ScopedFD& fd, |
| + base::TimeDelta time_stamp, |
| + unsigned short type, |
| + unsigned short code, |
| + int value) { |
| + if (fd.get() < 0) { |
| + LOG(ERROR) << "Invalid file descriptor"; |
|
jochen (gone - plz use gerrit)
2015/12/02 13:09:01
please use LOG or VLOG.
Can this happen it all? S
denniskempin
2015/12/03 01:26:41
Done.
|
| + return; |
| + } |
| + // 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 |
| + // 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)) { |
| + LOG(ERROR) << "Can't write to file descriptor"; |
| + } |
| +} |
| + |
| +void ArcInputBridge::SendSynReport(const base::ScopedFD& fd, |
| + base::TimeDelta time) { |
| + SendKernelEvent(fd, time, EV_SYN, SYN_REPORT, 0); |
| +} |
| + |
| +int ArcInputBridge::AcquireTouchSlot(ui::TouchEvent* event) { |
| + int slot_id; |
| + if (event->type() == ui::ET_TOUCH_PRESSED) { |
| + slot_id = FindTouchSlot(kEmptySlot); |
| + } else { |
| + slot_id = FindTouchSlot(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::FindTouchSlot(int tracking_id) { |
| + 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) { |
| + 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 |