Chromium Code Reviews| 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.h" | |
| 6 | |
| 7 #include <linux/input.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/posix/eintr_wrapper.h" | |
| 11 #include "base/time/time.h" | |
| 12 #include "components/arc/arc_bridge_service.h" | |
| 13 #include "ui/events/event_utils.h" | |
| 14 #include "ui/events/keycodes/dom/keycode_converter.h" | |
| 15 #include "ui/events/ozone/evdev/keyboard_util_evdev.h" | |
| 16 | |
| 17 namespace { | |
| 18 // 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.
| |
| 19 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
| |
| 20 static const int kKeyPressed = 1; | |
| 21 static const int kKeyRepeated = 2; | |
| 22 | |
| 23 // maximum number of supported multi-touch slots (simultaneous fingers) | |
| 24 static const int kMaxSlots = 64; | |
| 25 | |
| 26 // tracking id of an empty slot | |
| 27 static const int kEmptySlot = -1; | |
| 28 | |
| 29 // maximum possible pressure as defined in EventHubARC. | |
| 30 // todo(denniskempin): communicate maximum during initialization. | |
| 31 static const int kMaxPressure = 65536; | |
| 32 | |
| 33 struct MouseButtonMapping { | |
| 34 int ui_flag; | |
| 35 int evdev_code; | |
| 36 }; | |
| 37 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.
| |
| 38 {ui::EF_LEFT_MOUSE_BUTTON, BTN_LEFT}, | |
| 39 {ui::EF_RIGHT_MOUSE_BUTTON, BTN_RIGHT}, | |
| 40 {ui::EF_MIDDLE_MOUSE_BUTTON, BTN_MIDDLE}, | |
| 41 }; | |
| 42 | |
| 43 // Weak pointer. This class is owned by ChromeBrowserMainPartsChromeos. | |
| 44 arc::ArcInputBridge* g_arc_input_bridge = nullptr; | |
| 45 } | |
|
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.
| |
| 46 | |
| 47 namespace arc { | |
| 48 | |
| 49 ArcInputBridge::ArcInputBridge( | |
| 50 base::WeakPtr<ArcBridgeService> arc_bridge_service) | |
| 51 : arc_bridge_service_(arc_bridge_service), | |
| 52 current_slot_(-1), | |
| 53 current_slot_tracking_ids_(kMaxSlots, kEmptySlot) { | |
| 54 if (arc_bridge_service) { | |
| 55 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
| |
| 56 arc_bridge_service->AddObserver(this); | |
| 57 } | |
| 58 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
| |
| 59 } | |
| 60 | |
| 61 ArcInputBridge::~ArcInputBridge() { | |
| 62 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.
| |
| 63 arc_bridge_service_->RemoveObserver(this); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 // static | |
| 68 ArcInputBridge* ArcInputBridge::Get() { | |
| 69 DCHECK(g_arc_input_bridge); | |
| 70 return g_arc_input_bridge; | |
| 71 } | |
| 72 | |
| 73 void ArcInputBridge::OnInstanceBootPhase(InstanceBootPhase phase) { | |
| 74 // todo(denniskempin): Investigate how to turn around the communication | |
| 75 // and initiate the creation of devices from the instance-side. This would | |
| 76 // reduce the amount of state-tracking necessary. | |
| 77 if (phase != InstanceBootPhase::SYSTEM_SERVICES_READY) { | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 keyboard_fd_ = CreateBridgeInputDevice("ChromeOS Keyboard", "keyboard"); | |
| 82 mouse_fd_ = CreateBridgeInputDevice("ChromeOS Mouse", "mouse"); | |
| 83 touchscreen_fd_ = | |
| 84 CreateBridgeInputDevice("ChromeOS Touchscreen", "touchscreen"); | |
| 85 } | |
| 86 | |
| 87 void ArcInputBridge::SendInputEvent(ui::Event* event) { | |
| 88 if (event->IsKeyEvent()) { | |
| 89 SendKeyEvent(static_cast<ui::KeyEvent*>(event)); | |
| 90 } else if (event->IsMouseEvent()) { | |
| 91 SendMouseEvent(static_cast<ui::MouseEvent*>(event)); | |
| 92 } else if (event->IsTouchEvent()) { | |
| 93 SendTouchEvent(static_cast<ui::TouchEvent*>(event)); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void ArcInputBridge::SendKeyEvent(ui::KeyEvent* event) { | |
| 98 if (keyboard_fd_.get() < 0) { | |
| 99 LOG(WARNING) << "No keyboard bridge device available."; | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 int native_code = ui::KeycodeConverter::DomCodeToNativeKeycode(event->code()); | |
| 104 | |
| 105 unsigned short evdev_code = ui::NativeCodeToEvdevCode(native_code); | |
| 106 int evdev_value; | |
| 107 if (event->type() == ui::ET_KEY_PRESSED) { | |
| 108 if (event->flags() & ui::EF_IS_REPEAT) { | |
| 109 evdev_value = kKeyRepeated; | |
| 110 } else { | |
| 111 evdev_value = kKeyPressed; | |
| 112 } | |
| 113 } else if (event->type() == ui::ET_KEY_RELEASED) { | |
| 114 evdev_value = kKeyReleased; | |
| 115 } | |
| 116 | |
| 117 base::TimeDelta time_stamp = event->time_stamp(); | |
| 118 SendKernelEvent(keyboard_fd_, time_stamp, EV_KEY, evdev_code, evdev_value); | |
| 119 SendSynReport(keyboard_fd_, time_stamp); | |
| 120 } | |
| 121 | |
| 122 void ArcInputBridge::SendTouchEvent(ui::TouchEvent* event) { | |
| 123 if (touchscreen_fd_.get() < 0) { | |
| 124 LOG(WARNING) << "No touchscreen bridge device available."; | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 ui::PointerDetails details = event->pointer_details(); | |
| 129 base::TimeDelta time_stamp = event->time_stamp(); | |
| 130 | |
| 131 // find or assing a slot for this tracking id | |
| 132 int slot_id = AcquireTouchSlot(event); | |
| 133 if (slot_id < 0) { | |
| 134 LOG(ERROR) << "Ran out of slot IDs."; | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 // we only need to send the slot ID when it has changed. | |
| 139 if (slot_id != current_slot_) { | |
| 140 current_slot_ = slot_id; | |
| 141 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_SLOT, | |
| 142 current_slot_); | |
| 143 } | |
| 144 | |
| 145 // update tracking id | |
| 146 if (event->type() == ui::ET_TOUCH_PRESSED) { | |
| 147 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID, | |
| 148 event->touch_id()); | |
| 149 } else if (event->type() == ui::ET_TOUCH_RELEASED) { | |
| 150 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TRACKING_ID, | |
| 151 kEmptySlot); | |
| 152 } | |
| 153 | |
| 154 // update touch information | |
| 155 if (event->type() == ui::ET_TOUCH_MOVED || | |
| 156 event->type() == ui::ET_TOUCH_PRESSED) { | |
| 157 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_X, | |
| 158 event->x()); | |
| 159 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_POSITION_Y, | |
| 160 event->y()); | |
| 161 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MAJOR, | |
| 162 details.radius_x()); | |
| 163 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_TOUCH_MINOR, | |
| 164 details.radius_y()); | |
| 165 SendKernelEvent(touchscreen_fd_, time_stamp, EV_ABS, ABS_MT_PRESSURE, | |
| 166 details.force() * kMaxPressure); | |
| 167 } | |
| 168 SendSynReport(touchscreen_fd_, time_stamp); | |
| 169 } | |
| 170 | |
| 171 void ArcInputBridge::SendMouseEvent(ui::MouseEvent* event) { | |
| 172 if (mouse_fd_.get() < 0) { | |
| 173 LOG(WARNING) << "No mouse bridge device available."; | |
| 174 return; | |
| 175 } | |
| 176 | |
| 177 base::TimeDelta time_stamp = event->time_stamp(); | |
| 178 | |
| 179 // update location | |
| 180 if (event->type() == ui::ET_MOUSE_MOVED) { | |
| 181 SendKernelEvent(mouse_fd_, time_stamp, EV_ABS, ABS_X, event->x()); | |
| 182 SendKernelEvent(mouse_fd_, time_stamp, EV_ABS, ABS_Y, event->y()); | |
| 183 } | |
| 184 | |
| 185 // update buttons | |
| 186 if (event->type() == ui::ET_MOUSE_PRESSED || | |
| 187 event->type() == ui::ET_MOUSE_RELEASED) { | |
| 188 int evdev_value = static_cast<int>(event->type() == ui::ET_MOUSE_PRESSED); | |
| 189 for (MouseButtonMapping mapping : kMouseButtonMap) { | |
| 190 if (event->changed_button_flags() & mapping.ui_flag) { | |
| 191 SendKernelEvent(mouse_fd_, time_stamp, EV_KEY, mapping.evdev_code, | |
| 192 evdev_value); | |
| 193 } | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 // update scroll wheel | |
| 198 if (event->IsMouseWheelEvent()) { | |
| 199 ui::MouseWheelEvent* wheel_event = static_cast<ui::MouseWheelEvent*>(event); | |
| 200 SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_WHEEL, | |
| 201 wheel_event->y_offset()); | |
| 202 SendKernelEvent(mouse_fd_, time_stamp, EV_REL, REL_HWHEEL, | |
| 203 wheel_event->x_offset()); | |
| 204 } | |
| 205 | |
| 206 SendSynReport(mouse_fd_, time_stamp); | |
| 207 } | |
| 208 | |
| 209 void ArcInputBridge::SendKernelEvent(const base::ScopedFD& fd, | |
| 210 base::TimeDelta time_stamp, | |
| 211 unsigned short type, | |
| 212 unsigned short code, | |
| 213 int value) { | |
| 214 if (fd.get() < 0) { | |
| 215 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.
| |
| 216 return; | |
| 217 } | |
| 218 // Chrome does not always use the monotonic system time for event times. | |
| 219 // For now always force the event time to the current system time. | |
| 220 // todo(denniskempin): To enable performance tracing of events we will want | |
| 221 // to use the kernel-provided monotonic time stamps of events. | |
| 222 time_stamp = ui::EventTimeForNow(); | |
| 223 | |
| 224 struct input_event event; | |
| 225 event.time.tv_sec = time_stamp.InSeconds(); | |
| 226 base::TimeDelta remainder = | |
| 227 time_stamp - base::TimeDelta::FromSeconds(event.time.tv_sec); | |
| 228 event.time.tv_usec = remainder.InMicroseconds(); | |
| 229 event.type = type; | |
| 230 event.code = code; | |
| 231 event.value = value; | |
| 232 | |
| 233 // Write event to file descriptor | |
| 234 int num_written = write(fd.get(), reinterpret_cast<void*>(&event), | |
| 235 sizeof(struct input_event)); | |
| 236 if (num_written != sizeof(struct input_event)) { | |
| 237 LOG(ERROR) << "Can't write to file descriptor"; | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 void ArcInputBridge::SendSynReport(const base::ScopedFD& fd, | |
| 242 base::TimeDelta time) { | |
| 243 SendKernelEvent(fd, time, EV_SYN, SYN_REPORT, 0); | |
| 244 } | |
| 245 | |
| 246 int ArcInputBridge::AcquireTouchSlot(ui::TouchEvent* event) { | |
| 247 int slot_id; | |
| 248 if (event->type() == ui::ET_TOUCH_PRESSED) { | |
| 249 slot_id = FindTouchSlot(kEmptySlot); | |
| 250 } else { | |
| 251 slot_id = FindTouchSlot(event->touch_id()); | |
| 252 } | |
| 253 if (slot_id < 0) { | |
| 254 return -1; | |
| 255 } | |
| 256 | |
| 257 if (event->type() == ui::ET_TOUCH_RELEASED) { | |
| 258 current_slot_tracking_ids_[slot_id] = kEmptySlot; | |
| 259 } else if (event->type() == ui::ET_TOUCH_PRESSED) { | |
| 260 current_slot_tracking_ids_[slot_id] = event->touch_id(); | |
| 261 } | |
| 262 return slot_id; | |
| 263 } | |
| 264 | |
| 265 int ArcInputBridge::FindTouchSlot(int tracking_id) { | |
| 266 for (int i = 0; i < kMaxSlots; ++i) { | |
| 267 if (current_slot_tracking_ids_[i] == tracking_id) { | |
| 268 return i; | |
| 269 } | |
| 270 } | |
| 271 return -1; | |
| 272 } | |
| 273 | |
| 274 base::ScopedFD ArcInputBridge::CreateBridgeInputDevice( | |
| 275 const std::string& name, | |
| 276 const std::string& device_type) { | |
| 277 if (!arc_bridge_service_) { | |
| 278 LOG(ERROR) << "ArcBridgeService disappeared."; | |
| 279 return base::ScopedFD(); | |
| 280 } | |
| 281 | |
| 282 // Create file descriptor pair for communication | |
| 283 int fd[2]; | |
| 284 int res = HANDLE_EINTR(pipe(fd)); | |
| 285 if (res < 0) { | |
| 286 PLOG(ERROR) << "Cannot create pipe"; | |
| 287 } | |
| 288 | |
| 289 // The read end is sent to the instance, ownership of fd transfers. | |
| 290 if (!arc_bridge_service_->RegisterInputDevice(name, device_type, | |
| 291 base::ScopedFD(fd[0]))) { | |
| 292 close(fd[1]); // close write end. | |
| 293 LOG(ERROR) << "Cannot create bridge input device"; | |
| 294 return base::ScopedFD(); | |
| 295 } | |
| 296 | |
| 297 // return the write end | |
| 298 return base::ScopedFD(fd[1]).Pass(); | |
| 299 } | |
| 300 | |
| 301 } // namespace arc | |
| OLD | NEW |