blob: de6e4b019304e46d1137130aa166ec9a4e5dcbc2 [file] [log] [blame]
Harry Cutts79cc9fa2022-10-28 15:32:39 +00001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "../Macros.h"
18
Harry Cutts1f48a442022-11-15 17:38:36 +000019#include <chrono>
Harry Cutts74235542022-11-24 15:52:53 +000020
21#include <android/input.h>
22#include <log/log_main.h>
23#include "TouchCursorInputMapperCommon.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000024#include "TouchpadInputMapper.h"
25
26namespace android {
27
Harry Cutts1f48a442022-11-15 17:38:36 +000028namespace {
29
30short getMaxTouchCount(const InputDeviceContext& context) {
31 if (context.hasKeyCode(BTN_TOOL_QUINTTAP)) return 5;
32 if (context.hasKeyCode(BTN_TOOL_QUADTAP)) return 4;
33 if (context.hasKeyCode(BTN_TOOL_TRIPLETAP)) return 3;
34 if (context.hasKeyCode(BTN_TOOL_DOUBLETAP)) return 2;
35 if (context.hasKeyCode(BTN_TOOL_FINGER)) return 1;
36 return 0;
37}
38
39HardwareProperties createHardwareProperties(const InputDeviceContext& context) {
40 HardwareProperties props;
41 RawAbsoluteAxisInfo absMtPositionX;
42 context.getAbsoluteAxisInfo(ABS_MT_POSITION_X, &absMtPositionX);
43 props.left = absMtPositionX.minValue;
44 props.right = absMtPositionX.maxValue;
45 props.res_x = absMtPositionX.resolution;
46
47 RawAbsoluteAxisInfo absMtPositionY;
48 context.getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &absMtPositionY);
49 props.top = absMtPositionY.minValue;
50 props.bottom = absMtPositionY.maxValue;
51 props.res_y = absMtPositionY.resolution;
52
53 RawAbsoluteAxisInfo absMtOrientation;
54 context.getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &absMtOrientation);
55 props.orientation_minimum = absMtOrientation.minValue;
56 props.orientation_maximum = absMtOrientation.maxValue;
57
58 RawAbsoluteAxisInfo absMtSlot;
59 context.getAbsoluteAxisInfo(ABS_MT_SLOT, &absMtSlot);
60 props.max_finger_cnt = absMtSlot.maxValue - absMtSlot.minValue + 1;
61 props.max_touch_cnt = getMaxTouchCount(context);
62
63 // T5R2 ("Track 5, Report 2") is a feature of some old Synaptics touchpads that could track 5
64 // fingers but only report the coordinates of 2 of them. We don't know of any external touchpads
65 // that did this, so assume false.
66 props.supports_t5r2 = false;
67
68 props.support_semi_mt = context.hasInputProperty(INPUT_PROP_SEMI_MT);
69 props.is_button_pad = context.hasInputProperty(INPUT_PROP_BUTTONPAD);
70
71 // Mouse-only properties, which will always be false.
72 props.has_wheel = false;
73 props.wheel_is_hi_res = false;
74
75 // Linux Kernel haptic touchpad support isn't merged yet, so for now assume that no touchpads
76 // are haptic.
77 props.is_haptic_pad = false;
78 return props;
79}
80
Harry Cutts74235542022-11-24 15:52:53 +000081void gestureInterpreterCallback(void* clientData, const Gesture* gesture) {
82 TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData);
83 mapper->consumeGesture(gesture);
84}
85
86uint32_t gesturesButtonToMotionEventButton(uint32_t gesturesButton) {
87 switch (gesturesButton) {
88 case GESTURES_BUTTON_LEFT:
89 return AMOTION_EVENT_BUTTON_PRIMARY;
90 case GESTURES_BUTTON_MIDDLE:
91 return AMOTION_EVENT_BUTTON_TERTIARY;
92 case GESTURES_BUTTON_RIGHT:
93 return AMOTION_EVENT_BUTTON_SECONDARY;
94 case GESTURES_BUTTON_BACK:
95 return AMOTION_EVENT_BUTTON_BACK;
96 case GESTURES_BUTTON_FORWARD:
97 return AMOTION_EVENT_BUTTON_FORWARD;
98 default:
99 return 0;
100 }
Harry Cutts1f48a442022-11-15 17:38:36 +0000101}
102
103} // namespace
104
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000105TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext)
Harry Cutts1f48a442022-11-15 17:38:36 +0000106 : InputMapper(deviceContext),
107 mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter),
Harry Cutts74235542022-11-24 15:52:53 +0000108 mPointerController(getContext()->getPointerController(getDeviceId())),
Harry Cutts1f48a442022-11-15 17:38:36 +0000109 mTouchButtonAccumulator(deviceContext) {
110 mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD);
111 mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext));
Harry Cutts74235542022-11-24 15:52:53 +0000112 // Even though we don't explicitly delete copy/move semantics, it's safe to
113 // give away a pointer to TouchpadInputMapper here because
114 // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and
115 // 2) TouchpadInputMapper is stored as a unique_ptr and not moved.
116 mGestureInterpreter->SetCallback(gestureInterpreterCallback, this);
Harry Cutts1f48a442022-11-15 17:38:36 +0000117 // TODO(b/251196347): set a property provider, so we can change gesture properties.
118 // TODO(b/251196347): set a timer provider, so the library can use timers.
119
120 RawAbsoluteAxisInfo slotAxisInfo;
121 getAbsoluteAxisInfo(ABS_MT_SLOT, &slotAxisInfo);
122 if (!slotAxisInfo.valid || slotAxisInfo.maxValue <= 0) {
123 ALOGW("Touchpad \"%s\" doesn't have a valid ABS_MT_SLOT axis, and probably won't work "
124 "properly.",
125 getDeviceName().c_str());
126 }
127 mMotionAccumulator.configure(getDeviceContext(), slotAxisInfo.maxValue + 1, true);
128 mTouchButtonAccumulator.configure();
129}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000130
Harry Cutts74235542022-11-24 15:52:53 +0000131TouchpadInputMapper::~TouchpadInputMapper() {
132 if (mPointerController != nullptr) {
133 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
134 }
135}
136
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000137uint32_t TouchpadInputMapper::getSources() const {
138 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
139}
140
Harry Cutts1f48a442022-11-15 17:38:36 +0000141std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
142 mCursorButtonAccumulator.reset(getDeviceContext());
143 mTouchButtonAccumulator.reset();
144 mMscTimestamp = 0;
Harry Cutts74235542022-11-24 15:52:53 +0000145
146 mButtonState = 0;
Harry Cutts1f48a442022-11-15 17:38:36 +0000147 return InputMapper::reset(when);
148}
149
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000150std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) {
Harry Cutts74235542022-11-24 15:52:53 +0000151 std::list<NotifyArgs> out = {};
Harry Cutts1f48a442022-11-15 17:38:36 +0000152 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Harry Cutts74235542022-11-24 15:52:53 +0000153 out = sync(rawEvent->when, rawEvent->readTime);
Harry Cutts1f48a442022-11-15 17:38:36 +0000154 }
155 if (rawEvent->type == EV_MSC && rawEvent->code == MSC_TIMESTAMP) {
156 mMscTimestamp = rawEvent->value;
157 }
158 mCursorButtonAccumulator.process(rawEvent);
159 mMotionAccumulator.process(rawEvent);
160 mTouchButtonAccumulator.process(rawEvent);
Harry Cutts74235542022-11-24 15:52:53 +0000161 return out;
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000162}
163
Harry Cutts74235542022-11-24 15:52:53 +0000164std::list<NotifyArgs> TouchpadInputMapper::sync(nsecs_t when, nsecs_t readTime) {
Harry Cutts1f48a442022-11-15 17:38:36 +0000165 HardwareState hwState;
166 // The gestures library uses doubles to represent timestamps in seconds.
167 hwState.timestamp = std::chrono::duration<stime_t>(std::chrono::nanoseconds(when)).count();
168 hwState.msc_timestamp =
169 std::chrono::duration<stime_t>(std::chrono::microseconds(mMscTimestamp)).count();
170
171 hwState.buttons_down = 0;
172 if (mCursorButtonAccumulator.isLeftPressed()) {
173 hwState.buttons_down |= GESTURES_BUTTON_LEFT;
174 }
175 if (mCursorButtonAccumulator.isMiddlePressed()) {
176 hwState.buttons_down |= GESTURES_BUTTON_MIDDLE;
177 }
178 if (mCursorButtonAccumulator.isRightPressed()) {
179 hwState.buttons_down |= GESTURES_BUTTON_RIGHT;
180 }
181 if (mCursorButtonAccumulator.isBackPressed() || mCursorButtonAccumulator.isSidePressed()) {
182 hwState.buttons_down |= GESTURES_BUTTON_BACK;
183 }
184 if (mCursorButtonAccumulator.isForwardPressed() || mCursorButtonAccumulator.isExtraPressed()) {
185 hwState.buttons_down |= GESTURES_BUTTON_FORWARD;
186 }
187
188 std::vector<FingerState> fingers;
189 for (size_t i = 0; i < mMotionAccumulator.getSlotCount(); i++) {
190 MultiTouchMotionAccumulator::Slot slot = mMotionAccumulator.getSlot(i);
191 if (slot.isInUse()) {
192 FingerState& fingerState = fingers.emplace_back();
193 fingerState = {};
194 fingerState.touch_major = slot.getTouchMajor();
195 fingerState.touch_minor = slot.getTouchMinor();
196 fingerState.width_major = slot.getToolMajor();
197 fingerState.width_minor = slot.getToolMinor();
198 fingerState.pressure = slot.getPressure();
199 fingerState.orientation = slot.getOrientation();
200 fingerState.position_x = slot.getX();
201 fingerState.position_y = slot.getY();
202 fingerState.tracking_id = slot.getTrackingId();
203 }
204 }
205 hwState.fingers = fingers.data();
206 hwState.finger_cnt = fingers.size();
207 hwState.touch_cnt = mTouchButtonAccumulator.getTouchCount();
208
Harry Cutts74235542022-11-24 15:52:53 +0000209 mProcessing = true;
Harry Cutts1f48a442022-11-15 17:38:36 +0000210 mGestureInterpreter->PushHardwareState(&hwState);
Harry Cutts74235542022-11-24 15:52:53 +0000211 mProcessing = false;
212
213 std::list<NotifyArgs> out = processGestures(when, readTime);
214
Harry Cutts1f48a442022-11-15 17:38:36 +0000215 mMotionAccumulator.finishSync();
216 mMscTimestamp = 0;
Harry Cutts74235542022-11-24 15:52:53 +0000217 return out;
218}
219
220void TouchpadInputMapper::consumeGesture(const Gesture* gesture) {
221 ALOGD("Gesture ready: %s", gesture->String().c_str());
222 if (!mProcessing) {
223 ALOGE("Received gesture outside of the normal processing flow; ignoring it.");
224 return;
225 }
226 mGesturesToProcess.push_back(*gesture);
227}
228
229std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) {
230 std::list<NotifyArgs> out = {};
231 for (Gesture& gesture : mGesturesToProcess) {
232 switch (gesture.type) {
233 case kGestureTypeMove:
234 out.push_back(handleMove(when, readTime, gesture));
235 break;
236 case kGestureTypeButtonsChange:
237 out += handleButtonsChange(when, readTime, gesture);
238 break;
239 default:
240 // TODO(b/251196347): handle more gesture types.
241 break;
242 }
243 }
244 mGesturesToProcess.clear();
245 return out;
246}
247
248NotifyArgs TouchpadInputMapper::handleMove(nsecs_t when, nsecs_t readTime, const Gesture& gesture) {
249 PointerProperties props;
250 props.clear();
251 props.id = 0;
252 props.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
253
254 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
255 mPointerController->move(gesture.details.move.dx, gesture.details.move.dy);
256 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
257 float xCursorPosition, yCursorPosition;
258 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
259
260 PointerCoords coords;
261 coords.clear();
262 coords.setAxisValue(AMOTION_EVENT_AXIS_X, xCursorPosition);
263 coords.setAxisValue(AMOTION_EVENT_AXIS_Y, yCursorPosition);
264 coords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, gesture.details.move.dx);
265 coords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, gesture.details.move.dy);
266 const bool down = isPointerDown(mButtonState);
267 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
268
269 const int32_t action = down ? AMOTION_EVENT_ACTION_MOVE : AMOTION_EVENT_ACTION_HOVER_MOVE;
270 return makeMotionArgs(when, readTime, action, /* actionButton= */ 0, mButtonState,
271 /* pointerCount= */ 1, &props, &coords, xCursorPosition, yCursorPosition);
272}
273
274std::list<NotifyArgs> TouchpadInputMapper::handleButtonsChange(nsecs_t when, nsecs_t readTime,
275 const Gesture& gesture) {
276 std::list<NotifyArgs> out = {};
277
278 mPointerController->setPresentation(PointerControllerInterface::Presentation::POINTER);
279 mPointerController->unfade(PointerControllerInterface::Transition::IMMEDIATE);
280
281 PointerProperties props;
282 props.clear();
283 props.id = 0;
284 props.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
285
286 float xCursorPosition, yCursorPosition;
287 mPointerController->getPosition(&xCursorPosition, &yCursorPosition);
288
289 PointerCoords coords;
290 coords.clear();
291 coords.setAxisValue(AMOTION_EVENT_AXIS_X, xCursorPosition);
292 coords.setAxisValue(AMOTION_EVENT_AXIS_Y, yCursorPosition);
293 coords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X, 0);
294 coords.setAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y, 0);
295 const uint32_t buttonsPressed = gesture.details.buttons.down;
296 bool pointerDown = isPointerDown(mButtonState) ||
297 buttonsPressed &
298 (GESTURES_BUTTON_LEFT | GESTURES_BUTTON_MIDDLE | GESTURES_BUTTON_RIGHT);
299 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pointerDown ? 1.0f : 0.0f);
300
301 uint32_t newButtonState = mButtonState;
302 std::list<NotifyArgs> pressEvents = {};
303 for (uint32_t button = 1; button <= GESTURES_BUTTON_FORWARD; button <<= 1) {
304 if (buttonsPressed & button) {
305 uint32_t actionButton = gesturesButtonToMotionEventButton(button);
306 newButtonState |= actionButton;
307 pressEvents.push_back(makeMotionArgs(when, readTime, AMOTION_EVENT_ACTION_BUTTON_PRESS,
308 actionButton, newButtonState,
309 /* pointerCount= */ 1, &props, &coords,
310 xCursorPosition, yCursorPosition));
311 }
312 }
313 if (!isPointerDown(mButtonState) && isPointerDown(newButtonState)) {
314 mDownTime = when;
315 out.push_back(makeMotionArgs(when, readTime, AMOTION_EVENT_ACTION_DOWN,
316 /* actionButton= */ 0, newButtonState, /* pointerCount= */ 1,
317 &props, &coords, xCursorPosition, yCursorPosition));
318 }
319 out.splice(out.end(), pressEvents);
320
321 // The same button may be in both down and up in the same gesture, in which case we should treat
322 // it as having gone down and then up. So, we treat a single button change gesture as two state
323 // changes: a set of buttons going down, followed by a set of buttons going up.
324 mButtonState = newButtonState;
325
326 const uint32_t buttonsReleased = gesture.details.buttons.up;
327 for (uint32_t button = 1; button <= GESTURES_BUTTON_FORWARD; button <<= 1) {
328 if (buttonsReleased & button) {
329 uint32_t actionButton = gesturesButtonToMotionEventButton(button);
330 newButtonState &= ~actionButton;
331 out.push_back(makeMotionArgs(when, readTime, AMOTION_EVENT_ACTION_BUTTON_RELEASE,
332 actionButton, newButtonState, /* pointerCount= */ 1,
333 &props, &coords, xCursorPosition, yCursorPosition));
334 }
335 }
336 if (isPointerDown(mButtonState) && !isPointerDown(newButtonState)) {
337 coords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 0.0f);
338 out.push_back(makeMotionArgs(when, readTime, AMOTION_EVENT_ACTION_UP, /* actionButton= */ 0,
339 newButtonState, /* pointerCount= */ 1, &props, &coords,
340 xCursorPosition, yCursorPosition));
341 }
342 mButtonState = newButtonState;
343 return out;
344}
345
346NotifyMotionArgs TouchpadInputMapper::makeMotionArgs(nsecs_t when, nsecs_t readTime, int32_t action,
347 int32_t actionButton, int32_t buttonState,
348 uint32_t pointerCount,
349 const PointerProperties* pointerProperties,
350 const PointerCoords* pointerCoords,
351 float xCursorPosition, float yCursorPosition) {
352 // TODO(b/260226362): consider what the appropriate source for these events is.
353 const uint32_t source = AINPUT_SOURCE_MOUSE;
354
355 return NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), source,
356 mPointerController->getDisplayId(), /* policyFlags= */ 0, action,
357 /* actionButton= */ actionButton, /* flags= */ 0,
358 getContext()->getGlobalMetaState(), buttonState,
359 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount,
360 pointerProperties, pointerCoords,
361 /* xPrecision= */ 1.0f, /* yPrecision= */ 1.0f, xCursorPosition,
362 yCursorPosition, /* downTime= */ mDownTime, /* videoFrames= */ {});
Harry Cutts1f48a442022-11-15 17:38:36 +0000363}
364
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000365} // namespace android