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 #pragma once 18 19 #include <list> 20 #include <memory> 21 #include <set> 22 #include <vector> 23 24 #include <PointerControllerInterface.h> 25 #include <utils/Timers.h> 26 27 #include "CapturedTouchpadEventConverter.h" 28 #include "EventHub.h" 29 #include "InputDevice.h" 30 #include "InputMapper.h" 31 #include "InputReaderBase.h" 32 #include "NotifyArgs.h" 33 #include "accumulator/MultiTouchMotionAccumulator.h" 34 #include "gestures/GestureConverter.h" 35 #include "gestures/HardwareStateConverter.h" 36 #include "gestures/PropertyProvider.h" 37 #include "gestures/TimerProvider.h" 38 39 #include "include/gestures.h" 40 41 namespace android { 42 43 class TouchpadInputMapper : public InputMapper { 44 public: 45 template <class T, class... Args> 46 friend std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext, 47 const InputReaderConfiguration& readerConfig, 48 Args... args); 49 ~TouchpadInputMapper(); 50 51 uint32_t getSources() const override; 52 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override; 53 void dump(std::string& dump) override; 54 55 [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when, 56 const InputReaderConfiguration& config, 57 ConfigurationChanges changes) override; 58 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override; 59 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent& rawEvent) override; 60 [[nodiscard]] std::list<NotifyArgs> timeoutExpired(nsecs_t when) override; 61 62 void consumeGesture(const Gesture* gesture); 63 64 // A subset of InputDeviceIdentifier used for logging metrics, to avoid storing a copy of the 65 // strings in that bigger struct. 66 using MetricsIdentifier = std::tuple<uint16_t /*busId*/, uint16_t /*vendorId*/, 67 uint16_t /*productId*/, uint16_t /*version*/>; 68 69 std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() override; 70 71 private: 72 void resetGestureInterpreter(nsecs_t when); 73 explicit TouchpadInputMapper(InputDeviceContext& deviceContext, 74 const InputReaderConfiguration& readerConfig); 75 void updatePalmDetectionMetrics(); 76 [[nodiscard]] std::list<NotifyArgs> sendHardwareState(nsecs_t when, nsecs_t readTime, 77 SelfContainedHardwareState schs); 78 [[nodiscard]] std::list<NotifyArgs> processGestures(nsecs_t when, nsecs_t readTime); 79 80 std::unique_ptr<gestures::GestureInterpreter, void (*)(gestures::GestureInterpreter*)> 81 mGestureInterpreter; 82 83 PropertyProvider mPropertyProvider; 84 TimerProvider mTimerProvider; 85 86 // The MultiTouchMotionAccumulator is shared between the HardwareStateConverter and 87 // CapturedTouchpadEventConverter, so that if the touchpad is captured or released while touches 88 // are down, the relevant converter can still benefit from the current axis values stored in the 89 // accumulator. 90 MultiTouchMotionAccumulator mMotionAccumulator; 91 92 HardwareStateConverter mStateConverter; 93 GestureConverter mGestureConverter; 94 CapturedTouchpadEventConverter mCapturedEventConverter; 95 96 bool mPointerCaptured = false; 97 bool mResettingInterpreter = false; 98 std::vector<Gesture> mGesturesToProcess; 99 metricsIdFromInputDeviceIdentifier(const InputDeviceIdentifier & id)100 static MetricsIdentifier metricsIdFromInputDeviceIdentifier(const InputDeviceIdentifier& id) { 101 return std::make_tuple(id.bus, id.vendor, id.product, id.version); 102 } 103 const MetricsIdentifier mMetricsId; 104 // Tracking IDs for touches on the pad in the last evdev frame. 105 std::set<int32_t> mLastFrameTrackingIds; 106 // Tracking IDs for touches that have at some point been reported as palms by the touchpad. 107 std::set<int32_t> mPalmTrackingIds; 108 109 // The display that events generated by this mapper should target. This can be set to 110 // LogicalDisplayId::INVALID to target the focused display. If there is no display target (i.e. 111 // std::nullopt), all events will be ignored. 112 std::optional<ui::LogicalDisplayId> mDisplayId; 113 114 nsecs_t mGestureStartTime{0}; 115 }; 116 117 } // namespace android 118