1 /*
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"
22 #include "InputDispatcherFactory.h"
23 #include "InputReaderFactory.h"
24
25 #include <binder/IPCThreadState.h>
26
27 #include <log/log.h>
28 #include <unordered_map>
29
30 #include <private/android_filesystem_config.h>
31
32 namespace android {
33
InputManager(const sp<InputReaderPolicyInterface> & readerPolicy,const sp<InputDispatcherPolicyInterface> & dispatcherPolicy)34 InputManager::InputManager(
35 const sp<InputReaderPolicyInterface>& readerPolicy,
36 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
37 mDispatcher = createInputDispatcher(dispatcherPolicy);
38 mClassifier = new InputClassifier(mDispatcher);
39 mReader = createInputReader(readerPolicy, mClassifier);
40 }
41
~InputManager()42 InputManager::~InputManager() {
43 stop();
44 }
45
start()46 status_t InputManager::start() {
47 status_t result = mDispatcher->start();
48 if (result) {
49 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
50 return result;
51 }
52
53 result = mReader->start();
54 if (result) {
55 ALOGE("Could not start InputReader due to error %d.", result);
56
57 mDispatcher->stop();
58 return result;
59 }
60
61 return OK;
62 }
63
stop()64 status_t InputManager::stop() {
65 status_t status = OK;
66
67 status_t result = mReader->stop();
68 if (result) {
69 ALOGW("Could not stop InputReader due to error %d.", result);
70 status = result;
71 }
72
73 result = mDispatcher->stop();
74 if (result) {
75 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
76 status = result;
77 }
78
79 return status;
80 }
81
getReader()82 sp<InputReaderInterface> InputManager::getReader() {
83 return mReader;
84 }
85
getClassifier()86 sp<InputClassifierInterface> InputManager::getClassifier() {
87 return mClassifier;
88 }
89
getDispatcher()90 sp<InputDispatcherInterface> InputManager::getDispatcher() {
91 return mDispatcher;
92 }
93
94 class BinderWindowHandle : public InputWindowHandle {
95 public:
BinderWindowHandle(const InputWindowInfo & info)96 BinderWindowHandle(const InputWindowInfo& info) {
97 mInfo = info;
98 }
99
updateInfo()100 bool updateInfo() override {
101 return true;
102 }
103 };
104
setInputWindows(const std::vector<InputWindowInfo> & infos,const sp<ISetInputWindowsListener> & setInputWindowsListener)105 void InputManager::setInputWindows(const std::vector<InputWindowInfo>& infos,
106 const sp<ISetInputWindowsListener>& setInputWindowsListener) {
107 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> handlesPerDisplay;
108
109 std::vector<sp<InputWindowHandle>> handles;
110 for (const auto& info : infos) {
111 handlesPerDisplay.emplace(info.displayId, std::vector<sp<InputWindowHandle>>());
112 handlesPerDisplay[info.displayId].push_back(new BinderWindowHandle(info));
113 }
114 mDispatcher->setInputWindows(handlesPerDisplay);
115
116 if (setInputWindowsListener) {
117 setInputWindowsListener->onSetInputWindowsFinished();
118 }
119 }
120
121 // Used by tests only.
registerInputChannel(const sp<InputChannel> & channel)122 void 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 }
130 mDispatcher->registerInputChannel(channel);
131 }
132
unregisterInputChannel(const sp<InputChannel> & channel)133 void InputManager::unregisterInputChannel(const sp<InputChannel>& channel) {
134 mDispatcher->unregisterInputChannel(channel);
135 }
136
setMotionClassifierEnabled(bool enabled)137 void InputManager::setMotionClassifierEnabled(bool enabled) {
138 mClassifier->setMotionClassifierEnabled(enabled);
139 }
140
141 } // namespace android
142