blob: 92c65e19b64791a91e931e413b55d06fea76c14c [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 Pradhan678405c2023-09-08 17:18:32 +000044const bool ENABLE_POINTER_CHOREOGRAPHER = input_flags::enable_pointer_choreographer();
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
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000121 * -> PointerChoreographer
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000122 * -> InputProcessor
123 * -> InputDeviceMetricsCollector
124 * -> InputDispatcher
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700125 */
Prabir Pradhana41d2442023-04-20 21:30:40 +0000126InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000127 InputDispatcherPolicyInterface& dispatcherPolicy,
128 PointerChoreographerPolicyInterface& choreographerPolicy) {
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
135 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000136 mCollector = std::make_unique<InputDeviceMetricsCollector>(*mTracingStages.back());
137 mTracingStages.emplace_back(
138 std::make_unique<TracedInputListener>("MetricsCollector", *mCollector));
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000139 }
140
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000141 mProcessor = std::make_unique<InputProcessor>(*mTracingStages.back());
142 mTracingStages.emplace_back(
143 std::make_unique<TracedInputListener>("InputProcessor", *mProcessor));
144
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000145 if (ENABLE_POINTER_CHOREOGRAPHER) {
146 mChoreographer =
147 std::make_unique<PointerChoreographer>(*mTracingStages.back(), choreographerPolicy);
148 mTracingStages.emplace_back(
149 std::make_unique<TracedInputListener>("PointerChoreographer", *mChoreographer));
150 }
151
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000152 mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mTracingStages.back());
153 mTracingStages.emplace_back(
154 std::make_unique<TracedInputListener>("UnwantedInteractionBlocker", *mBlocker));
155
156 mReader = createInputReader(readerPolicy, *mTracingStages.back());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157}
158
159InputManager::~InputManager() {
160 stop();
161}
162
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700164 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165 if (result) {
166 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
167 return result;
168 }
169
Prabir Pradhan28efc192019-11-05 01:10:04 +0000170 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000172 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700174 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175 return result;
176 }
177
178 return OK;
179}
180
181status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700182 status_t status = OK;
183
Prabir Pradhan28efc192019-11-05 01:10:04 +0000184 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000186 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700187 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 }
189
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700190 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191 if (result) {
192 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700193 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800194 }
195
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700196 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800197}
198
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700199InputReaderInterface& InputManager::getReader() {
200 return *mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800201}
202
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000203PointerChoreographerInterface& InputManager::getChoreographer() {
204 return *mChoreographer;
205}
206
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700207InputProcessorInterface& InputManager::getProcessor() {
208 return *mProcessor;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800209}
210
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000211InputDeviceMetricsCollectorInterface& InputManager::getMetricsCollector() {
212 return *mCollector;
213}
214
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700215InputDispatcherInterface& InputManager::getDispatcher() {
216 return *mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217}
218
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700219void InputManager::monitor() {
220 mReader->monitor();
221 mBlocker->monitor();
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700222 mProcessor->monitor();
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700223 mDispatcher->monitor();
224}
225
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000226void InputManager::dump(std::string& dump) {
227 mReader->dump(dump);
228 dump += '\n';
229 mBlocker->dump(dump);
230 dump += '\n';
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000231 if (ENABLE_POINTER_CHOREOGRAPHER) {
232 mChoreographer->dump(dump);
233 dump += '\n';
234 }
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000235 mProcessor->dump(dump);
236 dump += '\n';
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000237 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
238 mCollector->dump(dump);
239 dump += '\n';
240 }
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000241 mDispatcher->dump(dump);
242 dump += '\n';
243}
244
Robert Carr1c4c5592018-09-24 13:18:43 -0700245// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700246binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700247 IPCThreadState* ipc = IPCThreadState::self();
248 const int uid = ipc->getCallingUid();
249 if (uid != AID_SHELL && uid != AID_ROOT) {
250 ALOGE("Invalid attempt to register input channel over IPC"
251 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
Chris Ye0783e992020-06-02 21:34:49 -0700252 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700253 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500254
Garfield Tan15601662020-09-22 15:32:38 -0700255 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900256 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700257 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
258 channel.error().message().c_str());
259 }
260 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700261 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700262}
263
Garfield Tan15601662020-09-22 15:32:38 -0700264binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
265 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700266 return binder::Status::ok();
267}
268
269status_t InputManager::dump(int fd, const Vector<String16>& args) {
270 std::string dump;
271
272 dump += " InputFlinger dump\n";
273
Siarhei Vishniakoucac84272023-06-28 14:43:25 -0700274 TEMP_FAILURE_RETRY(::write(fd, dump.c_str(), dump.size()));
Chris Ye0783e992020-06-02 21:34:49 -0700275 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700276}
277
Prabir Pradhan44e6e832023-06-06 00:03:25 +0000278binder::Status InputManager::setFocusedWindow(const gui::FocusRequest& request) {
Vishnu Naire798b472020-07-23 13:52:21 -0700279 mDispatcher->setFocusedWindow(request);
280 return binder::Status::ok();
281}
282
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283} // namespace android