blob: a7fd9bab950f8364ce22a70abde69ecfb7f8620e [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"
Prabir Pradhan29c95332018-11-14 20:14:11 -080022#include "InputReaderFactory.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080023
Robert Carr1c4c5592018-09-24 13:18:43 -070024#include <binder/IPCThreadState.h>
25
Mark Salyzyn7823e122016-09-29 08:08:05 -070026#include <log/log.h>
Robert Carr1cc78672018-07-31 14:25:57 -070027#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028
Robert Carr1c4c5592018-09-24 13:18:43 -070029#include <private/android_filesystem_config.h>
30
Michael Wrightd02c5b62014-02-10 15:10:22 -080031namespace android {
32
33InputManager::InputManager(
Michael Wrightd02c5b62014-02-10 15:10:22 -080034 const sp<InputReaderPolicyInterface>& readerPolicy,
35 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
36 mDispatcher = new InputDispatcher(dispatcherPolicy);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080037 mClassifier = new InputClassifier(mDispatcher);
38 mReader = createInputReader(readerPolicy, mClassifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -080039 initialize();
40}
41
42InputManager::~InputManager() {
43 stop();
44}
45
46void InputManager::initialize() {
47 mReaderThread = new InputReaderThread(mReader);
48 mDispatcherThread = new InputDispatcherThread(mDispatcher);
49}
50
51status_t InputManager::start() {
52 status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);
53 if (result) {
54 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
55 return result;
56 }
57
58 result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY);
59 if (result) {
60 ALOGE("Could not start InputReader thread due to error %d.", result);
61
62 mDispatcherThread->requestExit();
63 return result;
64 }
65
66 return OK;
67}
68
69status_t InputManager::stop() {
70 status_t result = mReaderThread->requestExitAndWait();
71 if (result) {
72 ALOGW("Could not stop InputReader thread due to error %d.", result);
73 }
74
75 result = mDispatcherThread->requestExitAndWait();
76 if (result) {
77 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
78 }
79
80 return OK;
81}
82
83sp<InputReaderInterface> InputManager::getReader() {
84 return mReader;
85}
86
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080087sp<InputClassifierInterface> InputManager::getClassifier() {
88 return mClassifier;
89}
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091sp<InputDispatcherInterface> InputManager::getDispatcher() {
92 return mDispatcher;
93}
94
Robert Carr1cc78672018-07-31 14:25:57 -070095class BinderWindowHandle : public InputWindowHandle {
96public:
Robert Carr740167f2018-10-11 19:03:41 -070097 BinderWindowHandle(const InputWindowInfo& info) {
Robert Carr1cc78672018-07-31 14:25:57 -070098 mInfo = info;
99 }
100
101 bool updateInfo() override {
102 return true;
103 }
104};
105
106void InputManager::setInputWindows(const Vector<InputWindowInfo>& infos) {
107 std::unordered_map<int32_t, Vector<sp<InputWindowHandle>>> handlesPerDisplay;
108
109 Vector<sp<InputWindowHandle>> handles;
110 for (const auto& info : infos) {
111 handlesPerDisplay.emplace(info.displayId, Vector<sp<InputWindowHandle>>());
112 handlesPerDisplay[info.displayId].add(new BinderWindowHandle(info));
113 }
114 for (auto const& i : handlesPerDisplay) {
115 mDispatcher->setInputWindows(i.second, i.first);
116 }
117}
118
chaviwfbe5d9c2018-12-26 12:23:37 -0800119void InputManager::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) {
120 mDispatcher->transferTouchFocus(fromToken, toToken);
121}
122
Robert Carr1c4c5592018-09-24 13:18:43 -0700123// Used by tests only.
124void InputManager::registerInputChannel(const sp<InputChannel>& channel) {
125 IPCThreadState* ipc = IPCThreadState::self();
126 const int uid = ipc->getCallingUid();
127 if (uid != AID_SHELL && uid != AID_ROOT) {
128 ALOGE("Invalid attempt to register input channel over IPC"
129 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
130 return;
131 }
132 mDispatcher->registerInputChannel(channel, false);
133}
134
135void InputManager::unregisterInputChannel(const sp<InputChannel>& channel) {
136 mDispatcher->unregisterInputChannel(channel);
137}
138
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139} // namespace android