blob: ee09d4831402d7d9df13ed3b0dc15ad91ad5c51e [file] [log] [blame]
Kevin Schoedel89af70b2017-03-03 18:11:37 -05001#include "VirtualTouchpadEvdev.h"
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08002
Kevin Schoedel43b5b062017-01-19 13:46:17 -05003#include <android/input.h>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08004#include <inttypes.h>
5#include <linux/input.h>
Alex Vakulenko4fe60582017-02-02 11:35:59 -08006#include <log/log.h>
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08007
Kevin Schoedel43b5b062017-01-19 13:46:17 -05008// References:
9// [0] Multi-touch (MT) Protocol,
10// https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt
11
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080012namespace android {
13namespace dvr {
14
15namespace {
16
Kevin Schoedel43b5b062017-01-19 13:46:17 -050017// 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.
Kevin Schoedel0108af72017-03-09 11:45:20 -050021static const char* const kDeviceNameFormat = "vr virtual touchpad %d";
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080022static constexpr int16_t kDeviceBusType = BUS_VIRTUAL;
Kevin Schoedel43b5b062017-01-19 13:46:17 -050023static constexpr int16_t kDeviceVendor = 0;
24static constexpr int16_t kDeviceProduct = 0;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080025static constexpr int16_t kDeviceVersion = 0x0001;
Kevin Schoedel43b5b062017-01-19 13:46:17 -050026
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080027static constexpr int32_t kWidth = 0x10000;
28static constexpr int32_t kHeight = 0x10000;
29static constexpr int32_t kSlots = 2;
30
31} // anonymous namespace
32
Kevin Schoedelde1cdae2017-03-17 11:07:06 -040033std::unique_ptr<VirtualTouchpad> VirtualTouchpadEvdev::Create() {
34 std::unique_ptr<VirtualTouchpadEvdev> touchpad(new VirtualTouchpadEvdev());
Kevin Schoedel0108af72017-03-09 11:45:20 -050035 touchpad->Reset();
Kevin Schoedelde1cdae2017-03-17 11:07:06 -040036 return touchpad;
Kevin Schoedel89af70b2017-03-03 18:11:37 -050037}
38
Kevin Schoedel0108af72017-03-09 11:45:20 -050039void 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 Vakulenkoe4eec202017-01-27 14:41:04 -080050 }
Kevin Schoedel0108af72017-03-09 11:45:20 -050051}
52
53status_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 Vakulenkoe4eec202017-01-27 14:41:04 -080077}
78
Kevin Schoedel4b64dd42017-03-07 13:06:25 -050079status_t VirtualTouchpadEvdev::Detach() {
Kevin Schoedel0108af72017-03-09 11:45:20 -050080 Reset();
Kevin Schoedel4b64dd42017-03-07 13:06:25 -050081 return OK;
82}
83
Kevin Schoedel0108af72017-03-09 11:45:20 -050084int VirtualTouchpadEvdev::Touch(int touchpad_id, float x, float y,
Kevin Schoedel3002b8a2017-03-06 14:34:39 -050085 float pressure) {
Kevin Schoedel0108af72017-03-09 11:45:20 -050086 if (touchpad_id < 0 || touchpad_id >= kTouchpads) {
87 return EINVAL;
88 }
Kevin Schoedel43b5b062017-01-19 13:46:17 -050089 if ((x < 0.0f) || (x >= 1.0f) || (y < 0.0f) || (y >= 1.0f)) {
90 return EINVAL;
91 }
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080092 int32_t device_x = x * kWidth;
93 int32_t device_y = y * kHeight;
Kevin Schoedel0108af72017-03-09 11:45:20 -050094 Touchpad& touchpad = touchpad_[touchpad_id];
95 touchpad.touches = ((touchpad.touches & 1) << 1) | (pressure > 0);
Kevin Schoedel3002b8a2017-03-06 14:34:39 -050096 ALOGV("(%f,%f) %f -> (%" PRId32 ",%" PRId32 ") %d", x, y, pressure, device_x,
Kevin Schoedel0108af72017-03-09 11:45:20 -050097 device_y, touchpad.touches);
Alex Vakulenkoe4eec202017-01-27 14:41:04 -080098
Kevin Schoedel0108af72017-03-09 11:45:20 -050099 if (!touchpad.injector) {
Kevin Schoedel43b5b062017-01-19 13:46:17 -0500100 return EvdevInjector::ERROR_SEQUENCING;
101 }
Kevin Schoedel0108af72017-03-09 11:45:20 -0500102 touchpad.injector->ResetError();
103 switch (touchpad.touches) {
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800104 case 0b00: // Hover continues.
Kevin Schoedel0108af72017-03-09 11:45:20 -0500105 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 Vakulenkoe4eec202017-01-27 14:41:04 -0800109 }
110 break;
111 case 0b01: // Touch begins.
112 // Press.
Kevin Schoedel0108af72017-03-09 11:45:20 -0500113 touchpad.injector->SendMultiTouchXY(0, 0, device_x, device_y);
114 touchpad.injector->SendKey(BTN_TOUCH, EvdevInjector::KEY_PRESS);
115 touchpad.injector->SendSynReport();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800116 break;
117 case 0b10: // Touch ends.
Kevin Schoedel0108af72017-03-09 11:45:20 -0500118 touchpad.injector->SendKey(BTN_TOUCH, EvdevInjector::KEY_RELEASE);
119 touchpad.injector->SendMultiTouchLift(0);
120 touchpad.injector->SendSynReport();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800121 break;
122 case 0b11: // Touch continues.
Kevin Schoedel0108af72017-03-09 11:45:20 -0500123 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 Vakulenkoe4eec202017-01-27 14:41:04 -0800127 }
128 break;
129 }
Kevin Schoedel0108af72017-03-09 11:45:20 -0500130 touchpad.last_device_x = device_x;
131 touchpad.last_device_y = device_y;
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800132
Kevin Schoedel0108af72017-03-09 11:45:20 -0500133 return touchpad.injector->GetError();
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800134}
135
Kevin Schoedel0108af72017-03-09 11:45:20 -0500136int 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 Schoedel43b5b062017-01-19 13:46:17 -0500142 if (!changes) {
143 return 0;
144 }
145 if (buttons & ~AMOTION_EVENT_BUTTON_BACK) {
146 return ENOTSUP;
147 }
Kevin Schoedel0108af72017-03-09 11:45:20 -0500148 ALOGV("change %X from %X to %X", changes, touchpad.last_motion_event_buttons,
Kevin Schoedel43b5b062017-01-19 13:46:17 -0500149 buttons);
150
Kevin Schoedel0108af72017-03-09 11:45:20 -0500151 if (!touchpad.injector) {
Kevin Schoedel43b5b062017-01-19 13:46:17 -0500152 return EvdevInjector::ERROR_SEQUENCING;
153 }
Kevin Schoedel0108af72017-03-09 11:45:20 -0500154 touchpad.injector->ResetError();
Kevin Schoedel43b5b062017-01-19 13:46:17 -0500155 if (changes & AMOTION_EVENT_BUTTON_BACK) {
Kevin Schoedel0108af72017-03-09 11:45:20 -0500156 touchpad.injector->SendKey(BTN_BACK, (buttons & AMOTION_EVENT_BUTTON_BACK)
157 ? EvdevInjector::KEY_PRESS
158 : EvdevInjector::KEY_RELEASE);
159 touchpad.injector->SendSynReport();
Kevin Schoedel43b5b062017-01-19 13:46:17 -0500160 }
Kevin Schoedel0108af72017-03-09 11:45:20 -0500161 touchpad.last_motion_event_buttons = buttons;
162 return touchpad.injector->GetError();
Kevin Schoedel43b5b062017-01-19 13:46:17 -0500163}
164
Kevin Schoedel4b64dd42017-03-07 13:06:25 -0500165void VirtualTouchpadEvdev::dumpInternal(String8& result) {
Kevin Schoedel0108af72017-03-09 11:45:20 -0500166 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 Schoedel4b64dd42017-03-07 13:06:25 -0500182 }
Kevin Schoedel4b64dd42017-03-07 13:06:25 -0500183}
184
Alex Vakulenkoe4eec202017-01-27 14:41:04 -0800185} // namespace dvr
186} // namespace android