blob: 359325faed24504015f07018e948cad929c9acba [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"
23#include "InputDispatcherThread.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080024#include "InputReaderFactory.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
35InputManager::InputManager(
Michael Wrightd02c5b62014-02-10 15:10:22 -080036 const sp<InputReaderPolicyInterface>& readerPolicy,
37 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
Garfield Tane84e6f92019-08-29 17:28:41 -070038 mDispatcher = createInputDispatcher(dispatcherPolicy);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080039 mClassifier = new InputClassifier(mDispatcher);
40 mReader = createInputReader(readerPolicy, mClassifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -080041 initialize();
42}
43
44InputManager::~InputManager() {
45 stop();
46}
47
48void InputManager::initialize() {
49 mReaderThread = new InputReaderThread(mReader);
50 mDispatcherThread = new InputDispatcherThread(mDispatcher);
51}
52
53status_t InputManager::start() {
54 status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY);
55 if (result) {
56 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
57 return result;
58 }
59
60 result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY);
61 if (result) {
62 ALOGE("Could not start InputReader thread due to error %d.", result);
63
64 mDispatcherThread->requestExit();
65 return result;
66 }
67
68 return OK;
69}
70
71status_t InputManager::stop() {
72 status_t result = mReaderThread->requestExitAndWait();
73 if (result) {
74 ALOGW("Could not stop InputReader thread due to error %d.", result);
75 }
76
77 result = mDispatcherThread->requestExitAndWait();
78 if (result) {
79 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
80 }
81
82 return OK;
83}
84
85sp<InputReaderInterface> InputManager::getReader() {
86 return mReader;
87}
88
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080089sp<InputClassifierInterface> InputManager::getClassifier() {
90 return mClassifier;
91}
92
Michael Wrightd02c5b62014-02-10 15:10:22 -080093sp<InputDispatcherInterface> InputManager::getDispatcher() {
94 return mDispatcher;
95}
96
Robert Carr1cc78672018-07-31 14:25:57 -070097class BinderWindowHandle : public InputWindowHandle {
98public:
Robert Carr740167f2018-10-11 19:03:41 -070099 BinderWindowHandle(const InputWindowInfo& info) {
Robert Carr1cc78672018-07-31 14:25:57 -0700100 mInfo = info;
101 }
102
103 bool updateInfo() override {
104 return true;
105 }
106};
107
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800108void InputManager::setInputWindows(const std::vector<InputWindowInfo>& infos,
chaviw291d88a2019-02-14 10:33:58 -0800109 const sp<ISetInputWindowsListener>& setInputWindowsListener) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800110 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> handlesPerDisplay;
Robert Carr1cc78672018-07-31 14:25:57 -0700111
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800112 std::vector<sp<InputWindowHandle>> handles;
Robert Carr1cc78672018-07-31 14:25:57 -0700113 for (const auto& info : infos) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800114 handlesPerDisplay.emplace(info.displayId, std::vector<sp<InputWindowHandle>>());
115 handlesPerDisplay[info.displayId].push_back(new BinderWindowHandle(info));
Robert Carr1cc78672018-07-31 14:25:57 -0700116 }
117 for (auto const& i : handlesPerDisplay) {
chaviw291d88a2019-02-14 10:33:58 -0800118 mDispatcher->setInputWindows(i.second, i.first, setInputWindowsListener);
Robert Carr1cc78672018-07-31 14:25:57 -0700119 }
120}
121
chaviwfbe5d9c2018-12-26 12:23:37 -0800122void InputManager::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) {
123 mDispatcher->transferTouchFocus(fromToken, toToken);
124}
125
Robert Carr1c4c5592018-09-24 13:18:43 -0700126// Used by tests only.
127void InputManager::registerInputChannel(const sp<InputChannel>& channel) {
128 IPCThreadState* ipc = IPCThreadState::self();
129 const int uid = ipc->getCallingUid();
130 if (uid != AID_SHELL && uid != AID_ROOT) {
131 ALOGE("Invalid attempt to register input channel over IPC"
132 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
133 return;
134 }
135 mDispatcher->registerInputChannel(channel, false);
136}
137
138void InputManager::unregisterInputChannel(const sp<InputChannel>& channel) {
139 mDispatcher->unregisterInputChannel(channel);
140}
141
Michael Wrightd02c5b62014-02-10 15:10:22 -0800142} // namespace android