Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 1 | #include "VirtualTouchpadEvdev.h" |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 2 | |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 3 | #include <android/input.h> |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 4 | #include <inttypes.h> |
| 5 | #include <linux/input.h> |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame] | 6 | #include <log/log.h> |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 7 | |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 8 | // References: |
| 9 | // [0] Multi-touch (MT) Protocol, |
| 10 | // https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt |
| 11 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 12 | namespace android { |
| 13 | namespace dvr { |
| 14 | |
| 15 | namespace { |
| 16 | |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 17 | // Virtual evdev device properties. The name is arbitrary, but Android can |
| 18 | // use it to look up device configuration, so it must be unique. Vendor and |
| 19 | // product values must be 0 to indicate an internal device and prevent a |
| 20 | // similar lookup that could conflict with a physical device. |
Santos Cordon | fa5cf46 | 2017-04-05 10:37:00 -0700 | [diff] [blame] | 21 | static const char* const kDeviceNameFormat = "vr-virtual-touchpad-%d"; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 22 | static constexpr int16_t kDeviceBusType = BUS_VIRTUAL; |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 23 | static constexpr int16_t kDeviceVendor = 0; |
| 24 | static constexpr int16_t kDeviceProduct = 0; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 25 | static constexpr int16_t kDeviceVersion = 0x0001; |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 26 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 27 | static constexpr int32_t kWidth = 0x10000; |
| 28 | static constexpr int32_t kHeight = 0x10000; |
| 29 | static constexpr int32_t kSlots = 2; |
| 30 | |
Kevin Schoedel | b299003 | 2017-06-15 11:21:13 -0400 | [diff] [blame] | 31 | static constexpr float kScrollScale = 100.0f; |
| 32 | |
Kevin Schoedel | d8fccf0 | 2017-06-05 11:13:20 -0400 | [diff] [blame] | 33 | int32_t scale_relative_scroll(float x) { |
Kevin Schoedel | b299003 | 2017-06-15 11:21:13 -0400 | [diff] [blame] | 34 | return kScrollScale * x; |
Kevin Schoedel | d8fccf0 | 2017-06-05 11:13:20 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 37 | } // anonymous namespace |
| 38 | |
Kevin Schoedel | de1cdae | 2017-03-17 11:07:06 -0400 | [diff] [blame] | 39 | std::unique_ptr<VirtualTouchpad> VirtualTouchpadEvdev::Create() { |
| 40 | std::unique_ptr<VirtualTouchpadEvdev> touchpad(new VirtualTouchpadEvdev()); |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 41 | touchpad->Reset(); |
Kevin Schoedel | de1cdae | 2017-03-17 11:07:06 -0400 | [diff] [blame] | 42 | return touchpad; |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 45 | void VirtualTouchpadEvdev::Reset() { |
| 46 | for (auto& touchpad : touchpad_) { |
| 47 | if (touchpad.injector) { |
| 48 | touchpad.injector->Close(); |
| 49 | } |
| 50 | touchpad.injector = nullptr; |
| 51 | touchpad.owned_injector.reset(); |
| 52 | touchpad.last_device_x = INT32_MIN; |
| 53 | touchpad.last_device_y = INT32_MIN; |
| 54 | touchpad.touches = 0; |
| 55 | touchpad.last_motion_event_buttons = 0; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 56 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | status_t VirtualTouchpadEvdev::Attach() { |
| 60 | status_t status = OK; |
| 61 | for (int i = 0; i < kTouchpads; ++i) { |
| 62 | Touchpad& touchpad = touchpad_[i]; |
| 63 | if (!touchpad.injector) { |
| 64 | touchpad.owned_injector.reset(new EvdevInjector()); |
| 65 | touchpad.injector = touchpad.owned_injector.get(); |
| 66 | } |
| 67 | String8 DeviceName; |
| 68 | DeviceName.appendFormat(kDeviceNameFormat, i); |
| 69 | touchpad.injector->ConfigureBegin(DeviceName, kDeviceBusType, |
| 70 | kDeviceVendor, kDeviceProduct, |
| 71 | kDeviceVersion); |
| 72 | touchpad.injector->ConfigureInputProperty(INPUT_PROP_DIRECT); |
| 73 | touchpad.injector->ConfigureMultiTouchXY(0, 0, kWidth - 1, kHeight - 1); |
| 74 | touchpad.injector->ConfigureAbsSlots(kSlots); |
Kevin Schoedel | b299003 | 2017-06-15 11:21:13 -0400 | [diff] [blame] | 75 | touchpad.injector->ConfigureRel(REL_WHEEL); |
| 76 | touchpad.injector->ConfigureRel(REL_HWHEEL); |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 77 | touchpad.injector->ConfigureKey(BTN_TOUCH); |
| 78 | touchpad.injector->ConfigureKey(BTN_BACK); |
| 79 | touchpad.injector->ConfigureEnd(); |
| 80 | if (const status_t configuration_status = touchpad.injector->GetError()) { |
| 81 | status = configuration_status; |
| 82 | } |
| 83 | } |
| 84 | return status; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 87 | status_t VirtualTouchpadEvdev::Detach() { |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 88 | Reset(); |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 89 | return OK; |
| 90 | } |
| 91 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 92 | int VirtualTouchpadEvdev::Touch(int touchpad_id, float x, float y, |
Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 93 | float pressure) { |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 94 | if (touchpad_id < 0 || touchpad_id >= kTouchpads) { |
| 95 | return EINVAL; |
| 96 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 97 | int32_t device_x = x * kWidth; |
| 98 | int32_t device_y = y * kHeight; |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 99 | Touchpad& touchpad = touchpad_[touchpad_id]; |
| 100 | touchpad.touches = ((touchpad.touches & 1) << 1) | (pressure > 0); |
Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 101 | ALOGV("(%f,%f) %f -> (%" PRId32 ",%" PRId32 ") %d", x, y, pressure, device_x, |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 102 | device_y, touchpad.touches); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 103 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 104 | if (!touchpad.injector) { |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 105 | return EvdevInjector::ERROR_SEQUENCING; |
| 106 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 107 | touchpad.injector->ResetError(); |
| 108 | switch (touchpad.touches) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 109 | case 0b00: // Hover continues. |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 110 | if (device_x != touchpad.last_device_x || |
| 111 | device_y != touchpad.last_device_y) { |
| 112 | touchpad.injector->SendMultiTouchXY(0, 0, device_x, device_y); |
| 113 | touchpad.injector->SendSynReport(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 114 | } |
| 115 | break; |
| 116 | case 0b01: // Touch begins. |
| 117 | // Press. |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 118 | touchpad.injector->SendMultiTouchXY(0, 0, device_x, device_y); |
| 119 | touchpad.injector->SendKey(BTN_TOUCH, EvdevInjector::KEY_PRESS); |
| 120 | touchpad.injector->SendSynReport(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 121 | break; |
| 122 | case 0b10: // Touch ends. |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 123 | touchpad.injector->SendKey(BTN_TOUCH, EvdevInjector::KEY_RELEASE); |
| 124 | touchpad.injector->SendMultiTouchLift(0); |
| 125 | touchpad.injector->SendSynReport(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 126 | break; |
| 127 | case 0b11: // Touch continues. |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 128 | if (device_x != touchpad.last_device_x || |
| 129 | device_y != touchpad.last_device_y) { |
| 130 | touchpad.injector->SendMultiTouchXY(0, 0, device_x, device_y); |
| 131 | touchpad.injector->SendSynReport(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 132 | } |
| 133 | break; |
| 134 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 135 | touchpad.last_device_x = device_x; |
| 136 | touchpad.last_device_y = device_y; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 137 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 138 | return touchpad.injector->GetError(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 141 | int VirtualTouchpadEvdev::ButtonState(int touchpad_id, int buttons) { |
| 142 | if (touchpad_id < 0 || touchpad_id >= kTouchpads) { |
| 143 | return EINVAL; |
| 144 | } |
| 145 | Touchpad& touchpad = touchpad_[touchpad_id]; |
| 146 | const int changes = touchpad.last_motion_event_buttons ^ buttons; |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 147 | if (!changes) { |
| 148 | return 0; |
| 149 | } |
| 150 | if (buttons & ~AMOTION_EVENT_BUTTON_BACK) { |
| 151 | return ENOTSUP; |
| 152 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 153 | ALOGV("change %X from %X to %X", changes, touchpad.last_motion_event_buttons, |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 154 | buttons); |
| 155 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 156 | if (!touchpad.injector) { |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 157 | return EvdevInjector::ERROR_SEQUENCING; |
| 158 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 159 | touchpad.injector->ResetError(); |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 160 | if (changes & AMOTION_EVENT_BUTTON_BACK) { |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 161 | touchpad.injector->SendKey(BTN_BACK, (buttons & AMOTION_EVENT_BUTTON_BACK) |
| 162 | ? EvdevInjector::KEY_PRESS |
| 163 | : EvdevInjector::KEY_RELEASE); |
| 164 | touchpad.injector->SendSynReport(); |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 165 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 166 | touchpad.last_motion_event_buttons = buttons; |
| 167 | return touchpad.injector->GetError(); |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Kevin Schoedel | d8fccf0 | 2017-06-05 11:13:20 -0400 | [diff] [blame] | 170 | int VirtualTouchpadEvdev::Scroll(int touchpad_id, float x, float y) { |
| 171 | if (touchpad_id < 0 || touchpad_id >= kTouchpads) { |
| 172 | return EINVAL; |
| 173 | } |
| 174 | if ((x < -1.0f) || (x > 1.0f) || (y < -1.0f) || (y > 1.0f)) { |
| 175 | return EINVAL; |
| 176 | } |
| 177 | Touchpad& touchpad = touchpad_[touchpad_id]; |
| 178 | if (!touchpad.injector) { |
| 179 | return EvdevInjector::ERROR_SEQUENCING; |
| 180 | } |
| 181 | touchpad.injector->ResetError(); |
| 182 | const int32_t scaled_x = scale_relative_scroll(x); |
| 183 | const int32_t scaled_y = scale_relative_scroll(y); |
Kevin Schoedel | b299003 | 2017-06-15 11:21:13 -0400 | [diff] [blame] | 184 | ALOGV("(%f,%f) -> (%" PRId32 ",%" PRId32 ")", x, y, scaled_x, scaled_y); |
Kevin Schoedel | d8fccf0 | 2017-06-05 11:13:20 -0400 | [diff] [blame] | 185 | if (scaled_x) { |
| 186 | touchpad.injector->SendRel(REL_HWHEEL, scaled_x); |
| 187 | } |
| 188 | if (scaled_y) { |
| 189 | touchpad.injector->SendRel(REL_WHEEL, scaled_y); |
| 190 | } |
| 191 | if (scaled_x || scaled_y) { |
| 192 | touchpad.injector->SendSynReport(); |
| 193 | } |
| 194 | return touchpad.injector->GetError(); |
| 195 | } |
| 196 | |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 197 | void VirtualTouchpadEvdev::dumpInternal(String8& result) { |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 198 | for (int i = 0; i < kTouchpads; ++i) { |
| 199 | const auto& touchpad = touchpad_[i]; |
| 200 | result.appendFormat("[virtual touchpad %d]\n", i); |
| 201 | if (!touchpad.injector) { |
| 202 | result.append("injector = none\n"); |
| 203 | return; |
| 204 | } |
| 205 | result.appendFormat("injector = %s\n", |
| 206 | touchpad.owned_injector ? "normal" : "test"); |
| 207 | result.appendFormat("touches = %d\n", touchpad.touches); |
| 208 | result.appendFormat("last_position = (%" PRId32 ", %" PRId32 ")\n", |
| 209 | touchpad.last_device_x, touchpad.last_device_y); |
| 210 | result.appendFormat("last_buttons = 0x%" PRIX32 "\n", |
| 211 | touchpad.last_motion_event_buttons); |
| 212 | touchpad.injector->dumpInternal(result); |
| 213 | result.append("\n"); |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 214 | } |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 217 | } // namespace dvr |
| 218 | } // namespace android |