blob: e81dcfe4f11393f1e03f6e1f0787a6a135be617b [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"
Michael Wrightd02c5b62014-02-10 15:10:22 -080024
Robert Carr1c4c5592018-09-24 13:18:43 -070025#include <binder/IPCThreadState.h>
26
Mark Salyzyn7823e122016-09-29 08:08:05 -070027#include <log/log.h>
Robert Carr1cc78672018-07-31 14:25:57 -070028#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029
Robert Carr1c4c5592018-09-24 13:18:43 -070030#include <private/android_filesystem_config.h>
31
Michael Wrightd02c5b62014-02-10 15:10:22 -080032namespace android {
33
chaviw3277faf2021-05-19 16:45:23 -050034using gui::FocusRequest;
35using gui::WindowInfo;
36using gui::WindowInfoHandle;
37
Garfield Tan15601662020-09-22 15:32:38 -070038static int32_t exceptionCodeFromStatusT(status_t status) {
39 switch (status) {
40 case OK:
41 return binder::Status::EX_NONE;
42 case INVALID_OPERATION:
43 return binder::Status::EX_UNSUPPORTED_OPERATION;
44 case BAD_VALUE:
45 case BAD_TYPE:
46 case NAME_NOT_FOUND:
47 return binder::Status::EX_ILLEGAL_ARGUMENT;
48 case NO_INIT:
49 return binder::Status::EX_ILLEGAL_STATE;
50 case PERMISSION_DENIED:
51 return binder::Status::EX_SECURITY;
52 default:
53 return binder::Status::EX_TRANSACTION_FAILED;
54 }
55}
56
Michael Wrightd02c5b62014-02-10 15:10:22 -080057InputManager::InputManager(
Michael Wrightd02c5b62014-02-10 15:10:22 -080058 const sp<InputReaderPolicyInterface>& readerPolicy,
59 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
Garfield Tane84e6f92019-08-29 17:28:41 -070060 mDispatcher = createInputDispatcher(dispatcherPolicy);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080061 mClassifier = new InputClassifier(mDispatcher);
62 mReader = createInputReader(readerPolicy, mClassifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -080063}
64
65InputManager::~InputManager() {
66 stop();
67}
68
Michael Wrightd02c5b62014-02-10 15:10:22 -080069status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070070 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080071 if (result) {
72 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
73 return result;
74 }
75
Prabir Pradhan28efc192019-11-05 01:10:04 +000076 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080077 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000078 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -080079
Prabir Pradhan3608aad2019-10-02 17:08:26 -070080 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080081 return result;
82 }
83
84 return OK;
85}
86
87status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070088 status_t status = OK;
89
Prabir Pradhan28efc192019-11-05 01:10:04 +000090 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080091 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000092 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -070093 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -080094 }
95
Prabir Pradhan3608aad2019-10-02 17:08:26 -070096 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 if (result) {
98 ALOGW("Could not stop InputDispatcher thread 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 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103}
104
105sp<InputReaderInterface> InputManager::getReader() {
106 return mReader;
107}
108
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800109sp<InputClassifierInterface> InputManager::getClassifier() {
110 return mClassifier;
111}
112
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113sp<InputDispatcherInterface> InputManager::getDispatcher() {
114 return mDispatcher;
115}
116
chaviw3277faf2021-05-19 16:45:23 -0500117class BinderWindowHandle : public WindowInfoHandle {
Robert Carr1cc78672018-07-31 14:25:57 -0700118public:
chaviw3277faf2021-05-19 16:45:23 -0500119 BinderWindowHandle(const WindowInfo& info) { mInfo = info; }
Robert Carr1cc78672018-07-31 14:25:57 -0700120
121 bool updateInfo() override {
122 return true;
123 }
124};
125
Chris Ye0783e992020-06-02 21:34:49 -0700126binder::Status InputManager::setInputWindows(
chaviw3277faf2021-05-19 16:45:23 -0500127 const std::vector<WindowInfo>& infos,
chaviw291d88a2019-02-14 10:33:58 -0800128 const sp<ISetInputWindowsListener>& setInputWindowsListener) {
chaviw3277faf2021-05-19 16:45:23 -0500129 std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay;
Robert Carr1cc78672018-07-31 14:25:57 -0700130
chaviw3277faf2021-05-19 16:45:23 -0500131 std::vector<sp<WindowInfoHandle>> handles;
Robert Carr1cc78672018-07-31 14:25:57 -0700132 for (const auto& info : infos) {
chaviw3277faf2021-05-19 16:45:23 -0500133 handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>());
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800134 handlesPerDisplay[info.displayId].push_back(new BinderWindowHandle(info));
Robert Carr1cc78672018-07-31 14:25:57 -0700135 }
Arthur Hung72d8dc32020-03-28 00:48:39 +0000136 mDispatcher->setInputWindows(handlesPerDisplay);
137
138 if (setInputWindowsListener) {
139 setInputWindowsListener->onSetInputWindowsFinished();
Robert Carr1cc78672018-07-31 14:25:57 -0700140 }
Chris Ye0783e992020-06-02 21:34:49 -0700141 return binder::Status::ok();
Robert Carr1cc78672018-07-31 14:25:57 -0700142}
143
Robert Carr1c4c5592018-09-24 13:18:43 -0700144// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700145binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700146 IPCThreadState* ipc = IPCThreadState::self();
147 const int uid = ipc->getCallingUid();
148 if (uid != AID_SHELL && uid != AID_ROOT) {
149 ALOGE("Invalid attempt to register input channel over IPC"
150 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
Chris Ye0783e992020-06-02 21:34:49 -0700151 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700152 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500153
Garfield Tan15601662020-09-22 15:32:38 -0700154 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900155 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700156 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
157 channel.error().message().c_str());
158 }
159 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700160 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700161}
162
Garfield Tan15601662020-09-22 15:32:38 -0700163binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
164 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700165 return binder::Status::ok();
166}
167
168status_t InputManager::dump(int fd, const Vector<String16>& args) {
169 std::string dump;
170
171 dump += " InputFlinger dump\n";
172
173 ::write(fd, dump.c_str(), dump.size());
174 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700175}
176
Vishnu Naire798b472020-07-23 13:52:21 -0700177binder::Status InputManager::setFocusedWindow(const FocusRequest& request) {
178 mDispatcher->setFocusedWindow(request);
179 return binder::Status::ok();
180}
181
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182} // namespace android