blob: 7d62ed9461426866b56e5823529a94c953bed773 [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
Prabir Pradhan44e6e832023-06-06 00:03:25 +000044int32_t exceptionCodeFromStatusT(status_t status) {
Garfield Tan15601662020-09-22 15:32:38 -070045 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 Pradhan44e6e832023-06-06 00:03:25 +000063// Convert a binder interface into a raw pointer to an AIBinder.
64using IInputFlingerRustBootstrapCallback = aidl::com::android::server::inputflinger::
65 IInputFlingerRust::IInputFlingerRustBootstrapCallback;
66IInputFlingerRustBootstrapCallbackAIBinder* 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.
83std::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 Vishniakouba0a8752021-09-14 14:43:25 -0700115/**
116 * The event flow is via the "InputListener" interface, as follows:
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000117 * InputReader
118 * -> UnwantedInteractionBlocker
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +0000119 * -> InputFilter
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000120 * -> PointerChoreographer
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000121 * -> InputProcessor
122 * -> InputDeviceMetricsCollector
123 * -> InputDispatcher
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700124 */
Prabir Pradhana41d2442023-04-20 21:30:40 +0000125InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000126 InputDispatcherPolicyInterface& dispatcherPolicy,
Vaibhav Devmurarie948d4b2023-12-28 13:41:32 +0000127 PointerChoreographerPolicyInterface& choreographerPolicy,
128 InputFilterPolicyInterface& inputFilterPolicy) {
Prabir Pradhan44e6e832023-06-06 00:03:25 +0000129 mInputFlingerRust = createInputFlingerRust();
130
Garfield Tane84e6f92019-08-29 17:28:41 -0700131 mDispatcher = createInputDispatcher(dispatcherPolicy);
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000132 mTracingStages.emplace_back(
133 std::make_unique<TracedInputListener>("InputDispatcher", *mDispatcher));
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000134
Vaibhav Devmurarie754bff2025-01-23 17:32:41 +0000135 mInputFilter = std::make_unique<InputFilter>(*mTracingStages.back(), *mInputFlingerRust,
136 inputFilterPolicy);
137 mTracingStages.emplace_back(
138 std::make_unique<TracedInputListener>("InputFilter", *mInputFilter));
Vaibhav Devmurari5766aee2023-11-03 17:21:25 +0000139
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000140 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000141 mCollector = std::make_unique<InputDeviceMetricsCollector>(*mTracingStages.back());
142 mTracingStages.emplace_back(
143 std::make_unique<TracedInputListener>("MetricsCollector", *mCollector));
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000144 }
145
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000146 mProcessor = std::make_unique<InputProcessor>(*mTracingStages.back());
147 mTracingStages.emplace_back(
148 std::make_unique<TracedInputListener>("InputProcessor", *mProcessor));
149
Prabir Pradhan657786c2024-05-03 23:19:01 +0000150 mChoreographer =
151 std::make_unique<PointerChoreographer>(*mTracingStages.back(), choreographerPolicy);
152 mTracingStages.emplace_back(
153 std::make_unique<TracedInputListener>("PointerChoreographer", *mChoreographer));
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000154
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000155 mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mTracingStages.back());
156 mTracingStages.emplace_back(
157 std::make_unique<TracedInputListener>("UnwantedInteractionBlocker", *mBlocker));
158
159 mReader = createInputReader(readerPolicy, *mTracingStages.back());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160}
161
162InputManager::~InputManager() {
163 stop();
164}
165
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700167 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 if (result) {
169 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
170 return result;
171 }
172
Prabir Pradhan28efc192019-11-05 01:10:04 +0000173 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000175 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700177 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 return result;
179 }
180
181 return OK;
182}
183
184status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700185 status_t status = OK;
186
Prabir Pradhan28efc192019-11-05 01:10:04 +0000187 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000189 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700190 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191 }
192
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700193 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800194 if (result) {
195 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700196 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800197 }
198
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700199 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200}
201
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700202InputReaderInterface& InputManager::getReader() {
203 return *mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204}
205
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000206PointerChoreographerInterface& InputManager::getChoreographer() {
207 return *mChoreographer;
208}
209
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700210InputProcessorInterface& InputManager::getProcessor() {
211 return *mProcessor;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800212}
213
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000214InputDeviceMetricsCollectorInterface& InputManager::getMetricsCollector() {
215 return *mCollector;
216}
217
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700218InputDispatcherInterface& InputManager::getDispatcher() {
219 return *mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220}
221
Vaibhav Devmurari953e6a42023-11-21 18:24:19 +0000222InputFilterInterface& InputManager::getInputFilter() {
223 return *mInputFilter;
224}
225
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700226void InputManager::monitor() {
227 mReader->monitor();
228 mBlocker->monitor();
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700229 mProcessor->monitor();
Prabir Pradhan95019ac2023-12-06 22:38:21 +0000230 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
231 mCollector->monitor();
232 }
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700233 mDispatcher->monitor();
234}
235
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000236void InputManager::dump(std::string& dump) {
237 mReader->dump(dump);
238 dump += '\n';
239 mBlocker->dump(dump);
240 dump += '\n';
Prabir Pradhan657786c2024-05-03 23:19:01 +0000241 mChoreographer->dump(dump);
242 dump += '\n';
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000243 mProcessor->dump(dump);
244 dump += '\n';
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000245 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
246 mCollector->dump(dump);
247 dump += '\n';
248 }
Vaibhav Devmurarie754bff2025-01-23 17:32:41 +0000249 mInputFilter->dump(dump);
250 dump += '\n';
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000251 mDispatcher->dump(dump);
252 dump += '\n';
253}
254
Robert Carr1c4c5592018-09-24 13:18:43 -0700255// Used by tests only.
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800256binder::Status InputManager::createInputChannel(const std::string& name,
257 android::os::InputChannelCore* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700258 IPCThreadState* ipc = IPCThreadState::self();
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800259 const uid_t uid = ipc->getCallingUid();
Robert Carr1c4c5592018-09-24 13:18:43 -0700260 if (uid != AID_SHELL && uid != AID_ROOT) {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800261 LOG(ERROR) << __func__ << " can only be called by SHELL or ROOT users, "
262 << "but was called from UID " << uid;
263 return binder::Status::
264 fromExceptionCode(EX_SECURITY,
265 "This uid is not allowed to call createInputChannel");
Robert Carr1c4c5592018-09-24 13:18:43 -0700266 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500267
Garfield Tan15601662020-09-22 15:32:38 -0700268 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900269 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700270 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
271 channel.error().message().c_str());
272 }
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -0800273 InputChannel::moveChannel(std::move(*channel), *outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700274 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700275}
276
Garfield Tan15601662020-09-22 15:32:38 -0700277binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
278 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700279 return binder::Status::ok();
280}
281
282status_t InputManager::dump(int fd, const Vector<String16>& args) {
283 std::string dump;
284
285 dump += " InputFlinger dump\n";
286
Siarhei Vishniakoucac84272023-06-28 14:43:25 -0700287 TEMP_FAILURE_RETRY(::write(fd, dump.c_str(), dump.size()));
Chris Ye0783e992020-06-02 21:34:49 -0700288 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700289}
290
Prabir Pradhan44e6e832023-06-06 00:03:25 +0000291binder::Status InputManager::setFocusedWindow(const gui::FocusRequest& request) {
Vishnu Naire798b472020-07-23 13:52:21 -0700292 mDispatcher->setFocusedWindow(request);
293 return binder::Status::ok();
294}
295
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296} // namespace android