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); |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 37 | mReader = createInputReader(readerPolicy, mDispatcher); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 38 | initialize(); |
| 39 | } |
| 40 | |
| 41 | InputManager::~InputManager() { |
| 42 | stop(); |
| 43 | } |
| 44 | |
| 45 | void InputManager::initialize() { |
| 46 | mReaderThread = new InputReaderThread(mReader); |
| 47 | mDispatcherThread = new InputDispatcherThread(mDispatcher); |
| 48 | } |
| 49 | |
| 50 | status_t InputManager::start() { |
| 51 | status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY); |
| 52 | if (result) { |
| 53 | ALOGE("Could not start InputDispatcher thread due to error %d.", result); |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY); |
| 58 | if (result) { |
| 59 | ALOGE("Could not start InputReader thread due to error %d.", result); |
| 60 | |
| 61 | mDispatcherThread->requestExit(); |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | return OK; |
| 66 | } |
| 67 | |
| 68 | status_t InputManager::stop() { |
| 69 | status_t result = mReaderThread->requestExitAndWait(); |
| 70 | if (result) { |
| 71 | ALOGW("Could not stop InputReader thread due to error %d.", result); |
| 72 | } |
| 73 | |
| 74 | result = mDispatcherThread->requestExitAndWait(); |
| 75 | if (result) { |
| 76 | ALOGW("Could not stop InputDispatcher thread due to error %d.", result); |
| 77 | } |
| 78 | |
| 79 | return OK; |
| 80 | } |
| 81 | |
| 82 | sp<InputReaderInterface> InputManager::getReader() { |
| 83 | return mReader; |
| 84 | } |
| 85 | |
| 86 | sp<InputDispatcherInterface> InputManager::getDispatcher() { |
| 87 | return mDispatcher; |
| 88 | } |
| 89 | |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 90 | class BinderApplicationHandle : public InputApplicationHandle { |
| 91 | public: |
| 92 | BinderApplicationHandle() = default; |
| 93 | |
| 94 | bool updateInfo() override { |
| 95 | return true; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | class BinderWindowHandle : public InputWindowHandle { |
| 100 | public: |
| 101 | BinderWindowHandle(const InputWindowInfo& info) : |
| 102 | InputWindowHandle(new BinderApplicationHandle()) { |
| 103 | |
| 104 | mInfo = info; |
| 105 | } |
| 106 | |
| 107 | bool updateInfo() override { |
| 108 | return true; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | void InputManager::setInputWindows(const Vector<InputWindowInfo>& infos) { |
| 113 | std::unordered_map<int32_t, Vector<sp<InputWindowHandle>>> handlesPerDisplay; |
| 114 | |
| 115 | Vector<sp<InputWindowHandle>> handles; |
| 116 | for (const auto& info : infos) { |
| 117 | handlesPerDisplay.emplace(info.displayId, Vector<sp<InputWindowHandle>>()); |
| 118 | handlesPerDisplay[info.displayId].add(new BinderWindowHandle(info)); |
| 119 | } |
| 120 | for (auto const& i : handlesPerDisplay) { |
| 121 | mDispatcher->setInputWindows(i.second, i.first); |
| 122 | } |
| 123 | } |
| 124 | |
Robert Carr | 1c4c559 | 2018-09-24 13:18:43 -0700 | [diff] [blame^] | 125 | // Used by tests only. |
| 126 | void InputManager::registerInputChannel(const sp<InputChannel>& channel) { |
| 127 | IPCThreadState* ipc = IPCThreadState::self(); |
| 128 | const int uid = ipc->getCallingUid(); |
| 129 | if (uid != AID_SHELL && uid != AID_ROOT) { |
| 130 | ALOGE("Invalid attempt to register input channel over IPC" |
| 131 | "from non shell/root entity (PID: %d)", ipc->getCallingPid()); |
| 132 | return; |
| 133 | } |
| 134 | mDispatcher->registerInputChannel(channel, false); |
| 135 | } |
| 136 | |
| 137 | void InputManager::unregisterInputChannel(const sp<InputChannel>& channel) { |
| 138 | mDispatcher->unregisterInputChannel(channel); |
| 139 | } |
| 140 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 141 | } // namespace android |