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