Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 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 Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame^] | 17 | #ifndef _UI_INPUTREADER_INPUT_READER_H |
| 18 | #define _UI_INPUTREADER_INPUT_READER_H |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 19 | |
| 20 | #include "EventHub.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | #include "InputListener.h" |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 22 | #include "InputReaderBase.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame^] | 23 | #include "InputReaderContext.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 24 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 25 | #include <utils/Condition.h> |
Atif Niyaz | 8384682 | 2019-07-18 15:17:40 -0700 | [diff] [blame] | 26 | #include <utils/KeyedVector.h> |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 27 | #include <utils/Mutex.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 28 | |
Siarhei Vishniakou | d634392 | 2018-07-06 23:33:37 +0100 | [diff] [blame] | 29 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 30 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 31 | namespace android { |
| 32 | |
| 33 | class InputDevice; |
| 34 | class InputMapper; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame^] | 35 | struct StylusState; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 36 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | /* 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 | */ |
| 48 | class InputReader : public InputReaderInterface { |
| 49 | public: |
Siarhei Vishniakou | 3bc7e09 | 2019-07-24 17:43:30 -0700 | [diff] [blame] | 50 | InputReader(std::shared_ptr<EventHubInterface> eventHub, |
| 51 | const sp<InputReaderPolicyInterface>& policy, |
| 52 | const sp<InputListenerInterface>& listener); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 53 | virtual ~InputReader(); |
| 54 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 55 | virtual void dump(std::string& dump); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 56 | virtual void monitor(); |
| 57 | |
| 58 | virtual void loopOnce(); |
| 59 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 60 | virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 61 | |
Siarhei Vishniakou | e54cb85 | 2017-03-21 17:48:16 -0700 | [diff] [blame] | 62 | virtual bool isInputDeviceEnabled(int32_t deviceId); |
| 63 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 64 | 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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 67 | |
Andrii Kulian | 763a3a4 | 2016-03-08 10:46:16 -0800 | [diff] [blame] | 68 | virtual void toggleCapsLockState(int32_t deviceId); |
| 69 | |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 70 | virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes, |
| 71 | const int32_t* keyCodes, uint8_t* outFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 72 | |
| 73 | virtual void requestRefreshConfiguration(uint32_t changes); |
| 74 | |
| 75 | virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 76 | ssize_t repeat, int32_t token); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 77 | virtual void cancelVibrate(int32_t deviceId, int32_t token); |
| 78 | |
Arthur Hung | c23540e | 2018-11-29 20:42:11 +0800 | [diff] [blame] | 79 | virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 80 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 81 | protected: |
| 82 | // These members are protected so they can be instrumented by test cases. |
| 83 | virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 84 | const InputDeviceIdentifier& identifier, |
| 85 | uint32_t classes); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 86 | |
| 87 | class ContextImpl : public InputReaderContext { |
| 88 | InputReader* mReader; |
| 89 | |
| 90 | public: |
Chih-Hung Hsieh | 6d2ede1 | 2016-09-01 11:28:23 -0700 | [diff] [blame] | 91 | explicit ContextImpl(InputReader* reader); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 92 | |
| 93 | virtual void updateGlobalMetaState(); |
| 94 | virtual int32_t getGlobalMetaState(); |
| 95 | virtual void disableVirtualKeysUntil(nsecs_t time); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 96 | virtual bool shouldDropVirtualKey(nsecs_t now, InputDevice* device, int32_t keyCode, |
| 97 | int32_t scanCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 98 | virtual void fadePointer(); |
| 99 | virtual void requestTimeoutAtTime(nsecs_t when); |
| 100 | virtual int32_t bumpGeneration(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 101 | virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 102 | virtual void dispatchExternalStylusState(const StylusState& outState); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 103 | virtual InputReaderPolicyInterface* getPolicy(); |
| 104 | virtual InputListenerInterface* getListener(); |
| 105 | virtual EventHubInterface* getEventHub(); |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 106 | virtual uint32_t getNextSequenceNum(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 107 | } mContext; |
| 108 | |
| 109 | friend class ContextImpl; |
| 110 | |
| 111 | private: |
| 112 | Mutex mLock; |
| 113 | |
| 114 | Condition mReaderIsAliveCondition; |
| 115 | |
Siarhei Vishniakou | 3bc7e09 | 2019-07-24 17:43:30 -0700 | [diff] [blame] | 116 | // 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 Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 120 | sp<InputReaderPolicyInterface> mPolicy; |
| 121 | sp<QueuedInputListener> mQueuedListener; |
| 122 | |
| 123 | InputReaderConfiguration mConfig; |
| 124 | |
Prabir Pradhan | 42611e0 | 2018-11-27 14:04:02 -0800 | [diff] [blame] | 125 | // used by InputReaderContext::getNextSequenceNum() as a counter for event sequence numbers |
| 126 | uint32_t mNextSequenceNum; |
| 127 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 128 | // 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 Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 148 | void notifyExternalStylusPresenceChanged(); |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 149 | void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices); |
Michael Wright | 842500e | 2015-03-13 17:32:02 -0700 | [diff] [blame] | 150 | void dispatchExternalStylusState(const StylusState& state); |
| 151 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 152 | void fadePointerLocked(); |
| 153 | |
| 154 | int32_t mGeneration; |
| 155 | int32_t bumpGenerationLocked(); |
| 156 | |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 157 | void getInputDevicesLocked(std::vector<InputDeviceInfo>& outInputDevices); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 158 | |
| 159 | nsecs_t mDisableVirtualKeysTimeout; |
| 160 | void disableVirtualKeysUntilLocked(nsecs_t time); |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 161 | bool shouldDropVirtualKeyLocked(nsecs_t now, InputDevice* device, int32_t keyCode, |
| 162 | int32_t scanCode); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 163 | |
| 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 Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 173 | GetStateFunc getStateFunc); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 174 | bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes, |
Prabir Pradhan | da7c00c | 2019-08-29 14:12:42 -0700 | [diff] [blame] | 175 | const int32_t* keyCodes, uint8_t* outFlags); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 178 | } // namespace android |
| 179 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame^] | 180 | #endif // _UI_INPUTREADER_INPUT_READER_H |