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 #include "UnwantedInteractionBlocker.h"
25
26 #include <binder/IPCThreadState.h>
27
28 #include <log/log.h>
29 #include <unordered_map>
30
31 #include <private/android_filesystem_config.h>
32
33 namespace android {
34
35 using gui::FocusRequest;
36
exceptionCodeFromStatusT(status_t status)37 static int32_t exceptionCodeFromStatusT(status_t status) {
38 switch (status) {
39 case OK:
40 return binder::Status::EX_NONE;
41 case INVALID_OPERATION:
42 return binder::Status::EX_UNSUPPORTED_OPERATION;
43 case BAD_VALUE:
44 case BAD_TYPE:
45 case NAME_NOT_FOUND:
46 return binder::Status::EX_ILLEGAL_ARGUMENT;
47 case NO_INIT:
48 return binder::Status::EX_ILLEGAL_STATE;
49 case PERMISSION_DENIED:
50 return binder::Status::EX_SECURITY;
51 default:
52 return binder::Status::EX_TRANSACTION_FAILED;
53 }
54 }
55
56 /**
57 * The event flow is via the "InputListener" interface, as follows:
58 * InputReader -> UnwantedInteractionBlocker -> InputClassifier -> InputDispatcher
59 */
InputManager(const sp<InputReaderPolicyInterface> & readerPolicy,const sp<InputDispatcherPolicyInterface> & dispatcherPolicy)60 InputManager::InputManager(
61 const sp<InputReaderPolicyInterface>& readerPolicy,
62 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
63 mDispatcher = createInputDispatcher(dispatcherPolicy);
64 mClassifier = std::make_unique<InputClassifier>(*mDispatcher);
65 mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mClassifier);
66 mReader = createInputReader(readerPolicy, *mBlocker);
67 }
68
~InputManager()69 InputManager::~InputManager() {
70 stop();
71 }
72
start()73 status_t InputManager::start() {
74 status_t result = mDispatcher->start();
75 if (result) {
76 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
77 return result;
78 }
79
80 result = mReader->start();
81 if (result) {
82 ALOGE("Could not start InputReader due to error %d.", result);
83
84 mDispatcher->stop();
85 return result;
86 }
87
88 return OK;
89 }
90
stop()91 status_t InputManager::stop() {
92 status_t status = OK;
93
94 status_t result = mReader->stop();
95 if (result) {
96 ALOGW("Could not stop InputReader due to error %d.", result);
97 status = result;
98 }
99
100 result = mDispatcher->stop();
101 if (result) {
102 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
103 status = result;
104 }
105
106 return status;
107 }
108
getReader()109 InputReaderInterface& InputManager::getReader() {
110 return *mReader;
111 }
112
getUnwantedInteractionBlocker()113 UnwantedInteractionBlockerInterface& InputManager::getUnwantedInteractionBlocker() {
114 return *mBlocker;
115 }
116
getClassifier()117 InputClassifierInterface& InputManager::getClassifier() {
118 return *mClassifier;
119 }
120
getDispatcher()121 InputDispatcherInterface& InputManager::getDispatcher() {
122 return *mDispatcher;
123 }
124
monitor()125 void InputManager::monitor() {
126 mReader->monitor();
127 mBlocker->monitor();
128 mClassifier->monitor();
129 mDispatcher->monitor();
130 }
131
132 // Used by tests only.
createInputChannel(const std::string & name,InputChannel * outChannel)133 binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
134 IPCThreadState* ipc = IPCThreadState::self();
135 const int uid = ipc->getCallingUid();
136 if (uid != AID_SHELL && uid != AID_ROOT) {
137 ALOGE("Invalid attempt to register input channel over IPC"
138 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
139 return binder::Status::ok();
140 }
141
142 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
143 if (!channel.ok()) {
144 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
145 channel.error().message().c_str());
146 }
147 (*channel)->copyTo(*outChannel);
148 return binder::Status::ok();
149 }
150
removeInputChannel(const sp<IBinder> & connectionToken)151 binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
152 mDispatcher->removeInputChannel(connectionToken);
153 return binder::Status::ok();
154 }
155
dump(int fd,const Vector<String16> & args)156 status_t InputManager::dump(int fd, const Vector<String16>& args) {
157 std::string dump;
158
159 dump += " InputFlinger dump\n";
160
161 ::write(fd, dump.c_str(), dump.size());
162 return NO_ERROR;
163 }
164
setFocusedWindow(const FocusRequest & request)165 binder::Status InputManager::setFocusedWindow(const FocusRequest& request) {
166 mDispatcher->setFocusedWindow(request);
167 return binder::Status::ok();
168 }
169
170 } // namespace android
171