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" |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 24 | #include "UnwantedInteractionBlocker.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 25 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 26 | #include <android/sysprop/InputProperties.sysprop.h> |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 27 | #include <binder/IPCThreadState.h> |
| 28 | |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 30 | #include <unordered_map> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 31 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 32 | #include <private/android_filesystem_config.h> |
| 33 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | namespace android { |
| 35 | |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 36 | static const bool ENABLE_INPUT_DEVICE_USAGE_METRICS = |
Prabir Pradhan | 7adabad | 2023-06-28 16:16:27 +0000 | [diff] [blame^] | 37 | sysprop::InputProperties::enable_input_device_usage_metrics().value_or(true); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 38 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 39 | using gui::FocusRequest; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 40 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 41 | static int32_t exceptionCodeFromStatusT(status_t status) { |
| 42 | switch (status) { |
| 43 | case OK: |
| 44 | return binder::Status::EX_NONE; |
| 45 | case INVALID_OPERATION: |
| 46 | return binder::Status::EX_UNSUPPORTED_OPERATION; |
| 47 | case BAD_VALUE: |
| 48 | case BAD_TYPE: |
| 49 | case NAME_NOT_FOUND: |
| 50 | return binder::Status::EX_ILLEGAL_ARGUMENT; |
| 51 | case NO_INIT: |
| 52 | return binder::Status::EX_ILLEGAL_STATE; |
| 53 | case PERMISSION_DENIED: |
| 54 | return binder::Status::EX_SECURITY; |
| 55 | default: |
| 56 | return binder::Status::EX_TRANSACTION_FAILED; |
| 57 | } |
| 58 | } |
| 59 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 60 | /** |
| 61 | * The event flow is via the "InputListener" interface, as follows: |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 62 | * InputReader |
| 63 | * -> UnwantedInteractionBlocker |
| 64 | * -> InputProcessor |
| 65 | * -> InputDeviceMetricsCollector |
| 66 | * -> InputDispatcher |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 67 | */ |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 68 | InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy, |
| 69 | InputDispatcherPolicyInterface& dispatcherPolicy) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 70 | mDispatcher = createInputDispatcher(dispatcherPolicy); |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 71 | |
| 72 | if (ENABLE_INPUT_DEVICE_USAGE_METRICS) { |
| 73 | mCollector = std::make_unique<InputDeviceMetricsCollector>(*mDispatcher); |
| 74 | } |
| 75 | |
| 76 | mProcessor = ENABLE_INPUT_DEVICE_USAGE_METRICS ? std::make_unique<InputProcessor>(*mCollector) |
| 77 | : std::make_unique<InputProcessor>(*mDispatcher); |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 78 | mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mProcessor); |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 79 | mReader = createInputReader(readerPolicy, *mBlocker); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | InputManager::~InputManager() { |
| 83 | stop(); |
| 84 | } |
| 85 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 86 | status_t InputManager::start() { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 87 | status_t result = mDispatcher->start(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 88 | if (result) { |
| 89 | ALOGE("Could not start InputDispatcher thread due to error %d.", result); |
| 90 | return result; |
| 91 | } |
| 92 | |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 93 | result = mReader->start(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 94 | if (result) { |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 95 | ALOGE("Could not start InputReader due to error %d.", result); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 96 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 97 | mDispatcher->stop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 98 | return result; |
| 99 | } |
| 100 | |
| 101 | return OK; |
| 102 | } |
| 103 | |
| 104 | status_t InputManager::stop() { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 105 | status_t status = OK; |
| 106 | |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 107 | status_t result = mReader->stop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 108 | if (result) { |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame] | 109 | ALOGW("Could not stop InputReader due to error %d.", result); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 110 | status = result; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 113 | result = mDispatcher->stop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 114 | if (result) { |
| 115 | ALOGW("Could not stop InputDispatcher thread due to error %d.", result); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 116 | status = result; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 119 | return status; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 122 | InputReaderInterface& InputManager::getReader() { |
| 123 | return *mReader; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 126 | InputProcessorInterface& InputManager::getProcessor() { |
| 127 | return *mProcessor; |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 130 | InputDeviceMetricsCollectorInterface& InputManager::getMetricsCollector() { |
| 131 | return *mCollector; |
| 132 | } |
| 133 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 134 | InputDispatcherInterface& InputManager::getDispatcher() { |
| 135 | return *mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 138 | void InputManager::monitor() { |
| 139 | mReader->monitor(); |
| 140 | mBlocker->monitor(); |
Siarhei Vishniakou | 9899603 | 2022-08-03 11:54:47 -0700 | [diff] [blame] | 141 | mProcessor->monitor(); |
Siarhei Vishniakou | 9f330c5 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 142 | mDispatcher->monitor(); |
| 143 | } |
| 144 | |
Prabir Pradhan | 370dbc9 | 2023-04-06 00:02:05 +0000 | [diff] [blame] | 145 | void InputManager::dump(std::string& dump) { |
| 146 | mReader->dump(dump); |
| 147 | dump += '\n'; |
| 148 | mBlocker->dump(dump); |
| 149 | dump += '\n'; |
| 150 | mProcessor->dump(dump); |
| 151 | dump += '\n'; |
Prabir Pradhan | addf8e9 | 2023-04-06 00:28:48 +0000 | [diff] [blame] | 152 | if (ENABLE_INPUT_DEVICE_USAGE_METRICS) { |
| 153 | mCollector->dump(dump); |
| 154 | dump += '\n'; |
| 155 | } |
Prabir Pradhan | 370dbc9 | 2023-04-06 00:02:05 +0000 | [diff] [blame] | 156 | mDispatcher->dump(dump); |
| 157 | dump += '\n'; |
| 158 | } |
| 159 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 160 | // Used by tests only. |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 161 | binder::Status InputManager::createInputChannel(const std::string& name, InputChannel* outChannel) { |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 162 | IPCThreadState* ipc = IPCThreadState::self(); |
| 163 | const int uid = ipc->getCallingUid(); |
| 164 | if (uid != AID_SHELL && uid != AID_ROOT) { |
| 165 | ALOGE("Invalid attempt to register input channel over IPC" |
| 166 | "from non shell/root entity (PID: %d)", ipc->getCallingPid()); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 167 | return binder::Status::ok(); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 168 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 169 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 170 | base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name); |
Bernie Innocenti | 189c0f8 | 2020-12-22 19:45:18 +0900 | [diff] [blame] | 171 | if (!channel.ok()) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 172 | return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()), |
| 173 | channel.error().message().c_str()); |
| 174 | } |
| 175 | (*channel)->copyTo(*outChannel); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 176 | return binder::Status::ok(); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 179 | binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) { |
| 180 | mDispatcher->removeInputChannel(connectionToken); |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 181 | return binder::Status::ok(); |
| 182 | } |
| 183 | |
| 184 | status_t InputManager::dump(int fd, const Vector<String16>& args) { |
| 185 | std::string dump; |
| 186 | |
| 187 | dump += " InputFlinger dump\n"; |
| 188 | |
| 189 | ::write(fd, dump.c_str(), dump.size()); |
| 190 | return NO_ERROR; |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Vishnu Nair | e798b47 | 2020-07-23 13:52:21 -0700 | [diff] [blame] | 193 | binder::Status InputManager::setFocusedWindow(const FocusRequest& request) { |
| 194 | mDispatcher->setFocusedWindow(request); |
| 195 | return binder::Status::ok(); |
| 196 | } |
| 197 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 198 | } // namespace android |