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