blob: 23d7fdf91d8386f11ffe9befdec2271f65abb9db [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),
95 mGestureConverter(*getContext(), 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
99 // give away a pointer to TouchpadInputMapper here because
100 // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and
101 // 2) TouchpadInputMapper is stored as a unique_ptr and not moved.
102 mGestureInterpreter->SetCallback(gestureInterpreterCallback, this);
Harry Cutts1f48a442022-11-15 17:38:36 +0000103 // TODO(b/251196347): set a property provider, so we can change gesture properties.
104 // TODO(b/251196347): set a timer provider, so the library can use timers.
Harry Cutts1f48a442022-11-15 17:38:36 +0000105}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000106
Harry Cutts74235542022-11-24 15:52:53 +0000107TouchpadInputMapper::~TouchpadInputMapper() {
108 if (mPointerController != nullptr) {
109 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
110 }
111}
112
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000113uint32_t TouchpadInputMapper::getSources() const {
114 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
115}
116
Harry Cuttsedf6ce72023-01-04 12:15:53 +0000117std::list<NotifyArgs> TouchpadInputMapper::configure(nsecs_t when,
118 const InputReaderConfiguration* config,
119 uint32_t changes) {
120 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
121 std::optional<int32_t> displayId = mPointerController->getDisplayId();
122 ui::Rotation orientation = ui::ROTATION_0;
123 if (displayId.has_value()) {
124 if (auto viewport = config->getDisplayViewportById(*displayId); viewport) {
125 orientation = getInverseRotation(viewport->orientation);
126 }
127 }
128 mGestureConverter.setOrientation(orientation);
129 }
130 return {};
131}
132
Harry Cutts1f48a442022-11-15 17:38:36 +0000133std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000134 mStateConverter.reset();
Harry Cutts4fb941a2022-12-14 19:14:04 +0000135 mGestureConverter.reset();
Harry Cutts1f48a442022-11-15 17:38:36 +0000136 return InputMapper::reset(when);
137}
138
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000139std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000140 std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent);
141 if (state) {
142 return sendHardwareState(rawEvent->when, rawEvent->readTime, *state);
143 } else {
144 return {};
Harry Cutts1f48a442022-11-15 17:38:36 +0000145 }
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000146}
147
Harry Cutts47db1c72022-12-13 19:20:47 +0000148std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime,
149 SelfContainedHardwareState schs) {
Harry Cutts74235542022-11-24 15:52:53 +0000150 mProcessing = true;
Harry Cutts47db1c72022-12-13 19:20:47 +0000151 mGestureInterpreter->PushHardwareState(&schs.state);
Harry Cutts74235542022-11-24 15:52:53 +0000152 mProcessing = false;
153
Harry Cutts47db1c72022-12-13 19:20:47 +0000154 return processGestures(when, readTime);
Harry Cutts74235542022-11-24 15:52:53 +0000155}
156
157void TouchpadInputMapper::consumeGesture(const Gesture* gesture) {
158 ALOGD("Gesture ready: %s", gesture->String().c_str());
159 if (!mProcessing) {
160 ALOGE("Received gesture outside of the normal processing flow; ignoring it.");
161 return;
162 }
163 mGesturesToProcess.push_back(*gesture);
164}
165
166std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) {
167 std::list<NotifyArgs> out = {};
168 for (Gesture& gesture : mGesturesToProcess) {
Harry Cutts4fb941a2022-12-14 19:14:04 +0000169 out += mGestureConverter.handleGesture(when, readTime, gesture);
Harry Cutts74235542022-11-24 15:52:53 +0000170 }
171 mGesturesToProcess.clear();
172 return out;
173}
174
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000175} // namespace android