blob: 4288069659167b4d1ba03beadecddf7488bb3cfb [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>
Atif Niyaz83846822019-07-18 15:17:40 -070026#include <utils/KeyedVector.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070027#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080028
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 *
41 * 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
43 * 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
46 * InputListener.
47 */
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
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080055 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -080056 virtual void monitor();
57
58 virtual void loopOnce();
59
Arthur Hung7c3ae9c2019-03-11 11:23:03 +080060 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -080061
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070062 virtual bool isInputDeviceEnabled(int32_t deviceId);
63
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070064 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode);
65 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode);
66 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw);
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
Andrii Kulian763a3a42016-03-08 10:46:16 -080068 virtual void toggleCapsLockState(int32_t deviceId);
69
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070070 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
71 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
73 virtual void requestRefreshConfiguration(uint32_t changes);
74
75 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070076 ssize_t repeat, int32_t token);
Michael Wrightd02c5b62014-02-10 15:10:22 -080077 virtual void cancelVibrate(int32_t deviceId, int32_t token);
78
Arthur Hungc23540e2018-11-29 20:42:11 +080079 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070080
Michael Wrightd02c5b62014-02-10 15:10:22 -080081protected:
82 // These members are protected so they can be instrumented by test cases.
83 virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070084 const InputDeviceIdentifier& identifier,
85 uint32_t classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -080086
87 class ContextImpl : public InputReaderContext {
88 InputReader* mReader;
89
90 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -070091 explicit ContextImpl(InputReader* reader);
Michael Wrightd02c5b62014-02-10 15:10:22 -080092
93 virtual void updateGlobalMetaState();
94 virtual int32_t getGlobalMetaState();
95 virtual void disableVirtualKeysUntil(nsecs_t time);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070096 virtual bool shouldDropVirtualKey(nsecs_t now, InputDevice* device, int32_t keyCode,
97 int32_t scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -080098 virtual void fadePointer();
99 virtual void requestTimeoutAtTime(nsecs_t when);
100 virtual int32_t bumpGeneration();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800101 virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700102 virtual void dispatchExternalStylusState(const StylusState& outState);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103 virtual InputReaderPolicyInterface* getPolicy();
104 virtual InputListenerInterface* getListener();
105 virtual EventHubInterface* getEventHub();
Prabir Pradhan42611e02018-11-27 14:04:02 -0800106 virtual uint32_t getNextSequenceNum();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107 } mContext;
108
109 friend class ContextImpl;
110
111private:
112 Mutex mLock;
113
114 Condition mReaderIsAliveCondition;
115
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700116 // This could be unique_ptr, but due to the way InputReader tests are written,
117 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test
118 // in parallel to passing it to the InputReader.
119 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120 sp<InputReaderPolicyInterface> mPolicy;
121 sp<QueuedInputListener> mQueuedListener;
122
123 InputReaderConfiguration mConfig;
124
Prabir Pradhan42611e02018-11-27 14:04:02 -0800125 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
126 uint32_t mNextSequenceNum;
127
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 // The event queue.
129 static const int EVENT_BUFFER_SIZE = 256;
130 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
131
132 KeyedVector<int32_t, InputDevice*> mDevices;
133
134 // low-level input event decoding and device management
135 void processEventsLocked(const RawEvent* rawEvents, size_t count);
136
137 void addDeviceLocked(nsecs_t when, int32_t deviceId);
138 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
139 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
140 void timeoutExpiredLocked(nsecs_t when);
141
142 void handleConfigurationChangedLocked(nsecs_t when);
143
144 int32_t mGlobalMetaState;
145 void updateGlobalMetaStateLocked();
146 int32_t getGlobalMetaStateLocked();
147
Michael Wright842500e2015-03-13 17:32:02 -0700148 void notifyExternalStylusPresenceChanged();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800149 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700150 void dispatchExternalStylusState(const StylusState& state);
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152 void fadePointerLocked();
153
154 int32_t mGeneration;
155 int32_t bumpGenerationLocked();
156
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800157 void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158
159 nsecs_t mDisableVirtualKeysTimeout;
160 void disableVirtualKeysUntilLocked(nsecs_t time);
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700161 bool shouldDropVirtualKeyLocked(nsecs_t now, InputDevice* device, int32_t keyCode,
162 int32_t scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163
164 nsecs_t mNextTimeout;
165 void requestTimeoutAtTimeLocked(nsecs_t when);
166
167 uint32_t mConfigurationChangesToRefresh;
168 void refreshConfigurationLocked(uint32_t changes);
169
170 // state queries
171 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
172 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700173 GetStateFunc getStateFunc);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700175 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176};
177
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178} // namespace android
179
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700180#endif // _UI_INPUTREADER_INPUT_READER_H