blob: cf3ca42d93283f1aed738b0527c721d74c738301 [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
Robert Carr1c4c5592018-09-24 13:18:43 -070024#include <binder/IPCThreadState.h>
25
Mark Salyzyn7823e122016-09-29 08:08:05 -070026#include <log/log.h>
Robert Carr1cc78672018-07-31 14:25:57 -070027#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028
Robert Carr1c4c5592018-09-24 13:18:43 -070029#include <private/android_filesystem_config.h>
30
Michael Wrightd02c5b62014-02-10 15:10:22 -080031namespace android {
32
33InputManager::InputManager(
Michael Wrightd02c5b62014-02-10 15:10:22 -080034 const sp<InputReaderPolicyInterface>& readerPolicy,
35 const sp<InputDispatcherPolicyInterface>& dispatcherPolicy) {
36 mDispatcher = new InputDispatcher(dispatcherPolicy);
Prabir Pradhan29c95332018-11-14 20:14:11 -080037 mReader = createInputReader(readerPolicy, mDispatcher);
Michael Wrightd02c5b62014-02-10 15:10:22 -080038 initialize();
39}
40
41InputManager::~InputManager() {
42 stop();
43}
44
45void InputManager::initialize() {
46 mReaderThread = new InputReaderThread(mReader);
47 mDispatcherThread = new InputDispatcherThread(mDispatcher);
48}
49
50status_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
68status_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
82sp<InputReaderInterface> InputManager::getReader() {
83 return mReader;
84}
85
86sp<InputDispatcherInterface> InputManager::getDispatcher() {
87 return mDispatcher;
88}
89
Robert Carr1cc78672018-07-31 14:25:57 -070090class BinderApplicationHandle : public InputApplicationHandle {
91public:
92 BinderApplicationHandle() = default;
93
94 bool updateInfo() override {
95 return true;
96 }
97};
98
99class BinderWindowHandle : public InputWindowHandle {
100public:
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
112void 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 Carr1c4c5592018-09-24 13:18:43 -0700125// Used by tests only.
126void 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
137void InputManager::unregisterInputChannel(const sp<InputChannel>& channel) {
138 mDispatcher->unregisterInputChannel(channel);
139}
140
Michael Wrightd02c5b62014-02-10 15:10:22 -0800141} // namespace android