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" |
| 22 | |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 23 | #include <log/log.h> |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 24 | #include <unordered_map> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | InputManager::InputManager( |
| 29 | const sp<EventHubInterface>& eventHub, |
| 30 | const sp<InputReaderPolicyInterface>& readerPolicy, |
| 31 | const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) { |
| 32 | mDispatcher = new InputDispatcher(dispatcherPolicy); |
| 33 | mReader = new InputReader(eventHub, readerPolicy, mDispatcher); |
| 34 | initialize(); |
| 35 | } |
| 36 | |
| 37 | InputManager::InputManager( |
| 38 | const sp<InputReaderInterface>& reader, |
| 39 | const sp<InputDispatcherInterface>& dispatcher) : |
| 40 | mReader(reader), |
| 41 | mDispatcher(dispatcher) { |
| 42 | initialize(); |
| 43 | } |
| 44 | |
| 45 | InputManager::~InputManager() { |
| 46 | stop(); |
| 47 | } |
| 48 | |
| 49 | void InputManager::initialize() { |
| 50 | mReaderThread = new InputReaderThread(mReader); |
| 51 | mDispatcherThread = new InputDispatcherThread(mDispatcher); |
| 52 | } |
| 53 | |
| 54 | status_t InputManager::start() { |
| 55 | status_t result = mDispatcherThread->run("InputDispatcher", PRIORITY_URGENT_DISPLAY); |
| 56 | if (result) { |
| 57 | ALOGE("Could not start InputDispatcher thread due to error %d.", result); |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | result = mReaderThread->run("InputReader", PRIORITY_URGENT_DISPLAY); |
| 62 | if (result) { |
| 63 | ALOGE("Could not start InputReader thread due to error %d.", result); |
| 64 | |
| 65 | mDispatcherThread->requestExit(); |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | return OK; |
| 70 | } |
| 71 | |
| 72 | status_t InputManager::stop() { |
| 73 | status_t result = mReaderThread->requestExitAndWait(); |
| 74 | if (result) { |
| 75 | ALOGW("Could not stop InputReader thread due to error %d.", result); |
| 76 | } |
| 77 | |
| 78 | result = mDispatcherThread->requestExitAndWait(); |
| 79 | if (result) { |
| 80 | ALOGW("Could not stop InputDispatcher thread due to error %d.", result); |
| 81 | } |
| 82 | |
| 83 | return OK; |
| 84 | } |
| 85 | |
| 86 | sp<InputReaderInterface> InputManager::getReader() { |
| 87 | return mReader; |
| 88 | } |
| 89 | |
| 90 | sp<InputDispatcherInterface> InputManager::getDispatcher() { |
| 91 | return mDispatcher; |
| 92 | } |
| 93 | |
Robert Carr | 1cc7867 | 2018-07-31 14:25:57 -0700 | [diff] [blame] | 94 | class BinderApplicationHandle : public InputApplicationHandle { |
| 95 | public: |
| 96 | BinderApplicationHandle() = default; |
| 97 | |
| 98 | bool updateInfo() override { |
| 99 | return true; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | class BinderWindowHandle : public InputWindowHandle { |
| 104 | public: |
| 105 | BinderWindowHandle(const InputWindowInfo& info) : |
| 106 | InputWindowHandle(new BinderApplicationHandle()) { |
| 107 | |
| 108 | mInfo = info; |
| 109 | } |
| 110 | |
| 111 | bool updateInfo() override { |
| 112 | return true; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | void InputManager::setInputWindows(const Vector<InputWindowInfo>& infos) { |
| 117 | std::unordered_map<int32_t, Vector<sp<InputWindowHandle>>> handlesPerDisplay; |
| 118 | |
| 119 | Vector<sp<InputWindowHandle>> handles; |
| 120 | for (const auto& info : infos) { |
| 121 | handlesPerDisplay.emplace(info.displayId, Vector<sp<InputWindowHandle>>()); |
| 122 | handlesPerDisplay[info.displayId].add(new BinderWindowHandle(info)); |
| 123 | } |
| 124 | for (auto const& i : handlesPerDisplay) { |
| 125 | mDispatcher->setInputWindows(i.second, i.first); |
| 126 | } |
| 127 | } |
| 128 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 129 | } // namespace android |