blob: e68946d734f6ebcdc706aa6b2d6af505d0b5e514 [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
34InputManager::InputManager(
Michael Wrightd02c5b62014-02-10 15:10:22 -080035 const sp<InputReaderPolicyInterface>& readerPolicy,
36 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
Garfield Tane84e6f92019-08-29 17:28:41 -070037 mDispatcher = createInputDispatcher(dispatcherPolicy);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080038 mClassifier = new InputClassifier(mDispatcher);
39 mReader = createInputReader(readerPolicy, mClassifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -080040}
41
42InputManager::~InputManager() {
43 stop();
44}
45
Michael Wrightd02c5b62014-02-10 15:10:22 -080046status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070047 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080048 if (result) {
49 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
50 return result;
51 }
52
Prabir Pradhan28efc192019-11-05 01:10:04 +000053 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080054 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000055 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
Prabir Pradhan3608aad2019-10-02 17:08:26 -070057 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080058 return result;
59 }
60
61 return OK;
62}
63
64status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070065 status_t status = OK;
66
Prabir Pradhan28efc192019-11-05 01:10:04 +000067 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080068 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000069 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -070070 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -080071 }
72
Prabir Pradhan3608aad2019-10-02 17:08:26 -070073 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080074 if (result) {
75 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -070076 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -080077 }
78
Prabir Pradhan3608aad2019-10-02 17:08:26 -070079 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -080080}
81
82sp<InputReaderInterface> InputManager::getReader() {
83 return mReader;
84}
85
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080086sp<InputClassifierInterface> InputManager::getClassifier() {
87 return mClassifier;
88}
89
Michael Wrightd02c5b62014-02-10 15:10:22 -080090sp<InputDispatcherInterface> InputManager::getDispatcher() {
91 return mDispatcher;
92}
93
Robert Carr1cc78672018-07-31 14:25:57 -070094class BinderWindowHandle : public InputWindowHandle {
95public:
Robert Carr740167f2018-10-11 19:03:41 -070096 BinderWindowHandle(const InputWindowInfo& info) {
Robert Carr1cc78672018-07-31 14:25:57 -070097 mInfo = info;
98 }
99
100 bool updateInfo() override {
101 return true;
102 }
103};
104
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800105void InputManager::setInputWindows(const std::vector<InputWindowInfo>& infos,
chaviw291d88a2019-02-14 10:33:58 -0800106 const sp<ISetInputWindowsListener>& setInputWindowsListener) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800107 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> handlesPerDisplay;
Robert Carr1cc78672018-07-31 14:25:57 -0700108
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800109 std::vector<sp<InputWindowHandle>> handles;
Robert Carr1cc78672018-07-31 14:25:57 -0700110 for (const auto& info : infos) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800111 handlesPerDisplay.emplace(info.displayId, std::vector<sp<InputWindowHandle>>());
112 handlesPerDisplay[info.displayId].push_back(new BinderWindowHandle(info));
Robert Carr1cc78672018-07-31 14:25:57 -0700113 }
Arthur Hung72d8dc32020-03-28 00:48:39 +0000114 mDispatcher->setInputWindows(handlesPerDisplay);
115
116 if (setInputWindowsListener) {
117 setInputWindowsListener->onSetInputWindowsFinished();
Robert Carr1cc78672018-07-31 14:25:57 -0700118 }
119}
120
Robert Carr1c4c5592018-09-24 13:18:43 -0700121// Used by tests only.
122void InputManager::registerInputChannel(const sp<InputChannel>& channel) {
123 IPCThreadState* ipc = IPCThreadState::self();
124 const int uid = ipc->getCallingUid();
125 if (uid != AID_SHELL && uid != AID_ROOT) {
126 ALOGE("Invalid attempt to register input channel over IPC"
127 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
128 return;
129 }
Siarhei Vishniakou7c34b232019-10-11 19:08:48 -0700130 mDispatcher->registerInputChannel(channel);
Robert Carr1c4c5592018-09-24 13:18:43 -0700131}
132
133void InputManager::unregisterInputChannel(const sp<InputChannel>& channel) {
134 mDispatcher->unregisterInputChannel(channel);
135}
136
Siarhei Vishniakouc60da192020-03-19 11:55:01 -0700137void InputManager::setMotionClassifierEnabled(bool enabled) {
138 mClassifier->setMotionClassifierEnabled(enabled);
139}
140
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141} // namespace android