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 | d8fccf0 | 2017-06-05 11:13:20 -0400 | [diff] [blame] | 31 | int32_t scale_relative_scroll(float x) { |
| 32 | // Guilty with an explanation, your honor. |
| 33 | // Ideally we should be able to communicate the full incoming precision |
| 34 | // to InputFlinger, through the evdev int32_t value, by scaling by a |
| 35 | // large factor, i.e. 2²³ for IEEE single precision floating point. |
| 36 | // However, although InputFlinger has |wheelVelocityControlParameters|, |
| 37 | // those parameters are currently hard coded, with a scale factor of 1.0. |
| 38 | // The observed evdev value for a physical mouse scroll wheel is usually |
| 39 | // ±1, with higher values up to ±4 for a very fast spin. So we imitate |
| 40 | // that. If the incoming value is not actually 0, the resulting magnitude |
| 41 | // should be at least 1, so that small movements are not lost. |
| 42 | // Adding IDC configurability of |VelocityControlParameters| may be |
| 43 | // desirable in the future. |
| 44 | return copysignf(ceilf(fabs(4.0f * x)), x); |
| 45 | } |
| 46 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 47 | } // anonymous namespace |
| 48 | |
Kevin Schoedel | de1cdae | 2017-03-17 11:07:06 -0400 | [diff] [blame] | 49 | std::unique_ptr<VirtualTouchpad> VirtualTouchpadEvdev::Create() { |
| 50 | std::unique_ptr<VirtualTouchpadEvdev> touchpad(new VirtualTouchpadEvdev()); |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 51 | touchpad->Reset(); |
Kevin Schoedel | de1cdae | 2017-03-17 11:07:06 -0400 | [diff] [blame] | 52 | return touchpad; |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 55 | void VirtualTouchpadEvdev::Reset() { |
| 56 | for (auto& touchpad : touchpad_) { |
| 57 | if (touchpad.injector) { |
| 58 | touchpad.injector->Close(); |
| 59 | } |
| 60 | touchpad.injector = nullptr; |
| 61 | touchpad.owned_injector.reset(); |
| 62 | touchpad.last_device_x = INT32_MIN; |
| 63 | touchpad.last_device_y = INT32_MIN; |
| 64 | touchpad.touches = 0; |
| 65 | touchpad.last_motion_event_buttons = 0; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 66 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | status_t VirtualTouchpadEvdev::Attach() { |
| 70 | status_t status = OK; |
| 71 | for (int i = 0; i < kTouchpads; ++i) { |
| 72 | Touchpad& touchpad = touchpad_[i]; |
| 73 | if (!touchpad.injector) { |
| 74 | touchpad.owned_injector.reset(new EvdevInjector()); |
| 75 | touchpad.injector = touchpad.owned_injector.get(); |
| 76 | } |
| 77 | String8 DeviceName; |
| 78 | DeviceName.appendFormat(kDeviceNameFormat, i); |
| 79 | touchpad.injector->ConfigureBegin(DeviceName, kDeviceBusType, |
| 80 | kDeviceVendor, kDeviceProduct, |
| 81 | kDeviceVersion); |
| 82 | touchpad.injector->ConfigureInputProperty(INPUT_PROP_DIRECT); |
| 83 | touchpad.injector->ConfigureMultiTouchXY(0, 0, kWidth - 1, kHeight - 1); |
| 84 | touchpad.injector->ConfigureAbsSlots(kSlots); |
| 85 | touchpad.injector->ConfigureKey(BTN_TOUCH); |
| 86 | touchpad.injector->ConfigureKey(BTN_BACK); |
| 87 | touchpad.injector->ConfigureEnd(); |
| 88 | if (const status_t configuration_status = touchpad.injector->GetError()) { |
| 89 | status = configuration_status; |
| 90 | } |
| 91 | } |
| 92 | return status; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 95 | status_t VirtualTouchpadEvdev::Detach() { |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 96 | Reset(); |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 97 | return OK; |
| 98 | } |
| 99 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 100 | int VirtualTouchpadEvdev::Touch(int touchpad_id, float x, float y, |
Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 101 | float pressure) { |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 102 | if (touchpad_id < 0 || touchpad_id >= kTouchpads) { |
| 103 | return EINVAL; |
| 104 | } |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 105 | if ((x < 0.0f) || (x >= 1.0f) || (y < 0.0f) || (y >= 1.0f)) { |
| 106 | return EINVAL; |
| 107 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 108 | int32_t device_x = x * kWidth; |
| 109 | int32_t device_y = y * kHeight; |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 110 | Touchpad& touchpad = touchpad_[touchpad_id]; |
| 111 | touchpad.touches = ((touchpad.touches & 1) << 1) | (pressure > 0); |
Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 112 | ALOGV("(%f,%f) %f -> (%" PRId32 ",%" PRId32 ") %d", x, y, pressure, device_x, |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 113 | device_y, touchpad.touches); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 114 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 115 | if (!touchpad.injector) { |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 116 | return EvdevInjector::ERROR_SEQUENCING; |
| 117 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 118 | touchpad.injector->ResetError(); |
| 119 | switch (touchpad.touches) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 120 | case 0b00: // Hover continues. |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 121 | if (device_x != touchpad.last_device_x || |
| 122 | device_y != touchpad.last_device_y) { |
| 123 | touchpad.injector->SendMultiTouchXY(0, 0, device_x, device_y); |
| 124 | touchpad.injector->SendSynReport(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 125 | } |
| 126 | break; |
| 127 | case 0b01: // Touch begins. |
| 128 | // Press. |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 129 | touchpad.injector->SendMultiTouchXY(0, 0, device_x, device_y); |
| 130 | touchpad.injector->SendKey(BTN_TOUCH, EvdevInjector::KEY_PRESS); |
| 131 | touchpad.injector->SendSynReport(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 132 | break; |
| 133 | case 0b10: // Touch ends. |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 134 | touchpad.injector->SendKey(BTN_TOUCH, EvdevInjector::KEY_RELEASE); |
| 135 | touchpad.injector->SendMultiTouchLift(0); |
| 136 | touchpad.injector->SendSynReport(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 137 | break; |
| 138 | case 0b11: // Touch continues. |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 139 | if (device_x != touchpad.last_device_x || |
| 140 | device_y != touchpad.last_device_y) { |
| 141 | touchpad.injector->SendMultiTouchXY(0, 0, device_x, device_y); |
| 142 | touchpad.injector->SendSynReport(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 143 | } |
| 144 | break; |
| 145 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 146 | touchpad.last_device_x = device_x; |
| 147 | touchpad.last_device_y = device_y; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 148 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 149 | return touchpad.injector->GetError(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 152 | int VirtualTouchpadEvdev::ButtonState(int touchpad_id, int buttons) { |
| 153 | if (touchpad_id < 0 || touchpad_id >= kTouchpads) { |
| 154 | return EINVAL; |
| 155 | } |
| 156 | Touchpad& touchpad = touchpad_[touchpad_id]; |
| 157 | const int changes = touchpad.last_motion_event_buttons ^ buttons; |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 158 | if (!changes) { |
| 159 | return 0; |
| 160 | } |
| 161 | if (buttons & ~AMOTION_EVENT_BUTTON_BACK) { |
| 162 | return ENOTSUP; |
| 163 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 164 | 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] | 165 | buttons); |
| 166 | |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 167 | if (!touchpad.injector) { |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 168 | return EvdevInjector::ERROR_SEQUENCING; |
| 169 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 170 | touchpad.injector->ResetError(); |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 171 | if (changes & AMOTION_EVENT_BUTTON_BACK) { |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 172 | touchpad.injector->SendKey(BTN_BACK, (buttons & AMOTION_EVENT_BUTTON_BACK) |
| 173 | ? EvdevInjector::KEY_PRESS |
| 174 | : EvdevInjector::KEY_RELEASE); |
| 175 | touchpad.injector->SendSynReport(); |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 176 | } |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 177 | touchpad.last_motion_event_buttons = buttons; |
| 178 | return touchpad.injector->GetError(); |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Kevin Schoedel | d8fccf0 | 2017-06-05 11:13:20 -0400 | [diff] [blame] | 181 | int VirtualTouchpadEvdev::Scroll(int touchpad_id, float x, float y) { |
| 182 | if (touchpad_id < 0 || touchpad_id >= kTouchpads) { |
| 183 | return EINVAL; |
| 184 | } |
| 185 | if ((x < -1.0f) || (x > 1.0f) || (y < -1.0f) || (y > 1.0f)) { |
| 186 | return EINVAL; |
| 187 | } |
| 188 | Touchpad& touchpad = touchpad_[touchpad_id]; |
| 189 | if (!touchpad.injector) { |
| 190 | return EvdevInjector::ERROR_SEQUENCING; |
| 191 | } |
| 192 | touchpad.injector->ResetError(); |
| 193 | const int32_t scaled_x = scale_relative_scroll(x); |
| 194 | const int32_t scaled_y = scale_relative_scroll(y); |
| 195 | if (scaled_x) { |
| 196 | touchpad.injector->SendRel(REL_HWHEEL, scaled_x); |
| 197 | } |
| 198 | if (scaled_y) { |
| 199 | touchpad.injector->SendRel(REL_WHEEL, scaled_y); |
| 200 | } |
| 201 | if (scaled_x || scaled_y) { |
| 202 | touchpad.injector->SendSynReport(); |
| 203 | } |
| 204 | return touchpad.injector->GetError(); |
| 205 | } |
| 206 | |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 207 | void VirtualTouchpadEvdev::dumpInternal(String8& result) { |
Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 208 | for (int i = 0; i < kTouchpads; ++i) { |
| 209 | const auto& touchpad = touchpad_[i]; |
| 210 | result.appendFormat("[virtual touchpad %d]\n", i); |
| 211 | if (!touchpad.injector) { |
| 212 | result.append("injector = none\n"); |
| 213 | return; |
| 214 | } |
| 215 | result.appendFormat("injector = %s\n", |
| 216 | touchpad.owned_injector ? "normal" : "test"); |
| 217 | result.appendFormat("touches = %d\n", touchpad.touches); |
| 218 | result.appendFormat("last_position = (%" PRId32 ", %" PRId32 ")\n", |
| 219 | touchpad.last_device_x, touchpad.last_device_y); |
| 220 | result.appendFormat("last_buttons = 0x%" PRIX32 "\n", |
| 221 | touchpad.last_motion_event_buttons); |
| 222 | touchpad.injector->dumpInternal(result); |
| 223 | result.append("\n"); |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 224 | } |
Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 227 | } // namespace dvr |
| 228 | } // namespace android |