blob: c563dbaad02b61d80b53f787c0a995ec8b9481d8 [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 Cutts74235542022-11-24 15:52:53 +000019#include <android/input.h>
Harry Cutts4fb941a2022-12-14 19:14:04 +000020#include <linux/input-event-codes.h>
Harry Cutts74235542022-11-24 15:52:53 +000021#include <log/log_main.h>
22#include "TouchCursorInputMapperCommon.h"
Harry Cutts79cc9fa2022-10-28 15:32:39 +000023#include "TouchpadInputMapper.h"
24
25namespace android {
26
Harry Cutts1f48a442022-11-15 17:38:36 +000027namespace {
28
29short getMaxTouchCount(const InputDeviceContext& context) {
Harry Cuttsb2552152022-12-13 17:18:09 +000030 if (context.hasScanCode(BTN_TOOL_QUINTTAP)) return 5;
31 if (context.hasScanCode(BTN_TOOL_QUADTAP)) return 4;
32 if (context.hasScanCode(BTN_TOOL_TRIPLETAP)) return 3;
33 if (context.hasScanCode(BTN_TOOL_DOUBLETAP)) return 2;
34 if (context.hasScanCode(BTN_TOOL_FINGER)) return 1;
Harry Cutts1f48a442022-11-15 17:38:36 +000035 return 0;
36}
37
38HardwareProperties createHardwareProperties(const InputDeviceContext& context) {
39 HardwareProperties props;
40 RawAbsoluteAxisInfo absMtPositionX;
41 context.getAbsoluteAxisInfo(ABS_MT_POSITION_X, &absMtPositionX);
42 props.left = absMtPositionX.minValue;
43 props.right = absMtPositionX.maxValue;
44 props.res_x = absMtPositionX.resolution;
45
46 RawAbsoluteAxisInfo absMtPositionY;
47 context.getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &absMtPositionY);
48 props.top = absMtPositionY.minValue;
49 props.bottom = absMtPositionY.maxValue;
50 props.res_y = absMtPositionY.resolution;
51
52 RawAbsoluteAxisInfo absMtOrientation;
53 context.getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &absMtOrientation);
54 props.orientation_minimum = absMtOrientation.minValue;
55 props.orientation_maximum = absMtOrientation.maxValue;
56
57 RawAbsoluteAxisInfo absMtSlot;
58 context.getAbsoluteAxisInfo(ABS_MT_SLOT, &absMtSlot);
59 props.max_finger_cnt = absMtSlot.maxValue - absMtSlot.minValue + 1;
60 props.max_touch_cnt = getMaxTouchCount(context);
61
62 // T5R2 ("Track 5, Report 2") is a feature of some old Synaptics touchpads that could track 5
63 // fingers but only report the coordinates of 2 of them. We don't know of any external touchpads
64 // that did this, so assume false.
65 props.supports_t5r2 = false;
66
67 props.support_semi_mt = context.hasInputProperty(INPUT_PROP_SEMI_MT);
68 props.is_button_pad = context.hasInputProperty(INPUT_PROP_BUTTONPAD);
69
70 // Mouse-only properties, which will always be false.
71 props.has_wheel = false;
72 props.wheel_is_hi_res = false;
73
74 // Linux Kernel haptic touchpad support isn't merged yet, so for now assume that no touchpads
75 // are haptic.
76 props.is_haptic_pad = false;
77 return props;
78}
79
Harry Cutts74235542022-11-24 15:52:53 +000080void gestureInterpreterCallback(void* clientData, const Gesture* gesture) {
81 TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData);
82 mapper->consumeGesture(gesture);
83}
84
Harry Cutts1f48a442022-11-15 17:38:36 +000085} // namespace
86
Harry Cutts79cc9fa2022-10-28 15:32:39 +000087TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext)
Harry Cutts1f48a442022-11-15 17:38:36 +000088 : InputMapper(deviceContext),
89 mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter),
Harry Cutts74235542022-11-24 15:52:53 +000090 mPointerController(getContext()->getPointerController(getDeviceId())),
Harry Cutts4fb941a2022-12-14 19:14:04 +000091 mStateConverter(deviceContext),
92 mGestureConverter(*getContext(), getDeviceId()) {
Harry Cutts1f48a442022-11-15 17:38:36 +000093 mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD);
94 mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext));
Harry Cutts74235542022-11-24 15:52:53 +000095 // Even though we don't explicitly delete copy/move semantics, it's safe to
96 // give away a pointer to TouchpadInputMapper here because
97 // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and
98 // 2) TouchpadInputMapper is stored as a unique_ptr and not moved.
99 mGestureInterpreter->SetCallback(gestureInterpreterCallback, this);
Harry Cutts1f48a442022-11-15 17:38:36 +0000100 // TODO(b/251196347): set a property provider, so we can change gesture properties.
101 // TODO(b/251196347): set a timer provider, so the library can use timers.
Harry Cutts1f48a442022-11-15 17:38:36 +0000102}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000103
Harry Cutts74235542022-11-24 15:52:53 +0000104TouchpadInputMapper::~TouchpadInputMapper() {
105 if (mPointerController != nullptr) {
106 mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE);
107 }
108}
109
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000110uint32_t TouchpadInputMapper::getSources() const {
111 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
112}
113
Harry Cutts1f48a442022-11-15 17:38:36 +0000114std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000115 mStateConverter.reset();
Harry Cutts4fb941a2022-12-14 19:14:04 +0000116 mGestureConverter.reset();
Harry Cutts1f48a442022-11-15 17:38:36 +0000117 return InputMapper::reset(when);
118}
119
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000120std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) {
Harry Cutts47db1c72022-12-13 19:20:47 +0000121 std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent);
122 if (state) {
123 return sendHardwareState(rawEvent->when, rawEvent->readTime, *state);
124 } else {
125 return {};
Harry Cutts1f48a442022-11-15 17:38:36 +0000126 }
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000127}
128
Harry Cutts47db1c72022-12-13 19:20:47 +0000129std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime,
130 SelfContainedHardwareState schs) {
Harry Cutts74235542022-11-24 15:52:53 +0000131 mProcessing = true;
Harry Cutts47db1c72022-12-13 19:20:47 +0000132 mGestureInterpreter->PushHardwareState(&schs.state);
Harry Cutts74235542022-11-24 15:52:53 +0000133 mProcessing = false;
134
Harry Cutts47db1c72022-12-13 19:20:47 +0000135 return processGestures(when, readTime);
Harry Cutts74235542022-11-24 15:52:53 +0000136}
137
138void TouchpadInputMapper::consumeGesture(const Gesture* gesture) {
139 ALOGD("Gesture ready: %s", gesture->String().c_str());
140 if (!mProcessing) {
141 ALOGE("Received gesture outside of the normal processing flow; ignoring it.");
142 return;
143 }
144 mGesturesToProcess.push_back(*gesture);
145}
146
147std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) {
148 std::list<NotifyArgs> out = {};
149 for (Gesture& gesture : mGesturesToProcess) {
Harry Cutts4fb941a2022-12-14 19:14:04 +0000150 out += mGestureConverter.handleGesture(when, readTime, gesture);
Harry Cutts74235542022-11-24 15:52:53 +0000151 }
152 mGesturesToProcess.clear();
153 return out;
154}
155
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000156} // namespace android