blob: 40ca6a7d6d6acfe92a269fecd81175b93696f31b [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"
22
Mark Salyzyn7823e122016-09-29 08:08:05 -070023#include <log/log.h>
Robert Carr1cc78672018-07-31 14:25:57 -070024#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080025
26namespace android {
27
28InputManager::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
37InputManager::InputManager(
38 const sp<InputReaderInterface>& reader,
39 const sp<InputDispatcherInterface>& dispatcher) :
40 mReader(reader),
41 mDispatcher(dispatcher) {
42 initialize();
43}
44
45InputManager::~InputManager() {
46 stop();
47}
48
49void InputManager::initialize() {
50 mReaderThread = new InputReaderThread(mReader);
51 mDispatcherThread = new InputDispatcherThread(mDispatcher);
52}
53
54status_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
72status_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
86sp<InputReaderInterface> InputManager::getReader() {
87 return mReader;
88}
89
90sp<InputDispatcherInterface> InputManager::getDispatcher() {
91 return mDispatcher;
92}
93
Robert Carr1cc78672018-07-31 14:25:57 -070094class BinderApplicationHandle : public InputApplicationHandle {
95public:
96 BinderApplicationHandle() = default;
97
98 bool updateInfo() override {
99 return true;
100 }
101};
102
103class BinderWindowHandle : public InputWindowHandle {
104public:
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
116void 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 Wrightd02c5b62014-02-10 15:10:22 -0800129} // namespace android