blob: 557eb3b7c49c54faaa2ff755f473f099cf4c22e7 [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"
Michael Wrightd02c5b62014-02-10 15:10:22 -080024
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070025#include <utils/Condition.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070026#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080027
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +000028#include <unordered_map>
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010029#include <vector>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
Michael Wrightd02c5b62014-02-10 15:10:22 -080031namespace android {
32
33class InputDevice;
34class InputMapper;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070035struct StylusState;
Michael Wrightd02c5b62014-02-10 15:10:22 -080036
Michael Wrightd02c5b62014-02-10 15:10:22 -080037/* The input reader reads raw event data from the event hub and processes it into input events
38 * that it sends to the input listener. Some functions of the input reader, such as early
39 * event filtering in low power states, are controlled by a separate policy object.
40 *
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +000041 * The InputReader owns a collection of InputMappers. Most of the work it does happens
42 * on the input reader thread but the InputReader can receive queries from other system
Michael Wrightd02c5b62014-02-10 15:10:22 -080043 * components running on arbitrary threads. To keep things manageable, the InputReader
44 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
45 * EventHub or the InputReaderPolicy but it is never held while calling into the
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +000046 * InputListener.
Michael Wrightd02c5b62014-02-10 15:10:22 -080047 */
48class InputReader : public InputReaderInterface {
49public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070050 InputReader(std::shared_ptr<EventHubInterface> eventHub,
51 const sp<InputReaderPolicyInterface>& policy,
52 const sp<InputListenerInterface>& listener);
Michael Wrightd02c5b62014-02-10 15:10:22 -080053 virtual ~InputReader();
54
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070055 virtual void dump(std::string& dump) override;
56 virtual void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080057
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +000058 virtual void loopOnce() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070060 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080061
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070062 virtual bool isInputDeviceEnabled(int32_t deviceId) override;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070063
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070064 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
65 int32_t scanCode) override;
66 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
67 int32_t keyCode) override;
68 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080069
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070070 virtual void toggleCapsLockState(int32_t deviceId) override;
Andrii Kulian763a3a42016-03-08 10:46:16 -080071
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070072 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070073 const int32_t* keyCodes, uint8_t* outFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070075 virtual void requestRefreshConfiguration(uint32_t changes) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080076
77 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070078 ssize_t repeat, int32_t token) override;
79 virtual void cancelVibrate(int32_t deviceId, int32_t token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080080
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070081 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) override;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070082
Michael Wrightd02c5b62014-02-10 15:10:22 -080083protected:
84 // These members are protected so they can be instrumented by test cases.
85 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070086 const InputDeviceIdentifier& identifier,
87 uint32_t classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -080088
89 class ContextImpl : public InputReaderContext {
90 InputReader* mReader;
91
92 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -070093 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -080094
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070095 virtual void updateGlobalMetaState() override;
96 virtual int32_t getGlobalMetaState() override;
97 virtual void disableVirtualKeysUntil(nsecs_t time) override;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070098 virtual bool shouldDropVirtualKey(nsecs_t now, InputDevice* device, int32_t keyCode,
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070099 int32_t scanCode) override;
100 virtual void fadePointer() override;
101 virtual void requestTimeoutAtTime(nsecs_t when) override;
102 virtual int32_t bumpGeneration() override;
103 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override;
104 virtual void dispatchExternalStylusState(const StylusState& outState) override;
105 virtual InputReaderPolicyInterface* getPolicy() override;
106 virtual InputListenerInterface* getListener() override;
107 virtual EventHubInterface* getEventHub() override;
108 virtual uint32_t getNextSequenceNum() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800109 } mContext;
110
111 friend class ContextImpl;
112
113private:
114 Mutex mLock;
115
116 Condition mReaderIsAliveCondition;
117
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700118 // This could be unique_ptr, but due to the way InputReader tests are written,
119 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test
120 // in parallel to passing it to the InputReader.
121 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800122 sp<InputReaderPolicyInterface> mPolicy;
123 sp<QueuedInputListener> mQueuedListener;
124
125 InputReaderConfiguration mConfig;
126
Prabir Pradhan42611e02018-11-27 14:04:02 -0800127 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
128 uint32_t mNextSequenceNum;
129
Michael Wrightd02c5b62014-02-10 15:10:22 -0800130 // The event queue.
131 static const int EVENT_BUFFER_SIZE = 256;
132 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
133
Nathaniel R. Lewis10793a62019-11-05 02:17:02 +0000134 std::unordered_map<int32_t /*deviceId*/, InputDevice*> mDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135
136 // low-level input event decoding and device management
137 void processEventsLocked(const RawEvent* rawEvents, size_t count);
138
139 void addDeviceLocked(nsecs_t when, int32_t deviceId);
140 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
141 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
142 void timeoutExpiredLocked(nsecs_t when);
143
144 void handleConfigurationChangedLocked(nsecs_t when);
145
146 int32_t mGlobalMetaState;
147 void updateGlobalMetaStateLocked();
148 int32_t getGlobalMetaStateLocked();
149
Michael Wright842500e2015-03-13 17:32:02 -0700150 void notifyExternalStylusPresenceChanged();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800151 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700152 void dispatchExternalStylusState(const StylusState& state);
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 void fadePointerLocked();
155
156 int32_t mGeneration;
157 int32_t bumpGenerationLocked();
158
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800159 void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160
161 nsecs_t mDisableVirtualKeysTimeout;
162 void disableVirtualKeysUntilLocked(nsecs_t time);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700163 bool shouldDropVirtualKeyLocked(nsecs_t now, InputDevice* device, int32_t keyCode,
164 int32_t scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
166 nsecs_t mNextTimeout;
167 void requestTimeoutAtTimeLocked(nsecs_t when);
168
169 uint32_t mConfigurationChangesToRefresh;
170 void refreshConfigurationLocked(uint32_t changes);
171
172 // state queries
173 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
174 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700175 GetStateFunc getStateFunc);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700177 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178};
179
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180} // namespace android
181
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700182#endif // _UI_INPUTREADER_INPUT_READER_H