Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include "components/arc/arc_bridge_input_devices.h" | |
| 2 | |
| 3 #include "base/logging.h" | |
| 4 #include "base/time/time.h" | |
| 5 #include "ui/events/keycodes/dom/keycode_converter.h" | |
| 6 #include "ui/events/ozone/evdev/keyboard_util_evdev.h" | |
| 7 | |
| 8 #include <linux/input.h> | |
| 9 | |
| 10 namespace arc { | |
| 11 | |
| 12 // BridgeInputDevice | |
| 13 | |
| 14 BridgeInputDevice::BridgeInputDevice(base::ScopedFD fd): fd_(fd.Pass()) {} | |
| 15 | |
| 16 void BridgeInputDevice::SendEvent(base::TimeDelta time, | |
|
elijahtaylor1
2015/11/02 22:30:29
time is highlighted in the code review tool, maybe
denniskempin
2015/11/16 18:39:28
Done.
| |
| 17 __u16 type, | |
|
elijahtaylor1
2015/11/02 22:30:29
nit: alignment?
denniskempin
2015/11/16 18:39:28
Done.
| |
| 18 __u16 code, | |
| 19 __s32 value) { | |
| 20 // Luckily Chrome on POSIX and the receiver both use monotonic time for | |
| 21 // input events, so we can just fill in the same timestamp. | |
|
elijahtaylor1
2015/11/02 22:30:29
on ARC, we had issues with wall time vs system tim
denniskempin
2015/11/16 18:39:28
I did notice some problems where android will star
| |
| 22 struct input_event event; | |
| 23 event.time.tv_sec = time.InSeconds(); | |
| 24 base::TimeDelta remainder = | |
| 25 time - base::TimeDelta::FromSeconds(event.time.tv_sec); | |
| 26 event.time.tv_usec = remainder.InMicroseconds(); | |
| 27 event.type = type; | |
| 28 event.code = code; | |
| 29 event.value = value; | |
| 30 | |
| 31 // Write event to file descriptor | |
| 32 int num_written = write(fd_.get(), reinterpret_cast<void*>(&event), | |
| 33 sizeof(struct input_event)); | |
| 34 if (num_written != sizeof(struct input_event)) { | |
| 35 LOG(ERROR) << "Can't write to file descriptor"; | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 void BridgeInputDevice::SendSynReport(base::TimeDelta time) { | |
| 40 SendEvent(time, EV_SYN, SYN_REPORT, 0); | |
| 41 } | |
| 42 | |
| 43 // KeyboardBridgeInputDevice | |
| 44 | |
| 45 void KeyboardBridgeInputDevice::OnKeyEvent(ui::KeyEvent* event) { | |
|
elijahtaylor1
2015/11/02 22:30:29
I feel like we should either include the keyboard
denniskempin
2015/11/16 18:39:28
This comment seems to be on the wrong file but I u
| |
| 46 int native_code = | |
| 47 ui::KeycodeConverter::DomCodeToNativeKeycode(event->code()); | |
| 48 | |
| 49 __u16 evdev_code = ui::NativeCodeToEvdevCode(native_code); | |
| 50 __u32 evdev_value; | |
| 51 if (event->type() == ui::ET_KEY_PRESSED) { | |
| 52 if (event->flags() & ui::EF_IS_REPEAT) { | |
| 53 evdev_value = kKeyRepeated; | |
| 54 } else { | |
| 55 evdev_value = kKeyPressed; | |
| 56 } | |
| 57 } else if (event->type() == ui::ET_KEY_RELEASED) { | |
| 58 evdev_value = kKeyReleased; | |
| 59 } | |
| 60 | |
| 61 SendEvent(event->time_stamp(), EV_KEY, evdev_code, evdev_value); | |
| 62 SendSynReport(event->time_stamp()); | |
| 63 } | |
| 64 | |
| 65 // MouseBridgeInputDevice | |
| 66 | |
| 67 void MouseBridgeInputDevice::OnMouseEvent(ui::MouseEvent* event) { | |
| 68 base::TimeDelta time = event->time_stamp(); | |
| 69 | |
| 70 // update location | |
| 71 SendEvent(time, EV_ABS, ABS_X, event->x()); | |
| 72 SendEvent(time, EV_ABS, ABS_Y, event->y()); | |
| 73 | |
| 74 // update buttons | |
| 75 SendMouseButton(event, ui::EF_LEFT_MOUSE_BUTTON, BTN_LEFT); | |
| 76 SendMouseButton(event, ui::EF_RIGHT_MOUSE_BUTTON, BTN_RIGHT); | |
| 77 SendMouseButton(event, ui::EF_MIDDLE_MOUSE_BUTTON, BTN_MIDDLE); | |
| 78 | |
| 79 // update scroll wheel | |
| 80 if (event->IsMouseWheelEvent()) { | |
| 81 ui::MouseWheelEvent* wheel_event = | |
| 82 static_cast<ui::MouseWheelEvent*>(event); | |
| 83 SendEvent(time, EV_REL, REL_WHEEL, wheel_event->y_offset()); | |
| 84 SendEvent(time, EV_REL, REL_HWHEEL, wheel_event->x_offset()); | |
| 85 } | |
| 86 | |
| 87 SendSynReport(time); | |
| 88 } | |
| 89 | |
| 90 void MouseBridgeInputDevice::SendMouseButton(ui::MouseEvent* event, int flag, | |
| 91 int evdev_code) { | |
| 92 if (event->changed_button_flags() & flag) { | |
| 93 bool button_value = event->flags() & flag; | |
| 94 SendEvent(event->time_stamp(), EV_KEY, evdev_code, (__s32)button_value); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 // TouchscreenBridgeInputDevice | |
| 99 | |
| 100 TouchscreenBridgeInputDevice::TouchscreenBridgeInputDevice(base::ScopedFD fd) : | |
| 101 BridgeInputDevice(fd.Pass()), | |
| 102 current_slot_tracking_ids_(kMaxSlots, kEmptySlot) {} | |
| 103 | |
| 104 void TouchscreenBridgeInputDevice::OnTouchEvent(ui::TouchEvent* event) { | |
| 105 ui::PointerDetails details = event->pointer_details(); | |
| 106 base::TimeDelta time = event->time_stamp(); | |
| 107 | |
| 108 // find or assing a slot for this tracking id | |
| 109 int slot_id = AcquireSlot(event); | |
| 110 if (slot_id < 0) { | |
| 111 LOG(ERROR) << "Ran out of slot IDs."; | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 // we only need to send the slot ID when it has changed. | |
| 116 if (slot_id != current_slot_) { | |
| 117 current_slot_ = slot_id; | |
| 118 SendEvent(time, EV_ABS, ABS_MT_SLOT, current_slot_); | |
| 119 } | |
| 120 | |
| 121 // update tracking id | |
| 122 if (event->type() == ui::ET_TOUCH_PRESSED) { | |
| 123 SendEvent(time, EV_ABS, ABS_MT_TRACKING_ID, event->touch_id()); | |
| 124 } else if (event->type() == ui::ET_TOUCH_RELEASED) { | |
| 125 SendEvent(time, EV_ABS, ABS_MT_TRACKING_ID, -1); | |
| 126 } | |
| 127 | |
| 128 // update touch information | |
| 129 if (event->type() == ui::ET_TOUCH_MOVED || | |
| 130 event->type() == ui::ET_TOUCH_PRESSED) { | |
| 131 SendEvent(time, EV_ABS, ABS_MT_POSITION_X, event->x()); | |
| 132 SendEvent(time, EV_ABS, ABS_MT_POSITION_Y, event->y()); | |
| 133 SendEvent(time, EV_ABS, ABS_MT_TOUCH_MAJOR, details.radius_x()); | |
| 134 SendEvent(time, EV_ABS, ABS_MT_TOUCH_MINOR, details.radius_y()); | |
| 135 } | |
| 136 SendSynReport(time); | |
| 137 } | |
| 138 | |
| 139 int TouchscreenBridgeInputDevice::AcquireSlot(ui::TouchEvent* event) { | |
| 140 int slot_id; | |
| 141 if (event->type() == ui::ET_TOUCH_PRESSED) { | |
| 142 slot_id = FindSlot(kEmptySlot); | |
| 143 } else { | |
| 144 slot_id = FindSlot(event->touch_id()); | |
| 145 } | |
| 146 if (slot_id < 0) { | |
| 147 return -1; | |
| 148 } | |
| 149 | |
| 150 if (event->type() == ui::ET_TOUCH_RELEASED) { | |
| 151 current_slot_tracking_ids_[slot_id] = kEmptySlot; | |
| 152 } else if (event->type() == ui::ET_TOUCH_PRESSED) { | |
| 153 current_slot_tracking_ids_[slot_id] = event->touch_id(); | |
| 154 } | |
| 155 return slot_id; | |
| 156 } | |
| 157 | |
| 158 int TouchscreenBridgeInputDevice::FindSlot(int tracking_id) { | |
| 159 for (int i = 0; i < kMaxSlots; ++i) { | |
| 160 if (current_slot_tracking_ids_[i] == tracking_id) { | |
| 161 return i; | |
| 162 } | |
| 163 } | |
| 164 return -1; | |
| 165 } | |
| 166 | |
| 167 } // namespace arc | |
| OLD | NEW |