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