Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 1 | /* |
| 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 Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 19 | #include <optional> |
| 20 | |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 21 | #include <android/input.h> |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame^] | 22 | #include <input/PrintTools.h> |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 23 | #include <linux/input-event-codes.h> |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 24 | #include <log/log_main.h> |
| 25 | #include "TouchCursorInputMapperCommon.h" |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 26 | #include "TouchpadInputMapper.h" |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 27 | #include "ui/Rotation.h" |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | short getMaxTouchCount(const InputDeviceContext& context) { |
Harry Cutts | b255215 | 2022-12-13 17:18:09 +0000 | [diff] [blame] | 34 | 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 Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | HardwareProperties 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 Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 84 | void gestureInterpreterCallback(void* clientData, const Gesture* gesture) { |
| 85 | TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData); |
| 86 | mapper->consumeGesture(gesture); |
| 87 | } |
| 88 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 89 | } // namespace |
| 90 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 91 | TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext) |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 92 | : InputMapper(deviceContext), |
| 93 | mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter), |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 94 | mPointerController(getContext()->getPointerController(getDeviceId())), |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 95 | mStateConverter(deviceContext), |
Harry Cutts | c5748d1 | 2022-12-02 17:30:18 +0000 | [diff] [blame] | 96 | mGestureConverter(*getContext(), deviceContext, getDeviceId()) { |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 97 | mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD); |
| 98 | mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext)); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 99 | // Even though we don't explicitly delete copy/move semantics, it's safe to |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 100 | // give away pointers to TouchpadInputMapper and its members here because |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 101 | // 1) mGestureInterpreter's lifecycle is determined by TouchpadInputMapper, and |
| 102 | // 2) TouchpadInputMapper is stored as a unique_ptr and not moved. |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 103 | mGestureInterpreter->SetPropProvider(const_cast<GesturesPropProvider*>(&gesturePropProvider), |
| 104 | &mPropertyProvider); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 105 | mGestureInterpreter->SetCallback(gestureInterpreterCallback, this); |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 106 | // TODO(b/251196347): set a timer provider, so the library can use timers. |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 107 | } |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 108 | |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 109 | TouchpadInputMapper::~TouchpadInputMapper() { |
| 110 | if (mPointerController != nullptr) { |
| 111 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 112 | } |
Harry Cutts | 1b21791 | 2023-01-03 17:13:19 +0000 | [diff] [blame] | 113 | |
| 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 Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 124 | uint32_t TouchpadInputMapper::getSources() const { |
| 125 | return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD; |
| 126 | } |
| 127 | |
Harry Cutts | ea73eaa | 2023-01-16 17:55:46 +0000 | [diff] [blame^] | 128 | void 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 Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame] | 136 | std::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 Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 152 | std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) { |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 153 | mStateConverter.reset(); |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 154 | mGestureConverter.reset(); |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 155 | return InputMapper::reset(when); |
| 156 | } |
| 157 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 158 | std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) { |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 159 | std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent); |
| 160 | if (state) { |
| 161 | return sendHardwareState(rawEvent->when, rawEvent->readTime, *state); |
| 162 | } else { |
| 163 | return {}; |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 164 | } |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 167 | std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime, |
| 168 | SelfContainedHardwareState schs) { |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 169 | mProcessing = true; |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 170 | mGestureInterpreter->PushHardwareState(&schs.state); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 171 | mProcessing = false; |
| 172 | |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 173 | return processGestures(when, readTime); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void 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 | |
| 185 | std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) { |
| 186 | std::list<NotifyArgs> out = {}; |
| 187 | for (Gesture& gesture : mGesturesToProcess) { |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 188 | out += mGestureConverter.handleGesture(when, readTime, gesture); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 189 | } |
| 190 | mGesturesToProcess.clear(); |
| 191 | return out; |
| 192 | } |
| 193 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 194 | } // namespace android |