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" |
| 23 | #include "InputDispatcherThread.h" |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 24 | #include "InputReaderFactory.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 25 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 26 | #include <binder/IPCThreadState.h> |
| 27 | |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 30 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 31 | #include <private/android_filesystem_config.h> |
| 32 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| 35 | InputManager::InputManager( |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 36 | const sp<InputReaderPolicyInterface>& readerPolicy, |
| 37 | const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) { |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 38 | mDispatcher = createInputDispatcher(dispatcherPolicy); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 39 | mClassifier = new InputClassifier(mDispatcher); |
| 40 | mReader = createInputReader(readerPolicy, mClassifier); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 41 | initialize(); |
| 42 | } |
| 43 | |
| 44 | InputManager::~InputManager() { |
| 45 | stop(); |
| 46 | } |
| 47 | |
| 48 | void InputManager::initialize() { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 49 | mDispatcherThread = new InputDispatcherThread(mDispatcher); |
| 50 | } |
| 51 | |
| 52 | status_t InputManager::start() { |
| 53 | status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY); |
| 54 | if (result) { |
| 55 | ALOGE("Could not start InputDispatcher thread due to error %d.", result); |
| 56 | return result; |
| 57 | } |
| 58 | |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame^] | 59 | result = mReader->start(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 60 | if (result) { |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame^] | 61 | ALOGE("Could not start InputReader due to error %d.", result); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 62 | |
| 63 | mDispatcherThread->requestExit(); |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | return OK; |
| 68 | } |
| 69 | |
| 70 | status_t InputManager::stop() { |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame^] | 71 | status_t result = mReader->stop(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 72 | if (result) { |
Prabir Pradhan | 28efc19 | 2019-11-05 01:10:04 +0000 | [diff] [blame^] | 73 | ALOGW("Could not stop InputReader due to error %d.", result); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | result = mDispatcherThread->requestExitAndWait(); |
| 77 | if (result) { |
| 78 | ALOGW("Could not stop InputDispatcher thread due to error %d.", result); |
| 79 | } |
| 80 | |
| 81 | return OK; |
| 82 | } |
| 83 | |
| 84 | sp<InputReaderInterface> InputManager::getReader() { |
| 85 | return mReader; |
| 86 | } |
| 87 | |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 88 | sp<InputClassifierInterface> InputManager::getClassifier() { |
| 89 | return mClassifier; |
| 90 | } |
| 91 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | sp<InputDispatcherInterface> InputManager::getDispatcher() { |
| 93 | return mDispatcher; |
| 94 | } |
| 95 | |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 96 | class BinderWindowHandle : public InputWindowHandle { |
| 97 | public: |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 98 | BinderWindowHandle(const InputWindowInfo& info) { |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 99 | mInfo = info; |
| 100 | } |
| 101 | |
| 102 | bool updateInfo() override { |
| 103 | return true; |
| 104 | } |
| 105 | }; |
| 106 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 107 | void InputManager::setInputWindows(const std::vector<InputWindowInfo>& infos, |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 108 | const sp<ISetInputWindowsListener>& setInputWindowsListener) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 109 | std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> handlesPerDisplay; |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 110 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 111 | std::vector<sp<InputWindowHandle>> handles; |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 112 | for (const auto& info : infos) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 113 | handlesPerDisplay.emplace(info.displayId, std::vector<sp<InputWindowHandle>>()); |
| 114 | handlesPerDisplay[info.displayId].push_back(new BinderWindowHandle(info)); |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 115 | } |
| 116 | for (auto const& i : handlesPerDisplay) { |
chaviw | 291d88a | 2019-02-14 10:33:58 -0800 | [diff] [blame] | 117 | mDispatcher->setInputWindows(i.second, i.first, setInputWindowsListener); |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 121 | // Used by tests only. |
| 122 | void InputManager::registerInputChannel(const sp<InputChannel>& channel) { |
| 123 | IPCThreadState* ipc = IPCThreadState::self(); |
| 124 | const int uid = ipc->getCallingUid(); |
| 125 | if (uid != AID_SHELL && uid != AID_ROOT) { |
| 126 | ALOGE("Invalid attempt to register input channel over IPC" |
| 127 | "from non shell/root entity (PID: %d)", ipc->getCallingPid()); |
| 128 | return; |
| 129 | } |
Siarhei Vishniakou | 7c34b23 | 2019-10-11 19:08:48 -0700 | [diff] [blame] | 130 | mDispatcher->registerInputChannel(channel); |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void InputManager::unregisterInputChannel(const sp<InputChannel>& channel) { |
| 134 | mDispatcher->unregisterInputChannel(channel); |
| 135 | } |
| 136 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 137 | } // namespace android |