blob: 472d7a15b4b91d7d3b50d94619b28e0037b5b866 [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 */
Michael Wrightd02c5b62014-02-10 15:10:22 -080060InputManager::InputManager(
Michael Wrightd02c5b62014-02-10 15:10:22 -080061 const sp<InputReaderPolicyInterface>& readerPolicy,
62 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
Garfield Tane84e6f92019-08-29 17:28:41 -070063 mDispatcher = createInputDispatcher(dispatcherPolicy);
Siarhei Vishniakou98996032022-08-03 11:54:47 -070064 mProcessor = std::make_unique<InputProcessor>(*mDispatcher);
65 mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mProcessor);
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -070066 mReader = createInputReader(readerPolicy, *mBlocker);
Michael Wrightd02c5b62014-02-10 15:10:22 -080067}
68
69InputManager::~InputManager() {
70 stop();
71}
72
Michael Wrightd02c5b62014-02-10 15:10:22 -080073status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070074 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080075 if (result) {
76 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
77 return result;
78 }
79
Prabir Pradhan28efc192019-11-05 01:10:04 +000080 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080081 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000082 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -080083
Prabir Pradhan3608aad2019-10-02 17:08:26 -070084 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080085 return result;
86 }
87
88 return OK;
89}
90
91status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070092 status_t status = OK;
93
Prabir Pradhan28efc192019-11-05 01:10:04 +000094 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080095 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000096 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -070097 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -080098 }
99
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700100 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101 if (result) {
102 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700103 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104 }
105
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700106 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107}
108
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700109InputReaderInterface& InputManager::getReader() {
110 return *mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111}
112
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700113InputProcessorInterface& InputManager::getProcessor() {
114 return *mProcessor;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800115}
116
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700117InputDispatcherInterface& InputManager::getDispatcher() {
118 return *mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119}
120
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700121void InputManager::monitor() {
122 mReader->monitor();
123 mBlocker->monitor();
Siarhei Vishniakou98996032022-08-03 11:54:47 -0700124 mProcessor->monitor();
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700125 mDispatcher->monitor();
126}
127
Prabir Pradhan370dbc92023-04-06 00:02:05 +0000128void InputManager::dump(std::string& dump) {
129 mReader->dump(dump);
130 dump += '\n';
131 mBlocker->dump(dump);
132 dump += '\n';
133 mProcessor->dump(dump);
134 dump += '\n';
135 mDispatcher->dump(dump);
136 dump += '\n';
137}
138
Robert Carr1c4c5592018-09-24 13:18:43 -0700139// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700140binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700141 IPCThreadState* ipc = IPCThreadState::self();
142 const int uid = ipc->getCallingUid();
143 if (uid != AID_SHELL && uid != AID_ROOT) {
144 ALOGE("Invalid attempt to register input channel over IPC"
145 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
Chris Ye0783e992020-06-02 21:34:49 -0700146 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700147 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500148
Garfield Tan15601662020-09-22 15:32:38 -0700149 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900150 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700151 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
152 channel.error().message().c_str());
153 }
154 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700155 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700156}
157
Garfield Tan15601662020-09-22 15:32:38 -0700158binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
159 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700160 return binder::Status::ok();
161}
162
163status_t InputManager::dump(int fd, const Vector<String16>& args) {
164 std::string dump;
165
166 dump += " InputFlinger dump\n";
167
168 ::write(fd, dump.c_str(), dump.size());
169 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700170}
171
Vishnu Naire798b472020-07-23 13:52:21 -0700172binder::Status InputManager::setFocusedWindow(const FocusRequest& request) {
173 mDispatcher->setFocusedWindow(request);
174 return binder::Status::ok();
175}
176
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177} // namespace android