blob: 41e5247b50f84152e3eafb85f859fc02e1e8edca [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
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 Tane84e6f92019-08-29 17:28:41 -070022#include "InputDispatcherFactory.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080023#include "InputReaderFactory.h"
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070024#include "UnwantedInteractionBlocker.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080025
Prabir Pradhan44e6e832023-06-06 00:03:25 +000026#include <aidl/com/android/server/inputflinger/IInputFlingerRust.h>
27#include <android/binder_interface_utils.h>
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000028#include <android/sysprop/InputProperties.sysprop.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070029#include <binder/IPCThreadState.h>
Prabir Pradhan678405c2023-09-08 17:18:32 +000030#include <com_android_input_flags.h>
Prabir Pradhan44e6e832023-06-06 00:03:25 +000031#include <inputflinger_bootstrap.rs.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070032#include <log/log.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070033#include <private/android_filesystem_config.h>
34
Prabir Pradhan678405c2023-09-08 17:18:32 +000035namespace input_flags = com::android::input::flags;
36
Michael Wrightd02c5b62014-02-10 15:10:22 -080037namespace android {
38
Prabir Pradhan44e6e832023-06-06 00:03:25 +000039namespace {
40
41const bool ENABLE_INPUT_DEVICE_USAGE_METRICS =
Prabir Pradhan7adabad2023-06-28 16:16:27 +000042 sysprop::InputProperties::enable_input_device_usage_metrics().value_or(true);
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000043
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +000044const bool ENABLE_INPUT_FILTER_RUST = input_flags::enable_input_filter_rust_impl();
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000045
Prabir Pradhan44e6e832023-06-06 00:03:25 +000046int32_t exceptionCodeFromStatusT(status_t status) {
Garfield Tan15601662020-09-22 15:32:38 -070047 switch (status) {
48 case OK:
49 return binder::Status::EX_NONE;
50 case INVALID_OPERATION:
51 return binder::Status::EX_UNSUPPORTED_OPERATION;
52 case BAD_VALUE:
53 case BAD_TYPE:
54 case NAME_NOT_FOUND:
55 return binder::Status::EX_ILLEGAL_ARGUMENT;
56 case NO_INIT:
57 return binder::Status::EX_ILLEGAL_STATE;
58 case PERMISSION_DENIED:
59 return binder::Status::EX_SECURITY;
60 default:
61 return binder::Status::EX_TRANSACTION_FAILED;
62 }
63}
64
Prabir Pradhan44e6e832023-06-06 00:03:25 +000065// Convert a binder interface into a raw pointer to an AIBinder.
66using IInputFlingerRustBootstrapCallback = aidl::com::android::server::inputflinger::
67 IInputFlingerRust::IInputFlingerRustBootstrapCallback;
68IInputFlingerRustBootstrapCallbackAIBinder* binderToPointer(
69 IInputFlingerRustBootstrapCallback& interface) {
70 ndk::SpAIBinder spAIBinder = interface.asBinder();
71 auto* ptr = spAIBinder.get();
72 AIBinder_incStrong(ptr);
73 return ptr;
74}
75
76// Create the Rust component of InputFlinger that uses AIDL interfaces as a the foreign function
77// interface (FFI). The bootstraping process for IInputFlingerRust is as follows:
78// - Create BnInputFlingerRustBootstrapCallback in C++.
79// - Use the cxxbridge ffi interface to call the Rust function `create_inputflinger_rust()`, and
80// pass the callback binder object as a raw pointer.
81// - The Rust implementation will create the implementation of IInputFlingerRust, and pass it
82// to C++ through the callback.
83// - After the Rust function returns, the binder interface provided to the callback will be the
84// only strong reference to the IInputFlingerRust.
85std::shared_ptr<IInputFlingerRust> createInputFlingerRust() {
86 using namespace aidl::com::android::server::inputflinger;
87
88 class Callback : public IInputFlingerRust::BnInputFlingerRustBootstrapCallback {
89 ndk::ScopedAStatus onProvideInputFlingerRust(
90 const std::shared_ptr<IInputFlingerRust>& inputFlingerRust) override {
91 mService = inputFlingerRust;
92 return ndk::ScopedAStatus::ok();
93 }
94
95 public:
96 std::shared_ptr<IInputFlingerRust> consumeInputFlingerRust() {
97 auto service = mService;
98 mService.reset();
99 return service;
100 }
101
102 private:
103 std::shared_ptr<IInputFlingerRust> mService;
104 };
105
106 auto callback = ndk::SharedRefBase::make<Callback>();
107 create_inputflinger_rust(binderToPointer(*callback));
108 auto service = callback->consumeInputFlingerRust();
109 LOG_ALWAYS_FATAL_IF(!service,
110 "create_inputflinger_rust did not provide the IInputFlingerRust "
111 "implementation through the callback.");
112 return service;
113}
114
115} // namespace
116
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700117/**
118 * The event flow is via the "InputListener" interface, as follows:
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000119 * InputReader
120 * -> UnwantedInteractionBlocker
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +0000121 * -> InputFilter
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000122 * -> PointerChoreographer
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000123 * -> InputProcessor
124 * -> InputDeviceMetricsCollector
125 * -> InputDispatcher
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700126 */
Prabir Pradhana41d2442023-04-20 21:30:40 +0000127InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000128 InputDispatcherPolicyInterface& dispatcherPolicy,
Vaibhav Devmurarie948d4b2023-12-28 13:41:32 +0000129 PointerChoreographerPolicyInterface& choreographerPolicy,
130 InputFilterPolicyInterface& inputFilterPolicy) {
Prabir Pradhan44e6e832023-06-06 00:03:25 +0000131 mInputFlingerRust = createInputFlingerRust();
132
Garfield Tane84e6f92019-08-29 17:28:41 -0700133 mDispatcher = createInputDispatcher(dispatcherPolicy);
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000134 mTracingStages.emplace_back(
135 std::make_unique<TracedInputListener>("InputDispatcher", *mDispatcher));
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000136
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +0000137 if (ENABLE_INPUT_FILTER_RUST) {
Vaibhav Devmurarie948d4b2023-12-28 13:41:32 +0000138 mInputFilter = std::make_unique<InputFilter>(*mTracingStages.back(), *mInputFlingerRust,
139 inputFilterPolicy);
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +0000140 mTracingStages.emplace_back(
141 std::make_unique<TracedInputListener>("InputFilter", *mInputFilter));
142 }
143
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000144 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000145 mCollector = std::make_unique<InputDeviceMetricsCollector>(*mTracingStages.back());
146 mTracingStages.emplace_back(
147 std::make_unique<TracedInputListener>("MetricsCollector", *mCollector));
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000148 }
149
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000150 mProcessor = std::make_unique<InputProcessor>(*mTracingStages.back());
151 mTracingStages.emplace_back(
152 std::make_unique<TracedInputListener>("InputProcessor", *mProcessor));
153
Prabir Pradhan657786c2024-05-03 23:19:01 +0000154 mChoreographer =
155 std::make_unique<PointerChoreographer>(*mTracingStages.back(), choreographerPolicy);
156 mTracingStages.emplace_back(
157 std::make_unique<TracedInputListener>("PointerChoreographer", *mChoreographer));
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000158
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000159 mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mTracingStages.back());
160 mTracingStages.emplace_back(
161 std::make_unique<TracedInputListener>("UnwantedInteractionBlocker", *mBlocker));
162
163 mReader = createInputReader(readerPolicy, *mTracingStages.back());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164}
165
166InputManager::~InputManager() {
167 stop();
168}
169
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700171 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 if (result) {
173 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
174 return result;
175 }
176
Prabir Pradhan28efc192019-11-05 01:10:04 +0000177 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000179 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700181 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182 return result;
183 }
184
185 return OK;
186}
187
188status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700189 status_t status = OK;
190
Prabir Pradhan28efc192019-11-05 01:10:04 +0000191 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000193 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700194 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 }
196
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700197 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198 if (result) {
199 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700200 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201 }
202
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700203 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204}
205
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700206InputReaderInterface& InputManager::getReader() {
207 return *mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208}
209
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000210PointerChoreographerInterface& InputManager::getChoreographer() {
211 return *mChoreographer;
212}
213
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700214InputProcessorInterface& InputManager::getProcessor() {
215 return *mProcessor;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800216}
217
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000218InputDeviceMetricsCollectorInterface& InputManager::getMetricsCollector() {
219 return *mCollector;
220}
221
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700222InputDispatcherInterface& InputManager::getDispatcher() {
223 return *mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224}
225
Vaibhav Devmurari953e6a42023-11-21 18:24:19 +0000226InputFilterInterface& InputManager::getInputFilter() {
227 return *mInputFilter;
228}
229
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700230void InputManager::monitor() {
231 mReader->monitor();
232 mBlocker->monitor();
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700233 mProcessor->monitor();
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000234 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
235 mCollector->monitor();
236 }
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700237 mDispatcher->monitor();
238}
239
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000240void InputManager::dump(std::string& dump) {
241 mReader->dump(dump);
242 dump += '\n';
243 mBlocker->dump(dump);
244 dump += '\n';
Prabir Pradhan657786c2024-05-03 23:19:01 +0000245 mChoreographer->dump(dump);
246 dump += '\n';
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000247 mProcessor->dump(dump);
248 dump += '\n';
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000249 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
250 mCollector->dump(dump);
251 dump += '\n';
252 }
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000253 mDispatcher->dump(dump);
254 dump += '\n';
255}
256
Robert Carr1c4c5592018-09-24 13:18:43 -0700257// Used by tests only.
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800258binder::Status InputManager::createInputChannel(const std::string& name,
259 android::os::InputChannelCore* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700260 IPCThreadState* ipc = IPCThreadState::self();
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800261 const uid_t uid = ipc->getCallingUid();
Robert Carr1c4c5592018-09-24 13:18:43 -0700262 if (uid != AID_SHELL && uid != AID_ROOT) {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800263 LOG(ERROR) << __func__ << " can only be called by SHELL or ROOT users, "
264 << "but was called from UID " << uid;
265 return binder::Status::
266 fromExceptionCode(EX_SECURITY,
267 "This uid is not allowed to call createInputChannel");
Robert Carr1c4c5592018-09-24 13:18:43 -0700268 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500269
Garfield Tan15601662020-09-22 15:32:38 -0700270 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900271 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700272 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
273 channel.error().message().c_str());
274 }
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -0800275 InputChannel::moveChannel(std::move(*channel), *outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700276 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700277}
278
Garfield Tan15601662020-09-22 15:32:38 -0700279binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
280 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700281 return binder::Status::ok();
282}
283
284status_t InputManager::dump(int fd, const Vector<String16>& args) {
285 std::string dump;
286
287 dump += " InputFlinger dump\n";
288
Siarhei Vishniakoucac84272023-06-28 14:43:25 -0700289 TEMP_FAILURE_RETRY(::write(fd, dump.c_str(), dump.size()));
Chris Ye0783e992020-06-02 21:34:49 -0700290 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700291}
292
Prabir Pradhan44e6e832023-06-06 00:03:25 +0000293binder::Status InputManager::setFocusedWindow(const gui::FocusRequest& request) {
Vishnu Naire798b472020-07-23 13:52:21 -0700294 mDispatcher->setFocusedWindow(request);
295 return binder::Status::ok();
296}
297
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298} // namespace android