blob: 89ba07ea9b43f16494a60564599734aea43bcf6d [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 Cuttsedf6ce72023-01-04 12:15:53 +000019#include <optional>
20
Harry Cutts74235542022-11-24 15:52:53 +000021#include <android/input.h>
Harry Cutts4fb941a2022-12-14 19:14:04 +000022#include <linux/input-event-codes.h>
Harry Cutts74235542022-11-24 15:52:53 +000023#include <log/log_main.h>
24#include "TouchCursorInputMapperCommon.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000025#include "TouchpadInputMapper.h"
Harry Cuttsedf6ce72023-01-04 12:15:53 +000026#include "ui/Rotation.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000027
28namespace android {
29
Harry Cutts1f48a442022-11-15 17:38:36 +000030namespace {
31
32short getMaxTouchCount(const InputDeviceContext& context) {
Harry Cuttsb2552152022-12-13 17:18:09 +000033 if (context.hasScanCode(BTN_TOOL_QUINTTAP)) return 5;
34 if (context.hasScanCode(BTN_TOOL_QUADTAP)) return 4;
35 if (context.hasScanCode(BTN_TOOL_TRIPLETAP)) return 3;
36 if (context.hasScanCode(BTN_TOOL_DOUBLETAP)) return 2;
37 if (context.hasScanCode(BTN_TOOL_FINGER)) return 1;
Harry Cutts1f48a442022-11-15 17:38:36 +000038 return 0;
39}
40
41HardwareProperties createHardwareProperties(const InputDeviceContext& context) {
42 HardwareProperties props;
43 RawAbsoluteAxisInfo absMtPositionX;
44 context.getAbsoluteAxisInfo(ABS_MT_POSITION_X, &absMtPositionX);
45 props.left = absMtPositionX.minValue;
46 props.right = absMtPositionX.maxValue;
47 props.res_x = absMtPositionX.resolution;
48
49 RawAbsoluteAxisInfo absMtPositionY;
50 context.getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &absMtPositionY);
51 props.top = absMtPositionY.minValue;
52 props.bottom = absMtPositionY.maxValue;
53 props.res_y = absMtPositionY.resolution;
54
55 RawAbsoluteAxisInfo absMtOrientation;
56 context.getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &absMtOrientation);
57 props.orientation_minimum = absMtOrientation.minValue;
58 props.orientation_maximum = absMtOrientation.maxValue;
59
60 RawAbsoluteAxisInfo absMtSlot;
61 context.getAbsoluteAxisInfo(ABS_MT_SLOT, &absMtSlot);
62 props.max_finger_cnt = absMtSlot.maxValue - absMtSlot.minValue + 1;
63 props.max_touch_cnt = getMaxTouchCount(context);
64
65 // T5R2 ("Track 5, Report 2") is a feature of some old Synaptics touchpads that could track 5
66 // fingers but only report the coordinates of 2 of them. We don't know of any external touchpads
67 // that did this, so assume false.
68 props.supports_t5r2 = false;
69
70 props.support_semi_mt = context.hasInputProperty(INPUT_PROP_SEMI_MT);
71 props.is_button_pad = context.hasInputProperty(INPUT_PROP_BUTTONPAD);
72
73 // Mouse-only properties, which will always be false.
74 props.has_wheel = false;
75 props.wheel_is_hi_res = false;
76
77 // Linux Kernel haptic touchpad support isn't merged yet, so for now assume that no touchpads
78 // are haptic.
79 props.is_haptic_pad = false;
80 return props;
81}
82
Harry Cutts74235542022-11-24 15:52:53 +000083void gestureInterpreterCallback(void* clientData, const Gesture* gesture) {
84 TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData);
85 mapper->consumeGesture(gesture);
86}
87
Harry Cutts1f48a442022-11-15 17:38:36 +000088} // namespace
89
Harry Cutts79cc9fa2022-10-28 15:32:39 +000090TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext)
Harry Cutts1f48a442022-11-15 17:38:36 +000091 : InputMapper(deviceContext),
92 mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter),
Harry Cutts74235542022-11-24 15:52:53 +000093 mPointerController(getContext()->getPointerController(getDeviceId())),
Harry Cutts4fb941a2022-12-14 19:14:04 +000094 mStateConverter(deviceContext),
Harry Cuttsc5748d12022-12-02 17:30:18 +000095 mGestureConverter(*getContext(), deviceContext, getDeviceId()) {
Harry Cutts1f48a442022-11-15 17:38:36 +000096 mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD);
97 mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext));
Harry Cutts74235542022-11-24 15:52:53 +000098 // Even though we don't explicitly delete copy/move semantics, it's safe to
Harry Cutts1b217912023-01-03 17:13:19 +000099 // give away pointers to TouchpadInputMapper and its members here because
Harry Cutts74235542022-11-24 15:52:53 +0000100 // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and
101 // 2) TouchpadInputMapper is stored as a unique_ptr and not moved.
Harry Cutts1b217912023-01-03 17:13:19 +0000102 mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider),
103 &mPropertyProvider);
Harry Cutts74235542022-11-24 15:52:53 +0000104 mGestureInterpreter->SetCallback(gestureInterpreterCallback, this);
Harry Cutts1f48a442022-11-15 17:38:36 +0000105 // TODO(b/251196347): set a timer provider, so the library can use timers.
Harry Cutts1f48a442022-11-15 17:38:36 +0000106}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000107
Harry Cutts74235542022-11-24 15:52:53 +0000108TouchpadInputMapper::~TouchpadInputMapper() {
109 if (mPointerController != nullptr) {
110 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
111 }
Harry Cutts1b217912023-01-03 17:13:19 +0000112
113 // The gesture interpreter's destructor will call its property provider's free function for all
114 // gesture properties, in this case calling PropertyProvider::freeProperty using a raw pointer
115 // to mPropertyProvider. Depending on the declaration order in TouchpadInputMapper.h, this may
116 // happen after mPropertyProvider has been destructed, causing allocation errors. Depending on
117 // declaration order to avoid crashes seems rather fragile, so explicitly clear the property
118 // provider here to ensure all the freeProperty calls happen before mPropertyProvider is
119 // destructed.
120 mGestureInterpreter->SetPropProvider(nullptr, nullptr);
Harry Cutts74235542022-11-24 15:52:53 +0000121}
122
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000123uint32_t TouchpadInputMapper::getSources() const {
124 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
125}
126
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000127std::list<NotifyArgs> TouchpadInputMapper::configure(nsecs_t when,
128 const InputReaderConfiguration* config,
129 uint32_t changes) {
130 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
131 std::optional<int32_t> displayId = mPointerController->getDisplayId();
132 ui::Rotation orientation = ui::ROTATION_0;
133 if (displayId.has_value()) {
134 if (auto viewport = config->getDisplayViewportById(*displayId); viewport) {
135 orientation = getInverseRotation(viewport->orientation);
136 }
137 }
138 mGestureConverter.setOrientation(orientation);
139 }
140 return {};
141}
142
Harry Cutts1f48a442022-11-15 17:38:36 +0000143std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000144 mStateConverter.reset();
Harry Cutts4fb941a2022-12-14 19:14:04 +0000145 mGestureConverter.reset();
Harry Cutts1f48a442022-11-15 17:38:36 +0000146 return InputMapper::reset(when);
147}
148
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000149std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000150 std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent);
151 if (state) {
152 return sendHardwareState(rawEvent->when, rawEvent->readTime, *state);
153 } else {
154 return {};
Harry Cutts1f48a442022-11-15 17:38:36 +0000155 }
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000156}
157
Harry Cutts47db1c72022-12-13 19:20:47 +0000158std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime,
159 SelfContainedHardwareState schs) {
Harry Cutts74235542022-11-24 15:52:53 +0000160 mProcessing = true;
Harry Cutts47db1c72022-12-13 19:20:47 +0000161 mGestureInterpreter->PushHardwareState(&schs.state);
Harry Cutts74235542022-11-24 15:52:53 +0000162 mProcessing = false;
163
Harry Cutts47db1c72022-12-13 19:20:47 +0000164 return processGestures(when, readTime);
Harry Cutts74235542022-11-24 15:52:53 +0000165}
166
167void TouchpadInputMapper::consumeGesture(const Gesture* gesture) {
168 ALOGD("Gesture ready: %s", gesture->String().c_str());
169 if (!mProcessing) {
170 ALOGE("Received gesture outside of the normal processing flow; ignoring it.");
171 return;
172 }
173 mGesturesToProcess.push_back(*gesture);
174}
175
176std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) {
177 std::list<NotifyArgs> out = {};
178 for (Gesture& gesture : mGesturesToProcess) {
Harry Cutts4fb941a2022-12-14 19:14:04 +0000179 out += mGestureConverter.handleGesture(when, readTime, gesture);
Harry Cutts74235542022-11-24 15:52:53 +0000180 }
181 mGesturesToProcess.clear();
182 return out;
183}
184
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000185} // namespace android