Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "InputManager" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include "InputManager.h" |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 22 | #include "InputDispatcherFactory.h" |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 23 | #include "InputReaderFactory.h" |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 24 | #include "UnwantedInteractionBlocker.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 25 | |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 26 | #include <aidl/com/android/server/inputflinger/IInputFlingerRust.h> |
| 27 | #include <android/binder_interface_utils.h> |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 28 | #include <android/sysprop/InputProperties.sysprop.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 29 | #include <binder/IPCThreadState.h> |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 30 | #include <inputflinger_bootstrap.rs.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 31 | #include <log/log.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 32 | #include <private/android_filesystem_config.h> |
| 33 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | namespace android { |
| 35 | |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | const bool ENABLE_INPUT_DEVICE_USAGE_METRICS = |
Prabir Pradhan | 7adabad | 2023-06-28 16:16:27 +0000 | [diff] [blame] | 39 | sysprop::InputProperties::enable_input_device_usage_metrics().value_or(true); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 40 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame^] | 41 | const bool ENABLE_POINTER_CHOREOGRAPHER = |
| 42 | sysprop::InputProperties::enable_pointer_choreographer().value_or(false); |
| 43 | |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 44 | int32_t exceptionCodeFromStatusT(status_t status) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 45 | switch (status) { |
| 46 | case OK: |
| 47 | return binder::Status::EX_NONE; |
| 48 | case INVALID_OPERATION: |
| 49 | return binder::Status::EX_UNSUPPORTED_OPERATION; |
| 50 | case BAD_VALUE: |
| 51 | case BAD_TYPE: |
| 52 | case NAME_NOT_FOUND: |
| 53 | return binder::Status::EX_ILLEGAL_ARGUMENT; |
| 54 | case NO_INIT: |
| 55 | return binder::Status::EX_ILLEGAL_STATE; |
| 56 | case PERMISSION_DENIED: |
| 57 | return binder::Status::EX_SECURITY; |
| 58 | default: |
| 59 | return binder::Status::EX_TRANSACTION_FAILED; |
| 60 | } |
| 61 | } |
| 62 | |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 63 | // Convert a binder interface into a raw pointer to an AIBinder. |
| 64 | using IInputFlingerRustBootstrapCallback = aidl::com::android::server::inputflinger:: |
| 65 | IInputFlingerRust::IInputFlingerRustBootstrapCallback; |
| 66 | IInputFlingerRustBootstrapCallbackAIBinder* binderToPointer( |
| 67 | IInputFlingerRustBootstrapCallback& interface) { |
| 68 | ndk::SpAIBinder spAIBinder = interface.asBinder(); |
| 69 | auto* ptr = spAIBinder.get(); |
| 70 | AIBinder_incStrong(ptr); |
| 71 | return ptr; |
| 72 | } |
| 73 | |
| 74 | // Create the Rust component of InputFlinger that uses AIDL interfaces as a the foreign function |
| 75 | // interface (FFI). The bootstraping process for IInputFlingerRust is as follows: |
| 76 | // - Create BnInputFlingerRustBootstrapCallback in C++. |
| 77 | // - Use the cxxbridge ffi interface to call the Rust function `create_inputflinger_rust()`, and |
| 78 | // pass the callback binder object as a raw pointer. |
| 79 | // - The Rust implementation will create the implementation of IInputFlingerRust, and pass it |
| 80 | // to C++ through the callback. |
| 81 | // - After the Rust function returns, the binder interface provided to the callback will be the |
| 82 | // only strong reference to the IInputFlingerRust. |
| 83 | std::shared_ptr<IInputFlingerRust> createInputFlingerRust() { |
| 84 | using namespace aidl::com::android::server::inputflinger; |
| 85 | |
| 86 | class Callback : public IInputFlingerRust::BnInputFlingerRustBootstrapCallback { |
| 87 | ndk::ScopedAStatus onProvideInputFlingerRust( |
| 88 | const std::shared_ptr<IInputFlingerRust>& inputFlingerRust) override { |
| 89 | mService = inputFlingerRust; |
| 90 | return ndk::ScopedAStatus::ok(); |
| 91 | } |
| 92 | |
| 93 | public: |
| 94 | std::shared_ptr<IInputFlingerRust> consumeInputFlingerRust() { |
| 95 | auto service = mService; |
| 96 | mService.reset(); |
| 97 | return service; |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | std::shared_ptr<IInputFlingerRust> mService; |
| 102 | }; |
| 103 | |
| 104 | auto callback = ndk::SharedRefBase::make<Callback>(); |
| 105 | create_inputflinger_rust(binderToPointer(*callback)); |
| 106 | auto service = callback->consumeInputFlingerRust(); |
| 107 | LOG_ALWAYS_FATAL_IF(!service, |
| 108 | "create_inputflinger_rust did not provide the IInputFlingerRust " |
| 109 | "implementation through the callback."); |
| 110 | return service; |
| 111 | } |
| 112 | |
| 113 | } // namespace |
| 114 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 115 | /** |
| 116 | * The event flow is via the "InputListener" interface, as follows: |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 117 | * InputReader |
| 118 | * -> UnwantedInteractionBlocker |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame^] | 119 | * -> PointerChoreographer |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 120 | * -> InputProcessor |
| 121 | * -> InputDeviceMetricsCollector |
| 122 | * -> InputDispatcher |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 123 | */ |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 124 | InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy, |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame^] | 125 | InputDispatcherPolicyInterface& dispatcherPolicy, |
| 126 | PointerChoreographerPolicyInterface& choreographerPolicy) { |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 127 | mInputFlingerRust = createInputFlingerRust(); |
| 128 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 129 | mDispatcher = createInputDispatcher(dispatcherPolicy); |
Prabir Pradhan | 274aa2c | 2023-08-18 17:27:07 +0000 | [diff] [blame] | 130 | mTracingStages.emplace_back( |
| 131 | std::make_unique<TracedInputListener>("InputDispatcher", *mDispatcher)); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 132 | |
| 133 | if (ENABLE_INPUT_DEVICE_USAGE_METRICS) { |
Prabir Pradhan | 274aa2c | 2023-08-18 17:27:07 +0000 | [diff] [blame] | 134 | mCollector = std::make_unique<InputDeviceMetricsCollector>(*mTracingStages.back()); |
| 135 | mTracingStages.emplace_back( |
| 136 | std::make_unique<TracedInputListener>("MetricsCollector", *mCollector)); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Prabir Pradhan | 274aa2c | 2023-08-18 17:27:07 +0000 | [diff] [blame] | 139 | mProcessor = std::make_unique<InputProcessor>(*mTracingStages.back()); |
| 140 | mTracingStages.emplace_back( |
| 141 | std::make_unique<TracedInputListener>("InputProcessor", *mProcessor)); |
| 142 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame^] | 143 | if (ENABLE_POINTER_CHOREOGRAPHER) { |
| 144 | mChoreographer = |
| 145 | std::make_unique<PointerChoreographer>(*mTracingStages.back(), choreographerPolicy); |
| 146 | mTracingStages.emplace_back( |
| 147 | std::make_unique<TracedInputListener>("PointerChoreographer", *mChoreographer)); |
| 148 | } |
| 149 | |
Prabir Pradhan | 274aa2c | 2023-08-18 17:27:07 +0000 | [diff] [blame] | 150 | mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mTracingStages.back()); |
| 151 | mTracingStages.emplace_back( |
| 152 | std::make_unique<TracedInputListener>("UnwantedInteractionBlocker", *mBlocker)); |
| 153 | |
| 154 | mReader = createInputReader(readerPolicy, *mTracingStages.back()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | InputManager::~InputManager() { |
| 158 | stop(); |
| 159 | } |
| 160 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 161 | status_t InputManager::start() { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 162 | status_t result = mDispatcher->start(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 163 | if (result) { |
| 164 | ALOGE("Could not start InputDispatcher thread due to error %d.", result); |
| 165 | return result; |
| 166 | } |
| 167 | |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 168 | result = mReader->start(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 169 | if (result) { |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 170 | ALOGE("Could not start InputReader due to error %d.", result); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 171 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 172 | mDispatcher->stop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 173 | return result; |
| 174 | } |
| 175 | |
| 176 | return OK; |
| 177 | } |
| 178 | |
| 179 | status_t InputManager::stop() { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 180 | status_t status = OK; |
| 181 | |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 182 | status_t result = mReader->stop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 183 | if (result) { |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 184 | ALOGW("Could not stop InputReader due to error %d.", result); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 185 | status = result; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 188 | result = mDispatcher->stop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 189 | if (result) { |
| 190 | ALOGW("Could not stop InputDispatcher thread due to error %d.", result); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 191 | status = result; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 194 | return status; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 197 | InputReaderInterface& InputManager::getReader() { |
| 198 | return *mReader; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame^] | 201 | PointerChoreographerInterface& InputManager::getChoreographer() { |
| 202 | return *mChoreographer; |
| 203 | } |
| 204 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 205 | InputProcessorInterface& InputManager::getProcessor() { |
| 206 | return *mProcessor; |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 209 | InputDeviceMetricsCollectorInterface& InputManager::getMetricsCollector() { |
| 210 | return *mCollector; |
| 211 | } |
| 212 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 213 | InputDispatcherInterface& InputManager::getDispatcher() { |
| 214 | return *mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 217 | void InputManager::monitor() { |
| 218 | mReader->monitor(); |
| 219 | mBlocker->monitor(); |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 220 | mProcessor->monitor(); |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 221 | mDispatcher->monitor(); |
| 222 | } |
| 223 | |
Prabir Pradhan | 370dbc9 | 2023-04-06 00:02:05 +0000 | [diff] [blame] | 224 | void InputManager::dump(std::string& dump) { |
| 225 | mReader->dump(dump); |
| 226 | dump += '\n'; |
| 227 | mBlocker->dump(dump); |
| 228 | dump += '\n'; |
Prabir Pradhan | b56e92c | 2023-06-09 23:40:37 +0000 | [diff] [blame^] | 229 | if (ENABLE_POINTER_CHOREOGRAPHER) { |
| 230 | mChoreographer->dump(dump); |
| 231 | dump += '\n'; |
| 232 | } |
Prabir Pradhan | 370dbc9 | 2023-04-06 00:02:05 +0000 | [diff] [blame] | 233 | mProcessor->dump(dump); |
| 234 | dump += '\n'; |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 235 | if (ENABLE_INPUT_DEVICE_USAGE_METRICS) { |
| 236 | mCollector->dump(dump); |
| 237 | dump += '\n'; |
| 238 | } |
Prabir Pradhan | 370dbc9 | 2023-04-06 00:02:05 +0000 | [diff] [blame] | 239 | mDispatcher->dump(dump); |
| 240 | dump += '\n'; |
| 241 | } |
| 242 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 243 | // Used by tests only. |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 244 | binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) { |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 245 | IPCThreadState* ipc = IPCThreadState::self(); |
| 246 | const int uid = ipc->getCallingUid(); |
| 247 | if (uid != AID_SHELL && uid != AID_ROOT) { |
| 248 | ALOGE("Invalid attempt to register input channel over IPC" |
| 249 | "from non shell/root entity (PID: %d)", ipc->getCallingPid()); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 250 | return binder::Status::ok(); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 251 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 252 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 253 | base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name); |
Bernie Innocenti | 189c0f8 | 2020-12-22 19:45:18 +0900 | [diff] [blame] | 254 | if (!channel.ok()) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 255 | return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()), |
| 256 | channel.error().message().c_str()); |
| 257 | } |
| 258 | (*channel)->copyTo(*outChannel); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 259 | return binder::Status::ok(); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 262 | binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) { |
| 263 | mDispatcher->removeInputChannel(connectionToken); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 264 | return binder::Status::ok(); |
| 265 | } |
| 266 | |
| 267 | status_t InputManager::dump(int fd, const Vector<String16>& args) { |
| 268 | std::string dump; |
| 269 | |
| 270 | dump += " InputFlinger dump\n"; |
| 271 | |
Siarhei Vishniakou | cac8427 | 2023-06-28 14:43:25 -0700 | [diff] [blame] | 272 | TEMP_FAILURE_RETRY(::write(fd, dump.c_str(), dump.size())); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 273 | return NO_ERROR; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Prabir Pradhan | 44e6e83 | 2023-06-06 00:03:25 +0000 | [diff] [blame] | 276 | binder::Status InputManager::setFocusedWindow(const gui::FocusRequest& request) { |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 277 | mDispatcher->setFocusedWindow(request); |
| 278 | return binder::Status::ok(); |
| 279 | } |
| 280 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 281 | } // namespace android |