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