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 "InputReaderFactory.h"
23
24 #include <binder/IPCThreadState.h>
25
26 #include <log/log.h>
27 #include <unordered_map>
28
29 #include <private/android_filesystem_config.h>
30
31 namespace android {
32
InputManager(const sp<InputReaderPolicyInterface> & readerPolicy,const sp<InputDispatcherPolicyInterface> & dispatcherPolicy)33 InputManager::InputManager(
34 const sp<InputReaderPolicyInterface>& readerPolicy,
35 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
36 mDispatcher = new InputDispatcher(dispatcherPolicy);
37 mClassifier = new InputClassifier(mDispatcher);
38 mReader = createInputReader(readerPolicy, mClassifier);
39 initialize();
40 }
41
~InputManager()42 InputManager::~InputManager() {
43 stop();
44 }
45
initialize()46 void InputManager::initialize() {
47 mReaderThread = new InputReaderThread(mReader);
48 mDispatcherThread = new InputDispatcherThread(mDispatcher);
49 }
50
start()51 status_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
stop()69 status_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
getReader()83 sp<InputReaderInterface> InputManager::getReader() {
84 return mReader;
85 }
86
getClassifier()87 sp<InputClassifierInterface> InputManager::getClassifier() {
88 return mClassifier;
89 }
90
getDispatcher()91 sp<InputDispatcherInterface> InputManager::getDispatcher() {
92 return mDispatcher;
93 }
94
95 class BinderWindowHandle : public InputWindowHandle {
96 public:
BinderWindowHandle(const InputWindowInfo & info)97 BinderWindowHandle(const InputWindowInfo& info) {
98 mInfo = info;
99 }
100
updateInfo()101 bool updateInfo() override {
102 return true;
103 }
104 };
105
setInputWindows(const std::vector<InputWindowInfo> & infos,const sp<ISetInputWindowsListener> & setInputWindowsListener)106 void InputManager::setInputWindows(const std::vector<InputWindowInfo>& infos,
107 const sp<ISetInputWindowsListener>& setInputWindowsListener) {
108 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> handlesPerDisplay;
109
110 std::vector<sp<InputWindowHandle>> handles;
111 for (const auto& info : infos) {
112 handlesPerDisplay.emplace(info.displayId, std::vector<sp<InputWindowHandle>>());
113 handlesPerDisplay[info.displayId].push_back(new BinderWindowHandle(info));
114 }
115 for (auto const& i : handlesPerDisplay) {
116 mDispatcher->setInputWindows(i.second, i.first, setInputWindowsListener);
117 }
118 }
119
transferTouchFocus(const sp<IBinder> & fromToken,const sp<IBinder> & toToken)120 void InputManager::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) {
121 mDispatcher->transferTouchFocus(fromToken, toToken);
122 }
123
124 // Used by tests only.
registerInputChannel(const sp<InputChannel> & channel)125 void InputManager::registerInputChannel(const sp<InputChannel>& channel) {
126 IPCThreadState* ipc = IPCThreadState::self();
127 const int uid = ipc->getCallingUid();
128 if (uid != AID_SHELL && uid != AID_ROOT) {
129 ALOGE("Invalid attempt to register input channel over IPC"
130 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
131 return;
132 }
133 mDispatcher->registerInputChannel(channel, false);
134 }
135
unregisterInputChannel(const sp<InputChannel> & channel)136 void InputManager::unregisterInputChannel(const sp<InputChannel>& channel) {
137 mDispatcher->unregisterInputChannel(channel);
138 }
139
140 } // namespace android
141