blob: 8c5bce7d135d062da40098da3143fefcde612f26 [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 <log/log_main.h>
20#include <chrono>
Harry Cutts79cc9fa2022-10-28 15:32:39 +000021#include "TouchpadInputMapper.h"
22
23namespace android {
24
Harry Cutts1f48a442022-11-15 17:38:36 +000025namespace {
26
27short getMaxTouchCount(const InputDeviceContext& context) {
28 if (context.hasKeyCode(BTN_TOOL_QUINTTAP)) return 5;
29 if (context.hasKeyCode(BTN_TOOL_QUADTAP)) return 4;
30 if (context.hasKeyCode(BTN_TOOL_TRIPLETAP)) return 3;
31 if (context.hasKeyCode(BTN_TOOL_DOUBLETAP)) return 2;
32 if (context.hasKeyCode(BTN_TOOL_FINGER)) return 1;
33 return 0;
34}
35
36HardwareProperties createHardwareProperties(const InputDeviceContext& context) {
37 HardwareProperties props;
38 RawAbsoluteAxisInfo absMtPositionX;
39 context.getAbsoluteAxisInfo(ABS_MT_POSITION_X, &absMtPositionX);
40 props.left = absMtPositionX.minValue;
41 props.right = absMtPositionX.maxValue;
42 props.res_x = absMtPositionX.resolution;
43
44 RawAbsoluteAxisInfo absMtPositionY;
45 context.getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &absMtPositionY);
46 props.top = absMtPositionY.minValue;
47 props.bottom = absMtPositionY.maxValue;
48 props.res_y = absMtPositionY.resolution;
49
50 RawAbsoluteAxisInfo absMtOrientation;
51 context.getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &absMtOrientation);
52 props.orientation_minimum = absMtOrientation.minValue;
53 props.orientation_maximum = absMtOrientation.maxValue;
54
55 RawAbsoluteAxisInfo absMtSlot;
56 context.getAbsoluteAxisInfo(ABS_MT_SLOT, &absMtSlot);
57 props.max_finger_cnt = absMtSlot.maxValue - absMtSlot.minValue + 1;
58 props.max_touch_cnt = getMaxTouchCount(context);
59
60 // T5R2 ("Track 5, Report 2") is a feature of some old Synaptics touchpads that could track 5
61 // fingers but only report the coordinates of 2 of them. We don't know of any external touchpads
62 // that did this, so assume false.
63 props.supports_t5r2 = false;
64
65 props.support_semi_mt = context.hasInputProperty(INPUT_PROP_SEMI_MT);
66 props.is_button_pad = context.hasInputProperty(INPUT_PROP_BUTTONPAD);
67
68 // Mouse-only properties, which will always be false.
69 props.has_wheel = false;
70 props.wheel_is_hi_res = false;
71
72 // Linux Kernel haptic touchpad support isn't merged yet, so for now assume that no touchpads
73 // are haptic.
74 props.is_haptic_pad = false;
75 return props;
76}
77
78void gestureInterpreterCallback(void* clientData, const struct Gesture* gesture) {
79 // TODO(b/251196347): turn the gesture into a NotifyArgs and dispatch it.
80 ALOGD("Gesture ready: %s", gesture->String().c_str());
81}
82
83} // namespace
84
Harry Cutts79cc9fa2022-10-28 15:32:39 +000085TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext)
Harry Cutts1f48a442022-11-15 17:38:36 +000086 : InputMapper(deviceContext),
87 mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter),
88 mTouchButtonAccumulator(deviceContext) {
89 mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD);
90 mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext));
91 mGestureInterpreter->SetCallback(gestureInterpreterCallback, nullptr);
92 // TODO(b/251196347): set a property provider, so we can change gesture properties.
93 // TODO(b/251196347): set a timer provider, so the library can use timers.
94
95 RawAbsoluteAxisInfo slotAxisInfo;
96 getAbsoluteAxisInfo(ABS_MT_SLOT, &slotAxisInfo);
97 if (!slotAxisInfo.valid || slotAxisInfo.maxValue <= 0) {
98 ALOGW("Touchpad \"%s\" doesn't have a valid ABS_MT_SLOT axis, and probably won't work "
99 "properly.",
100 getDeviceName().c_str());
101 }
102 mMotionAccumulator.configure(getDeviceContext(), slotAxisInfo.maxValue + 1, true);
103 mTouchButtonAccumulator.configure();
104}
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000105
106uint32_t TouchpadInputMapper::getSources() const {
107 return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD;
108}
109
Harry Cutts1f48a442022-11-15 17:38:36 +0000110std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) {
111 mCursorButtonAccumulator.reset(getDeviceContext());
112 mTouchButtonAccumulator.reset();
113 mMscTimestamp = 0;
114 return InputMapper::reset(when);
115}
116
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000117std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) {
Harry Cutts1f48a442022-11-15 17:38:36 +0000118 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
119 sync(rawEvent->when);
120 }
121 if (rawEvent->type == EV_MSC && rawEvent->code == MSC_TIMESTAMP) {
122 mMscTimestamp = rawEvent->value;
123 }
124 mCursorButtonAccumulator.process(rawEvent);
125 mMotionAccumulator.process(rawEvent);
126 mTouchButtonAccumulator.process(rawEvent);
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000127 return {};
128}
129
Harry Cutts1f48a442022-11-15 17:38:36 +0000130void TouchpadInputMapper::sync(nsecs_t when) {
131 HardwareState hwState;
132 // The gestures library uses doubles to represent timestamps in seconds.
133 hwState.timestamp = std::chrono::duration<stime_t>(std::chrono::nanoseconds(when)).count();
134 hwState.msc_timestamp =
135 std::chrono::duration<stime_t>(std::chrono::microseconds(mMscTimestamp)).count();
136
137 hwState.buttons_down = 0;
138 if (mCursorButtonAccumulator.isLeftPressed()) {
139 hwState.buttons_down |= GESTURES_BUTTON_LEFT;
140 }
141 if (mCursorButtonAccumulator.isMiddlePressed()) {
142 hwState.buttons_down |= GESTURES_BUTTON_MIDDLE;
143 }
144 if (mCursorButtonAccumulator.isRightPressed()) {
145 hwState.buttons_down |= GESTURES_BUTTON_RIGHT;
146 }
147 if (mCursorButtonAccumulator.isBackPressed() || mCursorButtonAccumulator.isSidePressed()) {
148 hwState.buttons_down |= GESTURES_BUTTON_BACK;
149 }
150 if (mCursorButtonAccumulator.isForwardPressed() || mCursorButtonAccumulator.isExtraPressed()) {
151 hwState.buttons_down |= GESTURES_BUTTON_FORWARD;
152 }
153
154 std::vector<FingerState> fingers;
155 for (size_t i = 0; i < mMotionAccumulator.getSlotCount(); i++) {
156 MultiTouchMotionAccumulator::Slot slot = mMotionAccumulator.getSlot(i);
157 if (slot.isInUse()) {
158 FingerState& fingerState = fingers.emplace_back();
159 fingerState = {};
160 fingerState.touch_major = slot.getTouchMajor();
161 fingerState.touch_minor = slot.getTouchMinor();
162 fingerState.width_major = slot.getToolMajor();
163 fingerState.width_minor = slot.getToolMinor();
164 fingerState.pressure = slot.getPressure();
165 fingerState.orientation = slot.getOrientation();
166 fingerState.position_x = slot.getX();
167 fingerState.position_y = slot.getY();
168 fingerState.tracking_id = slot.getTrackingId();
169 }
170 }
171 hwState.fingers = fingers.data();
172 hwState.finger_cnt = fingers.size();
173 hwState.touch_cnt = mTouchButtonAccumulator.getTouchCount();
174
175 mGestureInterpreter->PushHardwareState(&hwState);
176 mMotionAccumulator.finishSync();
177 mMscTimestamp = 0;
178}
179
Harry Cutts79cc9fa2022-10-28 15:32:39 +0000180} // namespace android