blob: 563018ad5ad8e906e27b88634da0e6696737bcf7 [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
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070017#ifndef _UI_INPUTREADER_INPUT_READER_H
18#define _UI_INPUTREADER_INPUT_READER_H
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Michael Wright17db18e2020-06-26 20:51:44 +010020#include <PointerControllerInterface.h>
21#include <utils/Condition.h>
22#include <utils/Mutex.h>
23
24#include <memory>
25#include <unordered_map>
26#include <vector>
27
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include "EventHub.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include "InputListener.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080030#include "InputReaderBase.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070031#include "InputReaderContext.h"
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070032#include "InputThread.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080033
Michael Wrightd02c5b62014-02-10 15:10:22 -080034namespace android {
35
36class InputDevice;
37class InputMapper;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070038struct StylusState;
Michael Wrightd02c5b62014-02-10 15:10:22 -080039
Michael Wrightd02c5b62014-02-10 15:10:22 -080040/* The input reader reads raw event data from the event hub and processes it into input events
41 * that it sends to the input listener. Some functions of the input reader, such as early
42 * event filtering in low power states, are controlled by a separate policy object.
43 *
Prabir Pradhan28efc192019-11-05 01:10:04 +000044 * The InputReader owns a collection of InputMappers. InputReader starts its own thread, where
45 * most of the work happens, but the InputReader can receive queries from other system
Michael Wrightd02c5b62014-02-10 15:10:22 -080046 * components running on arbitrary threads. To keep things manageable, the InputReader
47 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
48 * EventHub or the InputReaderPolicy but it is never held while calling into the
Prabir Pradhan28efc192019-11-05 01:10:04 +000049 * InputListener. All calls to InputListener must happen from InputReader's thread.
Michael Wrightd02c5b62014-02-10 15:10:22 -080050 */
51class InputReader : public InputReaderInterface {
52public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070053 InputReader(std::shared_ptr<EventHubInterface> eventHub,
54 const sp<InputReaderPolicyInterface>& policy,
55 const sp<InputListenerInterface>& listener);
Michael Wrightd02c5b62014-02-10 15:10:22 -080056 virtual ~InputReader();
57
arthurhungc903df12020-08-11 15:08:42 +080058 void dump(std::string& dump) override;
59 void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080060
arthurhungc903df12020-08-11 15:08:42 +080061 status_t start() override;
62 status_t stop() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080063
Chris Ye98d3f532020-10-01 21:48:59 -070064 std::vector<InputDeviceInfo> getInputDevices() const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080065
arthurhungc903df12020-08-11 15:08:42 +080066 bool isInputDeviceEnabled(int32_t deviceId) override;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070067
arthurhungc903df12020-08-11 15:08:42 +080068 int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) override;
69 int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) override;
70 int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080071
arthurhungc903df12020-08-11 15:08:42 +080072 void toggleCapsLockState(int32_t deviceId) override;
Andrii Kulian763a3a42016-03-08 10:46:16 -080073
arthurhungc903df12020-08-11 15:08:42 +080074 bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes,
75 uint8_t* outFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080076
arthurhungc903df12020-08-11 15:08:42 +080077 void requestRefreshConfiguration(uint32_t changes) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
arthurhungc903df12020-08-11 15:08:42 +080079 void vibrate(int32_t deviceId, const std::vector<VibrationElement>& pattern, ssize_t repeat,
80 int32_t token) override;
81 void cancelVibrate(int32_t deviceId, int32_t token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080082
arthurhungc903df12020-08-11 15:08:42 +080083 bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) override;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070084
Michael Wrightd02c5b62014-02-10 15:10:22 -080085protected:
86 // These members are protected so they can be instrumented by test cases.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080087 virtual std::shared_ptr<InputDevice> createDeviceLocked(
88 int32_t deviceId, const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -080089
Prabir Pradhan28efc192019-11-05 01:10:04 +000090 // With each iteration of the loop, InputReader reads and processes one incoming message from
91 // the EventHub.
92 void loopOnce();
93
Michael Wrightd02c5b62014-02-10 15:10:22 -080094 class ContextImpl : public InputReaderContext {
95 InputReader* mReader;
Garfield Tanff1f1bb2020-01-28 13:24:04 -080096 IdGenerator mIdGenerator;
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
98 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -070099 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100
arthurhungc903df12020-08-11 15:08:42 +0800101 void updateGlobalMetaState() override;
102 int32_t getGlobalMetaState() override;
103 void disableVirtualKeysUntil(nsecs_t time) override;
104 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override;
105 void fadePointer() override;
106 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override;
107 void requestTimeoutAtTime(nsecs_t when) override;
108 int32_t bumpGeneration() override;
109 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override;
110 void dispatchExternalStylusState(const StylusState& outState) override;
111 InputReaderPolicyInterface* getPolicy() override;
112 InputListenerInterface* getListener() override;
113 EventHubInterface* getEventHub() override;
114 int32_t getNextId() override;
115 void updateLedMetaState(int32_t metaState) override;
116 int32_t getLedMetaState() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117 } mContext;
118
119 friend class ContextImpl;
120
121private:
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700122 std::unique_ptr<InputThread> mThread;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000123
Chris Ye98d3f532020-10-01 21:48:59 -0700124 mutable Mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125
126 Condition mReaderIsAliveCondition;
127
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700128 // This could be unique_ptr, but due to the way InputReader tests are written,
129 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test
130 // in parallel to passing it to the InputReader.
131 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132 sp<InputReaderPolicyInterface> mPolicy;
133 sp<QueuedInputListener> mQueuedListener;
134
135 InputReaderConfiguration mConfig;
136
137 // The event queue.
138 static const int EVENT_BUFFER_SIZE = 256;
139 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
140
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800141 // An input device can represent a collection of EventHub devices. This map provides a way
142 // to lookup the input device instance from the EventHub device id.
143 std::unordered_map<int32_t /*eventHubId*/, std::shared_ptr<InputDevice>> mDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144
Chris Yee7310032020-09-22 15:36:28 -0700145 // An input device contains one or more eventHubId, this map provides a way to lookup the
146 // EventHubIds contained in the input device from the input device instance.
147 std::unordered_map<std::shared_ptr<InputDevice>, std::vector<int32_t> /*eventHubId*/>
148 mDeviceToEventHubIdsMap;
149
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 // low-level input event decoding and device management
151 void processEventsLocked(const RawEvent* rawEvents, size_t count);
152
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800153 void addDeviceLocked(nsecs_t when, int32_t eventHubId);
154 void removeDeviceLocked(nsecs_t when, int32_t eventHubId);
155 void processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents, size_t count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156 void timeoutExpiredLocked(nsecs_t when);
157
158 void handleConfigurationChangedLocked(nsecs_t when);
159
160 int32_t mGlobalMetaState;
161 void updateGlobalMetaStateLocked();
162 int32_t getGlobalMetaStateLocked();
163
arthurhungc903df12020-08-11 15:08:42 +0800164 int32_t mLedMetaState;
165 void updateLedMetaStateLocked(int32_t metaState);
166 int32_t getLedMetaStateLocked();
167
Michael Wright842500e2015-03-13 17:32:02 -0700168 void notifyExternalStylusPresenceChanged();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800169 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700170 void dispatchExternalStylusState(const StylusState& state);
171
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800172 // The PointerController that is shared among all the input devices that need it.
Michael Wright17db18e2020-06-26 20:51:44 +0100173 std::weak_ptr<PointerControllerInterface> mPointerController;
174 std::shared_ptr<PointerControllerInterface> getPointerControllerLocked(int32_t deviceId);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800175 void updatePointerDisplayLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176 void fadePointerLocked();
177
178 int32_t mGeneration;
179 int32_t bumpGenerationLocked();
180
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800181 int32_t mNextInputDeviceId;
182 int32_t nextInputDeviceIdLocked();
183
Chris Ye98d3f532020-10-01 21:48:59 -0700184 std::vector<InputDeviceInfo> getInputDevicesLocked() const;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186 nsecs_t mDisableVirtualKeysTimeout;
187 void disableVirtualKeysUntilLocked(nsecs_t time);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800188 bool shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189
190 nsecs_t mNextTimeout;
191 void requestTimeoutAtTimeLocked(nsecs_t when);
192
193 uint32_t mConfigurationChangesToRefresh;
194 void refreshConfigurationLocked(uint32_t changes);
195
196 // state queries
197 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
198 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700199 GetStateFunc getStateFunc);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700201 const int32_t* keyCodes, uint8_t* outFlags);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800202
203 // find an InputDevice from an InputDevice id
204 InputDevice* findInputDevice(int32_t deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205};
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207} // namespace android
208
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700209#endif // _UI_INPUTREADER_INPUT_READER_H