blob: 388423ca2017f953a013424b4c07fce05b4072ca [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
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 Pradhan29c95332018-11-14 20:14:11 -080022#include "InputReaderFactory.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080023
Mark Salyzyn7823e122016-09-29 08:08:05 -070024#include <log/log.h>
Robert Carr1cc78672018-07-31 14:25:57 -070025#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080026
27namespace android {
28
29InputManager::InputManager(
Michael Wrightd02c5b62014-02-10 15:10:22 -080030 const sp<InputReaderPolicyInterface>& readerPolicy,
31 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
32 mDispatcher = new InputDispatcher(dispatcherPolicy);
Prabir Pradhan29c95332018-11-14 20:14:11 -080033 mReader = createInputReader(readerPolicy, mDispatcher);
Michael Wrightd02c5b62014-02-10 15:10:22 -080034 initialize();
35}
36
37InputManager::~InputManager() {
38 stop();
39}
40
41void InputManager::initialize() {
42 mReaderThread = new InputReaderThread(mReader);
43 mDispatcherThread = new InputDispatcherThread(mDispatcher);
44}
45
46status_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
64status_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
78sp<InputReaderInterface> InputManager::getReader() {
79 return mReader;
80}
81
82sp<InputDispatcherInterface> InputManager::getDispatcher() {
83 return mDispatcher;
84}
85
Robert Carr1cc78672018-07-31 14:25:57 -070086class BinderApplicationHandle : public InputApplicationHandle {
87public:
88 BinderApplicationHandle() = default;
89
90 bool updateInfo() override {
91 return true;
92 }
93};
94
95class BinderWindowHandle : public InputWindowHandle {
96public:
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
108void 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 Wrightd02c5b62014-02-10 15:10:22 -0800121} // namespace android