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