Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 1 | #include "VirtualTouchpad.h" |
| 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 <cutils/log.h> |
| 5 | #include <inttypes.h> |
| 6 | #include <linux/input.h> |
| 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. |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 21 | static const char* const kDeviceName = "vr window manager virtual touchpad"; |
| 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 | |
| 33 | int VirtualTouchpad::Initialize() { |
| 34 | if (!injector_) { |
| 35 | owned_injector_.reset(new EvdevInjector()); |
| 36 | injector_ = owned_injector_.get(); |
| 37 | } |
| 38 | injector_->ConfigureBegin(kDeviceName, kDeviceBusType, kDeviceVendor, |
| 39 | kDeviceProduct, kDeviceVersion); |
| 40 | injector_->ConfigureInputProperty(INPUT_PROP_DIRECT); |
| 41 | injector_->ConfigureMultiTouchXY(0, 0, kWidth - 1, kHeight - 1); |
| 42 | injector_->ConfigureAbsSlots(kSlots); |
| 43 | injector_->ConfigureKey(BTN_TOUCH); |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame^] | 44 | injector_->ConfigureKey(BTN_BACK); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 45 | injector_->ConfigureEnd(); |
| 46 | return injector_->GetError(); |
| 47 | } |
| 48 | |
| 49 | int VirtualTouchpad::Touch(float x, float y, float pressure) { |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame^] | 50 | if ((x < 0.0f) || (x >= 1.0f) || (y < 0.0f) || (y >= 1.0f)) { |
| 51 | return EINVAL; |
| 52 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 53 | int32_t device_x = x * kWidth; |
| 54 | int32_t device_y = y * kHeight; |
| 55 | touches_ = ((touches_ & 1) << 1) | (pressure > 0); |
| 56 | ALOGV("(%f,%f) %f -> (%" PRId32 ",%" PRId32 ") %d", |
| 57 | x, y, pressure, device_x, device_y, touches_); |
| 58 | |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame^] | 59 | if (!injector_) { |
| 60 | return EvdevInjector::ERROR_SEQUENCING; |
| 61 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 62 | injector_->ResetError(); |
| 63 | switch (touches_) { |
| 64 | case 0b00: // Hover continues. |
| 65 | if (device_x != last_device_x_ || device_y != last_device_y_) { |
| 66 | injector_->SendMultiTouchXY(0, 0, device_x, device_y); |
| 67 | injector_->SendSynReport(); |
| 68 | } |
| 69 | break; |
| 70 | case 0b01: // Touch begins. |
| 71 | // Press. |
| 72 | injector_->SendMultiTouchXY(0, 0, device_x, device_y); |
| 73 | injector_->SendKey(BTN_TOUCH, EvdevInjector::KEY_PRESS); |
| 74 | injector_->SendSynReport(); |
| 75 | break; |
| 76 | case 0b10: // Touch ends. |
| 77 | injector_->SendKey(BTN_TOUCH, EvdevInjector::KEY_RELEASE); |
| 78 | injector_->SendMultiTouchLift(0); |
| 79 | injector_->SendSynReport(); |
| 80 | break; |
| 81 | case 0b11: // Touch continues. |
| 82 | if (device_x != last_device_x_ || device_y != last_device_y_) { |
| 83 | injector_->SendMultiTouchXY(0, 0, device_x, device_y); |
| 84 | injector_->SendSynReport(); |
| 85 | } |
| 86 | break; |
| 87 | } |
| 88 | last_device_x_ = device_x; |
| 89 | last_device_y_ = device_y; |
| 90 | |
| 91 | return injector_->GetError(); |
| 92 | } |
| 93 | |
Kevin Schoedel | 43b5b06 | 2017-01-19 13:46:17 -0500 | [diff] [blame^] | 94 | int VirtualTouchpad::ButtonState(int buttons) { |
| 95 | const int changes = last_motion_event_buttons_ ^ buttons; |
| 96 | if (!changes) { |
| 97 | return 0; |
| 98 | } |
| 99 | if (buttons & ~AMOTION_EVENT_BUTTON_BACK) { |
| 100 | return ENOTSUP; |
| 101 | } |
| 102 | ALOGV("change %X from %X to %X", changes, last_motion_event_buttons_, |
| 103 | buttons); |
| 104 | |
| 105 | if (!injector_) { |
| 106 | return EvdevInjector::ERROR_SEQUENCING; |
| 107 | } |
| 108 | injector_->ResetError(); |
| 109 | if (changes & AMOTION_EVENT_BUTTON_BACK) { |
| 110 | injector_->SendKey(BTN_BACK, |
| 111 | (buttons & AMOTION_EVENT_BUTTON_BACK) |
| 112 | ? EvdevInjector::KEY_PRESS |
| 113 | : EvdevInjector::KEY_RELEASE); |
| 114 | } |
| 115 | last_motion_event_buttons_ = buttons; |
| 116 | return injector_->GetError(); |
| 117 | } |
| 118 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 119 | } // namespace dvr |
| 120 | } // namespace android |