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