blob: 37b31875f8b973c6f8f2897db40a2e3b31bb41d9 [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 Pradhanaddf8e92023-04-06 00:28:48 +000026#include <android/sysprop/InputProperties.sysprop.h>
Robert Carr1c4c5592018-09-24 13:18:43 -070027#include <binder/IPCThreadState.h>
28
Mark Salyzyn7823e122016-09-29 08:08:05 -070029#include <log/log.h>
Robert Carr1cc78672018-07-31 14:25:57 -070030#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080031
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 Pradhanaddf8e92023-04-06 00:28:48 +000036static const bool ENABLE_INPUT_DEVICE_USAGE_METRICS =
Prabir Pradhan7adabad2023-06-28 16:16:27 +000037 sysprop::InputProperties::enable_input_device_usage_metrics().value_or(true);
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000038
chaviw3277faf2021-05-19 16:45:23 -050039using gui::FocusRequest;
chaviw3277faf2021-05-19 16:45:23 -050040
Garfield Tan15601662020-09-22 15:32:38 -070041static int32_t exceptionCodeFromStatusT(status_t status) {
42 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
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070060/**
61 * The event flow is via the "InputListener" interface, as follows:
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000062 * InputReader
63 * -> UnwantedInteractionBlocker
64 * -> InputProcessor
65 * -> InputDeviceMetricsCollector
66 * -> InputDispatcher
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070067 */
Prabir Pradhana41d2442023-04-20 21:30:40 +000068InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,
69 InputDispatcherPolicyInterface& dispatcherPolicy) {
Garfield Tane84e6f92019-08-29 17:28:41 -070070 mDispatcher = createInputDispatcher(dispatcherPolicy);
Prabir Pradhanaddf8e92023-04-06 00:28:48 +000071
72 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
73 mCollector = std::make_unique<InputDeviceMetricsCollector>(*mDispatcher);
74 }
75
76 mProcessor = ENABLE_INPUT_DEVICE_USAGE_METRICS ? std::make_unique<InputProcessor>(*mCollector)
77 : std::make_unique<InputProcessor>(*mDispatcher);
Siarhei Vishniakou98996032022-08-03 11:54:47 -070078 mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mProcessor);
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -070079 mReader = createInputReader(readerPolicy, *mBlocker);
Michael Wrightd02c5b62014-02-10 15:10:22 -080080}
81
82InputManager::~InputManager() {
83 stop();
84}
85
Michael Wrightd02c5b62014-02-10 15:10:22 -080086status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070087 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080088 if (result) {
89 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
90 return result;
91 }
92
Prabir Pradhan28efc192019-11-05 01:10:04 +000093 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080094 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000095 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -080096
Prabir Pradhan3608aad2019-10-02 17:08:26 -070097 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080098 return result;
99 }
100
101 return OK;
102}
103
104status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700105 status_t status = OK;
106
Prabir Pradhan28efc192019-11-05 01:10:04 +0000107 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800108 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +0000109 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700110 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111 }
112
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700113 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114 if (result) {
115 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700116 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117 }
118
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700119 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120}
121
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700122InputReaderInterface& InputManager::getReader() {
123 return *mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124}
125
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700126InputProcessorInterface& InputManager::getProcessor() {
127 return *mProcessor;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800128}
129
Prabir Pradhan8ede1d12023-05-08 19:37:44 +0000130InputDeviceMetricsCollectorInterface& InputManager::getMetricsCollector() {
131 return *mCollector;
132}
133
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700134InputDispatcherInterface& InputManager::getDispatcher() {
135 return *mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136}
137
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700138void InputManager::monitor() {
139 mReader->monitor();
140 mBlocker->monitor();
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700141 mProcessor->monitor();
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700142 mDispatcher->monitor();
143}
144
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000145void InputManager::dump(std::string& dump) {
146 mReader->dump(dump);
147 dump += '\n';
148 mBlocker->dump(dump);
149 dump += '\n';
150 mProcessor->dump(dump);
151 dump += '\n';
Prabir Pradhanaddf8e92023-04-06 00:28:48 +0000152 if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
153 mCollector->dump(dump);
154 dump += '\n';
155 }
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000156 mDispatcher->dump(dump);
157 dump += '\n';
158}
159
Robert Carr1c4c5592018-09-24 13:18:43 -0700160// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700161binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700162 IPCThreadState* ipc = IPCThreadState::self();
163 const int uid = ipc->getCallingUid();
164 if (uid != AID_SHELL && uid != AID_ROOT) {
165 ALOGE("Invalid attempt to register input channel over IPC"
166 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
Chris Ye0783e992020-06-02 21:34:49 -0700167 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700168 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500169
Garfield Tan15601662020-09-22 15:32:38 -0700170 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900171 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700172 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
173 channel.error().message().c_str());
174 }
175 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700176 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700177}
178
Garfield Tan15601662020-09-22 15:32:38 -0700179binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
180 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700181 return binder::Status::ok();
182}
183
184status_t InputManager::dump(int fd, const Vector<String16>& args) {
185 std::string dump;
186
187 dump += " InputFlinger dump\n";
188
189 ::write(fd, dump.c_str(), dump.size());
190 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700191}
192
Vishnu Naire798b472020-07-23 13:52:21 -0700193binder::Status InputManager::setFocusedWindow(const FocusRequest& request) {
194 mDispatcher->setFocusedWindow(request);
195 return binder::Status::ok();
196}
197
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198} // namespace android