blob: 9cb20526936a75ca4cbd1510783a1da948675c72 [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
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070058 virtual void dump(std::string& dump) override;
59 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080060
Prabir Pradhan28efc192019-11-05 01:10:04 +000061 virtual status_t start() override;
62 virtual status_t stop() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080063
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070064 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080065
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070066 virtual bool isInputDeviceEnabled(int32_t deviceId) override;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070067
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070068 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
69 int32_t scanCode) override;
70 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
71 int32_t keyCode) override;
72 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070074 virtual void toggleCapsLockState(int32_t deviceId) override;
Andrii Kulian763a3a42016-03-08 10:46:16 -080075
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070076 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070077 const int32_t* keyCodes, uint8_t* outFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080078
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070079 virtual void requestRefreshConfiguration(uint32_t changes) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080080
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000081 virtual void vibrate(int32_t deviceId, const std::vector<VibrationElement>& pattern,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070082 ssize_t repeat, int32_t token) override;
83 virtual void cancelVibrate(int32_t deviceId, int32_t token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080084
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070085 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) override;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070086
Michael Wrightd02c5b62014-02-10 15:10:22 -080087protected:
88 // These members are protected so they can be instrumented by test cases.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080089 virtual std::shared_ptr<InputDevice> createDeviceLocked(
90 int32_t deviceId, const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -080091
Prabir Pradhan28efc192019-11-05 01:10:04 +000092 // With each iteration of the loop, InputReader reads and processes one incoming message from
93 // the EventHub.
94 void loopOnce();
95
Michael Wrightd02c5b62014-02-10 15:10:22 -080096 class ContextImpl : public InputReaderContext {
97 InputReader* mReader;
Garfield Tanff1f1bb2020-01-28 13:24:04 -080098 IdGenerator mIdGenerator;
Michael Wrightd02c5b62014-02-10 15:10:22 -080099
100 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700101 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700103 virtual void updateGlobalMetaState() override;
104 virtual int32_t getGlobalMetaState() override;
105 virtual void disableVirtualKeysUntil(nsecs_t time) override;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800106 virtual bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700107 virtual void fadePointer() override;
Michael Wright17db18e2020-06-26 20:51:44 +0100108 virtual std::shared_ptr<PointerControllerInterface> getPointerController(
109 int32_t deviceId) override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700110 virtual void requestTimeoutAtTime(nsecs_t when) override;
111 virtual int32_t bumpGeneration() override;
112 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override;
113 virtual void dispatchExternalStylusState(const StylusState& outState) override;
114 virtual InputReaderPolicyInterface* getPolicy() override;
115 virtual InputListenerInterface* getListener() override;
116 virtual EventHubInterface* getEventHub() override;
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800117 virtual int32_t getNextId() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118 } mContext;
119
120 friend class ContextImpl;
121
122private:
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700123 std::unique_ptr<InputThread> mThread;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000124
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125 Mutex mLock;
126
127 Condition mReaderIsAliveCondition;
128
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700129 // This could be unique_ptr, but due to the way InputReader tests are written,
130 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test
131 // in parallel to passing it to the InputReader.
132 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800133 sp<InputReaderPolicyInterface> mPolicy;
134 sp<QueuedInputListener> mQueuedListener;
135
136 InputReaderConfiguration mConfig;
137
138 // The event queue.
139 static const int EVENT_BUFFER_SIZE = 256;
140 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
141
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800142 // An input device can represent a collection of EventHub devices. This map provides a way
143 // to lookup the input device instance from the EventHub device id.
144 std::unordered_map<int32_t /*eventHubId*/, std::shared_ptr<InputDevice>> mDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
146 // low-level input event decoding and device management
147 void processEventsLocked(const RawEvent* rawEvents, size_t count);
148
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800149 void addDeviceLocked(nsecs_t when, int32_t eventHubId);
150 void removeDeviceLocked(nsecs_t when, int32_t eventHubId);
151 void processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents, size_t count);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 void timeoutExpiredLocked(nsecs_t when);
153
154 void handleConfigurationChangedLocked(nsecs_t when);
155
156 int32_t mGlobalMetaState;
157 void updateGlobalMetaStateLocked();
158 int32_t getGlobalMetaStateLocked();
159
Michael Wright842500e2015-03-13 17:32:02 -0700160 void notifyExternalStylusPresenceChanged();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800161 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700162 void dispatchExternalStylusState(const StylusState& state);
163
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800164 // The PointerController that is shared among all the input devices that need it.
Michael Wright17db18e2020-06-26 20:51:44 +0100165 std::weak_ptr<PointerControllerInterface> mPointerController;
166 std::shared_ptr<PointerControllerInterface> getPointerControllerLocked(int32_t deviceId);
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800167 void updatePointerDisplayLocked();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 void fadePointerLocked();
169
170 int32_t mGeneration;
171 int32_t bumpGenerationLocked();
172
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800173 int32_t mNextInputDeviceId;
174 int32_t nextInputDeviceIdLocked();
175
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800176 void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177
178 nsecs_t mDisableVirtualKeysTimeout;
179 void disableVirtualKeysUntilLocked(nsecs_t time);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 bool shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181
182 nsecs_t mNextTimeout;
183 void requestTimeoutAtTimeLocked(nsecs_t when);
184
185 uint32_t mConfigurationChangesToRefresh;
186 void refreshConfigurationLocked(uint32_t changes);
187
188 // state queries
189 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
190 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700191 GetStateFunc getStateFunc);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700193 const int32_t* keyCodes, uint8_t* outFlags);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800194
195 // find an InputDevice from an InputDevice id
196 InputDevice* findInputDevice(int32_t deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800197};
198
Michael Wrightd02c5b62014-02-10 15:10:22 -0800199} // namespace android
200
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700201#endif // _UI_INPUTREADER_INPUT_READER_H