blob: 3d995899d042d820781eeec933a243cf82100ca1 [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
Garfield Tan15601662020-09-22 15:32:38 -070034static int32_t exceptionCodeFromStatusT(status_t status) {
35 switch (status) {
36 case OK:
37 return binder::Status::EX_NONE;
38 case INVALID_OPERATION:
39 return binder::Status::EX_UNSUPPORTED_OPERATION;
40 case BAD_VALUE:
41 case BAD_TYPE:
42 case NAME_NOT_FOUND:
43 return binder::Status::EX_ILLEGAL_ARGUMENT;
44 case NO_INIT:
45 return binder::Status::EX_ILLEGAL_STATE;
46 case PERMISSION_DENIED:
47 return binder::Status::EX_SECURITY;
48 default:
49 return binder::Status::EX_TRANSACTION_FAILED;
50 }
51}
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053InputManager::InputManager(
Michael Wrightd02c5b62014-02-10 15:10:22 -080054 const sp<InputReaderPolicyInterface>& readerPolicy,
55 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
Garfield Tane84e6f92019-08-29 17:28:41 -070056 mDispatcher = createInputDispatcher(dispatcherPolicy);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080057 mClassifier = new InputClassifier(mDispatcher);
58 mReader = createInputReader(readerPolicy, mClassifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -080059}
60
61InputManager::~InputManager() {
62 stop();
63}
64
Michael Wrightd02c5b62014-02-10 15:10:22 -080065status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070066 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080067 if (result) {
68 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
69 return result;
70 }
71
Prabir Pradhan28efc192019-11-05 01:10:04 +000072 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080073 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000074 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -080075
Prabir Pradhan3608aad2019-10-02 17:08:26 -070076 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080077 return result;
78 }
79
80 return OK;
81}
82
83status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070084 status_t status = OK;
85
Prabir Pradhan28efc192019-11-05 01:10:04 +000086 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080087 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000088 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -070089 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -080090 }
91
Prabir Pradhan3608aad2019-10-02 17:08:26 -070092 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080093 if (result) {
94 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -070095 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 }
97
Prabir Pradhan3608aad2019-10-02 17:08:26 -070098 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -080099}
100
101sp<InputReaderInterface> InputManager::getReader() {
102 return mReader;
103}
104
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800105sp<InputClassifierInterface> InputManager::getClassifier() {
106 return mClassifier;
107}
108
Michael Wrightd02c5b62014-02-10 15:10:22 -0800109sp<InputDispatcherInterface> InputManager::getDispatcher() {
110 return mDispatcher;
111}
112
Robert Carr1cc78672018-07-31 14:25:57 -0700113class BinderWindowHandle : public InputWindowHandle {
114public:
Chris Ye0783e992020-06-02 21:34:49 -0700115 BinderWindowHandle(const InputWindowInfo& info) { mInfo = info; }
Robert Carr1cc78672018-07-31 14:25:57 -0700116
117 bool updateInfo() override {
118 return true;
119 }
120};
121
Chris Ye0783e992020-06-02 21:34:49 -0700122binder::Status InputManager::setInputWindows(
123 const std::vector<InputWindowInfo>& infos,
chaviw291d88a2019-02-14 10:33:58 -0800124 const sp<ISetInputWindowsListener>& setInputWindowsListener) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800125 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> handlesPerDisplay;
Robert Carr1cc78672018-07-31 14:25:57 -0700126
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800127 std::vector<sp<InputWindowHandle>> handles;
Robert Carr1cc78672018-07-31 14:25:57 -0700128 for (const auto& info : infos) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800129 handlesPerDisplay.emplace(info.displayId, std::vector<sp<InputWindowHandle>>());
130 handlesPerDisplay[info.displayId].push_back(new BinderWindowHandle(info));
Robert Carr1cc78672018-07-31 14:25:57 -0700131 }
Arthur Hung72d8dc32020-03-28 00:48:39 +0000132 mDispatcher->setInputWindows(handlesPerDisplay);
133
134 if (setInputWindowsListener) {
135 setInputWindowsListener->onSetInputWindowsFinished();
Robert Carr1cc78672018-07-31 14:25:57 -0700136 }
Chris Ye0783e992020-06-02 21:34:49 -0700137 return binder::Status::ok();
Robert Carr1cc78672018-07-31 14:25:57 -0700138}
139
Robert Carr1c4c5592018-09-24 13:18:43 -0700140// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700141binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700142 IPCThreadState* ipc = IPCThreadState::self();
143 const int uid = ipc->getCallingUid();
144 if (uid != AID_SHELL && uid != AID_ROOT) {
145 ALOGE("Invalid attempt to register input channel over IPC"
146 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
Chris Ye0783e992020-06-02 21:34:49 -0700147 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700148 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500149
Garfield Tan15601662020-09-22 15:32:38 -0700150 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
151 if (!channel) {
152 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
153 channel.error().message().c_str());
154 }
155 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700156 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700157}
158
Garfield Tan15601662020-09-22 15:32:38 -0700159binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
160 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700161 return binder::Status::ok();
162}
163
164status_t InputManager::dump(int fd, const Vector<String16>& args) {
165 std::string dump;
166
167 dump += " InputFlinger dump\n";
168
169 ::write(fd, dump.c_str(), dump.size());
170 return NO_ERROR;
Robert Carr1c4c5592018-09-24 13:18:43 -0700171}
172
Vishnu Naire798b472020-07-23 13:52:21 -0700173binder::Status InputManager::setFocusedWindow(const FocusRequest& request) {
174 mDispatcher->setFocusedWindow(request);
175 return binder::Status::ok();
176}
177
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178} // namespace android