blob: 1d7ea00e0cddb8fa1624955f71f6f61188cc8300 [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 BinderWindowHandle : public InputWindowHandle {
91public:
Robert Carr740167f2018-10-11 19:03:41 -070092 BinderWindowHandle(const InputWindowInfo& info) {
Robert Carr1cc78672018-07-31 14:25:57 -070093 mInfo = info;
94 }
95
96 bool updateInfo() override {
97 return true;
98 }
99};
100
101void InputManager::setInputWindows(const Vector<InputWindowInfo>& infos) {
102 std::unordered_map<int32_t, Vector<sp<InputWindowHandle>>> handlesPerDisplay;
103
104 Vector<sp<InputWindowHandle>> handles;
105 for (const auto& info : infos) {
106 handlesPerDisplay.emplace(info.displayId, Vector<sp<InputWindowHandle>>());
107 handlesPerDisplay[info.displayId].add(new BinderWindowHandle(info));
108 }
109 for (auto const& i : handlesPerDisplay) {
110 mDispatcher->setInputWindows(i.second, i.first);
111 }
112}
113
chaviwfbe5d9c2018-12-26 12:23:37 -0800114void InputManager::transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken) {
115 mDispatcher->transferTouchFocus(fromToken, toToken);
116}
117
Robert Carr1c4c5592018-09-24 13:18:43 -0700118// Used by tests only.
119void InputManager::registerInputChannel(const sp<InputChannel>& channel) {
120 IPCThreadState* ipc = IPCThreadState::self();
121 const int uid = ipc->getCallingUid();
122 if (uid != AID_SHELL && uid != AID_ROOT) {
123 ALOGE("Invalid attempt to register input channel over IPC"
124 "from non shell/root entity (PID: %d)", ipc->getCallingPid());
125 return;
126 }
127 mDispatcher->registerInputChannel(channel, false);
128}
129
130void InputManager::unregisterInputChannel(const sp<InputChannel>& channel) {
131 mDispatcher->unregisterInputChannel(channel);
132}
133
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134} // namespace android