blob: 0733a1c7c6a57c63f41b9d2fccd910ef6277eb98 [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 Pradhan44e6e832023-06-06 00:03:25 +000041int32_t exceptionCodeFromStatusT(status_t status) {
Garfield Tan15601662020-09-22 15:32:38 -070042 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 Pradhan44e6e832023-06-06 00:03:25 +000060// Convert a binder interface into a raw pointer to an AIBinder.
61using IInputFlingerRustBootstrapCallback = aidl::com::android::server::inputflinger::
62 IInputFlingerRust::IInputFlingerRustBootstrapCallback;
63IInputFlingerRustBootstrapCallbackAIBinder* 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.
80std::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 Vishniakouba0a8752021-09-14 14:43:25 -0700112/**
113 * The event flow is via the "InputListener" interface, as follows:
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000114 * InputReader
115 * -> UnwantedInteractionBlocker
116 * -> InputProcessor
117 * -> InputDeviceMetricsCollector
118 * -> InputDispatcher
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700119 */
Prabir Pradhana41d2442023-04-20 21:30:40 +0000120InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,
121 InputDispatcherPolicyInterface& dispatcherPolicy) {
Prabir Pradhan44e6e832023-06-06 00:03:25 +0000122 mInputFlingerRust = createInputFlingerRust();
123
Garfield Tane84e6f92019-08-29 17:28:41 -0700124 mDispatcher = createInputDispatcher(dispatcherPolicy);
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000125 mTracingStages.emplace_back(
126 std::make_unique<TracedInputListener>("InputDispatcher", *mDispatcher));
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000127
128 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000129 mCollector = std::make_unique<InputDeviceMetricsCollector>(*mTracingStages.back());
130 mTracingStages.emplace_back(
131 std::make_unique<TracedInputListener>("MetricsCollector", *mCollector));
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000132 }
133
Prabir Pradhan274aa2c2023-08-18 17:27:07 +0000134 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 Wrightd02c5b62014-02-10 15:10:22 -0800143}
144
145InputManager::~InputManager() {
146 stop();
147}
148
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700150 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151 if (result) {
152 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
153 return result;
154 }
155
Prabir Pradhan28efc192019-11-05 01:10:04 +0000156 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000158 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700160 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161 return result;
162 }
163
164 return OK;
165}
166
167status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700168 status_t status = OK;
169
Prabir Pradhan28efc192019-11-05 01:10:04 +0000170 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000172 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700173 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174 }
175
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700176 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177 if (result) {
178 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700179 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 }
181
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700182 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183}
184
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700185InputReaderInterface& InputManager::getReader() {
186 return *mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187}
188
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700189InputProcessorInterface& InputManager::getProcessor() {
190 return *mProcessor;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800191}
192
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000193InputDeviceMetricsCollectorInterface& InputManager::getMetricsCollector() {
194 return *mCollector;
195}
196
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700197InputDispatcherInterface& InputManager::getDispatcher() {
198 return *mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199}
200
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700201void InputManager::monitor() {
202 mReader->monitor();
203 mBlocker->monitor();
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700204 mProcessor->monitor();
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700205 mDispatcher->monitor();
206}
207
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000208void 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 Pradhanaddf8e92023-04-06 00:28:48 +0000215 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
216 mCollector->dump(dump);
217 dump += '\n';
218 }
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000219 mDispatcher->dump(dump);
220 dump += '\n';
221}
222
Robert Carr1c4c5592018-09-24 13:18:43 -0700223// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700224binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700225 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 Ye0783e992020-06-02 21:34:49 -0700230 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700231 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500232
Garfield Tan15601662020-09-22 15:32:38 -0700233 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900234 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700235 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
236 channel.error().message().c_str());
237 }
238 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700239 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700240}
241
Garfield Tan15601662020-09-22 15:32:38 -0700242binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
243 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700244 return binder::Status::ok();
245}
246
247status_t InputManager::dump(int fd, const Vector<String16>& args) {
248 std::string dump;
249
250 dump += " InputFlinger dump\n";
251
Siarhei Vishniakoucac84272023-06-28 14:43:25 -0700252 TEMP_FAILURE_RETRY(::write(fd, dump.c_str(), dump.size()));
Chris Ye0783e992020-06-02 21:34:49 -0700253 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700254}
255
Prabir Pradhan44e6e832023-06-06 00:03:25 +0000256binder::Status InputManager::setFocusedWindow(const gui::FocusRequest& request) {
Vishnu Naire798b472020-07-23 13:52:21 -0700257 mDispatcher->setFocusedWindow(request);
258 return binder::Status::ok();
259}
260
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261} // namespace android