blob: 7b3658dfde06b5a63af2254b0d8c75927dd0d805 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
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"
Garfield Tane84e6f92019-08-29 17:28:41 -070022#include "InputDispatcherFactory.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080023#include "InputReaderFactory.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080024
Robert Carr1c4c5592018-09-24 13:18:43 -070025#include <binder/IPCThreadState.h>
26
Mark Salyzyn7823e122016-09-29 08:08:05 -070027#include <log/log.h>
Robert Carr1cc78672018-07-31 14:25:57 -070028#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029
Robert Carr1c4c5592018-09-24 13:18:43 -070030#include <private/android_filesystem_config.h>
31
Michael Wrightd02c5b62014-02-10 15:10:22 -080032namespace android {
33
chaviw3277faf2021-05-19 16:45:23 -050034using gui::FocusRequest;
35using gui::WindowInfo;
36using gui::WindowInfoHandle;
37
Garfield Tan15601662020-09-22 15:32:38 -070038static 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
Michael Wrightd02c5b62014-02-10 15:10:22 -080057InputManager::InputManager(
Michael Wrightd02c5b62014-02-10 15:10:22 -080058 const sp<InputReaderPolicyInterface>& readerPolicy,
59 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
Garfield Tane84e6f92019-08-29 17:28:41 -070060 mDispatcher = createInputDispatcher(dispatcherPolicy);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080061 mClassifier = new InputClassifier(mDispatcher);
62 mReader = createInputReader(readerPolicy, mClassifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -080063}
64
65InputManager::~InputManager() {
66 stop();
67}
68
Michael Wrightd02c5b62014-02-10 15:10:22 -080069status_t InputManager::start() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070070 status_t result = mDispatcher->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080071 if (result) {
72 ALOGE("Could not start InputDispatcher thread due to error %d.", result);
73 return result;
74 }
75
Prabir Pradhan28efc192019-11-05 01:10:04 +000076 result = mReader->start();
Michael Wrightd02c5b62014-02-10 15:10:22 -080077 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000078 ALOGE("Could not start InputReader due to error %d.", result);
Michael Wrightd02c5b62014-02-10 15:10:22 -080079
Prabir Pradhan3608aad2019-10-02 17:08:26 -070080 mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080081 return result;
82 }
83
84 return OK;
85}
86
87status_t InputManager::stop() {
Prabir Pradhan3608aad2019-10-02 17:08:26 -070088 status_t status = OK;
89
Prabir Pradhan28efc192019-11-05 01:10:04 +000090 status_t result = mReader->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080091 if (result) {
Prabir Pradhan28efc192019-11-05 01:10:04 +000092 ALOGW("Could not stop InputReader due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -070093 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -080094 }
95
Prabir Pradhan3608aad2019-10-02 17:08:26 -070096 result = mDispatcher->stop();
Michael Wrightd02c5b62014-02-10 15:10:22 -080097 if (result) {
98 ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
Prabir Pradhan3608aad2019-10-02 17:08:26 -070099 status = result;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100 }
101
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700102 return status;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103}
104
105sp<InputReaderInterface> InputManager::getReader() {
106 return mReader;
107}
108
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800109sp<InputClassifierInterface> InputManager::getClassifier() {
110 return mClassifier;
111}
112
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113sp<InputDispatcherInterface> InputManager::getDispatcher() {
114 return mDispatcher;
115}
116
Robert Carr1c4c5592018-09-24 13:18:43 -0700117// Used by tests only.
Garfield Tan15601662020-09-22 15:32:38 -0700118binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) {
Robert Carr1c4c5592018-09-24 13:18:43 -0700119 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());
Chris Ye0783e992020-06-02 21:34:49 -0700124 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700125 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500126
Garfield Tan15601662020-09-22 15:32:38 -0700127 base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
Bernie Innocenti189c0f82020-12-22 19:45:18 +0900128 if (!channel.ok()) {
Garfield Tan15601662020-09-22 15:32:38 -0700129 return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
130 channel.error().message().c_str());
131 }
132 (*channel)->copyTo(*outChannel);
Chris Ye0783e992020-06-02 21:34:49 -0700133 return binder::Status::ok();
Robert Carr1c4c5592018-09-24 13:18:43 -0700134}
135
Garfield Tan15601662020-09-22 15:32:38 -0700136binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
137 mDispatcher->removeInputChannel(connectionToken);
Chris Ye0783e992020-06-02 21:34:49 -0700138 return binder::Status::ok();
139}
140
141status_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;
Robert Carr1c4c5592018-09-24 13:18:43 -0700148}
149
Vishnu Naire798b472020-07-23 13:52:21 -0700150binder::Status InputManager::setFocusedWindow(const FocusRequest& request) {
151 mDispatcher->setFocusedWindow(request);
152 return binder::Status::ok();
153}
154
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155} // namespace android