blob: 0567a32550fadffc4d6eb41a6093761a704bce22 [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 Pradhan44e6e832023-06-06 00:03:25 +000030#include <inputflinger_bootstrap.rs.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070031#include <log/log.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070032#include <private/android_filesystem_config.h>
33
Michael Wrightd02c5b62014-02-10 15:10:22 -080034namespace android {
35
Prabir Pradhan44e6e832023-06-06 00:03:25 +000036namespace {
37
38const bool ENABLE_INPUT_DEVICE_USAGE_METRICS =
Prabir Pradhan7adabad2023-06-28 16:16:27 +000039 sysprop::InputProperties::enable_input_device_usage_metrics().value_or(true);
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000040
Prabir Pradhanb56e92c2023-06-09 23:40:37 +000041const bool ENABLE_POINTER_CHOREOGRAPHER =
42 sysprop::InputProperties::enable_pointer_choreographer().value_or(false);
43
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
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000119 * -> PointerChoreographer
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000120 * -> InputProcessor
121 * -> InputDeviceMetricsCollector
122 * -> InputDispatcher
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700123 */
Prabir Pradhana41d2442023-04-20 21:30:40 +0000124InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000125 InputDispatcherPolicyInterface& dispatcherPolicy,
126 PointerChoreographerPolicyInterface& choreographerPolicy) {
Prabir Pradhan44e6e832023-06-06 00:03:25 +0000127 mInputFlingerRust = createInputFlingerRust();
128
Garfield Tane84e6f92019-08-29 17:28:41 -0700129 mDispatcher = createInputDispatcher(dispatcherPolicy);
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000130 mTracingStages.emplace_back(
131 std::make_unique<TracedInputListener>("InputDispatcher", *mDispatcher));
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000132
133 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000134 mCollector = std::make_unique<InputDeviceMetricsCollector>(*mTracingStages.back());
135 mTracingStages.emplace_back(
136 std::make_unique<TracedInputListener>("MetricsCollector", *mCollector));
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000137 }
138
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000139 mProcessor = std::make_unique<InputProcessor>(*mTracingStages.back());
140 mTracingStages.emplace_back(
141 std::make_unique<TracedInputListener>("InputProcessor", *mProcessor));
142
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000143 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 Pradhan274aa2c2023-08-18 17:27:07 +0000150 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 Wrightd02c5b62014-02-10 15:10:22 -0800155}
156
157InputManager::~InputManager() {
158 stop();
159}
160
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700162 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 if (result) {
164 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
165 return result;
166 }
167
Prabir Pradhan28efc192019-11-05 01:10:04 +0000168 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000170 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700172 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173 return result;
174 }
175
176 return OK;
177}
178
179status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700180 status_t status = OK;
181
Prabir Pradhan28efc192019-11-05 01:10:04 +0000182 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000184 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700185 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 }
187
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700188 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189 if (result) {
190 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700191 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 }
193
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700194 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195}
196
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700197InputReaderInterface& InputManager::getReader() {
198 return *mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199}
200
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000201PointerChoreographerInterface& InputManager::getChoreographer() {
202 return *mChoreographer;
203}
204
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700205InputProcessorInterface& InputManager::getProcessor() {
206 return *mProcessor;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800207}
208
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000209InputDeviceMetricsCollectorInterface& InputManager::getMetricsCollector() {
210 return *mCollector;
211}
212
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700213InputDispatcherInterface& InputManager::getDispatcher() {
214 return *mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215}
216
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700217void InputManager::monitor() {
218 mReader->monitor();
219 mBlocker->monitor();
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700220 mProcessor->monitor();
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700221 mDispatcher->monitor();
222}
223
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000224void InputManager::dump(std::string& dump) {
225 mReader->dump(dump);
226 dump += '\n';
227 mBlocker->dump(dump);
228 dump += '\n';
Prabir Pradhanb56e92c2023-06-09 23:40:37 +0000229 if (ENABLE_POINTER_CHOREOGRAPHER) {
230 mChoreographer->dump(dump);
231 dump += '\n';
232 }
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000233 mProcessor->dump(dump);
234 dump += '\n';
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000235 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
236 mCollector->dump(dump);
237 dump += '\n';
238 }
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000239 mDispatcher->dump(dump);
240 dump += '\n';
241}
242
Robert Carr1c4c5592018-09-24 13:18:43 -0700243// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700244binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700245 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 Ye0783e992020-06-02 21:34:49 -0700250 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700251 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500252
Garfield Tan15601662020-09-22 15:32:38 -0700253 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900254 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700255 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
256 channel.error().message().c_str());
257 }
258 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700259 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700260}
261
Garfield Tan15601662020-09-22 15:32:38 -0700262binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
263 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700264 return binder::Status::ok();
265}
266
267status_t InputManager::dump(int fd, const Vector<String16>& args) {
268 std::string dump;
269
270 dump += " InputFlinger dump\n";
271
Siarhei Vishniakoucac84272023-06-28 14:43:25 -0700272 TEMP_FAILURE_RETRY(::write(fd, dump.c_str(), dump.size()));
Chris Ye0783e992020-06-02 21:34:49 -0700273 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700274}
275
Prabir Pradhan44e6e832023-06-06 00:03:25 +0000276binder::Status InputManager::setFocusedWindow(const gui::FocusRequest& request) {
Vishnu Naire798b472020-07-23 13:52:21 -0700277 mDispatcher->setFocusedWindow(request);
278 return binder::Status::ok();
279}
280
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281} // namespace android