blob: 7a9862d0657ecb494b688dd4f96c7bba9e2732e4 [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;
36using gui::WindowInfo;
37using gui::WindowInfoHandle;
38
Garfield Tan15601662020-09-22 15:32:38 -070039static int32_t exceptionCodeFromStatusT(status_t status) {
40 switch (status) {
41 case OK:
42 return binder::Status::EX_NONE;
43 case INVALID_OPERATION:
44 return binder::Status::EX_UNSUPPORTED_OPERATION;
45 case BAD_VALUE:
46 case BAD_TYPE:
47 case NAME_NOT_FOUND:
48 return binder::Status::EX_ILLEGAL_ARGUMENT;
49 case NO_INIT:
50 return binder::Status::EX_ILLEGAL_STATE;
51 case PERMISSION_DENIED:
52 return binder::Status::EX_SECURITY;
53 default:
54 return binder::Status::EX_TRANSACTION_FAILED;
55 }
56}
57
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070058/**
59 * The event flow is via the "InputListener" interface, as follows:
60 * InputReader -> UnwantedInteractionBlocker -> InputClassifier -> InputDispatcher
61 */
Michael Wrightd02c5b62014-02-10 15:10:22 -080062InputManager::InputManager(
Michael Wrightd02c5b62014-02-10 15:10:22 -080063 const sp<InputReaderPolicyInterface>& readerPolicy,
64 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
Garfield Tane84e6f92019-08-29 17:28:41 -070065 mDispatcher = createInputDispatcher(dispatcherPolicy);
Siarhei Vishniakou18050092021-09-01 13:32:49 -070066 mClassifier = std::make_unique<InputClassifier>(*mDispatcher);
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -070067 mUnwantedInteractionBlocker = std::make_unique<UnwantedInteractionBlocker>(*mClassifier);
68 mReader = createInputReader(readerPolicy, *mUnwantedInteractionBlocker);
Michael Wrightd02c5b62014-02-10 15:10:22 -080069}
70
71InputManager::~InputManager() {
72 stop();
73}
74
Michael Wrightd02c5b62014-02-10 15:10:22 -080075status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070076 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080077 if (result) {
78 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
79 return result;
80 }
81
Prabir Pradhan28efc192019-11-05 01:10:04 +000082 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080083 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000084 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -080085
Prabir Pradhan3608aad2019-10-02 17:08:26 -070086 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080087 return result;
88 }
89
90 return OK;
91}
92
93status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070094 status_t status = OK;
95
Prabir Pradhan28efc192019-11-05 01:10:04 +000096 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000098 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -070099 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100 }
101
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700102 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103 if (result) {
104 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700105 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106 }
107
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700108 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800109}
110
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700111InputReaderInterface& InputManager::getReader() {
112 return *mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113}
114
Siarhei Vishniakouba0a8752021-09-14 14:43:25 -0700115UnwantedInteractionBlockerInterface& InputManager::getUnwantedInteractionBlocker() {
116 return *mUnwantedInteractionBlocker;
117}
118
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700119InputClassifierInterface& InputManager::getClassifier() {
120 return *mClassifier;
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800121}
122
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700123InputDispatcherInterface& InputManager::getDispatcher() {
124 return *mDispatcher;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125}
126
Robert Carr1c4c5592018-09-24 13:18:43 -0700127// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700128binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700129 IPCThreadState* ipc = IPCThreadState::self();
130 const int uid = ipc->getCallingUid();
131 if (uid != AID_SHELL && uid != AID_ROOT) {
132 ALOGE("Invalid attempt to register input channel over IPC"
133 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
Chris Ye0783e992020-06-02 21:34:49 -0700134 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700135 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500136
Garfield Tan15601662020-09-22 15:32:38 -0700137 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900138 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700139 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
140 channel.error().message().c_str());
141 }
142 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700143 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700144}
145
Garfield Tan15601662020-09-22 15:32:38 -0700146binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
147 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700148 return binder::Status::ok();
149}
150
151status_t InputManager::dump(int fd, const Vector<String16>& args) {
152 std::string dump;
153
154 dump += " InputFlinger dump\n";
155
156 ::write(fd, dump.c_str(), dump.size());
157 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700158}
159
Vishnu Naire798b472020-07-23 13:52:21 -0700160binder::Status InputManager::setFocusedWindow(const FocusRequest& request) {
161 mDispatcher->setFocusedWindow(request);
162 return binder::Status::ok();
163}
164
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165} // namespace android