Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 17 | #define LOG_TAG "InputProcessor" |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 18 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 19 | #include "InputProcessor.h" |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 20 | #include "InputCommonConverter.h" |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 21 | |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 23 | #include <android/binder_manager.h> |
| 24 | #include <android/binder_process.h> |
Harry Cutts | 2800fb0 | 2022-09-15 13:49:23 +0000 | [diff] [blame] | 25 | #include <input/Input.h> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 26 | #include <inttypes.h> |
| 27 | #include <log/log.h> |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <cmath> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 30 | #if defined(__linux__) |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 31 | #include <pthread.h> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 32 | #endif |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 33 | #include <unordered_set> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 34 | |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 35 | #define INDENT1 " " |
| 36 | #define INDENT2 " " |
| 37 | #define INDENT3 " " |
| 38 | #define INDENT4 " " |
| 39 | #define INDENT5 " " |
| 40 | |
| 41 | using android::base::StringPrintf; |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 42 | using namespace std::chrono_literals; |
| 43 | using namespace ::aidl::android::hardware::input; |
| 44 | using aidl::android::hardware::input::processor::IInputProcessor; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 48 | // Max number of elements to store in mEvents. |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 49 | static constexpr size_t MAX_EVENTS = 5; |
| 50 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 51 | template <class K, class V> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 52 | static V getValueForKey(const std::unordered_map<K, V>& map, K key, V defaultValue) { |
| 53 | auto it = map.find(key); |
| 54 | if (it == map.end()) { |
| 55 | return defaultValue; |
| 56 | } |
| 57 | return it->second; |
| 58 | } |
| 59 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 60 | static MotionClassification getMotionClassification(common::Classification classification) { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 61 | static_assert(MotionClassification::NONE == |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 62 | static_cast<MotionClassification>(common::Classification::NONE)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 63 | static_assert(MotionClassification::AMBIGUOUS_GESTURE == |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 64 | static_cast<MotionClassification>(common::Classification::AMBIGUOUS_GESTURE)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 65 | static_assert(MotionClassification::DEEP_PRESS == |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 66 | static_cast<MotionClassification>(common::Classification::DEEP_PRESS)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 67 | return static_cast<MotionClassification>(classification); |
| 68 | } |
| 69 | |
| 70 | static bool isTouchEvent(const NotifyMotionArgs& args) { |
Siarhei Vishniakou | d948957 | 2021-11-12 20:08:38 -0800 | [diff] [blame] | 71 | return isFromSource(args.source, AINPUT_SOURCE_TOUCHPAD) || |
| 72 | isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 75 | static void setCurrentThreadName(const char* name) { |
| 76 | #if defined(__linux__) |
| 77 | // Set the thread name for debugging |
| 78 | pthread_setname_np(pthread_self(), name); |
| 79 | #else |
| 80 | (void*)(name); // prevent unused variable warning |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | static std::shared_ptr<IInputProcessor> getService() { |
| 85 | const std::string aidl_instance_name = std::string(IInputProcessor::descriptor) + "/default"; |
| 86 | |
| 87 | if (!AServiceManager_isDeclared(aidl_instance_name.c_str())) { |
| 88 | ALOGI("HAL %s is not declared", aidl_instance_name.c_str()); |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
| 92 | ndk::SpAIBinder binder(AServiceManager_waitForService(aidl_instance_name.c_str())); |
| 93 | return IInputProcessor::fromBinder(binder); |
| 94 | } |
| 95 | |
| 96 | // Temporarily releases a held mutex for the lifetime of the instance. |
| 97 | // Named to match std::scoped_lock |
| 98 | class scoped_unlock { |
| 99 | public: |
| 100 | explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); } |
| 101 | ~scoped_unlock() { mMutex.lock(); } |
| 102 | |
| 103 | private: |
| 104 | std::mutex& mMutex; |
| 105 | }; |
| 106 | |
| 107 | // --- ScopedDeathRecipient --- |
| 108 | ScopedDeathRecipient::ScopedDeathRecipient(AIBinder_DeathRecipient_onBinderDied onBinderDied, |
| 109 | void* cookie) |
| 110 | : mCookie(cookie) { |
| 111 | mRecipient = AIBinder_DeathRecipient_new(onBinderDied); |
| 112 | } |
| 113 | |
| 114 | void ScopedDeathRecipient::linkToDeath(AIBinder* binder) { |
| 115 | binder_status_t linked = AIBinder_linkToDeath(binder, mRecipient, mCookie); |
| 116 | if (linked != STATUS_OK) { |
| 117 | ALOGE("Could not link death recipient to the HAL death"); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | ScopedDeathRecipient::~ScopedDeathRecipient() { |
| 122 | AIBinder_DeathRecipient_delete(mRecipient); |
| 123 | } |
| 124 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 125 | // --- ClassifierEvent --- |
| 126 | |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 127 | ClassifierEvent::ClassifierEvent(const NotifyMotionArgs& args) |
| 128 | : type(ClassifierEventType::MOTION), args(args){}; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 129 | |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 130 | ClassifierEvent::ClassifierEvent(const NotifyDeviceResetArgs& args) |
| 131 | : type(ClassifierEventType::DEVICE_RESET), args(args){}; |
| 132 | |
| 133 | ClassifierEvent::ClassifierEvent(ClassifierEventType type, std::optional<NotifyArgs> args) |
| 134 | : type(type), args(args){}; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 135 | |
| 136 | ClassifierEvent& ClassifierEvent::operator=(ClassifierEvent&& other) { |
| 137 | type = other.type; |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 138 | args = other.args; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 139 | return *this; |
| 140 | } |
| 141 | |
| 142 | ClassifierEvent ClassifierEvent::createHalResetEvent() { |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 143 | return ClassifierEvent(ClassifierEventType::HAL_RESET, std::nullopt); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | ClassifierEvent ClassifierEvent::createExitEvent() { |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 147 | return ClassifierEvent(ClassifierEventType::EXIT, std::nullopt); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | std::optional<int32_t> ClassifierEvent::getDeviceId() const { |
| 151 | switch (type) { |
| 152 | case ClassifierEventType::MOTION: { |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 153 | const NotifyMotionArgs& motionArgs = std::get<NotifyMotionArgs>(*args); |
| 154 | return motionArgs.deviceId; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 155 | } |
| 156 | case ClassifierEventType::DEVICE_RESET: { |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 157 | const NotifyDeviceResetArgs& deviceResetArgs = std::get<NotifyDeviceResetArgs>(*args); |
| 158 | return deviceResetArgs.deviceId; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 159 | } |
| 160 | case ClassifierEventType::HAL_RESET: { |
| 161 | return std::nullopt; |
| 162 | } |
| 163 | case ClassifierEventType::EXIT: { |
| 164 | return std::nullopt; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // --- MotionClassifier --- |
| 170 | |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 171 | MotionClassifier::MotionClassifier(std::shared_ptr<IInputProcessor> service) |
| 172 | : mEvents(MAX_EVENTS), mService(std::move(service)) { |
Siarhei Vishniakou | 6dbc3f6 | 2019-02-04 14:30:11 -0800 | [diff] [blame] | 173 | // Under normal operation, we do not need to reset the HAL here. But in the case where system |
| 174 | // crashed, but HAL didn't, we may be connecting to an existing HAL process that might already |
| 175 | // have received events in the past. That means, that HAL could be in an inconsistent state |
| 176 | // once it receives events from the newly created MotionClassifier. |
| 177 | mEvents.push(ClassifierEvent::createHalResetEvent()); |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 178 | |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 179 | mHalThread = std::thread(&MotionClassifier::processEvents, this); |
| 180 | #if defined(__linux__) |
| 181 | // Set the thread name for debugging |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 182 | pthread_setname_np(mHalThread.native_handle(), "InputProcessor"); |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 183 | #endif |
| 184 | } |
| 185 | |
| 186 | std::unique_ptr<MotionClassifierInterface> MotionClassifier::create( |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 187 | std::shared_ptr<IInputProcessor> service) { |
| 188 | LOG_ALWAYS_FATAL_IF(service == nullptr); |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 189 | // Using 'new' to access a non-public constructor |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 190 | return std::unique_ptr<MotionClassifier>(new MotionClassifier(std::move(service))); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | MotionClassifier::~MotionClassifier() { |
| 194 | requestExit(); |
| 195 | mHalThread.join(); |
| 196 | } |
| 197 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 198 | /** |
| 199 | * Obtain the classification from the HAL for a given MotionEvent. |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 200 | * Should only be called from the InputProcessor thread (mHalThread). |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 201 | * Should not be called from the thread that notifyMotion runs on. |
| 202 | * |
| 203 | * There is no way to provide a timeout for a HAL call. So if the HAL takes too long |
| 204 | * to return a classification, this would directly impact the touch latency. |
| 205 | * To remove any possibility of negatively affecting the touch latency, the HAL |
| 206 | * is called from a dedicated thread. |
| 207 | */ |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 208 | void MotionClassifier::processEvents() { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 209 | while (true) { |
| 210 | ClassifierEvent event = mEvents.pop(); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 211 | bool halResponseOk = true; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 212 | switch (event.type) { |
| 213 | case ClassifierEventType::MOTION: { |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 214 | NotifyMotionArgs& motionArgs = std::get<NotifyMotionArgs>(*event.args); |
| 215 | common::MotionEvent motionEvent = notifyMotionArgsToHalMotionEvent(motionArgs); |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 216 | common::Classification classification; |
| 217 | ndk::ScopedAStatus response = mService->classify(motionEvent, &classification); |
| 218 | if (response.isOk()) { |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 219 | updateClassification(motionArgs.deviceId, motionArgs.eventTime, |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 220 | getMotionClassification(classification)); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 221 | } |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | case ClassifierEventType::DEVICE_RESET: { |
| 225 | const int32_t deviceId = *(event.getDeviceId()); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 226 | halResponseOk = mService->resetDevice(deviceId).isOk(); |
Siarhei Vishniakou | e3021d7 | 2020-02-28 15:25:41 -0800 | [diff] [blame] | 227 | clearDeviceState(deviceId); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 228 | break; |
| 229 | } |
| 230 | case ClassifierEventType::HAL_RESET: { |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 231 | halResponseOk = mService->reset().isOk(); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 232 | clearClassifications(); |
| 233 | break; |
| 234 | } |
| 235 | case ClassifierEventType::EXIT: { |
| 236 | clearClassifications(); |
| 237 | return; |
| 238 | } |
| 239 | } |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 240 | if (!halResponseOk) { |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 241 | ALOGE("Error communicating with InputProcessor HAL. " |
| 242 | "Exiting MotionClassifier HAL thread"); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 243 | clearClassifications(); |
| 244 | return; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void MotionClassifier::enqueueEvent(ClassifierEvent&& event) { |
| 250 | bool eventAdded = mEvents.push(std::move(event)); |
| 251 | if (!eventAdded) { |
| 252 | // If the queue is full, suspect the HAL is slow in processing the events. |
Siarhei Vishniakou | 9b5a821 | 2019-07-01 14:25:40 -0700 | [diff] [blame] | 253 | ALOGE("Could not add the event to the queue. Resetting"); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 254 | reset(); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | void MotionClassifier::requestExit() { |
| 259 | reset(); |
| 260 | mEvents.push(ClassifierEvent::createExitEvent()); |
| 261 | } |
| 262 | |
| 263 | void MotionClassifier::updateClassification(int32_t deviceId, nsecs_t eventTime, |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 264 | MotionClassification classification) { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 265 | std::scoped_lock lock(mLock); |
| 266 | const nsecs_t lastDownTime = getValueForKey(mLastDownTimes, deviceId, static_cast<nsecs_t>(0)); |
| 267 | if (eventTime < lastDownTime) { |
| 268 | // HAL just finished processing an event that belonged to an earlier gesture, |
| 269 | // but new gesture is already in progress. Drop this classification. |
| 270 | ALOGW("Received late classification. Late by at least %" PRId64 " ms.", |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 271 | nanoseconds_to_milliseconds(lastDownTime - eventTime)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | mClassifications[deviceId] = classification; |
| 275 | } |
| 276 | |
| 277 | void MotionClassifier::setClassification(int32_t deviceId, MotionClassification classification) { |
| 278 | std::scoped_lock lock(mLock); |
| 279 | mClassifications[deviceId] = classification; |
| 280 | } |
| 281 | |
| 282 | void MotionClassifier::clearClassifications() { |
| 283 | std::scoped_lock lock(mLock); |
| 284 | mClassifications.clear(); |
| 285 | } |
| 286 | |
| 287 | MotionClassification MotionClassifier::getClassification(int32_t deviceId) { |
| 288 | std::scoped_lock lock(mLock); |
| 289 | return getValueForKey(mClassifications, deviceId, MotionClassification::NONE); |
| 290 | } |
| 291 | |
| 292 | void MotionClassifier::updateLastDownTime(int32_t deviceId, nsecs_t downTime) { |
| 293 | std::scoped_lock lock(mLock); |
| 294 | mLastDownTimes[deviceId] = downTime; |
| 295 | mClassifications[deviceId] = MotionClassification::NONE; |
| 296 | } |
| 297 | |
Siarhei Vishniakou | e3021d7 | 2020-02-28 15:25:41 -0800 | [diff] [blame] | 298 | void MotionClassifier::clearDeviceState(int32_t deviceId) { |
| 299 | std::scoped_lock lock(mLock); |
| 300 | mClassifications.erase(deviceId); |
| 301 | mLastDownTimes.erase(deviceId); |
| 302 | } |
| 303 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 304 | MotionClassification MotionClassifier::classify(const NotifyMotionArgs& args) { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 305 | if ((args.action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_DOWN) { |
| 306 | updateLastDownTime(args.deviceId, args.downTime); |
| 307 | } |
| 308 | |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 309 | enqueueEvent(args); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 310 | return getClassification(args.deviceId); |
| 311 | } |
| 312 | |
| 313 | void MotionClassifier::reset() { |
| 314 | mEvents.clear(); |
| 315 | mEvents.push(ClassifierEvent::createHalResetEvent()); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Per-device reset. Clear the outstanding events that are going to be sent to HAL. |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 320 | * Request InputProcessor thread to call resetDevice for this particular device. |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 321 | */ |
| 322 | void MotionClassifier::reset(const NotifyDeviceResetArgs& args) { |
| 323 | int32_t deviceId = args.deviceId; |
| 324 | // Clear the pending events right away, to avoid unnecessary work done by the HAL. |
Prabir Pradhan | d567811 | 2023-05-18 01:57:10 +0000 | [diff] [blame] | 325 | mEvents.erase_if([deviceId](const ClassifierEvent& event) { |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 326 | std::optional<int32_t> eventDeviceId = event.getDeviceId(); |
| 327 | return eventDeviceId && (*eventDeviceId == deviceId); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 328 | }); |
Siarhei Vishniakou | 096257c | 2022-09-15 17:02:20 -0700 | [diff] [blame] | 329 | enqueueEvent(args); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 332 | void MotionClassifier::dump(std::string& dump) { |
| 333 | std::scoped_lock lock(mLock); |
Prabir Pradhan | d7740d6 | 2022-07-01 17:54:18 +0000 | [diff] [blame] | 334 | dump += StringPrintf(INDENT2 "mService connected: %s\n", mService ? "true" : "false"); |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 335 | dump += StringPrintf(INDENT2 "mEvents: %zu element(s) (max=%zu)\n", mEvents.size(), MAX_EVENTS); |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 336 | dump += INDENT2 "mClassifications, mLastDownTimes:\n"; |
| 337 | dump += INDENT3 "Device Id\tClassification\tLast down time"; |
| 338 | // Combine mClassifications and mLastDownTimes into a single table. |
| 339 | // Create a superset of device ids. |
| 340 | std::unordered_set<int32_t> deviceIds; |
| 341 | std::for_each(mClassifications.begin(), mClassifications.end(), |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 342 | [&deviceIds](auto pair) { deviceIds.insert(pair.first); }); |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 343 | std::for_each(mLastDownTimes.begin(), mLastDownTimes.end(), |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 344 | [&deviceIds](auto pair) { deviceIds.insert(pair.first); }); |
| 345 | for (int32_t deviceId : deviceIds) { |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 346 | const MotionClassification classification = |
| 347 | getValueForKey(mClassifications, deviceId, MotionClassification::NONE); |
| 348 | const nsecs_t downTime = getValueForKey(mLastDownTimes, deviceId, static_cast<nsecs_t>(0)); |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 349 | dump += StringPrintf("\n" INDENT4 "%" PRId32 "\t%s\t%" PRId64, deviceId, |
| 350 | motionClassificationToString(classification), downTime); |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Prabir Pradhan | d7740d6 | 2022-07-01 17:54:18 +0000 | [diff] [blame] | 354 | void MotionClassifier::monitor() { |
| 355 | std::scoped_lock lock(mLock); |
| 356 | if (mService) { |
| 357 | // Ping the HAL service to ensure it is alive and not blocked. |
| 358 | const binder_status_t status = AIBinder_ping(mService->asBinder().get()); |
| 359 | if (status != STATUS_OK) { |
| 360 | ALOGW("IInputProcessor HAL is not responding; binder ping result: %s", |
| 361 | AStatus_getDescription(AStatus_fromStatus(status))); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 366 | // --- InputProcessor --- |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 367 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 368 | InputProcessor::InputProcessor(InputListenerInterface& listener) : mQueuedListener(listener) {} |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 369 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 370 | void InputProcessor::onBinderDied(void* cookie) { |
| 371 | InputProcessor* processor = static_cast<InputProcessor*>(cookie); |
| 372 | if (processor == nullptr) { |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 373 | LOG_ALWAYS_FATAL("Cookie is not valid"); |
| 374 | return; |
| 375 | } |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 376 | processor->setMotionClassifierEnabled(false); |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 377 | } |
Siarhei Vishniakou | c9ac19e | 2020-03-19 11:55:01 -0700 | [diff] [blame] | 378 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 379 | void InputProcessor::setMotionClassifierEnabled(bool enabled) { |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 380 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | c9ac19e | 2020-03-19 11:55:01 -0700 | [diff] [blame] | 381 | if (enabled) { |
| 382 | ALOGI("Enabling motion classifier"); |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 383 | if (mInitializeMotionClassifier.valid()) { |
| 384 | scoped_unlock unlock(mLock); |
| 385 | std::future_status status = mInitializeMotionClassifier.wait_for(5s); |
| 386 | if (status != std::future_status::ready) { |
| 387 | /** |
| 388 | * We don't have a better option here than to crash. We can't stop the thread, |
| 389 | * and we can't continue because 'mInitializeMotionClassifier' will block in its |
| 390 | * destructor. |
| 391 | */ |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 392 | LOG_ALWAYS_FATAL("The thread to load IInputProcessor is stuck!"); |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 393 | } |
Siarhei Vishniakou | c9ac19e | 2020-03-19 11:55:01 -0700 | [diff] [blame] | 394 | } |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 395 | mInitializeMotionClassifier = std::async(std::launch::async, [this] { |
| 396 | setCurrentThreadName("Create MotionClassifier"); |
| 397 | std::shared_ptr<IInputProcessor> service = getService(); |
| 398 | if (service == nullptr) { |
| 399 | // Keep the MotionClassifier null, no service was found |
| 400 | return; |
| 401 | } |
| 402 | { // acquire lock |
| 403 | std::scoped_lock threadLock(mLock); |
| 404 | mHalDeathRecipient = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 405 | std::make_unique<ScopedDeathRecipient>(onBinderDied, /*cookie=*/this); |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 406 | mHalDeathRecipient->linkToDeath(service->asBinder().get()); |
| 407 | setMotionClassifierLocked(MotionClassifier::create(std::move(service))); |
| 408 | } // release lock |
| 409 | }); |
Siarhei Vishniakou | c9ac19e | 2020-03-19 11:55:01 -0700 | [diff] [blame] | 410 | } else { |
| 411 | ALOGI("Disabling motion classifier"); |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 412 | setMotionClassifierLocked(nullptr); |
Siarhei Vishniakou | c9ac19e | 2020-03-19 11:55:01 -0700 | [diff] [blame] | 413 | } |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 414 | } |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 415 | |
Prabir Pradhan | e3da4bb | 2023-04-05 23:51:23 +0000 | [diff] [blame] | 416 | void InputProcessor::notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) { |
| 417 | // pass through |
| 418 | mQueuedListener.notify(args); |
| 419 | mQueuedListener.flush(); |
| 420 | } |
| 421 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 422 | void InputProcessor::notifyKey(const NotifyKeyArgs& args) { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 423 | // pass through |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 424 | mQueuedListener.notifyKey(args); |
| 425 | mQueuedListener.flush(); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 428 | void InputProcessor::notifyMotion(const NotifyMotionArgs& args) { |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 429 | { // acquire lock |
| 430 | std::scoped_lock lock(mLock); |
| 431 | // MotionClassifier is only used for touch events, for now |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 432 | const bool sendToMotionClassifier = mMotionClassifier && isTouchEvent(args); |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 433 | if (!sendToMotionClassifier) { |
| 434 | mQueuedListener.notifyMotion(args); |
| 435 | } else { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 436 | NotifyMotionArgs newArgs(args); |
Harry Cutts | 2800fb0 | 2022-09-15 13:49:23 +0000 | [diff] [blame] | 437 | const MotionClassification newClassification = mMotionClassifier->classify(newArgs); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 438 | LOG_ALWAYS_FATAL_IF(args.classification != MotionClassification::NONE && |
Harry Cutts | 2800fb0 | 2022-09-15 13:49:23 +0000 | [diff] [blame] | 439 | newClassification != MotionClassification::NONE, |
| 440 | "Conflicting classifications %s (new) and %s (old)!", |
| 441 | motionClassificationToString(newClassification), |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 442 | motionClassificationToString(args.classification)); |
Harry Cutts | 2800fb0 | 2022-09-15 13:49:23 +0000 | [diff] [blame] | 443 | newArgs.classification = newClassification; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 444 | mQueuedListener.notifyMotion(newArgs); |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 445 | } |
| 446 | } // release lock |
| 447 | mQueuedListener.flush(); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 450 | void InputProcessor::notifySensor(const NotifySensorArgs& args) { |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 451 | // pass through |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 452 | mQueuedListener.notifySensor(args); |
| 453 | mQueuedListener.flush(); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 456 | void InputProcessor::notifyVibratorState(const NotifyVibratorStateArgs& args) { |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 457 | // pass through |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 458 | mQueuedListener.notifyVibratorState(args); |
| 459 | mQueuedListener.flush(); |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 462 | void InputProcessor::notifySwitch(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 463 | // pass through |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 464 | mQueuedListener.notifySwitch(args); |
| 465 | mQueuedListener.flush(); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 468 | void InputProcessor::notifyDeviceReset(const NotifyDeviceResetArgs& args) { |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 469 | { // acquire lock |
| 470 | std::scoped_lock lock(mLock); |
| 471 | if (mMotionClassifier) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 472 | mMotionClassifier->reset(args); |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 473 | } |
| 474 | } // release lock |
| 475 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 476 | // continue to next stage |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 477 | mQueuedListener.notifyDeviceReset(args); |
| 478 | mQueuedListener.flush(); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 481 | void InputProcessor::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) { |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 482 | // pass through |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 483 | mQueuedListener.notifyPointerCaptureChanged(args); |
| 484 | mQueuedListener.flush(); |
Prabir Pradhan | 7e18618 | 2020-11-10 13:56:45 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 487 | void InputProcessor::setMotionClassifierLocked( |
Siarhei Vishniakou | 34d6fef | 2022-02-01 19:03:45 -0800 | [diff] [blame] | 488 | std::unique_ptr<MotionClassifierInterface> motionClassifier) REQUIRES(mLock) { |
| 489 | if (motionClassifier == nullptr) { |
| 490 | // Destroy the ScopedDeathRecipient object, which will cause it to unlinkToDeath. |
| 491 | // We can't call 'unlink' here because we don't have the binder handle. |
| 492 | mHalDeathRecipient = nullptr; |
| 493 | } |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 494 | mMotionClassifier = std::move(motionClassifier); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 497 | void InputProcessor::dump(std::string& dump) { |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 498 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 499 | dump += "Input Processor State:\n"; |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 500 | dump += INDENT1 "Motion Classifier:\n"; |
| 501 | if (mMotionClassifier) { |
| 502 | mMotionClassifier->dump(dump); |
| 503 | } else { |
| 504 | dump += INDENT2 "<nullptr>"; |
| 505 | } |
| 506 | dump += "\n"; |
| 507 | } |
| 508 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 509 | void InputProcessor::monitor() { |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 510 | std::scoped_lock lock(mLock); |
Prabir Pradhan | d7740d6 | 2022-07-01 17:54:18 +0000 | [diff] [blame] | 511 | if (mMotionClassifier) mMotionClassifier->monitor(); |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 514 | InputProcessor::~InputProcessor() {} |
Siarhei Vishniakou | 1652397 | 2020-03-04 17:48:39 -0800 | [diff] [blame] | 515 | |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 516 | } // namespace android |