| 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 | 06936fe | 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 |  | 
|  | 31 | }  // anonymous namespace | 
|  | 32 |  | 
| Kevin Schoedel | de1cdae | 2017-03-17 11:07:06 -0400 | [diff] [blame] | 33 | std::unique_ptr<VirtualTouchpad> VirtualTouchpadEvdev::Create() { | 
|  | 34 | std::unique_ptr<VirtualTouchpadEvdev> touchpad(new VirtualTouchpadEvdev()); | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 35 | touchpad->Reset(); | 
| Kevin Schoedel | de1cdae | 2017-03-17 11:07:06 -0400 | [diff] [blame] | 36 | return touchpad; | 
| Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 39 | void VirtualTouchpadEvdev::Reset() { | 
|  | 40 | for (auto& touchpad : touchpad_) { | 
|  | 41 | if (touchpad.injector) { | 
|  | 42 | touchpad.injector->Close(); | 
|  | 43 | } | 
|  | 44 | touchpad.injector = nullptr; | 
|  | 45 | touchpad.owned_injector.reset(); | 
|  | 46 | touchpad.last_device_x = INT32_MIN; | 
|  | 47 | touchpad.last_device_y = INT32_MIN; | 
|  | 48 | touchpad.touches = 0; | 
|  | 49 | touchpad.last_motion_event_buttons = 0; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 50 | } | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
|  | 53 | status_t VirtualTouchpadEvdev::Attach() { | 
|  | 54 | status_t status = OK; | 
|  | 55 | for (int i = 0; i < kTouchpads; ++i) { | 
|  | 56 | Touchpad& touchpad = touchpad_[i]; | 
|  | 57 | if (!touchpad.injector) { | 
|  | 58 | touchpad.owned_injector.reset(new EvdevInjector()); | 
|  | 59 | touchpad.injector = touchpad.owned_injector.get(); | 
|  | 60 | } | 
|  | 61 | String8 DeviceName; | 
|  | 62 | DeviceName.appendFormat(kDeviceNameFormat, i); | 
|  | 63 | touchpad.injector->ConfigureBegin(DeviceName, kDeviceBusType, | 
|  | 64 | kDeviceVendor, kDeviceProduct, | 
|  | 65 | kDeviceVersion); | 
|  | 66 | touchpad.injector->ConfigureInputProperty(INPUT_PROP_DIRECT); | 
|  | 67 | touchpad.injector->ConfigureMultiTouchXY(0, 0, kWidth - 1, kHeight - 1); | 
|  | 68 | touchpad.injector->ConfigureAbsSlots(kSlots); | 
|  | 69 | touchpad.injector->ConfigureKey(BTN_TOUCH); | 
|  | 70 | touchpad.injector->ConfigureKey(BTN_BACK); | 
|  | 71 | touchpad.injector->ConfigureEnd(); | 
|  | 72 | if (const status_t configuration_status =  touchpad.injector->GetError()) { | 
|  | 73 | status = configuration_status; | 
|  | 74 | } | 
|  | 75 | } | 
|  | 76 | return status; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
| Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 79 | status_t VirtualTouchpadEvdev::Detach() { | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 80 | Reset(); | 
| Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 81 | return OK; | 
|  | 82 | } | 
|  | 83 |  | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 84 | int VirtualTouchpadEvdev::Touch(int touchpad_id, float x, float y, | 
| Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 85 | float pressure) { | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 86 | if (touchpad_id < 0 || touchpad_id >= kTouchpads) { | 
|  | 87 | return EINVAL; | 
|  | 88 | } | 
| Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 89 | if ((x < 0.0f) || (x >= 1.0f) || (y < 0.0f) || (y >= 1.0f)) { | 
|  | 90 | return EINVAL; | 
|  | 91 | } | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 92 | int32_t device_x = x * kWidth; | 
|  | 93 | int32_t device_y = y * kHeight; | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 94 | Touchpad& touchpad = touchpad_[touchpad_id]; | 
|  | 95 | touchpad.touches = ((touchpad.touches & 1) << 1) | (pressure > 0); | 
| Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 96 | ALOGV("(%f,%f) %f -> (%" PRId32 ",%" PRId32 ") %d", x, y, pressure, device_x, | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 97 | device_y, touchpad.touches); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 98 |  | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 99 | if (!touchpad.injector) { | 
| Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 100 | return EvdevInjector::ERROR_SEQUENCING; | 
|  | 101 | } | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 102 | touchpad.injector->ResetError(); | 
|  | 103 | switch (touchpad.touches) { | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 104 | case 0b00:  // Hover continues. | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 105 | if (device_x != touchpad.last_device_x || | 
|  | 106 | device_y != touchpad.last_device_y) { | 
|  | 107 | touchpad.injector->SendMultiTouchXY(0, 0, device_x, device_y); | 
|  | 108 | touchpad.injector->SendSynReport(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 109 | } | 
|  | 110 | break; | 
|  | 111 | case 0b01:  // Touch begins. | 
|  | 112 | // Press. | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 113 | touchpad.injector->SendMultiTouchXY(0, 0, device_x, device_y); | 
|  | 114 | touchpad.injector->SendKey(BTN_TOUCH, EvdevInjector::KEY_PRESS); | 
|  | 115 | touchpad.injector->SendSynReport(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 116 | break; | 
|  | 117 | case 0b10:  // Touch ends. | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 118 | touchpad.injector->SendKey(BTN_TOUCH, EvdevInjector::KEY_RELEASE); | 
|  | 119 | touchpad.injector->SendMultiTouchLift(0); | 
|  | 120 | touchpad.injector->SendSynReport(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 121 | break; | 
|  | 122 | case 0b11:  // Touch continues. | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 123 | if (device_x != touchpad.last_device_x || | 
|  | 124 | device_y != touchpad.last_device_y) { | 
|  | 125 | touchpad.injector->SendMultiTouchXY(0, 0, device_x, device_y); | 
|  | 126 | touchpad.injector->SendSynReport(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 127 | } | 
|  | 128 | break; | 
|  | 129 | } | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 130 | touchpad.last_device_x = device_x; | 
|  | 131 | touchpad.last_device_y = device_y; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 132 |  | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 133 | return touchpad.injector->GetError(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 136 | int VirtualTouchpadEvdev::ButtonState(int touchpad_id, int buttons) { | 
|  | 137 | if (touchpad_id < 0 || touchpad_id >= kTouchpads) { | 
|  | 138 | return EINVAL; | 
|  | 139 | } | 
|  | 140 | Touchpad& touchpad = touchpad_[touchpad_id]; | 
|  | 141 | const int changes = touchpad.last_motion_event_buttons ^ buttons; | 
| Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 142 | if (!changes) { | 
|  | 143 | return 0; | 
|  | 144 | } | 
|  | 145 | if (buttons & ~AMOTION_EVENT_BUTTON_BACK) { | 
|  | 146 | return ENOTSUP; | 
|  | 147 | } | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 148 | 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] | 149 | buttons); | 
|  | 150 |  | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 151 | if (!touchpad.injector) { | 
| Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 152 | return EvdevInjector::ERROR_SEQUENCING; | 
|  | 153 | } | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 154 | touchpad.injector->ResetError(); | 
| Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 155 | if (changes & AMOTION_EVENT_BUTTON_BACK) { | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 156 | touchpad.injector->SendKey(BTN_BACK, (buttons & AMOTION_EVENT_BUTTON_BACK) | 
|  | 157 | ? EvdevInjector::KEY_PRESS | 
|  | 158 | : EvdevInjector::KEY_RELEASE); | 
|  | 159 | touchpad.injector->SendSynReport(); | 
| Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 160 | } | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 161 | touchpad.last_motion_event_buttons = buttons; | 
|  | 162 | return touchpad.injector->GetError(); | 
| Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 165 | void VirtualTouchpadEvdev::dumpInternal(String8& result) { | 
| Kevin Schoedel | 0108af7 | 2017-03-09 11:45:20 -0500 | [diff] [blame] | 166 | for (int i = 0; i < kTouchpads; ++i) { | 
|  | 167 | const auto& touchpad = touchpad_[i]; | 
|  | 168 | result.appendFormat("[virtual touchpad %d]\n", i); | 
|  | 169 | if (!touchpad.injector) { | 
|  | 170 | result.append("injector = none\n"); | 
|  | 171 | return; | 
|  | 172 | } | 
|  | 173 | result.appendFormat("injector = %s\n", | 
|  | 174 | touchpad.owned_injector ? "normal" : "test"); | 
|  | 175 | result.appendFormat("touches = %d\n", touchpad.touches); | 
|  | 176 | result.appendFormat("last_position = (%" PRId32 ", %" PRId32 ")\n", | 
|  | 177 | touchpad.last_device_x, touchpad.last_device_y); | 
|  | 178 | result.appendFormat("last_buttons = 0x%" PRIX32 "\n", | 
|  | 179 | touchpad.last_motion_event_buttons); | 
|  | 180 | touchpad.injector->dumpInternal(result); | 
|  | 181 | result.append("\n"); | 
| Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 182 | } | 
| Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 183 | } | 
|  | 184 |  | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 185 | }  // namespace dvr | 
|  | 186 | }  // namespace android |