blob: b6313a1049f941affa39d26f9e2d9fa42ed6cc2a [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 Cuttsea73eaa2023-01-16 17:55:46 +000022#include <input/PrintTools.h>
Harry Cutts4fb941a2022-12-14 19:14:04 +000023#include <linux/input-event-codes.h>
Harry Cutts74235542022-11-24 15:52:53 +000024#include <log/log_main.h>
25#include "TouchCursorInputMapperCommon.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000026#include "TouchpadInputMapper.h"
Harry Cuttsedf6ce72023-01-04 12:15:53 +000027#include "ui/Rotation.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000028
29namespace android {
30
Harry Cutts1f48a442022-11-15 17:38:36 +000031namespace {
32
33short getMaxTouchCount(const InputDeviceContext& context) {
Harry Cuttsb2552152022-12-13 17:18:09 +000034 if (context.hasScanCode(BTN_TOOL_QUINTTAP)) return 5;
35 if (context.hasScanCode(BTN_TOOL_QUADTAP)) return 4;
36 if (context.hasScanCode(BTN_TOOL_TRIPLETAP)) return 3;
37 if (context.hasScanCode(BTN_TOOL_DOUBLETAP)) return 2;
38 if (context.hasScanCode(BTN_TOOL_FINGER)) return 1;
Harry Cutts1f48a442022-11-15 17:38:36 +000039 return 0;
40}
41
42HardwareProperties createHardwareProperties(const InputDeviceContext& context) {
43 HardwareProperties props;
44 RawAbsoluteAxisInfo absMtPositionX;
45 context.getAbsoluteAxisInfo(ABS_MT_POSITION_X, &absMtPositionX);
46 props.left = absMtPositionX.minValue;
47 props.right = absMtPositionX.maxValue;
48 props.res_x = absMtPositionX.resolution;
49
50 RawAbsoluteAxisInfo absMtPositionY;
51 context.getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &absMtPositionY);
52 props.top = absMtPositionY.minValue;
53 props.bottom = absMtPositionY.maxValue;
54 props.res_y = absMtPositionY.resolution;
55
56 RawAbsoluteAxisInfo absMtOrientation;
57 context.getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &absMtOrientation);
58 props.orientation_minimum = absMtOrientation.minValue;
59 props.orientation_maximum = absMtOrientation.maxValue;
60
61 RawAbsoluteAxisInfo absMtSlot;
62 context.getAbsoluteAxisInfo(ABS_MT_SLOT, &absMtSlot);
63 props.max_finger_cnt = absMtSlot.maxValue - absMtSlot.minValue + 1;
64 props.max_touch_cnt = getMaxTouchCount(context);
65
66 // T5R2 ("Track 5, Report 2") is a feature of some old Synaptics touchpads that could track 5
67 // fingers but only report the coordinates of 2 of them. We don't know of any external touchpads
68 // that did this, so assume false.
69 props.supports_t5r2 = false;
70
71 props.support_semi_mt = context.hasInputProperty(INPUT_PROP_SEMI_MT);
72 props.is_button_pad = context.hasInputProperty(INPUT_PROP_BUTTONPAD);
73
74 // Mouse-only properties, which will always be false.
75 props.has_wheel = false;
76 props.wheel_is_hi_res = false;
77
78 // Linux Kernel haptic touchpad support isn't merged yet, so for now assume that no touchpads
79 // are haptic.
80 props.is_haptic_pad = false;
81 return props;
82}
83
Harry Cutts74235542022-11-24 15:52:53 +000084void gestureInterpreterCallback(void* clientData, const Gesture* gesture) {
85 TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData);
86 mapper->consumeGesture(gesture);
87}
88
Harry Cutts1f48a442022-11-15 17:38:36 +000089} // namespace
90
Harry Cutts79cc9fa2022-10-28 15:32:39 +000091TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext)
Harry Cutts1f48a442022-11-15 17:38:36 +000092 : InputMapper(deviceContext),
93 mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter),
Harry Cutts74235542022-11-24 15:52:53 +000094 mPointerController(getContext()->getPointerController(getDeviceId())),
Harry Cutts4fb941a2022-12-14 19:14:04 +000095 mStateConverter(deviceContext),
Harry Cuttsc5748d12022-12-02 17:30:18 +000096 mGestureConverter(*getContext(), deviceContext, getDeviceId()) {
Harry Cutts1f48a442022-11-15 17:38:36 +000097 mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD);
98 mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext));
Harry Cutts74235542022-11-24 15:52:53 +000099 // Even though we don't explicitly delete copy/move semantics, it's safe to
Harry Cutts1b217912023-01-03 17:13:19 +0000100 // give away pointers to TouchpadInputMapper and its members here because
Harry Cutts74235542022-11-24 15:52:53 +0000101 // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and
102 // 2) TouchpadInputMapper is stored as a unique_ptr and not moved.
Harry Cutts1b217912023-01-03 17:13:19 +0000103 mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider),
104 &mPropertyProvider);
Harry Cutts74235542022-11-24 15:52:53 +0000105 mGestureInterpreter->SetCallback(gestureInterpreterCallback, this);
Harry Cutts1f48a442022-11-15 17:38:36 +0000106 // TODO(b/251196347): set a timer provider, so the library can use timers.
Harry Cutts1f48a442022-11-15 17:38:36 +0000107}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000108
Harry Cutts74235542022-11-24 15:52:53 +0000109TouchpadInputMapper::~TouchpadInputMapper() {
110 if (mPointerController != nullptr) {
111 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
112 }
Harry Cutts1b217912023-01-03 17:13:19 +0000113
114 // The gesture interpreter's destructor will call its property provider's free function for all
115 // gesture properties, in this case calling PropertyProvider::freeProperty using a raw pointer
116 // to mPropertyProvider. Depending on the declaration order in TouchpadInputMapper.h, this may
117 // happen after mPropertyProvider has been destructed, causing allocation errors. Depending on
118 // declaration order to avoid crashes seems rather fragile, so explicitly clear the property
119 // provider here to ensure all the freeProperty calls happen before mPropertyProvider is
120 // destructed.
121 mGestureInterpreter->SetPropProvider(nullptr, nullptr);
Harry Cutts74235542022-11-24 15:52:53 +0000122}
123
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000124uint32_t TouchpadInputMapper::getSources() const {
125 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
126}
127
Harry Cuttsea73eaa2023-01-16 17:55:46 +0000128void TouchpadInputMapper::dump(std::string& dump) {
129 dump += INDENT2 "Touchpad Input Mapper:\n";
130 dump += INDENT3 "Gesture converter:\n";
131 dump += addLinePrefix(mGestureConverter.dump(), INDENT4);
132 dump += INDENT3 "Gesture properties:\n";
133 dump += addLinePrefix(mPropertyProvider.dump(), INDENT4);
134}
135
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000136std::list<NotifyArgs> TouchpadInputMapper::configure(nsecs_t when,
137 const InputReaderConfiguration* config,
138 uint32_t changes) {
139 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
140 std::optional<int32_t> displayId = mPointerController->getDisplayId();
141 ui::Rotation orientation = ui::ROTATION_0;
142 if (displayId.has_value()) {
143 if (auto viewport = config->getDisplayViewportById(*displayId); viewport) {
144 orientation = getInverseRotation(viewport->orientation);
145 }
146 }
147 mGestureConverter.setOrientation(orientation);
148 }
149 return {};
150}
151
Harry Cutts1f48a442022-11-15 17:38:36 +0000152std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000153 mStateConverter.reset();
Harry Cutts4fb941a2022-12-14 19:14:04 +0000154 mGestureConverter.reset();
Harry Cutts1f48a442022-11-15 17:38:36 +0000155 return InputMapper::reset(when);
156}
157
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000158std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000159 std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent);
160 if (state) {
161 return sendHardwareState(rawEvent->when, rawEvent->readTime, *state);
162 } else {
163 return {};
Harry Cutts1f48a442022-11-15 17:38:36 +0000164 }
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000165}
166
Harry Cutts47db1c72022-12-13 19:20:47 +0000167std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime,
168 SelfContainedHardwareState schs) {
Harry Cutts74235542022-11-24 15:52:53 +0000169 mProcessing = true;
Harry Cutts47db1c72022-12-13 19:20:47 +0000170 mGestureInterpreter->PushHardwareState(&schs.state);
Harry Cutts74235542022-11-24 15:52:53 +0000171 mProcessing = false;
172
Harry Cutts47db1c72022-12-13 19:20:47 +0000173 return processGestures(when, readTime);
Harry Cutts74235542022-11-24 15:52:53 +0000174}
175
176void TouchpadInputMapper::consumeGesture(const Gesture* gesture) {
177 ALOGD("Gesture ready: %s", gesture->String().c_str());
178 if (!mProcessing) {
179 ALOGE("Received gesture outside of the normal processing flow; ignoring it.");
180 return;
181 }
182 mGesturesToProcess.push_back(*gesture);
183}
184
185std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) {
186 std::list<NotifyArgs> out = {};
187 for (Gesture& gesture : mGesturesToProcess) {
Harry Cutts4fb941a2022-12-14 19:14:04 +0000188 out += mGestureConverter.handleGesture(when, readTime, gesture);
Harry Cutts74235542022-11-24 15:52:53 +0000189 }
190 mGesturesToProcess.clear();
191 return out;
192}
193
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000194} // namespace android