blob: e29c8f219c9022a01ac48e70bab4c9eaadc41544 [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 Pradhan186d5b52019-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 Pradhan186d5b52019-08-30 15:27:05 -070023#include "InputReaderContext.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080024
Prabir Pradhan2b281812019-08-29 14:12:42 -070025#include <utils/BitSet.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070026#include <utils/Condition.h>
Prabir Pradhan2b281812019-08-29 14:12:42 -070027#include <utils/KeyedVector.h>
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070028#include <utils/Mutex.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include <utils/Timers.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080030
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 Pradhan186d5b52019-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 *
43 * The InputReader owns a collection of InputMappers. Most of the work it does happens
44 * on the input reader thread but the InputReader can receive queries from other system
45 * 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
48 * InputListener.
49 */
50class InputReader : public InputReaderInterface {
51public:
Prabir Pradhan2b281812019-08-29 14:12:42 -070052 InputReader(const sp<EventHubInterface>& eventHub, const sp<InputReaderPolicyInterface>& policy,
53 const sp<InputListenerInterface>& listener);
Michael Wrightd02c5b62014-02-10 15:10:22 -080054 virtual ~InputReader();
55
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -080056 virtual void dump(std::string& dump);
Michael Wrightd02c5b62014-02-10 15:10:22 -080057 virtual void monitor();
58
59 virtual void loopOnce();
60
Arthur Hung7c3ae9c2019-03-11 11:23:03 +080061 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070063 virtual bool isInputDeviceEnabled(int32_t deviceId);
64
Prabir Pradhan2b281812019-08-29 14:12:42 -070065 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode);
66 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode);
67 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw);
Michael Wrightd02c5b62014-02-10 15:10:22 -080068
Andrii Kulian763a3a42016-03-08 10:46:16 -080069 virtual void toggleCapsLockState(int32_t deviceId);
70
Prabir Pradhan2b281812019-08-29 14:12:42 -070071 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
72 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
74 virtual void requestRefreshConfiguration(uint32_t changes);
75
76 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
Prabir Pradhan2b281812019-08-29 14:12:42 -070077 ssize_t repeat, int32_t token);
Michael Wrightd02c5b62014-02-10 15:10:22 -080078 virtual void cancelVibrate(int32_t deviceId, int32_t token);
79
Arthur Hungc23540e2018-11-29 20:42:11 +080080 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId);
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 Pradhan2b281812019-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 Pradhan2b281812019-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
116 sp<EventHubInterface> mEventHub;
117 sp<InputReaderPolicyInterface> mPolicy;
118 sp<QueuedInputListener> mQueuedListener;
119
120 InputReaderConfiguration mConfig;
121
Prabir Pradhan42611e02018-11-27 14:04:02 -0800122 // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers
123 uint32_t mNextSequenceNum;
124
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125 // The event queue.
126 static const int EVENT_BUFFER_SIZE = 256;
127 RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
128
129 KeyedVector<int32_t, InputDevice*> mDevices;
130
131 // low-level input event decoding and device management
132 void processEventsLocked(const RawEvent* rawEvents, size_t count);
133
134 void addDeviceLocked(nsecs_t when, int32_t deviceId);
135 void removeDeviceLocked(nsecs_t when, int32_t deviceId);
136 void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
137 void timeoutExpiredLocked(nsecs_t when);
138
139 void handleConfigurationChangedLocked(nsecs_t when);
140
141 int32_t mGlobalMetaState;
142 void updateGlobalMetaStateLocked();
143 int32_t getGlobalMetaStateLocked();
144
Michael Wright842500e2015-03-13 17:32:02 -0700145 void notifyExternalStylusPresenceChanged();
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800146 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices);
Michael Wright842500e2015-03-13 17:32:02 -0700147 void dispatchExternalStylusState(const StylusState& state);
148
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149 void fadePointerLocked();
150
151 int32_t mGeneration;
152 int32_t bumpGenerationLocked();
153
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800154 void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155
156 nsecs_t mDisableVirtualKeysTimeout;
157 void disableVirtualKeysUntilLocked(nsecs_t time);
Prabir Pradhan2b281812019-08-29 14:12:42 -0700158 bool shouldDropVirtualKeyLocked(nsecs_t now, InputDevice* device, int32_t keyCode,
159 int32_t scanCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160
161 nsecs_t mNextTimeout;
162 void requestTimeoutAtTimeLocked(nsecs_t when);
163
164 uint32_t mConfigurationChangesToRefresh;
165 void refreshConfigurationLocked(uint32_t changes);
166
167 // state queries
168 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
169 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Prabir Pradhan2b281812019-08-29 14:12:42 -0700170 GetStateFunc getStateFunc);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Prabir Pradhan2b281812019-08-29 14:12:42 -0700172 const int32_t* keyCodes, uint8_t* outFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173};
174
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175} // namespace android
176
Prabir Pradhan186d5b52019-08-30 15:27:05 -0700177#endif // _UI_INPUTREADER_INPUT_READER_H