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 | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 22 | #include <linux/input-event-codes.h> |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 23 | #include <log/log_main.h> |
| 24 | #include "TouchCursorInputMapperCommon.h" |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 25 | #include "TouchpadInputMapper.h" |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame^] | 26 | #include "ui/Rotation.h" |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | short getMaxTouchCount(const InputDeviceContext& context) { |
Harry Cutts | b255215 | 2022-12-13 17:18:09 +0000 | [diff] [blame] | 33 | 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 Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | HardwareProperties 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 Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 83 | void gestureInterpreterCallback(void* clientData, const Gesture* gesture) { |
| 84 | TouchpadInputMapper* mapper = static_cast<TouchpadInputMapper*>(clientData); |
| 85 | mapper->consumeGesture(gesture); |
| 86 | } |
| 87 | |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 88 | } // namespace |
| 89 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 90 | TouchpadInputMapper::TouchpadInputMapper(InputDeviceContext& deviceContext) |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 91 | : InputMapper(deviceContext), |
| 92 | mGestureInterpreter(NewGestureInterpreter(), DeleteGestureInterpreter), |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 93 | mPointerController(getContext()->getPointerController(getDeviceId())), |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 94 | mStateConverter(deviceContext), |
| 95 | mGestureConverter(*getContext(), getDeviceId()) { |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 96 | mGestureInterpreter->Initialize(GESTURES_DEVCLASS_TOUCHPAD); |
| 97 | mGestureInterpreter->SetHardwareProperties(createHardwareProperties(deviceContext)); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 98 | // 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 Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 103 | // 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 Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 105 | } |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 106 | |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 107 | TouchpadInputMapper::~TouchpadInputMapper() { |
| 108 | if (mPointerController != nullptr) { |
| 109 | mPointerController->fade(PointerControllerInterface::Transition::IMMEDIATE); |
| 110 | } |
| 111 | } |
| 112 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 113 | uint32_t TouchpadInputMapper::getSources() const { |
| 114 | return AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD; |
| 115 | } |
| 116 | |
Harry Cutts | edf6ce7 | 2023-01-04 12:15:53 +0000 | [diff] [blame^] | 117 | std::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 Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 133 | std::list<NotifyArgs> TouchpadInputMapper::reset(nsecs_t when) { |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 134 | mStateConverter.reset(); |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 135 | mGestureConverter.reset(); |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 136 | return InputMapper::reset(when); |
| 137 | } |
| 138 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 139 | std::list<NotifyArgs> TouchpadInputMapper::process(const RawEvent* rawEvent) { |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 140 | std::optional<SelfContainedHardwareState> state = mStateConverter.processRawEvent(rawEvent); |
| 141 | if (state) { |
| 142 | return sendHardwareState(rawEvent->when, rawEvent->readTime, *state); |
| 143 | } else { |
| 144 | return {}; |
Harry Cutts | 1f48a44 | 2022-11-15 17:38:36 +0000 | [diff] [blame] | 145 | } |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 148 | std::list<NotifyArgs> TouchpadInputMapper::sendHardwareState(nsecs_t when, nsecs_t readTime, |
| 149 | SelfContainedHardwareState schs) { |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 150 | mProcessing = true; |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 151 | mGestureInterpreter->PushHardwareState(&schs.state); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 152 | mProcessing = false; |
| 153 | |
Harry Cutts | 47db1c7 | 2022-12-13 19:20:47 +0000 | [diff] [blame] | 154 | return processGestures(when, readTime); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void 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 | |
| 166 | std::list<NotifyArgs> TouchpadInputMapper::processGestures(nsecs_t when, nsecs_t readTime) { |
| 167 | std::list<NotifyArgs> out = {}; |
| 168 | for (Gesture& gesture : mGesturesToProcess) { |
Harry Cutts | 4fb941a | 2022-12-14 19:14:04 +0000 | [diff] [blame] | 169 | out += mGestureConverter.handleGesture(when, readTime, gesture); |
Harry Cutts | 7423554 | 2022-11-24 15:52:53 +0000 | [diff] [blame] | 170 | } |
| 171 | mGesturesToProcess.clear(); |
| 172 | return out; |
| 173 | } |
| 174 | |
Harry Cutts | 79cc9fa | 2022-10-28 15:32:39 +0000 | [diff] [blame] | 175 | } // namespace android |