blob: ca1a081e482c24259c27c58026a879fb053db537 [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
20#include "EventHub.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080021#include "InputListener.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080022#include "InputReaderBase.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070023#include "InputReaderContext.h"
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070024#include "InputThread.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080025
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -080026#include <PointerControllerInterface.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070027#include <utils/Condition.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070028#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +000030#include <unordered_map>
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010031#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080032
Michael Wrightd02c5b62014-02-10 15:10:22 -080033namespace android {
34
35class InputDevice;
36class InputMapper;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070037struct StylusState;
Michael Wrightd02c5b62014-02-10 15:10:22 -080038
Michael Wrightd02c5b62014-02-10 15:10:22 -080039/* The input reader reads raw event data from the event hub and processes it into input events
40 * that it sends to the input listener. Some functions of the input reader, such as early
41 * event filtering in low power states, are controlled by a separate policy object.
42 *
Prabir Pradhan28efc192019-11-05 01:10:04 +000043 * The InputReader owns a collection of InputMappers. InputReader starts its own thread, where
44 * most of the work happens, but the InputReader can receive queries from other system
Michael Wrightd02c5b62014-02-10 15:10:22 -080045 * components running on arbitrary threads. To keep things manageable, the InputReader
46 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
47 * EventHub or the InputReaderPolicy but it is never held while calling into the
Prabir Pradhan28efc192019-11-05 01:10:04 +000048 * InputListener. All calls to InputListener must happen from InputReader's thread.
Michael Wrightd02c5b62014-02-10 15:10:22 -080049 */
50class InputReader : public InputReaderInterface {
51public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070052 InputReader(std::shared_ptr<EventHubInterface> eventHub,
53 const sp<InputReaderPolicyInterface>& policy,
54 const sp<InputListenerInterface>& listener);
Michael Wrightd02c5b62014-02-10 15:10:22 -080055 virtual ~InputReader();
56
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070057 virtual void dump(std::string& dump) override;
58 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
Prabir Pradhan28efc192019-11-05 01:10:04 +000060 virtual status_t start() override;
61 virtual status_t stop() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070063 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080064
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070065 virtual bool isInputDeviceEnabled(int32_t deviceId) override;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070066
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070067 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
68 int32_t scanCode) override;
69 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
70 int32_t keyCode) override;
71 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070073 virtual void toggleCapsLockState(int32_t deviceId) override;
Andrii Kulian763a3a42016-03-08 10:46:16 -080074
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070075 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070076 const int32_t* keyCodes, uint8_t* outFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080077
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070078 virtual void requestRefreshConfiguration(uint32_t changes) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080079
80 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070081 ssize_t repeat, int32_t token) override;
82 virtual void cancelVibrate(int32_t deviceId, int32_t token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080083
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070084 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) override;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070085
Michael Wrightd02c5b62014-02-10 15:10:22 -080086protected:
87 // These members are protected so they can be instrumented by test cases.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080088 virtual std::shared_ptr<InputDevice> createDeviceLocked(
89 int32_t deviceId, const InputDeviceIdentifier& identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -080090
Prabir Pradhan28efc192019-11-05 01:10:04 +000091 // With each iteration of the loop, InputReader reads and processes one incoming message from
92 // the EventHub.
93 void loopOnce();
94
Michael Wrightd02c5b62014-02-10 15:10:22 -080095 class ContextImpl : public InputReaderContext {
96 InputReader* mReader;
97
98 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -070099 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700101 virtual void updateGlobalMetaState() override;
102 virtual int32_t getGlobalMetaState() override;
103 virtual void disableVirtualKeysUntil(nsecs_t time) override;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800104 virtual bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700105 virtual void fadePointer() override;
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800106 virtual sp<PointerControllerInterface> getPointerController(int32_t deviceId) override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -0700107 virtual void requestTimeoutAtTime(nsecs_t when) override;
108 virtual int32_t bumpGeneration() override;
109 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override;
110 virtual void dispatchExternalStylusState(const StylusState& outState) override;
111 virtual InputReaderPolicyInterface* getPolicy() override;
112 virtual InputListenerInterface* getListener() override;
113 virtual EventHubInterface* getEventHub() override;
114 virtual uint32_t getNextSequenceNum() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115 } mContext;
116
117 friend class ContextImpl;
118
119private:
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700120 std::unique_ptr<InputThread> mThread;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000121
Michael Wrightd02c5b62014-02-10 15:10:22 -0800122 Mutex mLock;
123
124 Condition mReaderIsAliveCondition;
125
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700126 // This could be unique_ptr, but due to the way InputReader tests are written,
127 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test
128 // in parallel to passing it to the InputReader.
129 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800130 sp<InputReaderPolicyInterface> mPolicy;
131 sp<QueuedInputListener> mQueuedListener;
132
133 InputReaderConfiguration mConfig;
134
Prabir Pradhan42611e02018-11-27 14:04:02 -0800135 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
136 uint32_t mNextSequenceNum;
137
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138 // 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.
165 wp<PointerControllerInterface> mPointerController;
166 sp<PointerControllerInterface> getPointerControllerLocked(int32_t deviceId);
167 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