blob: ddebcad0d30bfb273778a11981fd533928ab305a [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
Robert Carr1c4c5592018-09-24 13:18:43 -070026#include <binder/IPCThreadState.h>
27
Mark Salyzyn7823e122016-09-29 08:08:05 -070028#include <log/log.h>
Robert Carr1cc78672018-07-31 14:25:57 -070029#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Robert Carr1c4c5592018-09-24 13:18:43 -070031#include <private/android_filesystem_config.h>
32
Michael Wrightd02c5b62014-02-10 15:10:22 -080033namespace android {
34
chaviw3277faf2021-05-19 16:45:23 -050035using gui::FocusRequest;
chaviw3277faf2021-05-19 16:45:23 -050036
Garfield Tan15601662020-09-22 15:32:38 -070037static int32_t exceptionCodeFromStatusT(status_t status) {
38 switch (status) {
39 case OK:
40 return binder::Status::EX_NONE;
41 case INVALID_OPERATION:
42 return binder::Status::EX_UNSUPPORTED_OPERATION;
43 case BAD_VALUE:
44 case BAD_TYPE:
45 case NAME_NOT_FOUND:
46 return binder::Status::EX_ILLEGAL_ARGUMENT;
47 case NO_INIT:
48 return binder::Status::EX_ILLEGAL_STATE;
49 case PERMISSION_DENIED:
50 return binder::Status::EX_SECURITY;
51 default:
52 return binder::Status::EX_TRANSACTION_FAILED;
53 }
54}
55
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070056/**
57 * The event flow is via the "InputListener" interface, as follows:
Siarhei Vishniakou98996032022-08-03 11:54:47 -070058 * InputReader -> UnwantedInteractionBlocker -> InputProcessor -> InputDispatcher
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070059 */
Prabir Pradhana41d2442023-04-20 21:30:40 +000060InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,
61 InputDispatcherPolicyInterface& dispatcherPolicy) {
Garfield Tane84e6f92019-08-29 17:28:41 -070062 mDispatcher = createInputDispatcher(dispatcherPolicy);
Siarhei Vishniakou98996032022-08-03 11:54:47 -070063 mProcessor = std::make_unique<InputProcessor>(*mDispatcher);
64 mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mProcessor);
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -070065 mReader = createInputReader(readerPolicy, *mBlocker);
Michael Wrightd02c5b62014-02-10 15:10:22 -080066}
67
68InputManager::~InputManager() {
69 stop();
70}
71
Michael Wrightd02c5b62014-02-10 15:10:22 -080072status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070073 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080074 if (result) {
75 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
76 return result;
77 }
78
Prabir Pradhan28efc192019-11-05 01:10:04 +000079 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080080 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000081 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -080082
Prabir Pradhan3608aad2019-10-02 17:08:26 -070083 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080084 return result;
85 }
86
87 return OK;
88}
89
90status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070091 status_t status = OK;
92
Prabir Pradhan28efc192019-11-05 01:10:04 +000093 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080094 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000095 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -070096 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 }
98
Prabir Pradhan3608aad2019-10-02 17:08:26 -070099 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100 if (result) {
101 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700102 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103 }
104
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700105 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106}
107
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700108InputReaderInterface& InputManager::getReader() {
109 return *mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110}
111
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700112InputProcessorInterface& InputManager::getProcessor() {
113 return *mProcessor;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800114}
115
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700116InputDispatcherInterface& InputManager::getDispatcher() {
117 return *mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118}
119
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700120void InputManager::monitor() {
121 mReader->monitor();
122 mBlocker->monitor();
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700123 mProcessor->monitor();
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700124 mDispatcher->monitor();
125}
126
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000127void InputManager::dump(std::string& dump) {
128 mReader->dump(dump);
129 dump += '\n';
130 mBlocker->dump(dump);
131 dump += '\n';
132 mProcessor->dump(dump);
133 dump += '\n';
134 mDispatcher->dump(dump);
135 dump += '\n';
136}
137
Robert Carr1c4c5592018-09-24 13:18:43 -0700138// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700139binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700140 IPCThreadState* ipc = IPCThreadState::self();
141 const int uid = ipc->getCallingUid();
142 if (uid != AID_SHELL && uid != AID_ROOT) {
143 ALOGE("Invalid attempt to register input channel over IPC"
144 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
Chris Ye0783e992020-06-02 21:34:49 -0700145 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700146 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500147
Garfield Tan15601662020-09-22 15:32:38 -0700148 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900149 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700150 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
151 channel.error().message().c_str());
152 }
153 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700154 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700155}
156
Garfield Tan15601662020-09-22 15:32:38 -0700157binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
158 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700159 return binder::Status::ok();
160}
161
162status_t InputManager::dump(int fd, const Vector<String16>& args) {
163 std::string dump;
164
165 dump += " InputFlinger dump\n";
166
167 ::write(fd, dump.c_str(), dump.size());
168 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700169}
170
Vishnu Naire798b472020-07-23 13:52:21 -0700171binder::Status InputManager::setFocusedWindow(const FocusRequest& request) {
172 mDispatcher->setFocusedWindow(request);
173 return binder::Status::ok();
174}
175
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176} // namespace android