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