• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
exceptionCodeFromStatusT(status_t status)34 static int32_t exceptionCodeFromStatusT(status_t status) {
35     switch (status) {
36         case OK:
37             return binder::Status::EX_NONE;
38         case INVALID_OPERATION:
39             return binder::Status::EX_UNSUPPORTED_OPERATION;
40         case BAD_VALUE:
41         case BAD_TYPE:
42         case NAME_NOT_FOUND:
43             return binder::Status::EX_ILLEGAL_ARGUMENT;
44         case NO_INIT:
45             return binder::Status::EX_ILLEGAL_STATE;
46         case PERMISSION_DENIED:
47             return binder::Status::EX_SECURITY;
48         default:
49             return binder::Status::EX_TRANSACTION_FAILED;
50     }
51 }
52 
InputManager(const sp<InputReaderPolicyInterface> & readerPolicy,const sp<InputDispatcherPolicyInterface> & dispatcherPolicy)53 InputManager::InputManager(
54         const sp<InputReaderPolicyInterface>& readerPolicy,
55         const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
56     mDispatcher = createInputDispatcher(dispatcherPolicy);
57     mClassifier = new InputClassifier(mDispatcher);
58     mReader = createInputReader(readerPolicy, mClassifier);
59 }
60 
~InputManager()61 InputManager::~InputManager() {
62     stop();
63 }
64 
start()65 status_t InputManager::start() {
66     status_t result = mDispatcher->start();
67     if (result) {
68         ALOGE("Could not start InputDispatcher thread due to error %d.", result);
69         return result;
70     }
71 
72     result = mReader->start();
73     if (result) {
74         ALOGE("Could not start InputReader due to error %d.", result);
75 
76         mDispatcher->stop();
77         return result;
78     }
79 
80     return OK;
81 }
82 
stop()83 status_t InputManager::stop() {
84     status_t status = OK;
85 
86     status_t result = mReader->stop();
87     if (result) {
88         ALOGW("Could not stop InputReader due to error %d.", result);
89         status = result;
90     }
91 
92     result = mDispatcher->stop();
93     if (result) {
94         ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
95         status = result;
96     }
97 
98     return status;
99 }
100 
getReader()101 sp<InputReaderInterface> InputManager::getReader() {
102     return mReader;
103 }
104 
getClassifier()105 sp<InputClassifierInterface> InputManager::getClassifier() {
106     return mClassifier;
107 }
108 
getDispatcher()109 sp<InputDispatcherInterface> InputManager::getDispatcher() {
110     return mDispatcher;
111 }
112 
113 class BinderWindowHandle : public InputWindowHandle {
114 public:
BinderWindowHandle(const InputWindowInfo & info)115     BinderWindowHandle(const InputWindowInfo& info) { mInfo = info; }
116 
updateInfo()117     bool updateInfo() override {
118         return true;
119     }
120 };
121 
setInputWindows(const std::vector<InputWindowInfo> & infos,const sp<ISetInputWindowsListener> & setInputWindowsListener)122 binder::Status InputManager::setInputWindows(
123         const std::vector<InputWindowInfo>& infos,
124         const sp<ISetInputWindowsListener>& setInputWindowsListener) {
125     std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> handlesPerDisplay;
126 
127     std::vector<sp<InputWindowHandle>> handles;
128     for (const auto& info : infos) {
129         handlesPerDisplay.emplace(info.displayId, std::vector<sp<InputWindowHandle>>());
130         handlesPerDisplay[info.displayId].push_back(new BinderWindowHandle(info));
131     }
132     mDispatcher->setInputWindows(handlesPerDisplay);
133 
134     if (setInputWindowsListener) {
135         setInputWindowsListener->onSetInputWindowsFinished();
136     }
137     return binder::Status::ok();
138 }
139 
140 // Used by tests only.
createInputChannel(const std::string & name,InputChannel * outChannel)141 binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
142     IPCThreadState* ipc = IPCThreadState::self();
143     const int uid = ipc->getCallingUid();
144     if (uid != AID_SHELL && uid != AID_ROOT) {
145         ALOGE("Invalid attempt to register input channel over IPC"
146                 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
147         return binder::Status::ok();
148     }
149 
150     base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
151     if (!channel.ok()) {
152         return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
153                                                  channel.error().message().c_str());
154     }
155     (*channel)->copyTo(*outChannel);
156     return binder::Status::ok();
157 }
158 
removeInputChannel(const sp<IBinder> & connectionToken)159 binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
160     mDispatcher->removeInputChannel(connectionToken);
161     return binder::Status::ok();
162 }
163 
dump(int fd,const Vector<String16> & args)164 status_t InputManager::dump(int fd, const Vector<String16>& args) {
165     std::string dump;
166 
167     dump += " InputFlinger dump\n";
168 
169     ::write(fd, dump.c_str(), dump.size());
170     return NO_ERROR;
171 }
172 
setFocusedWindow(const FocusRequest & request)173 binder::Status InputManager::setFocusedWindow(const FocusRequest& request) {
174     mDispatcher->setFocusedWindow(request);
175     return binder::Status::ok();
176 }
177 
178 } // namespace android
179