blob: 9767cd9b7139f924f66ce4f5758b01d46261bb0c [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:
58 * InputReader -> UnwantedInteractionBlocker -> InputClassifier -> InputDispatcher
59 */
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 Vishniakou18050092021-09-01 13:32:49 -070064 mClassifier = std::make_unique<InputClassifier>(*mDispatcher);
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -070065 mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mClassifier);
66 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 Vishniakouba0a8752021-09-14 14:43:25 -0700113UnwantedInteractionBlockerInterface& InputManager::getUnwantedInteractionBlocker() {
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700114 return *mBlocker;
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700115}
116
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700117InputClassifierInterface& InputManager::getClassifier() {
118 return *mClassifier;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800119}
120
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700121InputDispatcherInterface& InputManager::getDispatcher() {
122 return *mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123}
124
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700125void InputManager::monitor() {
126 mReader->monitor();
127 mBlocker->monitor();
128 mClassifier->monitor();
129 mDispatcher->monitor();
130}
131
Robert Carr1c4c5592018-09-24 13:18:43 -0700132// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700133binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700134 IPCThreadState* ipc = IPCThreadState::self();
135 const int uid = ipc->getCallingUid();
136 if (uid != AID_SHELL && uid != AID_ROOT) {
137 ALOGE("Invalid attempt to register input channel over IPC"
138 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
Chris Ye0783e992020-06-02 21:34:49 -0700139 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700140 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500141
Garfield Tan15601662020-09-22 15:32:38 -0700142 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900143 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700144 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
145 channel.error().message().c_str());
146 }
147 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700148 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700149}
150
Garfield Tan15601662020-09-22 15:32:38 -0700151binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
152 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700153 return binder::Status::ok();
154}
155
156status_t InputManager::dump(int fd, const Vector<String16>& args) {
157 std::string dump;
158
159 dump += " InputFlinger dump\n";
160
161 ::write(fd, dump.c_str(), dump.size());
162 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700163}
164
Vishnu Naire798b472020-07-23 13:52:21 -0700165binder::Status InputManager::setFocusedWindow(const FocusRequest& request) {
166 mDispatcher->setFocusedWindow(request);
167 return binder::Status::ok();
168}
169
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170} // namespace android