| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 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" | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 22 | #include "InputDispatcherFactory.h" | 
| Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 23 | #include "InputReaderFactory.h" | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 24 |  | 
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 25 | #include <binder/IPCThreadState.h> | 
|  | 26 |  | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 27 | #include <log/log.h> | 
| Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 28 | #include <unordered_map> | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 29 |  | 
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 30 | #include <private/android_filesystem_config.h> | 
|  | 31 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 32 | namespace android { | 
|  | 33 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 34 | using gui::FocusRequest; | 
|  | 35 | using gui::WindowInfo; | 
|  | 36 | using gui::WindowInfoHandle; | 
|  | 37 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 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 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 57 | InputManager::InputManager( | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 | const sp<InputReaderPolicyInterface>& readerPolicy, | 
|  | 59 | const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) { | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 60 | mDispatcher = createInputDispatcher(dispatcherPolicy); | 
| Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 61 | mClassifier = new InputClassifier(mDispatcher); | 
|  | 62 | mReader = createInputReader(readerPolicy, mClassifier); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
|  | 65 | InputManager::~InputManager() { | 
|  | 66 | stop(); | 
|  | 67 | } | 
|  | 68 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 | status_t InputManager::start() { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 70 | status_t result = mDispatcher->start(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 71 | if (result) { | 
|  | 72 | ALOGE("Could not start InputDispatcher thread due to error %d.", result); | 
|  | 73 | return result; | 
|  | 74 | } | 
|  | 75 |  | 
| Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 76 | result = mReader->start(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 77 | if (result) { | 
| Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 78 | ALOGE("Could not start InputReader due to error %d.", result); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 79 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 80 | mDispatcher->stop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 81 | return result; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | return OK; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | status_t InputManager::stop() { | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 88 | status_t status = OK; | 
|  | 89 |  | 
| Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 90 | status_t result = mReader->stop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 91 | if (result) { | 
| Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 92 | ALOGW("Could not stop InputReader due to error %d.", result); | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 93 | status = result; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 96 | result = mDispatcher->stop(); | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 97 | if (result) { | 
|  | 98 | ALOGW("Could not stop InputDispatcher thread due to error %d.", result); | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 99 | status = result; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 102 | return status; | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | sp<InputReaderInterface> InputManager::getReader() { | 
|  | 106 | return mReader; | 
|  | 107 | } | 
|  | 108 |  | 
| Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 109 | sp<InputClassifierInterface> InputManager::getClassifier() { | 
|  | 110 | return mClassifier; | 
|  | 111 | } | 
|  | 112 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 113 | sp<InputDispatcherInterface> InputManager::getDispatcher() { | 
|  | 114 | return mDispatcher; | 
|  | 115 | } | 
|  | 116 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 117 | class BinderWindowHandle : public WindowInfoHandle { | 
| Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 118 | public: | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 119 | BinderWindowHandle(const WindowInfo& info) { mInfo = info; } | 
| Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | bool updateInfo() override { | 
|  | 122 | return true; | 
|  | 123 | } | 
|  | 124 | }; | 
|  | 125 |  | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 126 | binder::Status InputManager::setInputWindows( | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 127 | const std::vector<WindowInfo>& infos, | 
| chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 128 | const sp<ISetInputWindowsListener>& setInputWindowsListener) { | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 129 | std::unordered_map<int32_t, std::vector<sp<WindowInfoHandle>>> handlesPerDisplay; | 
| Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 130 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 131 | std::vector<sp<WindowInfoHandle>> handles; | 
| Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 132 | for (const auto& info : infos) { | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 133 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<WindowInfoHandle>>()); | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 134 | handlesPerDisplay[info.displayId].push_back(new BinderWindowHandle(info)); | 
| Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 135 | } | 
| Arthur Hung | 72d8dc3 | 2020-03-28 00:48:39 +0000 | [diff] [blame] | 136 | mDispatcher->setInputWindows(handlesPerDisplay); | 
|  | 137 |  | 
|  | 138 | if (setInputWindowsListener) { | 
|  | 139 | setInputWindowsListener->onSetInputWindowsFinished(); | 
| Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 140 | } | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 141 | return binder::Status::ok(); | 
| Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 144 | // Used by tests only. | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 145 | binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) { | 
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 146 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 147 | const int uid = ipc->getCallingUid(); | 
|  | 148 | if (uid != AID_SHELL && uid != AID_ROOT) { | 
|  | 149 | ALOGE("Invalid attempt to register input channel over IPC" | 
|  | 150 | "from non shell/root entity (PID: %d)", ipc->getCallingPid()); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 151 | return binder::Status::ok(); | 
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 152 | } | 
| Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 153 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 154 | base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name); | 
| Bernie Innocenti | 189c0f8 | 2020-12-22 19:45:18 +0900 | [diff] [blame] | 155 | if (!channel.ok()) { | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 156 | return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()), | 
|  | 157 | channel.error().message().c_str()); | 
|  | 158 | } | 
|  | 159 | (*channel)->copyTo(*outChannel); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 160 | return binder::Status::ok(); | 
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 163 | binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) { | 
|  | 164 | mDispatcher->removeInputChannel(connectionToken); | 
| Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 165 | return binder::Status::ok(); | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | status_t InputManager::dump(int fd, const Vector<String16>& args) { | 
|  | 169 | std::string dump; | 
|  | 170 |  | 
|  | 171 | dump += " InputFlinger dump\n"; | 
|  | 172 |  | 
|  | 173 | ::write(fd, dump.c_str(), dump.size()); | 
|  | 174 | return NO_ERROR; | 
| Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 177 | binder::Status InputManager::setFocusedWindow(const FocusRequest& request) { | 
|  | 178 | mDispatcher->setFocusedWindow(request); | 
|  | 179 | return binder::Status::ok(); | 
|  | 180 | } | 
|  | 181 |  | 
| Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 182 | } // namespace android |